diff options
author | Brian M. Rzycki <brzycki@gmail.com> | 2019-04-08 18:20:35 +0000 |
---|---|---|
committer | Brian M. Rzycki <brzycki@gmail.com> | 2019-04-08 18:20:35 +0000 |
commit | 887865c1ad6e2a50251846a1ab907ec000271c17 (patch) | |
tree | dc75fc09ac4b343cd3efd832e60881cf6c74981a /llvm/lib | |
parent | ebf1830bb1ddb7a49f3e037bca83b3ec4f60a05c (diff) | |
download | bcm5719-llvm-887865c1ad6e2a50251846a1ab907ec000271c17.tar.gz bcm5719-llvm-887865c1ad6e2a50251846a1ab907ec000271c17.zip |
[JumpThreading] Fix incorrect fold conditional after indirectbr/callbr
Fixes bug 40992: https://bugs.llvm.org/show_bug.cgi?id=40992
There is potential for miscompiled code emitted from JumpThreading when
analyzing a block with one or more indirectbr or callbr predecessors. The
ProcessThreadableEdges() function incorrectly folds conditional branches
into an unconditional branch.
This patch prevents incorrect branch folding without fully pessimizing
other potential threading opportunities through the same basic block.
This IR shape was manually fed in via opt and is unclear if clang and the
full pass pipeline will ever emit similar code shapes.
Thanks to Matthias Liedtke for the bug report and simplified IR example.
Differential Revision: https://reviews.llvm.org/D60284
llvm-svn: 357930
Diffstat (limited to 'llvm/lib')
-rw-r--r-- | llvm/lib/Transforms/Scalar/JumpThreading.cpp | 6 |
1 files changed, 1 insertions, 5 deletions
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp index 264ea3aa22a..123c8b9630c 100644 --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -1606,7 +1606,6 @@ bool JumpThreadingPass::ProcessThreadableEdges(Value *Cond, BasicBlock *BB, Constant *OnlyVal = nullptr; Constant *MultipleVal = (Constant *)(intptr_t)~0ULL; - unsigned PredWithKnownDest = 0; for (const auto &PredValue : PredValues) { BasicBlock *Pred = PredValue.second; if (!SeenPreds.insert(Pred).second) @@ -1643,9 +1642,6 @@ bool JumpThreadingPass::ProcessThreadableEdges(Value *Cond, BasicBlock *BB, OnlyVal = MultipleVal; } - // We know where this predecessor is going. - ++PredWithKnownDest; - // If the predecessor ends with an indirect goto, we can't change its // destination. Same for CallBr. if (isa<IndirectBrInst>(Pred->getTerminator()) || @@ -1663,7 +1659,7 @@ bool JumpThreadingPass::ProcessThreadableEdges(Value *Cond, BasicBlock *BB, // not thread. By doing so, we do not need to duplicate the current block and // also miss potential opportunities in case we dont/cant duplicate. if (OnlyDest && OnlyDest != MultipleDestSentinel) { - if (PredWithKnownDest == (size_t)pred_size(BB)) { + if (BB->hasNPredecessors(PredToDestList.size())) { bool SeenFirstBranchToOnlyDest = false; std::vector <DominatorTree::UpdateType> Updates; Updates.reserve(BB->getTerminator()->getNumSuccessors() - 1); |