llvm.org GIT mirror llvm / 660cab3
Use LVI to eliminate conditional branches where we've tested a related condition previously. Update tests for this change. This fixes PR5652. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@112270 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 9 years ago
4 changed file(s) with 53 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
655655
656656 // Figure out the possible values of the query BEFORE this branch.
657657 LVILatticeVal InBlock = getBlockValue(BBFrom);
658 if (!InBlock.isConstantRange()) return InBlock;
658 if (!InBlock.isConstantRange())
659 return LVILatticeVal::getRange(TrueValues);
659660
660661 // Find all potential values that satisfy both the input and output
661662 // conditions.
668668 }
669669 }
670670 }
671
672 // For a comparison where the LHS is outside this block, it's possible
673 // that we've branch on it before. Used LVI to see if we can simplify
674 // the branch based on that.
675 BranchInst *CondBr = dyn_cast(BB->getTerminator());
676 Constant *CondConst = dyn_cast(CondCmp->getOperand(1));
677 if (LVI && CondBr && CondConst && CondBr->isConditional() &&
678 (!isa(CondCmp->getOperand(0)) ||
679 cast(CondCmp->getOperand(0))->getParent() != BB)) {
680 // For predecessor edge, determine if the comparison is true or false
681 // on that edge. If they're all true or all false, we can simplify the
682 // branch.
683 // FIXME: We could handle mixed true/false by duplicating code.
684 unsigned Trues = 0, Falses = 0, predcount = 0;
685 for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB);PI != PE; ++PI){
686 ++predcount;
687 LazyValueInfo::Tristate Ret =
688 LVI->getPredicateOnEdge(CondCmp->getPredicate(),
689 CondCmp->getOperand(0), CondConst, *PI, BB);
690 if (Ret == LazyValueInfo::True)
691 ++Trues;
692 else if (Ret == LazyValueInfo::False)
693 ++Falses;
694 }
695
696 // If we can determine the branch direction statically, converted
697 // the conditional branch to an unconditional one.
698 if (Trues && Trues == predcount) {
699 RemovePredecessorAndSimplify(CondBr->getSuccessor(1), BB, TD);
700 BranchInst::Create(CondBr->getSuccessor(0), CondBr);
701 CondBr->eraseFromParent();
702 return true;
703 } else if (Falses && Falses == predcount) {
704 RemovePredecessorAndSimplify(CondBr->getSuccessor(0), BB, TD);
705 BranchInst::Create(CondBr->getSuccessor(1), CondBr);
706 CondBr->eraseFromParent();
707 return true;
708 }
709 }
671710 }
672711
673712 // Check for some cases that are worth simplifying. Right now we want to look
146146 ; CHECK: @test6
147147 %tmp455 = icmp eq i32 %A, 42
148148 br i1 %tmp455, label %BB1, label %BB2
149
149
150 ; CHECK: call i32 @f2()
151 ; CHECK-NEXT: ret i32 3
152
153 ; CHECK: call i32 @f1()
154 ; CHECK-NOT: br
155 ; CHECK: call void @f3()
156 ; CHECK-NOT: br
157 ; CHECK: ret i32 4
158
150159 BB2:
151 ; CHECK: call i32 @f1()
152 ; CHECK-NEXT: call void @f3()
153 ; CHECK-NEXT: ret i32 4
154160 call i32 @f1()
155161 br label %BB1
156162
None ; RUN: opt -S -jump-threading -enable-jump-threading-lvi < %s | FileCheck %s
0 ; RUN: opt -S -jump-threading -enable-jump-threading-lvi -dce < %s | FileCheck %s
11 target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
22 target triple = "x86_64-apple-darwin10.4"
33
2424 %toBoolnot.i.i = icmp ult i8 %1, 21 ; [#uses=1]
2525 br i1 %toBoolnot.i.i, label %bb6.i.i, label %_ZN4llvm8dyn_castINS_11InstructionEPNS_5ValueEEENS_10cast_rettyIT_T0_E8ret_typeERKS6_.exit
2626
27 ; CHECK-NOT: assert
2728 bb6.i.i: ; preds = %bb.i
2829 tail call void @__assert_rtn(i8* getelementptr inbounds ([5 x i8]* @_ZZN4llvm4castINS_11InstructionEPNS_5ValueEEENS_10cast_rettyIT_T0_E8ret_typeERKS6_E8__func__, i64 0, i64 0), i8* getelementptr inbounds ([31 x i8]* @.str, i64 0, i64 0), i32 202, i8* getelementptr inbounds ([59 x i8]* @.str1, i64 0, i64 0)) noreturn
2930 unreachable