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