[LLVMdev] Does getelementptr get optimized in compile-time?

Duncan Sands baldrick at free.fr
Thu Jan 3 00:50:36 PST 2013

Hi Journeyer,

 > I've got a question about getelementptr.
> I've been told that getelementptr is evaluated in compile-time NOT in run-time.

this is impossible if the arguments are not constants.  If they are constant,
then the optimizers will turn a getelementptr instruction into a getelementptr
constant expression (http://llvm.org/docs/LangRef.html#constant-expressions).
Even so it usually isn't evaluable at compile time, since typically the pointer
operand is a global variable for which the address is only known when the
program loader loads the final executable into memory.  If however the operands
are all numerical values (eg the pointer operand is "null" and the others are
numbers like "1") then it will be turned into a number by the optimizers.

Ciao, Duncan.

> So I tested it with clang web compiler below.
> http://llvm.org/demo/index.cgi
> But with LTO nor Standard, neither one optimize getelementptr out.
> Does getelementptr get evaluated in compile-time?
> Or doesn't it get executed in run-time? (I believed so. Now I am wondering.)
> Thank you in advance.

More information about the llvm-dev mailing list