In the example bellow, I am trying to extract all the global variables referenced by each instruction, but I keep missing some of them.
... // inside a BB
for (Instruction * I : BB) {
for (Use &U : I->operands()) {
if(GlobalVariable * GV = dyn_cast<GlobalVariable >(U)){
// Do something with GV
GV->dump();
}
}
}
But when I am targeting getting the global values of:
@end = global i8 0, align 1
@.str = private unnamed_addr constant [4 x i8] c"YES\00", align 1
@.str.2 = private unnamed_addr constant [3 x i8] c"NO\00", align 1
define void @TempF() {
entry:
%tmp8 = load i8, i8* @end, align 1
%tmp9 = trunc i8 %tmp8 to i1
%tmp10 = select i1 %tmp9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i32 0, i32 0)
ret void
}
When running my pass on this IR, only @end = global i8 0, align 1
gets printed. But not ...
@.str = private unnamed_addr constant [4 x i8] c"YES\00", align 1
@.str.2 = private unnamed_addr constant [3 x i8] c"NO\00", align 1
I understand that @.str
is not part of the operands as @end
is. @.str
is something reference by one of the operands.
What modifications do I have to implement to be able to get the global variables inside the operands?
In your case, the select instruction has GetElementPtr operators. You will need to extend your code to both detect this case, and then iterate through the arguments (the pointer and ) to the operator. I have suggested an extension to your for loop below, currently printing out these other cases.
for (Use &U : (&*I)->operands()) {
if(GlobalVariable * GV = dyn_cast<GlobalVariable>(U)){
// Do something with GV
GV->dump();
}
else if (GEPOperator* gepo = dyn_cast<GEPOperator>(&U))
{
errs() << "GEPO - " << *gepo << "\n";
if (GlobalVariable* gv = dyn_cast<GlobalVariable>(gepo->getPointerOperand()))
{
errs() << "GV - " << *gv << "\n";
}
for (auto it = gepo->idx_begin(), et = gepo->idx_end(); it != et; ++it)
{
if (GlobalVariable* gv = dyn_cast<GlobalVariable>(*it))
{
errs() << "GVi - " << *gv << "\n";
}
}
}
}