Reputation: 165
I have following LLVM IR and I want to extract the variable name string
from the store instruction. Please guide me on how can I do this.
%call = call noalias i8* @malloc(i64 100) #3
store i8* %call, i8** %string, align 8
My LLVM pass looks like
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;
}
Upvotes: 4
Views: 3097
Reputation: 34411
After checking that I
is a StoreInst
you can iterate between I.op_begin()
and I.op_end()
:
for (auto op = I.op_begin(); op != I.op_end(); op++) {
Value* v = op.get();
StringRef name = v->getName();
}
Upvotes: 3