Skip to content
LangRef.html 117 KiB
Newer Older
</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>

<pre>
  %X = global int 17
  %Y = global int 42
  %Z = global [2 x int*] [ int* %X, int* %Y ]
</pre>

</div>

<!-- ======================================================================= -->
Reid Spencer's avatar
Reid Spencer committed
<div class="doc_subsection"><a name="undefvalues">Undefined Values</a></div>
Reid Spencer's avatar
Reid Spencer committed
  <p>The string '<tt>undef</tt>' is recognized as a type-less constant that has 
John Criswell's avatar
John Criswell committed
  no specific value.  Undefined values may be of any type and be used anywhere 
Reid Spencer's avatar
Reid Spencer committed
  a constant is permitted.</p>
Reid Spencer's avatar
Reid Spencer committed
  <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
John Criswell's avatar
John Criswell committed
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>cast ( CST to TYPE )</tt></b></dt>

  <dd>Cast a constant to another type.</dd>

  <dt><b><tt>getelementptr ( 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>OPCODE ( LHS, RHS )</tt></b></dt>

Reid Spencer's avatar
Reid Spencer committed
  <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
John Criswell's avatar
John Criswell committed
  values are allowed).</dd>
Chris Lattner's avatar
Chris Lattner committed
<!-- *********************************************************************** -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_section"> <a name="instref">Instruction Reference</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<!-- *********************************************************************** -->
Chris Lattner's avatar
Chris Lattner committed
<p>The LLVM instruction set consists of several different
classifications of instructions: <a href="#terminators">terminator
John Criswell's avatar
John Criswell committed
instructions</a>, <a href="#binaryops">binary instructions</a>,
<a href="#bitwiseops">bitwise binary instructions</a>, <a
Chris Lattner's avatar
Chris Lattner committed
 href="#memoryops">memory instructions</a>, and <a href="#otherops">other
instructions</a>.</p>
Chris Lattner's avatar
Chris Lattner committed
<!-- ======================================================================= -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsection"> <a name="terminators">Terminator
Instructions</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<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
Chris Lattner's avatar
Chris Lattner committed
 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>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_ret">'<tt>ret</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<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>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>ret</tt>' instruction is used to return control flow (and a
John Criswell's avatar
John Criswell committed
value) from a function back to the caller.</p>
John Criswell's avatar
John Criswell committed
<p>There are two forms of the '<tt>ret</tt>' instruction: one that
Chris Lattner's avatar
Chris Lattner committed
returns a value and then causes control flow, and one that just causes
control flow to occur.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>ret</tt>' instruction may return any '<a
 href="#t_firstclass">first class</a>' type.  Notice that a function is
not <a href="#wellformed">well formed</a> if there exists a '<tt>ret</tt>'
instruction inside of the function that returns a value that does not
match the return type of the function.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<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
John Criswell's avatar
John Criswell committed
 href="#i_call"><tt>call</tt></a>" instruction, execution continues at
Chris Lattner's avatar
Chris Lattner committed
the instruction after the call.  If the caller was an "<a
 href="#i_invoke"><tt>invoke</tt></a>" instruction, execution continues
John Criswell's avatar
John Criswell committed
at the beginning of the "normal" destination block.  If the instruction
Chris Lattner's avatar
Chris Lattner committed
returns a value, that value shall set the call or invoke instruction's
return value.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  ret int 5                       <i>; Return an integer value of 5</i>
  ret void                        <i>; Return from a void function</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_br">'<tt>br</tt>' Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  br bool &lt;cond&gt;, label &lt;iftrue&gt;, label &lt;iffalse&gt;<br>  br label &lt;dest&gt;          <i>; Unconditional branch</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<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>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The conditional branch form of the '<tt>br</tt>' instruction takes a
single '<tt>bool</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>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>Upon execution of a conditional '<tt>br</tt>' instruction, the '<tt>bool</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>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>Test:<br>  %cond = <a href="#i_setcc">seteq</a> int %a, %b<br>  br bool %cond, label %IfEqual, label %IfUnequal<br>IfEqual:<br>  <a
 href="#i_ret">ret</a> int 1<br>IfUnequal:<br>  <a href="#i_ret">ret</a> int 0<br></pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
   <a name="i_switch">'<tt>switch</tt>' Instruction</a>
