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 static char ID; 26 27 bool GlobalOpt; 28 29 public: 30 AMDGPUAlwaysInline(bool GlobalOpt) : ModulePass(ID), GlobalOpt(GlobalOpt) { } 31 bool runOnModule(Module &M) override; 32 StringRef getPassName() const override { return "AMDGPU Always Inline Pass"; } 33 }; 34 35 } // End anonymous namespace 36 37 char AMDGPUAlwaysInline::ID = 0; 38 39 bool AMDGPUAlwaysInline::runOnModule(Module &M) { 40 std::vector<GlobalAlias*> AliasesToRemove; 41 std::vector<Function *> FuncsToClone; 42 43 for (GlobalAlias &A : M.aliases()) { 44 if (Function* F = dyn_cast<Function>(A.getAliasee())) { 45 A.replaceAllUsesWith(F); 46 AliasesToRemove.push_back(&A); 47 } 48 } 49 50 if (GlobalOpt) { 51 for (GlobalAlias* A : AliasesToRemove) { 52 A->eraseFromParent(); 53 } 54 } 55 56 for (Function &F : M) { 57 if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() && 58 !F.hasFnAttribute(Attribute::NoInline)) 59 FuncsToClone.push_back(&F); 60 } 61 62 for (Function *F : FuncsToClone) { 63 ValueToValueMapTy VMap; 64 Function *NewFunc = CloneFunction(F, VMap); 65 NewFunc->setLinkage(GlobalValue::InternalLinkage); 66 F->replaceAllUsesWith(NewFunc); 67 } 68 69 for (Function &F : M) { 70 if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) { 71 F.addFnAttr(Attribute::AlwaysInline); 72 } 73 } 74 return false; 75 } 76 77 ModulePass *llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt) { 78 return new AMDGPUAlwaysInline(GlobalOpt); 79 } 80