Constant folding in clang

Hello,
let’s say I have this function:

int main(int argc, char const *argv[])
{
  return 10+1;
}

When I compile it with -emit-llvm -S I expect that clang doesn’t fold my constant, but if I look at the .s file i see

ret i32 11

How do I prevent clang to perform folding, or, in general, any kind of optimization?
Thanks

-O0?

already tried, doesn’t work

Hello,
let’s say I have this function:

int main(int argc, char const *argv)
{
return 10+1;
}

When I compile it with -emit-llvm -S I expect that clang doesn’t fold my constant, but if I look at the .s file i see

ret i32 11

How do I prevent clang to perform folding, or, in general, any kind of optimization?

Why do you want this? It’s possible you would be better served by examining the AST, not the IR.

That’s a good question indeed. I have to do some examinations over the IR, so looking at the AST won’t be good for me.
For example, I am trying to trigger the ConstProp optimization, but I don’t find a case where this is used.

Perhaps you could generate testcases by putting your values into local variables, and running mem2reg before the optimization pass you want to investigate?

And also, I always thought that clang codegeneration was as transparent as possible, trying not to interfere with the logic flow of the source code, and leaving the optimizations as business for LLVM. I fear that, besides this example, clang would do other subtle optimizations. What I would like, if it exists, is to use an option to disable them.

There is no option to disable constant folding when generating IR. There are some cases where such constant folding is essential in order to produce correct IR – for instance, for globals in C or constexpr variables in C++, we cannot generate IR which computes the value, we must actually emit the value itself. In other cases, we could emit the expression as a non-constant, but the produced IR is generally not intended to preserve any language-level semantics, so we don’t wastefully generate IR we know can be constant-folded. Additionally, the mechanism we use to generate IR does some constant folding itself.

The exact mechanism that Richard is referring to here is IRBuilder,
which does constant folding by default. You can try to plug in a noop
constant folder (IRBuilder's second template arg) and see if that
avoids the constant folding that you want to avoid (see the typedef of
CGBuilderTy in lib/CodeGen/CGBuilder.h).

-- Sean Silva