blob: 1d03714874e284277aea2b137a4d4ac3d2dfd49c [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 {
Tom Stellard5cbb53c2014-11-03 19:49:05 +000025 static char ID;
26
Stanislav Mekhanoshin89653df2017-03-30 20:16:02 +000027 bool GlobalOpt;
28
Tom Stellard5cbb53c2014-11-03 19:49:05 +000029public:
Stanislav Mekhanoshin89653df2017-03-30 20:16:02 +000030 AMDGPUAlwaysInline(bool GlobalOpt) : ModulePass(ID), GlobalOpt(GlobalOpt) { }
Tom Stellard5cbb53c2014-11-03 19:49:05 +000031 bool runOnModule(Module &M) override;
Mehdi Amini117296c2016-10-01 02:56:57 +000032 StringRef getPassName() const override { return "AMDGPU Always Inline Pass"; }
Tom Stellard5cbb53c2014-11-03 19:49:05 +000033};
34
35} // End anonymous namespace
36
37char AMDGPUAlwaysInline::ID = 0;
38
39bool AMDGPUAlwaysInline::runOnModule(Module &M) {
Nikolay Haustoveba80892016-08-31 11:18:33 +000040 std::vector<GlobalAlias*> AliasesToRemove;
Matt Arsenaultca95d442015-07-13 19:08:36 +000041 std::vector<Function *> FuncsToClone;
Tom Stellard5cbb53c2014-11-03 19:49:05 +000042
Nikolay Haustoveba80892016-08-31 11:18:33 +000043 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
Stanislav Mekhanoshin89653df2017-03-30 20:16:02 +000050 if (GlobalOpt) {
51 for (GlobalAlias* A : AliasesToRemove) {
52 A->eraseFromParent();
53 }
Nikolay Haustoveba80892016-08-31 11:18:33 +000054 }
55
Matt Arsenaultca95d442015-07-13 19:08:36 +000056 for (Function &F : M) {
Matt Arsenaultdeaef8e2015-04-22 17:10:44 +000057 if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() &&
58 !F.hasFnAttribute(Attribute::NoInline))
Tom Stellard5cbb53c2014-11-03 19:49:05 +000059 FuncsToClone.push_back(&F);
60 }
61
62 for (Function *F : FuncsToClone) {
63 ValueToValueMapTy VMap;
Peter Collingbournedba99562016-05-10 20:23:24 +000064 Function *NewFunc = CloneFunction(F, VMap);
Tom Stellard5cbb53c2014-11-03 19:49:05 +000065 NewFunc->setLinkage(GlobalValue::InternalLinkage);
Tom Stellard5cbb53c2014-11-03 19:49:05 +000066 F->replaceAllUsesWith(NewFunc);
67 }
68
Matt Arsenaultca95d442015-07-13 19:08:36 +000069 for (Function &F : M) {
Matt Arsenaultdeaef8e2015-04-22 17:10:44 +000070 if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) {
Tom Stellard5cbb53c2014-11-03 19:49:05 +000071 F.addFnAttr(Attribute::AlwaysInline);
72 }
73 }
74 return false;
75}
76
Stanislav Mekhanoshin89653df2017-03-30 20:16:02 +000077ModulePass *llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt) {
78 return new AMDGPUAlwaysInline(GlobalOpt);
Tom Stellard5cbb53c2014-11-03 19:49:05 +000079}