LLVM 24.0.0git
VPlanConstruction.cpp
Go to the documentation of this file.
1//===-- VPlanConstruction.cpp - Transforms for initial VPlan construction -===//
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/// \file
10/// This file implements transforms for initial VPlan construction.
11///
12//===----------------------------------------------------------------------===//
13
15#include "VPlan.h"
16#include "VPlanAnalysis.h"
17#include "VPlanCFG.h"
18#include "VPlanDominatorTree.h"
19#include "VPlanHelpers.h"
20#include "VPlanPatternMatch.h"
21#include "VPlanTransforms.h"
22#include "VPlanUtils.h"
24#include "llvm/Analysis/Loads.h"
31#include "llvm/IR/InstrTypes.h"
32#include "llvm/IR/MDBuilder.h"
33#include "llvm/Support/Debug.h"
37
38#define DEBUG_TYPE "vplan"
39
40using namespace llvm;
41using namespace LoopVectorizationUtils;
42using namespace VPlanPatternMatch;
43
44namespace {
45// Class that is used to build the plain CFG for the incoming IR.
46class PlainCFGBuilder {
47 // The outermost loop of the input loop nest considered for vectorization.
48 Loop *TheLoop;
49
50 // Loop Info analysis.
51 LoopInfo *LI;
52
53 // Loop versioning for alias metadata.
54 LoopVersioning *LVer;
55
56 // Vectorization plan that we are working on.
57 std::unique_ptr<VPlan> Plan;
58
59 // Builder of the VPlan instruction-level representation.
60 VPBuilder VPIRBuilder;
61
62 // NOTE: The following maps are intentionally destroyed after the plain CFG
63 // construction because subsequent VPlan-to-VPlan transformation may
64 // invalidate them.
65 // Map incoming BasicBlocks to their newly-created VPBasicBlocks.
67 // Map incoming Value definitions to their newly-created VPValues.
68 DenseMap<Value *, VPValue *> IRDef2VPValue;
69
70 // Hold phi node's that need to be fixed once the plain CFG has been built.
72
73 // Utility functions.
74 void setVPBBPredsFromBB(VPBasicBlock *VPBB, BasicBlock *BB);
75 void fixHeaderPhis();
76 VPBasicBlock *getOrCreateVPBB(BasicBlock *BB);
77#ifndef NDEBUG
78 bool isExternalDef(Value *Val);
79#endif
80 VPValue *getOrCreateVPOperand(Value *IRVal);
81 void createVPInstructionsForVPBB(VPBasicBlock *VPBB, BasicBlock *BB);
82
83public:
84 PlainCFGBuilder(Loop *Lp, LoopInfo *LI, LoopVersioning *LVer, Type *IdxTy)
85 : TheLoop(Lp), LI(LI), LVer(LVer),
86 Plan(std::make_unique<VPlan>(Lp, IdxTy)) {}
87
88 /// Build plain CFG for TheLoop and connect it to Plan's entry.
89 std::unique_ptr<VPlan> buildPlainCFG();
90};
91} // anonymous namespace
92
93// Set predecessors of \p VPBB in the same order as they are in \p BB. \p VPBB
94// must have no predecessors.
95void PlainCFGBuilder::setVPBBPredsFromBB(VPBasicBlock *VPBB, BasicBlock *BB) {
96 // Collect VPBB predecessors.
98 for (BasicBlock *Pred : predecessors(BB))
99 VPBBPreds.push_back(getOrCreateVPBB(Pred));
100 VPBB->setPredecessors(VPBBPreds);
101}
102
103static bool isHeaderBB(BasicBlock *BB, Loop *L) {
104 return L && BB == L->getHeader();
105}
106
107// Add operands to VPInstructions representing phi nodes from the input IR.
108void PlainCFGBuilder::fixHeaderPhis() {
109 for (auto *Phi : PhisToFix) {
110 assert(IRDef2VPValue.count(Phi) && "Missing VPInstruction for PHINode.");
111 VPValue *VPVal = IRDef2VPValue[Phi];
112 assert(isa<VPPhi>(VPVal) && "Expected VPPhi for phi node.");
113 auto *PhiR = cast<VPPhi>(VPVal);
114 assert(PhiR->getNumOperands() == 0 && "Expected VPPhi with no operands.");
115 assert(isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent())) &&
116 "Expected Phi in header block.");
117 assert(Phi->getNumOperands() == 2 &&
118 "header phi must have exactly 2 operands");
119 for (BasicBlock *Pred : predecessors(Phi->getParent()))
120 PhiR->addIncoming(
121 getOrCreateVPOperand(Phi->getIncomingValueForBlock(Pred)));
122 }
123}
124
125// Create a new empty VPBasicBlock for an incoming BasicBlock or retrieve an
126// existing one if it was already created.
127VPBasicBlock *PlainCFGBuilder::getOrCreateVPBB(BasicBlock *BB) {
128 if (auto *VPBB = BB2VPBB.lookup(BB)) {
129 // Retrieve existing VPBB.
130 return VPBB;
131 }
132
133 // Create new VPBB.
134 StringRef Name = BB->getName();
135 LLVM_DEBUG(dbgs() << "Creating VPBasicBlock for " << Name << "\n");
136 VPBasicBlock *VPBB = Plan->createVPBasicBlock(Name);
137 BB2VPBB[BB] = VPBB;
138 return VPBB;
139}
140
141#ifndef NDEBUG
142// Return true if \p Val is considered an external definition. An external
143// definition is either:
144// 1. A Value that is not an Instruction. This will be refined in the future.
145// 2. An Instruction that is outside of the IR region represented in VPlan,
146// i.e., is not part of the loop nest.
147bool PlainCFGBuilder::isExternalDef(Value *Val) {
148 // All the Values that are not Instructions are considered external
149 // definitions for now.
151 if (!Inst)
152 return true;
153
154 // Check whether Instruction definition is in loop body.
155 return !TheLoop->contains(Inst);
156}
157#endif
158
159// Create a new VPValue or retrieve an existing one for the Instruction's
160// operand \p IRVal. This function must only be used to create/retrieve VPValues
161// for *Instruction's operands* and not to create regular VPInstruction's. For
162// the latter, please, look at 'createVPInstructionsForVPBB'.
163VPValue *PlainCFGBuilder::getOrCreateVPOperand(Value *IRVal) {
164 auto VPValIt = IRDef2VPValue.find(IRVal);
165 if (VPValIt != IRDef2VPValue.end())
166 // Operand has an associated VPInstruction or VPValue that was previously
167 // created.
168 return VPValIt->second;
169
170 // Operand doesn't have a previously created VPInstruction/VPValue. This
171 // means that operand is:
172 // A) a definition external to VPlan,
173 // B) any other Value without specific representation in VPlan.
174 // For now, we use VPValue to represent A and B and classify both as external
175 // definitions. We may introduce specific VPValue subclasses for them in the
176 // future.
177 assert(isExternalDef(IRVal) && "Expected external definition as operand.");
178
179 // A and B: Create VPValue and add it to the pool of external definitions and
180 // to the Value->VPValue map.
181 VPValue *NewVPVal = Plan->getOrAddLiveIn(IRVal);
182 IRDef2VPValue[IRVal] = NewVPVal;
183 return NewVPVal;
184}
185
186// Create new VPInstructions in a VPBasicBlock, given its BasicBlock
187// counterpart. This function must be invoked in RPO so that the operands of a
188// VPInstruction in \p BB have been visited before (except for Phi nodes).
189void PlainCFGBuilder::createVPInstructionsForVPBB(VPBasicBlock *VPBB,
190 BasicBlock *BB) {
191 VPIRBuilder.setInsertPoint(VPBB);
192 // TODO: Model and preserve debug intrinsics in VPlan.
193 for (Instruction &InstRef : *BB) {
194 Instruction *Inst = &InstRef;
195
196 // There shouldn't be any VPValue for Inst at this point. Otherwise, we
197 // visited Inst when we shouldn't, breaking the RPO traversal order.
198 assert(!IRDef2VPValue.count(Inst) &&
199 "Instruction shouldn't have been visited.");
200
201 if (isa<UncondBrInst>(Inst))
202 // Skip the rest of the Instruction processing for Branch instructions.
203 continue;
204
205 if (auto *Br = dyn_cast<CondBrInst>(Inst)) {
206 // Conditional branch instruction are represented using BranchOnCond
207 // recipes.
208 VPValue *Cond = getOrCreateVPOperand(Br->getCondition());
209 VPIRBuilder.createNaryOp(VPInstruction::BranchOnCond, {Cond}, Inst, {},
210 VPIRMetadata(*Inst), Inst->getDebugLoc());
211 continue;
212 }
213
214 if (auto *SI = dyn_cast<SwitchInst>(Inst)) {
215 // Don't emit recipes for unconditional switch instructions.
216 if (SI->getNumCases() == 0)
217 continue;
218 SmallVector<VPValue *> Ops = {getOrCreateVPOperand(SI->getCondition())};
219 for (auto Case : SI->cases())
220 Ops.push_back(getOrCreateVPOperand(Case.getCaseValue()));
221 VPIRBuilder.createNaryOp(Instruction::Switch, Ops, Inst, {},
222 VPIRMetadata(*Inst), Inst->getDebugLoc());
223 continue;
224 }
225
226 VPSingleDefRecipe *NewR;
227 if (auto *Phi = dyn_cast<PHINode>(Inst)) {
228 // Phi node's operands may not have been visited at this point. We create
229 // an empty VPInstruction that we will fix once the whole plain CFG has
230 // been built.
231 NewR = VPIRBuilder.createScalarPhi({}, Phi->getDebugLoc(), "vec.phi",
232 *Phi, Phi->getType());
233 NewR->setUnderlyingValue(Phi);
234 if (isHeaderBB(Phi->getParent(), LI->getLoopFor(Phi->getParent()))) {
235 // Header phis need to be fixed after the VPBB for the latch has been
236 // created.
237 PhisToFix.push_back(Phi);
238 } else {
239 // Add operands for VPPhi in the order matching its predecessors in
240 // VPlan.
241 DenseMap<const VPBasicBlock *, VPValue *> VPPredToIncomingValue;
242 for (unsigned I = 0; I != Phi->getNumOperands(); ++I) {
243 VPPredToIncomingValue[BB2VPBB[Phi->getIncomingBlock(I)]] =
244 getOrCreateVPOperand(Phi->getIncomingValue(I));
245 }
246 for (VPBlockBase *Pred : VPBB->getPredecessors())
247 cast<VPPhi>(NewR)->addIncoming(
248 VPPredToIncomingValue.lookup(Pred->getExitingBasicBlock()));
249 }
250 } else {
251 // Build VPIRMetadata from the instruction and add loop versioning
252 // metadata for loads and stores.
253 VPIRMetadata MD(*Inst);
254 if (isa<LoadInst, StoreInst>(Inst) && LVer) {
255 const auto &[AliasScopeMD, NoAliasMD] =
256 LVer->getNoAliasMetadataFor(Inst);
257 if (AliasScopeMD)
258 MD.setMetadata(LLVMContext::MD_alias_scope, AliasScopeMD);
259 if (NoAliasMD)
260 MD.setMetadata(LLVMContext::MD_noalias, NoAliasMD);
261 }
262
263 // Translate LLVM-IR operands into VPValue operands and set them in the
264 // new VPInstruction.
265 SmallVector<VPValue *, 4> VPOperands;
266 for (Value *Op : Inst->operands())
267 VPOperands.push_back(getOrCreateVPOperand(Op));
268
269 if (auto *CI = dyn_cast<CastInst>(Inst)) {
270 NewR = VPIRBuilder.createScalarCast(CI->getOpcode(), VPOperands[0],
271 CI->getType(), CI->getDebugLoc(),
272 VPIRFlags(*CI), MD);
273 NewR->setUnderlyingValue(CI);
274 } else if (auto *LI = dyn_cast<LoadInst>(Inst)) {
275 NewR = VPIRBuilder.createScalarLoad(LI->getType(), VPOperands[0],
276 LI->getDebugLoc(), MD);
277 NewR->setUnderlyingValue(LI);
278 } else {
279 // Build VPInstruction for any arbitrary Instruction without specific
280 // representation in VPlan.
281 NewR = VPIRBuilder.createNaryOp(
282 Inst->getOpcode(), VPOperands, Inst, VPIRFlags(*Inst), MD,
283 Inst->getDebugLoc(), "", Inst->getType());
284 }
285 }
286
287 IRDef2VPValue[Inst] = NewR;
288 }
289}
290
291// Main interface to build the plain CFG.
292std::unique_ptr<VPlan> PlainCFGBuilder::buildPlainCFG() {
293 VPIRBasicBlock *Entry = cast<VPIRBasicBlock>(Plan->getEntry());
294 BB2VPBB[Entry->getIRBasicBlock()] = Entry;
295 for (VPIRBasicBlock *ExitVPBB : Plan->getExitBlocks())
296 BB2VPBB[ExitVPBB->getIRBasicBlock()] = ExitVPBB;
297
298 // 1. Scan the body of the loop in a topological order to visit each basic
299 // block after having visited its predecessor basic blocks. Create a VPBB for
300 // each BB and link it to its successor and predecessor VPBBs. Note that
301 // predecessors must be set in the same order as they are in the incomming IR.
302 // Otherwise, there might be problems with existing phi nodes and algorithm
303 // based on predecessors traversal.
304
305 // Loop PH needs to be explicitly visited since it's not taken into account by
306 // LoopBlocksDFS.
307 BasicBlock *ThePreheaderBB = TheLoop->getLoopPreheader();
308 assert((ThePreheaderBB->getTerminator()->getNumSuccessors() == 1) &&
309 "Unexpected loop preheader");
310 for (auto &I : *ThePreheaderBB) {
311 if (I.getType()->isVoidTy())
312 continue;
313 IRDef2VPValue[&I] = Plan->getOrAddLiveIn(&I);
314 }
315
316 LoopBlocksRPO RPO(TheLoop);
317 RPO.perform(LI);
318
319 for (BasicBlock *BB : RPO) {
320 // Create or retrieve the VPBasicBlock for this BB.
321 VPBasicBlock *VPBB = getOrCreateVPBB(BB);
322 // Set VPBB predecessors in the same order as they are in the incoming BB.
323 setVPBBPredsFromBB(VPBB, BB);
324
325 // Create VPInstructions for BB.
326 createVPInstructionsForVPBB(VPBB, BB);
327
328 // Set VPBB successors. We create empty VPBBs for successors if they don't
329 // exist already. Recipes will be created when the successor is visited
330 // during the RPO traversal.
331 if (auto *SI = dyn_cast<SwitchInst>(BB->getTerminator())) {
333 getOrCreateVPBB(SI->getDefaultDest())};
334 for (auto Case : SI->cases())
335 Succs.push_back(getOrCreateVPBB(Case.getCaseSuccessor()));
336 VPBB->setSuccessors(Succs);
337 continue;
338 }
339 if (auto *BI = dyn_cast<UncondBrInst>(BB->getTerminator())) {
340 VPBB->setOneSuccessor(getOrCreateVPBB(BI->getSuccessor()));
341 continue;
342 }
343 auto *BI = cast<CondBrInst>(BB->getTerminator());
344 BasicBlock *IRSucc0 = BI->getSuccessor(0);
345 BasicBlock *IRSucc1 = BI->getSuccessor(1);
346 VPBasicBlock *Successor0 = getOrCreateVPBB(IRSucc0);
347 VPBasicBlock *Successor1 = getOrCreateVPBB(IRSucc1);
348 VPBB->setTwoSuccessors(Successor0, Successor1);
349 }
350
351 for (auto *EB : Plan->getExitBlocks())
352 setVPBBPredsFromBB(EB, EB->getIRBasicBlock());
353
354 // 2. The whole CFG has been built at this point so all the input Values must
355 // have a VPlan counterpart. Fix VPlan header phi by adding their
356 // corresponding VPlan operands.
357 fixHeaderPhis();
358
359 Plan->getEntry()->setOneSuccessor(getOrCreateVPBB(TheLoop->getHeader()));
360 Plan->getEntry()->setPlan(&*Plan);
361
362 // Fix VPlan loop-closed-ssa exit phi's by adding incoming operands to the
363 // VPIRInstructions wrapping them.
364 // // Note that the operand order corresponds to IR predecessor order, and may
365 // need adjusting when VPlan predecessors are added, if an exit block has
366 // multiple predecessor.
367 for (auto *EB : Plan->getExitBlocks()) {
368 for (VPRecipeBase &R : EB->phis()) {
369 auto *PhiR = cast<VPIRPhi>(&R);
370 PHINode &Phi = PhiR->getIRPhi();
371 assert(PhiR->getNumOperands() == 0 &&
372 "no phi operands should be added yet");
373 for (BasicBlock *Pred : predecessors(EB->getIRBasicBlock()))
374 PhiR->addIncoming(
375 getOrCreateVPOperand(Phi.getIncomingValueForBlock(Pred)));
376 }
377 }
378
379 LLVM_DEBUG(Plan->setName("Plain CFG\n"); dbgs() << *Plan);
380 return std::move(Plan);
381}
382
383/// Checks if \p HeaderVPB is a loop header block in the plain CFG; that is, it
384/// has exactly 2 predecessors (preheader and latch), where the block
385/// dominates the latch and the preheader dominates the block. If it is a
386/// header block return true and canonicalize the predecessors of the header
387/// (making sure the preheader appears first and the latch second) and the
388/// successors of the latch (making sure the loop exit comes first). Otherwise
389/// return false.
391 const VPDominatorTree &VPDT) {
392 ArrayRef<VPBlockBase *> Preds = HeaderVPB->getPredecessors();
393 if (Preds.size() != 2)
394 return false;
395
396 auto *PreheaderVPBB = Preds[0];
397 auto *LatchVPBB = Preds[1];
398 if (!VPDT.dominates(PreheaderVPBB, HeaderVPB) ||
399 !VPDT.dominates(HeaderVPB, LatchVPBB)) {
400 std::swap(PreheaderVPBB, LatchVPBB);
401
402 if (!VPDT.dominates(PreheaderVPBB, HeaderVPB) ||
403 !VPDT.dominates(HeaderVPB, LatchVPBB))
404 return false;
405
406 // Canonicalize predecessors of header so that preheader is first and
407 // latch second.
408 HeaderVPB->swapPredecessors();
409 for (VPRecipeBase &R : cast<VPBasicBlock>(HeaderVPB)->phis())
410 R.swapOperands();
411 }
412
413 // The two successors of conditional branch match the condition, with the
414 // first successor corresponding to true and the second to false. We
415 // canonicalize the successors of the latch when introducing the region, such
416 // that the latch exits the region when its condition is true; invert the
417 // original condition if the original CFG branches to the header on true.
418 // Note that the exit edge is not yet connected for top-level loops.
419 if (LatchVPBB->getSingleSuccessor() ||
420 LatchVPBB->getSuccessors()[0] != HeaderVPB)
421 return true;
422
423 assert(LatchVPBB->getNumSuccessors() == 2 && "Must have 2 successors");
424 auto *Term = cast<VPBasicBlock>(LatchVPBB)->getTerminator();
425 assert(cast<VPInstruction>(Term)->getOpcode() ==
427 "terminator must be a BranchOnCond");
428 auto *Not = new VPInstruction(VPInstruction::Not, {Term->getOperand(0)});
429 Not->insertBefore(Term);
430 Term->setOperand(0, Not);
431 LatchVPBB->swapSuccessors();
432
433 return true;
434}
435
436/// Create a new VPRegionBlock for the loop starting at \p HeaderVPB. For the
437/// outermost loop adjust the regions exiting terminator to be based on the
438/// canonical IV.
439static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB, DebugLoc DL) {
440 auto *PreheaderVPBB = HeaderVPB->getPredecessors()[0];
441 auto *LatchVPBB = cast<VPBasicBlock>(HeaderVPB->getPredecessors()[1]);
442 auto *OutermostHeaderVPBB =
444
445 VPBlockUtils::disconnectBlocks(PreheaderVPBB, HeaderVPB);
446 VPBlockUtils::disconnectBlocks(LatchVPBB, HeaderVPB);
447
448 // Create an empty region first and insert it between PreheaderVPBB and
449 // the exit blocks, taking care to preserve the original predecessor &
450 // successor order of blocks. Set region entry and exiting after both
451 // HeaderVPB and LatchVPBB have been disconnected from their
452 // predecessors/successors. Only the outermost loop has a canonical IV. Nested
453 // loops are assigned a canonical IV of null type and unknown debug location.
454 bool IsOutermost = HeaderVPB == OutermostHeaderVPBB;
455 Type *CanIVTy = nullptr;
456 if (IsOutermost)
457 CanIVTy = Plan.getVectorTripCount().getType();
458 else
460 auto *R = Plan.createLoopRegion(CanIVTy, DL);
461
462 // Transfer latch's successors to the region.
464
465 VPBlockUtils::connectBlocks(PreheaderVPBB, R);
466 R->setEntry(HeaderVPB);
467 R->setExiting(LatchVPBB);
468
469 // All VPBB's reachable shallowly from HeaderVPB belong to the current region.
470 for (VPBlockBase *VPBB : vp_depth_first_shallow(HeaderVPB))
471 VPBB->setParent(R);
472
473 if (!IsOutermost)
474 return;
475
476 auto *LatchTerm = LatchVPBB->getTerminator();
477 VPBuilder Builder(LatchTerm);
478 // Add a VPInstruction to increment the scalar canonical IV by VF * UF.
479 // Initially the induction increment is guaranteed to not wrap, but that may
480 // change later, e.g. when tail-folding, when the flags need to be dropped.
481 auto *CanonicalIVIncrement = Builder.createAdd(
482 R->getCanonicalIV(), &Plan.getVFxUF(), DL, "index.next", {true, false});
483
484 if (match(LatchTerm, m_BranchOnTwoConds())) {
485 auto *IsLatchExitTaken = Builder.createICmp(
486 CmpInst::ICMP_EQ, CanonicalIVIncrement, &Plan.getVectorTripCount());
487 LatchTerm->setOperand(1, IsLatchExitTaken);
488 } else {
489 // We are replacing the branch to exit the region. Remove the original
490 // BranchOnCond.
491 assert(match(LatchTerm, m_BranchOnCond()) && "Unexpected terminator");
492 DebugLoc LatchDL = LatchTerm->getDebugLoc();
493 Builder.createNaryOp(VPInstruction::BranchOnCount,
494 {CanonicalIVIncrement, &Plan.getVectorTripCount()},
495 LatchDL);
496 LatchTerm->eraseFromParent();
497 }
498}
499
500/// Creates extracts for values in \p Plan defined in a loop region and used
501/// outside a loop region.
502static void createExtractsForLiveOuts(VPlan &Plan, VPBasicBlock *MiddleVPBB) {
503 VPBuilder B(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
504 for (VPBasicBlock *EB : Plan.getExitBlocks()) {
505 if (!is_contained(EB->predecessors(), MiddleVPBB))
506 continue;
507
508 for (VPRecipeBase &R : EB->phis()) {
509 auto *ExitIRI = cast<VPIRPhi>(&R);
510 VPValue *Exiting = ExitIRI->getIncomingValueForBlock(MiddleVPBB);
511 if (isa<VPIRValue>(Exiting))
512 continue;
513 Exiting = B.createNaryOp(VPInstruction::ExtractLastPart, Exiting);
514 Exiting = B.createNaryOp(VPInstruction::ExtractLastLane, Exiting);
515 ExitIRI->setIncomingValueForBlock(MiddleVPBB, Exiting);
516 }
517 }
518}
519
520static void addInitialSkeleton(VPlan &Plan, Type *InductionTy,
521 PredicatedScalarEvolution &PSE, Loop *TheLoop) {
522 VPDominatorTree VPDT(Plan);
523
524 auto *HeaderVPBB = cast<VPBasicBlock>(Plan.getEntry()->getSingleSuccessor());
525 canonicalHeaderAndLatch(HeaderVPBB, VPDT);
526 auto *LatchVPBB = cast<VPBasicBlock>(HeaderVPBB->getPredecessors()[1]);
527
528 VPBasicBlock *VecPreheader = Plan.createVPBasicBlock("vector.ph");
529 VPBlockUtils::insertBlockAfter(VecPreheader, Plan.getEntry());
530
531 VPBasicBlock *MiddleVPBB = Plan.createVPBasicBlock("middle.block");
532 // The canonical LatchVPBB has the header block as last successor. If it has
533 // another successor, this successor is an exit block - insert middle block on
534 // its edge. Otherwise, add middle block as another successor retaining header
535 // as last. In the latter case, the latch has no conditional terminator yet,
536 // so insert a placeholder BranchOnCond that always continues to the header.
537 // It will be canonicalized to a BranchOnCount later
538 if (LatchVPBB->getNumSuccessors() == 2) {
539 VPBlockBase *LatchExitVPB = LatchVPBB->getSuccessors()[0];
540 VPBlockUtils::insertOnEdge(LatchVPBB, LatchExitVPB, MiddleVPBB);
541 } else {
542 VPBlockUtils::connectBlocks(LatchVPBB, MiddleVPBB);
543 LatchVPBB->swapSuccessors();
545 {Plan.getFalse()});
546 }
547
548 // Create SCEV and VPValue for the trip count.
549 // We use the symbolic max backedge-taken-count, which works also when
550 // vectorizing loops with uncountable early exits.
551 const SCEV *BackedgeTakenCountSCEV = PSE.getSymbolicMaxBackedgeTakenCount();
552 assert(!isa<SCEVCouldNotCompute>(BackedgeTakenCountSCEV) &&
553 "Invalid backedge-taken count");
554 ScalarEvolution &SE = *PSE.getSE();
555 const SCEV *TripCount = SE.getTripCountFromExitCount(BackedgeTakenCountSCEV,
556 InductionTy, TheLoop);
558
559 VPBasicBlock *ScalarPH = Plan.createVPBasicBlock("scalar.ph");
561
562 // The connection order corresponds to the operands of the conditional branch,
563 // with the middle block already connected to the exit block.
564 VPBlockUtils::connectBlocks(MiddleVPBB, ScalarPH);
565 // Also connect the entry block to the scalar preheader.
566 // TODO: Also introduce a branch recipe together with the minimum trip count
567 // check.
568 VPBlockUtils::connectBlocks(Plan.getEntry(), ScalarPH);
569 Plan.getEntry()->swapSuccessors();
570
571 createExtractsForLiveOuts(Plan, MiddleVPBB);
572
573 // Create resume phis in the scalar preheader for each phi in the scalar loop.
574 // Their incoming value from the vector loop will be the last lane of the
575 // corresponding vector loop header phi.
576 VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->getFirstNonPhi());
577 VPBuilder ScalarPHBuilder(ScalarPH);
578 assert(equal(ScalarPH->getPredecessors(),
579 ArrayRef<VPBlockBase *>({MiddleVPBB, Plan.getEntry()})) &&
580 "unexpected predecessor order of scalar ph");
581 for (const auto &[PhiR, ScalarPhiR] :
582 zip_equal(HeaderVPBB->phis(), Plan.getScalarHeader()->phis())) {
583 auto *VectorPhiR = cast<VPPhi>(&PhiR);
584 VPValue *BackedgeVal = VectorPhiR->getOperand(1);
585 VPValue *ResumeFromVectorLoop =
586 MiddleBuilder.createNaryOp(VPInstruction::ExtractLastPart, BackedgeVal);
587 ResumeFromVectorLoop = MiddleBuilder.createNaryOp(
588 VPInstruction::ExtractLastLane, ResumeFromVectorLoop);
589 // Create scalar resume phi, with the first operand being the incoming value
590 // from the middle block and the second operand coming from the entry block.
591 auto *ResumePhiR = ScalarPHBuilder.createScalarPhi(
592 {ResumeFromVectorLoop, VectorPhiR->getOperand(0)},
593 VectorPhiR->getDebugLoc());
594 cast<VPIRPhi>(&ScalarPhiR)->addIncoming(ResumePhiR);
595 }
596}
597
598/// Check \p Plan's live-in and replace them with constants, if they can be
599/// simplified via SCEV.
602 auto GetSimplifiedLiveInViaSCEV = [&](VPValue *VPV) -> VPValue * {
603 const SCEV *Expr = vputils::getSCEVExprForVPValue(VPV, PSE);
604 if (auto *C = dyn_cast<SCEVConstant>(Expr))
605 return Plan.getOrAddLiveIn(C->getValue());
606 return nullptr;
607 };
608
609 for (VPValue *LiveIn : to_vector(Plan.getLiveIns())) {
610 if (VPValue *SimplifiedLiveIn = GetSimplifiedLiveInViaSCEV(LiveIn))
611 LiveIn->replaceAllUsesWith(SimplifiedLiveIn);
612 }
613}
614
615/// To make RUN_VPLAN_PASS print initial VPlan.
617
618std::unique_ptr<VPlan>
619VPlanTransforms::buildVPlan0(Loop *TheLoop, LoopInfo &LI, Type *InductionTy,
621 LoopVersioning *LVer) {
622 PlainCFGBuilder Builder(TheLoop, &LI, LVer, InductionTy);
623 std::unique_ptr<VPlan> VPlan0 = Builder.buildPlainCFG();
624 addInitialSkeleton(*VPlan0, InductionTy, PSE, TheLoop);
625 simplifyLiveInsWithSCEV(*VPlan0, PSE);
626
628 return VPlan0;
629}
630
631/// Creates a VPWidenIntOrFpInductionRecipe or VPWidenPointerInductionRecipe
632/// for \p Phi based on \p IndDesc.
633static VPHeaderPHIRecipe *
635 const InductionDescriptor &IndDesc, VPlan &Plan,
636 PredicatedScalarEvolution &PSE, Loop &OrigLoop,
637 DebugLoc DL) {
638 [[maybe_unused]] ScalarEvolution &SE = *PSE.getSE();
639 assert(SE.isLoopInvariant(IndDesc.getStep(), &OrigLoop) &&
640 "step must be loop invariant");
641 assert((Plan.getLiveIn(IndDesc.getStartValue()) == Start ||
642 (SE.isSCEVable(IndDesc.getStartValue()->getType()) &&
643 PSE.getSCEV(IndDesc.getStartValue()) ==
644 vputils::getSCEVExprForVPValue(Start, PSE))) &&
645 "Start VPValue must match IndDesc's start value");
646
647 VPValue *Step =
649
650 VPValue *BackedgeVal = PhiR->getOperand(1);
651 // Replace live-out extracts of WideIV's backedge value by ExitingIVValue
652 // recipes. optimizeInductionLiveOutUsers will later compute the proper
653 // DerivedIV.
654 //
655 // For an IV that requires SCEV predicate, keep extracting the exit values
656 // from the loop directly, as the pre-computed exit value as-is would be
657 // incorrect outside the loop.
658 auto ReplaceExtractsWithExitingIVValueIfPossible = [&](VPWidenInductionRecipe
659 *WideIV) {
660 bool IsPredicated = !WideIV->getNoWrapPredicates().empty();
661 for (VPUser *U : to_vector(BackedgeVal->users())) {
663 continue;
664 auto *ExtractLastPart = cast<VPInstruction>(U);
665 VPUser *ExtractLastPartUser = ExtractLastPart->getSingleUser();
666 assert(ExtractLastPartUser && "must have a single user");
667 if (!match(ExtractLastPartUser, m_ExtractLastLane(m_VPValue())))
668 continue;
669 auto *ExtractLastLane = cast<VPInstruction>(ExtractLastPartUser);
670 assert(is_contained(ExtractLastLane->getParent()->successors(),
671 Plan.getScalarPreheader()) &&
672 "last lane must be extracted in the middle block");
673 // Keep the vector extract for exit-block live-out uses of a predicated
674 // IV.
675 if (IsPredicated &&
676 any_of(ExtractLastLane->users(), [&](VPUser *LaneUser) {
677 auto *R = cast<VPRecipeBase>(LaneUser);
678 return Plan.isExitBlock(R->getParent());
679 }))
680 continue;
681 VPBuilder Builder(ExtractLastLane);
682 ExtractLastLane->replaceAllUsesWith(
683 Builder.createNaryOp(VPInstruction::ExitingIVValue, {WideIV}));
684 ExtractLastLane->eraseFromParent();
685 ExtractLastPart->eraseFromParent();
686 }
687 };
688
690 auto *WideIV = new VPWidenPointerInductionRecipe(
691 Phi, Start, Step, &Plan.getVFxUF(), IndDesc, DL);
692 ReplaceExtractsWithExitingIVValueIfPossible(WideIV);
693 return WideIV;
694 }
695
698 "must have an integer or float induction at this point");
699
700 // Update wide induction increments to use the same step as the corresponding
701 // wide induction. This enables detecting induction increments directly in
702 // VPlan and removes redundant splats.
703 if (match(BackedgeVal, m_Add(m_Specific(PhiR), m_VPValue())))
704 BackedgeVal->getDefiningRecipe()->setOperand(1, Step);
705
706 // It is always safe to copy over the NoWrap and FastMath flags. In
707 // particular, when folding tail by masking, the masked-off lanes are never
708 // used, so it is safe.
710
711 auto *WideIV = new VPWidenIntOrFpInductionRecipe(
712 Phi, Start, Step, &Plan.getVF(), IndDesc, Flags, DL);
713
714 ReplaceExtractsWithExitingIVValueIfPossible(WideIV);
715 return WideIV;
716}
717
718/// Try to sink users of \p FOR after \p Previous. \returns true if sinking
719/// succeeded or was not necessary, and false otherwise.
720static bool
722 VPRecipeBase *Previous,
723 const VPDominatorTree &VPDT) {
724 // Collect recipes that need sinking.
727 Seen.insert(Previous);
728 auto TryToPushSinkCandidate = [&](VPRecipeBase *SinkCandidate) {
729 // The previous value must not depend on the users of the recurrence phi.
730 // In that case, FOR is not a fixed order recurrence.
731 if (SinkCandidate == Previous)
732 return false;
733
734 if (isa<VPHeaderPHIRecipe>(SinkCandidate) ||
735 !Seen.insert(SinkCandidate).second ||
736 VPDT.properlyDominates(Previous, SinkCandidate))
737 return true;
738
739 if (vputils::cannotHoistOrSinkRecipe(*SinkCandidate, /*Sinking=*/true))
740 return false;
741
742 WorkList.push_back(SinkCandidate);
743 return true;
744 };
745
746 // Recursively sink users of FOR after Previous.
747 WorkList.push_back(FOR);
748 for (unsigned I = 0; I != WorkList.size(); ++I) {
749 VPRecipeBase *Current = WorkList[I];
750 assert(Current->getNumDefinedValues() == 1 &&
751 "only recipes with a single defined value expected");
752
753 for (VPUser *User : Current->getVPSingleValue()->users()) {
754 if (!TryToPushSinkCandidate(cast<VPRecipeBase>(User)))
755 return false;
756 }
757 }
758
759 // Keep recipes to sink ordered by dominance so earlier instructions are
760 // processed first.
761 sort(WorkList, [&VPDT](const VPRecipeBase *A, const VPRecipeBase *B) {
762 return VPDT.properlyDominates(A, B);
763 });
764
765 for (VPRecipeBase *SinkCandidate : WorkList) {
766 if (SinkCandidate == FOR)
767 continue;
768
769 SinkCandidate->moveAfter(Previous);
770 Previous = SinkCandidate;
771 }
772 return true;
773}
774
775/// Try to hoist \p Previous and its operands before all users of \p FOR.
776/// \returns true if hoisting succeeded or was not necessary, and false
777/// otherwise.
779 VPRecipeBase *Previous,
780 const VPDominatorTree &VPDT) {
782 return false;
783
784 // Collect recipes that need hoisting.
785 SmallVector<VPRecipeBase *> HoistCandidates;
787 // Find the closest hoist point by looking at all users of FOR and selecting
788 // the recipe dominating all other users.
789 VPRecipeBase *HoistPoint = nullptr;
790 for (VPUser *U : FOR->users()) {
791 auto *R = cast<VPRecipeBase>(U);
792 if (!HoistPoint || VPDT.properlyDominates(R, HoistPoint))
793 HoistPoint = R;
794 }
795 assert(all_of(FOR->users(),
796 [&VPDT, HoistPoint](VPUser *U) {
797 auto *R = cast<VPRecipeBase>(U);
798 return HoistPoint == R ||
799 VPDT.properlyDominates(HoistPoint, R);
800 }) &&
801 "HoistPoint must dominate all users of FOR");
802
803 auto NeedsHoisting = [HoistPoint, &VPDT,
804 &Visited](VPValue *HoistCandidateV) -> VPRecipeBase * {
805 VPRecipeBase *HoistCandidate = HoistCandidateV->getDefiningRecipe();
806 if (!HoistCandidate)
807 return nullptr;
808 // Hoist candidate was already visited, no need to hoist.
809 if (!Visited.insert(HoistCandidate).second)
810 return nullptr;
811 // If we reached a recipe that dominates HoistPoint, we don't need to
812 // hoist the recipe.
813 if (VPDT.properlyDominates(HoistCandidate, HoistPoint))
814 return nullptr;
815 return HoistCandidate;
816 };
817
818 if (!NeedsHoisting(Previous->getVPSingleValue()))
819 return true;
820
821 // Recursively try to hoist Previous and its operands before all users of
822 // FOR.
823 HoistCandidates.push_back(Previous);
824
825 for (unsigned I = 0; I != HoistCandidates.size(); ++I) {
826 VPRecipeBase *Current = HoistCandidates[I];
827 assert(Current->getNumDefinedValues() == 1 &&
828 "only recipes with a single defined value expected");
830 return false;
831
832 for (VPValue *Op : Current->operands()) {
833 // If we reach FOR, it means the original Previous depends on some other
834 // recurrence that in turn depends on FOR. If that is the case, we would
835 // also need to hoist recipes involving the other FOR, which may break
836 // dependencies.
837 if (Op == FOR)
838 return false;
839
840 if (auto *R = NeedsHoisting(Op)) {
841 // Bail out if the recipe defines multiple values.
842 // TODO: Hoisting such recipes requires additional handling.
843 if (R->getNumDefinedValues() != 1)
844 return false;
845 HoistCandidates.push_back(R);
846 }
847 }
848 }
849
850 // Order recipes to hoist by dominance so earlier instructions are processed
851 // first.
852 sort(HoistCandidates, [&VPDT](const VPRecipeBase *A, const VPRecipeBase *B) {
853 return VPDT.properlyDominates(A, B);
854 });
855
856 for (VPRecipeBase *HoistCandidate : HoistCandidates) {
857 HoistCandidate->moveBefore(*HoistPoint->getParent(),
858 HoistPoint->getIterator());
859 }
860
861 return true;
862}
863
864/// Sink users of fixed-order recurrences past or hoist before the recipe
865/// defining the previous value, introduce FirstOrderRecurrenceSplice
866/// VPInstructions, and replace FOR uses. Returns false if hoisting or sinking
867/// fails.
869 const VPDominatorTree &VPDT) {
870 auto FORs =
873 [](VPRecipeBase &R) {
874 return cast<VPFirstOrderRecurrencePHIRecipe>(&R);
875 });
876 for (VPFirstOrderRecurrencePHIRecipe *FOR : FORs) {
877 // Follow through FOR phi chains to find the actual Previous recipe.
878 // Fixed-order recurrences do not contain cycles, so this loop is
879 // guaranteed to terminate.
881 VPRecipeBase *Previous = FOR->getBackedgeValue()->getDefiningRecipe();
882 while (auto *PrevPhi =
884 assert(PrevPhi->getParent() == FOR->getParent() &&
885 "PrevPhi must be in same block as FOR");
886 assert(SeenPhis.insert(PrevPhi).second &&
887 "PrevPhi must not be visited multiple times");
888 Previous = PrevPhi->getBackedgeValue()->getDefiningRecipe();
889 }
890
891 VPBasicBlock *InsertBlock = FOR->getParent();
892 VPBasicBlock::iterator InsertPt = InsertBlock->getFirstNonPhi();
893 if (Previous) {
894 // Sink FOR users after Previous or hoist Previous before FOR users.
895 if (!sinkRecurrenceUsersAfterPrevious(FOR, Previous, VPDT) &&
896 !hoistPreviousBeforeFORUsers(FOR, Previous, VPDT))
897 return false;
898 InsertBlock = Previous->getParent();
899 InsertPt = isa<VPHeaderPHIRecipe>(Previous)
900 ? InsertBlock->getFirstNonPhi()
901 : std::next(Previous->getIterator());
902 }
903
904 // Create FirstOrderRecurrenceSplice and replace FOR uses.
905 VPBuilder LoopBuilder(InsertBlock, InsertPt);
906 auto *RecurSplice =
908 {FOR, FOR->getBackedgeValue()});
909 FOR->replaceUsesWithIf(RecurSplice, [RecurSplice](VPUser &U, unsigned) {
910 return &U != RecurSplice;
911 });
912 }
913
914 return true;
915}
916
918 VPlan &Plan, PredicatedScalarEvolution &PSE, Loop &OrigLoop,
919 const VPDominatorTree &VPDT,
922 const SmallPtrSetImpl<const PHINode *> &FixedOrderRecurrences,
923 const SmallPtrSetImpl<PHINode *> &InLoopReductions, bool AllowReordering) {
924 // Retrieve the header manually from the intial plain-CFG VPlan.
925 auto [HeaderVPBB, LatchVPBB] = VPBlockUtils::getPlainCFGHeaderAndLatch(Plan);
926 assert(VPDT.dominates(HeaderVPBB, LatchVPBB) &&
927 "header must dominate its latch");
928
929 auto CreateHeaderPhiRecipe = [&](VPPhi *PhiR) -> VPHeaderPHIRecipe * {
930 // TODO: Gradually replace uses of underlying instruction by analyses on
931 // VPlan.
932 auto *Phi = cast<PHINode>(PhiR->getUnderlyingInstr());
933 assert(PhiR->getNumOperands() == 2 &&
934 "Must have 2 operands for header phis");
935
936 // Extract common values once.
937 VPIRValue *Start = cast<VPIRValue>(PhiR->getOperand(0));
938 VPValue *BackedgeValue = PhiR->getOperand(1);
939
940 if (FixedOrderRecurrences.contains(Phi)) {
941 // TODO: Currently fixed-order recurrences are modeled as chains of
942 // first-order recurrences. If there are no users of the intermediate
943 // recurrences in the chain, the fixed order recurrence should be
944 // modeled directly, enabling more efficient codegen.
945 return new VPFirstOrderRecurrencePHIRecipe(Phi, *Start, *BackedgeValue);
946 }
947
948 auto InductionIt = Inductions.find(Phi);
949 if (InductionIt != Inductions.end())
950 return createWidenInductionRecipe(Phi, PhiR, Start, InductionIt->second,
951 Plan, PSE, OrigLoop,
952 PhiR->getDebugLoc());
953
954 assert(Reductions.contains(Phi) && "only reductions are expected now");
955 const RecurrenceDescriptor &RdxDesc = Reductions.lookup(Phi);
957 Phi->getIncomingValueForBlock(OrigLoop.getLoopPreheader()) &&
958 "incoming value must match start value");
959 // Will be updated later to >1 if reduction is partial.
960 unsigned ScaleFactor = 1;
961 bool UseOrderedReductions = !AllowReordering && RdxDesc.isOrdered();
962 return new VPReductionPHIRecipe(
963 Phi, RdxDesc.getRecurrenceKind(), *Start, *BackedgeValue,
964 getReductionStyle(InLoopReductions.contains(Phi), UseOrderedReductions,
965 ScaleFactor),
966 Phi->getType()->isFloatingPointTy() ? RdxDesc.getFastMathFlags()
967 : VPIRFlags(),
969 };
970
971 for (VPRecipeBase &R : make_early_inc_range(HeaderVPBB->phis())) {
972 auto *PhiR = cast<VPPhi>(&R);
973 VPHeaderPHIRecipe *HeaderPhiR = CreateHeaderPhiRecipe(PhiR);
974 HeaderPhiR->insertBefore(PhiR);
975 PhiR->replaceAllUsesWith(HeaderPhiR);
976 PhiR->eraseFromParent();
977 }
978
979 if (!tryToSinkOrHoistRecurrenceUsers(HeaderVPBB, VPDT))
980 return false;
981
982 // Skip renaming resume phi recipes, if any header phi has been removed.
983 if (range_size(HeaderVPBB->phis()) !=
985 return true;
986 for (const auto &[HeaderPhiR, ScalarPhiR] :
987 zip_equal(HeaderVPBB->phis(), Plan.getScalarPreheader()->phis())) {
988 auto *ResumePhiR = cast<VPPhi>(&ScalarPhiR);
989 if (isa<VPFirstOrderRecurrencePHIRecipe>(&HeaderPhiR)) {
990 ResumePhiR->setName("scalar.recur.init");
991 auto *ExtractLastLane = cast<VPInstruction>(ResumePhiR->getOperand(0));
992 ExtractLastLane->setName("vector.recur.extract");
993 continue;
994 }
995 ResumePhiR->setName(isa<VPWidenInductionRecipe>(HeaderPhiR)
996 ? "bc.resume.val"
997 : "bc.merge.rdx");
998 }
999 return true;
1000}
1001
1004 bool OptForSize,
1005 unsigned SCEVCheckThreshold,
1007 Loop *TheLoop) {
1008 // Collect which wide IVs have predicates and add them to PSE.
1009 auto [HeaderVPBB, _] = VPBlockUtils::getPlainCFGHeaderAndLatch(Plan);
1011 for (auto &R : HeaderVPBB->phis()) {
1012 auto *WideIV = dyn_cast<VPWidenInductionRecipe>(&R);
1013 if (!WideIV || WideIV->getNoWrapPredicates().empty())
1014 continue;
1015 PredicatedIVs.insert(WideIV);
1016 for (const auto *P : WideIV->getNoWrapPredicates())
1017 PSE.addPredicate(*P);
1018 }
1019
1020 unsigned TotalComplexity = PSE.getPredicate().getComplexity();
1021 if (TotalComplexity && OptForSize) {
1022 LLVM_DEBUG(
1023 dbgs() << "LV: Not vectorizing: SCEV predicates needed for induction "
1024 "but optimizing for size\n");
1026 "Runtime SCEV check is required with -Os/-Oz",
1027 "runtime SCEV checks needed but optimizing for size",
1028 "CantVersionLoopWithOptForSize", ORE, TheLoop);
1029 return false;
1030 }
1031
1032 if (TotalComplexity > SCEVCheckThreshold) {
1033 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Too many SCEV checks needed ("
1034 << TotalComplexity << " > " << SCEVCheckThreshold
1035 << ")\n");
1037 "Too many SCEV checks needed",
1038 "Too many SCEV assumptions need to be made and checked at runtime",
1039 "TooManySCEVRunTimeChecks", ORE, TheLoop);
1040 return false;
1041 }
1042
1043 return true;
1044}
1045
1047 ElementCount MinVF) {
1050
1051 for (VPRecipeBase &R : Header->phis()) {
1052 auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&R);
1053 if (!PhiR || !PhiR->isInLoop() || (MinVF.isScalar() && !PhiR->isOrdered()))
1054 continue;
1055
1056 RecurKind Kind = PhiR->getRecurrenceKind();
1060 "AnyOf and Find reductions are not allowed for in-loop reductions");
1061
1062 bool IsFPRecurrence =
1064 FastMathFlags FMFs =
1065 IsFPRecurrence ? FastMathFlags::getFast() : FastMathFlags();
1066
1067 // Collect the chain of "link" recipes for the reduction starting at PhiR.
1069 Worklist.insert(PhiR);
1070 for (unsigned I = 0; I != Worklist.size(); ++I) {
1071 VPSingleDefRecipe *Cur = Worklist[I];
1072 for (VPUser *U : Cur->users()) {
1073 auto *UserRecipe = cast<VPSingleDefRecipe>(U);
1074 if (!UserRecipe->getParent()->getEnclosingLoopRegion()) {
1075 assert((UserRecipe->getParent() == Plan.getMiddleBlock() ||
1076 UserRecipe->getParent() == Plan.getScalarPreheader()) &&
1077 "U must be either in the loop region, the middle block or the "
1078 "scalar preheader.");
1079 continue;
1080 }
1081
1082 // Stores using instructions will be sunk later.
1083 if (match(UserRecipe, m_VPInstruction<Instruction::Store>()))
1084 continue;
1085 Worklist.insert(UserRecipe);
1086 }
1087 }
1088
1089 // Visit operation "Links" along the reduction chain top-down starting from
1090 // the phi until LoopExitValue. We keep track of the previous item
1091 // (PreviousLink) to tell which of the two operands of a Link will remain
1092 // scalar and which will be reduced. For minmax by select(cmp), Link will be
1093 // the select instructions. Blend recipes of in-loop reduction phi's will
1094 // get folded to their non-phi operand, as the reduction recipe handles the
1095 // condition directly.
1096 VPSingleDefRecipe *PreviousLink = PhiR; // Aka Worklist[0].
1097 for (VPSingleDefRecipe *CurrentLink : drop_begin(Worklist)) {
1098 if (auto *Blend = dyn_cast<VPBlendRecipe>(CurrentLink)) {
1099 assert(Blend->getNumIncomingValues() == 2 &&
1100 "Blend must have 2 incoming values");
1101 unsigned PhiRIdx = Blend->getIncomingValue(0) == PhiR ? 0 : 1;
1102 assert(Blend->getIncomingValue(PhiRIdx) == PhiR &&
1103 "PhiR must be an operand of the blend");
1104 Blend->replaceAllUsesWith(Blend->getIncomingValue(1 - PhiRIdx));
1105 continue;
1106 }
1107
1108 if (IsFPRecurrence) {
1109 FastMathFlags CurFMF =
1110 cast<VPRecipeWithIRFlags>(CurrentLink)->getFastMathFlagsOrNone();
1111 if (match(CurrentLink, m_Select(m_VPValue(), m_VPValue(), m_VPValue())))
1112 CurFMF |= cast<VPRecipeWithIRFlags>(CurrentLink->getOperand(0))
1113 ->getFastMathFlagsOrNone();
1114 FMFs &= CurFMF;
1115 }
1116
1117 Instruction *CurrentLinkI = CurrentLink->getUnderlyingInstr();
1118
1119 // Recognize a call to the llvm.fmuladd intrinsic.
1120 bool IsFMulAdd = Kind == RecurKind::FMulAdd;
1121 VPValue *VecOp;
1122 VPBasicBlock *LinkVPBB = CurrentLink->getParent();
1123 if (IsFMulAdd) {
1125 "Expected current VPInstruction to be a call to the "
1126 "llvm.fmuladd intrinsic");
1127 assert(CurrentLink->getOperand(2) == PreviousLink &&
1128 "expected a call where the previous link is the added operand");
1129
1130 // If the instruction is a call to the llvm.fmuladd intrinsic then we
1131 // need to create an fmul recipe (multiplying the first two operands of
1132 // the fmuladd together) to use as the vector operand for the fadd
1133 // reduction.
1134 auto *FMulRecipe = new VPInstruction(
1135 Instruction::FMul,
1136 {CurrentLink->getOperand(0), CurrentLink->getOperand(1)},
1137 CurrentLinkI->getFastMathFlags());
1138 LinkVPBB->insert(FMulRecipe, CurrentLink->getIterator());
1139 VecOp = FMulRecipe;
1140 } else if (Kind == RecurKind::AddChainWithSubs &&
1141 match(CurrentLink, m_Sub(m_VPValue(), m_VPValue()))) {
1142 Type *PhiTy = PhiR->getScalarType();
1143 auto *Zero = Plan.getConstantInt(PhiTy, 0);
1144 VPBuilder Builder(LinkVPBB, CurrentLink->getIterator());
1145 auto *Sub = Builder.createSub(Zero, CurrentLink->getOperand(1),
1146 CurrentLinkI->getDebugLoc());
1147 Sub->setUnderlyingValue(CurrentLinkI);
1148 VecOp = Sub;
1149 } else {
1150 // Index of the first operand which holds a non-mask vector operand.
1151 unsigned IndexOfFirstOperand = 0;
1153 if (match(CurrentLink, m_Cmp(m_VPValue(), m_VPValue())))
1154 continue;
1155 assert(match(CurrentLink,
1157 "must be a select recipe");
1158 IndexOfFirstOperand = 1;
1159 }
1160 // Note that for non-commutable operands (cmp-selects), the semantics of
1161 // the cmp-select are captured in the recurrence kind.
1162 unsigned VecOpId =
1163 CurrentLink->getOperand(IndexOfFirstOperand) == PreviousLink
1164 ? IndexOfFirstOperand + 1
1165 : IndexOfFirstOperand;
1166 VecOp = CurrentLink->getOperand(VecOpId);
1167 assert(
1168 VecOp != PreviousLink &&
1169 CurrentLink->getOperand(
1170 cast<VPInstruction>(CurrentLink)->getNumOperandsWithoutMask() -
1171 1 - (VecOpId - IndexOfFirstOperand)) == PreviousLink &&
1172 "PreviousLink must be the operand other than VecOp");
1173 }
1174
1175 assert(PhiR->getVFScaleFactor() == 1 &&
1176 "inloop reductions must be unscaled");
1177 VPValue *CondOp = cast<VPInstruction>(CurrentLink)->getMask();
1178 auto *RedRecipe = new VPReductionRecipe(
1179 Kind, FMFs, CurrentLinkI, PreviousLink, VecOp, CondOp,
1180 getReductionStyle(/*IsInLoop=*/true, PhiR->isOrdered(), 1),
1181 CurrentLinkI->getDebugLoc());
1182 // Append the recipe to the end of the VPBasicBlock because we need to
1183 // ensure that it comes after all of it's inputs, including CondOp.
1184 // Delete CurrentLink as it will be invalid if its operand is replaced
1185 // with a reduction defined at the bottom of the block in the next link.
1186 if (LinkVPBB->getNumSuccessors() == 0)
1187 RedRecipe->insertBefore(&*std::prev(std::prev(LinkVPBB->end())));
1188 else
1189 LinkVPBB->appendRecipe(RedRecipe);
1190
1191 CurrentLink->replaceAllUsesWith(RedRecipe);
1192 // Move any store recipes using the RedRecipe that appear before it in the
1193 // same block to just after the RedRecipe.
1194 for (VPUser *U : make_early_inc_range(RedRecipe->users())) {
1195 auto *UserR = dyn_cast<VPRecipeBase>(U);
1196 if (!UserR || UserR->getParent() != LinkVPBB)
1197 continue;
1199 continue;
1200 UserR->moveAfter(RedRecipe);
1201 }
1202 ToDelete.push_back(CurrentLink);
1203 PreviousLink = RedRecipe;
1204 }
1205 }
1206
1207 for (VPRecipeBase *R : ToDelete)
1208 R->eraseFromParent();
1209}
1210
1211/// Check if all loads in the loop are dereferenceable. Iterates over the
1212/// loop body blocks reachable from \p HeaderVPBB. Returns false if any
1213/// non-dereferenceable load is found.
1214static bool areAllLoadsDereferenceable(VPBasicBlock *HeaderVPBB, Loop *TheLoop,
1216 DominatorTree &DT, AssumptionCache *AC) {
1217 ScalarEvolution &SE = *PSE.getSE();
1218 const DataLayout &DL = TheLoop->getHeader()->getDataLayout();
1219 for (VPBasicBlock *VPBB : vp_rpo_plain_cfg_loop_body(HeaderVPBB)) {
1220 for (VPRecipeBase &R : *VPBB) {
1221 auto *VPI = dyn_cast<VPInstructionWithType>(&R);
1222 if (!VPI || VPI->getOpcode() != Instruction::Load) {
1223 assert(!R.mayReadFromMemory() && "unexpected recipe reading memory");
1224 continue;
1225 }
1226
1227 // Get the pointer SCEV for dereferenceability checking.
1228 VPValue *Ptr = VPI->getOperand(0);
1229 const SCEV *PtrSCEV = vputils::getSCEVExprForVPValue(Ptr, PSE, TheLoop);
1230 if (isa<SCEVCouldNotCompute>(PtrSCEV)) {
1231 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Found non-dereferenceable "
1232 "load with SCEVCouldNotCompute pointer\n");
1233 return false;
1234 }
1235
1236 // Check dereferenceability using the SCEV-based version.
1237 Type *LoadTy = VPI->getScalarType();
1238 const SCEV *SizeSCEV =
1239 SE.getStoreSizeOfExpr(DL.getIndexType(PtrSCEV->getType()), LoadTy);
1240 auto *Load = cast<LoadInst>(VPI->getUnderlyingValue());
1242 if (isDereferenceableAndAlignedInLoop(PtrSCEV, Load->getAlign(), SizeSCEV,
1243 TheLoop, SE, DT, AC, &Preds))
1244 continue;
1245
1246 LLVM_DEBUG(
1247 dbgs() << "LV: Not vectorizing: Auto-vectorization of loops with "
1248 "potentially faulting load is not supported.\n");
1249 return false;
1250 }
1251 }
1252 return true;
1253}
1254
1256 Loop *TheLoop,
1258 DominatorTree &DT, AssumptionCache *AC) {
1259 auto *MiddleVPBB = VPBlockUtils::getPlainCFGMiddleBlock(Plan);
1260 auto [HeaderVPBB, LatchVPBB] = VPBlockUtils::getPlainCFGHeaderAndLatch(Plan);
1261
1262 // TODO: We would like to detect uncountable exits and stores within loops
1263 // with such exits from the VPlan alone. Exit detection can be moved
1264 // here from handleUncountableEarlyExits, but we need to improve
1265 // detection of recipes which may write to memory.
1267 // Dereferenceability is checked separately for uncountable exit loops with
1268 // stores, as only the loads contributing to the exit condition need to
1269 // be checked.
1270 if (Style == UncountableExitStyle::ReadOnly &&
1271 !areAllLoadsDereferenceable(HeaderVPBB, TheLoop, PSE, DT, AC))
1272 return false;
1273 // TODO: Check target preference for style.
1274 return handleUncountableEarlyExits(Plan, HeaderVPBB, LatchVPBB, MiddleVPBB,
1275 TheLoop, PSE, DT, AC, Style);
1276 }
1277
1278 // Disconnect countable early exits from the loop, leaving it with a single
1279 // exit from the latch. Countable early exits are left for a scalar epilog.
1280 for (auto [EarlyExitingVPBB, EB] : vputils::getEarlyExits(Plan, MiddleVPBB)) {
1281 // Remove phi operands for the early exiting block.
1282 for (VPRecipeBase &R : EB->phis())
1283 cast<VPIRPhi>(&R)->removeIncomingValueFor(EarlyExitingVPBB);
1284 EarlyExitingVPBB->getTerminator()->eraseFromParent();
1285 VPBlockUtils::disconnectBlocks(EarlyExitingVPBB, EB);
1286 }
1287 return true;
1288}
1289
1291 auto *MiddleVPBB = VPBlockUtils::getPlainCFGMiddleBlock(Plan);
1292 // If MiddleVPBB has a single successor then the original loop does not exit
1293 // via the latch and the single successor must be the scalar preheader.
1294 // There's no need to add a runtime check to MiddleVPBB.
1295 if (MiddleVPBB->getNumSuccessors() == 1) {
1296 assert(MiddleVPBB->getSingleSuccessor() == Plan.getScalarPreheader() &&
1297 "must have ScalarPH as single successor");
1298 return;
1299 }
1300
1301 assert(MiddleVPBB->getNumSuccessors() == 2 && "must have 2 successors");
1302
1303 // Add a check in the middle block to see if we have completed all of the
1304 // iterations in the first vector loop.
1305 //
1306 // Three cases:
1307 // 1) If we require a scalar epilogue, the scalar ph must execute. Set the
1308 // condition to false.
1309 // 2) If (N - N%VF) == N, then we *don't* need to run the
1310 // remainder. Thus if tail is to be folded, we know we don't need to run
1311 // the remainder and we can set the condition to true.
1312 // 3) Otherwise, construct a runtime check.
1313
1314 // We use the same DebugLoc as the scalar loop latch terminator instead of
1315 // the corresponding compare because they may have ended up with different
1316 // line numbers and we want to avoid awkward line stepping while debugging.
1317 // E.g., if the compare has got a line number inside the loop.
1318 auto *LatchVPBB = cast<VPBasicBlock>(MiddleVPBB->getSinglePredecessor());
1319 DebugLoc LatchDL = LatchVPBB->getTerminator()->getDebugLoc();
1320 VPBuilder Builder(MiddleVPBB);
1321 VPValue *Cmp =
1322 Builder.createICmp(CmpInst::ICMP_EQ, Plan.getTripCount(),
1323 &Plan.getVectorTripCount(), LatchDL, "cmp.n");
1324 Builder.createNaryOp(VPInstruction::BranchOnCond, {Cmp}, LatchDL);
1325}
1326
1328 VPDominatorTree VPDT(Plan);
1330 Plan.getEntry());
1331 for (VPBlockBase *HeaderVPB : POT)
1332 if (canonicalHeaderAndLatch(HeaderVPB, VPDT))
1333 createLoopRegion(Plan, HeaderVPB, DL);
1334
1335 VPRegionBlock *TopRegion = Plan.getVectorLoopRegion();
1336 TopRegion->setName("vector loop");
1337 TopRegion->getEntryBasicBlock()->setName("vector.body");
1338}
1339
1341 assert(Plan.getExitBlocks().size() == 1 &&
1342 "only a single-exit block is supported currently");
1343 assert(Plan.getExitBlocks().front()->getSinglePredecessor() ==
1344 Plan.getMiddleBlock() &&
1345 "the exit block must have middle block as single predecessor");
1346
1347 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
1348 assert(LoopRegion->getSingleSuccessor() == Plan.getMiddleBlock() &&
1349 "The vector loop region must have the middle block as its single "
1350 "successor for now");
1351 VPBasicBlock *Header = LoopRegion->getEntryBasicBlock();
1352
1353 Header->splitAt(Header->getFirstNonPhi());
1354
1355 // Abstract header mask, materialized into concrete recipes later.
1356 VPValue *HeaderMask = LoopRegion->createHeaderMask();
1357 VPBuilder Builder(Header, Header->getFirstNonPhi());
1358 Builder.createNaryOp(VPInstruction::BranchOnCond, HeaderMask);
1359
1360 VPBasicBlock *OrigLatch = LoopRegion->getExitingBasicBlock();
1361 VPValue *IVInc;
1362 [[maybe_unused]] bool TermBranchOnCount =
1363 match(OrigLatch->getTerminator(),
1365 m_Specific(&Plan.getVectorTripCount())));
1366 assert(TermBranchOnCount &&
1367 match(IVInc, m_Add(m_Specific(LoopRegion->getCanonicalIV()),
1368 m_Specific(&Plan.getVFxUF()))) &&
1369 std::next(IVInc->getDefiningRecipe()->getIterator()) ==
1370 OrigLatch->getTerminator()->getIterator() &&
1371 "Unexpected canonical iv increment");
1372
1373 // Split the latch at the IV update, and branch to it from the header mask.
1374 VPBasicBlock *Latch =
1375 OrigLatch->splitAt(IVInc->getDefiningRecipe()->getIterator());
1376 Latch->setName("vector.latch");
1377 VPBlockUtils::connectBlocks(Header, Latch);
1378
1379 // Collect any values defined in the loop that need a phi. Currently this
1380 // includes header phi backedges and live-outs extracted in the middle block.
1381 // TODO: Handle early exits via Plan.getExitBlocks()
1383 for (VPRecipeBase &R : Header->phis())
1385 NeedsPhi[cast<VPHeaderPHIRecipe>(R).getBackedgeValue()].push_back(&R);
1386
1387 VPValue *V;
1388 for (VPRecipeBase &R : *Plan.getMiddleBlock())
1389 if (match(&R, m_ExtractLastPart(m_VPValue(V))))
1390 NeedsPhi[V].push_back(&R);
1391
1392 // Insert phis for values coming past the end of the tail.
1393 Builder.setInsertPoint(Latch, Latch->begin());
1394 for (const auto &[V, Users] : NeedsPhi) {
1395 if (isa<VPIRValue>(V))
1396 continue;
1397 VPValue *TailVal = Plan.getPoison(V->getScalarType());
1398 VPIRFlags Flags;
1400 "Value used by more than two reduction phis?");
1402 auto *RdxPhi =
1403 RedIt != Users.end() ? cast<VPReductionPHIRecipe>(*RedIt) : nullptr;
1404 if (RdxPhi && !RdxPhi->isInLoop()) {
1405 TailVal = RdxPhi;
1406 Flags = *RdxPhi;
1407 }
1408
1409 VPInstruction *Phi = Builder.createScalarPhi({V, TailVal}, {}, "", Flags);
1410 for (VPUser *U : Users)
1411 U->replaceUsesOfWith(V, Phi);
1412 }
1413
1414 // Any extract of the last element must be updated to extract from the last
1415 // active lane of the header mask instead (i.e., the lane corresponding to the
1416 // last active iteration).
1417 Builder.setInsertPoint(Plan.getMiddleBlock()->getTerminator());
1418 for (VPRecipeBase &R : *Plan.getMiddleBlock()) {
1419 VPValue *Op;
1421 continue;
1422
1423 // Compute the index of the last active lane.
1424 VPValue *LastActiveLane = Builder.createLastActiveLane(HeaderMask);
1425 auto *Ext =
1426 Builder.createNaryOp(VPInstruction::ExtractLane, {LastActiveLane, Op});
1427 R.getVPSingleValue()->replaceAllUsesWith(Ext);
1428 }
1429
1430 // VectorTripCount now equals TripCount so simplify the MiddleVPBB branch.
1434 m_Specific(&Plan.getVectorTripCount())))) &&
1435 "Unexpected MiddleVPBB branch");
1436 Plan.getMiddleBlock()->getTerminator()->setOperand(0, Plan.getTrue());
1437}
1438
1439/// Insert \p CheckBlockVPBB on the edge leading to the vector preheader,
1440/// connecting it to both vector and scalar preheaders. Updates scalar
1441/// preheader phis to account for the new predecessor.
1443 VPBasicBlock *CheckBlockVPBB) {
1444 VPBlockBase *VectorPH = Plan.getVectorPreheader();
1445 auto *ScalarPH = cast<VPBasicBlock>(Plan.getScalarPreheader());
1446 VPBlockBase *PreVectorPH = VectorPH->getSinglePredecessor();
1447 VPBlockUtils::insertOnEdge(PreVectorPH, VectorPH, CheckBlockVPBB);
1448 VPBlockUtils::connectBlocks(CheckBlockVPBB, ScalarPH);
1449 CheckBlockVPBB->swapSuccessors();
1450 unsigned NumPreds = ScalarPH->getNumPredecessors();
1451 for (VPRecipeBase &R : ScalarPH->phis()) {
1452 auto *Phi = cast<VPPhi>(&R);
1453 assert(Phi->getNumIncoming() == NumPreds - 1 &&
1454 "must have incoming values for all predecessors");
1455 Phi->addIncoming(Phi->getOperand(NumPreds - 2));
1456 }
1457}
1458
1459// Likelyhood of bypassing the vectorized loop due to a runtime check block,
1460// including memory overlap checks block and wrapping/unit-stride checks block.
1461static constexpr uint32_t CheckBypassWeights[] = {1, 127};
1462
1463/// Create a BranchOnCond terminator in \p CheckBlockVPBB. Optionally adds
1464/// branch weights.
1465static void addBypassBranch(VPlan &Plan, VPBasicBlock *CheckBlockVPBB,
1466 VPValue *Cond, bool AddBranchWeights) {
1468 auto *Term = VPBuilder(CheckBlockVPBB)
1470 if (AddBranchWeights) {
1471 MDBuilder MDB(Plan.getContext());
1472 MDNode *BranchWeights =
1473 MDB.createBranchWeights(CheckBypassWeights, /*IsExpected=*/false);
1474 Term->setMetadata(LLVMContext::MD_prof, BranchWeights);
1475 }
1476}
1477
1479 VPBasicBlock *CheckBlock,
1480 bool AddBranchWeights) {
1481 insertCheckBlockBeforeVectorLoop(Plan, CheckBlock);
1482 addBypassBranch(Plan, CheckBlock, Cond, AddBranchWeights);
1483}
1484
1486 BasicBlock *CheckBlock,
1487 bool AddBranchWeights) {
1488 VPValue *CondVPV = Plan.getOrAddLiveIn(Cond);
1489 VPBasicBlock *CheckBlockVPBB = Plan.createVPIRBasicBlock(CheckBlock);
1490 attachVPCheckBlock(Plan, CondVPV, CheckBlockVPBB, AddBranchWeights);
1491}
1492
1494 VPlan &Plan, ElementCount VF, unsigned UF,
1495 ElementCount MinProfitableTripCount, bool RequiresScalarEpilogue,
1496 bool TailFolded, Loop *OrigLoop, const uint32_t *MinItersBypassWeights,
1498 // Generate code to check if the loop's trip count is less than VF * UF, or
1499 // equal to it in case a scalar epilogue is required; this implies that the
1500 // vector trip count is zero. This check also covers the case where adding one
1501 // to the backedge-taken count overflowed leading to an incorrect trip count
1502 // of zero. In this case we will also jump to the scalar loop.
1503 CmpInst::Predicate CmpPred =
1504 RequiresScalarEpilogue ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT;
1505 // If tail is to be folded, vector loop takes care of all iterations.
1506 VPValue *TripCountVPV = Plan.getTripCount();
1507 const SCEV *TripCount = vputils::getSCEVExprForVPValue(TripCountVPV, PSE);
1508 Type *TripCountTy = TripCount->getType();
1509 ScalarEvolution &SE = *PSE.getSE();
1510 auto GetMinTripCount = [&]() -> const SCEV * {
1511 // Compute max(MinProfitableTripCount, UF * VF) and return it.
1512 const SCEV *VFxUF =
1513 SE.getElementCount(TripCountTy, (VF * UF), SCEV::FlagNUW);
1514 if (UF * VF.getKnownMinValue() >=
1515 MinProfitableTripCount.getKnownMinValue()) {
1516 // TODO: SCEV should be able to simplify test.
1517 return VFxUF;
1518 }
1519 const SCEV *MinProfitableTripCountSCEV =
1520 SE.getElementCount(TripCountTy, MinProfitableTripCount, SCEV::FlagNUW);
1521 return SE.getUMaxExpr(MinProfitableTripCountSCEV, VFxUF);
1522 };
1523
1524 VPBuilder Builder(CheckBlock);
1525 VPValue *TripCountCheck = Plan.getFalse();
1526 const SCEV *Step = GetMinTripCount();
1527 // TripCountCheck = false, folding tail implies positive vector trip
1528 // count.
1529 if (!TailFolded) {
1530 // TODO: Emit unconditional branch to vector preheader instead of
1531 // conditional branch with known condition.
1532 TripCount = SE.applyLoopGuards(TripCount, OrigLoop);
1533 // Check if the trip count is < the step.
1534 if (SE.isKnownPredicate(CmpPred, TripCount, Step)) {
1535 // TODO: Ensure step is at most the trip count when determining max VF and
1536 // UF, w/o tail folding.
1537 TripCountCheck = Plan.getTrue();
1538 } else if (!SE.isKnownPredicate(CmpInst::getInversePredicate(CmpPred),
1539 TripCount, Step)) {
1540 // Generate the minimum iteration check only if we cannot prove the
1541 // check is known to be true, or known to be false.
1542 // Try to expand Step into VPInstructions in CheckBlock; otherwise fall
1543 // back to a VPExpandSCEV recipe in the plan's entry block.
1544 VPValue *MinTripCountVPV =
1545 VPSCEVExpander(Builder, *PSE.getSE(), DL).tryToExpand(Step);
1546 if (!MinTripCountVPV)
1547 MinTripCountVPV = VPBuilder(Plan.getEntry()).createExpandSCEV(Step);
1548 TripCountCheck = Builder.createICmp(
1549 CmpPred, TripCountVPV, MinTripCountVPV, DL, "min.iters.check");
1550 } // else step known to be < trip count, use TripCountCheck preset to false.
1551 }
1552 VPInstruction *Term =
1553 Builder.createNaryOp(VPInstruction::BranchOnCond, {TripCountCheck}, DL);
1555 MDBuilder MDB(Plan.getContext());
1556 MDNode *BranchWeights = MDB.createBranchWeights(
1557 ArrayRef(MinItersBypassWeights, 2), /*IsExpected=*/false);
1558 Term->setMetadata(LLVMContext::MD_prof, BranchWeights);
1559 }
1560}
1561
1563 VPlan &Plan, ElementCount VF, unsigned UF, bool RequiresScalarEpilogue,
1564 Loop *OrigLoop, const uint32_t *MinItersBypassWeights, DebugLoc DL,
1566 auto *CheckBlock = Plan.createVPBasicBlock("vector.main.loop.iter.check");
1567 insertCheckBlockBeforeVectorLoop(Plan, CheckBlock);
1569 RequiresScalarEpilogue, /*TailFolded=*/false,
1570 OrigLoop, MinItersBypassWeights, DL, PSE,
1571 CheckBlock);
1572}
1573
1575 VPlan &Plan, Value *VectorTripCount, bool RequiresScalarEpilogue,
1576 ElementCount EpilogueVF, unsigned EpilogueUF, unsigned MainLoopStep,
1577 unsigned EpilogueLoopStep, ScalarEvolution &SE) {
1578 // Add the minimum iteration check for the epilogue vector loop.
1579 VPValue *TC = Plan.getTripCount();
1580 Value *TripCount = TC->getLiveInIRValue();
1581 VPBuilder Builder(cast<VPBasicBlock>(Plan.getEntry()));
1582 VPValue *VFxUF = Builder.createExpandSCEV(SE.getElementCount(
1583 TripCount->getType(), (EpilogueVF * EpilogueUF), SCEV::FlagNUW));
1584 VPValue *Count = Builder.createSub(TC, Plan.getOrAddLiveIn(VectorTripCount),
1585 DebugLoc::getUnknown(), "n.vec.remaining");
1586
1587 // Generate code to check if the loop's trip count is less than VF * UF of
1588 // the vector epilogue loop.
1589 auto P = RequiresScalarEpilogue ? ICmpInst::ICMP_ULE : ICmpInst::ICMP_ULT;
1590 auto *CheckMinIters = Builder.createICmp(
1591 P, Count, VFxUF, DebugLoc::getUnknown(), "min.epilog.iters.check");
1592 VPInstruction *Branch =
1593 Builder.createNaryOp(VPInstruction::BranchOnCond, CheckMinIters);
1594
1595 // We assume the remaining `Count` is equally distributed in
1596 // [0, MainLoopStep)
1597 // So the probability for `Count < EpilogueLoopStep` should be
1598 // min(MainLoopStep, EpilogueLoopStep) / MainLoopStep
1599 // TODO: Improve the estimate by taking the estimated trip count into
1600 // consideration.
1601 unsigned EstimatedSkipCount = std::min(MainLoopStep, EpilogueLoopStep);
1602 const uint32_t Weights[] = {EstimatedSkipCount,
1603 MainLoopStep - EstimatedSkipCount};
1604 MDBuilder MDB(Plan.getContext());
1605 MDNode *BranchWeights =
1606 MDB.createBranchWeights(Weights, /*IsExpected=*/false);
1607 Branch->setMetadata(LLVMContext::MD_prof, BranchWeights);
1608}
1609
1610/// Find and return the final select instruction of the FindIV result pattern
1611/// for the given \p BackedgeVal:
1612/// select(icmp ne ComputeReductionResult(ReducedIV), Sentinel),
1613/// ComputeReductionResult(ReducedIV), Start.
1615 return cast<VPInstruction>(
1616 vputils::findRecipe(BackedgeVal, [BackedgeVal](VPRecipeBase *R) {
1617 auto *VPI = dyn_cast<VPInstruction>(R);
1618 return VPI &&
1619 matchFindIVResult(VPI, m_Specific(BackedgeVal), m_VPValue());
1620 }));
1621}
1622
1624 auto GetMinOrMaxCompareValue =
1625 [](VPReductionPHIRecipe *RedPhiR) -> VPValue * {
1626 auto *MinOrMaxR =
1627 dyn_cast_or_null<VPRecipeWithIRFlags>(RedPhiR->getBackedgeValue());
1628 if (!MinOrMaxR)
1629 return nullptr;
1630
1631 // Check that MinOrMaxR is a VPWidenIntrinsicRecipe or VPReplicateRecipe
1632 // with an intrinsic that matches the reduction kind.
1633 Intrinsic::ID ExpectedIntrinsicID =
1634 getMinMaxReductionIntrinsicOp(RedPhiR->getRecurrenceKind());
1635 if (!match(MinOrMaxR, m_Intrinsic(ExpectedIntrinsicID)))
1636 return nullptr;
1637
1638 if (MinOrMaxR->getOperand(0) == RedPhiR)
1639 return MinOrMaxR->getOperand(1);
1640
1641 assert(MinOrMaxR->getOperand(1) == RedPhiR &&
1642 "Reduction phi operand expected");
1643 return MinOrMaxR->getOperand(0);
1644 };
1645
1646 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
1648 MinOrMaxNumReductionsToHandle;
1649 bool HasUnsupportedPhi = false;
1650 for (auto &R : LoopRegion->getEntryBasicBlock()->phis()) {
1652 continue;
1653 auto *Cur = dyn_cast<VPReductionPHIRecipe>(&R);
1654 if (!Cur) {
1655 // TODO: Also support fixed-order recurrence phis.
1656 HasUnsupportedPhi = true;
1657 continue;
1658 }
1660 Cur->getRecurrenceKind())) {
1661 HasUnsupportedPhi = true;
1662 continue;
1663 }
1664
1665 VPValue *MinOrMaxOp = GetMinOrMaxCompareValue(Cur);
1666 if (!MinOrMaxOp)
1667 return false;
1668
1669 MinOrMaxNumReductionsToHandle.emplace_back(Cur, MinOrMaxOp);
1670 }
1671
1672 if (MinOrMaxNumReductionsToHandle.empty())
1673 return true;
1674
1675 // We won't be able to resume execution in the scalar tail, if there are
1676 // unsupported header phis or there is no scalar tail at all, due to
1677 // tail-folding.
1678 if (HasUnsupportedPhi || !Plan.hasScalarTail())
1679 return false;
1680
1681 /// Check if the vector loop of \p Plan can early exit and restart
1682 /// execution of last vector iteration in the scalar loop. This requires all
1683 /// recipes up to early exit point be side-effect free as they are
1684 /// re-executed. Currently we check that the loop is free of any recipe that
1685 /// may write to memory. Expected to operate on an early VPlan w/o nested
1686 /// regions.
1689 auto *VPBB = cast<VPBasicBlock>(VPB);
1690 for (auto &R : *VPBB) {
1691 if (R.mayWriteToMemory() && !match(&R, m_BranchOnCount()))
1692 return false;
1693 }
1694 }
1695
1696 VPBasicBlock *LatchVPBB = LoopRegion->getExitingBasicBlock();
1697 VPBuilder LatchBuilder(LatchVPBB->getTerminator());
1698 VPValue *AllNaNLanes = nullptr;
1699 SmallPtrSet<VPValue *, 2> RdxResults;
1700 for (const auto &[_, MinOrMaxOp] : MinOrMaxNumReductionsToHandle) {
1701 VPValue *RedNaNLanes =
1702 LatchBuilder.createFCmp(CmpInst::FCMP_UNO, MinOrMaxOp, MinOrMaxOp);
1703 AllNaNLanes = AllNaNLanes ? LatchBuilder.createOr(AllNaNLanes, RedNaNLanes)
1704 : RedNaNLanes;
1705 }
1706
1707 VPValue *AnyNaNLane =
1708 LatchBuilder.createNaryOp(VPInstruction::AnyOf, {AllNaNLanes});
1709 VPBasicBlock *MiddleVPBB = Plan.getMiddleBlock();
1710 VPBuilder MiddleBuilder(MiddleVPBB, MiddleVPBB->begin());
1711 for (const auto &[RedPhiR, _] : MinOrMaxNumReductionsToHandle) {
1713 RedPhiR->getRecurrenceKind()) &&
1714 "unsupported reduction");
1715
1716 // If we exit early due to NaNs, compute the final reduction result based on
1717 // the reduction phi at the beginning of the last vector iteration.
1718 auto *RdxResult = vputils::findComputeReductionResult(RedPhiR);
1719 assert(RdxResult && "must find a ComputeReductionResult");
1720
1721 auto *NewSel = MiddleBuilder.createSelect(AnyNaNLane, RedPhiR,
1722 RdxResult->getOperand(0));
1723 RdxResult->setOperand(0, NewSel);
1724 assert(!RdxResults.contains(RdxResult) && "RdxResult already used");
1725 RdxResults.insert(RdxResult);
1726 }
1727
1728 auto *LatchExitingBranch = LatchVPBB->getTerminator();
1729 assert(match(LatchExitingBranch, m_BranchOnCount(m_VPValue(), m_VPValue())) &&
1730 "Unexpected terminator");
1731 auto *IsLatchExitTaken = LatchBuilder.createICmp(
1732 CmpInst::ICMP_EQ, LatchExitingBranch->getOperand(0),
1733 LatchExitingBranch->getOperand(1));
1734 auto *AnyExitTaken = LatchBuilder.createOr(AnyNaNLane, IsLatchExitTaken);
1735 LatchBuilder.createNaryOp(VPInstruction::BranchOnCond, AnyExitTaken);
1736 LatchExitingBranch->eraseFromParent();
1737
1738 // Update resume phis for inductions in the scalar preheader. If AnyNaNLane is
1739 // true, the resume from the start of the last vector iteration via the
1740 // canonical IV, otherwise from the original value.
1741 auto IsTC = [&Plan](VPValue *V) {
1742 return V == &Plan.getVectorTripCount() || V == Plan.getTripCount();
1743 };
1744 for (auto &R : Plan.getScalarPreheader()->phis()) {
1745 auto *ResumeR = cast<VPPhi>(&R);
1746 VPValue *VecV = ResumeR->getOperand(0);
1747 if (RdxResults.contains(VecV))
1748 continue;
1749 if (auto *DerivedIV = dyn_cast<VPDerivedIVRecipe>(VecV)) {
1750 VPValue *DIVTC = DerivedIV->getOperand(1);
1751 if (DerivedIV->hasOneUse() && IsTC(DIVTC)) {
1752 auto *NewSel = MiddleBuilder.createSelect(
1753 AnyNaNLane, LoopRegion->getCanonicalIV(), DIVTC);
1754 DerivedIV->moveAfter(MiddleBuilder.getRecipeAtInsertPoint());
1755 DerivedIV->setOperand(1, NewSel);
1756 continue;
1757 }
1758 }
1759 // Bail out and abandon the current, partially modified, VPlan if we
1760 // encounter resume phi that cannot be updated yet.
1761 if (!IsTC(VecV)) {
1762 LLVM_DEBUG(dbgs() << "Found resume phi we cannot update for VPlan with "
1763 "FMaxNum/FMinNum reduction.\n");
1764 return false;
1765 }
1766 auto *NewSel = MiddleBuilder.createSelect(
1767 AnyNaNLane, LoopRegion->getCanonicalIV(), VecV);
1768 ResumeR->setOperand(0, NewSel);
1769 }
1770
1771 auto *MiddleTerm = MiddleVPBB->getTerminator();
1772 MiddleBuilder.setInsertPoint(MiddleTerm);
1773 VPValue *MiddleCond = MiddleTerm->getOperand(0);
1774 VPValue *NewCond =
1775 MiddleBuilder.createAnd(MiddleCond, MiddleBuilder.createNot(AnyNaNLane));
1776 MiddleTerm->setOperand(0, NewCond);
1777 return true;
1778}
1779
1781 if (Plan.hasScalarVFOnly())
1782 return false;
1783
1784 // We want to create the following nodes:
1785 // vector.body:
1786 // ...new WidenPHI recipe introduced to keep the mask value for the latest
1787 // iteration where any lane was active.
1788 // mask.phi = phi [ ir<false>, vector.ph ], [ vp<new.mask>, vector.body ]
1789 // ...data.phi (a VPReductionPHIRecipe for a FindLast reduction) already
1790 // exists, but needs updating to use 'new.data' for the backedge value.
1791 // data.phi = phi ir<default.val>, vp<new.data>
1792 //
1793 // ...'data' and 'compare' created by existing nodes...
1794 //
1795 // ...new recipes introduced to determine whether to update the reduction
1796 // values or keep the current one.
1797 // any.active = i1 any-of ir<compare>
1798 // new.mask = select vp<any.active>, ir<compare>, vp<mask.phi>
1799 // new.data = select vp<any.active>, ir<data>, ir<data.phi>
1800 //
1801 // middle.block:
1802 // ...extract-last-active replaces compute-reduction-result.
1803 // result = extract-last-active vp<new.data>, vp<new.mask>, ir<default.val>
1804
1806 for (VPRecipeBase &Phi :
1808 auto *PhiR = dyn_cast<VPReductionPHIRecipe>(&Phi);
1810 PhiR->getRecurrenceKind()))
1811 Phis.push_back(PhiR);
1812 }
1813
1814 if (Phis.empty())
1815 return true;
1816
1817 VPValue *HeaderMask = Plan.getVectorLoopRegion()->getHeaderMask();
1818 for (VPReductionPHIRecipe *PhiR : Phis) {
1819 // Find the condition for the select/blend.
1820 VPValue *BackedgeSelect = PhiR->getBackedgeValue();
1821 VPValue *CondSelect = BackedgeSelect;
1822
1823 // If there's a header mask, the backedge select will not be the find-last
1824 // select.
1825 if (HeaderMask &&
1826 !match(BackedgeSelect,
1827 m_SelectLike(m_Specific(HeaderMask), m_VPValue(CondSelect),
1828 m_Specific(PhiR))))
1829 return false;
1830
1831 VPValue *Cond = nullptr, *Op1 = nullptr, *Op2 = nullptr;
1832
1833 // If we're matching a blend rather than a select, there should be one
1834 // incoming value which is the data, then all other incoming values should
1835 // be the phi.
1836 auto MatchBlend = [&](VPRecipeBase *R) {
1837 auto *Blend = dyn_cast<VPBlendRecipe>(R);
1838 if (!Blend)
1839 return false;
1840 assert(!Blend->isNormalized() && "must run before blend normalizaion");
1841 unsigned NumIncomingDataValues = 0;
1842 for (unsigned I = 0; I < Blend->getNumIncomingValues(); ++I) {
1843 VPValue *Incoming = Blend->getIncomingValue(I);
1844 if (Incoming != PhiR) {
1845 ++NumIncomingDataValues;
1846 Cond = Blend->getMask(I);
1847 Op1 = Incoming;
1848 Op2 = PhiR;
1849 }
1850 }
1851 return NumIncomingDataValues == 1;
1852 };
1853
1854 VPSingleDefRecipe *SelectR =
1856 if (!match(SelectR,
1857 m_Select(m_VPValue(Cond), m_VPValue(Op1), m_VPValue(Op2))) &&
1858 !MatchBlend(SelectR))
1859 return false;
1860
1861 assert(Cond != HeaderMask && "Cond must not be HeaderMask");
1862
1863 // Find final reduction computation and replace it with an
1864 // extract.last.active intrinsic.
1865 auto *RdxResult =
1867 assert(RdxResult && "Could not find reduction result");
1868
1869 // Add mask phi.
1870 VPBuilder Builder = VPBuilder::getToInsertAfter(PhiR);
1871 auto *MaskPHI = Builder.createWidenPhi(Plan.getFalse());
1872
1873 // Add select for mask.
1874 Builder.setInsertPoint(SelectR);
1875
1876 if (Op1 == PhiR) {
1877 // Normalize to selecting the data operand when the condition is true by
1878 // swapping operands and negating the condition.
1879 std::swap(Op1, Op2);
1880 Cond = Builder.createNot(Cond);
1881 }
1882 assert(Op2 == PhiR && "data value must be selected if Cond is true");
1883
1884 if (HeaderMask)
1885 Cond = Builder.createLogicalAnd(HeaderMask, Cond);
1886
1887 VPValue *AnyOf = Builder.createNaryOp(VPInstruction::AnyOf, {Cond});
1888 VPValue *MaskSelect = Builder.createSelect(AnyOf, Cond, MaskPHI);
1889 MaskPHI->addIncoming(MaskSelect);
1890
1891 // Replace select for data.
1892 VPValue *DataSelect =
1893 Builder.createSelect(AnyOf, Op1, Op2, SelectR->getDebugLoc());
1894 SelectR->replaceAllUsesWith(DataSelect);
1895 PhiR->setBackedgeValue(DataSelect);
1896 SelectR->eraseFromParent();
1897
1898 Builder.setInsertPoint(RdxResult);
1899 auto *ExtractLastActive =
1900 Builder.createNaryOp(VPInstruction::ExtractLastActive,
1901 {PhiR->getStartValue(), DataSelect, MaskSelect},
1902 RdxResult->getDebugLoc());
1903 RdxResult->replaceAllUsesWith(ExtractLastActive);
1904 RdxResult->eraseFromParent();
1905 }
1906
1907 return true;
1908}
1909
1910/// Given a first argmin/argmax pattern with strict predicate consisting of
1911/// 1) a MinOrMax reduction \p MinOrMaxPhiR producing \p MinOrMaxResult,
1912/// 2) a wide induction \p WideIV,
1913/// 3) a FindLastIV reduction \p FindLastIVPhiR using \p WideIV,
1914/// return the smallest index of the FindLastIV reduction result using UMin,
1915/// unless \p MinOrMaxResult equals the start value of its MinOrMax reduction.
1916/// In that case, return the start value of the FindLastIV reduction instead.
1917/// If \p WideIV is not canonical, a new canonical wide IV is added, and the
1918/// final result is scaled back to the non-canonical \p WideIV.
1919/// The final value of the FindLastIV reduction is originally computed using
1920/// \p FindIVSelect, \p FindIVCmp, and \p FindIVRdxResult, which are replaced
1921/// and removed.
1922/// Returns true if the pattern was handled successfully, false otherwise.
1924 VPlan &Plan, VPReductionPHIRecipe *MinOrMaxPhiR,
1925 VPReductionPHIRecipe *FindLastIVPhiR, VPWidenIntOrFpInductionRecipe *WideIV,
1926 VPInstruction *MinOrMaxResult, VPInstruction *FindIVSelect,
1927 VPRecipeBase *FindIVCmp, VPInstruction *FindIVRdxResult) {
1928 assert(!FindLastIVPhiR->isInLoop() && !FindLastIVPhiR->isOrdered() &&
1929 "inloop and ordered reductions not supported");
1930 assert(FindLastIVPhiR->getVFScaleFactor() == 1 &&
1931 "FindIV reduction must not be scaled");
1932
1934 // TODO: Support non (i.e., narrower than) canonical IV types.
1935 // TODO: Emit remarks for failed transformations.
1936 if (Ty != WideIV->getScalarType())
1937 return false;
1938
1939 auto *FindIVSelectR = cast<VPSingleDefRecipe>(
1940 FindLastIVPhiR->getBackedgeValue()->getDefiningRecipe());
1941 assert(
1942 match(FindIVSelectR, m_Select(m_VPValue(), m_VPValue(), m_VPValue())) &&
1943 "backedge value must be a select");
1944 if (FindIVSelectR->getOperand(1) != WideIV &&
1945 FindIVSelectR->getOperand(2) != WideIV)
1946 return false;
1947
1948 // If the original wide IV is not canonical, create a new one. The canonical
1949 // wide IV is guaranteed to not wrap for all lanes that are active in the
1950 // vector loop.
1951 if (!WideIV->isCanonical()) {
1952 VPIRValue *Zero = Plan.getConstantInt(Ty, 0);
1953 VPIRValue *One = Plan.getConstantInt(Ty, 1);
1954 auto *WidenCanIV = new VPWidenIntOrFpInductionRecipe(
1955 nullptr, Zero, One, WideIV->getVFValue(),
1956 WideIV->getInductionDescriptor(),
1957 VPIRFlags::WrapFlagsTy(/*HasNUW=*/true, /*HasNSW=*/false),
1958 WideIV->getDebugLoc());
1959 WidenCanIV->insertBefore(WideIV);
1960
1961 // Update the select to use the wide canonical IV.
1962 FindIVSelectR->setOperand(FindIVSelectR->getOperand(1) == WideIV ? 1 : 2,
1963 WidenCanIV);
1964 }
1965 FindLastIVPhiR->setOperand(0, Plan.getPoison(Ty));
1966
1967 // The reduction using MinOrMaxPhiR needs adjusting to compute the correct
1968 // result:
1969 // 1. Find the first canonical indices corresponding to partial min/max
1970 // values, using loop reductions.
1971 // 2. Find which of the partial min/max values are equal to the overall
1972 // min/max value.
1973 // 3. Select among the canonical indices those corresponding to the overall
1974 // min/max value.
1975 // 4. Find the first canonical index of overall min/max and scale it back to
1976 // the original IV using VPDerivedIVRecipe.
1977 // 5. If the overall min/max equals the starting min/max, the condition in
1978 // the loop was always false, due to being strict; return the start value
1979 // of FindLastIVPhiR in that case.
1980 //
1981 // For example, we transforms two independent reduction result computations
1982 // for
1983 //
1984 // <x1> vector loop: {
1985 // vector.body:
1986 // ...
1987 // ir<%iv> = WIDEN-INDUCTION nuw nsw ir<10>, ir<1>, vp<%0>
1988 // WIDEN-REDUCTION-PHI ir<%min.idx> = phi ir<sentinel.min.start>,
1989 // ir<%min.idx.next>
1990 // WIDEN-REDUCTION-PHI ir<%min.val> = phi ir<100>, ir<%min.val.next>
1991 // ....
1992 // WIDEN-INTRINSIC ir<%min.val.next> = call llvm.umin(ir<%min.val>, ir<%l>)
1993 // WIDEN ir<%min.idx.next> = select ir<%cmp>, ir<%iv>, ir<%min.idx>
1994 // ...
1995 // }
1996 // Successor(s): middle.block
1997 //
1998 // middle.block:
1999 // vp<%iv.rdx> = compute-reduction-result (smax) vp<%min.idx.next>
2000 // vp<%min.result> = compute-reduction-result (umin) ir<%min.val.next>
2001 // vp<%cmp> = icmp ne vp<%iv.rdx>, ir<sentinel.min.start>
2002 // vp<%find.iv.result> = select vp<%cmp>, vp<%iv.rdx>, ir<10>
2003 //
2004 //
2005 // Into:
2006 //
2007 // vp<%reduced.min> = compute-reduction-result (umin) ir<%min.val.next>
2008 // vp<%reduced.mins.mask> = icmp eq ir<%min.val.next>, vp<%reduced.min>
2009 // vp<%idxs2reduce> = select vp<%reduced.mins.mask>, ir<%min.idx.next>,
2010 // ir<MaxUInt>
2011 // vp<%reduced.idx> = compute-reduction-result (umin) vp<%idxs2reduce>
2012 // vp<%scaled.idx> = DERIVED-IV ir<20> + vp<%reduced.idx> * ir<1>
2013 // vp<%always.false> = icmp eq vp<%reduced.min>, ir<100>
2014 // vp<%final.idx> = select vp<%always.false>, ir<10>,
2015 // vp<%scaled.idx>
2016
2017 VPBuilder Builder(FindIVRdxResult);
2018 VPValue *MinOrMaxExiting = MinOrMaxResult->getOperand(0);
2019 auto *FinalMinOrMaxCmp =
2020 Builder.createICmp(CmpInst::ICMP_EQ, MinOrMaxExiting, MinOrMaxResult);
2021 VPValue *LastIVExiting = FindIVRdxResult->getOperand(0);
2022 VPValue *MaxIV =
2023 Plan.getConstantInt(APInt::getMaxValue(Ty->getIntegerBitWidth()));
2024 auto *FinalIVSelect =
2025 Builder.createSelect(FinalMinOrMaxCmp, LastIVExiting, MaxIV);
2026 VPIRFlags RdxFlags(RecurKind::UMin, false, false, FastMathFlags());
2027 VPSingleDefRecipe *FinalCanIV = Builder.createNaryOp(
2028 VPInstruction::ComputeReductionResult, {FinalIVSelect}, RdxFlags,
2029 FindIVRdxResult->getDebugLoc());
2030
2031 // If we used a new wide canonical IV convert the reduction result back to the
2032 // original IV scale before the final select.
2033 if (!WideIV->isCanonical()) {
2034 auto *DerivedIVRecipe = new VPDerivedIVRecipe(
2036 nullptr, // No FPBinOp for integer induction
2037 WideIV->getStartValue(), FinalCanIV, WideIV->getStepValue());
2038 DerivedIVRecipe->insertBefore(Builder.getRecipeAtInsertPoint());
2039 FinalCanIV = DerivedIVRecipe;
2040 }
2041
2042 // If the final min/max value matches its start value, the condition in the
2043 // loop was always false, i.e. no induction value has been selected. If that's
2044 // the case, set the result of the IV reduction to its start value.
2045 VPValue *AlwaysFalse = Builder.createICmp(CmpInst::ICMP_EQ, MinOrMaxResult,
2046 MinOrMaxPhiR->getStartValue());
2047 VPValue *FinalIV = Builder.createSelect(
2048 AlwaysFalse, FindIVSelect->getOperand(2), FinalCanIV);
2049 FindIVSelect->replaceAllUsesWith(FinalIV);
2050
2051 // Erase the old FindIV result pattern which is now dead.
2052 FindIVSelect->eraseFromParent();
2053 FindIVCmp->eraseFromParent();
2054 FindIVRdxResult->eraseFromParent();
2055 return true;
2056}
2057
2060 Loop *TheLoop) {
2061 for (auto &PhiR : make_early_inc_range(
2063 auto *MinOrMaxPhiR = dyn_cast<VPReductionPHIRecipe>(&PhiR);
2064 // TODO: check for multi-uses in VPlan directly.
2065 if (!MinOrMaxPhiR || !MinOrMaxPhiR->hasUsesOutsideReductionChain())
2066 continue;
2067
2068 // MinOrMaxPhiR has users outside the reduction cycle in the loop. Check if
2069 // the only other user is a FindLastIV reduction. MinOrMaxPhiR must have
2070 // exactly 2 users:
2071 // 1) the min/max operation of the reduction cycle, and
2072 // 2) the compare of a FindLastIV reduction cycle. This compare must match
2073 // the min/max operation - comparing MinOrMaxPhiR with the operand of the
2074 // min/max operation, and be used only by the select of the FindLastIV
2075 // reduction cycle.
2076 RecurKind RdxKind = MinOrMaxPhiR->getRecurrenceKind();
2077 assert(
2079 "only min/max recurrences support users outside the reduction chain");
2080
2081 auto *MinOrMaxOp =
2082 dyn_cast<VPRecipeWithIRFlags>(MinOrMaxPhiR->getBackedgeValue());
2083 if (!MinOrMaxOp)
2084 return false;
2085
2086 // Check that MinOrMaxOp is a VPWidenIntrinsicRecipe or VPReplicateRecipe
2087 // with an intrinsic that matches the reduction kind.
2088 Intrinsic::ID ExpectedIntrinsicID = getMinMaxReductionIntrinsicOp(RdxKind);
2089 if (!match(MinOrMaxOp, m_Intrinsic(ExpectedIntrinsicID)))
2090 return false;
2091
2092 // MinOrMaxOp must have 2 users: 1) MinOrMaxPhiR and 2)
2093 // ComputeReductionResult.
2094 assert(MinOrMaxOp->getNumUsers() == 2 &&
2095 "MinOrMaxOp must have exactly 2 users");
2096 VPValue *MinOrMaxOpValue = MinOrMaxOp->getOperand(0);
2097 if (MinOrMaxOpValue == MinOrMaxPhiR)
2098 MinOrMaxOpValue = MinOrMaxOp->getOperand(1);
2099
2100 VPValue *CmpOpA;
2101 VPValue *CmpOpB;
2102 CmpPredicate Pred;
2104 MinOrMaxPhiR, m_Cmp(Pred, m_VPValue(CmpOpA), m_VPValue(CmpOpB))));
2105 if (!Cmp || Cmp->getNumUsers() != 1 ||
2106 (CmpOpA != MinOrMaxOpValue && CmpOpB != MinOrMaxOpValue))
2107 return false;
2108
2109 if (MinOrMaxOpValue != CmpOpB)
2110 Pred = CmpInst::getSwappedPredicate(Pred);
2111
2112 // MinOrMaxPhiR must have exactly 2 users:
2113 // * MinOrMaxOp,
2114 // * Cmp (that's part of a FindLastIV chain).
2115 if (MinOrMaxPhiR->getNumUsers() != 2)
2116 return false;
2117
2118 VPInstruction *MinOrMaxResult =
2120 assert(is_contained(MinOrMaxPhiR->users(), MinOrMaxOp) &&
2121 "one user must be MinOrMaxOp");
2122 assert(MinOrMaxResult && "MinOrMaxResult must be a user of MinOrMaxOp");
2123
2124 // Cmp must be used by the select of a FindLastIV chain.
2125 VPValue *Sel = dyn_cast<VPSingleDefRecipe>(Cmp->getSingleUser());
2126 VPValue *IVOp, *FindIV;
2127 if (!Sel || Sel->getNumUsers() != 2 ||
2128 !match(Sel,
2130 return false;
2131
2133 std::swap(FindIV, IVOp);
2134 Pred = CmpInst::getInversePredicate(Pred);
2135 }
2136
2137 auto *FindIVPhiR = dyn_cast<VPReductionPHIRecipe>(FindIV);
2139 FindIVPhiR->getRecurrenceKind()))
2140 return false;
2141
2142 assert(!FindIVPhiR->isInLoop() && !FindIVPhiR->isOrdered() &&
2143 "cannot handle inloop/ordered reductions yet");
2144
2145 // Check if FindIVPhiR is a FindLast pattern by checking the MinMaxKind
2146 // on its ComputeReductionResult. SMax/UMax indicates FindLast.
2147 VPInstruction *FindIVResult =
2149 FindIVPhiR->getBackedgeValue());
2150 assert(FindIVResult &&
2151 "must be able to retrieve the FindIVResult VPInstruction");
2152 RecurKind FindIVMinMaxKind = FindIVResult->getRecurKind();
2153 if (FindIVMinMaxKind != RecurKind::SMax &&
2154 FindIVMinMaxKind != RecurKind::UMax)
2155 return false;
2156
2157 // TODO: Support cases where IVOp is the IV increment.
2158 if (!match(IVOp, m_TruncOrSelf(m_VPValue(IVOp))) ||
2160 return false;
2161
2162 // Check if the predicate is compatible with the reduction kind.
2163 bool IsValidKindPred = [RdxKind, Pred]() {
2164 switch (RdxKind) {
2165 case RecurKind::UMin:
2166 return Pred == CmpInst::ICMP_UGE || Pred == CmpInst::ICMP_UGT;
2167 case RecurKind::UMax:
2168 return Pred == CmpInst::ICMP_ULE || Pred == CmpInst::ICMP_ULT;
2169 case RecurKind::SMax:
2170 return Pred == CmpInst::ICMP_SLE || Pred == CmpInst::ICMP_SLT;
2171 case RecurKind::SMin:
2172 return Pred == CmpInst::ICMP_SGE || Pred == CmpInst::ICMP_SGT;
2173 default:
2174 llvm_unreachable("unhandled recurrence kind");
2175 }
2176 }();
2177 if (!IsValidKindPred) {
2178 ORE->emit([&]() {
2180 DEBUG_TYPE, "VectorizationMultiUseReductionPredicate",
2181 TheLoop->getStartLoc(), TheLoop->getHeader())
2182 << "Multi-use reduction with predicate "
2184 << " incompatible with reduction kind";
2185 });
2186 return false;
2187 }
2188
2189 auto *FindIVSelect = findFindIVSelect(FindIVPhiR->getBackedgeValue());
2190 auto *FindIVCmp = FindIVSelect->getOperand(0)->getDefiningRecipe();
2191 auto *FindIVRdxResult = cast<VPInstruction>(FindIVCmp->getOperand(0));
2192 assert(FindIVSelect->getParent() == MinOrMaxResult->getParent() &&
2193 "both results must be computed in the same block");
2194 // Reducing to a scalar min or max value is placed right before reducing to
2195 // its scalar iteration, in order to generate instructions that use both
2196 // their operands.
2197 MinOrMaxResult->moveBefore(*FindIVRdxResult->getParent(),
2198 FindIVRdxResult->getIterator());
2199
2200 bool IsStrictPredicate = ICmpInst::isLT(Pred) || ICmpInst::isGT(Pred);
2201 if (IsStrictPredicate) {
2202 if (!handleFirstArgMinOrMax(Plan, MinOrMaxPhiR, FindIVPhiR,
2204 MinOrMaxResult, FindIVSelect, FindIVCmp,
2205 FindIVRdxResult))
2206 return false;
2207 continue;
2208 }
2209
2210 // The reduction using MinOrMaxPhiR needs adjusting to compute the correct
2211 // result:
2212 // 1. We need to find the last IV for which the condition based on the
2213 // min/max recurrence is true,
2214 // 2. Compare the partial min/max reduction result to its final value and,
2215 // 3. Select the lanes of the partial FindLastIV reductions which
2216 // correspond to the lanes matching the min/max reduction result.
2217 //
2218 // For example, this transforms
2219 // vp<%min.result> = compute-reduction-result ir<%min.val.next>
2220 // vp<%iv.rdx> = compute-reduction-result (smax) vp<%min.idx.next>
2221 // vp<%cmp> = icmp ne vp<%iv.rdx>, SENTINEL
2222 // vp<%find.iv.result> = select vp<%cmp>, vp<%iv.rdx>, ir<0>
2223 //
2224 // into:
2225 //
2226 // vp<min.result> = compute-reduction-result ir<%min.val.next>
2227 // vp<%final.min.cmp> = icmp eq ir<%min.val.next>, vp<min.result>
2228 // vp<%final.iv> = select vp<%final.min.cmp>, vp<%min.idx.next>, SENTINEL
2229 // vp<%iv.rdx> = compute-reduction-result (smax) vp<%final.iv>
2230 // vp<%cmp> = icmp ne vp<%iv.rdx>, SENTINEL
2231 // vp<%find.iv.result> = select vp<%cmp>, vp<%iv.rdx>, ir<0>
2232 //
2233 VPBuilder B(FindIVRdxResult);
2234 VPValue *MinOrMaxExiting = MinOrMaxResult->getOperand(0);
2235 auto *FinalMinOrMaxCmp =
2236 B.createICmp(CmpInst::ICMP_EQ, MinOrMaxExiting, MinOrMaxResult);
2237 VPValue *Sentinel = FindIVCmp->getOperand(1);
2238 VPValue *LastIVExiting = FindIVRdxResult->getOperand(0);
2239 auto *FinalIVSelect =
2240 B.createSelect(FinalMinOrMaxCmp, LastIVExiting, Sentinel);
2241 FindIVRdxResult->setOperand(0, FinalIVSelect);
2242 }
2243 return true;
2244}
2245
2247 VPRegionBlock *LoopRegion = Plan.getVectorLoopRegion();
2248 VPValue *HeaderMask = LoopRegion->getHeaderMask();
2249 Type *I1Ty = IntegerType::getInt1Ty(Plan.getContext());
2250
2251 VPBuilder Builder(Plan.getVectorPreheader());
2252 auto *AliasMask = Builder.createNaryOp(
2253 VPInstruction::IncomingAliasMask, {}, nullptr, {}, {},
2254 DebugLoc::getUnknown(), "incoming.alias.mask", I1Ty);
2255
2256 VPBasicBlock *Header = LoopRegion->getEntryBasicBlock();
2257 Builder = VPBuilder(Header, Header->getFirstNonPhi());
2258
2259 // Update all existing users of the header mask to "HeaderMask & AliasMask".
2260 auto *ClampedHeaderMask = Builder.createAnd(HeaderMask, AliasMask);
2261 HeaderMask->replaceUsesWithIf(ClampedHeaderMask, [&](VPUser &U, unsigned) {
2262 return &U != ClampedHeaderMask;
2263 });
2264}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEBUG_TYPE
#define _
iv Induction Variable Users
Definition IVUsers.cpp:48
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
This file provides a LoopVectorizationPlanner class.
static constexpr uint32_t MinItersBypassWeights[]
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
const SmallVectorImpl< MachineOperand > & Cond
This file defines less commonly used SmallVector utilities.
#define LLVM_DEBUG(...)
Definition Debug.h:119
This pass exposes codegen information to IR-level passes.
static bool isHeaderBB(BasicBlock *BB, Loop *L)
static bool handleFirstArgMinOrMax(VPlan &Plan, VPReductionPHIRecipe *MinOrMaxPhiR, VPReductionPHIRecipe *FindLastIVPhiR, VPWidenIntOrFpInductionRecipe *WideIV, VPInstruction *MinOrMaxResult, VPInstruction *FindIVSelect, VPRecipeBase *FindIVCmp, VPInstruction *FindIVRdxResult)
Given a first argmin/argmax pattern with strict predicate consisting of 1) a MinOrMax reduction MinOr...
static VPHeaderPHIRecipe * createWidenInductionRecipe(PHINode *Phi, VPPhi *PhiR, VPIRValue *Start, const InductionDescriptor &IndDesc, VPlan &Plan, PredicatedScalarEvolution &PSE, Loop &OrigLoop, DebugLoc DL)
Creates a VPWidenIntOrFpInductionRecipe or VPWidenPointerInductionRecipe for Phi based on IndDesc.
static void insertCheckBlockBeforeVectorLoop(VPlan &Plan, VPBasicBlock *CheckBlockVPBB)
Insert CheckBlockVPBB on the edge leading to the vector preheader, connecting it to both vector and s...
static void simplifyLiveInsWithSCEV(VPlan &Plan, PredicatedScalarEvolution &PSE)
Check Plan's live-in and replace them with constants, if they can be simplified via SCEV.
static void addBypassBranch(VPlan &Plan, VPBasicBlock *CheckBlockVPBB, VPValue *Cond, bool AddBranchWeights)
Create a BranchOnCond terminator in CheckBlockVPBB.
static bool sinkRecurrenceUsersAfterPrevious(VPFirstOrderRecurrencePHIRecipe *FOR, VPRecipeBase *Previous, const VPDominatorTree &VPDT)
Try to sink users of FOR after Previous.
static bool canonicalHeaderAndLatch(VPBlockBase *HeaderVPB, const VPDominatorTree &VPDT)
Checks if HeaderVPB is a loop header block in the plain CFG; that is, it has exactly 2 predecessors (...
static void addInitialSkeleton(VPlan &Plan, Type *InductionTy, PredicatedScalarEvolution &PSE, Loop *TheLoop)
static bool hoistPreviousBeforeFORUsers(VPFirstOrderRecurrencePHIRecipe *FOR, VPRecipeBase *Previous, const VPDominatorTree &VPDT)
Try to hoist Previous and its operands before all users of FOR.
static bool areAllLoadsDereferenceable(VPBasicBlock *HeaderVPBB, Loop *TheLoop, PredicatedScalarEvolution &PSE, DominatorTree &DT, AssumptionCache *AC)
Check if all loads in the loop are dereferenceable.
static void createLoopRegion(VPlan &Plan, VPBlockBase *HeaderVPB, DebugLoc DL)
Create a new VPRegionBlock for the loop starting at HeaderVPB.
static VPInstruction * findFindIVSelect(VPValue *BackedgeVal)
Find and return the final select instruction of the FindIV result pattern for the given BackedgeVal: ...
static bool tryToSinkOrHoistRecurrenceUsers(VPBasicBlock *HeaderVPBB, const VPDominatorTree &VPDT)
Sink users of fixed-order recurrences past or hoist before the recipe defining the previous value,...
static constexpr uint32_t CheckBypassWeights[]
static void printAfterInitialConstruction(VPlan &)
To make RUN_VPLAN_PASS print initial VPlan.
static void createExtractsForLiveOuts(VPlan &Plan, VPBasicBlock *MiddleVPBB)
Creates extracts for values in Plan defined in a loop region and used outside a loop region.
This file implements dominator tree analysis for a single level of a VPlan's H-CFG.
This file contains the declarations of different VPlan-related auxiliary helpers.
This file provides utility VPlan to VPlan transformations.
#define RUN_VPLAN_PASS_NO_VERIFY(PASS,...)
This file contains the declarations of the Vectorization Plan base classes:
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
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
A cache of @llvm.assume calls within a function.
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.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:740
@ ICMP_SLT
signed less than
Definition InstrTypes.h:769
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:770
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:764
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:763
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:767
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:765
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:768
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:766
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition InstrTypes.h:750
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:890
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:852
static LLVM_ABI StringRef getPredicateName(Predicate P)
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
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
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:250
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
static constexpr ElementCount getFixed(ScalarTy MinVal)
Definition TypeSize.h:309
constexpr bool isScalar() const
Exactly one element.
Definition TypeSize.h:320
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:23
static FastMathFlags getFast()
Definition FMF.h:50
static bool isLT(Predicate P)
Return true if the predicate is SLT or ULT.
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
A struct for saving information about induction variables.
InductionKind getKind() const
const SCEV * getStep() const
@ IK_FpInduction
Floating point induction variable.
@ IK_PtrInduction
Pointer induction var. Step = C.
@ IK_IntInduction
Integer induction variable. Step = C.
Value * getStartValue() const
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
LLVM_ABI FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within this loop.
BlockT * getHeader() const
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
This class emits a version of the loop where run-time checks ensure that may-alias pointers can't ove...
LLVM_ABI std::pair< MDNode *, MDNode * > getNoAliasMetadataFor(const Instruction *OrigInst) const
Returns a pair containing the alias_scope and noalias metadata nodes for OrigInst,...
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
DebugLoc getStartLoc() const
Return the debug location of the start of this loop.
Definition LoopInfo.cpp:669
LLVM_ABI MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
Definition MDBuilder.cpp:38
Metadata node.
Definition Metadata.h:1069
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
iterator find(const KeyT &Key)
Definition MapVector.h:156
iterator end()
Definition MapVector.h:69
The optimization diagnostic interface.
LLVM_ABI void emit(DiagnosticInfoOptimizationBase &OptDiag)
Output the remark via the diagnostic handler and to the optimization record file.
Diagnostic information for missed-optimization remarks.
Post-order traversal of a graph.
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
LLVM_ABI void addPredicate(const SCEVPredicate &Pred)
Adds a new predicate.
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
LLVM_ABI const SCEVPredicate & getPredicate() const
LLVM_ABI const SCEV * getSymbolicMaxBackedgeTakenCount()
Get the (predicated) symbolic max backedge count for the analyzed loop.
LLVM_ABI const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
static bool isFMulAddIntrinsic(Instruction *I)
Returns true if the instruction is a call to the llvm.fmuladd intrinsic.
FastMathFlags getFastMathFlags() const
static bool isFPMinMaxNumRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a floating-point minnum/maxnum kind.
bool hasUsesOutsideReductionChain() const
Returns true if the reduction PHI has any uses outside the reduction chain.
static bool isFindLastRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
TrackingVH< Value > getRecurrenceStartValue() const
static bool isAnyOfRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
RecurKind getRecurrenceKind() const
bool isOrdered() const
Expose an ordered FP reduction to the instance users.
static LLVM_ABI bool isFloatingPointRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is a floating point kind.
static bool isFindIVRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is of the form select(cmp(),x,y) where one of (x,...
static bool isIntMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is an integer min/max kind.
static bool isMinMaxRecurrenceKind(RecurKind Kind)
Returns true if the recurrence kind is any min/max kind.
virtual unsigned getComplexity() const
Returns the estimated complexity of this predicate.
This class represents an analyzed expression in the program.
static constexpr auto FlagNUW
Type * getType() const
Return the LLVM type of this SCEV expression.
The main scalar evolution driver.
LLVM_ABI const SCEV * getTripCountFromExitCount(const SCEV *ExitCount)
A version of getTripCountFromExitCount below which always picks an evaluation type which can not resu...
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 bool isSCEVable(Type *Ty) const
Test if values of the given type are analyzable within the SCEV framework.
LLVM_ABI const SCEV * getElementCount(Type *Ty, ElementCount EC, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap)
LLVM_ABI const SCEV * getUMaxExpr(SCEVUse LHS, SCEVUse RHS)
LLVM_ABI const SCEV * getStoreSizeOfExpr(Type *IntTy, Type *StoreTy)
Return an expression for the store size of StoreTy that is type IntTy.
LLVM_ABI bool isKnownPredicate(CmpPredicate Pred, SCEVUse LHS, SCEVUse RHS)
Test if the given expression is known to satisfy the condition described by Pred, LHS,...
LLVM_ABI const SCEV * applyLoopGuards(const SCEV *Expr, const Loop *L)
Try to apply information from loop guards for L to Expr.
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
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
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.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
static LLVM_ABI IntegerType * getInt1Ty(LLVMContext &C)
Definition Type.cpp:306
op_range operands()
Definition User.h:267
VPBasicBlock serves as the leaf of the Hierarchical Control-Flow Graph.
Definition VPlan.h:4380
void appendRecipe(VPRecipeBase *Recipe)
Augment the existing recipes of a VPBasicBlock with an additional Recipe as the last recipe.
Definition VPlan.h:4455
RecipeListTy::iterator iterator
Instruction iterators...
Definition VPlan.h:4407
iterator end()
Definition VPlan.h:4417
iterator begin()
Recipe iterator methods.
Definition VPlan.h:4415
iterator_range< iterator > phis()
Returns an iterator range over the PHI-like recipes in the block.
Definition VPlan.h:4468
iterator getFirstNonPhi()
Return the position of the first non-phi node recipe in the block.
Definition VPlan.cpp:266
VPBasicBlock * splitAt(iterator SplitAt)
Split current block at SplitAt by inserting a new block between the current block and its successors ...
Definition VPlan.cpp:584
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:4446
VPBlockBase is the building block of the Hierarchical Control-Flow Graph.
Definition VPlan.h:94
void setSuccessors(ArrayRef< VPBlockBase * > NewSuccs)
Set each VPBasicBlock in NewSuccss as successor of this VPBlockBase.
Definition VPlan.h:315
VPRegionBlock * getParent()
Definition VPlan.h:192
const VPBasicBlock * getExitingBasicBlock() const
Definition VPlan.cpp:236
void setName(const Twine &newName)
Definition VPlan.h:185
size_t getNumSuccessors() const
Definition VPlan.h:243
void swapSuccessors()
Swap successors of the block. The block must have exactly 2 successors.
Definition VPlan.h:337
void setPredecessors(ArrayRef< VPBlockBase * > NewPreds)
Set each VPBasicBlock in NewPreds as predecessor of this VPBlockBase.
Definition VPlan.h:306
const VPBlocksTy & getPredecessors() const
Definition VPlan.h:228
void setTwoSuccessors(VPBlockBase *IfTrue, VPBlockBase *IfFalse)
Set two given VPBlockBases IfTrue and IfFalse to be the two successors of this VPBlockBase.
Definition VPlan.h:297
VPBlockBase * getSinglePredecessor() const
Definition VPlan.h:239
void swapPredecessors()
Swap predecessors of the block.
Definition VPlan.h:329
const VPBasicBlock * getEntryBasicBlock() const
Definition VPlan.cpp:216
void setOneSuccessor(VPBlockBase *Successor)
Set a given VPBlockBase Successor as the single successor of this VPBlockBase.
Definition VPlan.h:286
void setParent(VPRegionBlock *P)
Definition VPlan.h:203
VPBlockBase * getSingleSuccessor() const
Definition VPlan.h:233
const VPBlocksTy & getSuccessors() const
Definition VPlan.h:217
static void insertBlockAfter(VPBlockBase *NewBlock, VPBlockBase *BlockPtr)
Insert disconnected VPBlockBase NewBlock after BlockPtr.
Definition VPlanUtils.h:282
static void insertOnEdge(VPBlockBase *From, VPBlockBase *To, VPBlockBase *BlockPtr)
Inserts BlockPtr on the edge between From and To.
Definition VPlanUtils.h:421
static VPBasicBlock * getPlainCFGMiddleBlock(const VPlan &Plan)
Returns the middle block of Plan in plain CFG form (before regions are formed).
static void connectBlocks(VPBlockBase *From, VPBlockBase *To, unsigned PredIdx=-1u, unsigned SuccIdx=-1u)
Connect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:330
static void disconnectBlocks(VPBlockBase *From, VPBlockBase *To)
Disconnect VPBlockBases From and To bi-directionally.
Definition VPlanUtils.h:348
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 void transferSuccessors(VPBlockBase *Old, VPBlockBase *New)
Transfer successors from Old to New. New must have no successors.
Definition VPlanUtils.h:368
VPlan-based builder utility analogous to IRBuilder.
VPInstruction * createOr(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPInstruction * createNot(VPValue *Operand, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
void setInsertPoint(const VPInsertPoint &IP)
Set the current insert point.
VPInstruction * createScalarCast(Instruction::CastOps Opcode, VPValue *Op, Type *ResultTy, DebugLoc DL, const VPIRMetadata &Metadata={})
VPInstruction * createFCmp(CmpInst::Predicate Pred, VPValue *A, VPValue *B, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
Create a new FCmp VPInstruction with predicate Pred and operands A and B.
VPRecipeBase * getRecipeAtInsertPoint() const
Get the recipe at the current insert point or nullptr if the insert point is the end of the block.
VPInstructionWithType * createScalarLoad(Type *ResultTy, VPValue *Addr, DebugLoc DL, const VPIRMetadata &Metadata={})
static VPBuilder getToInsertAfter(VPRecipeBase *R)
Create a VPBuilder to insert after R.
VPInstruction * createICmp(CmpInst::Predicate Pred, VPValue *A, VPValue *B, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
Create a new ICmp VPInstruction with predicate Pred and operands A and B.
VPInstruction * createAnd(VPValue *LHS, VPValue *RHS, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="")
VPPhi * createScalarPhi(ArrayRef< VPValue * > IncomingValues, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", const VPIRFlags &Flags={}, Type *ResultTy=nullptr)
VPInstruction * createSelect(VPValue *Cond, VPValue *TrueVal, VPValue *FalseVal, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", const VPIRFlags &Flags={})
VPExpandSCEVRecipe * createExpandSCEV(const SCEV *Expr)
VPInstruction * createNaryOp(unsigned Opcode, ArrayRef< VPValue * > Operands, Instruction *Inst=nullptr, const VPIRFlags &Flags={}, const VPIRMetadata &MD={}, DebugLoc DL=DebugLoc::getUnknown(), const Twine &Name="", Type *ResultTy=nullptr)
Create an N-ary operation with Opcode, Operands and set Inst as its underlying Instruction.
unsigned getNumDefinedValues() const
Returns the number of values defined by the VPDef.
Definition VPlanValue.h:578
VPValue * getVPSingleValue()
Returns the only VPValue defined by the VPDef.
Definition VPlanValue.h:551
A recipe for converting Current into Start + Current * Step.
Definition VPlan.h:4174
Template specialization of the standard LLVM dominator tree utility for VPBlockBases.
bool properlyDominates(const VPRecipeBase *A, const VPRecipeBase *B) const
A pure virtual base class for all recipes modeling header phis, including phis for first order recurr...
Definition VPlan.h:2437
virtual VPValue * getBackedgeValue()
Returns the incoming value from the loop backedge.
Definition VPlan.h:2484
VPValue * getStartValue()
Returns the start value of the phi, if one is set.
Definition VPlan.h:2473
Class to record and manage LLVM IR flags.
Definition VPlan.h:704
RecurKind getRecurKind() const
Definition VPlan.h:1065
This is a concrete Recipe that models a single VPlan-level instruction.
Definition VPlan.h:1234
@ ExtractLastActive
Extracts the last active lane from a set of vectors.
Definition VPlan.h:1336
@ ExtractLane
Extracts a single lane (first operand) from a set of vector operands.
Definition VPlan.h:1327
@ ExitingIVValue
Compute the exiting value of a wide induction after vectorization, that is the value of the last lane...
Definition VPlan.h:1340
@ ComputeReductionResult
Reduce the operands to the final reduction result using the operation specified via the operation's V...
Definition VPlan.h:1280
void addIncoming(VPValue *IncomingV)
Append IncomingV as an incoming value to the phi-like recipe.
Definition VPlan.h:1667
VPRecipeBase is a base class modeling a sequence of one or more output IR instructions.
Definition VPlan.h:411
VPBasicBlock * getParent()
Definition VPlan.h:483
DebugLoc getDebugLoc() const
Returns the debug location of the recipe.
Definition VPlan.h:561
void moveBefore(VPBasicBlock &BB, iplist< VPRecipeBase >::iterator I)
Unlink this recipe and insert into BB before I.
void insertBefore(VPRecipeBase *InsertPos)
Insert an unlinked recipe into a basic block immediately before the specified recipe.
iplist< VPRecipeBase >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
void moveAfter(VPRecipeBase *MovePos)
Unlink this recipe from its current VPBasicBlock and insert it into the VPBasicBlock that MovePos liv...
Type * getScalarType() const
Returns the scalar type of this VPRecipeValue.
Definition VPlanValue.h:354
A recipe for handling reduction phis.
Definition VPlan.h:2856
bool isOrdered() const
Returns true, if the phi is part of an ordered reduction.
Definition VPlan.h:2916
unsigned getVFScaleFactor() const
Get the factor that the VF of this recipe's output should be scaled by, or 1 if it isn't scaled.
Definition VPlan.h:2900
bool isInLoop() const
Returns true if the phi is part of an in-loop reduction.
Definition VPlan.h:2919
A recipe to represent inloop, ordered or partial reduction operations.
Definition VPlan.h:3224
VPRegionBlock represents a collection of VPBasicBlocks and VPRegionBlocks which form a Single-Entry-S...
Definition VPlan.h:4605
VPRegionValue * createHeaderMask()
Create the header mask for the region and return it.
Definition VPlan.h:4752
Type * getCanonicalIVType() const
Return the type of the canonical IV for loop regions.
Definition VPlan.h:4733
VPRegionValue * getCanonicalIV()
Return the canonical induction variable of the region, null for replicating regions.
Definition VPlan.h:4725
VPRegionValue * getHeaderMask() const
Return the header mask of the region, or null if not set.
Definition VPlan.h:4738
DebugLoc getDebugLoc() const
Returns the debug location of the VPRegionValue.
Definition VPlanValue.h:267
Lightweight SCEV-to-VPlan expander.
Definition VPlanUtils.h:250
VPValue * tryToExpand(const SCEV *S)
Try to expand S into recipes and live-ins using the builder.
VPSingleDefRecipe is a base class for recipes that model a sequence of one or more output IR that def...
Definition VPlan.h:619
Type * getType() const
Returns the scalar type of this symbolic value.
Definition VPlanValue.h:232
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
void setOperand(unsigned I, VPValue *New)
Definition VPlanValue.h:447
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
Value * getLiveInIRValue() const
Return the underlying IR value for a VPIRValue.
Definition VPlan.cpp:143
VPRecipeBase * getDefiningRecipe()
Returns the recipe defining this VPValue or nullptr if it is not defined by a recipe,...
Definition VPlan.cpp:130
void setUnderlyingValue(Value *Val)
Definition VPlanValue.h:209
void replaceAllUsesWith(VPValue *New)
Definition VPlan.cpp:1488
unsigned getNumUsers() const
Definition VPlanValue.h:115
void replaceUsesWithIf(VPValue *New, llvm::function_ref< bool(VPUser &U, unsigned Idx)> ShouldReplace)
Go through the uses list for this VPValue and make each use point to New if the callback ShouldReplac...
Definition VPlan.cpp:1494
user_range users()
Definition VPlanValue.h:157
Base class for widened induction (VPWidenIntOrFpInductionRecipe and VPWidenPointerInductionRecipe),...
Definition VPlan.h:2511
VPIRValue * getStartValue() const
Returns the start value of the induction.
Definition VPlan.h:2559
VPValue * getStepValue()
Returns the step value of the induction.
Definition VPlan.h:2562
const InductionDescriptor & getInductionDescriptor() const
Returns the induction descriptor for the recipe.
Definition VPlan.h:2582
A recipe for handling phi nodes of integer and floating-point inductions, producing their vector valu...
Definition VPlan.h:2611
bool isCanonical() const
Returns true if the induction is canonical, i.e.
VPlan models a candidate for vectorization, encoding various decisions take to produce efficient outp...
Definition VPlan.h:4792
VPIRValue * getLiveIn(Value *V) const
Return the live-in VPIRValue for V, if there is one or nullptr otherwise.
Definition VPlan.h:5124
LLVMContext & getContext() const
Definition VPlan.h:4995
VPBasicBlock * getEntry()
Definition VPlan.h:4888
VPValue * getTripCount() const
The trip count of the original loop.
Definition VPlan.h:4953
VPIRValue * getFalse()
Return a VPIRValue wrapping i1 false.
Definition VPlan.h:5090
VPSymbolicValue & getVFxUF()
Returns VF * UF of the vector loop region.
Definition VPlan.h:4993
auto getLiveIns() const
Return the list of live-in VPValues available in the VPlan.
Definition VPlan.h:5127
VPIRValue * getPoison(Type *Ty)
Return a VPIRValue wrapping a poison value of type Ty.
Definition VPlan.h:5118
ArrayRef< VPIRBasicBlock * > getExitBlocks() const
Return an ArrayRef containing VPIRBasicBlocks wrapping the exit blocks of the original scalar loop.
Definition VPlan.h:4947
VPSymbolicValue & getVectorTripCount()
The vector trip count.
Definition VPlan.h:4983
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:5067
VPRegionBlock * createLoopRegion(Type *CanIVTy, DebugLoc DL, const std::string &Name="", VPBlockBase *Entry=nullptr, VPBlockBase *Exiting=nullptr)
Create a new loop region with a canonical IV using CanIVTy and DL.
Definition VPlan.h:5162
LLVM_ABI_FOR_TEST VPRegionBlock * getVectorLoopRegion()
Returns the VPRegionBlock of the vector loop.
Definition VPlan.cpp:1077
void setTripCount(VPValue *NewTripCount)
Set the trip count assuming it is currently null; if it is not - use resetTripCount().
Definition VPlan.h:4960
VPBasicBlock * getMiddleBlock()
Returns the 'middle' block of the plan, that is the block that selects whether to execute the scalar ...
Definition VPlan.h:4923
VPBasicBlock * createVPBasicBlock(const Twine &Name, VPRecipeBase *Recipe=nullptr)
Create a new VPBasicBlock with Name and containing Recipe if present.
Definition VPlan.h:5150
LLVM_ABI_FOR_TEST VPIRBasicBlock * createVPIRBasicBlock(BasicBlock *IRBB)
Create a VPIRBasicBlock from IRBB containing VPIRInstructions for all instructions in IRBB,...
Definition VPlan.cpp:1332
VPIRValue * getTrue()
Return a VPIRValue wrapping i1 true.
Definition VPlan.h:5087
VPBasicBlock * getVectorPreheader() const
Returns the preheader of the vector loop region, if one exists, or null otherwise.
Definition VPlan.h:4893
bool hasScalarVFOnly() const
Definition VPlan.h:5035
VPBasicBlock * getScalarPreheader() const
Return the VPBasicBlock for the preheader of the scalar loop.
Definition VPlan.h:4937
VPIRBasicBlock * getScalarHeader() const
Return the VPIRBasicBlock wrapping the header of the scalar loop.
Definition VPlan.h:4943
VPSymbolicValue & getVF()
Returns the VF of the vector loop region.
Definition VPlan.h:4986
bool hasScalarTail() const
Returns true if the scalar tail may execute after the vector loop, i.e.
Definition VPlan.h:5220
VPIRValue * getConstantInt(Type *Ty, uint64_t Val, bool IsSigned=false)
Return a VPIRValue wrapping a ConstantInt with the given type and value.
Definition VPlan.h:5101
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
self_iterator getIterator()
Definition ilist_node.h:123
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ Entry
Definition COFF.h:862
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
void reportVectorizationFailure(const StringRef DebugMsg, const StringRef OREMsg, const StringRef ORETag, OptimizationRemarkEmitter *ORE, const Loop *TheLoop, Instruction *I=nullptr)
Reports a vectorization failure: print DebugMsg for debugging purposes along with the corresponding o...
auto m_Cmp()
Matches any compare instruction and ignore it.
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, TruncInst >, OpTy > m_TruncOrSelf(const OpTy &Op)
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
SelectLike_match< CondTy, LTy, RTy > m_SelectLike(const CondTy &C, const LTy &TrueC, const RTy &FalseC)
Matches a value that behaves like a boolean-controlled select, i.e.
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
VPInstruction_match< VPInstruction::ExtractLastLane, VPInstruction_match< VPInstruction::ExtractLastPart, Op0_t > > m_ExtractLastLaneOfLastPart(const Op0_t &Op0)
bool matchFindIVResult(VPInstruction *VPI, Op0_t ReducedIV, Op1_t Start)
Match FindIV result pattern: select(icmp ne ComputeReductionResult(ReducedIV), Sentinel),...
VPInstruction_match< VPInstruction::BranchOnTwoConds > m_BranchOnTwoConds()
VPInstruction_match< VPInstruction::ExtractLastLane, Op0_t > m_ExtractLastLane(const Op0_t &Op0)
VPInstruction_match< VPInstruction::BranchOnCount > m_BranchOnCount()
auto m_VPValue()
Match an arbitrary VPValue and ignore it.
VPInstruction_match< VPInstruction::ExtractLastPart, Op0_t > m_ExtractLastPart(const Op0_t &Op0)
VPRecipeBase * findUserOf(VPValue *V, const MatchT &P)
If V is used by a recipe matching pattern P, return it.
match_bind< VPInstruction > m_VPInstruction(VPInstruction *&V)
Match a VPInstruction, capturing if we match.
VPInstruction_match< VPInstruction::BranchOnCond > m_BranchOnCond()
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
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.
VPInstruction * findComputeReductionResult(VPReductionPHIRecipe *PhiR)
Find the ComputeReductionResult recipe for PhiR, looking through selects inserted for predicated redu...
SmallVector< std::pair< VPBasicBlock *, VPIRBasicBlock * > > getEarlyExits(const VPlan &Plan, const VPBlockBase *MiddleVPBB)
Returns the (early exiting block, exit block) pairs of Plan, i.e.
VPIRFlags getFlagsFromIndDesc(const InductionDescriptor &ID)
Extracts and returns NoWrap and FastMath flags from the induction binop in ID.
Definition VPlanUtils.h:132
VPRecipeBase * findRecipe(VPValue *Start, PredT Pred)
Search Start's users for a recipe satisfying Pred, looking through recipes with definitions.
Definition VPlanUtils.h:149
const SCEV * getSCEVExprForVPValue(const VPValue *V, PredicatedScalarEvolution &PSE, const Loop *L=nullptr)
Return the SCEV expression for V.
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
SmallVector< VPBasicBlock * > vp_rpo_plain_cfg_loop_body(VPBasicBlock *Header)
Returns the VPBasicBlocks forming the loop body of a plain (pre-region) VPlan in reverse post-order s...
Definition VPlanCFG.h:262
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
LLVM_ABI Intrinsic::ID getMinMaxReductionIntrinsicOp(Intrinsic::ID RdxID)
Returns the min/max intrinsic used when expanding a min/max reduction.
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
ReductionStyle getReductionStyle(bool InLoop, bool Ordered, unsigned ScaleFactor)
Definition VPlan.h:2843
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
@ Load
The value being inserted comes from a load (InsertElement only).
auto map_to_vector(ContainerTy &&C, FuncTy &&F)
Map a range to a SmallVector with element types deduced from the mapping.
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
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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
constexpr size_t range_size(R &&Range)
Returns the size of the Range, i.e., the number of elements.
Definition STLExtras.h:1694
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1636
UncountableExitStyle
Different methods of handling early exits.
Definition VPlan.h:79
@ ReadOnly
No side effects to worry about, so we can process any uncountable exits in the loop and branch either...
Definition VPlan.h:84
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
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...
iterator_range< filter_iterator< detail::IterOfRange< RangeT >, PredicateT > > make_filter_range(RangeT &&Range, PredicateT Pred)
Convenience function that takes a range of elements and a predicate, and return a new filter_iterator...
Definition STLExtras.h:551
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
RecurKind
These are the kinds of recurrences that we support.
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
@ FindIV
FindIV reduction with select(icmp(),x,y) where one of (x,y) is a loop induction variable (increasing ...
@ AnyOf
AnyOf reduction with select(cmp(),x,y) where one of (x,y) is loop invariant, and both x and y are int...
@ FMulAdd
Sum of float products with llvm.fmuladd(a * b + sum).
@ SMax
Signed integer max implemented in terms of select(cmp()).
@ SMin
Signed integer min implemented in terms of select(cmp()).
@ Sub
Subtraction of integers.
@ AddChainWithSubs
A chain of adds and subs.
@ UMax
Unsigned integer max implemented in terms of select(cmp()).
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition STLExtras.h:2019
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
auto predecessors(const MachineBasicBlock *BB)
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
bool equal(L &&LRange, R &&RRange)
Wrapper function around std::equal to detect if pair-wise elements between two ranges are the same.
Definition STLExtras.h:2146
LLVM_ABI bool isDereferenceableAndAlignedInLoop(LoadInst *LI, Loop *L, ScalarEvolution &SE, DominatorTree &DT, AssumptionCache *AC=nullptr, SmallVectorImpl< const SCEVPredicate * > *Predicates=nullptr)
Return true if we can prove that the given load (which is assumed to be within the specified loop) wo...
Definition Loads.cpp:304
constexpr detail::IsaCheckPredicate< Types... > IsaPred
Function object wrapper for the llvm::isa type check.
Definition Casting.h:866
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880
A recipe for handling first-order recurrence phis.
Definition VPlan.h:2794
A VPValue representing a live-in from the input IR or a constant.
Definition VPlanValue.h:279
static void foldTailByMasking(VPlan &Plan)
Adapts the vector loop region for tail folding by introducing a header mask and conditionally executi...
static void addMinimumVectorEpilogueIterationCheck(VPlan &Plan, Value *VectorTripCount, bool RequiresScalarEpilogue, ElementCount EpilogueVF, unsigned EpilogueUF, unsigned MainLoopStep, unsigned EpilogueLoopStep, ScalarEvolution &SE)
Add a check to Plan to see if the epilogue vector loop should be executed.
static bool handleMultiUseReductions(VPlan &Plan, OptimizationRemarkEmitter *ORE, Loop *TheLoop)
Try to legalize reductions with multiple in-loop uses.
static bool handleFindLastReductions(VPlan &Plan)
Check if Plan contains any FindLast reductions.
static void createInLoopReductionRecipes(VPlan &Plan, ElementCount MinVF)
Create VPReductionRecipes for in-loop reductions.
static LLVM_ABI_FOR_TEST void createLoopRegions(VPlan &Plan, DebugLoc DL)
Replace loops in Plan's flat CFG with VPRegionBlocks, turning Plan's flat CFG into a hierarchical CFG...
static LLVM_ABI_FOR_TEST std::unique_ptr< VPlan > buildVPlan0(Loop *TheLoop, LoopInfo &LI, Type *InductionTy, PredicatedScalarEvolution &PSE, LoopVersioning *LVer=nullptr)
Create a base VPlan0, serving as the common starting point for all later candidates.
static LLVM_ABI_FOR_TEST void addMiddleCheck(VPlan &Plan)
If a check is needed to guard executing the scalar epilogue loop, it will be added to the middle bloc...
static bool createHeaderPhiRecipes(VPlan &Plan, PredicatedScalarEvolution &PSE, Loop &OrigLoop, const VPDominatorTree &VPDT, const MapVector< PHINode *, InductionDescriptor > &Inductions, const MapVector< PHINode *, RecurrenceDescriptor > &Reductions, const SmallPtrSetImpl< const PHINode * > &FixedOrderRecurrences, const SmallPtrSetImpl< PHINode * > &InLoopReductions, bool AllowReordering)
Replace VPPhi recipes in Plan's header with corresponding VPHeaderPHIRecipe subclasses for inductions...
static void attachAliasMaskToHeaderMask(VPlan &Plan)
Attaches the alias-mask to the existing header-mask.
static LLVM_ABI_FOR_TEST bool handleEarlyExits(VPlan &Plan, UncountableExitStyle Style, Loop *TheLoop, PredicatedScalarEvolution &PSE, DominatorTree &DT, AssumptionCache *AC)
Update Plan to account for all early exits.
static bool handleMaxMinNumReductions(VPlan &Plan)
Check if Plan contains any FMaxNum or FMinNum reductions.
static void attachCheckBlock(VPlan &Plan, Value *Cond, BasicBlock *CheckBlock, bool AddBranchWeights)
static bool finalizeSCEVPredicates(VPlan &Plan, PredicatedScalarEvolution &PSE, bool OptForSize, unsigned SCEVCheckThreshold, OptimizationRemarkEmitter *ORE, Loop *TheLoop)
Finalize SCEV predicates by adding induction predicates from Plan to PSE and checking constraints.
static void addIterationCountCheckBlock(VPlan &Plan, ElementCount VF, unsigned UF, bool RequiresScalarEpilogue, Loop *OrigLoop, const uint32_t *MinItersBypassWeights, DebugLoc DL, PredicatedScalarEvolution &PSE)
Add a new check block before the vector preheader to Plan to check if the main vector loop should be ...
static bool handleUncountableEarlyExits(VPlan &Plan, VPBasicBlock *HeaderVPBB, VPBasicBlock *LatchVPBB, VPBasicBlock *MiddleVPBB, Loop *TheLoop, PredicatedScalarEvolution &PSE, DominatorTree &DT, AssumptionCache *AC, UncountableExitStyle Style)
Update Plan to account for uncountable early exits by introducing appropriate branching logic in the ...
static void addMinimumIterationCheck(VPlan &Plan, ElementCount VF, unsigned UF, ElementCount MinProfitableTripCount, bool RequiresScalarEpilogue, bool TailFolded, Loop *OrigLoop, const uint32_t *MinItersBypassWeights, DebugLoc DL, PredicatedScalarEvolution &PSE, VPBasicBlock *CheckBlock)
static void attachVPCheckBlock(VPlan &Plan, VPValue *Cond, VPBasicBlock *CheckBlock, bool AddBranchWeights)
Wrap runtime check block CheckBlock in a VPIRBB and Cond in a VPValue and connect the block to Plan,...