1 //===- LoopVectorizationLegality.cpp --------------------------------------===// 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 // This file provides loop vectorization legality analysis. Original code 10 // resided in LoopVectorize.cpp for a long time. 11 // 12 // At this point, it is implemented as a utility class, not as an analysis 13 // pass. It should be easy to create an analysis pass around it if there 14 // is a need (but D45420 needs to happen first). 15 // 16 17 #include "llvm/Transforms/Vectorize/LoopVectorizationLegality.h" 18 #include "llvm/Analysis/Loads.h" 19 #include "llvm/Analysis/LoopInfo.h" 20 #include "llvm/Analysis/TargetLibraryInfo.h" 21 #include "llvm/Analysis/ValueTracking.h" 22 #include "llvm/Analysis/VectorUtils.h" 23 #include "llvm/IR/IntrinsicInst.h" 24 #include "llvm/IR/PatternMatch.h" 25 #include "llvm/Transforms/Utils/SizeOpts.h" 26 #include "llvm/Transforms/Vectorize/LoopVectorize.h" 27 28 using namespace llvm; 29 using namespace PatternMatch; 30 31 #define LV_NAME "loop-vectorize" 32 #define DEBUG_TYPE LV_NAME 33 34 extern cl::opt<bool> EnableVPlanPredication; 35 36 static cl::opt<bool> 37 EnableIfConversion("enable-if-conversion", cl::init(true), cl::Hidden, 38 cl::desc("Enable if-conversion during vectorization.")); 39 40 // TODO: Move size-based thresholds out of legality checking, make cost based 41 // decisions instead of hard thresholds. 42 static cl::opt<unsigned> VectorizeSCEVCheckThreshold( 43 "vectorize-scev-check-threshold", cl::init(16), cl::Hidden, 44 cl::desc("The maximum number of SCEV checks allowed.")); 45 46 static cl::opt<unsigned> PragmaVectorizeSCEVCheckThreshold( 47 "pragma-vectorize-scev-check-threshold", cl::init(128), cl::Hidden, 48 cl::desc("The maximum number of SCEV checks allowed with a " 49 "vectorize(enable) pragma")); 50 51 /// Maximum vectorization interleave count. 52 static const unsigned MaxInterleaveFactor = 16; 53 54 namespace llvm { 55 56 bool LoopVectorizeHints::Hint::validate(unsigned Val) { 57 switch (Kind) { 58 case HK_WIDTH: 59 return isPowerOf2_32(Val) && Val <= VectorizerParams::MaxVectorWidth; 60 case HK_INTERLEAVE: 61 return isPowerOf2_32(Val) && Val <= MaxInterleaveFactor; 62 case HK_FORCE: 63 return (Val <= 1); 64 case HK_ISVECTORIZED: 65 case HK_PREDICATE: 66 case HK_SCALABLE: 67 return (Val == 0 || Val == 1); 68 } 69 return false; 70 } 71 72 LoopVectorizeHints::LoopVectorizeHints(const Loop *L, 73 bool InterleaveOnlyWhenForced, 74 OptimizationRemarkEmitter &ORE) 75 : Width("vectorize.width", VectorizerParams::VectorizationFactor, HK_WIDTH), 76 Interleave("interleave.count", InterleaveOnlyWhenForced, HK_INTERLEAVE), 77 Force("vectorize.enable", FK_Undefined, HK_FORCE), 78 IsVectorized("isvectorized", 0, HK_ISVECTORIZED), 79 Predicate("vectorize.predicate.enable", FK_Undefined, HK_PREDICATE), 80 Scalable("vectorize.scalable.enable", false, HK_SCALABLE), TheLoop(L), 81 ORE(ORE) { 82 // Populate values with existing loop metadata. 83 getHintsFromMetadata(); 84 85 // force-vector-interleave overrides DisableInterleaving. 86 if (VectorizerParams::isInterleaveForced()) 87 Interleave.Value = VectorizerParams::VectorizationInterleave; 88 89 if (IsVectorized.Value != 1) 90 // If the vectorization width and interleaving count are both 1 then 91 // consider the loop to have been already vectorized because there's 92 // nothing more that we can do. 93 IsVectorized.Value = 94 getWidth() == ElementCount::getFixed(1) && getInterleave() == 1; 95 LLVM_DEBUG(if (InterleaveOnlyWhenForced && getInterleave() == 1) dbgs() 96 << "LV: Interleaving disabled by the pass manager\n"); 97 } 98 99 void LoopVectorizeHints::setAlreadyVectorized() { 100 LLVMContext &Context = TheLoop->getHeader()->getContext(); 101 102 MDNode *IsVectorizedMD = MDNode::get( 103 Context, 104 {MDString::get(Context, "llvm.loop.isvectorized"), 105 ConstantAsMetadata::get(ConstantInt::get(Context, APInt(32, 1)))}); 106 MDNode *LoopID = TheLoop->getLoopID(); 107 MDNode *NewLoopID = 108 makePostTransformationMetadata(Context, LoopID, 109 {Twine(Prefix(), "vectorize.").str(), 110 Twine(Prefix(), "interleave.").str()}, 111 {IsVectorizedMD}); 112 TheLoop->setLoopID(NewLoopID); 113 114 // Update internal cache. 115 IsVectorized.Value = 1; 116 } 117 118 bool LoopVectorizeHints::allowVectorization( 119 Function *F, Loop *L, bool VectorizeOnlyWhenForced) const { 120 if (getForce() == LoopVectorizeHints::FK_Disabled) { 121 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: #pragma vectorize disable.\n"); 122 emitRemarkWithHints(); 123 return false; 124 } 125 126 if (VectorizeOnlyWhenForced && getForce() != LoopVectorizeHints::FK_Enabled) { 127 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: No #pragma vectorize enable.\n"); 128 emitRemarkWithHints(); 129 return false; 130 } 131 132 if (getIsVectorized() == 1) { 133 LLVM_DEBUG(dbgs() << "LV: Not vectorizing: Disabled/already vectorized.\n"); 134 // FIXME: Add interleave.disable metadata. This will allow 135 // vectorize.disable to be used without disabling the pass and errors 136 // to differentiate between disabled vectorization and a width of 1. 137 ORE.emit([&]() { 138 return OptimizationRemarkAnalysis(vectorizeAnalysisPassName(), 139 "AllDisabled", L->getStartLoc(), 140 L->getHeader()) 141 << "loop not vectorized: vectorization and interleaving are " 142 "explicitly disabled, or the loop has already been " 143 "vectorized"; 144 }); 145 return false; 146 } 147 148 return true; 149 } 150 151 void LoopVectorizeHints::emitRemarkWithHints() const { 152 using namespace ore; 153 154 ORE.emit([&]() { 155 if (Force.Value == LoopVectorizeHints::FK_Disabled) 156 return OptimizationRemarkMissed(LV_NAME, "MissedExplicitlyDisabled", 157 TheLoop->getStartLoc(), 158 TheLoop->getHeader()) 159 << "loop not vectorized: vectorization is explicitly disabled"; 160 else { 161 OptimizationRemarkMissed R(LV_NAME, "MissedDetails", 162 TheLoop->getStartLoc(), TheLoop->getHeader()); 163 R << "loop not vectorized"; 164 if (Force.Value == LoopVectorizeHints::FK_Enabled) { 165 R << " (Force=" << NV("Force", true); 166 if (Width.Value != 0) 167 R << ", Vector Width=" << NV("VectorWidth", getWidth()); 168 if (getInterleave() != 0) 169 R << ", Interleave Count=" << NV("InterleaveCount", getInterleave()); 170 R << ")"; 171 } 172 return R; 173 } 174 }); 175 } 176 177 const char *LoopVectorizeHints::vectorizeAnalysisPassName() const { 178 if (getWidth() == ElementCount::getFixed(1)) 179 return LV_NAME; 180 if (getForce() == LoopVectorizeHints::FK_Disabled) 181 return LV_NAME; 182 if (getForce() == LoopVectorizeHints::FK_Undefined && getWidth().isZero()) 183 return LV_NAME; 184 return OptimizationRemarkAnalysis::AlwaysPrint; 185 } 186 187 void LoopVectorizeHints::getHintsFromMetadata() { 188 MDNode *LoopID = TheLoop->getLoopID(); 189 if (!LoopID) 190 return; 191 192 // First operand should refer to the loop id itself. 193 assert(LoopID->getNumOperands() > 0 && "requires at least one operand"); 194 assert(LoopID->getOperand(0) == LoopID && "invalid loop id"); 195 196 for (unsigned i = 1, ie = LoopID->getNumOperands(); i < ie; ++i) { 197 const MDString *S = nullptr; 198 SmallVector<Metadata *, 4> Args; 199 200 // The expected hint is either a MDString or a MDNode with the first 201 // operand a MDString. 202 if (const MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i))) { 203 if (!MD || MD->getNumOperands() == 0) 204 continue; 205 S = dyn_cast<MDString>(MD->getOperand(0)); 206 for (unsigned i = 1, ie = MD->getNumOperands(); i < ie; ++i) 207 Args.push_back(MD->getOperand(i)); 208 } else { 209 S = dyn_cast<MDString>(LoopID->getOperand(i)); 210 assert(Args.size() == 0 && "too many arguments for MDString"); 211 } 212 213 if (!S) 214 continue; 215 216 // Check if the hint starts with the loop metadata prefix. 217 StringRef Name = S->getString(); 218 if (Args.size() == 1) 219 setHint(Name, Args[0]); 220 } 221 } 222 223 void LoopVectorizeHints::setHint(StringRef Name, Metadata *Arg) { 224 if (!Name.startswith(Prefix())) 225 return; 226 Name = Name.substr(Prefix().size(), StringRef::npos); 227 228 const ConstantInt *C = mdconst::dyn_extract<ConstantInt>(Arg); 229 if (!C) 230 return; 231 unsigned Val = C->getZExtValue(); 232 233 Hint *Hints[] = {&Width, &Interleave, &Force, 234 &IsVectorized, &Predicate, &Scalable}; 235 for (auto H : Hints) { 236 if (Name == H->Name) { 237 if (H->validate(Val)) 238 H->Value = Val; 239 else 240 LLVM_DEBUG(dbgs() << "LV: ignoring invalid hint '" << Name << "'\n"); 241 break; 242 } 243 } 244 } 245 246 // Return true if the inner loop \p Lp is uniform with regard to the outer loop 247 // \p OuterLp (i.e., if the outer loop is vectorized, all the vector lanes 248 // executing the inner loop will execute the same iterations). This check is 249 // very constrained for now but it will be relaxed in the future. \p Lp is 250 // considered uniform if it meets all the following conditions: 251 // 1) it has a canonical IV (starting from 0 and with stride 1), 252 // 2) its latch terminator is a conditional branch and, 253 // 3) its latch condition is a compare instruction whose operands are the 254 // canonical IV and an OuterLp invariant. 255 // This check doesn't take into account the uniformity of other conditions not 256 // related to the loop latch because they don't affect the loop uniformity. 257 // 258 // NOTE: We decided to keep all these checks and its associated documentation 259 // together so that we can easily have a picture of the current supported loop 260 // nests. However, some of the current checks don't depend on \p OuterLp and 261 // would be redundantly executed for each \p Lp if we invoked this function for 262 // different candidate outer loops. This is not the case for now because we 263 // don't currently have the infrastructure to evaluate multiple candidate outer 264 // loops and \p OuterLp will be a fixed parameter while we only support explicit 265 // outer loop vectorization. It's also very likely that these checks go away 266 // before introducing the aforementioned infrastructure. However, if this is not 267 // the case, we should move the \p OuterLp independent checks to a separate 268 // function that is only executed once for each \p Lp. 269 static bool isUniformLoop(Loop *Lp, Loop *OuterLp) { 270 assert(Lp->getLoopLatch() && "Expected loop with a single latch."); 271 272 // If Lp is the outer loop, it's uniform by definition. 273 if (Lp == OuterLp) 274 return true; 275 assert(OuterLp->contains(Lp) && "OuterLp must contain Lp."); 276 277 // 1. 278 PHINode *IV = Lp->getCanonicalInductionVariable(); 279 if (!IV) { 280 LLVM_DEBUG(dbgs() << "LV: Canonical IV not found.\n"); 281 return false; 282 } 283 284 // 2. 285 BasicBlock *Latch = Lp->getLoopLatch(); 286 auto *LatchBr = dyn_cast<BranchInst>(Latch->getTerminator()); 287 if (!LatchBr || LatchBr->isUnconditional()) { 288 LLVM_DEBUG(dbgs() << "LV: Unsupported loop latch branch.\n"); 289 return false; 290 } 291 292 // 3. 293 auto *LatchCmp = dyn_cast<CmpInst>(LatchBr->getCondition()); 294 if (!LatchCmp) { 295 LLVM_DEBUG( 296 dbgs() << "LV: Loop latch condition is not a compare instruction.\n"); 297 return false; 298 } 299 300 Value *CondOp0 = LatchCmp->getOperand(0); 301 Value *CondOp1 = LatchCmp->getOperand(1); 302 Value *IVUpdate = IV->getIncomingValueForBlock(Latch); 303 if (!(CondOp0 == IVUpdate && OuterLp->isLoopInvariant(CondOp1)) && 304 !(CondOp1 == IVUpdate && OuterLp->isLoopInvariant(CondOp0))) { 305 LLVM_DEBUG(dbgs() << "LV: Loop latch condition is not uniform.\n"); 306 return false; 307 } 308 309 return true; 310 } 311 312 // Return true if \p Lp and all its nested loops are uniform with regard to \p 313 // OuterLp. 314 static bool isUniformLoopNest(Loop *Lp, Loop *OuterLp) { 315 if (!isUniformLoop(Lp, OuterLp)) 316 return false; 317 318 // Check if nested loops are uniform. 319 for (Loop *SubLp : *Lp) 320 if (!isUniformLoopNest(SubLp, OuterLp)) 321 return false; 322 323 return true; 324 } 325 326 /// Check whether it is safe to if-convert this phi node. 327 /// 328 /// Phi nodes with constant expressions that can trap are not safe to if 329 /// convert. 330 static bool canIfConvertPHINodes(BasicBlock *BB) { 331 for (PHINode &Phi : BB->phis()) { 332 for (Value *V : Phi.incoming_values()) 333 if (auto *C = dyn_cast<Constant>(V)) 334 if (C->canTrap()) 335 return false; 336 } 337 return true; 338 } 339 340 static Type *convertPointerToIntegerType(const DataLayout &DL, Type *Ty) { 341 if (Ty->isPointerTy()) 342 return DL.getIntPtrType(Ty); 343 344 // It is possible that char's or short's overflow when we ask for the loop's 345 // trip count, work around this by changing the type size. 346 if (Ty->getScalarSizeInBits() < 32) 347 return Type::getInt32Ty(Ty->getContext()); 348 349 return Ty; 350 } 351 352 static Type *getWiderType(const DataLayout &DL, Type *Ty0, Type *Ty1) { 353 Ty0 = convertPointerToIntegerType(DL, Ty0); 354 Ty1 = convertPointerToIntegerType(DL, Ty1); 355 if (Ty0->getScalarSizeInBits() > Ty1->getScalarSizeInBits()) 356 return Ty0; 357 return Ty1; 358 } 359 360 /// Check that the instruction has outside loop users and is not an 361 /// identified reduction variable. 362 static bool hasOutsideLoopUser(const Loop *TheLoop, Instruction *Inst, 363 SmallPtrSetImpl<Value *> &AllowedExit) { 364 // Reductions, Inductions and non-header phis are allowed to have exit users. All 365 // other instructions must not have external users. 366 if (!AllowedExit.count(Inst)) 367 // Check that all of the users of the loop are inside the BB. 368 for (User *U : Inst->users()) { 369 Instruction *UI = cast<Instruction>(U); 370 // This user may be a reduction exit value. 371 if (!TheLoop->contains(UI)) { 372 LLVM_DEBUG(dbgs() << "LV: Found an outside user for : " << *UI << '\n'); 373 return true; 374 } 375 } 376 return false; 377 } 378 379 int LoopVectorizationLegality::isConsecutivePtr(Value *Ptr) { 380 const ValueToValueMap &Strides = 381 getSymbolicStrides() ? *getSymbolicStrides() : ValueToValueMap(); 382 383 Function *F = TheLoop->getHeader()->getParent(); 384 bool OptForSize = F->hasOptSize() || 385 llvm::shouldOptimizeForSize(TheLoop->getHeader(), PSI, BFI, 386 PGSOQueryType::IRPass); 387 bool CanAddPredicate = !OptForSize; 388 int Stride = getPtrStride(PSE, Ptr, TheLoop, Strides, CanAddPredicate, false); 389 if (Stride == 1 || Stride == -1) 390 return Stride; 391 return 0; 392 } 393 394 bool LoopVectorizationLegality::isUniform(Value *V) { 395 return LAI->isUniform(V); 396 } 397 398 bool LoopVectorizationLegality::canVectorizeOuterLoop() { 399 assert(!TheLoop->isInnermost() && "We are not vectorizing an outer loop."); 400 // Store the result and return it at the end instead of exiting early, in case 401 // allowExtraAnalysis is used to report multiple reasons for not vectorizing. 402 bool Result = true; 403 bool DoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE); 404 405 for (BasicBlock *BB : TheLoop->blocks()) { 406 // Check whether the BB terminator is a BranchInst. Any other terminator is 407 // not supported yet. 408 auto *Br = dyn_cast<BranchInst>(BB->getTerminator()); 409 if (!Br) { 410 reportVectorizationFailure("Unsupported basic block terminator", 411 "loop control flow is not understood by vectorizer", 412 "CFGNotUnderstood", ORE, TheLoop); 413 if (DoExtraAnalysis) 414 Result = false; 415 else 416 return false; 417 } 418 419 // Check whether the BranchInst is a supported one. Only unconditional 420 // branches, conditional branches with an outer loop invariant condition or 421 // backedges are supported. 422 // FIXME: We skip these checks when VPlan predication is enabled as we 423 // want to allow divergent branches. This whole check will be removed 424 // once VPlan predication is on by default. 425 if (!EnableVPlanPredication && Br && Br->isConditional() && 426 !TheLoop->isLoopInvariant(Br->getCondition()) && 427 !LI->isLoopHeader(Br->getSuccessor(0)) && 428 !LI->isLoopHeader(Br->getSuccessor(1))) { 429 reportVectorizationFailure("Unsupported conditional branch", 430 "loop control flow is not understood by vectorizer", 431 "CFGNotUnderstood", ORE, TheLoop); 432 if (DoExtraAnalysis) 433 Result = false; 434 else 435 return false; 436 } 437 } 438 439 // Check whether inner loops are uniform. At this point, we only support 440 // simple outer loops scenarios with uniform nested loops. 441 if (!isUniformLoopNest(TheLoop /*loop nest*/, 442 TheLoop /*context outer loop*/)) { 443 reportVectorizationFailure("Outer loop contains divergent loops", 444 "loop control flow is not understood by vectorizer", 445 "CFGNotUnderstood", ORE, TheLoop); 446 if (DoExtraAnalysis) 447 Result = false; 448 else 449 return false; 450 } 451 452 // Check whether we are able to set up outer loop induction. 453 if (!setupOuterLoopInductions()) { 454 reportVectorizationFailure("Unsupported outer loop Phi(s)", 455 "Unsupported outer loop Phi(s)", 456 "UnsupportedPhi", ORE, TheLoop); 457 if (DoExtraAnalysis) 458 Result = false; 459 else 460 return false; 461 } 462 463 return Result; 464 } 465 466 void LoopVectorizationLegality::addInductionPhi( 467 PHINode *Phi, const InductionDescriptor &ID, 468 SmallPtrSetImpl<Value *> &AllowedExit) { 469 Inductions[Phi] = ID; 470 471 // In case this induction also comes with casts that we know we can ignore 472 // in the vectorized loop body, record them here. All casts could be recorded 473 // here for ignoring, but suffices to record only the first (as it is the 474 // only one that may bw used outside the cast sequence). 475 const SmallVectorImpl<Instruction *> &Casts = ID.getCastInsts(); 476 if (!Casts.empty()) 477 InductionCastsToIgnore.insert(*Casts.begin()); 478 479 Type *PhiTy = Phi->getType(); 480 const DataLayout &DL = Phi->getModule()->getDataLayout(); 481 482 // Get the widest type. 483 if (!PhiTy->isFloatingPointTy()) { 484 if (!WidestIndTy) 485 WidestIndTy = convertPointerToIntegerType(DL, PhiTy); 486 else 487 WidestIndTy = getWiderType(DL, PhiTy, WidestIndTy); 488 } 489 490 // Int inductions are special because we only allow one IV. 491 if (ID.getKind() == InductionDescriptor::IK_IntInduction && 492 ID.getConstIntStepValue() && ID.getConstIntStepValue()->isOne() && 493 isa<Constant>(ID.getStartValue()) && 494 cast<Constant>(ID.getStartValue())->isNullValue()) { 495 496 // Use the phi node with the widest type as induction. Use the last 497 // one if there are multiple (no good reason for doing this other 498 // than it is expedient). We've checked that it begins at zero and 499 // steps by one, so this is a canonical induction variable. 500 if (!PrimaryInduction || PhiTy == WidestIndTy) 501 PrimaryInduction = Phi; 502 } 503 504 // Both the PHI node itself, and the "post-increment" value feeding 505 // back into the PHI node may have external users. 506 // We can allow those uses, except if the SCEVs we have for them rely 507 // on predicates that only hold within the loop, since allowing the exit 508 // currently means re-using this SCEV outside the loop (see PR33706 for more 509 // details). 510 if (PSE.getUnionPredicate().isAlwaysTrue()) { 511 AllowedExit.insert(Phi); 512 AllowedExit.insert(Phi->getIncomingValueForBlock(TheLoop->getLoopLatch())); 513 } 514 515 LLVM_DEBUG(dbgs() << "LV: Found an induction variable.\n"); 516 } 517 518 bool LoopVectorizationLegality::setupOuterLoopInductions() { 519 BasicBlock *Header = TheLoop->getHeader(); 520 521 // Returns true if a given Phi is a supported induction. 522 auto isSupportedPhi = [&](PHINode &Phi) -> bool { 523 InductionDescriptor ID; 524 if (InductionDescriptor::isInductionPHI(&Phi, TheLoop, PSE, ID) && 525 ID.getKind() == InductionDescriptor::IK_IntInduction) { 526 addInductionPhi(&Phi, ID, AllowedExit); 527 return true; 528 } else { 529 // Bail out for any Phi in the outer loop header that is not a supported 530 // induction. 531 LLVM_DEBUG( 532 dbgs() 533 << "LV: Found unsupported PHI for outer loop vectorization.\n"); 534 return false; 535 } 536 }; 537 538 if (llvm::all_of(Header->phis(), isSupportedPhi)) 539 return true; 540 else 541 return false; 542 } 543 544 /// Checks if a function is scalarizable according to the TLI, in 545 /// the sense that it should be vectorized and then expanded in 546 /// multiple scalarcalls. This is represented in the 547 /// TLI via mappings that do not specify a vector name, as in the 548 /// following example: 549 /// 550 /// const VecDesc VecIntrinsics[] = { 551 /// {"llvm.phx.abs.i32", "", 4} 552 /// }; 553 static bool isTLIScalarize(const TargetLibraryInfo &TLI, const CallInst &CI) { 554 const StringRef ScalarName = CI.getCalledFunction()->getName(); 555 bool Scalarize = TLI.isFunctionVectorizable(ScalarName); 556 // Check that all known VFs are not associated to a vector 557 // function, i.e. the vector name is emty. 558 if (Scalarize) { 559 ElementCount WidestFixedVF, WidestScalableVF; 560 TLI.getWidestVF(ScalarName, WidestFixedVF, WidestScalableVF); 561 for (ElementCount VF = ElementCount::getFixed(2); 562 ElementCount::isKnownLE(VF, WidestFixedVF); VF *= 2) 563 Scalarize &= !TLI.isFunctionVectorizable(ScalarName, VF); 564 for (ElementCount VF = ElementCount::getScalable(1); 565 ElementCount::isKnownLE(VF, WidestScalableVF); VF *= 2) 566 Scalarize &= !TLI.isFunctionVectorizable(ScalarName, VF); 567 assert((WidestScalableVF.isZero() || !Scalarize) && 568 "Caller may decide to scalarize a variant using a scalable VF"); 569 } 570 return Scalarize; 571 } 572 573 bool LoopVectorizationLegality::canVectorizeInstrs() { 574 BasicBlock *Header = TheLoop->getHeader(); 575 576 // For each block in the loop. 577 for (BasicBlock *BB : TheLoop->blocks()) { 578 // Scan the instructions in the block and look for hazards. 579 for (Instruction &I : *BB) { 580 if (auto *Phi = dyn_cast<PHINode>(&I)) { 581 Type *PhiTy = Phi->getType(); 582 // Check that this PHI type is allowed. 583 if (!PhiTy->isIntegerTy() && !PhiTy->isFloatingPointTy() && 584 !PhiTy->isPointerTy()) { 585 reportVectorizationFailure("Found a non-int non-pointer PHI", 586 "loop control flow is not understood by vectorizer", 587 "CFGNotUnderstood", ORE, TheLoop); 588 return false; 589 } 590 591 // If this PHINode is not in the header block, then we know that we 592 // can convert it to select during if-conversion. No need to check if 593 // the PHIs in this block are induction or reduction variables. 594 if (BB != Header) { 595 // Non-header phi nodes that have outside uses can be vectorized. Add 596 // them to the list of allowed exits. 597 // Unsafe cyclic dependencies with header phis are identified during 598 // legalization for reduction, induction and first order 599 // recurrences. 600 AllowedExit.insert(&I); 601 continue; 602 } 603 604 // We only allow if-converted PHIs with exactly two incoming values. 605 if (Phi->getNumIncomingValues() != 2) { 606 reportVectorizationFailure("Found an invalid PHI", 607 "loop control flow is not understood by vectorizer", 608 "CFGNotUnderstood", ORE, TheLoop, Phi); 609 return false; 610 } 611 612 RecurrenceDescriptor RedDes; 613 if (RecurrenceDescriptor::isReductionPHI(Phi, TheLoop, RedDes, DB, AC, 614 DT)) { 615 Requirements->addExactFPMathInst(RedDes.getExactFPMathInst()); 616 AllowedExit.insert(RedDes.getLoopExitInstr()); 617 Reductions[Phi] = RedDes; 618 continue; 619 } 620 621 // TODO: Instead of recording the AllowedExit, it would be good to record the 622 // complementary set: NotAllowedExit. These include (but may not be 623 // limited to): 624 // 1. Reduction phis as they represent the one-before-last value, which 625 // is not available when vectorized 626 // 2. Induction phis and increment when SCEV predicates cannot be used 627 // outside the loop - see addInductionPhi 628 // 3. Non-Phis with outside uses when SCEV predicates cannot be used 629 // outside the loop - see call to hasOutsideLoopUser in the non-phi 630 // handling below 631 // 4. FirstOrderRecurrence phis that can possibly be handled by 632 // extraction. 633 // By recording these, we can then reason about ways to vectorize each 634 // of these NotAllowedExit. 635 InductionDescriptor ID; 636 if (InductionDescriptor::isInductionPHI(Phi, TheLoop, PSE, ID)) { 637 addInductionPhi(Phi, ID, AllowedExit); 638 Requirements->addExactFPMathInst(ID.getExactFPMathInst()); 639 continue; 640 } 641 642 if (RecurrenceDescriptor::isFirstOrderRecurrence(Phi, TheLoop, 643 SinkAfter, DT)) { 644 AllowedExit.insert(Phi); 645 FirstOrderRecurrences.insert(Phi); 646 continue; 647 } 648 649 // As a last resort, coerce the PHI to a AddRec expression 650 // and re-try classifying it a an induction PHI. 651 if (InductionDescriptor::isInductionPHI(Phi, TheLoop, PSE, ID, true)) { 652 addInductionPhi(Phi, ID, AllowedExit); 653 continue; 654 } 655 656 reportVectorizationFailure("Found an unidentified PHI", 657 "value that could not be identified as " 658 "reduction is used outside the loop", 659 "NonReductionValueUsedOutsideLoop", ORE, TheLoop, Phi); 660 return false; 661 } // end of PHI handling 662 663 // We handle calls that: 664 // * Are debug info intrinsics. 665 // * Have a mapping to an IR intrinsic. 666 // * Have a vector version available. 667 auto *CI = dyn_cast<CallInst>(&I); 668 669 if (CI && !getVectorIntrinsicIDForCall(CI, TLI) && 670 !isa<DbgInfoIntrinsic>(CI) && 671 !(CI->getCalledFunction() && TLI && 672 (!VFDatabase::getMappings(*CI).empty() || 673 isTLIScalarize(*TLI, *CI)))) { 674 // If the call is a recognized math libary call, it is likely that 675 // we can vectorize it given loosened floating-point constraints. 676 LibFunc Func; 677 bool IsMathLibCall = 678 TLI && CI->getCalledFunction() && 679 CI->getType()->isFloatingPointTy() && 680 TLI->getLibFunc(CI->getCalledFunction()->getName(), Func) && 681 TLI->hasOptimizedCodeGen(Func); 682 683 if (IsMathLibCall) { 684 // TODO: Ideally, we should not use clang-specific language here, 685 // but it's hard to provide meaningful yet generic advice. 686 // Also, should this be guarded by allowExtraAnalysis() and/or be part 687 // of the returned info from isFunctionVectorizable()? 688 reportVectorizationFailure( 689 "Found a non-intrinsic callsite", 690 "library call cannot be vectorized. " 691 "Try compiling with -fno-math-errno, -ffast-math, " 692 "or similar flags", 693 "CantVectorizeLibcall", ORE, TheLoop, CI); 694 } else { 695 reportVectorizationFailure("Found a non-intrinsic callsite", 696 "call instruction cannot be vectorized", 697 "CantVectorizeLibcall", ORE, TheLoop, CI); 698 } 699 return false; 700 } 701 702 // Some intrinsics have scalar arguments and should be same in order for 703 // them to be vectorized (i.e. loop invariant). 704 if (CI) { 705 auto *SE = PSE.getSE(); 706 Intrinsic::ID IntrinID = getVectorIntrinsicIDForCall(CI, TLI); 707 for (unsigned i = 0, e = CI->getNumArgOperands(); i != e; ++i) 708 if (hasVectorInstrinsicScalarOpd(IntrinID, i)) { 709 if (!SE->isLoopInvariant(PSE.getSCEV(CI->getOperand(i)), TheLoop)) { 710 reportVectorizationFailure("Found unvectorizable intrinsic", 711 "intrinsic instruction cannot be vectorized", 712 "CantVectorizeIntrinsic", ORE, TheLoop, CI); 713 return false; 714 } 715 } 716 } 717 718 // Check that the instruction return type is vectorizable. 719 // Also, we can't vectorize extractelement instructions. 720 if ((!VectorType::isValidElementType(I.getType()) && 721 !I.getType()->isVoidTy()) || 722 isa<ExtractElementInst>(I)) { 723 reportVectorizationFailure("Found unvectorizable type", 724 "instruction return type cannot be vectorized", 725 "CantVectorizeInstructionReturnType", ORE, TheLoop, &I); 726 return false; 727 } 728 729 // Check that the stored type is vectorizable. 730 if (auto *ST = dyn_cast<StoreInst>(&I)) { 731 Type *T = ST->getValueOperand()->getType(); 732 if (!VectorType::isValidElementType(T)) { 733 reportVectorizationFailure("Store instruction cannot be vectorized", 734 "store instruction cannot be vectorized", 735 "CantVectorizeStore", ORE, TheLoop, ST); 736 return false; 737 } 738 739 // For nontemporal stores, check that a nontemporal vector version is 740 // supported on the target. 741 if (ST->getMetadata(LLVMContext::MD_nontemporal)) { 742 // Arbitrarily try a vector of 2 elements. 743 auto *VecTy = FixedVectorType::get(T, /*NumElts=*/2); 744 assert(VecTy && "did not find vectorized version of stored type"); 745 if (!TTI->isLegalNTStore(VecTy, ST->getAlign())) { 746 reportVectorizationFailure( 747 "nontemporal store instruction cannot be vectorized", 748 "nontemporal store instruction cannot be vectorized", 749 "CantVectorizeNontemporalStore", ORE, TheLoop, ST); 750 return false; 751 } 752 } 753 754 } else if (auto *LD = dyn_cast<LoadInst>(&I)) { 755 if (LD->getMetadata(LLVMContext::MD_nontemporal)) { 756 // For nontemporal loads, check that a nontemporal vector version is 757 // supported on the target (arbitrarily try a vector of 2 elements). 758 auto *VecTy = FixedVectorType::get(I.getType(), /*NumElts=*/2); 759 assert(VecTy && "did not find vectorized version of load type"); 760 if (!TTI->isLegalNTLoad(VecTy, LD->getAlign())) { 761 reportVectorizationFailure( 762 "nontemporal load instruction cannot be vectorized", 763 "nontemporal load instruction cannot be vectorized", 764 "CantVectorizeNontemporalLoad", ORE, TheLoop, LD); 765 return false; 766 } 767 } 768 769 // FP instructions can allow unsafe algebra, thus vectorizable by 770 // non-IEEE-754 compliant SIMD units. 771 // This applies to floating-point math operations and calls, not memory 772 // operations, shuffles, or casts, as they don't change precision or 773 // semantics. 774 } else if (I.getType()->isFloatingPointTy() && (CI || I.isBinaryOp()) && 775 !I.isFast()) { 776 LLVM_DEBUG(dbgs() << "LV: Found FP op with unsafe algebra.\n"); 777 Hints->setPotentiallyUnsafe(); 778 } 779 780 // Reduction instructions are allowed to have exit users. 781 // All other instructions must not have external users. 782 if (hasOutsideLoopUser(TheLoop, &I, AllowedExit)) { 783 // We can safely vectorize loops where instructions within the loop are 784 // used outside the loop only if the SCEV predicates within the loop is 785 // same as outside the loop. Allowing the exit means reusing the SCEV 786 // outside the loop. 787 if (PSE.getUnionPredicate().isAlwaysTrue()) { 788 AllowedExit.insert(&I); 789 continue; 790 } 791 reportVectorizationFailure("Value cannot be used outside the loop", 792 "value cannot be used outside the loop", 793 "ValueUsedOutsideLoop", ORE, TheLoop, &I); 794 return false; 795 } 796 } // next instr. 797 } 798 799 if (!PrimaryInduction) { 800 if (Inductions.empty()) { 801 reportVectorizationFailure("Did not find one integer induction var", 802 "loop induction variable could not be identified", 803 "NoInductionVariable", ORE, TheLoop); 804 return false; 805 } else if (!WidestIndTy) { 806 reportVectorizationFailure("Did not find one integer induction var", 807 "integer loop induction variable could not be identified", 808 "NoIntegerInductionVariable", ORE, TheLoop); 809 return false; 810 } else { 811 LLVM_DEBUG(dbgs() << "LV: Did not find one integer induction var.\n"); 812 } 813 } 814 815 // For first order recurrences, we use the previous value (incoming value from 816 // the latch) to check if it dominates all users of the recurrence. Bail out 817 // if we have to sink such an instruction for another recurrence, as the 818 // dominance requirement may not hold after sinking. 819 BasicBlock *LoopLatch = TheLoop->getLoopLatch(); 820 if (any_of(FirstOrderRecurrences, [LoopLatch, this](const PHINode *Phi) { 821 Instruction *V = 822 cast<Instruction>(Phi->getIncomingValueForBlock(LoopLatch)); 823 return SinkAfter.find(V) != SinkAfter.end(); 824 })) 825 return false; 826 827 // Now we know the widest induction type, check if our found induction 828 // is the same size. If it's not, unset it here and InnerLoopVectorizer 829 // will create another. 830 if (PrimaryInduction && WidestIndTy != PrimaryInduction->getType()) 831 PrimaryInduction = nullptr; 832 833 return true; 834 } 835 836 bool LoopVectorizationLegality::canVectorizeMemory() { 837 LAI = &(*GetLAA)(*TheLoop); 838 const OptimizationRemarkAnalysis *LAR = LAI->getReport(); 839 if (LAR) { 840 ORE->emit([&]() { 841 return OptimizationRemarkAnalysis(Hints->vectorizeAnalysisPassName(), 842 "loop not vectorized: ", *LAR); 843 }); 844 } 845 if (!LAI->canVectorizeMemory()) 846 return false; 847 848 if (LAI->hasDependenceInvolvingLoopInvariantAddress()) { 849 reportVectorizationFailure("Stores to a uniform address", 850 "write to a loop invariant address could not be vectorized", 851 "CantVectorizeStoreToLoopInvariantAddress", ORE, TheLoop); 852 return false; 853 } 854 Requirements->addRuntimePointerChecks(LAI->getNumRuntimePointerChecks()); 855 PSE.addPredicate(LAI->getPSE().getUnionPredicate()); 856 857 return true; 858 } 859 860 bool LoopVectorizationLegality::isInductionPhi(const Value *V) { 861 Value *In0 = const_cast<Value *>(V); 862 PHINode *PN = dyn_cast_or_null<PHINode>(In0); 863 if (!PN) 864 return false; 865 866 return Inductions.count(PN); 867 } 868 869 bool LoopVectorizationLegality::isCastedInductionVariable(const Value *V) { 870 auto *Inst = dyn_cast<Instruction>(V); 871 return (Inst && InductionCastsToIgnore.count(Inst)); 872 } 873 874 bool LoopVectorizationLegality::isInductionVariable(const Value *V) { 875 return isInductionPhi(V) || isCastedInductionVariable(V); 876 } 877 878 bool LoopVectorizationLegality::isFirstOrderRecurrence(const PHINode *Phi) { 879 return FirstOrderRecurrences.count(Phi); 880 } 881 882 bool LoopVectorizationLegality::blockNeedsPredication(BasicBlock *BB) { 883 return LoopAccessInfo::blockNeedsPredication(BB, TheLoop, DT); 884 } 885 886 bool LoopVectorizationLegality::blockCanBePredicated( 887 BasicBlock *BB, SmallPtrSetImpl<Value *> &SafePtrs, 888 SmallPtrSetImpl<const Instruction *> &MaskedOp, 889 SmallPtrSetImpl<Instruction *> &ConditionalAssumes, 890 bool PreserveGuards) const { 891 const bool IsAnnotatedParallel = TheLoop->isAnnotatedParallel(); 892 893 for (Instruction &I : *BB) { 894 // Check that we don't have a constant expression that can trap as operand. 895 for (Value *Operand : I.operands()) { 896 if (auto *C = dyn_cast<Constant>(Operand)) 897 if (C->canTrap()) 898 return false; 899 } 900 901 // We can predicate blocks with calls to assume, as long as we drop them in 902 // case we flatten the CFG via predication. 903 if (match(&I, m_Intrinsic<Intrinsic::assume>())) { 904 ConditionalAssumes.insert(&I); 905 continue; 906 } 907 908 // Do not let llvm.experimental.noalias.scope.decl block the vectorization. 909 // TODO: there might be cases that it should block the vectorization. Let's 910 // ignore those for now. 911 if (isa<NoAliasScopeDeclInst>(&I)) 912 continue; 913 914 // We might be able to hoist the load. 915 if (I.mayReadFromMemory()) { 916 auto *LI = dyn_cast<LoadInst>(&I); 917 if (!LI) 918 return false; 919 if (!SafePtrs.count(LI->getPointerOperand())) { 920 // !llvm.mem.parallel_loop_access implies if-conversion safety. 921 // Otherwise, record that the load needs (real or emulated) masking 922 // and let the cost model decide. 923 if (!IsAnnotatedParallel || PreserveGuards) 924 MaskedOp.insert(LI); 925 continue; 926 } 927 } 928 929 if (I.mayWriteToMemory()) { 930 auto *SI = dyn_cast<StoreInst>(&I); 931 if (!SI) 932 return false; 933 // Predicated store requires some form of masking: 934 // 1) masked store HW instruction, 935 // 2) emulation via load-blend-store (only if safe and legal to do so, 936 // be aware on the race conditions), or 937 // 3) element-by-element predicate check and scalar store. 938 MaskedOp.insert(SI); 939 continue; 940 } 941 if (I.mayThrow()) 942 return false; 943 } 944 945 return true; 946 } 947 948 bool LoopVectorizationLegality::canVectorizeWithIfConvert() { 949 if (!EnableIfConversion) { 950 reportVectorizationFailure("If-conversion is disabled", 951 "if-conversion is disabled", 952 "IfConversionDisabled", 953 ORE, TheLoop); 954 return false; 955 } 956 957 assert(TheLoop->getNumBlocks() > 1 && "Single block loops are vectorizable"); 958 959 // A list of pointers which are known to be dereferenceable within scope of 960 // the loop body for each iteration of the loop which executes. That is, 961 // the memory pointed to can be dereferenced (with the access size implied by 962 // the value's type) unconditionally within the loop header without 963 // introducing a new fault. 964 SmallPtrSet<Value *, 8> SafePointers; 965 966 // Collect safe addresses. 967 for (BasicBlock *BB : TheLoop->blocks()) { 968 if (!blockNeedsPredication(BB)) { 969 for (Instruction &I : *BB) 970 if (auto *Ptr = getLoadStorePointerOperand(&I)) 971 SafePointers.insert(Ptr); 972 continue; 973 } 974 975 // For a block which requires predication, a address may be safe to access 976 // in the loop w/o predication if we can prove dereferenceability facts 977 // sufficient to ensure it'll never fault within the loop. For the moment, 978 // we restrict this to loads; stores are more complicated due to 979 // concurrency restrictions. 980 ScalarEvolution &SE = *PSE.getSE(); 981 for (Instruction &I : *BB) { 982 LoadInst *LI = dyn_cast<LoadInst>(&I); 983 if (LI && !LI->getType()->isVectorTy() && !mustSuppressSpeculation(*LI) && 984 isDereferenceableAndAlignedInLoop(LI, TheLoop, SE, *DT)) 985 SafePointers.insert(LI->getPointerOperand()); 986 } 987 } 988 989 // Collect the blocks that need predication. 990 BasicBlock *Header = TheLoop->getHeader(); 991 for (BasicBlock *BB : TheLoop->blocks()) { 992 // We don't support switch statements inside loops. 993 if (!isa<BranchInst>(BB->getTerminator())) { 994 reportVectorizationFailure("Loop contains a switch statement", 995 "loop contains a switch statement", 996 "LoopContainsSwitch", ORE, TheLoop, 997 BB->getTerminator()); 998 return false; 999 } 1000 1001 // We must be able to predicate all blocks that need to be predicated. 1002 if (blockNeedsPredication(BB)) { 1003 if (!blockCanBePredicated(BB, SafePointers, MaskedOp, 1004 ConditionalAssumes)) { 1005 reportVectorizationFailure( 1006 "Control flow cannot be substituted for a select", 1007 "control flow cannot be substituted for a select", 1008 "NoCFGForSelect", ORE, TheLoop, 1009 BB->getTerminator()); 1010 return false; 1011 } 1012 } else if (BB != Header && !canIfConvertPHINodes(BB)) { 1013 reportVectorizationFailure( 1014 "Control flow cannot be substituted for a select", 1015 "control flow cannot be substituted for a select", 1016 "NoCFGForSelect", ORE, TheLoop, 1017 BB->getTerminator()); 1018 return false; 1019 } 1020 } 1021 1022 // We can if-convert this loop. 1023 return true; 1024 } 1025 1026 // Helper function to canVectorizeLoopNestCFG. 1027 bool LoopVectorizationLegality::canVectorizeLoopCFG(Loop *Lp, 1028 bool UseVPlanNativePath) { 1029 assert((UseVPlanNativePath || Lp->isInnermost()) && 1030 "VPlan-native path is not enabled."); 1031 1032 // TODO: ORE should be improved to show more accurate information when an 1033 // outer loop can't be vectorized because a nested loop is not understood or 1034 // legal. Something like: "outer_loop_location: loop not vectorized: 1035 // (inner_loop_location) loop control flow is not understood by vectorizer". 1036 1037 // Store the result and return it at the end instead of exiting early, in case 1038 // allowExtraAnalysis is used to report multiple reasons for not vectorizing. 1039 bool Result = true; 1040 bool DoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE); 1041 1042 // We must have a loop in canonical form. Loops with indirectbr in them cannot 1043 // be canonicalized. 1044 if (!Lp->getLoopPreheader()) { 1045 reportVectorizationFailure("Loop doesn't have a legal pre-header", 1046 "loop control flow is not understood by vectorizer", 1047 "CFGNotUnderstood", ORE, TheLoop); 1048 if (DoExtraAnalysis) 1049 Result = false; 1050 else 1051 return false; 1052 } 1053 1054 // We must have a single backedge. 1055 if (Lp->getNumBackEdges() != 1) { 1056 reportVectorizationFailure("The loop must have a single backedge", 1057 "loop control flow is not understood by vectorizer", 1058 "CFGNotUnderstood", ORE, TheLoop); 1059 if (DoExtraAnalysis) 1060 Result = false; 1061 else 1062 return false; 1063 } 1064 1065 // We currently must have a single "exit block" after the loop. Note that 1066 // multiple "exiting blocks" inside the loop are allowed, provided they all 1067 // reach the single exit block. 1068 // TODO: This restriction can be relaxed in the near future, it's here solely 1069 // to allow separation of changes for review. We need to generalize the phi 1070 // update logic in a number of places. 1071 if (!Lp->getUniqueExitBlock()) { 1072 reportVectorizationFailure("The loop must have a unique exit block", 1073 "loop control flow is not understood by vectorizer", 1074 "CFGNotUnderstood", ORE, TheLoop); 1075 if (DoExtraAnalysis) 1076 Result = false; 1077 else 1078 return false; 1079 } 1080 return Result; 1081 } 1082 1083 bool LoopVectorizationLegality::canVectorizeLoopNestCFG( 1084 Loop *Lp, bool UseVPlanNativePath) { 1085 // Store the result and return it at the end instead of exiting early, in case 1086 // allowExtraAnalysis is used to report multiple reasons for not vectorizing. 1087 bool Result = true; 1088 bool DoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE); 1089 if (!canVectorizeLoopCFG(Lp, UseVPlanNativePath)) { 1090 if (DoExtraAnalysis) 1091 Result = false; 1092 else 1093 return false; 1094 } 1095 1096 // Recursively check whether the loop control flow of nested loops is 1097 // understood. 1098 for (Loop *SubLp : *Lp) 1099 if (!canVectorizeLoopNestCFG(SubLp, UseVPlanNativePath)) { 1100 if (DoExtraAnalysis) 1101 Result = false; 1102 else 1103 return false; 1104 } 1105 1106 return Result; 1107 } 1108 1109 bool LoopVectorizationLegality::canVectorize(bool UseVPlanNativePath) { 1110 // Store the result and return it at the end instead of exiting early, in case 1111 // allowExtraAnalysis is used to report multiple reasons for not vectorizing. 1112 bool Result = true; 1113 1114 bool DoExtraAnalysis = ORE->allowExtraAnalysis(DEBUG_TYPE); 1115 // Check whether the loop-related control flow in the loop nest is expected by 1116 // vectorizer. 1117 if (!canVectorizeLoopNestCFG(TheLoop, UseVPlanNativePath)) { 1118 if (DoExtraAnalysis) 1119 Result = false; 1120 else 1121 return false; 1122 } 1123 1124 // We need to have a loop header. 1125 LLVM_DEBUG(dbgs() << "LV: Found a loop: " << TheLoop->getHeader()->getName() 1126 << '\n'); 1127 1128 // Specific checks for outer loops. We skip the remaining legal checks at this 1129 // point because they don't support outer loops. 1130 if (!TheLoop->isInnermost()) { 1131 assert(UseVPlanNativePath && "VPlan-native path is not enabled."); 1132 1133 if (!canVectorizeOuterLoop()) { 1134 reportVectorizationFailure("Unsupported outer loop", 1135 "unsupported outer loop", 1136 "UnsupportedOuterLoop", 1137 ORE, TheLoop); 1138 // TODO: Implement DoExtraAnalysis when subsequent legal checks support 1139 // outer loops. 1140 return false; 1141 } 1142 1143 LLVM_DEBUG(dbgs() << "LV: We can vectorize this outer loop!\n"); 1144 return Result; 1145 } 1146 1147 assert(TheLoop->isInnermost() && "Inner loop expected."); 1148 // Check if we can if-convert non-single-bb loops. 1149 unsigned NumBlocks = TheLoop->getNumBlocks(); 1150 if (NumBlocks != 1 && !canVectorizeWithIfConvert()) { 1151 LLVM_DEBUG(dbgs() << "LV: Can't if-convert the loop.\n"); 1152 if (DoExtraAnalysis) 1153 Result = false; 1154 else 1155 return false; 1156 } 1157 1158 // Check if we can vectorize the instructions and CFG in this loop. 1159 if (!canVectorizeInstrs()) { 1160 LLVM_DEBUG(dbgs() << "LV: Can't vectorize the instructions or CFG\n"); 1161 if (DoExtraAnalysis) 1162 Result = false; 1163 else 1164 return false; 1165 } 1166 1167 // Go over each instruction and look at memory deps. 1168 if (!canVectorizeMemory()) { 1169 LLVM_DEBUG(dbgs() << "LV: Can't vectorize due to memory conflicts\n"); 1170 if (DoExtraAnalysis) 1171 Result = false; 1172 else 1173 return false; 1174 } 1175 1176 LLVM_DEBUG(dbgs() << "LV: We can vectorize this loop" 1177 << (LAI->getRuntimePointerChecking()->Need 1178 ? " (with a runtime bound check)" 1179 : "") 1180 << "!\n"); 1181 1182 unsigned SCEVThreshold = VectorizeSCEVCheckThreshold; 1183 if (Hints->getForce() == LoopVectorizeHints::FK_Enabled) 1184 SCEVThreshold = PragmaVectorizeSCEVCheckThreshold; 1185 1186 if (PSE.getUnionPredicate().getComplexity() > SCEVThreshold) { 1187 reportVectorizationFailure("Too many SCEV checks needed", 1188 "Too many SCEV assumptions need to be made and checked at runtime", 1189 "TooManySCEVRunTimeChecks", ORE, TheLoop); 1190 if (DoExtraAnalysis) 1191 Result = false; 1192 else 1193 return false; 1194 } 1195 1196 // Okay! We've done all the tests. If any have failed, return false. Otherwise 1197 // we can vectorize, and at this point we don't have any other mem analysis 1198 // which may limit our maximum vectorization factor, so just return true with 1199 // no restrictions. 1200 return Result; 1201 } 1202 1203 bool LoopVectorizationLegality::prepareToFoldTailByMasking() { 1204 1205 LLVM_DEBUG(dbgs() << "LV: checking if tail can be folded by masking.\n"); 1206 1207 SmallPtrSet<const Value *, 8> ReductionLiveOuts; 1208 1209 for (auto &Reduction : getReductionVars()) 1210 ReductionLiveOuts.insert(Reduction.second.getLoopExitInstr()); 1211 1212 // TODO: handle non-reduction outside users when tail is folded by masking. 1213 for (auto *AE : AllowedExit) { 1214 // Check that all users of allowed exit values are inside the loop or 1215 // are the live-out of a reduction. 1216 if (ReductionLiveOuts.count(AE)) 1217 continue; 1218 for (User *U : AE->users()) { 1219 Instruction *UI = cast<Instruction>(U); 1220 if (TheLoop->contains(UI)) 1221 continue; 1222 LLVM_DEBUG( 1223 dbgs() 1224 << "LV: Cannot fold tail by masking, loop has an outside user for " 1225 << *UI << "\n"); 1226 return false; 1227 } 1228 } 1229 1230 // The list of pointers that we can safely read and write to remains empty. 1231 SmallPtrSet<Value *, 8> SafePointers; 1232 1233 SmallPtrSet<const Instruction *, 8> TmpMaskedOp; 1234 SmallPtrSet<Instruction *, 8> TmpConditionalAssumes; 1235 1236 // Check and mark all blocks for predication, including those that ordinarily 1237 // do not need predication such as the header block. 1238 for (BasicBlock *BB : TheLoop->blocks()) { 1239 if (!blockCanBePredicated(BB, SafePointers, TmpMaskedOp, 1240 TmpConditionalAssumes, 1241 /* MaskAllLoads= */ true)) { 1242 LLVM_DEBUG(dbgs() << "LV: Cannot fold tail by masking as requested.\n"); 1243 return false; 1244 } 1245 } 1246 1247 LLVM_DEBUG(dbgs() << "LV: can fold tail by masking.\n"); 1248 1249 MaskedOp.insert(TmpMaskedOp.begin(), TmpMaskedOp.end()); 1250 ConditionalAssumes.insert(TmpConditionalAssumes.begin(), 1251 TmpConditionalAssumes.end()); 1252 1253 return true; 1254 } 1255 1256 } // namespace llvm 1257