Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Missed induction variable optimization #30530

Open Quuxplusone opened 7 years ago

Quuxplusone commented 7 years ago
Bugzilla Link PR31557
Status NEW
Importance P normal
Reported by Carrot (carrot@google.com)
Reported on 2017-01-05 17:41:28 -0800
Last modified on 2017-01-13 00:02:20 -0800
Version trunk
Hardware PC Linux
CC ditaliano@apple.com, hfinkel@anl.gov, llvm-bugs@lists.llvm.org, sanjoy@playingwithpointers.com
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
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
Quuxplusone commented 7 years ago
add      30, 30, 4              // *

should be

        add      30, 30, 3              // *