[llvm-dev] What is ConstantExpr?

John Criswell via llvm-dev llvm-dev at lists.llvm.org
Thu Mar 9 06:39:43 PST 2017


On 3/9/17 8:28 AM, Zeson Wu via llvm-dev wrote:
> Hi, All.
>
> Does anybody know about ConstantExpr in llvm? What's it?
> Since it always appears after llvm optimization such as -O2 level, 
> what is it supposed to be to codegen? I am wondering it represents 
> constant value which can be determined or computed at 
> compile-time(actually is link-time) to improve performance. Although 
> we do not know the actual constant value util the object file is linked.

You're pretty much got it.  A Constant Expression (ConstantExpr) is 
simply a constant value.  Since some constant values depend upon 
architecture-dependent features (e.g., structure layout, pointer size, 
etc.), LLVM provides the ConstantExpr to represent them in a (more or 
less) architecture-independent way.

For example, a GEP with constant indices on an internal global variable 
will always compute the same value; it is a constant. However, we use a 
GEP ConstantExpr to represent it; the backend code generator converts it 
to the appropriate numerical constant when generating native code.

For more information on the ConstantExpr, please see the LLVM Language 
Reference Manual (http://llvm.org/docs/LangRef.html#constant-expressions).

Regards,

John Criswell

>
> Here is a my example, but there is still existing code to compute 
> value in run-time.
>
> cat a.C
> int n=5;
> int main(){
>   long a = (long)&n+7;
>   int b = a;
>   return b;
> }
>
> clang++ a.C  -c -O2 -emit-llvm -S;cat a.ll
> ; ModuleID = 'a.C'
> target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
> target triple = "x86_64-apple-macosx10.12.0"
>
> @n = global i32 5, align 4
>
> ; Function Attrs: norecurse nounwind readnone ssp uwtable
> define i32 @main() #0 {
>   ret i32 trunc (i64 add (i64 ptrtoint (i32* @n to i64), i64 7) to i32)
> }
>
> clang++ a.C  -c -O2;objdump -d a.O
>
> a.O:file format Mach-O 64-bit x86-64
>
> Disassembly of section __TEXT,__text:
> _main:
>        0:55 pushq%rbp
>        1:48 89 e5 movq%rsp, %rbp
>        4:48 8d 05 00 00 00 00 leaq(%rip), %rax
>        b:83 c0 07 *addl$7, %eax*
>        e:5d popq%rbp
>        f:c3 retq
>
> I am confused about what is its functionality in llvm?
>
> Thanks.
> ---------
>
> Zeson
>
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev


-- 
John Criswell
Assistant Professor
Department of Computer Science, University of Rochester
http://www.cs.rochester.edu/u/criswell

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170309/d6f0bb74/attachment.html>


More information about the llvm-dev mailing list