</div>

Chris Lattner's avatar
Chris Lattner committed
<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>

Chris Lattner's avatar
Chris Lattner committed
<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>
Chris Lattner's avatar
Chris Lattner committed
<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>

Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<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>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>

<pre>
 <i>; Emulate a conditional br instruction</i>
 %Val = <a href="#i_cast">cast</a> bool %value to int
 switch int %Val, label %truedest [int 0, label %falsedest ]

 <i>; Emulate an unconditional br instruction</i>
 switch uint 0, label %dest [ ]

 <i>; Implement a jump table:</i>
 switch uint %val, label %otherwise [ uint 0, label %onzero 
                                      uint 1, label %onone 
                                      uint 2, label %ontwo ]
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
  <a name="i_invoke">'<tt>invoke</tt>' Instruction</a>
</div>

Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>

<pre>
  &lt;result&gt; = invoke [<a href="#callingconv">cconv</a>] &lt;ptr to function ty&gt; %&lt;function ptr val&gt;(&lt;function args&gt;) 
                to label &lt;normal label&gt; except label &lt;exception label&gt;
</pre>


<p>The '<tt>invoke</tt>' instruction causes control to transfer to a specified
function, with the possibility of control flow transfer to either the
John Criswell's avatar
John Criswell committed
'<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
John Criswell's avatar
John Criswell committed
href="#i_unwind"><tt>unwind</tt></a>" instruction, control is interrupted and
continued at the dynamically nearest "exception" label.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>This instruction requires several arguments:</p>
Chris Lattner's avatar
Chris Lattner committed
<ol>
John Criswell's avatar
John Criswell committed
    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>'<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>

Chris Lattner's avatar
Chris Lattner committed
</ol>
Chris Lattner's avatar
Chris Lattner committed
<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>

Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
<pre>
  %retval = invoke int %Test(int 15)             to label %Continue
              except label %TestCleanup     <i>; {int}:retval set</i>
  %retval = invoke <a href="#callingconv">coldcc</a> int %Test(int 15)             to label %Continue
              except label %TestCleanup     <i>; {int}:retval set</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_unwind">'<tt>unwind</tt>'
Instruction</a> </div>

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


<p>The '<tt>unwind</tt>' intrinsic 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 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>



Chris Lattner's avatar
Chris Lattner committed
<!-- ======================================================================= -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsection"> <a name="binaryops">Binary Operations</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<p>Binary operators are used to do most of the computation in a
program.  They require two operands, 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_packed">packed</a> data type. 
The result value of a binary operator is not
Chris Lattner's avatar
Chris Lattner committed
necessarily the same type as its operands.</p>
<p>There are several different binary operators:</p>
</div>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_add">'<tt>add</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = add &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
<p>The '<tt>add</tt>' instruction returns the sum of its two operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>add</tt>' instruction must be either <a
 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a> values.
 This instruction can also take <a href="#t_packed">packed</a> versions of the values.
