2004-02-28 11:26:20 +08:00
|
|
|
//===- CodeExtractor.cpp - Pull code region into a new function -----------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-02-28 11:26:20 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-02-28 11:26:20 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the interface to tear out a code region, such as an
|
|
|
|
// individual loop or a parallel section, into a new function, replacing it with
|
|
|
|
// a call to the new function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
#include "llvm/Transforms/Utils/CodeExtractor.h"
|
2004-02-28 11:26:20 +08:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2004-05-12 14:01:40 +08:00
|
|
|
#include "llvm/Intrinsics.h"
|
2009-07-06 06:41:43 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2004-02-28 11:26:20 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2004-03-18 11:49:40 +08:00
|
|
|
#include "llvm/Analysis/Dominators.h"
|
2004-02-28 11:26:20 +08:00
|
|
|
#include "llvm/Analysis/LoopInfo.h"
|
2012-05-05 05:33:30 +08:00
|
|
|
#include "llvm/Analysis/RegionInfo.h"
|
|
|
|
#include "llvm/Analysis/RegionIterator.h"
|
2004-03-14 11:17:22 +08:00
|
|
|
#include "llvm/Analysis/Verifier.h"
|
2004-02-28 11:26:20 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-11 21:10:19 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-08-23 12:37:46 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-01-09 09:06:49 +08:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2004-09-02 06:55:40 +08:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
2004-02-28 11:26:20 +08:00
|
|
|
#include <algorithm>
|
2004-03-15 06:34:55 +08:00
|
|
|
#include <set>
|
2004-02-28 11:26:20 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
// Provide a command-line option to aggregate function arguments into a struct
|
2008-12-13 13:21:37 +08:00
|
|
|
// for functions produced by the code extractor. This is useful when converting
|
2004-04-24 07:54:17 +08:00
|
|
|
// extracted functions to pthread-based code, as only one argument (void*) can
|
|
|
|
// be passed in to pthread_create().
|
|
|
|
static cl::opt<bool>
|
|
|
|
AggregateArgsOpt("aggregate-extracted-args", cl::Hidden,
|
|
|
|
cl::desc("Aggregate arguments to code-extracted functions"));
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
/// \brief Test whether a block is valid for extraction.
|
|
|
|
static bool isBlockValidForExtraction(const BasicBlock &BB) {
|
|
|
|
// Landing pads must be in the function where they were inserted for cleanup.
|
|
|
|
if (BB.isLandingPad())
|
|
|
|
return false;
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
// Don't hoist code containing allocas, invokes, or vastarts.
|
|
|
|
for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
|
|
|
|
if (isa<AllocaInst>(I) || isa<InvokeInst>(I))
|
2004-05-12 14:01:40 +08:00
|
|
|
return false;
|
2012-05-04 18:18:49 +08:00
|
|
|
if (const CallInst *CI = dyn_cast<CallInst>(I))
|
|
|
|
if (const Function *F = CI->getCalledFunction())
|
|
|
|
if (F->getIntrinsicID() == Intrinsic::vastart)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Build a set of blocks to extract if the input blocks are viable.
|
2012-05-05 05:33:30 +08:00
|
|
|
template <typename IteratorT>
|
|
|
|
static SetVector<BasicBlock *> buildExtractionBlockSet(IteratorT BBBegin,
|
|
|
|
IteratorT BBEnd) {
|
2012-05-04 18:18:49 +08:00
|
|
|
SetVector<BasicBlock *> Result;
|
|
|
|
|
2012-05-05 05:33:30 +08:00
|
|
|
assert(BBBegin != BBEnd);
|
2012-05-04 18:26:45 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
// Loop over the blocks, adding them to our set-vector, and aborting with an
|
|
|
|
// empty set if we encounter invalid blocks.
|
2012-05-05 05:33:30 +08:00
|
|
|
for (IteratorT I = BBBegin, E = BBEnd; I != E; ++I) {
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Result.insert(*I))
|
2012-05-04 19:17:06 +08:00
|
|
|
llvm_unreachable("Repeated basic blocks in extraction input");
|
2012-05-04 18:18:49 +08:00
|
|
|
|
|
|
|
if (!isBlockValidForExtraction(**I)) {
|
|
|
|
Result.clear();
|
2012-05-04 19:14:19 +08:00
|
|
|
return Result;
|
2004-05-12 14:01:40 +08:00
|
|
|
}
|
2012-05-04 18:18:49 +08:00
|
|
|
}
|
2004-05-12 14:01:40 +08:00
|
|
|
|
2012-05-04 18:26:45 +08:00
|
|
|
#ifndef NDEBUG
|
2012-05-05 05:33:30 +08:00
|
|
|
for (SetVector<BasicBlock *>::iterator I = llvm::next(Result.begin()),
|
|
|
|
E = Result.end();
|
2012-05-04 18:26:45 +08:00
|
|
|
I != E; ++I)
|
|
|
|
for (pred_iterator PI = pred_begin(*I), PE = pred_end(*I);
|
|
|
|
PI != PE; ++PI)
|
|
|
|
assert(Result.count(*PI) &&
|
|
|
|
"No blocks in this region may have entries from outside the region"
|
|
|
|
" except for the first block!");
|
|
|
|
#endif
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2012-05-05 05:33:30 +08:00
|
|
|
/// \brief Helper to call buildExtractionBlockSet with an ArrayRef.
|
|
|
|
static SetVector<BasicBlock *>
|
|
|
|
buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs) {
|
|
|
|
return buildExtractionBlockSet(BBs.begin(), BBs.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Helper to call buildExtractionBlockSet with a RegionNode.
|
|
|
|
static SetVector<BasicBlock *>
|
|
|
|
buildExtractionBlockSet(const RegionNode &RN) {
|
|
|
|
if (!RN.isSubRegion())
|
|
|
|
// Just a single BasicBlock.
|
|
|
|
return buildExtractionBlockSet(RN.getNodeAs<BasicBlock>());
|
|
|
|
|
|
|
|
const Region &R = *RN.getNodeAs<Region>();
|
|
|
|
|
|
|
|
return buildExtractionBlockSet(R.block_begin(), R.block_end());
|
|
|
|
}
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
CodeExtractor::CodeExtractor(BasicBlock *BB, bool AggregateArgs)
|
|
|
|
: DT(0), AggregateArgs(AggregateArgs||AggregateArgsOpt),
|
|
|
|
Blocks(buildExtractionBlockSet(BB)), NumExitBlocks(~0U) {}
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
|
|
|
|
bool AggregateArgs)
|
|
|
|
: DT(DT), AggregateArgs(AggregateArgs||AggregateArgsOpt),
|
|
|
|
Blocks(buildExtractionBlockSet(BBs)), NumExitBlocks(~0U) {}
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
CodeExtractor::CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs)
|
|
|
|
: DT(&DT), AggregateArgs(AggregateArgs||AggregateArgsOpt),
|
|
|
|
Blocks(buildExtractionBlockSet(L.getBlocks())), NumExitBlocks(~0U) {}
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2012-05-05 05:33:30 +08:00
|
|
|
CodeExtractor::CodeExtractor(DominatorTree &DT, const RegionNode &RN,
|
|
|
|
bool AggregateArgs)
|
|
|
|
: DT(&DT), AggregateArgs(AggregateArgs||AggregateArgsOpt),
|
|
|
|
Blocks(buildExtractionBlockSet(RN)), NumExitBlocks(~0U) {}
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
/// definedInRegion - Return true if the specified value is defined in the
|
|
|
|
/// extracted region.
|
|
|
|
static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) {
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
if (Blocks.count(I->getParent()))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// definedInCaller - Return true if the specified value is defined in the
|
|
|
|
/// function being code extracted, but not in the region being extracted.
|
|
|
|
/// These values must be passed in as live-ins to the function.
|
|
|
|
static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) {
|
|
|
|
if (isa<Argument>(V)) return true;
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(V))
|
|
|
|
if (!Blocks.count(I->getParent()))
|
|
|
|
return true;
|
|
|
|
return false;
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
2012-05-04 19:20:27 +08:00
|
|
|
void CodeExtractor::findInputsOutputs(ValueSet &Inputs,
|
|
|
|
ValueSet &Outputs) const {
|
|
|
|
for (SetVector<BasicBlock *>::const_iterator I = Blocks.begin(),
|
|
|
|
E = Blocks.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
BasicBlock *BB = *I;
|
|
|
|
|
|
|
|
// If a used value is defined outside the region, it's an input. If an
|
|
|
|
// instruction is used outside the region, it's an output.
|
|
|
|
for (BasicBlock::iterator II = BB->begin(), IE = BB->end();
|
|
|
|
II != IE; ++II) {
|
|
|
|
for (User::op_iterator OI = II->op_begin(), OE = II->op_end();
|
|
|
|
OI != OE; ++OI)
|
|
|
|
if (definedInCaller(Blocks, *OI))
|
|
|
|
Inputs.insert(*OI);
|
|
|
|
|
|
|
|
for (Value::use_iterator UI = II->use_begin(), UE = II->use_end();
|
|
|
|
UI != UE; ++UI)
|
|
|
|
if (!definedInRegion(Blocks, *UI)) {
|
|
|
|
Outputs.insert(II);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
/// severSplitPHINodes - If a PHI node has multiple inputs from outside of the
|
|
|
|
/// region, we need to split the entry block of the region so that the PHI node
|
|
|
|
/// is easier to deal with.
|
|
|
|
void CodeExtractor::severSplitPHINodes(BasicBlock *&Header) {
|
2011-03-30 19:19:20 +08:00
|
|
|
unsigned NumPredsFromRegion = 0;
|
2004-05-12 23:29:13 +08:00
|
|
|
unsigned NumPredsOutsideRegion = 0;
|
|
|
|
|
2007-03-23 00:38:57 +08:00
|
|
|
if (Header != &Header->getParent()->getEntryBlock()) {
|
2004-05-12 23:29:13 +08:00
|
|
|
PHINode *PN = dyn_cast<PHINode>(Header->begin());
|
|
|
|
if (!PN) return; // No PHI nodes.
|
|
|
|
|
|
|
|
// If the header node contains any PHI nodes, check to see if there is more
|
|
|
|
// than one entry from outside the region. If so, we need to sever the
|
|
|
|
// header block into two.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(PN->getIncomingBlock(i)))
|
2011-03-30 19:19:20 +08:00
|
|
|
++NumPredsFromRegion;
|
2004-05-12 23:29:13 +08:00
|
|
|
else
|
|
|
|
++NumPredsOutsideRegion;
|
|
|
|
|
|
|
|
// If there is one (or fewer) predecessor from outside the region, we don't
|
|
|
|
// need to do anything special.
|
|
|
|
if (NumPredsOutsideRegion <= 1) return;
|
|
|
|
}
|
2004-05-12 14:01:40 +08:00
|
|
|
|
2004-05-12 23:29:13 +08:00
|
|
|
// Otherwise, we need to split the header block into two pieces: one
|
|
|
|
// containing PHI nodes merging values from outside of the region, and a
|
|
|
|
// second that contains all of the code for the block and merges back any
|
|
|
|
// incoming values from inside of the region.
|
2008-05-24 05:05:58 +08:00
|
|
|
BasicBlock::iterator AfterPHIs = Header->getFirstNonPHI();
|
2004-05-12 23:29:13 +08:00
|
|
|
BasicBlock *NewBB = Header->splitBasicBlock(AfterPHIs,
|
|
|
|
Header->getName()+".ce");
|
|
|
|
|
|
|
|
// We only want to code extract the second block now, and it becomes the new
|
|
|
|
// header of the region.
|
|
|
|
BasicBlock *OldPred = Header;
|
2012-05-04 18:18:49 +08:00
|
|
|
Blocks.remove(OldPred);
|
|
|
|
Blocks.insert(NewBB);
|
2004-05-12 23:29:13 +08:00
|
|
|
Header = NewBB;
|
|
|
|
|
|
|
|
// Okay, update dominator sets. The blocks that dominate the new one are the
|
|
|
|
// blocks that dominate TIBB plus the new block itself.
|
2007-06-22 01:23:45 +08:00
|
|
|
if (DT)
|
|
|
|
DT->splitBlock(NewBB);
|
2004-05-12 14:01:40 +08:00
|
|
|
|
2004-05-12 23:29:13 +08:00
|
|
|
// Okay, now we need to adjust the PHI nodes and any branches from within the
|
|
|
|
// region to go to the new header block instead of the old header block.
|
2011-03-30 19:19:20 +08:00
|
|
|
if (NumPredsFromRegion) {
|
2004-05-12 23:29:13 +08:00
|
|
|
PHINode *PN = cast<PHINode>(OldPred->begin());
|
|
|
|
// Loop over all of the predecessors of OldPred that are in the region,
|
|
|
|
// changing them to branch to NewBB instead.
|
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(PN->getIncomingBlock(i))) {
|
2004-05-12 23:29:13 +08:00
|
|
|
TerminatorInst *TI = PN->getIncomingBlock(i)->getTerminator();
|
|
|
|
TI->replaceUsesOfWith(OldPred, NewBB);
|
|
|
|
}
|
|
|
|
|
2011-04-15 13:18:47 +08:00
|
|
|
// Okay, everything within the region is now branching to the right block, we
|
2004-05-12 23:29:13 +08:00
|
|
|
// just have to update the PHI nodes now, inserting PHI nodes into NewBB.
|
2004-09-16 01:06:42 +08:00
|
|
|
for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
|
|
|
|
PHINode *PN = cast<PHINode>(AfterPHIs);
|
2004-05-12 23:29:13 +08:00
|
|
|
// Create a new PHI node in the new region, which has an incoming value
|
|
|
|
// from OldPred of PN.
|
2011-03-30 19:28:46 +08:00
|
|
|
PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion,
|
|
|
|
PN->getName()+".ce", NewBB->begin());
|
2004-05-12 23:29:13 +08:00
|
|
|
NewPN->addIncoming(PN, OldPred);
|
|
|
|
|
|
|
|
// Loop over all of the incoming value in PN, moving them to NewPN if they
|
|
|
|
// are from the extracted region.
|
|
|
|
for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(PN->getIncomingBlock(i))) {
|
2004-05-12 23:29:13 +08:00
|
|
|
NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i));
|
|
|
|
PN->removeIncomingValue(i);
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-05-13 00:07:41 +08:00
|
|
|
}
|
2004-05-12 23:29:13 +08:00
|
|
|
|
2004-05-13 00:07:41 +08:00
|
|
|
void CodeExtractor::splitReturnBlocks() {
|
2012-05-04 18:18:49 +08:00
|
|
|
for (SetVector<BasicBlock *>::iterator I = Blocks.begin(), E = Blocks.end();
|
|
|
|
I != E; ++I)
|
2009-08-25 07:32:14 +08:00
|
|
|
if (ReturnInst *RI = dyn_cast<ReturnInst>((*I)->getTerminator())) {
|
|
|
|
BasicBlock *New = (*I)->splitBasicBlock(RI, (*I)->getName()+".ret");
|
|
|
|
if (DT) {
|
2010-09-11 06:25:58 +08:00
|
|
|
// Old dominates New. New node dominates all other nodes dominated
|
|
|
|
// by Old.
|
2009-08-25 07:32:14 +08:00
|
|
|
DomTreeNode *OldNode = DT->getNode(*I);
|
2009-08-26 01:35:37 +08:00
|
|
|
SmallVector<DomTreeNode*, 8> Children;
|
2009-08-25 07:32:14 +08:00
|
|
|
for (DomTreeNode::iterator DI = OldNode->begin(), DE = OldNode->end();
|
|
|
|
DI != DE; ++DI)
|
|
|
|
Children.push_back(*DI);
|
|
|
|
|
|
|
|
DomTreeNode *NewNode = DT->addNewBlock(New, *I);
|
|
|
|
|
2009-08-26 01:35:37 +08:00
|
|
|
for (SmallVector<DomTreeNode*, 8>::iterator I = Children.begin(),
|
2009-08-25 07:32:14 +08:00
|
|
|
E = Children.end(); I != E; ++I)
|
|
|
|
DT->changeImmediateDominator(*I, NewNode);
|
|
|
|
}
|
|
|
|
}
|
2004-05-12 14:01:40 +08:00
|
|
|
}
|
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
/// constructFunction - make a function based on inputs and outputs, as follows:
|
|
|
|
/// f(in0, ..., inN, out0, ..., outN)
|
|
|
|
///
|
2012-05-04 18:18:49 +08:00
|
|
|
Function *CodeExtractor::constructFunction(const ValueSet &inputs,
|
|
|
|
const ValueSet &outputs,
|
2004-03-18 13:28:49 +08:00
|
|
|
BasicBlock *header,
|
2004-02-28 11:26:20 +08:00
|
|
|
BasicBlock *newRootNode,
|
|
|
|
BasicBlock *newHeader,
|
2004-03-18 13:28:49 +08:00
|
|
|
Function *oldFunction,
|
|
|
|
Module *M) {
|
2010-01-05 09:26:44 +08:00
|
|
|
DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
|
|
|
|
DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// This function returns unsigned, outputs will go back by reference.
|
2004-05-12 12:14:24 +08:00
|
|
|
switch (NumExitBlocks) {
|
|
|
|
case 0:
|
2009-08-14 05:58:54 +08:00
|
|
|
case 1: RetTy = Type::getVoidTy(header->getContext()); break;
|
|
|
|
case 2: RetTy = Type::getInt1Ty(header->getContext()); break;
|
|
|
|
default: RetTy = Type::getInt16Ty(header->getContext()); break;
|
2004-05-12 12:14:24 +08:00
|
|
|
}
|
|
|
|
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> paramTy;
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// Add the types of the input values to the function's argument list
|
2012-05-04 18:18:49 +08:00
|
|
|
for (ValueSet::const_iterator i = inputs.begin(), e = inputs.end();
|
|
|
|
i != e; ++i) {
|
2004-02-28 11:26:20 +08:00
|
|
|
const Value *value = *i;
|
2010-01-05 09:26:44 +08:00
|
|
|
DEBUG(dbgs() << "value used in func: " << *value << "\n");
|
2004-02-28 11:26:20 +08:00
|
|
|
paramTy.push_back(value->getType());
|
|
|
|
}
|
|
|
|
|
2004-03-18 11:49:40 +08:00
|
|
|
// Add the types of the output values to the function's argument list.
|
2012-05-04 18:18:49 +08:00
|
|
|
for (ValueSet::const_iterator I = outputs.begin(), E = outputs.end();
|
2004-03-18 11:49:40 +08:00
|
|
|
I != E; ++I) {
|
2010-01-05 09:26:44 +08:00
|
|
|
DEBUG(dbgs() << "instr used in func: " << **I << "\n");
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs)
|
|
|
|
paramTy.push_back((*I)->getType());
|
|
|
|
else
|
2009-07-30 06:17:13 +08:00
|
|
|
paramTy.push_back(PointerType::getUnqual((*I)->getType()));
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
2010-01-05 09:26:44 +08:00
|
|
|
DEBUG(dbgs() << "Function type: " << *RetTy << " f(");
|
2011-07-12 22:06:48 +08:00
|
|
|
for (std::vector<Type*>::iterator i = paramTy.begin(),
|
2006-11-26 18:17:54 +08:00
|
|
|
e = paramTy.end(); i != e; ++i)
|
2010-01-05 09:26:44 +08:00
|
|
|
DEBUG(dbgs() << **i << ", ");
|
|
|
|
DEBUG(dbgs() << ")\n");
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
|
2009-07-06 06:41:43 +08:00
|
|
|
PointerType *StructPtr =
|
2009-08-06 07:16:16 +08:00
|
|
|
PointerType::getUnqual(StructType::get(M->getContext(), paramTy));
|
2004-04-24 07:54:17 +08:00
|
|
|
paramTy.clear();
|
|
|
|
paramTy.push_back(StructPtr);
|
|
|
|
}
|
2011-07-18 12:54:35 +08:00
|
|
|
FunctionType *funcType =
|
2009-07-30 06:17:13 +08:00
|
|
|
FunctionType::get(RetTy, paramTy, false);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// Create the new function
|
2008-04-07 04:25:17 +08:00
|
|
|
Function *newFunction = Function::Create(funcType,
|
|
|
|
GlobalValue::InternalLinkage,
|
|
|
|
oldFunction->getName() + "_" +
|
|
|
|
header->getName(), M);
|
2008-12-18 13:52:56 +08:00
|
|
|
// If the old function is no-throw, so is the new one.
|
|
|
|
if (oldFunction->doesNotThrow())
|
2012-10-10 11:12:49 +08:00
|
|
|
newFunction->setDoesNotThrow();
|
2008-12-18 13:52:56 +08:00
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
newFunction->getBasicBlockList().push_back(newRootNode);
|
|
|
|
|
2004-03-18 11:49:40 +08:00
|
|
|
// Create an iterator to name all of the arguments we inserted.
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator AI = newFunction->arg_begin();
|
2004-03-18 11:49:40 +08:00
|
|
|
|
|
|
|
// Rewrite all users of the inputs in the extracted region to use the
|
2004-04-24 07:54:17 +08:00
|
|
|
// arguments (or appropriate addressing into struct) instead.
|
|
|
|
for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
|
|
|
|
Value *RewriteVal;
|
|
|
|
if (AggregateArgs) {
|
2007-09-04 23:46:09 +08:00
|
|
|
Value *Idx[2];
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext()));
|
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i);
|
2004-04-24 07:54:17 +08:00
|
|
|
TerminatorInst *TI = newFunction->begin()->getTerminator();
|
2009-07-24 16:24:36 +08:00
|
|
|
GetElementPtrInst *GEP =
|
2011-07-25 17:48:08 +08:00
|
|
|
GetElementPtrInst::Create(AI, Idx, "gep_" + inputs[i]->getName(), TI);
|
2009-07-24 16:24:36 +08:00
|
|
|
RewriteVal = new LoadInst(GEP, "loadgep_" + inputs[i]->getName(), TI);
|
2004-04-24 07:54:17 +08:00
|
|
|
} else
|
|
|
|
RewriteVal = AI++;
|
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
std::vector<User*> Users(inputs[i]->use_begin(), inputs[i]->use_end());
|
|
|
|
for (std::vector<User*>::iterator use = Users.begin(), useE = Users.end();
|
2004-03-14 11:17:22 +08:00
|
|
|
use != useE; ++use)
|
|
|
|
if (Instruction* inst = dyn_cast<Instruction>(*use))
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(inst->getParent()))
|
2004-04-24 07:54:17 +08:00
|
|
|
inst->replaceUsesOfWith(inputs[i], RewriteVal);
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
// Set names for input and output arguments.
|
|
|
|
if (!AggregateArgs) {
|
2005-03-15 12:54:21 +08:00
|
|
|
AI = newFunction->arg_begin();
|
2004-04-24 07:54:17 +08:00
|
|
|
for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI)
|
2008-04-15 01:38:21 +08:00
|
|
|
AI->setName(inputs[i]->getName());
|
2004-04-24 07:54:17 +08:00
|
|
|
for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI)
|
2005-04-22 07:48:37 +08:00
|
|
|
AI->setName(outputs[i]->getName()+".out");
|
2004-04-24 07:54:17 +08:00
|
|
|
}
|
2004-03-18 11:49:40 +08:00
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
// Rewrite branches to basic blocks outside of the loop to new dummy blocks
|
|
|
|
// within the new function. This must be done before we lose track of which
|
|
|
|
// blocks were originally in the code region.
|
|
|
|
std::vector<User*> Users(header->use_begin(), header->use_end());
|
2004-03-18 13:28:49 +08:00
|
|
|
for (unsigned i = 0, e = Users.size(); i != e; ++i)
|
|
|
|
// The BasicBlock which contains the branch is not in the region
|
|
|
|
// modify the branch target to a new block
|
|
|
|
if (TerminatorInst *TI = dyn_cast<TerminatorInst>(Users[i]))
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Blocks.count(TI->getParent()) &&
|
2004-03-18 13:28:49 +08:00
|
|
|
TI->getParent()->getParent() == oldFunction)
|
|
|
|
TI->replaceUsesOfWith(header, newHeader);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
return newFunction;
|
|
|
|
}
|
|
|
|
|
2009-08-26 01:42:07 +08:00
|
|
|
/// FindPhiPredForUseInBlock - Given a value and a basic block, find a PHI
|
|
|
|
/// that uses the value within the basic block, and return the predecessor
|
|
|
|
/// block associated with that use, or return 0 if none is found.
|
2009-08-26 01:26:32 +08:00
|
|
|
static BasicBlock* FindPhiPredForUseInBlock(Value* Used, BasicBlock* BB) {
|
|
|
|
for (Value::use_iterator UI = Used->use_begin(),
|
|
|
|
UE = Used->use_end(); UI != UE; ++UI) {
|
|
|
|
PHINode *P = dyn_cast<PHINode>(*UI);
|
|
|
|
if (P && P->getParent() == BB)
|
|
|
|
return P->getIncomingBlock(UI);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
/// emitCallAndSwitchStatement - This method sets up the caller side by adding
|
|
|
|
/// the call instruction, splitting any PHI nodes in the header block as
|
|
|
|
/// necessary.
|
|
|
|
void CodeExtractor::
|
|
|
|
emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
|
2012-05-04 18:18:49 +08:00
|
|
|
ValueSet &inputs, ValueSet &outputs) {
|
2004-05-12 14:01:40 +08:00
|
|
|
// Emit a call to the new function, passing in: *pointer to struct (if
|
|
|
|
// aggregating parameters), or plan inputs and allocated memory for outputs
|
2009-08-25 08:54:39 +08:00
|
|
|
std::vector<Value*> params, StructValues, ReloadOutputs, Reloads;
|
2009-08-14 05:58:54 +08:00
|
|
|
|
|
|
|
LLVMContext &Context = newFunction->getContext();
|
2004-04-24 07:54:17 +08:00
|
|
|
|
|
|
|
// Add inputs as params, or to be filled into the struct
|
2012-05-04 18:18:49 +08:00
|
|
|
for (ValueSet::iterator i = inputs.begin(), e = inputs.end(); i != e; ++i)
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs)
|
|
|
|
StructValues.push_back(*i);
|
|
|
|
else
|
|
|
|
params.push_back(*i);
|
|
|
|
|
|
|
|
// Create allocas for the outputs
|
2012-05-04 18:18:49 +08:00
|
|
|
for (ValueSet::iterator i = outputs.begin(), e = outputs.end(); i != e; ++i) {
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs) {
|
|
|
|
StructValues.push_back(*i);
|
|
|
|
} else {
|
|
|
|
AllocaInst *alloca =
|
2009-07-16 07:53:25 +08:00
|
|
|
new AllocaInst((*i)->getType(), 0, (*i)->getName()+".loc",
|
2004-04-24 07:54:17 +08:00
|
|
|
codeReplacer->getParent()->begin()->begin());
|
|
|
|
ReloadOutputs.push_back(alloca);
|
|
|
|
params.push_back(alloca);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AllocaInst *Struct = 0;
|
|
|
|
if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
|
2011-07-12 22:06:48 +08:00
|
|
|
std::vector<Type*> ArgTypes;
|
2012-05-04 18:18:49 +08:00
|
|
|
for (ValueSet::iterator v = StructValues.begin(),
|
2004-04-24 07:54:17 +08:00
|
|
|
ve = StructValues.end(); v != ve; ++v)
|
|
|
|
ArgTypes.push_back((*v)->getType());
|
|
|
|
|
|
|
|
// Allocate a struct at the beginning of this function
|
2009-08-06 07:16:16 +08:00
|
|
|
Type *StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
|
2005-04-22 07:48:37 +08:00
|
|
|
Struct =
|
2009-07-16 07:53:25 +08:00
|
|
|
new AllocaInst(StructArgTy, 0, "structArg",
|
2004-04-24 07:54:17 +08:00
|
|
|
codeReplacer->getParent()->begin()->begin());
|
|
|
|
params.push_back(Struct);
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
|
2007-09-04 23:46:09 +08:00
|
|
|
Value *Idx[2];
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
|
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i);
|
2004-04-24 07:54:17 +08:00
|
|
|
GetElementPtrInst *GEP =
|
2011-07-25 17:48:08 +08:00
|
|
|
GetElementPtrInst::Create(Struct, Idx,
|
2008-04-07 04:25:17 +08:00
|
|
|
"gep_" + StructValues[i]->getName());
|
2004-04-24 07:54:17 +08:00
|
|
|
codeReplacer->getInstList().push_back(GEP);
|
|
|
|
StoreInst *SI = new StoreInst(StructValues[i], GEP);
|
|
|
|
codeReplacer->getInstList().push_back(SI);
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
}
|
2004-04-24 07:54:17 +08:00
|
|
|
|
|
|
|
// Emit the call to the function
|
2011-07-15 16:37:34 +08:00
|
|
|
CallInst *call = CallInst::Create(newFunction, params,
|
2008-04-07 04:25:17 +08:00
|
|
|
NumExitBlocks > 1 ? "targetBlock" : "");
|
2004-04-24 07:54:17 +08:00
|
|
|
codeReplacer->getInstList().push_back(call);
|
|
|
|
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator OutputArgBegin = newFunction->arg_begin();
|
2004-04-24 07:54:17 +08:00
|
|
|
unsigned FirstOut = inputs.size();
|
|
|
|
if (!AggregateArgs)
|
|
|
|
std::advance(OutputArgBegin, inputs.size());
|
2004-03-18 12:12:05 +08:00
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
// Reload the outputs passed in by reference
|
2004-03-18 11:49:40 +08:00
|
|
|
for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
|
2004-04-24 07:54:17 +08:00
|
|
|
Value *Output = 0;
|
|
|
|
if (AggregateArgs) {
|
2007-09-04 23:46:09 +08:00
|
|
|
Value *Idx[2];
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
|
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i);
|
2005-04-22 07:48:37 +08:00
|
|
|
GetElementPtrInst *GEP
|
2011-07-25 17:48:08 +08:00
|
|
|
= GetElementPtrInst::Create(Struct, Idx,
|
2008-04-07 04:25:17 +08:00
|
|
|
"gep_reload_" + outputs[i]->getName());
|
2004-04-24 07:54:17 +08:00
|
|
|
codeReplacer->getInstList().push_back(GEP);
|
|
|
|
Output = GEP;
|
|
|
|
} else {
|
|
|
|
Output = ReloadOutputs[i];
|
|
|
|
}
|
|
|
|
LoadInst *load = new LoadInst(Output, outputs[i]->getName()+".reload");
|
2009-08-25 08:54:39 +08:00
|
|
|
Reloads.push_back(load);
|
2004-03-18 11:49:40 +08:00
|
|
|
codeReplacer->getInstList().push_back(load);
|
|
|
|
std::vector<User*> Users(outputs[i]->use_begin(), outputs[i]->use_end());
|
|
|
|
for (unsigned u = 0, e = Users.size(); u != e; ++u) {
|
|
|
|
Instruction *inst = cast<Instruction>(Users[u]);
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Blocks.count(inst->getParent()))
|
2004-03-18 11:49:40 +08:00
|
|
|
inst->replaceUsesOfWith(outputs[i], load);
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
}
|
2004-03-15 07:05:49 +08:00
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
// Now we can emit a switch statement using the call as a value.
|
2004-05-12 12:14:24 +08:00
|
|
|
SwitchInst *TheSwitch =
|
2009-08-14 05:58:54 +08:00
|
|
|
SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)),
|
2008-04-07 04:25:17 +08:00
|
|
|
codeReplacer, 0, codeReplacer);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// Since there may be multiple exits from the original region, make the new
|
2004-03-15 07:05:49 +08:00
|
|
|
// function return an unsigned, switch on that number. This loop iterates
|
|
|
|
// over all of the blocks in the extracted region, updating any terminator
|
|
|
|
// instructions in the to-be-extracted region that branch to blocks that are
|
|
|
|
// not in the region to be extracted.
|
|
|
|
std::map<BasicBlock*, BasicBlock*> ExitBlockMap;
|
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
unsigned switchVal = 0;
|
2012-05-04 18:18:49 +08:00
|
|
|
for (SetVector<BasicBlock*>::const_iterator i = Blocks.begin(),
|
|
|
|
e = Blocks.end(); i != e; ++i) {
|
2004-03-15 07:05:49 +08:00
|
|
|
TerminatorInst *TI = (*i)->getTerminator();
|
|
|
|
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Blocks.count(TI->getSuccessor(i))) {
|
2004-03-15 07:05:49 +08:00
|
|
|
BasicBlock *OldTarget = TI->getSuccessor(i);
|
|
|
|
// add a new basic block which returns the appropriate value
|
|
|
|
BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
|
|
|
|
if (!NewTarget) {
|
|
|
|
// If we don't already have an exit stub for this non-extracted
|
|
|
|
// destination, create one now!
|
2009-08-14 05:58:54 +08:00
|
|
|
NewTarget = BasicBlock::Create(Context,
|
|
|
|
OldTarget->getName() + ".exitStub",
|
2008-04-07 04:25:17 +08:00
|
|
|
newFunction);
|
2004-05-12 12:14:24 +08:00
|
|
|
unsigned SuccNum = switchVal++;
|
|
|
|
|
|
|
|
Value *brVal = 0;
|
|
|
|
switch (NumExitBlocks) {
|
|
|
|
case 0:
|
|
|
|
case 1: break; // No value needed.
|
|
|
|
case 2: // Conditional branch, return a bool
|
2009-08-14 05:58:54 +08:00
|
|
|
brVal = ConstantInt::get(Type::getInt1Ty(Context), !SuccNum);
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
2009-08-14 05:58:54 +08:00
|
|
|
brVal = ConstantInt::get(Type::getInt16Ty(Context), SuccNum);
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
}
|
2004-03-15 07:05:49 +08:00
|
|
|
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst *NTRet = ReturnInst::Create(Context, brVal, NewTarget);
|
2004-03-15 07:05:49 +08:00
|
|
|
|
|
|
|
// Update the switch instruction.
|
2009-08-14 05:58:54 +08:00
|
|
|
TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context),
|
|
|
|
SuccNum),
|
2004-05-12 12:14:24 +08:00
|
|
|
OldTarget);
|
2004-03-15 07:05:49 +08:00
|
|
|
|
|
|
|
// Restore values just before we exit
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator OAI = OutputArgBegin;
|
2004-04-24 07:54:17 +08:00
|
|
|
for (unsigned out = 0, e = outputs.size(); out != e; ++out) {
|
|
|
|
// For an invoke, the normal destination is the only one that is
|
|
|
|
// dominated by the result of the invocation
|
|
|
|
BasicBlock *DefBlock = cast<Instruction>(outputs[out])->getParent();
|
2004-11-13 08:06:45 +08:00
|
|
|
|
|
|
|
bool DominatesDef = true;
|
|
|
|
|
2004-11-13 07:50:44 +08:00
|
|
|
if (InvokeInst *Invoke = dyn_cast<InvokeInst>(outputs[out])) {
|
2004-04-24 07:54:17 +08:00
|
|
|
DefBlock = Invoke->getNormalDest();
|
2004-11-13 07:50:44 +08:00
|
|
|
|
|
|
|
// Make sure we are looking at the original successor block, not
|
|
|
|
// at a newly inserted exit block, which won't be in the dominator
|
|
|
|
// info.
|
|
|
|
for (std::map<BasicBlock*, BasicBlock*>::iterator I =
|
|
|
|
ExitBlockMap.begin(), E = ExitBlockMap.end(); I != E; ++I)
|
|
|
|
if (DefBlock == I->second) {
|
|
|
|
DefBlock = I->first;
|
|
|
|
break;
|
|
|
|
}
|
2004-11-13 08:06:45 +08:00
|
|
|
|
|
|
|
// In the extract block case, if the block we are extracting ends
|
|
|
|
// with an invoke instruction, make sure that we don't emit a
|
|
|
|
// store of the invoke value for the unwind block.
|
2007-06-08 06:17:16 +08:00
|
|
|
if (!DT && DefBlock != OldTarget)
|
2004-11-13 08:06:45 +08:00
|
|
|
DominatesDef = false;
|
2004-11-13 07:50:44 +08:00
|
|
|
}
|
|
|
|
|
2009-08-25 08:54:39 +08:00
|
|
|
if (DT) {
|
2007-06-08 06:17:16 +08:00
|
|
|
DominatesDef = DT->dominates(DefBlock, OldTarget);
|
2009-08-25 08:54:39 +08:00
|
|
|
|
|
|
|
// If the output value is used by a phi in the target block,
|
|
|
|
// then we need to test for dominance of the phi's predecessor
|
|
|
|
// instead. Unfortunately, this a little complicated since we
|
|
|
|
// have already rewritten uses of the value to uses of the reload.
|
2009-08-26 01:26:32 +08:00
|
|
|
BasicBlock* pred = FindPhiPredForUseInBlock(Reloads[out],
|
|
|
|
OldTarget);
|
|
|
|
if (pred && DT && DT->dominates(DefBlock, pred))
|
|
|
|
DominatesDef = true;
|
2009-08-25 08:54:39 +08:00
|
|
|
}
|
2004-11-13 08:06:45 +08:00
|
|
|
|
|
|
|
if (DominatesDef) {
|
2004-04-24 07:54:17 +08:00
|
|
|
if (AggregateArgs) {
|
2007-09-04 23:46:09 +08:00
|
|
|
Value *Idx[2];
|
2009-08-14 05:58:54 +08:00
|
|
|
Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
|
|
|
|
Idx[1] = ConstantInt::get(Type::getInt32Ty(Context),
|
|
|
|
FirstOut+out);
|
2004-04-24 07:54:17 +08:00
|
|
|
GetElementPtrInst *GEP =
|
2011-07-25 17:48:08 +08:00
|
|
|
GetElementPtrInst::Create(OAI, Idx,
|
2008-04-07 04:25:17 +08:00
|
|
|
"gep_" + outputs[out]->getName(),
|
|
|
|
NTRet);
|
2004-04-24 07:54:17 +08:00
|
|
|
new StoreInst(outputs[out], GEP, NTRet);
|
2004-11-13 08:06:45 +08:00
|
|
|
} else {
|
2004-04-24 07:54:17 +08:00
|
|
|
new StoreInst(outputs[out], OAI, NTRet);
|
2004-11-13 08:06:45 +08:00
|
|
|
}
|
|
|
|
}
|
2004-04-24 07:54:17 +08:00
|
|
|
// Advance output iterator even if we don't emit a store
|
|
|
|
if (!AggregateArgs) ++OAI;
|
|
|
|
}
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
2004-03-15 06:34:55 +08:00
|
|
|
|
2004-03-15 07:05:49 +08:00
|
|
|
// rewrite the original branch instruction with this new target
|
|
|
|
TI->setSuccessor(i, NewTarget);
|
|
|
|
}
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
2004-03-15 07:43:24 +08:00
|
|
|
|
2004-05-12 11:22:33 +08:00
|
|
|
// Now that we've done the deed, simplify the switch instruction.
|
2011-07-18 12:54:35 +08:00
|
|
|
Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
|
2004-05-12 12:14:24 +08:00
|
|
|
switch (NumExitBlocks) {
|
|
|
|
case 0:
|
2004-08-12 11:17:02 +08:00
|
|
|
// There are no successors (the block containing the switch itself), which
|
2004-04-24 07:54:17 +08:00
|
|
|
// means that previously this was the last part of the function, and hence
|
|
|
|
// this should be rewritten as a `ret'
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-04-24 07:54:17 +08:00
|
|
|
// Check if the function should return a value
|
2010-01-05 21:12:22 +08:00
|
|
|
if (OldFnRetTy->isVoidTy()) {
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst::Create(Context, 0, TheSwitch); // Return void
|
2004-08-12 11:17:02 +08:00
|
|
|
} else if (OldFnRetTy == TheSwitch->getCondition()->getType()) {
|
2004-04-24 07:54:17 +08:00
|
|
|
// return what we have
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst::Create(Context, TheSwitch->getCondition(), TheSwitch);
|
2004-08-12 11:17:02 +08:00
|
|
|
} else {
|
|
|
|
// Otherwise we must have code extracted an unwind or something, just
|
|
|
|
// return whatever we want.
|
2009-08-14 05:58:54 +08:00
|
|
|
ReturnInst::Create(Context,
|
|
|
|
Constant::getNullValue(OldFnRetTy), TheSwitch);
|
2004-08-12 11:17:02 +08:00
|
|
|
}
|
2004-04-24 07:54:17 +08:00
|
|
|
|
2008-06-22 06:08:46 +08:00
|
|
|
TheSwitch->eraseFromParent();
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
// Only a single destination, change the switch into an unconditional
|
|
|
|
// branch.
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch);
|
2008-06-22 06:08:46 +08:00
|
|
|
TheSwitch->eraseFromParent();
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
case 2:
|
2008-04-07 04:25:17 +08:00
|
|
|
BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2),
|
|
|
|
call, TheSwitch);
|
2008-06-22 06:08:46 +08:00
|
|
|
TheSwitch->eraseFromParent();
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Otherwise, make the default destination of the switch instruction be one
|
|
|
|
// of the other successors.
|
SwitchInst refactoring.
The purpose of refactoring is to hide operand roles from SwitchInst user (programmer). If you want to play with operands directly, probably you will need lower level methods than SwitchInst ones (TerminatorInst or may be User). After this patch we can reorganize SwitchInst operands and successors as we want.
What was done:
1. Changed semantics of index inside the getCaseValue method:
getCaseValue(0) means "get first case", not a condition. Use getCondition() if you want to resolve the condition. I propose don't mix SwitchInst case indexing with low level indexing (TI successors indexing, User's operands indexing), since it may be dangerous.
2. By the same reason findCaseValue(ConstantInt*) returns actual number of case value. 0 means first case, not default. If there is no case with given value, ErrorIndex will returned.
3. Added getCaseSuccessor method. I propose to avoid usage of TerminatorInst::getSuccessor if you want to resolve case successor BB. Use getCaseSuccessor instead, since internal SwitchInst organization of operands/successors is hidden and may be changed in any moment.
4. Added resolveSuccessorIndex and resolveCaseIndex. The main purpose of these methods is to see how case successors are really mapped in TerminatorInst.
4.1 "resolveSuccessorIndex" was created if you need to level down from SwitchInst to TerminatorInst. It returns TerminatorInst's successor index for given case successor.
4.2 "resolveCaseIndex" converts low level successors index to case index that curresponds to the given successor.
Note: There are also related compatability fix patches for dragonegg, klee, llvm-gcc-4.0, llvm-gcc-4.2, safecode, clang.
llvm-svn: 149481
2012-02-01 15:49:51 +08:00
|
|
|
TheSwitch->setCondition(call);
|
|
|
|
TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks));
|
2012-03-08 15:06:20 +08:00
|
|
|
// Remove redundant case
|
2012-06-22 22:53:30 +08:00
|
|
|
SwitchInst::CaseIt ToBeRemoved(TheSwitch, NumExitBlocks-1);
|
|
|
|
TheSwitch->removeCase(ToBeRemoved);
|
2004-05-12 12:14:24 +08:00
|
|
|
break;
|
2004-03-15 07:43:24 +08:00
|
|
|
}
|
2004-02-28 11:26:20 +08:00
|
|
|
}
|
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
void CodeExtractor::moveCodeToFunction(Function *newFunction) {
|
2012-05-04 18:18:49 +08:00
|
|
|
Function *oldFunc = (*Blocks.begin())->getParent();
|
2004-05-12 14:01:40 +08:00
|
|
|
Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList();
|
|
|
|
Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList();
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
for (SetVector<BasicBlock*>::const_iterator i = Blocks.begin(),
|
|
|
|
e = Blocks.end(); i != e; ++i) {
|
2004-05-12 14:01:40 +08:00
|
|
|
// Delete the basic block from the old function, and the list of blocks
|
|
|
|
oldBlocks.remove(*i);
|
|
|
|
|
|
|
|
// Insert this basic block into the new function
|
|
|
|
newBlocks.push_back(*i);
|
|
|
|
}
|
|
|
|
}
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
Function *CodeExtractor::extractCodeRegion() {
|
|
|
|
if (!isEligible())
|
2004-04-24 07:54:17 +08:00
|
|
|
return 0;
|
|
|
|
|
2012-05-04 18:18:49 +08:00
|
|
|
ValueSet inputs, outputs;
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// Assumption: this is a single-entry code region, and the header is the first
|
2004-03-15 09:18:23 +08:00
|
|
|
// block in the region.
|
2012-05-04 18:18:49 +08:00
|
|
|
BasicBlock *header = *Blocks.begin();
|
2004-05-12 14:01:40 +08:00
|
|
|
|
2004-05-13 00:07:41 +08:00
|
|
|
// If we have to split PHI nodes or the entry block, do so now.
|
2004-05-12 23:29:13 +08:00
|
|
|
severSplitPHINodes(header);
|
|
|
|
|
2004-05-13 00:07:41 +08:00
|
|
|
// If we have any return instructions in the region, split those blocks so
|
|
|
|
// that the return is not in the region.
|
|
|
|
splitReturnBlocks();
|
|
|
|
|
2004-02-28 11:26:20 +08:00
|
|
|
Function *oldFunction = header->getParent();
|
|
|
|
|
|
|
|
// This takes place of the original loop
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(),
|
|
|
|
"codeRepl", oldFunction,
|
2008-05-15 18:04:30 +08:00
|
|
|
header);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
|
|
|
// The new function needs a root node because other nodes can branch to the
|
2004-05-12 14:01:40 +08:00
|
|
|
// head of the region, but the entry node of a function cannot have preds.
|
2009-08-14 05:58:54 +08:00
|
|
|
BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(),
|
|
|
|
"newFuncRoot");
|
2008-04-07 04:25:17 +08:00
|
|
|
newFuncRoot->getInstList().push_back(BranchInst::Create(header));
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
// Find inputs to, outputs from the code region.
|
|
|
|
findInputsOutputs(inputs, outputs);
|
|
|
|
|
2012-05-04 19:20:27 +08:00
|
|
|
SmallPtrSet<BasicBlock *, 1> ExitBlocks;
|
|
|
|
for (SetVector<BasicBlock *>::iterator I = Blocks.begin(), E = Blocks.end();
|
|
|
|
I != E; ++I)
|
|
|
|
for (succ_iterator SI = succ_begin(*I), SE = succ_end(*I); SI != SE; ++SI)
|
|
|
|
if (!Blocks.count(*SI))
|
|
|
|
ExitBlocks.insert(*SI);
|
|
|
|
NumExitBlocks = ExitBlocks.size();
|
|
|
|
|
2004-05-12 14:01:40 +08:00
|
|
|
// Construct new function based on inputs/outputs & add allocas for all defs.
|
2004-05-12 23:29:13 +08:00
|
|
|
Function *newFunction = constructFunction(inputs, outputs, header,
|
2005-04-22 07:48:37 +08:00
|
|
|
newFuncRoot,
|
2004-03-15 09:18:23 +08:00
|
|
|
codeReplacer, oldFunction,
|
|
|
|
oldFunction->getParent());
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2004-03-15 06:34:55 +08:00
|
|
|
emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2004-03-15 06:34:55 +08:00
|
|
|
moveCodeToFunction(newFunction);
|
2004-02-28 11:26:20 +08:00
|
|
|
|
2004-05-12 23:29:13 +08:00
|
|
|
// Loop over all of the PHI nodes in the header block, and change any
|
2004-03-18 13:28:49 +08:00
|
|
|
// references to the old incoming edge to be the new incoming edge.
|
2004-09-16 01:06:42 +08:00
|
|
|
for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
2004-03-18 13:28:49 +08:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (!Blocks.count(PN->getIncomingBlock(i)))
|
2004-03-18 13:28:49 +08:00
|
|
|
PN->setIncomingBlock(i, newFuncRoot);
|
2004-09-16 01:06:42 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-03-18 13:38:31 +08:00
|
|
|
// Look at all successors of the codeReplacer block. If any of these blocks
|
|
|
|
// had PHI nodes in them, we need to update the "from" block to be the code
|
|
|
|
// replacer, not the original block in the extracted region.
|
|
|
|
std::vector<BasicBlock*> Succs(succ_begin(codeReplacer),
|
|
|
|
succ_end(codeReplacer));
|
|
|
|
for (unsigned i = 0, e = Succs.size(); i != e; ++i)
|
2004-09-16 01:06:42 +08:00
|
|
|
for (BasicBlock::iterator I = Succs[i]->begin(); isa<PHINode>(I); ++I) {
|
|
|
|
PHINode *PN = cast<PHINode>(I);
|
2004-08-13 11:27:07 +08:00
|
|
|
std::set<BasicBlock*> ProcessedPreds;
|
2004-03-18 13:38:31 +08:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
2012-05-04 18:18:49 +08:00
|
|
|
if (Blocks.count(PN->getIncomingBlock(i))) {
|
2004-08-13 11:27:07 +08:00
|
|
|
if (ProcessedPreds.insert(PN->getIncomingBlock(i)).second)
|
|
|
|
PN->setIncomingBlock(i, codeReplacer);
|
|
|
|
else {
|
|
|
|
// There were multiple entries in the PHI for this block, now there
|
|
|
|
// is only one, so remove the duplicated entries.
|
|
|
|
PN->removeIncomingValue(i, false);
|
|
|
|
--i; --e;
|
|
|
|
}
|
2008-02-20 19:26:25 +08:00
|
|
|
}
|
2004-08-13 11:27:07 +08:00
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2006-12-07 09:30:32 +08:00
|
|
|
//cerr << "NEW FUNCTION: " << *newFunction;
|
2004-05-12 23:29:13 +08:00
|
|
|
// verifyFunction(*newFunction);
|
|
|
|
|
2006-12-07 09:30:32 +08:00
|
|
|
// cerr << "OLD FUNCTION: " << *oldFunction;
|
2004-05-12 23:29:13 +08:00
|
|
|
// verifyFunction(*oldFunction);
|
2004-03-18 13:38:31 +08:00
|
|
|
|
2009-07-11 21:10:19 +08:00
|
|
|
DEBUG(if (verifyFunction(*newFunction))
|
2010-04-08 06:58:41 +08:00
|
|
|
report_fatal_error("verifyFunction failed!"));
|
2004-02-28 11:26:20 +08:00
|
|
|
return newFunction;
|
|
|
|
}
|