1//===-- AMDGPUCodeGenPrepare.cpp ------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10/// This pass does misc. AMDGPU optimizations on IR *just* before instruction
11/// selection.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AMDGPU.h"
16#include "AMDGPUMemoryUtils.h"
17#include "AMDGPUTargetMachine.h"
18#include "llvm/Analysis/AssumptionCache.h"
19#include "llvm/Analysis/Loads.h"
20#include "llvm/Analysis/UniformityAnalysis.h"
21#include "llvm/Analysis/ValueTracking.h"
22#include "llvm/CodeGen/TargetPassConfig.h"
23#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/InstVisitor.h"
25#include "llvm/IR/IntrinsicsAMDGPU.h"
26#include "llvm/InitializePasses.h"
27#include "llvm/Transforms/Utils/Local.h"
28
29#define DEBUG_TYPE "amdgpu-late-codegenprepare"
30
31using namespace llvm;
32
33namespace {
34
35class AMDGPULateCodeGenPrepare
36 : public InstVisitor<AMDGPULateCodeGenPrepare, bool> {
37 Function &F;
38 const DataLayout &DL;
39 const GCNSubtarget &ST;
40
41 AssumptionCache *const AC;
42 UniformityInfo &UA;
43
44 SmallVector<WeakTrackingVH, 8> DeadInsts;
45
46public:
47 AMDGPULateCodeGenPrepare(Function &F, const GCNSubtarget &ST,
48 AssumptionCache *AC, UniformityInfo &UA)
49 : F(F), DL(F.getDataLayout()), ST(ST), AC(AC), UA(UA) {}
50 bool run();
51 bool visitInstruction(Instruction &) { return false; }
52
53 // Widening may read padding bytes past the original access, so require the
54 // whole AccessSize-byte range at Base to be dereferenceable, not just Base
55 // itself aligned.
56 bool isSafeToWidenLoad(const Value *Base, uint64_t AccessSize,
57 const Instruction *CtxI) const {
58 return isDereferenceableAndAlignedPointer(
59 V: Base, Alignment: Align(4),
60 Size: APInt(DL.getIndexTypeSizeInBits(Ty: Base->getType()), AccessSize),
61 Q: SimplifyQuery(DL, /*TLI=*/nullptr, /*DT=*/nullptr, AC, CtxI));
62 }
63
64 bool canWidenScalarExtLoad(LoadInst &LI) const;
65 bool visitLoadInst(LoadInst &LI);
66};
67
68using ValueToValueMap = DenseMap<const Value *, Value *>;
69
70class LiveRegOptimizer {
71private:
72 Module &Mod;
73 const DataLayout &DL;
74 const GCNSubtarget &ST;
75
76 /// The scalar type to convert to
77 Type *const ConvertToScalar;
78 /// Map of Value -> Converted Value
79 ValueToValueMap ValMap;
80 /// Map of containing conversions from Optimal Type -> Original Type per BB.
81 DenseMap<BasicBlock *, ValueToValueMap> BBUseValMap;
82
83public:
84 /// Calculate the and \p return the type to convert to given a problematic \p
85 /// OriginalType. In some instances, we may widen the type (e.g. v2i8 -> i32).
86 Type *calculateConvertType(Type *OriginalType);
87 /// Convert the virtual register defined by \p V to the compatible vector of
88 /// legal type
89 Value *convertToOptType(Instruction *V, BasicBlock::iterator &InstPt);
90 /// Convert the virtual register defined by \p V back to the original type \p
91 /// ConvertType, stripping away the MSBs in cases where there was an imperfect
92 /// fit (e.g. v2i32 -> v7i8)
93 Value *convertFromOptType(Type *ConvertType, Instruction *V,
94 BasicBlock::iterator &InstPt,
95 BasicBlock *InsertBlock);
96 /// Check for problematic PHI nodes or cross-bb values based on the value
97 /// defined by \p I, and coerce to legal types if necessary. For problematic
98 /// PHI node, we coerce all incoming values in a single invocation.
99 bool optimizeLiveType(Instruction *I,
100 SmallVectorImpl<WeakTrackingVH> &DeadInsts);
101
102 // Whether or not the type should be replaced to avoid inefficient
103 // legalization code
104 bool shouldReplace(Type *ITy) {
105 FixedVectorType *VTy = dyn_cast<FixedVectorType>(Val: ITy);
106 if (!VTy)
107 return false;
108
109 const auto *TLI = ST.getTargetLowering();
110
111 Type *EltTy = VTy->getElementType();
112 // If the element size is not is not a multiple scalar size, then we can't
113 // do any bit packing
114 if (!EltTy->isIntegerTy() ||
115 ConvertToScalar->getScalarSizeInBits() % EltTy->getScalarSizeInBits())
116 return false;
117
118 // Only coerce illegal types
119 TargetLoweringBase::LegalizeKind LK =
120 TLI->getTypeConversion(Context&: EltTy->getContext(), VT: EVT::getEVT(Ty: EltTy, HandleUnknown: false));
121 return LK.first != TargetLoweringBase::TypeLegal;
122 }
123
124 bool isOpLegal(const Instruction *I) {
125 if (isa<IntrinsicInst>(Val: I))
126 return true;
127
128 // Any store is a profitable sink (prevents flip-flopping)
129 if (isa<StoreInst>(Val: I))
130 return true;
131
132 if (auto *BO = dyn_cast<BinaryOperator>(Val: I)) {
133 if (auto *VT = dyn_cast<FixedVectorType>(Val: BO->getType())) {
134 if (const auto *IT = dyn_cast<IntegerType>(Val: VT->getElementType())) {
135 unsigned EB = IT->getBitWidth();
136 unsigned EC = VT->getNumElements();
137 // Check for SDWA-compatible operation
138 if ((EB == 8 || EB == 16) && ST.hasSDWA() && EC * EB <= 32) {
139 switch (BO->getOpcode()) {
140 case Instruction::Add:
141 case Instruction::Sub:
142 case Instruction::And:
143 case Instruction::Or:
144 case Instruction::Xor:
145 return true;
146 default:
147 break;
148 }
149 }
150 }
151 }
152 }
153
154 return false;
155 }
156
157 bool isCoercionProfitable(Instruction *II) {
158 SmallPtrSet<Instruction *, 4> CVisited;
159 SmallVector<Instruction *, 4> UserList;
160
161 // Check users for profitable conditions (across block user which can
162 // natively handle the illegal vector).
163 for (User *V : II->users())
164 if (auto *UseInst = dyn_cast<Instruction>(Val: V))
165 UserList.push_back(Elt: UseInst);
166
167 auto IsLookThru = [](Instruction *II) {
168 if (const auto *Intr = dyn_cast<IntrinsicInst>(Val: II))
169 return Intr->getIntrinsicID() == Intrinsic::amdgcn_perm;
170 return isa<PHINode, ShuffleVectorInst, InsertElementInst,
171 ExtractElementInst, CastInst, FreezeInst>(Val: II);
172 };
173
174 while (!UserList.empty()) {
175 auto CII = UserList.pop_back_val();
176 if (!CVisited.insert(Ptr: CII).second)
177 continue;
178
179 // Same-BB filter must look at the *user*; and allow non-lookthrough
180 // users when the def is a PHI (loop-header pattern).
181 if (CII->getParent() == II->getParent() && !IsLookThru(CII) &&
182 !isa<PHINode>(Val: II))
183 continue;
184
185 if (isOpLegal(I: CII))
186 return true;
187
188 if (IsLookThru(CII))
189 for (User *V : CII->users())
190 if (auto *UseInst = dyn_cast<Instruction>(Val: V))
191 UserList.push_back(Elt: UseInst);
192 }
193 return false;
194 }
195
196 LiveRegOptimizer(Module &Mod, const GCNSubtarget &ST)
197 : Mod(Mod), DL(Mod.getDataLayout()), ST(ST),
198 ConvertToScalar(Type::getInt32Ty(C&: Mod.getContext())) {}
199};
200
201} // end anonymous namespace
202
203bool AMDGPULateCodeGenPrepare::run() {
204 // "Optimize" the virtual regs that cross basic block boundaries. When
205 // building the SelectionDAG, vectors of illegal types that cross basic blocks
206 // will be scalarized and widened, with each scalar living in its
207 // own register. To work around this, this optimization converts the
208 // vectors to equivalent vectors of legal type (which are converted back
209 // before uses in subsequent blocks), to pack the bits into fewer physical
210 // registers (used in CopyToReg/CopyFromReg pairs).
211 LiveRegOptimizer LRO(*F.getParent(), ST);
212
213 bool Changed = false;
214
215 bool HasScalarSubwordLoads = ST.hasScalarSubwordLoads();
216
217 for (auto &BB : reverse(C&: F))
218 for (Instruction &I : make_early_inc_range(Range: reverse(C&: BB))) {
219 Changed |= !HasScalarSubwordLoads && visit(I);
220 Changed |= LRO.optimizeLiveType(I: &I, DeadInsts);
221 }
222
223 RecursivelyDeleteTriviallyDeadInstructionsPermissive(DeadInsts);
224 return Changed;
225}
226
227Type *LiveRegOptimizer::calculateConvertType(Type *OriginalType) {
228 assert(OriginalType->getScalarSizeInBits() <=
229 ConvertToScalar->getScalarSizeInBits());
230
231 FixedVectorType *VTy = cast<FixedVectorType>(Val: OriginalType);
232
233 TypeSize OriginalSize = DL.getTypeSizeInBits(Ty: VTy);
234 TypeSize ConvertScalarSize = DL.getTypeSizeInBits(Ty: ConvertToScalar);
235 unsigned ConvertEltCount =
236 (OriginalSize + ConvertScalarSize - 1) / ConvertScalarSize;
237
238 if (OriginalSize <= ConvertScalarSize)
239 return IntegerType::get(C&: Mod.getContext(), NumBits: ConvertScalarSize);
240
241 return VectorType::get(ElementType: Type::getIntNTy(C&: Mod.getContext(), N: ConvertScalarSize),
242 NumElements: ConvertEltCount, Scalable: false);
243}
244
245Value *LiveRegOptimizer::convertToOptType(Instruction *V,
246 BasicBlock::iterator &InsertPt) {
247 FixedVectorType *VTy = cast<FixedVectorType>(Val: V->getType());
248 Type *NewTy = calculateConvertType(OriginalType: V->getType());
249
250 TypeSize OriginalSize = DL.getTypeSizeInBits(Ty: VTy);
251 TypeSize NewSize = DL.getTypeSizeInBits(Ty: NewTy);
252
253 IRBuilder<> Builder(InsertPt);
254 // If there is a bitsize match, we can fit the old vector into a new vector of
255 // desired type.
256 if (OriginalSize == NewSize)
257 return Builder.CreateBitCast(V, DestTy: NewTy, Name: V->getName() + ".bc");
258
259 // If there is a bitsize mismatch, we must use a wider vector.
260 assert(NewSize > OriginalSize);
261 uint64_t ExpandedVecElementCount = NewSize / VTy->getScalarSizeInBits();
262
263 SmallVector<int, 8> ShuffleMask;
264 uint64_t OriginalElementCount = VTy->getElementCount().getFixedValue();
265 for (unsigned I = 0; I < OriginalElementCount; I++)
266 ShuffleMask.push_back(Elt: I);
267
268 for (uint64_t I = OriginalElementCount; I < ExpandedVecElementCount; I++)
269 ShuffleMask.push_back(Elt: OriginalElementCount);
270
271 Value *ExpandedVec = Builder.CreateShuffleVector(V, Mask: ShuffleMask);
272 return Builder.CreateBitCast(V: ExpandedVec, DestTy: NewTy, Name: V->getName() + ".bc");
273}
274
275Value *LiveRegOptimizer::convertFromOptType(Type *ConvertType, Instruction *V,
276 BasicBlock::iterator &InsertPt,
277 BasicBlock *InsertBB) {
278 FixedVectorType *NewVTy = cast<FixedVectorType>(Val: ConvertType);
279
280 TypeSize OriginalSize = DL.getTypeSizeInBits(Ty: V->getType());
281 TypeSize NewSize = DL.getTypeSizeInBits(Ty: NewVTy);
282
283 IRBuilder<> Builder(InsertPt);
284 // If there is a bitsize match, we simply convert back to the original type.
285 if (OriginalSize == NewSize)
286 return Builder.CreateBitCast(V, DestTy: NewVTy, Name: V->getName() + ".bc");
287
288 // If there is a bitsize mismatch, then we must have used a wider value to
289 // hold the bits.
290 assert(OriginalSize > NewSize);
291 // For wide scalars, we can just truncate the value.
292 if (!V->getType()->isVectorTy()) {
293 Instruction *Trunc = cast<Instruction>(
294 Val: Builder.CreateTrunc(V, DestTy: IntegerType::get(C&: Mod.getContext(), NumBits: NewSize)));
295 return cast<Instruction>(Val: Builder.CreateBitCast(V: Trunc, DestTy: NewVTy));
296 }
297
298 // For wider vectors, we must strip the MSBs to convert back to the original
299 // type.
300 VectorType *ExpandedVT = VectorType::get(
301 ElementType: Type::getIntNTy(C&: Mod.getContext(), N: NewVTy->getScalarSizeInBits()),
302 NumElements: (OriginalSize / NewVTy->getScalarSizeInBits()), Scalable: false);
303 Instruction *Converted =
304 cast<Instruction>(Val: Builder.CreateBitCast(V, DestTy: ExpandedVT));
305
306 unsigned NarrowElementCount = NewVTy->getElementCount().getFixedValue();
307 SmallVector<int, 8> ShuffleMask(NarrowElementCount);
308 std::iota(first: ShuffleMask.begin(), last: ShuffleMask.end(), value: 0);
309
310 return Builder.CreateShuffleVector(V: Converted, Mask: ShuffleMask);
311}
312
313bool LiveRegOptimizer::optimizeLiveType(
314 Instruction *I, SmallVectorImpl<WeakTrackingVH> &DeadInsts) {
315 SmallVector<Instruction *, 4> Worklist;
316 SmallPtrSet<PHINode *, 4> PhiNodes;
317 SmallPtrSet<Instruction *, 4> Defs;
318 SmallPtrSet<Instruction *, 4> Uses;
319 SmallPtrSet<Instruction *, 4> Visited;
320
321 Worklist.push_back(Elt: cast<Instruction>(Val: I));
322 while (!Worklist.empty()) {
323 Instruction *II = Worklist.pop_back_val();
324
325 if (!Visited.insert(Ptr: II).second)
326 continue;
327
328 if (!shouldReplace(ITy: II->getType()))
329 continue;
330
331 if (!isCoercionProfitable(II))
332 continue;
333
334 if (PHINode *Phi = dyn_cast<PHINode>(Val: II)) {
335 PhiNodes.insert(Ptr: Phi);
336 // Collect all the incoming values of problematic PHI nodes.
337 for (Value *V : Phi->incoming_values()) {
338 // Repeat the collection process for newly found PHI nodes.
339 if (PHINode *OpPhi = dyn_cast<PHINode>(Val: V)) {
340 if (!PhiNodes.count(Ptr: OpPhi) && !Visited.count(Ptr: OpPhi))
341 Worklist.push_back(Elt: OpPhi);
342 continue;
343 }
344
345 Instruction *IncInst = dyn_cast<Instruction>(Val: V);
346 // Other incoming value types (e.g. vector literals) are unhandled
347 if (!IncInst && !isa<ConstantAggregateZero>(Val: V))
348 return false;
349
350 // Collect all other incoming values for coercion.
351 if (IncInst && !IncInst->isTerminator())
352 Defs.insert(Ptr: IncInst);
353 }
354 }
355
356 // Collect all relevant uses.
357 for (User *V : II->users()) {
358 // Repeat the collection process for problematic PHI nodes.
359 if (PHINode *OpPhi = dyn_cast<PHINode>(Val: V)) {
360 if (!PhiNodes.count(Ptr: OpPhi) && !Visited.count(Ptr: OpPhi))
361 Worklist.push_back(Elt: OpPhi);
362 continue;
363 }
364
365 Instruction *UseInst = cast<Instruction>(Val: V);
366 // Collect all uses of PHINodes and any use the crosses BB boundaries.
367 if (UseInst->getParent() != II->getParent() || isa<PHINode>(Val: II)) {
368 Uses.insert(Ptr: UseInst);
369 if (!isa<PHINode>(Val: II) && !II->isTerminator())
370 Defs.insert(Ptr: II);
371 }
372 }
373 }
374
375 // Coerce and track the defs.
376 for (Instruction *D : Defs) {
377 if (!ValMap.contains(Val: D)) {
378 BasicBlock::iterator InsertPt = std::next(x: D->getIterator());
379 Value *ConvertVal = convertToOptType(V: D, InsertPt);
380 assert(ConvertVal);
381 ValMap[D] = ConvertVal;
382 }
383 }
384
385 // Construct new-typed PHI nodes.
386 for (PHINode *Phi : PhiNodes) {
387 ValMap[Phi] = PHINode::Create(Ty: calculateConvertType(OriginalType: Phi->getType()),
388 NumReservedValues: Phi->getNumIncomingValues(),
389 NameStr: Phi->getName() + ".tc", InsertBefore: Phi->getIterator());
390 }
391
392 // Connect all the PHI nodes with their new incoming values.
393 for (PHINode *Phi : PhiNodes) {
394 PHINode *NewPhi = cast<PHINode>(Val: ValMap[Phi]);
395 bool MissingIncVal = false;
396 for (int I = 0, E = Phi->getNumIncomingValues(); I < E; I++) {
397 Value *IncVal = Phi->getIncomingValue(i: I);
398 if (isa<ConstantAggregateZero>(Val: IncVal)) {
399 Type *NewType = calculateConvertType(OriginalType: Phi->getType());
400 NewPhi->addIncoming(V: ConstantInt::get(Ty: NewType, V: 0, IsSigned: false),
401 BB: Phi->getIncomingBlock(i: I));
402 } else if (Value *Val = ValMap.lookup(Val: IncVal))
403 NewPhi->addIncoming(V: Val, BB: Phi->getIncomingBlock(i: I));
404 else
405 MissingIncVal = true;
406 }
407 if (MissingIncVal) {
408 Value *DeadVal = ValMap[Phi];
409 // The coercion chain of the PHI is broken. Delete the Phi
410 // from the ValMap and any connected / user Phis.
411 SmallVector<Value *, 4> PHIWorklist;
412 SmallPtrSet<Value *, 4> VisitedPhis;
413 PHIWorklist.push_back(Elt: DeadVal);
414 while (!PHIWorklist.empty()) {
415 Value *NextDeadValue = PHIWorklist.pop_back_val();
416 VisitedPhis.insert(Ptr: NextDeadValue);
417 auto OriginalPhi =
418 llvm::find_if(Range&: PhiNodes, P: [this, &NextDeadValue](PHINode *CandPhi) {
419 return ValMap[CandPhi] == NextDeadValue;
420 });
421 // This PHI may have already been removed from maps when
422 // unwinding a previous Phi
423 if (OriginalPhi != PhiNodes.end())
424 ValMap.erase(Val: *OriginalPhi);
425
426 for (User *U : NextDeadValue->users()) {
427 if (!VisitedPhis.contains(Ptr: cast<PHINode>(Val: U)))
428 PHIWorklist.push_back(Elt: U);
429 }
430 NextDeadValue->replaceAllUsesWith(
431 V: PoisonValue::get(T: NextDeadValue->getType()));
432
433 DeadInsts.emplace_back(Args: cast<Instruction>(Val: NextDeadValue));
434 }
435 } else {
436 DeadInsts.emplace_back(Args: cast<Instruction>(Val: Phi));
437 }
438 }
439 // Coerce back to the original type and replace the uses.
440 for (Instruction *U : Uses) {
441 // Replace all converted operands for a use.
442 for (auto [OpIdx, Op] : enumerate(First: U->operands())) {
443 if (Value *Val = ValMap.lookup(Val: Op)) {
444 Value *NewVal = nullptr;
445 if (BBUseValMap.contains(Val: U->getParent()) &&
446 BBUseValMap[U->getParent()].contains(Val))
447 NewVal = BBUseValMap[U->getParent()][Val];
448 else {
449 // Not getFirstNonPHIIt, which would insert in front of a landingpad.
450 BasicBlock::iterator InsertPt = U->getParent()->getFirstInsertionPt();
451 // We may pick up ops that were previously converted for users in
452 // other blocks. If there is an originally typed definition of the Op
453 // already in this block, simply reuse it.
454 if (isa<Instruction>(Val: Op) && !isa<PHINode>(Val: Op) &&
455 U->getParent() == cast<Instruction>(Val&: Op)->getParent()) {
456 NewVal = Op;
457 } else {
458 NewVal =
459 convertFromOptType(ConvertType: Op->getType(), V: cast<Instruction>(Val: ValMap[Op]),
460 InsertPt, InsertBB: U->getParent());
461 BBUseValMap[U->getParent()][ValMap[Op]] = NewVal;
462 }
463 }
464 assert(NewVal);
465 U->setOperand(i: OpIdx, Val: NewVal);
466 }
467 }
468 }
469
470 return true;
471}
472
473bool AMDGPULateCodeGenPrepare::canWidenScalarExtLoad(LoadInst &LI) const {
474 unsigned AS = LI.getPointerAddressSpace();
475 // Skip non-constant address space.
476 if (AS != AMDGPUAS::CONSTANT_ADDRESS &&
477 AS != AMDGPUAS::CONSTANT_ADDRESS_32BIT)
478 return false;
479 // Skip non-simple loads.
480 if (!LI.isSimple())
481 return false;
482 Type *Ty = LI.getType();
483 // Skip aggregate types.
484 if (Ty->isAggregateType())
485 return false;
486 unsigned TySize = DL.getTypeStoreSize(Ty);
487 // Only handle sub-DWORD loads.
488 if (TySize >= 4)
489 return false;
490 // That load must be at least naturally aligned.
491 if (LI.getAlign() < DL.getABITypeAlign(Ty))
492 return false;
493 // It should be uniform, i.e. a scalar load.
494 return UA.isUniformAtDef(V: &LI);
495}
496
497bool AMDGPULateCodeGenPrepare::visitLoadInst(LoadInst &LI) {
498 if (!ST.getCLOpts().late_codegenprepare_widen_constant_loads)
499 return false;
500
501 // Skip if that load is already aligned on DWORD at least as it's handled in
502 // SDAG.
503 if (LI.getAlign() >= 4)
504 return false;
505
506 if (!canWidenScalarExtLoad(LI))
507 return false;
508
509 int64_t Offset = 0;
510 auto *Base =
511 GetPointerBaseWithConstantOffset(Ptr: LI.getPointerOperand(), Offset, DL);
512
513 int64_t Adjust = Offset & 0x3;
514 int64_t AccessOffset = Offset - Adjust;
515 if (AccessOffset < 0 || !isSafeToWidenLoad(Base, AccessSize: AccessOffset + 4, CtxI: &LI))
516 return false;
517
518 IRBuilder<> IRB(&LI);
519 IRB.SetCurrentDebugLocation(LI.getDebugLoc());
520
521 unsigned LdBits = DL.getTypeStoreSizeInBits(Ty: LI.getType());
522 auto *IntNTy = Type::getIntNTy(C&: LI.getContext(), N: LdBits);
523
524 auto *NewPtr = IRB.CreateConstGEP1_64(
525 Ty: IRB.getInt8Ty(),
526 Ptr: IRB.CreateAddrSpaceCast(V: Base, DestTy: LI.getPointerOperand()->getType()),
527 Idx0: Offset - Adjust);
528
529 LoadInst *NewLd = IRB.CreateAlignedLoad(Ty: IRB.getInt32Ty(), Ptr: NewPtr, Align: Align(4));
530 AMDGPU::copyMetadataForWidenedLoad(Dest&: *NewLd, Source: LI);
531
532 unsigned ShAmt = Adjust * 8;
533 Value *Shifted = ShAmt ? IRB.CreateLShr(LHS: NewLd, RHS: ShAmt) : NewLd;
534 Value *NewVal = IRB.CreateBitCast(
535 V: IRB.CreateTrunc(V: Shifted, DestTy: DL.typeSizeEqualsStoreSize(Ty: LI.getType())
536 ? IntNTy
537 : LI.getType()),
538 DestTy: LI.getType());
539 LI.replaceAllUsesWith(V: NewVal);
540 DeadInsts.emplace_back(Args: &LI);
541
542 return true;
543}
544
545PreservedAnalyses
546AMDGPULateCodeGenPreparePass::run(Function &F, FunctionAnalysisManager &FAM) {
547 const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
548 AssumptionCache &AC = FAM.getResult<AssumptionAnalysis>(IR&: F);
549 UniformityInfo &UI = FAM.getResult<UniformityInfoAnalysis>(IR&: F);
550
551 bool Changed = AMDGPULateCodeGenPrepare(F, ST, &AC, UI).run();
552
553 if (!Changed)
554 return PreservedAnalyses::all();
555 PreservedAnalyses PA = PreservedAnalyses::none();
556 PA.preserveSet<CFGAnalyses>();
557 return PA;
558}
559
560class AMDGPULateCodeGenPrepareLegacy : public FunctionPass {
561public:
562 static char ID;
563
564 AMDGPULateCodeGenPrepareLegacy() : FunctionPass(ID) {}
565
566 StringRef getPassName() const override {
567 return "AMDGPU IR late optimizations";
568 }
569
570 void getAnalysisUsage(AnalysisUsage &AU) const override {
571 AU.addRequired<TargetPassConfig>();
572 AU.addRequired<AssumptionCacheTracker>();
573 AU.addRequired<UniformityInfoWrapperPass>();
574 // Invalidates UniformityInfo
575 AU.setPreservesCFG();
576 }
577
578 bool runOnFunction(Function &F) override;
579};
580
581bool AMDGPULateCodeGenPrepareLegacy::runOnFunction(Function &F) {
582 if (skipFunction(F))
583 return false;
584
585 const TargetPassConfig &TPC = getAnalysis<TargetPassConfig>();
586 const TargetMachine &TM = TPC.getTM<TargetMachine>();
587 const GCNSubtarget &ST = TM.getSubtarget<GCNSubtarget>(F);
588
589 AssumptionCache &AC =
590 getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
591 UniformityInfo &UI =
592 getAnalysis<UniformityInfoWrapperPass>().getUniformityInfo();
593
594 return AMDGPULateCodeGenPrepare(F, ST, &AC, UI).run();
595}
596
597INITIALIZE_PASS_BEGIN(AMDGPULateCodeGenPrepareLegacy, DEBUG_TYPE,
598 "AMDGPU IR late optimizations", false, false)
599INITIALIZE_PASS_DEPENDENCY(TargetPassConfig)
600INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
601INITIALIZE_PASS_DEPENDENCY(UniformityInfoWrapperPass)
602INITIALIZE_PASS_END(AMDGPULateCodeGenPrepareLegacy, DEBUG_TYPE,
603 "AMDGPU IR late optimizations", false, false)
604
605char AMDGPULateCodeGenPrepareLegacy::ID = 0;
606
607FunctionPass *llvm::createAMDGPULateCodeGenPrepareLegacyPass() {
608 return new AMDGPULateCodeGenPrepareLegacy();
609}
610