diff options
author | Serge Guelton <sguelton@quarkslab.com> | 2017-06-27 18:57:53 +0000 |
---|---|---|
committer | Serge Guelton <sguelton@quarkslab.com> | 2017-06-27 18:57:53 +0000 |
commit | 7bc405aa4c0f4c2e4582d5b8acc6eb8fc7e8d592 (patch) | |
tree | 4b5a68f4d3a7bf550743cd64660b03765662fd53 /llvm/lib/Transforms/Utils/CodeExtractor.cpp | |
parent | 6a92381c50dbc25bd8b4c7cbe31c9deaeb4f8806 (diff) | |
download | bcm5719-llvm-7bc405aa4c0f4c2e4582d5b8acc6eb8fc7e8d592.tar.gz bcm5719-llvm-7bc405aa4c0f4c2e4582d5b8acc6eb8fc7e8d592.zip |
[CodeExtractor] Prevent extraction of block involving blockaddress
BlockAddress are only valid within their function context, which does not
interact well with CodeExtractor. Detect this case and prevent it.
Differential Revision: https://reviews.llvm.org/D33839
llvm-svn: 306448
Diffstat (limited to 'llvm/lib/Transforms/Utils/CodeExtractor.cpp')
-rw-r--r-- | llvm/lib/Transforms/Utils/CodeExtractor.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/CodeExtractor.cpp b/llvm/lib/Transforms/Utils/CodeExtractor.cpp index 5d57ed9718f..30d8856cfbe 100644 --- a/llvm/lib/Transforms/Utils/CodeExtractor.cpp +++ b/llvm/lib/Transforms/Utils/CodeExtractor.cpp @@ -59,6 +59,33 @@ bool CodeExtractor::isBlockValidForExtraction(const BasicBlock &BB) { // Landing pads must be in the function where they were inserted for cleanup. if (BB.isEHPad()) return false; + // taking the address of a basic block moved to another function is illegal + if (BB.hasAddressTaken()) + return false; + + // don't hoist code that uses another basicblock address, as it's likely to + // lead to unexpected behavior, like cross-function jumps + SmallPtrSet<User const *, 16> Visited; + SmallVector<User const *, 16> ToVisit; + + for (Instruction const &Inst : BB) + ToVisit.push_back(&Inst); + + while (!ToVisit.empty()) { + User const *Curr = ToVisit.pop_back_val(); + if (!Visited.insert(Curr).second) + continue; + if (isa<BlockAddress const>(Curr)) + return false; // even a reference to self is likely to be not compatible + + if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->getParent() != &BB) + continue; + + for (auto const &U : Curr->operands()) { + if (auto *UU = dyn_cast<User>(U)) + ToVisit.push_back(UU); + } + } // Don't hoist code containing allocas, invokes, or vastarts. for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) { |