2009-06-14 08:26:32 +00:00
|
|
|
//===- PartialInlining.cpp - Inline parts of functions --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass performs partial inlining, typically by inlining an if statement
|
|
|
|
// that surrounds the body of the function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2014-03-04 11:45:46 +00:00
|
|
|
#include "llvm/IR/CFG.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-06-14 08:26:32 +00:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Transforms/Utils/Cloning.h"
|
2012-05-04 10:18:49 +00:00
|
|
|
#include "llvm/Transforms/Utils/CodeExtractor.h"
|
2009-06-14 08:26:32 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "partialinlining"
|
|
|
|
|
2009-06-15 20:50:26 +00:00
|
|
|
STATISTIC(NumPartialInlined, "Number of functions partially inlined");
|
|
|
|
|
2009-06-14 08:26:32 +00:00
|
|
|
namespace {
|
2009-10-25 06:33:48 +00:00
|
|
|
struct PartialInliner : public ModulePass {
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override { }
|
2009-06-14 08:26:32 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-19 17:21:58 +00:00
|
|
|
PartialInliner() : ModulePass(ID) {
|
|
|
|
initializePartialInlinerPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2014-03-05 09:10:37 +00:00
|
|
|
|
|
|
|
bool runOnModule(Module& M) override;
|
|
|
|
|
2009-06-14 08:26:32 +00:00
|
|
|
private:
|
|
|
|
Function* unswitchFunction(Function* F);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
char PartialInliner::ID = 0;
|
2010-07-21 22:09:45 +00:00
|
|
|
INITIALIZE_PASS(PartialInliner, "partial-inliner",
|
2010-10-07 22:25:06 +00:00
|
|
|
"Partial Inliner", false, false)
|
2009-06-14 08:26:32 +00:00
|
|
|
|
|
|
|
ModulePass* llvm::createPartialInliningPass() { return new PartialInliner(); }
|
|
|
|
|
|
|
|
Function* PartialInliner::unswitchFunction(Function* F) {
|
|
|
|
// First, verify that this function is an unswitching candidate...
|
|
|
|
BasicBlock* entryBlock = F->begin();
|
2009-09-08 19:53:15 +00:00
|
|
|
BranchInst *BR = dyn_cast<BranchInst>(entryBlock->getTerminator());
|
|
|
|
if (!BR || BR->isUnconditional())
|
2009-06-14 08:26:32 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
BasicBlock* returnBlock = 0;
|
|
|
|
BasicBlock* nonReturnBlock = 0;
|
|
|
|
unsigned returnCount = 0;
|
|
|
|
for (succ_iterator SI = succ_begin(entryBlock), SE = succ_end(entryBlock);
|
|
|
|
SI != SE; ++SI)
|
|
|
|
if (isa<ReturnInst>((*SI)->getTerminator())) {
|
|
|
|
returnBlock = *SI;
|
|
|
|
returnCount++;
|
|
|
|
} else
|
|
|
|
nonReturnBlock = *SI;
|
|
|
|
|
|
|
|
if (returnCount != 1)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
// Clone the function, so that we can hack away on it.
|
2010-10-13 01:36:30 +00:00
|
|
|
ValueToValueMapTy VMap;
|
2010-08-26 15:41:53 +00:00
|
|
|
Function* duplicateFunction = CloneFunction(F, VMap,
|
|
|
|
/*ModuleLevelChanges=*/false);
|
2009-06-14 08:26:32 +00:00
|
|
|
duplicateFunction->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
F->getParent()->getFunctionList().push_back(duplicateFunction);
|
2010-06-24 00:33:28 +00:00
|
|
|
BasicBlock* newEntryBlock = cast<BasicBlock>(VMap[entryBlock]);
|
|
|
|
BasicBlock* newReturnBlock = cast<BasicBlock>(VMap[returnBlock]);
|
|
|
|
BasicBlock* newNonReturnBlock = cast<BasicBlock>(VMap[nonReturnBlock]);
|
2009-06-14 08:26:32 +00:00
|
|
|
|
|
|
|
// Go ahead and update all uses to the duplicate, so that we can just
|
|
|
|
// use the inliner functionality when we're done hacking.
|
|
|
|
F->replaceAllUsesWith(duplicateFunction);
|
|
|
|
|
|
|
|
// Special hackery is needed with PHI nodes that have inputs from more than
|
|
|
|
// one extracted block. For simplicity, just split the PHIs into a two-level
|
|
|
|
// sequence of PHIs, some of which will go in the extracted region, and some
|
|
|
|
// of which will go outside.
|
|
|
|
BasicBlock* preReturn = newReturnBlock;
|
|
|
|
newReturnBlock = newReturnBlock->splitBasicBlock(
|
|
|
|
newReturnBlock->getFirstNonPHI());
|
|
|
|
BasicBlock::iterator I = preReturn->begin();
|
|
|
|
BasicBlock::iterator Ins = newReturnBlock->begin();
|
|
|
|
while (I != preReturn->end()) {
|
|
|
|
PHINode* OldPhi = dyn_cast<PHINode>(I);
|
|
|
|
if (!OldPhi) break;
|
|
|
|
|
2011-03-30 11:28:46 +00:00
|
|
|
PHINode* retPhi = PHINode::Create(OldPhi->getType(), 2, "", Ins);
|
2009-06-14 08:26:32 +00:00
|
|
|
OldPhi->replaceAllUsesWith(retPhi);
|
|
|
|
Ins = newReturnBlock->getFirstNonPHI();
|
|
|
|
|
|
|
|
retPhi->addIncoming(I, preReturn);
|
|
|
|
retPhi->addIncoming(OldPhi->getIncomingValueForBlock(newEntryBlock),
|
|
|
|
newEntryBlock);
|
|
|
|
OldPhi->removeIncomingValue(newEntryBlock);
|
|
|
|
|
|
|
|
++I;
|
|
|
|
}
|
|
|
|
newEntryBlock->getTerminator()->replaceUsesOfWith(preReturn, newReturnBlock);
|
|
|
|
|
|
|
|
// Gather up the blocks that we're going to extract.
|
|
|
|
std::vector<BasicBlock*> toExtract;
|
|
|
|
toExtract.push_back(newNonReturnBlock);
|
|
|
|
for (Function::iterator FI = duplicateFunction->begin(),
|
|
|
|
FE = duplicateFunction->end(); FI != FE; ++FI)
|
|
|
|
if (&*FI != newEntryBlock && &*FI != newReturnBlock &&
|
|
|
|
&*FI != newNonReturnBlock)
|
|
|
|
toExtract.push_back(FI);
|
|
|
|
|
|
|
|
// The CodeExtractor needs a dominator tree.
|
|
|
|
DominatorTree DT;
|
2014-01-13 13:07:17 +00:00
|
|
|
DT.recalculate(*duplicateFunction);
|
|
|
|
|
2010-02-10 16:03:48 +00:00
|
|
|
// Extract the body of the if.
|
2012-05-04 10:18:49 +00:00
|
|
|
Function* extractedFunction
|
|
|
|
= CodeExtractor(toExtract, &DT).extractCodeRegion();
|
2009-06-14 08:26:32 +00:00
|
|
|
|
2010-04-22 23:07:58 +00:00
|
|
|
InlineFunctionInfo IFI;
|
|
|
|
|
2009-06-14 08:26:32 +00:00
|
|
|
// Inline the top-level if test into all callers.
|
2014-03-09 03:16:01 +00:00
|
|
|
std::vector<User *> Users(duplicateFunction->user_begin(),
|
|
|
|
duplicateFunction->user_end());
|
2009-06-14 08:26:32 +00:00
|
|
|
for (std::vector<User*>::iterator UI = Users.begin(), UE = Users.end();
|
|
|
|
UI != UE; ++UI)
|
2010-04-22 23:07:58 +00:00
|
|
|
if (CallInst *CI = dyn_cast<CallInst>(*UI))
|
|
|
|
InlineFunction(CI, IFI);
|
|
|
|
else if (InvokeInst *II = dyn_cast<InvokeInst>(*UI))
|
|
|
|
InlineFunction(II, IFI);
|
2009-06-14 08:26:32 +00:00
|
|
|
|
|
|
|
// Ditch the duplicate, since we're done with it, and rewrite all remaining
|
|
|
|
// users (function pointers, etc.) back to the original function.
|
|
|
|
duplicateFunction->replaceAllUsesWith(F);
|
|
|
|
duplicateFunction->eraseFromParent();
|
|
|
|
|
2009-06-15 20:50:26 +00:00
|
|
|
++NumPartialInlined;
|
|
|
|
|
2009-06-14 08:26:32 +00:00
|
|
|
return extractedFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PartialInliner::runOnModule(Module& M) {
|
|
|
|
std::vector<Function*> worklist;
|
|
|
|
worklist.reserve(M.size());
|
|
|
|
for (Module::iterator FI = M.begin(), FE = M.end(); FI != FE; ++FI)
|
|
|
|
if (!FI->use_empty() && !FI->isDeclaration())
|
2010-01-05 16:20:55 +00:00
|
|
|
worklist.push_back(&*FI);
|
2009-06-14 08:26:32 +00:00
|
|
|
|
|
|
|
bool changed = false;
|
|
|
|
while (!worklist.empty()) {
|
|
|
|
Function* currFunc = worklist.back();
|
|
|
|
worklist.pop_back();
|
|
|
|
|
|
|
|
if (currFunc->use_empty()) continue;
|
|
|
|
|
|
|
|
bool recursive = false;
|
2014-03-09 03:16:01 +00:00
|
|
|
for (User *U : currFunc->users())
|
|
|
|
if (Instruction* I = dyn_cast<Instruction>(U))
|
2009-06-14 08:26:32 +00:00
|
|
|
if (I->getParent()->getParent() == currFunc) {
|
|
|
|
recursive = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (recursive) continue;
|
|
|
|
|
|
|
|
|
|
|
|
if (Function* newFunc = unswitchFunction(currFunc)) {
|
|
|
|
worklist.push_back(newFunc);
|
|
|
|
changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
2009-07-03 15:30:58 +00:00
|
|
|
}
|