1 //===-- AMDGPULowerIntrinsics.cpp -----------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "AMDGPUSubtarget.h"
12 #include "llvm/CodeGen/TargetPassConfig.h"
13 #include "llvm/IR/Constants.h"
14 #include "llvm/IR/Instructions.h"
15 #include "llvm/IR/IntrinsicInst.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Transforms/Utils/LowerMemIntrinsics.h"
19 #define DEBUG_TYPE "amdgpu-lower-intrinsics"
25 const unsigned MaxStaticSize = 1024;
27 class AMDGPULowerIntrinsics : public ModulePass {
29 bool makeLIDRangeMetadata(Function &F) const;
34 AMDGPULowerIntrinsics() : ModulePass(ID) {}
36 bool runOnModule(Module &M) override;
37 StringRef getPassName() const override {
38 return "AMDGPU Lower Intrinsics";
44 char AMDGPULowerIntrinsics::ID = 0;
46 char &llvm::AMDGPULowerIntrinsicsID = AMDGPULowerIntrinsics::ID;
48 INITIALIZE_PASS(AMDGPULowerIntrinsics, DEBUG_TYPE, "Lower intrinsics", false,
51 // TODO: Should refine based on estimated number of accesses (e.g. does it
52 // require splitting based on alignment)
53 static bool shouldExpandOperationWithSize(Value *Size) {
54 ConstantInt *CI = dyn_cast<ConstantInt>(Size);
55 return !CI || (CI->getZExtValue() > MaxStaticSize);
58 static bool expandMemIntrinsicUses(Function &F) {
59 Intrinsic::ID ID = F.getIntrinsicID();
62 for (auto I = F.user_begin(), E = F.user_end(); I != E;) {
63 Instruction *Inst = cast<Instruction>(*I);
67 case Intrinsic::memcpy: {
68 auto *Memcpy = cast<MemCpyInst>(Inst);
69 if (shouldExpandOperationWithSize(Memcpy->getLength())) {
70 expandMemCpyAsLoop(Memcpy);
72 Memcpy->eraseFromParent();
77 case Intrinsic::memmove: {
78 auto *Memmove = cast<MemMoveInst>(Inst);
79 if (shouldExpandOperationWithSize(Memmove->getLength())) {
80 expandMemMoveAsLoop(Memmove);
82 Memmove->eraseFromParent();
87 case Intrinsic::memset: {
88 auto *Memset = cast<MemSetInst>(Inst);
89 if (shouldExpandOperationWithSize(Memset->getLength())) {
90 expandMemSetAsLoop(Memset);
92 Memset->eraseFromParent();
105 bool AMDGPULowerIntrinsics::makeLIDRangeMetadata(Function &F) const {
106 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
110 const TargetMachine &TM = TPC->getTM<TargetMachine>();
111 const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>(F);
112 bool Changed = false;
114 for (auto *U : F.users()) {
115 auto *CI = dyn_cast<CallInst>(U);
119 Changed |= ST.makeLIDRangeMetadata(CI);
124 bool AMDGPULowerIntrinsics::runOnModule(Module &M) {
125 bool Changed = false;
127 for (Function &F : M) {
128 if (!F.isDeclaration())
131 switch (F.getIntrinsicID()) {
132 case Intrinsic::memcpy:
133 case Intrinsic::memmove:
134 case Intrinsic::memset:
135 if (expandMemIntrinsicUses(F))
139 case Intrinsic::amdgcn_workitem_id_x:
140 case Intrinsic::r600_read_tidig_x:
141 case Intrinsic::amdgcn_workitem_id_y:
142 case Intrinsic::r600_read_tidig_y:
143 case Intrinsic::amdgcn_workitem_id_z:
144 case Intrinsic::r600_read_tidig_z:
145 case Intrinsic::r600_read_local_size_x:
146 case Intrinsic::r600_read_local_size_y:
147 case Intrinsic::r600_read_local_size_z:
148 Changed |= makeLIDRangeMetadata(F);
159 ModulePass *llvm::createAMDGPULowerIntrinsicsPass() {
160 return new AMDGPULowerIntrinsics();