[llvm] [SimplifyCFG] Add support for hoisting commutative instructions (PR #104805)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 19 09:20:34 PDT 2024


================
@@ -1583,6 +1583,26 @@ static void hoistLockstepIdenticalDbgVariableRecords(
   }
 }
 
+static bool areIdenticalUpToCommutativity(const Instruction *I1,
+                                          const Instruction *I2) {
+  if (I1->isIdenticalToWhenDefined(I2))
+    return true;
+
+  if (auto *Cmp1 = dyn_cast<CmpInst>(I1))
+    if (auto *Cmp2 = dyn_cast<CmpInst>(I2))
+      return Cmp1->getPredicate() == Cmp2->getSwappedPredicate() &&
+             Cmp1->getOperand(0) == Cmp2->getOperand(1) &&
+             Cmp1->getOperand(1) == Cmp2->getOperand(0);
+
+  if (I1->isCommutative() && I1->isSameOperationAs(I2)) {
+    return I1->getOperand(0) == I2->getOperand(1) &&
+           I1->getOperand(1) == I2->getOperand(0) &&
+           equal(drop_begin(I1->operands(), 2), drop_begin(I2->operands(), 2));
+  }
+
+  return false;
+}
----------------
goldsteinn wrote:

Although unrelated to the complexity canonicalization case, should we also handle `select` here?

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


More information about the llvm-commits mailing list