c++cllvmcompiler-optimization

Is O3 a fixed optimization sequence? And how to change frame-pointer value in LLVM IR?


I use the following command to figure out the sequence of clang O3,

$ opt -enable-new-pm=0 -O3 -debug-pass=Arguments input.ll

and I get a very long optimization sequence.

Is that sequence same for all of code? Or O3 can change the order according to the source code?

And, I found that if I use -O0 flag to generate IR file, the attributes may be like this,

attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-pr    otector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }
attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-    features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" }

we can see the value of frame-pointer = all, and I think that may let the code slow. ( It will be turned to none by using O3 flag)

How can I change this value by opt command?


Solution

  • Yes, optimization sequence is the same for all inputs. But note that opt's -O3 may not be the same as clang's -O3.

    As for disabling the frame pointer, you can remove it

    $ clang input.c -emit-llvm -S -O0 -o fp.ll
    $ grep frame-pointer fp.ll
    attributes #0 = { ... "frame-pointer"="all" ... }
    $ clang input.c -emit-llvm -S -O0 -fomit-frame-pointer -o nofp.ll
    $ grep frame-pointer nofp.ll
    attributes #0 = { ... "frame-pointer"="none" ... }
    
    $ opt -frame-pointer=none fp.ll -S -o fp_removed.ll
    $ grep frame-pointer fp_removed.ll
    attributes #0 = { ... "frame-pointer"="none" ... }