1 //===-- AMDGPUAlwaysInlinePass.cpp - Promote Allocas ----------------------===//
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 /// This pass marks all internal functions as always_inline and creates
12 /// duplicates of all other functions a marks the duplicates as always_inline.
14 //===----------------------------------------------------------------------===//
17 #include "llvm/IR/Module.h"
18 #include "llvm/Transforms/Utils/Cloning.h"
24 class AMDGPUAlwaysInline : public ModulePass {
30 AMDGPUAlwaysInline(bool GlobalOpt) : ModulePass(ID), GlobalOpt(GlobalOpt) { }
31 bool runOnModule(Module &M) override;
32 StringRef getPassName() const override { return "AMDGPU Always Inline Pass"; }
35 } // End anonymous namespace
37 char AMDGPUAlwaysInline::ID = 0;
39 bool AMDGPUAlwaysInline::runOnModule(Module &M) {
40 std::vector<GlobalAlias*> AliasesToRemove;
41 std::vector<Function *> FuncsToClone;
43 for (GlobalAlias &A : M.aliases()) {
44 if (Function* F = dyn_cast<Function>(A.getAliasee())) {
45 A.replaceAllUsesWith(F);
46 AliasesToRemove.push_back(&A);
51 for (GlobalAlias* A : AliasesToRemove) {
56 for (Function &F : M) {
57 if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() &&
58 !F.hasFnAttribute(Attribute::NoInline))
59 FuncsToClone.push_back(&F);
62 for (Function *F : FuncsToClone) {
63 ValueToValueMapTy VMap;
64 Function *NewFunc = CloneFunction(F, VMap);
65 NewFunc->setLinkage(GlobalValue::InternalLinkage);
66 F->replaceAllUsesWith(NewFunc);
69 for (Function &F : M) {
70 if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) {
71 F.addFnAttr(Attribute::AlwaysInline);
77 ModulePass *llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt) {
78 return new AMDGPUAlwaysInline(GlobalOpt);