Alberto Barbaro via llvm-dev
2022-Jan-25 07:00 UTC
[llvm-dev] How to calculate the offset obtained via a GEP instruction
Hi all, so I'm trying to understand how to manually calculate the offset calculated by a GEP instruction. I found that this question was asked over 6 years ago on stackoverflow[1] as well but never got a real answer. Since I need exactly the same, is there anyone willing to help me to understand how to calculate the offset? Thanks [1] https://stackoverflow.com/questions/32444497/determine-byte-offset-of-getelementptr -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20220125/e039d0ba/attachment.html>
Markus Böck via llvm-dev
2022-Jan-25 07:04 UTC
[llvm-dev] How to calculate the offset obtained via a GEP instruction
The easiest way to do so in my experience is to change the base operand to a null pointer of the type and then convert the result of the GEP to an integer using ptrtoint. That will give you the offset in bytes and a pass using the target info will constant fold it as well. On Tue, Jan 25, 2022, 08:00 Alberto Barbaro via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi all, > so I'm trying to understand how to manually calculate the offset > calculated by a GEP instruction. I found that this question was asked over > 6 years ago on stackoverflow[1] as well but never got a real answer. > > Since I need exactly the same, is there anyone willing to help me to > understand how to calculate the offset? > > Thanks > > > [1] > https://stackoverflow.com/questions/32444497/determine-byte-offset-of-getelementptr > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20220125/98bd470e/attachment.html>
Nikita Popov via llvm-dev
2022-Jan-25 08:25 UTC
[llvm-dev] How to calculate the offset obtained via a GEP instruction
On Tue, Jan 25, 2022 at 8:00 AM Alberto Barbaro via llvm-dev < llvm-dev at lists.llvm.org> wrote:> Hi all, > so I'm trying to understand how to manually calculate the offset > calculated by a GEP instruction. I found that this question was asked over > 6 years ago on stackoverflow[1] as well but never got a real answer. > > Since I need exactly the same, is there anyone willing to help me to > understand how to calculate the offset? >You can use GEPOperator::accumulateConstantOffset(). Or more generically, there is Value::stripAndAccumulateConstantOffsets(), which can look through multiple GEPs, bitcasts, etc. Regards, Nikita -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20220125/2163e44a/attachment-0001.html>