Index: lib/Transforms/Utils/CodeExtractor.cpp =================================================================== --- lib/Transforms/Utils/CodeExtractor.cpp +++ lib/Transforms/Utils/CodeExtractor.cpp @@ -70,6 +70,29 @@ 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 Visited; + SmallVector ToVisit; + + for (Instruction const &Inst : BB) + ToVisit.push_back(&Inst); + + // loop + while (!ToVisit.empty()) { + User const *Curr = ToVisit.pop_back_val(); + if (Visited.count(Curr)) + continue; + Visited.insert(Curr); + if (isa(Curr)) { + return true; // even a reference to self is likely to be not compatible + } + for (auto const &U : Curr->operands()) { + if (auto *UU = dyn_cast(U)) + ToVisit.push_back(UU); + } + } + return true; }