Both arguments must have identical types.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
<p>The value produced is the integer or floating point sum of the two
operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = add int 4, %var          <i>; yields {int}:result = 4 + %var</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_sub">'<tt>sub</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = sub &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
<p>The '<tt>sub</tt>' instruction returns the difference of its two
operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<p>Note that the '<tt>sub</tt>' instruction is used to represent the '<tt>neg</tt>'
instruction present in most other intermediate representations.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>sub</tt>' instruction must be either <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
values. 
This instruction can also take <a href="#t_packed">packed</a> versions of the values.
Both arguments must have identical types.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The value produced is the integer or floating point difference of
the two operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = sub int 4, %var          <i>; yields {int}:result = 4 - %var</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = sub int 0, %val          <i>; yields {int}:result = -%var</i>
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_mul">'<tt>mul</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = mul &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The  '<tt>mul</tt>' instruction returns the product of its two
operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>mul</tt>' instruction must be either <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
values. 
This instruction can also take <a href="#t_packed">packed</a> versions of the values.
Both arguments must have identical types.</p>
Chris Lattner's avatar
Chris Lattner committed
<p>The value produced is the integer or floating point product of the
two operands.</p>
<p>There is no signed vs unsigned multiplication.  The appropriate
action is taken based on the type of the operand.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = mul int 4, %var          <i>; yields {int}:result = 4 * %var</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_div">'<tt>div</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = div &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>div</tt>' instruction returns the quotient of its two
operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>div</tt>' instruction must be either <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
values. 
This instruction can also take <a href="#t_packed">packed</a> versions of the values.
Both arguments must have identical types.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The value produced is the integer or floating point quotient of the
two operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = div int 4, %var          <i>; yields {int}:result = 4 / %var</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_rem">'<tt>rem</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = rem &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>rem</tt>' instruction returns the remainder from the
division of its two operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>rem</tt>' instruction must be either <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integer">integer</a> or <a href="#t_floating">floating point</a>
values. 
This instruction can also take <a href="#t_packed">packed</a> versions of the values.
Both arguments must have identical types.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>This returns the <i>remainder</i> of a division (where the result
has the same sign as the divisor), not the <i>modulus</i> (where the
result has the same sign as the dividend) of a value.  For more
information about the difference, see: <a
 href="http://mathforum.org/dr.math/problems/anne.4.28.99.html">The
Math Forum</a>.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = rem int 4, %var          <i>; yields {int}:result = 4 % %var</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_setcc">'<tt>set<i>cc</i></tt>'
Instructions</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = seteq &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = setne &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
  &lt;result&gt; = setlt &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
  &lt;result&gt; = setgt &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
  &lt;result&gt; = setle &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
  &lt;result&gt; = setge &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {bool}:result</i>
</pre>
Chris Lattner's avatar
Chris Lattner committed
<h5>Overview:</h5>
<p>The '<tt>set<i>cc</i></tt>' family of instructions returns a boolean
value based on a comparison of their two operands.</p>
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>set<i>cc</i></tt>' instructions must
be of <a href="#t_firstclass">first class</a> type (it is not possible
to compare '<tt>label</tt>'s, '<tt>array</tt>'s, '<tt>structure</tt>'
or '<tt>void</tt>' values, etc...).  Both arguments must have identical
types.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>seteq</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
value if both operands are equal.<br>
The '<tt>setne</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
value if both operands are unequal.<br>
The '<tt>setlt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
value if the first operand is less than the second operand.<br>
The '<tt>setgt</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
value if the first operand is greater than the second operand.<br>
The '<tt>setle</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
value if the first operand is less than or equal to the second operand.<br>
The '<tt>setge</tt>' instruction yields a <tt>true</tt> '<tt>bool</tt>'
value if the first operand is greater than or equal to the second
operand.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = seteq int   4, 5        <i>; yields {bool}:result = false</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = setne float 4, 5        <i>; yields {bool}:result = true</i>
  &lt;result&gt; = setlt uint  4, 5        <i>; yields {bool}:result = true</i>
  &lt;result&gt; = setgt sbyte 4, 5        <i>; yields {bool}:result = false</i>
  &lt;result&gt; = setle sbyte 4, 5        <i>; yields {bool}:result = true</i>
  &lt;result&gt; = setge sbyte 4, 5        <i>; yields {bool}:result = false</i>
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- ======================================================================= -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsection"> <a name="bitwiseops">Bitwise Binary
Operations</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<p>Bitwise binary operators are used to do various forms of
bit-twiddling in a program.  They are generally very efficient
instructions and can commonly be strength reduced from other
Chris Lattner's avatar
Chris Lattner committed
instructions.  They require two operands, execute an operation on them,
and produce a single value.  The resulting value of the bitwise binary
operators is always the same type as its first operand.</p>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_and">'<tt>and</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = and &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>and</tt>' instruction returns the bitwise logical and of
its two operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>and</tt>' instruction must be <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integral">integral</a> values.  Both arguments must have
identical types.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
<p>The truth table used for the '<tt>and</tt>' instruction is:</p>
Chris Lattner's avatar
Chris Lattner committed
<p> </p>
<div style="align: center">
<table border="1" cellspacing="0" cellpadding="4">
Chris Lattner's avatar
Chris Lattner committed
  <tbody>
    <tr>
      <td>In0</td>
      <td>In1</td>
      <td>Out</td>
    </tr>
    <tr>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>0</td>
      <td>1</td>
      <td>0</td>
    </tr>
    <tr>
      <td>1</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>1</td>
    </tr>
  </tbody>
