https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90270
Bug ID: 90270 Summary: Do not select best induction variable optimization Product: gcc Version: 8.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: rjiejie at me dot com Target Milestone: --- Using built-in specs. COLLECT_GCC=/home/jojo/work/csky/cskytoolchain/csky-toolchain-build-riscv/riscv-install/bin/riscv64-unknown-linux-gnu-gcc COLLECT_LTO_WRAPPER=/home/jojo/work/csky/cskytoolchain/csky-toolchain-build-riscv/riscv-install/libexec/gcc/riscv64-unknown-linux-gnu/8.1.0/lto-wrapper Target: riscv64-unknown-linux-gnu Configured with: /home/jojo/work/csky/cskytoolchain/csky-toolchain-build-riscv/riscv-gcc/configure --target=riscv64-unknown-linux-gnu --prefix=/home/jojo/work/csky/cskytoolchain/csky-toolchain-build-riscv/riscv-install --with-sysroot=/home/jojo/work/csky/cskytoolchain/csky-toolchain-build-riscv/riscv-install/sysroot --with-system-zlib --enable-shared --enable-tls --enable-languages=c,c++ --disable-libmudflap --disable-libssp --disable-libquadmath --disable-nls --disable-bootstrap --src=.././riscv-gcc --enable-checking=yes --with-pkgversion= --disable-multilib --with-abi=lp64 --with-arch=rv64imac 'CFLAGS_FOR_TARGET=-O2 -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-O2 -mcmodel=medlow' CFLAGS='-O0 -g' CXXFLAGS='-O0 -g' Thread model: posix gcc version 8.1.0 () The following case do not select the best iv vars: extern unsigned short int crcu32(unsigned int newval, unsigned short int crc); unsigned short int func(unsigned short int crc) { unsigned int final_counts[8]; unsigned int track_counts[8]; unsigned int i; for (i=0; i< 8; i++) { crc=crcu32(final_counts[i],crc); crc=crcu32(track_counts[i],crc); } return crc; } the asm code: .L2: slli s0,s1,2 add a5,sp,s0 lw a0,-4(a5) addi s1,s1,1 call crcu32 addi a5,sp,32 add s0,a5,s0 mv a1,a0 lw a0,-4(s0) call crcu32 mv a1,a0 bne s1,s2,.L2 i debug and found some info from "ivopts" tree optimization, the bellow additional code will adjust cost of some type address in file tree-ssa-loop-ivopts.c: /* Cost of small invariant expression adjusted against loop niters is usually zero, which makes it difficult to be differentiated from candidate based on loop invariant variables. Secondly, the generated invariant expression may not be hoisted out of loop by following pass. We penalize the cost by rounding up in order to neutralize such effects. */ cost.cost = adjust_setup_cost (data, cost.cost, true); cost.scratch = cost.cost; when i remove the two lines, the created asm code will better: .L2: lw a0,0(s0) addi s0,s0,4 addi s1,s1,4 call crcu32 mv a1,a0 lw a0,-4(s1) call crcu32 mv a1,a0 bne s0,s2,.L2