blob: eee3207f90402e5da2d8fd02dc2fa8e2d3e16e32 [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>LLVM Assembly Language Reference Manual</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="Chris Lattner">
<meta name="description"
content="LLVM Assembly Language Reference Manual.">
<link rel="stylesheet" href="llvm.css" type="text/css">
</head>
<body>
<div class="doc_title"> LLVM Language Reference Manual </div>
<ol>
<li><a href="#abstract">Abstract</a></li>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#identifiers">Identifiers</a></li>
<li><a href="#highlevel">High Level Structure</a>
<ol>
<li><a href="#modulestructure">Module Structure</a></li>
<li><a href="#linkage">Linkage Types</a>
<ol>
<li><a href="#linkage_private">'<tt>private</tt>' Linkage</a></li>
<li><a href="#linkage_linker_private">'<tt>linker_private</tt>' Linkage</a></li>
<li><a href="#linkage_internal">'<tt>internal</tt>' Linkage</a></li>
<li><a href="#linkage_available_externally">'<tt>available_externally</tt>' Linkage</a></li>
<li><a href="#linkage_linkonce">'<tt>linkonce</tt>' Linkage</a></li>
<li><a href="#linkage_common">'<tt>common</tt>' Linkage</a></li>
<li><a href="#linkage_weak">'<tt>weak</tt>' Linkage</a></li>
<li><a href="#linkage_appending">'<tt>appending</tt>' Linkage</a></li>
<li><a href="#linkage_externweak">'<tt>extern_weak</tt>' Linkage</a></li>
<li><a href="#linkage_linkonce">'<tt>linkonce_odr</tt>' Linkage</a></li>
<li><a href="#linkage_weak">'<tt>weak_odr</tt>' Linkage</a></li>
<li><a href="#linkage_external">'<tt>externally visible</tt>' Linkage</a></li>
<li><a href="#linkage_dllimport">'<tt>dllimport</tt>' Linkage</a></li>
<li><a href="#linkage_dllexport">'<tt>dllexport</tt>' Linkage</a></li>
</ol>
</li>
<li><a href="#callingconv">Calling Conventions</a></li>
<li><a href="#namedtypes">Named Types</a></li>
<li><a href="#globalvars">Global Variables</a></li>
<li><a href="#functionstructure">Functions</a></li>
<li><a href="#aliasstructure">Aliases</a></li>
<li><a href="#paramattrs">Parameter Attributes</a></li>
<li><a href="#fnattrs">Function Attributes</a></li>
<li><a href="#gc">Garbage Collector Names</a></li>
<li><a href="#moduleasm">Module-Level Inline Assembly</a></li>
<li><a href="#datalayout">Data Layout</a></li>
<li><a href="#pointeraliasing">Pointer Aliasing Rules</a></li>
</ol>
</li>
<li><a href="#typesystem">Type System</a>
<ol>
<li><a href="#t_classifications">Type Classifications</a></li>
<li><a href="#t_primitive">Primitive Types</a>
<ol>
<li><a href="#t_floating">Floating Point Types</a></li>
<li><a href="#t_void">Void Type</a></li>
<li><a href="#t_label">Label Type</a></li>
<li><a href="#t_metadata">Metadata Type</a></li>
</ol>
</li>
<li><a href="#t_derived">Derived Types</a>
<ol>
<li><a href="#t_integer">Integer Type</a></li>
<li><a href="#t_array">Array Type</a></li>
<li><a href="#t_function">Function Type</a></li>
<li><a href="#t_pointer">Pointer Type</a></li>
<li><a href="#t_struct">Structure Type</a></li>
<li><a href="#t_pstruct">Packed Structure Type</a></li>
<li><a href="#t_vector">Vector Type</a></li>
<li><a href="#t_opaque">Opaque Type</a></li>
</ol>
</li>
<li><a href="#t_uprefs">Type Up-references</a></li>
</ol>
</li>
<li><a href="#constants">Constants</a>
<ol>
<li><a href="#simpleconstants">Simple Constants</a></li>
<li><a href="#complexconstants">Complex Constants</a></li>
<li><a href="#globalconstants">Global Variable and Function Addresses</a></li>
<li><a href="#undefvalues">Undefined Values</a></li>
<li><a href="#constantexprs">Constant Expressions</a></li>
<li><a href="#metadata">Embedded Metadata</a></li>
</ol>
</li>
<li><a href="#othervalues">Other Values</a>
<ol>
<li><a href="#inlineasm">Inline Assembler Expressions</a></li>
</ol>
</li>
<li><a href="#intrinsic_globals">Intrinsic Global Variables</a>
<ol>
<li><a href="#intg_used">The '<tt>llvm.used</tt>' Global Variable</a></li>
<li><a href="#intg_compiler_used">The '<tt>llvm.compiler.used</tt>'
Global Variable</a></li>
<li><a href="#intg_global_ctors">The '<tt>llvm.global_ctors</tt>'
Global Variable</a></li>
<li><a href="#intg_global_dtors">The '<tt>llvm.global_dtors</tt>'
Global Variable</a></li>
</ol>
</li>
<li><a href="#instref">Instruction Reference</a>
<ol>
<li><a href="#terminators">Terminator Instructions</a>
<ol>
<li><a href="#i_ret">'<tt>ret</tt>' Instruction</a></li>
<li><a href="#i_br">'<tt>br</tt>' Instruction</a></li>
<li><a href="#i_switch">'<tt>switch</tt>' Instruction</a></li>
<li><a href="#i_invoke">'<tt>invoke</tt>' Instruction</a></li>
<li><a href="#i_unwind">'<tt>unwind</tt>' Instruction</a></li>
<li><a href="#i_unreachable">'<tt>unreachable</tt>' Instruction</a></li>
</ol>
</li>
<li><a href="#binaryops">Binary Operations</a>
<ol>
<li><a href="#i_add">'<tt>add</tt>' Instruction</a></li>
<li><a href="#i_fadd">'<tt>fadd</tt>' Instruction</a></li>
<li><a href="#i_sub">'<tt>sub</tt>' Instruction</a></li>
<li><a href="#i_fsub">'<tt>fsub</tt>' Instruction</a></li>
<li><a href="#i_mul">'<tt>mul</tt>' Instruction</a></li>
<li><a href="#i_fmul">'<tt>fmul</tt>' Instruction</a></li>
<li><a href="#i_udiv">'<tt>udiv</tt>' Instruction</a></li>
<li><a href="#i_sdiv">'<tt>sdiv</tt>' Instruction</a></li>
<li><a href="#i_fdiv">'<tt>fdiv</tt>' Instruction</a></li>
<li><a href="#i_urem">'<tt>urem</tt>' Instruction</a></li>
<li><a href="#i_srem">'<tt>srem</tt>' Instruction</a></li>
<li><a href="#i_frem">'<tt>frem</tt>' Instruction</a></li>
</ol>
</li>
<li><a href="#bitwiseops">Bitwise Binary Operations</a>
<ol>
<li><a href="#i_shl">'<tt>shl</tt>' Instruction</a></li>
<li><a href="#i_lshr">'<tt>lshr</tt>' Instruction</a></li>
<li><a href="#i_ashr">'<tt>ashr</tt>' Instruction</a></li>
<li><a href="#i_and">'<tt>and</tt>' Instruction</a></li>
<li><a href="#i_or">'<tt>or</tt>' Instruction</a></li>
<li><a href="#i_xor">'<tt>xor</tt>' Instruction</a></li>
</ol>
</li>
<li><a href="#vectorops">Vector Operations</a>
<ol>
<li><a href="#i_extractelement">'<tt>extractelement</tt>' Instruction</a></li>
<li><a href="#i_insertelement">'<tt>insertelement</tt>' Instruction</a></li>
<li><a href="#i_shufflevector">'<tt>shufflevector</tt>' Instruction</a></li>
</ol>
</li>
<li><a href="#aggregateops">Aggregate Operations</a>
<ol>
<li><a href="#i_extractvalue">'<tt>extractvalue</tt>' Instruction</a></li>
<li><a href="#i_insertvalue">'<tt>insertvalue</tt>' Instruction</a></li>
</ol>
</li>
<li><a href="#memoryops">Memory Access and Addressing Operations</a>
<ol>
<li><a href="#i_malloc">'<tt>malloc</tt>' Instruction</a></li>
<li><a href="#i_free">'<tt>free</tt>' Instruction</a></li>
<li><a href="#i_alloca">'<tt>alloca</tt>' Instruction</a></li>
<li><a href="#i_load">'<tt>load</tt>' Instruction</a></li>
<li><a href="#i_store">'<tt>store</tt>' Instruction</a></li>
<li><a href="#i_getelementptr">'<tt>getelementptr</tt>' Instruction</a></li>
</ol>
</li>
<li><a href="#convertops">Conversion Operations</a>
<ol>
<li><a href="#i_trunc">'<tt>trunc .. to</tt>' Instruction</a></li>
<li><a href="#i_zext">'<tt>zext .. to</tt>' Instruction</a></li>
<li><a href="#i_sext">'<tt>sext .. to</tt>' Instruction</a></li>
<li><a href="#i_fptrunc">'<tt>fptrunc .. to</tt>' Instruction</a></li>
<li><a href="#i_fpext">'<tt>fpext .. to</tt>' Instruction</a></li>
<li><a href="#i_fptoui">'<tt>fptoui .. to</tt>' Instruction</a></li>
<li><a href="#i_fptosi">'<tt>fptosi .. to</tt>' Instruction</a></li>
<li><a href="#i_uitofp">'<tt>uitofp .. to</tt>' Instruction</a></li>
<li><a href="#i_sitofp">'<tt>sitofp .. to</tt>' Instruction</a></li>
<li><a href="#i_ptrtoint">'<tt>ptrtoint .. to</tt>' Instruction</a></li>
<li><a href="#i_inttoptr">'<tt>inttoptr .. to</tt>' Instruction</a></li>
<li><a href="#i_bitcast">'<tt>bitcast .. to</tt>' Instruction</a></li>
</ol>
</li>
<li><a href="#otherops">Other Operations</a>
<ol>
<li><a href="#i_icmp">'<tt>icmp</tt>' Instruction</a></li>
<li><a href="#i_fcmp">'<tt>fcmp</tt>' Instruction</a></li>
<li><a href="#i_phi">'<tt>phi</tt>' Instruction</a></li>
<li><a href="#i_select">'<tt>select</tt>' Instruction</a></li>
<li><a href="#i_call">'<tt>call</tt>' Instruction</a></li>
<li><a href="#i_va_arg">'<tt>va_arg</tt>' Instruction</a></li>
</ol>
</li>
</ol>
</li>
<li><a href="#intrinsics">Intrinsic Functions</a>
<ol>
<li><a href="#int_varargs">Variable Argument Handling Intrinsics</a>
<ol>
<li><a href="#int_va_start">'<tt>llvm.va_start</tt>' Intrinsic</a></li>
<li><a href="#int_va_end">'<tt>llvm.va_end</tt>' Intrinsic</a></li>
<li><a href="#int_va_copy">'<tt>llvm.va_copy</tt>' Intrinsic</a></li>
</ol>
</li>
<li><a href="#int_gc">Accurate Garbage Collection Intrinsics</a>
<ol>
<li><a href="#int_gcroot">'<tt>llvm.gcroot</tt>' Intrinsic</a></li>
<li><a href="#int_gcread">'<tt>llvm.gcread</tt>' Intrinsic</a></li>
<li><a href="#int_gcwrite">'<tt>llvm.gcwrite</tt>' Intrinsic</a></li>
</ol>
</li>
<li><a href="#int_codegen">Code Generator Intrinsics</a>
<ol>
<li><a href="#int_returnaddress">'<tt>llvm.returnaddress</tt>' Intrinsic</a></li>
<li><a href="#int_frameaddress">'<tt>llvm.frameaddress</tt>' Intrinsic</a></li>
<li><a href="#int_stacksave">'<tt>llvm.stacksave</tt>' Intrinsic</a></li>
<li><a href="#int_stackrestore">'<tt>llvm.stackrestore</tt>' Intrinsic</a></li>
<li><a href="#int_prefetch">'<tt>llvm.prefetch</tt>' Intrinsic</a></li>
<li><a href="#int_pcmarker">'<tt>llvm.pcmarker</tt>' Intrinsic</a></li>
<li><a href="#int_readcyclecounter"><tt>llvm.readcyclecounter</tt>' Intrinsic</a></li>
</ol>
</li>
<li><a href="#int_libc">Standard C Library Intrinsics</a>
<ol>
<li><a href="#int_memcpy">'<tt>llvm.memcpy.*</tt>' Intrinsic</a></li>
<li><a href="#int_memmove">'<tt>llvm.memmove.*</tt>' Intrinsic</a></li>
<li><a href="#int_memset">'<tt>llvm.memset.*</tt>' Intrinsic</a></li>
<li><a href="#int_sqrt">'<tt>llvm.sqrt.*</tt>' Intrinsic</a></li>
<li><a href="#int_powi">'<tt>llvm.powi.*</tt>' Intrinsic</a></li>
<li><a href="#int_sin">'<tt>llvm.sin.*</tt>' Intrinsic</a></li>
<li><a href="#int_cos">'<tt>llvm.cos.*</tt>' Intrinsic</a></li>
<li><a href="#int_pow">'<tt>llvm.pow.*</tt>' Intrinsic</a></li>
</ol>
</li>
<li><a href="#int_manip">Bit Manipulation Intrinsics</a>
<ol>
<li><a href="#int_bswap">'<tt>llvm.bswap.*</tt>' Intrinsics</a></li>
<li><a href="#int_ctpop">'<tt>llvm.ctpop.*</tt>' Intrinsic </a></li>
<li><a href="#int_ctlz">'<tt>llvm.ctlz.*</tt>' Intrinsic </a></li>
<li><a href="#int_cttz">'<tt>llvm.cttz.*</tt>' Intrinsic </a></li>
</ol>
</li>
<li><a href="#int_overflow">Arithmetic with Overflow Intrinsics</a>
<ol>
<li><a href="#int_sadd_overflow">'<tt>llvm.sadd.with.overflow.*</tt> Intrinsics</a></li>
<li><a href="#int_uadd_overflow">'<tt>llvm.uadd.with.overflow.*</tt> Intrinsics</a></li>
<li><a href="#int_ssub_overflow">'<tt>llvm.ssub.with.overflow.*</tt> Intrinsics</a></li>
<li><a href="#int_usub_overflow">'<tt>llvm.usub.with.overflow.*</tt> Intrinsics</a></li>
<li><a href="#int_smul_overflow">'<tt>llvm.smul.with.overflow.*</tt> Intrinsics</a></li>
<li><a href="#int_umul_overflow">'<tt>llvm.umul.with.overflow.*</tt> Intrinsics</a></li>
</ol>
</li>
<li><a href="#int_debugger">Debugger intrinsics</a></li>
<li><a href="#int_eh">Exception Handling intrinsics</a></li>
<li><a href="#int_trampoline">Trampoline Intrinsic</a>
<ol>
<li><a href="#int_it">'<tt>llvm.init.trampoline</tt>' Intrinsic</a></li>
</ol>
</li>
<li><a href="#int_atomics">Atomic intrinsics</a>
<ol>
<li><a href="#int_memory_barrier"><tt>llvm.memory_barrier</tt></a></li>
<li><a href="#int_atomic_cmp_swap"><tt>llvm.atomic.cmp.swap</tt></a></li>
<li><a href="#int_atomic_swap"><tt>llvm.atomic.swap</tt></a></li>
<li><a href="#int_atomic_load_add"><tt>llvm.atomic.load.add</tt></a></li>
<li><a href="#int_atomic_load_sub"><tt>llvm.atomic.load.sub</tt></a></li>
<li><a href="#int_atomic_load_and"><tt>llvm.atomic.load.and</tt></a></li>
<li><a href="#int_atomic_load_nand"><tt>llvm.atomic.load.nand</tt></a></li>
<li><a href="#int_atomic_load_or"><tt>llvm.atomic.load.or</tt></a></li>
<li><a href="#int_atomic_load_xor"><tt>llvm.atomic.load.xor</tt></a></li>
<li><a href="#int_atomic_load_max"><tt>llvm.atomic.load.max</tt></a></li>
<li><a href="#int_atomic_load_min"><tt>llvm.atomic.load.min</tt></a></li>
<li><a href="#int_atomic_load_umax"><tt>llvm.atomic.load.umax</tt></a></li>
<li><a href="#int_atomic_load_umin"><tt>llvm.atomic.load.umin</tt></a></li>
</ol>
</li>
<li><a href="#int_general">General intrinsics</a>
<ol>
<li><a href="#int_var_annotation">
'<tt>llvm.var.annotation</tt>' Intrinsic</a></li>
<li><a href="#int_annotation">
'<tt>llvm.annotation.*</tt>' Intrinsic</a></li>
<li><a href="#int_trap">
'<tt>llvm.trap</tt>' Intrinsic</a></li>
<li><a href="#int_stackprotector">
'<tt>llvm.stackprotector</tt>' Intrinsic</a></li>
</ol>
</li>
</ol>
</li>
</ol>
<div class="doc_author">
<p>Written by <a href="mailto:sabre@nondot.org">Chris Lattner</a>
and <a href="mailto:vadve@cs.uiuc.edu">Vikram Adve</a></p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="abstract">Abstract </a></div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>This document is a reference manual for the LLVM assembly language. LLVM is
a Static Single Assignment (SSA) based representation that provides type
safety, low-level operations, flexibility, and the capability of representing
'all' high-level languages cleanly. It is the common code representation
used throughout all phases of the LLVM compilation strategy.</p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="introduction">Introduction</a> </div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>The LLVM code representation is designed to be used in three different forms:
as an in-memory compiler IR, as an on-disk bitcode representation (suitable
for fast loading by a Just-In-Time compiler), and as a human readable
assembly language representation. This allows LLVM to provide a powerful
intermediate representation for efficient compiler transformations and
analysis, while providing a natural means to debug and visualize the
transformations. The three different forms of LLVM are all equivalent. This
document describes the human readable representation and notation.</p>
<p>The LLVM representation aims to be light-weight and low-level while being
expressive, typed, and extensible at the same time. It aims to be a
"universal IR" of sorts, by being at a low enough level that high-level ideas
may be cleanly mapped to it (similar to how microprocessors are "universal
IR's", allowing many source languages to be mapped to them). By providing
type information, LLVM can be used as the target of optimizations: for
example, through pointer analysis, it can be proven that a C automatic
variable is never accessed outside of the current function... allowing it to
be promoted to a simple SSA value instead of a memory location.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="wellformed">Well-Formedness</a> </div>
<div class="doc_text">
<p>It is important to note that this document describes 'well formed' LLVM
assembly language. There is a difference between what the parser accepts and
what is considered 'well formed'. For example, the following instruction is
syntactically okay, but not well formed:</p>
<div class="doc_code">
<pre>
%x = <a href="#i_add">add</a> i32 1, %x
</pre>
</div>
<p>...because the definition of <tt>%x</tt> does not dominate all of its
uses. The LLVM infrastructure provides a verification pass that may be used
to verify that an LLVM module is well formed. This pass is automatically run
by the parser after parsing input assembly and by the optimizer before it
outputs bitcode. The violations pointed out by the verifier pass indicate
bugs in transformation passes or input to the parser.</p>
</div>
<!-- Describe the typesetting conventions here. -->
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="identifiers">Identifiers</a> </div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>LLVM identifiers come in two basic types: global and local. Global
identifiers (functions, global variables) begin with the <tt>'@'</tt>
character. Local identifiers (register names, types) begin with
the <tt>'%'</tt> character. Additionally, there are three different formats
for identifiers, for different purposes:</p>
<ol>
<li>Named values are represented as a string of characters with their prefix.
For example, <tt>%foo</tt>, <tt>@DivisionByZero</tt>,
<tt>%a.really.long.identifier</tt>. The actual regular expression used is
'<tt>[%@][a-zA-Z$._][a-zA-Z$._0-9]*</tt>'. Identifiers which require
other characters in their names can be surrounded with quotes. Special
characters may be escaped using <tt>"\xx"</tt> where <tt>xx</tt> is the
ASCII code for the character in hexadecimal. In this way, any character
can be used in a name value, even quotes themselves.</li>
<li>Unnamed values are represented as an unsigned numeric value with their
prefix. For example, <tt>%12</tt>, <tt>@2</tt>, <tt>%44</tt>.</li>
<li>Constants, which are described in a <a href="#constants">section about
constants</a>, below.</li>
</ol>
<p>LLVM requires that values start with a prefix for two reasons: Compilers
don't need to worry about name clashes with reserved words, and the set of
reserved words may be expanded in the future without penalty. Additionally,
unnamed identifiers allow a compiler to quickly come up with a temporary
variable without having to avoid symbol table conflicts.</p>
<p>Reserved words in LLVM are very similar to reserved words in other
languages. There are keywords for different opcodes
('<tt><a href="#i_add">add</a></tt>',
'<tt><a href="#i_bitcast">bitcast</a></tt>',
'<tt><a href="#i_ret">ret</a></tt>', etc...), for primitive type names
('<tt><a href="#t_void">void</a></tt>',
'<tt><a href="#t_primitive">i32</a></tt>', etc...), and others. These
reserved words cannot conflict with variable names, because none of them
start with a prefix character (<tt>'%'</tt> or <tt>'@'</tt>).</p>
<p>Here is an example of LLVM code to multiply the integer variable
'<tt>%X</tt>' by 8:</p>
<p>The easy way:</p>
<div class="doc_code">
<pre>
%result = <a href="#i_mul">mul</a> i32 %X, 8
</pre>
</div>
<p>After strength reduction:</p>
<div class="doc_code">
<pre>
%result = <a href="#i_shl">shl</a> i32 %X, i8 3
</pre>
</div>
<p>And the hard way:</p>
<div class="doc_code">
<pre>
<a href="#i_add">add</a> i32 %X, %X <i>; yields {i32}:%0</i>
<a href="#i_add">add</a> i32 %0, %0 <i>; yields {i32}:%1</i>
%result = <a href="#i_add">add</a> i32 %1, %1
</pre>
</div>
<p>This last way of multiplying <tt>%X</tt> by 8 illustrates several important
lexical features of LLVM:</p>
<ol>
<li>Comments are delimited with a '<tt>;</tt>' and go until the end of
line.</li>
<li>Unnamed temporaries are created when the result of a computation is not
assigned to a named value.</li>
<li>Unnamed temporaries are numbered sequentially</li>
</ol>
<p>...and it also shows a convention that we follow in this document. When
demonstrating instructions, we will follow an instruction with a comment that
defines the type and name of value produced. Comments are shown in italic
text.</p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="highlevel">High Level Structure</a> </div>
<!-- *********************************************************************** -->
<!-- ======================================================================= -->
<div class="doc_subsection"> <a name="modulestructure">Module Structure</a>
</div>
<div class="doc_text">
<p>LLVM programs are composed of "Module"s, each of which is a translation unit
of the input programs. Each module consists of functions, global variables,
and symbol table entries. Modules may be combined together with the LLVM
linker, which merges function (and global variable) definitions, resolves
forward declarations, and merges symbol table entries. Here is an example of
the "hello world" module:</p>
<div class="doc_code">
<pre><i>; Declare the string constant as a global constant...</i>
<a href="#identifiers">@.LC0</a> = <a href="#linkage_internal">internal</a> <a
href="#globalvars">constant</a> <a href="#t_array">[13 x i8]</a> c"hello world\0A\00" <i>; [13 x i8]*</i>
<i>; External declaration of the puts function</i>
<a href="#functionstructure">declare</a> i32 @puts(i8 *) <i>; i32(i8 *)* </i>
<i>; Definition of main function</i>
define i32 @main() { <i>; i32()* </i>
<i>; Convert [13 x i8]* to i8 *...</i>
%cast210 = <a
href="#i_getelementptr">getelementptr</a> [13 x i8]* @.LC0, i64 0, i64 0 <i>; i8 *</i>
<i>; Call puts function to write out the string to stdout...</i>
<a
href="#i_call">call</a> i32 @puts(i8 * %cast210) <i>; i32</i>
<a
href="#i_ret">ret</a> i32 0<br>}<br>
</pre>
</div>
<p>This example is made up of a <a href="#globalvars">global variable</a> named
"<tt>.LC0</tt>", an external declaration of the "<tt>puts</tt>" function, and
a <a href="#functionstructure">function definition</a> for
"<tt>main</tt>".</p>
<p>In general, a module is made up of a list of global values, where both
functions and global variables are global values. Global values are
represented by a pointer to a memory location (in this case, a pointer to an
array of char, and a pointer to a function), and have one of the
following <a href="#linkage">linkage types</a>.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="linkage">Linkage Types</a>
</div>
<div class="doc_text">
<p>All Global Variables and Functions have one of the following types of
linkage:</p>
<dl>
<dt><tt><b><a name="linkage_private">private</a></b></tt>: </dt>
<dd>Global values with private linkage are only directly accessible by objects
in the current module. In particular, linking code into a module with an
private global value may cause the private to be renamed as necessary to
avoid collisions. Because the symbol is private to the module, all
references can be updated. This doesn't show up in any symbol table in the
object file.</dd>
<dt><tt><b><a name="linkage_linker_private">linker_private</a></b></tt>: </dt>
<dd>Similar to private, but the symbol is passed through the assembler and
removed by the linker after evaluation.</dd>
<dt><tt><b><a name="linkage_internal">internal</a></b></tt>: </dt>
<dd>Similar to private, but the value shows as a local symbol
(<tt>STB_LOCAL</tt> in the case of ELF) in the object file. This
corresponds to the notion of the '<tt>static</tt>' keyword in C.</dd>
<dt><tt><b><a name="linkage_available_externally">available_externally</a></b></tt>: </dt>
<dd>Globals with "<tt>available_externally</tt>" linkage are never emitted
into the object file corresponding to the LLVM module. They exist to
allow inlining and other optimizations to take place given knowledge of
the definition of the global, which is known to be somewhere outside the
module. Globals with <tt>available_externally</tt> linkage are allowed to
be discarded at will, and are otherwise the same as <tt>linkonce_odr</tt>.
This linkage type is only allowed on definitions, not declarations.</dd>
<dt><tt><b><a name="linkage_linkonce">linkonce</a></b></tt>: </dt>
<dd>Globals with "<tt>linkonce</tt>" linkage are merged with other globals of
the same name when linkage occurs. This is typically used to implement
inline functions, templates, or other code which must be generated in each
translation unit that uses it. Unreferenced <tt>linkonce</tt> globals are
allowed to be discarded.</dd>
<dt><tt><b><a name="linkage_weak">weak</a></b></tt>: </dt>
<dd>"<tt>weak</tt>" linkage has the same merging semantics as
<tt>linkonce</tt> linkage, except that unreferenced globals with
<tt>weak</tt> linkage may not be discarded. This is used for globals that
are declared "weak" in C source code.</dd>
<dt><tt><b><a name="linkage_common">common</a></b></tt>: </dt>
<dd>"<tt>common</tt>" linkage is most similar to "<tt>weak</tt>" linkage, but
they are used for tentative definitions in C, such as "<tt>int X;</tt>" at
global scope.
Symbols with "<tt>common</tt>" linkage are merged in the same way as
<tt>weak symbols</tt>, and they may not be deleted if unreferenced.
<tt>common</tt> symbols may not have an explicit section,
must have a zero initializer, and may not be marked '<a
href="#globalvars"><tt>constant</tt></a>'. Functions and aliases may not
have common linkage.</dd>
<dt><tt><b><a name="linkage_appending">appending</a></b></tt>: </dt>
<dd>"<tt>appending</tt>" linkage may only be applied to global variables of
pointer to array type. When two global variables with appending linkage
are linked together, the two global arrays are appended together. This is
the LLVM, typesafe, equivalent of having the system linker append together
"sections" with identical names when .o files are linked.</dd>
<dt><tt><b><a name="linkage_externweak">extern_weak</a></b></tt>: </dt>
<dd>The semantics of this linkage follow the ELF object file model: the symbol
is weak until linked, if not linked, the symbol becomes null instead of
being an undefined reference.</dd>
<dt><tt><b><a name="linkage_linkonce">linkonce_odr</a></b></tt>: </dt>
<dt><tt><b><a name="linkage_weak">weak_odr</a></b></tt>: </dt>
<dd>Some languages allow differing globals to be merged, such as two functions
with different semantics. Other languages, such as <tt>C++</tt>, ensure
that only equivalent globals are ever merged (the "one definition rule" -
"ODR"). Such languages can use the <tt>linkonce_odr</tt>
and <tt>weak_odr</tt> linkage types to indicate that the global will only
be merged with equivalent globals. These linkage types are otherwise the
same as their non-<tt>odr</tt> versions.</dd>
<dt><tt><b><a name="linkage_external">externally visible</a></b></tt>:</dt>
<dd>If none of the above identifiers are used, the global is externally
visible, meaning that it participates in linkage and can be used to
resolve external symbol references.</dd>
</dl>
<p>The next two types of linkage are targeted for Microsoft Windows platform
only. They are designed to support importing (exporting) symbols from (to)
DLLs (Dynamic Link Libraries).</p>
<dl>
<dt><tt><b><a name="linkage_dllimport">dllimport</a></b></tt>: </dt>
<dd>"<tt>dllimport</tt>" linkage causes the compiler to reference a function
or variable via a global pointer to a pointer that is set up by the DLL
exporting the symbol. On Microsoft Windows targets, the pointer name is
formed by combining <code>__imp_</code> and the function or variable
name.</dd>
<dt><tt><b><a name="linkage_dllexport">dllexport</a></b></tt>: </dt>
<dd>"<tt>dllexport</tt>" linkage causes the compiler to provide a global
pointer to a pointer in a DLL, so that it can be referenced with the
<tt>dllimport</tt> attribute. On Microsoft Windows targets, the pointer
name is formed by combining <code>__imp_</code> and the function or
variable name.</dd>
</dl>
<p>For example, since the "<tt>.LC0</tt>" variable is defined to be internal, if
another module defined a "<tt>.LC0</tt>" variable and was linked with this
one, one of the two would be renamed, preventing a collision. Since
"<tt>main</tt>" and "<tt>puts</tt>" are external (i.e., lacking any linkage
declarations), they are accessible outside of the current module.</p>
<p>It is illegal for a function <i>declaration</i> to have any linkage type
other than "externally visible", <tt>dllimport</tt>
or <tt>extern_weak</tt>.</p>
<p>Aliases can have only <tt>external</tt>, <tt>internal</tt>, <tt>weak</tt>
or <tt>weak_odr</tt> linkages.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="callingconv">Calling Conventions</a>
</div>
<div class="doc_text">
<p>LLVM <a href="#functionstructure">functions</a>, <a href="#i_call">calls</a>
and <a href="#i_invoke">invokes</a> can all have an optional calling
convention specified for the call. The calling convention of any pair of
dynamic caller/callee must match, or the behavior of the program is
undefined. The following calling conventions are supported by LLVM, and more
may be added in the future:</p>
<dl>
<dt><b>"<tt>ccc</tt>" - The C calling convention</b>:</dt>
<dd>This calling convention (the default if no other calling convention is
specified) matches the target C calling conventions. This calling
convention supports varargs function calls and tolerates some mismatch in
the declared prototype and implemented declaration of the function (as
does normal C).</dd>
<dt><b>"<tt>fastcc</tt>" - The fast calling convention</b>:</dt>
<dd>This calling convention attempts to make calls as fast as possible
(e.g. by passing things in registers). This calling convention allows the
target to use whatever tricks it wants to produce fast code for the
target, without having to conform to an externally specified ABI
(Application Binary Interface). Implementations of this convention should
allow arbitrary <a href="CodeGenerator.html#tailcallopt">tail call
optimization</a> to be supported. This calling convention does not
support varargs and requires the prototype of all callees to exactly match
the prototype of the function definition.</dd>
<dt><b>"<tt>coldcc</tt>" - The cold calling convention</b>:</dt>
<dd>This calling convention attempts to make code in the caller as efficient
as possible under the assumption that the call is not commonly executed.
As such, these calls often preserve all registers so that the call does
not break any live ranges in the caller side. This calling convention
does not support varargs and requires the prototype of all callees to
exactly match the prototype of the function definition.</dd>
<dt><b>"<tt>cc &lt;<em>n</em>&gt;</tt>" - Numbered convention</b>:</dt>
<dd>Any calling convention may be specified by number, allowing
target-specific calling conventions to be used. Target specific calling
conventions start at 64.</dd>
</dl>
<p>More calling conventions can be added/defined on an as-needed basis, to
support Pascal conventions or any other well-known target-independent
convention.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="visibility">Visibility Styles</a>
</div>
<div class="doc_text">
<p>All Global Variables and Functions have one of the following visibility
styles:</p>
<dl>
<dt><b>"<tt>default</tt>" - Default style</b>:</dt>
<dd>On targets that use the ELF object file format, default visibility means
that the declaration is visible to other modules and, in shared libraries,
means that the declared entity may be overridden. On Darwin, default
visibility means that the declaration is visible to other modules. Default
visibility corresponds to "external linkage" in the language.</dd>
<dt><b>"<tt>hidden</tt>" - Hidden style</b>:</dt>
<dd>Two declarations of an object with hidden visibility refer to the same
object if they are in the same shared object. Usually, hidden visibility
indicates that the symbol will not be placed into the dynamic symbol
table, so no other module (executable or shared library) can reference it
directly.</dd>
<dt><b>"<tt>protected</tt>" - Protected style</b>:</dt>
<dd>On ELF, protected visibility indicates that the symbol will be placed in
the dynamic symbol table, but that references within the defining module
will bind to the local symbol. That is, the symbol cannot be overridden by
another module.</dd>
</dl>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="namedtypes">Named Types</a>
</div>
<div class="doc_text">
<p>LLVM IR allows you to specify name aliases for certain types. This can make
it easier to read the IR and make the IR more condensed (particularly when
recursive types are involved). An example of a name specification is:</p>
<div class="doc_code">
<pre>
%mytype = type { %mytype*, i32 }
</pre>
</div>
<p>You may give a name to any <a href="#typesystem">type</a> except
"<a href="t_void">void</a>". Type name aliases may be used anywhere a type
is expected with the syntax "%mytype".</p>
<p>Note that type names are aliases for the structural type that they indicate,
and that you can therefore specify multiple names for the same type. This
often leads to confusing behavior when dumping out a .ll file. Since LLVM IR
uses structural typing, the name is not part of the type. When printing out
LLVM IR, the printer will pick <em>one name</em> to render all types of a
particular shape. This means that if you have code where two different
source types end up having the same LLVM type, that the dumper will sometimes
print the "wrong" or unexpected type. This is an important design point and
isn't going to change.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="globalvars">Global Variables</a>
</div>
<div class="doc_text">
<p>Global variables define regions of memory allocated at compilation time
instead of run-time. Global variables may optionally be initialized, may
have an explicit section to be placed in, and may have an optional explicit
alignment specified. A variable may be defined as "thread_local", which
means that it will not be shared by threads (each thread will have a
separated copy of the variable). A variable may be defined as a global
"constant," which indicates that the contents of the variable
will <b>never</b> be modified (enabling better optimization, allowing the
global data to be placed in the read-only section of an executable, etc).
Note that variables that need runtime initialization cannot be marked
"constant" as there is a store to the variable.</p>
<p>LLVM explicitly allows <em>declarations</em> of global variables to be marked
constant, even if the final definition of the global is not. This capability
can be used to enable slightly better optimization of the program, but
requires the language definition to guarantee that optimizations based on the
'constantness' are valid for the translation units that do not include the
definition.</p>
<p>As SSA values, global variables define pointer values that are in scope
(i.e. they dominate) all basic blocks in the program. Global variables
always define a pointer to their "content" type because they describe a
region of memory, and all memory objects in LLVM are accessed through
pointers.</p>
<p>A global variable may be declared to reside in a target-specific numbered
address space. For targets that support them, address spaces may affect how
optimizations are performed and/or what target instructions are used to
access the variable. The default address space is zero. The address space
qualifier must precede any other attributes.</p>
<p>LLVM allows an explicit section to be specified for globals. If the target
supports it, it will emit globals to the section specified.</p>
<p>An explicit alignment may be specified for a global. If not present, or if
the alignment is set to zero, the alignment of the global is set by the
target to whatever it feels convenient. If an explicit alignment is
specified, the global is forced to have at least that much alignment. All
alignments must be a power of 2.</p>
<p>For example, the following defines a global in a numbered address space with
an initializer, section, and alignment:</p>
<div class="doc_code">
<pre>
@G = addrspace(5) constant float 1.0, section "foo", align 4
</pre>
</div>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="functionstructure">Functions</a>
</div>
<div class="doc_text">
<p>LLVM function definitions consist of the "<tt>define</tt>" keyord, an
optional <a href="#linkage">linkage type</a>, an optional
<a href="#visibility">visibility style</a>, an optional
<a href="#callingconv">calling convention</a>, a return type, an optional
<a href="#paramattrs">parameter attribute</a> for the return type, a function
name, a (possibly empty) argument list (each with optional
<a href="#paramattrs">parameter attributes</a>), optional
<a href="#fnattrs">function attributes</a>, an optional section, an optional
alignment, an optional <a href="#gc">garbage collector name</a>, an opening
curly brace, a list of basic blocks, and a closing curly brace.</p>
<p>LLVM function declarations consist of the "<tt>declare</tt>" keyword, an
optional <a href="#linkage">linkage type</a>, an optional
<a href="#visibility">visibility style</a>, an optional
<a href="#callingconv">calling convention</a>, a return type, an optional
<a href="#paramattrs">parameter attribute</a> for the return type, a function
name, a possibly empty list of arguments, an optional alignment, and an
optional <a href="#gc">garbage collector name</a>.</p>
<p>A function definition contains a list of basic blocks, forming the CFG
(Control Flow Graph) for the function. Each basic block may optionally start
with a label (giving the basic block a symbol table entry), contains a list
of instructions, and ends with a <a href="#terminators">terminator</a>
instruction (such as a branch or function return).</p>
<p>The first basic block in a function is special in two ways: it is immediately
executed on entrance to the function, and it is not allowed to have
predecessor basic blocks (i.e. there can not be any branches to the entry
block of a function). Because the block can have no predecessors, it also
cannot have any <a href="#i_phi">PHI nodes</a>.</p>
<p>LLVM allows an explicit section to be specified for functions. If the target
supports it, it will emit functions to the section specified.</p>
<p>An explicit alignment may be specified for a function. If not present, or if
the alignment is set to zero, the alignment of the function is set by the
target to whatever it feels convenient. If an explicit alignment is
specified, the function is forced to have at least that much alignment. All
alignments must be a power of 2.</p>
<h5>Syntax:</h5>
<div class="doc_code">
<pre>
define [<a href="#linkage">linkage</a>] [<a href="#visibility">visibility</a>]
[<a href="#callingconv">cconv</a>] [<a href="#paramattrs">ret attrs</a>]
&lt;ResultType&gt; @&lt;FunctionName&gt; ([argument list])
[<a href="#fnattrs">fn Attrs</a>] [section "name"] [align N]
[<a href="#gc">gc</a>] { ... }
</pre>
</div>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="aliasstructure">Aliases</a>
</div>
<div class="doc_text">
<p>Aliases act as "second name" for the aliasee value (which can be either
function, global variable, another alias or bitcast of global value). Aliases
may have an optional <a href="#linkage">linkage type</a>, and an
optional <a href="#visibility">visibility style</a>.</p>
<h5>Syntax:</h5>
<div class="doc_code">
<pre>
@&lt;Name&gt; = alias [Linkage] [Visibility] &lt;AliaseeTy&gt; @&lt;Aliasee&gt;
</pre>
</div>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="paramattrs">Parameter Attributes</a></div>
<div class="doc_text">
<p>The return type and each parameter of a function type may have a set of
<i>parameter attributes</i> associated with them. Parameter attributes are
used to communicate additional information about the result or parameters of
a function. Parameter attributes are considered to be part of the function,
not of the function type, so functions with different parameter attributes
can have the same function type.</p>
<p>Parameter attributes are simple keywords that follow the type specified. If
multiple parameter attributes are needed, they are space separated. For
example:</p>
<div class="doc_code">
<pre>
declare i32 @printf(i8* noalias nocapture, ...)
declare i32 @atoi(i8 zeroext)
declare signext i8 @returns_signed_char()
</pre>
</div>
<p>Note that any attributes for the function result (<tt>nounwind</tt>,
<tt>readonly</tt>) come immediately after the argument list.</p>
<p>Currently, only the following parameter attributes are defined:</p>
<dl>
<dt><tt>zeroext</tt></dt>
<dd>This indicates to the code generator that the parameter or return value
should be zero-extended to a 32-bit value by the caller (for a parameter)
or the callee (for a return value).</dd>
<dt><tt>signext</tt></dt>
<dd>This indicates to the code generator that the parameter or return value
should be sign-extended to a 32-bit value by the caller (for a parameter)
or the callee (for a return value).</dd>
<dt><tt>inreg</tt></dt>
<dd>This indicates that this parameter or return value should be treated in a
special target-dependent fashion during while emitting code for a function
call or return (usually, by putting it in a register as opposed to memory,
though some targets use it to distinguish between two different kinds of
registers). Use of this attribute is target-specific.</dd>
<dt><tt><a name="byval">byval</a></tt></dt>
<dd>This indicates that the pointer parameter should really be passed by value
to the function. The attribute implies that a hidden copy of the pointee
is made between the caller and the callee, so the callee is unable to
modify the value in the callee. This attribute is only valid on LLVM
pointer arguments. It is generally used to pass structs and arrays by
value, but is also valid on pointers to scalars. The copy is considered
to belong to the caller not the callee (for example,
<tt><a href="#readonly">readonly</a></tt> functions should not write to
<tt>byval</tt> parameters). This is not a valid attribute for return
values. The byval attribute also supports specifying an alignment with
the align attribute. This has a target-specific effect on the code
generator that usually indicates a desired alignment for the synthesized
stack slot.</dd>
<dt><tt>sret</tt></dt>
<dd>This indicates that the pointer parameter specifies the address of a
structure that is the return value of the function in the source program.
This pointer must be guaranteed by the caller to be valid: loads and
stores to the structure may be assumed by the callee to not to trap. This
may only be applied to the first parameter. This is not a valid attribute
for return values. </dd>
<dt><tt>noalias</tt></dt>
<dd>This indicates that the pointer does not alias any global or any other
parameter. The caller is responsible for ensuring that this is the
case. On a function return value, <tt>noalias</tt> additionally indicates
that the pointer does not alias any other pointers visible to the
caller. For further details, please see the discussion of the NoAlias
response in
<a href="http://llvm.org/docs/AliasAnalysis.html#MustMayNo">alias
analysis</a>.</dd>
<dt><tt>nocapture</tt></dt>
<dd>This indicates that the callee does not make any copies of the pointer
that outlive the callee itself. This is not a valid attribute for return
values.</dd>
<dt><tt>nest</tt></dt>
<dd>This indicates that the pointer parameter can be excised using the
<a href="#int_trampoline">trampoline intrinsics</a>. This is not a valid
attribute for return values.</dd>
</dl>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="gc">Garbage Collector Names</a>
</div>
<div class="doc_text">
<p>Each function may specify a garbage collector name, which is simply a
string:</p>
<div class="doc_code">
<pre>
define void @f() gc "name" { ...
</pre>
</div>
<p>The compiler declares the supported values of <i>name</i>. Specifying a
collector which will cause the compiler to alter its output in order to
support the named garbage collection algorithm.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="fnattrs">Function Attributes</a>
</div>
<div class="doc_text">
<p>Function attributes are set to communicate additional information about a
function. Function attributes are considered to be part of the function, not
of the function type, so functions with different parameter attributes can
have the same function type.</p>
<p>Function attributes are simple keywords that follow the type specified. If
multiple attributes are needed, they are space separated. For example:</p>
<div class="doc_code">
<pre>
define void @f() noinline { ... }
define void @f() alwaysinline { ... }
define void @f() alwaysinline optsize { ... }
define void @f() optsize
</pre>
</div>
<dl>
<dt><tt>alwaysinline</tt></dt>
<dd>This attribute indicates that the inliner should attempt to inline this
function into callers whenever possible, ignoring any active inlining size
threshold for this caller.</dd>
<dt><tt>noinline</tt></dt>
<dd>This attribute indicates that the inliner should never inline this
function in any situation. This attribute may not be used together with
the <tt>alwaysinline</tt> attribute.</dd>
<dt><tt>optsize</tt></dt>
<dd>This attribute suggests that optimization passes and code generator passes
make choices that keep the code size of this function low, and otherwise
do optimizations specifically to reduce code size.</dd>
<dt><tt>noreturn</tt></dt>
<dd>This function attribute indicates that the function never returns
normally. This produces undefined behavior at runtime if the function
ever does dynamically return.</dd>
<dt><tt>nounwind</tt></dt>
<dd>This function attribute indicates that the function never returns with an
unwind or exceptional control flow. If the function does unwind, its
runtime behavior is undefined.</dd>
<dt><tt>readnone</tt></dt>
<dd>This attribute indicates that the function computes its result (or decides
to unwind an exception) based strictly on its arguments, without
dereferencing any pointer arguments or otherwise accessing any mutable
state (e.g. memory, control registers, etc) visible to caller functions.
It does not write through any pointer arguments
(including <tt><a href="#byval">byval</a></tt> arguments) and never
changes any state visible to callers. This means that it cannot unwind
exceptions by calling the <tt>C++</tt> exception throwing methods, but
could use the <tt>unwind</tt> instruction.</dd>
<dt><tt><a name="readonly">readonly</a></tt></dt>
<dd>This attribute indicates that the function does not write through any
pointer arguments (including <tt><a href="#byval">byval</a></tt>
arguments) or otherwise modify any state (e.g. memory, control registers,
etc) visible to caller functions. It may dereference pointer arguments
and read state that may be set in the caller. A readonly function always
returns the same value (or unwinds an exception identically) when called
with the same set of arguments and global state. It cannot unwind an
exception by calling the <tt>C++</tt> exception throwing methods, but may
use the <tt>unwind</tt> instruction.</dd>
<dt><tt><a name="ssp">ssp</a></tt></dt>
<dd>This attribute indicates that the function should emit a stack smashing
protector. It is in the form of a "canary"&mdash;a random value placed on
the stack before the local variables that's checked upon return from the
function to see if it has been overwritten. A heuristic is used to
determine if a function needs stack protectors or not.<br>
<br>
If a function that has an <tt>ssp</tt> attribute is inlined into a
function that doesn't have an <tt>ssp</tt> attribute, then the resulting
function will have an <tt>ssp</tt> attribute.</dd>
<dt><tt>sspreq</tt></dt>
<dd>This attribute indicates that the function should <em>always</em> emit a
stack smashing protector. This overrides
the <tt><a href="#ssp">ssp</a></tt> function attribute.<br>
<br>
If a function that has an <tt>sspreq</tt> attribute is inlined into a
function that doesn't have an <tt>sspreq</tt> attribute or which has
an <tt>ssp</tt> attribute, then the resulting function will have
an <tt>sspreq</tt> attribute.</dd>
<dt><tt>noredzone</tt></dt>
<dd>This attribute indicates that the code generator should not use a red
zone, even if the target-specific ABI normally permits it.</dd>
<dt><tt>noimplicitfloat</tt></dt>
<dd>This attributes disables implicit floating point instructions.</dd>
<dt><tt>naked</tt></dt>
<dd>This attribute disables prologue / epilogue emission for the function.
This can have very system-specific consequences.</dd>
</dl>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="moduleasm">Module-Level Inline Assembly</a>
</div>
<div class="doc_text">
<p>Modules may contain "module-level inline asm" blocks, which corresponds to
the GCC "file scope inline asm" blocks. These blocks are internally
concatenated by LLVM and treated as a single unit, but may be separated in
the <tt>.ll</tt> file if desired. The syntax is very simple:</p>
<div class="doc_code">
<pre>
module asm "inline asm code goes here"
module asm "more can go here"
</pre>
</div>
<p>The strings can contain any character by escaping non-printable characters.
The escape sequence used is simply "\xx" where "xx" is the two digit hex code
for the number.</p>
<p>The inline asm code is simply printed to the machine code .s file when
assembly code is generated.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="datalayout">Data Layout</a>
</div>
<div class="doc_text">
<p>A module may specify a target specific data layout string that specifies how
data is to be laid out in memory. The syntax for the data layout is
simply:</p>
<div class="doc_code">
<pre>
target datalayout = "<i>layout specification</i>"
</pre>
</div>
<p>The <i>layout specification</i> consists of a list of specifications
separated by the minus sign character ('-'). Each specification starts with
a letter and may include other information after the letter to define some
aspect of the data layout. The specifications accepted are as follows:</p>
<dl>
<dt><tt>E</tt></dt>
<dd>Specifies that the target lays out data in big-endian form. That is, the
bits with the most significance have the lowest address location.</dd>
<dt><tt>e</tt></dt>
<dd>Specifies that the target lays out data in little-endian form. That is,
the bits with the least significance have the lowest address
location.</dd>
<dt><tt>p:<i>size</i>:<i>abi</i>:<i>pref</i></tt></dt>
<dd>This specifies the <i>size</i> of a pointer and its <i>abi</i> and
<i>preferred</i> alignments. All sizes are in bits. Specifying
the <i>pref</i> alignment is optional. If omitted, the
preceding <tt>:</tt> should be omitted too.</dd>
<dt><tt>i<i>size</i>:<i>abi</i>:<i>pref</i></tt></dt>
<dd>This specifies the alignment for an integer type of a given bit
<i>size</i>. The value of <i>size</i> must be in the range [1,2^23).</dd>
<dt><tt>v<i>size</i>:<i>abi</i>:<i>pref</i></tt></dt>
<dd>This specifies the alignment for a vector type of a given bit
<i>size</i>.</dd>
<dt><tt>f<i>size</i>:<i>abi</i>:<i>pref</i></tt></dt>
<dd>This specifies the alignment for a floating point type of a given bit
<i>size</i>. The value of <i>size</i> must be either 32 (float) or 64
(double).</dd>
<dt><tt>a<i>size</i>:<i>abi</i>:<i>pref</i></tt></dt>
<dd>This specifies the alignment for an aggregate type of a given bit
<i>size</i>.</dd>
<dt><tt>s<i>size</i>:<i>abi</i>:<i>pref</i></tt></dt>
<dd>This specifies the alignment for a stack object of a given bit
<i>size</i>.</dd>
</dl>
<p>When constructing the data layout for a given target, LLVM starts with a
default set of specifications which are then (possibly) overriden by the
specifications in the <tt>datalayout</tt> keyword. The default specifications
are given in this list:</p>
<ul>
<li><tt>E</tt> - big endian</li>
<li><tt>p:32:64:64</tt> - 32-bit pointers with 64-bit alignment</li>
<li><tt>i1:8:8</tt> - i1 is 8-bit (byte) aligned</li>
<li><tt>i8:8:8</tt> - i8 is 8-bit (byte) aligned</li>
<li><tt>i16:16:16</tt> - i16 is 16-bit aligned</li>
<li><tt>i32:32:32</tt> - i32 is 32-bit aligned</li>
<li><tt>i64:32:64</tt> - i64 has ABI alignment of 32-bits but preferred
alignment of 64-bits</li>
<li><tt>f32:32:32</tt> - float is 32-bit aligned</li>
<li><tt>f64:64:64</tt> - double is 64-bit aligned</li>
<li><tt>v64:64:64</tt> - 64-bit vector is 64-bit aligned</li>
<li><tt>v128:128:128</tt> - 128-bit vector is 128-bit aligned</li>
<li><tt>a0:0:1</tt> - aggregates are 8-bit aligned</li>
<li><tt>s0:64:64</tt> - stack objects are 64-bit aligned</li>
</ul>
<p>When LLVM is determining the alignment for a given type, it uses the
following rules:</p>
<ol>
<li>If the type sought is an exact match for one of the specifications, that
specification is used.</li>
<li>If no match is found, and the type sought is an integer type, then the
smallest integer type that is larger than the bitwidth of the sought type
is used. If none of the specifications are larger than the bitwidth then
the the largest integer type is used. For example, given the default
specifications above, the i7 type will use the alignment of i8 (next
largest) while both i65 and i256 will use the alignment of i64 (largest
specified).</li>
<li>If no match is found, and the type sought is a vector type, then the
largest vector type that is smaller than the sought vector type will be
used as a fall back. This happens because &lt;128 x double&gt; can be
implemented in terms of 64 &lt;2 x double&gt;, for example.</li>
</ol>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="pointeraliasing">Pointer Aliasing Rules</a>
</div>
<div class="doc_text">
<p>Any memory access must be done through a pointer value associated
with an address range of the memory access, otherwise the behavior
is undefined. Pointer values are associated with address ranges
according to the following rules:</p>
<ul>
<li>A pointer value formed from a
<tt><a href="#i_getelementptr">getelementptr</a></tt> instruction
is associated with the addresses associated with the first operand
of the <tt>getelementptr</tt>.</li>
<li>An address of a global variable is associated with the address
range of the variable's storage.</li>
<li>The result value of an allocation instruction is associated with
the address range of the allocated storage.</li>
<li>A null pointer in the default address-space is associated with
no address.</li>
<li>A pointer value formed by an
<tt><a href="#i_inttoptr">inttoptr</a></tt> is associated with all
address ranges of all pointer values that contribute (directly or
indirectly) to the computation of the pointer's value.</li>
<li>The result value of a
<tt><a href="#i_bitcast">bitcast</a></tt> is associated with all
addresses associated with the operand of the <tt>bitcast</tt>.</li>
<li>An integer constant other than zero or a pointer value returned
from a function not defined within LLVM may be associated with address
ranges allocated through mechanisms other than those provided by
LLVM. Such ranges shall not overlap with any ranges of addresses
allocated by mechanisms provided by LLVM.</li>
</ul>
<p>LLVM IR does not associate types with memory. The result type of a
<tt><a href="#i_load">load</a></tt> merely indicates the size and
alignment of the memory from which to load, as well as the
interpretation of the value. The first operand of a
<tt><a href="#i_store">store</a></tt> similarly only indicates the size
and alignment of the store.</p>
<p>Consequently, type-based alias analysis, aka TBAA, aka
<tt>-fstrict-aliasing</tt>, is not applicable to general unadorned
LLVM IR. <a href="#metadata">Metadata</a> may be used to encode
additional information which specialized optimization passes may use
to implement type-based alias analysis.</p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="typesystem">Type System</a> </div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>The LLVM type system is one of the most important features of the
intermediate representation. Being typed enables a number of optimizations
to be performed on the intermediate representation directly, without having
to do extra analyses on the side before the transformation. A strong type
system makes it easier to read the generated code and enables novel analyses
and transformations that are not feasible to perform on normal three address
code representations.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"> <a name="t_classifications">Type
Classifications</a> </div>
<div class="doc_text">
<p>The types fall into a few useful classifications:</p>
<table border="1" cellspacing="0" cellpadding="4">
<tbody>
<tr><th>Classification</th><th>Types</th></tr>
<tr>
<td><a href="#t_integer">integer</a></td>
<td><tt>i1, i2, i3, ... i8, ... i16, ... i32, ... i64, ... </tt></td>
</tr>
<tr>
<td><a href="#t_floating">floating point</a></td>
<td><tt>float, double, x86_fp80, fp128, ppc_fp128</tt></td>
</tr>
<tr>
<td><a name="t_firstclass">first class</a></td>
<td><a href="#t_integer">integer</a>,
<a href="#t_floating">floating point</a>,
<a href="#t_pointer">pointer</a>,
<a href="#t_vector">vector</a>,
<a href="#t_struct">structure</a>,
<a href="#t_array">array</a>,
<a href="#t_label">label</a>,
<a href="#t_metadata">metadata</a>.
</td>
</tr>
<tr>
<td><a href="#t_primitive">primitive</a></td>
<td><a href="#t_label">label</a>,
<a href="#t_void">void</a>,
<a href="#t_floating">floating point</a>,
<a href="#t_metadata">metadata</a>.</td>
</tr>
<tr>
<td><a href="#t_derived">derived</a></td>
<td><a href="#t_integer">integer</a>,
<a href="#t_array">array</a>,
<a href="#t_function">function</a>,
<a href="#t_pointer">pointer</a>,
<a href="#t_struct">structure</a>,
<a href="#t_pstruct">packed structure</a>,
<a href="#t_vector">vector</a>,
<a href="#t_opaque">opaque</a>.
</td>
</tr>
</tbody>
</table>
<p>The <a href="#t_firstclass">first class</a> types are perhaps the most
important. Values of these types are the only ones which can be produced by
instructions, passed as arguments, or used as operands to instructions.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"> <a name="t_primitive">Primitive Types</a> </div>
<div class="doc_text">
<p>The primitive types are the fundamental building blocks of the LLVM
system.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_floating">Floating Point Types</a> </div>
<div class="doc_text">
<table>
<tbody>
<tr><th>Type</th><th>Description</th></tr>
<tr><td><tt>float</tt></td><td>32-bit floating point value</td></tr>
<tr><td><tt>double</tt></td><td>64-bit floating point value</td></tr>
<tr><td><tt>fp128</tt></td><td>128-bit floating point value (112-bit mantissa)</td></tr>
<tr><td><tt>x86_fp80</tt></td><td>80-bit floating point value (X87)</td></tr>
<tr><td><tt>ppc_fp128</tt></td><td>128-bit floating point value (two 64-bits)</td></tr>
</tbody>
</table>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_void">Void Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>The void type does not represent any value and has no size.</p>
<h5>Syntax:</h5>
<pre>
void
</pre>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_label">Label Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>The label type represents code labels.</p>
<h5>Syntax:</h5>
<pre>
label
</pre>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_metadata">Metadata Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>The metadata type represents embedded metadata. The only derived type that
may contain metadata is <tt>metadata*</tt> or a function type that returns or
takes metadata typed parameters, but not pointer to metadata types.</p>
<h5>Syntax:</h5>
<pre>
metadata
</pre>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"> <a name="t_derived">Derived Types</a> </div>
<div class="doc_text">
<p>The real power in LLVM comes from the derived types in the system. This is
what allows a programmer to represent arrays, functions, pointers, and other
useful types. Note that these derived types may be recursive: For example,
it is possible to have a two dimensional array.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_integer">Integer Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>The integer type is a very simple derived type that simply specifies an
arbitrary bit width for the integer type desired. Any bit width from 1 bit to
2^23-1 (about 8 million) can be specified.</p>
<h5>Syntax:</h5>
<pre>
iN
</pre>
<p>The number of bits the integer will occupy is specified by the <tt>N</tt>
value.</p>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>i1</tt></td>
<td class="left">a single-bit integer.</td>
</tr>
<tr class="layout">
<td class="left"><tt>i32</tt></td>
<td class="left">a 32-bit integer.</td>
</tr>
<tr class="layout">
<td class="left"><tt>i1942652</tt></td>
<td class="left">a really big integer of over 1 million bits.</td>
</tr>
</table>
<p>Note that the code generator does not yet support large integer types to be
used as function return types. The specific limit on how large a return type
the code generator can currently handle is target-dependent; currently it's
often 64 bits for 32-bit targets and 128 bits for 64-bit targets.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_array">Array Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>The array type is a very simple derived type that arranges elements
sequentially in memory. The array type requires a size (number of elements)
and an underlying data type.</p>
<h5>Syntax:</h5>
<pre>
[&lt;# elements&gt; x &lt;elementtype&gt;]
</pre>
<p>The number of elements is a constant integer value; <tt>elementtype</tt> may
be any type with a size.</p>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>[40 x i32]</tt></td>
<td class="left">Array of 40 32-bit integer values.</td>
</tr>
<tr class="layout">
<td class="left"><tt>[41 x i32]</tt></td>
<td class="left">Array of 41 32-bit integer values.</td>
</tr>
<tr class="layout">
<td class="left"><tt>[4 x i8]</tt></td>
<td class="left">Array of 4 8-bit integer values.</td>
</tr>
</table>
<p>Here are some examples of multidimensional arrays:</p>
<table class="layout">
<tr class="layout">
<td class="left"><tt>[3 x [4 x i32]]</tt></td>
<td class="left">3x4 array of 32-bit integer values.</td>
</tr>
<tr class="layout">
<td class="left"><tt>[12 x [10 x float]]</tt></td>
<td class="left">12x10 array of single precision floating point values.</td>
</tr>
<tr class="layout">
<td class="left"><tt>[2 x [3 x [4 x i16]]]</tt></td>
<td class="left">2x3x4 array of 16-bit integer values.</td>
</tr>
</table>
<p>Note that 'variable sized arrays' can be implemented in LLVM with a zero
length array. Normally, accesses past the end of an array are undefined in
LLVM (e.g. it is illegal to access the 5th element of a 3 element array). As
a special case, however, zero length arrays are recognized to be variable
length. This allows implementation of 'pascal style arrays' with the LLVM
type "<tt>{ i32, [0 x float]}</tt>", for example.</p>
<p>Note that the code generator does not yet support large aggregate types to be
used as function return types. The specific limit on how large an aggregate
return type the code generator can currently handle is target-dependent, and
also dependent on the aggregate element types.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_function">Function Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>The function type can be thought of as a function signature. It consists of
a return type and a list of formal parameter types. The return type of a
function type is a scalar type, a void type, or a struct type. If the return
type is a struct type then all struct elements must be of first class types,
and the struct must have at least one element.</p>
<h5>Syntax:</h5>
<pre>
&lt;returntype list&gt; (&lt;parameter list&gt;)
</pre>
<p>...where '<tt>&lt;parameter list&gt;</tt>' is a comma-separated list of type
specifiers. Optionally, the parameter list may include a type <tt>...</tt>,
which indicates that the function takes a variable number of arguments.
Variable argument functions can access their arguments with
the <a href="#int_varargs">variable argument handling intrinsic</a>
functions. '<tt>&lt;returntype list&gt;</tt>' is a comma-separated list of
<a href="#t_firstclass">first class</a> type specifiers.</p>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>i32 (i32)</tt></td>
<td class="left">function taking an <tt>i32</tt>, returning an <tt>i32</tt>
</td>
</tr><tr class="layout">
<td class="left"><tt>float&nbsp;(i16&nbsp;signext,&nbsp;i32&nbsp;*)&nbsp;*
</tt></td>
<td class="left"><a href="#t_pointer">Pointer</a> to a function that takes
an <tt>i16</tt> that should be sign extended and a
<a href="#t_pointer">pointer</a> to <tt>i32</tt>, returning
<tt>float</tt>.
</td>
</tr><tr class="layout">
<td class="left"><tt>i32 (i8*, ...)</tt></td>
<td class="left">A vararg function that takes at least one
<a href="#t_pointer">pointer</a> to <tt>i8 </tt> (char in C),
which returns an integer. This is the signature for <tt>printf</tt> in
LLVM.
</td>
</tr><tr class="layout">
<td class="left"><tt>{i32, i32} (i32)</tt></td>
<td class="left">A function taking an <tt>i32</tt>, returning two
<tt>i32</tt> values as an aggregate of type <tt>{ i32, i32 }</tt>
</td>
</tr>
</table>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_struct">Structure Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>The structure type is used to represent a collection of data members together
in memory. The packing of the field types is defined to match the ABI of the
underlying processor. The elements of a structure may be any type that has a
size.</p>
<p>Structures are accessed using '<tt><a href="#i_load">load</a></tt> and
'<tt><a href="#i_store">store</a></tt>' by getting a pointer to a field with
the '<tt><a href="#i_getelementptr">getelementptr</a></tt>' instruction.</p>
<h5>Syntax:</h5>
<pre>
{ &lt;type list&gt; }
</pre>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>{ i32, i32, i32 }</tt></td>
<td class="left">A triple of three <tt>i32</tt> values</td>
</tr><tr class="layout">
<td class="left"><tt>{&nbsp;float,&nbsp;i32&nbsp;(i32)&nbsp;*&nbsp;}</tt></td>
<td class="left">A pair, where the first element is a <tt>float</tt> and the
second element is a <a href="#t_pointer">pointer</a> to a
<a href="#t_function">function</a> that takes an <tt>i32</tt>, returning
an <tt>i32</tt>.</td>
</tr>
</table>
<p>Note that the code generator does not yet support large aggregate types to be
used as function return types. The specific limit on how large an aggregate
return type the code generator can currently handle is target-dependent, and
also dependent on the aggregate element types.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_pstruct">Packed Structure Type</a>
</div>
<div class="doc_text">
<h5>Overview:</h5>
<p>The packed structure type is used to represent a collection of data members
together in memory. There is no padding between fields. Further, the
alignment of a packed structure is 1 byte. The elements of a packed
structure may be any type that has a size.</p>
<p>Structures are accessed using '<tt><a href="#i_load">load</a></tt> and
'<tt><a href="#i_store">store</a></tt>' by getting a pointer to a field with
the '<tt><a href="#i_getelementptr">getelementptr</a></tt>' instruction.</p>
<h5>Syntax:</h5>
<pre>
&lt; { &lt;type list&gt; } &gt;
</pre>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>&lt; { i32, i32, i32 } &gt;</tt></td>
<td class="left">A triple of three <tt>i32</tt> values</td>
</tr><tr class="layout">
<td class="left">
<tt>&lt;&nbsp;{&nbsp;float,&nbsp;i32&nbsp;(i32)*&nbsp;}&nbsp;&gt;</tt></td>
<td class="left">A pair, where the first element is a <tt>float</tt> and the
second element is a <a href="#t_pointer">pointer</a> to a
<a href="#t_function">function</a> that takes an <tt>i32</tt>, returning
an <tt>i32</tt>.</td>
</tr>
</table>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_pointer">Pointer Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>As in many languages, the pointer type represents a pointer or reference to
another object, which must live in memory. Pointer types may have an optional
address space attribute defining the target-specific numbered address space
where the pointed-to object resides. The default address space is zero.</p>
<p>Note that LLVM does not permit pointers to void (<tt>void*</tt>) nor does it
permit pointers to labels (<tt>label*</tt>). Use <tt>i8*</tt> instead.</p>
<h5>Syntax:</h5>
<pre>
&lt;type&gt; *
</pre>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>[4 x i32]*</tt></td>
<td class="left">A <a href="#t_pointer">pointer</a> to <a
href="#t_array">array</a> of four <tt>i32</tt> values.</td>
</tr>
<tr class="layout">
<td class="left"><tt>i32 (i32 *) *</tt></td>
<td class="left"> A <a href="#t_pointer">pointer</a> to a <a
href="#t_function">function</a> that takes an <tt>i32*</tt>, returning an
<tt>i32</tt>.</td>
</tr>
<tr class="layout">
<td class="left"><tt>i32 addrspace(5)*</tt></td>
<td class="left">A <a href="#t_pointer">pointer</a> to an <tt>i32</tt> value
that resides in address space #5.</td>
</tr>
</table>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_vector">Vector Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>A vector type is a simple derived type that represents a vector of elements.
Vector types are used when multiple primitive data are operated in parallel
using a single instruction (SIMD). A vector type requires a size (number of
elements) and an underlying primitive data type. Vectors must have a power
of two length (1, 2, 4, 8, 16 ...). Vector types are considered
<a href="#t_firstclass">first class</a>.</p>
<h5>Syntax:</h5>
<pre>
&lt; &lt;# elements&gt; x &lt;elementtype&gt; &gt;
</pre>
<p>The number of elements is a constant integer value; elementtype may be any
integer or floating point type.</p>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>&lt;4 x i32&gt;</tt></td>
<td class="left">Vector of 4 32-bit integer values.</td>
</tr>
<tr class="layout">
<td class="left"><tt>&lt;8 x float&gt;</tt></td>
<td class="left">Vector of 8 32-bit floating-point values.</td>
</tr>
<tr class="layout">
<td class="left"><tt>&lt;2 x i64&gt;</tt></td>
<td class="left">Vector of 2 64-bit integer values.</td>
</tr>
</table>
<p>Note that the code generator does not yet support large vector types to be
used as function return types. The specific limit on how large a vector
return type codegen can currently handle is target-dependent; currently it's
often a few times longer than a hardware vector register.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="t_opaque">Opaque Type</a> </div>
<div class="doc_text">
<h5>Overview:</h5>
<p>Opaque types are used to represent unknown types in the system. This
corresponds (for example) to the C notion of a forward declared structure
type. In LLVM, opaque types can eventually be resolved to any type (not just
a structure type).</p>
<h5>Syntax:</h5>
<pre>
opaque
</pre>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>opaque</tt></td>
<td class="left">An opaque type.</td>
</tr>
</table>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="t_uprefs">Type Up-references</a>
</div>
<div class="doc_text">
<h5>Overview:</h5>
<p>An "up reference" allows you to refer to a lexically enclosing type without
requiring it to have a name. For instance, a structure declaration may
contain a pointer to any of the types it is lexically a member of. Example
of up references (with their equivalent as named type declarations)
include:</p>
<pre>
{ \2 * } %x = type { %x* }
{ \2 }* %y = type { %y }*
\1* %z = type %z*
</pre>
<p>An up reference is needed by the asmprinter for printing out cyclic types
when there is no declared name for a type in the cycle. Because the
asmprinter does not want to print out an infinite type string, it needs a
syntax to handle recursive types that have no names (all names are optional
in llvm IR).</p>
<h5>Syntax:</h5>
<pre>
\&lt;level&gt;
</pre>
<p>The level is the count of the lexical type that is being referred to.</p>
<h5>Examples:</h5>
<table class="layout">
<tr class="layout">
<td class="left"><tt>\1*</tt></td>
<td class="left">Self-referential pointer.</td>
</tr>
<tr class="layout">
<td class="left"><tt>{ { \3*, i8 }, i32 }</tt></td>
<td class="left">Recursive structure where the upref refers to the out-most
structure.</td>
</tr>
</table>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="constants">Constants</a> </div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>LLVM has several different basic types of constants. This section describes
them all and their syntax.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="simpleconstants">Simple Constants</a></div>
<div class="doc_text">
<dl>
<dt><b>Boolean constants</b></dt>
<dd>The two strings '<tt>true</tt>' and '<tt>false</tt>' are both valid
constants of the <tt><a href="#t_primitive">i1</a></tt> type.</dd>
<dt><b>Integer constants</b></dt>
<dd>Standard integers (such as '4') are constants of
the <a href="#t_integer">integer</a> type. Negative numbers may be used
with integer types.</dd>
<dt><b>Floating point constants</b></dt>
<dd>Floating point constants use standard decimal notation (e.g. 123.421),
exponential notation (e.g. 1.23421e+2), or a more precise hexadecimal
notation (see below). The assembler requires the exact decimal value of a
floating-point constant. For example, the assembler accepts 1.25 but
rejects 1.3 because 1.3 is a repeating decimal in binary. Floating point
constants must have a <a href="#t_floating">floating point</a> type. </dd>
<dt><b>Null pointer constants</b></dt>
<dd>The identifier '<tt>null</tt>' is recognized as a null pointer constant
and must be of <a href="#t_pointer">pointer type</a>.</dd>
</dl>
<p>The one non-intuitive notation for constants is the hexadecimal form of
floating point constants. For example, the form '<tt>double
0x432ff973cafa8000</tt>' is equivalent to (but harder to read than)
'<tt>double 4.5e+15</tt>'. The only time hexadecimal floating point
constants are required (and the only time that they are generated by the
disassembler) is when a floating point constant must be emitted but it cannot
be represented as a decimal floating point number in a reasonable number of
digits. For example, NaN's, infinities, and other special values are
represented in their IEEE hexadecimal format so that assembly and disassembly
do not cause any bits to change in the constants.</p>
<p>When using the hexadecimal form, constants of types float and double are
represented using the 16-digit form shown above (which matches the IEEE754
representation for double); float values must, however, be exactly
representable as IEE754 single precision. Hexadecimal format is always used
for long double, and there are three forms of long double. The 80-bit format
used by x86 is represented as <tt>0xK</tt> followed by 20 hexadecimal digits.
The 128-bit format used by PowerPC (two adjacent doubles) is represented
by <tt>0xM</tt> followed by 32 hexadecimal digits. The IEEE 128-bit format
is represented by <tt>0xL</tt> followed by 32 hexadecimal digits; no
currently supported target uses this format. Long doubles will only work if
they match the long double format on your target. All hexadecimal formats
are big-endian (sign bit at the left).</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="aggregateconstants"></a> <!-- old anchor -->
<a name="complexconstants">Complex Constants</a>
</div>
<div class="doc_text">
<p>Complex constants are a (potentially recursive) combination of simple
constants and smaller complex constants.</p>
<dl>
<dt><b>Structure constants</b></dt>
<dd>Structure constants are represented with notation similar to structure
type definitions (a comma separated list of elements, surrounded by braces
(<tt>{}</tt>)). For example: "<tt>{ i32 4, float 17.0, i32* @G }</tt>",
where "<tt>@G</tt>" is declared as "<tt>@G = external global i32</tt>".
Structure constants must have <a href="#t_struct">structure type</a>, and
the number and types of elements must match those specified by the
type.</dd>
<dt><b>Array constants</b></dt>
<dd>Array constants are represented with notation similar to array type
definitions (a comma separated list of elements, surrounded by square
brackets (<tt>[]</tt>)). For example: "<tt>[ i32 42, i32 11, i32 74
]</tt>". Array constants must have <a href="#t_array">array type</a>, and
the number and types of elements must match those specified by the
type.</dd>
<dt><b>Vector constants</b></dt>
<dd>Vector constants are represented with notation similar to vector type
definitions (a comma separated list of elements, surrounded by
less-than/greater-than's (<tt>&lt;&gt;</tt>)). For example: "<tt>&lt; i32
42, i32 11, i32 74, i32 100 &gt;</tt>". Vector constants must
have <a href="#t_vector">vector type</a>, and the number and types of
elements must match those specified by the type.</dd>
<dt><b>Zero initialization</b></dt>
<dd>The string '<tt>zeroinitializer</tt>' can be used to zero initialize a
value to zero of <em>any</em> type, including scalar and aggregate types.
This is often used to avoid having to print large zero initializers
(e.g. for large arrays) and is always exactly equivalent to using explicit
zero initializers.</dd>
<dt><b>Metadata node</b></dt>
<dd>A metadata node is a structure-like constant with
<a href="#t_metadata">metadata type</a>. For example: "<tt>metadata !{
i32 0, metadata !"test" }</tt>". Unlike other constants that are meant to
be interpreted as part of the instruction stream, metadata is a place to
attach additional information such as debug info.</dd>
</dl>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="globalconstants">Global Variable and Function Addresses</a>
</div>
<div class="doc_text">
<p>The addresses of <a href="#globalvars">global variables</a>
and <a href="#functionstructure">functions</a> are always implicitly valid
(link-time) constants. These constants are explicitly referenced when
the <a href="#identifiers">identifier for the global</a> is used and always
have <a href="#t_pointer">pointer</a> type. For example, the following is a
legal LLVM file:</p>
<div class="doc_code">
<pre>
@X = global i32 17
@Y = global i32 42
@Z = global [2 x i32*] [ i32* @X, i32* @Y ]
</pre>
</div>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="undefvalues">Undefined Values</a></div>
<div class="doc_text">
<p>The string '<tt>undef</tt>' is recognized as a type-less constant that has no
specific value. Undefined values may be of any type and be used anywhere a
constant is permitted.</p>
<p>Undefined values indicate to the compiler that the program is well defined no
matter what value is used, giving the compiler more freedom to optimize.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="constantexprs">Constant Expressions</a>
</div>
<div class="doc_text">
<p>Constant expressions are used to allow expressions involving other constants
to be used as constants. Constant expressions may be of
any <a href="#t_firstclass">first class</a> type and may involve any LLVM
operation that does not have side effects (e.g. load and call are not
supported). The following is the syntax for constant expressions:</p>
<dl>
<dt><b><tt>trunc ( CST to TYPE )</tt></b></dt>
<dd>Truncate a constant to another type. The bit size of CST must be larger
than the bit size of TYPE. Both types must be integers.</dd>
<dt><b><tt>zext ( CST to TYPE )</tt></b></dt>
<dd>Zero extend a constant to another type. The bit size of CST must be
smaller or equal to the bit size of TYPE. Both types must be
integers.</dd>
<dt><b><tt>sext ( CST to TYPE )</tt></b></dt>
<dd>Sign extend a constant to another type. The bit size of CST must be
smaller or equal to the bit size of TYPE. Both types must be
integers.</dd>
<dt><b><tt>fptrunc ( CST to TYPE )</tt></b></dt>
<dd>Truncate a floating point constant to another floating point type. The
size of CST must be larger than the size of TYPE. Both types must be
floating point.</dd>
<dt><b><tt>fpext ( CST to TYPE )</tt></b></dt>
<dd>Floating point extend a constant to another type. The size of CST must be
smaller or equal to the size of TYPE. Both types must be floating
point.</dd>
<dt><b><tt>fptoui ( CST to TYPE )</tt></b></dt>
<dd>Convert a floating point constant to the corresponding unsigned integer
constant. TYPE must be a scalar or vector integer type. CST must be of
scalar or vector floating point type. Both CST and TYPE must be scalars,
or vectors of the same number of elements. If the value won't fit in the
integer type, the results are undefined.</dd>
<dt><b><tt>fptosi ( CST to TYPE )</tt></b></dt>
<dd>Convert a floating point constant to the corresponding signed integer
constant. TYPE must be a scalar or vector integer type. CST must be of
scalar or vector floating point type. Both CST and TYPE must be scalars,
or vectors of the same number of elements. If the value won't fit in the
integer type, the results are undefined.</dd>
<dt><b><tt>uitofp ( CST to TYPE )</tt></b></dt>
<dd>Convert an unsigned integer constant to the corresponding floating point
constant. TYPE must be a scalar or vector floating point type. CST must be
of scalar or vector integer type. Both CST and TYPE must be scalars, or
vectors of the same number of elements. If the value won't fit in the
floating point type, the results are undefined.</dd>
<dt><b><tt>sitofp ( CST to TYPE )</tt></b></dt>
<dd>Convert a signed integer constant to the corresponding floating point
constant. TYPE must be a scalar or vector floating point type. CST must be
of scalar or vector integer type. Both CST and TYPE must be scalars, or
vectors of the same number of elements. If the value won't fit in the
floating point type, the results are undefined.</dd>
<dt><b><tt>ptrtoint ( CST to TYPE )</tt></b></dt>
<dd>Convert a pointer typed constant to the corresponding integer constant
<tt>TYPE</tt> must be an integer type. <tt>CST</tt> must be of pointer
type. The <tt>CST</tt> value is zero extended, truncated, or unchanged to
make it fit in <tt>TYPE</tt>.</dd>
<dt><b><tt>inttoptr ( CST to TYPE )</tt></b></dt>
<dd>Convert a integer constant to a pointer constant. TYPE must be a pointer
type. CST must be of integer type. The CST value is zero extended,
truncated, or unchanged to make it fit in a pointer size. This one is
<i>really</i> dangerous!</dd>
<dt><b><tt>bitcast ( CST to TYPE )</tt></b></dt>
<dd>Convert a constant, CST, to another TYPE. The constraints of the operands
are the same as those for the <a href="#i_bitcast">bitcast
instruction</a>.</dd>
<dt><b><tt>getelementptr ( CSTPTR, IDX0, IDX1, ... )</tt></b></dt>
<dt><b><tt>getelementptr inbounds ( CSTPTR, IDX0, IDX1, ... )</tt></b></dt>
<dd>Perform the <a href="#i_getelementptr">getelementptr operation</a> on
constants. As with the <a href="#i_getelementptr">getelementptr</a>
instruction, the index list may have zero or more indexes, which are
required to make sense for the type of "CSTPTR".</dd>
<dt><b><tt>select ( COND, VAL1, VAL2 )</tt></b></dt>
<dd>Perform the <a href="#i_select">select operation</a> on constants.</dd>
<dt><b><tt>icmp COND ( VAL1, VAL2 )</tt></b></dt>
<dd>Performs the <a href="#i_icmp">icmp operation</a> on constants.</dd>
<dt><b><tt>fcmp COND ( VAL1, VAL2 )</tt></b></dt>
<dd>Performs the <a href="#i_fcmp">fcmp operation</a> on constants.</dd>
<dt><b><tt>extractelement ( VAL, IDX )</tt></b></dt>
<dd>Perform the <a href="#i_extractelement">extractelement operation</a> on
constants.</dd>
<dt><b><tt>insertelement ( VAL, ELT, IDX )</tt></b></dt>
<dd>Perform the <a href="#i_insertelement">insertelement operation</a> on
constants.</dd>
<dt><b><tt>shufflevector ( VEC1, VEC2, IDXMASK )</tt></b></dt>
<dd>Perform the <a href="#i_shufflevector">shufflevector operation</a> on
constants.</dd>
<dt><b><tt>OPCODE ( LHS, RHS )</tt></b></dt>
<dd>Perform the specified operation of the LHS and RHS constants. OPCODE may
be any of the <a href="#binaryops">binary</a>
or <a href="#bitwiseops">bitwise binary</a> operations. The constraints
on operands are the same as those for the corresponding instruction
(e.g. no bitwise operations on floating point values are allowed).</dd>
</dl>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"><a name="metadata">Embedded Metadata</a>
</div>
<div class="doc_text">
<p>Embedded metadata provides a way to attach arbitrary data to the instruction
stream without affecting the behaviour of the program. There are two
metadata primitives, strings and nodes. All metadata has the
<tt>metadata</tt> type and is identified in syntax by a preceding exclamation
point ('<tt>!</tt>').</p>
<p>A metadata string is a string surrounded by double quotes. It can contain
any character by escaping non-printable characters with "\xx" where "xx" is
the two digit hex code. For example: "<tt>!"test\00"</tt>".</p>
<p>Metadata nodes are represented with notation similar to structure constants
(a comma separated list of elements, surrounded by braces and preceeded by an
exclamation point). For example: "<tt>!{ metadata !"test\00", i32
10}</tt>".</p>
<p>A metadata node will attempt to track changes to the values it holds. In the
event that a value is deleted, it will be replaced with a typeless
"<tt>null</tt>", such as "<tt>metadata !{null, i32 10}</tt>".</p>
<p>Optimizations may rely on metadata to provide additional information about
the program that isn't available in the instructions, or that isn't easily
computable. Similarly, the code generator may expect a certain metadata
format to be used to express debugging information.</p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="othervalues">Other Values</a> </div>
<!-- *********************************************************************** -->
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="inlineasm">Inline Assembler Expressions</a>
</div>
<div class="doc_text">
<p>LLVM supports inline assembler expressions (as opposed
to <a href="#moduleasm"> Module-Level Inline Assembly</a>) through the use of
a special value. This value represents the inline assembler as a string
(containing the instructions to emit), a list of operand constraints (stored
as a string), and a flag that indicates whether or not the inline asm
expression has side effects. An example inline assembler expression is:</p>
<div class="doc_code">
<pre>
i32 (i32) asm "bswap $0", "=r,r"
</pre>
</div>
<p>Inline assembler expressions may <b>only</b> be used as the callee operand of
a <a href="#i_call"><tt>call</tt> instruction</a>. Thus, typically we
have:</p>
<div class="doc_code">
<pre>
%X = call i32 asm "<a href="#int_bswap">bswap</a> $0", "=r,r"(i32 %Y)
</pre>
</div>
<p>Inline asms with side effects not visible in the constraint list must be
marked as having side effects. This is done through the use of the
'<tt>sideeffect</tt>' keyword, like so:</p>
<div class="doc_code">
<pre>
call void asm sideeffect "eieio", ""()
</pre>
</div>
<p>TODO: The format of the asm and constraints string still need to be
documented here. Constraints on what can be done (e.g. duplication, moving,
etc need to be documented). This is probably best done by reference to
another document that covers inline asm from a holistic perspective.</p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section">
<a name="intrinsic_globals">Intrinsic Global Variables</a>
</div>
<!-- *********************************************************************** -->
<p>LLVM has a number of "magic" global variables that contain data that affect
code generation or other IR semantics. These are documented here. All globals
of this sort should have a section specified as "<tt>llvm.metadata</tt>". This
section and all globals that start with "<tt>llvm.</tt>" are reserved for use
by LLVM.</p>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="intg_used">The '<tt>llvm.used</tt>' Global Variable</a>
</div>
<div class="doc_text">
<p>The <tt>@llvm.used</tt> global is an array with i8* element type which has <a
href="#linkage_appending">appending linkage</a>. This array contains a list of
pointers to global variables and functions which may optionally have a pointer
cast formed of bitcast or getelementptr. For example, a legal use of it is:</p>
<pre>
@X = global i8 4
@Y = global i32 123
@llvm.used = appending global [2 x i8*] [
i8* @X,
i8* bitcast (i32* @Y to i8*)
], section "llvm.metadata"
</pre>
<p>If a global variable appears in the <tt>@llvm.used</tt> list, then the
compiler, assembler, and linker are required to treat the symbol as if there is
a reference to the global that it cannot see. For example, if a variable has
internal linkage and no references other than that from the <tt>@llvm.used</tt>
list, it cannot be deleted. This is commonly used to represent references from
inline asms and other things the compiler cannot "see", and corresponds to
"attribute((used))" in GNU C.</p>
<p>On some targets, the code generator must emit a directive to the assembler or
object file to prevent the assembler and linker from molesting the symbol.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="intg_compiler_used">The '<tt>llvm.compiler.used</tt>' Global Variable</a>
</div>
<div class="doc_text">
<p>The <tt>@llvm.compiler.used</tt> directive is the same as the
<tt>@llvm.used</tt> directive, except that it only prevents the compiler from
touching the symbol. On targets that support it, this allows an intelligent
linker to optimize references to the symbol without being impeded as it would be
by <tt>@llvm.used</tt>.</p>
<p>This is a rare construct that should only be used in rare circumstances, and
should not be exposed to source languages.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="intg_global_ctors">The '<tt>llvm.global_ctors</tt>' Global Variable</a>
</div>
<div class="doc_text">
<p>TODO: Describe this.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection">
<a name="intg_global_dtors">The '<tt>llvm.global_dtors</tt>' Global Variable</a>
</div>
<div class="doc_text">
<p>TODO: Describe this.</p>
</div>
<!-- *********************************************************************** -->
<div class="doc_section"> <a name="instref">Instruction Reference</a> </div>
<!-- *********************************************************************** -->
<div class="doc_text">
<p>The LLVM instruction set consists of several different classifications of
instructions: <a href="#terminators">terminator
instructions</a>, <a href="#binaryops">binary instructions</a>,
<a href="#bitwiseops">bitwise binary instructions</a>,
<a href="#memoryops">memory instructions</a>, and
<a href="#otherops">other instructions</a>.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"> <a name="terminators">Terminator
Instructions</a> </div>
<div class="doc_text">
<p>As mentioned <a href="#functionstructure">previously</a>, every basic block
in a program ends with a "Terminator" instruction, which indicates which
block should be executed after the current block is finished. These
terminator instructions typically yield a '<tt>void</tt>' value: they produce
control flow, not values (the one exception being the
'<a href="#i_invoke"><tt>invoke</tt></a>' instruction).</p>
<p>There are six different terminator instructions: the
'<a href="#i_ret"><tt>ret</tt></a>' instruction, the
'<a href="#i_br"><tt>br</tt></a>' instruction, the
'<a href="#i_switch"><tt>switch</tt></a>' instruction, the
'<a href="#i_invoke"><tt>invoke</tt></a>' instruction, the
'<a href="#i_unwind"><tt>unwind</tt></a>' instruction, and the
'<a href="#i_unreachable"><tt>unreachable</tt></a>' instruction.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="i_ret">'<tt>ret</tt>'
Instruction</a> </div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
ret &lt;type&gt; &lt;value&gt; <i>; Return a value from a non-void function</i>
ret void <i>; Return from void function</i>
</pre>
<h5>Overview:</h5>
<p>The '<tt>ret</tt>' instruction is used to return control flow (and optionally
a value) from a function back to the caller.</p>
<p>There are two forms of the '<tt>ret</tt>' instruction: one that returns a
value and then causes control flow, and one that just causes control flow to
occur.</p>
<h5>Arguments:</h5>
<p>The '<tt>ret</tt>' instruction optionally accepts a single argument, the
return value. The type of the return value must be a
'<a href="#t_firstclass">first class</a>' type.</p>
<p>A function is not <a href="#wellformed">well formed</a> if it it has a
non-void return type and contains a '<tt>ret</tt>' instruction with no return
value or a return value with a type that does not match its type, or if it
has a void return type and contains a '<tt>ret</tt>' instruction with a
return value.</p>
<h5>Semantics:</h5>
<p>When the '<tt>ret</tt>' instruction is executed, control flow returns back to
the calling function's context. If the caller is a
"<a href="#i_call"><tt>call</tt></a>" instruction, execution continues at the
instruction after the call. If the caller was an
"<a href="#i_invoke"><tt>invoke</tt></a>" instruction, execution continues at
the beginning of the "normal" destination block. If the instruction returns
a value, that value shall set the call or invoke instruction's return
value.</p>
<h5>Example:</h5>
<pre>
ret i32 5 <i>; Return an integer value of 5</i>
ret void <i>; Return from a void function</i>
ret { i32, i8 } { i32 4, i8 2 } <i>; Return a struct of values 4 and 2</i>
</pre>
<p>Note that the code generator does not yet fully support large
return values. The specific sizes that are currently supported are
dependent on the target. For integers, on 32-bit targets the limit
is often 64 bits, and on 64-bit targets the limit is often 128 bits.
For aggregate types, the current limits are dependent on the element
types; for example targets are often limited to 2 total integer
elements and 2 total floating-point elements.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="i_br">'<tt>br</tt>' Instruction</a> </div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
br i1 &lt;cond&gt;, label &lt;iftrue&gt;, label &lt;iffalse&gt;<br> br label &lt;dest&gt; <i>; Unconditional branch</i>
</pre>
<h5>Overview:</h5>
<p>The '<tt>br</tt>' instruction is used to cause control flow to transfer to a
different basic block in the current function. There are two forms of this
instruction, corresponding to a conditional branch and an unconditional
branch.</p>
<h5>Arguments:</h5>
<p>The conditional branch form of the '<tt>br</tt>' instruction takes a single
'<tt>i1</tt>' value and two '<tt>label</tt>' values. The unconditional form
of the '<tt>br</tt>' instruction takes a single '<tt>label</tt>' value as a
target.</p>
<h5>Semantics:</h5>
<p>Upon execution of a conditional '<tt>br</tt>' instruction, the '<tt>i1</tt>'
argument is evaluated. If the value is <tt>true</tt>, control flows to the
'<tt>iftrue</tt>' <tt>label</tt> argument. If "cond" is <tt>false</tt>,
control flows to the '<tt>iffalse</tt>' <tt>label</tt> argument.</p>
<h5>Example:</h5>
<pre>
Test:
%cond = <a href="#i_icmp">icmp</a> eq i32 %a, %b
br i1 %cond, label %IfEqual, label %IfUnequal
IfEqual:
<a href="#i_ret">ret</a> i32 1
IfUnequal:
<a href="#i_ret">ret</a> i32 0
</pre>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
<a name="i_switch">'<tt>switch</tt>' Instruction</a>
</div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
switch &lt;intty&gt; &lt;value&gt;, label &lt;defaultdest&gt; [ &lt;intty&gt; &lt;val&gt;, label &lt;dest&gt; ... ]
</pre>
<h5>Overview:</h5>
<p>The '<tt>switch</tt>' instruction is used to transfer control flow to one of
several different places. It is a generalization of the '<tt>br</tt>'
instruction, allowing a branch to occur to one of many possible
destinations.</p>
<h5>Arguments:</h5>
<p>The '<tt>switch</tt>' instruction uses three parameters: an integer
comparison value '<tt>value</tt>', a default '<tt>label</tt>' destination,
and an array of pairs of comparison value constants and '<tt>label</tt>'s.
The table is not allowed to contain duplicate constant entries.</p>
<h5>Semantics:</h5>
<p>The <tt>switch</tt> instruction specifies a table of values and
destinations. When the '<tt>switch</tt>' instruction is executed, this table
is searched for the given value. If the value is found, control flow is
transfered to the corresponding destination; otherwise, control flow is
transfered to the default destination.</p>
<h5>Implementation:</h5>
<p>Depending on properties of the target machine and the particular
<tt>switch</tt> instruction, this instruction may be code generated in
different ways. For example, it could be generated as a series of chained
conditional branches or with a lookup table.</p>
<h5>Example:</h5>
<pre>
<i>; Emulate a conditional br instruction</i>
%Val = <a href="#i_zext">zext</a> i1 %value to i32
switch i32 %Val, label %truedest [ i32 0, label %falsedest ]
<i>; Emulate an unconditional br instruction</i>
switch i32 0, label %dest [ ]
<i>; Implement a jump table:</i>
switch i32 %val, label %otherwise [ i32 0, label %onzero
i32 1, label %onone
i32 2, label %ontwo ]
</pre>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
<a name="i_invoke">'<tt>invoke</tt>' Instruction</a>
</div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
&lt;result&gt; = invoke [<a href="#callingconv">cconv</a>] [<a href="#paramattrs">ret attrs</a>] &lt;ptr to function ty&gt; &lt;function ptr val&gt;(&lt;function args&gt;) [<a href="#fnattrs">fn attrs</a>]
to label &lt;normal label&gt; unwind label &lt;exception label&gt;
</pre>
<h5>Overview:</h5>
<p>The '<tt>invoke</tt>' instruction causes control to transfer to a specified
function, with the possibility of control flow transfer to either the
'<tt>normal</tt>' label or the '<tt>exception</tt>' label. If the callee
function returns with the "<tt><a href="#i_ret">ret</a></tt>" instruction,
control flow will return to the "normal" label. If the callee (or any
indirect callees) returns with the "<a href="#i_unwind"><tt>unwind</tt></a>"
instruction, control is interrupted and continued at the dynamically nearest
"exception" label.</p>
<h5>Arguments:</h5>
<p>This instruction requires several arguments:</p>
<ol>
<li>The optional "cconv" marker indicates which <a href="#callingconv">calling
convention</a> the call should use. If none is specified, the call
defaults to using C calling conventions.</li>
<li>The optional <a href="#paramattrs">Parameter Attributes</a> list for
return values. Only '<tt>zeroext</tt>', '<tt>signext</tt>', and
'<tt>inreg</tt>' attributes are valid here.</li>
<li>'<tt>ptr to function ty</tt>': shall be the signature of the pointer to
function value being invoked. In most cases, this is a direct function
invocation, but indirect <tt>invoke</tt>s are just as possible, branching
off an arbitrary pointer to function value.</li>
<li>'<tt>function ptr val</tt>': An LLVM value containing a pointer to a
function to be invoked. </li>
<li>'<tt>function args</tt>': argument list whose types match the function
signature argument types. If the function signature indicates the
function accepts a variable number of arguments, the extra arguments can
be specified.</li>
<li>'<tt>normal label</tt>': the label reached when the called function
executes a '<tt><a href="#i_ret">ret</a></tt>' instruction. </li>
<li>'<tt>exception label</tt>': the label reached when a callee returns with
the <a href="#i_unwind"><tt>unwind</tt></a> instruction. </li>
<li>The optional <a href="#fnattrs">function attributes</a> list. Only
'<tt>noreturn</tt>', '<tt>nounwind</tt>', '<tt>readonly</tt>' and
'<tt>readnone</tt>' attributes are valid here.</li>
</ol>
<h5>Semantics:</h5>
<p>This instruction is designed to operate as a standard
'<tt><a href="#i_call">call</a></tt>' instruction in most regards. The
primary difference is that it establishes an association with a label, which
is used by the runtime library to unwind the stack.</p>
<p>This instruction is used in languages with destructors to ensure that proper
cleanup is performed in the case of either a <tt>longjmp</tt> or a thrown
exception. Additionally, this is important for implementation of
'<tt>catch</tt>' clauses in high-level languages that support them.</p>
<p>For the purposes of the SSA form, the definition of the value returned by the
'<tt>invoke</tt>' instruction is deemed to occur on the edge from the current
block to the "normal" label. If the callee unwinds then no return value is
available.</p>
<h5>Example:</h5>
<pre>
%retval = invoke i32 @Test(i32 15) to label %Continue
unwind label %TestCleanup <i>; {i32}:retval set</i>
%retval = invoke <a href="#callingconv">coldcc</a> i32 %Testfnptr(i32 15) to label %Continue
unwind label %TestCleanup <i>; {i32}:retval set</i>
</pre>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="i_unwind">'<tt>unwind</tt>'
Instruction</a> </div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
unwind
</pre>
<h5>Overview:</h5>
<p>The '<tt>unwind</tt>' instruction unwinds the stack, continuing control flow
at the first callee in the dynamic call stack which used
an <a href="#i_invoke"><tt>invoke</tt></a> instruction to perform the call.
This is primarily used to implement exception handling.</p>
<h5>Semantics:</h5>
<p>The '<tt>unwind</tt>' instruction causes execution of the current function to
immediately halt. The dynamic call stack is then searched for the
first <a href="#i_invoke"><tt>invoke</tt></a> instruction on the call stack.
Once found, execution continues at the "exceptional" destination block
specified by the <tt>invoke</tt> instruction. If there is no <tt>invoke</tt>
instruction in the dynamic call chain, undefined behavior results.</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection"> <a name="i_unreachable">'<tt>unreachable</tt>'
Instruction</a> </div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
unreachable
</pre>
<h5>Overview:</h5>
<p>The '<tt>unreachable</tt>' instruction has no defined semantics. This
instruction is used to inform the optimizer that a particular portion of the
code is not reachable. This can be used to indicate that the code after a
no-return function cannot be reached, and other facts.</p>
<h5>Semantics:</h5>
<p>The '<tt>unreachable</tt>' instruction has no defined semantics.</p>
</div>
<!-- ======================================================================= -->
<div class="doc_subsection"> <a name="binaryops">Binary Operations</a> </div>
<div class="doc_text">
<p>Binary operators are used to do most of the computation in a program. They
require two operands of the same type, execute an operation on them, and
produce a single value. The operands might represent multiple data, as is
the case with the <a href="#t_vector">vector</a> data type. The result value
has the same type as its operands.</p>
<p>There are several different binary operators:</p>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
<a name="i_add">'<tt>add</tt>' Instruction</a>
</div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
&lt;result&gt; = add &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
&lt;result&gt; = add nuw &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
&lt;result&gt; = add nsw &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
&lt;result&gt; = add nuw nsw &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
</pre>
<h5>Overview:</h5>
<p>The '<tt>add</tt>' instruction returns the sum of its two operands.</p>
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>add</tt>' instruction must
be <a href="#t_integer">integer</a> or <a href="#t_vector">vector</a> of
integer values. Both arguments must have identical types.</p>
<h5>Semantics:</h5>
<p>The value produced is the integer sum of the two operands.</p>
<p>If the sum has unsigned overflow, the result returned is the mathematical
result modulo 2<sup>n</sup>, where n is the bit width of the result.</p>
<p>Because LLVM integers use a two's complement representation, this instruction
is appropriate for both signed and unsigned integers.</p>
<p><tt>nuw</tt> and <tt>nsw</tt> stand for &quot;No Unsigned Wrap&quot;
and &quot;No Signed Wrap&quot;, respectively. If the <tt>nuw</tt> and/or
<tt>nsw</tt> keywords are present, the result value of the <tt>add</tt>
is undefined if unsigned and/or signed overflow, respectively, occurs.</p>
<h5>Example:</h5>
<pre>
&lt;result&gt; = add i32 4, %var <i>; yields {i32}:result = 4 + %var</i>
</pre>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
<a name="i_fadd">'<tt>fadd</tt>' Instruction</a>
</div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
&lt;result&gt; = fadd &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
</pre>
<h5>Overview:</h5>
<p>The '<tt>fadd</tt>' instruction returns the sum of its two operands.</p>
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>fadd</tt>' instruction must be
<a href="#t_floating">floating point</a> or <a href="#t_vector">vector</a> of
floating point values. Both arguments must have identical types.</p>
<h5>Semantics:</h5>
<p>The value produced is the floating point sum of the two operands.</p>
<h5>Example:</h5>
<pre>
&lt;result&gt; = fadd float 4.0, %var <i>; yields {float}:result = 4.0 + %var</i>
</pre>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
<a name="i_sub">'<tt>sub</tt>' Instruction</a>
</div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
&lt;result&gt; = sub &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
&lt;result&gt; = sub nuw &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
&lt;result&gt; = sub nsw &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
&lt;result&gt; = sub nuw nsw &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
</pre>
<h5>Overview:</h5>
<p>The '<tt>sub</tt>' instruction returns the difference of its two
operands.</p>
<p>Note that the '<tt>sub</tt>' instruction is used to represent the
'<tt>neg</tt>' instruction present in most other intermediate
representations.</p>
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>sub</tt>' instruction must
be <a href="#t_integer">integer</a> or <a href="#t_vector">vector</a> of
integer values. Both arguments must have identical types.</p>
<h5>Semantics:</h5>
<p>The value produced is the integer difference of the two operands.</p>
<p>If the difference has unsigned overflow, the result returned is the
mathematical result modulo 2<sup>n</sup>, where n is the bit width of the
result.</p>
<p>Because LLVM integers use a two's complement representation, this instruction
is appropriate for both signed and unsigned integers.</p>
<p><tt>nuw</tt> and <tt>nsw</tt> stand for &quot;No Unsigned Wrap&quot;
and &quot;No Signed Wrap&quot;, respectively. If the <tt>nuw</tt> and/or
<tt>nsw</tt> keywords are present, the result value of the <tt>sub</tt>
is undefined if unsigned and/or signed overflow, respectively, occurs.</p>
<h5>Example:</h5>
<pre>
&lt;result&gt; = sub i32 4, %var <i>; yields {i32}:result = 4 - %var</i>
&lt;result&gt; = sub i32 0, %val <i>; yields {i32}:result = -%var</i>
</pre>
</div>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
<a name="i_fsub">'<tt>fsub</tt>' Instruction</a>
</div>
<div class="doc_text">
<h5>Syntax:</h5>
<pre>
&lt;result&gt; = fsub &lt;ty&gt; &lt;op1&gt;, &lt;op2&gt; <i>; yields {ty}:result</i>
</pre>
<h5>Overview:</h5>
<p>The '<tt>fsub</tt>' instruction returns the difference of its two
operands.</p>
<p>Note that the '<tt>fsub</tt>' instruction is used to represent the
'<tt>fneg</tt>' instruction present in most other intermediate
representations.</p>