2014-11-04 03:49:05 +08: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
|
2017-06-16 07:02:55 +08:00
|
|
|
/// duplicates of all other functions and marks the duplicates as always_inline.
|
2014-11-04 03:49:05 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "AMDGPU.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class AMDGPUAlwaysInline : public ModulePass {
|
2017-03-31 04:16:02 +08:00
|
|
|
bool GlobalOpt;
|
|
|
|
|
2014-11-04 03:49:05 +08:00
|
|
|
public:
|
2017-06-03 02:02:42 +08:00
|
|
|
static char ID;
|
|
|
|
|
|
|
|
AMDGPUAlwaysInline(bool GlobalOpt = false) :
|
|
|
|
ModulePass(ID), GlobalOpt(GlobalOpt) { }
|
2014-11-04 03:49:05 +08:00
|
|
|
bool runOnModule(Module &M) override;
|
2016-10-01 10:56:57 +08:00
|
|
|
StringRef getPassName() const override { return "AMDGPU Always Inline Pass"; }
|
2014-11-04 03:49:05 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // End anonymous namespace
|
|
|
|
|
2017-06-03 02:02:42 +08:00
|
|
|
INITIALIZE_PASS(AMDGPUAlwaysInline, "amdgpu-always-inline",
|
|
|
|
"AMDGPU Inline All Functions", false, false)
|
|
|
|
|
2014-11-04 03:49:05 +08:00
|
|
|
char AMDGPUAlwaysInline::ID = 0;
|
|
|
|
|
|
|
|
bool AMDGPUAlwaysInline::runOnModule(Module &M) {
|
2016-08-31 19:18:33 +08:00
|
|
|
std::vector<GlobalAlias*> AliasesToRemove;
|
2015-07-14 03:08:36 +08:00
|
|
|
std::vector<Function *> FuncsToClone;
|
2014-11-04 03:49:05 +08:00
|
|
|
|
2016-08-31 19:18:33 +08:00
|
|
|
for (GlobalAlias &A : M.aliases()) {
|
|
|
|
if (Function* F = dyn_cast<Function>(A.getAliasee())) {
|
|
|
|
A.replaceAllUsesWith(F);
|
|
|
|
AliasesToRemove.push_back(&A);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:16:02 +08:00
|
|
|
if (GlobalOpt) {
|
|
|
|
for (GlobalAlias* A : AliasesToRemove) {
|
|
|
|
A->eraseFromParent();
|
|
|
|
}
|
2016-08-31 19:18:33 +08:00
|
|
|
}
|
|
|
|
|
2015-07-14 03:08:36 +08:00
|
|
|
for (Function &F : M) {
|
2015-04-23 01:10:44 +08:00
|
|
|
if (!F.hasLocalLinkage() && !F.isDeclaration() && !F.use_empty() &&
|
|
|
|
!F.hasFnAttribute(Attribute::NoInline))
|
2014-11-04 03:49:05 +08:00
|
|
|
FuncsToClone.push_back(&F);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Function *F : FuncsToClone) {
|
|
|
|
ValueToValueMapTy VMap;
|
2016-05-11 04:23:24 +08:00
|
|
|
Function *NewFunc = CloneFunction(F, VMap);
|
2014-11-04 03:49:05 +08:00
|
|
|
NewFunc->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
F->replaceAllUsesWith(NewFunc);
|
|
|
|
}
|
|
|
|
|
2015-07-14 03:08:36 +08:00
|
|
|
for (Function &F : M) {
|
2015-04-23 01:10:44 +08:00
|
|
|
if (F.hasLocalLinkage() && !F.hasFnAttribute(Attribute::NoInline)) {
|
2014-11-04 03:49:05 +08:00
|
|
|
F.addFnAttr(Attribute::AlwaysInline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-31 04:16:02 +08:00
|
|
|
ModulePass *llvm::createAMDGPUAlwaysInlinePass(bool GlobalOpt) {
|
|
|
|
return new AMDGPUAlwaysInline(GlobalOpt);
|
2014-11-04 03:49:05 +08:00
|
|
|
}
|