Hi Nikita,
so I think I made some progress but I'm not quite there yet.
So the GEP Instruction that I'm interested to analyze are:
%3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0
%4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1
%5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2
and the Point struct is declared in the following way:
struct Point
{
int x;
char y;
long z;
};
Reading some doc online I tried the following:
Module *M = I.getModule();
I.dump();
APInt ap_offset(32, 0, false);
std::cout << "ap_offset: " << ap_offset.getSExtValue() << "\n";
std::cout << "Accumulated offset: " << I.accumulateConstantOffset(M->getDataLayout(), ap_offset) << "\n";
std::cout << "ap_offset: " << ap_offset.getSExtValue() << "\n";
The output is something like:
%3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0
ap_offset: 0
Accumulated offset: 1
ap_offset: 0
%4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1
ap_offset: 0
Accumulated offset: 1
ap_offset: 4
%5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2
ap_offset: 0
Accumulated offset: 1
ap_offset: 8
%3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0
ap_offset: 0
Accumulated offset: 1
ap_offset: 0
%4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1
ap_offset: 0
Accumulated offset: 1
ap_offset: 4
%5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2
ap_offset: 0
Accumulated offset: 1
ap_offset: 8
%3 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 0
ap_offset: 0
Accumulated offset: 1
ap_offset: 0
%4 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 1
ap_offset: 0
Accumulated offset: 1
ap_offset: 4
%5 = getelementptr inbounds %struct.Point, %struct.Point* %2, i32 0, i32 2
ap_offset: 0
Accumulated offset: 1
ap_offset: 8
I think the output is almost correct because the offset is increased by 4 each time... but the struct has char and long elements so the offset does not always look right. I think it due to the fact that the GEP is referring only to i32.
How should I fix this situation? Few lines of code would be very helpful
Thanks
Alberto