blob: 63f5fb3cdf005efb39e6f4b0372d5b5ce84ce05f [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
27public:
28 AMDGPUAlwaysInline() : ModulePass(ID) { }
29 bool runOnModule(Module &M) override;
30 const char *getPassName() const override { return "AMDGPU Always Inline Pass"; }
31};
32
33} // End anonymous namespace
34
35char AMDGPUAlwaysInline::ID = 0;
36
37bool AMDGPUAlwaysInline::runOnModule(Module &M) {
Matt Arsenaultca95d442015-07-13 19:08:36 +000038 std::vector<Function *> FuncsToClone;
Tom Stellard5cbb53c2014-11-03 19:49:05 +000039
Matt Arsenaultca95d442015-07-13 19:08:36 +000040 for (Function &F : M) {
Matt Arsenaultdeaef8e2015-04-22 17:10:44 +000041 if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() &&
42 !F.hasFnAttribute(Attribute::NoInline))
Tom Stellard5cbb53c2014-11-03 19:49:05 +000043 FuncsToClone.push_back(&F);
44 }
45
46 for (Function *F : FuncsToClone) {
47 ValueToValueMapTy VMap;
Peter Collingbournedba99562016-05-10 20:23:24 +000048 Function *NewFunc = CloneFunction(F, VMap);
Tom Stellard5cbb53c2014-11-03 19:49:05 +000049 NewFunc->setLinkage(GlobalValue::InternalLinkage);
Tom Stellard5cbb53c2014-11-03 19:49:05 +000050 F->replaceAllUsesWith(NewFunc);
51 }
52
Matt Arsenaultca95d442015-07-13 19:08:36 +000053 for (Function &F : M) {
Matt Arsenaultdeaef8e2015-04-22 17:10:44 +000054 if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) {
Tom Stellard5cbb53c2014-11-03 19:49:05 +000055 F.addFnAttr(Attribute::AlwaysInline);
56 }
57 }
58 return false;
59}
60
61ModulePass *llvm::createAMDGPUAlwaysInlinePass() {
62 return new AMDGPUAlwaysInline();
63}