aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/lib/Transforms/Utils/SimplifyCFG.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/SimplifyCFG.cpp15
1 files changed, 10 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index f08ab18..805bc32 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -4241,11 +4241,6 @@ bool SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst *BI,
BasicBlock *BB = BI->getParent();
- // MSAN does not like undefs as branch condition which can be introduced
- // with "explicit branch".
- if (ExtraCase && BB->getParent()->hasFnAttribute(Attribute::SanitizeMemory))
- return false;
-
LLVM_DEBUG(dbgs() << "Converting 'icmp' chain with " << Values.size()
<< " cases into SWITCH. BB is:\n"
<< *BB);
@@ -4263,6 +4258,16 @@ bool SimplifyCFGOpt::SimplifyBranchOnICmpChain(BranchInst *BI,
Instruction *OldTI = BB->getTerminator();
Builder.SetInsertPoint(OldTI);
+ // There can be an unintended UB if extra values are Poison. Before the
+ // transformation, extra values may not be evaluated according to the
+ // condition, and it will not raise UB. But after transformation, we are
+ // evaluating extra values before checking the condition, and it will raise
+ // UB. It can be solved by adding freeze instruction to extra values.
+ AssumptionCache *AC = Options.AC;
+
+ if (!isGuaranteedNotToBeUndefOrPoison(ExtraCase, AC, BI, nullptr))
+ ExtraCase = Builder.CreateFreeze(ExtraCase);
+
if (TrueWhenEqual)
Builder.CreateCondBr(ExtraCase, EdgeBB, NewBB);
else