Skip to content

Commit 83da21a

Browse files
committed
[VPlan] Generalize type inference for binary VPInstructions (NFC).
Generalize logic to set the result type for ops where the result type and the types of all operands match. Use it to support any unary and binops.
1 parent d474976 commit 83da21a

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "VPlanAnalysis.h"
1010
#include "VPlan.h"
1111
#include "llvm/ADT/TypeSwitch.h"
12+
#include "llvm/IR/Instruction.h"
1213

1314
using namespace llvm;
1415

@@ -26,7 +27,24 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPBlendRecipe *R) {
2627
}
2728

2829
Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
29-
switch (R->getOpcode()) {
30+
// Set the result type from the first operand, check if the types for all
31+
// other operands match and cache them.
32+
auto SetResultTyFromOp = [this, R]() {
33+
Type *ResTy = inferScalarType(R->getOperand(0));
34+
for (unsigned Op = 1; Op != R->getNumOperands(); ++Op) {
35+
VPValue *OtherV = R->getOperand(Op);
36+
assert(inferScalarType(OtherV) == ResTy &&
37+
"different types inferred for different operands");
38+
CachedTypes[OtherV] = ResTy;
39+
}
40+
return ResTy;
41+
};
42+
43+
unsigned Opcode = R->getOpcode();
44+
if (Instruction::isBinaryOp(Opcode) || Instruction::isUnaryOp(Opcode))
45+
return SetResultTyFromOp();
46+
47+
switch (Opcode) {
3048
case Instruction::Select: {
3149
Type *ResTy = inferScalarType(R->getOperand(1));
3250
VPValue *OtherV = R->getOperand(2);
@@ -35,28 +53,16 @@ Type *VPTypeAnalysis::inferScalarTypeForRecipe(const VPInstruction *R) {
3553
CachedTypes[OtherV] = ResTy;
3654
return ResTy;
3755
}
38-
case Instruction::Or:
3956
case Instruction::ICmp:
40-
case VPInstruction::FirstOrderRecurrenceSplice: {
41-
Type *ResTy = inferScalarType(R->getOperand(0));
42-
VPValue *OtherV = R->getOperand(1);
43-
assert(inferScalarType(OtherV) == ResTy &&
44-
"different types inferred for different operands");
45-
CachedTypes[OtherV] = ResTy;
46-
return ResTy;
47-
}
57+
case VPInstruction::FirstOrderRecurrenceSplice:
58+
case VPInstruction::Not:
59+
return SetResultTyFromOp();
4860
case VPInstruction::ExtractFromEnd: {
4961
Type *BaseTy = inferScalarType(R->getOperand(0));
5062
if (auto *VecTy = dyn_cast<VectorType>(BaseTy))
5163
return VecTy->getElementType();
5264
return BaseTy;
5365
}
54-
case VPInstruction::Not: {
55-
Type *ResTy = inferScalarType(R->getOperand(0));
56-
assert(IntegerType::get(Ctx, 1) == ResTy &&
57-
"unexpected scalar type inferred for operand");
58-
return ResTy;
59-
}
6066
case VPInstruction::LogicalAnd:
6167
return IntegerType::get(Ctx, 1);
6268
case VPInstruction::PtrAdd:

0 commit comments

Comments
 (0)