Skip to content

Commit 2e06c25

Browse files
committed
add findInstructionWithOpcode
1 parent 64d3405 commit 2e06c25

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,25 @@ convertTo(Instruction *I, const InstructionsState &S) {
12051205
static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
12061206
const TargetLibraryInfo &TLI);
12071207

1208+
/// Find an instruction with a specific opcode in VL.
1209+
/// \param VL Array of values to search through. Must contain only Instructions
1210+
/// and PoisonValues.
1211+
/// \param Opcode The instruction opcode to search for
1212+
/// \returns
1213+
/// - The first instruction found with matching opcode
1214+
/// - nullptr if no matching instruction is found
1215+
Instruction *findInstructionWithOpcode(ArrayRef<Value *> VL, unsigned Opcode) {
1216+
for (Value *V : VL) {
1217+
if (isa<PoisonValue>(V))
1218+
continue;
1219+
assert(isa<Instruction>(V) && "Only accepts PoisonValue and Instruction.");
1220+
auto *Inst = cast<Instruction>(V);
1221+
if (Inst->getOpcode() == Opcode)
1222+
return Inst;
1223+
}
1224+
return nullptr;
1225+
}
1226+
12081227
/// Checks if the provided operands of 2 cmp instructions are compatible, i.e.
12091228
/// compatible instructions or constants, or just some other regular values.
12101229
static bool areCompatibleCmpOps(Value *BaseOp0, Value *BaseOp1, Value *Op0,
@@ -1259,17 +1278,6 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
12591278
(VL.size() == 2 && InstCnt < 2))
12601279
return InstructionsState::invalid();
12611280

1262-
auto FindInstructionWithOpcode = [&](unsigned Opcode) {
1263-
for (Value *V : VL) {
1264-
if (isa<PoisonValue>(V))
1265-
continue;
1266-
auto *Inst = cast<Instruction>(V);
1267-
if (Inst->getOpcode() == Opcode)
1268-
return Inst;
1269-
}
1270-
llvm_unreachable("Opcode not found.");
1271-
};
1272-
12731281
bool IsCastOp = isa<CastInst>(MainOp);
12741282
bool IsBinOp = isa<BinaryOperator>(MainOp);
12751283
bool IsCmpOp = isa<CmpInst>(MainOp);
@@ -1429,8 +1437,10 @@ static InstructionsState getSameOpcode(ArrayRef<Value *> VL,
14291437
}
14301438

14311439
if (IsBinOp) {
1432-
MainOp = FindInstructionWithOpcode(BinOpHelper.getMainOpcode());
1433-
AltOp = FindInstructionWithOpcode(BinOpHelper.getAltOpcode());
1440+
MainOp = findInstructionWithOpcode(VL, BinOpHelper.getMainOpcode());
1441+
assert(MainOp && "Cannot find MainOp with Opcode from BinOpHelper.");
1442+
AltOp = findInstructionWithOpcode(VL, BinOpHelper.getAltOpcode());
1443+
assert(MainOp && "Cannot find AltOp with Opcode from BinOpHelper.");
14341444
}
14351445
assert((MainOp == AltOp || !allSameOpcode(VL)) &&
14361446
"Incorrect implementation of allSameOpcode.");

0 commit comments

Comments
 (0)