Skip to main content

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/// The three parts of a bulk memory operation, taken apart so that nothing has to know the order.
218///
219/// It exists because the length is a number on almost every one of these and a value on a few, and
220/// a reader that takes the operands apart itself is a reader that can get the number from the
221/// payload without noticing that this one has an operand instead. Asking for this hands back the
222/// length either way and there is no shape of it that reads as a length when it is not one.
223#[derive(Clone, Copy, Debug, PartialEq, Eq)]
224pub struct Bulk {
225    /// Where it writes.
226    pub to: Value,
227    /// What it puts there: the address it reads for a copy, the byte for a fill.
228    pub with: Value,
229    /// How many bytes, where the program works the count out rather than the compiler.
230    ///
231    /// `None` is the ordinary one, where the count is [`MemInfo::size`].
232    pub length: Option<Value>,
233}
234
235/// What an access does beyond naming an address.
236#[derive(Clone, Copy, Debug, PartialEq, Eq)]
237pub struct MemInfo {
238    /// How many bytes the access covers, for the ones whose size is not their result type.
239    ///
240    /// A `load` takes its size from the type it produces. An `alloca` and a `memset` do not,
241    /// and this is where theirs is.
242    ///
243    /// Zero on a bulk operation that carries its length as an operand, which is the one shape
244    /// where this is not the count. [`Bulk`] is how those are read, and the verifier refuses a
245    /// zero here on one that has no operand, so a reader that goes through it cannot mistake the
246    /// one for the other.
247    pub size: u64,
248    /// The alignment the access is known to have, in bytes.
249    pub align: u32,
250    /// How strongly it is ordered, with [`MemOrder::NotAtomic`] for an ordinary access.
251    pub order: MemOrder,
252    /// The type-based aliasing node, if the front end knew one.
253    pub tbaa: Option<Meta>,
254    /// How many bytes of its record this access owns, counting the padding after it.
255    ///
256    /// Zero for an access that is not a member of a record, and zero when the front end was not
257    /// asked to work it out. What it is for is the init plane of
258    /// `spec/safe-memory/09-type-init-and-races.md` section 9.3: under `-fsafety-init=nopadding`
259    /// a store through a member records the padding after the member as written too, so that a
260    /// record filled a member at a time comes out whole and the ordinary reads of it, which are a
261    /// `memcmp` or a hash or a `write` of the record, are not refused.
262    ///
263    /// Only the init plane reads it. A bounds check over these bytes would be asking about bytes
264    /// the access does not touch, and a type plane write over them would be saying the padding
265    /// holds a value of the member's type, which it does not.
266    pub owns: u32,
267    /// Which `restrict` scope the access is in and which pointer it went through.
268    pub restrict: Restrict,
269}
270
271/// Which `restrict` scope an access is in, and which pointer inside that scope it went through.
272///
273/// Two small numbers, which is the whole of the mechanism. GCC spells them
274/// `MR_DEPENDENCE_CLIQUE` and `MR_DEPENDENCE_BASE` at `gcc/tree-ssa-alias.cc:2503` and the rule
275/// is one line: same clique and different base means the two accesses cannot touch the same
276/// byte, because that is exactly what `restrict` promises. A clique is one scope, numbered as
277/// lowering enters it, and a base is one `restrict` pointer declared inside it. Clique zero
278/// means nothing is known, which is what every access that is not under a `restrict` gets.
279///
280/// This is spec 9.4's scope tree rather than a blanket assumption, and it costs four bytes that
281/// were padding in [`MemInfo`] already.
282#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
283pub struct Restrict {
284    /// The scope, with zero meaning no information.
285    pub clique: u16,
286    /// The pointer within that scope, which only means anything when the clique is not zero.
287    pub base: u16,
288}
289
290impl Restrict {
291    /// No information, which is what an access outside any `restrict` scope carries.
292    pub const NONE: Self = Self { clique: 0, base: 0 };
293
294    /// Whether `restrict` says these two accesses cannot touch the same byte.
295    ///
296    /// Only accesses. GCC's PR71062 is what happens when this answer is used to fold a
297    /// comparison of the two pointers: `restrict` constrains what is read and written through a
298    /// pointer and says nothing about what the pointer's value is, so two pointers that may not
299    /// be used to reach the same object can still compare equal. A rule that folds `p == q` to
300    /// false on the strength of this is wrong.
301    #[must_use]
302    pub const fn disjoint(self, other: Self) -> bool {
303        self.clique != 0 && self.clique == other.clique && self.base != other.base
304    }
305}
306
307/// A metadata node, in the module's table.
308pub type Meta = Idx<MetaNode>;
309
310/// A node of the metadata graph.
311///
312/// Two kinds share the one table and the one numbering, because both of them are the compiler's
313/// interned type universe seen from a different side and a reader chasing a `!3` should not have
314/// to know which table it came out of.
315#[derive(Clone, Copy, Debug, PartialEq, Eq)]
316pub enum MetaNode {
317    /// What aliasing needs: a type, and where it sits in the tree of types.
318    Tbaa(TbaaNode),
319    /// What the type plane needs: one entry in the vocabulary its bytes are written in.
320    Plane(PlaneNode),
321}
322
323impl MetaNode {
324    /// The aliasing node this is, or `None` when it is a plane entry.
325    #[must_use]
326    pub const fn tbaa(self) -> Option<TbaaNode> {
327        match self {
328            Self::Tbaa(node) => Some(node),
329            Self::Plane(_) => None,
330        }
331    }
332
333    /// The plane entry this is, or `None` when it is an aliasing node.
334    #[must_use]
335    pub const fn plane(self) -> Option<PlaneNode> {
336        match self {
337            Self::Plane(node) => Some(node),
338            Self::Tbaa(_) => None,
339        }
340    }
341
342    /// The node one level up, which a plane entry never has.
343    ///
344    /// The tree is the aliasing tree and a plane entry is not in it. A plane entry that names a
345    /// type points at a node of that tree, and that is a reference and not a parent: the walk
346    /// that answers an aliasing query has no business leaving the tree it is walking.
347    #[must_use]
348    pub const fn parent(self) -> Option<Meta> {
349        match self {
350            Self::Tbaa(node) => node.parent,
351            Self::Plane(_) => None,
352        }
353    }
354
355    /// The node it points at, which is the parent of an aliasing node and the type of a plane
356    /// entry, and is what has to come earlier in the table than the node itself.
357    #[must_use]
358    pub const fn points_at(self) -> Option<Meta> {
359        match self {
360            Self::Tbaa(node) => node.parent,
361            Self::Plane(PlaneNode::Type(node)) => Some(node),
362            Self::Plane(_) => None,
363        }
364    }
365}
366
367/// A node of the type based aliasing tree.
368///
369/// The tree this forms is checked by the verifier, since a cycle in it would make the aliasing
370/// query that walks it not terminate, and the place to find that out is here and not there.
371#[derive(Clone, Copy, Debug, PartialEq, Eq)]
372pub struct TbaaNode {
373    /// What this node is called, which is what the printer writes and the parser reads.
374    pub name: Symbol,
375    /// The node one level up, with the root having none.
376    pub parent: Option<Meta>,
377    /// The offset within the parent, for a member of a struct type.
378    pub offset: u64,
379}
380
381/// One entry in the type plane's vocabulary, per `spec/safe-memory/09-type-init-and-races.md`
382/// section 9.1.
383///
384/// The plane maps every byte to one of these, so this is what a `meta_type` writes and what a
385/// `check_type` is asking about. Three of the four are the distinguished values that document
386/// says the plane has beyond the types themselves, and they are why the plane needs a node kind
387/// of its own rather than pointing straight at an aliasing node: there is no aliasing node for
388/// "nobody has stored here yet".
389#[derive(Clone, Copy, Debug, PartialEq, Eq)]
390pub enum PlaneNode {
391    /// A type, named by the aliasing node that is that type.
392    ///
393    /// The same node the front end already interned, so the plane's vocabulary is exactly the
394    /// compiler's and a report can name a type in the spelling the source used.
395    Type(Meta),
396    /// Bytes nothing has stored through, or stored from an untyped source.
397    ///
398    /// Compatible with every access, because storage with no declared type takes its effective
399    /// type from the store, which is C's rule and is also the only choice that does not fire at
400    /// every boundary with uninstrumented code.
401    NoType,
402    /// Bytes stored through a character type, which is compatible with every access.
403    ///
404    /// This is what makes the byte-wise copy idiom work. C 6.5 says a character access is
405    /// always permitted and that a store through a character lvalue does not set an effective
406    /// type, so the plane says `character` over those bytes and the later read of the field
407    /// still passes.
408    Character,
409    /// Byte `k` of a pointer shaped word.
410    ///
411    /// A pointer is not one type over its bytes, it is a word whose bytes are only meaningful
412    /// together, so reading four bytes out of the middle of one is a different thing from
413    /// reading four bytes of an `int` and the plane has to be able to say which byte it is.
414    PointerSlot(u8),
415}
416
417/// What a call needs beyond its arguments.
418#[derive(Clone, Copy, Debug, PartialEq, Eq)]
419pub struct CallInfo {
420    /// The name, for a direct call. `None` for a call through an address, where the address is
421    /// the first operand.
422    pub callee: Option<Symbol>,
423    /// The signature it is called with, which is where the ABI attributes are.
424    pub signature: Sig,
425    /// What the ABI asks of the arguments the signature does not name, one entry for each of
426    /// them.
427    ///
428    /// Only a variadic call has any, because only a variadic call passes an argument no
429    /// parameter stands for, and it is empty when every one of them travels as the value in
430    /// hand, which is nearly always. A structure the classification puts in the argument area
431    /// is the case it exists for: the bytes travel and there is no parameter to hang the
432    /// [`Abi::ByVal`] on, so it hangs here instead.
433    pub varargs: AbiList,
434}
435
436/// A signature, in the function's table.
437pub type Sig = Idx<Signature>;
438
439/// What a `switch` needs beyond the value it switches on.
440#[derive(Clone, Copy, Debug, PartialEq, Eq)]
441pub struct SwitchInfo {
442    /// The targets, with the default first and one for each case after it.
443    pub targets: BlockCallList,
444    /// The case values, one for each target after the default.
445    pub cases: ImmList,
446}
447
448/// What an object read off a variable argument list is.
449///
450/// The access says how many bytes it is and what it is aligned to, which is the whole of what an
451/// object the convention put in the caller's argument area needs: it is there, and those two say
452/// where the argument behind it starts. An object that travelled in registers is not there at all.
453/// It is in the callee's own register save area, in as many places as it has eightbytes, and which
454/// register file each of those came from is not something the size and the alignment say. So the
455/// slots say it, and they are empty for the object that went in memory.
456///
457/// The classification is the front end's, because it is the one that still has the type. By the
458/// time an instruction reaches a backend the type is a size and an alignment, and the algorithm in
459/// section 3.5.7 of the psABI wants more than that.
460#[derive(Clone, Copy, Debug, PartialEq, Eq)]
461pub struct VaInfo {
462    /// The object, as any other access describes one.
463    pub mem: Idx<MemInfo>,
464    /// Where each of its eightbytes travelled, or nothing at all for one that travelled whole in
465    /// the caller's memory.
466    pub slots: SlotList,
467}
468
469/// What inline assembly needs.
470///
471/// The semantics belong to the inline assembly document. What is here is the shape: a
472/// template, the constraints, the clobbers, and the successors that make `asm goto` the one
473/// instruction whose being a terminator is a property of the instruction and not the opcode.
474#[derive(Clone, Copy, Debug, PartialEq, Eq)]
475pub struct AsmInfo {
476    /// The template string, as written.
477    pub template: Symbol,
478    /// The constraint list, as written.
479    pub constraints: Symbol,
480    /// The clobber list, as written.
481    pub clobbers: Symbol,
482    /// The labels, which are empty for everything except `asm goto`.
483    pub targets: BlockCallList,
484}
485
486/// Everything an instruction carries that is not a value operand.
487///
488/// Anything that fits in eight bytes is here and anything larger is an index into a side
489/// table, so that the common instructions, which are the arithmetic ones carrying nothing at
490/// all, do not pay for the rare ones.
491#[derive(Clone, Copy, Debug, PartialEq, Eq)]
492pub enum Extra {
493    /// Nothing, which is most instructions.
494    None,
495    /// A constant, for `iconst`, `fconst` and `splat`.
496    Imm(Idx<Imm>),
497    /// A name, for `global_addr` and for a target-specific intrinsic.
498    Symbol(Symbol),
499    /// Which comparison, for `icmp`.
500    IntPred(IntPred),
501    /// Which comparison, for `fcmp`.
502    FloatPred(FloatPred),
503    /// An access, for the loads, the stores, the copies and `alloca`.
504    Mem(Idx<MemInfo>),
505    /// An atomic read-modify-write, which is an access and which operation.
506    Rmw(RmwOp, Idx<MemInfo>),
507    /// A barrier's ordering, for `fence`.
508    Order(MemOrder),
509    /// What a `prefetch` is a hint about, which is a read or a write and how much locality.
510    Prefetch(PrefetchHint),
511    /// How many frames up to walk, for `frame_address` and `return_address`.
512    Depth(u32),
513    /// Which question an `object_size` asks, from zero to three.
514    Question(u8),
515    /// The targets of a branch, with the default first for a `switch`.
516    Targets(BlockCallList),
517    /// A call.
518    Call(Idx<CallInfo>),
519    /// A `switch`, which is targets and the values that select them.
520    Switch(Idx<SwitchInfo>),
521    /// Inline assembly.
522    Asm(Idx<AsmInfo>),
523    /// An object read off a variable argument list, which is an access and how it travelled.
524    VaObject(Idx<VaInfo>),
525    /// What kind of storage an instance is, for `meta_begin`.
526    Class(StorageClass),
527    /// Who a range went to, for `meta_transfer`.
528    Owner(Owner),
529    /// A metadata node, for `meta_type`, which is the one plane write that names a type.
530    Node(Meta),
531    /// Why an exemption was declared, for `safe_region_begin`.
532    Reason(Symbol),
533}
534
535impl Extra {
536    /// Which shape this is, without the payload.
537    ///
538    /// The verifier compares this with [`Opcode::extra_kind`], because an instruction carrying
539    /// the payload of some other opcode prints as text the parser cannot read back.
540    #[must_use]
541    pub const fn kind(self) -> ExtraKind {
542        match self {
543            Self::None => ExtraKind::None,
544            Self::Imm(_) => ExtraKind::Imm,
545            Self::Symbol(_) => ExtraKind::Symbol,
546            Self::IntPred(_) => ExtraKind::IntPred,
547            Self::FloatPred(_) => ExtraKind::FloatPred,
548            Self::Mem(_) => ExtraKind::Mem,
549            Self::Rmw(..) => ExtraKind::Rmw,
550            Self::Order(_) => ExtraKind::Order,
551            Self::Prefetch(_) => ExtraKind::Prefetch,
552            Self::Depth(_) => ExtraKind::Depth,
553            Self::Question(_) => ExtraKind::Question,
554            Self::Targets(_) => ExtraKind::Targets,
555            Self::Call(_) => ExtraKind::Call,
556            Self::Switch(_) => ExtraKind::Switch,
557            Self::Asm(_) => ExtraKind::Asm,
558            Self::VaObject(_) => ExtraKind::VaObject,
559            Self::Class(_) => ExtraKind::Class,
560            Self::Owner(_) => ExtraKind::Owner,
561            Self::Node(_) => ExtraKind::Node,
562            Self::Reason(_) => ExtraKind::Reason,
563        }
564    }
565}
566
567/// One instruction.
568///
569/// There is no result type here. Each result is a value in the function's value table and the
570/// type is on the value, which means a reader asking what an instruction produces asks the
571/// same question about `add` as about `call`, and there is no second copy of the type to
572/// disagree with the first.
573#[derive(Clone, Copy, Debug, PartialEq, Eq)]
574pub struct InstData {
575    /// Which instruction this is.
576    pub opcode: Opcode,
577    /// What the optimizer is licensed to assume about it.
578    pub flags: Flags,
579    /// How many values it produces.
580    pub results: u8,
581    /// The first of them, with the rest following it in the value table.
582    pub first_result: Option<Value>,
583    /// Its value operands.
584    pub args: ValueList,
585    /// Everything else it carries.
586    pub extra: Extra,
587}
588
589impl InstData {
590    /// An instruction with no operands, no flags, no results and nothing extra.
591    #[must_use]
592    pub const fn new(opcode: Opcode) -> Self {
593        Self {
594            opcode,
595            flags: Flags::NONE,
596            results: 0,
597            first_result: None,
598            args: ValueList::EMPTY,
599            extra: Extra::None,
600        }
601    }
602
603    /// The values it produces, in order.
604    pub fn results(&self) -> impl Iterator<Item = Value> + use<> {
605        let first = self.first_result.map_or(0, Idx::raw);
606        (0..u32::from(self.results)).map(move |offset| Value::new(first + offset))
607    }
608
609    /// The run of targets it branches to, which is empty when it does not branch.
610    ///
611    /// A `switch` keeps its targets in a side table, so this reads `Extra::Targets` only and
612    /// the function is what answers for the rest.
613    #[must_use]
614    pub fn targets(&self) -> BlockCallList {
615        match self.extra {
616            Extra::Targets(targets) => targets,
617            _ => BlockCallList::EMPTY,
618        }
619    }
620}
621
622/// How one parameter or one return value travels, beyond what its type says.
623///
624/// The IR's types are the machine's and not C's, so a `ptr` parameter says nothing about
625/// whether the pointer is the argument or whether the object it points at is, and an `i8` says
626/// nothing about which half of the register above it the callee may read. Both are the ABI's
627/// answer rather than the type's, which is why they are here and not on [`Type`].
628///
629/// A signature carrying one of these has already had the ABI applied to it. What the walk to
630/// the IR builds first is the C-level form, where every parameter is [`Abi::Plain`], and the
631/// classification in `rucc-target` is what turns one into the other.
632#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
633pub enum Abi {
634    /// The value itself, in the type it is written as.
635    #[default]
636    Plain,
637    /// An integer narrower than a register, with the bits above it its own sign.
638    ///
639    /// Which of these an ABI asks for is not a property of the value: `unsigned char` is
640    /// [`Abi::Sext`] on the Darwin ABIs and [`Abi::Zext`] elsewhere, and on SysV neither the
641    /// caller nor the callee may assume anything about those bits at all.
642    Sext,
643    /// An integer narrower than a register, with zeroes above it.
644    Zext,
645    /// The bytes of the object the pointer points at, in the argument area, with no address
646    /// travelling anywhere.
647    ///
648    /// The caller makes the copy the callee is free to write to, which is what makes this a C
649    /// call by value rather than a pointer the callee must not keep.
650    ByVal {
651        /// How many bytes travel.
652        size: u64,
653        /// What the copy is aligned to, which is the C alignment of the type and not the
654        /// pointer's.
655        align: u32,
656    },
657    /// Somewhere for the return value to go, whose address the caller passes as the first
658    /// argument because the value does not fit in the registers a return comes back in.
659    Sret {
660        /// How many bytes the callee writes.
661        size: u64,
662        /// What the space is aligned to.
663        align: u32,
664    },
665}
666
667impl Abi {
668    /// Whether this describes an object behind a pointer rather than the value in hand.
669    #[must_use]
670    pub const fn indirect(self) -> bool {
671        matches!(self, Self::ByVal { .. } | Self::Sret { .. })
672    }
673
674    /// The size and alignment of that object, for the two that have one.
675    #[must_use]
676    pub const fn object(self) -> Option<(u64, u32)> {
677        match self {
678            Self::ByVal { size, align } | Self::Sret { size, align } => Some((size, align)),
679            _ => None,
680        }
681    }
682}
683
684/// One parameter, or one return value: a type and how it travels.
685#[derive(Clone, Copy, Debug, PartialEq, Eq)]
686pub struct Param {
687    /// The type the IR sees, which for the indirect forms is `ptr`.
688    pub ty: Type,
689    /// What the ABI asks of it.
690    pub abi: Abi,
691}
692
693impl Param {
694    /// A parameter of this type, in its C-level form.
695    #[must_use]
696    pub const fn new(ty: Type) -> Self {
697        Self { ty, abi: Abi::Plain }
698    }
699
700    /// A parameter of this type travelling this way.
701    #[must_use]
702    pub const fn with_abi(ty: Type, abi: Abi) -> Self {
703        Self { ty, abi }
704    }
705}
706
707/// What a function takes and returns.
708///
709/// A signature is not a type. Nothing in the IR has a function type, because a `ptr` has no
710/// pointee and there is nothing else a function type could sit on. A `call_indirect` names the
711/// signature it is called with, and that is where the ABI attributes are read from.
712#[derive(Clone, Debug, PartialEq, Eq, Default)]
713pub struct Signature {
714    /// What it takes, in their C-level form until the ABI has been applied.
715    pub params: Vec<Param>,
716    /// What it returns, which is empty for a `void` function and for one whose return value
717    /// comes back through an [`Abi::Sret`] parameter.
718    pub returns: Vec<Param>,
719    /// Whether it takes arguments beyond the ones named.
720    pub variadic: bool,
721}
722
723impl Signature {
724    /// A signature taking and returning nothing.
725    #[must_use]
726    pub fn new() -> Self {
727        Self::default()
728    }
729
730    /// The same signature with these parameters, each in its C-level form.
731    #[must_use]
732    pub fn with_params(mut self, params: &[Type]) -> Self {
733        self.params = params.iter().copied().map(Param::new).collect();
734        self
735    }
736
737    /// The same signature returning these, each in its C-level form.
738    #[must_use]
739    pub fn with_returns(mut self, returns: &[Type]) -> Self {
740        self.returns = returns.iter().copied().map(Param::new).collect();
741        self
742    }
743
744    /// The same signature with one more parameter, travelling the way the ABI said.
745    #[must_use]
746    pub fn and_param(mut self, param: Param) -> Self {
747        self.params.push(param);
748        self
749    }
750
751    /// The same signature with one more return value, travelling the way the ABI said.
752    #[must_use]
753    pub fn and_return(mut self, param: Param) -> Self {
754        self.returns.push(param);
755        self
756    }
757
758    /// The types it takes, without what the ABI asks of them.
759    pub fn param_types(&self) -> impl Iterator<Item = Type> + use<'_> {
760        self.params.iter().map(|param| param.ty)
761    }
762
763    /// The types it returns.
764    pub fn return_types(&self) -> impl Iterator<Item = Type> + use<'_> {
765        self.returns.iter().map(|param| param.ty)
766    }
767
768    /// The same signature, variadic.
769    #[must_use]
770    pub fn variadic(mut self) -> Self {
771        self.variadic = true;
772        self
773    }
774}
775
776/// One basic block: parameters, then instructions, then exactly one terminator.
777///
778/// The instructions are a doubly linked list rather than a vector, so that inserting one in
779/// the middle of a block does not move the ones after it. An optimizer does that constantly,
780/// and a move would invalidate every [`Inst`] anybody was holding.
781#[derive(Clone, Debug, Default, PartialEq, Eq)]
782pub struct BlockData {
783    /// The values arriving here, which is what other IRs spell as phi nodes.
784    ///
785    /// A `Vec` and not a run in a pool, because SSA construction adds a parameter to a loop
786    /// header long after the blocks that come after it have been built, and a run in a pool
787    /// cannot grow in the middle.
788    pub params: Vec<Value>,
789    /// The first instruction, or `None` for a block nothing has been put in yet.
790    pub first: Option<Inst>,
791    /// The last instruction, which is the terminator once the block is finished.
792    pub last: Option<Inst>,
793    /// The block before this one in layout order.
794    pub prev: Option<Block>,
795    /// The block after it.
796    pub next: Option<Block>,
797}
798
799/// Where one instruction sits.
800#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
801pub struct InstLayout {
802    /// The block it is in, or `None` if it has been made and not yet inserted.
803    pub block: Option<Block>,
804    /// The instruction before it in that block.
805    pub prev: Option<Inst>,
806    /// The instruction after it.
807    pub next: Option<Inst>,
808}
809
810#[cfg(test)]
811mod tests {
812    use super::*;
813
814    #[test]
815    fn an_immediate_keeps_only_the_bits_its_type_has() {
816        let byte = Type::int(8);
817        assert_eq!(Imm::int(-1, byte).unsigned(), 0xff);
818        assert_eq!(Imm::int(-1, byte).signed(byte), -1);
819        assert_eq!(Imm::int(255, byte), Imm::int(-1, byte));
820        assert_eq!(Imm::int(127, byte).signed(byte), 127);
821        assert_eq!(Imm::int(128, byte).signed(byte), -128);
822    }
823
824    #[test]
825    fn a_widest_immediate_is_not_truncated() {
826        let word = Type::int(128);
827        assert_eq!(Imm::int(i128::MIN, word).signed(word), i128::MIN);
828        assert_eq!(Imm::int(i128::MAX, word).signed(word), i128::MAX);
829        assert_eq!(Imm::int(-1, word).unsigned(), u128::MAX);
830    }
831
832    #[test]
833    fn a_one_bit_immediate_is_a_bit() {
834        let bit = Type::I1;
835        assert_eq!(Imm::int(1, bit).unsigned(), 1);
836        assert_eq!(Imm::int(3, bit).unsigned(), 1);
837        assert_eq!(Imm::int(2, bit).unsigned(), 0);
838        // The one bit is the sign bit, so the only two values are zero and minus one.
839        assert_eq!(Imm::int(1, bit).signed(bit), -1);
840    }
841
842    #[test]
843    fn a_floating_immediate_keeps_its_bits() {
844        let bits = f64::NAN.to_bits() | 0x7;
845        assert_eq!(Imm::from_bits(u128::from(bits)).bits(), u128::from(bits));
846    }
847
848    #[test]
849    fn an_instruction_with_no_results_yields_none() {
850        let inst = InstData::new(Opcode::Store);
851        assert_eq!(inst.results().count(), 0);
852    }
853
854    #[test]
855    fn results_follow_the_first_one() {
856        let mut inst = InstData::new(Opcode::SAddOverflow);
857        inst.first_result = Some(Value::new(4));
858        inst.results = 2;
859        let got: Vec<u32> = inst.results().map(Idx::raw).collect();
860        assert_eq!(got, [4, 5]);
861    }
862
863    #[test]
864    fn a_jump_says_where_it_goes() {
865        let mut inst = InstData::new(Opcode::Jump);
866        inst.extra = Extra::Targets(BlockCallList::new(Idx::new(0), Idx::new(1)));
867        assert_eq!(inst.targets().len(), 1);
868    }
869
870    #[test]
871    fn a_signature_is_built_by_saying_what_it_takes_and_returns() {
872        let sig = Signature::new()
873            .with_params(&[Type::int(32), Type::PTR])
874            .with_returns(&[Type::int(32)])
875            .variadic();
876        assert_eq!(sig.param_types().collect::<Vec<_>>(), [Type::int(32), Type::PTR]);
877        assert_eq!(sig.return_types().collect::<Vec<_>>(), [Type::int(32)]);
878        assert!(sig.variadic);
879        assert_eq!(Signature::new(), Signature::default());
880    }
881
882    #[test]
883    fn a_parameter_says_how_it_travels_and_not_only_what_it_is() {
884        let object = Abi::ByVal { size: 24, align: 8 };
885        let sig = Signature::new()
886            .and_param(Param::with_abi(Type::PTR, Abi::Sret { size: 32, align: 16 }))
887            .and_param(Param::with_abi(Type::PTR, object))
888            .and_param(Param::with_abi(Type::int(8), Abi::Zext));
889        // The types alone say `ptr, ptr, i8`, which is three of the calls in any C program and
890        // none of them the same call.
891        assert_eq!(sig.param_types().collect::<Vec<_>>(), [Type::PTR, Type::PTR, Type::int(8)]);
892        assert_eq!(sig.params[1].abi.object(), Some((24, 8)));
893        assert!(sig.params[0].abi.indirect() && !sig.params[2].abi.indirect());
894        assert_eq!(Param::new(Type::PTR).abi, Abi::Plain);
895        assert_eq!(Abi::Plain.object(), None);
896    }
897
898    #[test]
899    fn an_instruction_stays_small() {
900        // Not a promise, a tripwire. Every function in the program is a run of these, and a
901        // change that doubles this should be a change somebody decided to make.
902        assert!(size_of::<InstData>() <= 32, "{}", size_of::<InstData>());
903        assert_eq!(size_of::<ValueData>(), 16);
904        assert_eq!(size_of::<Extra>(), 12);
905    }
906}