Hello,
Using LLVM+clang to do some static analysis, I face some issues due to the fact that when manipulating arrays that are global variables, IR getelementptr instructions seem to be somehow inlined.
For example:
int a[256];
int main() {
a[0] = 1;
}
gives as a result the following IR code (with clang+llvm 8, without any flag).
@a = common dso_local global [256 x i32] zeroinitializer, align 16
; Function Attrs: noinline nounwind optnone sspstrong uwtable
define dso_local i32 @main() #0 {
store i32 1, i32* getelementptr inbounds ([256 x i32], [256 x i32]* @a, i64 0, i64 0), align 16
ret i32 0
}
Hence two questions.
- What is the main reason behind this behavior?
- Is there a way to disable it (ideally while preserving optimisations disabled)?
Thank you in advance,
Antonin Reitz
Hence two questions.
- What is the main reason behind this behavior?
The getelementptr has all constant argument, meaning it itself becomes
an GetElementPtrConstantExpr. ConstantExprs (in contrast to
instructions) are inlined by the IR emitter.
- Is there a way to disable it (ideally while preserving optimisations
disabled)?
Not that I know of.
Michael
Is it something that would be relatively easy to change with a local patch for LLVM without introducing a lot of modifications? (only disabling GetElementPtrConstantExprs inlining)
This is really something that would help having simpler static analysis, even if additional analysis of the code can be used as a (quite ugly) workaround.
Thank you very much,
Antonin
One preprocess the in-memory IR before passing it to the IR printer:
Walk through all operands of all instructions in the module, if it
contains a GetElementPtrConstantExpr (it could be an argument of
another ConstantExpr), replace it with an GetElementPtrInst, which is
inserted before the instruction using it.
Michael
Are you aware of the GEPOperator[*] class? It's designed to provide a
uniform interface to both GetElementPtrInst and a
GetElementPtrConstantExpr instances. It probably wouldn't be suitable
for all uses, but if it covers yours it would make the point moot.
Cheers.
Tim.
[*] https://llvm.org/doxygen/classllvm_1_1GEPOperator.html