Exemplo n.º 1
0
bool
TransformComponentWithUse(Module& M, ComponentInterfaceTransform& T)
{
    bool modified = false;
    for (ComponentInterfaceTransform::FMap::const_iterator i = T.rewrites.begin(), e = T.rewrites.end(); i != e; ++i) {

        errs() << "Looking for calls to " << i->first << "\n";

        Function* f = M.getFunction(i->first);
        if (f == NULL) continue;

        for (Function::use_iterator ui = f->use_begin(), ue = f->use_end(); ui != ue; ++ui) {
            Use* use = &(*ui);
            User* user = ui->getUser();

            if (isa<CallInst>(user) ||  isa<InvokeInst>(user)) {

                // The instruction is a call site
                CallSite cs(cast<Instruction>(user));

                // If we are not the callee we should bail
                if( ! cs.isCallee(use)) {
                    continue;
                }

                const CallRewrite* const rw = T.lookupRewrite(i->first, cs.arg_begin(), cs.arg_end());
                if (rw == NULL) {
                    continue;
                }

#if DUMP
                BasicBlock* owner = cs.getInstruction()->getParent();
                errs() << "Specializing (inter-module) call to '" << cs.getCalledFunction()->getName()
                       << "' in function '" << (owner == NULL ? "??" : owner->getParent()->getName())
                       << "' on arguments [";
                for (unsigned int i = 0, cnt = 0; i < cs.arg_size(); ++i) {
                    if (!vector_in(rw->args, i)) {
                        if (cnt++ != 0)
                            errs() << ",";
                        errs() << i << "=(" << *cs.getArgument(i) << ")";
                    }
                }
                errs() << "]\n";
#endif

                Instruction* newInst = applyRewriteToCall(M, rw, cs);
                llvm::ReplaceInstWithInst(cs.getInstruction(), newInst);

                modified = true;
            }
        }
    }

    return modified;
}
Exemplo n.º 2
0
static GlobalVariable* parameter_access_global_variable(Argument* Arg)
{
   Function* F = Arg->getParent();
   for(Function::use_iterator U = F->use_begin(), E = F->use_end(); U!=E;++U){
      CallInst* CI = dyn_cast<CallInst>(U->getUser());
      if(!CI) continue;
      Value* T = CI->getArgOperand(Arg->getArgNo());
      GlobalVariable* G = NULL;
      if((G = dyn_cast<GlobalVariable>(T)))
         return G;

      if(Argument* A = dyn_cast<Argument>(T))
         G = parameter_access_global_variable(A);
      else if(Instruction* I = dyn_cast<Instruction>(T))
         G = access_global_variable(I);

      if(G) return G;
   }
   return NULL;
}