46 #define DEBUG_TYPE "code-extractor"
54 cl::desc(
"Aggregate arguments to code-extracted functions"));
64 if (isa<AllocaInst>(
I) || isa<InvokeInst>(
I))
66 if (
const CallInst *CI = dyn_cast<CallInst>(
I))
67 if (
const Function *
F = CI->getCalledFunction())
68 if (
F->getIntrinsicID() == Intrinsic::vastart)
76 template <
typename IteratorT>
86 if (!Result.
insert(*BBBegin))
93 }
while (++BBBegin != BBEnd);
102 "No blocks in this region may have entries from outside the region"
103 " except for the first block!");
144 NumExitBlocks(~0U) {}
156 if (Blocks.
count(
I->getParent()))
165 if (isa<Argument>(V))
return true;
167 if (!Blocks.
count(
I->getParent()))
195 void CodeExtractor::severSplitPHINodes(
BasicBlock *&Header) {
196 unsigned NumPredsFromRegion = 0;
197 unsigned NumPredsOutsideRegion = 0;
208 ++NumPredsFromRegion;
210 ++NumPredsOutsideRegion;
214 if (NumPredsOutsideRegion <= 1)
return;
228 Blocks.remove(OldPred);
229 Blocks.insert(NewBB);
239 if (NumPredsFromRegion) {
251 for (AfterPHIs = OldPred->
begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
252 PHINode *PN = cast<PHINode>(AfterPHIs);
272 void CodeExtractor::splitReturnBlocks() {
276 Block->splitBasicBlock(RI->getIterator(),
Block->getName() +
".ret");
295 Function *CodeExtractor::constructFunction(
const ValueSet &inputs,
296 const ValueSet &outputs,
302 DEBUG(
dbgs() <<
"inputs: " << inputs.size() <<
"\n");
303 DEBUG(
dbgs() <<
"outputs: " << outputs.size() <<
"\n");
306 switch (NumExitBlocks) {
313 std::vector<Type*> paramTy;
316 for (
Value *value : inputs) {
317 DEBUG(
dbgs() <<
"value used in func: " << *value <<
"\n");
318 paramTy.push_back(value->getType());
322 for (
Value *output : outputs) {
323 DEBUG(
dbgs() <<
"instr used in func: " << *output <<
"\n");
325 paramTy.push_back(output->getType());
331 dbgs() <<
"Function type: " << *RetTy <<
" f(";
332 for (
Type *
i : paramTy)
333 dbgs() << *
i <<
", ";
338 if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
367 for (
auto Attr : AB.td_attrs())
368 newFunction->
addFnAttr(Attr.first, Attr.second);
377 for (
unsigned i = 0, e = inputs.size();
i != e; ++
i) {
385 StructTy, &*AI, Idx,
"gep_" + inputs[
i]->
getName(), TI);
390 std::vector<User*>
Users(inputs[
i]->user_begin(), inputs[
i]->user_end());
393 if (Blocks.count(inst->getParent()))
394 inst->replaceUsesOfWith(inputs[
i], RewriteVal);
398 if (!AggregateArgs) {
400 for (
unsigned i = 0, e = inputs.size();
i != e; ++
i, ++AI)
401 AI->setName(inputs[
i]->getName());
402 for (
unsigned i = 0, e = outputs.size();
i != e; ++
i, ++AI)
403 AI->setName(outputs[
i]->getName()+
".out");
410 for (
unsigned i = 0, e = Users.size();
i != e; ++
i)
439 ValueSet &inputs, ValueSet &outputs) {
442 std::vector<Value*> params, StructValues, ReloadOutputs, Reloads;
447 for (
Value *input : inputs)
449 StructValues.push_back(input);
451 params.push_back(input);
454 for (
Value *output : outputs) {
456 StructValues.push_back(output);
459 new AllocaInst(output->getType(),
nullptr, output->getName() +
".loc",
461 ReloadOutputs.push_back(alloca);
462 params.push_back(alloca);
468 if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
469 std::vector<Type*> ArgTypes;
471 ve = StructValues.end(); v != ve; ++v)
472 ArgTypes.push_back((*v)->getType());
476 Struct =
new AllocaInst(StructArgTy,
nullptr,
"structArg",
478 params.push_back(Struct);
480 for (
unsigned i = 0, e = inputs.size(); i != e; ++
i) {
485 StructArgTy, Struct, Idx,
"gep_" + StructValues[i]->
getName());
494 NumExitBlocks > 1 ?
"targetBlock" :
"");
498 unsigned FirstOut = inputs.size();
503 for (
unsigned i = 0, e = outputs.size(); i != e; ++
i) {
504 Value *Output =
nullptr;
510 StructArgTy, Struct, Idx,
"gep_reload_" + outputs[i]->
getName());
514 Output = ReloadOutputs[
i];
517 Reloads.push_back(load);
519 std::vector<User*>
Users(outputs[i]->user_begin(), outputs[i]->user_end());
520 for (
unsigned u = 0, e = Users.size(); u != e; ++u) {
530 codeReplacer, 0, codeReplacer);
537 std::map<BasicBlock*, BasicBlock*> ExitBlockMap;
539 unsigned switchVal = 0;
546 BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
551 OldTarget->
getName() +
".exitStub",
553 unsigned SuccNum = switchVal++;
555 Value *brVal =
nullptr;
556 switch (NumExitBlocks) {
576 for (
unsigned out = 0, e = outputs.size(); out != e; ++out) {
581 bool DominatesDef =
true;
584 if (
auto *Invoke = dyn_cast<InvokeInst>(outputs[out]))
585 NormalDest = Invoke->getNormalDest();
588 DefBlock = NormalDest;
593 for (
const auto &
I : ExitBlockMap)
594 if (DefBlock ==
I.second) {
602 if (!DT && DefBlock != OldTarget)
603 DominatesDef =
false;
607 DominatesDef = DT->
dominates(DefBlock, OldTarget);
615 if (pred && DT && DT->
dominates(DefBlock, pred))
626 StructArgTy, &*OAI, Idx,
"gep_" + outputs[out]->
getName(),
630 new StoreInst(outputs[out], &*OAI, NTRet);
634 if (!AggregateArgs) ++OAI;
645 switch (NumExitBlocks) {
688 void CodeExtractor::moveCodeToFunction(
Function *newFunction) {
702 void CodeExtractor::calculateNewCallTerminatorWeights(
714 Distribution BranchDist;
718 BlockNode ExitNode(i);
719 uint64_t ExitFreq = ExitWeights[TI->
getSuccessor(i)].getFrequency();
721 BranchDist.addExit(ExitNode, ExitFreq);
727 if (BranchDist.Total == 0)
731 BranchDist.normalize();
734 for (
unsigned I = 0,
E = BranchDist.Weights.
size();
I <
E; ++
I) {
735 const auto &Weight = BranchDist.Weights[
I];
738 BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
761 assert(BPI &&
"Both BPI and BFI are required to preserve profile info");
763 if (Blocks.count(Pred))
771 severSplitPHINodes(header);
781 "codeRepl", oldFunction,
800 if (!Blocks.count(*SI)) {
810 NumExitBlocks = ExitBlocks.
size();
813 Function *newFunction = constructFunction(inputs, outputs, header,
815 codeReplacer, oldFunction,
827 emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
829 moveCodeToFunction(newFunction);
832 if (
BFI && NumExitBlocks > 1)
833 calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
847 std::vector<BasicBlock*> Succs(
succ_begin(codeReplacer),
849 for (
unsigned i = 0, e = Succs.size(); i != e; ++
i)
852 std::set<BasicBlock*> ProcessedPreds;
DomTreeNodeBase< NodeT > * addNewBlock(NodeT *BB, NodeT *DomBB)
Add a new node to the dominator tree information.
Return a value (possibly void), from a function.
SymbolTableList< Instruction >::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
iterator_range< use_iterator > uses()
static IntegerType * getInt1Ty(LLVMContext &C)
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
A Module instance is used to store all the information related to an LLVM module. ...
void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
This class represents a function call, abstracting a target machine's calling convention.
const_iterator begin(StringRef path)
Get begin iterator over path.
Type * getReturnType() const
Returns the type of the ret val.
bool isEHPad() const
Return true if this basic block is an exception handling block.
const Function * getParent() const
Return the enclosing method, or null if none.
const Instruction & front() const
bool isSubRegion() const
Is this RegionNode a subregion?
An instruction for reading from memory.
uint64_t getFrequency() const
Returns the frequency as a fixpoint number scaled by the entry frequency.
iv Induction Variable Users
static IntegerType * getInt16Ty(LLVMContext &C)
iterator end()
Get an iterator to the end of the SetVector.
T * getNodeAs() const
Get the content of this RegionNode.
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
StringRef getName() const
Return a constant reference to the value's name.
iterator begin()
Instruction iterator methods.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, Instruction *InsertBefore=nullptr)
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
Remove an incoming value.
bool doesNotThrow() const
Determine if the function cannot unwind.
Class to represent struct types.
A Use represents the edge between a Value definition and its users.
static void advance(T &it, size_t Val)
Instruction * getFirstNonPHI()
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
static StringRef getName(Value *V)
Interval::succ_iterator succ_begin(Interval *I)
succ_begin/succ_end - define methods so that Intervals may be used just like BasicBlocks can with the...
Class to represent function types.
bool insert(const value_type &X)
Insert a new element into the SetVector.
const T & getValue() const LLVM_LVALUE_FUNCTION
iterator begin()
Get an iterator to the beginning of the SetVector.
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
Base class for the actual dominator tree node.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
void setSuccessor(unsigned idx, BasicBlock *B)
Update the specified successor to point at the provided block.
An instruction for storing to memory.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
Interval::succ_iterator succ_end(Interval *I)
unsigned getNumIncomingValues() const
Return the number of incoming edges.
void replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
unsigned getNumSuccessors() const
Return the number of successors that this terminator has.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Subclasses of this class are all able to terminate a basic block.
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
BasicBlock * getSuccessor(unsigned idx) const
Return the specified successor.
This is an important class for using LLVM in a threaded context.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Distribution of unscaled probability weight.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Interval::pred_iterator pred_begin(Interval *I)
pred_begin/pred_end - define methods so that Intervals may be used just like BasicBlocks can with the...
static Type * getVoidTy(LLVMContext &C)
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const InstListType & getInstList() const
Return the underlying instruction list container.
Interval::pred_iterator pred_end(Interval *I)
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
self_iterator getIterator()
LLVMContext & getContext() const
All values hold a context through their type.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
void setEntryCount(uint64_t Count)
Set the entry count for this function.
Iterator for intrusive lists based on ilist_node.
const BasicBlockListType & getBasicBlockList() const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
static PointerType * getUnqual(Type *ElementType)
This constructs a pointer to an object of the specified type in the generic address space (address sp...
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
static StructType * get(LLVMContext &Context, ArrayRef< Type * > Elements, bool isPacked=false)
This static method is the primary way to create a literal StructType.
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, Instruction *InsertBefore=nullptr)
static CallInst * Create(Value *Func, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=None, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
void setEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors, BranchProbability Prob)
Set the raw edge probability for the given edge.
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=nullptr)
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
pred_range predecessors(BasicBlock *BB)
const BasicBlock & getEntryBlock() const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
AttributeSet getAttributes() const
Return the attribute list for this Function.
void clear()
Completely clear the SetVector.
void push_back(pointer val)
iterator_range< user_iterator > users()
Value * getCondition() const
pointer remove(iterator &IT)
block_iterator block_begin()
BasicBlock * getSuccessor(unsigned idx) const
Analysis providing branch probability information.
size_type count(const key_type &key) const
Count the number of elements of a given key in the SetVector.
block_iterator block_end()
static IntegerType * getInt32Ty(LLVMContext &C)
void setCondition(Value *V)
Represents a single loop in the control flow graph.
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
bool verifyFunction(const Function &F, raw_ostream *OS=nullptr)
Check a function for errors, useful for use when debugging a pass.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Rename collisions when linking (static functions).
bool hasUWTable() const
True if the ABI mandates (or the user requested) that this function be in a unwind table...
BasicBlock * splitBasicBlock(iterator I, const Twine &BBName="")
Split the basic block into two basic blocks at the specified instruction.
void removeCase(CaseIt i)
This method removes the specified case and its successor from the switch instruction.
void changeImmediateDominator(DomTreeNodeBase< NodeT > *N, DomTreeNodeBase< NodeT > *NewIDom)
changeImmediateDominator - This method is used to update the dominator tree information when a node's...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
const BasicBlock & front() const
LLVMContext & getContext() const
Get the context in which this basic block lives.
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
vector_type::const_iterator iterator
void setDefaultDest(BasicBlock *DefaultCase)
A vector that has set insertion semantics.
static const Function * getParent(const Value *V)
BranchProbability getEdgeProbability(const BasicBlock *Src, unsigned IndexInSuccessors) const
Get an edge's probability, relative to other out-edges of the Src.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=nullptr)
static BranchProbability getZero()
DomTreeNodeBase< NodeT > * getNode(NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
const BasicBlock * getParent() const
Representative of a block.
LLVMContext & getContext() const
Get the global data context.
bool isVoidTy() const
Return true if this is 'void'.
an instruction to allocate memory on the stack
void splitBlock(NodeT *NewBB)
splitBlock - BB is split and now it has one successor.
AttributeSet getFnAttributes() const
The function attributes are returned.