1 //===-- AMDGPUAlwaysInlinePass.cpp - Promote Allocas ----------------------===// 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 /// \file 11 /// This pass marks all internal functions as always_inline and creates 12 /// duplicates of all other functions a marks the duplicates as always_inline. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "AMDGPU.h" 17 #include "llvm/IR/Module.h" 18 #include "llvm/Transforms/Utils/Cloning.h" 19 20 using namespace llvm; 21 22 namespace { 23 24 class AMDGPUAlwaysInline : public ModulePass { 25 bool GlobalOpt; 26 27 public: 28 static char ID; 29 30 AMDGPUAlwaysInline(bool GlobalOpt = false) : 31 ModulePass(ID), GlobalOpt(GlobalOpt) { } 32 bool runOnModule(Module &M) override; 33 StringRef getPassName() const override { return "AMDGPU Always Inline Pass"; } 34 }; 35 36 } // End anonymous namespace 37 38 INITIALIZE_PASS(AMDGPUAlwaysInline, "amdgpu-always-inline", 39 "AMDGPU Inline All Functions", false, false) 40 41 char AMDGPUAlwaysInline::ID = 0; 42 43 bool AMDGPUAlwaysInline::runOnModule(Module &M) { 44 std::vector<GlobalAlias*> AliasesToRemove; 45 std::vector<Function *> FuncsToClone; 46 47 for (GlobalAlias &A : M.aliases()) { 48 if (Function* F = dyn_cast<Function>(A.getAliasee())) { 49 A.replaceAllUsesWith(F); 50 AliasesToRemove.push_back(&A); 51 } 52 } 53 54 if (GlobalOpt) { 55 for (GlobalAlias* A : AliasesToRemove) { 56 A->eraseFromParent(); 57 } 58 } 59 60 for (Function &F : M) { 61 if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() && 62 !F.hasFnAttribute(Attribute::NoInline)) 63 FuncsToClone.push_back(&F); 64 } 65 66 for (Function *F : FuncsToClone) { 67 ValueToValueMapTy VMap; 68 Function *NewFunc = CloneFunction(F, VMap); 69 NewFunc->setLinkage(GlobalValue::InternalLinkage); 70 F->replaceAllUsesWith(NewFunc); 71 } 72 73 for (Function &F : M) { 74 if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) { 75 F.addFnAttr(Attribute::AlwaysInline); 76 } 77 } 78 return false; 79 } 80 81 ModulePass *llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt) { 82 return new AMDGPUAlwaysInline(GlobalOpt); 83 } 84