</table>
</div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = and int 4, %var         <i>; yields {int}:result = 4 &amp; %var</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = and int 15, 40          <i>; yields {int}:result = 8</i>
  &lt;result&gt; = and int 4, 8            <i>; yields {int}:result = 0</i>
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_or">'<tt>or</tt>' Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = or &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<h5>Overview:</h5>
<p>The '<tt>or</tt>' instruction returns the bitwise logical inclusive
or of its two operands.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>or</tt>' instruction must be <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integral">integral</a> values.  Both arguments must have
identical types.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
<p>The truth table used for the '<tt>or</tt>' instruction is:</p>
Chris Lattner's avatar
Chris Lattner committed
<p> </p>
<div style="align: center">
Chris Lattner's avatar
Chris Lattner committed
<table border="1" cellspacing="0" cellpadding="4">
  <tbody>
    <tr>
      <td>In0</td>
      <td>In1</td>
      <td>Out</td>
    </tr>
    <tr>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>0</td>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <td>1</td>
      <td>0</td>
      <td>1</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>1</td>
    </tr>
  </tbody>
</table>
</div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = or int 4, %var         <i>; yields {int}:result = 4 | %var</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = or int 15, 40          <i>; yields {int}:result = 47</i>
  &lt;result&gt; = or int 4, 8            <i>; yields {int}:result = 12</i>
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_xor">'<tt>xor</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = xor &lt;ty&gt; &lt;var1&gt;, &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>xor</tt>' instruction returns the bitwise logical exclusive
or of its two operands.  The <tt>xor</tt> is used to implement the
"one's complement" operation, which is the "~" operator in C.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The two arguments to the '<tt>xor</tt>' instruction must be <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integral">integral</a> values.  Both arguments must have
identical types.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
<p>The truth table used for the '<tt>xor</tt>' instruction is:</p>
Chris Lattner's avatar
Chris Lattner committed
<p> </p>
<div style="align: center">
Chris Lattner's avatar
Chris Lattner committed
<table border="1" cellspacing="0" cellpadding="4">
  <tbody>
    <tr>
      <td>In0</td>
      <td>In1</td>
      <td>Out</td>
    </tr>
    <tr>
      <td>0</td>
      <td>0</td>
      <td>0</td>
    </tr>
    <tr>
      <td>0</td>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <td>1</td>
      <td>0</td>
      <td>1</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
</div>
Chris Lattner's avatar
Chris Lattner committed
<p> </p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = xor int 4, %var         <i>; yields {int}:result = 4 ^ %var</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = xor int 15, 40          <i>; yields {int}:result = 39</i>
  &lt;result&gt; = xor int 4, 8            <i>; yields {int}:result = 12</i>
  &lt;result&gt; = xor int %V, -1          <i>; yields {int}:result = ~%V</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_shl">'<tt>shl</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = shl &lt;ty&gt; &lt;var1&gt;, ubyte &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>shl</tt>' instruction returns the first operand shifted to
the left a specified number of bits.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The first argument to the '<tt>shl</tt>' instruction must be an <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integer">integer</a> type.  The second argument must be an '<tt>ubyte</tt>'
type.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
<p>The value produced is <tt>var1</tt> * 2<sup><tt>var2</tt></sup>.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = shl int 4, ubyte %var   <i>; yields {int}:result = 4 &lt;&lt; %var</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = shl int 4, ubyte 2      <i>; yields {int}:result = 16</i>
  &lt;result&gt; = shl int 1, ubyte 10     <i>; yields {int}:result = 1024</i>
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_shr">'<tt>shr</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = shr &lt;ty&gt; &lt;var1&gt;, ubyte &lt;var2&gt;   <i>; yields {ty}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>shr</tt>' instruction returns the first operand shifted to
the right a specified number of bits.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The first argument to the '<tt>shr</tt>' instruction must be an <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_integer">integer</a> type.  The second argument must be an '<tt>ubyte</tt>'
type.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>If the first argument is a <a href="#t_signed">signed</a> type, the
most significant bit is duplicated in the newly free'd bit positions. 
If the first argument is unsigned, zero bits shall fill the empty
positions.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = shr int 4, ubyte %var   <i>; yields {int}:result = 4 &gt;&gt; %var</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = shr uint 4, ubyte 1     <i>; yields {uint}:result = 2</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = shr int 4, ubyte 2      <i>; yields {int}:result = 1</i>
Chris Lattner's avatar
Chris Lattner committed
  &lt;result&gt; = shr sbyte 4, ubyte 3    <i>; yields {sbyte}:result = 0</i>
  &lt;result&gt; = shr sbyte -2, ubyte 1   <i>; yields {sbyte}:result = -1</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- ======================================================================= -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsection"> <a name="memoryops">Memory Access
