DOC: Add first version of updated documentation.

This commit is contained in:
Travis E. Oliphant 2012-08-09 13:53:21 -05:00
commit 415c01f745
19 changed files with 474 additions and 595 deletions

View file

@ -1,12 +1,16 @@
+------------------------+
| layout: page |
+------------------------+
| title: LLVM Concepts |
+------------------------+
********************
LLVM Concepts
********************
This section explains a few concepts related to LLVM, not specific to
llvmpy.
.. toctree::
:hidden:
Intermediate Representation
===========================
@ -36,7 +40,6 @@ instructions related to variable argument handling, exception handling,
and garbage collection. These allow high-level languages to be
represented cleanly in the IR.
--------------
SSA Form and PHI Nodes
======================
@ -51,15 +54,24 @@ when a variable can be assigned a different value based on the path of
control flow. For example, the value of *b* at the end of execution of
the snippet below:
{% highlight c %} a = 1; if (v < 10) a = 2; b = a; {% endhighlight %}
.. code-block:: c
a = 1;
if (v < 10)
a = 2;
b = a;
cannot be determined statically. The value of '2' cannot be assigned to
the 'original' *a*, since *a* can be assigned to only once. There are
two *a* 's in there, and the last assignment has to choose between which
version to pick. This is accomplished by adding a PHI node:
{% highlight c %} a1 = 1; if (v < 10) a2 = 2; b = PHI(a1, a2); {%
endhighlight %}
.. code-block:: c
a1 = 1;
if (v < 10)
a2 = 2;
b = PHI(a1, a2);
The PHI node selects *a1* or *a2*, depending on where the control
reached the PHI node. The argument *a1* of the PHI node is associated
@ -68,7 +80,6 @@ with the block *"a1 = 1;"* and *a2* with the block *"a2 = 2;"*.
PHI nodes have to be explicitly created in the LLVM IR. Accordingly the
LLVM instruction set has an instruction called *phi*.
--------------
LLVM Assembly Language
======================
@ -90,30 +101,48 @@ language code.
Just to get a feel of the LLVM assembly language, here's a function in
C, and the corresponding LLVM assembly (as generated by the demo page):
{% highlight c %} /\* compute sum of 1..n \*/ unsigned sum(unsigned n) {
if (n == 0) return 0; else return n + sum(n-1); } {% endhighlight %}
.. code-block:: c
/* compute sum of 1..n */
unsigned sum(unsigned n) {
if (n == 0)
return 0;
else
return n + sum(n-1);
}
The corresponding LLVM assembly:
{% highlight llvm %} ; ModuleID = '/tmp/webcompile/\_7149\_0.bc' target
datalayout =
"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86\_64-linux-gnu"
.. code-block:: llvm
define i32 @sum(i32 %n) nounwind readnone { entry: %0 = icmp eq i32 %n,
0 ; [#uses=1] br i1 %0, label %bb2, label %bb1
; ModuleID = '/tmp/webcompile/_7149_0.bc'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-linux-gnu"
bb1: ; preds = %entry %1 = add i32 %n, -1 ; [#uses=2] %2 = icmp eq i32
%1, 0 ; [#uses=1] br i1 %2, label %sum.exit, label %bb1.i
define i32 @sum(i32 %n) nounwind readnone {
entry:
%0 = icmp eq i32 %n, 0 ; [#uses=1]
br i1 %0, label %bb2, label %bb1
bb1.i: ; preds = %bb1 %3 = add i32 %n, -2 ; [#uses=1] %4 = tail call i32
@sum(i32 %3) nounwind ; [#uses=1] %5 = add i32 %4, %1 ; [#uses=1] br
label %sum.exit
bb1: ; preds = %entry
%1 = add i32 %n, -1 ; [#uses=2]
%2 = icmp eq i32 %1, 0 ; [#uses=1]
br i1 %2, label %sum.exit, label %bb1.i
sum.exit: ; preds = %bb1.i, %bb1 %6 = phi i32 [ %5, %bb1.i ], [ 0, %bb1
] ; [#uses=1] %7 = add i32 %6, %n ; [#uses=1] ret i32 %7
bb1.i: ; preds = %bb1
%3 = add i32 %n, -2 ; [#uses=1]
%4 = tail call i32 @sum(i32 %3) nounwind ; [#uses=1]
%5 = add i32 %4, %1 ; [#uses=1]
br label %sum.exit
bb2: ; preds = %entry ret i32 0 } {% endhighlight %}
sum.exit: ; preds = %bb1.i, %bb1
%6 = phi i32 [ %5, %bb1.i ], [ 0, %bb1 ] ; [#uses=1]
%7 = add i32 %6, %n ; [#uses=1]
ret i32 %7
bb2: ; preds = %entry
ret i32 0
}
Note the usage of SSA form. The long string called ``target datalayout``
is a specification of the platform ABI (like endianness, sizes of types,
@ -122,7 +151,6 @@ alignment etc.).
The `LLVM Language Reference <http://www.llvm.org/docs/LangRef.html>`_
defines the LLVM assembly language including the entire instruction set.
--------------
Modules
=======
@ -139,7 +167,6 @@ contained within modules. Modules may be combined (linked) together to
give a bigger resultant module. During this process LLVM attempts to
reconcile the references between the combined modules.
--------------
Optimization and Passes
=======================
@ -184,7 +211,6 @@ LLVM defines two kinds of pass managers:
`PassManager <http://llvm.org/docs/doxygen/html/classllvm_1_1PassManager.html>`_
manages module passes for optimizing the entire module.
--------------
Bitcode
=======
@ -195,7 +221,6 @@ compiler <http://llvm.org/docs/LangRef.html#introduction>`_. See `LLVM
documentation <http://llvm.org/docs/BitCodeFormat.html>`_ for detail
about the bitcode format.
--------------
Execution Engine, JIT and Interpreter
=====================================
@ -209,6 +234,3 @@ multiple modules.
Inter-module reference is not possible. That is module ``A`` cannot
call a function in module ``B``, directly.
--------------
**Next** -- `llvmpy Package <./llvm-py_package.html>`_