[llvm] r177432 - IndVarSimplify: do not recompute an IV value outside of the loop if :
Arnaud A. de Grandmaison
arnaud.adegm at gmail.com
Tue Mar 19 13:00:22 PDT 2013
Author: aadg
Date: Tue Mar 19 15:00:22 2013
New Revision: 177432
URL: http://llvm.org/viewvc/llvm-project?rev=177432&view=rev
Log:
IndVarSimplify: do not recompute an IV value outside of the loop if :
- it is trivially known to be used inside the loop in a way that can not be optimized away
- there is no use outside of the loop which can take advantage of the computation hoisting
Added:
llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=177432&r1=177431&r2=177432&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp Tue Mar 19 15:00:22 2013
@@ -535,6 +535,45 @@ void IndVarSimplify::RewriteLoopExitValu
if (!SE->isLoopInvariant(ExitValue, L))
continue;
+ // Computing the value outside of the loop brings no benefit if :
+ // - it is definitely used inside the loop in a way which can not be
+ // optimized away.
+ // - no use outside of the loop can take advantage of hoisting the
+ // computation out of the loop
+ if (ExitValue->getSCEVType()>=scMulExpr) {
+ unsigned NumHardInternalUses = 0;
+ unsigned NumSoftExternalUses = 0;
+ unsigned NumUses = 0;
+ for (Value::use_iterator IB=Inst->use_begin(), IE=Inst->use_end();
+ IB!=IE && NumUses<=6 ; ++IB) {
+ Instruction *UseInstr = cast<Instruction>(*IB);
+ unsigned Opc = UseInstr->getOpcode();
+ NumUses++;
+ if (L->contains(UseInstr)) {
+ if (Opc == Instruction::Call || Opc == Instruction::Ret)
+ NumHardInternalUses++;
+ } else {
+ if (Opc == Instruction::PHI) {
+ // Do not count the Phi as a use. LCSSA may have inserted
+ // plenty of trivial ones.
+ NumUses--;
+ for (Value::use_iterator PB=UseInstr->use_begin(),
+ PE=UseInstr->use_end();
+ PB!=PE && NumUses<=6 ; ++PB, ++NumUses) {
+ unsigned PhiOpc = cast<Instruction>(*PB)->getOpcode();
+ if (PhiOpc != Instruction::Call && PhiOpc != Instruction::Ret)
+ NumSoftExternalUses++;
+ }
+ continue;
+ }
+ if (Opc != Instruction::Call && Opc != Instruction::Ret)
+ NumSoftExternalUses++;
+ }
+ }
+ if (NumUses <= 6 && NumHardInternalUses && !NumSoftExternalUses)
+ continue;
+ }
+
Value *ExitVal = Rewriter.expandCodeFor(ExitValue, PN->getType(), Inst);
DEBUG(dbgs() << "INDVARS: RLEV: AfterLoopVal = " << *ExitVal << '\n'
Added: llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll?rev=177432&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll (added)
+++ llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll Tue Mar 19 15:00:22 2013
@@ -0,0 +1,69 @@
+; RUN: opt < %s -indvars -S | FileCheck %s
+
+; This tests that the IV is not recomputed outside of the loop when it is known
+; to be computed by the loop and used in the loop any way. In the example below
+; although a's value can be computed outside of the loop, there is no benefit
+; in doing so as it has to be computed by the loop anyway.
+;
+; extern void func(unsigned val);
+;
+; void test(unsigned m)
+; {
+; unsigned a = 0;
+;
+; for (int i=0; i<186; i++) {
+; a += m;
+; func(a);
+; }
+;
+; func(a);
+; }
+
+declare void @func(i32)
+
+; CHECK: @test
+define void @test(i32 %m) nounwind uwtable {
+entry:
+ br label %for.body
+
+for.body: ; preds = %for.body, %entry
+ %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %add = add i32 %a.05, %m
+; CHECK: tail call void @func(i32 %add)
+ tail call void @func(i32 %add)
+ %inc = add nsw i32 %i.06, 1
+ %exitcond = icmp eq i32 %inc, 186
+ br i1 %exitcond, label %for.end, label %for.body
+
+for.end: ; preds = %for.body
+; CHECK: for.end:
+; CHECK-NOT: mul i32 %m, 186
+; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
+; CHECK-NEXT: tail call void @func(i32 %add.lcssa)
+ tail call void @func(i32 %add)
+ ret void
+}
+
+; CHECK: @test2
+define i32 @test2(i32 %m) nounwind uwtable {
+entry:
+ br label %for.body
+
+for.body: ; preds = %for.body, %entry
+ %i.06 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %a.05 = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %add = add i32 %a.05, %m
+; CHECK: tail call void @func(i32 %add)
+ tail call void @func(i32 %add)
+ %inc = add nsw i32 %i.06, 1
+ %exitcond = icmp eq i32 %inc, 186
+ br i1 %exitcond, label %for.end, label %for.body
+
+for.end: ; preds = %for.body
+; CHECK: for.end:
+; CHECK-NOT: mul i32 %m, 186
+; CHECK:%add.lcssa = phi i32 [ %add, %for.body ]
+; CHECK-NEXT: ret i32 %add.lcssa
+ ret i32 %add
+}
More information about the llvm-commits
mailing list