[llvm-dev] Need help: How to turn off the constant folding optimization in llvm

526 views
Skip to first unread message

Md. Syadus Sefat via llvm-dev

unread,
Jan 22, 2019, 2:43:54 PM1/22/19
to llvm...@lists.llvm.org
Hi, 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:
image.png
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.

Thank you. 
Regards,
SS






 


via llvm-dev

unread,
Jan 22, 2019, 4:15:31 PM1/22/19
to syadus...@gmail.com, llvm...@lists.llvm.org

Usually when someone talks about "constant folding optimization" they mean an LLVM pass that operates on the IR to combine constants in operations that had been expressed as instructions in the IR.

I think what you are looking at is a constant-expression evaluation that is done before any IR is generated; in effect, it is being performed by the parser, not the optimizer.  I'm not aware of a command-line option to turn this off, although there might be one.

If you just want to look at simple examples of IR, you could do this instead:

    int y = 2;

    y *= 4;

and this will produce a 'mul' instruction.

--paulr

Cranmer, Joshua via llvm-dev

unread,
Jan 22, 2019, 4:30:42 PM1/22/19
to Md. Syadus Sefat, llvm...@lists.llvm.org

The way that the front-end lowers code to IR causes this sort of constant folding to happen even before any LLVM IR is generated. Essentially, when you do the AST traversal, you’re going to essentially see the following code get run:

 

IRBuilder<> Builder;

Value *LHS = Builder.getInt32(2);

Value *RHS = Builder.getInt32(4);

// LHS and RHS are ConstantInt values because they’re constant expressions.

Value *Res = Builder.CreateMul(LHS, RHS); // Because LHS and RHS are constant values, the IRBuilder folds this to a constant expression.

 

This constant folding cannot be turned off. (I’m also assuming there’s no other constant folding going on at the Clang AST level).

 

From: llvm-dev [mailto:llvm-dev...@lists.llvm.org] On Behalf Of Md. Syadus Sefat via llvm-dev


Sent: Sunday, January 20, 2019 19:30
To: llvm...@lists.llvm.org

Reply all
Reply to author
Forward
0 new messages