LLVM  5.0.0
AMDGPUAlwaysInlinePass.cpp
Go to the documentation of this file.
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 and marks the duplicates as always_inline.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "AMDGPU.h"
17 #include "llvm/IR/Module.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);
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 
82  return new AMDGPUAlwaysInline(GlobalOpt);
83 }
ModulePass * createAMDGPUAlwaysInlinePass(bool GlobalOpt=true)
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
Function * CloneFunction(Function *F, ValueToValueMapTy &VMap, ClonedCodeInfo *CodeInfo=nullptr)
CloneFunction - Return a copy of the specified function and add it to that function's module...
#define F(x, y, z)
Definition: MD5.cpp:55
#define A
Definition: LargeTest.cpp:12
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:34
Module.h This file contains the declarations for the Module class.
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:425
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:235
Rename collisions when linking (static functions).
Definition: GlobalValue.h:56
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49