Rouzbeh
Rouzbeh

Reputation: 2431

LLVM IR optimization

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?

Upvotes: 2

Views: 2772

Answers (2)

DTharun
DTharun

Reputation: 786

This option can be used with clang -Xclang -disable-O0-optnone to prevent generation of optnone attribute.

Upvotes: 9

arrowd
arrowd

Reputation: 34391

When you run clang somefile.c, it defaults to -O0 optimization level, which emits main function with optnone attribute. This attribute prevents optimizations, which is why you don't see the result of mem2reg.

You have to remove optnone attribute if you want opt to do work:

clang -S -emit-llvm const.c -o - | sed s/optnone// | opt -S -mem2reg

Note thet mem2reg and its counterpart reg2mem passes are not strictly optimizing. They are just converting the IR from/to SSA form.

Upvotes: 4

Related Questions