@@ -6076,38 +6076,36 @@ class HorizontalReduction {
6076
6076
6077
6077
explicit operator bool () const { return Opcode; }
6078
6078
6079
- // / Get the index of the first operand.
6080
- unsigned getFirstOperandIndex () const {
6081
- assert (!!*this && " The opcode is not set." );
6079
+ // / Return true if this operation is any kind of minimum or maximum.
6080
+ bool isMinMax () const {
6082
6081
switch (Kind) {
6082
+ case RK_Arithmetic:
6083
+ return false ;
6083
6084
case RK_Min:
6084
- case RK_UMin:
6085
6085
case RK_Max:
6086
+ case RK_UMin:
6086
6087
case RK_UMax:
6087
- return 1 ;
6088
- case RK_Arithmetic:
6088
+ return true ;
6089
6089
case RK_None:
6090
6090
break ;
6091
6091
}
6092
- return 0 ;
6092
+ llvm_unreachable (" Reduction kind is not set" );
6093
+ }
6094
+
6095
+ // / Get the index of the first operand.
6096
+ unsigned getFirstOperandIndex () const {
6097
+ assert (!!*this && " The opcode is not set." );
6098
+ // We allow calling this before 'Kind' is set, so handle that specially.
6099
+ if (Kind == RK_None)
6100
+ return 0 ;
6101
+ return isMinMax () ? 1 : 0 ;
6093
6102
}
6094
6103
6095
6104
// / Total number of operands in the reduction operation.
6096
6105
unsigned getNumberOfOperands () const {
6097
6106
assert (Kind != RK_None && !!*this && LHS && RHS &&
6098
6107
" Expected reduction operation." );
6099
- switch (Kind) {
6100
- case RK_Arithmetic:
6101
- return 2 ;
6102
- case RK_Min:
6103
- case RK_UMin:
6104
- case RK_Max:
6105
- case RK_UMax:
6106
- return 3 ;
6107
- case RK_None:
6108
- break ;
6109
- }
6110
- llvm_unreachable (" Reduction kind is not set" );
6108
+ return isMinMax () ? 3 : 2 ;
6111
6109
}
6112
6110
6113
6111
// / Checks if the operation has the same parent as \p P.
@@ -6116,79 +6114,46 @@ class HorizontalReduction {
6116
6114
" Expected reduction operation." );
6117
6115
if (!IsRedOp)
6118
6116
return I->getParent () == P;
6119
- switch (Kind) {
6120
- case RK_Arithmetic:
6121
- // Arithmetic reduction operation must be used once only.
6122
- return I->getParent () == P;
6123
- case RK_Min:
6124
- case RK_UMin:
6125
- case RK_Max:
6126
- case RK_UMax: {
6117
+ if (isMinMax ()) {
6127
6118
// SelectInst must be used twice while the condition op must have single
6128
6119
// use only.
6129
6120
auto *Cmp = cast<Instruction>(cast<SelectInst>(I)->getCondition ());
6130
6121
return I->getParent () == P && Cmp && Cmp->getParent () == P;
6131
6122
}
6132
- case RK_None:
6133
- break ;
6134
- }
6135
- llvm_unreachable (" Reduction kind is not set" );
6123
+ // Arithmetic reduction operation must be used once only.
6124
+ return I->getParent () == P;
6136
6125
}
6126
+
6137
6127
// / Expected number of uses for reduction operations/reduced values.
6138
6128
bool hasRequiredNumberOfUses (Instruction *I, bool IsReductionOp) const {
6139
6129
assert (Kind != RK_None && !!*this && LHS && RHS &&
6140
6130
" Expected reduction operation." );
6141
- switch (Kind) {
6142
- case RK_Arithmetic:
6143
- return I->hasOneUse ();
6144
- case RK_Min:
6145
- case RK_UMin:
6146
- case RK_Max:
6147
- case RK_UMax:
6131
+ if (isMinMax ())
6148
6132
return I->hasNUses (2 ) &&
6149
6133
(!IsReductionOp ||
6150
6134
cast<SelectInst>(I)->getCondition ()->hasOneUse ());
6151
- case RK_None:
6152
- break ;
6153
- }
6154
- llvm_unreachable (" Reduction kind is not set" );
6135
+ return I->hasOneUse ();
6155
6136
}
6156
6137
6157
6138
// / Initializes the list of reduction operations.
6158
6139
void initReductionOps (ReductionOpsListType &ReductionOps) {
6159
6140
assert (Kind != RK_None && !!*this && LHS && RHS &&
6160
6141
" Expected reduction operation." );
6161
- switch (Kind) {
6162
- case RK_Arithmetic:
6163
- ReductionOps.assign (1 , ReductionOpsType ());
6164
- break ;
6165
- case RK_Min:
6166
- case RK_UMin:
6167
- case RK_Max:
6168
- case RK_UMax:
6142
+ if (isMinMax ())
6169
6143
ReductionOps.assign (2 , ReductionOpsType ());
6170
- break ;
6171
- case RK_None:
6172
- llvm_unreachable (" Reduction kind is not set" );
6173
- }
6144
+ else
6145
+ ReductionOps.assign (1 , ReductionOpsType ());
6174
6146
}
6147
+
6175
6148
// / Add all reduction operations for the reduction instruction \p I.
6176
6149
void addReductionOps (Instruction *I, ReductionOpsListType &ReductionOps) {
6177
6150
assert (Kind != RK_None && !!*this && LHS && RHS &&
6178
6151
" Expected reduction operation." );
6179
- switch (Kind) {
6180
- case RK_Arithmetic:
6181
- ReductionOps[0 ].emplace_back (I);
6182
- break ;
6183
- case RK_Min:
6184
- case RK_UMin:
6185
- case RK_Max:
6186
- case RK_UMax:
6152
+ if (isMinMax ()) {
6187
6153
ReductionOps[0 ].emplace_back (cast<SelectInst>(I)->getCondition ());
6188
6154
ReductionOps[1 ].emplace_back (I);
6189
- break ;
6190
- case RK_None:
6191
- llvm_unreachable (" Reduction kind is not set" );
6155
+ } else {
6156
+ ReductionOps[0 ].emplace_back (I);
6192
6157
}
6193
6158
}
6194
6159
@@ -6246,18 +6211,7 @@ class HorizontalReduction {
6246
6211
Value *getLHS () const { return LHS; }
6247
6212
Value *getRHS () const { return RHS; }
6248
6213
Type *getConditionType () const {
6249
- switch (Kind) {
6250
- case RK_Arithmetic:
6251
- return nullptr ;
6252
- case RK_Min:
6253
- case RK_Max:
6254
- case RK_UMin:
6255
- case RK_UMax:
6256
- return CmpInst::makeCmpResultType (LHS->getType ());
6257
- case RK_None:
6258
- break ;
6259
- }
6260
- llvm_unreachable (" Reduction kind is not set" );
6214
+ return isMinMax () ? CmpInst::makeCmpResultType (LHS->getType ()) : nullptr ;
6261
6215
}
6262
6216
6263
6217
// / Creates reduction operation with the current opcode with the IR flags
0 commit comments