Sorry for the repost, I accidentally pressed submission before typing the question, so I immediately removed that post and made this new one.
Let me first provide an example and explain what I am trying to do:
So for a code that looks like this:
int main() {
char buffer[50] = {0};
char *c = "Hello World!\n";
strcpy(buffer,c);
}
It will provide the following IR code (I am omitting a lot of parts for the sake of brevity):
@.str.2 = private unnamed_addr constant [14 x i8] c"Hello World!\0A\00", align 1
define dso_local i32 @main() #0 {
1: %2 = alloca [500 x i8], align 1
2: %3 = alloca i8*, align 8
3: store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %3, align 8
4: %4 = bitcast [500 x i8]* %2 to i8*
5: call void @llvm.memset.p0i8.i64(i8* align 1 %4, i8 0, i64 500, i1 false)
6: %6 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
7: %7 = load i8*, i8** %3, align 8
8: %8 = call i8* @strcpy(i8* %6, i8* %7) #4
}
So my goal at the moment is trying to obtain the array size(?) of GEP instruction at line 3 (basically the first operand of [14 x i8]
, more specifically I’m interested in the number 14
:
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i8** %3, align 8
;
So interestingly enough as getelementptr
instruction is part of an operand of store
instruction, if I were to do the following, it will never satisfy the if
condition:
void InstVisitor::visitStoreInst(Instruction &I){
if (dyn_cast<GetElementPtrInst>(I.getOperand(0))) {
// should be here if operand is a GEP instruction
} else {
errs() << *I.getOperand(0) << "\n";
errs() << *I.getOperand(0)->stripPointerCasts() << "\n";
}
}
Therefore, I played around a bit with this instruction and figured out how to make it output like the following (I have posted this code in the above else
condition):
i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0)
@.str.2 = private unnamed_addr constant [14 x i8] c"Hello World!\0A\00", align 1
However, from here, I am unable to get the value I need, which is either [14 x i8]
or int value of 14
.
I am a bit lost at this point… I looked through how GEP instruction works, but it seems like this is not a GEP instruction at the moment as it is a direct operand to store
instruction, and tried looking at the API
documentation, but still to no avail.
Thank you for any suggestions in advance,