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"
27#include "llvm/IR/Dominators.h"
30
31using namespace llvm;
32using namespace llvm::VPlanPatternMatch;
33using namespace llvm::SCEVPatternMatch;
34
36 if (isa<VPBlendRecipe>(U))
37 return true;
38 auto *VPI = dyn_cast<VPInstruction>(U);
39 if (!VPI)
40 return false;
41 unsigned Opcode = VPI->getOpcode();
42 switch (Opcode) {
43 case Instruction::ExtractValue:
44 case Instruction::FCmp:
45 case Instruction::ICmp:
46 case Instruction::Select:
47 case Instruction::Freeze:
49 // TODO: Cover additional opcodes.
50 return true;
52 return Op != VPI->getOperand(0);
53 default:
54 return Instruction::isBinaryOp(Opcode) || Instruction::isCast(Opcode);
55 }
56}
57
60 Worklist.insert(Def);
61 for (unsigned I = 0; I != Worklist.size(); ++I) {
62 const VPValue *Cur = Worklist[I];
63 for (const VPUser *U : Cur->users()) {
64 if (propagatesFirstLaneUse(U, Cur))
66 else if (!U->usesFirstLaneOnly(Cur))
67 return false;
68 }
69 }
70 return true;
71}
72
74 return all_of(Def->users(),
75 [Def](const VPUser *U) { return U->usesFirstPartOnly(Def); });
76}
77
79 return all_of(Def->users(),
80 [Def](const VPUser *U) { return U->usesScalars(Def); });
81}
82
84 bool ExpandInIR) {
85 if (auto *E = dyn_cast<SCEVConstant>(Expr))
86 return Plan.getOrAddLiveIn(E->getValue());
87 // Skip SCEV expansion if Expr is a SCEVUnknown wrapping a non-instruction
88 // value. Otherwise the value may be defined in a loop and using it directly
89 // will break LCSSA form. The SCEV expansion takes care of preserving LCSSA
90 // form.
91 auto *U = dyn_cast<SCEVUnknown>(Expr);
92 if (U && !isa<Instruction>(U->getValue()))
93 return Plan.getOrAddLiveIn(U->getValue());
94 auto *Expanded = new VPExpandSCEVRecipe(Expr, ExpandInIR);
95 VPBasicBlock *EntryVPBB = Plan.getEntry();
96 auto Iter = EntryVPBB->getFirstNonPhi();
97 while (Iter != EntryVPBB->end() && isa<VPIRInstruction>(*Iter))
98 ++Iter;
99 Expanded->insertBefore(Iter);
100 return Expanded;
101}
102
103/// Returns true if \p V being poison is guaranteed to trigger UB because it
104/// propagates to the address of a memory recipe.
105static bool poisonGuaranteesUB(const VPValue *V) {
108
109 auto PropagatesPoisonFromRecipeOp = [](const VPRecipeBase *R) {
111 return false;
112 unsigned Opcode = vputils::getOpcode(R->getVPSingleValue());
113 return Instruction::isCast(Opcode) || Opcode == Instruction::GetElementPtr;
114 };
115
116 Worklist.push_back(V);
117
118 while (!Worklist.empty()) {
119 const VPValue *Current = Worklist.pop_back_val();
120 if (!Visited.insert(Current).second)
121 continue;
122
123 for (VPUser *U : Current->users()) {
124 // Check if Current is used as an address operand for load/store.
125 auto *R = cast<VPRecipeBase>(U);
126 if (auto *MemR = dyn_cast<VPWidenMemoryRecipe>(R)) {
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 for (const VPValue *Op : R->operands()) {
140 if (Op == Current && PropagatesPoisonFromRecipeOp(R)) {
141 Worklist.push_back(R->getVPSingleValue());
142 break;
143 }
144 }
145 }
146 }
147
148 return false;
149}
150
152 // Like IR stripPointerCasts, look through GEPs with all-zero indices and
153 // casts to find a root GEP VPInstruction.
154 while (auto *PtrVPI = dyn_cast<VPInstruction>(Ptr)) {
155 unsigned Opcode = PtrVPI->getOpcode();
156 if (Opcode == Instruction::GetElementPtr) {
157 if (!all_of(drop_begin(PtrVPI->operands()), match_fn(m_ZeroInt())))
158 return PtrVPI->getGEPNoWrapFlags();
159 Ptr = PtrVPI->getOperand(0);
160 continue;
161 }
162 if (Opcode != Instruction::BitCast && Opcode != Instruction::AddrSpaceCast)
163 break;
164 Ptr = PtrVPI->getOperand(0);
165 }
166 return GEPNoWrapFlags::none();
167}
168
171 const Loop *L) {
172 ScalarEvolution &SE = *PSE.getSE();
173 if (auto *RV = dyn_cast<VPRegionValue>(V)) {
174 assert(RV == RV->getDefiningRegion()->getCanonicalIV() &&
175 "RegionValue must be canonical IV");
176 if (!L)
177 return SE.getCouldNotCompute();
178 return SE.getAddRecExpr(SE.getZero(RV->getType()), SE.getOne(RV->getType()),
179 L, SCEV::FlagNone);
180 }
181
183 Value *LiveIn = V->getUnderlyingValue();
184 if (LiveIn && SE.isSCEVable(LiveIn->getType()))
185 return SE.getSCEV(LiveIn);
186 return SE.getCouldNotCompute();
187 }
188
189 // Helper to create SCEVs for binary and unary operations.
190 auto CreateSCEV = [&](ArrayRef<VPValue *> Ops,
191 function_ref<const SCEV *(ArrayRef<SCEVUse>)> CreateFn)
192 -> const SCEV * {
194 for (VPValue *Op : Ops) {
195 const SCEV *S = getSCEVExprForVPValue(Op, PSE, L);
197 return SE.getCouldNotCompute();
198 SCEVOps.push_back(S);
199 }
200 return PSE.getPredicatedSCEV(CreateFn(SCEVOps));
201 };
202
203 VPValue *LHSVal, *RHSVal;
204 if (match(V, m_Add(m_VPValue(LHSVal), m_VPValue(RHSVal))))
205 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
206 return SE.getAddExpr(Ops[0], Ops[1], SCEV::FlagNone, 0);
207 });
208 if (match(V, m_BinaryOr(m_VPValue(LHSVal), m_VPValue(RHSVal))))
209 if (cast<VPRecipeWithIRFlags>(V->getDefiningRecipe())->isDisjoint())
210 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
211 return SE.getAddExpr(Ops[0], Ops[1], SCEV::FlagNone, 0);
212 });
213 if (match(V, m_Sub(m_VPValue(LHSVal), m_VPValue(RHSVal))))
214 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
215 return SE.getMinusSCEV(Ops[0], Ops[1], SCEV::FlagNone, 0);
216 });
217 if (match(V, m_Not(m_VPValue(LHSVal)))) {
218 // not X = xor X, -1 = -1 - X
219 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
220 return SE.getMinusSCEV(SE.getMinusOne(Ops[0]->getType()), Ops[0]);
221 });
222 }
223 if (match(V, m_Mul(m_VPValue(LHSVal), m_VPValue(RHSVal))))
224 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
225 return SE.getMulExpr(Ops[0], Ops[1], SCEV::FlagNone, 0);
226 });
227 // Handle shl by constant: x << c is equivalent to x * (1 << c). A shift
228 // amount >= the bit width produces poison; do not rewrite it, as
229 // getPowerOfTwo requires the power to be in range.
230 uint64_t ShiftAmt;
231 if (match(V, m_Shl(m_VPValue(LHSVal), m_ConstantInt(ShiftAmt))) &&
232 ShiftAmt < LHSVal->getScalarType()->getScalarSizeInBits())
233 return CreateSCEV(LHSVal, [&](ArrayRef<SCEVUse> Ops) {
234 return SE.getMulExpr(Ops[0],
235 SE.getPowerOfTwo(Ops[0]->getType(), ShiftAmt));
236 });
237 if (match(V, m_LShr(m_VPValue(LHSVal), m_ConstantInt(ShiftAmt)))) {
238 Type *Ty = V->getScalarType();
239 if (ShiftAmt < SE.getTypeSizeInBits(Ty))
240 return CreateSCEV(LHSVal, [&](ArrayRef<SCEVUse> Ops) {
241 return SE.getUDivExpr(Ops[0], SE.getPowerOfTwo(Ty, ShiftAmt));
242 });
243 }
244 if (match(V, m_UDiv(m_VPValue(LHSVal), m_VPValue(RHSVal))))
245 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
246 return SE.getUDivExpr(Ops[0], Ops[1]);
247 });
248 if (match(V, m_URem(m_VPValue(LHSVal), m_VPValue(RHSVal))))
249 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
250 return SE.getURemExpr(Ops[0], Ops[1]);
251 });
252 // A SDiv with non-negative operands is equivalent to an UDiv.
253 if (match(V, m_SDiv(m_VPValue(LHSVal), m_VPValue(RHSVal)))) {
254 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
255 if (!SE.isKnownNonNegative(Ops[0]) || !SE.isKnownNonNegative(Ops[1]))
256 return SE.getCouldNotCompute();
257 return SE.getUDivExpr(Ops[0], Ops[1]);
258 });
259 }
260 // A SRem with non-negative operands is equivalent to an URem.
261 if (match(V, m_SRem(m_VPValue(LHSVal), m_VPValue(RHSVal)))) {
262 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
263 if (!SE.isKnownNonNegative(Ops[0]) || !SE.isKnownNonNegative(Ops[1]))
264 return SE.getCouldNotCompute();
265 return SE.getURemExpr(Ops[0], Ops[1]);
266 });
267 }
268 // Handle AND with constant mask: x & (2^n - 1) can be represented as x % 2^n.
269 const APInt *Mask;
270 if (match(V, m_c_BinaryAnd(m_VPValue(LHSVal), m_APInt(Mask))) &&
271 (*Mask + 1).isPowerOf2())
272 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
273 return SE.getURemExpr(Ops[0], SE.getConstant(*Mask + 1));
274 });
275 // SCEV models ptrtoaddr, but not ptrtoint, mirroring createSCEV.
276 if (match(V, m_PtrToAddr(m_VPValue(LHSVal))))
277 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
278 return SE.getPtrToAddrExpr(Ops[0]);
279 });
280 if (match(V, m_Trunc(m_VPValue(LHSVal)))) {
281 Type *DestTy = V->getScalarType();
282 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
283 return SE.getTruncateExpr(Ops[0], DestTy);
284 });
285 }
286 if (match(V, m_ZExt(m_VPValue(LHSVal)))) {
287 Type *DestTy = V->getScalarType();
288 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
289 return SE.getZeroExtendExpr(Ops[0], DestTy);
290 });
291 }
292 if (match(V, m_SExt(m_VPValue(LHSVal)))) {
293 Type *DestTy = V->getScalarType();
294
295 // Mirror SCEV's createSCEV handling for sext(sub nsw): push sign extension
296 // onto the operands before computing the subtraction.
297 VPValue *SubLHS, *SubRHS;
298 auto *SubR = dyn_cast<VPRecipeWithIRFlags>(LHSVal);
299 if (match(LHSVal, m_Sub(m_VPValue(SubLHS), m_VPValue(SubRHS))) && SubR &&
300 SubR->hasNoSignedWrap() && poisonGuaranteesUB(LHSVal)) {
301 const SCEV *V1 = getSCEVExprForVPValue(SubLHS, PSE, L);
302 const SCEV *V2 = getSCEVExprForVPValue(SubRHS, PSE, L);
304 return SE.getMinusSCEV(SE.getSignExtendExpr(V1, DestTy),
305 SE.getSignExtendExpr(V2, DestTy), SCEV::FlagNSW);
306 }
307
308 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
309 return SE.getSignExtendExpr(Ops[0], DestTy);
310 });
311 }
312 if (match(V,
314 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
315 return SE.getUMaxExpr(Ops[0], Ops[1]);
316 });
317 if (match(V,
319 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
320 return SE.getSMaxExpr(Ops[0], Ops[1]);
321 });
322 if (match(V,
324 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
325 return SE.getUMinExpr(Ops[0], Ops[1]);
326 });
327 if (match(V,
329 return CreateSCEV({LHSVal, RHSVal}, [&](ArrayRef<SCEVUse> Ops) {
330 return SE.getSMinExpr(Ops[0], Ops[1]);
331 });
333 return CreateSCEV({LHSVal}, [&](ArrayRef<SCEVUse> Ops) {
334 // is_int_min_poison is local to this intrinsic: poison on INT_MIN is
335 // not proof that the input is never INT_MIN, nor that poison reaches
336 // UB. Do not translate it to SCEV's global IsNSW flag.
337 return SE.getAbsExpr(Ops[0], /*IsNSW=*/false);
338 });
339
341 Type *SourceElementType;
342 if (match(V, m_GetElementPtr(SourceElementType, Ops))) {
343 return CreateSCEV(Ops, [&](ArrayRef<SCEVUse> Ops) {
344 return SE.getGEPExpr(Ops.front(), Ops.drop_front(), SourceElementType);
345 });
346 }
347
348 // TODO: Support constructing SCEVs for more recipes as needed.
349 const VPRecipeBase *DefR = V->getDefiningRecipe();
350 const SCEV *Expr =
352 .Case([](const VPExpandSCEVRecipe *R) { return R->getSCEV(); })
353 .Case([&SE, &PSE, L](const VPWidenIntOrFpInductionRecipe *R) {
354 const SCEV *Step = getSCEVExprForVPValue(R->getStepValue(), PSE, L);
355 if (!L || isa<SCEVCouldNotCompute>(Step))
356 return SE.getCouldNotCompute();
357 const SCEV *Start =
358 getSCEVExprForVPValue(R->getStartValue(), PSE, L);
359 const SCEV *AddRec =
360 SE.getAddRecExpr(Start, Step, L, SCEV::FlagNone);
361 if (R->isTruncated())
362 return SE.getTruncateExpr(AddRec, R->getScalarType());
363 return AddRec;
364 })
365 .Case([&SE, &PSE,
366 L](const VPWidenPointerInductionRecipe *R) -> const SCEV * {
367 const SCEV *Start =
368 getSCEVExprForVPValue(R->getStartValue(), PSE, L);
369 if (!L || isa<SCEVCouldNotCompute>(Start))
370 return SE.getCouldNotCompute();
371 const SCEV *Step = getSCEVExprForVPValue(R->getStepValue(), PSE, L);
372 if (isa<SCEVCouldNotCompute>(Step))
373 return SE.getCouldNotCompute();
374 return SE.getAddRecExpr(Start, Step, L, SCEV::FlagNone);
375 })
376 .Case([&SE, &PSE, L](const VPDerivedIVRecipe *R) -> const SCEV * {
377 const SCEV *Start = getSCEVExprForVPValue(R->getOperand(0), PSE, L);
378 const SCEV *IV = getSCEVExprForVPValue(R->getOperand(1), PSE, L);
379 const SCEV *Scale = getSCEVExprForVPValue(R->getOperand(2), PSE, L);
380 if (any_of(ArrayRef({Start, IV, Scale}),
382 return SE.getCouldNotCompute();
383
384 return SE.getAddExpr(
385 SE.getTruncateOrSignExtend(Start, IV->getType()),
386 SE.getMulExpr(
387 IV, SE.getTruncateOrSignExtend(Scale, IV->getType())));
388 })
389 .Case([&SE, &PSE, L](const VPScalarIVStepsRecipe *R) {
390 const SCEV *IV = getSCEVExprForVPValue(R->getOperand(0), PSE, L);
391 const SCEV *Step = getSCEVExprForVPValue(R->getOperand(1), PSE, L);
393 return SE.getCouldNotCompute();
394 return SE.getTruncateOrSignExtend(IV, Step->getType());
395 })
396 .Default(
397 [&SE](const VPRecipeBase *) { return SE.getCouldNotCompute(); });
398
399 return PSE.getPredicatedSCEV(Expr);
400}
401
402std::optional<int64_t>
404 PredicatedScalarEvolution &PSE, const Loop *L) {
405 assert(!hasIrregularType(AccessTy, L->getHeader()->getDataLayout()) &&
406 "should not try to widen irregular types");
407 const SCEV *AddrSCEV = getSCEVExprForVPValue(Addr, PSE, L);
408 auto *AddRec = dyn_cast<SCEVAddRecExpr>(AddrSCEV);
409 if (!AddRec)
410 return {};
411
412 return getStrideFromAddRec(AddRec, L, AccessTy, /*Ptr=*/nullptr, PSE);
413}
414
416 const Loop *L) {
417 // If address is an SCEVAddExpr, we require that all operands must be either
418 // be invariant or a (possibly sign-extend) affine AddRec.
419 if (auto *PtrAdd = dyn_cast<SCEVAddExpr>(Addr)) {
420 return all_of(PtrAdd->operands(), [&SE, L](const SCEV *Op) {
421 return SE.isLoopInvariant(Op, L) ||
422 match(Op, m_scev_SExt(m_scev_AffineAddRec(m_SCEV(), m_SCEV()))) ||
423 match(Op, m_scev_AffineAddRec(m_SCEV(), m_SCEV()));
424 });
425 }
426
427 // Otherwise, check if address is loop invariant or an affine add recurrence.
428 return SE.isLoopInvariant(Addr, L) ||
430}
431
432unsigned vputils::getOpcode(const VPValue *V) {
436 VPWidenLoadEVLRecipe>([](auto *I) { return I->getOpcode(); })
439 // For recipes that do not directly map to LLVM IR instructions,
440 // assign opcodes after the last VPInstruction opcode (which is also
441 // after the last IR Instruction opcode), based on the VPRecipeID.
442 return VPInstruction::OpsEnd + 1 + I->getVPRecipeID();
443 })
444 .Default([](auto *) { return 0; });
445}
446
447std::optional<std::pair<bool, unsigned>>
450 return std::make_pair(true, IID);
451 if (unsigned Opcode = vputils::getOpcode(V))
452 return std::make_pair(false, Opcode);
453 return {};
454}
455
456/// Returns true if \p Opcode preserves uniformity, i.e., if all operands are
457/// uniform, the result will also be uniform.
458static bool preservesUniformity(unsigned Opcode) {
459 if (Instruction::isBinaryOp(Opcode) || Instruction::isCast(Opcode))
460 return true;
461 switch (Opcode) {
462 case Instruction::Freeze:
463 case Instruction::GetElementPtr:
464 case Instruction::ICmp:
465 case Instruction::FCmp:
466 case Instruction::Select:
471 return true;
472 default:
473 return false;
474 }
475}
476
478 // TODO: Handle more opcodes and recipes.
480 return false;
481 unsigned Opcode = getOpcode(V);
482 return Instruction::isUnaryOp(Opcode) || Instruction::isBinaryOp(Opcode);
483}
484
486 // Live-in, symbolic and canonical-IV region values are single-scalar.
487 if (auto *RV = dyn_cast<VPRegionValue>(VPV))
488 return RV == RV->getDefiningRegion()->getCanonicalIV();
490 return true;
491
492 if (auto *Rep = dyn_cast<VPReplicateRecipe>(VPV)) {
493 const VPRegionBlock *RegionOfR = Rep->getRegion();
494 // Don't consider recipes in replicate regions as uniform yet; their first
495 // lane cannot be accessed when executing the replicate region for other
496 // lanes.
497 if (RegionOfR && RegionOfR->isReplicator())
498 return false;
499 return Rep->isSingleScalar() || (preservesUniformity(Rep->getOpcode()) &&
500 all_of(Rep->operands(), isSingleScalar));
501 }
504 if (auto *WidenR = dyn_cast<VPWidenRecipe>(VPV)) {
505 return preservesUniformity(WidenR->getOpcode()) &&
506 all_of(WidenR->operands(), isSingleScalar);
507 }
508 if (auto *VPI = dyn_cast<VPInstruction>(VPV))
509 return VPI->isSingleScalar() || VPI->isVectorToScalar() ||
510 (preservesUniformity(VPI->getOpcode()) &&
511 all_of(VPI->operands(), isSingleScalar));
512 if (auto *RR = dyn_cast<VPReductionRecipe>(VPV))
513 return !RR->isPartialReduction();
515 VPV))
516 return true;
517 if (auto *Expr = dyn_cast<VPExpressionRecipe>(VPV))
518 return Expr->isVectorToScalar();
519
520 // VPExpandSCEVRecipes must be placed in the entry and are always uniform.
521 return isa<VPExpandSCEVRecipe>(VPV);
522}
523
525 // Live-ins, symbolic and canonical-IV region values are uniform.
526 if (auto *RV = dyn_cast<VPRegionValue>(V))
527 return RV == RV->getDefiningRegion()->getCanonicalIV();
529 return true;
530
531 const VPRecipeBase *R = V->getDefiningRecipe();
532 const VPBasicBlock *VPBB = R ? R->getParent() : nullptr;
533 const VPlan *Plan = VPBB ? VPBB->getPlan() : nullptr;
534 if (VPBB &&
535 (VPBB == Plan->getVectorPreheader() || VPBB == Plan->getEntry())) {
536 if (match(R,
539 return false;
540 return all_of(R->operands(), isUniformAcrossVFsAndUFs);
541 }
542
544 .Case([](const VPDerivedIVRecipe *R) { return true; })
545 .Case([](const VPReplicateRecipe *R) {
546 // Be conservative about side-effects, except for the
547 // known-side-effecting assumes and stores, which we know will be
548 // uniform. Each alloca creates a distinct allocation, so allocas are
549 // never uniform.
550 return R->isSingleScalar() && R->getOpcode() != Instruction::Alloca &&
551 (!R->mayHaveSideEffects() ||
552 isa<AssumeInst, StoreInst>(R->getUnderlyingInstr())) &&
553 all_of(R->operands(), isUniformAcrossVFsAndUFs);
554 })
555 .Case([](const VPWidenRecipe *R) {
556 return preservesUniformity(R->getOpcode()) &&
557 all_of(R->operands(), isUniformAcrossVFsAndUFs);
558 })
559 .Case([](const VPPhi *) {
560 // Bail out on VPPhi, as we can end up in infinite cycles.
561 return false;
562 })
563 .Case([](const VPInstruction *VPI) {
564 return (VPI->isSingleScalar() || VPI->isVectorToScalar() ||
567 })
568 .Case([](const VPWidenCastRecipe *R) {
569 // A cast is uniform according to its operand.
570 return isUniformAcrossVFsAndUFs(R->getOperand(0));
571 })
572 .Default([](const VPRecipeBase *) { // A value is considered non-uniform
573 // unless proven otherwise.
574 return false;
575 });
576}
577
579 if (auto *RepR = dyn_cast<VPReplicateRecipe>(R))
580 return RepR->doesGeneratePerAllLanes();
581 if (auto *VPI = dyn_cast<VPInstruction>(R))
582 return VPI->doesGeneratePerAllLanes();
583 if (auto *SIVSteps = dyn_cast<VPScalarIVStepsRecipe>(R))
584 return SIVSteps->doesGeneratePerAllLanes();
585 return false;
586}
587
589 auto DepthFirst = vp_depth_first_shallow(Plan.getEntry());
590 auto I = find_if(DepthFirst, [&VPDT](VPBlockBase *VPB) {
591 return VPBlockUtils::isHeader(VPB, VPDT);
592 });
593 return I == DepthFirst.end() ? nullptr : cast<VPBasicBlock>(*I);
594}
595
597 if (!R)
598 return 1;
599 if (auto *RR = dyn_cast<VPReductionPHIRecipe>(R))
600 return RR->getVFScaleFactor();
601 if (auto *RR = dyn_cast<VPReductionRecipe>(R))
602 return RR->getVFScaleFactor();
603 if (auto *ER = dyn_cast<VPExpressionRecipe>(R))
604 return ER->getVFScaleFactor();
605 assert(
608 "getting scaling factor of reduction-start-vector not implemented yet");
609 return 1;
610}
611
612bool vputils::cannotHoistOrSinkRecipe(const VPRecipeBase &R, bool Sinking) {
613 // Assumes don't alias anything or throw; as long as they're guaranteed to
614 // execute, they're safe to hoist. They should however not be sunk, as it
615 // would destroy information.
617 return Sinking;
618 if (R.mayHaveSideEffects() || R.mayReadFromMemory() || R.isPhi())
619 return true;
620 // Allocas cannot be hoisted.
621 auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
622 return RepR && RepR->getOpcode() == Instruction::Alloca;
623}
624
627 VPBasicBlock *LastBB) {
628 assert(FirstBB->getParent() == LastBB->getParent() &&
629 "FirstBB and LastBB from different regions");
630#ifndef NDEBUG
631 bool InSingleSuccChain = false;
632 for (VPBlockBase *Succ = FirstBB; Succ; Succ = Succ->getSingleSuccessor())
633 InSingleSuccChain |= (Succ == LastBB);
634 assert(InSingleSuccChain &&
635 "LastBB unreachable from FirstBB in single-successor chain");
636#endif
637 auto Blocks = to_vector(
639 auto *LastIt = find(Blocks, LastBB);
640 assert(LastIt != Blocks.end() &&
641 "LastBB unreachable from FirstBB in depth-first traversal");
642 Blocks.erase(std::next(LastIt), Blocks.end());
643 return Blocks;
644}
645
647 for (VPRecipeBase &R : *Plan.getVectorPreheader())
649 return cast<VPInstruction>(&R);
650 return nullptr;
651}
652
654vputils::getEarlyExits(const VPlan &Plan, const VPBlockBase *MiddleVPBB) {
656 for (VPIRBasicBlock *ExitVPBB : Plan.getExitBlocks())
657 for (VPBlockBase *Pred : ExitVPBB->getPredecessors())
658 if (Pred != MiddleVPBB)
659 Exits.emplace_back(cast<VPBasicBlock>(Pred), ExitVPBB);
660 return Exits;
661}
662
665 Instruction::BinaryOps InductionOpcode, FPMathOperator *FPBinOp,
666 Type *ResultTy, VPValue *StartV, VPValue *Step, DebugLoc DL,
667 VPBuilder &Builder, const VPIRFlags::WrapFlagsTy &Flags) {
668 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
669 VPBasicBlock *HeaderVPBB = LoopRegion->getEntryBasicBlock();
670 VPValue *CanonicalIV = LoopRegion->getCanonicalIV();
671 VPValue *BaseIV =
672 Builder.createDerivedIV(Kind, FPBinOp, StartV, CanonicalIV, Step, Flags);
673
674 // Truncate BaseIV and Step to ResultTy.
675 if (ResultTy->isIntegerTy()) {
676 [[maybe_unused]] Type *BaseIVTy = BaseIV->getScalarType();
677 assert(BaseIVTy->isIntegerTy() && "Integral BaseIV type expected");
678 assert(BaseIVTy->getPrimitiveSizeInBits() >=
679 ResultTy->getPrimitiveSizeInBits() &&
680 "BaseIV's type must be at least as large as ResultTy");
681 BaseIV = Builder.createScalarZExtOrTrunc(BaseIV, ResultTy, DL);
682 [[maybe_unused]] Type *StepTy = Step->getScalarType();
683 assert(StepTy->isIntegerTy() && "Integral Step type expected");
684 assert(StepTy->getPrimitiveSizeInBits() >=
685 ResultTy->getPrimitiveSizeInBits() &&
686 "Step's type must be at least as large as ResultTy");
687 auto *VecPreheader =
689 VPBuilder::InsertPointGuard Guard(Builder);
690 Builder.setInsertPoint(VecPreheader);
691 Step = Builder.createScalarZExtOrTrunc(Step, ResultTy, DL);
692 }
693 return Builder.createScalarIVSteps(InductionOpcode, FPBinOp, BaseIV, Step,
694 &Plan.getVF(), DL);
695}
696
697VPValue *
699 VPlan &Plan, VPBuilder &Builder) {
700 VPValue *StepV = PtrIV->getStepValue();
701 VPIRValue *StartV = Plan.getZero(StepV->getScalarType());
703 Plan, InductionDescriptor::IK_IntInduction, Instruction::Add, nullptr,
704 StartV->getScalarType(), StartV, StepV, PtrIV->getDebugLoc(), Builder);
705
706 return Builder.createPtrAdd(PtrIV->getStartValue(), Steps,
707 PtrIV->getDebugLoc(), "next.gep");
708}
709
711 const VPDominatorTree &VPDT) {
712 auto *VPBB = dyn_cast<VPBasicBlock>(VPB);
713 if (!VPBB)
714 return false;
715
716 // If VPBB is in a region R, VPBB is a loop header if R is a loop region with
717 // VPBB as its entry, i.e., free of predecessors.
718 if (auto *R = VPBB->getParent())
719 return !R->isReplicator() && !VPBB->hasPredecessors();
720
721 // A header dominates its second predecessor (the latch), with the other
722 // predecessor being the preheader
723 return VPB->getPredecessors().size() == 2 &&
724 VPDT.dominates(VPB, VPB->getPredecessors()[1]);
725}
726
728 const VPDominatorTree &VPDT) {
729 // A latch has a header as its last successor, with its other successors
730 // leaving the loop. A preheader OTOH has a header as its first (and only)
731 // successor.
732 return VPB->getNumSuccessors() >= 2 &&
734}
735
736std::pair<VPBasicBlock *, VPBasicBlock *>
739 Plan.getEntry()->getNumSuccessors() == 1
740 ? Plan.getEntry()->getSingleSuccessor()
741 : Plan.getEntry()->getSuccessors()[1]->getSingleSuccessor());
742 assert(Header->getNumPredecessors() == 2 &&
743 "Header must have exactly 2 predecessors");
744 auto *Latch = cast<VPBasicBlock>(Header->getPredecessors()[1]);
745 return {Header, Latch};
746}
747
751
753 const VPPhi *PhiR) {
754 if (ID.getKind() == InductionDescriptor::IK_FpInduction)
755 return ID.getInductionBinOp()->getFastMathFlags();
756
757 // The flags only bound the induction values if the increment directly
758 // updates PhiR.
759 VPValue *Inc = PhiR->getOperand(1);
760 if (match(Inc, m_c_Add(m_Specific(PhiR), m_VPValue())))
761 return cast<VPInstruction>(Inc)->getNoWrapFlagsOrNone();
762
763 if (match(Inc, m_Sub(m_Specific(PhiR), m_VPValue()))) {
764 // The step of a sub induction is negated, so NUW cannot be preserved. NSW
765 // can, if the step is not the signed minimum.
766 std::optional<APInt> Step = ID.getConstStepValue();
767 bool NSW = cast<VPInstruction>(Inc)->getNoWrapFlagsOrNone().HasNSW &&
768 Step && !Step->isMinSignedValue();
769 return VPIRFlags::WrapFlagsTy(/*NUW*/ false, NSW);
770 }
771
772 return VPIRFlags::WrapFlagsTy(false, false);
773}
774
775std::optional<MemoryLocation>
777 auto *M = dyn_cast<VPIRMetadata>(&R);
778 if (!M)
779 return std::nullopt;
781 // Populate noalias metadata from VPIRMetadata.
782 if (MDNode *NoAliasMD = M->getMetadata(LLVMContext::MD_noalias))
783 Loc.AATags.NoAlias = NoAliasMD;
784 if (MDNode *AliasScopeMD = M->getMetadata(LLVMContext::MD_alias_scope))
785 Loc.AATags.Scope = AliasScopeMD;
786 return Loc;
787}
788
790 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
791 VPRegionValue *CanIV = LoopRegion->getCanonicalIV();
792 assert(CanIV && "Expected loop region to have a canonical IV");
793
794 VPSymbolicValue &VFxUF = Plan.getVFxUF();
795
796 // Check if \p Step matches the expected increment step, accounting for
797 // materialization of VFxUF and UF.
798 auto IsIncrementStep = [&](VPValue *Step) -> bool {
799 if (!VFxUF.isMaterialized())
800 return Step == &VFxUF;
801
802 VPSymbolicValue &UF = Plan.getUF();
803 if (!UF.isMaterialized())
804 return Step == &UF ||
805 match(Step, m_c_Mul(m_Specific(&Plan.getUF()), m_VScale()));
806
807 // Alias masking: step is number of active lanes of a dependence mask.
808 if (match(Step, m_ZExtOrTruncOrSelf(
810 return true;
811
812 unsigned ConcreteUF = Plan.getConcreteUF();
813 // Fixed VF: step is just the concrete UF.
814 if (match(Step, m_SpecificInt(ConcreteUF)))
815 return true;
816
817 // Scalable VF: step involves VScale.
818 if (ConcreteUF == 1)
819 return match(Step, m_VScale());
820 if (match(Step, m_c_Mul(m_SpecificInt(ConcreteUF), m_VScale())))
821 return true;
822 // mul(VScale, ConcreteUF) may have been simplified to
823 // shl(VScale, log2(ConcreteUF)) when ConcreteUF is a power of 2.
824 return isPowerOf2_32(ConcreteUF) &&
825 match(Step, m_Shl(m_VScale(), m_SpecificInt(Log2_32(ConcreteUF))));
826 };
827
828 VPInstruction *Increment = nullptr;
829 for (VPUser *U : CanIV->users()) {
830 VPValue *Step;
831 if (isa<VPInstruction>(U) &&
832 match(U, m_c_Add(m_Specific(CanIV), m_VPValue(Step))) &&
833 IsIncrementStep(Step)) {
834 assert(!Increment && "There must be a unique increment");
836 }
837 }
838
839 assert((!VFxUF.isMaterialized() || Increment) &&
840 "After materializing VFxUF, an increment must exist");
841 assert((!Increment ||
842 LoopRegion->hasCanonicalIVNUW() == Increment->hasNoUnsignedWrap()) &&
843 "NUW flag in region and increment must match");
844 return Increment;
845}
846
847/// Find the ComputeReductionResult recipe for \p PhiR, looking through selects
848/// inserted for predicated reductions or tail folding.
850 VPValue *BackedgeVal = PhiR->getBackedgeValue();
851 if (auto *Res =
853 return Res;
854
855 // Look through selects inserted for tail folding or predicated reductions.
857 BackedgeVal, m_Select(m_VPValue(), m_VPValue(), m_VPValue()));
858 if (!SelR)
859 return nullptr;
861}
862
865 SmallVector<const VPValue *> WorkList = {V};
866
867 while (!WorkList.empty()) {
868 const VPValue *Cur = WorkList.pop_back_val();
869 if (!Seen.insert(Cur).second)
870 continue;
871
872 auto *Blend = dyn_cast<VPBlendRecipe>(Cur);
873 // Skip blends that use V only through a compare by checking if any incoming
874 // value was already visited.
875 if (Blend && none_of(seq<unsigned>(0, Blend->getNumIncomingValues()),
876 [&](unsigned I) {
877 return Seen.contains(Blend->getIncomingValue(I));
878 }))
879 continue;
880
881 for (VPUser *U : Cur->users()) {
882 if (auto *InterleaveR = dyn_cast<VPInterleaveBase>(U))
883 if (InterleaveR->getAddr() == Cur)
884 return true;
885 // Cur is used as the pointer of a (possibly masked) load (operand 0) or
886 // store (operand 1).
889 m_Specific(Cur)))))
890 return true;
892 if (MemR->getAddr() == Cur && MemR->isConsecutive())
893 return true;
894 }
895 }
896
897 // The legacy cost model only supports scalarization loads/stores with phi
898 // addresses, if the phi is directly used as load/store address. Don't
899 // traverse further for Blends and first-order recurrence splices.
900 if (Blend ||
901 match(Cur,
903 continue;
904
905 // Only traverse further through users that also define a value (and can
906 // thus have their own users walked). Skip when Cur is only used as mask ,
907 // as well as loads: a loaded value does not depend on the load's operand.
908 for (VPUser *U : Cur->users()) {
909 auto *VPI = dyn_cast<VPInstruction>(U);
910 if (VPI && VPI->getMask() == Cur &&
911 none_of(VPI->operandsWithoutMask(), equal_to(Cur)))
912 continue;
914 continue;
915 if (auto *SDR = dyn_cast<VPSingleDefRecipe>(U))
916 WorkList.push_back(SDR);
917 }
918 }
919 return false;
920}
921
922/// Try to find a loop-invariant IR value for \p S in the plan's entry block
923/// that can be reused. Returns the corresponding live-in VPValue, or nullptr
924/// if no reusable IR value is found.
925VPValue *VPSCEVExpander::tryToReuseIRValue(const SCEV *S) {
927 return nullptr;
928 VPlan &Plan = Builder.getPlan();
929 BasicBlock *PH = cast<VPIRBasicBlock>(Plan.getEntry())->getIRBasicBlock();
930 for (Value *V : SE.getSCEVValues(S)) {
931 // Only reuse instructions in the plan's entry block, or, when a
932 // DominatorTree is available, any instruction that dominates it.
933 // Instructions in sibling branches may not dominate the entry block.
934 auto *I = dyn_cast<Instruction>(V);
935 if (!I)
936 return Plan.getOrAddLiveIn(V);
937 if (!SE.DT.dominates(I->getParent(), PH))
938 continue;
939 Loop *IL = SE.LI.getLoopFor(I->getParent());
940 if (IL && !IL->contains(PH))
941 continue;
942 SmallVector<Instruction *> DropPoisonGeneratingInsts;
943 if (!SE.canReuseInstruction(S, I, DropPoisonGeneratingInsts))
944 continue;
945 for (Instruction *DropI : DropPoisonGeneratingInsts)
947 return Plan.getOrAddLiveIn(V);
948 }
949 return nullptr;
950}
951
952VPValue *VPSCEVExpander::expandAsPtrAdd(const SCEVNAryExpr *S) {
953 VPValue *Base = expand(SE.getPointerBase(S));
954 VPValue *Offset = expand(SE.removePointerBase(S));
955 GEPNoWrapFlags GEPFlags = S->hasNoUnsignedWrap()
957 : GEPNoWrapFlags::none();
958 return Builder.createNoWrapPtrAdd(Base, Offset, GEPFlags, DL);
959}
960
961#ifndef NDEBUG
962/// Returns true if \p V dominates the insert point of \p Builder.
963static bool dominatesInsertPoint(const VPValue *V, const VPBuilder &Builder) {
964 const VPRecipeBase *R = V->getDefiningRecipe();
965 if (!R)
966 return true;
967 VPDominatorTree VPDT(Builder.getPlan());
968 if (VPRecipeBase *InsertPt = Builder.getRecipeAtInsertPoint())
969 return VPDT.properlyDominates(R, InsertPt);
970 return VPDT.dominates(R->getParent(), Builder.getInsertBlock());
971}
972#endif
973
976 if (VPValue *V = InsertedExpressions.lookup(Key)) {
977 assert(dominatesInsertPoint(V, Builder) &&
978 "reused expansion must dominate the insert point");
979 return V;
980 }
981 VPValue *V = expandImpl(S);
982 InsertedExpressions[Key] = V;
983 return V;
984}
985
986VPValue *VPSCEVExpander::expandImpl(const SCEV *S) {
987 if (VPValue *V = tryToReuseIRValue(S))
988 return V;
989
990 if (HoistOutOf && !SafeUDivMode && SE.isLoopInvariant(S, HoistOutOf) &&
992 return vputils::getOrCreateVPValueForSCEVExpr(Builder.getPlan(), S,
993 /*ExpandInIR=*/true);
994
995 switch (S->getSCEVType()) {
996 case scConstant:
997 return Builder.getPlan().getOrAddLiveIn(cast<SCEVConstant>(S)->getValue());
998 case scUnknown:
999 return Builder.getPlan().getOrAddLiveIn(cast<SCEVUnknown>(S)->getValue());
1000 case scVScale:
1001 return Builder.createVScale(S->getType(), DL);
1002 case scAddExpr: {
1003 auto *AddE = cast<SCEVAddExpr>(S);
1004 VPIRFlags::WrapFlagsTy WrapFlags(AddE->hasNoUnsignedWrap(),
1005 AddE->hasNoSignedWrap());
1006
1007 // Expand pointer SCEVAddExpr as a ptradd of the pointer base and the
1008 // integer offset, matching SCEVExpander.
1009 if (S->getType()->isPointerTy())
1010 return expandAsPtrAdd(AddE);
1011
1012 // Non-constant-negative add operands are expanded negated and subtracted
1013 // from the running result below, instead of being negated and added.
1014 auto UseSubtract = [](const SCEV *Op) {
1015 return Op->isNonConstantNegative();
1016 };
1017 // Iterate in reverse so that constants are emitted last, and move the
1018 // subtracted operands last, matching SCEVExpander's LoopCompare, so that
1019 // they don't start the running result.
1020 SmallVector<const SCEV *, 2> SCEVOps(reverse(AddE->operands()));
1021 stable_sort(SCEVOps, [&](const SCEV *L, const SCEV *R) {
1022 return !UseSubtract(L) && UseSubtract(R);
1023 });
1025 for (const SCEV *Op : SCEVOps) {
1026 // The first operand starts the result, so it is never subtracted.
1027 bool Negate = !Ops.empty() && UseSubtract(Op);
1028 Ops.push_back(expand(Negate ? SE.getNegativeSCEV(Op) : Op));
1029 }
1030 VPValue *Result = Ops.front();
1031 for (auto [Op, OpV] : drop_begin(zip_equal(SCEVOps, Ops))) {
1032 if (UseSubtract(Op)) {
1033 // Result + (-Op) == Result - Op, which saves the multiply for the
1034 // negation. NSW only transfers if negating Op cannot overflow, see
1035 // ScalarEvolution::getMinusSCEV.
1036 bool HasNSW =
1037 WrapFlags.HasNSW && !SE.getSignedRangeMin(Op).isMinSignedValue();
1038 Result = Builder.createOverflowingOp(Instruction::Sub, {Result, OpV},
1039 {/*HasNUW=*/false, HasNSW}, DL);
1040 continue;
1041 }
1042 Result = Builder.createOverflowingOp(Instruction::Add, {Result, OpV},
1043 WrapFlags, DL);
1044 }
1045 return Result;
1046 }
1047 case scMulExpr: {
1048 auto *MulE = cast<SCEVMulExpr>(S);
1049 VPIRFlags::WrapFlagsTy WrapFlags(MulE->hasNoUnsignedWrap(),
1050 MulE->hasNoSignedWrap());
1052 for (const SCEV *Op : reverse(MulE->operands()))
1053 Ops.push_back(expand(Op));
1054 VPValue *Result = Ops.front();
1055 for (VPValue *OpV : drop_begin(Ops)) {
1056 Result = Builder.createOverflowingOp(Instruction::Mul, {Result, OpV},
1057 WrapFlags, DL);
1058 }
1059 return Result;
1060 }
1061 case scUDivExpr: {
1062 auto *UDiv = cast<SCEVUDivExpr>(S);
1063 VPValue *LHS = expand(UDiv->getLHS());
1064 const SCEV *RHSExpr = UDiv->getRHS();
1065 VPValue *RHS = expand(RHSExpr);
1066 if (SafeUDivMode) {
1067 // Make sure the UDiv's divisor is guaranteed to not be zero/poison, to
1068 // avoid UB.
1069 Type *Ty = UDiv->getType();
1070 bool GuaranteedNotPoison =
1072 if (!GuaranteedNotPoison)
1073 RHS = Builder.createFreeze(RHS, DL);
1074 if (!SE.isKnownNonZero(RHSExpr) || !GuaranteedNotPoison)
1075 RHS = Builder.createScalarIntrinsic(
1076 Intrinsic::umax, {RHS, Builder.getPlan().getConstantInt(Ty, 1)}, Ty,
1077 DL);
1078 }
1079 return Builder.createNaryOp(Instruction::UDiv, {LHS, RHS},
1080 VPIRFlags::getDefaultFlags(Instruction::UDiv),
1081 DL);
1082 }
1083 case scTruncate:
1084 case scZeroExtend:
1085 case scSignExtend:
1086 case scPtrToAddr: {
1087 auto *Cast = cast<SCEVCastExpr>(S);
1088 VPValue *Op = expand(Cast->getOperand());
1089 Instruction::CastOps Opcode;
1090 switch (S->getSCEVType()) {
1091 case scTruncate:
1092 Opcode = Instruction::Trunc;
1093 break;
1094 case scZeroExtend:
1095 Opcode = Instruction::ZExt;
1096 break;
1097 case scSignExtend:
1098 Opcode = Instruction::SExt;
1099 break;
1100 case scPtrToAddr:
1101 Opcode = Instruction::PtrToAddr;
1102 break;
1103 default:
1104 llvm_unreachable("Unhandled cast SCEV");
1105 }
1106
1107 // For ptrtoaddr of an IR value, reuse an existing cast dominating the
1108 // entry if there is one. Otherwise leave it to SCEVExpander, which places
1109 // the cast right after the IR value's definition, so all users can share
1110 // it.
1111 if (Opcode == Instruction::PtrToAddr) {
1112 VPlan &Plan = Builder.getPlan();
1113 BasicBlock *PH = cast<VPIRBasicBlock>(Plan.getEntry())->getIRBasicBlock();
1114 if (auto *IRV = dyn_cast<VPIRValue>(Op)) {
1116 IRV->getValue(), S->getType(), PH->getDataLayout(),
1117 [&](const CastInst *CI) {
1118 return SE.DT.dominates(CI->getParent(), PH);
1119 }))
1120 return Plan.getOrAddLiveIn(CI);
1122 }
1123 }
1124
1125 std::optional<VPIRFlags> Flags;
1126 if (Opcode == Instruction::ZExt)
1127 Flags =
1128 VPIRFlags::NonNegFlagsTy(SE.isKnownNonNegative(Cast->getOperand()));
1129
1130 return Builder.createScalarCast(Opcode, Op, S->getType(), DL, Flags);
1131 }
1132 case scUMaxExpr:
1133 case scSMaxExpr:
1134 case scUMinExpr:
1135 case scSMinExpr:
1136 case scSequentialUMinExpr: {
1137 auto *MinMax = cast<SCEVNAryExpr>(S);
1138 Intrinsic::ID IntrinsicID;
1139 switch (S->getSCEVType()) {
1140 case scUMaxExpr:
1141 IntrinsicID = Intrinsic::umax;
1142 break;
1143 case scSMaxExpr:
1144 IntrinsicID = Intrinsic::smax;
1145 break;
1146 case scUMinExpr:
1148 IntrinsicID = Intrinsic::umin;
1149 break;
1150 case scSMinExpr:
1151 IntrinsicID = Intrinsic::smin;
1152 break;
1153 default:
1154 llvm_unreachable("Unexpected min/max SCEV type");
1155 }
1156 // Chain operands in reverse order matching SCEVExpander's expansion of
1157 // min/max expressions. In SafeUDivMode freeze expansion results of operands
1158 // other than the first for sequential UMins, to avoid short-circuiting
1159 // divide-by-0/poison.
1160 bool IsSequential = S->getSCEVType() == scSequentialUMinExpr;
1161 Type *ResultTy = MinMax->getType();
1162 bool PrevSafeMode = SafeUDivMode;
1164 for (const SCEV *SCEVOp : reverse(MinMax->operands())) {
1165 bool MayShortCircuit =
1166 IsSequential && Ops.size() != MinMax->getNumOperands() - 1;
1167 SafeUDivMode = MayShortCircuit || PrevSafeMode;
1168 VPValue *OpV = expand(SCEVOp);
1169 SafeUDivMode = PrevSafeMode;
1170 if (MayShortCircuit)
1171 OpV = Builder.createFreeze(OpV, DL);
1172 Ops.push_back(OpV);
1173 }
1174 VPValue *Result = Ops.front();
1175 for (VPValue *Op : drop_begin(Ops)) {
1176 if (!ResultTy->isPointerTy()) {
1177 Result = Builder.createScalarIntrinsic(IntrinsicID, {Result, Op},
1178 ResultTy, DL);
1179 continue;
1180 }
1181 // The min/max intrinsics don't support pointer operands, so expand
1182 // pointer-typed min/max as cmp + select, matching SCEVExpander.
1183 VPValue *Cmp = Builder.createICmp(
1184 MinMaxIntrinsic::getPredicate(IntrinsicID), Result, Op, DL);
1185 Result = Builder.createSelect(Cmp, Result, Op, DL);
1186 Function &F = *Builder.getPlan().getIRFunction();
1187 if (MDNode *MD =
1189 cast<VPInstruction>(Result)->setMetadata(LLVMContext::MD_prof, MD);
1190 }
1191 return Result;
1192 }
1193 case scAddRecExpr: {
1194 auto *AR = cast<SCEVAddRecExpr>(S);
1195 VPlan &Plan = Builder.getPlan();
1196 BasicBlock *PH = cast<VPIRBasicBlock>(Plan.getEntry())->getIRBasicBlock();
1197 assert(SE.DT.dominates(AR->getLoop()->getHeader(), PH) &&
1198 "can only expand AddRecs for loops outside VPlan's scope");
1199
1200 Type *Ty = AR->getType();
1201 if (auto [LCSSAPhi, Diff] = SCEVExpander::findReusableLCSSAPhi(SE, AR, PH);
1202 LCSSAPhi) {
1203 VPValue *DiffV = expand(Diff);
1204 VPValue *BaseV = Plan.getOrAddLiveIn(LCSSAPhi);
1205 if (LCSSAPhi->getType()->isPointerTy()) {
1206 if (Ty->isPointerTy())
1207 return Builder.createPtrAdd(BaseV, DiffV, DL);
1208 BaseV = Builder.createScalarCast(Instruction::PtrToAddr, BaseV, Ty, DL);
1209 }
1210 return Builder.createAdd(BaseV, DiffV, DL);
1211 }
1212
1213 // Mirror SCEVExpander::visitAddRecExpr, but only expand the canonical IV
1214 // {0,+,1} and non-affine AddRecs to IR (which will be created outside the
1215 // scope of VPlan). The AddRec is then scaled and offset using
1216 // VPInstructions.
1217 const Loop *L = AR->getLoop();
1218 auto m_scev_CanonicalIV = // NOLINT
1220 if (!AR->isAffine() || match(AR, m_scev_CanonicalIV))
1222
1223 const SCEV *Step = AR->getStepRecurrence(SE);
1224 // {0,+,F} --> {0,+,1} * F
1225 if (AR->getStart()->isZero()) {
1226 const SCEV *CanIV =
1227 SE.getAddRecExpr(SE.getZero(Ty), SE.getOne(Ty), L, SCEV::FlagNone);
1228 return Builder.createOverflowingOp(
1229 Instruction::Mul, {expand(CanIV), expand(Step)},
1230 {SE.willNotOverflow(Instruction::Mul, /*Signed=*/false, CanIV, Step),
1231 SE.willNotOverflow(Instruction::Mul, /*Signed=*/true, CanIV, Step)},
1232 DL);
1233 }
1234
1235 // {X,+,F} --> X + {0,+,F}
1236 if (Ty->isPointerTy())
1237 return expandAsPtrAdd(AR);
1238 VPValue *Start = expand(AR->getStart());
1239 VPValue *Rest = expand(SE.getAddRecExpr(SE.getZero(Ty), Step, L,
1240 AR->getNoWrapFlags(SCEV::FlagNW)));
1241 return Builder.createAdd(Start, Rest, DL, "",
1242 {AR->hasNoUnsignedWrap(), false});
1243 }
1244 case scCouldNotCompute:
1245 llvm_unreachable("Attempt to expand a SCEVCouldNotCompute");
1246 }
1247 llvm_unreachable("Unknown SCEV kind!");
1248}
1249
1251 // Do remove conditional assume instructions as their conditions may be
1252 // flattened.
1253 auto *RepR = dyn_cast<VPReplicateRecipe>(&R);
1254 bool IsConditionalAssume = RepR && RepR->isPredicated() &&
1256 if (IsConditionalAssume)
1257 return true;
1258
1259 if (R.mayHaveSideEffects())
1260 return false;
1261
1262 // Forbid removing trip-count expressions.
1263 if (isa<VPExpandSCEVRecipe>(R) &&
1264 R.getVPSingleValue() == R.getParent()->getPlan()->getTripCount())
1265 return false;
1266
1267 // Recipe is dead if no user keeps the recipe alive.
1268 return all_of(R.definedValues(), [](VPValue *V) { return V->user_empty(); });
1269}
1270
1272 SmallVector<VPValue *> WorkList;
1274 WorkList.push_back(V);
1275
1276 while (!WorkList.empty()) {
1277 VPValue *Cur = WorkList.pop_back_val();
1278 if (!Seen.insert(Cur).second)
1279 continue;
1280 VPRecipeBase *R = Cur->getDefiningRecipe();
1281 if (!R)
1282 continue;
1283 if (!isDeadRecipe(*R))
1284 continue;
1285 append_range(WorkList, R->operands());
1286 R->eraseFromParent();
1287 }
1288}
1289
1292 for (unsigned I = 0; I != Users.size(); ++I) {
1294 for (VPValue *V : Cur->definedValues())
1295 Users.insert_range(V->users());
1296 }
1297 return Users.takeVector();
1298}
1299
1300/// Returns \p Num / \p Denom as a BranchProbability, clamped so a ratio that is
1301/// neither zero nor one does not round to zero or one. BlockFrequencyInfo also
1302/// keeps a zero-weight edge distinguishable from an unreachable one.
1304 uint64_t Denom) {
1306 if (Num == 0 || Num == Denom)
1307 return P;
1308 return BranchProbability::getRaw(std::clamp(
1309 P.getNumerator(), 1u, BranchProbability::getDenominator() - 1));
1310}
1311
1317
1318/// Returns the probability of each successor edge of \p VPBB, computed via
1319/// BranchProbabilityInfo::getEdgeProbabilitiesFromWeights from the branch
1320/// weights recorded on its terminator, or std::nullopt if not available.
1321static std::optional<SmallVector<BranchProbability>>
1323 // With a single successor the edge is always taken and needs no weights.
1324 if (VPBB->getSingleSuccessor())
1326
1327 SmallVector<uint32_t> Weights;
1329 if (!Term || !extractBranchWeights(Term->getBranchWeights(), Weights) ||
1330 Weights.size() != VPBB->getNumSuccessors())
1331 return std::nullopt;
1333}
1334
1337 using BFIBase = BlockFrequencyInfoImplBase;
1338 assert(!Blocks.empty() && "expected at least the header block");
1339 // Distribute the header's frequency using BFI. Nodes for blocks are numbered
1340 // in reverse post-order. Edges leaving Blocks, i.e. a plain CFG's edges to
1341 // the middle block or to an exit block, exit to a node outside the loop.
1342 BFIBase BFI;
1343 BFIBase::BlockNode Header(0), Outside(Blocks.size());
1344 BFIBase::LoopData &Loop = BFI.Loops.emplace_back(nullptr, Header);
1346 for (auto [Idx, VPBB] : enumerate(Blocks)) {
1347 Nodes[VPBB] = BFIBase::BlockNode(Idx);
1348 BFI.Working.emplace_back(BFIBase::BlockNode(Idx)).Loop = &Loop;
1349 }
1350 BFI.Working.emplace_back(Outside);
1351 BFI.Working[Header.Index].getMass() = BFIBase::BlockMass::getFull();
1352
1353 // Keep track nodes reached via an edge without branch weighs or with
1354 // estimated ones
1355 SmallVector<bool> IsUnknown(Blocks.size()), IsEstimated(Blocks.size());
1356 for (auto [Idx, VPBB] : enumerate(Blocks)) {
1357 BFIBase::BlockNode Node(Idx);
1358 auto Probs = getSuccessorProbabilities(VPBB);
1359 auto *Term = dyn_cast_if_present<VPInstruction>(VPBB->getTerminator());
1360 bool TermIsEstimated = Term && Term->hasEstimatedBranchWeights();
1361 BFIBase::Distribution Dist;
1362 for (auto [SuccIdx, Succ] : enumerate(VPBB->getSuccessors())) {
1363 BFIBase::BlockNode SuccNode = Nodes.lookup_or(Succ, Outside);
1364 if (SuccNode != Header && SuccNode != Outside) {
1365 IsUnknown[SuccNode.Index] |= IsUnknown[Idx] || !Probs;
1366 IsEstimated[SuccNode.Index] |= IsEstimated[Idx] || TermIsEstimated;
1367 }
1368 if (Probs)
1369 BFI.addToDist(Dist, &Loop, Node, SuccNode,
1370 getWeightFromBranchProb((*Probs)[SuccIdx]));
1371 }
1372 if (Probs)
1373 BFI.distributeMass(Node, &Loop, Dist);
1374 }
1375
1376 // Round frequencies up to at least 1, so all edges are reached with a
1377 // non-zero frequency, to distinguish rarely executed blocks from unreachable
1378 // ones. blocks distinguishable from unreachable ones.
1380 Frequencies;
1381 for (auto [Idx, VPBB] : enumerate(Blocks)) {
1382 std::optional<VPExecutionFrequency> &Freq = Frequencies[VPBB];
1383 if (IsUnknown[Idx])
1384 continue;
1385 uint64_t Mass = BFI.Working[Idx].getMass().getMass();
1386 Freq.emplace(BlockFrequency(std::max<uint64_t>(Mass, 1)), IsEstimated[Idx]);
1387 }
1388 return Frequencies;
1389}
1390
1393 const DataLayout &DL) {
1394 auto OpcodeOrIID = getOpcodeOrIntrinsicID(&R);
1395 if (!OpcodeOrIID)
1396 return nullptr;
1397
1399 for (VPValue *Op : Operands) {
1400 VPValue *Candidate = Op;
1401 match(Op, m_Broadcast(m_VPValue(Candidate)));
1402 if (!match(Candidate, m_LiveIn()))
1403 return nullptr;
1404 Value *V = Candidate->getUnderlyingValue();
1405 if (!V)
1406 return nullptr;
1407 Ops.push_back(V);
1408 }
1409
1410 VPlan &Plan = *R.getParent()->getPlan();
1411 auto FoldToIRValue = [&]() -> Value * {
1412 InstSimplifyFolder Folder(DL);
1413 if (OpcodeOrIID->first) {
1414 // VPInstructions store the called intrinsic as last operand.
1415 if (isa<VPInstruction>(R))
1416 Ops.pop_back();
1417
1418 auto *RFlags = dyn_cast<VPRecipeWithIRFlags>(&R);
1419 return Folder.FoldIntrinsic(OpcodeOrIID->second, Ops, R.getScalarType(),
1420 RFlags ? RFlags->getFastMathFlagsOrNone()
1421 : FastMathFlags());
1422 }
1423 unsigned Opcode = OpcodeOrIID->second;
1424 if (Instruction::isBinaryOp(Opcode))
1425 return Folder.FoldBinOp(static_cast<Instruction::BinaryOps>(Opcode),
1426 Ops[0], Ops[1]);
1427 if (Instruction::isCast(Opcode))
1428 return Folder.FoldCast(static_cast<Instruction::CastOps>(Opcode), Ops[0],
1429 R.getVPSingleValue()->getScalarType());
1430 switch (Opcode) {
1431 case VPInstruction::Not:
1432 return Folder.FoldBinOp(Instruction::BinaryOps::Xor, Ops[0],
1434 case Instruction::Select:
1435 return Folder.FoldSelect(Ops[0], Ops[1], Ops[2]);
1436 case Instruction::ICmp:
1437 case Instruction::FCmp:
1438 return Folder.FoldCmp(cast<VPRecipeWithIRFlags>(R).getPredicate(), Ops[0],
1439 Ops[1]);
1440 case Instruction::GetElementPtr: {
1441 auto &RFlags = cast<VPRecipeWithIRFlags>(R);
1442 auto *GEP = cast<GetElementPtrInst>(RFlags.getUnderlyingInstr());
1443 return Folder.FoldGEP(DL, GEP->getSourceElementType(), Ops[0],
1444 drop_begin(Ops), RFlags.getGEPNoWrapFlags());
1445 }
1448 return Folder.FoldGEP(DL, IntegerType::getInt8Ty(Plan.getContext()),
1449 Ops[0], Ops[1],
1450 cast<VPRecipeWithIRFlags>(R).getGEPNoWrapFlags());
1451 // An extract of a live-in is an extract of a broadcast, so return the
1452 // broadcasted element.
1453 case Instruction::ExtractElement:
1454 assert(!Ops[0]->getType()->isVectorTy() && "Live-ins should be scalar");
1455 return Ops[0];
1458 uint64_t Multiplier = 1;
1459 if (Opcode == VPInstruction::WideActiveLaneMask) {
1460 // Optimizing WideALM can only happen after the Plan is unrolled.
1461 if (!Plan.isUnrolled())
1462 return nullptr;
1463 Multiplier = cast<ConstantInt>(Ops[2])->getZExtValue();
1464 Ops.pop_back();
1465 }
1466
1467 // We rely on the fact that different VPlans are created for the
1468 // fixed-vector and scalable-vector cases.
1469 ElementCount MaxVF =
1471 Multiplier;
1472
1473 Type *I1Ty = IntegerType::getInt1Ty(Plan.getContext());
1474 if (auto *C = dyn_cast_if_present<Constant>(Folder.FoldIntrinsic(
1475 Intrinsic::get_active_lane_mask, Ops,
1476 VectorType::get(I1Ty, MaxVF), {}, Plan.getIRFunction()))) {
1477 // We cannot handle vector constants that are not all-true or all-false,
1478 // because they would not be collapsable to a scalar constant, that
1479 // would be necessary for live-in simplification.
1480 if (C->isOneValue())
1481 return ConstantInt::getTrue(I1Ty);
1482 if (C->isNullValue())
1483 return ConstantInt::getFalse(I1Ty);
1484 }
1485 }
1486 }
1487 return nullptr;
1488 };
1489
1490 if (Value *V = FoldToIRValue())
1491 return Plan.getOrAddLiveIn(V);
1492 return nullptr;
1493}
1494
1496 VPlan &Plan, function_ref<VPValue *(VPValue *Op)> MatchPerm,
1499 vp_depth_first_deep(Plan.getEntry()))) {
1500 for (VPSingleDefRecipe &Def :
1502 if (!isElementwise(&Def))
1503 continue;
1504
1505 // At least one of the ops must be a permutation.
1506 if (none_of(Def.operands(), MatchPerm))
1507 continue;
1508
1509 // All operands must be a single-use permutation or a live in (splat).
1510 if (!all_of(Def.operands(), [&MatchPerm](VPValue *Op) {
1511 return (Op->hasOneUse() && MatchPerm(Op)) || match(Op, m_LiveIn());
1512 }))
1513 continue;
1514
1515 // Remove the inner permutations.
1516 for (unsigned I = 0, E = Def.getNumOperands(); I != E; ++I)
1517 if (VPValue *X = MatchPerm(Def.getOperand(I)))
1518 Def.setOperand(I, X);
1519
1520 VPSingleDefRecipe *Res = BuildPerm(&Def);
1521 Res->insertAfter(&Def);
1522 Def.replaceUsesWithIf(Res, not_equal_to(Res));
1523 }
1524 }
1525}
1526
1527// Implements the algorithm described in "Simple and Efficient Construction of
1528// Static Single Assignment Form" by Braun et al.
1531 assert(!Defs.empty() && "Defs shouldn't be empty");
1532 assert(
1534 "VPBB isn't reachable from entry");
1535 if (VPValue *Def = Defs.lookup(VPBB))
1536 return Def;
1537 // If the entry block is reached and there's still no def, then Defs is
1538 // missing a definition that covers this path.
1539 assert(VPBB->getNumPredecessors() && "Not all paths have def");
1540
1541 if (VPBlockBase *Pred = VPBB->getSinglePredecessor())
1542 return reconstructSSA(cast<VPBasicBlock>(Pred), Defs);
1543
1544 // Multiple predecessors, create a join.
1545 Type *Ty = Defs.begin()->second->getScalarType();
1546 VPPhi *Phi = VPBuilder(VPBB->getFirstNonPhi())
1547 .createScalarPhi({}, DebugLoc::getUnknown(), "", {}, Ty);
1548 Defs[VPBB] = Phi;
1549 for (auto *Pred : VPBB->predecessors())
1550 Phi->addIncoming(reconstructSSA(cast<VPBasicBlock>(Pred), Defs));
1551
1552 // Fold away trivial phis.
1553 // TODO: Remove phi users which have become trivial too.
1554 if (all_equal(Phi->incoming_values())) {
1555 VPValue *Common = Phi->getIncomingValue(0);
1556 Phi->replaceAllUsesWith(Common);
1557 for (auto &[_, V] : Defs)
1558 if (V == Phi)
1559 V = Common;
1560 Defs[VPBB] = Common;
1561 Phi->eraseFromParent();
1562 return Common;
1563 }
1564
1565 return Phi;
1566}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
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 F(x, y, z)
Definition MD5.cpp:54
#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 bool dominatesInsertPoint(const VPValue *V, const VPBuilder &Builder)
Returns true if V dominates the insert point of Builder.
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 std::optional< SmallVector< BranchProbability > > getSuccessorProbabilities(const VPBasicBlock *VPBB)
Returns the probability of each successor edge of VPBB, computed via BranchProbabilityInfo::getEdgePr...
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 ...
Value * RHS
Value * LHS
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
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.
Base class for BlockFrequencyInfoImpl.
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
static LLVM_ABI SmallVector< BranchProbability > getEdgeProbabilitiesFromWeights(ArrayRef< uint32_t > Weights)
Returns the probabilities of edges with branch weights Weights.
static LLVM_ABI BranchProbability getBranchProbability(uint64_t Numerator, uint64_t Denominator)
static constexpr BranchProbability getOne()
static uint32_t getDenominator()
static constexpr BranchProbability getRaw(uint32_t N)
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
static LLVM_ABI ConstantInt * getFalse(LLVMContext &Context)
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
static DebugLoc getUnknown()
Definition DebugLoc.h:153
bool empty() const
Definition DenseMap.h:716
ValueT lookup_or(const_arg_type_t< KeyT > Val, U &&Default) const
Definition DenseMap.h:803
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:793
iterator begin()
Definition DenseMap.h:682
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_FpInduction
Floating point induction variable.
@ 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
bool contains(const LoopT *L) const
Return true if the specified loop is contained within this loop.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1081
Representation for a specific memory location.
ICmpInst::Predicate getPredicate() const
Returns the comparison predicate underlying the intrinsic.
PointerIntPair - This class implements a pair of a pointer and small integer.
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 ...
static LLVM_ABI std::pair< PHINode *, const SCEV * > findReusableLCSSAPhi(ScalarEvolution &SE, SCEVUseT< const SCEVAddRecExpr * > S, const BasicBlock *InsertBB)
Find a phi in the exit block of S's loop, which must dominate InsertBB, such that S can be expanded i...
This node is a base class providing common functionality for n'ary operators.
This class represents an analyzed expression in the program.
static constexpr auto FlagNSW
Type * getType() const
Return the LLVM type of this SCEV expression.
static constexpr auto FlagNone
SCEVTypes getSCEVType() const
static constexpr auto FlagNW
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 const SCEV * getMinusSCEV(SCEVUse LHS, SCEVUse RHS, SCEVFlags Flags=SCEV::FlagNone, unsigned Depth=0)
Return LHS-RHS.
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.
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 SCEVUse getAddRecExpr(SCEVUse Start, SCEVUse Step, const Loop *L, SCEVFlagsPair Flags)
Get an add recurrence expression for the specified loop.
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 SCEVUse getAddExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
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 SCEVUse getMulExpr(SmallVectorImpl< SCEVUse > &Ops, SCEVFlagsPair Flags={}, 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 * getPtrToAddrExpr(const SCEV *Op)
LLVM_ABI const SCEV * getSMaxExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI bool isGuaranteedNotToCauseUB(const SCEV *Op)
Returns true if Op is guaranteed not to cause immediate UB.
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
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition SetVector.h:157
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.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:345
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:277
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:297
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:363
LLVM_ABI TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition Type.cpp:187
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:296
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:4494
iterator end()
Definition VPlan.h:4535
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:228
VPRecipeBase * getTerminator()
If the block has multiple successors, return the branch recipe terminating the block.
Definition VPlan.cpp:607
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:99
VPRegionBlock * getParent()
Definition VPlan.h:197
iterator_range< VPBlockBase ** > predecessors()
Definition VPlan.h:230
size_t getNumSuccessors() const
Definition VPlan.h:247
size_t getNumPredecessors() const
Definition VPlan.h:248
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:232
VPlan * getPlan()
Definition VPlan.h:201
VPBlockBase * getSinglePredecessor() const
Definition VPlan.h:243
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:183
VPBlockBase * getSingleHierarchicalPredecessor()
Definition VPlan.h:273
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:237
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:221
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:441
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.
VPPhi * createScalarPhi(ArrayRef< VPValue * > IncomingValues, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", std::optional< VPIRFlags > Flags=std::nullopt, Type *ResultTy=nullptr)
Create a phi with IncomingValues, using the default flags for the result type, unless Flags is set.
ArrayRef< VPRecipeValue * > definedValues()
Returns an ArrayRef of the values defined by the VPDef.
Definition VPlanValue.h:572
A recipe for converting Current into Start + Current * Step.
Definition VPlan.h:4265
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
LLVM_ABI_FOR_TEST bool properlyDominates(const VPRecipeBase *A, const VPRecipeBase *B) const
Recipe to expand a SCEV expression.
Definition VPlan.h:4089
virtual VPValue * getBackedgeValue()
Returns the incoming value from the loop backedge.
Definition VPlan.h:2547
VPValue * getStartValue()
Returns the start value of the phi, if one is set.
Definition VPlan.h:2536
A special type of VPBasicBlock that wraps an existing IR basic block.
Definition VPlan.h:4653
Class to record and manage LLVM IR flags.
Definition VPlan.h:685
static LLVM_ABI_FOR_TEST 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:1324
@ ReductionStartVector
Start vector for reductions with 3 operands: the original start value, the identity value for the red...
Definition VPlan.h:1425
unsigned getOpcode() const
Definition VPlan.h:1516
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.
VPPredInstPHIRecipe is a recipe for generating the phi nodes needed when control converges back from ...
Definition VPlan.h:3772
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:406
DebugLoc getDebugLoc() const
Returns the debug location of the recipe.
Definition VPlan.h:550
void insertAfter(VPRecipeBase *InsertPos)
Insert an unlinked Recipe into a basic block immediately after the specified Recipe.
A recipe for handling reduction phis.
Definition VPlan.h:2907
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:4725
bool isReplicator() const
An indicator whether this region is to generate multiple replicated instances of output IR correspond...
Definition VPlan.h:4801
bool hasCanonicalIVNUW() const
Indicates if NUW is set for the canonical IV increment, for loop regions.
Definition VPlan.h:4889
VPRegionValue * getCanonicalIV()
Return the canonical induction variable of the region, null for replicating regions.
Definition VPlan.h:4845
VPValues are defined by a VPRegionBlock, like the canonical IV.
Definition VPlanValue.h:247
VPReplicateRecipe replicates a given instruction producing multiple scalar copies of the original sca...
Definition VPlan.h:3457
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:4326
VPSingleDefRecipe is a base class for recipes that model a sequence of one or more output IR that def...
Definition VPlan.h:608
A symbolic live-in VPValue, used for values like vector trip count, VF, and VFxUF.
Definition VPlanValue.h:212
bool isMaterialized() const
Returns true if this value has been materialized.
Definition VPlanValue.h:230
This class augments VPValue with operands which provide the inverse def-use edges from VPValue's user...
Definition VPlanValue.h:396
operand_range operands()
Definition VPlanValue.h:472
VPValue * getOperand(unsigned N) const
Definition VPlanValue.h:437
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:142
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition VPlan.cpp:123
Value * getUnderlyingValue() const
Return the underlying Value attached to this VPValue.
Definition VPlanValue.h:75
user_range users()
Definition VPlanValue.h:157
A recipe to compute the pointers for widened memory accesses of SourceElementTy, with the Stride expr...
Definition VPlan.h:2416
VPWidenCastRecipe is a recipe to create vector cast instructions.
Definition VPlan.h:1916
A recipe for handling GEP instructions.
Definition VPlan.h:2274
VPValue * getStepValue()
Returns the step value of the induction.
Definition VPlan.h:2614
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition VPlan.h:2660
A recipe for widened phis.
Definition VPlan.h:2794
VPWidenRecipe is a recipe for producing a widened instruction using the opcode and operands of the re...
Definition VPlan.h:1849
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4912
LLVMContext & getContext() const
Definition VPlan.h:5109
VPBasicBlock * getEntry()
Definition VPlan.h:5004
iterator_range< SmallSetVector< ElementCount, 2 >::iterator > vectorFactors() const
Returns an iterator range over all VFs of the plan.
Definition VPlan.h:5142
VPSymbolicValue & getVFxUF()
Returns VF * UF of the vector loop region.
Definition VPlan.h:5107
ArrayRef< VPIRBasicBlock * > getExitBlocks() const
Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of the original scalar loop.
Definition VPlan.h:5070
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:5185
VPIRValue * getZero(Type *Ty)
Return a VPIRValue wrapping the null value of type Ty.
Definition VPlan.h:5211
bool isUnrolled() const
Returns true if the VPlan already has been unrolled, i.e.
Definition VPlan.h:5176
Function * getIRFunction() const
Definition VPlan.h:5117
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1065
unsigned getConcreteUF() const
Returns the concrete UF of the plan, after unrolling.
Definition VPlan.h:5163
VPBasicBlock * getVectorPreheader() const
Returns the preheader of the vector loop region, if one exists, or null otherwise.
Definition VPlan.h:5009
VPSymbolicValue & getUF()
Returns the UF of the vector loop region.
Definition VPlan.h:5104
VPBasicBlock * getScalarPreheader() const
Return the VPBasicBlock for the preheader of the scalar loop.
Definition VPlan.h:5060
VPSymbolicValue & getVF()
Returns the VF of the vector loop region.
Definition VPlan.h:5100
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
static constexpr bool isKnownLT(const FixedOrScalableQuantity &LHS, const FixedOrScalableQuantity &RHS)
Definition TypeSize.h:217
An efficient, type-erasing, non-owning reference to a callable.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
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.
cst_pred_ty< is_one > m_scev_One()
Match an integer 1.
specificloop_ty m_SpecificLoop(const Loop *L)
cst_pred_ty< is_zero > m_scev_Zero()
Match an integer 0.
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.
RecipeTy * findSingleUser(VPValue *V, const MatchT &P)
Return the sole user of V matching pattern P, casting it to RecipeTy, or nullptr.
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)
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 the full mass.
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...
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...
std::optional< int64_t > getConstantStride(VPValue *Addr, Type *AccessTy, PredicatedScalarEvolution &PSE, const Loop *L)
If the pointer operand Addr of a memory access is an affine AddRec w.r.t.
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,...
LLVM_ABI_FOR_TEST VPValue * reconstructSSA(VPBasicBlock *VPBB, DenseMap< VPBasicBlock *, VPValue * > &Defs)
Insert phis to reconstruct SSA for a single value starting from VPBB.
bool onlyFirstPartUsed(const VPValue *Def)
Returns true if only the first part of Def is used by all users.
Intrinsic::ID getIntrinsicID(const Ty *R)
Return the intrinsic ID underlying a call.
Definition VPlanUtils.h:99
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 by all users.
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.
DenseMap< const VPBasicBlock *, std::optional< VPExecutionFrequency > > computeExecutionFrequencies(ArrayRef< VPBasicBlock * > Blocks)
Computes for each block in Blocks, which must be in reverse post-order, the frequency with which it e...
VPScalarIVStepsRecipe * createScalarIVSteps(VPlan &Plan, InductionDescriptor::InductionKind Kind, Instruction::BinaryOps InductionOpcode, FPMathOperator *FPBinOp, Type *ResultTy, 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...
bool propagatesFirstLaneUse(const VPUser *U, const VPValue *Op)
Returns true if checking whether U uses the first lane of Op requires recursively checking Op's users...
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.
VPIRFlags getFlagsForInduction(const InductionDescriptor &ID, const VPPhi *PhiR)
Extracts and returns NoWrap flags from PhiR and fast-math flags from ID.
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.
LLVM_ABI_FOR_TEST bool isUniformAcrossVFsAndUFs(const VPValue *V)
Checks if V is uniform across all VF lanes and UF parts.
VPValue * getOrCreateVPValueForSCEVExpr(VPlan &Plan, const SCEV *Expr, bool ExpandInIR=false)
Get or create a VPValue that corresponds to the expansion of Expr.
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...
LLVM_ABI_FOR_TEST 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.
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:316
@ Offset
Definition DWP.cpp:577
constexpr auto not_equal_to(T &&Arg)
Functor variant of std::not_equal_to that can be used as a UnaryPredicate in functional algorithms li...
Definition STLExtras.h:2196
void stable_sort(R &&Range)
Definition STLExtras.h:2132
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:1781
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:1755
uint32_t getWeightFromBranchProb(const BranchProbability Prob)
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:856
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2570
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
VPBuilderBase<> VPBuilder
Definition VPlan.h:69
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:2224
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:649
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:2189
auto make_isa_range(RangeT &&Range)
Return a range over Range containing only elements for which isa<T> holds, casting each of them to T.
Definition STLExtras.h:567
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:1762
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:408
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
bool hasIrregularType(Type *Ty, const DataLayout &DL)
A helper function that returns true if the given type is irregular.
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:1769
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...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
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
LLVM_ABI MDNode * getExplicitlyUnknownBranchWeightsIfProfiled(Function &F, StringRef PassName)
Returns a metadata node containing unknown branch weights if the function has an entry count,...
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
DWARFExpression::Operation Op
auto max_element(R &&Range)
Provide wrappers to std::max_element which take ranges instead of having to pass begin/end explicitly...
Definition STLExtras.h:2104
ArrayRef(const T &OneElt) -> ArrayRef< T >
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:1788
constexpr auto seq(T Begin, T End)
Iterate over an integral type from Begin up to - but not including - End.
Definition Sequence.h:341
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1963
bool all_equal(std::initializer_list< T > Values)
Returns true if all Values in the initializer lists are equal or the list.
Definition STLExtras.h:2182
LLVM_ABI std::optional< int64_t > getStrideFromAddRec(const SCEVAddRecExpr *AR, const Loop *Lp, Type *AccessTy, Value *Ptr, PredicatedScalarEvolution &PSE)
If AR is an affine AddRec for Lp with a constant step, return the step in units of AccessTy's allocat...
@ Increment
Incrementally increasing token ID.
Definition AllocToken.h:26
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
A VPValue representing a live-in from the input IR or a constant.
Definition VPlanValue.h:274
A recipe for widening load operations with vector-predication intrinsics, using the address to load f...
Definition VPlan.h:3927
A recipe for widening load operations, using the address to load from and an optional mask.
Definition VPlan.h:3872