[llvm] [SimplifyCFG] Preserve common TBAA metadata when hoisting instructions. (PR #97158)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Dec 13 03:58:15 PST 2024
================
@@ -1807,17 +1878,39 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(Instruction *TI,
SuccIterPairs.push_back(SuccIterPair(SuccItr, 0));
}
- // Check if only hoisting terminators is allowed. This does not add new
- // instructions to the hoist location.
- if (EqTermsOnly) {
- // Skip any debug intrinsics, as they are free to hoist.
- for (auto &SuccIter : make_first_range(SuccIterPairs)) {
- auto *INonDbg = &*skipDebugIntrinsics(SuccIter);
- if (!INonDbg->isTerminator())
- return false;
+ if (AllInstsEqOnly) {
+ // Check if all instructions in the successor blocks match. This allows
+ // hoisting all instructions and removing the blocks we are hoisting from,
+ // so does not add any new instructions.
+ bool AllSame = true;
+ SmallVector<BasicBlock *> Succs = to_vector(successors(BB));
+ // Check if sizes and terminators of all successors match.
+ if (any_of(Succs, [&Succs](BasicBlock *Succ) {
+ Instruction *Term0 = Succs[0]->getTerminator();
+ Instruction *Term = Succ->getTerminator();
+ if (!Term->isSameOperationAs(Term0) ||
+ !equal(Term->operands(), Term0->operands()))
+ return true;
+ return Succs[0]->sizeWithoutDebug() != Succ->sizeWithoutDebug();
+ })) {
+ AllSame = false;
+ } else {
+ LockstepReverseIterator LRI(Succs);
+ while (LRI.isValid()) {
+ Instruction *I0 = (*LRI)[0];
+ if (any_of(*LRI, [I0](Instruction *I) {
+ return !areIdenticalUpToCommutativity(I0, I);
+ })) {
+ AllSame = false;
+ break;
+ }
+ --LRI;
+ }
}
- // Now we know that we only need to hoist debug intrinsics and the
- // terminator. Let the loop below handle those 2 cases.
+ if (!AllSame)
+ return false;
+ // Now we know that all instructions in all successors can be hoisted Let
----------------
nikic wrote:
```suggestion
// Now we know that all instructions in all successors can be hoisted. Let
```
https://github.com/llvm/llvm-project/pull/97158
More information about the llvm-commits
mailing list