Operations</a></div>
Chris Lattner's avatar
Chris Lattner committed
<p>A key design point of an SSA-based representation is how it
represents memory.  In LLVM, no memory locations are in SSA form, which
makes things very simple.  This section describes how to read, write,
allocate, and free memory in LLVM.</p>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_malloc">'<tt>malloc</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = malloc &lt;type&gt;, uint &lt;NumElements&gt;     <i>; yields {type*}:result</i>
  &lt;result&gt; = malloc &lt;type&gt;                         <i>; yields {type*}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>malloc</tt>' instruction allocates memory from the system
heap and returns a pointer to it.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
John Criswell's avatar
John Criswell committed
<p>The '<tt>malloc</tt>' instruction allocates <tt>sizeof(&lt;type&gt;)*NumElements</tt>
bytes of memory from the operating system and returns a pointer of the
Chris Lattner's avatar
Chris Lattner committed
appropriate type to the program.  The second form of the instruction is
a shorter version of the first instruction that defaults to allocating
one element.</p>
<p>'<tt>type</tt>' must be a sized type.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>Memory is allocated using the system "<tt>malloc</tt>" function, and
a pointer is returned.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  %array  = malloc [4 x ubyte ]                    <i>; yields {[%4 x ubyte]*}:array</i>
Chris Lattner's avatar
Chris Lattner committed
  %size   = <a
 href="#i_add">add</a> uint 2, 2                          <i>; yields {uint}:size = uint 4</i>
  %array1 = malloc ubyte, uint 4                   <i>; yields {ubyte*}:array1</i>
  %array2 = malloc [12 x ubyte], uint %size        <i>; yields {[12 x ubyte]*}:array2</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_free">'<tt>free</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  free &lt;type&gt; &lt;value&gt;                              <i>; yields {void}</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>free</tt>' instruction returns memory back to the unused
John Criswell's avatar
John Criswell committed
memory heap to be reallocated in the future.</p>
Chris Lattner's avatar
Chris Lattner committed
<p> </p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>'<tt>value</tt>' shall be a pointer value that points to a value
that was allocated with the '<tt><a href="#i_malloc">malloc</a></tt>'
instruction.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
<p>Access to the memory pointed to by the pointer is no longer defined
Chris Lattner's avatar
Chris Lattner committed
after this instruction executes.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  %array  = <a href="#i_malloc">malloc</a> [4 x ubyte]                    <i>; yields {[4 x ubyte]*}:array</i>
Chris Lattner's avatar
Chris Lattner committed
            free   [4 x ubyte]* %array
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_alloca">'<tt>alloca</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = alloca &lt;type&gt;, uint &lt;NumElements&gt;  <i>; yields {type*}:result</i>
  &lt;result&gt; = alloca &lt;type&gt;                      <i>; yields {type*}:result</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>The '<tt>alloca</tt>' instruction allocates memory on the current
