1 //===----------- VectorUtils.cpp - Vectorizer utility functions -----------===// 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 defines vectorizer utilities. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/Analysis/VectorUtils.h" 14 #include "llvm/ADT/EquivalenceClasses.h" 15 #include "llvm/Analysis/DemandedBits.h" 16 #include "llvm/Analysis/LoopInfo.h" 17 #include "llvm/Analysis/LoopIterator.h" 18 #include "llvm/Analysis/ScalarEvolution.h" 19 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 20 #include "llvm/Analysis/TargetTransformInfo.h" 21 #include "llvm/Analysis/ValueTracking.h" 22 #include "llvm/IR/Constants.h" 23 #include "llvm/IR/GetElementPtrTypeIterator.h" 24 #include "llvm/IR/IRBuilder.h" 25 #include "llvm/IR/PatternMatch.h" 26 #include "llvm/IR/Value.h" 27 28 #define DEBUG_TYPE "vectorutils" 29 30 using namespace llvm; 31 using namespace llvm::PatternMatch; 32 33 /// Maximum factor for an interleaved memory access. 34 static cl::opt<unsigned> MaxInterleaveGroupFactor( 35 "max-interleave-group-factor", cl::Hidden, 36 cl::desc("Maximum factor for an interleaved access group (default = 8)"), 37 cl::init(8)); 38 39 /// Return true if all of the intrinsic's arguments and return type are scalars 40 /// for the scalar form of the intrinsic and vectors for the vector form of the 41 /// intrinsic. 42 bool llvm::isTriviallyVectorizable(Intrinsic::ID ID) { 43 switch (ID) { 44 case Intrinsic::bswap: // Begin integer bit-manipulation. 45 case Intrinsic::bitreverse: 46 case Intrinsic::ctpop: 47 case Intrinsic::ctlz: 48 case Intrinsic::cttz: 49 case Intrinsic::fshl: 50 case Intrinsic::fshr: 51 case Intrinsic::sadd_sat: 52 case Intrinsic::ssub_sat: 53 case Intrinsic::uadd_sat: 54 case Intrinsic::usub_sat: 55 case Intrinsic::smul_fix: 56 case Intrinsic::umul_fix: 57 case Intrinsic::sqrt: // Begin floating-point. 58 case Intrinsic::sin: 59 case Intrinsic::cos: 60 case Intrinsic::exp: 61 case Intrinsic::exp2: 62 case Intrinsic::log: 63 case Intrinsic::log10: 64 case Intrinsic::log2: 65 case Intrinsic::fabs: 66 case Intrinsic::minnum: 67 case Intrinsic::maxnum: 68 case Intrinsic::minimum: 69 case Intrinsic::maximum: 70 case Intrinsic::copysign: 71 case Intrinsic::floor: 72 case Intrinsic::ceil: 73 case Intrinsic::trunc: 74 case Intrinsic::rint: 75 case Intrinsic::nearbyint: 76 case Intrinsic::round: 77 case Intrinsic::pow: 78 case Intrinsic::fma: 79 case Intrinsic::fmuladd: 80 case Intrinsic::powi: 81 case Intrinsic::canonicalize: 82 return true; 83 default: 84 return false; 85 } 86 } 87 88 /// Identifies if the intrinsic has a scalar operand. It check for 89 /// ctlz,cttz and powi special intrinsics whose argument is scalar. 90 bool llvm::hasVectorInstrinsicScalarOpd(Intrinsic::ID ID, 91 unsigned ScalarOpdIdx) { 92 switch (ID) { 93 case Intrinsic::ctlz: 94 case Intrinsic::cttz: 95 case Intrinsic::powi: 96 return (ScalarOpdIdx == 1); 97 case Intrinsic::smul_fix: 98 case Intrinsic::umul_fix: 99 return (ScalarOpdIdx == 2); 100 default: 101 return false; 102 } 103 } 104 105 /// Returns intrinsic ID for call. 106 /// For the input call instruction it finds mapping intrinsic and returns 107 /// its ID, in case it does not found it return not_intrinsic. 108 Intrinsic::ID llvm::getVectorIntrinsicIDForCall(const CallInst *CI, 109 const TargetLibraryInfo *TLI) { 110 Intrinsic::ID ID = getIntrinsicForCallSite(CI, TLI); 111 if (ID == Intrinsic::not_intrinsic) 112 return Intrinsic::not_intrinsic; 113 114 if (isTriviallyVectorizable(ID) || ID == Intrinsic::lifetime_start || 115 ID == Intrinsic::lifetime_end || ID == Intrinsic::assume || 116 ID == Intrinsic::sideeffect) 117 return ID; 118 return Intrinsic::not_intrinsic; 119 } 120 121 /// Find the operand of the GEP that should be checked for consecutive 122 /// stores. This ignores trailing indices that have no effect on the final 123 /// pointer. 124 unsigned llvm::getGEPInductionOperand(const GetElementPtrInst *Gep) { 125 const DataLayout &DL = Gep->getModule()->getDataLayout(); 126 unsigned LastOperand = Gep->getNumOperands() - 1; 127 unsigned GEPAllocSize = DL.getTypeAllocSize(Gep->getResultElementType()); 128 129 // Walk backwards and try to peel off zeros. 130 while (LastOperand > 1 && match(Gep->getOperand(LastOperand), m_Zero())) { 131 // Find the type we're currently indexing into. 132 gep_type_iterator GEPTI = gep_type_begin(Gep); 133 std::advance(GEPTI, LastOperand - 2); 134 135 // If it's a type with the same allocation size as the result of the GEP we 136 // can peel off the zero index. 137 if (DL.getTypeAllocSize(GEPTI.getIndexedType()) != GEPAllocSize) 138 break; 139 --LastOperand; 140 } 141 142 return LastOperand; 143 } 144 145 /// If the argument is a GEP, then returns the operand identified by 146 /// getGEPInductionOperand. However, if there is some other non-loop-invariant 147 /// operand, it returns that instead. 148 Value *llvm::stripGetElementPtr(Value *Ptr, ScalarEvolution *SE, Loop *Lp) { 149 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr); 150 if (!GEP) 151 return Ptr; 152 153 unsigned InductionOperand = getGEPInductionOperand(GEP); 154 155 // Check that all of the gep indices are uniform except for our induction 156 // operand. 157 for (unsigned i = 0, e = GEP->getNumOperands(); i != e; ++i) 158 if (i != InductionOperand && 159 !SE->isLoopInvariant(SE->getSCEV(GEP->getOperand(i)), Lp)) 160 return Ptr; 161 return GEP->getOperand(InductionOperand); 162 } 163 164 /// If a value has only one user that is a CastInst, return it. 165 Value *llvm::getUniqueCastUse(Value *Ptr, Loop *Lp, Type *Ty) { 166 Value *UniqueCast = nullptr; 167 for (User *U : Ptr->users()) { 168 CastInst *CI = dyn_cast<CastInst>(U); 169 if (CI && CI->getType() == Ty) { 170 if (!UniqueCast) 171 UniqueCast = CI; 172 else 173 return nullptr; 174 } 175 } 176 return UniqueCast; 177 } 178 179 /// Get the stride of a pointer access in a loop. Looks for symbolic 180 /// strides "a[i*stride]". Returns the symbolic stride, or null otherwise. 181 Value *llvm::getStrideFromPointer(Value *Ptr, ScalarEvolution *SE, Loop *Lp) { 182 auto *PtrTy = dyn_cast<PointerType>(Ptr->getType()); 183 if (!PtrTy || PtrTy->isAggregateType()) 184 return nullptr; 185 186 // Try to remove a gep instruction to make the pointer (actually index at this 187 // point) easier analyzable. If OrigPtr is equal to Ptr we are analyzing the 188 // pointer, otherwise, we are analyzing the index. 189 Value *OrigPtr = Ptr; 190 191 // The size of the pointer access. 192 int64_t PtrAccessSize = 1; 193 194 Ptr = stripGetElementPtr(Ptr, SE, Lp); 195 const SCEV *V = SE->getSCEV(Ptr); 196 197 if (Ptr != OrigPtr) 198 // Strip off casts. 199 while (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) 200 V = C->getOperand(); 201 202 const SCEVAddRecExpr *S = dyn_cast<SCEVAddRecExpr>(V); 203 if (!S) 204 return nullptr; 205 206 V = S->getStepRecurrence(*SE); 207 if (!V) 208 return nullptr; 209 210 // Strip off the size of access multiplication if we are still analyzing the 211 // pointer. 212 if (OrigPtr == Ptr) { 213 if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(V)) { 214 if (M->getOperand(0)->getSCEVType() != scConstant) 215 return nullptr; 216 217 const APInt &APStepVal = cast<SCEVConstant>(M->getOperand(0))->getAPInt(); 218 219 // Huge step value - give up. 220 if (APStepVal.getBitWidth() > 64) 221 return nullptr; 222 223 int64_t StepVal = APStepVal.getSExtValue(); 224 if (PtrAccessSize != StepVal) 225 return nullptr; 226 V = M->getOperand(1); 227 } 228 } 229 230 // Strip off casts. 231 Type *StripedOffRecurrenceCast = nullptr; 232 if (const SCEVCastExpr *C = dyn_cast<SCEVCastExpr>(V)) { 233 StripedOffRecurrenceCast = C->getType(); 234 V = C->getOperand(); 235 } 236 237 // Look for the loop invariant symbolic value. 238 const SCEVUnknown *U = dyn_cast<SCEVUnknown>(V); 239 if (!U) 240 return nullptr; 241 242 Value *Stride = U->getValue(); 243 if (!Lp->isLoopInvariant(Stride)) 244 return nullptr; 245 246 // If we have stripped off the recurrence cast we have to make sure that we 247 // return the value that is used in this loop so that we can replace it later. 248 if (StripedOffRecurrenceCast) 249 Stride = getUniqueCastUse(Stride, Lp, StripedOffRecurrenceCast); 250 251 return Stride; 252 } 253 254 /// Given a vector and an element number, see if the scalar value is 255 /// already around as a register, for example if it were inserted then extracted 256 /// from the vector. 257 Value *llvm::findScalarElement(Value *V, unsigned EltNo) { 258 assert(V->getType()->isVectorTy() && "Not looking at a vector?"); 259 VectorType *VTy = cast<VectorType>(V->getType()); 260 unsigned Width = VTy->getNumElements(); 261 if (EltNo >= Width) // Out of range access. 262 return UndefValue::get(VTy->getElementType()); 263 264 if (Constant *C = dyn_cast<Constant>(V)) 265 return C->getAggregateElement(EltNo); 266 267 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { 268 // If this is an insert to a variable element, we don't know what it is. 269 if (!isa<ConstantInt>(III->getOperand(2))) 270 return nullptr; 271 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); 272 273 // If this is an insert to the element we are looking for, return the 274 // inserted value. 275 if (EltNo == IIElt) 276 return III->getOperand(1); 277 278 // Otherwise, the insertelement doesn't modify the value, recurse on its 279 // vector input. 280 return findScalarElement(III->getOperand(0), EltNo); 281 } 282 283 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { 284 unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements(); 285 int InEl = SVI->getMaskValue(EltNo); 286 if (InEl < 0) 287 return UndefValue::get(VTy->getElementType()); 288 if (InEl < (int)LHSWidth) 289 return findScalarElement(SVI->getOperand(0), InEl); 290 return findScalarElement(SVI->getOperand(1), InEl - LHSWidth); 291 } 292 293 // Extract a value from a vector add operation with a constant zero. 294 // TODO: Use getBinOpIdentity() to generalize this. 295 Value *Val; Constant *C; 296 if (match(V, m_Add(m_Value(Val), m_Constant(C)))) 297 if (Constant *Elt = C->getAggregateElement(EltNo)) 298 if (Elt->isNullValue()) 299 return findScalarElement(Val, EltNo); 300 301 // Otherwise, we don't know. 302 return nullptr; 303 } 304 305 /// Get splat value if the input is a splat vector or return nullptr. 306 /// This function is not fully general. It checks only 2 cases: 307 /// the input value is (1) a splat constants vector or (2) a sequence 308 /// of instructions that broadcast a single value into a vector. 309 /// 310 const llvm::Value *llvm::getSplatValue(const Value *V) { 311 312 if (auto *C = dyn_cast<Constant>(V)) 313 if (isa<VectorType>(V->getType())) 314 return C->getSplatValue(); 315 316 auto *ShuffleInst = dyn_cast<ShuffleVectorInst>(V); 317 if (!ShuffleInst) 318 return nullptr; 319 // All-zero (or undef) shuffle mask elements. 320 for (int MaskElt : ShuffleInst->getShuffleMask()) 321 if (MaskElt != 0 && MaskElt != -1) 322 return nullptr; 323 // The first shuffle source is 'insertelement' with index 0. 324 auto *InsertEltInst = 325 dyn_cast<InsertElementInst>(ShuffleInst->getOperand(0)); 326 if (!InsertEltInst || !isa<ConstantInt>(InsertEltInst->getOperand(2)) || 327 !cast<ConstantInt>(InsertEltInst->getOperand(2))->isZero()) 328 return nullptr; 329 330 return InsertEltInst->getOperand(1); 331 } 332 333 MapVector<Instruction *, uint64_t> 334 llvm::computeMinimumValueSizes(ArrayRef<BasicBlock *> Blocks, DemandedBits &DB, 335 const TargetTransformInfo *TTI) { 336 337 // DemandedBits will give us every value's live-out bits. But we want 338 // to ensure no extra casts would need to be inserted, so every DAG 339 // of connected values must have the same minimum bitwidth. 340 EquivalenceClasses<Value *> ECs; 341 SmallVector<Value *, 16> Worklist; 342 SmallPtrSet<Value *, 4> Roots; 343 SmallPtrSet<Value *, 16> Visited; 344 DenseMap<Value *, uint64_t> DBits; 345 SmallPtrSet<Instruction *, 4> InstructionSet; 346 MapVector<Instruction *, uint64_t> MinBWs; 347 348 // Determine the roots. We work bottom-up, from truncs or icmps. 349 bool SeenExtFromIllegalType = false; 350 for (auto *BB : Blocks) 351 for (auto &I : *BB) { 352 InstructionSet.insert(&I); 353 354 if (TTI && (isa<ZExtInst>(&I) || isa<SExtInst>(&I)) && 355 !TTI->isTypeLegal(I.getOperand(0)->getType())) 356 SeenExtFromIllegalType = true; 357 358 // Only deal with non-vector integers up to 64-bits wide. 359 if ((isa<TruncInst>(&I) || isa<ICmpInst>(&I)) && 360 !I.getType()->isVectorTy() && 361 I.getOperand(0)->getType()->getScalarSizeInBits() <= 64) { 362 // Don't make work for ourselves. If we know the loaded type is legal, 363 // don't add it to the worklist. 364 if (TTI && isa<TruncInst>(&I) && TTI->isTypeLegal(I.getType())) 365 continue; 366 367 Worklist.push_back(&I); 368 Roots.insert(&I); 369 } 370 } 371 // Early exit. 372 if (Worklist.empty() || (TTI && !SeenExtFromIllegalType)) 373 return MinBWs; 374 375 // Now proceed breadth-first, unioning values together. 376 while (!Worklist.empty()) { 377 Value *Val = Worklist.pop_back_val(); 378 Value *Leader = ECs.getOrInsertLeaderValue(Val); 379 380 if (Visited.count(Val)) 381 continue; 382 Visited.insert(Val); 383 384 // Non-instructions terminate a chain successfully. 385 if (!isa<Instruction>(Val)) 386 continue; 387 Instruction *I = cast<Instruction>(Val); 388 389 // If we encounter a type that is larger than 64 bits, we can't represent 390 // it so bail out. 391 if (DB.getDemandedBits(I).getBitWidth() > 64) 392 return MapVector<Instruction *, uint64_t>(); 393 394 uint64_t V = DB.getDemandedBits(I).getZExtValue(); 395 DBits[Leader] |= V; 396 DBits[I] = V; 397 398 // Casts, loads and instructions outside of our range terminate a chain 399 // successfully. 400 if (isa<SExtInst>(I) || isa<ZExtInst>(I) || isa<LoadInst>(I) || 401 !InstructionSet.count(I)) 402 continue; 403 404 // Unsafe casts terminate a chain unsuccessfully. We can't do anything 405 // useful with bitcasts, ptrtoints or inttoptrs and it'd be unsafe to 406 // transform anything that relies on them. 407 if (isa<BitCastInst>(I) || isa<PtrToIntInst>(I) || isa<IntToPtrInst>(I) || 408 !I->getType()->isIntegerTy()) { 409 DBits[Leader] |= ~0ULL; 410 continue; 411 } 412 413 // We don't modify the types of PHIs. Reductions will already have been 414 // truncated if possible, and inductions' sizes will have been chosen by 415 // indvars. 416 if (isa<PHINode>(I)) 417 continue; 418 419 if (DBits[Leader] == ~0ULL) 420 // All bits demanded, no point continuing. 421 continue; 422 423 for (Value *O : cast<User>(I)->operands()) { 424 ECs.unionSets(Leader, O); 425 Worklist.push_back(O); 426 } 427 } 428 429 // Now we've discovered all values, walk them to see if there are 430 // any users we didn't see. If there are, we can't optimize that 431 // chain. 432 for (auto &I : DBits) 433 for (auto *U : I.first->users()) 434 if (U->getType()->isIntegerTy() && DBits.count(U) == 0) 435 DBits[ECs.getOrInsertLeaderValue(I.first)] |= ~0ULL; 436 437 for (auto I = ECs.begin(), E = ECs.end(); I != E; ++I) { 438 uint64_t LeaderDemandedBits = 0; 439 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) 440 LeaderDemandedBits |= DBits[*MI]; 441 442 uint64_t MinBW = (sizeof(LeaderDemandedBits) * 8) - 443 llvm::countLeadingZeros(LeaderDemandedBits); 444 // Round up to a power of 2 445 if (!isPowerOf2_64((uint64_t)MinBW)) 446 MinBW = NextPowerOf2(MinBW); 447 448 // We don't modify the types of PHIs. Reductions will already have been 449 // truncated if possible, and inductions' sizes will have been chosen by 450 // indvars. 451 // If we are required to shrink a PHI, abandon this entire equivalence class. 452 bool Abort = false; 453 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) 454 if (isa<PHINode>(*MI) && MinBW < (*MI)->getType()->getScalarSizeInBits()) { 455 Abort = true; 456 break; 457 } 458 if (Abort) 459 continue; 460 461 for (auto MI = ECs.member_begin(I), ME = ECs.member_end(); MI != ME; ++MI) { 462 if (!isa<Instruction>(*MI)) 463 continue; 464 Type *Ty = (*MI)->getType(); 465 if (Roots.count(*MI)) 466 Ty = cast<Instruction>(*MI)->getOperand(0)->getType(); 467 if (MinBW < Ty->getScalarSizeInBits()) 468 MinBWs[cast<Instruction>(*MI)] = MinBW; 469 } 470 } 471 472 return MinBWs; 473 } 474 475 /// Add all access groups in @p AccGroups to @p List. 476 template <typename ListT> 477 static void addToAccessGroupList(ListT &List, MDNode *AccGroups) { 478 // Interpret an access group as a list containing itself. 479 if (AccGroups->getNumOperands() == 0) { 480 assert(isValidAsAccessGroup(AccGroups) && "Node must be an access group"); 481 List.insert(AccGroups); 482 return; 483 } 484 485 for (auto &AccGroupListOp : AccGroups->operands()) { 486 auto *Item = cast<MDNode>(AccGroupListOp.get()); 487 assert(isValidAsAccessGroup(Item) && "List item must be an access group"); 488 List.insert(Item); 489 } 490 } 491 492 MDNode *llvm::uniteAccessGroups(MDNode *AccGroups1, MDNode *AccGroups2) { 493 if (!AccGroups1) 494 return AccGroups2; 495 if (!AccGroups2) 496 return AccGroups1; 497 if (AccGroups1 == AccGroups2) 498 return AccGroups1; 499 500 SmallSetVector<Metadata *, 4> Union; 501 addToAccessGroupList(Union, AccGroups1); 502 addToAccessGroupList(Union, AccGroups2); 503 504 if (Union.size() == 0) 505 return nullptr; 506 if (Union.size() == 1) 507 return cast<MDNode>(Union.front()); 508 509 LLVMContext &Ctx = AccGroups1->getContext(); 510 return MDNode::get(Ctx, Union.getArrayRef()); 511 } 512 513 MDNode *llvm::intersectAccessGroups(const Instruction *Inst1, 514 const Instruction *Inst2) { 515 bool MayAccessMem1 = Inst1->mayReadOrWriteMemory(); 516 bool MayAccessMem2 = Inst2->mayReadOrWriteMemory(); 517 518 if (!MayAccessMem1 && !MayAccessMem2) 519 return nullptr; 520 if (!MayAccessMem1) 521 return Inst2->getMetadata(LLVMContext::MD_access_group); 522 if (!MayAccessMem2) 523 return Inst1->getMetadata(LLVMContext::MD_access_group); 524 525 MDNode *MD1 = Inst1->getMetadata(LLVMContext::MD_access_group); 526 MDNode *MD2 = Inst2->getMetadata(LLVMContext::MD_access_group); 527 if (!MD1 || !MD2) 528 return nullptr; 529 if (MD1 == MD2) 530 return MD1; 531 532 // Use set for scalable 'contains' check. 533 SmallPtrSet<Metadata *, 4> AccGroupSet2; 534 addToAccessGroupList(AccGroupSet2, MD2); 535 536 SmallVector<Metadata *, 4> Intersection; 537 if (MD1->getNumOperands() == 0) { 538 assert(isValidAsAccessGroup(MD1) && "Node must be an access group"); 539 if (AccGroupSet2.count(MD1)) 540 Intersection.push_back(MD1); 541 } else { 542 for (const MDOperand &Node : MD1->operands()) { 543 auto *Item = cast<MDNode>(Node.get()); 544 assert(isValidAsAccessGroup(Item) && "List item must be an access group"); 545 if (AccGroupSet2.count(Item)) 546 Intersection.push_back(Item); 547 } 548 } 549 550 if (Intersection.size() == 0) 551 return nullptr; 552 if (Intersection.size() == 1) 553 return cast<MDNode>(Intersection.front()); 554 555 LLVMContext &Ctx = Inst1->getContext(); 556 return MDNode::get(Ctx, Intersection); 557 } 558 559 /// \returns \p I after propagating metadata from \p VL. 560 Instruction *llvm::propagateMetadata(Instruction *Inst, ArrayRef<Value *> VL) { 561 Instruction *I0 = cast<Instruction>(VL[0]); 562 SmallVector<std::pair<unsigned, MDNode *>, 4> Metadata; 563 I0->getAllMetadataOtherThanDebugLoc(Metadata); 564 565 for (auto Kind : {LLVMContext::MD_tbaa, LLVMContext::MD_alias_scope, 566 LLVMContext::MD_noalias, LLVMContext::MD_fpmath, 567 LLVMContext::MD_nontemporal, LLVMContext::MD_invariant_load, 568 LLVMContext::MD_access_group}) { 569 MDNode *MD = I0->getMetadata(Kind); 570 571 for (int J = 1, E = VL.size(); MD && J != E; ++J) { 572 const Instruction *IJ = cast<Instruction>(VL[J]); 573 MDNode *IMD = IJ->getMetadata(Kind); 574 switch (Kind) { 575 case LLVMContext::MD_tbaa: 576 MD = MDNode::getMostGenericTBAA(MD, IMD); 577 break; 578 case LLVMContext::MD_alias_scope: 579 MD = MDNode::getMostGenericAliasScope(MD, IMD); 580 break; 581 case LLVMContext::MD_fpmath: 582 MD = MDNode::getMostGenericFPMath(MD, IMD); 583 break; 584 case LLVMContext::MD_noalias: 585 case LLVMContext::MD_nontemporal: 586 case LLVMContext::MD_invariant_load: 587 MD = MDNode::intersect(MD, IMD); 588 break; 589 case LLVMContext::MD_access_group: 590 MD = intersectAccessGroups(Inst, IJ); 591 break; 592 default: 593 llvm_unreachable("unhandled metadata"); 594 } 595 } 596 597 Inst->setMetadata(Kind, MD); 598 } 599 600 return Inst; 601 } 602 603 Constant * 604 llvm::createBitMaskForGaps(IRBuilder<> &Builder, unsigned VF, 605 const InterleaveGroup<Instruction> &Group) { 606 // All 1's means mask is not needed. 607 if (Group.getNumMembers() == Group.getFactor()) 608 return nullptr; 609 610 // TODO: support reversed access. 611 assert(!Group.isReverse() && "Reversed group not supported."); 612 613 SmallVector<Constant *, 16> Mask; 614 for (unsigned i = 0; i < VF; i++) 615 for (unsigned j = 0; j < Group.getFactor(); ++j) { 616 unsigned HasMember = Group.getMember(j) ? 1 : 0; 617 Mask.push_back(Builder.getInt1(HasMember)); 618 } 619 620 return ConstantVector::get(Mask); 621 } 622 623 Constant *llvm::createReplicatedMask(IRBuilder<> &Builder, 624 unsigned ReplicationFactor, unsigned VF) { 625 SmallVector<Constant *, 16> MaskVec; 626 for (unsigned i = 0; i < VF; i++) 627 for (unsigned j = 0; j < ReplicationFactor; j++) 628 MaskVec.push_back(Builder.getInt32(i)); 629 630 return ConstantVector::get(MaskVec); 631 } 632 633 Constant *llvm::createInterleaveMask(IRBuilder<> &Builder, unsigned VF, 634 unsigned NumVecs) { 635 SmallVector<Constant *, 16> Mask; 636 for (unsigned i = 0; i < VF; i++) 637 for (unsigned j = 0; j < NumVecs; j++) 638 Mask.push_back(Builder.getInt32(j * VF + i)); 639 640 return ConstantVector::get(Mask); 641 } 642 643 Constant *llvm::createStrideMask(IRBuilder<> &Builder, unsigned Start, 644 unsigned Stride, unsigned VF) { 645 SmallVector<Constant *, 16> Mask; 646 for (unsigned i = 0; i < VF; i++) 647 Mask.push_back(Builder.getInt32(Start + i * Stride)); 648 649 return ConstantVector::get(Mask); 650 } 651 652 Constant *llvm::createSequentialMask(IRBuilder<> &Builder, unsigned Start, 653 unsigned NumInts, unsigned NumUndefs) { 654 SmallVector<Constant *, 16> Mask; 655 for (unsigned i = 0; i < NumInts; i++) 656 Mask.push_back(Builder.getInt32(Start + i)); 657 658 Constant *Undef = UndefValue::get(Builder.getInt32Ty()); 659 for (unsigned i = 0; i < NumUndefs; i++) 660 Mask.push_back(Undef); 661 662 return ConstantVector::get(Mask); 663 } 664 665 /// A helper function for concatenating vectors. This function concatenates two 666 /// vectors having the same element type. If the second vector has fewer 667 /// elements than the first, it is padded with undefs. 668 static Value *concatenateTwoVectors(IRBuilder<> &Builder, Value *V1, 669 Value *V2) { 670 VectorType *VecTy1 = dyn_cast<VectorType>(V1->getType()); 671 VectorType *VecTy2 = dyn_cast<VectorType>(V2->getType()); 672 assert(VecTy1 && VecTy2 && 673 VecTy1->getScalarType() == VecTy2->getScalarType() && 674 "Expect two vectors with the same element type"); 675 676 unsigned NumElts1 = VecTy1->getNumElements(); 677 unsigned NumElts2 = VecTy2->getNumElements(); 678 assert(NumElts1 >= NumElts2 && "Unexpect the first vector has less elements"); 679 680 if (NumElts1 > NumElts2) { 681 // Extend with UNDEFs. 682 Constant *ExtMask = 683 createSequentialMask(Builder, 0, NumElts2, NumElts1 - NumElts2); 684 V2 = Builder.CreateShuffleVector(V2, UndefValue::get(VecTy2), ExtMask); 685 } 686 687 Constant *Mask = createSequentialMask(Builder, 0, NumElts1 + NumElts2, 0); 688 return Builder.CreateShuffleVector(V1, V2, Mask); 689 } 690 691 Value *llvm::concatenateVectors(IRBuilder<> &Builder, ArrayRef<Value *> Vecs) { 692 unsigned NumVecs = Vecs.size(); 693 assert(NumVecs > 1 && "Should be at least two vectors"); 694 695 SmallVector<Value *, 8> ResList; 696 ResList.append(Vecs.begin(), Vecs.end()); 697 do { 698 SmallVector<Value *, 8> TmpList; 699 for (unsigned i = 0; i < NumVecs - 1; i += 2) { 700 Value *V0 = ResList[i], *V1 = ResList[i + 1]; 701 assert((V0->getType() == V1->getType() || i == NumVecs - 2) && 702 "Only the last vector may have a different type"); 703 704 TmpList.push_back(concatenateTwoVectors(Builder, V0, V1)); 705 } 706 707 // Push the last vector if the total number of vectors is odd. 708 if (NumVecs % 2 != 0) 709 TmpList.push_back(ResList[NumVecs - 1]); 710 711 ResList = TmpList; 712 NumVecs = ResList.size(); 713 } while (NumVecs > 1); 714 715 return ResList[0]; 716 } 717 718 bool InterleavedAccessInfo::isStrided(int Stride) { 719 unsigned Factor = std::abs(Stride); 720 return Factor >= 2 && Factor <= MaxInterleaveGroupFactor; 721 } 722 723 void InterleavedAccessInfo::collectConstStrideAccesses( 724 MapVector<Instruction *, StrideDescriptor> &AccessStrideInfo, 725 const ValueToValueMap &Strides) { 726 auto &DL = TheLoop->getHeader()->getModule()->getDataLayout(); 727 728 // Since it's desired that the load/store instructions be maintained in 729 // "program order" for the interleaved access analysis, we have to visit the 730 // blocks in the loop in reverse postorder (i.e., in a topological order). 731 // Such an ordering will ensure that any load/store that may be executed 732 // before a second load/store will precede the second load/store in 733 // AccessStrideInfo. 734 LoopBlocksDFS DFS(TheLoop); 735 DFS.perform(LI); 736 for (BasicBlock *BB : make_range(DFS.beginRPO(), DFS.endRPO())) 737 for (auto &I : *BB) { 738 auto *LI = dyn_cast<LoadInst>(&I); 739 auto *SI = dyn_cast<StoreInst>(&I); 740 if (!LI && !SI) 741 continue; 742 743 Value *Ptr = getLoadStorePointerOperand(&I); 744 // We don't check wrapping here because we don't know yet if Ptr will be 745 // part of a full group or a group with gaps. Checking wrapping for all 746 // pointers (even those that end up in groups with no gaps) will be overly 747 // conservative. For full groups, wrapping should be ok since if we would 748 // wrap around the address space we would do a memory access at nullptr 749 // even without the transformation. The wrapping checks are therefore 750 // deferred until after we've formed the interleaved groups. 751 int64_t Stride = getPtrStride(PSE, Ptr, TheLoop, Strides, 752 /*Assume=*/true, /*ShouldCheckWrap=*/false); 753 754 const SCEV *Scev = replaceSymbolicStrideSCEV(PSE, Strides, Ptr); 755 PointerType *PtrTy = dyn_cast<PointerType>(Ptr->getType()); 756 uint64_t Size = DL.getTypeAllocSize(PtrTy->getElementType()); 757 758 // An alignment of 0 means target ABI alignment. 759 unsigned Align = getLoadStoreAlignment(&I); 760 if (!Align) 761 Align = DL.getABITypeAlignment(PtrTy->getElementType()); 762 763 AccessStrideInfo[&I] = StrideDescriptor(Stride, Scev, Size, Align); 764 } 765 } 766 767 // Analyze interleaved accesses and collect them into interleaved load and 768 // store groups. 769 // 770 // When generating code for an interleaved load group, we effectively hoist all 771 // loads in the group to the location of the first load in program order. When 772 // generating code for an interleaved store group, we sink all stores to the 773 // location of the last store. This code motion can change the order of load 774 // and store instructions and may break dependences. 775 // 776 // The code generation strategy mentioned above ensures that we won't violate 777 // any write-after-read (WAR) dependences. 778 // 779 // E.g., for the WAR dependence: a = A[i]; // (1) 780 // A[i] = b; // (2) 781 // 782 // The store group of (2) is always inserted at or below (2), and the load 783 // group of (1) is always inserted at or above (1). Thus, the instructions will 784 // never be reordered. All other dependences are checked to ensure the 785 // correctness of the instruction reordering. 786 // 787 // The algorithm visits all memory accesses in the loop in bottom-up program 788 // order. Program order is established by traversing the blocks in the loop in 789 // reverse postorder when collecting the accesses. 790 // 791 // We visit the memory accesses in bottom-up order because it can simplify the 792 // construction of store groups in the presence of write-after-write (WAW) 793 // dependences. 794 // 795 // E.g., for the WAW dependence: A[i] = a; // (1) 796 // A[i] = b; // (2) 797 // A[i + 1] = c; // (3) 798 // 799 // We will first create a store group with (3) and (2). (1) can't be added to 800 // this group because it and (2) are dependent. However, (1) can be grouped 801 // with other accesses that may precede it in program order. Note that a 802 // bottom-up order does not imply that WAW dependences should not be checked. 803 void InterleavedAccessInfo::analyzeInterleaving( 804 bool EnablePredicatedInterleavedMemAccesses) { 805 LLVM_DEBUG(dbgs() << "LV: Analyzing interleaved accesses...\n"); 806 const ValueToValueMap &Strides = LAI->getSymbolicStrides(); 807 808 // Holds all accesses with a constant stride. 809 MapVector<Instruction *, StrideDescriptor> AccessStrideInfo; 810 collectConstStrideAccesses(AccessStrideInfo, Strides); 811 812 if (AccessStrideInfo.empty()) 813 return; 814 815 // Collect the dependences in the loop. 816 collectDependences(); 817 818 // Holds all interleaved store groups temporarily. 819 SmallSetVector<InterleaveGroup<Instruction> *, 4> StoreGroups; 820 // Holds all interleaved load groups temporarily. 821 SmallSetVector<InterleaveGroup<Instruction> *, 4> LoadGroups; 822 823 // Search in bottom-up program order for pairs of accesses (A and B) that can 824 // form interleaved load or store groups. In the algorithm below, access A 825 // precedes access B in program order. We initialize a group for B in the 826 // outer loop of the algorithm, and then in the inner loop, we attempt to 827 // insert each A into B's group if: 828 // 829 // 1. A and B have the same stride, 830 // 2. A and B have the same memory object size, and 831 // 3. A belongs in B's group according to its distance from B. 832 // 833 // Special care is taken to ensure group formation will not break any 834 // dependences. 835 for (auto BI = AccessStrideInfo.rbegin(), E = AccessStrideInfo.rend(); 836 BI != E; ++BI) { 837 Instruction *B = BI->first; 838 StrideDescriptor DesB = BI->second; 839 840 // Initialize a group for B if it has an allowable stride. Even if we don't 841 // create a group for B, we continue with the bottom-up algorithm to ensure 842 // we don't break any of B's dependences. 843 InterleaveGroup<Instruction> *Group = nullptr; 844 if (isStrided(DesB.Stride) && 845 (!isPredicated(B->getParent()) || EnablePredicatedInterleavedMemAccesses)) { 846 Group = getInterleaveGroup(B); 847 if (!Group) { 848 LLVM_DEBUG(dbgs() << "LV: Creating an interleave group with:" << *B 849 << '\n'); 850 Group = createInterleaveGroup(B, DesB.Stride, DesB.Align); 851 } 852 if (B->mayWriteToMemory()) 853 StoreGroups.insert(Group); 854 else 855 LoadGroups.insert(Group); 856 } 857 858 for (auto AI = std::next(BI); AI != E; ++AI) { 859 Instruction *A = AI->first; 860 StrideDescriptor DesA = AI->second; 861 862 // Our code motion strategy implies that we can't have dependences 863 // between accesses in an interleaved group and other accesses located 864 // between the first and last member of the group. Note that this also 865 // means that a group can't have more than one member at a given offset. 866 // The accesses in a group can have dependences with other accesses, but 867 // we must ensure we don't extend the boundaries of the group such that 868 // we encompass those dependent accesses. 869 // 870 // For example, assume we have the sequence of accesses shown below in a 871 // stride-2 loop: 872 // 873 // (1, 2) is a group | A[i] = a; // (1) 874 // | A[i-1] = b; // (2) | 875 // A[i-3] = c; // (3) 876 // A[i] = d; // (4) | (2, 4) is not a group 877 // 878 // Because accesses (2) and (3) are dependent, we can group (2) with (1) 879 // but not with (4). If we did, the dependent access (3) would be within 880 // the boundaries of the (2, 4) group. 881 if (!canReorderMemAccessesForInterleavedGroups(&*AI, &*BI)) { 882 // If a dependence exists and A is already in a group, we know that A 883 // must be a store since A precedes B and WAR dependences are allowed. 884 // Thus, A would be sunk below B. We release A's group to prevent this 885 // illegal code motion. A will then be free to form another group with 886 // instructions that precede it. 887 if (isInterleaved(A)) { 888 InterleaveGroup<Instruction> *StoreGroup = getInterleaveGroup(A); 889 StoreGroups.remove(StoreGroup); 890 releaseGroup(StoreGroup); 891 } 892 893 // If a dependence exists and A is not already in a group (or it was 894 // and we just released it), B might be hoisted above A (if B is a 895 // load) or another store might be sunk below A (if B is a store). In 896 // either case, we can't add additional instructions to B's group. B 897 // will only form a group with instructions that it precedes. 898 break; 899 } 900 901 // At this point, we've checked for illegal code motion. If either A or B 902 // isn't strided, there's nothing left to do. 903 if (!isStrided(DesA.Stride) || !isStrided(DesB.Stride)) 904 continue; 905 906 // Ignore A if it's already in a group or isn't the same kind of memory 907 // operation as B. 908 // Note that mayReadFromMemory() isn't mutually exclusive to 909 // mayWriteToMemory in the case of atomic loads. We shouldn't see those 910 // here, canVectorizeMemory() should have returned false - except for the 911 // case we asked for optimization remarks. 912 if (isInterleaved(A) || 913 (A->mayReadFromMemory() != B->mayReadFromMemory()) || 914 (A->mayWriteToMemory() != B->mayWriteToMemory())) 915 continue; 916 917 // Check rules 1 and 2. Ignore A if its stride or size is different from 918 // that of B. 919 if (DesA.Stride != DesB.Stride || DesA.Size != DesB.Size) 920 continue; 921 922 // Ignore A if the memory object of A and B don't belong to the same 923 // address space 924 if (getLoadStoreAddressSpace(A) != getLoadStoreAddressSpace(B)) 925 continue; 926 927 // Calculate the distance from A to B. 928 const SCEVConstant *DistToB = dyn_cast<SCEVConstant>( 929 PSE.getSE()->getMinusSCEV(DesA.Scev, DesB.Scev)); 930 if (!DistToB) 931 continue; 932 int64_t DistanceToB = DistToB->getAPInt().getSExtValue(); 933 934 // Check rule 3. Ignore A if its distance to B is not a multiple of the 935 // size. 936 if (DistanceToB % static_cast<int64_t>(DesB.Size)) 937 continue; 938 939 // All members of a predicated interleave-group must have the same predicate, 940 // and currently must reside in the same BB. 941 BasicBlock *BlockA = A->getParent(); 942 BasicBlock *BlockB = B->getParent(); 943 if ((isPredicated(BlockA) || isPredicated(BlockB)) && 944 (!EnablePredicatedInterleavedMemAccesses || BlockA != BlockB)) 945 continue; 946 947 // The index of A is the index of B plus A's distance to B in multiples 948 // of the size. 949 int IndexA = 950 Group->getIndex(B) + DistanceToB / static_cast<int64_t>(DesB.Size); 951 952 // Try to insert A into B's group. 953 if (Group->insertMember(A, IndexA, DesA.Align)) { 954 LLVM_DEBUG(dbgs() << "LV: Inserted:" << *A << '\n' 955 << " into the interleave group with" << *B 956 << '\n'); 957 InterleaveGroupMap[A] = Group; 958 959 // Set the first load in program order as the insert position. 960 if (A->mayReadFromMemory()) 961 Group->setInsertPos(A); 962 } 963 } // Iteration over A accesses. 964 } // Iteration over B accesses. 965 966 // Remove interleaved store groups with gaps. 967 for (auto *Group : StoreGroups) 968 if (Group->getNumMembers() != Group->getFactor()) { 969 LLVM_DEBUG( 970 dbgs() << "LV: Invalidate candidate interleaved store group due " 971 "to gaps.\n"); 972 releaseGroup(Group); 973 } 974 // Remove interleaved groups with gaps (currently only loads) whose memory 975 // accesses may wrap around. We have to revisit the getPtrStride analysis, 976 // this time with ShouldCheckWrap=true, since collectConstStrideAccesses does 977 // not check wrapping (see documentation there). 978 // FORNOW we use Assume=false; 979 // TODO: Change to Assume=true but making sure we don't exceed the threshold 980 // of runtime SCEV assumptions checks (thereby potentially failing to 981 // vectorize altogether). 982 // Additional optional optimizations: 983 // TODO: If we are peeling the loop and we know that the first pointer doesn't 984 // wrap then we can deduce that all pointers in the group don't wrap. 985 // This means that we can forcefully peel the loop in order to only have to 986 // check the first pointer for no-wrap. When we'll change to use Assume=true 987 // we'll only need at most one runtime check per interleaved group. 988 for (auto *Group : LoadGroups) { 989 // Case 1: A full group. Can Skip the checks; For full groups, if the wide 990 // load would wrap around the address space we would do a memory access at 991 // nullptr even without the transformation. 992 if (Group->getNumMembers() == Group->getFactor()) 993 continue; 994 995 // Case 2: If first and last members of the group don't wrap this implies 996 // that all the pointers in the group don't wrap. 997 // So we check only group member 0 (which is always guaranteed to exist), 998 // and group member Factor - 1; If the latter doesn't exist we rely on 999 // peeling (if it is a non-reversed accsess -- see Case 3). 1000 Value *FirstMemberPtr = getLoadStorePointerOperand(Group->getMember(0)); 1001 if (!getPtrStride(PSE, FirstMemberPtr, TheLoop, Strides, /*Assume=*/false, 1002 /*ShouldCheckWrap=*/true)) { 1003 LLVM_DEBUG( 1004 dbgs() << "LV: Invalidate candidate interleaved group due to " 1005 "first group member potentially pointer-wrapping.\n"); 1006 releaseGroup(Group); 1007 continue; 1008 } 1009 Instruction *LastMember = Group->getMember(Group->getFactor() - 1); 1010 if (LastMember) { 1011 Value *LastMemberPtr = getLoadStorePointerOperand(LastMember); 1012 if (!getPtrStride(PSE, LastMemberPtr, TheLoop, Strides, /*Assume=*/false, 1013 /*ShouldCheckWrap=*/true)) { 1014 LLVM_DEBUG( 1015 dbgs() << "LV: Invalidate candidate interleaved group due to " 1016 "last group member potentially pointer-wrapping.\n"); 1017 releaseGroup(Group); 1018 } 1019 } else { 1020 // Case 3: A non-reversed interleaved load group with gaps: We need 1021 // to execute at least one scalar epilogue iteration. This will ensure 1022 // we don't speculatively access memory out-of-bounds. We only need 1023 // to look for a member at index factor - 1, since every group must have 1024 // a member at index zero. 1025 if (Group->isReverse()) { 1026 LLVM_DEBUG( 1027 dbgs() << "LV: Invalidate candidate interleaved group due to " 1028 "a reverse access with gaps.\n"); 1029 releaseGroup(Group); 1030 continue; 1031 } 1032 LLVM_DEBUG( 1033 dbgs() << "LV: Interleaved group requires epilogue iteration.\n"); 1034 RequiresScalarEpilogue = true; 1035 } 1036 } 1037 } 1038 1039 void InterleavedAccessInfo::invalidateGroupsRequiringScalarEpilogue() { 1040 // If no group had triggered the requirement to create an epilogue loop, 1041 // there is nothing to do. 1042 if (!requiresScalarEpilogue()) 1043 return; 1044 1045 // Avoid releasing a Group twice. 1046 SmallPtrSet<InterleaveGroup<Instruction> *, 4> DelSet; 1047 for (auto &I : InterleaveGroupMap) { 1048 InterleaveGroup<Instruction> *Group = I.second; 1049 if (Group->requiresScalarEpilogue()) 1050 DelSet.insert(Group); 1051 } 1052 for (auto *Ptr : DelSet) { 1053 LLVM_DEBUG( 1054 dbgs() 1055 << "LV: Invalidate candidate interleaved group due to gaps that " 1056 "require a scalar epilogue (not allowed under optsize) and cannot " 1057 "be masked (not enabled). \n"); 1058 releaseGroup(Ptr); 1059 } 1060 1061 RequiresScalarEpilogue = false; 1062 } 1063 1064 template <typename InstT> 1065 void InterleaveGroup<InstT>::addMetadata(InstT *NewInst) const { 1066 llvm_unreachable("addMetadata can only be used for Instruction"); 1067 } 1068 1069 namespace llvm { 1070 template <> 1071 void InterleaveGroup<Instruction>::addMetadata(Instruction *NewInst) const { 1072 SmallVector<Value *, 4> VL; 1073 std::transform(Members.begin(), Members.end(), std::back_inserter(VL), 1074 [](std::pair<int, Instruction *> p) { return p.second; }); 1075 propagateMetadata(NewInst, VL); 1076 } 1077 } 1078