blob: 0b426bc63dd5590bedc6b24f610a960c14361b66 [file] [log] [blame]
Tom Stellard5cbb53c2014-11-03 19:49:05 +00001//===-- 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;
Matt Arsenaultdeaef8e2015-04-22 17:10:44 +000043 if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() &&
44 !F.hasFnAttribute(Attribute::NoInline))
Tom Stellard5cbb53c2014-11-03 19:49:05 +000045 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;
Matt Arsenaultdeaef8e2015-04-22 17:10:44 +000058 if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) {
Tom Stellard5cbb53c2014-11-03 19:49:05 +000059 F.addFnAttr(Attribute::AlwaysInline);
60 }
61 }
62 return false;
63}
64
65ModulePass *llvm::createAMDGPUAlwaysInlinePass() {
66 return new AMDGPUAlwaysInline();
67}