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?
->dump()outside of your if? What other operands do you see then? Probably you need another if(s) to act accordingly on their contents, no?getNumOperands()!=0), similar to what Brian done, but, perhaps, by casting them to instructions. Still trying to implement it. Am I correct to assume only instructions have operands?