LLVM 19.0.0git
AMDGPUPromoteAlloca.cpp
Go to the documentation of this file.
1//===-- AMDGPUPromoteAlloca.cpp - Promote Allocas -------------------------===//
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// Eliminates allocas by either converting them into vectors or by migrating
10// them to local address space.
11//
12// Two passes are exposed by this file:
13// - "promote-alloca-to-vector", which runs early in the pipeline and only
14// promotes to vector. Promotion to vector is almost always profitable
15// except when the alloca is too big and the promotion would result in
16// very high register pressure.
17// - "promote-alloca", which does both promotion to vector and LDS and runs
18// much later in the pipeline. This runs after SROA because promoting to
19// LDS is of course less profitable than getting rid of the alloca or
20// vectorizing it, thus we only want to do it when the only alternative is
21// lowering the alloca to stack.
22//
23// Note that both of them exist for the old and new PMs. The new PM passes are
24// declared in AMDGPU.h and the legacy PM ones are declared here.s
25//
26//===----------------------------------------------------------------------===//
27
28#include "AMDGPU.h"
29#include "GCNSubtarget.h"
31#include "llvm/ADT/STLExtras.h"
37#include "llvm/IR/IRBuilder.h"
39#include "llvm/IR/IntrinsicsAMDGPU.h"
40#include "llvm/IR/IntrinsicsR600.h"
42#include "llvm/Pass.h"
45
46#define DEBUG_TYPE "amdgpu-promote-alloca"
47
48using namespace llvm;
49
50namespace {
51
52static cl::opt<bool>
53 DisablePromoteAllocaToVector("disable-promote-alloca-to-vector",
54 cl::desc("Disable promote alloca to vector"),
55 cl::init(false));
56
57static cl::opt<bool>
58 DisablePromoteAllocaToLDS("disable-promote-alloca-to-lds",
59 cl::desc("Disable promote alloca to LDS"),
60 cl::init(false));
61
62static cl::opt<unsigned> PromoteAllocaToVectorLimit(
63 "amdgpu-promote-alloca-to-vector-limit",
64 cl::desc("Maximum byte size to consider promote alloca to vector"),
65 cl::init(0));
66
67// Shared implementation which can do both promotion to vector and to LDS.
68class AMDGPUPromoteAllocaImpl {
69private:
70 const TargetMachine &TM;
71 Module *Mod = nullptr;
72 const DataLayout *DL = nullptr;
73
74 // FIXME: This should be per-kernel.
75 uint32_t LocalMemLimit = 0;
76 uint32_t CurrentLocalMemUsage = 0;
77 unsigned MaxVGPRs;
78
79 bool IsAMDGCN = false;
80 bool IsAMDHSA = false;
81
82 std::pair<Value *, Value *> getLocalSizeYZ(IRBuilder<> &Builder);
83 Value *getWorkitemID(IRBuilder<> &Builder, unsigned N);
84
85 /// BaseAlloca is the alloca root the search started from.
86 /// Val may be that alloca or a recursive user of it.
87 bool collectUsesWithPtrTypes(Value *BaseAlloca, Value *Val,
88 std::vector<Value *> &WorkList) const;
89
90 /// Val is a derived pointer from Alloca. OpIdx0/OpIdx1 are the operand
91 /// indices to an instruction with 2 pointer inputs (e.g. select, icmp).
92 /// Returns true if both operands are derived from the same alloca. Val should
93 /// be the same value as one of the input operands of UseInst.
94 bool binaryOpIsDerivedFromSameAlloca(Value *Alloca, Value *Val,
95 Instruction *UseInst, int OpIdx0,
96 int OpIdx1) const;
97
98 /// Check whether we have enough local memory for promotion.
99 bool hasSufficientLocalMem(const Function &F);
100
101 bool tryPromoteAllocaToVector(AllocaInst &I);
102 bool tryPromoteAllocaToLDS(AllocaInst &I, bool SufficientLDS);
103
104public:
105 AMDGPUPromoteAllocaImpl(TargetMachine &TM) : TM(TM) {
106 const Triple &TT = TM.getTargetTriple();
107 IsAMDGCN = TT.getArch() == Triple::amdgcn;
108 IsAMDHSA = TT.getOS() == Triple::AMDHSA;
109 }
110
111 bool run(Function &F, bool PromoteToLDS);
112};
113
114// FIXME: This can create globals so should be a module pass.
115class AMDGPUPromoteAlloca : public FunctionPass {
116public:
117 static char ID;
118
119 AMDGPUPromoteAlloca() : FunctionPass(ID) {}
120
121 bool runOnFunction(Function &F) override {
122 if (skipFunction(F))
123 return false;
124 if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>())
125 return AMDGPUPromoteAllocaImpl(TPC->getTM<TargetMachine>())
126 .run(F, /*PromoteToLDS*/ true);
127 return false;
128 }
129
130 StringRef getPassName() const override { return "AMDGPU Promote Alloca"; }
131
132 void getAnalysisUsage(AnalysisUsage &AU) const override {
133 AU.setPreservesCFG();
135 }
136};
137
138class AMDGPUPromoteAllocaToVector : public FunctionPass {
139public:
140 static char ID;
141
142 AMDGPUPromoteAllocaToVector() : FunctionPass(ID) {}
143
144 bool runOnFunction(Function &F) override {
145 if (skipFunction(F))
146 return false;
147 if (auto *TPC = getAnalysisIfAvailable<TargetPassConfig>())
148 return AMDGPUPromoteAllocaImpl(TPC->getTM<TargetMachine>())
149 .run(F, /*PromoteToLDS*/ false);
150 return false;
151 }
152
153 StringRef getPassName() const override {
154 return "AMDGPU Promote Alloca to vector";
155 }
156
157 void getAnalysisUsage(AnalysisUsage &AU) const override {
158 AU.setPreservesCFG();
160 }
161};
162
163unsigned getMaxVGPRs(const TargetMachine &TM, const Function &F) {
164 if (!TM.getTargetTriple().isAMDGCN())
165 return 128;
166
167 const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
168 unsigned MaxVGPRs = ST.getMaxNumVGPRs(ST.getWavesPerEU(F).first);
169
170 // A non-entry function has only 32 caller preserved registers.
171 // Do not promote alloca which will force spilling unless we know the function
172 // will be inlined.
173 if (!F.hasFnAttribute(Attribute::AlwaysInline) &&
174 !AMDGPU::isEntryFunctionCC(F.getCallingConv()))
175 MaxVGPRs = std::min(MaxVGPRs, 32u);
176 return MaxVGPRs;
177}
178
179} // end anonymous namespace
180
181char AMDGPUPromoteAlloca::ID = 0;
182char AMDGPUPromoteAllocaToVector::ID = 0;
183
185 "AMDGPU promote alloca to vector or LDS", false, false)
186// Move LDS uses from functions to kernels before promote alloca for accurate
187// estimation of LDS available
188INITIALIZE_PASS_DEPENDENCY(AMDGPULowerModuleLDSLegacy)
189INITIALIZE_PASS_END(AMDGPUPromoteAlloca, DEBUG_TYPE,
190 "AMDGPU promote alloca to vector or LDS", false, false)
191
192INITIALIZE_PASS(AMDGPUPromoteAllocaToVector, DEBUG_TYPE "-to-vector",
193 "AMDGPU promote alloca to vector", false, false)
194
195char &llvm::AMDGPUPromoteAllocaID = AMDGPUPromoteAlloca::ID;
196char &llvm::AMDGPUPromoteAllocaToVectorID = AMDGPUPromoteAllocaToVector::ID;
197
200 bool Changed = AMDGPUPromoteAllocaImpl(TM).run(F, /*PromoteToLDS*/ true);
201 if (Changed) {
204 return PA;
205 }
206 return PreservedAnalyses::all();
207}
208
211 bool Changed = AMDGPUPromoteAllocaImpl(TM).run(F, /*PromoteToLDS*/ false);
212 if (Changed) {
215 return PA;
216 }
217 return PreservedAnalyses::all();
218}
219
221 return new AMDGPUPromoteAlloca();
222}
223
225 return new AMDGPUPromoteAllocaToVector();
226}
227
228bool AMDGPUPromoteAllocaImpl::run(Function &F, bool PromoteToLDS) {
229 Mod = F.getParent();
230 DL = &Mod->getDataLayout();
231
233 if (!ST.isPromoteAllocaEnabled())
234 return false;
235
236 MaxVGPRs = getMaxVGPRs(TM, F);
237
238 bool SufficientLDS = PromoteToLDS ? hasSufficientLocalMem(F) : false;
239
241 for (Instruction &I : F.getEntryBlock()) {
242 if (AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
243 // Array allocations are probably not worth handling, since an allocation
244 // of the array type is the canonical form.
245 if (!AI->isStaticAlloca() || AI->isArrayAllocation())
246 continue;
247 Allocas.push_back(AI);
248 }
249 }
250
251 bool Changed = false;
252 for (AllocaInst *AI : Allocas) {
253 if (tryPromoteAllocaToVector(*AI))
254 Changed = true;
255 else if (PromoteToLDS && tryPromoteAllocaToLDS(*AI, SufficientLDS))
256 Changed = true;
257 }
258
259 // NOTE: tryPromoteAllocaToVector removes the alloca, so Allocas contains
260 // dangling pointers. If we want to reuse it past this point, the loop above
261 // would need to be updated to remove successfully promoted allocas.
262
263 return Changed;
264}
265
267 ConstantInt *SrcIndex = nullptr;
268 ConstantInt *DestIndex = nullptr;
269};
270
271// Checks if the instruction I is a memset user of the alloca AI that we can
272// deal with. Currently, only non-volatile memsets that affect the whole alloca
273// are handled.
275 const DataLayout &DL) {
276 using namespace PatternMatch;
277 // For now we only care about non-volatile memsets that affect the whole type
278 // (start at index 0 and fill the whole alloca).
279 //
280 // TODO: Now that we moved to PromoteAlloca we could handle any memsets
281 // (except maybe volatile ones?) - we just need to use shufflevector if it
282 // only affects a subset of the vector.
283 const unsigned Size = DL.getTypeStoreSize(AI->getAllocatedType());
284 return I->getOperand(0) == AI &&
285 match(I->getOperand(2), m_SpecificInt(Size)) && !I->isVolatile();
286}
287
288static Value *
290 const std::map<GetElementPtrInst *, Value *> &GEPIdx) {
291 auto *GEP = dyn_cast<GetElementPtrInst>(Ptr->stripPointerCasts());
292 if (!GEP)
293 return ConstantInt::getNullValue(Type::getInt32Ty(Ptr->getContext()));
294
295 auto I = GEPIdx.find(GEP);
296 assert(I != GEPIdx.end() && "Must have entry for GEP!");
297 return I->second;
298}
299
301 Type *VecElemTy, const DataLayout &DL) {
302 // TODO: Extracting a "multiple of X" from a GEP might be a useful generic
303 // helper.
304 unsigned BW = DL.getIndexTypeSizeInBits(GEP->getType());
305 MapVector<Value *, APInt> VarOffsets;
306 APInt ConstOffset(BW, 0);
307 if (GEP->getPointerOperand()->stripPointerCasts() != Alloca ||
308 !GEP->collectOffset(DL, BW, VarOffsets, ConstOffset))
309 return nullptr;
310
311 unsigned VecElemSize = DL.getTypeAllocSize(VecElemTy);
312 if (VarOffsets.size() > 1)
313 return nullptr;
314
315 if (VarOffsets.size() == 1) {
316 // Only handle cases where we don't need to insert extra arithmetic
317 // instructions.
318 const auto &VarOffset = VarOffsets.front();
319 if (!ConstOffset.isZero() || VarOffset.second != VecElemSize)
320 return nullptr;
321 return VarOffset.first;
322 }
323
324 APInt Quot;
325 uint64_t Rem;
326 APInt::udivrem(ConstOffset, VecElemSize, Quot, Rem);
327 if (Rem != 0)
328 return nullptr;
329
330 return ConstantInt::get(GEP->getContext(), Quot);
331}
332
333/// Promotes a single user of the alloca to a vector form.
334///
335/// \param Inst Instruction to be promoted.
336/// \param DL Module Data Layout.
337/// \param VectorTy Vectorized Type.
338/// \param VecStoreSize Size of \p VectorTy in bytes.
339/// \param ElementSize Size of \p VectorTy element type in bytes.
340/// \param TransferInfo MemTransferInst info map.
341/// \param GEPVectorIdx GEP -> VectorIdx cache.
342/// \param CurVal Current value of the vector (e.g. last stored value)
343/// \param[out] DeferredLoads \p Inst is added to this vector if it can't
344/// be promoted now. This happens when promoting requires \p
345/// CurVal, but \p CurVal is nullptr.
346/// \return the stored value if \p Inst would have written to the alloca, or
347/// nullptr otherwise.
349 Instruction *Inst, const DataLayout &DL, FixedVectorType *VectorTy,
350 unsigned VecStoreSize, unsigned ElementSize,
352 std::map<GetElementPtrInst *, Value *> &GEPVectorIdx, Value *CurVal,
353 SmallVectorImpl<LoadInst *> &DeferredLoads) {
354 // Note: we use InstSimplifyFolder because it can leverage the DataLayout
355 // to do more folding, especially in the case of vector splats.
358 Builder.SetInsertPoint(Inst);
359
360 const auto GetOrLoadCurrentVectorValue = [&]() -> Value * {
361 if (CurVal)
362 return CurVal;
363
364 // If the current value is not known, insert a dummy load and lower it on
365 // the second pass.
366 LoadInst *Dummy =
367 Builder.CreateLoad(VectorTy, PoisonValue::get(Builder.getPtrTy()),
368 "promotealloca.dummyload");
369 DeferredLoads.push_back(Dummy);
370 return Dummy;
371 };
372
373 const auto CreateTempPtrIntCast = [&Builder, DL](Value *Val,
374 Type *PtrTy) -> Value * {
375 assert(DL.getTypeStoreSize(Val->getType()) == DL.getTypeStoreSize(PtrTy));
376 const unsigned Size = DL.getTypeStoreSizeInBits(PtrTy);
377 if (!PtrTy->isVectorTy())
378 return Builder.CreateBitOrPointerCast(Val, Builder.getIntNTy(Size));
379 const unsigned NumPtrElts = cast<FixedVectorType>(PtrTy)->getNumElements();
380 // If we want to cast to cast, e.g. a <2 x ptr> into a <4 x i32>, we need to
381 // first cast the ptr vector to <2 x i64>.
382 assert((Size % NumPtrElts == 0) && "Vector size not divisble");
383 Type *EltTy = Builder.getIntNTy(Size / NumPtrElts);
384 return Builder.CreateBitOrPointerCast(
385 Val, FixedVectorType::get(EltTy, NumPtrElts));
386 };
387
388 Type *VecEltTy = VectorTy->getElementType();
389
390 switch (Inst->getOpcode()) {
391 case Instruction::Load: {
392 // Loads can only be lowered if the value is known.
393 if (!CurVal) {
394 DeferredLoads.push_back(cast<LoadInst>(Inst));
395 return nullptr;
396 }
397
399 cast<LoadInst>(Inst)->getPointerOperand(), GEPVectorIdx);
400
401 // We're loading the full vector.
402 Type *AccessTy = Inst->getType();
403 TypeSize AccessSize = DL.getTypeStoreSize(AccessTy);
404 if (AccessSize == VecStoreSize && cast<Constant>(Index)->isZeroValue()) {
405 if (AccessTy->isPtrOrPtrVectorTy())
406 CurVal = CreateTempPtrIntCast(CurVal, AccessTy);
407 else if (CurVal->getType()->isPtrOrPtrVectorTy())
408 CurVal = CreateTempPtrIntCast(CurVal, CurVal->getType());
409 Value *NewVal = Builder.CreateBitOrPointerCast(CurVal, AccessTy);
410 Inst->replaceAllUsesWith(NewVal);
411 return nullptr;
412 }
413
414 // Loading a subvector.
415 if (isa<FixedVectorType>(AccessTy)) {
416 assert(AccessSize.isKnownMultipleOf(DL.getTypeStoreSize(VecEltTy)));
417 const unsigned NumLoadedElts = AccessSize / DL.getTypeStoreSize(VecEltTy);
418 auto *SubVecTy = FixedVectorType::get(VecEltTy, NumLoadedElts);
419 assert(DL.getTypeStoreSize(SubVecTy) == DL.getTypeStoreSize(AccessTy));
420
421 Value *SubVec = PoisonValue::get(SubVecTy);
422 for (unsigned K = 0; K < NumLoadedElts; ++K) {
423 Value *CurIdx =
424 Builder.CreateAdd(Index, ConstantInt::get(Index->getType(), K));
425 SubVec = Builder.CreateInsertElement(
426 SubVec, Builder.CreateExtractElement(CurVal, CurIdx), K);
427 }
428
429 if (AccessTy->isPtrOrPtrVectorTy())
430 SubVec = CreateTempPtrIntCast(SubVec, AccessTy);
431 else if (SubVecTy->isPtrOrPtrVectorTy())
432 SubVec = CreateTempPtrIntCast(SubVec, SubVecTy);
433
434 SubVec = Builder.CreateBitOrPointerCast(SubVec, AccessTy);
435 Inst->replaceAllUsesWith(SubVec);
436 return nullptr;
437 }
438
439 // We're loading one element.
440 Value *ExtractElement = Builder.CreateExtractElement(CurVal, Index);
441 if (AccessTy != VecEltTy)
442 ExtractElement = Builder.CreateBitOrPointerCast(ExtractElement, AccessTy);
443
444 Inst->replaceAllUsesWith(ExtractElement);
445 return nullptr;
446 }
447 case Instruction::Store: {
448 // For stores, it's a bit trickier and it depends on whether we're storing
449 // the full vector or not. If we're storing the full vector, we don't need
450 // to know the current value. If this is a store of a single element, we
451 // need to know the value.
452 StoreInst *SI = cast<StoreInst>(Inst);
453 Value *Index = calculateVectorIndex(SI->getPointerOperand(), GEPVectorIdx);
454 Value *Val = SI->getValueOperand();
455
456 // We're storing the full vector, we can handle this without knowing CurVal.
457 Type *AccessTy = Val->getType();
458 TypeSize AccessSize = DL.getTypeStoreSize(AccessTy);
459 if (AccessSize == VecStoreSize && cast<Constant>(Index)->isZeroValue()) {
460 if (AccessTy->isPtrOrPtrVectorTy())
461 Val = CreateTempPtrIntCast(Val, AccessTy);
462 else if (VectorTy->isPtrOrPtrVectorTy())
463 Val = CreateTempPtrIntCast(Val, VectorTy);
464 return Builder.CreateBitOrPointerCast(Val, VectorTy);
465 }
466
467 // Storing a subvector.
468 if (isa<FixedVectorType>(AccessTy)) {
469 assert(AccessSize.isKnownMultipleOf(DL.getTypeStoreSize(VecEltTy)));
470 const unsigned NumWrittenElts =
471 AccessSize / DL.getTypeStoreSize(VecEltTy);
472 const unsigned NumVecElts = VectorTy->getNumElements();
473 auto *SubVecTy = FixedVectorType::get(VecEltTy, NumWrittenElts);
474 assert(DL.getTypeStoreSize(SubVecTy) == DL.getTypeStoreSize(AccessTy));
475
476 if (SubVecTy->isPtrOrPtrVectorTy())
477 Val = CreateTempPtrIntCast(Val, SubVecTy);
478 else if (AccessTy->isPtrOrPtrVectorTy())
479 Val = CreateTempPtrIntCast(Val, AccessTy);
480
481 Val = Builder.CreateBitOrPointerCast(Val, SubVecTy);
482
483 Value *CurVec = GetOrLoadCurrentVectorValue();
484 for (unsigned K = 0, NumElts = std::min(NumWrittenElts, NumVecElts);
485 K < NumElts; ++K) {
486 Value *CurIdx =
487 Builder.CreateAdd(Index, ConstantInt::get(Index->getType(), K));
488 CurVec = Builder.CreateInsertElement(
489 CurVec, Builder.CreateExtractElement(Val, K), CurIdx);
490 }
491 return CurVec;
492 }
493
494 if (Val->getType() != VecEltTy)
495 Val = Builder.CreateBitOrPointerCast(Val, VecEltTy);
496 return Builder.CreateInsertElement(GetOrLoadCurrentVectorValue(), Val,
497 Index);
498 }
499 case Instruction::Call: {
500 if (auto *MTI = dyn_cast<MemTransferInst>(Inst)) {
501 // For memcpy, we need to know curval.
502 ConstantInt *Length = cast<ConstantInt>(MTI->getLength());
503 unsigned NumCopied = Length->getZExtValue() / ElementSize;
504 MemTransferInfo *TI = &TransferInfo[MTI];
505 unsigned SrcBegin = TI->SrcIndex->getZExtValue();
506 unsigned DestBegin = TI->DestIndex->getZExtValue();
507
508 SmallVector<int> Mask;
509 for (unsigned Idx = 0; Idx < VectorTy->getNumElements(); ++Idx) {
510 if (Idx >= DestBegin && Idx < DestBegin + NumCopied) {
511 Mask.push_back(SrcBegin++);
512 } else {
513 Mask.push_back(Idx);
514 }
515 }
516
517 return Builder.CreateShuffleVector(GetOrLoadCurrentVectorValue(), Mask);
518 }
519
520 if (auto *MSI = dyn_cast<MemSetInst>(Inst)) {
521 // For memset, we don't need to know the previous value because we
522 // currently only allow memsets that cover the whole alloca.
523 Value *Elt = MSI->getOperand(1);
524 const unsigned BytesPerElt = DL.getTypeStoreSize(VecEltTy);
525 if (BytesPerElt > 1) {
526 Value *EltBytes = Builder.CreateVectorSplat(BytesPerElt, Elt);
527
528 // If the element type of the vector is a pointer, we need to first cast
529 // to an integer, then use a PtrCast.
530 if (VecEltTy->isPointerTy()) {
531 Type *PtrInt = Builder.getIntNTy(BytesPerElt * 8);
532 Elt = Builder.CreateBitCast(EltBytes, PtrInt);
533 Elt = Builder.CreateIntToPtr(Elt, VecEltTy);
534 } else
535 Elt = Builder.CreateBitCast(EltBytes, VecEltTy);
536 }
537
538 return Builder.CreateVectorSplat(VectorTy->getElementCount(), Elt);
539 }
540
541 if (auto *Intr = dyn_cast<IntrinsicInst>(Inst)) {
542 if (Intr->getIntrinsicID() == Intrinsic::objectsize) {
543 Intr->replaceAllUsesWith(
544 Builder.getIntN(Intr->getType()->getIntegerBitWidth(),
545 DL.getTypeAllocSize(VectorTy)));
546 return nullptr;
547 }
548 }
549
550 llvm_unreachable("Unsupported call when promoting alloca to vector");
551 }
552
553 default:
554 llvm_unreachable("Inconsistency in instructions promotable to vector");
555 }
556
557 llvm_unreachable("Did not return after promoting instruction!");
558}
559
560static bool isSupportedAccessType(FixedVectorType *VecTy, Type *AccessTy,
561 const DataLayout &DL) {
562 // Access as a vector type can work if the size of the access vector is a
563 // multiple of the size of the alloca's vector element type.
564 //
565 // Examples:
566 // - VecTy = <8 x float>, AccessTy = <4 x float> -> OK
567 // - VecTy = <4 x double>, AccessTy = <2 x float> -> OK
568 // - VecTy = <4 x double>, AccessTy = <3 x float> -> NOT OK
569 // - 3*32 is not a multiple of 64
570 //
571 // We could handle more complicated cases, but it'd make things a lot more
572 // complicated.
573 if (isa<FixedVectorType>(AccessTy)) {
574 TypeSize AccTS = DL.getTypeStoreSize(AccessTy);
575 TypeSize VecTS = DL.getTypeStoreSize(VecTy->getElementType());
576 return AccTS.isKnownMultipleOf(VecTS);
577 }
578
580 DL);
581}
582
583/// Iterates over an instruction worklist that may contain multiple instructions
584/// from the same basic block, but in a different order.
585template <typename InstContainer>
586static void forEachWorkListItem(const InstContainer &WorkList,
587 std::function<void(Instruction *)> Fn) {
588 // Bucket up uses of the alloca by the block they occur in.
589 // This is important because we have to handle multiple defs/uses in a block
590 // ourselves: SSAUpdater is purely for cross-block references.
592 for (Instruction *User : WorkList)
593 UsesByBlock[User->getParent()].insert(User);
594
595 for (Instruction *User : WorkList) {
596 BasicBlock *BB = User->getParent();
597 auto &BlockUses = UsesByBlock[BB];
598
599 // Already processed, skip.
600 if (BlockUses.empty())
601 continue;
602
603 // Only user in the block, directly process it.
604 if (BlockUses.size() == 1) {
605 Fn(User);
606 continue;
607 }
608
609 // Multiple users in the block, do a linear scan to see users in order.
610 for (Instruction &Inst : *BB) {
611 if (!BlockUses.contains(&Inst))
612 continue;
613
614 Fn(&Inst);
615 }
616
617 // Clear the block so we know it's been processed.
618 BlockUses.clear();
619 }
620}
621
622// FIXME: Should try to pick the most likely to be profitable allocas first.
623bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToVector(AllocaInst &Alloca) {
624 LLVM_DEBUG(dbgs() << "Trying to promote to vector: " << Alloca << '\n');
625
626 if (DisablePromoteAllocaToVector) {
627 LLVM_DEBUG(dbgs() << " Promote alloca to vector is disabled\n");
628 return false;
629 }
630
631 Type *AllocaTy = Alloca.getAllocatedType();
632 auto *VectorTy = dyn_cast<FixedVectorType>(AllocaTy);
633 if (auto *ArrayTy = dyn_cast<ArrayType>(AllocaTy)) {
634 if (VectorType::isValidElementType(ArrayTy->getElementType()) &&
635 ArrayTy->getNumElements() > 0)
636 VectorTy = FixedVectorType::get(ArrayTy->getElementType(),
637 ArrayTy->getNumElements());
638 }
639
640 // Use up to 1/4 of available register budget for vectorization.
641 unsigned Limit = PromoteAllocaToVectorLimit ? PromoteAllocaToVectorLimit * 8
642 : (MaxVGPRs * 32);
643
644 if (DL->getTypeSizeInBits(AllocaTy) * 4 > Limit) {
645 LLVM_DEBUG(dbgs() << " Alloca too big for vectorization with " << MaxVGPRs
646 << " registers available\n");
647 return false;
648 }
649
650 // FIXME: There is no reason why we can't support larger arrays, we
651 // are just being conservative for now.
652 // FIXME: We also reject alloca's of the form [ 2 x [ 2 x i32 ]] or
653 // equivalent. Potentially these could also be promoted but we don't currently
654 // handle this case
655 if (!VectorTy) {
656 LLVM_DEBUG(dbgs() << " Cannot convert type to vector\n");
657 return false;
658 }
659
660 if (VectorTy->getNumElements() > 16 || VectorTy->getNumElements() < 2) {
661 LLVM_DEBUG(dbgs() << " " << *VectorTy
662 << " has an unsupported number of elements\n");
663 return false;
664 }
665
666 std::map<GetElementPtrInst *, Value *> GEPVectorIdx;
668 SmallVector<Instruction *> UsersToRemove;
669 SmallVector<Instruction *> DeferredInsts;
672
673 const auto RejectUser = [&](Instruction *Inst, Twine Msg) {
674 LLVM_DEBUG(dbgs() << " Cannot promote alloca to vector: " << Msg << "\n"
675 << " " << *Inst << "\n");
676 return false;
677 };
678
679 for (Use &U : Alloca.uses())
680 Uses.push_back(&U);
681
682 LLVM_DEBUG(dbgs() << " Attempting promotion to: " << *VectorTy << "\n");
683
684 Type *VecEltTy = VectorTy->getElementType();
685 unsigned ElementSize = DL->getTypeSizeInBits(VecEltTy) / 8;
686 while (!Uses.empty()) {
687 Use *U = Uses.pop_back_val();
688 Instruction *Inst = cast<Instruction>(U->getUser());
689
690 if (Value *Ptr = getLoadStorePointerOperand(Inst)) {
691 // This is a store of the pointer, not to the pointer.
692 if (isa<StoreInst>(Inst) &&
693 U->getOperandNo() != StoreInst::getPointerOperandIndex())
694 return RejectUser(Inst, "pointer is being stored");
695
696 Type *AccessTy = getLoadStoreType(Inst);
697 if (AccessTy->isAggregateType())
698 return RejectUser(Inst, "unsupported load/store as aggregate");
699 assert(!AccessTy->isAggregateType() || AccessTy->isArrayTy());
700
701 // Check that this is a simple access of a vector element.
702 bool IsSimple = isa<LoadInst>(Inst) ? cast<LoadInst>(Inst)->isSimple()
703 : cast<StoreInst>(Inst)->isSimple();
704 if (!IsSimple)
705 return RejectUser(Inst, "not a simple load or store");
706
707 Ptr = Ptr->stripPointerCasts();
708
709 // Alloca already accessed as vector.
710 if (Ptr == &Alloca && DL->getTypeStoreSize(Alloca.getAllocatedType()) ==
711 DL->getTypeStoreSize(AccessTy)) {
712 WorkList.push_back(Inst);
713 continue;
714 }
715
716 if (!isSupportedAccessType(VectorTy, AccessTy, *DL))
717 return RejectUser(Inst, "not a supported access type");
718
719 WorkList.push_back(Inst);
720 continue;
721 }
722
723 if (isa<BitCastInst>(Inst)) {
724 // Look through bitcasts.
725 for (Use &U : Inst->uses())
726 Uses.push_back(&U);
727 UsersToRemove.push_back(Inst);
728 continue;
729 }
730
731 if (auto *GEP = dyn_cast<GetElementPtrInst>(Inst)) {
732 // If we can't compute a vector index from this GEP, then we can't
733 // promote this alloca to vector.
734 Value *Index = GEPToVectorIndex(GEP, &Alloca, VecEltTy, *DL);
735 if (!Index)
736 return RejectUser(Inst, "cannot compute vector index for GEP");
737
738 GEPVectorIdx[GEP] = Index;
739 for (Use &U : Inst->uses())
740 Uses.push_back(&U);
741 UsersToRemove.push_back(Inst);
742 continue;
743 }
744
745 if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst);
746 MSI && isSupportedMemset(MSI, &Alloca, *DL)) {
747 WorkList.push_back(Inst);
748 continue;
749 }
750
751 if (MemTransferInst *TransferInst = dyn_cast<MemTransferInst>(Inst)) {
752 if (TransferInst->isVolatile())
753 return RejectUser(Inst, "mem transfer inst is volatile");
754
755 ConstantInt *Len = dyn_cast<ConstantInt>(TransferInst->getLength());
756 if (!Len || (Len->getZExtValue() % ElementSize))
757 return RejectUser(Inst, "mem transfer inst length is non-constant or "
758 "not a multiple of the vector element size");
759
760 if (!TransferInfo.count(TransferInst)) {
761 DeferredInsts.push_back(Inst);
762 WorkList.push_back(Inst);
763 TransferInfo[TransferInst] = MemTransferInfo();
764 }
765
766 auto getPointerIndexOfAlloca = [&](Value *Ptr) -> ConstantInt * {
767 GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Ptr);
768 if (Ptr != &Alloca && !GEPVectorIdx.count(GEP))
769 return nullptr;
770
771 return dyn_cast<ConstantInt>(calculateVectorIndex(Ptr, GEPVectorIdx));
772 };
773
774 unsigned OpNum = U->getOperandNo();
775 MemTransferInfo *TI = &TransferInfo[TransferInst];
776 if (OpNum == 0) {
777 Value *Dest = TransferInst->getDest();
778 ConstantInt *Index = getPointerIndexOfAlloca(Dest);
779 if (!Index)
780 return RejectUser(Inst, "could not calculate constant dest index");
781 TI->DestIndex = Index;
782 } else {
783 assert(OpNum == 1);
784 Value *Src = TransferInst->getSource();
785 ConstantInt *Index = getPointerIndexOfAlloca(Src);
786 if (!Index)
787 return RejectUser(Inst, "could not calculate constant src index");
788 TI->SrcIndex = Index;
789 }
790 continue;
791 }
792
793 if (auto *Intr = dyn_cast<IntrinsicInst>(Inst)) {
794 if (Intr->getIntrinsicID() == Intrinsic::objectsize) {
795 WorkList.push_back(Inst);
796 continue;
797 }
798 }
799
800 // Ignore assume-like intrinsics and comparisons used in assumes.
801 if (isAssumeLikeIntrinsic(Inst)) {
802 if (!Inst->use_empty())
803 return RejectUser(Inst, "assume-like intrinsic cannot have any users");
804 UsersToRemove.push_back(Inst);
805 continue;
806 }
807
808 if (isa<ICmpInst>(Inst) && all_of(Inst->users(), [](User *U) {
809 return isAssumeLikeIntrinsic(cast<Instruction>(U));
810 })) {
811 UsersToRemove.push_back(Inst);
812 continue;
813 }
814
815 return RejectUser(Inst, "unhandled alloca user");
816 }
817
818 while (!DeferredInsts.empty()) {
819 Instruction *Inst = DeferredInsts.pop_back_val();
820 MemTransferInst *TransferInst = cast<MemTransferInst>(Inst);
821 // TODO: Support the case if the pointers are from different alloca or
822 // from different address spaces.
823 MemTransferInfo &Info = TransferInfo[TransferInst];
824 if (!Info.SrcIndex || !Info.DestIndex)
825 return RejectUser(
826 Inst, "mem transfer inst is missing constant src and/or dst index");
827 }
828
829 LLVM_DEBUG(dbgs() << " Converting alloca to vector " << *AllocaTy << " -> "
830 << *VectorTy << '\n');
831 const unsigned VecStoreSize = DL->getTypeStoreSize(VectorTy);
832
833 // Alloca is uninitialized memory. Imitate that by making the first value
834 // undef.
835 SSAUpdater Updater;
836 Updater.Initialize(VectorTy, "promotealloca");
837 Updater.AddAvailableValue(Alloca.getParent(), UndefValue::get(VectorTy));
838
839 // First handle the initial worklist.
840 SmallVector<LoadInst *, 4> DeferredLoads;
841 forEachWorkListItem(WorkList, [&](Instruction *I) {
842 BasicBlock *BB = I->getParent();
843 // On the first pass, we only take values that are trivially known, i.e.
844 // where AddAvailableValue was already called in this block.
846 I, *DL, VectorTy, VecStoreSize, ElementSize, TransferInfo, GEPVectorIdx,
847 Updater.FindValueForBlock(BB), DeferredLoads);
848 if (Result)
849 Updater.AddAvailableValue(BB, Result);
850 });
851
852 // Then handle deferred loads.
853 forEachWorkListItem(DeferredLoads, [&](Instruction *I) {
855 BasicBlock *BB = I->getParent();
856 // On the second pass, we use GetValueInMiddleOfBlock to guarantee we always
857 // get a value, inserting PHIs as needed.
859 I, *DL, VectorTy, VecStoreSize, ElementSize, TransferInfo, GEPVectorIdx,
860 Updater.GetValueInMiddleOfBlock(I->getParent()), NewDLs);
861 if (Result)
862 Updater.AddAvailableValue(BB, Result);
863 assert(NewDLs.empty() && "No more deferred loads should be queued!");
864 });
865
866 // Delete all instructions. On the first pass, new dummy loads may have been
867 // added so we need to collect them too.
868 DenseSet<Instruction *> InstsToDelete(WorkList.begin(), WorkList.end());
869 InstsToDelete.insert(DeferredLoads.begin(), DeferredLoads.end());
870 for (Instruction *I : InstsToDelete) {
871 assert(I->use_empty());
872 I->eraseFromParent();
873 }
874
875 // Delete all the users that are known to be removeable.
876 for (Instruction *I : reverse(UsersToRemove)) {
877 I->dropDroppableUses();
878 assert(I->use_empty());
879 I->eraseFromParent();
880 }
881
882 // Alloca should now be dead too.
883 assert(Alloca.use_empty());
884 Alloca.eraseFromParent();
885 return true;
886}
887
888std::pair<Value *, Value *>
889AMDGPUPromoteAllocaImpl::getLocalSizeYZ(IRBuilder<> &Builder) {
890 Function &F = *Builder.GetInsertBlock()->getParent();
892
893 if (!IsAMDHSA) {
894 Function *LocalSizeYFn =
895 Intrinsic::getDeclaration(Mod, Intrinsic::r600_read_local_size_y);
896 Function *LocalSizeZFn =
897 Intrinsic::getDeclaration(Mod, Intrinsic::r600_read_local_size_z);
898
899 CallInst *LocalSizeY = Builder.CreateCall(LocalSizeYFn, {});
900 CallInst *LocalSizeZ = Builder.CreateCall(LocalSizeZFn, {});
901
902 ST.makeLIDRangeMetadata(LocalSizeY);
903 ST.makeLIDRangeMetadata(LocalSizeZ);
904
905 return std::pair(LocalSizeY, LocalSizeZ);
906 }
907
908 // We must read the size out of the dispatch pointer.
909 assert(IsAMDGCN);
910
911 // We are indexing into this struct, and want to extract the workgroup_size_*
912 // fields.
913 //
914 // typedef struct hsa_kernel_dispatch_packet_s {
915 // uint16_t header;
916 // uint16_t setup;
917 // uint16_t workgroup_size_x ;
918 // uint16_t workgroup_size_y;
919 // uint16_t workgroup_size_z;
920 // uint16_t reserved0;
921 // uint32_t grid_size_x ;
922 // uint32_t grid_size_y ;
923 // uint32_t grid_size_z;
924 //
925 // uint32_t private_segment_size;
926 // uint32_t group_segment_size;
927 // uint64_t kernel_object;
928 //
929 // #ifdef HSA_LARGE_MODEL
930 // void *kernarg_address;
931 // #elif defined HSA_LITTLE_ENDIAN
932 // void *kernarg_address;
933 // uint32_t reserved1;
934 // #else
935 // uint32_t reserved1;
936 // void *kernarg_address;
937 // #endif
938 // uint64_t reserved2;
939 // hsa_signal_t completion_signal; // uint64_t wrapper
940 // } hsa_kernel_dispatch_packet_t
941 //
942 Function *DispatchPtrFn =
943 Intrinsic::getDeclaration(Mod, Intrinsic::amdgcn_dispatch_ptr);
944
945 CallInst *DispatchPtr = Builder.CreateCall(DispatchPtrFn, {});
946 DispatchPtr->addRetAttr(Attribute::NoAlias);
947 DispatchPtr->addRetAttr(Attribute::NonNull);
948 F.removeFnAttr("amdgpu-no-dispatch-ptr");
949
950 // Size of the dispatch packet struct.
951 DispatchPtr->addDereferenceableRetAttr(64);
952
953 Type *I32Ty = Type::getInt32Ty(Mod->getContext());
954 Value *CastDispatchPtr = Builder.CreateBitCast(
955 DispatchPtr, PointerType::get(I32Ty, AMDGPUAS::CONSTANT_ADDRESS));
956
957 // We could do a single 64-bit load here, but it's likely that the basic
958 // 32-bit and extract sequence is already present, and it is probably easier
959 // to CSE this. The loads should be mergeable later anyway.
960 Value *GEPXY = Builder.CreateConstInBoundsGEP1_64(I32Ty, CastDispatchPtr, 1);
961 LoadInst *LoadXY = Builder.CreateAlignedLoad(I32Ty, GEPXY, Align(4));
962
963 Value *GEPZU = Builder.CreateConstInBoundsGEP1_64(I32Ty, CastDispatchPtr, 2);
964 LoadInst *LoadZU = Builder.CreateAlignedLoad(I32Ty, GEPZU, Align(4));
965
966 MDNode *MD = MDNode::get(Mod->getContext(), std::nullopt);
967 LoadXY->setMetadata(LLVMContext::MD_invariant_load, MD);
968 LoadZU->setMetadata(LLVMContext::MD_invariant_load, MD);
969 ST.makeLIDRangeMetadata(LoadZU);
970
971 // Extract y component. Upper half of LoadZU should be zero already.
972 Value *Y = Builder.CreateLShr(LoadXY, 16);
973
974 return std::pair(Y, LoadZU);
975}
976
977Value *AMDGPUPromoteAllocaImpl::getWorkitemID(IRBuilder<> &Builder,
978 unsigned N) {
979 Function *F = Builder.GetInsertBlock()->getParent();
982 StringRef AttrName;
983
984 switch (N) {
985 case 0:
986 IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_x
987 : (Intrinsic::ID)Intrinsic::r600_read_tidig_x;
988 AttrName = "amdgpu-no-workitem-id-x";
989 break;
990 case 1:
991 IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_y
992 : (Intrinsic::ID)Intrinsic::r600_read_tidig_y;
993 AttrName = "amdgpu-no-workitem-id-y";
994 break;
995
996 case 2:
997 IntrID = IsAMDGCN ? (Intrinsic::ID)Intrinsic::amdgcn_workitem_id_z
998 : (Intrinsic::ID)Intrinsic::r600_read_tidig_z;
999 AttrName = "amdgpu-no-workitem-id-z";
1000 break;
1001 default:
1002 llvm_unreachable("invalid dimension");
1003 }
1004
1005 Function *WorkitemIdFn = Intrinsic::getDeclaration(Mod, IntrID);
1006 CallInst *CI = Builder.CreateCall(WorkitemIdFn);
1007 ST.makeLIDRangeMetadata(CI);
1008 F->removeFnAttr(AttrName);
1009
1010 return CI;
1011}
1012
1013static bool isCallPromotable(CallInst *CI) {
1014 IntrinsicInst *II = dyn_cast<IntrinsicInst>(CI);
1015 if (!II)
1016 return false;
1017
1018 switch (II->getIntrinsicID()) {
1019 case Intrinsic::memcpy:
1020 case Intrinsic::memmove:
1021 case Intrinsic::memset:
1022 case Intrinsic::lifetime_start:
1023 case Intrinsic::lifetime_end:
1024 case Intrinsic::invariant_start:
1025 case Intrinsic::invariant_end:
1026 case Intrinsic::launder_invariant_group:
1027 case Intrinsic::strip_invariant_group:
1028 case Intrinsic::objectsize:
1029 return true;
1030 default:
1031 return false;
1032 }
1033}
1034
1035bool AMDGPUPromoteAllocaImpl::binaryOpIsDerivedFromSameAlloca(
1036 Value *BaseAlloca, Value *Val, Instruction *Inst, int OpIdx0,
1037 int OpIdx1) const {
1038 // Figure out which operand is the one we might not be promoting.
1039 Value *OtherOp = Inst->getOperand(OpIdx0);
1040 if (Val == OtherOp)
1041 OtherOp = Inst->getOperand(OpIdx1);
1042
1043 if (isa<ConstantPointerNull>(OtherOp))
1044 return true;
1045
1046 Value *OtherObj = getUnderlyingObject(OtherOp);
1047 if (!isa<AllocaInst>(OtherObj))
1048 return false;
1049
1050 // TODO: We should be able to replace undefs with the right pointer type.
1051
1052 // TODO: If we know the other base object is another promotable
1053 // alloca, not necessarily this alloca, we can do this. The
1054 // important part is both must have the same address space at
1055 // the end.
1056 if (OtherObj != BaseAlloca) {
1057 LLVM_DEBUG(
1058 dbgs() << "Found a binary instruction with another alloca object\n");
1059 return false;
1060 }
1061
1062 return true;
1063}
1064
1065bool AMDGPUPromoteAllocaImpl::collectUsesWithPtrTypes(
1066 Value *BaseAlloca, Value *Val, std::vector<Value *> &WorkList) const {
1067
1068 for (User *User : Val->users()) {
1069 if (is_contained(WorkList, User))
1070 continue;
1071
1072 if (CallInst *CI = dyn_cast<CallInst>(User)) {
1073 if (!isCallPromotable(CI))
1074 return false;
1075
1076 WorkList.push_back(User);
1077 continue;
1078 }
1079
1080 Instruction *UseInst = cast<Instruction>(User);
1081 if (UseInst->getOpcode() == Instruction::PtrToInt)
1082 return false;
1083
1084 if (LoadInst *LI = dyn_cast<LoadInst>(UseInst)) {
1085 if (LI->isVolatile())
1086 return false;
1087
1088 continue;
1089 }
1090
1091 if (StoreInst *SI = dyn_cast<StoreInst>(UseInst)) {
1092 if (SI->isVolatile())
1093 return false;
1094
1095 // Reject if the stored value is not the pointer operand.
1096 if (SI->getPointerOperand() != Val)
1097 return false;
1098 } else if (AtomicRMWInst *RMW = dyn_cast<AtomicRMWInst>(UseInst)) {
1099 if (RMW->isVolatile())
1100 return false;
1101 } else if (AtomicCmpXchgInst *CAS = dyn_cast<AtomicCmpXchgInst>(UseInst)) {
1102 if (CAS->isVolatile())
1103 return false;
1104 }
1105
1106 // Only promote a select if we know that the other select operand
1107 // is from another pointer that will also be promoted.
1108 if (ICmpInst *ICmp = dyn_cast<ICmpInst>(UseInst)) {
1109 if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, ICmp, 0, 1))
1110 return false;
1111
1112 // May need to rewrite constant operands.
1113 WorkList.push_back(ICmp);
1114 }
1115
1116 if (UseInst->getOpcode() == Instruction::AddrSpaceCast) {
1117 // Give up if the pointer may be captured.
1118 if (PointerMayBeCaptured(UseInst, true, true))
1119 return false;
1120 // Don't collect the users of this.
1121 WorkList.push_back(User);
1122 continue;
1123 }
1124
1125 // Do not promote vector/aggregate type instructions. It is hard to track
1126 // their users.
1127 if (isa<InsertValueInst>(User) || isa<InsertElementInst>(User))
1128 return false;
1129
1130 if (!User->getType()->isPointerTy())
1131 continue;
1132
1133 if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(UseInst)) {
1134 // Be conservative if an address could be computed outside the bounds of
1135 // the alloca.
1136 if (!GEP->isInBounds())
1137 return false;
1138 }
1139
1140 // Only promote a select if we know that the other select operand is from
1141 // another pointer that will also be promoted.
1142 if (SelectInst *SI = dyn_cast<SelectInst>(UseInst)) {
1143 if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, SI, 1, 2))
1144 return false;
1145 }
1146
1147 // Repeat for phis.
1148 if (PHINode *Phi = dyn_cast<PHINode>(UseInst)) {
1149 // TODO: Handle more complex cases. We should be able to replace loops
1150 // over arrays.
1151 switch (Phi->getNumIncomingValues()) {
1152 case 1:
1153 break;
1154 case 2:
1155 if (!binaryOpIsDerivedFromSameAlloca(BaseAlloca, Val, Phi, 0, 1))
1156 return false;
1157 break;
1158 default:
1159 return false;
1160 }
1161 }
1162
1163 WorkList.push_back(User);
1164 if (!collectUsesWithPtrTypes(BaseAlloca, User, WorkList))
1165 return false;
1166 }
1167
1168 return true;
1169}
1170
1171bool AMDGPUPromoteAllocaImpl::hasSufficientLocalMem(const Function &F) {
1172
1173 FunctionType *FTy = F.getFunctionType();
1175
1176 // If the function has any arguments in the local address space, then it's
1177 // possible these arguments require the entire local memory space, so
1178 // we cannot use local memory in the pass.
1179 for (Type *ParamTy : FTy->params()) {
1180 PointerType *PtrTy = dyn_cast<PointerType>(ParamTy);
1181 if (PtrTy && PtrTy->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
1182 LocalMemLimit = 0;
1183 LLVM_DEBUG(dbgs() << "Function has local memory argument. Promoting to "
1184 "local memory disabled.\n");
1185 return false;
1186 }
1187 }
1188
1189 LocalMemLimit = ST.getAddressableLocalMemorySize();
1190 if (LocalMemLimit == 0)
1191 return false;
1192
1194 SmallPtrSet<const Constant *, 8> VisitedConstants;
1196
1197 auto visitUsers = [&](const GlobalVariable *GV, const Constant *Val) -> bool {
1198 for (const User *U : Val->users()) {
1199 if (const Instruction *Use = dyn_cast<Instruction>(U)) {
1200 if (Use->getParent()->getParent() == &F)
1201 return true;
1202 } else {
1203 const Constant *C = cast<Constant>(U);
1204 if (VisitedConstants.insert(C).second)
1205 Stack.push_back(C);
1206 }
1207 }
1208
1209 return false;
1210 };
1211
1212 for (GlobalVariable &GV : Mod->globals()) {
1214 continue;
1215
1216 if (visitUsers(&GV, &GV)) {
1217 UsedLDS.insert(&GV);
1218 Stack.clear();
1219 continue;
1220 }
1221
1222 // For any ConstantExpr uses, we need to recursively search the users until
1223 // we see a function.
1224 while (!Stack.empty()) {
1225 const Constant *C = Stack.pop_back_val();
1226 if (visitUsers(&GV, C)) {
1227 UsedLDS.insert(&GV);
1228 Stack.clear();
1229 break;
1230 }
1231 }
1232 }
1233
1234 const DataLayout &DL = Mod->getDataLayout();
1235 SmallVector<std::pair<uint64_t, Align>, 16> AllocatedSizes;
1236 AllocatedSizes.reserve(UsedLDS.size());
1237
1238 for (const GlobalVariable *GV : UsedLDS) {
1239 Align Alignment =
1240 DL.getValueOrABITypeAlignment(GV->getAlign(), GV->getValueType());
1241 uint64_t AllocSize = DL.getTypeAllocSize(GV->getValueType());
1242
1243 // HIP uses an extern unsized array in local address space for dynamically
1244 // allocated shared memory. In that case, we have to disable the promotion.
1245 if (GV->hasExternalLinkage() && AllocSize == 0) {
1246 LocalMemLimit = 0;
1247 LLVM_DEBUG(dbgs() << "Function has a reference to externally allocated "
1248 "local memory. Promoting to local memory "
1249 "disabled.\n");
1250 return false;
1251 }
1252
1253 AllocatedSizes.emplace_back(AllocSize, Alignment);
1254 }
1255
1256 // Sort to try to estimate the worst case alignment padding
1257 //
1258 // FIXME: We should really do something to fix the addresses to a more optimal
1259 // value instead
1260 llvm::sort(AllocatedSizes, llvm::less_second());
1261
1262 // Check how much local memory is being used by global objects
1263 CurrentLocalMemUsage = 0;
1264
1265 // FIXME: Try to account for padding here. The real padding and address is
1266 // currently determined from the inverse order of uses in the function when
1267 // legalizing, which could also potentially change. We try to estimate the
1268 // worst case here, but we probably should fix the addresses earlier.
1269 for (auto Alloc : AllocatedSizes) {
1270 CurrentLocalMemUsage = alignTo(CurrentLocalMemUsage, Alloc.second);
1271 CurrentLocalMemUsage += Alloc.first;
1272 }
1273
1274 unsigned MaxOccupancy =
1275 ST.getOccupancyWithLocalMemSize(CurrentLocalMemUsage, F);
1276
1277 // Restrict local memory usage so that we don't drastically reduce occupancy,
1278 // unless it is already significantly reduced.
1279
1280 // TODO: Have some sort of hint or other heuristics to guess occupancy based
1281 // on other factors..
1282 unsigned OccupancyHint = ST.getWavesPerEU(F).second;
1283 if (OccupancyHint == 0)
1284 OccupancyHint = 7;
1285
1286 // Clamp to max value.
1287 OccupancyHint = std::min(OccupancyHint, ST.getMaxWavesPerEU());
1288
1289 // Check the hint but ignore it if it's obviously wrong from the existing LDS
1290 // usage.
1291 MaxOccupancy = std::min(OccupancyHint, MaxOccupancy);
1292
1293 // Round up to the next tier of usage.
1294 unsigned MaxSizeWithWaveCount =
1295 ST.getMaxLocalMemSizeWithWaveCount(MaxOccupancy, F);
1296
1297 // Program is possibly broken by using more local mem than available.
1298 if (CurrentLocalMemUsage > MaxSizeWithWaveCount)
1299 return false;
1300
1301 LocalMemLimit = MaxSizeWithWaveCount;
1302
1303 LLVM_DEBUG(dbgs() << F.getName() << " uses " << CurrentLocalMemUsage
1304 << " bytes of LDS\n"
1305 << " Rounding size to " << MaxSizeWithWaveCount
1306 << " with a maximum occupancy of " << MaxOccupancy << '\n'
1307 << " and " << (LocalMemLimit - CurrentLocalMemUsage)
1308 << " available for promotion\n");
1309
1310 return true;
1311}
1312
1313// FIXME: Should try to pick the most likely to be profitable allocas first.
1314bool AMDGPUPromoteAllocaImpl::tryPromoteAllocaToLDS(AllocaInst &I,
1315 bool SufficientLDS) {
1316 LLVM_DEBUG(dbgs() << "Trying to promote to LDS: " << I << '\n');
1317
1318 if (DisablePromoteAllocaToLDS) {
1319 LLVM_DEBUG(dbgs() << " Promote alloca to LDS is disabled\n");
1320 return false;
1321 }
1322
1323 const DataLayout &DL = Mod->getDataLayout();
1324 IRBuilder<> Builder(&I);
1325
1326 const Function &ContainingFunction = *I.getParent()->getParent();
1327 CallingConv::ID CC = ContainingFunction.getCallingConv();
1328
1329 // Don't promote the alloca to LDS for shader calling conventions as the work
1330 // item ID intrinsics are not supported for these calling conventions.
1331 // Furthermore not all LDS is available for some of the stages.
1332 switch (CC) {
1335 break;
1336 default:
1337 LLVM_DEBUG(
1338 dbgs()
1339 << " promote alloca to LDS not supported with calling convention.\n");
1340 return false;
1341 }
1342
1343 // Not likely to have sufficient local memory for promotion.
1344 if (!SufficientLDS)
1345 return false;
1346
1347 const AMDGPUSubtarget &ST = AMDGPUSubtarget::get(TM, ContainingFunction);
1348 unsigned WorkGroupSize = ST.getFlatWorkGroupSizes(ContainingFunction).second;
1349
1350 Align Alignment =
1351 DL.getValueOrABITypeAlignment(I.getAlign(), I.getAllocatedType());
1352
1353 // FIXME: This computed padding is likely wrong since it depends on inverse
1354 // usage order.
1355 //
1356 // FIXME: It is also possible that if we're allowed to use all of the memory
1357 // could end up using more than the maximum due to alignment padding.
1358
1359 uint32_t NewSize = alignTo(CurrentLocalMemUsage, Alignment);
1360 uint32_t AllocSize =
1361 WorkGroupSize * DL.getTypeAllocSize(I.getAllocatedType());
1362 NewSize += AllocSize;
1363
1364 if (NewSize > LocalMemLimit) {
1365 LLVM_DEBUG(dbgs() << " " << AllocSize
1366 << " bytes of local memory not available to promote\n");
1367 return false;
1368 }
1369
1370 CurrentLocalMemUsage = NewSize;
1371
1372 std::vector<Value *> WorkList;
1373
1374 if (!collectUsesWithPtrTypes(&I, &I, WorkList)) {
1375 LLVM_DEBUG(dbgs() << " Do not know how to convert all uses\n");
1376 return false;
1377 }
1378
1379 LLVM_DEBUG(dbgs() << "Promoting alloca to local memory\n");
1380
1381 Function *F = I.getParent()->getParent();
1382
1383 Type *GVTy = ArrayType::get(I.getAllocatedType(), WorkGroupSize);
1386 Twine(F->getName()) + Twine('.') + I.getName(), nullptr,
1389 GV->setAlignment(I.getAlign());
1390
1391 Value *TCntY, *TCntZ;
1392
1393 std::tie(TCntY, TCntZ) = getLocalSizeYZ(Builder);
1394 Value *TIdX = getWorkitemID(Builder, 0);
1395 Value *TIdY = getWorkitemID(Builder, 1);
1396 Value *TIdZ = getWorkitemID(Builder, 2);
1397
1398 Value *Tmp0 = Builder.CreateMul(TCntY, TCntZ, "", true, true);
1399 Tmp0 = Builder.CreateMul(Tmp0, TIdX);
1400 Value *Tmp1 = Builder.CreateMul(TIdY, TCntZ, "", true, true);
1401 Value *TID = Builder.CreateAdd(Tmp0, Tmp1);
1402 TID = Builder.CreateAdd(TID, TIdZ);
1403
1404 LLVMContext &Context = Mod->getContext();
1405 Value *Indices[] = {Constant::getNullValue(Type::getInt32Ty(Context)), TID};
1406
1407 Value *Offset = Builder.CreateInBoundsGEP(GVTy, GV, Indices);
1408 I.mutateType(Offset->getType());
1409 I.replaceAllUsesWith(Offset);
1410 I.eraseFromParent();
1411
1412 SmallVector<IntrinsicInst *> DeferredIntrs;
1413
1414 for (Value *V : WorkList) {
1415 CallInst *Call = dyn_cast<CallInst>(V);
1416 if (!Call) {
1417 if (ICmpInst *CI = dyn_cast<ICmpInst>(V)) {
1419
1420 if (isa<ConstantPointerNull>(CI->getOperand(0)))
1421 CI->setOperand(0, ConstantPointerNull::get(NewTy));
1422
1423 if (isa<ConstantPointerNull>(CI->getOperand(1)))
1424 CI->setOperand(1, ConstantPointerNull::get(NewTy));
1425
1426 continue;
1427 }
1428
1429 // The operand's value should be corrected on its own and we don't want to
1430 // touch the users.
1431 if (isa<AddrSpaceCastInst>(V))
1432 continue;
1433
1435
1436 // FIXME: It doesn't really make sense to try to do this for all
1437 // instructions.
1438 V->mutateType(NewTy);
1439
1440 // Adjust the types of any constant operands.
1441 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
1442 if (isa<ConstantPointerNull>(SI->getOperand(1)))
1443 SI->setOperand(1, ConstantPointerNull::get(NewTy));
1444
1445 if (isa<ConstantPointerNull>(SI->getOperand(2)))
1446 SI->setOperand(2, ConstantPointerNull::get(NewTy));
1447 } else if (PHINode *Phi = dyn_cast<PHINode>(V)) {
1448 for (unsigned I = 0, E = Phi->getNumIncomingValues(); I != E; ++I) {
1449 if (isa<ConstantPointerNull>(Phi->getIncomingValue(I)))
1450 Phi->setIncomingValue(I, ConstantPointerNull::get(NewTy));
1451 }
1452 }
1453
1454 continue;
1455 }
1456
1457 IntrinsicInst *Intr = cast<IntrinsicInst>(Call);
1458 Builder.SetInsertPoint(Intr);
1459 switch (Intr->getIntrinsicID()) {
1460 case Intrinsic::lifetime_start:
1461 case Intrinsic::lifetime_end:
1462 // These intrinsics are for address space 0 only
1463 Intr->eraseFromParent();
1464 continue;
1465 case Intrinsic::memcpy:
1466 case Intrinsic::memmove:
1467 // These have 2 pointer operands. In case if second pointer also needs
1468 // to be replaced we defer processing of these intrinsics until all
1469 // other values are processed.
1470 DeferredIntrs.push_back(Intr);
1471 continue;
1472 case Intrinsic::memset: {
1473 MemSetInst *MemSet = cast<MemSetInst>(Intr);
1474 Builder.CreateMemSet(MemSet->getRawDest(), MemSet->getValue(),
1475 MemSet->getLength(), MemSet->getDestAlign(),
1476 MemSet->isVolatile());
1477 Intr->eraseFromParent();
1478 continue;
1479 }
1480 case Intrinsic::invariant_start:
1481 case Intrinsic::invariant_end:
1482 case Intrinsic::launder_invariant_group:
1483 case Intrinsic::strip_invariant_group:
1484 Intr->eraseFromParent();
1485 // FIXME: I think the invariant marker should still theoretically apply,
1486 // but the intrinsics need to be changed to accept pointers with any
1487 // address space.
1488 continue;
1489 case Intrinsic::objectsize: {
1490 Value *Src = Intr->getOperand(0);
1491 Function *ObjectSize = Intrinsic::getDeclaration(
1492 Mod, Intrinsic::objectsize,
1493 {Intr->getType(),
1495
1496 CallInst *NewCall = Builder.CreateCall(
1497 ObjectSize,
1498 {Src, Intr->getOperand(1), Intr->getOperand(2), Intr->getOperand(3)});
1499 Intr->replaceAllUsesWith(NewCall);
1500 Intr->eraseFromParent();
1501 continue;
1502 }
1503 default:
1504 Intr->print(errs());
1505 llvm_unreachable("Don't know how to promote alloca intrinsic use.");
1506 }
1507 }
1508
1509 for (IntrinsicInst *Intr : DeferredIntrs) {
1510 Builder.SetInsertPoint(Intr);
1511 Intrinsic::ID ID = Intr->getIntrinsicID();
1512 assert(ID == Intrinsic::memcpy || ID == Intrinsic::memmove);
1513
1514 MemTransferInst *MI = cast<MemTransferInst>(Intr);
1515 auto *B = Builder.CreateMemTransferInst(
1516 ID, MI->getRawDest(), MI->getDestAlign(), MI->getRawSource(),
1517 MI->getSourceAlign(), MI->getLength(), MI->isVolatile());
1518
1519 for (unsigned I = 0; I != 2; ++I) {
1520 if (uint64_t Bytes = Intr->getParamDereferenceableBytes(I)) {
1521 B->addDereferenceableParamAttr(I, Bytes);
1522 }
1523 }
1524
1525 Intr->eraseFromParent();
1526 }
1527
1528 return true;
1529}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
unsigned Intr
AMDGPU promote alloca to vector or LDS
static Value * GEPToVectorIndex(GetElementPtrInst *GEP, AllocaInst *Alloca, Type *VecElemTy, const DataLayout &DL)
static Value * calculateVectorIndex(Value *Ptr, const std::map< GetElementPtrInst *, Value * > &GEPIdx)
static bool isSupportedAccessType(FixedVectorType *VecTy, Type *AccessTy, const DataLayout &DL)
static void forEachWorkListItem(const InstContainer &WorkList, std::function< void(Instruction *)> Fn)
Iterates over an instruction worklist that may contain multiple instructions from the same basic bloc...
static bool isSupportedMemset(MemSetInst *I, AllocaInst *AI, const DataLayout &DL)
static bool isCallPromotable(CallInst *CI)
#define DEBUG_TYPE
static Value * promoteAllocaUserToVector(Instruction *Inst, const DataLayout &DL, FixedVectorType *VectorTy, unsigned VecStoreSize, unsigned ElementSize, DenseMap< MemTransferInst *, MemTransferInfo > &TransferInfo, std::map< GetElementPtrInst *, Value * > &GEPVectorIdx, Value *CurVal, SmallVectorImpl< LoadInst * > &DeferredLoads)
Promotes a single user of the alloca to a vector form.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
uint64_t Size
Rewrite Partial Register Uses
AMD GCN specific subclass of TargetSubtarget.
Hexagon Common GEP
IRTranslator LLVM IR MI
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
LLVMContext & Context
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
Module * Mod
const char LLVMTargetMachineRef TM
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
Target-Independent Code Generator Pass Configuration Options pass.
static const AMDGPUSubtarget & get(const MachineFunction &MF)
Class for arbitrary precision integers.
Definition: APInt.h:76
static void udivrem(const APInt &LHS, const APInt &RHS, APInt &Quotient, APInt &Remainder)
Dual division/remainder interface.
Definition: APInt.cpp:1764
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
an instruction to allocate memory on the stack
Definition: Instructions.h:59
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:119
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:348
Represent the analysis usage information of a pass.
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:269
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
Definition: Type.cpp:647
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:522
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:727
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:214
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:70
void addDereferenceableRetAttr(uint64_t Bytes)
adds the dereferenceable attribute to the list of attributes.
Definition: InstrTypes.h:1679
void addRetAttr(Attribute::AttrKind Kind)
Adds the attribute to the return value.
Definition: InstrTypes.h:1600
This class represents a function call, abstracting a target machine's calling convention.
static bool isBitOrNoopPointerCastable(Type *SrcTy, Type *DestTy, const DataLayout &DL)
Check whether a bitcast, inttoptr, or ptrtoint cast between these types is valid and a no-op.
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:888
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:146
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1691
This is an important base class in LLVM.
Definition: Constant.h:41
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:356
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:151
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:220
Implements a dense probed hash-table based set.
Definition: DenseSet.h:271
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:539
unsigned getNumElements() const
Definition: DerivedTypes.h:582
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:692
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:178
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:262
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:949
MaybeAlign getAlign() const
Returns the alignment of the given variable or function.
Definition: GlobalObject.h:80
void setAlignment(Align Align)
Sets the alignment attribute of the GlobalObject.
Definition: Globals.cpp:128
bool hasExternalLinkage() const
Definition: GlobalValue.h:510
void setUnnamedAddr(UnnamedAddr Val)
Definition: GlobalValue.h:231
unsigned getAddressSpace() const
Definition: GlobalValue.h:205
@ InternalLinkage
Rename collisions when linking (static functions).
Definition: GlobalValue.h:59
Type * getValueType() const
Definition: GlobalValue.h:296
This instruction compares its operands according to the predicate given to the constructor.
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2455
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2443
IntegerType * getIntNTy(unsigned N)
Fetch the type representing an N-bit integer.
Definition: IRBuilder.h:533
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition: IRBuilder.h:1806
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1212
CallInst * CreateMemSet(Value *Ptr, Value *Val, uint64_t Size, MaybeAlign Align, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Create and insert a memset to the specified pointer and the specified value.
Definition: IRBuilder.h:589
Value * CreateIntToPtr(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2105
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1431
BasicBlock * GetInsertBlock() const
Definition: IRBuilder.h:174
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:1875
CallInst * CreateMemTransferInst(Intrinsic::ID IntrID, Value *Dst, MaybeAlign DstAlign, Value *Src, MaybeAlign SrcAlign, Value *Size, bool isVolatile=false, MDNode *TBAATag=nullptr, MDNode *TBAAStructTag=nullptr, MDNode *ScopeTag=nullptr, MDNode *NoAliasTag=nullptr)
Definition: IRBuilder.cpp:219
Value * CreateBitOrPointerCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2188
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2110
ConstantInt * getIntN(unsigned N, uint64_t C)
Get a constant N-bit value, zero extended or truncated from a 64-bit value.
Definition: IRBuilder.h:491
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1789
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2477
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1321
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition: IRBuilder.h:563
Value * CreateConstInBoundsGEP1_64(Type *Ty, Value *Ptr, uint64_t Idx0, const Twine &Name="")
Definition: IRBuilder.h:1936
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:180
CallInst * CreateCall(FunctionType *FTy, Value *Callee, ArrayRef< Value * > Args=std::nullopt, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2395
Value * CreateMul(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:1355
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2649
InstSimplifyFolder - Use InstructionSimplify to fold operations to existing values.
const BasicBlock * getParent() const
Definition: Instruction.h:150
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:93
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1633
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:250
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:54
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:178
Metadata node.
Definition: Metadata.h:1067
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1541
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
size_type size() const
Definition: MapVector.h:60
std::pair< KeyT, ValueT > & front()
Definition: MapVector.h:83
Value * getLength() const
Value * getRawDest() const
MaybeAlign getDestAlign() const
bool isVolatile() const
Value * getValue() const
This class wraps the llvm.memset and llvm.memset.inline intrinsics.
This class wraps the llvm.memcpy/memmove intrinsics.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
static PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1743
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:109
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:115
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:144
Helper class for SSA formation on a set of values defined in multiple blocks.
Definition: SSAUpdater.h:40
Value * FindValueForBlock(BasicBlock *BB) const
Return the value for the specified block if the SSAUpdater has one, otherwise return nullptr.
Definition: SSAUpdater.cpp:66
void Initialize(Type *Ty, StringRef Name)
Reset this object to get ready for a new set of SSA updates with type 'Ty'.
Definition: SSAUpdater.cpp:53
Value * GetValueInMiddleOfBlock(BasicBlock *BB)
Construct SSA form, materializing a value that is live in the middle of the specified block.
Definition: SSAUpdater.cpp:98
void AddAvailableValue(BasicBlock *BB, Value *V)
Indicate that a rewritten value is available in the specified block with the specified value.
Definition: SSAUpdater.cpp:70
This class represents the LLVM 'select' instruction.
size_type size() const
Definition: SmallPtrSet.h:94
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
bool empty() const
Definition: SmallVector.h:94
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void reserve(size_type N)
Definition: SmallVector.h:676
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:302
static unsigned getPointerOperandIndex()
Definition: Instructions.h:404
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition: Type.h:252
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition: Type.h:295
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:262
static IntegerType * getInt32Ty(LLVMContext &C)
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1724
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void setOperand(unsigned i, Value *Val)
Definition: User.h:174
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
iterator_range< user_iterator > users()
Definition: Value.h:421
bool use_empty() const
Definition: Value.h:344
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:1074
iterator_range< use_iterator > uses()
Definition: Value.h:376
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:683
ElementCount getElementCount() const
Return an ElementCount instance to represent the (possibly scalable) number of elements in the vector...
Definition: DerivedTypes.h:641
Type * getElementType() const
Definition: DerivedTypes.h:436
constexpr bool isKnownMultipleOf(ScalarTy RHS) const
This function tells the caller whether the element count is known at compile time to be a multiple of...
Definition: TypeSize.h:180
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
TargetPassConfig.
@ LOCAL_ADDRESS
Address space for local memory.
@ CONSTANT_ADDRESS
Address space for constant memory (VTX2).
const CustomOperand< const MCSubtargetInfo & > Msg[]
bool isEntryFunctionCC(CallingConv::ID CC)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
Definition: CallingConv.h:200
@ SPIR_KERNEL
Used for SPIR kernel functions.
Definition: CallingConv.h:144
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
Function * getDeclaration(Module *M, ID id, ArrayRef< Type * > Tys=std::nullopt)
Create or insert an LLVM Function declaration for an intrinsic, and return it.
Definition: Function.cpp:1447
specific_intval< false > m_SpecificInt(APInt V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:862
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
NodeAddr< PhiNode * > Phi
Definition: RDFGraph.h:390
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:456
@ Length
Definition: DWP.cpp:456
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:1731
bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
FunctionPass * createAMDGPUPromoteAllocaToVector()
auto reverse(ContainerTy &&C)
Definition: STLExtras.h:428
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1656
bool PointerMayBeCaptured(const Value *V, bool ReturnCaptures, bool StoreCaptures, unsigned MaxUsesToExplore=0)
PointerMayBeCaptured - Return true if this pointer value may be captured by the enclosing function (w...
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
FunctionPass * createAMDGPUPromoteAlloca()
@ Mod
The access may modify the value stored in memory.
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:155
char & AMDGPUPromoteAllocaID
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1888
char & AMDGPUPromoteAllocaToVectorID
Type * getLoadStoreType(Value *I)
A helper function that returns the type of a load or store instruction.
#define N
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Function object to check whether the second component of a container supported by std::get (like std:...
Definition: STLExtras.h:1468