Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 1 | //===-- AMDGPULowerIntrinsics.cpp -----------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "AMDGPU.h" |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 11 | #include "AMDGPUSubtarget.h" |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 12 | #include "llvm/CodeGen/TargetPassConfig.h" |
Sean Fertile | 9cd1cdf | 2017-07-07 02:00:06 +0000 | [diff] [blame] | 13 | #include "llvm/Analysis/TargetTransformInfo.h" |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 14 | #include "llvm/IR/Constants.h" |
| 15 | #include "llvm/IR/Instructions.h" |
| 16 | #include "llvm/IR/IntrinsicInst.h" |
| 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/Transforms/Utils/LowerMemIntrinsics.h" |
| 19 | |
| 20 | #define DEBUG_TYPE "amdgpu-lower-intrinsics" |
| 21 | |
| 22 | using namespace llvm; |
| 23 | |
| 24 | namespace { |
| 25 | |
| 26 | const unsigned MaxStaticSize = 1024; |
| 27 | |
| 28 | class AMDGPULowerIntrinsics : public ModulePass { |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 29 | private: |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 30 | bool makeLIDRangeMetadata(Function &F) const; |
| 31 | |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 32 | public: |
| 33 | static char ID; |
| 34 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 35 | AMDGPULowerIntrinsics() : ModulePass(ID) {} |
| 36 | |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 37 | bool runOnModule(Module &M) override; |
Sean Fertile | 9cd1cdf | 2017-07-07 02:00:06 +0000 | [diff] [blame] | 38 | bool expandMemIntrinsicUses(Function &F); |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 39 | StringRef getPassName() const override { |
| 40 | return "AMDGPU Lower Intrinsics"; |
| 41 | } |
Sean Fertile | 9cd1cdf | 2017-07-07 02:00:06 +0000 | [diff] [blame] | 42 | |
| 43 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 44 | AU.addRequired<TargetTransformInfoWrapperPass>(); |
| 45 | } |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | } |
| 49 | |
| 50 | char AMDGPULowerIntrinsics::ID = 0; |
| 51 | |
| 52 | char &llvm::AMDGPULowerIntrinsicsID = AMDGPULowerIntrinsics::ID; |
| 53 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 54 | INITIALIZE_PASS(AMDGPULowerIntrinsics, DEBUG_TYPE, "Lower intrinsics", false, |
| 55 | false) |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 56 | |
| 57 | // TODO: Should refine based on estimated number of accesses (e.g. does it |
| 58 | // require splitting based on alignment) |
| 59 | static bool shouldExpandOperationWithSize(Value *Size) { |
| 60 | ConstantInt *CI = dyn_cast<ConstantInt>(Size); |
| 61 | return !CI || (CI->getZExtValue() > MaxStaticSize); |
| 62 | } |
| 63 | |
Sean Fertile | 9cd1cdf | 2017-07-07 02:00:06 +0000 | [diff] [blame] | 64 | bool AMDGPULowerIntrinsics::expandMemIntrinsicUses(Function &F) { |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 65 | Intrinsic::ID ID = F.getIntrinsicID(); |
NAKAMURA Takumi | 022c6e4 | 2017-02-12 13:15:31 +0000 | [diff] [blame] | 66 | bool Changed = false; |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 67 | |
| 68 | for (auto I = F.user_begin(), E = F.user_end(); I != E;) { |
| 69 | Instruction *Inst = cast<Instruction>(*I); |
| 70 | ++I; |
| 71 | |
| 72 | switch (ID) { |
| 73 | case Intrinsic::memcpy: { |
| 74 | auto *Memcpy = cast<MemCpyInst>(Inst); |
| 75 | if (shouldExpandOperationWithSize(Memcpy->getLength())) { |
Sean Fertile | 9cd1cdf | 2017-07-07 02:00:06 +0000 | [diff] [blame] | 76 | Function *ParentFunc = Memcpy->getParent()->getParent(); |
| 77 | const TargetTransformInfo &TTI = |
| 78 | getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*ParentFunc); |
| 79 | expandMemCpyAsLoop(Memcpy, TTI); |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 80 | Changed = true; |
| 81 | Memcpy->eraseFromParent(); |
| 82 | } |
| 83 | |
| 84 | break; |
| 85 | } |
| 86 | case Intrinsic::memmove: { |
| 87 | auto *Memmove = cast<MemMoveInst>(Inst); |
| 88 | if (shouldExpandOperationWithSize(Memmove->getLength())) { |
| 89 | expandMemMoveAsLoop(Memmove); |
| 90 | Changed = true; |
| 91 | Memmove->eraseFromParent(); |
| 92 | } |
| 93 | |
| 94 | break; |
| 95 | } |
| 96 | case Intrinsic::memset: { |
| 97 | auto *Memset = cast<MemSetInst>(Inst); |
| 98 | if (shouldExpandOperationWithSize(Memset->getLength())) { |
| 99 | expandMemSetAsLoop(Memset); |
| 100 | Changed = true; |
| 101 | Memset->eraseFromParent(); |
| 102 | } |
| 103 | |
| 104 | break; |
| 105 | } |
| 106 | default: |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | return Changed; |
| 112 | } |
| 113 | |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 114 | bool AMDGPULowerIntrinsics::makeLIDRangeMetadata(Function &F) const { |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 115 | auto *TPC = getAnalysisIfAvailable<TargetPassConfig>(); |
| 116 | if (!TPC) |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 117 | return false; |
| 118 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 119 | const TargetMachine &TM = TPC->getTM<TargetMachine>(); |
| 120 | const AMDGPUSubtarget &ST = TM.getSubtarget<AMDGPUSubtarget>(F); |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 121 | bool Changed = false; |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 122 | |
| 123 | for (auto *U : F.users()) { |
| 124 | auto *CI = dyn_cast<CallInst>(U); |
| 125 | if (!CI) |
| 126 | continue; |
| 127 | |
| 128 | Changed |= ST.makeLIDRangeMetadata(CI); |
| 129 | } |
| 130 | return Changed; |
| 131 | } |
| 132 | |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 133 | bool AMDGPULowerIntrinsics::runOnModule(Module &M) { |
| 134 | bool Changed = false; |
| 135 | |
| 136 | for (Function &F : M) { |
| 137 | if (!F.isDeclaration()) |
| 138 | continue; |
| 139 | |
| 140 | switch (F.getIntrinsicID()) { |
| 141 | case Intrinsic::memcpy: |
| 142 | case Intrinsic::memmove: |
| 143 | case Intrinsic::memset: |
| 144 | if (expandMemIntrinsicUses(F)) |
| 145 | Changed = true; |
| 146 | break; |
Stanislav Mekhanoshin | c90347d | 2017-04-12 20:48:56 +0000 | [diff] [blame] | 147 | |
| 148 | case Intrinsic::amdgcn_workitem_id_x: |
| 149 | case Intrinsic::r600_read_tidig_x: |
| 150 | case Intrinsic::amdgcn_workitem_id_y: |
| 151 | case Intrinsic::r600_read_tidig_y: |
| 152 | case Intrinsic::amdgcn_workitem_id_z: |
| 153 | case Intrinsic::r600_read_tidig_z: |
| 154 | case Intrinsic::r600_read_local_size_x: |
| 155 | case Intrinsic::r600_read_local_size_y: |
| 156 | case Intrinsic::r600_read_local_size_z: |
| 157 | Changed |= makeLIDRangeMetadata(F); |
| 158 | break; |
| 159 | |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 160 | default: |
| 161 | break; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | return Changed; |
| 166 | } |
| 167 | |
Francis Visoiu Mistrih | 8b61764 | 2017-05-18 17:21:13 +0000 | [diff] [blame] | 168 | ModulePass *llvm::createAMDGPULowerIntrinsicsPass() { |
| 169 | return new AMDGPULowerIntrinsics(); |
Matt Arsenault | 0699ef3 | 2017-02-09 22:00:42 +0000 | [diff] [blame] | 170 | } |