diff options
author | Juneyoung Lee <aqjune@gmail.com> | 2020-02-29 15:45:04 +0900 |
---|---|---|
committer | Juneyoung Lee <aqjune@gmail.com> | 2020-03-04 11:43:31 +0900 |
commit | 952ad4701cf0d8da79789f6b83ddaa386c60d535 (patch) | |
tree | be74109ed2e1e52e23140dcfefd696e1cd10dc49 /llvm/lib/Analysis/ValueTracking.cpp | |
parent | e91e1df6ab74006e96b0cca94192e935542705a4 (diff) | |
download | llvm-952ad4701cf0d8da79789f6b83ddaa386c60d535.zip llvm-952ad4701cf0d8da79789f6b83ddaa386c60d535.tar.gz llvm-952ad4701cf0d8da79789f6b83ddaa386c60d535.tar.bz2 |
[ValueTracking] Let isGuaranteedNotToBeUndefOrPoison look into branch conditions of dominating blocks' terminators
Summary:
```
br i1 c, BB1, BB2:
BB1:
use1(c)
BB2:
use2(c)
```
In BB1 and BB2, c is never undef or poison because otherwise the branch would have triggered UB.
Checked with Alive2
Reviewers: xbolva00, spatel, lebedev.ri, reames, jdoerfert, nlopes, sanjoy
Reviewed By: reames
Subscribers: jdoerfert, hiraditya, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D75401
Diffstat (limited to 'llvm/lib/Analysis/ValueTracking.cpp')
-rw-r--r-- | llvm/lib/Analysis/ValueTracking.cpp | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index e11f5e2..34f8abd 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -4525,7 +4525,9 @@ bool llvm::isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch); } -bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V) { +bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V, + const Instruction *CtxI, + const DominatorTree *DT) { // If the value is a freeze instruction, then it can never // be undef or poison. if (isa<FreezeInst>(V)) @@ -4558,6 +4560,29 @@ bool llvm::isGuaranteedNotToBeUndefOrPoison(const Value *V) { return true; } + if (!CtxI || !DT) + return false; + + // If V is used as a branch condition before reaching CtxI, V cannot be + // undef or poison. + // br V, BB1, BB2 + // BB1: + // CtxI ; V cannot be undef or poison here + auto Dominator = DT->getNode(CtxI->getParent())->getIDom(); + while (Dominator) { + auto *TI = Dominator->getBlock()->getTerminator(); + + if (auto BI = dyn_cast<BranchInst>(TI)) { + if (BI->isConditional() && BI->getCondition() == V) + return true; + } else if (auto SI = dyn_cast<SwitchInst>(TI)) { + if (SI->getCondition() == V) + return true; + } + + Dominator = Dominator->getIDom(); + } + return false; } |