AMDGPUAlwaysInlinePass.cpp revision 284734
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
20using namespace llvm;
21
22namespace {
23
24class AMDGPUAlwaysInline : public ModulePass {
25
26  static char ID;
27
28public:
29  AMDGPUAlwaysInline() : ModulePass(ID) { }
30  bool runOnModule(Module &M) override;
31  const char *getPassName() const override { return "AMDGPU Always Inline Pass"; }
32};
33
34} // End anonymous namespace
35
36char AMDGPUAlwaysInline::ID = 0;
37
38bool AMDGPUAlwaysInline::runOnModule(Module &M) {
39
40  std::vector<Function*> FuncsToClone;
41  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
42    Function &F = *I;
43    if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() &&
44        !F.hasFnAttribute(Attribute::NoInline))
45      FuncsToClone.push_back(&F);
46  }
47
48  for (Function *F : FuncsToClone) {
49    ValueToValueMapTy VMap;
50    Function *NewFunc = CloneFunction(F, VMap, false);
51    NewFunc->setLinkage(GlobalValue::InternalLinkage);
52    F->getParent()->getFunctionList().push_back(NewFunc);
53    F->replaceAllUsesWith(NewFunc);
54  }
55
56  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
57    Function &F = *I;
58    if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) {
59      F.addFnAttr(Attribute::AlwaysInline);
60    }
61  }
62  return false;
63}
64
65ModulePass *llvm::createAMDGPUAlwaysInlinePass() {
66  return new AMDGPUAlwaysInline();
67}
68