
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
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