From: Chad Rosier Date: Mon, 25 Apr 2016 17:23:36 +0000 (+0000) Subject: [ValueTracking] Improve isImpliedCondition when the dominating cond is false. X-Git-Tag: android-x86-7.1-r4~34604 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=3ac68db42d1a07ace19bb48c3f89c2de3f65adcd;p=android-x86%2Fexternal-llvm.git [ValueTracking] Improve isImpliedCondition when the dominating cond is false. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@267430 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Analysis/ValueTracking.h b/include/llvm/Analysis/ValueTracking.h index 6e58ee07998..76249b9634f 100644 --- a/include/llvm/Analysis/ValueTracking.h +++ b/include/llvm/Analysis/ValueTracking.h @@ -463,11 +463,10 @@ template class ArrayRef; /// T | T | F /// F | T | T /// (A) - Optional isImpliedCondition(Value *LHS, Value *RHS, - const DataLayout &DL, unsigned Depth = 0, - AssumptionCache *AC = nullptr, - const Instruction *CxtI = nullptr, - const DominatorTree *DT = nullptr); + Optional isImpliedCondition( + Value *LHS, Value *RHS, const DataLayout &DL, bool InvertAPred = false, + unsigned Depth = 0, AssumptionCache *AC = nullptr, + const Instruction *CxtI = nullptr, const DominatorTree *DT = nullptr); } // end namespace llvm #endif diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index 5ada78a5350..a9b8af1017c 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -3962,8 +3962,8 @@ static Optional isImpliedCondMatchingOperands(CmpInst::Predicate APred, } Optional llvm::isImpliedCondition(Value *LHS, Value *RHS, - const DataLayout &DL, unsigned Depth, - AssumptionCache *AC, + const DataLayout &DL, bool InvertAPred, + unsigned Depth, AssumptionCache *AC, const Instruction *CxtI, const DominatorTree *DT) { assert(LHS->getType() == RHS->getType() && "mismatched type"); @@ -3971,7 +3971,7 @@ Optional llvm::isImpliedCondition(Value *LHS, Value *RHS, assert(OpTy->getScalarType()->isIntegerTy(1)); // LHS ==> RHS by definition - if (LHS == RHS) + if (!InvertAPred && LHS == RHS) return true; if (OpTy->isVectorTy()) @@ -3987,6 +3987,9 @@ Optional llvm::isImpliedCondition(Value *LHS, Value *RHS, !match(RHS, m_ICmp(BPred, m_Value(BLHS), m_Value(BRHS)))) return None; + if (InvertAPred) + APred = CmpInst::getInversePredicate(APred); + Optional Implication = isImpliedCondMatchingOperands(APred, ALHS, ARHS, BPred, BLHS, BRHS); if (Implication) diff --git a/lib/Transforms/Scalar/JumpThreading.cpp b/lib/Transforms/Scalar/JumpThreading.cpp index ba406a8e8b3..75c340fd593 100644 --- a/lib/Transforms/Scalar/JumpThreading.cpp +++ b/lib/Transforms/Scalar/JumpThreading.cpp @@ -925,11 +925,14 @@ bool JumpThreading::ProcessImpliedCondition(BasicBlock *BB) { while (CurrentPred && Iter++ < ImplicationSearchThreshold) { auto *PBI = dyn_cast(CurrentPred->getTerminator()); - if (!PBI || !PBI->isConditional() || PBI->getSuccessor(0) != CurrentBB) + if (!PBI || !PBI->isConditional()) + return false; + if (PBI->getSuccessor(0) != CurrentBB && PBI->getSuccessor(1) != CurrentBB) return false; + bool FalseDest = PBI->getSuccessor(1) == CurrentBB; Optional Implication = - isImpliedCondition(PBI->getCondition(), Cond, DL); + isImpliedCondition(PBI->getCondition(), Cond, DL, FalseDest); if (Implication) { BI->getSuccessor(*Implication ? 1 : 0)->removePredecessor(BB); BranchInst::Create(BI->getSuccessor(*Implication ? 0 : 1), BI); diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 53bea2191bc..23199b37b29 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -2703,11 +2703,13 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI, // If BI is reached from the true path of PBI and PBI's condition implies // BI's condition, we know the direction of the BI branch. - if (PBI->getSuccessor(0) == BI->getParent() && + if ((PBI->getSuccessor(0) == BI->getParent() || + PBI->getSuccessor(1) == BI->getParent()) && PBI->getSuccessor(0) != PBI->getSuccessor(1) && BB->getSinglePredecessor()) { - Optional Implication = - isImpliedCondition(PBI->getCondition(), BI->getCondition(), DL); + bool FalseDest = PBI->getSuccessor(1) == BI->getParent(); + Optional Implication = isImpliedCondition( + PBI->getCondition(), BI->getCondition(), DL, FalseDest); if (Implication) { // Turn this into a branch on constant. auto *OldCond = BI->getCondition(); diff --git a/test/Transforms/JumpThreading/implied-cond.ll b/test/Transforms/JumpThreading/implied-cond.ll index 991f78ff497..6da05791456 100644 --- a/test/Transforms/JumpThreading/implied-cond.ll +++ b/test/Transforms/JumpThreading/implied-cond.ll @@ -125,3 +125,53 @@ if.end: if.end3: ret void } + +declare void @is(i1) + +; If A >=s B is false then A <=s B is implied true. +; CHECK-LABEL: @test_sge_sle +; CHECK: call void @is(i1 true) +; CHECK-NOT: call void @is(i1 false) +define void @test_sge_sle(i32 %a, i32 %b) { + %cmp1 = icmp sge i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp sle i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A <=s B is false then A <=s B is implied false. +; CHECK-LABEL: @test_sle_sle +; CHECK-NOT: call void @is(i1 true) +; CHECK: call void @is(i1 false) +define void @test_sle_sle(i32 %a, i32 %b) { + %cmp1 = icmp sle i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp sle i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} diff --git a/test/Transforms/SimplifyCFG/implied-cond-matching-false-dest.ll b/test/Transforms/SimplifyCFG/implied-cond-matching-false-dest.ll new file mode 100644 index 00000000000..1d29813ecfa --- /dev/null +++ b/test/Transforms/SimplifyCFG/implied-cond-matching-false-dest.ll @@ -0,0 +1,339 @@ +; RUN: opt %s -S -simplifycfg | FileCheck %s + +declare void @is(i1) + +; If A == B is false then A == B is implied false. +; CHECK-LABEL: @test_eq_eq +; CHECK-NOT: call void @is(i1 true) +; CHECK: call void @is(i1 false) +define void @test_eq_eq(i32 %a, i32 %b) { + %cmp1 = icmp eq i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp eq i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A == B is false then A != B is implied true. +; CHECK-LABEL: @test_eq_ne +; CHECK: call void @is(i1 true) +; CHECK-NOT: call void @is(i1 false) +define void @test_eq_ne(i32 %a, i32 %b) { + %cmp1 = icmp eq i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp ne i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A != B is false then A != B is implied false. +; CHECK-LABEL: @test_ne_ne +; CHECK-NOT: call void @is(i1 true) +; CHECK: call void @is(i1 false) +define void @test_ne_ne(i32 %a, i32 %b) { + %cmp1 = icmp ne i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp ne i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A != B is false then A >u B is implied false. +; CHECK-LABEL: @test_ne_ugt +; CHECK-NOT: call void @is(i1 true) +; CHECK: call void @is(i1 false) +define void @test_ne_ugt(i32 %a, i32 %b) { + %cmp1 = icmp ne i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp ugt i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A != B is false then A >=u B is implied true. +; CHECK-LABEL: @test_ne_uge +; CHECK: call void @is(i1 true) +; CHECK-NOT: call void @is(i1 false) +define void @test_ne_uge(i32 %a, i32 %b) { + %cmp1 = icmp ne i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp uge i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A != B is false then A u B is false then A >u B is implied false. +; CHECK-LABEL: @test_ugt_ugt +; CHECK-NOT: call void @is(i1 true) +; CHECK: call void @is(i1 false) +define void @test_ugt_ugt(i32 %a, i32 %b) { + %cmp1 = icmp ugt i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp ugt i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A >u B is false then A <=u B is implied true. +; CHECK-LABEL: @test_ugt_ule +; CHECK: call void @is(i1 true) +; CHECK-NOT: call void @is(i1 false) +define void @test_ugt_ule(i32 %a, i32 %b) { + %cmp1 = icmp ugt i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp ule i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A >=u B is false then A >=u B is implied false. +; CHECK-LABEL: @test_uge_uge +; CHECK-NOT: call void @is(i1 true) +; CHECK: call void @is(i1 false) +define void @test_uge_uge(i32 %a, i32 %b) { + %cmp1 = icmp uge i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp uge i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A >=u B is false then A =u B is false then A <=u B is implied true. +; CHECK-LABEL: @test_uge_ule +; CHECK: call void @is(i1 true) +; CHECK-NOT: call void @is(i1 false) +define void @test_uge_ule(i32 %a, i32 %b) { + %cmp1 = icmp uge i32 %a, %b + br i1 %cmp1, label %untaken, label %taken + +taken: + %cmp2 = icmp ule i32 %a, %b + br i1 %cmp2, label %istrue, label %isfalse + +istrue: + call void @is(i1 true) + ret void + +isfalse: + call void @is(i1 false) + ret void + +untaken: + ret void +} + +; If A