2015-01-18 16:17:27 +00:00
|
|
|
//===-- AMDGPUAlwaysInlinePass.cpp - Promote Allocas ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
/// This pass marks all internal functions as always_inline and creates
|
|
|
|
/// duplicates of all other functions a marks the duplicates as always_inline.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AMDGPUAlwaysInline : public ModulePass {
|
|
|
|
static char ID;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AMDGPUAlwaysInline() : ModulePass(ID) { }
|
|
|
|
bool runOnModule(Module &M) override;
|
2017-01-02 19:17:04 +00:00
|
|
|
StringRef getPassName() const override { return "AMDGPU Always Inline Pass"; }
|
2015-01-18 16:17:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
|
|
|
char AMDGPUAlwaysInline::ID = 0;
|
|
|
|
|
|
|
|
bool AMDGPUAlwaysInline::runOnModule(Module &M) {
|
2017-01-02 19:17:04 +00:00
|
|
|
std::vector<GlobalAlias*> AliasesToRemove;
|
2015-08-07 23:01:33 +00:00
|
|
|
std::vector<Function *> FuncsToClone;
|
2015-01-18 16:17:27 +00:00
|
|
|
|
2017-01-02 19:17:04 +00:00
|
|
|
for (GlobalAlias &A : M.aliases()) {
|
|
|
|
if (Function* F = dyn_cast<Function>(A.getAliasee())) {
|
|
|
|
A.replaceAllUsesWith(F);
|
|
|
|
AliasesToRemove.push_back(&A);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (GlobalAlias* A : AliasesToRemove) {
|
|
|
|
A->eraseFromParent();
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:01:33 +00:00
|
|
|
for (Function &F : M) {
|
2015-05-21 06:57:07 +00:00
|
|
|
if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() &&
|
|
|
|
!F.hasFnAttribute(Attribute::NoInline))
|
2015-01-18 16:17:27 +00:00
|
|
|
FuncsToClone.push_back(&F);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Function *F : FuncsToClone) {
|
|
|
|
ValueToValueMapTy VMap;
|
2016-07-23 20:41:05 +00:00
|
|
|
Function *NewFunc = CloneFunction(F, VMap);
|
2015-01-18 16:17:27 +00:00
|
|
|
NewFunc->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
F->replaceAllUsesWith(NewFunc);
|
|
|
|
}
|
|
|
|
|
2015-08-07 23:01:33 +00:00
|
|
|
for (Function &F : M) {
|
2015-05-21 06:57:07 +00:00
|
|
|
if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) {
|
2015-01-18 16:17:27 +00:00
|
|
|
F.addFnAttr(Attribute::AlwaysInline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModulePass *llvm::createAMDGPUAlwaysInlinePass() {
|
|
|
|
return new AMDGPUAlwaysInline();
|
|
|
|
}
|