rucc_ir/inst.rs
1//! What one instruction is, and what a value is.
2//!
3//! Design: `spec/08-ir.md` sections 8.1 and 8.3.
4//!
5//! An instruction is an [`Opcode`], a set of [`Flags`], a run of value operands, and whatever
6//! else that opcode needs, which is [`Extra`]. Everything that fits in eight bytes is in the
7//! [`Extra`] itself and everything larger is an index into a side table, so the instruction
8//! stays small enough that walking a function is walking one dense array.
9//!
10//! A value is the result of an instruction or a parameter of a block, and it is nothing else.
11//! There is no constant operand kind: a constant is an [`Opcode::IConst`] with a result like
12//! any other instruction. That is what makes the dominance rule in the verifier a single rule
13//! with no exceptions, and it costs nothing, because a constant with no uses is deleted by the
14//! same pass that deletes anything else with no uses.
15
16use rucc_base::{Idx, IdxRange, Symbol};
17
18use rucc_target::Slot;
19
20use crate::{
21 ExtraKind, Flags, FloatPred, IntPred, MemOrder, Opcode, Owner, PrefetchHint, RmwOp,
22 StorageClass, Type,
23};
24
25/// One value: the result of an instruction, or a parameter of a block.
26pub type Value = Idx<ValueData>;
27/// One instruction, in the function that owns it.
28pub type Inst = Idx<InstData>;
29/// One basic block, in the function that owns it.
30pub type Block = Idx<BlockData>;
31
32/// The table of references to values, which is what an operand list is a run of.
33#[derive(Debug)]
34pub struct ValueRef;
35/// A run of value operands.
36pub type ValueList = IdxRange<ValueRef>;
37/// A run of branch targets, which is what a terminator's successors are.
38pub type BlockCallList = IdxRange<BlockCall>;
39/// A run of immediates, which is what a `switch` holds its case values in.
40pub type ImmList = IdxRange<Imm>;
41/// A run of ABI attributes, which is what a call says about the arguments its signature does
42/// not name.
43pub type AbiList = IdxRange<Abi>;
44/// A run of eightbytes, which is how an object read off a variable argument list travelled.
45pub type SlotList = IdxRange<Slot>;
46
47/// A constant, in the immediate table.
48///
49/// The bits and nothing else. An integer is stored two's complement in as many of the low bits
50/// as its type is wide, and a floating point value is stored as its bit pattern, so the same
51/// table holds both and the type on the result says how to read it. That keeps a bit-preserving
52/// answer for a NaN payload, which a value of a Rust floating type would not.
53#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
54pub struct Imm(u128);
55
56impl Imm {
57 /// The bits, as they are stored.
58 #[must_use]
59 pub const fn bits(self) -> u128 {
60 self.0
61 }
62
63 /// An immediate holding these bits.
64 #[must_use]
65 pub const fn from_bits(bits: u128) -> Self {
66 Self(bits)
67 }
68
69 /// An integer, with the bits above `ty` cleared.
70 ///
71 /// A value is stored in exactly the width its type has, so two immediates are equal when
72 /// they are the same value, which is what lets an equality on the table stand in for an
73 /// equality on the numbers.
74 ///
75 /// # Panics
76 ///
77 /// Panics if `ty` is not an integer type.
78 #[must_use]
79 pub fn int(value: i128, ty: Type) -> Self {
80 assert!(ty.is_int(), "an integer immediate needs an integer type");
81 Self(value as u128 & mask(ty.bits()))
82 }
83
84 /// The value read as unsigned.
85 #[must_use]
86 pub const fn unsigned(self) -> u128 {
87 self.0
88 }
89
90 /// The value read as signed, with the sign bit of `ty` extended.
91 ///
92 /// # Panics
93 ///
94 /// Panics if `ty` is not an integer type.
95 #[must_use]
96 pub fn signed(self, ty: Type) -> i128 {
97 assert!(ty.is_int(), "an integer immediate needs an integer type");
98 let spare = 128 - ty.bits();
99 // Shifting left and then arithmetic right is the branch-free way to sign extend from
100 // an arbitrary width, and it is correct for a width of 128 because the shift is zero.
101 ((self.0 << spare) as i128) >> spare
102 }
103}
104
105/// The low `bits` bits set, and a width of 128 meaning all of them.
106fn mask(bits: u32) -> u128 {
107 if bits >= 128 { u128::MAX } else { (1u128 << bits) - 1 }
108}
109
110/// How often an arm of a branch is the one taken, where something knows.
111///
112/// Parts out of [`Hint::SCALE`], and nothing at all for an arm nobody has said anything about,
113/// which is almost every arm of almost every branch. A hint is what somebody claimed and not what
114/// a heuristic guessed: `__builtin_expect` writes one, a profile will write one, and the ten
115/// static predictors in `rucc_opt::predict` write none, because a guess that was written down
116/// would be indistinguishable afterwards from a fact.
117///
118/// The scale is ten thousandths because that is the scale `rucc_opt::Probability` and
119/// `rucc_mir::Weight` are in, and a number that changed scale on its way through the compiler is a
120/// number somebody will eventually divide twice.
121#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
122pub struct Hint(Option<u16>);
123
124impl Hint {
125 /// What a hint is out of.
126 pub const SCALE: u32 = 10_000;
127
128 /// Nothing said about this arm, which is what every arm starts as.
129 pub const NONE: Self = Self(None);
130
131 /// This arm is taken `parts` times in [`Hint::SCALE`].
132 ///
133 /// More than the scale is certainty rather than a mistake worth refusing, because the callers
134 /// that can produce one are doing arithmetic whose answer is certainty, so it is clamped.
135 #[must_use]
136 pub fn parts(parts: u32) -> Self {
137 Self(Some(u16::try_from(parts.min(Self::SCALE)).unwrap_or(u16::MAX)))
138 }
139
140 /// The number, or nothing where nothing was said.
141 #[must_use]
142 pub fn taken(self) -> Option<u32> {
143 self.0.map(u32::from)
144 }
145
146 /// The hint the other arm of a two armed branch carries, so that the two sum to certainty.
147 #[must_use]
148 pub fn complement(self) -> Self {
149 match self.0 {
150 Some(parts) => Self::parts(Self::SCALE - u32::from(parts)),
151 None => Self::NONE,
152 }
153 }
154}
155
156/// A branch target, and the values passed to it.
157///
158/// This is the whole reason there are no phi nodes. The arguments are here, in the branch,
159/// beside the block they go to, so removing a predecessor is one edit in one place and there
160/// is no second list anywhere that has to be kept in step with this one.
161///
162/// The hint is here for the same reason and not on the instruction: a branch has as many arms as
163/// it has block calls, and a weight held anywhere else would be a second list to keep in step with
164/// this one.
165#[derive(Clone, Copy, Debug, PartialEq, Eq)]
166pub struct BlockCall {
167 /// Where control goes.
168 pub block: Block,
169 /// What is passed, one for each of the block's parameters.
170 pub args: ValueList,
171 /// How often this arm is the one taken, where something said so.
172 pub hint: Hint,
173}
174
175impl BlockCall {
176 /// An arm going to a block with arguments and nothing said about how often it is taken.
177 #[must_use]
178 pub const fn new(block: Block, args: ValueList) -> Self {
179 Self { block, args, hint: Hint::NONE }
180 }
181
182 /// An arm going to a block that takes no arguments.
183 #[must_use]
184 pub const fn to(block: Block) -> Self {
185 Self::new(block, ValueList::EMPTY)
186 }
187}
188
189/// What defines a value.
190#[derive(Clone, Copy, Debug, PartialEq, Eq)]
191pub enum Def {
192 /// The result of an instruction, at this position among its results.
193 Result {
194 /// The instruction.
195 inst: Inst,
196 /// Which of its results this is.
197 index: u8,
198 },
199 /// A parameter of a block, at this position among its parameters.
200 Param {
201 /// The block.
202 block: Block,
203 /// Which of its parameters this is.
204 index: u32,
205 },
206}
207
208/// One value.
209#[derive(Clone, Copy, Debug, PartialEq, Eq)]
210pub struct ValueData {
211 /// Its type.
212 pub ty: Type,
213 /// Where it comes from.
214 pub def: Def,
215}
216
217/// What an access does beyond naming an address.
218#[derive(Clone, Copy, Debug, PartialEq, Eq)]
219pub struct MemInfo {
220 /// How many bytes the access covers, for the ones whose size is not their result type.
221 ///
222 /// A `load` takes its size from the type it produces. An `alloca` and a `memset` do not,
223 /// and this is where theirs is.
224 pub size: u64,
225 /// The alignment the access is known to have, in bytes.
226 pub align: u32,
227 /// How strongly it is ordered, with [`MemOrder::NotAtomic`] for an ordinary access.
228 pub order: MemOrder,
229 /// The type-based aliasing node, if the front end knew one.
230 pub tbaa: Option<Meta>,
231 /// How many bytes of its record this access owns, counting the padding after it.
232 ///
233 /// Zero for an access that is not a member of a record, and zero when the front end was not
234 /// asked to work it out. What it is for is the init plane of
235 /// `spec/safe-memory/09-type-init-and-races.md` section 9.3: under `-fsafety-init=nopadding`
236 /// a store through a member records the padding after the member as written too, so that a
237 /// record filled a member at a time comes out whole and the ordinary reads of it, which are a
238 /// `memcmp` or a hash or a `write` of the record, are not refused.
239 ///
240 /// Only the init plane reads it. A bounds check over these bytes would be asking about bytes
241 /// the access does not touch, and a type plane write over them would be saying the padding
242 /// holds a value of the member's type, which it does not.
243 pub owns: u32,
244 /// Which `restrict` scope the access is in and which pointer it went through.
245 pub restrict: Restrict,
246}
247
248/// Which `restrict` scope an access is in, and which pointer inside that scope it went through.
249///
250/// Two small numbers, which is the whole of the mechanism. GCC spells them
251/// `MR_DEPENDENCE_CLIQUE` and `MR_DEPENDENCE_BASE` at `gcc/tree-ssa-alias.cc:2503` and the rule
252/// is one line: same clique and different base means the two accesses cannot touch the same
253/// byte, because that is exactly what `restrict` promises. A clique is one scope, numbered as
254/// lowering enters it, and a base is one `restrict` pointer declared inside it. Clique zero
255/// means nothing is known, which is what every access that is not under a `restrict` gets.
256///
257/// This is spec 9.4's scope tree rather than a blanket assumption, and it costs four bytes that
258/// were padding in [`MemInfo`] already.
259#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
260pub struct Restrict {
261 /// The scope, with zero meaning no information.
262 pub clique: u16,
263 /// The pointer within that scope, which only means anything when the clique is not zero.
264 pub base: u16,
265}
266
267impl Restrict {
268 /// No information, which is what an access outside any `restrict` scope carries.
269 pub const NONE: Self = Self { clique: 0, base: 0 };
270
271 /// Whether `restrict` says these two accesses cannot touch the same byte.
272 ///
273 /// Only accesses. GCC's PR71062 is what happens when this answer is used to fold a
274 /// comparison of the two pointers: `restrict` constrains what is read and written through a
275 /// pointer and says nothing about what the pointer's value is, so two pointers that may not
276 /// be used to reach the same object can still compare equal. A rule that folds `p == q` to
277 /// false on the strength of this is wrong.
278 #[must_use]
279 pub const fn disjoint(self, other: Self) -> bool {
280 self.clique != 0 && self.clique == other.clique && self.base != other.base
281 }
282}
283
284/// A metadata node, in the module's table.
285pub type Meta = Idx<MetaNode>;
286
287/// A node of the metadata graph.
288///
289/// Two kinds share the one table and the one numbering, because both of them are the compiler's
290/// interned type universe seen from a different side and a reader chasing a `!3` should not have
291/// to know which table it came out of.
292#[derive(Clone, Copy, Debug, PartialEq, Eq)]
293pub enum MetaNode {
294 /// What aliasing needs: a type, and where it sits in the tree of types.
295 Tbaa(TbaaNode),
296 /// What the type plane needs: one entry in the vocabulary its bytes are written in.
297 Plane(PlaneNode),
298}
299
300impl MetaNode {
301 /// The aliasing node this is, or `None` when it is a plane entry.
302 #[must_use]
303 pub const fn tbaa(self) -> Option<TbaaNode> {
304 match self {
305 Self::Tbaa(node) => Some(node),
306 Self::Plane(_) => None,
307 }
308 }
309
310 /// The plane entry this is, or `None` when it is an aliasing node.
311 #[must_use]
312 pub const fn plane(self) -> Option<PlaneNode> {
313 match self {
314 Self::Plane(node) => Some(node),
315 Self::Tbaa(_) => None,
316 }
317 }
318
319 /// The node one level up, which a plane entry never has.
320 ///
321 /// The tree is the aliasing tree and a plane entry is not in it. A plane entry that names a
322 /// type points at a node of that tree, and that is a reference and not a parent: the walk
323 /// that answers an aliasing query has no business leaving the tree it is walking.
324 #[must_use]
325 pub const fn parent(self) -> Option<Meta> {
326 match self {
327 Self::Tbaa(node) => node.parent,
328 Self::Plane(_) => None,
329 }
330 }
331
332 /// The node it points at, which is the parent of an aliasing node and the type of a plane
333 /// entry, and is what has to come earlier in the table than the node itself.
334 #[must_use]
335 pub const fn points_at(self) -> Option<Meta> {
336 match self {
337 Self::Tbaa(node) => node.parent,
338 Self::Plane(PlaneNode::Type(node)) => Some(node),
339 Self::Plane(_) => None,
340 }
341 }
342}
343
344/// A node of the type based aliasing tree.
345///
346/// The tree this forms is checked by the verifier, since a cycle in it would make the aliasing
347/// query that walks it not terminate, and the place to find that out is here and not there.
348#[derive(Clone, Copy, Debug, PartialEq, Eq)]
349pub struct TbaaNode {
350 /// What this node is called, which is what the printer writes and the parser reads.
351 pub name: Symbol,
352 /// The node one level up, with the root having none.
353 pub parent: Option<Meta>,
354 /// The offset within the parent, for a member of a struct type.
355 pub offset: u64,
356}
357
358/// One entry in the type plane's vocabulary, per `spec/safe-memory/09-type-init-and-races.md`
359/// section 9.1.
360///
361/// The plane maps every byte to one of these, so this is what a `meta_type` writes and what a
362/// `check_type` is asking about. Three of the four are the distinguished values that document
363/// says the plane has beyond the types themselves, and they are why the plane needs a node kind
364/// of its own rather than pointing straight at an aliasing node: there is no aliasing node for
365/// "nobody has stored here yet".
366#[derive(Clone, Copy, Debug, PartialEq, Eq)]
367pub enum PlaneNode {
368 /// A type, named by the aliasing node that is that type.
369 ///
370 /// The same node the front end already interned, so the plane's vocabulary is exactly the
371 /// compiler's and a report can name a type in the spelling the source used.
372 Type(Meta),
373 /// Bytes nothing has stored through, or stored from an untyped source.
374 ///
375 /// Compatible with every access, because storage with no declared type takes its effective
376 /// type from the store, which is C's rule and is also the only choice that does not fire at
377 /// every boundary with uninstrumented code.
378 NoType,
379 /// Bytes stored through a character type, which is compatible with every access.
380 ///
381 /// This is what makes the byte-wise copy idiom work. C 6.5 says a character access is
382 /// always permitted and that a store through a character lvalue does not set an effective
383 /// type, so the plane says `character` over those bytes and the later read of the field
384 /// still passes.
385 Character,
386 /// Byte `k` of a pointer shaped word.
387 ///
388 /// A pointer is not one type over its bytes, it is a word whose bytes are only meaningful
389 /// together, so reading four bytes out of the middle of one is a different thing from
390 /// reading four bytes of an `int` and the plane has to be able to say which byte it is.
391 PointerSlot(u8),
392}
393
394/// What a call needs beyond its arguments.
395#[derive(Clone, Copy, Debug, PartialEq, Eq)]
396pub struct CallInfo {
397 /// The name, for a direct call. `None` for a call through an address, where the address is
398 /// the first operand.
399 pub callee: Option<Symbol>,
400 /// The signature it is called with, which is where the ABI attributes are.
401 pub signature: Sig,
402 /// What the ABI asks of the arguments the signature does not name, one entry for each of
403 /// them.
404 ///
405 /// Only a variadic call has any, because only a variadic call passes an argument no
406 /// parameter stands for, and it is empty when every one of them travels as the value in
407 /// hand, which is nearly always. A structure the classification puts in the argument area
408 /// is the case it exists for: the bytes travel and there is no parameter to hang the
409 /// [`Abi::ByVal`] on, so it hangs here instead.
410 pub varargs: AbiList,
411}
412
413/// A signature, in the function's table.
414pub type Sig = Idx<Signature>;
415
416/// What a `switch` needs beyond the value it switches on.
417#[derive(Clone, Copy, Debug, PartialEq, Eq)]
418pub struct SwitchInfo {
419 /// The targets, with the default first and one for each case after it.
420 pub targets: BlockCallList,
421 /// The case values, one for each target after the default.
422 pub cases: ImmList,
423}
424
425/// What an object read off a variable argument list is.
426///
427/// The access says how many bytes it is and what it is aligned to, which is the whole of what an
428/// object the convention put in the caller's argument area needs: it is there, and those two say
429/// where the argument behind it starts. An object that travelled in registers is not there at all.
430/// It is in the callee's own register save area, in as many places as it has eightbytes, and which
431/// register file each of those came from is not something the size and the alignment say. So the
432/// slots say it, and they are empty for the object that went in memory.
433///
434/// The classification is the front end's, because it is the one that still has the type. By the
435/// time an instruction reaches a backend the type is a size and an alignment, and the algorithm in
436/// section 3.5.7 of the psABI wants more than that.
437#[derive(Clone, Copy, Debug, PartialEq, Eq)]
438pub struct VaInfo {
439 /// The object, as any other access describes one.
440 pub mem: Idx<MemInfo>,
441 /// Where each of its eightbytes travelled, or nothing at all for one that travelled whole in
442 /// the caller's memory.
443 pub slots: SlotList,
444}
445
446/// What inline assembly needs.
447///
448/// The semantics belong to the inline assembly document. What is here is the shape: a
449/// template, the constraints, the clobbers, and the successors that make `asm goto` the one
450/// instruction whose being a terminator is a property of the instruction and not the opcode.
451#[derive(Clone, Copy, Debug, PartialEq, Eq)]
452pub struct AsmInfo {
453 /// The template string, as written.
454 pub template: Symbol,
455 /// The constraint list, as written.
456 pub constraints: Symbol,
457 /// The clobber list, as written.
458 pub clobbers: Symbol,
459 /// The labels, which are empty for everything except `asm goto`.
460 pub targets: BlockCallList,
461}
462
463/// Everything an instruction carries that is not a value operand.
464///
465/// Anything that fits in eight bytes is here and anything larger is an index into a side
466/// table, so that the common instructions, which are the arithmetic ones carrying nothing at
467/// all, do not pay for the rare ones.
468#[derive(Clone, Copy, Debug, PartialEq, Eq)]
469pub enum Extra {
470 /// Nothing, which is most instructions.
471 None,
472 /// A constant, for `iconst`, `fconst` and `splat`.
473 Imm(Idx<Imm>),
474 /// A name, for `global_addr` and for a target-specific intrinsic.
475 Symbol(Symbol),
476 /// Which comparison, for `icmp`.
477 IntPred(IntPred),
478 /// Which comparison, for `fcmp`.
479 FloatPred(FloatPred),
480 /// An access, for the loads, the stores, the copies and `alloca`.
481 Mem(Idx<MemInfo>),
482 /// An atomic read-modify-write, which is an access and which operation.
483 Rmw(RmwOp, Idx<MemInfo>),
484 /// A barrier's ordering, for `fence`.
485 Order(MemOrder),
486 /// What a `prefetch` is a hint about, which is a read or a write and how much locality.
487 Prefetch(PrefetchHint),
488 /// The targets of a branch, with the default first for a `switch`.
489 Targets(BlockCallList),
490 /// A call.
491 Call(Idx<CallInfo>),
492 /// A `switch`, which is targets and the values that select them.
493 Switch(Idx<SwitchInfo>),
494 /// Inline assembly.
495 Asm(Idx<AsmInfo>),
496 /// An object read off a variable argument list, which is an access and how it travelled.
497 VaObject(Idx<VaInfo>),
498 /// What kind of storage an instance is, for `meta_begin`.
499 Class(StorageClass),
500 /// Who a range went to, for `meta_transfer`.
501 Owner(Owner),
502 /// A metadata node, for `meta_type`, which is the one plane write that names a type.
503 Node(Meta),
504 /// Why an exemption was declared, for `safe_region_begin`.
505 Reason(Symbol),
506}
507
508impl Extra {
509 /// Which shape this is, without the payload.
510 ///
511 /// The verifier compares this with [`Opcode::extra_kind`], because an instruction carrying
512 /// the payload of some other opcode prints as text the parser cannot read back.
513 #[must_use]
514 pub const fn kind(self) -> ExtraKind {
515 match self {
516 Self::None => ExtraKind::None,
517 Self::Imm(_) => ExtraKind::Imm,
518 Self::Symbol(_) => ExtraKind::Symbol,
519 Self::IntPred(_) => ExtraKind::IntPred,
520 Self::FloatPred(_) => ExtraKind::FloatPred,
521 Self::Mem(_) => ExtraKind::Mem,
522 Self::Rmw(..) => ExtraKind::Rmw,
523 Self::Order(_) => ExtraKind::Order,
524 Self::Prefetch(_) => ExtraKind::Prefetch,
525 Self::Targets(_) => ExtraKind::Targets,
526 Self::Call(_) => ExtraKind::Call,
527 Self::Switch(_) => ExtraKind::Switch,
528 Self::Asm(_) => ExtraKind::Asm,
529 Self::VaObject(_) => ExtraKind::VaObject,
530 Self::Class(_) => ExtraKind::Class,
531 Self::Owner(_) => ExtraKind::Owner,
532 Self::Node(_) => ExtraKind::Node,
533 Self::Reason(_) => ExtraKind::Reason,
534 }
535 }
536}
537
538/// One instruction.
539///
540/// There is no result type here. Each result is a value in the function's value table and the
541/// type is on the value, which means a reader asking what an instruction produces asks the
542/// same question about `add` as about `call`, and there is no second copy of the type to
543/// disagree with the first.
544#[derive(Clone, Copy, Debug, PartialEq, Eq)]
545pub struct InstData {
546 /// Which instruction this is.
547 pub opcode: Opcode,
548 /// What the optimizer is licensed to assume about it.
549 pub flags: Flags,
550 /// How many values it produces.
551 pub results: u8,
552 /// The first of them, with the rest following it in the value table.
553 pub first_result: Option<Value>,
554 /// Its value operands.
555 pub args: ValueList,
556 /// Everything else it carries.
557 pub extra: Extra,
558}
559
560impl InstData {
561 /// An instruction with no operands, no flags, no results and nothing extra.
562 #[must_use]
563 pub const fn new(opcode: Opcode) -> Self {
564 Self {
565 opcode,
566 flags: Flags::NONE,
567 results: 0,
568 first_result: None,
569 args: ValueList::EMPTY,
570 extra: Extra::None,
571 }
572 }
573
574 /// The values it produces, in order.
575 pub fn results(&self) -> impl Iterator<Item = Value> + use<> {
576 let first = self.first_result.map_or(0, Idx::raw);
577 (0..u32::from(self.results)).map(move |offset| Value::new(first + offset))
578 }
579
580 /// The run of targets it branches to, which is empty when it does not branch.
581 ///
582 /// A `switch` keeps its targets in a side table, so this reads `Extra::Targets` only and
583 /// the function is what answers for the rest.
584 #[must_use]
585 pub fn targets(&self) -> BlockCallList {
586 match self.extra {
587 Extra::Targets(targets) => targets,
588 _ => BlockCallList::EMPTY,
589 }
590 }
591}
592
593/// How one parameter or one return value travels, beyond what its type says.
594///
595/// The IR's types are the machine's and not C's, so a `ptr` parameter says nothing about
596/// whether the pointer is the argument or whether the object it points at is, and an `i8` says
597/// nothing about which half of the register above it the callee may read. Both are the ABI's
598/// answer rather than the type's, which is why they are here and not on [`Type`].
599///
600/// A signature carrying one of these has already had the ABI applied to it. What the walk to
601/// the IR builds first is the C-level form, where every parameter is [`Abi::Plain`], and the
602/// classification in `rucc-target` is what turns one into the other.
603#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
604pub enum Abi {
605 /// The value itself, in the type it is written as.
606 #[default]
607 Plain,
608 /// An integer narrower than a register, with the bits above it its own sign.
609 ///
610 /// Which of these an ABI asks for is not a property of the value: `unsigned char` is
611 /// [`Abi::Sext`] on the Darwin ABIs and [`Abi::Zext`] elsewhere, and on SysV neither the
612 /// caller nor the callee may assume anything about those bits at all.
613 Sext,
614 /// An integer narrower than a register, with zeroes above it.
615 Zext,
616 /// The bytes of the object the pointer points at, in the argument area, with no address
617 /// travelling anywhere.
618 ///
619 /// The caller makes the copy the callee is free to write to, which is what makes this a C
620 /// call by value rather than a pointer the callee must not keep.
621 ByVal {
622 /// How many bytes travel.
623 size: u64,
624 /// What the copy is aligned to, which is the C alignment of the type and not the
625 /// pointer's.
626 align: u32,
627 },
628 /// Somewhere for the return value to go, whose address the caller passes as the first
629 /// argument because the value does not fit in the registers a return comes back in.
630 Sret {
631 /// How many bytes the callee writes.
632 size: u64,
633 /// What the space is aligned to.
634 align: u32,
635 },
636}
637
638impl Abi {
639 /// Whether this describes an object behind a pointer rather than the value in hand.
640 #[must_use]
641 pub const fn indirect(self) -> bool {
642 matches!(self, Self::ByVal { .. } | Self::Sret { .. })
643 }
644
645 /// The size and alignment of that object, for the two that have one.
646 #[must_use]
647 pub const fn object(self) -> Option<(u64, u32)> {
648 match self {
649 Self::ByVal { size, align } | Self::Sret { size, align } => Some((size, align)),
650 _ => None,
651 }
652 }
653}
654
655/// One parameter, or one return value: a type and how it travels.
656#[derive(Clone, Copy, Debug, PartialEq, Eq)]
657pub struct Param {
658 /// The type the IR sees, which for the indirect forms is `ptr`.
659 pub ty: Type,
660 /// What the ABI asks of it.
661 pub abi: Abi,
662}
663
664impl Param {
665 /// A parameter of this type, in its C-level form.
666 #[must_use]
667 pub const fn new(ty: Type) -> Self {
668 Self { ty, abi: Abi::Plain }
669 }
670
671 /// A parameter of this type travelling this way.
672 #[must_use]
673 pub const fn with_abi(ty: Type, abi: Abi) -> Self {
674 Self { ty, abi }
675 }
676}
677
678/// What a function takes and returns.
679///
680/// A signature is not a type. Nothing in the IR has a function type, because a `ptr` has no
681/// pointee and there is nothing else a function type could sit on. A `call_indirect` names the
682/// signature it is called with, and that is where the ABI attributes are read from.
683#[derive(Clone, Debug, PartialEq, Eq, Default)]
684pub struct Signature {
685 /// What it takes, in their C-level form until the ABI has been applied.
686 pub params: Vec<Param>,
687 /// What it returns, which is empty for a `void` function and for one whose return value
688 /// comes back through an [`Abi::Sret`] parameter.
689 pub returns: Vec<Param>,
690 /// Whether it takes arguments beyond the ones named.
691 pub variadic: bool,
692}
693
694impl Signature {
695 /// A signature taking and returning nothing.
696 #[must_use]
697 pub fn new() -> Self {
698 Self::default()
699 }
700
701 /// The same signature with these parameters, each in its C-level form.
702 #[must_use]
703 pub fn with_params(mut self, params: &[Type]) -> Self {
704 self.params = params.iter().copied().map(Param::new).collect();
705 self
706 }
707
708 /// The same signature returning these, each in its C-level form.
709 #[must_use]
710 pub fn with_returns(mut self, returns: &[Type]) -> Self {
711 self.returns = returns.iter().copied().map(Param::new).collect();
712 self
713 }
714
715 /// The same signature with one more parameter, travelling the way the ABI said.
716 #[must_use]
717 pub fn and_param(mut self, param: Param) -> Self {
718 self.params.push(param);
719 self
720 }
721
722 /// The same signature with one more return value, travelling the way the ABI said.
723 #[must_use]
724 pub fn and_return(mut self, param: Param) -> Self {
725 self.returns.push(param);
726 self
727 }
728
729 /// The types it takes, without what the ABI asks of them.
730 pub fn param_types(&self) -> impl Iterator<Item = Type> + use<'_> {
731 self.params.iter().map(|param| param.ty)
732 }
733
734 /// The types it returns.
735 pub fn return_types(&self) -> impl Iterator<Item = Type> + use<'_> {
736 self.returns.iter().map(|param| param.ty)
737 }
738
739 /// The same signature, variadic.
740 #[must_use]
741 pub fn variadic(mut self) -> Self {
742 self.variadic = true;
743 self
744 }
745}
746
747/// One basic block: parameters, then instructions, then exactly one terminator.
748///
749/// The instructions are a doubly linked list rather than a vector, so that inserting one in
750/// the middle of a block does not move the ones after it. An optimizer does that constantly,
751/// and a move would invalidate every [`Inst`] anybody was holding.
752#[derive(Clone, Debug, Default, PartialEq, Eq)]
753pub struct BlockData {
754 /// The values arriving here, which is what other IRs spell as phi nodes.
755 ///
756 /// A `Vec` and not a run in a pool, because SSA construction adds a parameter to a loop
757 /// header long after the blocks that come after it have been built, and a run in a pool
758 /// cannot grow in the middle.
759 pub params: Vec<Value>,
760 /// The first instruction, or `None` for a block nothing has been put in yet.
761 pub first: Option<Inst>,
762 /// The last instruction, which is the terminator once the block is finished.
763 pub last: Option<Inst>,
764 /// The block before this one in layout order.
765 pub prev: Option<Block>,
766 /// The block after it.
767 pub next: Option<Block>,
768}
769
770/// Where one instruction sits.
771#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
772pub struct InstLayout {
773 /// The block it is in, or `None` if it has been made and not yet inserted.
774 pub block: Option<Block>,
775 /// The instruction before it in that block.
776 pub prev: Option<Inst>,
777 /// The instruction after it.
778 pub next: Option<Inst>,
779}
780
781#[cfg(test)]
782mod tests {
783 use super::*;
784
785 #[test]
786 fn an_immediate_keeps_only_the_bits_its_type_has() {
787 let byte = Type::int(8);
788 assert_eq!(Imm::int(-1, byte).unsigned(), 0xff);
789 assert_eq!(Imm::int(-1, byte).signed(byte), -1);
790 assert_eq!(Imm::int(255, byte), Imm::int(-1, byte));
791 assert_eq!(Imm::int(127, byte).signed(byte), 127);
792 assert_eq!(Imm::int(128, byte).signed(byte), -128);
793 }
794
795 #[test]
796 fn a_widest_immediate_is_not_truncated() {
797 let word = Type::int(128);
798 assert_eq!(Imm::int(i128::MIN, word).signed(word), i128::MIN);
799 assert_eq!(Imm::int(i128::MAX, word).signed(word), i128::MAX);
800 assert_eq!(Imm::int(-1, word).unsigned(), u128::MAX);
801 }
802
803 #[test]
804 fn a_one_bit_immediate_is_a_bit() {
805 let bit = Type::I1;
806 assert_eq!(Imm::int(1, bit).unsigned(), 1);
807 assert_eq!(Imm::int(3, bit).unsigned(), 1);
808 assert_eq!(Imm::int(2, bit).unsigned(), 0);
809 // The one bit is the sign bit, so the only two values are zero and minus one.
810 assert_eq!(Imm::int(1, bit).signed(bit), -1);
811 }
812
813 #[test]
814 fn a_floating_immediate_keeps_its_bits() {
815 let bits = f64::NAN.to_bits() | 0x7;
816 assert_eq!(Imm::from_bits(u128::from(bits)).bits(), u128::from(bits));
817 }
818
819 #[test]
820 fn an_instruction_with_no_results_yields_none() {
821 let inst = InstData::new(Opcode::Store);
822 assert_eq!(inst.results().count(), 0);
823 }
824
825 #[test]
826 fn results_follow_the_first_one() {
827 let mut inst = InstData::new(Opcode::SAddOverflow);
828 inst.first_result = Some(Value::new(4));
829 inst.results = 2;
830 let got: Vec<u32> = inst.results().map(Idx::raw).collect();
831 assert_eq!(got, [4, 5]);
832 }
833
834 #[test]
835 fn a_jump_says_where_it_goes() {
836 let mut inst = InstData::new(Opcode::Jump);
837 inst.extra = Extra::Targets(BlockCallList::new(Idx::new(0), Idx::new(1)));
838 assert_eq!(inst.targets().len(), 1);
839 }
840
841 #[test]
842 fn a_signature_is_built_by_saying_what_it_takes_and_returns() {
843 let sig = Signature::new()
844 .with_params(&[Type::int(32), Type::PTR])
845 .with_returns(&[Type::int(32)])
846 .variadic();
847 assert_eq!(sig.param_types().collect::<Vec<_>>(), [Type::int(32), Type::PTR]);
848 assert_eq!(sig.return_types().collect::<Vec<_>>(), [Type::int(32)]);
849 assert!(sig.variadic);
850 assert_eq!(Signature::new(), Signature::default());
851 }
852
853 #[test]
854 fn a_parameter_says_how_it_travels_and_not_only_what_it_is() {
855 let object = Abi::ByVal { size: 24, align: 8 };
856 let sig = Signature::new()
857 .and_param(Param::with_abi(Type::PTR, Abi::Sret { size: 32, align: 16 }))
858 .and_param(Param::with_abi(Type::PTR, object))
859 .and_param(Param::with_abi(Type::int(8), Abi::Zext));
860 // The types alone say `ptr, ptr, i8`, which is three of the calls in any C program and
861 // none of them the same call.
862 assert_eq!(sig.param_types().collect::<Vec<_>>(), [Type::PTR, Type::PTR, Type::int(8)]);
863 assert_eq!(sig.params[1].abi.object(), Some((24, 8)));
864 assert!(sig.params[0].abi.indirect() && !sig.params[2].abi.indirect());
865 assert_eq!(Param::new(Type::PTR).abi, Abi::Plain);
866 assert_eq!(Abi::Plain.object(), None);
867 }
868
869 #[test]
870 fn an_instruction_stays_small() {
871 // Not a promise, a tripwire. Every function in the program is a run of these, and a
872 // change that doubles this should be a change somebody decided to make.
873 assert!(size_of::<InstData>() <= 32, "{}", size_of::<InstData>());
874 assert_eq!(size_of::<ValueData>(), 16);
875 assert_eq!(size_of::<Extra>(), 12);
876 }
877}