[llvm] r342444 - [IndVars] Remove unreasonable checks in rewriteLoopExitValues

Max Kazantsev via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 17 21:57:19 PDT 2018


Author: mkazantsev
Date: Mon Sep 17 21:57:18 2018
New Revision: 342444

URL: http://llvm.org/viewvc/llvm-project?rev=342444&view=rev
Log:
[IndVars] Remove unreasonable checks in rewriteLoopExitValues

A piece of logic in rewriteLoopExitValues has a weird check on number of
users which allowed an unprofitable transform in case if an instruction has
more than 6 users.

Differential Revision: https://reviews.llvm.org/D51404
Reviewed By: etherzhhb

Modified:
    llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
    llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll

Modified: llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=342444&r1=342443&r2=342444&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp Mon Sep 17 21:57:18 2018
@@ -603,12 +603,9 @@ bool IndVarSimplify::rewriteLoopExitValu
         if (ExitValue->getSCEVType()>=scMulExpr) {
           bool HasHardInternalUses = false;
           bool HasSoftExternalUses = false;
-          unsigned NumUses = 0;
-          for (auto IB = Inst->user_begin(), IE = Inst->user_end();
-               IB != IE && NumUses <= 6; ++IB) {
-            Instruction *UseInstr = cast<Instruction>(*IB);
+          for (auto *IB : Inst->users()) {
+            Instruction *UseInstr = cast<Instruction>(IB);
             unsigned Opc = UseInstr->getOpcode();
-            NumUses++;
             if (L->contains(UseInstr)) {
               if (Opc == Instruction::Call)
                 HasHardInternalUses = true;
@@ -616,11 +613,8 @@ bool IndVarSimplify::rewriteLoopExitValu
               if (Opc == Instruction::PHI) {
                 // Do not count the Phi as a use. LCSSA may have inserted
                 // plenty of trivial ones.
-                NumUses--;
-                for (auto PB = UseInstr->user_begin(),
-                          PE = UseInstr->user_end();
-                     PB != PE && NumUses <= 6; ++PB, ++NumUses) {
-                  unsigned PhiOpc = cast<Instruction>(*PB)->getOpcode();
+                for (auto *PB : UseInstr->users()) {
+                  unsigned PhiOpc = cast<Instruction>(PB)->getOpcode();
                   if (PhiOpc != Instruction::Call &&
                       PhiOpc != Instruction::Ret) {
                     HasSoftExternalUses = true;
@@ -635,7 +629,7 @@ bool IndVarSimplify::rewriteLoopExitValu
               }
             }
           }
-          if (NumUses <= 6 && HasHardInternalUses && !HasSoftExternalUses)
+          if (HasHardInternalUses && !HasSoftExternalUses)
             continue;
         }
 

Modified: llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll?rev=342444&r1=342443&r2=342444&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll (original)
+++ llvm/trunk/test/Transforms/IndVarSimplify/dont-recompute.ll Mon Sep 17 21:57:18 2018
@@ -67,3 +67,33 @@ for.end:
 ; CHECK-NEXT: ret i32 %add.lcssa
   ret i32 %add
 }
+
+; CHECK-LABEL: @test3(
+define void @test3(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
+  mul i32 %add, 1
+  mul i32 %add, 1
+  mul i32 %add, 1
+  mul i32 %add, 1
+  mul i32 %add, 1
+  mul i32 %add, 1
+; 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
+}




More information about the llvm-commits mailing list