LLVM 23.0.0git
VPlanUtils.cpp
Go to the documentation of this file.
1//===- VPlanUtils.cpp - VPlan-related utilities ---------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "VPlanUtils.h"
11#include "VPlanAnalysis.h"
12#include "VPlanCFG.h"
13#include "VPlanDominatorTree.h"
14#include "VPlanPatternMatch.h"
15#include "llvm/ADT/TypeSwitch.h"
19#include "llvm/IR/Dominators.h"
21
22using namespace llvm;
23using namespace llvm::VPlanPatternMatch;
24using namespace llvm::SCEVPatternMatch;
25
27 return all_of(Def->users(),
28 [Def](const VPUser *U) { return U->usesFirstLaneOnly(Def); });
29}
30
32 return all_of(Def->users(),
33 [Def](const VPUser *U) { return U->usesFirstPartOnly(Def); });
34}
35
37 return all_of(Def->users(),
38 [Def](const VPUser *U) { return U->usesScalars(Def); });
39}
40
42 if (auto *E = dyn_cast<SCEVConstant>(Expr))
43 return Plan.getOrAddLiveIn(E->getValue());
44 // Skip SCEV expansion if Expr is a SCEVUnknown wrapping a non-instruction
45 // value. Otherwise the value may be defined in a loop and using it directly
46 // will break LCSSA form. The SCEV expansion takes care of preserving LCSSA
47 // form.
48 auto *U = dyn_cast<SCEVUnknown>(Expr);
49 if (U && !isa<Instruction>(U->getValue()))
50 return Plan.getOrAddLiveIn(U->getValue());
51 auto *Expanded = new VPExpandSCEVRecipe(Expr);
52 VPBasicBlock *EntryVPBB = Plan.getEntry();
53 auto Iter = EntryVPBB->getFirstNonPhi();
54 while (Iter != EntryVPBB->end() && isa<VPIRInstruction>(*Iter))
55 ++Iter;
56 EntryVPBB->insert(Expanded, Iter);
57 return Expanded;
58}
59
60bool vputils::isHeaderMask(const VPValue *V, const VPlan &Plan) {
62 return true;
63
64 VPValue *A, *B;
65
66 auto m_CanonicalScalarIVSteps = m_ScalarIVSteps(
69 m_One(), m_Specific(&Plan.getVF()));
70
71 // A wide canonical IV is either an explicit VPWidenCanonicalIVRecipe or a
72 // canonical VPWidenIntOrFpInductionRecipe.
73 auto m_WideCanonicalIV =
75
77 return B == Plan.getTripCount() &&
78 (match(A, m_CanonicalScalarIVSteps) || match(A, m_WideCanonicalIV));
79
80 // For scalar plans, the header mask uses the scalar steps.
81 if (match(V, m_ICmp(m_CanonicalScalarIVSteps,
83 assert(Plan.hasScalarVFOnly() &&
84 "Non-scalar VF using scalar IV steps for header mask?");
85 return true;
86 }
87
88 auto MaskMatch = m_ICmp(m_VPValue(A), m_VPValue(B));
89 if (match(V, m_CombineOr(MaskMatch, m_Reverse(MaskMatch))))
90 return match(A, m_WideCanonicalIV) && B == Plan.getBackedgeTakenCount();
91
92 return match(
95}
96
97/// Returns true if \p R propagates poison from any operand to its result.
101 [](const VPRecipeBase *) { return true; })
102 .Case([](const VPReplicateRecipe *Rep) {
103 // GEP and casts propagate poison from all operands.
104 unsigned Opcode = Rep->getOpcode();
105 return Opcode == Instruction::GetElementPtr ||
106 Instruction::isCast(Opcode);
107 })
108 .Default([](const VPRecipeBase *) { return false; });
109}
110
111/// Returns true if \p V being poison is guaranteed to trigger UB because it
112/// propagates to the address of a memory recipe.
113static bool poisonGuaranteesUB(const VPValue *V) {
116
117 Worklist.push_back(V);
118
119 while (!Worklist.empty()) {
120 const VPValue *Current = Worklist.pop_back_val();
121 if (!Visited.insert(Current).second)
122 continue;
123
124 for (VPUser *U : Current->users()) {
125 // Check if Current is used as an address operand for load/store.
127 if (MemR->getAddr() == Current)
128 return true;
129 continue;
130 }
131 if (auto *Rep = dyn_cast<VPReplicateRecipe>(U)) {
132 unsigned Opcode = Rep->getOpcode();
133 if ((Opcode == Instruction::Load && Rep->getOperand(0) == Current) ||
134 (Opcode == Instruction::Store && Rep->getOperand(1) == Current))
135 return true;
136 }
137
138 // Check if poison propagates through this recipe to any of its users.
139 auto *R = cast<VPRecipeBase>(U);
140 for (const VPValue *Op : R->operands()) {
141 if (Op == Current && propagatesPoisonFromRecipeOp(R)) {
142 Worklist.push_back(R->getVPSingleValue());
143 break;
144 }
145 }
146 }
147 }
148
149 return false;
150}
151
153 // Like IR stripPointerCasts, look through GEPs with all-zero indices and
154 // casts to find a root GEP VPInstruction.
155 while (auto *PtrVPI = dyn_cast<VPInstruction>(Ptr)) {
156 unsigned Opcode = PtrVPI->getOpcode();
157 if (Opcode == Instruction::GetElementPtr) {
158 if (any_of(drop_begin(PtrVPI->operands()),
159 [](VPValue *Op) { return !match(Op, m_ZeroInt()); }))
160 return PtrVPI->getGEPNoWrapFlags();
161 Ptr = PtrVPI->getOperand(0);
162 continue;
163 }
164 if (Opcode != Instruction::BitCast && Opcode != Instruction::AddrSpaceCast)
165 break;
166 Ptr = PtrVPI->getOperand(0);
167 }
168 return GEPNoWrapFlags::none();
169}
170
173 const Loop *L) {
174 ScalarEvolution &SE = *PSE.getSE();
176 Value *LiveIn = V->getUnderlyingValue();
177 if (LiveIn && SE.isSCEVable(LiveIn->getType()))
178 return SE.getSCEV(LiveIn);
179 return SE.getCouldNotCompute();
180 }
181
182 if (auto *RV = dyn_cast<VPRegionValue>(V)) {
183 assert(RV == RV->getDefiningRegion()->getCanonicalIV() &&
184 "RegionValue must be canonical IV");
185 if (!L)
186 return SE.getCouldNotCompute();
187 return SE.getAddRecExpr(SE.getZero(RV->getType()), SE.getOne(RV->getType()),
189 }
190
191 // Helper to create SCEVs for binary and unary operations.
192 auto CreateSCEV = [&](ArrayRef<VPValue *> Ops,
193 function_ref<const SCEV *(ArrayRef<SCEVUse>)> CreateFn)
194 -> const SCEV * {
196 for (VPValue *Op : Ops) {
197 const SCEV *S = getSCEVExprForVPValue(Op, PSE, L);
199 return SE.getCouldNotCompute();
200 SCEVOps.push_back(S);
201 }
202 return PSE.getPredicatedSCEV(CreateFn(SCEVOps));
203 };
204
205 VPValue *LHSVal, *RHSVal;
206 if (match(V, m_Add(m_VPValue(LHSVal), m_VPValue(RHSVal))))
207 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
208 return SE.getAddExpr(Ops[0], Ops[1], SCEV::FlagAnyWrap, 0);
209 });
210 if (match(V, m_Sub(m_VPValue(LHSVal), m_VPValue(RHSVal))))
211 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
212 return SE.getMinusSCEV(Ops[0], Ops[1], SCEV::FlagAnyWrap, 0);
213 });
214 if (match(V, m_Not(m_VPValue(LHSVal)))) {
215 // not X = xor X, -1 = -1 - X
216 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
217 return SE.getMinusSCEV(SE.getMinusOne(Ops[0]->getType()), Ops[0]);
218 });
219 }
220 if (match(V, m_Mul(m_VPValue(LHSVal), m_VPValue(RHSVal))))
221 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
222 return SE.getMulExpr(Ops[0], Ops[1], SCEV::FlagAnyWrap, 0);
223 });
224 // Handle shl by constant: x << c is equivalent to x * (1 << c). A shift
225 // amount >= the bit width produces poison; do not rewrite it, as
226 // getPowerOfTwo requires the power to be in range.
227 uint64_t ShiftAmt;
228 if (match(V, m_Shl(m_VPValue(LHSVal), m_ConstantInt(ShiftAmt))) &&
229 ShiftAmt < LHSVal->getScalarType()->getScalarSizeInBits())
230 return CreateSCEV(LHSVal, [&](ArrayRef<SCEVUse> Ops) {
231 return SE.getMulExpr(Ops[0],
232 SE.getPowerOfTwo(Ops[0]->getType(), ShiftAmt));
233 });
234 if (match(V, m_LShr(m_VPValue(LHSVal), m_ConstantInt(ShiftAmt)))) {
235 Type *Ty = V->getScalarType();
236 if (ShiftAmt < SE.getTypeSizeInBits(Ty))
237 return CreateSCEV(LHSVal, [&](ArrayRef<SCEVUse> Ops) {
238 return SE.getUDivExpr(Ops[0], SE.getPowerOfTwo(Ty, ShiftAmt));
239 });
240 }
241 if (match(V, m_UDiv(m_VPValue(LHSVal), m_VPValue(RHSVal))))
242 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
243 return SE.getUDivExpr(Ops[0], Ops[1]);
244 });
245 if (match(V, m_URem(m_VPValue(LHSVal), m_VPValue(RHSVal))))
246 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
247 return SE.getURemExpr(Ops[0], Ops[1]);
248 });
249 // Handle AND with constant mask: x & (2^n - 1) can be represented as x % 2^n.
250 const APInt *Mask;
251 if (match(V, m_c_BinaryAnd(m_VPValue(LHSVal), m_APInt(Mask))) &&
252 (*Mask + 1).isPowerOf2())
253 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
254 return SE.getURemExpr(Ops[0], SE.getConstant(*Mask + 1));
255 });
256 if (match(V, m_Trunc(m_VPValue(LHSVal)))) {
257 Type *DestTy = V->getScalarType();
258 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
259 return SE.getTruncateExpr(Ops[0], DestTy);
260 });
261 }
262 if (match(V, m_ZExt(m_VPValue(LHSVal)))) {
263 Type *DestTy = V->getScalarType();
264 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
265 return SE.getZeroExtendExpr(Ops[0], DestTy);
266 });
267 }
268 if (match(V, m_SExt(m_VPValue(LHSVal)))) {
269 Type *DestTy = V->getScalarType();
270
271 // Mirror SCEV's createSCEV handling for sext(sub nsw): push sign extension
272 // onto the operands before computing the subtraction.
273 VPValue *SubLHS, *SubRHS;
274 auto *SubR = dyn_cast<VPRecipeWithIRFlags>(LHSVal);
275 if (match(LHSVal, m_Sub(m_VPValue(SubLHS), m_VPValue(SubRHS))) && SubR &&
276 SubR->hasNoSignedWrap() && poisonGuaranteesUB(LHSVal)) {
277 const SCEV *V1 = getSCEVExprForVPValue(SubLHS, PSE, L);
278 const SCEV *V2 = getSCEVExprForVPValue(SubRHS, PSE, L);
280 return SE.getMinusSCEV(SE.getSignExtendExpr(V1, DestTy),
281 SE.getSignExtendExpr(V2, DestTy), SCEV::FlagNSW);
282 }
283
284 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
285 return SE.getSignExtendExpr(Ops[0], DestTy);
286 });
287 }
288 if (match(V,
290 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
291 return SE.getUMaxExpr(Ops[0], Ops[1]);
292 });
293 if (match(V,
295 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
296 return SE.getSMaxExpr(Ops[0], Ops[1]);
297 });
298 if (match(V,
300 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
301 return SE.getUMinExpr(Ops[0], Ops[1]);
302 });
303 if (match(V,
305 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
306 return SE.getSMinExpr(Ops[0], Ops[1]);
307 });
309 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
310 // is_int_min_poison is local to this intrinsic: poison on INT_MIN is
311 // not proof that the input is never INT_MIN, nor that poison reaches
312 // UB. Do not translate it to SCEV's global IsNSW flag.
313 return SE.getAbsExpr(Ops[0], /*IsNSW=*/false);
314 });
315
317 Type *SourceElementType;
318 if (match(V, m_GetElementPtr(SourceElementType, Ops))) {
319 return CreateSCEV(Ops, [&](ArrayRef<SCEVUse> Ops) {
320 return SE.getGEPExpr(Ops.front(), Ops.drop_front(), SourceElementType);
321 });
322 }
323
324 // TODO: Support constructing SCEVs for more recipes as needed.
325 const VPRecipeBase *DefR = V->getDefiningRecipe();
326 const SCEV *Expr =
328 .Case([](const VPExpandSCEVRecipe *R) { return R->getSCEV(); })
329 .Case([&SE, &PSE, L](const VPWidenIntOrFpInductionRecipe *R) {
330 const SCEV *Step = getSCEVExprForVPValue(R->getStepValue(), PSE, L);
331 if (!L || isa<SCEVCouldNotCompute>(Step))
332 return SE.getCouldNotCompute();
333 const SCEV *Start =
334 getSCEVExprForVPValue(R->getStartValue(), PSE, L);
335 const SCEV *AddRec =
336 SE.getAddRecExpr(Start, Step, L, SCEV::FlagAnyWrap);
337 if (R->getTruncInst())
338 return SE.getTruncateExpr(AddRec, R->getScalarType());
339 return AddRec;
340 })
341 .Case([&SE, &PSE, L](const VPWidenPointerInductionRecipe *R) {
342 const SCEV *Start =
343 getSCEVExprForVPValue(R->getStartValue(), PSE, L);
344 if (!L || isa<SCEVCouldNotCompute>(Start))
345 return SE.getCouldNotCompute();
346 const SCEV *Step = getSCEVExprForVPValue(R->getStepValue(), PSE, L);
347 if (isa<SCEVCouldNotCompute>(Step))
348 return SE.getCouldNotCompute();
349 return SE.getAddRecExpr(Start, Step, L, SCEV::FlagAnyWrap);
350 })
351 .Case([&SE, &PSE, L](const VPDerivedIVRecipe *R) {
352 const SCEV *Start = getSCEVExprForVPValue(R->getOperand(0), PSE, L);
353 const SCEV *IV = getSCEVExprForVPValue(R->getOperand(1), PSE, L);
354 const SCEV *Scale = getSCEVExprForVPValue(R->getOperand(2), PSE, L);
355 if (any_of(ArrayRef({Start, IV, Scale}),
357 return SE.getCouldNotCompute();
358
359 return SE.getAddExpr(
360 SE.getTruncateOrSignExtend(Start, IV->getType()),
361 SE.getMulExpr(
362 IV, SE.getTruncateOrSignExtend(Scale, IV->getType())));
363 })
364 .Case([&SE, &PSE, L](const VPScalarIVStepsRecipe *R) {
365 const SCEV *IV = getSCEVExprForVPValue(R->getOperand(0), PSE, L);
366 const SCEV *Step = getSCEVExprForVPValue(R->getOperand(1), PSE, L);
368 return SE.getCouldNotCompute();
369 return SE.getTruncateOrSignExtend(IV, Step->getType());
370 })
371 .Default(
372 [&SE](const VPRecipeBase *) { return SE.getCouldNotCompute(); });
373
374 return PSE.getPredicatedSCEV(Expr);
375}
376
378 const Loop *L) {
379 // If address is an SCEVAddExpr, we require that all operands must be either
380 // be invariant or a (possibly sign-extend) affine AddRec.
381 if (auto *PtrAdd = dyn_cast<SCEVAddExpr>(Addr)) {
382 return all_of(PtrAdd->operands(), [&SE, L](const SCEV *Op) {
383 return SE.isLoopInvariant(Op, L) ||
384 match(Op, m_scev_SExt(m_scev_AffineAddRec(m_SCEV(), m_SCEV()))) ||
385 match(Op, m_scev_AffineAddRec(m_SCEV(), m_SCEV()));
386 });
387 }
388
389 // Otherwise, check if address is loop invariant or an affine add recurrence.
390 return SE.isLoopInvariant(Addr, L) ||
392}
393
394/// Returns true if \p Opcode preserves uniformity, i.e., if all operands are
395/// uniform, the result will also be uniform.
396static bool preservesUniformity(unsigned Opcode) {
397 if (Instruction::isBinaryOp(Opcode) || Instruction::isCast(Opcode))
398 return true;
399 switch (Opcode) {
400 case Instruction::Freeze:
401 case Instruction::GetElementPtr:
402 case Instruction::ICmp:
403 case Instruction::FCmp:
404 case Instruction::Select:
409 return true;
410 default:
411 return false;
412 }
413}
414
416 // Live-in, symbolic and region-values represent single-scalar values.
418 return true;
419
420 if (auto *Rep = dyn_cast<VPReplicateRecipe>(VPV)) {
421 const VPRegionBlock *RegionOfR = Rep->getRegion();
422 // Don't consider recipes in replicate regions as uniform yet; their first
423 // lane cannot be accessed when executing the replicate region for other
424 // lanes.
425 if (RegionOfR && RegionOfR->isReplicator())
426 return false;
427 return Rep->isSingleScalar() || (preservesUniformity(Rep->getOpcode()) &&
428 all_of(Rep->operands(), isSingleScalar));
429 }
432 if (auto *WidenR = dyn_cast<VPWidenRecipe>(VPV)) {
433 return preservesUniformity(WidenR->getOpcode()) &&
434 all_of(WidenR->operands(), isSingleScalar);
435 }
436 if (auto *VPI = dyn_cast<VPInstruction>(VPV))
437 return VPI->isSingleScalar() || VPI->isVectorToScalar() ||
438 (preservesUniformity(VPI->getOpcode()) &&
439 all_of(VPI->operands(), isSingleScalar));
440 if (auto *RR = dyn_cast<VPReductionRecipe>(VPV))
441 return !RR->isPartialReduction();
443 VPV))
444 return true;
445 if (auto *Expr = dyn_cast<VPExpressionRecipe>(VPV))
446 return Expr->isVectorToScalar();
447
448 // VPExpandSCEVRecipes must be placed in the entry and are always uniform.
449 return isa<VPExpandSCEVRecipe>(VPV);
450}
451
453 // Live-ins and region values are uniform.
455 return true;
456
457 const VPRecipeBase *R = V->getDefiningRecipe();
458 const VPBasicBlock *VPBB = R ? R->getParent() : nullptr;
459 const VPlan *Plan = VPBB ? VPBB->getPlan() : nullptr;
460 if (VPBB) {
461 if ((VPBB == Plan->getVectorPreheader() || VPBB == Plan->getEntry())) {
462 if (match(V->getDefiningRecipe(),
464 return false;
465 return all_of(R->operands(), isUniformAcrossVFsAndUFs);
466 }
467 }
468
470 .Case([](const VPDerivedIVRecipe *R) { return true; })
471 .Case([](const VPReplicateRecipe *R) {
472 // Be conservative about side-effects, except for the
473 // known-side-effecting assumes and stores, which we know will be
474 // uniform.
475 return R->isSingleScalar() &&
476 (!R->mayHaveSideEffects() ||
477 isa<AssumeInst, StoreInst>(R->getUnderlyingInstr())) &&
478 all_of(R->operands(), isUniformAcrossVFsAndUFs);
479 })
480 .Case([](const VPWidenRecipe *R) {
481 return preservesUniformity(R->getOpcode()) &&
482 all_of(R->operands(), isUniformAcrossVFsAndUFs);
483 })
484 .Case([](const VPPhi *) {
485 // Bail out on VPPhi, as we can end up in infinite cycles.
486 return false;
487 })
488 .Case([](const VPInstruction *VPI) {
489 return (VPI->isSingleScalar() || VPI->isVectorToScalar() ||
492 })
493 .Case([](const VPWidenCastRecipe *R) {
494 // A cast is uniform according to its operand.
495 return isUniformAcrossVFsAndUFs(R->getOperand(0));
496 })
497 .Default([](const VPRecipeBase *) { // A value is considered non-uniform
498 // unless proven otherwise.
499 return false;
500 });
501}
502
504 auto DepthFirst = vp_depth_first_shallow(Plan.getEntry());
505 auto I = find_if(DepthFirst, [&VPDT](VPBlockBase *VPB) {
506 return VPBlockUtils::isHeader(VPB, VPDT);
507 });
508 return I == DepthFirst.end() ? nullptr : cast<VPBasicBlock>(*I);
509}
510
512 if (!R)
513 return 1;
514 if (auto *RR = dyn_cast<VPReductionPHIRecipe>(R))
515 return RR->getVFScaleFactor();
516 if (auto *RR = dyn_cast<VPReductionRecipe>(R))
517 return RR->getVFScaleFactor();
518 if (auto *ER = dyn_cast<VPExpressionRecipe>(R))
519 return ER->getVFScaleFactor();
520 assert(
523 "getting scaling factor of reduction-start-vector not implemented yet");
524 return 1;
525}
526
527bool vputils::cannotHoistOrSinkRecipe(const VPRecipeBase &R, bool Sinking) {
528 // Assumes don't alias anything or throw; as long as they're guaranteed to
529 // execute, they're safe to hoist. They should however not be sunk, as it
530 // would destroy information.
532 return Sinking;
533 if (R.mayHaveSideEffects() || R.mayReadFromMemory() || R.isPhi())
534 return true;
535 // Allocas cannot be hoisted.
536 auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
537 return RepR && RepR->getOpcode() == Instruction::Alloca;
538}
539
541 if (VPValue *AliasMask = findIncomingAliasMask(Plan)) {
542 assert(match(AliasMask->getSingleUser(),
543 m_c_BinaryAnd(m_VPValue(), m_Specific(AliasMask))) &&
544 "AliasMask must only be used with the original header mask");
545 return cast<VPSingleDefRecipe>(AliasMask->getSingleUser());
546 }
547
548 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
549 SmallVector<VPValue *> WideCanonicalIVs;
550 auto *WideCanonicalIV =
552 assert(count_if(LoopRegion->getCanonicalIV()->users(),
554 "Must have at most one VPWideCanonicalIVRecipe");
555 if (WideCanonicalIV)
556 WideCanonicalIVs.push_back(WideCanonicalIV);
557
558 // Also include VPWidenIntOrFpInductionRecipes that represent a widened
559 // version of the canonical induction.
560 VPBasicBlock *HeaderVPBB = LoopRegion->getEntryBasicBlock();
561 for (VPRecipeBase &Phi : HeaderVPBB->phis()) {
563 if (match(&Phi, m_CanonicalWidenIV(WidenIV)))
564 WideCanonicalIVs.push_back(WidenIV);
565 }
566
567 // Walk users of wide canonical IVs and find the single compare of the form
568 // (ICMP_ULE, WideCanonicalIV, backedge-taken-count).
569 VPSingleDefRecipe *HeaderMask = nullptr;
570 for (auto *Wide : WideCanonicalIVs) {
571 for (VPUser *U : Wide->users()) {
572 auto *VPI = dyn_cast<VPInstruction>(U);
573 if (!VPI || !vputils::isHeaderMask(VPI, Plan))
574 continue;
575
576 assert(VPI->getOperand(0) == Wide &&
577 "WidenCanonicalIV must be the first operand of the compare");
578 assert(!HeaderMask && "Multiple header masks found?");
579 HeaderMask = VPI;
580 }
581 }
582
583 for (VPRecipeBase &R : LoopRegion->getEntryBasicBlock()->phis()) {
584 auto *Def = cast<VPSingleDefRecipe>(&R);
585 if (vputils::isHeaderMask(Def, Plan)) {
586 assert(!HeaderMask && "Multiple header masks found?");
587 HeaderMask = Def;
588 }
589 }
590
591 return HeaderMask;
592}
593
596 VPBasicBlock *LastBB) {
597 assert(FirstBB->getParent() == LastBB->getParent() &&
598 "FirstBB and LastBB from different regions");
599#ifndef NDEBUG
600 bool InSingleSuccChain = false;
601 for (VPBlockBase *Succ = FirstBB; Succ; Succ = Succ->getSingleSuccessor())
602 InSingleSuccChain |= (Succ == LastBB);
603 assert(InSingleSuccChain &&
604 "LastBB unreachable from FirstBB in single-successor chain");
605#endif
606 auto Blocks = to_vector(
608 auto *LastIt = find(Blocks, LastBB);
609 assert(LastIt != Blocks.end() &&
610 "LastBB unreachable from FirstBB in depth-first traversal");
611 Blocks.erase(std::next(LastIt), Blocks.end());
612 return Blocks;
613}
614
616 for (VPRecipeBase &R : *Plan.getVectorPreheader())
618 return cast<VPInstruction>(&R);
619 return nullptr;
620}
621
623 const VPDominatorTree &VPDT) {
624 auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
625 if (!VPBB)
626 return false;
627
628 // If VPBB is in a region R, VPBB is a loop header if R is a loop region with
629 // VPBB as its entry, i.e., free of predecessors.
630 if (auto *R = VPBB->getParent())
631 return !R->isReplicator() && !VPBB->hasPredecessors();
632
633 // A header dominates its second predecessor (the latch), with the other
634 // predecessor being the preheader
635 return VPB->getPredecessors().size() == 2 &&
636 VPDT.dominates(VPB, VPB->getPredecessors()[1]);
637}
638
640 const VPDominatorTree &VPDT) {
641 // A latch has a header as its last successor, with its other successors
642 // leaving the loop. A preheader OTOH has a header as its first (and only)
643 // successor.
644 return VPB->getNumSuccessors() >= 2 &&
646}
647
648std::pair<VPBasicBlock *, VPBasicBlock *>
650 auto *Header = cast<VPBasicBlock>(
651 Plan.getEntry()->getSuccessors()[1]->getSingleSuccessor());
652 auto *Latch = cast<VPBasicBlock>(Header->getPredecessors()[1]);
653 return {Header, Latch};
654}
655
659
660std::optional<MemoryLocation>
662 auto *M = dyn_cast<VPIRMetadata>(&R);
663 if (!M)
664 return std::nullopt;
666 // Populate noalias metadata from VPIRMetadata.
667 if (MDNode *NoAliasMD = M->getMetadata(LLVMContext::MD_noalias))
668 Loc.AATags.NoAlias = NoAliasMD;
669 if (MDNode *AliasScopeMD = M->getMetadata(LLVMContext::MD_alias_scope))
670 Loc.AATags.Scope = AliasScopeMD;
671 return Loc;
672}
673
675 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
676 VPRegionValue *CanIV = LoopRegion->getCanonicalIV();
677 assert(CanIV && "Expected loop region to have a canonical IV");
678
679 VPSymbolicValue &VFxUF = Plan.getVFxUF();
680
681 // Check if \p Step matches the expected increment step, accounting for
682 // materialization of VFxUF and UF.
683 auto IsIncrementStep = [&](VPValue *Step) -> bool {
684 if (!VFxUF.isMaterialized())
685 return Step == &VFxUF;
686
687 VPSymbolicValue &UF = Plan.getUF();
688 if (!UF.isMaterialized())
689 return Step == &UF ||
690 match(Step, m_c_Mul(m_Specific(&Plan.getUF()),
692
693 // Alias masking: step is number of active lanes of a dependence mask.
694 if (match(Step, m_ZExtOrTruncOrSelf(
696 return true;
697
698 unsigned ConcreteUF = Plan.getConcreteUF();
699 // Fixed VF: step is just the concrete UF.
700 if (match(Step, m_SpecificInt(ConcreteUF)))
701 return true;
702
703 // Scalable VF: step involves VScale.
704 if (ConcreteUF == 1)
706 if (match(Step, m_c_Mul(m_SpecificInt(ConcreteUF),
708 return true;
709 // mul(VScale, ConcreteUF) may have been simplified to
710 // shl(VScale, log2(ConcreteUF)) when ConcreteUF is a power of 2.
711 return isPowerOf2_32(ConcreteUF) &&
713 m_SpecificInt(Log2_32(ConcreteUF))));
714 };
715
716 VPInstruction *Increment = nullptr;
717 for (VPUser *U : CanIV->users()) {
718 VPValue *Step;
719 if (isa<VPInstruction>(U) &&
720 match(U, m_c_Add(m_Specific(CanIV), m_VPValue(Step))) &&
721 IsIncrementStep(Step)) {
722 assert(!Increment && "There must be a unique increment");
724 }
725 }
726
727 assert((!VFxUF.isMaterialized() || Increment) &&
728 "After materializing VFxUF, an increment must exist");
729 assert((!Increment ||
730 LoopRegion->hasCanonicalIVNUW() == Increment->hasNoUnsignedWrap()) &&
731 "NUW flag in region and increment must match");
732 return Increment;
733}
734
735/// Find the ComputeReductionResult recipe for \p PhiR, looking through selects
736/// inserted for predicated reductions or tail folding.
738 VPValue *BackedgeVal = PhiR->getBackedgeValue();
739 if (auto *Res =
741 return Res;
742
743 // Look through selects inserted for tail folding or predicated reductions.
744 VPRecipeBase *SelR =
745 findUserOf(BackedgeVal, m_Select(m_VPValue(), m_VPValue(), m_VPValue()));
746 if (!SelR)
747 return nullptr;
750}
751
754 SmallVector<const VPValue *> WorkList = {V};
755
756 while (!WorkList.empty()) {
757 const VPValue *Cur = WorkList.pop_back_val();
758 if (!Seen.insert(Cur).second)
759 continue;
760
761 auto *Blend = dyn_cast<VPBlendRecipe>(Cur);
762 // Skip blends that use V only through a compare by checking if any incoming
763 // value was already visited.
764 if (Blend && none_of(seq<unsigned>(0, Blend->getNumIncomingValues()),
765 [&](unsigned I) {
766 return Seen.contains(Blend->getIncomingValue(I));
767 }))
768 continue;
769
770 for (VPUser *U : Cur->users()) {
771 if (auto *InterleaveR = dyn_cast<VPInterleaveBase>(U))
772 if (InterleaveR->getAddr() == Cur)
773 return true;
774 // Cur is used as the pointer of a (possibly masked) load (operand 0) or
775 // store (operand 1).
778 m_Specific(Cur)))))
779 return true;
781 if (MemR->getAddr() == Cur && MemR->isConsecutive())
782 return true;
783 }
784 }
785
786 // The legacy cost model only supports scalarization loads/stores with phi
787 // addresses, if the phi is directly used as load/store address. Don't
788 // traverse further for Blends.
789 if (Blend)
790 continue;
791
792 // Only traverse further through users that also define a value (and can
793 // thus have their own users walked).
794 for (VPUser *U : Cur->users())
795 if (auto *SDR = dyn_cast<VPSingleDefRecipe>(U))
796 WorkList.push_back(SDR);
797 }
798 return false;
799}
800
801/// Try to find a loop-invariant IR value for \p S in the plan's entry block
802/// that can be reused. Returns the corresponding live-in VPValue, or nullptr
803/// if no reusable IR value is found.
804VPValue *VPSCEVExpander::tryToReuseIRValue(const SCEV *S) {
806 return nullptr;
807 VPlan &Plan = Builder.getPlan();
808 BasicBlock *PH = cast<VPIRBasicBlock>(Plan.getEntry())->getIRBasicBlock();
809 for (Value *V : SE.getSCEVValues(S)) {
810 // Only reuse instructions in the plan's entry block, or, when a
811 // DominatorTree is available, any instruction that dominates it.
812 // Instructions in sibling branches may not dominate the entry block.
813 auto *I = dyn_cast<Instruction>(V);
814 if (!I)
815 return Plan.getOrAddLiveIn(V);
816 if (!SE.DT.dominates(I->getParent(), PH))
817 continue;
818 SmallVector<Instruction *> DropPoisonGeneratingInsts;
819 if (!SE.canReuseInstruction(S, I, DropPoisonGeneratingInsts))
820 continue;
821 for (Instruction *DropI : DropPoisonGeneratingInsts)
823 return Plan.getOrAddLiveIn(V);
824 }
825 return nullptr;
826}
827
829 if (VPValue *V = tryToReuseIRValue(S))
830 return V;
831
832 switch (S->getSCEVType()) {
833 case scConstant:
834 return Builder.getPlan().getOrAddLiveIn(cast<SCEVConstant>(S)->getValue());
835 case scUnknown:
836 return Builder.getPlan().getOrAddLiveIn(cast<SCEVUnknown>(S)->getValue());
837 case scVScale:
838 return Builder.createNaryOp(VPInstruction::VScale, {}, S->getType());
839 case scAddExpr:
840 case scMulExpr: {
841 if (S->getType()->isPointerTy())
842 return nullptr;
843 auto *NAry = cast<SCEVNAryExpr>(S);
844 unsigned Opcode =
845 S->getSCEVType() == scAddExpr ? Instruction::Add : Instruction::Mul;
846 // Iterate in reverse so that constants are emitted last.
848 for (const SCEVUse &Op : reverse(NAry->operands())) {
849 VPValue *OpV = tryToExpand(Op);
850 if (!OpV)
851 return nullptr;
852 Ops.push_back(OpV);
853 }
854 VPIRFlags::WrapFlagsTy WrapFlags(NAry->hasNoUnsignedWrap(),
855 NAry->hasNoSignedWrap());
856 VPValue *Result = Ops.front();
857 for (VPValue *Op : drop_begin(Ops))
858 Result = Builder.createOverflowingOp(Opcode, {Result, Op}, WrapFlags, DL);
859 return Result;
860 }
861 case scUDivExpr: {
862 auto *UDiv = cast<SCEVUDivExpr>(S);
863 VPValue *LHS = tryToExpand(UDiv->getLHS());
864 if (!LHS)
865 return nullptr;
866 VPValue *RHS = tryToExpand(UDiv->getRHS());
867 if (!RHS)
868 return nullptr;
869 return Builder.createNaryOp(Instruction::UDiv, {LHS, RHS},
870 VPIRFlags::getDefaultFlags(Instruction::UDiv),
871 DL);
872 }
873 default:
874 return nullptr;
875 }
876}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static constexpr Value * getValue(Ty &ValueOrUse)
static Value * getOpcode(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
This file provides a LoopVectorizationPlanner class.
#define I(x, y, z)
Definition MD5.cpp:57
This file provides utility analysis objects describing memory locations.
static unsigned getScalarSizeInBits(Type *Ty)
This file implements the TypeSwitch template, which mimics a switch() statement whose cases are type ...
This file implements dominator tree analysis for a single level of a VPlan's H-CFG.
static bool propagatesPoisonFromRecipeOp(const VPRecipeBase *R)
Returns true if R propagates poison from any operand to its result.
static bool preservesUniformity(unsigned Opcode)
Returns true if Opcode preserves uniformity, i.e., if all operands are uniform, the result will also ...
static bool poisonGuaranteesUB(const VPValue *V)
Returns true if V being poison is guaranteed to trigger UB because it propagates to the address of a ...
static const uint32_t IV[8]
Definition blake3_impl.h:83
Class for arbitrary precision integers.
Definition APInt.h:78
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
LLVM Basic Block Representation.
Definition BasicBlock.h:62
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags none()
bool isCast() const
bool isBinaryOp() const
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1069
Representation for a specific memory location.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
LLVM_ABI const SCEV * getPredicatedSCEV(const SCEV *Expr)
Returns the rewritten SCEV for Expr in the context of the current SCEV predicate.
static LLVM_ABI void dropPoisonGeneratingAnnotationsAndReinfer(ScalarEvolution &SE, Instruction *I)
Drop poison-generating flags from I, then try re-infer via SCEV.
This class represents an analyzed expression in the program.
static constexpr auto FlagAnyWrap
static constexpr auto FlagNSW
SCEVTypes getSCEVType() const
LLVM_ABI Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
LLVM_ABI const SCEV * getUDivExpr(SCEVUse LHS, SCEVUse RHS)
Get a canonical unsigned division expression, or something simpler if possible.
LLVM_ABI const SCEV * getAbsExpr(const SCEV *Op, bool IsNSW)
LLVM_ABI const SCEV * getURemExpr(SCEVUse LHS, SCEVUse RHS)
Represents an unsigned remainder expression based on unsigned division.
LLVM_ABI const SCEV * getSMinExpr(SCEVUse LHS, SCEVUse RHS)
const SCEV * getZero(Type *Ty)
Return a SCEV for the constant 0 of a specific type.
LLVM_ABI uint64_t getTypeSizeInBits(Type *Ty) const
Return the size in bits of the specified type, for which isSCEVable must return true.
LLVM_ABI const SCEV * getConstant(ConstantInt *V)
LLVM_ABI const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
LLVM_ABI const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Return LHS-RHS.
LLVM_ABI const SCEV * getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L, SCEV::NoWrapFlags Flags)
Get an add recurrence expression for the specified loop.
const SCEV * getOne(Type *Ty)
Return a SCEV for the constant 1 of a specific type.
LLVM_ABI bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
LLVM_ABI const SCEV * getZeroExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI const SCEV * getTruncateExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
LLVM_ABI const SCEV * getUMaxExpr(SCEVUse LHS, SCEVUse RHS)
const SCEV * getMinusOne(Type *Ty)
Return a SCEV for the constant -1 of a specific type.
LLVM_ABI const SCEV * getCouldNotCompute()
LLVM_ABI const SCEV * getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
LLVM_ABI const SCEV * getSignExtendExpr(const SCEV *Op, Type *Ty, unsigned Depth=0)
const SCEV * getPowerOfTwo(Type *Ty, unsigned Power)
Return a SCEV for the constant Power of two.
LLVM_ABI const SCEV * getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
LLVM_ABI const SCEV * getSMaxExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI bool canReuseInstruction(const SCEV *S, Instruction *I, SmallVectorImpl< Instruction * > &DropPoisonGeneratingInsts)
Check whether it is poison-safe to represent the expression S using the instruction I.
LLVM_ABI const SCEV * getGEPExpr(GEPOperator *GEP, ArrayRef< SCEVUse > IndexExprs)
Returns an expression for a GEP.
LLVM_ABI const SCEV * getUMinExpr(SCEVUse LHS, SCEVUse RHS, bool Sequential=false)
LLVM_ABI const SCEV * getTruncateOrSignExtend(const SCEV *V, Type *Ty, unsigned Depth=0)
Return a SCEV corresponding to a conversion of the input value to the specified type.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This class implements a switch-like dispatch statement for a value of 'T' using dyn_cast functionalit...
Definition TypeSwitch.h:89
TypeSwitch< T, ResultT > & Case(CallableT &&caseFn)
Add a case on the given type.
Definition TypeSwitch.h:98
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:4407
iterator end()
Definition VPlan.h:4444
iterator_range< iterator > phis()
Returns an iterator range over the PHI-like recipes in the block.
Definition VPlan.h:4495
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:266
void insert(VPRecipeBase *Recipe, iterator InsertPt)
Definition VPlan.h:4473
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:94
VPRegionBlock * getParent()
Definition VPlan.h:186
size_t getNumSuccessors() const
Definition VPlan.h:237
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:222
VPlan * getPlan()
Definition VPlan.cpp:211
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:216
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:227
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:211
static bool isLatch(const VPBlockBase *VPB, const VPDominatorTree &VPDT)
Returns true if VPB is a loop latch, using isHeader().
static VPBasicBlock * getPlainCFGMiddleBlock(const VPlan &Plan)
Returns the middle block of Plan in plain CFG form (before regions are formed).
static bool isHeader(const VPBlockBase *VPB, const VPDominatorTree &VPDT)
Returns true if VPB is a loop header, based on regions or VPDT in their absence.
static auto blocksOnly(T &&Range)
Return an iterator range over Range which only includes BlockTy blocks.
Definition VPlanUtils.h:312
static std::pair< VPBasicBlock *, VPBasicBlock * > getPlainCFGHeaderAndLatch(const VPlan &Plan)
Returns the header and latch of the outermost loop of Plan in plain CFG form (before regions are form...
static SmallVector< VPBasicBlock * > blocksInSingleSuccessorChainBetween(VPBasicBlock *FirstBB, VPBasicBlock *LastBB)
Returns the blocks between FirstBB and LastBB, where FirstBB to LastBB forms a single-sucessor chain.
A recipe for converting the input value IV value to the corresponding value of an IV with different s...
Definition VPlan.h:4188
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
Recipe to expand a SCEV expression.
Definition VPlan.h:4020
virtual VPValue * getBackedgeValue()
Returns the incoming value from the loop backedge.
Definition VPlan.h:2483
static VPIRFlags getDefaultFlags(unsigned Opcode)
Returns default flags for Opcode for opcodes that support it, asserts otherwise.
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:1226
@ ReductionStartVector
Start vector for reductions with 3 operands: the original start value, the identity value for the red...
Definition VPlan.h:1315
@ VScale
Returns the value for vscale.
Definition VPlan.h:1348
unsigned getOpcode() const
Definition VPlan.h:1417
bool isVectorToScalar() const
Returns true if this VPInstruction produces a scalar value from a vector, e.g.
bool isSingleScalar() const
Returns true if this VPInstruction's operands are single scalars and the result is also a single scal...
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:402
A recipe for handling reduction phis.
Definition VPlan.h:2864
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:4617
bool isReplicator() const
An indicator whether this region is to generate multiple replicated instances of output IR correspond...
Definition VPlan.h:4693
bool hasCanonicalIVNUW() const
Indicates if NUW is set for the canonical IV increment, for loop regions.
Definition VPlan.h:4742
VPRegionValue * getCanonicalIV()
Return the canonical induction variable of the region, null for replicating regions.
Definition VPlan.h:4729
VPValues defined by a VPRegionBlock, like the canonical IV.
Definition VPlanValue.h:215
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:3398
unsigned getOpcode() const
Definition VPlan.h:3491
VPValue * tryToExpand(const SCEV *S)
Try to expand S into recipes and live-ins using the builder.
A recipe for handling phi nodes of integer and floating-point inductions, producing their scalar valu...
Definition VPlan.h:4255
VPSingleDefRecipe is a base class for recipes that model a sequence of one or more output IR that def...
Definition VPlan.h:609
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:384
operand_range operands()
Definition VPlanValue.h:457
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:50
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition VPlan.cpp:130
user_range users()
Definition VPlanValue.h:157
VPWidenCastRecipe is a recipe to create vector cast instructions.
Definition VPlan.h:1878
A recipe for handling GEP instructions.
Definition VPlan.h:2206
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition VPlan.h:2623
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1817
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4765
VPBasicBlock * getEntry()
Definition VPlan.h:4861
VPValue * getTripCount() const
The trip count of the original loop.
Definition VPlan.h:4924
VPSymbolicValue & getVFxUF()
Returns VF * UF of the vector loop region.
Definition VPlan.h:4964
VPValue * getBackedgeTakenCount() const
Definition VPlan.h:4951
VPIRValue * getOrAddLiveIn(Value *V)
Gets the live-in VPIRValue for V or adds a new live-in (if none exists yet) for V.
Definition VPlan.h:5038
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1061
unsigned getConcreteUF() const
Returns the concrete UF of the plan, after unrolling.
Definition VPlan.h:5016
VPBasicBlock * getVectorPreheader() const
Returns the preheader of the vector loop region, if one exists, or null otherwise.
Definition VPlan.h:4866
VPSymbolicValue & getUF()
Returns the UF of the vector loop region.
Definition VPlan.h:4961
bool hasScalarVFOnly() const
Definition VPlan.h:5006
VPBasicBlock * getScalarPreheader() const
Return the VPBasicBlock for the preheader of the scalar loop.
Definition VPlan.h:4904
VPSymbolicValue & getVF()
Returns the VF of the vector loop region.
Definition VPlan.h:4957
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
An efficient, type-erasing, non-owning reference to a callable.
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
match_combine_or< Ty... > m_CombineOr(const Ty &...Ps)
Combine pattern matchers matching any of Ps patterns.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
auto m_ZExtOrTruncOrSelf(const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
auto m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
bool match(const SCEV *S, const Pattern &P)
SCEVAffineAddRec_match< Op0_t, Op1_t, match_isa< const Loop > > m_scev_AffineAddRec(const Op0_t &Op0, const Op1_t &Op1)
AllRecipe_commutative_match< Instruction::And, Op0_t, Op1_t > m_c_BinaryAnd(const Op0_t &Op0, const Op1_t &Op1)
Match a binary AND operation.
AllRecipe_match< Opcode, Op0_t, Op1_t > m_Binary(const Op0_t &Op0, const Op1_t &Op1)
AllRecipe_match< Opcode, Op0_t > m_Unary(const Op0_t &Op0)
canonical_widen_iv_match m_CanonicalWidenIV()
VPInstruction_match< VPInstruction::ActiveLaneMask, Op0_t, Op1_t, Op2_t > m_ActiveLaneMask(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
auto m_GetElementPtr(const Op0_t &Op0, const Op1_t &Op1)
canonical_iv_match m_CanonicalIV()
auto m_VPValue()
Match an arbitrary VPValue and ignore it.
match_bind< VPInstruction > m_VPInstruction(VPInstruction *&V)
Match a VPInstruction, capturing if we match.
auto m_DerivedIV(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
static VPRecipeBase * findUserOf(VPValue *V, const MatchT &P)
If V is used by a recipe matching pattern P, return it.
auto m_ScalarIVSteps(const Op0_t &Op0, const Op1_t &Op1, const Op2_t &Op2)
VPInstruction_match< VPInstruction::Reverse, Op0_t > m_Reverse(const Op0_t &Op0)
bool isSingleScalar(const VPValue *VPV)
Returns true if VPV is a single scalar, either because it produces the same value for all lanes or on...
VPValue * getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr)
Get or create a VPValue that corresponds to the expansion of Expr.
bool cannotHoistOrSinkRecipe(const VPRecipeBase &R, bool Sinking=false)
Return true if we do not know how to (mechanically) hoist or sink R.
VPBasicBlock * getFirstLoopHeader(VPlan &Plan, VPDominatorTree &VPDT)
Returns the header block of the first, top-level loop, or null if none exist.
bool isAddressSCEVForCost(const SCEV *Addr, ScalarEvolution &SE, const Loop *L)
Returns true if Addr is an address SCEV that can be passed to TTI::getAddressComputationCost,...
bool onlyFirstPartUsed(const VPValue *Def)
Returns true if only the first part of Def is used.
VPInstruction * findComputeReductionResult(VPReductionPHIRecipe *PhiR)
Find the ComputeReductionResult recipe for PhiR, looking through selects inserted for predicated redu...
VPInstruction * findCanonicalIVIncrement(VPlan &Plan)
Find the canonical IV increment of Plan's vector loop region.
std::optional< MemoryLocation > getMemoryLocation(const VPRecipeBase &R)
Return a MemoryLocation for R with noalias metadata populated from R, if the recipe is supported and ...
bool onlyFirstLaneUsed(const VPValue *Def)
Returns true if only the first lane of Def is used.
VPValue * findIncomingAliasMask(const VPlan &Plan)
Finds the incoming alias-mask within the vector preheader.
VPSingleDefRecipe * findHeaderMask(VPlan &Plan)
Collect the header mask with the pattern: (ICMP_ULE, WideCanonicalIV, backedge-taken-count) Note: If ...
bool onlyScalarValuesUsed(const VPValue *Def)
Returns true if only scalar values of Def are used by all users.
bool isUniformAcrossVFsAndUFs(const VPValue *V)
Checks if V is uniform across all VF lanes and UF parts.
bool isUsedByLoadStoreAddress(const VPValue *V)
Returns true if V is used as part of the address of another load or store.
GEPNoWrapFlags getGEPFlagsForPtr(VPValue *Ptr)
Returns the GEP nowrap flags for Ptr, looking through pointer casts mirroring Value::stripPointerCast...
const SCEV * getSCEVExprForVPValue(const VPValue *V, PredicatedScalarEvolution &PSE, const Loop *L=nullptr)
Return the SCEV expression for V.
unsigned getVFScaleFactor(VPRecipeBase *R)
Get the VF scaling factor applied to the recipe's output, if the recipe has one.
bool isHeaderMask(const VPValue *V, const VPlan &Plan)
Return true if V is a header mask in Plan.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
auto find(R &&Range, const T &Val)
Provide wrappers to std::find which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1764
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1738
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< df_iterator< VPBlockShallowTraversalWrapper< VPBlockBase * > > > vp_depth_first_shallow(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order.
Definition VPlanCFG.h:253
iterator_range< df_iterator< VPBlockDeepTraversalWrapper< VPBlockBase * > > > vp_depth_first_deep(VPBlockBase *G)
Returns an iterator range to traverse the graph starting at G in depth-first order while traversing t...
Definition VPlanCFG.h:288
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1745
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:279
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1752
SmallVector< ValueTypeFromRangeType< R >, Size > to_vector(R &&Range)
Given a range of type R, iterate the entire range and return a SmallVector with elements of the vecto...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2018
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1771
auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:305
@ Increment
Incrementally increasing token ID.
Definition AllocToken.h:26
@ Default
The result value is uniform if and only if all operands are uniform.
Definition Uniformity.h:20
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
SCEVUseT< const SCEV * > SCEVUse
A symbolic live-in VPValue, used for values like vector trip count, VF, and VFxUF.
Definition VPlanValue.h:286
bool isMaterialized() const
Returns true if this symbolic value has been materialized.
Definition VPlanValue.h:297