[llvm-dev] What is ConstantExpr?

Zeson Wu via llvm-dev llvm-dev at lists.llvm.org
Sun Mar 19 08:53:33 PDT 2017


Yes, but the example you give is a global variable. How about local
variable which is also folded into constant expression as blow.

@a = global i32 0, align 4
define signext i32 @main()  {
  ret i32 trunc (i64 add (i64 ptrtoint (i32* @a to i64), i64 5) to i32)
}

I found the computation still exists in assembly code which as blow. Could
it be that a constant replaces the result of trunc operation theoretically?
Does it need a assembler(or linker?) to do the process to calculate the
result on the variable `a` such as  `.quad   trunc_macro(a+5)` as a temp
symbol value used by moving it to eax register to return?

I am wondering what is the difference between constant expression and
separate instruction?
For example,

define signext i32 @main()  {
  %1 = ptrtoint i32* @a to i64
  %2 = add i64 %1, 5
  %3 = trunc i64 %2 to i32
  ret i32 %3
}

The code above would be only folded in optimized mode such as -O1 or -O2.
define signext i32 @main() #0 {
  ret i32 trunc (i64 add (i64 ptrtoint (i32* @a to i64), i64 5) to i32)
}



2017-03-18 7:04 GMT+08:00 Matthias Braun <mbraun at apple.com>:

>
> > On Mar 9, 2017, at 5:28 AM, Zeson Wu via llvm-dev <
> llvm-dev at lists.llvm.org> wrote:
> >
> > Hi, All.
> >
> > Does anybody know about ConstantExpr in llvm? What's it?
> The short version is: Some values can stand on their own in llvm
> independently of a basic block or a function. These include things like
> numbers, addresses of global variables or functions, etc. You can even do
> computations on them giving you ConstantExpr. Take for example:
>
> int array[10];
> int *x = &array + 5;
>
> giving this llvm IR:
>
> @array = common global [10 x i32] zeroinitializer, align 16
> @x = global i32* bitcast (i8* getelementptr (i8, i8* bitcast ([10 x i32]*
> @array to i8*), i64 20) to i32*), align 8
>
> and this assembly:
>
>         .globl  _x                      ## @x
>         .p2align        3
> _x:
>         .quad   _array+20
>
> They will be lowered at various places (some in the backend, some by the
> linker, some by the dynamic loader) but will be a constant value when the
> program is loaded.
>
> - Matthias
>



-- 
Zeson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170319/98b0bdcb/attachment-0001.html>


More information about the llvm-dev mailing list