本文整理汇总了C++中function::arg_iterator::getName方法的典型用法代码示例。如果您正苦于以下问题:C++ arg_iterator::getName方法的具体用法?C++ arg_iterator::getName怎么用?C++ arg_iterator::getName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类function::arg_iterator
的用法示例。
在下文中一共展示了arg_iterator::getName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: runOnModule
bool LLPE::runOnModule(Module &M) {
vector<int> argv = readInputFile();
for (Module::iterator F = M.begin(), F_end = M.end(); F != F_end; ++F) {
for (Function::arg_iterator A = F->arg_begin(), A_end = F->arg_end(); A != A_end; ++A) {
//Search for variables referencing argv
if (A->getName() == "argv") {
//Iterate through uses of argv
for (Value::use_iterator U = A->use_begin(), U_end = A->use_end(); U != U_end; ++U) {
Instruction *User = dyn_cast<Instruction>(*U);
StoreInst *SI = dyn_cast<StoreInst>(User);
AllocaInst *OrigAlloca = dyn_cast<AllocaInst>(SI->getOperand(1));
for (Value::use_iterator U2 = OrigAlloca->use_begin(), U2_end = OrigAlloca->use_end(); U2 != U2_end; ++U2) {
Instruction *User2 = dyn_cast<Instruction>(*U2);
for (Value::use_iterator U3 = User2->use_begin(), U3_end = OrigAlloca->use_end(); U3 != U3_end; ++U3) {
searchForStoreInstruction(dyn_cast<Instruction>(*U3)->getParent(), argv);
}
}
}
}
}
}
return true;
}
示例2: dumpStack
void ExecutionState::dumpStack(llvm::raw_ostream &out) const {
unsigned idx = 0;
const KInstruction *target = prevPC;
for (ExecutionState::stack_ty::const_reverse_iterator
it = stack.rbegin(), ie = stack.rend();
it != ie; ++it) {
const StackFrame &sf = *it;
Function *f = sf.kf->function;
const InstructionInfo &ii = *target->info;
out << "\t#" << idx++;
std::stringstream AssStream;
AssStream << std::setw(8) << std::setfill('0') << ii.assemblyLine;
out << AssStream.str();
out << " in " << f->getName().str() << " (";
// Yawn, we could go up and print varargs if we wanted to.
unsigned index = 0;
for (Function::arg_iterator ai = f->arg_begin(), ae = f->arg_end();
ai != ae; ++ai) {
if (ai!=f->arg_begin()) out << ", ";
out << ai->getName().str();
// XXX should go through function
ref<Expr> value = sf.locals[sf.kf->getArgRegister(index++)].value;
if (value.get() && isa<ConstantExpr>(value))
out << "=" << value;
}
out << ")";
if (ii.file != "")
out << " at " << ii.file << ":" << ii.line;
out << "\n";
target = sf.caller;
}
}
示例3: dump
void StackTrace::dump(std::ostream &out) const {
unsigned idx = 0;
for (stack_t::const_iterator it = contents.begin(); it != contents.end(); it++) {
Function *f = it->first.first->function;
const InstructionInfo &ii = *it->first.second->info;
out << "\t#" << idx++
<< " " << std::setw(8) << std::setfill('0') << ii.assemblyLine
<< " in " << f->getName().str() << " (";
unsigned index = 0;
for (Function::arg_iterator ai = f->arg_begin(), ae = f->arg_end();
ai != ae; ++ai) {
if (ai!=f->arg_begin()) out << ", ";
out << ai->getName().str();
// XXX should go through function
ref<Expr> value = it->second[index++];
if (isa<ConstantExpr>(value))
out << "=" << value;
}
out << ")";
if (ii.file != "")
out << " at " << ii.file << ":" << ii.line;
out << "\n";
}
}
示例4: get_arg_count
int get_arg_count(const char *name, Function *op)
{
uint8_t args_present[MAX_ARGS];
int nb_args, i, n;
const char *p;
for(i = 0;i < MAX_ARGS; i++)
args_present[i] = 0;
// compute the number of arguments by looking at
// the uses of the op parameters
for (Function::arg_iterator i = op->arg_begin(), e = op->arg_end(); i != e; ++i) {
const char *tmpArgName = i->getName().c_str();
char *argName = (char *) malloc(strlen(tmpArgName + 1));
strcpy(argName, tmpArgName);
if (strstart(argName, "__op_param", &p)) {
if (i->hasNUsesOrMore(1)) {
n = strtoul(p, NULL, 10);
if (n > MAX_ARGS)
error("too many arguments in %s", name);
args_present[n - 1] = 1;
}
}
}
for (i = 1; i <= MAX_ARGS; i++) {
char *funcName = (char *) malloc(20);
sprintf(funcName, "__op_gen_label%d", i);
Function *f = ops->getFunction(std::string(funcName));
if (f != NULL) {
for (Function::use_iterator j = f->use_begin(), e = f->use_end(); j != e; ++j) {
if (Instruction *inst = dyn_cast<Instruction>(*j)) {
if (inst->getParent()->getParent() == op) {
args_present[i - 1] = 1;
}
}
}
} else {
std::cout << "symbol not found\n";
}
}
nb_args = 0;
while (nb_args < MAX_ARGS && args_present[nb_args])
nb_args++;
for(i = nb_args; i < MAX_ARGS; i++) {
if (args_present[i])
error("inconsistent argument numbering in %s", name);
}
return nb_args;
}
示例5: lowerIncomingArguments
/// lowerIncomingArguments - To avoid having to handle incoming arguments
/// specially, we lower each arg to a copy instruction in the entry block. This
/// ensures that the argument value itself cannot be live out of the entry
/// block.
void SjLjEHPrepare::lowerIncomingArguments(Function &F) {
BasicBlock::iterator AfterAllocaInsPt = F.begin()->begin();
while (isa<AllocaInst>(AfterAllocaInsPt) &&
isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsPt)->getArraySize()))
++AfterAllocaInsPt;
for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
++AI) {
Type *Ty = AI->getType();
if (isa<StructType>(Ty) || isa<ArrayType>(Ty)) {
// Aggregate types can't be cast, but are legal argument types,
// so we have to handle them differently. We use
// select i8 true, %arg, undef to achieve the same goal
Value *TrueValue = ConstantInt::getTrue(F.getContext());
Value *UndefValue = UndefValue::get(Ty);
Instruction *SI = SelectInst::Create(TrueValue, AI, UndefValue,
AI->getName() + ".tmp",
AfterAllocaInsPt);
AI->replaceAllUsesWith(SI);
SI->setOperand(1, AI);
} else {
// This is always a no-op cast because we're casting AI to AI->getType()
// so src and destination types are identical. BitCast is the only
// possibility.
CastInst *NC = new BitCastInst(AI, AI->getType(), AI->getName() + ".tmp",
AfterAllocaInsPt);
AI->replaceAllUsesWith(NC);
// Set the operand of the cast instruction back to the AllocaInst.
// Normally it's forbidden to replace a CastInst's operand because it
// could cause the opcode to reflect an illegal conversion. However, we're
// replacing it here with the same value it was constructed with. We do
// this because the above replaceAllUsesWith() clobbered the operand, but
// we want this one to remain.
NC->setOperand(0, AI);
}
}
}
示例6: while
/* for the vi and lsup, find their corresponding argument*/
Value *FindFunctionArgumentOfInstr(Instruction *I, Function *F) {
int notFound = 1;
Value *via = 0;
Function::arg_iterator FI = F->arg_begin(), FE = F->arg_end();
while (FI != FE && notFound) {
if (FI->getName() == I->getName()) {
via = &*FI;
notFound = 0;
}
++FI;
}
return via;
}
示例7: CloneFunctionInto
// Maybe make a clone, if a clone is made, return a pointer to it, if a clone
// was not made return nullptr.
Function *CSDataRando::makeFunctionClone(Function *F) {
// Now we know how many arguments need to be passed, so we make the clones
FuncInfo &FI = FunctionInfo[F];
if (FI.ArgNodes.size() == 0) {
// No additional args to pass, no need to clone.
return nullptr;
}
// Determine the type of the new function, we insert the new parameters for
// the masks after the normal arguments, but before any va_args
Type *MaskTy = TypeBuilder<mask_t, false>::get(F->getContext());
FunctionType *OldFuncTy = F->getFunctionType();
std::vector<Type*> ArgTys;
ArgTys.insert(ArgTys.end(), OldFuncTy->param_begin(), OldFuncTy->param_end());
ArgTys.insert(ArgTys.end(), FI.ArgNodes.size(), MaskTy);
FunctionType *CloneFuncTy = FunctionType::get(OldFuncTy->getReturnType(), ArgTys, OldFuncTy->isVarArg());
Function *Clone = Function::Create(CloneFuncTy, Function::InternalLinkage, F->getName() + "_CONTEXT_SENSITIVE");
F->getParent()->getFunctionList().insert(F->getIterator(), Clone);
Function::arg_iterator CI = Clone->arg_begin(), CE = Clone->arg_end();
// Map the old arguments to the clone arguments and set the name of the
// clone arguments the same as the original.
for (Function::arg_iterator i = F->arg_begin(), e = F->arg_end(); i != e && CI != CE; i++, CI++) {
FI.OldToNewMap[&*i] = &*CI;
CI->setName(i->getName());
}
// Set the name of the arg masks and associate them with the nodes they are
// the masks for.
for (unsigned i = 0, e = FI.ArgNodes.size(); i != e; ++i, ++CI) {
CI->setName("arg_mask");
FI.ArgMaskMap[FI.ArgNodes[i]] = &*CI;
}
SmallVector<ReturnInst*, 8> Returns;
CloneFunctionInto(Clone, F, FI.OldToNewMap, false, Returns);
Clone->setCallingConv(F->getCallingConv());
// Invert OldToNewMap
for (auto I : FI.OldToNewMap) {
FI.NewToOldMap[I.second] = I.first;
}
NumClones++;
return Clone;
}
示例8: copy_function
void HeterotbbTransform::copy_function (Function* NF, Function* F) {
DenseMap<const Value*, Value *> ValueMap;
// Get the names of the parameters for old function
for(Function::arg_iterator FI = F->arg_begin(), FE=F->arg_end(), DI=NF->arg_begin(); FE!=FI; ++FI,++DI) {
DI->setName(FI->getName());
ValueMap[FI]=DI;
}
for (Function::const_iterator BI=F->begin(),BE = F->end(); BI != BE; ++BI) {
const BasicBlock &FBB = *BI;
BasicBlock *NFBB = BasicBlock::Create(FBB.getContext(), "", NF);
ValueMap[&FBB] = NFBB;
if (FBB.hasName()) {
NFBB->setName(FBB.getName());
//DEBUG(dbgs()<<NFBB->getName()<<"\n");
}
for (BasicBlock::const_iterator II = FBB.begin(), IE = FBB.end(); II != IE; ++II) {
Instruction *NFInst = II->clone(/*F->getContext()*/);
if (II->hasName()) NFInst->setName(II->getName());
const Instruction *FInst = &(*II);
rewrite_instruction((Instruction *)FInst, NFInst, ValueMap);
NFBB->getInstList().push_back(NFInst);
ValueMap[II] = NFInst;
}
}
// Remap the instructions again to take care of forward jumps
for (Function::iterator BB = NF->begin(), BE=NF->end(); BB != BE; ++ BB) {
for (BasicBlock::iterator II = BB->begin(); II != BB->end(); ++II) {
int opIdx = 0;
//DEBUG(dbgs()<<*II<<"\n");
for (User::op_iterator i = II->op_begin(), e = II->op_end(); i != e; ++i, opIdx++) {
Value *V = *i;
if (ValueMap[V] != NULL) {
II->setOperand(opIdx, ValueMap[V]);
}
}
}
}
//NF->dump();
}
示例9: lowerIncomingArguments
/// lowerIncomingArguments - To avoid having to handle incoming arguments
/// specially, we lower each arg to a copy instruction in the entry block. This
/// ensures that the argument value itself cannot be live out of the entry
/// block.
void SjLjEHPass::lowerIncomingArguments(Function &F) {
BasicBlock::iterator AfterAllocaInsPt = F.begin()->begin();
while (isa<AllocaInst>(AfterAllocaInsPt) &&
isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsPt)->getArraySize()))
++AfterAllocaInsPt;
for (Function::arg_iterator
AI = F.arg_begin(), AE = F.arg_end(); AI != AE; ++AI) {
Type *Ty = AI->getType();
// Aggregate types can't be cast, but are legal argument types, so we have
// to handle them differently. We use an extract/insert pair as a
// lightweight method to achieve the same goal.
if (isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) {
Instruction *EI = ExtractValueInst::Create(AI, 0, "", AfterAllocaInsPt);
Instruction *NI = InsertValueInst::Create(AI, EI, 0);
NI->insertAfter(EI);
AI->replaceAllUsesWith(NI);
// Set the operand of the instructions back to the AllocaInst.
EI->setOperand(0, AI);
NI->setOperand(0, AI);
} else {
// This is always a no-op cast because we're casting AI to AI->getType()
// so src and destination types are identical. BitCast is the only
// possibility.
CastInst *NC =
new BitCastInst(AI, AI->getType(), AI->getName() + ".tmp",
AfterAllocaInsPt);
AI->replaceAllUsesWith(NC);
// Set the operand of the cast instruction back to the AllocaInst.
// Normally it's forbidden to replace a CastInst's operand because it
// could cause the opcode to reflect an illegal conversion. However, we're
// replacing it here with the same value it was constructed with. We do
// this because the above replaceAllUsesWith() clobbered the operand, but
// we want this one to remain.
NC->setOperand(0, AI);
}
}
}
示例10: dupFuncArgu
/////////////////////////
//dupFuncArgu() //
/////////////////////////
void InsDuplica::dupFuncArgu(Function &F) {
BasicBlock *firstBB = F.begin();
Instruction *firstI = firstBB->begin();
for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
AI != E; ++AI) {
arguSet.insert(AI);
//if argument is used only once or all on the same block,
//do not dup it.
if (AI->hasOneUse()) {
valueMap[AI] = AI;
} else {
//make copy of cast
Type *arguType = AI->getType();
//FIXME unknow signed or not
CastInst* newCast = CastInst::CreateIntegerCast(AI, arguType, 1, AI->getName()+"_dup", firstI);
//CastInst *newCast = new CastInst(AI, arguType, AI->getName()+"_dup", firstI);
valueMap[AI] = newCast;
}
}
}
示例11: lowerIncomingArguments
/// lowerIncomingArguments - To avoid having to handle incoming arguments
/// specially, we lower each arg to a copy instruction in the entry block. This
/// ensures that the argument value itself cannot be live out of the entry
/// block.
void SjLjEHPrepare::lowerIncomingArguments(Function &F) {
BasicBlock::iterator AfterAllocaInsPt = F.begin()->begin();
while (isa<AllocaInst>(AfterAllocaInsPt) &&
isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsPt)->getArraySize()))
++AfterAllocaInsPt;
for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end(); AI != AE;
++AI) {
Type *Ty = AI->getType();
// Use 'select i8 true, %arg, undef' to simulate a 'no-op' instruction.
Value *TrueValue = ConstantInt::getTrue(F.getContext());
Value *UndefValue = UndefValue::get(Ty);
Instruction *SI = SelectInst::Create(TrueValue, AI, UndefValue,
AI->getName() + ".tmp",
AfterAllocaInsPt);
AI->replaceAllUsesWith(SI);
// Reset the operand, because it was clobbered by the RAUW above.
SI->setOperand(1, AI);
}
}
示例12: linkFunctionBody
// linkFunctionBody - Copy the source function over into the dest function and
// fix up references to values. At this point we know that Dest is an external
// function, and that Src is not.
void ModuleLinker::linkFunctionBody(Function *Dst, Function *Src) {
assert(Src && Dst && Dst->isDeclaration() && !Src->isDeclaration());
// Go through and convert function arguments over, remembering the mapping.
Function::arg_iterator DI = Dst->arg_begin();
for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
I != E; ++I, ++DI) {
DI->setName(I->getName()); // Copy the name over.
// Add a mapping to our mapping.
ValueMap[I] = DI;
}
if (Mode == Linker::DestroySource) {
// Splice the body of the source function into the dest function.
Dst->getBasicBlockList().splice(Dst->end(), Src->getBasicBlockList());
// At this point, all of the instructions and values of the function are now
// copied over. The only problem is that they are still referencing values in
// the Source function as operands. Loop through all of the operands of the
// functions and patch them up to point to the local versions.
for (Function::iterator BB = Dst->begin(), BE = Dst->end(); BB != BE; ++BB)
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
RemapInstruction(I, ValueMap, RF_IgnoreMissingEntries, &TypeMap);
} else {
// Clone the body of the function into the dest function.
SmallVector<ReturnInst*, 8> Returns; // Ignore returns.
CloneFunctionInto(Dst, Src, ValueMap, false, Returns, "", NULL, &TypeMap);
}
// There is no need to map the arguments anymore.
for (Function::arg_iterator I = Src->arg_begin(), E = Src->arg_end();
I != E; ++I)
ValueMap.erase(I);
}
示例13: SplitCriticalEdge
// First thing we need to do is scan the whole function for values that are
// live across unwind edges. Each value that is live across an unwind edge
// we spill into a stack location, guaranteeing that there is nothing live
// across the unwind edge. This process also splits all critical edges
// coming out of invoke's.
void LowerInvoke::
splitLiveRangesLiveAcrossInvokes(std::vector<InvokeInst*> &Invokes) {
// First step, split all critical edges from invoke instructions.
for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
InvokeInst *II = Invokes[i];
SplitCriticalEdge(II, 0, this);
SplitCriticalEdge(II, 1, this);
assert(!isa<PHINode>(II->getNormalDest()) &&
!isa<PHINode>(II->getUnwindDest()) &&
"critical edge splitting left single entry phi nodes?");
}
Function *F = Invokes.back()->getParent()->getParent();
// To avoid having to handle incoming arguments specially, we lower each arg
// to a copy instruction in the entry block. This ensures that the argument
// value itself cannot be live across the entry block.
BasicBlock::iterator AfterAllocaInsertPt = F->begin()->begin();
while (isa<AllocaInst>(AfterAllocaInsertPt) &&
isa<ConstantInt>(cast<AllocaInst>(AfterAllocaInsertPt)->getArraySize()))
++AfterAllocaInsertPt;
for (Function::arg_iterator AI = F->arg_begin(), E = F->arg_end();
AI != E; ++AI) {
// This is always a no-op cast because we're casting AI to AI->getType() so
// src and destination types are identical. BitCast is the only possibility.
CastInst *NC = new BitCastInst(
AI, AI->getType(), AI->getName()+".tmp", AfterAllocaInsertPt);
AI->replaceAllUsesWith(NC);
// Normally its is forbidden to replace a CastInst's operand because it
// could cause the opcode to reflect an illegal conversion. However, we're
// replacing it here with the same value it was constructed with to simply
// make NC its user.
NC->setOperand(0, AI);
}
// Finally, scan the code looking for instructions with bad live ranges.
for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
for (BasicBlock::iterator II = BB->begin(), E = BB->end(); II != E; ++II) {
// Ignore obvious cases we don't have to handle. In particular, most
// instructions either have no uses or only have a single use inside the
// current block. Ignore them quickly.
Instruction *Inst = II;
if (Inst->use_empty()) continue;
if (Inst->hasOneUse() &&
cast<Instruction>(Inst->use_back())->getParent() == BB &&
!isa<PHINode>(Inst->use_back())) continue;
// If this is an alloca in the entry block, it's not a real register
// value.
if (AllocaInst *AI = dyn_cast<AllocaInst>(Inst))
if (isa<ConstantInt>(AI->getArraySize()) && BB == F->begin())
continue;
// Avoid iterator invalidation by copying users to a temporary vector.
std::vector<Instruction*> Users;
for (Value::use_iterator UI = Inst->use_begin(), E = Inst->use_end();
UI != E; ++UI) {
Instruction *User = cast<Instruction>(*UI);
if (User->getParent() != BB || isa<PHINode>(User))
Users.push_back(User);
}
// Scan all of the uses and see if the live range is live across an unwind
// edge. If we find a use live across an invoke edge, create an alloca
// and spill the value.
std::set<InvokeInst*> InvokesWithStoreInserted;
// Find all of the blocks that this value is live in.
std::set<BasicBlock*> LiveBBs;
LiveBBs.insert(Inst->getParent());
while (!Users.empty()) {
Instruction *U = Users.back();
Users.pop_back();
if (!isa<PHINode>(U)) {
MarkBlocksLiveIn(U->getParent(), LiveBBs);
} else {
// Uses for a PHI node occur in their predecessor block.
PHINode *PN = cast<PHINode>(U);
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
if (PN->getIncomingValue(i) == Inst)
MarkBlocksLiveIn(PN->getIncomingBlock(i), LiveBBs);
}
}
// Now that we know all of the blocks that this thing is live in, see if
// it includes any of the unwind locations.
bool NeedsSpill = false;
for (unsigned i = 0, e = Invokes.size(); i != e; ++i) {
BasicBlock *UnwindBlock = Invokes[i]->getUnwindDest();
if (UnwindBlock != BB && LiveBBs.count(UnwindBlock)) {
NeedsSpill = true;
}
}
//.........这里部分代码省略.........
示例14: runOnModule
//
// Method: runOnModule()
//
// Description:
// Entry point for this LLVM pass.
// Clone functions that take GEPs as arguments
//
// Inputs:
// M - A reference to the LLVM module to transform
//
// Outputs:
// M - The transformed LLVM module.
//
// Return value:
// true - The module was modified.
// false - The module was not modified.
//
bool GEPExprArgs::runOnModule(Module& M) {
bool changed;
do {
changed = false;
for (Module::iterator F = M.begin(); F != M.end(); ++F){
for (Function::iterator B = F->begin(), FE = F->end(); B != FE; ++B) {
for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
CallInst *CI = dyn_cast<CallInst>(I++);
if(!CI)
continue;
if(CI->hasByValArgument())
continue;
// if the GEP calls a function, that is externally defined,
// or might be changed, ignore this call site.
Function *F = CI->getCalledFunction();
if (!F || (F->isDeclaration() || F->mayBeOverridden()))
continue;
if(F->hasStructRetAttr())
continue;
if(F->isVarArg())
continue;
// find the argument we must replace
Function::arg_iterator ai = F->arg_begin(), ae = F->arg_end();
unsigned argNum = 1;
for(; argNum < CI->getNumOperands();argNum++, ++ai) {
if(ai->use_empty())
continue;
if (isa<GEPOperator>(CI->getOperand(argNum)))
break;
}
// if no argument was a GEP operator to be changed
if(ai == ae)
continue;
GEPOperator *GEP = dyn_cast<GEPOperator>(CI->getOperand(argNum));
if(!GEP->hasAllConstantIndices())
continue;
// Construct the new Type
// Appends the struct Type at the beginning
std::vector<Type*>TP;
TP.push_back(GEP->getPointerOperand()->getType());
for(unsigned c = 1; c < CI->getNumOperands();c++) {
TP.push_back(CI->getOperand(c)->getType());
}
//return type is same as that of original instruction
FunctionType *NewFTy = FunctionType::get(CI->getType(), TP, false);
Function *NewF;
numSimplified++;
if(numSimplified > 800)
return true;
NewF = Function::Create(NewFTy,
GlobalValue::InternalLinkage,
F->getName().str() + ".TEST",
&M);
Function::arg_iterator NI = NewF->arg_begin();
NI->setName("GEParg");
++NI;
ValueToValueMapTy ValueMap;
for (Function::arg_iterator II = F->arg_begin(); NI != NewF->arg_end(); ++II, ++NI) {
ValueMap[II] = NI;
NI->setName(II->getName());
NI->addAttr(F->getAttributes().getParamAttributes(II->getArgNo() + 1));
}
NewF->setAttributes(NewF->getAttributes().addAttr(
0, F->getAttributes().getRetAttributes()));
// Perform the cloning.
SmallVector<ReturnInst*,100> Returns;
CloneFunctionInto(NewF, F, ValueMap, false, Returns);
std::vector<Value*> fargs;
for(Function::arg_iterator ai = NewF->arg_begin(),
ae= NewF->arg_end(); ai != ae; ++ai) {
fargs.push_back(ai);
}
//.........这里部分代码省略.........
示例15: runOnModule
//
// Method: runOnModule()
//
// Description:
// Entry point for this LLVM pass.
// If a function returns a struct, make it return
// a pointer to the struct.
//
// Inputs:
// M - A reference to the LLVM module to transform
//
// Outputs:
// M - The transformed LLVM module.
//
// Return value:
// true - The module was modified.
// false - The module was not modified.
//
bool StructRet::runOnModule(Module& M) {
const llvm::DataLayout targetData(&M);
std::vector<Function*> worklist;
for (Module::iterator I = M.begin(); I != M.end(); ++I)
if (!I->mayBeOverridden()) {
if(I->hasAddressTaken())
continue;
if(I->getReturnType()->isStructTy()) {
worklist.push_back(I);
}
}
while(!worklist.empty()) {
Function *F = worklist.back();
worklist.pop_back();
Type *NewArgType = F->getReturnType()->getPointerTo();
// Construct the new Type
std::vector<Type*>TP;
TP.push_back(NewArgType);
for (Function::arg_iterator ii = F->arg_begin(), ee = F->arg_end();
ii != ee; ++ii) {
TP.push_back(ii->getType());
}
FunctionType *NFTy = FunctionType::get(F->getReturnType(), TP, F->isVarArg());
// Create the new function body and insert it into the module.
Function *NF = Function::Create(NFTy,
F->getLinkage(),
F->getName(), &M);
ValueToValueMapTy ValueMap;
Function::arg_iterator NI = NF->arg_begin();
NI->setName("ret");
++NI;
for (Function::arg_iterator II = F->arg_begin(); II != F->arg_end(); ++II, ++NI) {
ValueMap[II] = NI;
NI->setName(II->getName());
AttributeSet attrs = F->getAttributes().getParamAttributes(II->getArgNo() + 1);
if (!attrs.isEmpty())
NI->addAttr(attrs);
}
// Perform the cloning.
SmallVector<ReturnInst*,100> Returns;
if (!F->isDeclaration())
CloneFunctionInto(NF, F, ValueMap, false, Returns);
std::vector<Value*> fargs;
for(Function::arg_iterator ai = NF->arg_begin(),
ae= NF->arg_end(); ai != ae; ++ai) {
fargs.push_back(ai);
}
NF->setAttributes(NF->getAttributes().addAttributes(
M.getContext(), 0, F->getAttributes().getRetAttributes()));
NF->setAttributes(NF->getAttributes().addAttributes(
M.getContext(), ~0, F->getAttributes().getFnAttributes()));
for (Function::iterator B = NF->begin(), FE = NF->end(); B != FE; ++B) {
for (BasicBlock::iterator I = B->begin(), BE = B->end(); I != BE;) {
ReturnInst * RI = dyn_cast<ReturnInst>(I++);
if(!RI)
continue;
LoadInst *LI = dyn_cast<LoadInst>(RI->getOperand(0));
assert(LI && "Return should be preceded by a load instruction");
IRBuilder<> Builder(RI);
Builder.CreateMemCpy(fargs.at(0),
LI->getPointerOperand(),
targetData.getTypeStoreSize(LI->getType()),
targetData.getPrefTypeAlignment(LI->getType()));
}
}
for(Value::use_iterator ui = F->use_begin(), ue = F->use_end();
ui != ue; ) {
CallInst *CI = dyn_cast<CallInst>(*ui++);
if(!CI)
continue;
if(CI->getCalledFunction() != F)
continue;
if(CI->hasByValArgument())
continue;
AllocaInst *AllocaNew = new AllocaInst(F->getReturnType(), 0, "", CI);
//.........这里部分代码省略.........