stack frame of the procedure that is live until the current function
returns to its caller.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
<p>The '<tt>alloca</tt>' instruction allocates <tt>sizeof(&lt;type&gt;)*NumElements</tt>
Chris Lattner's avatar
Chris Lattner committed
bytes of memory on the runtime stack, returning a pointer of the
appropriate type to the program.  The second form of the instruction is
a shorter version of the first that defaults to allocating one element.</p>
<p>'<tt>type</tt>' may be any sized type.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>
John Criswell's avatar
John Criswell committed
<p>Memory is allocated; a pointer is returned.  '<tt>alloca</tt>'d
Chris Lattner's avatar
Chris Lattner committed
memory is automatically released when the function returns.  The '<tt>alloca</tt>'
instruction is commonly used to represent automatic variables that must
have an address available.  When the function returns (either with the <tt><a
 href="#i_ret">ret</a></tt> or <tt><a href="#i_unwind">unwind</a></tt>
instructions), the memory is reclaimed.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  %ptr = alloca int                              <i>; yields {int*}:ptr</i>
  %ptr = alloca int, uint 4                      <i>; yields {int*}:ptr</i>
</pre>
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_load">'<tt>load</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<pre>  &lt;result&gt; = load &lt;ty&gt;* &lt;pointer&gt;<br>  &lt;result&gt; = volatile load &lt;ty&gt;* &lt;pointer&gt;<br></pre>
<p>The '<tt>load</tt>' instruction is used to read from memory.</p>
Chris Lattner's avatar
Chris Lattner committed
<p>The argument to the '<tt>load</tt>' instruction specifies the memory
address to load from.  The pointer must point to a <a
Chris Lattner's avatar
Chris Lattner committed
 href="#t_firstclass">first class</a> type.  If the <tt>load</tt> is
Chris Lattner's avatar
Chris Lattner committed
marked as <tt>volatile</tt> then the optimizer is not allowed to modify
the number or order of execution of this <tt>load</tt> with other
volatile <tt>load</tt> and <tt><a href="#i_store">store</a></tt>
instructions. </p>
<p>The location of memory pointed to is loaded.</p>
<h5>Examples:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  %ptr = <a href="#i_alloca">alloca</a> int                               <i>; yields {int*}:ptr</i>
  <a
 href="#i_store">store</a> int 3, int* %ptr                          <i>; yields {void}</i>
  %val = load int* %ptr                           <i>; yields {int}:val = int 3</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
Chris Lattner's avatar
Chris Lattner committed
<!-- _______________________________________________________________________ -->
Chris Lattner's avatar
Chris Lattner committed
<div class="doc_subsubsection"> <a name="i_store">'<tt>store</tt>'
Instruction</a> </div>
Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  store &lt;ty&gt; &lt;value&gt;, &lt;ty&gt;* &lt;pointer&gt;                   <i>; yields {void}</i>
  volatile store &lt;ty&gt; &lt;value&gt;, &lt;ty&gt;* &lt;pointer&gt;                   <i>; yields {void}</i>
Chris Lattner's avatar
Chris Lattner committed
</pre>
<h5>Overview:</h5>
<p>The '<tt>store</tt>' instruction is used to write to memory.</p>
Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>
Chris Lattner's avatar
Chris Lattner committed
<p>There are two arguments to the '<tt>store</tt>' instruction: a value
to store and an address to store it into.  The type of the '<tt>&lt;pointer&gt;</tt>'
operand must be a pointer to the type of the '<tt>&lt;value&gt;</tt>'
John Criswell's avatar
John Criswell committed
operand. If the <tt>store</tt> is marked as <tt>volatile</tt>, then the
Chris Lattner's avatar
Chris Lattner committed
optimizer is not allowed to modify the number or order of execution of
this <tt>store</tt> with other volatile <tt>load</tt> and <tt><a
 href="#i_store">store</a></tt> instructions.</p>
<h5>Semantics:</h5>
<p>The contents of memory are updated to contain '<tt>&lt;value&gt;</tt>'
at the location specified by the '<tt>&lt;pointer&gt;</tt>' operand.</p>
<h5>Example:</h5>
Chris Lattner's avatar
Chris Lattner committed
<pre>  %ptr = <a href="#i_alloca">alloca</a> int                               <i>; yields {int*}:ptr</i>
  <a
 href="#i_store">store</a> int 3, int* %ptr                          <i>; yields {void}</i>
Chris Lattner's avatar
Chris Lattner committed
  %val = load int* %ptr                           <i>; yields {int}:val = int 3</i>
</pre>
<!-- _______________________________________________________________________ -->
<div class="doc_subsubsection">
   <a name="i_getelementptr">'<tt>getelementptr</tt>' Instruction</a>
