Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 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 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | class AMDGPUAlwaysInline : public ModulePass { |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 25 | static char ID; |
| 26 | |
| 27 | public: |
| 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 | |
| 35 | char AMDGPUAlwaysInline::ID = 0; |
| 36 | |
| 37 | bool AMDGPUAlwaysInline::runOnModule(Module &M) { |
Nikolay Haustov | eba8089 | 2016-08-31 11:18:33 +0000 | [diff] [blame^] | 38 | std::vector<GlobalAlias*> AliasesToRemove; |
Matt Arsenault | ca95d44 | 2015-07-13 19:08:36 +0000 | [diff] [blame] | 39 | std::vector<Function *> FuncsToClone; |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 40 | |
Nikolay Haustov | eba8089 | 2016-08-31 11:18:33 +0000 | [diff] [blame^] | 41 | for (GlobalAlias &A : M.aliases()) { |
| 42 | if (Function* F = dyn_cast<Function>(A.getAliasee())) { |
| 43 | A.replaceAllUsesWith(F); |
| 44 | AliasesToRemove.push_back(&A); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | for (GlobalAlias* A : AliasesToRemove) { |
| 49 | A->eraseFromParent(); |
| 50 | } |
| 51 | |
Matt Arsenault | ca95d44 | 2015-07-13 19:08:36 +0000 | [diff] [blame] | 52 | for (Function &F : M) { |
Matt Arsenault | deaef8e | 2015-04-22 17:10:44 +0000 | [diff] [blame] | 53 | if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() && |
| 54 | !F.hasFnAttribute(Attribute::NoInline)) |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 55 | FuncsToClone.push_back(&F); |
| 56 | } |
| 57 | |
| 58 | for (Function *F : FuncsToClone) { |
| 59 | ValueToValueMapTy VMap; |
Peter Collingbourne | dba9956 | 2016-05-10 20:23:24 +0000 | [diff] [blame] | 60 | Function *NewFunc = CloneFunction(F, VMap); |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 61 | NewFunc->setLinkage(GlobalValue::InternalLinkage); |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 62 | F->replaceAllUsesWith(NewFunc); |
| 63 | } |
| 64 | |
Matt Arsenault | ca95d44 | 2015-07-13 19:08:36 +0000 | [diff] [blame] | 65 | for (Function &F : M) { |
Matt Arsenault | deaef8e | 2015-04-22 17:10:44 +0000 | [diff] [blame] | 66 | if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) { |
Tom Stellard | 5cbb53c | 2014-11-03 19:49:05 +0000 | [diff] [blame] | 67 | F.addFnAttr(Attribute::AlwaysInline); |
| 68 | } |
| 69 | } |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | ModulePass *llvm::createAMDGPUAlwaysInlinePass() { |
| 74 | return new AMDGPUAlwaysInline(); |
| 75 | } |