在LLVM传递中获取变量名称

时间:2017-12-26 12:00:39

标签: c llvm

我有以下LLVM IR,我想从store指令中提取变量名string。请指导我如何做到这一点。

 %call = call noalias i8* @malloc(i64 100) #3
 store i8* %call, i8** %string, align 8

我的LLVM传递看起来像

virtual bool runOnModule(Module &M) {
  for (Function &F: M) { 
    for (BasicBlock &B: F) {
        for (Instruction &I: B) {
            if(CallInst* call_inst = dyn_cast<CallInst>(&I)) {
                Function* fn = call_inst->getCalledFunction();
                StringRef fn_name = fn->getName();
                errs() << fn_name << " : " << call_inst->getArgOperand(0) << "\n";
                //for(auto args = fn->arg_begin(); args != fn->arg_end(); ++args) {
                    //  ConstantInt* arg = dyn_cast<ConstantInt>(&(*args));
                    //  errs() << arg->getValue() << "\n";
                //}    
            } else {
                       errs() << I.getName() << "\n" << I.getOpcodeName();
            }
    }
     }  
  }
  return false;
}

1 个答案:

答案 0 :(得分:3)

检查IStoreInst后,您可以在I.op_begin()I.op_end()之间进行迭代:

for (auto op = I.op_begin(); op != I.op_end(); op++) {
  Value* v = op.get();
  StringRef name = v->getName();
}