[llvm-dev] What is ConstantExpr?

Matthias Braun via llvm-dev
Fri, 17 Mar 2017 23:04:55 -0000

> 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