]> granicus.if.org Git - llvm/commitdiff
[IndVars] Remove dead instructions after folding trivial loop exit
authorPhilip Reames <listmail@philipreames.com>
Sun, 23 Jun 2019 17:06:57 +0000 (17:06 +0000)
committerPhilip Reames <listmail@philipreames.com>
Sun, 23 Jun 2019 17:06:57 +0000 (17:06 +0000)
In rL364135, I taught IndVars to fold exiting branches in loops with a zero backedge taken count (i.e. loops that only run one iteration).  This extends that to eliminate the dead comparison left around.

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

lib/Transforms/Scalar/IndVarSimplify.cpp
test/Transforms/IndVarSimplify/eliminate-comparison.ll
test/Transforms/IndVarSimplify/eliminate-trunc.ll
test/Transforms/IndVarSimplify/floating-point-iv.ll

index 57f246373f0ccce0867282c9ad70d4b67e2fe0aa..c173f9e4c03a4eefaff19b0695302a7e220048bf 100644 (file)
@@ -2732,10 +2732,12 @@ bool IndVarSimplify::run(Loop *L) {
       if (ExitCount->isZero()) {
         auto *BI = cast<BranchInst>(ExitingBB->getTerminator());
         bool ExitIfTrue = !L->contains(*succ_begin(ExitingBB));
-        auto *NewCond = ExitIfTrue ?
-          ConstantInt::getTrue(BI->getCondition()->getType()) :
-          ConstantInt::getFalse(BI->getCondition()->getType());
+        auto *OldCond = BI->getCondition();
+        auto *NewCond = ExitIfTrue ? ConstantInt::getTrue(OldCond->getType()) :
+          ConstantInt::getFalse(OldCond->getType());
         BI->setCondition(NewCond);
+        if (OldCond->use_empty())
+          DeadInsts.push_back(OldCond);
         Changed = true;
         continue;
       }
index 49088601125eda43166127c986db25993433b180..4b4a05a94f513c3b4b4450dc5bac96621c0b9522 100644 (file)
@@ -276,14 +276,8 @@ define i32 @func_12() nounwind uwtable {
 ; CHECK-NEXT:    tail call void @llvm.trap()
 ; CHECK-NEXT:    unreachable
 ; CHECK:       forcond38:
-; CHECK-NEXT:    [[__KEY8_0:%.*]] = phi i32 [ [[TMP81:%.*]], [[NOASSERT68:%.*]] ], [ 2, [[FORCOND38_PREHEADER]] ]
-; CHECK-NEXT:    br i1 true, label [[NOASSERT68]], label [[UNROLLEDEND:%.*]]
+; CHECK-NEXT:    br i1 true, label [[NOASSERT68:%.*]], label [[UNROLLEDEND:%.*]]
 ; CHECK:       noassert68:
-; CHECK-NEXT:    [[TMP57:%.*]] = sdiv i32 -32768, [[__KEY8_0]]
-; CHECK-NEXT:    [[SEXT34:%.*]] = shl i32 [[TMP57]], 16
-; CHECK-NEXT:    [[SEXT21:%.*]] = shl i32 [[TMP57]], 16
-; CHECK-NEXT:    [[TMP76:%.*]] = icmp ne i32 [[SEXT34]], [[SEXT21]]
-; CHECK-NEXT:    [[TMP81]] = add nuw nsw i32 [[__KEY8_0]], 1
 ; CHECK-NEXT:    br i1 false, label [[FORCOND38]], label [[ASSERT77:%.*]]
 ; CHECK:       assert77:
 ; CHECK-NEXT:    tail call void @llvm.trap()
index eaa20b5bf6df87b308762bfc93d8c9b053c59025..74e422b797bf9175550da38e85dc32faa9079eb4 100644 (file)
@@ -170,10 +170,6 @@ define void @test_06(i32 %n) {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
-; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ -2147483649, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
-; CHECK-NEXT:    [[IV_NEXT]] = add nuw nsw i64 [[IV]], 1
-; CHECK-NEXT:    [[NARROW_IV:%.*]] = trunc i64 [[IV]] to i32
-; CHECK-NEXT:    [[CMP:%.*]] = icmp slt i32 [[NARROW_IV]], [[N:%.*]]
 ; CHECK-NEXT:    br i1 false, label [[LOOP]], label [[EXIT:%.*]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret void
@@ -350,10 +346,6 @@ define void @test_06_unsigned(i32 %n) {
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
-; CHECK-NEXT:    [[IV:%.*]] = phi i64 [ -1, [[ENTRY:%.*]] ], [ [[IV_NEXT:%.*]], [[LOOP]] ]
-; CHECK-NEXT:    [[IV_NEXT]] = add nsw i64 [[IV]], 1
-; CHECK-NEXT:    [[NARROW_IV:%.*]] = trunc i64 [[IV]] to i32
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ult i32 [[NARROW_IV]], [[N:%.*]]
 ; CHECK-NEXT:    br i1 false, label [[LOOP]], label [[EXIT:%.*]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret void
index c5bf3860ab5d62c1239321409c4175358c197ac8..ef08863428e4b11210a1b40b639f3bd6d311c148 100644 (file)
@@ -50,7 +50,7 @@ bb:           ; preds = %bb, %entry
 return:
        ret void
 ; CHECK-LABEL: @test3(
-; CHECK: fcmp
+; CHECK: br i1 false
 }
 
 define void @test4() nounwind {