Book Image

LLVM Cookbook

Book Image

LLVM Cookbook

Overview of this book

Table of Contents (16 chapters)
LLVM Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Transforming LLVM IR


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.

Getting ready

You need to have the opt tool installed.

How to do it...

The opt tool runs the transformation pass as in the following command:

$opt –passname input.ll –o output.ll
  1. Let's take an actual example now. We create the LLVM IR equivalent to the C code used in the recipe Converting a C source code to LLVM assembly:

    $ cat multiply.c
    int mult() {
    int a =5;
    int b = 3;
    int c = a * b;
    return c;
    }
    
  2. Converting and outputting it, we get the unoptimized output:

    $ 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
    }
    
  3. Now use the opt tool to transform it to a form where memory is promoted to register:

    $ 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
    }
    

How it works...

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.

There's more...

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 Elimination

  • bb-vectorize: Basic-Block Vectorization

  • constprop: Simple constant propagation

  • dce: Dead Code Elimination

  • deadargelim: Dead Argument Elimination

  • globaldce: Dead Global Elimination

  • globalopt: Global Variable Optimizer

  • gvn: Global Value Numbering

  • inline: Function Integration/Inlining

  • instcombine: Combine redundant instructions

  • licm: Loop Invariant Code Motion

  • loop: unswitch: Unswitch loops

  • loweratomic: Lower atomic intrinsics to non-atomic form

  • lowerinvoke: Lower invokes to calls, for unwindless code generators

  • lowerswitch: Lower SwitchInsts to branches

  • mem2reg: Promote Memory to Register

  • memcpyopt: MemCpy Optimization

  • simplifycfg: Simplify the CFG

  • sink: Code sinking

  • tailcallelim: Tail Call Elimination

Run 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.

Note

To see the mapping of how C code is converted to IR, after converting the C code to IR, as discussed in an earlier recipe Converting a C source code to LLVM assembly, run the mem2reg pass. It will then help you understand how a C instruction is getting mapped into IR instructions.