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/IR/Constants.h"
13 #include "llvm/IR/Instructions.h"
14 #include "llvm/IR/IntrinsicInst.h"
15 #include "llvm/IR/Module.h"
16 #include "llvm/Transforms/Utils/LowerMemIntrinsics.h"
18 #define DEBUG_TYPE "amdgpu-lower-intrinsics"
24 const unsigned MaxStaticSize = 1024;
26 class AMDGPULowerIntrinsics : public ModulePass {
28 const TargetMachine *TM;
30 bool makeLIDRangeMetadata(Function &F) const;
35 AMDGPULowerIntrinsics(const TargetMachine *TM = nullptr)
36 : ModulePass(ID), TM(TM) { }
37 bool runOnModule(Module &M) override;
38 StringRef getPassName() const override {
39 return "AMDGPU Lower Intrinsics";
45 char AMDGPULowerIntrinsics::ID = 0;
47 char &llvm::AMDGPULowerIntrinsicsID = AMDGPULowerIntrinsics::ID;
49 INITIALIZE_TM_PASS(AMDGPULowerIntrinsics, DEBUG_TYPE,
50 "Lower intrinsics", false, false)
52 // TODO: Should refine based on estimated number of accesses (e.g. does it
53 // require splitting based on alignment)
54 static bool shouldExpandOperationWithSize(Value *Size) {
55 ConstantInt *CI = dyn_cast<ConstantInt>(Size);
56 return !CI || (CI->getZExtValue() > MaxStaticSize);
59 static bool expandMemIntrinsicUses(Function &F) {
60 Intrinsic::ID ID = F.getIntrinsicID();
63 for (auto I = F.user_begin(), E = F.user_end(); I != E;) {
64 Instruction *Inst = cast<Instruction>(*I);
68 case Intrinsic::memcpy: {
69 auto *Memcpy = cast<MemCpyInst>(Inst);
70 if (shouldExpandOperationWithSize(Memcpy->getLength())) {
71 expandMemCpyAsLoop(Memcpy);
73 Memcpy->eraseFromParent();
78 case Intrinsic::memmove: {
79 auto *Memmove = cast<MemMoveInst>(Inst);
80 if (shouldExpandOperationWithSize(Memmove->getLength())) {
81 expandMemMoveAsLoop(Memmove);
83 Memmove->eraseFromParent();
88 case Intrinsic::memset: {
89 auto *Memset = cast<MemSetInst>(Inst);
90 if (shouldExpandOperationWithSize(Memset->getLength())) {
91 expandMemSetAsLoop(Memset);
93 Memset->eraseFromParent();
106 bool AMDGPULowerIntrinsics::makeLIDRangeMetadata(Function &F) const {
110 bool Changed = false;
111 const AMDGPUSubtarget &ST = TM->getSubtarget<AMDGPUSubtarget>(F);
113 for (auto *U : F.users()) {
114 auto *CI = dyn_cast<CallInst>(U);
118 Changed |= ST.makeLIDRangeMetadata(CI);
123 bool AMDGPULowerIntrinsics::runOnModule(Module &M) {
124 bool Changed = false;
126 for (Function &F : M) {
127 if (!F.isDeclaration())
130 switch (F.getIntrinsicID()) {
131 case Intrinsic::memcpy:
132 case Intrinsic::memmove:
133 case Intrinsic::memset:
134 if (expandMemIntrinsicUses(F))
138 case Intrinsic::amdgcn_workitem_id_x:
139 case Intrinsic::r600_read_tidig_x:
140 case Intrinsic::amdgcn_workitem_id_y:
141 case Intrinsic::r600_read_tidig_y:
142 case Intrinsic::amdgcn_workitem_id_z:
143 case Intrinsic::r600_read_tidig_z:
144 case Intrinsic::r600_read_local_size_x:
145 case Intrinsic::r600_read_local_size_y:
146 case Intrinsic::r600_read_local_size_z:
147 Changed |= makeLIDRangeMetadata(F);
158 ModulePass *llvm::createAMDGPULowerIntrinsicsPass(const TargetMachine *TM) {
159 return new AMDGPULowerIntrinsics(TM);