I am trying to follow this link in order to generate an IR representation for a c code. The c code that I am using is as follows
void main() {
int c1 = 17;
int c2 = 25;
int c3 = c1 + c2;
printf("Value = %d\n", c3);
}
Which I save it as const.c. Once it is saved, I use the following command in order to generate a .bc file.
clang -c -emit-llvm const.c -o const.bc
Once the .bc file is generated, I want to use the following command in order to generate the optimized version of the const.bc file which is named const.reg.bc.
opt -mem2reg const.bc > const.reg.bc
I don't have any issues generating these files but for some reason both of them are exactly the same and no optimization happens. The results should be different, I mean const.reg.bc should be an optimized version of the const.bc file. But for some reason it does not happen. Can someone tell me what is it that I am not doing right?
When you run
clang somefile.c, it defaults to -O0 optimization level, which emits main function withoptnoneattribute. This attribute prevents optimizations, which is why you don't see the result ofmem2reg.You have to remove
optnoneattribute if you wantoptto do work:Note thet
mem2regand its counterpartreg2mempasses are not strictly optimizing. They are just converting the IR from/to SSA form.