aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ValueTracking.cpp
diff options
context:
space:
mode:
authorJuneyoung Lee <aqjune@gmail.com>2020-09-24 18:52:30 +0900
committerJuneyoung Lee <aqjune@gmail.com>2020-09-25 08:57:57 +0900
commit1c45220028a8aa39ad2f26c8c3c6234d1a816086 (patch)
tree4f8f43cf0e15900471b520bed401a2fb698f7e23 /llvm/lib/Analysis/ValueTracking.cpp
parent495a5e94baadefa6ed50390e6655021c127ea266 (diff)
downloadllvm-1c45220028a8aa39ad2f26c8c3c6234d1a816086.zip
llvm-1c45220028a8aa39ad2f26c8c3c6234d1a816086.tar.gz
llvm-1c45220028a8aa39ad2f26c8c3c6234d1a816086.tar.bz2
[ValueTracking] Check uses of Argument if it is given to isGuaranteedNotToBeUndefOrPoison
This is a patch that allows isGuaranteedNotToBeUndefOrPoison to return more precise result when an argument is given, by looking through its uses at the entry block (and following blocks as well, if it is checking poison only). This is useful when there is a function call with noundef arguments at the entry block. Reviewed By: nikic Differential Revision: https://reviews.llvm.org/D88207
Diffstat (limited to 'llvm/lib/Analysis/ValueTracking.cpp')
-rw-r--r--llvm/lib/Analysis/ValueTracking.cpp56
1 files changed, 32 insertions, 24 deletions
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 1a89495..682baac 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -4871,7 +4871,7 @@ bool llvm::canCreatePoison(const Operator *Op) {
return ::canCreateUndefOrPoison(Op, /*PoisonOnly=*/true);
}
-static bool programUndefinedIfUndefOrPoison(const Instruction *Inst,
+static bool programUndefinedIfUndefOrPoison(const Value *V,
bool PoisonOnly);
static bool isGuaranteedNotToBeUndefOrPoison(const Value *V,
@@ -4931,10 +4931,8 @@ static bool isGuaranteedNotToBeUndefOrPoison(const Value *V,
return true;
}
- if (auto *I = dyn_cast<Instruction>(V)) {
- if (programUndefinedIfUndefOrPoison(I, PoisonOnly))
- return true;
- }
+ if (programUndefinedIfUndefOrPoison(V, PoisonOnly))
+ return true;
// CxtI may be null or a cloned instruction.
if (!CtxI || !CtxI->getParent() || !DT)
@@ -5161,7 +5159,7 @@ bool llvm::mustTriggerUB(const Instruction *I,
return false;
}
-static bool programUndefinedIfUndefOrPoison(const Instruction *Inst,
+static bool programUndefinedIfUndefOrPoison(const Value *V,
bool PoisonOnly) {
// We currently only look for uses of values within the same basic
// block, as that makes it easier to guarantee that the uses will be
@@ -5170,9 +5168,20 @@ static bool programUndefinedIfUndefOrPoison(const Instruction *Inst,
// FIXME: Expand this to consider uses beyond the same basic block. To do
// this, look out for the distinction between post-dominance and strong
// post-dominance.
- const BasicBlock *BB = Inst->getParent();
+ const BasicBlock *BB = nullptr;
+ BasicBlock::const_iterator Begin;
+ if (const auto *Inst = dyn_cast<Instruction>(V)) {
+ BB = Inst->getParent();
+ Begin = Inst->getIterator();
+ Begin++;
+ } else if (const auto *Arg = dyn_cast<Argument>(V)) {
+ BB = &Arg->getParent()->getEntryBlock();
+ Begin = BB->begin();
+ } else {
+ return false;
+ }
- BasicBlock::const_iterator Begin = Inst->getIterator(), End = BB->end();
+ BasicBlock::const_iterator End = BB->end();
if (!PoisonOnly) {
// Be conservative & just check whether a value is passed to a noundef
@@ -5185,7 +5194,7 @@ static bool programUndefinedIfUndefOrPoison(const Instruction *Inst,
if (const auto *CB = dyn_cast<CallBase>(&I)) {
for (unsigned i = 0; i < CB->arg_size(); ++i) {
if (CB->paramHasAttr(i, Attribute::NoUndef) &&
- CB->getArgOperand(i) == Inst)
+ CB->getArgOperand(i) == V)
return true;
}
}
@@ -5199,27 +5208,26 @@ static bool programUndefinedIfUndefOrPoison(const Instruction *Inst,
// does.
SmallSet<const Value *, 16> YieldsPoison;
SmallSet<const BasicBlock *, 4> Visited;
- YieldsPoison.insert(Inst);
- Visited.insert(Inst->getParent());
+
+ YieldsPoison.insert(V);
+ auto Propagate = [&](const User *User) {
+ if (propagatesPoison(cast<Operator>(User)))
+ YieldsPoison.insert(User);
+ };
+ for_each(V->users(), Propagate);
+ Visited.insert(BB);
unsigned Iter = 0;
while (Iter++ < MaxAnalysisRecursionDepth) {
for (auto &I : make_range(Begin, End)) {
- if (&I != Inst) {
- if (mustTriggerUB(&I, YieldsPoison))
- return true;
- if (!isGuaranteedToTransferExecutionToSuccessor(&I))
- return false;
- }
+ if (mustTriggerUB(&I, YieldsPoison))
+ return true;
+ if (!isGuaranteedToTransferExecutionToSuccessor(&I))
+ return false;
// Mark poison that propagates from I through uses of I.
- if (YieldsPoison.count(&I)) {
- for (const User *User : I.users()) {
- const Instruction *UserI = cast<Instruction>(User);
- if (propagatesPoison(cast<Operator>(UserI)))
- YieldsPoison.insert(User);
- }
- }
+ if (YieldsPoison.count(&I))
+ for_each(I.users(), Propagate);
}
if (auto *NextBB = BB->getSingleSuccessor()) {