From: Philip Reames Date: Sun, 23 Jun 2019 17:06:57 +0000 (+0000) Subject: [IndVars] Remove dead instructions after folding trivial loop exit X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=91371a51532af963fe4fcca3516ce3382f332cbb;p=llvm [IndVars] Remove dead instructions after folding trivial loop exit 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 --- diff --git a/lib/Transforms/Scalar/IndVarSimplify.cpp b/lib/Transforms/Scalar/IndVarSimplify.cpp index 57f246373f0..c173f9e4c03 100644 --- a/lib/Transforms/Scalar/IndVarSimplify.cpp +++ b/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -2732,10 +2732,12 @@ bool IndVarSimplify::run(Loop *L) { if (ExitCount->isZero()) { auto *BI = cast(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; } diff --git a/test/Transforms/IndVarSimplify/eliminate-comparison.ll b/test/Transforms/IndVarSimplify/eliminate-comparison.ll index 49088601125..4b4a05a94f5 100644 --- a/test/Transforms/IndVarSimplify/eliminate-comparison.ll +++ b/test/Transforms/IndVarSimplify/eliminate-comparison.ll @@ -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() diff --git a/test/Transforms/IndVarSimplify/eliminate-trunc.ll b/test/Transforms/IndVarSimplify/eliminate-trunc.ll index eaa20b5bf6d..74e422b797b 100644 --- a/test/Transforms/IndVarSimplify/eliminate-trunc.ll +++ b/test/Transforms/IndVarSimplify/eliminate-trunc.ll @@ -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 diff --git a/test/Transforms/IndVarSimplify/floating-point-iv.ll b/test/Transforms/IndVarSimplify/floating-point-iv.ll index c5bf3860ab5..ef08863428e 100644 --- a/test/Transforms/IndVarSimplify/floating-point-iv.ll +++ b/test/Transforms/IndVarSimplify/floating-point-iv.ll @@ -50,7 +50,7 @@ bb: ; preds = %bb, %entry return: ret void ; CHECK-LABEL: @test3( -; CHECK: fcmp +; CHECK: br i1 false } define void @test4() nounwind {