[llvm] [SimplifyCFG] Preserve common TBAA metadata when hoisting instructions. (PR #97158)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 13 04:29:18 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();
----------------
fhahn wrote:

Done thanks!

https://github.com/llvm/llvm-project/pull/97158


More information about the llvm-commits mailing list