blob: 3b7b8dea1accb6658adad14e756d6d32708b8562 [file] [log] [blame]
Owen Anderson75bbde82007-10-20 05:23:06 +00001<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
2 "http://www.w3.org/TR/html4/strict.dtd">
3
4<html>
5<head>
6 <title>LLVM Tutorial 1: A First Function</title>
7 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
8 <meta name="author" content="Owen Anderson">
9 <meta name="description"
10 content="LLVM Tutorial 1: A First Function.">
11 <link rel="stylesheet" href="../llvm.css" type="text/css">
12</head>
13
14<body>
15
16<div class="doc_title"> LLVM Tutorial 1: A First Function </div>
17
18<div class="doc_author">
19 <p>Written by <a href="mailto:owen@apple.com">Owen Anderson</a></p>
20</div>
21
Owen Anderson334b8732007-10-22 06:35:07 +000022<!-- *********************************************************************** -->
Owen Anderson334b8732007-10-22 06:35:07 +000023<div class="doc_section"><a name="intro">A First Function</a></div>
24<!-- *********************************************************************** -->
25
Owen Anderson75bbde82007-10-20 05:23:06 +000026<div class="doc_text">
27
Chris Lattnerdd684e22008-02-10 19:11:04 +000028<p>For starters, let's consider a relatively straightforward function that takes three integer parameters and returns an arithmetic combination of them. This is nice and simple, especially since it involves no control flow:</p>
Owen Anderson75bbde82007-10-20 05:23:06 +000029
30<div class="doc_code">
31<pre>
Owen Anderson22340ef2007-10-20 05:41:39 +000032int mul_add(int x, int y, int z) {
33 return x * y + z;
34}
Owen Anderson75bbde82007-10-20 05:23:06 +000035</pre>
36</div>
37
38<p>As a preview, the LLVM IR we’re going to end up generating for this function will look like:</p>
39
40<div class="doc_code">
41<pre>
Owen Anderson22340ef2007-10-20 05:41:39 +000042define i32 @mul_add(i32 %x, i32 %y, i32 %z) {
43entry:
44 %tmp = mul i32 %x, %y
45 %tmp2 = add i32 %tmp, %z
46 ret i32 %tmp2
47}
Owen Anderson75bbde82007-10-20 05:23:06 +000048</pre>
49</div>
50
Bill Wendling9a345e52008-05-19 00:19:02 +000051<p>If you're unsure what the above code says, skim through the <a href="../LangRef.html">LLVM Language Reference Manual</a> and convince yourself that the above LLVM IR is actually equivalent to the original function. Once you’re satisfied with that, let's move on to actually generating it programmatically!</p>
Owen Anderson75bbde82007-10-20 05:23:06 +000052
Owen Andersona7e5ef42007-10-25 06:49:29 +000053<p>Of course, before we can start, we need to <code>#include</code> the appropriate LLVM header files:</p>
54
55<div class="doc_code">
56<pre>
Gabor Greif6f6aff02009-03-11 20:23:40 +000057#include "llvm/Module.h"
58#include "llvm/Function.h"
59#include "llvm/PassManager.h"
60#include "llvm/CallingConv.h"
61#include "llvm/Analysis/Verifier.h"
62#include "llvm/Assembly/PrintModulePass.h"
63#include "llvm/Support/IRBuilder.h"
Bill Wendling285ea5c2009-05-12 18:29:42 +000064#include "llvm/Support/raw_ostream.h"
Owen Andersona7e5ef42007-10-25 06:49:29 +000065</pre>
66</div>
Owen Anderson75bbde82007-10-20 05:23:06 +000067
Bill Wendling9a345e52008-05-19 00:19:02 +000068<p>Now, let's get started on our real program. Here's what our basic <code>main()</code> will look like:</p>
Owen Anderson75bbde82007-10-20 05:23:06 +000069
70<div class="doc_code">
71<pre>
72using namespace llvm;
73
74Module* makeLLVMModule();
75
76int main(int argc, char**argv) {
77 Module* Mod = makeLLVMModule();
78
79 verifyModule(*Mod, PrintMessageAction);
80
81 PassManager PM;
Bill Wendling285ea5c2009-05-12 18:29:42 +000082 PM.add(createPrintModulePass(&amp;outs()));
Owen Anderson75bbde82007-10-20 05:23:06 +000083 PM.run(*Mod);
84
Bill Wendlingea5a6b302008-05-19 00:15:33 +000085 delete Mod;
Owen Anderson75bbde82007-10-20 05:23:06 +000086 return 0;
87}
88</pre>
89</div>
90
Chris Lattnerdd684e22008-02-10 19:11:04 +000091<p>The first segment is pretty simple: it creates an LLVM “module.” In LLVM, a module represents a single unit of code that is to be processed together. A module contains things like global variables, function declarations, and implementations. Here we’ve declared a <code>makeLLVMModule()</code> function to do the real work of creating the module. Don’t worry, we’ll be looking at that one next!</p>
Owen Anderson75bbde82007-10-20 05:23:06 +000092
Bill Wendling9a345e52008-05-19 00:19:02 +000093<p>The second segment runs the LLVM module verifier on our newly created module. While this probably isn’t really necessary for a simple module like this one, it's always a good idea, especially if you’re generating LLVM IR based on some input. The verifier will print an error message if your LLVM module is malformed in any way.</p>
Owen Anderson75bbde82007-10-20 05:23:06 +000094
Bill Wendlingc0892282008-05-19 00:05:30 +000095<p>Finally, we instantiate an LLVM <code>PassManager</code> and run
96the <code>PrintModulePass</code> on our module. LLVM uses an explicit pass
97infrastructure to manage optimizations and various other things.
98A <code>PassManager</code>, as should be obvious from its name, manages passes:
99it is responsible for scheduling them, invoking them, and ensuring the proper
100disposal after we’re done with them. For this example, we’re just using a
101trivial pass that prints out our module in textual form.</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000102
Bill Wendling9a345e52008-05-19 00:19:02 +0000103<p>Now onto the interesting part: creating and populating a module. Here's the
Bill Wendlingc0892282008-05-19 00:05:30 +0000104first chunk of our <code>makeLLVMModule()</code>:</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000105
106<div class="doc_code">
107<pre>
108Module* makeLLVMModule() {
109 // Module Construction
Chris Lattnera8f23072009-07-21 22:47:03 +0000110 Module* mod = new Module("test", getGlobalContext());
Owen Anderson75bbde82007-10-20 05:23:06 +0000111</pre>
112</div>
113
114<p>Exciting, isn’t it!? All we’re doing here is instantiating a module and giving it a name. The name isn’t particularly important unless you’re going to be dealing with multiple modules at once.</p>
115
116<div class="doc_code">
117<pre>
Chris Lattnerdd684e22008-02-10 19:11:04 +0000118 Constant* c = mod-&gt;getOrInsertFunction("mul_add",
Owen Anderson9b3cf312007-10-22 06:29:31 +0000119 /*ret type*/ IntegerType::get(32),
120 /*args*/ IntegerType::get(32),
121 IntegerType::get(32),
122 IntegerType::get(32),
123 /*varargs terminated with null*/ NULL);
Owen Anderson75bbde82007-10-20 05:23:06 +0000124
Owen Anderson64255002007-10-20 05:40:47 +0000125 Function* mul_add = cast&lt;Function&gt;(c);
Chris Lattnerdd684e22008-02-10 19:11:04 +0000126 mul_add-&gt;setCallingConv(CallingConv::C);
Owen Anderson75bbde82007-10-20 05:23:06 +0000127</pre>
128</div>
129
Chris Lattnerdd684e22008-02-10 19:11:04 +0000130<p>We construct our <code>Function</code> by calling <code>getOrInsertFunction()</code> on our module, passing in the name, return type, and argument types of the function. In the case of our <code>mul_add</code> function, that means one 32-bit integer for the return value and three 32-bit integers for the arguments.</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000131
Chris Lattnerdd684e22008-02-10 19:11:04 +0000132<p>You'll notice that <code>getOrInsertFunction()</code> doesn't actually return a <code>Function*</code>. This is because <code>getOrInsertFunction()</code> will return a cast of the existing function if the function already existed with a different prototype. Since we know that there's not already a <code>mul_add</code> function, we can safely just cast <code>c</code> to a <code>Function*</code>.
Owen Anderson64255002007-10-20 05:40:47 +0000133
Bill Wendlingc0892282008-05-19 00:05:30 +0000134<p>In addition, we set the calling convention for our new function to be the C
135calling convention. This isn’t strictly necessary, but it ensures that our new
136function will interoperate properly with C code, which is a good thing.</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000137
138<div class="doc_code">
139<pre>
Chris Lattnerdd684e22008-02-10 19:11:04 +0000140 Function::arg_iterator args = mul_add-&gt;arg_begin();
Owen Anderson75bbde82007-10-20 05:23:06 +0000141 Value* x = args++;
Chris Lattnerdd684e22008-02-10 19:11:04 +0000142 x-&gt;setName("x");
Owen Anderson75bbde82007-10-20 05:23:06 +0000143 Value* y = args++;
Chris Lattnerdd684e22008-02-10 19:11:04 +0000144 y-&gt;setName("y");
Owen Anderson75bbde82007-10-20 05:23:06 +0000145 Value* z = args++;
Chris Lattnerdd684e22008-02-10 19:11:04 +0000146 z-&gt;setName("z");
Owen Anderson75bbde82007-10-20 05:23:06 +0000147</pre>
148</div>
149
Bill Wendling9a345e52008-05-19 00:19:02 +0000150<p>While we’re setting up our function, let's also give names to the parameters. This also isn’t strictly necessary (LLVM will generate names for them if you don’t specify them), but it’ll make looking at our output somewhat more pleasant. To name the parameters, we iterate over the arguments of our function and call <code>setName()</code> on them. We’ll also keep the pointer to <code>x</code>, <code>y</code>, and <code>z</code> around, since we’ll need them when we get around to creating instructions.</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000151
Chris Lattnerdd684e22008-02-10 19:11:04 +0000152<p>Great! We have a function now. But what good is a function if it has no body? Before we start working on a body for our new function, we need to recall some details of the LLVM IR. The IR, being an abstract assembly language, represents control flow using jumps (we call them branches), both conditional and unconditional. The straight-line sequences of code between branches are called basic blocks, or just blocks. To create a body for our function, we fill it with blocks:</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000153
154<div class="doc_code">
155<pre>
Owen Anderson35b47072009-08-13 21:58:54 +0000156 BasicBlock* block = BasicBlock::Create(getGlobalContext(), "entry", mul_add);
Gabor Greif56cca5f2009-03-11 19:51:07 +0000157 IRBuilder&lt;&gt; builder(block);
Owen Anderson75bbde82007-10-20 05:23:06 +0000158</pre>
159</div>
160
Duncan Sandsa989c7e2008-04-13 06:22:09 +0000161<p>We create a new basic block, as you might expect, by calling its constructor. All we need to tell it is its name and the function to which it belongs. In addition, we’re creating an <code>IRBuilder</code> object, which is a convenience interface for creating instructions and appending them to the end of a block. Instructions can be created through their constructors as well, but some of their interfaces are quite complicated. Unless you need a lot of control, using <code>IRBuilder</code> will make your life simpler.</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000162
163<div class="doc_code">
164<pre>
165 Value* tmp = builder.CreateBinOp(Instruction::Mul,
Bill Wendlingea5a6b302008-05-19 00:15:33 +0000166 x, y, "tmp");
Owen Anderson75bbde82007-10-20 05:23:06 +0000167 Value* tmp2 = builder.CreateBinOp(Instruction::Add,
168 tmp, z, "tmp2");
169
170 builder.CreateRet(tmp2);
Owen Anderson9b3cf312007-10-22 06:29:31 +0000171
172 return mod;
Owen Anderson75bbde82007-10-20 05:23:06 +0000173}
174</pre>
175</div>
176
Bill Wendling9a345e52008-05-19 00:19:02 +0000177<p>The final step in creating our function is to create the instructions that make it up. Our <code>mul_add</code> function is composed of just three instructions: a multiply, an add, and a return. <code>IRBuilder</code> gives us a simple interface for constructing these instructions and appending them to the “entry” block. Each of the calls to <code>IRBuilder</code> returns a <code>Value*</code> that represents the value yielded by the instruction. You’ll also notice that, above, <code>x</code>, <code>y</code>, and <code>z</code> are also <code>Value*</code>'s, so it's clear that instructions operate on <code>Value*</code>'s.</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000178
Bill Wendling9a345e52008-05-19 00:19:02 +0000179<p>And that's it! Now you can compile and run your code, and get a wonderful textual print out of the LLVM IR we saw at the beginning. To compile, use the following command line as a guide:</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000180
Owen Andersona7e5ef42007-10-25 06:49:29 +0000181<div class="doc_code">
182<pre>
Owen Andersonbbc4e3d2008-03-24 21:38:01 +0000183# c++ -g tut1.cpp `llvm-config --cxxflags --ldflags --libs core` -o tut1
Chris Lattnerdd684e22008-02-10 19:11:04 +0000184# ./tut1
Owen Andersona7e5ef42007-10-25 06:49:29 +0000185</pre>
186</div>
187
188<p>The <code>llvm-config</code> utility is used to obtain the necessary GCC-compatible compiler flags for linking with LLVM. For this example, we only need the 'core' library. We'll use others once we start adding optimizers and the JIT engine.</p>
Owen Anderson75bbde82007-10-20 05:23:06 +0000189
Chris Lattnerdd684e22008-02-10 19:11:04 +0000190<a href="JITTutorial2.html">Next: A More Complicated Function</a>
Owen Anderson75bbde82007-10-20 05:23:06 +0000191</div>
192
Owen Anderson8e75bf72007-10-25 06:45:01 +0000193<!-- *********************************************************************** -->
194<hr>
195<address>
196 <a href="http://jigsaw.w3.org/css-validator/check/referer"><img
197 src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Valid CSS!"></a>
198 <a href="http://validator.w3.org/check/referer"><img
199 src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!"></a>
200
201 <a href="mailto:owen@apple.com">Owen Anderson</a><br>
202 <a href="http://llvm.org">The LLVM Compiler Infrastructure</a><br>
Chris Lattnera8f23072009-07-21 22:47:03 +0000203 Last modified: $Date: 2009-07-21 11:05:13 -0700 (Tue, 21 Jul 2009) $
Owen Anderson8e75bf72007-10-25 06:45:01 +0000204</address>
205
Owen Anderson75bbde82007-10-20 05:23:06 +0000206</body>
Duncan Sands1c5ae822007-11-05 15:15:50 +0000207</html>