</div>

Chris Lattner's avatar
Chris Lattner committed
<h5>Syntax:</h5>
<pre>
  &lt;result&gt; = getelementptr &lt;ty&gt;* &lt;ptrval&gt;{, &lt;ty&gt; &lt;idx&gt;}*
</pre>

Chris Lattner's avatar
Chris Lattner committed
<h5>Overview:</h5>

<p>
The '<tt>getelementptr</tt>' instruction is used to get the address of a
subelement of an aggregate data structure.</p>

Chris Lattner's avatar
Chris Lattner committed
<h5>Arguments:</h5>

<p>This instruction takes a list of integer constants that indicate what
elements of the aggregate object to index to.  The actual types of the arguments
provided depend on the type of the first pointer argument.  The
'<tt>getelementptr</tt>' instruction is used to index down through the type
levels of a structure or to a specific index in an array.  When indexing into a
structure, only <tt>uint</tt>
John Criswell's avatar
John Criswell committed
integer constants are allowed.  When indexing into an array or pointer,
<tt>int</tt> and <tt>long</tt> indexes are allowed of any sign.</p>

Chris Lattner's avatar
Chris Lattner committed
<p>For example, let's consider a C code fragment and how it gets
compiled to LLVM:</p>

<pre>
  struct RT {
    char A;
    int B[10][20];
    char C;
  };
  struct ST {
    int X;
    double Y;
    struct RT Z;
  };

  int *foo(struct ST *s) {
    return &amp;s[1].Z.B[5][13];
  }
</pre>

<p>The LLVM code generated by the GCC frontend is:</p>

<pre>
  %RT = type { sbyte, [10 x [20 x int]], sbyte }
  %ST = type { int, double, %RT }

  implementation

  int* %foo(%ST* %s) {
  entry:
    %reg = getelementptr %ST* %s, int 1, uint 2, uint 1, int 5, int 13
Chris Lattner's avatar
Chris Lattner committed
<h5>Semantics:</h5>

<p>The index types specified for the '<tt>getelementptr</tt>' instruction depend
John Criswell's avatar
John Criswell committed
on the pointer type that is being indexed into. <a href="#t_pointer">Pointer</a>
Chris Lattner's avatar
Chris Lattner committed
and <a href="#t_array">array</a> types require <tt>uint</tt>, <tt>int</tt>,
<tt>ulong</tt>, or <tt>long</tt> values, and <a href="#t_struct">structure</a>
types require <tt>uint</tt> <b>constants</b>.</p>

<p>In the example above, the first index is indexing into the '<tt>%ST*</tt>'
type, which is a pointer, yielding a '<tt>%ST</tt>' = '<tt>{ int, double, %RT
}</tt>' type, a structure.  The second index indexes into the third element of
the structure, yielding a '<tt>%RT</tt>' = '<tt>{ sbyte, [10 x [20 x int]],
sbyte }</tt>' type, another structure.  The third index indexes into the second
element of the structure, yielding a '<tt>[10 x [20 x int]]</tt>' type, an
array.  The two dimensions of the array are subscripted into, yielding an
'<tt>int</tt>' type.  The '<tt>getelementptr</tt>' instruction returns a pointer
to this element, thus computing a value of '<tt>int*</tt>' type.</p>

Chris Lattner's avatar
Chris Lattner committed
<p>Note that it is perfectly legal to index partially through a
structure, returning a pointer to an inner element.  Because of this,
the LLVM code for the given testcase is equivalent to:</p>
    %t1 = getelementptr %ST* %s, int 1                        <i>; yields %ST*:%t1</i>
    %t2 = getelementptr %ST* %t1, int 0, uint 2               <i>; yields %RT*:%t2</i>
    %t3 = getelementptr %RT* %t2, int 0, uint 1               <i>; yields [10 x [20 x int]]*:%t3</i>
    %t4 = getelementptr [10 x [20 x int]]* %t3, int 0, int 5  <i>; yields [20 x int]*:%t4</i>
    %t5 = getelementptr [20 x int]* %t4, int 0, int 13        <i>; yields int*:%t5</i>
    ret int* %t5
  }