[llvm] [SLP] Make getSameOpcode support interchangeable instructions. (PR #133888)

Han-Kuan Chen via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 13 23:57:50 PDT 2025


================
@@ -599,6 +599,28 @@ static std::optional<unsigned> getElementIndex(const Value *Inst,
   return Index;
 }
 
+/// \returns true if all of the values in \p VL use the same opcode.
+/// For comparison instructions, also checks if predicates match.
+/// PoisonValues are considered matching.
+/// Interchangeable instructions are not considered.
+static bool allSameOpcode(ArrayRef<Value *> VL) {
+  auto *It = find_if(VL, IsaPred<Instruction>);
+  if (It == VL.end())
+    return true;
+  Instruction *MainOp = cast<Instruction>(*It);
+  unsigned Opcode = MainOp->getOpcode();
+  bool IsCmpOp = isa<CmpInst>(MainOp);
+  CmpInst::Predicate BasePred = IsCmpOp ? cast<CmpInst>(MainOp)->getPredicate()
+                                        : CmpInst::BAD_ICMP_PREDICATE;
+  return std::all_of(It, VL.end(), [&](Value *V) {
+    if (auto *CI = dyn_cast<CmpInst>(V))
+      return BasePred == CI->getPredicate();
+    if (auto *I = dyn_cast<Instruction>(V))
+      return I->getOpcode() == Opcode;
+    return isa<PoisonValue>(V);
+  });
+}
+
----------------
HanKuanChen wrote:

Cannot implement allSameOpcode by isAlternateInstruction.

The MainOp and AltOp of isAlternateInstruction come from getSameOpcode, but getSameOpcode already takes interchangeable instructions into account.

However, the current allSameOpcode "refinds" MainOp and AltOp without considering alternate or interchangeable instructions.

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


More information about the llvm-commits mailing list