In this recipe, we will see how we can transform the IR from one form to another using the opt tool. We will see different optimizations being applied to IR code.
The opt
tool runs the transformation pass as in the following command:
$opt –passname input.ll –o output.ll
$ cat multiply.c int mult() { int a =5; int b = 3; int c = a * b; return c; }
$ clang -emit-llvm -S multiply.c -o multiply.ll $ cat multiply.ll ; ModuleID = 'multiply.c' target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" ; Function Attrs: nounwind uwtable define i32 @mult() #0 { %a = alloca i32, align 4 %b = alloca i32, align 4 %c = alloca i32, align 4 store i32 5, i32* %a, align 4 store i32 3, i32* %b, align 4 %1 = load i32* %a, align 4 %2 = load i32* %b, align 4 %3 = mul nsw i32 %1, %2 store i32 %3, i32* %c, align 4 %4 = load i32* %c, align 4 ret i32 %4 }
$ opt -mem2reg -S multiply.ll -o multiply1.ll $ cat multiply1.ll ; ModuleID = 'multiply.ll' target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" ; Function Attrs: nounwind uwtable define i32 @mult(i32 %a, i32 %b) #0 { %1 = mul nsw i32 %a, %b ret i32 %1 }
The opt
, LLVM optimizer, and analyzer tools take the input.ll
file as the input and run the pass passname
on it. The output after running the pass is obtained in the output.ll
file that contains the IR code after the transformation. There can be more than one pass passed to the opt tool.
When the –analyze
option is passed to opt, it performs various analyses of the input source and prints results usually on the standard output or standard error. Also, the output can be redirected to a file when it is meant to be fed to another program.
When the –analyze option is not passed to opt, it runs the transformation passes meant to optimize the input file.
Some of the important transformations are listed as follows, which can be passed as a flag to the opt tool:
adce
: Aggressive Dead Code Eliminationbb-vectorize
: Basic-Block Vectorizationconstprop
: Simple constant propagationdce
: Dead Code Eliminationdeadargelim
: Dead Argument Eliminationglobaldce
: Dead Global Eliminationglobalopt
: Global Variable Optimizergvn
: Global Value Numberinginline
: Function Integration/Inlininginstcombine
: Combine redundant instructionslicm
: Loop Invariant Code Motionloop
: unswitch: Unswitch loopsloweratomic
: Lower atomic intrinsics to non-atomic formlowerinvoke
: Lower invokes to calls, for unwindless code generatorslowerswitch
: Lower SwitchInsts to branchesmem2reg
: Promote Memory to Registermemcpyopt
: MemCpy Optimizationsimplifycfg
: Simplify the CFGsink
: Code sinkingtailcallelim
: Tail Call EliminationRun at least some of the preceding passes to get an understanding of how they work. To get to the appropriate source code on which these passes might be applicable, go to the llvm/test/Transforms
directory. For each of the above mentioned passes, you can see the test codes. Apply the relevant pass and see how the test code is getting modified.
18.118.0.145