File: values.rst

package info (click to toggle)
llvmlite 0.46.0-0.1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,140 kB
  • sloc: python: 13,605; cpp: 3,192; makefile: 185; sh: 168
file content (616 lines) | stat: -rw-r--r-- 16,311 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
.. _ir-values:

======
Values
======

.. currentmodule:: llvmlite.ir


.. contents::
   :local:
   :depth: 1

A :ref:`module` consists mostly of values.

.. data:: Undefined

   An undefined value, mapping to LLVM's ``undef``.


.. class:: Value

   The base class for all IR values.


.. class:: _ConstOpMixin

   This is the base class for :class:`Constant` and :class:`GlobalValue`; do
   not instantiate it directly.

   Integer arithmetic operations:

   * .. method:: add(other)

      Integer add `self` and `other`.

   * .. method:: sub(other)

      Integer subtract `other` from `self`.

   * .. method:: mul(other)

      Integer multiply `self` with `other`.

   * .. method:: udiv(other)

      Unsigned integer divide `self` by `other`.

   * .. method:: sdiv(other)a

      Signed integer divide `self` by `other`.

   * .. method:: urem(other)

      Unsigned integer remainder of `self` divided by `other`.

   * .. method:: srem(other)

      Signed integer remainder of `self` divided by `other`.

   * .. method:: neg()

      Negate `self`.

   Integer logical operations:

   * .. method:: shl(other)

      Left-shift `self` by `other` bits.

   * .. method:: ashr(other)

      Arithmetic, signed, right-shift `self` by `other` bits.

   * .. method:: lshr(other)

      Logical right-shift `self` by `other` bits.

   * .. method:: or_(other)

      Bitwise OR `self` with `other`.

   * .. method:: and_(other)

      Bitwise AND `self` with `other`.

   * .. method:: xor(other)

      Bitwise XOR `self` with `other`.

   Floating point arithmetic:

   * .. method:: fadd(other)

      Floating-point add `self` and `other`.

   * .. method:: fsub(other)

      Floating-point subtract `other` from `self`.

   * .. method:: fmul(other)

      Floating-point multiply `self` by `other`.

   * .. method:: fdiv(other)

      Floating-point divide `self` by `other`.

   * .. method:: frem(other)

      Floating-point remainder of `self` divided by `other`.

   Comparisons:

   * .. method:: icmp_signed(cmpop, other)

      Signed integer compare `self` with `other`. The string `cmpop` can be one
      of ``<``, ``<=``, ``==``, ``!=``, ``>=`` or ``>``.

   * .. method:: icmp_unsigned(cmpop, other)

      Unsigned integer compare `self` with `other`. The string `cmpop` can be
      one of ``<``, ``<=``, ``==``, ``!=``, ``>=`` or ``>``.

   * .. method:: fcmp_ordered(cmpop, other)

      Floating-point ordered compare `self` with `other`. The string `cmpop`
      can be one of ``<``, ``<=``, ``==``, ``!=``, ``>=`` or ``>``.

   * .. method:: fcmp_unordered(cmpop, other)

      Floating-point unordered compare `self` with `other`. The string `cmpop`
      can be one of ``<``, ``<=``, ``==``, ``!=``, ``>=`` or ``>``.

   Integer casts:

   * .. method:: trunc(typ)

      Truncate `self` to integer type `typ`.

   * .. method:: zext(typ)

      Zero-extend `self` to integer type `typ`.

   * .. method:: sext(typ)

       Sign-extend `self` to integer type `typ`.

   * .. method:: bitcast(typ)

      Convert this pointer constant to a constant of the given pointer type
      `typ`.

   Floating-point casts:

   * .. method:: fptrunc(typ)

      Truncate (approximate) floating-point value `self` to floating-point
      type `typ`.

   * .. method:: fpext(typ)

      Extend floating-point value `self` to floating-point type `typ`.

   Integer / floating-point conversion:

   * .. method:: fptoui(typ)

      Convert floating-point value `self` to unsigned integer type `typ`.

   * .. method:: uitofp(typ)

      Convert unsigned integer value `self` to floating-point type `typ`.

   * .. method:: fptosi(typ)

      Convert floating-point value `self` to signed integer type `typ`.

   * .. method:: sitofp(typ)

      Convert signed integer value `self` to floating-point type `typ`.

   Integer / pointer conversions:

   * .. method:: inttoptr(typ)

      Convert this integer constant `self` to a constant of the given pointer
      type `typ`.

   * .. method:: ptrtoint(typ)

      Convert this pointer constant `self` to a constant of the given integer
      type `typ`.

