[llvm] [SimplifyCFG] Use hash map to continue hoisting the common instructions (PR #78615)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Wed Mar 20 08:35:48 PDT 2024
================
@@ -1608,16 +1618,39 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
auto *TI = BB->getTerminator();
+ SmallVector<BasicBlock *, 8> SuccessorBBs;
+ for (auto *Succ : successors(BB)) {
+ BasicBlock::iterator SuccItr = Succ->begin();
+ // If we find an unreachable instruction at the beginning of a basic block,
+ // we can still hoist instructions from the rest of the basic blocks.
+ if (isa<UnreachableInst>(*SuccItr))
+ continue;
+ SuccessorBBs.push_back(Succ);
+ }
+
+ // Find the smallest BB because we always want to iterate over instructions
+ // of the smallest Successor.
+ auto *SmallestBB = *std::min_element(SuccessorBBs.begin(), SuccessorBBs.end(),
+ [](BasicBlock *BB1, BasicBlock *BB2) {
+ return BB1->size() < BB2->size();
+ });
+ std::iter_swap(
+ SuccessorBBs.begin(),
+ std::find(SuccessorBBs.begin(), SuccessorBBs.end(), SmallestBB));
+
----------------
nikic wrote:
Isn't this going to regress existing behavior though? Currently basic hoisting will work independently of block size and it seems undesirable to lose that entirely. Or do you think that hoisting for large blocks will not matter?
I do agree that we need to avoid needlessly hashing large blocks, but I don't think we can do this using a simple block size limit. One possibility would be to compute hashes up to N instructions ahead rather than doing it all upfront.
https://github.com/llvm/llvm-project/pull/78615
More information about the llvm-commits
mailing list