OSDN Git Service

[LoopUnrollAnalyzer] Take into account cost of instructions controlling branches...
authorMichael Zolotukhin <mzolotukhin@apple.com>
Wed, 18 May 2016 21:20:12 +0000 (21:20 +0000)
committerMichael Zolotukhin <mzolotukhin@apple.com>
Wed, 18 May 2016 21:20:12 +0000 (21:20 +0000)
Previously, we didn't add their and their operands cost, which could've
resulted in unrolling loops for no actual benefit.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269985 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Scalar/LoopUnrollPass.cpp
test/Transforms/LoopUnroll/full-unroll-heuristics-2.ll

index 034cb04..d167c44 100644 (file)
@@ -506,6 +506,7 @@ analyzeLoopUnrollCost(const Loop *L, unsigned TripCount, DominatorTree &DT,
           BBWorklist.insert(Succ);
         else
           ExitWorklist.insert({BB, Succ});
+      AddCostRecursively(*TI, Iteration);
     }
 
     // If we found no optimization opportunities on the first iteration, we
index f62141d..11c9f96 100644 (file)
@@ -55,3 +55,35 @@ loop.end:                                            ; preds = %loop
   %r.lcssa = phi i32 [ %r, %loop ]
   ret i32 %r.lcssa
 }
+
+; In this case the loaded value is used only to control branch.
+; If we missed that, we could've thought that it's unused and unrolling would
+; clean up almost entire loop. Make sure that we do not unroll such loop.
+; CHECK-LABEL: @foo3
+; CHECK: br i1 %exitcond, label %loop.end, label %loop.header
+define i32 @foo3(i32* noalias nocapture readonly %src) {
+entry:
+  br label %loop.header
+
+loop.header:
+  %iv = phi i64 [ 0, %entry ], [ %inc, %loop.latch ]
+  %r1  = phi i32 [ 0, %entry ], [ %r3, %loop.latch ]
+  %arrayidx = getelementptr inbounds i32, i32* %src, i64 %iv
+  %src_element = load i32, i32* %arrayidx, align 4
+  %cmp = icmp eq i32 0, %src_element
+  br i1 %cmp, label %loop.if, label %loop.latch
+
+loop.if:
+  %r2 = add i32 %r1, 1
+  br label %loop.latch
+
+loop.latch:
+  %r3 = phi i32 [%r1, %loop.header], [%r2, %loop.if]
+  %inc = add nuw nsw i64 %iv, 1
+  %exitcond = icmp eq i64 %inc, 9
+  br i1 %exitcond, label %loop.end, label %loop.header
+
+loop.end:
+  %r.lcssa = phi i32 [ %r3, %loop.latch ]
+  ret i32 %r.lcssa
+}