.. class:: Constant(typ, constant)

   A literal value.

   * *typ* is the type of the represented value---a
     :class:`~llvmlite.ir.Type` instance.

   * *constant* is the Python value to be represented.

   Which Python types are allowed for *constant* depends on *typ*:

   * All types accept :data:`Undefined` and convert it to
     LLVM's ``undef``.

   * All types accept ``None`` and convert it to LLVM's
     ``zeroinitializer``.

   * :class:`IntType` accepts any Python integer or boolean.

   * :class:`FloatType` and :class:`DoubleType` accept any
     Python real number.

   * Aggregate types---array and structure types---accept a
     sequence of Python values corresponding to the type's
     element types.

   * :class:`ArrayType` accepts a single :class:`bytearray`
     instance to initialize the array from a string of bytes.
     This is useful for character constants.

   See also :class:`_ConstOpMixin`.

   .. classmethod:: literal_array(elements)

      An alternate constructor for constant arrays.

      * *elements* is a sequence of values, :class:`Constant` or
        otherwise.
      * All *elements* must have the same type.
      * Returns a constant array containing the *elements*, in
        order.

   .. classmethod:: literal_struct(elements, packed=False)

      An alternate constructor for constant structs.

      * *elements* is a sequence of values, :class:`Constant` or
        otherwise.
      * *packed* controls whether to use packed layout.
      * Returns a constant struct containing the
        *elements* in order.

   .. method:: gep(indices)

      Compute the address of the inner element given by the
      sequence of *indices*. The constant must have a pointer
      type.

   NOTE: You cannot define constant functions. Use a
   :ref:`function declaration` instead.

.. class:: Argument

   One of a function's arguments. Arguments have the
   :meth:`add_attribute` method.

   .. method:: add_attribute(attr)

      Add an argument attribute to this argument. *attr* is a
      Python string.

.. class:: Block

   A :ref:`basic block`.  Do not instantiate or mutate this type
   directly. Instead, call the helper methods on
   :class:`Function` and :class:`IRBuilder`.

   Basic blocks have the following methods and attributes:

   * .. method:: replace(old, new)

        Replace the instruction *old* with the other instruction
        *new* in this block's list of instructions. All uses of
        *old* in the whole function are also patched. *old* and *new*
        are :class:`Instruction` objects.

   * .. attribute:: function

        The function this block is defined in.

   * .. attribute:: is_terminated

        Whether this block ends with a
        :ref:`terminator instruction <terminator>`.

   * .. attribute:: terminator

        The block's :ref:`terminator instruction <terminator>`, if any.
        Otherwise ``None``.

.. class:: BlockAddress

   A constant representing an address of a basic block.

   Block address constants have the following attributes:

   * .. attribute:: function

        The function in which the basic block is defined.

   * .. attribute:: basic_block

        The basic block. Must be a part of :attr:`function`.


Metadata
========

There are several kinds of :ref:`metadata` values.


.. class:: MetaDataString(module, value)

   A string literal for use in metadata.

   * *module* is the module that the metadata belongs to.
   * *value* is a Python string.

.. class:: MDValue

   A metadata node. To create an instance, call
   :meth:`Module.add_metadata`.

.. class:: DIValue

   A debug information descriptor, containing key-value pairs.
   To create an instance, call :meth:`Module.add_debug_info`.

.. class:: DIToken(value)

   A debug information "token," representing a well-known
   enumeration value. *value* is the enumeration name.

   EXAMPLE: ``'DW_LANG_Python'``

.. class:: NamedMetaData

   A named metadata node. To create an instance, call
   :meth:`Module.add_named_metadata`. :class:`NamedMetaData` has
   the :meth:`add` method:

   .. method:: add(md)

      Append the given piece of metadata to the collection of
      operands referred to by the :class:`NamedMetaData`. *md* can
      be either a :class:`MetaDataString` or a :class:`MDValue`.

Global values
==============

Global values are values accessible using a module-wide name.

.. class:: GlobalValue

   The base class for global values. Global values have the
   following writable attributes:

   * .. attribute:: linkage

        A Python string describing the linkage behavior of the
        global value---for example, whether it is visible from
        other modules. The default is the empty string, meaning
        "external."

   * .. attribute:: storage_class

        A Python string describing the storage class of the
        global value.

        * The default is the empty string, meaning "default."
        * Other possible values include ``dllimport`` and
          ``dllexport``.

   * .. attribute:: section

        A Python string describing the section a global value
        should be in after translation. The default is the empty
        string, meaning no specific section.


   See also :class:`_ConstOpMixin`.

.. class:: GlobalVariable(module, typ, name, addrspace=0)

   A global variable.

   * *module* is where the variable is defined.

   * *typ* is the variable's type. It cannot be a function type.
     To declare a global function, use :class:`Function`.

     The type of the returned Value is a pointer to *typ*.
     To read the contents of the variable, you need to
     :meth:`~IRBuilder.load` from the returned Value.
     To write to the variable, you need to
     :meth:`~IRBuilder.store` to the returned Value.

   * *name* is the variable's name---a Python string.

   * *addrspace* is an optional address space to store the
     variable in.

   Global variables have the following writable attributes:

   * .. method:: set_metadata(name, node)

        Add metadata with the given *name*, pointing to the given
        metadata *node*---an instance of :class:`MDValue`.

   * .. attribute:: global_constant

        * If ``True``, the variable is declared a constant,
          meaning that its contents cannot be modified.
        * The default is ``False``.

   * .. attribute:: unnamed_addr

        * If ``True``, the address of the variable is deemed
          insignificant, meaning that it is merged with other
          variables that have the same initializer.
        * The default is ``False``.

   * .. attribute:: initializer

        The variable's initialization value---probably a
        :class:`Constant` of type *typ*. The default is ``None``,
        meaning that the variable is uninitialized.

   * .. attribute:: align

        An explicit alignment in bytes. The default is ``None``,
        meaning that the default alignment for the variable's
        type is used.

