Jaeger
Jaeger

Reputation: 159

How to turn off the constant folding optimization in llvm

I am new to clang and llvm. I'm trying to generate an unoptimized version of bit code from a c source code. I found that the generated bit code is having the constant folding optimization which I don't want. I'm using this command: clang -O0 -Xclang -disable-O0-optnone test1.c -S -emit-llvm -o test1.ll

The test1.c file has the following code:

int test() {
int y;
y = 2 * 4;
return y;
}

The content of the test1.ll file: enter image description here

Instead of generating an instruction for multiplying 2 and 4, it is directly storing the value 8 by doing the constant folding operation: store i32 8, i32* %1, align 4

It would be really nice if someone kindly let me know what I am missing and how should I turn off the constant folding optimization. The version of llvm I am using is 6.0.0.

Thank you.

Upvotes: 1

Views: 1448

Answers (1)

arrowd
arrowd

Reputation: 34411

It would be really nice if someone kindly let me know what I am missing and how should I turn off the constant folding optimization. The version of llvm I am using is 6.0.0.

It is a Clang feature and can't be turned off even with -O0. To workaround this try making variables global, pass them as parameters to the function, or just write the IR manually.

Upvotes: 1

Related Questions