Hello everyone,
I’m encountering an issue while working on an LLVM pass that processes StoreInst instructions. Specifically, I’m having trouble with accessing the first element of a structure after LLVM’s optimization phase. It seems that when the first element of a structure is being stored, LLVM optimizes away the GetElementPtr (GEP) instruction and directly accesses the base address, making it difficult to capture the store instruction for the first element of the struct in my pass and deduce the structure name and field
Additionally, I noticed that in newer versions of LLVM, both getPointerElementType() and getNonOpaquePointerElementType() have been deprecated. I am aware that LLVM now uses opaque pointers, and I need to adapt my pass to this change, but I’m struggling to find the best approach.
Issue Details:
In debug mode, when I store to the first element of a structure, the LLVM IR uses %global, and I can handle it as expected with a GEP instruction. However, in release mode, the IR uses %0, and there’s no GEP, making it impossible to detect that I’m storing to the first element of a struct.
Here’s an example of the difference:
• Debug Mode:
Store i32%val , ptr %global_structtypedef
• Release Mode:
store i32 %val, ptr %0, align 4
As you can see, the GEP instruction for the first element is removed, and LLVM directly uses %0 as the pointer to the base address. This prevents my pass from detecting that this is the first element of the structure.
Steps I Have Taken:
1. I’ve tried using the now-deprecated getPointerElementType() and getNonOpaquePointerElementType(), but I understand that I need to adopt a new approach since opaque pointers do not carry type information.
2. I’ve attempted to directly analyze the pointer (getOperand(1) in the store instruction), but without the GEP for the first element, I’m unsure how to correctly associate it with the structure’s first field.
My Questions:
1. What is the recommended approach in newer LLVM versions to detect the type of the memory being written to?
2. How can I manually identify that a store is targeting the first element of a structure when LLVM optimizes away the GEP?
3. Is there a way to adjust the LLVM optimization to retain more debug information about the first element of a struct, even in release mode?
Any suggestions or insights on how to address this issue would be greatly appreciated!
Thank you!