44 bool hasCalls =
false, hasDynamicAllocas =
false, hasStaticAllocas =
false;
51 NewInst->
setName(II->getName()+NameSuffix);
55 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
56 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
57 if (isa<ConstantInt>(AI->getArraySize()))
58 hasStaticAllocas =
true;
60 hasDynamicAllocas =
true;
78 bool ModuleLevelChanges,
83 assert(NameSuffix &&
"NameSuffix cannot be null!");
88 assert(VMap.
count(
I) &&
"No mapping from source argument specified!");
96 if (
Argument *Anew = dyn_cast<Argument>(VMap[
I])) {
100 Anew->addAttr(attrs);
105 AttributeSet::ReturnIndex,
109 AttributeSet::FunctionIndex,
134 const_cast<BasicBlock*>(&BB));
146 BE = NewFunc->
end(); BB != BE; ++BB)
151 TypeMapper, Materializer);
163 bool ModuleLevelChanges,
165 std::vector<Type*> ArgTypes;
173 ArgTypes.push_back(
I->getType());
187 DestI->setName(
I->getName());
201 struct PruningFunctionCloner {
205 bool ModuleLevelChanges;
206 const char *NameSuffix;
212 bool moduleLevelChanges,
213 const char *nameSuffix,
216 : NewFunc(newFunc), OldFunc(oldFunc),
217 VMap(valueMap), ModuleLevelChanges(moduleLevelChanges),
218 NameSuffix(nameSuffix), CodeInfo(codeInfo),
TD(td) {
224 std::vector<const BasicBlock*> &ToClone);
230 void PruningFunctionCloner::CloneBlock(
const BasicBlock *BB,
231 std::vector<const BasicBlock*> &ToClone){
232 WeakVH &BBEntry = VMap[BB];
253 const_cast<BasicBlock*>(BB));
258 bool hasCalls =
false, hasDynamicAllocas =
false, hasStaticAllocas =
false;
268 if (!isa<PHINode>(NewInst)) {
278 if (
Value *MappedV = VMap.lookup(V))
288 NewInst->
setName(II->getName()+NameSuffix);
291 hasCalls |= (isa<CallInst>(II) && !isa<DbgInfoIntrinsic>(II));
292 if (
const AllocaInst *AI = dyn_cast<AllocaInst>(II)) {
293 if (isa<ConstantInt>(AI->getArraySize()))
294 hasStaticAllocas =
true;
296 hasDynamicAllocas =
true;
302 bool TerminatorDone =
false;
303 if (
const BranchInst *BI = dyn_cast<BranchInst>(OldTI)) {
304 if (BI->isConditional()) {
309 Value *V = VMap[BI->getCondition()];
310 Cond = dyn_cast_or_null<ConstantInt>(V);
317 ToClone.push_back(Dest);
318 TerminatorDone =
true;
321 }
else if (
const SwitchInst *SI = dyn_cast<SwitchInst>(OldTI)) {
325 Value *V = VMap[SI->getCondition()];
326 Cond = dyn_cast_or_null<ConstantInt>(V);
332 ToClone.push_back(Dest);
333 TerminatorDone =
true;
337 if (!TerminatorDone) {
342 VMap[OldTI] = NewInst;
351 CodeInfo->ContainsCalls |= hasCalls;
352 CodeInfo->ContainsDynamicAllocas |= hasDynamicAllocas;
353 CodeInfo->ContainsDynamicAllocas |= hasStaticAllocas &&
367 bool ModuleLevelChanges,
369 const char *NameSuffix,
373 assert(NameSuffix &&
"NameSuffix cannot be null!");
377 E = OldFunc->
arg_end(); II != E; ++II)
378 assert(VMap.
count(II) &&
"No mapping from source argument specified!");
381 PruningFunctionCloner PFC(NewFunc, OldFunc, VMap, ModuleLevelChanges,
382 NameSuffix, CodeInfo, TD);
385 std::vector<const BasicBlock*> CloneWorklist;
387 while (!CloneWorklist.empty()) {
389 CloneWorklist.pop_back();
390 PFC.CloneBlock(BB, CloneWorklist);
402 BasicBlock *NewBB = cast_or_null<BasicBlock>(V);
403 if (NewBB == 0)
continue;
411 if (
const PHINode *PN = dyn_cast<PHINode>(
I))
424 for (
unsigned phino = 0, e = PHIToResolve.
size(); phino != e; ) {
425 const PHINode *OPN = PHIToResolve[phino];
428 BasicBlock *NewBB = cast<BasicBlock>(VMap[OldBB]);
432 for (; phino != PHIToResolve.
size() &&
433 PHIToResolve[phino]->getParent() == OldBB; ++phino) {
434 OPN = PHIToResolve[phino];
435 PHINode *PN = cast<PHINode>(VMap[OPN]);
436 for (
unsigned pred = 0, e = NumPreds; pred != e; ++pred) {
438 if (
BasicBlock *MappedBlock = cast_or_null<BasicBlock>(V)) {
442 assert(InVal &&
"Unknown input value?");
460 assert(NumPreds < PN->getNumIncomingValues());
462 std::map<BasicBlock*, unsigned> PredCount;
476 for (std::map<BasicBlock*, unsigned>::iterator PCI =PredCount.begin(),
477 E = PredCount.end(); PCI != E; ++PCI) {
479 for (
unsigned NumToRemove = PCI->second; NumToRemove; --NumToRemove)
488 PN = cast<PHINode>(NewBB->
begin());
492 while ((PN = dyn_cast<PHINode>(I++))) {
495 assert(VMap[OldI] == PN &&
"VMap mismatch");
510 for (
unsigned Idx = 0, Size = PHIToResolve.
size(); Idx != Size; ++Idx)
511 if (
PHINode *PN = dyn_cast<PHINode>(VMap[PHIToResolve[Idx]]))
520 while (I != NewFunc->
end()) {
525 I->getSinglePredecessor() ==
I)) {
548 assert(!isa<PHINode>(Dest->
begin()));
558 I->getInstList().splice(I->end(), Dest->
getInstList());
572 if (
ReturnInst *RI = dyn_cast<ReturnInst>(I->getTerminator()))
void push_back(const T &Elt)
LinkageTypes getLinkage() const
LLVMContext & getContext() const
LLVM Argument representation.
const Instruction & back() const
void CloneAndPruneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=0, const DataLayout *TD=0, Instruction *TheCall=0)
AttributeSet getParamAttributes(unsigned Index) const
The attributes for the specified index are returned.
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void DeleteDeadBlock(BasicBlock *BB)
const Function * getParent() const
Return the enclosing method, or null if none.
void RemapInstruction(Instruction *I, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst * > &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=0, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
Value * MapValue(const Value *V, ValueToValueMapTy &VM, RemapFlags Flags=RF_None, ValueMapTypeRemapper *TypeMapper=0, ValueMaterializer *Materializer=0)
AttributeSet getRetAttributes() const
The attributes for the ret value are returned.
StringRef getName() const
void push_back(NodeTy *val)
Value * removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty=true)
static BranchInst * Create(BasicBlock *IfTrue, Instruction *InsertBefore=0)
bool hasAddressTaken() const
Returns true if there are any uses of this basic block other than direct branches, switches, etc. to it.
void setName(const Twine &Name)
Instruction * clone() const
uint64_t getZExtValue() const
Return the zero extended value.
bool count(const KeyT &Val) const
count - Return true if the specified key is in the map.
BasicBlock * getSuccessor(unsigned i) const
static FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
void replaceAllUsesWith(Value *V)
unsigned getNumIncomingValues() const
unsigned getNumSuccessors() const
unsigned getNumSlots() const
Return the number of slots used in this attribute list. This is the number of arguments that have an ...
LLVM Basic Block Representation.
BasicBlock * getSuccessor(unsigned idx) const
static BlockAddress * get(Function *F, BasicBlock *BB)
get - Return a BlockAddress for the specified function and basic block.
LLVM Constant Representation.
Interval::pred_iterator pred_begin(Interval *I)
BasicBlock * getIncomingBlock(unsigned i) const
const InstListType & getInstList() const
Return the underlying instruction list container.
Interval::pred_iterator pred_end(Interval *I)
static UndefValue * get(Type *T)
Value * SimplifyInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
bool isConditional() const
const BasicBlockListType & getBasicBlockList() const
Class for constant integers.
void setIncomingBlock(unsigned i, BasicBlock *BB)
Value * getIncomingValue(unsigned i) const
void eraseFromParent()
Unlink 'this' from the containing function and delete it.
BasicBlockTy * getCaseSuccessor()
Resolves successor for current case.
const BasicBlock & getEntryBlock() const
bool ConstantFoldTerminator(BasicBlock *BB, bool DeleteDeadConditions=false, const TargetLibraryInfo *TLI=0)
AttributeSet getAttributes() const
Return the attribute list for this Function.
BasicBlock * getSinglePredecessor()
Return this block if it has a single predecessor block. Otherwise return a null pointer.
BasicBlock * CloneBasicBlock(const BasicBlock *BB, ValueToValueMapTy &VMap, const Twine &NameSuffix="", Function *F=0, ClonedCodeInfo *CodeInfo=0)
TerminatorInst * getTerminator()
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
FunctionType * getFunctionType() const
Function * CloneFunction(const Function *F, ValueToValueMapTy &VMap, bool ModuleLevelChanges, ClonedCodeInfo *CodeInfo=0)
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=0, BasicBlock *InsertBefore=0)
Creates a new BasicBlock.
void setAttributes(AttributeSet attrs)
Set the attribute list for this Function.
Type * getReturnType() const
bool ContainsDynamicAllocas
const BasicBlock & front() const
LLVMContext & getContext() const
Get the context in which this basic block lives.
LLVM Value Representation.
void setIncomingValue(unsigned i, Value *V)
AttributeSet addAttributes(LLVMContext &C, unsigned Index, AttributeSet Attrs) const
Add attributes to the attribute set at the given index. Since attribute sets are immutable, this returns a new set.
const BasicBlock * getParent() const
INITIALIZE_PASS(GlobalMerge,"global-merge","Global Merge", false, false) bool GlobalMerge const DataLayout * TD
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, const Twine &N="", Module *M=0)
bool recursivelySimplifyInstruction(Instruction *I, const DataLayout *TD=0, const TargetLibraryInfo *TLI=0, const DominatorTree *DT=0)
Recursively attempt to simplify an instruction.
AttributeSet getFnAttributes() const
The function attributes are returned.