47 cl::desc(
"Aggregate arguments to code-extracted functions"));
57 if (isa<AllocaInst>(
I) || isa<InvokeInst>(
I))
59 if (
const CallInst *CI = dyn_cast<CallInst>(
I))
60 if (
const Function *
F = CI->getCalledFunction())
69 template <
typename IteratorT>
74 assert(BBBegin != BBEnd);
78 for (IteratorT
I = BBBegin, E = BBEnd;
I != E; ++
I) {
94 assert(Result.
count(*PI) &&
95 "No blocks in this region may have entries from outside the region"
96 " except for the first block!");
142 if (Blocks.
count(
I->getParent()))
151 if (isa<Argument>(V))
return true;
153 if (!Blocks.
count(
I->getParent()))
187 void CodeExtractor::severSplitPHINodes(
BasicBlock *&Header) {
188 unsigned NumPredsFromRegion = 0;
189 unsigned NumPredsOutsideRegion = 0;
200 ++NumPredsFromRegion;
202 ++NumPredsOutsideRegion;
206 if (NumPredsOutsideRegion <= 1)
return;
220 Blocks.remove(OldPred);
221 Blocks.insert(NewBB);
231 if (NumPredsFromRegion) {
243 for (AfterPHIs = OldPred->
begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
244 PHINode *PN = cast<PHINode>(AfterPHIs);
264 void CodeExtractor::splitReturnBlocks() {
267 if (
ReturnInst *RI = dyn_cast<ReturnInst>((*I)->getTerminator())) {
281 E = Children.
end();
I != E; ++
I)
290 Function *CodeExtractor::constructFunction(
const ValueSet &inputs,
291 const ValueSet &outputs,
297 DEBUG(
dbgs() <<
"inputs: " << inputs.size() <<
"\n");
298 DEBUG(
dbgs() <<
"outputs: " << outputs.size() <<
"\n");
301 switch (NumExitBlocks) {
308 std::vector<Type*> paramTy;
313 const Value *value = *i;
314 DEBUG(
dbgs() <<
"value used in func: " << *value <<
"\n");
315 paramTy.push_back(value->
getType());
321 DEBUG(
dbgs() <<
"instr used in func: " << **
I <<
"\n");
323 paramTy.push_back((*I)->getType());
328 DEBUG(
dbgs() <<
"Function type: " << *RetTy <<
" f(");
329 for (std::vector<Type*>::iterator i = paramTy.begin(),
330 e = paramTy.end(); i != e; ++i)
334 if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
338 paramTy.push_back(StructPtr);
359 for (
unsigned i = 0, e = inputs.size(); i != e; ++i) {
372 std::vector<User*>
Users(inputs[i]->use_begin(), inputs[i]->use_end());
373 for (std::vector<User*>::iterator use =
Users.begin(), useE =
Users.end();
375 if (
Instruction* inst = dyn_cast<Instruction>(*use))
376 if (Blocks.count(inst->getParent()))
377 inst->replaceUsesOfWith(inputs[i], RewriteVal);
381 if (!AggregateArgs) {
383 for (
unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI)
384 AI->setName(inputs[i]->getName());
385 for (
unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI)
386 AI->setName(outputs[i]->getName()+
".out");
393 for (
unsigned i = 0, e =
Users.size(); i != e; ++i)
409 UE = Used->
use_end(); UI != UE; ++UI) {
423 ValueSet &inputs, ValueSet &outputs) {
426 std::vector<Value*> params, StructValues, ReloadOutputs, Reloads;
433 StructValues.push_back(*i);
435 params.push_back(*i);
440 StructValues.push_back(*i);
443 new AllocaInst((*i)->getType(), 0, (*i)->getName()+
".loc",
445 ReloadOutputs.push_back(alloca);
446 params.push_back(alloca);
451 if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
452 std::vector<Type*> ArgTypes;
454 ve = StructValues.end(); v != ve; ++v)
455 ArgTypes.push_back((*v)->getType());
462 params.push_back(Struct);
464 for (
unsigned i = 0, e = inputs.size(); i != e; ++i) {
470 "gep_" + StructValues[i]->
getName());
479 NumExitBlocks > 1 ?
"targetBlock" :
"");
483 unsigned FirstOut = inputs.size();
488 for (
unsigned i = 0, e = outputs.size(); i != e; ++i) {
496 "gep_reload_" + outputs[i]->
getName());
500 Output = ReloadOutputs[i];
503 Reloads.push_back(load);
505 std::vector<User*>
Users(outputs[i]->use_begin(), outputs[i]->use_end());
506 for (
unsigned u = 0, e =
Users.size(); u != e; ++u) {
516 codeReplacer, 0, codeReplacer);
523 std::map<BasicBlock*, BasicBlock*> ExitBlockMap;
525 unsigned switchVal = 0;
527 e = Blocks.end(); i != e; ++i) {
533 BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
538 OldTarget->
getName() +
".exitStub",
540 unsigned SuccNum = switchVal++;
543 switch (NumExitBlocks) {
563 for (
unsigned out = 0, e = outputs.size(); out != e; ++out) {
568 bool DominatesDef =
true;
570 if (
InvokeInst *Invoke = dyn_cast<InvokeInst>(outputs[out])) {
571 DefBlock = Invoke->getNormalDest();
576 for (std::map<BasicBlock*, BasicBlock*>::iterator
I =
577 ExitBlockMap.begin(), E = ExitBlockMap.end();
I != E; ++
I)
578 if (DefBlock ==
I->second) {
586 if (!DT && DefBlock != OldTarget)
587 DominatesDef =
false;
591 DominatesDef = DT->
dominates(DefBlock, OldTarget);
599 if (pred && DT && DT->
dominates(DefBlock, pred))
611 "gep_" + outputs[out]->
getName(),
619 if (!AggregateArgs) ++OAI;
630 switch (NumExitBlocks) {
673 void CodeExtractor::moveCodeToFunction(
Function *newFunction) {
679 e = Blocks.end(); i != e; ++i) {
699 severSplitPHINodes(header);
709 "codeRepl", oldFunction,
725 if (!Blocks.count(*SI))
727 NumExitBlocks = ExitBlocks.
size();
730 Function *newFunction = constructFunction(inputs, outputs, header,
732 codeReplacer, oldFunction,
735 emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
737 moveCodeToFunction(newFunction);
751 std::vector<BasicBlock*> Succs(
succ_begin(codeReplacer),
753 for (
unsigned i = 0, e = Succs.size(); i != e; ++i)
756 std::set<BasicBlock*> ProcessedPreds;
void push_back(const T &Elt)
DomTreeNode * getNode(BasicBlock *BB) const
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
LLVMContext & getContext() const
The main container class for the LLVM Intermediate Representation.
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
block_iterator block_begin()
void splitBlock(BasicBlock *NewBB)
const_iterator begin(StringRef path)
Get begin iterator over path.
Type * getReturnType() const
const Function * getParent() const
Return the enclosing method, or null if none.
A RegionNode represents a subregion or a BasicBlock that is part of a Region.
vector_type::const_iterator const_iterator
iv Induction Variable Users
static IntegerType * getInt16Ty(LLVMContext &C)
iterator end()
Get an iterator to the end of the SetVector.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
static Constant * getNullValue(Type *Ty)
StringRef getName() const
static error_code advance(T &it, size_t Val)
void push_back(NodeTy *val)
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
bool doesNotThrow() const
Determine if the function cannot unwind.
#define llvm_unreachable(msg)
void changeImmediateDominator(BasicBlock *N, BasicBlock *NewIDom)
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=0)
block_iterator block_end()
Interval::succ_iterator succ_begin(Interval *I)
bool insert(const value_type &X)
Insert a new element into the SetVector.
iterator begin()
Get an iterator to the beginning of the SetVector.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
void setSuccessor(unsigned idx, BasicBlock *B)
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumIncomingValues() const
void replaceUsesOfWith(Value *From, Value *To)
unsigned getNumSuccessors() const
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
A single entry single exit Region.
Interval::pred_iterator pred_begin(Interval *I)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=0)
bool verifyFunction(const Function &F, VerifierFailureAction action=AbortProcessAction)
static Type * getVoidTy(LLVMContext &C)
BasicBlock * getIncomingBlock(unsigned i) const
ItTy next(ItTy it, Dist n)
const InstListType & getInstList() const
Return the underlying instruction list container.
Interval::pred_iterator pred_end(Interval *I)
bool dominates(const DomTreeNode *A, const DomTreeNode *B) const
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, const Twine &NameStr="", Instruction *InsertBefore=0)
const BasicBlockListType & getBasicBlockList() const
static PointerType * getUnqual(Type *ElementType)
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
static GetElementPtrInst * Create(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=0)
const BasicBlock & getEntryBlock() const
raw_ostream & dbgs()
dbgs - Return a circular-buffered debug stream.
void clear()
Completely clear the SetVector.
Value * getCondition() const
BasicBlock * getSuccessor(unsigned idx) const
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
std::string getName(ID id, ArrayRef< Type * > Tys=None)
static IntegerType * getInt32Ty(LLVMContext &C)
void setCondition(Value *V)
bool isSubRegion() const
Is this RegionNode a subregion?
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
bool isLandingPad() const
Return true if this basic block is a landing pad.
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
Rename collisions when linking (static functions).
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
DomTreeNode * addNewBlock(BasicBlock *BB, BasicBlock *DomBB)
void removeCase(CaseIt i)
static ReturnInst * Create(LLVMContext &C, Value *retVal=0, Instruction *InsertBefore=0)
std::vector< DomTreeNodeBase< NodeT > * >::iterator iterator
LLVMContext & getContext() const
Get the context in which this basic block lives.
LLVM Value Representation.
vector_type::const_iterator iterator
T * getNodeAs() const
Get the content of this RegionNode.
void setDefaultDest(BasicBlock *DefaultCase)
A vector that has set insertion semantics.
static const Function * getParent(const Value *V)
NodeTy * remove(iterator &IT)
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, Instruction *InsertBefore=0)
const BasicBlock * getParent() const
LLVMContext & getContext() const
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=0)
bool isVoidTy() const
isVoidTy - Return true if this is 'void'.