llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28k stars 11.56k forks source link

Missed induction variable optimization #30905

Open weiguozhi opened 7 years ago

weiguozhi commented 7 years ago
Bugzilla Link 31557
Version trunk
OS Linux
CC @hfinkel,@sanjoy

Extended Description

Compile the following code with options

--target=powerpc64le-grtev4-linux-gnu -m64 -O2 -mvsx -mcpu=power8

extern void bar(int);

void foo(const int* p, int n) { for (int j = 0; j < n; ++j) { int k = p[j]; bar(k); } }

I got:

    clrldi   30, 4, 32
    addi 4, 3, -4
    .p2align        5

.LBB0_2: # %for.body

=>This Inner Loop Header: Depth=1

    lwa 3, 4(4)
    addi 29, 4, 4
    bl _Z3bari
    nop
    addi 30, 30, -1                
    mr 4, 29
    cmpldi   30, 0
    bne      0, .LBB0_2

Var j is an induction variable, it is only used to index an array, so it can be optimized away, like following

    clrldi   30, 4, 32
    sldi     30, 30, 2              // *
    add      30, 30, 4              // *
    addi 4, 3, -4
    .p2align        5

.LBB0_2: # %for.body

=>This Inner Loop Header: Depth=1

    lwa 3, 4(4)
    addi 29, 4, 4
    bl _Z3bari
    nop

// addi 30, 30, -1
mr 4, 29 cmpld 30, 29 // * bne 0, .LBB0_2

weiguozhi commented 7 years ago
    add      30, 30, 4              // *

should be

    add      30, 30, 3              // *