Visualizing LLVM IR CFG using GraphViz

The LLVM IR control flow graph can be visualized using the GraphViz tool. It gives a visual depiction of the nodes formed and how the code flow follows in the IR generated. Since the important data structures in LLVM are graphs, this can be a very useful way to understand the IR flow when writing a custom pass or studying the behavior of the IR pattern.

Getting ready

  1. To install graphviz on Ubuntu, first add its ppa repository:
    $ sudo apt-add-repository ppa:dperry/ppa-graphviz-test
    
  2. Update the package repository:
    $ sudo apt-get update
    
  3. Install graphviz:
    $ sudo apt-get install graphviz
    

    Note

    If you get the graphviz : Depends: libgraphviz4 (>= 2.18) but it is not going to be installed error, run the following commands:

    $ sudo apt-get remove libcdt4
    $ sudo apt-get remove libpathplan4
    

    Then install graphviz again with the following command:

    $ sudo apt-get install graphviz
    

How to do it…

  1. Once the IR has been converted to DAG, it can be viewed in different phases. Create a test.ll file with the following code:
    $ cat test.ll
    define i32 @test(i32 %a, i32 %b, i32 %c) {
      %add = add nsw i32 %a, %b
      %div = sdiv i32 %add, %c
      ret i32 %div
    }
    
  2. To display the DAG after it is built, before the first optimization pass, enter the following command:
    $ llc -view-dag-combine1-dags test.ll
    

    The following diagram shows the DAG before the first optimization pass:

    How to do it…
  3. To display the DAG before legalization, run this command:
    $ llc -view-legalize-dags test.ll
    

    Here is a diagram that shows the DAG before the legalization phase:

    How to do it…
  4. To display the DAG before the second optimization pass, run the following command:
    $ llc -view-dag-combine2-dags test.ll
    

    The following diagram shows the DAG before the second optimization pass:

    How to do it…
  5. To display the DAG before the selection phase, enter this command:
    $ llc -view-isel-dags test.ll
    

    Here is a diagram that shows the DAG before the selection phase:

    How to do it…
  6. To display the DAG before scheduling, run the following command:
    $ llc -view-sched-dags test.ll
    

    The following diagram shows the DAG before the scheduling phase:

    How to do it…
  7. To display the scheduler's dependency graph, run this command:
    $ llc -view-sunit-dags test.ll
    

    This diagram shows the scheduler's dependency graph:

    How to do it…

Notice the difference in the DAG before and after the legalize phase. The sdiv node has been converted into an sdivrem node. The x86 target doesn't support the sdiv node but supports the sdivrem instruction. In a way, the sdiv instruction is illegal for the x86 target. The legalize phase converted it into an sdivrem instruction, which is supported by the x86 target.

Also note the difference in the DAG before and after the instruction selection (ISel) phase. Target-machine-independent instructions such as Load are converted into the MOV32rm machine code (which means, move 32-bit data from the memory to the register). The ISel phase is an important phase that will be described in later recipes.

Observe the scheduling units for the DAG. Each unit is linked to other units, which shows the dependency between them. This dependency information is very important for deciding scheduling algorithms. In the preceding case, scheduling unit 0 (SU0) is dependent on scheduling unit 1 (SU1). So, the instructions in SU0 cannot be scheduled before the instructions in SU1. SU1 is dependent on SU2, and so is SU2 on SU3.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.192.120