LLVM 24.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/MapVector.h"
16#include "llvm/ADT/SetVector.h"
18#include "llvm/ADT/TypeSwitch.h"
24#include "llvm/IR/Dominators.h"
27
28using namespace llvm;
29using namespace llvm::VPlanPatternMatch;
30using namespace llvm::SCEVPatternMatch;
31
33 return all_of(Def->users(),
34 [Def](const VPUser *U) { return U->usesFirstLaneOnly(Def); });
35}
36
38 return all_of(Def->users(),
39 [Def](const VPUser *U) { return U->usesFirstPartOnly(Def); });
40}
41
43 return all_of(Def->users(),
44 [Def](const VPUser *U) { return U->usesScalars(Def); });
45}
46
48 if (auto *E = dyn_cast<SCEVConstant>(Expr))
49 return Plan.getOrAddLiveIn(E->getValue());
50 // Skip SCEV expansion if Expr is a SCEVUnknown wrapping a non-instruction
51 // value. Otherwise the value may be defined in a loop and using it directly
52 // will break LCSSA form. The SCEV expansion takes care of preserving LCSSA
53 // form.
54 auto *U = dyn_cast<SCEVUnknown>(Expr);
55 if (U && !isa<Instruction>(U->getValue()))
56 return Plan.getOrAddLiveIn(U->getValue());
57 auto *Expanded = new VPExpandSCEVRecipe(Expr);
58 VPBasicBlock *EntryVPBB = Plan.getEntry();
59 auto Iter = EntryVPBB->getFirstNonPhi();
60 while (Iter != EntryVPBB->end() && isa<VPIRInstruction>(*Iter))
61 ++Iter;
62 EntryVPBB->insert(Expanded, Iter);
63 return Expanded;
64}
65
66/// Returns true if \p V being poison is guaranteed to trigger UB because it
67/// propagates to the address of a memory recipe.
68static bool poisonGuaranteesUB(const VPValue *V) {
71
72 auto PropagatesPoisonFromRecipeOp = [](const VPRecipeBase *R) {
74 return false;
75 unsigned Opcode = vputils::getOpcode(R->getVPSingleValue());
76 return Instruction::isCast(Opcode) || Opcode == Instruction::GetElementPtr;
77 };
78
79 Worklist.push_back(V);
80
81 while (!Worklist.empty()) {
82 const VPValue *Current = Worklist.pop_back_val();
83 if (!Visited.insert(Current).second)
84 continue;
85
86 for (VPUser *U : Current->users()) {
87 // Check if Current is used as an address operand for load/store.
88 auto *R = cast<VPRecipeBase>(U);
89 if (auto *MemR = dyn_cast<VPWidenMemoryRecipe>(R)) {
90 if (MemR->getAddr() == Current)
91 return true;
92 continue;
93 }
94 if (auto *Rep = dyn_cast<VPReplicateRecipe>(U)) {
95 unsigned Opcode = Rep->getOpcode();
96 if ((Opcode == Instruction::Load && Rep->getOperand(0) == Current) ||
97 (Opcode == Instruction::Store && Rep->getOperand(1) == Current))
98 return true;
99 }
100
101 // Check if poison propagates through this recipe to any of its users.
102 for (const VPValue *Op : R->operands()) {
103 if (Op == Current && PropagatesPoisonFromRecipeOp(R)) {
104 Worklist.push_back(R->getVPSingleValue());
105 break;
106 }
107 }
108 }
109 }
110
111 return false;
112}
113
115 // Like IR stripPointerCasts, look through GEPs with all-zero indices and
116 // casts to find a root GEP VPInstruction.
117 while (auto *PtrVPI = dyn_cast<VPInstruction>(Ptr)) {
118 unsigned Opcode = PtrVPI->getOpcode();
119 if (Opcode == Instruction::GetElementPtr) {
120 if (!all_of(drop_begin(PtrVPI->operands()), match_fn(m_ZeroInt())))
121 return PtrVPI->getGEPNoWrapFlags();
122 Ptr = PtrVPI->getOperand(0);
123 continue;
124 }
125 if (Opcode != Instruction::BitCast && Opcode != Instruction::AddrSpaceCast)
126 break;
127 Ptr = PtrVPI->getOperand(0);
128 }
129 return GEPNoWrapFlags::none();
130}
131
134 const Loop *L) {
135 ScalarEvolution &SE = *PSE.getSE();
136 if (auto *RV = dyn_cast<VPRegionValue>(V)) {
137 assert(RV == RV->getDefiningRegion()->getCanonicalIV() &&
138 "RegionValue must be canonical IV");
139 if (!L)
140 return SE.getCouldNotCompute();
141 return SE.getAddRecExpr(SE.getZero(RV->getType()), SE.getOne(RV->getType()),
143 }
144
146 Value *LiveIn = V->getUnderlyingValue();
147 if (LiveIn && SE.isSCEVable(LiveIn->getType()))
148 return SE.getSCEV(LiveIn);
149 return SE.getCouldNotCompute();
150 }
151
152 // Helper to create SCEVs for binary and unary operations.
153 auto CreateSCEV = [&](ArrayRef<VPValue *> Ops,
154 function_ref<const SCEV *(ArrayRef<SCEVUse>)> CreateFn)
155 -> const SCEV * {
157 for (VPValue *Op : Ops) {
158 const SCEV *S = getSCEVExprForVPValue(Op, PSE, L);
160 return SE.getCouldNotCompute();
161 SCEVOps.push_back(S);
162 }
163 return PSE.getPredicatedSCEV(CreateFn(SCEVOps));
164 };
165
166 VPValue *LHSVal, *RHSVal;
167 if (match(V, m_Add(m_VPValue(LHSVal), m_VPValue(RHSVal))))
168 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
169 return SE.getAddExpr(Ops[0], Ops[1], SCEV::FlagAnyWrap, 0);
170 });
171 if (match(V, m_BinaryOr(m_VPValue(LHSVal), m_VPValue(RHSVal))))
172 if (cast<VPRecipeWithIRFlags>(V->getDefiningRecipe())->isDisjoint())
173 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
174 return SE.getAddExpr(Ops[0], Ops[1], SCEV::FlagAnyWrap, 0);
175 });
176 if (match(V, m_Sub(m_VPValue(LHSVal), m_VPValue(RHSVal))))
177 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
178 return SE.getMinusSCEV(Ops[0], Ops[1], SCEV::FlagAnyWrap, 0);
179 });
180 if (match(V, m_Not(m_VPValue(LHSVal)))) {
181 // not X = xor X, -1 = -1 - X
182 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
183 return SE.getMinusSCEV(SE.getMinusOne(Ops[0]->getType()), Ops[0]);
184 });
185 }
186 if (match(V, m_Mul(m_VPValue(LHSVal), m_VPValue(RHSVal))))
187 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
188 return SE.getMulExpr(Ops[0], Ops[1], SCEV::FlagAnyWrap, 0);
189 });
190 // Handle shl by constant: x << c is equivalent to x * (1 << c). A shift
191 // amount >= the bit width produces poison; do not rewrite it, as
192 // getPowerOfTwo requires the power to be in range.
193 uint64_t ShiftAmt;
194 if (match(V, m_Shl(m_VPValue(LHSVal), m_ConstantInt(ShiftAmt))) &&
195 ShiftAmt < LHSVal->getScalarType()->getScalarSizeInBits())
196 return CreateSCEV(LHSVal, [&](ArrayRef<SCEVUse> Ops) {
197 return SE.getMulExpr(Ops[0],
198 SE.getPowerOfTwo(Ops[0]->getType(), ShiftAmt));
199 });
200 if (match(V, m_LShr(m_VPValue(LHSVal), m_ConstantInt(ShiftAmt)))) {
201 Type *Ty = V->getScalarType();
202 if (ShiftAmt < SE.getTypeSizeInBits(Ty))
203 return CreateSCEV(LHSVal, [&](ArrayRef<SCEVUse> Ops) {
204 return SE.getUDivExpr(Ops[0], SE.getPowerOfTwo(Ty, ShiftAmt));
205 });
206 }
207 if (match(V, m_UDiv(m_VPValue(LHSVal), m_VPValue(RHSVal))))
208 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
209 return SE.getUDivExpr(Ops[0], Ops[1]);
210 });
211 if (match(V, m_URem(m_VPValue(LHSVal), m_VPValue(RHSVal))))
212 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
213 return SE.getURemExpr(Ops[0], Ops[1]);
214 });
215 // A SDiv with non-negative operands is equivalent to an UDiv.
216 if (match(V, m_SDiv(m_VPValue(LHSVal), m_VPValue(RHSVal)))) {
217 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
218 if (!SE.isKnownNonNegative(Ops[0]) || !SE.isKnownNonNegative(Ops[1]))
219 return SE.getCouldNotCompute();
220 return SE.getUDivExpr(Ops[0], Ops[1]);
221 });
222 }
223 // A SRem with non-negative operands is equivalent to an URem.
224 if (match(V, m_SRem(m_VPValue(LHSVal), m_VPValue(RHSVal)))) {
225 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
226 if (!SE.isKnownNonNegative(Ops[0]) || !SE.isKnownNonNegative(Ops[1]))
227 return SE.getCouldNotCompute();
228 return SE.getURemExpr(Ops[0], Ops[1]);
229 });
230 }
231 // Handle AND with constant mask: x & (2^n - 1) can be represented as x % 2^n.
232 const APInt *Mask;
233 if (match(V, m_c_BinaryAnd(m_VPValue(LHSVal), m_APInt(Mask))) &&
234 (*Mask + 1).isPowerOf2())
235 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
236 return SE.getURemExpr(Ops[0], SE.getConstant(*Mask + 1));
237 });
238 // SCEV models ptrtoaddr, but not ptrtoint, mirroring createSCEV.
239 if (match(V, m_PtrToAddr(m_VPValue(LHSVal))))
240 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
241 return SE.getPtrToAddrExpr(Ops[0]);
242 });
243 if (match(V, m_Trunc(m_VPValue(LHSVal)))) {
244 Type *DestTy = V->getScalarType();
245 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
246 return SE.getTruncateExpr(Ops[0], DestTy);
247 });
248 }
249 if (match(V, m_ZExt(m_VPValue(LHSVal)))) {
250 Type *DestTy = V->getScalarType();
251 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
252 return SE.getZeroExtendExpr(Ops[0], DestTy);
253 });
254 }
255 if (match(V, m_SExt(m_VPValue(LHSVal)))) {
256 Type *DestTy = V->getScalarType();
257
258 // Mirror SCEV's createSCEV handling for sext(sub nsw): push sign extension
259 // onto the operands before computing the subtraction.
260 VPValue *SubLHS, *SubRHS;
261 auto *SubR = dyn_cast<VPRecipeWithIRFlags>(LHSVal);
262 if (match(LHSVal, m_Sub(m_VPValue(SubLHS), m_VPValue(SubRHS))) && SubR &&
263 SubR->hasNoSignedWrap() && poisonGuaranteesUB(LHSVal)) {
264 const SCEV *V1 = getSCEVExprForVPValue(SubLHS, PSE, L);
265 const SCEV *V2 = getSCEVExprForVPValue(SubRHS, PSE, L);
267 return SE.getMinusSCEV(SE.getSignExtendExpr(V1, DestTy),
268 SE.getSignExtendExpr(V2, DestTy), SCEV::FlagNSW);
269 }
270
271 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
272 return SE.getSignExtendExpr(Ops[0], DestTy);
273 });
274 }
275 if (match(V,
277 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
278 return SE.getUMaxExpr(Ops[0], Ops[1]);
279 });
280 if (match(V,
282 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
283 return SE.getSMaxExpr(Ops[0], Ops[1]);
284 });
285 if (match(V,
287 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
288 return SE.getUMinExpr(Ops[0], Ops[1]);
289 });
290 if (match(V,
292 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
293 return SE.getSMinExpr(Ops[0], Ops[1]);
294 });
296 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
297 // is_int_min_poison is local to this intrinsic: poison on INT_MIN is
298 // not proof that the input is never INT_MIN, nor that poison reaches
299 // UB. Do not translate it to SCEV's global IsNSW flag.
300 return SE.getAbsExpr(Ops[0], /*IsNSW=*/false);
301 });
302
304 Type *SourceElementType;
305 if (match(V, m_GetElementPtr(SourceElementType, Ops))) {
306 return CreateSCEV(Ops, [&](ArrayRef<SCEVUse> Ops) {
307 return SE.getGEPExpr(Ops.front(), Ops.drop_front(), SourceElementType);
308 });
309 }
310
311 // TODO: Support constructing SCEVs for more recipes as needed.
312 const VPRecipeBase *DefR = V->getDefiningRecipe();
313 const SCEV *Expr =
315 .Case([](const VPExpandSCEVRecipe *R) { return R->getSCEV(); })
316 .Case([&SE, &PSE, L](const VPWidenIntOrFpInductionRecipe *R) {
317 const SCEV *Step = getSCEVExprForVPValue(R->getStepValue(), PSE, L);
318 if (!L || isa<SCEVCouldNotCompute>(Step))
319 return SE.getCouldNotCompute();
320 const SCEV *Start =
321 getSCEVExprForVPValue(R->getStartValue(), PSE, L);
322 const SCEV *AddRec =
323 SE.getAddRecExpr(Start, Step, L, SCEV::FlagAnyWrap);
324 if (R->getTruncInst())
325 return SE.getTruncateExpr(AddRec, R->getScalarType());
326 return AddRec;
327 })
328 .Case([&SE, &PSE, L](const VPWidenPointerInductionRecipe *R) {
329 const SCEV *Start =
330 getSCEVExprForVPValue(R->getStartValue(), PSE, L);
331 if (!L || isa<SCEVCouldNotCompute>(Start))
332 return SE.getCouldNotCompute();
333 const SCEV *Step = getSCEVExprForVPValue(R->getStepValue(), PSE, L);
334 if (isa<SCEVCouldNotCompute>(Step))
335 return SE.getCouldNotCompute();
336 return SE.getAddRecExpr(Start, Step, L, SCEV::FlagAnyWrap);
337 })
338 .Case([&SE, &PSE, L](const VPDerivedIVRecipe *R) {
339 const SCEV *Start = getSCEVExprForVPValue(R->getOperand(0), PSE, L);
340 const SCEV *IV = getSCEVExprForVPValue(R->getOperand(1), PSE, L);
341 const SCEV *Scale = getSCEVExprForVPValue(R->getOperand(2), PSE, L);
342 if (any_of(ArrayRef({Start, IV, Scale}),
344 return SE.getCouldNotCompute();
345
346 return SE.getAddExpr(
347 SE.getTruncateOrSignExtend(Start, IV->getType()),
348 SE.getMulExpr(
349 IV, SE.getTruncateOrSignExtend(Scale, IV->getType())));
350 })
351 .Case([&SE, &PSE, L](const VPScalarIVStepsRecipe *R) {
352 const SCEV *IV = getSCEVExprForVPValue(R->getOperand(0), PSE, L);
353 const SCEV *Step = getSCEVExprForVPValue(R->getOperand(1), PSE, L);
355 return SE.getCouldNotCompute();
356 return SE.getTruncateOrSignExtend(IV, Step->getType());
357 })
358 .Default(
359 [&SE](const VPRecipeBase *) { return SE.getCouldNotCompute(); });
360
361 return PSE.getPredicatedSCEV(Expr);
362}
363
365 const Loop *L) {
366 // If address is an SCEVAddExpr, we require that all operands must be either
367 // be invariant or a (possibly sign-extend) affine AddRec.
368 if (auto *PtrAdd = dyn_cast<SCEVAddExpr>(Addr)) {
369 return all_of(PtrAdd->operands(), [&SE, L](const SCEV *Op) {
370 return SE.isLoopInvariant(Op, L) ||
371 match(Op, m_scev_SExt(m_scev_AffineAddRec(m_SCEV(), m_SCEV()))) ||
372 match(Op, m_scev_AffineAddRec(m_SCEV(), m_SCEV()));
373 });
374 }
375
376 // Otherwise, check if address is loop invariant or an affine add recurrence.
377 return SE.isLoopInvariant(Addr, L) ||
379}
380
381unsigned vputils::getOpcode(const VPValue *V) {
385 VPWidenLoadEVLRecipe>([](auto *I) { return I->getOpcode(); })
386 .Case<VPVectorPointerRecipe, VPPredInstPHIRecipe, VPScalarIVStepsRecipe>(
387 [](auto *I) {
388 // For recipes that do not directly map to LLVM IR instructions,
389 // assign opcodes after the last VPInstruction opcode (which is also
390 // after the last IR Instruction opcode), based on the VPRecipeID.
391 return VPInstruction::OpsEnd + 1 + I->getVPRecipeID();
392 })
393 .Default([](auto *) { return 0; });
394}
395
396std::optional<std::pair<bool, unsigned>>
399 return std::make_pair(true, IID);
400 if (unsigned Opcode = vputils::getOpcode(V))
401 return std::make_pair(false, Opcode);
402 return {};
403}
404
405/// Returns true if \p Opcode preserves uniformity, i.e., if all operands are
406/// uniform, the result will also be uniform.
407static bool preservesUniformity(unsigned Opcode) {
408 if (Instruction::isBinaryOp(Opcode) || Instruction::isCast(Opcode))
409 return true;
410 switch (Opcode) {
411 case Instruction::Freeze:
412 case Instruction::GetElementPtr:
413 case Instruction::ICmp:
414 case Instruction::FCmp:
415 case Instruction::Select:
420 return true;
421 default:
422 return false;
423 }
424}
425
427 // TODO: Handle more opcodes and recipes.
429 return false;
430 unsigned Opcode = getOpcode(V);
431 return Instruction::isUnaryOp(Opcode) || Instruction::isBinaryOp(Opcode);
432}
433
435 // Live-in, symbolic and canonical-IV region values are single-scalar.
436 if (auto *RV = dyn_cast<VPRegionValue>(VPV))
437 return RV == RV->getDefiningRegion()->getCanonicalIV();
439 return true;
440
441 if (auto *Rep = dyn_cast<VPReplicateRecipe>(VPV)) {
442 const VPRegionBlock *RegionOfR = Rep->getRegion();
443 // Don't consider recipes in replicate regions as uniform yet; their first
444 // lane cannot be accessed when executing the replicate region for other
445 // lanes.
446 if (RegionOfR && RegionOfR->isReplicator())
447 return false;
448 return Rep->isSingleScalar() || (preservesUniformity(Rep->getOpcode()) &&
449 all_of(Rep->operands(), isSingleScalar));
450 }
453 if (auto *WidenR = dyn_cast<VPWidenRecipe>(VPV)) {
454 return preservesUniformity(WidenR->getOpcode()) &&
455 all_of(WidenR->operands(), isSingleScalar);
456 }
457 if (auto *VPI = dyn_cast<VPInstruction>(VPV))
458 return VPI->isSingleScalar() || VPI->isVectorToScalar() ||
459 (preservesUniformity(VPI->getOpcode()) &&
460 all_of(VPI->operands(), isSingleScalar));
461 if (auto *RR = dyn_cast<VPReductionRecipe>(VPV))
462 return !RR->isPartialReduction();
464 VPV))
465 return true;
466 if (auto *Expr = dyn_cast<VPExpressionRecipe>(VPV))
467 return Expr->isVectorToScalar();
468
469 // VPExpandSCEVRecipes must be placed in the entry and are always uniform.
470 return isa<VPExpandSCEVRecipe>(VPV);
471}
472
474 // Live-ins, symbolic and canonical-IV region values are uniform.
475 if (auto *RV = dyn_cast<VPRegionValue>(V))
476 return RV == RV->getDefiningRegion()->getCanonicalIV();
478 return true;
479
480 const VPRecipeBase *R = V->getDefiningRecipe();
481 const VPBasicBlock *VPBB = R ? R->getParent() : nullptr;
482 const VPlan *Plan = VPBB ? VPBB->getPlan() : nullptr;
483 if (VPBB &&
484 (VPBB == Plan->getVectorPreheader() || VPBB == Plan->getEntry())) {
485 if (match(R,
488 return false;
489 return all_of(R->operands(), isUniformAcrossVFsAndUFs);
490 }
491
493 .Case([](const VPDerivedIVRecipe *R) { return true; })
494 .Case([](const VPReplicateRecipe *R) {
495 // Be conservative about side-effects, except for the
496 // known-side-effecting assumes and stores, which we know will be
497 // uniform.
498 return R->isSingleScalar() &&
499 (!R->mayHaveSideEffects() ||
500 isa<AssumeInst, StoreInst>(R->getUnderlyingInstr())) &&
501 all_of(R->operands(), isUniformAcrossVFsAndUFs);
502 })
503 .Case([](const VPWidenRecipe *R) {
504 return preservesUniformity(R->getOpcode()) &&
505 all_of(R->operands(), isUniformAcrossVFsAndUFs);
506 })
507 .Case([](const VPPhi *) {
508 // Bail out on VPPhi, as we can end up in infinite cycles.
509 return false;
510 })
511 .Case([](const VPInstruction *VPI) {
512 return (VPI->isSingleScalar() || VPI->isVectorToScalar() ||
515 })
516 .Case([](const VPWidenCastRecipe *R) {
517 // A cast is uniform according to its operand.
518 return isUniformAcrossVFsAndUFs(R->getOperand(0));
519 })
520 .Default([](const VPRecipeBase *) { // A value is considered non-uniform
521 // unless proven otherwise.
522 return false;
523 });
524}
525
527 if (auto *RepR = dyn_cast<VPReplicateRecipe>(R))
528 return RepR->doesGeneratePerAllLanes();
529 if (auto *VPI = dyn_cast<VPInstruction>(R))
530 return VPI->doesGeneratePerAllLanes();
531 if (auto *SIVSteps = dyn_cast<VPScalarIVStepsRecipe>(R))
532 return SIVSteps->doesGeneratePerAllLanes();
533 return false;
534}
535
537 auto DepthFirst = vp_depth_first_shallow(Plan.getEntry());
538 auto I = find_if(DepthFirst, [&VPDT](VPBlockBase *VPB) {
539 return VPBlockUtils::isHeader(VPB, VPDT);
540 });
541 return I == DepthFirst.end() ? nullptr : cast<VPBasicBlock>(*I);
542}
543
545 if (!R)
546 return 1;
547 if (auto *RR = dyn_cast<VPReductionPHIRecipe>(R))
548 return RR->getVFScaleFactor();
549 if (auto *RR = dyn_cast<VPReductionRecipe>(R))
550 return RR->getVFScaleFactor();
551 if (auto *ER = dyn_cast<VPExpressionRecipe>(R))
552 return ER->getVFScaleFactor();
553 assert(
556 "getting scaling factor of reduction-start-vector not implemented yet");
557 return 1;
558}
559
560bool vputils::cannotHoistOrSinkRecipe(const VPRecipeBase &R, bool Sinking) {
561 // Assumes don't alias anything or throw; as long as they're guaranteed to
562 // execute, they're safe to hoist. They should however not be sunk, as it
563 // would destroy information.
565 return Sinking;
566 if (R.mayHaveSideEffects() || R.mayReadFromMemory() || R.isPhi())
567 return true;
568 // Allocas cannot be hoisted.
569 auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
570 return RepR && RepR->getOpcode() == Instruction::Alloca;
571}
572
575 VPBasicBlock *LastBB) {
576 assert(FirstBB->getParent() == LastBB->getParent() &&
577 "FirstBB and LastBB from different regions");
578#ifndef NDEBUG
579 bool InSingleSuccChain = false;
580 for (VPBlockBase *Succ = FirstBB; Succ; Succ = Succ->getSingleSuccessor())
581 InSingleSuccChain |= (Succ == LastBB);
582 assert(InSingleSuccChain &&
583 "LastBB unreachable from FirstBB in single-successor chain");
584#endif
585 auto Blocks = to_vector(
587 auto *LastIt = find(Blocks, LastBB);
588 assert(LastIt != Blocks.end() &&
589 "LastBB unreachable from FirstBB in depth-first traversal");
590 Blocks.erase(std::next(LastIt), Blocks.end());
591 return Blocks;
592}
593
595 for (VPRecipeBase &R : *Plan.getVectorPreheader())
597 return cast<VPInstruction>(&R);
598 return nullptr;
599}
600
602vputils::getEarlyExits(const VPlan &Plan, const VPBlockBase *MiddleVPBB) {
604 for (VPIRBasicBlock *ExitVPBB : Plan.getExitBlocks())
605 for (VPBlockBase *Pred : ExitVPBB->getPredecessors())
606 if (Pred != MiddleVPBB)
607 Exits.emplace_back(cast<VPBasicBlock>(Pred), ExitVPBB);
608 return Exits;
609}
610
613 Instruction::BinaryOps InductionOpcode, FPMathOperator *FPBinOp,
614 Instruction *TruncI, VPValue *StartV, VPValue *Step, DebugLoc DL,
615 VPBuilder &Builder, const VPIRFlags::WrapFlagsTy &Flags) {
616 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
617 VPBasicBlock *HeaderVPBB = LoopRegion->getEntryBasicBlock();
618 VPValue *CanonicalIV = LoopRegion->getCanonicalIV();
619 VPSingleDefRecipe *BaseIV =
620 Builder.createDerivedIV(Kind, FPBinOp, StartV, CanonicalIV, Step, Flags);
621
622 // Truncate base induction if needed.
623 Type *ResultTy = BaseIV->getScalarType();
624 if (TruncI) {
625 Type *TruncTy = TruncI->getType();
626 assert(ResultTy->getScalarSizeInBits() > TruncTy->getScalarSizeInBits() &&
627 "Not truncating.");
628 assert(ResultTy->isIntegerTy() && "Truncation requires an integer type");
629 BaseIV = Builder.createScalarCast(Instruction::Trunc, BaseIV, TruncTy, DL);
630 ResultTy = TruncTy;
631 }
632
633 // Truncate step if needed.
634 Type *StepTy = Step->getScalarType();
635 if (ResultTy != StepTy) {
636 assert(StepTy->getScalarSizeInBits() > ResultTy->getScalarSizeInBits() &&
637 "Not truncating.");
638 assert(StepTy->isIntegerTy() && "Truncation requires an integer type");
639 auto *VecPreheader =
641 VPBuilder::InsertPointGuard Guard(Builder);
642 Builder.setInsertPoint(VecPreheader);
643 Step = Builder.createScalarCast(Instruction::Trunc, Step, ResultTy, DL);
644 }
645 return Builder.createScalarIVSteps(InductionOpcode, FPBinOp, BaseIV, Step,
646 &Plan.getVF(), DL);
647}
648
649VPValue *
651 VPlan &Plan, VPBuilder &Builder) {
652 const InductionDescriptor &ID = PtrIV->getInductionDescriptor();
653 VPIRValue *StartV = Plan.getZero(ID.getStep()->getType());
654 VPValue *StepV = PtrIV->getOperand(1);
656 Plan, InductionDescriptor::IK_IntInduction, Instruction::Add, nullptr,
657 nullptr, StartV, StepV, PtrIV->getDebugLoc(), Builder);
658
659 return Builder.createPtrAdd(PtrIV->getStartValue(), Steps,
660 PtrIV->getDebugLoc(), "next.gep");
661}
662
664 const VPDominatorTree &VPDT) {
665 auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
666 if (!VPBB)
667 return false;
668
669 // If VPBB is in a region R, VPBB is a loop header if R is a loop region with
670 // VPBB as its entry, i.e., free of predecessors.
671 if (auto *R = VPBB->getParent())
672 return !R->isReplicator() && !VPBB->hasPredecessors();
673
674 // A header dominates its second predecessor (the latch), with the other
675 // predecessor being the preheader
676 return VPB->getPredecessors().size() == 2 &&
677 VPDT.dominates(VPB, VPB->getPredecessors()[1]);
678}
679
681 const VPDominatorTree &VPDT) {
682 // A latch has a header as its last successor, with its other successors
683 // leaving the loop. A preheader OTOH has a header as its first (and only)
684 // successor.
685 return VPB->getNumSuccessors() >= 2 &&
687}
688
689std::pair<VPBasicBlock *, VPBasicBlock *>
692 Plan.getEntry()->getNumSuccessors() == 1
693 ? Plan.getEntry()->getSingleSuccessor()
694 : Plan.getEntry()->getSuccessors()[1]->getSingleSuccessor());
695 assert(Header->getNumPredecessors() == 2 &&
696 "Header must have exactly 2 predecessors");
697 auto *Latch = cast<VPBasicBlock>(Header->getPredecessors()[1]);
698 return {Header, Latch};
699}
700
704
705std::optional<MemoryLocation>
707 auto *M = dyn_cast<VPIRMetadata>(&R);
708 if (!M)
709 return std::nullopt;
711 // Populate noalias metadata from VPIRMetadata.
712 if (MDNode *NoAliasMD = M->getMetadata(LLVMContext::MD_noalias))
713 Loc.AATags.NoAlias = NoAliasMD;
714 if (MDNode *AliasScopeMD = M->getMetadata(LLVMContext::MD_alias_scope))
715 Loc.AATags.Scope = AliasScopeMD;
716 return Loc;
717}
718
720 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
721 VPRegionValue *CanIV = LoopRegion->getCanonicalIV();
722 assert(CanIV && "Expected loop region to have a canonical IV");
723
724 VPSymbolicValue &VFxUF = Plan.getVFxUF();
725
726 // Check if \p Step matches the expected increment step, accounting for
727 // materialization of VFxUF and UF.
728 auto IsIncrementStep = [&](VPValue *Step) -> bool {
729 if (!VFxUF.isMaterialized())
730 return Step == &VFxUF;
731
732 VPSymbolicValue &UF = Plan.getUF();
733 if (!UF.isMaterialized())
734 return Step == &UF ||
735 match(Step, m_c_Mul(m_Specific(&Plan.getUF()), m_VScale()));
736
737 // Alias masking: step is number of active lanes of a dependence mask.
738 if (match(Step, m_ZExtOrTruncOrSelf(
740 return true;
741
742 unsigned ConcreteUF = Plan.getConcreteUF();
743 // Fixed VF: step is just the concrete UF.
744 if (match(Step, m_SpecificInt(ConcreteUF)))
745 return true;
746
747 // Scalable VF: step involves VScale.
748 if (ConcreteUF == 1)
749 return match(Step, m_VScale());
750 if (match(Step, m_c_Mul(m_SpecificInt(ConcreteUF), m_VScale())))
751 return true;
752 // mul(VScale, ConcreteUF) may have been simplified to
753 // shl(VScale, log2(ConcreteUF)) when ConcreteUF is a power of 2.
754 return isPowerOf2_32(ConcreteUF) &&
755 match(Step, m_Shl(m_VScale(), m_SpecificInt(Log2_32(ConcreteUF))));
756 };
757
758 VPInstruction *Increment = nullptr;
759 for (VPUser *U : CanIV->users()) {
760 VPValue *Step;
761 if (isa<VPInstruction>(U) &&
762 match(U, m_c_Add(m_Specific(CanIV), m_VPValue(Step))) &&
763 IsIncrementStep(Step)) {
764 assert(!Increment && "There must be a unique increment");
766 }
767 }
768
769 assert((!VFxUF.isMaterialized() || Increment) &&
770 "After materializing VFxUF, an increment must exist");
771 assert((!Increment ||
772 LoopRegion->hasCanonicalIVNUW() == Increment->hasNoUnsignedWrap()) &&
773 "NUW flag in region and increment must match");
774 return Increment;
775}
776
777/// Find the ComputeReductionResult recipe for \p PhiR, looking through selects
778/// inserted for predicated reductions or tail folding.
780 VPValue *BackedgeVal = PhiR->getBackedgeValue();
781 if (auto *Res =
783 return Res;
784
785 // Look through selects inserted for tail folding or predicated reductions.
786 VPRecipeBase *SelR =
787 findUserOf(BackedgeVal, m_Select(m_VPValue(), m_VPValue(), m_VPValue()));
788 if (!SelR)
789 return nullptr;
792}
793
796 SmallVector<const VPValue *> WorkList = {V};
797
798 while (!WorkList.empty()) {
799 const VPValue *Cur = WorkList.pop_back_val();
800 if (!Seen.insert(Cur).second)
801 continue;
802
803 auto *Blend = dyn_cast<VPBlendRecipe>(Cur);
804 // Skip blends that use V only through a compare by checking if any incoming
805 // value was already visited.
806 if (Blend && none_of(seq<unsigned>(0, Blend->getNumIncomingValues()),
807 [&](unsigned I) {
808 return Seen.contains(Blend->getIncomingValue(I));
809 }))
810 continue;
811
812 for (VPUser *U : Cur->users()) {
813 if (auto *InterleaveR = dyn_cast<VPInterleaveBase>(U))
814 if (InterleaveR->getAddr() == Cur)
815 return true;
816 // Cur is used as the pointer of a (possibly masked) load (operand 0) or
817 // store (operand 1).
820 m_Specific(Cur)))))
821 return true;
823 if (MemR->getAddr() == Cur && MemR->isConsecutive())
824 return true;
825 }
826 }
827
828 // The legacy cost model only supports scalarization loads/stores with phi
829 // addresses, if the phi is directly used as load/store address. Don't
830 // traverse further for Blends.
831 if (Blend)
832 continue;
833
834 // Only traverse further through users that also define a value (and can
835 // thus have their own users walked). Skip when Cur is only used as mask ,
836 // as well as loads: a loaded value does not depend on the load's operand.
837 for (VPUser *U : Cur->users()) {
838 auto *VPI = dyn_cast<VPInstruction>(U);
839 if (VPI && VPI->getMask() == Cur &&
840 none_of(VPI->operandsWithoutMask(), equal_to(Cur)))
841 continue;
843 continue;
844 if (auto *SDR = dyn_cast<VPSingleDefRecipe>(U))
845 WorkList.push_back(SDR);
846 }
847 }
848 return false;
849}
850
851/// Try to find a loop-invariant IR value for \p S in the plan's entry block
852/// that can be reused. Returns the corresponding live-in VPValue, or nullptr
853/// if no reusable IR value is found.
854VPValue *VPSCEVExpander::tryToReuseIRValue(const SCEV *S) {
856 return nullptr;
857 VPlan &Plan = Builder.getPlan();
858 BasicBlock *PH = cast<VPIRBasicBlock>(Plan.getEntry())->getIRBasicBlock();
859 for (Value *V : SE.getSCEVValues(S)) {
860 // Only reuse instructions in the plan's entry block, or, when a
861 // DominatorTree is available, any instruction that dominates it.
862 // Instructions in sibling branches may not dominate the entry block.
863 auto *I = dyn_cast<Instruction>(V);
864 if (!I)
865 return Plan.getOrAddLiveIn(V);
866 if (!SE.DT.dominates(I->getParent(), PH))
867 continue;
868 SmallVector<Instruction *> DropPoisonGeneratingInsts;
869 if (!SE.canReuseInstruction(S, I, DropPoisonGeneratingInsts))
870 continue;
871 for (Instruction *DropI : DropPoisonGeneratingInsts)
873 return Plan.getOrAddLiveIn(V);
874 }
875 return nullptr;
876}
877
879 if (VPValue *V = tryToReuseIRValue(S))
880 return V;
881
882 switch (S->getSCEVType()) {
883 case scConstant:
884 return Builder.getPlan().getOrAddLiveIn(cast<SCEVConstant>(S)->getValue());
885 case scUnknown:
886 return Builder.getPlan().getOrAddLiveIn(cast<SCEVUnknown>(S)->getValue());
887 case scVScale:
888 return Builder.createVScale(S->getType(), DL);
889 case scAddExpr: {
890 auto *AddE = cast<SCEVAddExpr>(S);
891 VPIRFlags::WrapFlagsTy WrapFlags(AddE->hasNoUnsignedWrap(),
892 AddE->hasNoSignedWrap());
893
894 // Expand pointer SCEVAddExpr as a ptradd of the pointer base and the
895 // integer offset, matching SCEVExpander.
896 if (S->getType()->isPointerTy()) {
897 VPValue *Base = expand(SE.getPointerBase(S));
898 VPValue *Offset = expand(SE.removePointerBase(S));
899 GEPNoWrapFlags GEPFlags = WrapFlags.HasNUW
902 return Builder.createNoWrapPtrAdd(Base, Offset, GEPFlags, DL);
903 }
904
905 // Non-constant-negative add operands are expanded negated and subtracted
906 // from the running result below, instead of being negated and added.
907 auto UseSubtract = [](const SCEV *Op) {
908 return Op->isNonConstantNegative();
909 };
910 // Iterate in reverse so that constants are emitted last, and move the
911 // subtracted operands last, matching SCEVExpander's LoopCompare, so that
912 // they don't start the running result.
913 SmallVector<const SCEV *, 2> SCEVOps(reverse(AddE->operands()));
914 stable_sort(SCEVOps, [&](const SCEV *L, const SCEV *R) {
915 return !UseSubtract(L) && UseSubtract(R);
916 });
918 for (const SCEV *Op : SCEVOps) {
919 // The first operand starts the result, so it is never subtracted.
920 bool Negate = !Ops.empty() && UseSubtract(Op);
921 Ops.push_back(expand(Negate ? SE.getNegativeSCEV(Op) : Op));
922 }
923 VPValue *Result = Ops.front();
924 for (auto [Op, OpV] : drop_begin(zip_equal(SCEVOps, Ops))) {
925 if (UseSubtract(Op)) {
926 // Result + (-Op) == Result - Op, which saves the multiply for the
927 // negation. NSW only transfers if negating Op cannot overflow, see
928 // ScalarEvolution::getMinusSCEV.
929 bool HasNSW =
930 WrapFlags.HasNSW && !SE.getSignedRangeMin(Op).isMinSignedValue();
931 Result = Builder.createOverflowingOp(Instruction::Sub, {Result, OpV},
932 {/*HasNUW=*/false, HasNSW}, DL);
933 continue;
934 }
935 Result = Builder.createOverflowingOp(Instruction::Add, {Result, OpV},
936 WrapFlags, DL);
937 }
938 return Result;
939 }
940 case scMulExpr: {
941 auto *MulE = cast<SCEVMulExpr>(S);
942 VPIRFlags::WrapFlagsTy WrapFlags(MulE->hasNoUnsignedWrap(),
943 MulE->hasNoSignedWrap());
945 for (const SCEV *Op : reverse(MulE->operands()))
946 Ops.push_back(expand(Op));
947 VPValue *Result = Ops.front();
948 for (VPValue *OpV : drop_begin(Ops)) {
949 Result = Builder.createOverflowingOp(Instruction::Mul, {Result, OpV},
950 WrapFlags, DL);
951 }
952 return Result;
953 }
954 case scUDivExpr: {
955 auto *UDiv = cast<SCEVUDivExpr>(S);
956 VPValue *LHS = expand(UDiv->getLHS());
957 const SCEV *RHSExpr = UDiv->getRHS();
958 VPValue *RHS = expand(RHSExpr);
959 if (SafeUDivMode) {
960 // Make sure the UDiv's divisor is guaranteed to not be zero/poison, to
961 // avoid UB.
962 Type *Ty = UDiv->getType();
963 bool GuaranteedNotPoison =
965 if (!GuaranteedNotPoison)
966 RHS = Builder.createScalarFreeze(RHS, DL);
967 if (!SE.isKnownNonZero(RHSExpr) || !GuaranteedNotPoison)
968 RHS = Builder.createScalarIntrinsic(
969 Intrinsic::umax, {RHS, Builder.getPlan().getConstantInt(Ty, 1)}, Ty,
970 DL);
971 }
972 return Builder.createNaryOp(Instruction::UDiv, {LHS, RHS},
973 VPIRFlags::getDefaultFlags(Instruction::UDiv),
974 DL);
975 }
976 case scTruncate:
977 case scZeroExtend:
978 case scSignExtend:
979 case scPtrToAddr: {
980 auto *Cast = cast<SCEVCastExpr>(S);
981 VPValue *Op = expand(Cast->getOperand());
983 switch (S->getSCEVType()) {
984 case scTruncate:
985 Opcode = Instruction::Trunc;
986 break;
987 case scZeroExtend:
988 Opcode = Instruction::ZExt;
989 break;
990 case scSignExtend:
991 Opcode = Instruction::SExt;
992 break;
993 case scPtrToAddr:
994 Opcode = Instruction::PtrToAddr;
995 break;
996 default:
997 llvm_unreachable("Unhandled cast SCEV");
998 }
999
1000 // When expanding ptrtoaddr, first check if there's an existing ptrtoint we
1001 // can reuse.
1002 if (Opcode == Instruction::PtrToAddr) {
1003 VPlan &Plan = Builder.getPlan();
1004 BasicBlock *PH = cast<VPIRBasicBlock>(Plan.getEntry())->getIRBasicBlock();
1005 if (auto *IRV = dyn_cast<VPIRValue>(Op)) {
1007 IRV->getValue(), S->getType(), PH->getDataLayout(),
1008 [&](const CastInst *CI) {
1009 return SE.DT.dominates(CI->getParent(), PH);
1010 }))
1011 return Plan.getOrAddLiveIn(CI);
1012 }
1013 }
1014
1015 std::optional<VPIRFlags> Flags;
1016 if (Opcode == Instruction::ZExt)
1017 Flags =
1018 VPIRFlags::NonNegFlagsTy(SE.isKnownNonNegative(Cast->getOperand()));
1019
1020 return Builder.createScalarCast(Opcode, Op, S->getType(), DL, Flags);
1021 }
1022 case scUMaxExpr:
1023 case scSMaxExpr:
1024 case scUMinExpr:
1025 case scSMinExpr:
1026 case scSequentialUMinExpr: {
1027 auto *MinMax = cast<SCEVNAryExpr>(S);
1028 Intrinsic::ID IntrinsicID;
1029 switch (S->getSCEVType()) {
1030 case scUMaxExpr:
1031 IntrinsicID = Intrinsic::umax;
1032 break;
1033 case scSMaxExpr:
1034 IntrinsicID = Intrinsic::smax;
1035 break;
1036 case scUMinExpr:
1038 IntrinsicID = Intrinsic::umin;
1039 break;
1040 case scSMinExpr:
1041 IntrinsicID = Intrinsic::smin;
1042 break;
1043 default:
1044 llvm_unreachable("Unexpected min/max SCEV type");
1045 }
1046 // Chain operands in reverse order matching SCEVExpander's expansion of
1047 // min/max expressions. In SafeUDivMode freeze expansion results of operands
1048 // other than the first for sequential UMins, to avoid short-circuiting
1049 // divide-by-0/poison.
1050 bool IsSequential = S->getSCEVType() == scSequentialUMinExpr;
1051 Type *ResultTy = MinMax->getType();
1052 bool PrevSafeMode = SafeUDivMode;
1054 for (const SCEV *SCEVOp : reverse(MinMax->operands())) {
1055 bool MayShortCircuit =
1056 IsSequential && Ops.size() != MinMax->getNumOperands() - 1;
1057 SafeUDivMode = MayShortCircuit || PrevSafeMode;
1058 VPValue *OpV = expand(SCEVOp);
1059 SafeUDivMode = PrevSafeMode;
1060 if (MayShortCircuit)
1061 OpV = Builder.createScalarFreeze(OpV, DL);
1062 Ops.push_back(OpV);
1063 }
1064 VPValue *Result = Ops.front();
1065 for (VPValue *Op : drop_begin(Ops))
1066 Result = Builder.createScalarIntrinsic(IntrinsicID, {Result, Op},
1067 ResultTy, DL);
1068 return Result;
1069 }
1070 case scAddRecExpr: {
1071 auto *AR = cast<SCEVAddRecExpr>(S);
1072 VPlan &Plan = Builder.getPlan();
1073 [[maybe_unused]] BasicBlock *PH =
1074 cast<VPIRBasicBlock>(Plan.getEntry())->getIRBasicBlock();
1075 assert(SE.DT.dominates(AR->getLoop()->getHeader(), PH) &&
1076 "can only expand AddRecs for loops outside VPlan's scope");
1077
1078 // Try to expand AR by re-using an existing canonical IV in the Plan's
1079 // entry. A canonical IV must be affine and integer typed.
1080 if (!AR->isAffine() || !AR->getType()->isIntegerTy())
1082 auto FoundCanIV =
1083 find_if(Plan.getEntry()->phis(), [&](const VPRecipeBase &R) {
1084 if (!SE.isSCEVable(cast<VPIRPhi>(R).getIRPhi().getType()))
1085 return false;
1086 const SCEV *Candidate = SE.getSCEV(&cast<VPIRPhi>(R).getIRPhi());
1087 return match(Candidate,
1088 m_scev_AffineAddRec(m_scev_Zero(), m_scev_One(),
1089 m_SpecificLoop(AR->getLoop()))) &&
1090 Candidate->getType() == AR->getType();
1091 });
1092 if (FoundCanIV == Plan.getEntry()->phis().end())
1094
1095 // {Start, +, Step} --> Start + IV * Step, since the AddRec is affine.
1096 // Compute Offset = IV * Step.
1097 VPValue *Start = expand(AR->getStart());
1098 Value *CanonicalIV = &cast<VPIRPhi>(FoundCanIV)->getIRPhi();
1100 SE.getMulExpr(SE.getUnknown(CanonicalIV), AR->getStepRecurrence(SE)));
1101
1102 // Compute Start + Offset with nuw from the AddRec.
1103 return Builder.createAdd(Start, Offset, DL, "",
1104 {AR->hasNoUnsignedWrap(), false});
1105 }
1106 case scCouldNotCompute:
1107 llvm_unreachable("Attempt to expand a SCEVCouldNotCompute");
1108 }
1109 llvm_unreachable("Unknown SCEV kind!");
1110}
1111
1113 // Do remove conditional assume instructions as their conditions may be
1114 // flattened.
1115 auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
1116 bool IsConditionalAssume = RepR && RepR->isPredicated() &&
1118 if (IsConditionalAssume)
1119 return true;
1120
1121 if (R.mayHaveSideEffects())
1122 return false;
1123
1124 // Forbid removing trip-count expressions.
1125 if (isa<VPExpandSCEVRecipe>(R) &&
1126 R.getVPSingleValue() == R.getParent()->getPlan()->getTripCount())
1127 return false;
1128
1129 // Recipe is dead if no user keeps the recipe alive.
1130 return all_of(R.definedValues(), [](VPValue *V) { return V->user_empty(); });
1131}
1132
1134 SmallVector<VPValue *> WorkList;
1136 WorkList.push_back(V);
1137
1138 while (!WorkList.empty()) {
1139 VPValue *Cur = WorkList.pop_back_val();
1140 if (!Seen.insert(Cur).second)
1141 continue;
1142 VPRecipeBase *R = Cur->getDefiningRecipe();
1143 if (!R)
1144 continue;
1145 if (!isDeadRecipe(*R))
1146 continue;
1147 append_range(WorkList, R->operands());
1148 R->eraseFromParent();
1149 }
1150}
1151
1154 for (unsigned I = 0; I != Users.size(); ++I) {
1156 for (VPValue *V : Cur->definedValues())
1157 Users.insert_range(V->users());
1158 }
1159 return Users.takeVector();
1160}
1161
1162/// Returns \p Num / \p Denom as a BranchProbability, clamped so a ratio that is
1163/// neither zero nor one does not round to zero or one. BlockFrequencyInfo also
1164/// keeps a zero-weight edge distinguishable from an unreachable one.
1166 uint64_t Denom) {
1168 if (Num == 0 || Num == Denom)
1169 return P;
1170 return BranchProbability::getRaw(std::clamp(
1171 P.getNumerator(), 1u, BranchProbability::getDenominator() - 1));
1172}
1173
1178
1179/// Returns the probability of reaching each unique successor of \p VPBB, taken
1180/// from the branch weights recorded on its terminator, or unknown if not
1181/// available. See llvm::getBranchProbability in
1182/// llvm/Transforms/Utils/LoopUtils.h for the IR version.
1185 ArrayRef<VPBlockBase *> Successors = VPBB->getSuccessors();
1186 // With a single successor the edge is always taken and needs no weights.
1187 if (VPBlockBase *Succ = VPBB->getSingleSuccessor())
1189
1190 // Take the branch weights off the terminator. Without usable weights all
1191 // successors have unknown probability; zero the weights, so the accumulation
1192 // below still visits each of them.
1193 SmallVector<uint32_t> Weights;
1195 if (!Term ||
1196 !extractBranchWeights(Term->getMetadata(LLVMContext::MD_prof), Weights) ||
1197 Weights.size() != Successors.size())
1198 Weights.assign(Successors.size(), 0);
1199 uint64_t Total = sum_of(Weights, uint64_t(0));
1200
1201 // Sum the weights of parallel edges to the same successor, so that the
1202 // division below rounds once per successor rather than once per edge.
1204 for (const auto &[Succ, Weight] : zip_equal(Successors, Weights))
1205 WeightPerSuccessor[cast<VPBasicBlock>(Succ)] += Weight;
1206
1207 return map_to_vector<2>(WeightPerSuccessor, [Total](const auto &SuccWeight) {
1208 auto [Succ, Weight] = SuccWeight;
1209 if (Total == 0)
1210 return std::make_pair(Succ, BranchProbability::getUnknown());
1211 return std::make_pair(Succ,
1213 });
1214}
1215
1216/// Returns \p Freq scaled by \p Prob, rounding up to 1 instead of 0 to keep a
1217/// rarely executed block distinguishable from an unreachable one.
1219 BranchProbability Prob) {
1220 BlockFrequency Scaled = Freq * Prob;
1221 if (Scaled == BlockFrequency() && Freq != BlockFrequency() && !Prob.isZero())
1222 return BlockFrequency(1);
1223 return Scaled;
1224}
1225
1228 assert(!Blocks.empty() && "expected at least the header block");
1229 // Push each block's frequency along its outgoing edges. Blocks is a DAG in
1230 // reverse post-order (the loop region's backedge is implicit), so a block's
1231 // frequency is final by the time it is visited.
1233 Frequencies.reserve(Blocks.size());
1234 // The header (first block) always executes, the others start out unreachable.
1235 Frequencies[Blocks.front()] = BlockFrequency(AlwaysExecutesFreq);
1236 for (VPBasicBlock *VPBB : Blocks.drop_front())
1237 Frequencies[VPBB] = BlockFrequency();
1238
1239 for (VPBasicBlock *VPBB : Blocks) {
1240 std::optional<BlockFrequency> SrcFreq = Frequencies.at(VPBB);
1241 for (const auto &[Succ, EdgeProb] : getSuccessorProbabilities(VPBB)) {
1242 std::optional<BlockFrequency> &SuccFreq = Frequencies.at(Succ);
1243 // An unknown edge or predecessor poisons the successor.
1244 if (!SrcFreq || EdgeProb.isUnknown() || !SuccFreq) {
1245 SuccFreq = std::nullopt;
1246 continue;
1247 }
1248 // The sum can only exceed AlwaysExecutesFreq by rounding.
1249 SuccFreq = std::min(BlockFrequency(AlwaysExecutesFreq),
1250 *SuccFreq + scaleKeepingNonZero(*SrcFreq, EdgeProb));
1251 }
1252 }
1253 return Frequencies;
1254}
1255
1258 const DataLayout &DL) {
1259 auto OpcodeOrIID = getOpcodeOrIntrinsicID(&R);
1260 if (!OpcodeOrIID)
1261 return nullptr;
1262
1264 for (VPValue *Op : Operands) {
1265 VPValue *Candidate = Op;
1266 match(Op, m_Broadcast(m_VPValue(Candidate)));
1267 if (!match(Candidate, m_LiveIn()))
1268 return nullptr;
1269 Value *V = Candidate->getUnderlyingValue();
1270 if (!V)
1271 return nullptr;
1272 Ops.push_back(V);
1273 }
1274
1275 VPlan &Plan = *R.getParent()->getPlan();
1276 auto FoldToIRValue = [&]() -> Value * {
1277 InstSimplifyFolder Folder(DL);
1278 if (OpcodeOrIID->first) {
1279 // VPInstructions store the called intrinsic as last operand.
1280 if (isa<VPInstruction>(R))
1281 Ops.pop_back();
1282
1283 auto *RFlags = dyn_cast<VPRecipeWithIRFlags>(&R);
1284 return Folder.FoldIntrinsic(OpcodeOrIID->second, Ops, R.getScalarType(),
1285 RFlags ? RFlags->getFastMathFlagsOrNone()
1286 : FastMathFlags());
1287 }
1288 unsigned Opcode = OpcodeOrIID->second;
1289 if (Instruction::isBinaryOp(Opcode))
1290 return Folder.FoldBinOp(static_cast<Instruction::BinaryOps>(Opcode),
1291 Ops[0], Ops[1]);
1292 if (Instruction::isCast(Opcode))
1293 return Folder.FoldCast(static_cast<Instruction::CastOps>(Opcode), Ops[0],
1294 R.getVPSingleValue()->getScalarType());
1295 switch (Opcode) {
1296 case VPInstruction::Not:
1297 return Folder.FoldBinOp(Instruction::BinaryOps::Xor, Ops[0],
1299 case Instruction::Select:
1300 return Folder.FoldSelect(Ops[0], Ops[1], Ops[2]);
1301 case Instruction::ICmp:
1302 case Instruction::FCmp:
1303 return Folder.FoldCmp(cast<VPRecipeWithIRFlags>(R).getPredicate(), Ops[0],
1304 Ops[1]);
1305 case Instruction::GetElementPtr: {
1306 auto &RFlags = cast<VPRecipeWithIRFlags>(R);
1307 auto *GEP = cast<GetElementPtrInst>(RFlags.getUnderlyingInstr());
1308 return Folder.FoldGEP(GEP->getSourceElementType(), Ops[0],
1309 drop_begin(Ops), RFlags.getGEPNoWrapFlags());
1310 }
1313 return Folder.FoldGEP(IntegerType::getInt8Ty(Plan.getContext()), Ops[0],
1314 Ops[1],
1315 cast<VPRecipeWithIRFlags>(R).getGEPNoWrapFlags());
1316 // An extract of a live-in is an extract of a broadcast, so return the
1317 // broadcasted element.
1318 case Instruction::ExtractElement:
1319 assert(!Ops[0]->getType()->isVectorTy() && "Live-ins should be scalar");
1320 return Ops[0];
1321 }
1322 return nullptr;
1323 };
1324
1325 if (Value *V = FoldToIRValue())
1326 return Plan.getOrAddLiveIn(V);
1327 return nullptr;
1328}
1329
1331 VPlan &Plan, function_ref<VPValue *(VPValue *Op)> MatchPerm,
1334 vp_depth_first_deep(Plan.getEntry()))) {
1335 for (VPRecipeBase &R : make_early_inc_range(*VPBB)) {
1336 auto *Def = dyn_cast<VPSingleDefRecipe>(&R);
1337 if (!Def || !isElementwise(Def))
1338 continue;
1339
1340 // At least one of the ops must be a permutation.
1341 if (none_of(Def->operands(), MatchPerm))
1342 continue;
1343
1344 // All operands must be a single-use permutation or a live in (splat).
1345 if (!all_of(Def->operands(), [&MatchPerm](VPValue *Op) {
1346 return (Op->hasOneUse() && MatchPerm(Op)) || match(Op, m_LiveIn());
1347 }))
1348 continue;
1349
1350 // Remove the inner permutations.
1351 for (unsigned I = 0, E = Def->getNumOperands(); I != E; ++I)
1352 if (VPValue *X = MatchPerm(Def->getOperand(I)))
1353 Def->setOperand(I, X);
1354
1355 VPSingleDefRecipe *Res = BuildPerm(Def);
1356 Res->insertAfter(Def);
1357 Def->replaceUsesWithIf(
1358 Res, [&Res](VPUser &U, unsigned _) { return &U != Res; });
1359 }
1360 }
1361}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
unsigned uint64_t
@ Scaled
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
Hexagon Common GEP
#define _
iv Induction Variable Users
Definition IVUsers.cpp:48
static constexpr Value * getValue(Ty &ValueOrUse)
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 implements a map that provides insertion order iteration.
This file provides utility analysis objects describing memory locations.
#define P(N)
This file contains the declarations for profiling metadata utility functions.
SI Fold Operands
This file implements a set that has insertion order iteration characteristics.
This file defines less commonly used SmallVector utilities.
static unsigned getScalarSizeInBits(Type *Ty)
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
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 BranchProbability getBranchProbabilityKeepingPartial(uint64_t Num, uint64_t Denom)
Returns Num / Denom as a BranchProbability, clamped so a ratio that is neither zero nor one does not ...
static BlockFrequency scaleKeepingNonZero(BlockFrequency Freq, BranchProbability Prob)
Returns Freq scaled by Prob, rounding up to 1 instead of 0 to keep a rarely executed block distinguis...
static bool preservesUniformity(unsigned Opcode)
Returns true if Opcode preserves uniformity, i.e., if all operands are uniform, the result will also ...
static SmallVector< std::pair< const VPBasicBlock *, BranchProbability >, 2 > getSuccessorProbabilities(const VPBasicBlock *VPBB)
Returns the probability of reaching each unique successor of VPBB, taken from the branch weights reco...
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
ArrayRef< T > drop_front(size_t N=1) const
Drop the first N elements of the array.
Definition ArrayRef.h:194
const T & front() const
Get the first element.
Definition ArrayRef.h:144
size_t size() const
Get the array size.
Definition ArrayRef.h:141
bool empty() const
Check if the array is empty.
Definition ArrayRef.h:136
LLVM Basic Block Representation.
Definition BasicBlock.h:62
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this basic block belongs to.
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
static constexpr BranchProbability getOne()
static uint32_t getDenominator()
static constexpr BranchProbability getUnknown()
static constexpr BranchProbability getRaw(uint32_t N)
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:512
static LLVM_ABI Constant * getAllOnesValue(Type *Ty)
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
A debug info location.
Definition DebugLoc.h:126
ValueT & at(const_arg_type_t< KeyT > Val)
Return the entry for the specified key, or abort if no such entry exists.
Definition DenseMap.h:268
void reserve(size_type NumEntries)
Grow the densemap so that it can contain at least NumEntries items before resizing again.
Definition DenseMap.h:176
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.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:202
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags noUnsignedWrap()
static GEPNoWrapFlags none()
A struct for saving information about induction variables.
InductionKind
This enum represents the kinds of inductions that we support.
@ IK_IntInduction
Integer induction variable. Step = C.
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
bool isCast() const
bool isBinaryOp() const
bool isUnaryOp() 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.
static LLVM_ABI CastInst * findReusableCastForPtrToAddr(Value *PtrOp, Type *Ty, const DataLayout &DL, function_ref< bool(const CastInst *)> Dominates)
Find an existing cast among PtrOp's users that computes the same value as a ptrtoaddr of PtrOp to Ty ...
This class represents an analyzed expression in the program.
static constexpr auto FlagAnyWrap
static constexpr auto FlagNSW
Type * getType() const
Return the LLVM type of this SCEV expression.
SCEVTypes getSCEVType() const
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 bool isKnownNonNegative(const SCEV *S)
Test if the given expression is known to be non-negative.
LLVM_ABI const SCEV * getZeroExtendExpr(SCEVUse Op, Type *Ty, unsigned Depth=0)
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.
static LLVM_ABI bool isGuaranteedNotToBePoison(const SCEV *Op)
Returns true if Op is guaranteed to not be poison.
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 * getTruncateExpr(SCEVUse 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 * getSignExtendExpr(SCEVUse 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.
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 * getPtrToAddrExpr(const SCEV *Op)
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.
A vector that has set insertion semantics.
Definition SetVector.h:57
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 assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
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
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:232
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:4453
iterator end()
Definition VPlan.h:4490
iterator_range< iterator > phis()
Returns an iterator range over the PHI-like recipes in the block.
Definition VPlan.h:4541
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:266
VPRecipeBase * getTerminator()
If the block has multiple successors, return the branch recipe terminating the block.
Definition VPlan.cpp:663
void insert(VPRecipeBase *Recipe, iterator InsertPt)
Definition VPlan.h:4519
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:95
VPRegionBlock * getParent()
Definition VPlan.h:193
size_t getNumSuccessors() const
Definition VPlan.h:244
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:229
VPlan * getPlan()
Definition VPlan.cpp:211
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:216
VPBlockBase * getSingleHierarchicalPredecessor()
Definition VPlan.h:280
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:234
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:218
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:404
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.
RAII object that stores the current insertion point and restores it when the object is destroyed.
VPlan-based builder utility analogous to IRBuilder.
ArrayRef< VPRecipeValue * > definedValues()
Returns an ArrayRef of the values defined by the VPDef.
Definition VPlanValue.h:573
A recipe for converting Current into Start + Current * Step.
Definition VPlan.h:4234
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
Recipe to expand a SCEV expression.
Definition VPlan.h:4066
virtual VPValue * getBackedgeValue()
Returns the incoming value from the loop backedge.
Definition VPlan.h:2529
A special type of VPBasicBlock that wraps an existing IR basic block.
Definition VPlan.h:4606
static VPIRFlags getDefaultFlags(unsigned Opcode, Type *ResultTy=nullptr)
Returns default flags for Opcode and scalar ResultTy for opcodes that support it, asserts otherwise.
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:1266
@ ReductionStartVector
Start vector for reductions with 3 operands: the original start value, the identity value for the red...
Definition VPlan.h:1363
unsigned getOpcode() const
Definition VPlan.h:1460
bool isVectorToScalar() const
Returns true if this VPInstruction produces a scalar value from a vector, e.g.
bool isSingleScalar() const
Returns true if the recipe produces a single scalar value.
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:412
DebugLoc getDebugLoc() const
Returns the debug location of the recipe.
Definition VPlan.h:562
void insertAfter(VPRecipeBase *InsertPos)
Insert an unlinked Recipe into a basic block immediately after the specified Recipe.
Type * getScalarType() const
Returns the scalar type of this VPRecipeValue.
Definition VPlanValue.h:354
A recipe for handling reduction phis.
Definition VPlan.h:2899
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:4678
bool isReplicator() const
An indicator whether this region is to generate multiple replicated instances of output IR correspond...
Definition VPlan.h:4754
bool hasCanonicalIVNUW() const
Indicates if NUW is set for the canonical IV increment, for loop regions.
Definition VPlan.h:4842
VPRegionValue * getCanonicalIV()
Return the canonical induction variable of the region, null for replicating regions.
Definition VPlan.h:4798
VPValues are defined by a VPRegionBlock, like the canonical IV.
Definition VPlanValue.h:252
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:3436
VPValue * expand(const SCEV *S)
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:4295
VPSingleDefRecipe is a base class for recipes that model a sequence of one or more output IR that def...
Definition VPlan.h:620
A symbolic live-in VPValue, used for values like vector trip count, VF, and VFxUF.
Definition VPlanValue.h:217
bool isMaterialized() const
Returns true if this value has been materialized.
Definition VPlanValue.h:235
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:401
operand_range operands()
Definition VPlanValue.h:474
VPValue * getOperand(unsigned N) const
Definition VPlanValue.h:442
This is the base class of the VPlan Def/Use graph, used for modeling the data flow into,...
Definition VPlanValue.h:50
Type * getScalarType() const
Returns the scalar type of this VPValue, dispatching based on the concrete subclass.
Definition VPlan.cpp:149
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition VPlan.cpp:130
Value * getUnderlyingValue() const
Return the underlying Value attached to this VPValue.
Definition VPlanValue.h:75
user_range users()
Definition VPlanValue.h:157
VPWidenCastRecipe is a recipe to create vector cast instructions.
Definition VPlan.h:1925
A recipe for handling GEP instructions.
Definition VPlan.h:2252
VPValue * getStartValue() const
Returns the start value of the induction.
Definition VPlan.h:2602
const InductionDescriptor & getInductionDescriptor() const
Returns the induction descriptor for the recipe.
Definition VPlan.h:2625
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition VPlan.h:2654
A recipe for widened phis.
Definition VPlan.h:2786
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1859
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4865
LLVMContext & getContext() const
Definition VPlan.h:5075
VPBasicBlock * getEntry()
Definition VPlan.h:4961
VPSymbolicValue & getVFxUF()
Returns VF * UF of the vector loop region.
Definition VPlan.h:5073
ArrayRef< VPIRBasicBlock * > getExitBlocks() const
Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of the original scalar loop.
Definition VPlan.h:5027
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:5147
VPIRValue * getZero(Type *Ty)
Return a VPIRValue wrapping the null value of type Ty.
Definition VPlan.h:5173
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1086
unsigned getConcreteUF() const
Returns the concrete UF of the plan, after unrolling.
Definition VPlan.h:5125
VPBasicBlock * getVectorPreheader() const
Returns the preheader of the vector loop region, if one exists, or null otherwise.
Definition VPlan.h:4966
VPSymbolicValue & getUF()
Returns the UF of the vector loop region.
Definition VPlan.h:5070
VPBasicBlock * getScalarPreheader() const
Return the VPBasicBlock for the preheader of the scalar loop.
Definition VPlan.h:5017
VPSymbolicValue & getVF()
Returns the VF of the vector loop region.
Definition VPlan.h:5066
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.
IteratorT end() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
CastOperator_match< OpTy, Instruction::PtrToAddr > m_PtrToAddr(const OpTy &Op)
Matches PtrToAddr.
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.
auto match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
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)
auto m_VScale()
Matches a call to llvm.vscale().
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.
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
BinaryOp_match< LHS, RHS, Instruction::SDiv > m_SDiv(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(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< Instruction::Or, Op0_t, Op1_t > m_BinaryOr(const Op0_t &Op0, const Op1_t &Op1)
Match a binary OR 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)
auto m_GetElementPtr(const Op0_t &Op0, const Op1_t &Op1)
auto m_VPValue()
Match an arbitrary VPValue and ignore it.
VPInstruction_match< VPInstruction::ExtractVectorForPart, Op0_t, Op1_t > m_ExtractVectorForPart(const Op0_t &Op0, const Op1_t &Op1)
VPRecipeBase * findUserOf(VPValue *V, const MatchT &P)
If V is used by a recipe matching pattern P, return it.
VPInstruction_match< VPInstruction::Broadcast, Op0_t > m_Broadcast(const Op0_t &Op0)
match_bind< VPInstruction > m_VPInstruction(VPInstruction *&V)
Match a VPInstruction, capturing if we match.
void pullOutPermutationsImpl(VPlan &Plan, function_ref< VPValue *(VPValue *Op)> Perm, function_ref< VPSingleDefRecipe *(VPSingleDefRecipe *X)> Build)
Template-independent implementation for pullOutPermutations.
BranchProbability getExecutionProbability(BlockFrequency Freq)
Returns Freq as a BranchProbability, relative to AlwaysExecutesFreq.
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.
unsigned getOpcode(const VPValue *V)
Return the instruction opcode for the recipe defining V or 0 for unsupported recipes and VPValues not...
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.
Intrinsic::ID getIntrinsicID(const Ty *R)
Return the intrinsic ID underlying a call.
Definition VPlanUtils.h:87
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.
VPIRValue * tryToFoldLiveIns(VPSingleDefRecipe &R, ArrayRef< VPValue * > Operands, const DataLayout &DL)
Try to fold R using InstSimplifyFolder.
SmallVector< std::pair< VPBasicBlock *, VPIRBasicBlock * > > getEarlyExits(const VPlan &Plan, const VPBlockBase *MiddleVPBB)
Returns the (early exiting block, exit block) pairs of Plan, i.e.
VPValue * findIncomingAliasMask(const VPlan &Plan)
Finds the incoming alias-mask within the vector preheader.
constexpr uint64_t AlwaysExecutesFreq
Denominator of the frequencies computed by computeExecutionFrequencies, i.e.
Definition VPlanUtils.h:229
DenseMap< const VPBasicBlock *, std::optional< BlockFrequency > > computeExecutionFrequencies(ArrayRef< VPBasicBlock * > Blocks)
Computes for each block in Blocks, which must be in reverse post-order, the frequency with which it e...
void recursivelyDeleteDeadRecipes(VPValue *V)
Recursively delete V and any of its operands that become dead.
bool doesGeneratePerAllLanes(const VPRecipeBase *R)
Returns true if R produces scalar values for all VF lanes.
bool isDeadRecipe(VPRecipeBase &R)
Returns true if R is dead, i.e.
bool isElementwise(const VPValue *V)
Return true if V is elementwise, i.e. none of the lanes are permuted.
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.
std::optional< std::pair< bool, unsigned > > getOpcodeOrIntrinsicID(const VPValue *V)
Get the instruction opcode or intrinsic ID for the recipe defining V.
VPValue * scalarizeVPWidenPointerInduction(VPWidenPointerInductionRecipe *PtrIV, VPlan &Plan, VPBuilder &Builder)
Scalarize a VPWidenPointerInductionRecipe by replacing it with a PtrAdd (IndStart,...
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.
SmallVector< VPUser * > collectUsersRecursively(VPValue *V)
Collect all users of V, looking through recipes that define other values.
VPScalarIVStepsRecipe * createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind, Instruction::BinaryOps InductionOpcode, FPMathOperator *FPBinOp, Instruction *TruncI, VPValue *StartV, VPValue *Step, DebugLoc DL, VPBuilder &Builder, const VPIRFlags::WrapFlagsTy &Flags={})
Create a scalar-iv-steps recipe over Plan's canonical IV for an induction of Kind with InductionOpcod...
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
@ Offset
Definition DWP.cpp:577
void stable_sort(R &&Range)
Definition STLExtras.h:2116
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:1765
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:1739
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
Definition STLExtras.h:840
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto map_to_vector(ContainerTy &&C, FuncTy &&F)
Map a range to a SmallVector with element types deduced from the mapping.
constexpr from_range_t from_range
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
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:250
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:285
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
Definition STLExtras.h:2173
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:1746
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:326
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:280
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:1753
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 sum_of(R &&Range, E Init=E{0})
Returns the sum of all values in Range with Init initial value.
Definition STLExtras.h:1717
LLVM_ABI bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
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:1772
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
@ 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
A MapVector that performs no allocations if smaller than a certain size.
Definition MapVector.h:342
A VPValue representing a live-in from the input IR or a constant.
Definition VPlanValue.h:279
A recipe for widening load operations with vector-predication intrinsics, using the address to load f...
Definition VPlan.h:3906
A recipe for widening load operations, using the address to load from and an optional mask.
Definition VPlan.h:3853