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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
.. _gmir-opcodes:
Generic Opcodes
===============
.. contents::
:local:
.. note::
This documentation does not yet fully account for vectors. Many of the
scalar/integer/floating-point operations can also take vectors.
Constants
---------
G_IMPLICIT_DEF
^^^^^^^^^^^^^^
An undefined value.
.. code-block:: none
%0:_(s32) = G_IMPLICIT_DEF
G_CONSTANT
^^^^^^^^^^
An integer constant.
.. code-block:: none
%0:_(s32) = G_CONSTANT i32 1
G_FCONSTANT
^^^^^^^^^^^
A floating point constant.
.. code-block:: none
%0:_(s32) = G_FCONSTANT float 1.0
G_FRAME_INDEX
^^^^^^^^^^^^^
The address of an object in the stack frame.
.. code-block:: none
%1:_(p0) = G_FRAME_INDEX %stack.0.ptr0
G_GLOBAL_VALUE
^^^^^^^^^^^^^^
The address of a global value.
.. code-block:: none
%0(p0) = G_GLOBAL_VALUE @var_local
G_BLOCK_ADDR
^^^^^^^^^^^^
The address of a basic block.
.. code-block:: none
%0:_(p0) = G_BLOCK_ADDR blockaddress(@test_blockaddress, %ir-block.block)
Integer Extension and Truncation
--------------------------------
G_ANYEXT
^^^^^^^^
Extend the underlying scalar type of an operation, leaving the high bits
unspecified.
.. code-block:: none
%1:_(s32) = G_ANYEXT %0:_(s16)
G_SEXT
^^^^^^
Sign extend the underlying scalar type of an operation, copying the sign bit
into the newly-created space.
.. code-block:: none
%1:_(s32) = G_SEXT %0:_(s16)
G_SEXT_INREG
^^^^^^^^^^^^
Sign extend the value from an arbitrary bit position, copying the sign bit
into all bits above it. This is equivalent to a shl + ashr pair with an
appropriate shift amount. $sz is an immediate (MachineOperand::isImm()
returns true) to allow targets to have some bitwidths legal and others
lowered. This opcode is particularly useful if the target has sign-extension
instructions that are cheaper than the constituent shifts as the optimizer is
able to make decisions on whether it's better to hang on to the G_SEXT_INREG
or to lower it and optimize the individual shifts.
.. code-block:: none
%1:_(s32) = G_SEXT_INREG %0:_(s32), 16
G_ZEXT
^^^^^^
Zero extend the underlying scalar type of an operation, putting zero bits
into the newly-created space.
.. code-block:: none
%1:_(s32) = G_ZEXT %0:_(s16)
G_TRUNC
^^^^^^^
Truncate the underlying scalar type of an operation. This is equivalent to
G_EXTRACT for scalar types, but acts elementwise on vectors.
.. code-block:: none
%1:_(s16) = G_TRUNC %0:_(s32)
Type Conversions
----------------
G_INTTOPTR
^^^^^^^^^^
Convert an integer to a pointer.
.. code-block:: none
%1:_(p0) = G_INTTOPTR %0:_(s32)
G_PTRTOINT
^^^^^^^^^^
Convert a pointer to an integer.
.. code-block:: none
%1:_(s32) = G_PTRTOINT %0:_(p0)
G_BITCAST
^^^^^^^^^
Reinterpret a value as a new type. This is usually done without changing any
bits but this is not always the case due a sublety in the definition of the
:ref:`LLVM-IR Bitcast Instruction <i_bitcast>`.
.. code-block:: none
%1:_(s64) = G_BITCAST %0:_(<2 x s32>)
G_ADDRSPACE_CAST
^^^^^^^^^^^^^^^^
Convert a pointer to an address space to a pointer to another address space.
.. code-block:: none
%1:_(p1) = G_ADDRSPACE_CAST %0:_(p0)
.. caution::
:ref:`i_addrspacecast` doesn't mention what happens if the cast is simply
invalid (i.e. if the address spaces are disjoint).
Scalar Operations
-----------------
G_EXTRACT
^^^^^^^^^
Extract a register of the specified size, starting from the block given by
index. This will almost certainly be mapped to sub-register COPYs after
register banks have been selected.
G_INSERT
^^^^^^^^
Insert a smaller register into a larger one at the specified bit-index.
G_MERGE_VALUES
^^^^^^^^^^^^^^
Concatenate multiple registers of the same size into a wider register.
The input operands are always ordered from lowest bits to highest:
.. code-block:: none
%0:(s32) = G_MERGE_VALUES %bits_0_7:(s8), %bits_8_15:(s8),
%bits_16_23:(s8), %bits_24_31:(s8)
G_UNMERGE_VALUES
^^^^^^^^^^^^^^^^
Extract multiple registers of the specified size, starting from blocks given by
indexes. This will almost certainly be mapped to sub-register COPYs after
register banks have been selected.
The output operands are always ordered from lowest bits to highest:
.. code-block:: none
%bits_0_7:(s8), %bits_8_15:(s8),
%bits_16_23:(s8), %bits_24_31:(s8) = G_UNMERGE_VALUES %0:(s32)
G_BSWAP
^^^^^^^
Reverse the order of the bytes in a scalar.
.. code-block:: none
%1:_(s32) = G_BSWAP %0:_(s32)
G_BITREVERSE
^^^^^^^^^^^^
Reverse the order of the bits in a scalar.
.. code-block:: none
%1:_(s32) = G_BITREVERSE %0:_(s32)
Integer Operations
-------------------
G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR, G_SDIV, G_UDIV, G_SREM, G_UREM
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
These each perform their respective integer arithmetic on a scalar.
.. code-block:: none
%2:_(s32) = G_ADD %0:_(s32), %1:_(s32)
G_SHL, G_LSHR, G_ASHR
^^^^^^^^^^^^^^^^^^^^^
Shift the bits of a scalar left or right inserting zeros (sign-bit for G_ASHR).
G_ICMP
^^^^^^
Perform integer comparison producing non-zero (true) or zero (false). It's
target specific whether a true value is 1, ~0U, or some other non-zero value.
G_SELECT
^^^^^^^^
Select between two values depending on a zero/non-zero value.
.. code-block:: none
%5:_(s32) = G_SELECT %4(s1), %6, %2
G_PTR_ADD
^^^^^^^^^
Add a scalar offset in addressible units to a pointer. Addressible units are
typically bytes but this may vary between targets.
.. code-block:: none
%1:_(p0) = G_PTR_ADD %0:_(p0), %1:_(s32)
.. caution::
There are currently no in-tree targets that use this with addressable units
not equal to 8 bit.
G_PTR_MASK
^^^^^^^^^^
Zero the least significant N bits of a pointer.
.. code-block:: none
%1:_(p0) = G_PTR_MASK %0, 3
G_SMIN, G_SMAX, G_UMIN, G_UMAX
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Take the minimum/maximum of two values.
.. code-block:: none
%5:_(s32) = G_SMIN %6, %2
G_UADDO, G_SADDO, G_USUBO, G_SSUBO, G_SMULO, G_UMULO
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Perform the requested arithmetic and produce a carry output in addition to the
normal result.
.. code-block:: none
%3:_(s32), %4:_(s1) = G_UADDO %0, %1
G_UADDE, G_SADDE, G_USUBE, G_SSUBE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Perform the requested arithmetic and consume a carry input in addition to the
normal input. Also produce a carry output in addition to the normal result.
.. code-block:: none
%4:_(s32), %5:_(s1) = G_UADDE %0, %1, %3:_(s1)
G_UMULH, G_SMULH
^^^^^^^^^^^^^^^^
Multiply two numbers at twice the incoming bit width (signed) and return
the high half of the result.
.. code-block:: none
%3:_(s32) = G_UMULH %0, %1
G_CTLZ, G_CTTZ, G_CTPOP
^^^^^^^^^^^^^^^^^^^^^^^
Count leading zeros, trailing zeros, or number of set bits.
.. code-block:: none
%2:_(s33) = G_CTLZ_ZERO_UNDEF %1
%2:_(s33) = G_CTTZ_ZERO_UNDEF %1
%2:_(s33) = G_CTPOP %1
G_CTLZ_ZERO_UNDEF, G_CTTZ_ZERO_UNDEF
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Count leading zeros or trailing zeros. If the value is zero then the result is
undefined.
.. code-block:: none
%2:_(s33) = G_CTLZ_ZERO_UNDEF %1
%2:_(s33) = G_CTTZ_ZERO_UNDEF %1
Floating Point Operations
-------------------------
G_FCMP
^^^^^^
Perform floating point comparison producing non-zero (true) or zero
(false). It's target specific whether a true value is 1, ~0U, or some other
non-zero value.
G_FNEG
^^^^^^
Floating point negation.
G_FPEXT
^^^^^^^
Convert a floating point value to a larger type.
G_FPTRUNC
^^^^^^^^^
Convert a floating point value to a narrower type.
G_FPTOSI, G_FPTOUI, G_SITOFP, G_UITOFP
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Convert between integer and floating point.
G_FABS
^^^^^^
Take the absolute value of a floating point value.
G_FCOPYSIGN
^^^^^^^^^^^
Copy the value of the first operand, replacing the sign bit with that of the
second operand.
G_FCANONICALIZE
^^^^^^^^^^^^^^^
See :ref:`i_intr_llvm_canonicalize`.
G_FMINNUM
^^^^^^^^^
Perform floating-point minimum on two values.
In the case where a single input is a NaN (either signaling or quiet),
the non-NaN input is returned.
The return value of (FMINNUM 0.0, -0.0) could be either 0.0 or -0.0.
G_FMAXNUM
^^^^^^^^^
Perform floating-point maximum on two values.
In the case where a single input is a NaN (either signaling or quiet),
the non-NaN input is returned.
The return value of (FMAXNUM 0.0, -0.0) could be either 0.0 or -0.0.
G_FMINNUM_IEEE
^^^^^^^^^^^^^^
Perform floating-point minimum on two values, following the IEEE-754 2008
definition. This differs from FMINNUM in the handling of signaling NaNs. If one
input is a signaling NaN, returns a quiet NaN.
G_FMAXNUM_IEEE
^^^^^^^^^^^^^^
Perform floating-point maximum on two values, following the IEEE-754 2008
definition. This differs from FMAXNUM in the handling of signaling NaNs. If one
input is a signaling NaN, returns a quiet NaN.
G_FMINIMUM
^^^^^^^^^^
NaN-propagating minimum that also treat -0.0 as less than 0.0. While
FMINNUM_IEEE follow IEEE 754-2008 semantics, FMINIMUM follows IEEE 754-2018
draft semantics.
G_FMAXIMUM
^^^^^^^^^^
NaN-propagating maximum that also treat -0.0 as less than 0.0. While
FMAXNUM_IEEE follow IEEE 754-2008 semantics, FMAXIMUM follows IEEE 754-2018
draft semantics.
G_FADD, G_FSUB, G_FMUL, G_FDIV, G_FREM
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Perform the specified floating point arithmetic.
G_FMA
^^^^^
Perform a fused multiply add (i.e. without the intermediate rounding step).
G_FMAD
^^^^^^
Perform a non-fused multiply add (i.e. with the intermediate rounding step).
G_FPOW
^^^^^^
Raise the first operand to the power of the second.
G_FEXP, G_FEXP2
^^^^^^^^^^^^^^^
Calculate the base-e or base-2 exponential of a value
G_FLOG, G_FLOG2, G_FLOG10
^^^^^^^^^^^^^^^^^^^^^^^^^
Calculate the base-e, base-2, or base-10 respectively.
G_FCEIL, G_FCOS, G_FSIN, G_FSQRT, G_FFLOOR, G_FRINT, G_FNEARBYINT
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
These correspond to the standard C functions of the same name.
G_INTRINSIC_TRUNC
^^^^^^^^^^^^^^^^^
Returns the operand rounded to the nearest integer not larger in magnitude than the operand.
G_INTRINSIC_ROUND
^^^^^^^^^^^^^^^^^
Returns the operand rounded to the nearest integer.
Vector Specific Operations
--------------------------
G_CONCAT_VECTORS
^^^^^^^^^^^^^^^^
Concatenate two vectors to form a longer vector.
G_BUILD_VECTOR, G_BUILD_VECTOR_TRUNC
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Create a vector from multiple scalar registers. No implicit
conversion is performed (i.e. the result element type must be the
same as all source operands)
The _TRUNC version truncates the larger operand types to fit the
destination vector elt type.
G_INSERT_VECTOR_ELT
^^^^^^^^^^^^^^^^^^^
Insert an element into a vector
G_EXTRACT_VECTOR_ELT
^^^^^^^^^^^^^^^^^^^^
Extract an element from a vector
G_SHUFFLE_VECTOR
^^^^^^^^^^^^^^^^
Concatenate two vectors and shuffle the elements according to the mask operand.
The mask operand should be an IR Constant which exactly matches the
corresponding mask for the IR shufflevector instruction.
Memory Operations
-----------------
G_LOAD, G_SEXTLOAD, G_ZEXTLOAD
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Generic load. Expects a MachineMemOperand in addition to explicit
operands. If the result size is larger than the memory size, the
high bits are undefined, sign-extended, or zero-extended respectively.
Only G_LOAD is valid if the result is a vector type. If the result is larger
than the memory size, the high elements are undefined (i.e. this is not a
per-element, vector anyextload)
G_INDEXED_LOAD
^^^^^^^^^^^^^^
Generic indexed load. Combines a GEP with a load. $newaddr is set to $base + $offset.
If $am is 0 (post-indexed), then the value is loaded from $base; if $am is 1 (pre-indexed)
then the value is loaded from $newaddr.
G_INDEXED_SEXTLOAD
^^^^^^^^^^^^^^^^^^
Same as G_INDEXED_LOAD except that the load performed is sign-extending, as with G_SEXTLOAD.
G_INDEXED_ZEXTLOAD
^^^^^^^^^^^^^^^^^^
Same as G_INDEXED_LOAD except that the load performed is zero-extending, as with G_ZEXTLOAD.
G_STORE
^^^^^^^
Generic store. Expects a MachineMemOperand in addition to explicit operands.
G_INDEXED_STORE
^^^^^^^^^^^^^^^
Combines a store with a GEP. See description of G_INDEXED_LOAD for indexing behaviour.
G_ATOMIC_CMPXCHG_WITH_SUCCESS
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Generic atomic cmpxchg with internal success check. Expects a
MachineMemOperand in addition to explicit operands.
G_ATOMIC_CMPXCHG
^^^^^^^^^^^^^^^^
Generic atomic cmpxchg. Expects a MachineMemOperand in addition to explicit
operands.
G_ATOMICRMW_XCHG, G_ATOMICRMW_ADD, G_ATOMICRMW_SUB, G_ATOMICRMW_AND, G_ATOMICRMW_NAND, G_ATOMICRMW_OR, G_ATOMICRMW_XOR, G_ATOMICRMW_MAX, G_ATOMICRMW_MIN, G_ATOMICRMW_UMAX, G_ATOMICRMW_UMIN, G_ATOMICRMW_FADD, G_ATOMICRMW_FSUB
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Generic atomicrmw. Expects a MachineMemOperand in addition to explicit
operands.
G_FENCE
^^^^^^^
.. caution::
I couldn't find any documentation on this at the time of writing.
Control Flow
------------
G_PHI
^^^^^
Implement the φ node in the SSA graph representing the function.
.. code-block:: none
%1(s8) = G_PHI %7(s8), %bb.0, %3(s8), %bb.1
G_BR
^^^^
Unconditional branch
G_BRCOND
^^^^^^^^
Conditional branch
G_BRINDIRECT
^^^^^^^^^^^^
Indirect branch
G_BRJT
^^^^^^
Indirect branch to jump table entry
G_JUMP_TABLE
^^^^^^^^^^^^
.. caution::
I found no documentation for this instruction at the time of writing.
G_INTRINSIC, G_INTRINSIC_W_SIDE_EFFECTS
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Call an intrinsic
The _W_SIDE_EFFECTS version is considered to have unknown side-effects and
as such cannot be reordered acrosss other side-effecting instructions.
.. note::
Unlike SelectionDAG, there is no _VOID variant. Both of these are permitted
to have zero, one, or multiple results.
Variadic Arguments
------------------
G_VASTART
^^^^^^^^^
.. caution::
I found no documentation for this instruction at the time of writing.
G_VAARG
^^^^^^^
.. caution::
I found no documentation for this instruction at the time of writing.
Other Operations
----------------
G_DYN_STACKALLOC
^^^^^^^^^^^^^^^^
Dynamically realign the stack pointer to the specified alignment
.. code-block:: none
%8:_(p0) = G_DYN_STACKALLOC %7(s64), 32
.. caution::
What does it mean for the immediate to be 0? It happens in the tests
|