25
25
#define LLVM_TRANSFORMS_VECTORIZE_LOOPVECTORIZATIONPLANNER_H
26
26
27
27
#include " VPlan.h"
28
- #include " llvm/ADT/SmallSet .h"
28
+ #include " VPlanConstantFolder .h"
29
29
#include " llvm/Support/InstructionCost.h"
30
30
31
31
namespace llvm {
@@ -49,6 +49,7 @@ extern cl::opt<unsigned> ForceTargetInstructionCost;
49
49
class VPBuilder {
50
50
VPBasicBlock *BB = nullptr ;
51
51
VPBasicBlock::iterator InsertPt = VPBasicBlock::iterator();
52
+ VPConstantFolder Folder;
52
53
53
54
// / Insert \p VPI in BB at InsertPt if BB is set.
54
55
template <typename T> T *tryInsertInstruction (T *R) {
@@ -69,6 +70,11 @@ class VPBuilder {
69
70
return createInstruction (Opcode, ArrayRef<VPValue *>(Operands), DL, Name);
70
71
}
71
72
73
+ VPValue *getOrAddLiveIn (Value *V) {
74
+ assert (BB && " Expected insertion point to be set" );
75
+ return BB->getPlan ()->getOrAddLiveIn (V);
76
+ }
77
+
72
78
public:
73
79
VPBuilder () = default;
74
80
VPBuilder (VPBasicBlock *InsertBB) { setInsertPoint (InsertBB); }
@@ -183,31 +189,45 @@ class VPBuilder {
183
189
184
190
VPValue *createNot (VPValue *Operand, DebugLoc DL = {},
185
191
const Twine &Name = " " ) {
192
+ if (auto *V = Folder.foldNot (Operand))
193
+ if (BB)
194
+ return getOrAddLiveIn (V);
186
195
return createInstruction (VPInstruction::Not, {Operand}, DL, Name);
187
196
}
188
197
189
198
VPValue *createAnd (VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
190
199
const Twine &Name = " " ) {
200
+ if (auto *V = Folder.foldAnd (LHS, RHS))
201
+ if (BB)
202
+ return getOrAddLiveIn (V);
191
203
return createInstruction (Instruction::BinaryOps::And, {LHS, RHS}, DL, Name);
192
204
}
193
205
194
206
VPValue *createOr (VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
195
207
const Twine &Name = " " ) {
196
-
208
+ if (auto *V = Folder.foldOr (LHS, RHS))
209
+ if (BB)
210
+ return getOrAddLiveIn (V);
197
211
return tryInsertInstruction (new VPInstruction (
198
212
Instruction::BinaryOps::Or, {LHS, RHS},
199
213
VPRecipeWithIRFlags::DisjointFlagsTy (false ), DL, Name));
200
214
}
201
215
202
216
VPValue *createLogicalAnd (VPValue *LHS, VPValue *RHS, DebugLoc DL = {},
203
217
const Twine &Name = " " ) {
218
+ if (auto *V = Folder.foldLogicalAnd (LHS, RHS))
219
+ if (BB)
220
+ return getOrAddLiveIn (V);
204
221
return tryInsertInstruction (
205
222
new VPInstruction (VPInstruction::LogicalAnd, {LHS, RHS}, DL, Name));
206
223
}
207
224
208
225
VPValue *createSelect (VPValue *Cond, VPValue *TrueVal, VPValue *FalseVal,
209
226
DebugLoc DL = {}, const Twine &Name = " " ,
210
227
std::optional<FastMathFlags> FMFs = std::nullopt) {
228
+ if (auto *V = Folder.foldSelect (Cond, TrueVal, FalseVal))
229
+ if (BB)
230
+ return getOrAddLiveIn (V);
211
231
auto *Select =
212
232
FMFs ? new VPInstruction (Instruction::Select, {Cond, TrueVal, FalseVal},
213
233
*FMFs, DL, Name)
@@ -223,17 +243,26 @@ class VPBuilder {
223
243
DebugLoc DL = {}, const Twine &Name = " " ) {
224
244
assert (Pred >= CmpInst::FIRST_ICMP_PREDICATE &&
225
245
Pred <= CmpInst::LAST_ICMP_PREDICATE && " invalid predicate" );
246
+ if (auto *V = Folder.foldCmp (Pred, A, B))
247
+ if (BB)
248
+ return getOrAddLiveIn (V);
226
249
return tryInsertInstruction (
227
250
new VPInstruction (Instruction::ICmp, Pred, A, B, DL, Name));
228
251
}
229
252
230
- VPInstruction *createPtrAdd (VPValue *Ptr, VPValue *Offset, DebugLoc DL = {},
231
- const Twine &Name = " " ) {
253
+ VPValue *createPtrAdd (VPValue *Ptr, VPValue *Offset, DebugLoc DL = {},
254
+ const Twine &Name = " " ) {
255
+ if (auto *V = Folder.foldPtrAdd (Ptr, Offset, GEPNoWrapFlags::none ()))
256
+ if (BB)
257
+ return getOrAddLiveIn (V);
232
258
return tryInsertInstruction (
233
259
new VPInstruction (Ptr, Offset, GEPNoWrapFlags::none (), DL, Name));
234
260
}
235
261
VPValue *createInBoundsPtrAdd (VPValue *Ptr, VPValue *Offset, DebugLoc DL = {},
236
262
const Twine &Name = " " ) {
263
+ if (auto *V = Folder.foldPtrAdd (Ptr, Offset, GEPNoWrapFlags::inBounds ()))
264
+ if (BB)
265
+ return getOrAddLiveIn (V);
237
266
return tryInsertInstruction (
238
267
new VPInstruction (Ptr, Offset, GEPNoWrapFlags::inBounds (), DL, Name));
239
268
}
@@ -249,14 +278,20 @@ class VPBuilder {
249
278
new VPDerivedIVRecipe (Kind, FPBinOp, Start, Current, Step, Name));
250
279
}
251
280
252
- VPScalarCastRecipe *createScalarCast (Instruction::CastOps Opcode, VPValue *Op,
253
- Type *ResultTy, DebugLoc DL) {
281
+ VPValue *createScalarCast (Instruction::CastOps Opcode, VPValue *Op,
282
+ Type *ResultTy, DebugLoc DL) {
283
+ if (auto *V = Folder.foldCast (Opcode, Op, ResultTy))
284
+ if (BB)
285
+ return getOrAddLiveIn (V);
254
286
return tryInsertInstruction (
255
287
new VPScalarCastRecipe (Opcode, Op, ResultTy, DL));
256
288
}
257
289
258
- VPWidenCastRecipe *createWidenCast (Instruction::CastOps Opcode, VPValue *Op,
259
- Type *ResultTy) {
290
+ VPValue *createWidenCast (Instruction::CastOps Opcode, VPValue *Op,
291
+ Type *ResultTy) {
292
+ if (auto *V = Folder.foldCast (Opcode, Op, ResultTy))
293
+ if (BB)
294
+ return getOrAddLiveIn (V);
260
295
return tryInsertInstruction (new VPWidenCastRecipe (Opcode, Op, ResultTy));
261
296
}
262
297
0 commit comments