[Local] Verify opcodes match for all insts passed to mergeFlags (NFC). (#141231)
The logic for tracking flags relies on all instructions having the same
opcode. Add an assert to check, as suggested in
https://github.com/llvm/llvm-project/pull/140406.
PR: https://github.com/llvm/llvm-project/pull/141231
diff --git a/llvm/include/llvm/Transforms/Utils/Local.h b/llvm/include/llvm/Transforms/Utils/Local.h
index 9214aad..6162557 100644
--- a/llvm/include/llvm/Transforms/Utils/Local.h
+++ b/llvm/include/llvm/Transforms/Utils/Local.h
@@ -565,6 +565,12 @@
bool HasNSW = true;
bool IsDisjoint = true;
+#ifndef NDEBUG
+ /// Opcode of merged instructions. All instructions passed to mergeFlags must
+ /// have the same opcode.
+ std::optional<unsigned> Opcode;
+#endif
+
// Note: At the moment, users are responsible to manage AllKnownNonNegative
// and AllKnownNonZero manually. AllKnownNonNegative can be true in a case
// where one of the operands is negative, but one the operators is not NSW.
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 69dcd30..fe1391a 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -4364,6 +4364,13 @@
}
void OverflowTracking::mergeFlags(Instruction &I) {
+#ifndef NDEBUG
+ if (Opcode)
+ assert(Opcode == I.getOpcode() &&
+ "can only use mergeFlags on instructions with matching opcodes");
+ else
+ Opcode = I.getOpcode();
+#endif
if (isa<OverflowingBinaryOperator>(&I)) {
HasNUW &= I.hasNoUnsignedWrap();
HasNSW &= I.hasNoSignedWrap();