.. class:: Function(module, typ, name)

   A global function.

   * *module* is where the function is defined.
   * *typ* is the function's type---a :class:`FunctionType`
     instance.
   * *name* is the function's name---a Python string.

   If a global function has any basic blocks, it is a
   :ref:`function definition`. Otherwise, it is a
   :ref:`function declaration`.

   Functions have the following methods and attributes:

   * .. method:: append_basic_block(name='')

        Append a :ref:`basic block` to this function's body.

        * If *name* is not empty, it names the block's entry
          :ref:`label`.
        * Returns a new :class:`Block`.

   * .. method:: insert_basic_block(before, name='')

        Similar to :meth:`append_basic_block`, but inserts it
        before the basic block *before* in the function's list
        of basic blocks.

   * .. method:: set_metadata(name, node)

        Add a function-specific metadata named *name* pointing to the
        given metadata *node*---an :class:`MDValue`.

   * .. attribute:: args

        The function's arguments as a tuple of :class:`Argument`
        instances.

   * .. attribute:: attributes

        A set of function attributes. Each optional attribute is
        a Python string. By default this is empty. Use the
        `.add()` method to add an attribute::

         fnty = ir.FunctionType(ir.DoubleType(), (ir.DoubleType(),))
         fn = Function(module, fnty, "sqrt")
         fn.attributes.add("alwaysinline")

   * .. attribute:: calling_convention

        The function's calling convention---a Python string. The
        default is the empty string.

   * .. attribute:: is_declaration

        Indicates whether the global function is a declaration
        or a definition.

        * If ``True``, it is a declaration.
        * If ``False``, it is a definition.


Instructions
=============

Every :ref:`instruction` is also a value:

* It has a name---the recipient's name.
* It has a well-defined type.
* It can be used as an operand in other instructions or in
  literals.

Usually, you should not instantiate instruction types directly.
Use the helper methods on the :class:`IRBuilder` class.

.. class:: Instruction

   The base class for all instructions. Instructions have the
   following method and attributes:

   * .. method:: set_metadata(name, node)

        Add an instruction-specific metadata *name* pointing to the
        given metadata *node*---an :class:`MDValue`.

   * .. method:: replace_usage(old, new)

        Replace the operand *old* with the other instruction *new*.

   * .. attribute:: function

        The function that contains this instruction.

   * .. attribute:: module

        The module that defines this instruction's function.


.. class:: PredictableInstr

   The class of instructions for which we can specify the
   probabilities of different outcomes---for example, a switch or
   a conditional branch. Predictable instructions have the
   :meth:`set_weights` method.

   .. method:: set_weights(weights)

      Set the weights of the instruction's possible outcomes.
      *weights* is a sequence of positive integers, each
      corresponding to a different outcome and specifying its
      relative probability compared to other outcomes. The
      greater the number, the likelier the outcome.


.. class:: SwitchInstr

   A switch instruction. Switch instructions have the
   :meth:`add_case` method.

   .. method:: add_case(val, block)

      Add a case to the switch instruction.

      * *val* should be a :class:`Constant` or a Python value
        compatible with the switch instruction's operand type.
      * *block* is a :class:`Block` to jump to if val and the
        switch operand compare equal.

.. class:: IndirectBranch

   An indirect branch instruction. Indirect branch instructions
   have the :meth:`add_destination` method.

   .. method:: add_destination(value, block)

      Add an outgoing edge. The indirect branch instruction must
      refer to every basic block it can transfer control to.

.. class:: PhiInstr

   A phi instruction. Phi instructions have the
   :meth:`add_incoming` method.

   .. method:: add_incoming(value, block)

      Add an incoming edge. Whenever transfer is controlled
      from *block*---a :class:`Block`---the phi instruction
      takes the given *value*.

.. class:: LandingPad

   A landing pad. Landing pads have the :meth:`add_clause` method:

   .. method:: add_clause(value, block)

      Add a catch or filter clause. Create catch clauses using
      :class:`CatchClause` and filter clauses using
      :class:`FilterClause`.


Landing pad clauses
====================

Landing pads have the following classes associated with them.

.. class:: CatchClause(value)

   A catch clause. Instructs the personality function to compare
   the in-flight exception typeinfo with *value*, which should
   have type `IntType(8).as_pointer().as_pointer()`.

.. class:: FilterClause(value)

   A filter clause. Instructs the personality function to check
   inclusion of the the in-flight exception typeinfo in *value*,
   which should have type
   `ArrayType(IntType(8).as_pointer().as_pointer(), ...)`.