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 crate::{ExtraKind, Flags, FloatPred, IntPred, MemOrder, Opcode, RmwOp, Type};
19
20/// One value: the result of an instruction, or a parameter of a block.
21pub type Value = Idx<ValueData>;
22/// One instruction, in the function that owns it.
23pub type Inst = Idx<InstData>;
24/// One basic block, in the function that owns it.
25pub type Block = Idx<BlockData>;
26
27/// The table of references to values, which is what an operand list is a run of.
28#[derive(Debug)]
29pub struct ValueRef;
30/// A run of value operands.
31pub type ValueList = IdxRange<ValueRef>;
32/// A run of branch targets, which is what a terminator's successors are.
33pub type BlockCallList = IdxRange<BlockCall>;
34/// A run of immediates, which is what a `switch` holds its case values in.
35pub type ImmList = IdxRange<Imm>;
36
37/// A constant, in the immediate table.
38///
39/// The bits and nothing else. An integer is stored two's complement in as many of the low bits
40/// as its type is wide, and a floating point value is stored as its bit pattern, so the same
41/// table holds both and the type on the result says how to read it. That keeps a bit-preserving
42/// answer for a NaN payload, which a value of a Rust floating type would not.
43#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
44pub struct Imm(u128);
45
46impl Imm {
47    /// The bits, as they are stored.
48    #[must_use]
49    pub const fn bits(self) -> u128 {
50        self.0
51    }
52
53    /// An immediate holding these bits.
54    #[must_use]
55    pub const fn from_bits(bits: u128) -> Self {
56        Self(bits)
57    }
58
59    /// An integer, with the bits above `ty` cleared.
60    ///
61    /// A value is stored in exactly the width its type has, so two immediates are equal when
62    /// they are the same value, which is what lets an equality on the table stand in for an
63    /// equality on the numbers.
64    ///
65    /// # Panics
66    ///
67    /// Panics if `ty` is not an integer type.
68    #[must_use]
69    pub fn int(value: i128, ty: Type) -> Self {
70        assert!(ty.is_int(), "an integer immediate needs an integer type");
71        Self(value as u128 & mask(ty.bits()))
72    }
73
74    /// The value read as unsigned.
75    #[must_use]
76    pub const fn unsigned(self) -> u128 {
77        self.0
78    }
79
80    /// The value read as signed, with the sign bit of `ty` extended.
81    ///
82    /// # Panics
83    ///
84    /// Panics if `ty` is not an integer type.
85    #[must_use]
86    pub fn signed(self, ty: Type) -> i128 {
87        assert!(ty.is_int(), "an integer immediate needs an integer type");
88        let spare = 128 - ty.bits();
89        // Shifting left and then arithmetic right is the branch-free way to sign extend from
90        // an arbitrary width, and it is correct for a width of 128 because the shift is zero.
91        ((self.0 << spare) as i128) >> spare
92    }
93}
94
95/// The low `bits` bits set, and a width of 128 meaning all of them.
96fn mask(bits: u32) -> u128 {
97    if bits >= 128 { u128::MAX } else { (1u128 << bits) - 1 }
98}
99
100/// A branch target, and the values passed to it.
101///
102/// This is the whole reason there are no phi nodes. The arguments are here, in the branch,
103/// beside the block they go to, so removing a predecessor is one edit in one place and there
104/// is no second list anywhere that has to be kept in step with this one.
105#[derive(Clone, Copy, Debug, PartialEq, Eq)]
106pub struct BlockCall {
107    /// Where control goes.
108    pub block: Block,
109    /// What is passed, one for each of the block's parameters.
110    pub args: ValueList,
111}
112
113/// What defines a value.
114#[derive(Clone, Copy, Debug, PartialEq, Eq)]
115pub enum Def {
116    /// The result of an instruction, at this position among its results.
117    Result {
118        /// The instruction.
119        inst: Inst,
120        /// Which of its results this is.
121        index: u8,
122    },
123    /// A parameter of a block, at this position among its parameters.
124    Param {
125        /// The block.
126        block: Block,
127        /// Which of its parameters this is.
128        index: u32,
129    },
130}
131
132/// One value.
133#[derive(Clone, Copy, Debug, PartialEq, Eq)]
134pub struct ValueData {
135    /// Its type.
136    pub ty: Type,
137    /// Where it comes from.
138    pub def: Def,
139}
140
141/// What an access does beyond naming an address.
142#[derive(Clone, Copy, Debug, PartialEq, Eq)]
143pub struct MemInfo {
144    /// How many bytes the access covers, for the ones whose size is not their result type.
145    ///
146    /// A `load` takes its size from the type it produces. An `alloca` and a `memset` do not,
147    /// and this is where theirs is.
148    pub size: u64,
149    /// The alignment the access is known to have, in bytes.
150    pub align: u32,
151    /// How strongly it is ordered, with [`MemOrder::NotAtomic`] for an ordinary access.
152    pub order: MemOrder,
153    /// The type-based aliasing node, if the front end knew one.
154    pub tbaa: Option<Meta>,
155}
156
157/// A metadata node, in the module's table.
158pub type Meta = Idx<MetaNode>;
159
160/// A node of the metadata graph, which for now is only what aliasing needs.
161///
162/// The tree this forms is checked by the verifier, since a cycle in it would make the aliasing
163/// query that walks it not terminate, and the place to find that out is here and not there.
164#[derive(Clone, Copy, Debug, PartialEq, Eq)]
165pub struct MetaNode {
166    /// What this node is called, which is what the printer writes and the parser reads.
167    pub name: Symbol,
168    /// The node one level up, with the root having none.
169    pub parent: Option<Meta>,
170    /// The offset within the parent, for a member of a struct type.
171    pub offset: u64,
172}
173
174/// What a call needs beyond its arguments.
175#[derive(Clone, Copy, Debug, PartialEq, Eq)]
176pub struct CallInfo {
177    /// The name, for a direct call. `None` for a call through an address, where the address is
178    /// the first operand.
179    pub callee: Option<Symbol>,
180    /// The signature it is called with, which is where the ABI attributes are.
181    pub signature: Sig,
182}
183
184/// A signature, in the function's table.
185pub type Sig = Idx<Signature>;
186
187/// What a `switch` needs beyond the value it switches on.
188#[derive(Clone, Copy, Debug, PartialEq, Eq)]
189pub struct SwitchInfo {
190    /// The targets, with the default first and one for each case after it.
191    pub targets: BlockCallList,
192    /// The case values, one for each target after the default.
193    pub cases: ImmList,
194}
195
196/// What inline assembly needs.
197///
198/// The semantics belong to the inline assembly document. What is here is the shape: a
199/// template, the constraints, the clobbers, and the successors that make `asm goto` the one
200/// instruction whose being a terminator is a property of the instruction and not the opcode.
201#[derive(Clone, Copy, Debug, PartialEq, Eq)]
202pub struct AsmInfo {
203    /// The template string, as written.
204    pub template: Symbol,
205    /// The constraint list, as written.
206    pub constraints: Symbol,
207    /// The clobber list, as written.
208    pub clobbers: Symbol,
209    /// The labels, which are empty for everything except `asm goto`.
210    pub targets: BlockCallList,
211}
212
213/// Everything an instruction carries that is not a value operand.
214///
215/// Anything that fits in eight bytes is here and anything larger is an index into a side
216/// table, so that the common instructions, which are the arithmetic ones carrying nothing at
217/// all, do not pay for the rare ones.
218#[derive(Clone, Copy, Debug, PartialEq, Eq)]
219pub enum Extra {
220    /// Nothing, which is most instructions.
221    None,
222    /// A constant, for `iconst`, `fconst` and `splat`.
223    Imm(Idx<Imm>),
224    /// A name, for `global_addr` and for a target-specific intrinsic.
225    Symbol(Symbol),
226    /// Which comparison, for `icmp`.
227    IntPred(IntPred),
228    /// Which comparison, for `fcmp`.
229    FloatPred(FloatPred),
230    /// An access, for the loads, the stores, the copies and `alloca`.
231    Mem(Idx<MemInfo>),
232    /// An atomic read-modify-write, which is an access and which operation.
233    Rmw(RmwOp, Idx<MemInfo>),
234    /// A barrier's ordering, for `fence`.
235    Order(MemOrder),
236    /// The targets of a branch, with the default first for a `switch`.
237    Targets(BlockCallList),
238    /// A call.
239    Call(Idx<CallInfo>),
240    /// A `switch`, which is targets and the values that select them.
241    Switch(Idx<SwitchInfo>),
242    /// Inline assembly.
243    Asm(Idx<AsmInfo>),
244}
245
246impl Extra {
247    /// Which shape this is, without the payload.
248    ///
249    /// The verifier compares this with [`Opcode::extra_kind`], because an instruction carrying
250    /// the payload of some other opcode prints as text the parser cannot read back.
251    #[must_use]
252    pub const fn kind(self) -> ExtraKind {
253        match self {
254            Self::None => ExtraKind::None,
255            Self::Imm(_) => ExtraKind::Imm,
256            Self::Symbol(_) => ExtraKind::Symbol,
257            Self::IntPred(_) => ExtraKind::IntPred,
258            Self::FloatPred(_) => ExtraKind::FloatPred,
259            Self::Mem(_) => ExtraKind::Mem,
260            Self::Rmw(..) => ExtraKind::Rmw,
261            Self::Order(_) => ExtraKind::Order,
262            Self::Targets(_) => ExtraKind::Targets,
263            Self::Call(_) => ExtraKind::Call,
264            Self::Switch(_) => ExtraKind::Switch,
265            Self::Asm(_) => ExtraKind::Asm,
266        }
267    }
268}
269
270/// One instruction.
271///
272/// There is no result type here. Each result is a value in the function's value table and the
273/// type is on the value, which means a reader asking what an instruction produces asks the
274/// same question about `add` as about `call`, and there is no second copy of the type to
275/// disagree with the first.
276#[derive(Clone, Copy, Debug, PartialEq, Eq)]
277pub struct InstData {
278    /// Which instruction this is.
279    pub opcode: Opcode,
280    /// What the optimizer is licensed to assume about it.
281    pub flags: Flags,
282    /// How many values it produces.
283    pub results: u8,
284    /// The first of them, with the rest following it in the value table.
285    pub first_result: Option<Value>,
286    /// Its value operands.
287    pub args: ValueList,
288    /// Everything else it carries.
289    pub extra: Extra,
290}
291
292impl InstData {
293    /// An instruction with no operands, no flags, no results and nothing extra.
294    #[must_use]
295    pub const fn new(opcode: Opcode) -> Self {
296        Self {
297            opcode,
298            flags: Flags::NONE,
299            results: 0,
300            first_result: None,
301            args: ValueList::EMPTY,
302            extra: Extra::None,
303        }
304    }
305
306    /// The values it produces, in order.
307    pub fn results(&self) -> impl Iterator<Item = Value> + use<> {
308        let first = self.first_result.map_or(0, Idx::raw);
309        (0..u32::from(self.results)).map(move |offset| Value::new(first + offset))
310    }
311
312    /// The run of targets it branches to, which is empty when it does not branch.
313    ///
314    /// A `switch` keeps its targets in a side table, so this reads `Extra::Targets` only and
315    /// the function is what answers for the rest.
316    #[must_use]
317    pub fn targets(&self) -> BlockCallList {
318        match self.extra {
319            Extra::Targets(targets) => targets,
320            _ => BlockCallList::EMPTY,
321        }
322    }
323}
324
325/// What a function takes and returns.
326///
327/// A signature is not a type. Nothing in the IR has a function type, because a `ptr` has no
328/// pointee and there is nothing else a function type could sit on. A `call_indirect` names the
329/// signature it is called with, and that is where the ABI attributes are read from.
330#[derive(Clone, Debug, PartialEq, Eq, Default)]
331pub struct Signature {
332    /// What it takes, in their C-level form before the ABI has been applied.
333    pub params: Vec<Type>,
334    /// What it returns, which is empty for a `void` function.
335    pub returns: Vec<Type>,
336    /// Whether it takes arguments beyond the ones named.
337    pub variadic: bool,
338}
339
340impl Signature {
341    /// A signature taking and returning nothing.
342    #[must_use]
343    pub fn new() -> Self {
344        Self::default()
345    }
346
347    /// The same signature with these parameters.
348    #[must_use]
349    pub fn with_params(mut self, params: &[Type]) -> Self {
350        self.params = params.to_vec();
351        self
352    }
353
354    /// The same signature returning these.
355    #[must_use]
356    pub fn with_returns(mut self, returns: &[Type]) -> Self {
357        self.returns = returns.to_vec();
358        self
359    }
360
361    /// The same signature, variadic.
362    #[must_use]
363    pub fn variadic(mut self) -> Self {
364        self.variadic = true;
365        self
366    }
367}
368
369/// One basic block: parameters, then instructions, then exactly one terminator.
370///
371/// The instructions are a doubly linked list rather than a vector, so that inserting one in
372/// the middle of a block does not move the ones after it. An optimizer does that constantly,
373/// and a move would invalidate every [`Inst`] anybody was holding.
374#[derive(Clone, Debug, Default, PartialEq, Eq)]
375pub struct BlockData {
376    /// The values arriving here, which is what other IRs spell as phi nodes.
377    ///
378    /// A `Vec` and not a run in a pool, because SSA construction adds a parameter to a loop
379    /// header long after the blocks that come after it have been built, and a run in a pool
380    /// cannot grow in the middle.
381    pub params: Vec<Value>,
382    /// The first instruction, or `None` for a block nothing has been put in yet.
383    pub first: Option<Inst>,
384    /// The last instruction, which is the terminator once the block is finished.
385    pub last: Option<Inst>,
386    /// The block before this one in layout order.
387    pub prev: Option<Block>,
388    /// The block after it.
389    pub next: Option<Block>,
390}
391
392/// Where one instruction sits.
393#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
394pub struct InstLayout {
395    /// The block it is in, or `None` if it has been made and not yet inserted.
396    pub block: Option<Block>,
397    /// The instruction before it in that block.
398    pub prev: Option<Inst>,
399    /// The instruction after it.
400    pub next: Option<Inst>,
401}
402
403#[cfg(test)]
404mod tests {
405    use super::*;
406
407    #[test]
408    fn an_immediate_keeps_only_the_bits_its_type_has() {
409        let byte = Type::int(8);
410        assert_eq!(Imm::int(-1, byte).unsigned(), 0xff);
411        assert_eq!(Imm::int(-1, byte).signed(byte), -1);
412        assert_eq!(Imm::int(255, byte), Imm::int(-1, byte));
413        assert_eq!(Imm::int(127, byte).signed(byte), 127);
414        assert_eq!(Imm::int(128, byte).signed(byte), -128);
415    }
416
417    #[test]
418    fn a_widest_immediate_is_not_truncated() {
419        let word = Type::int(128);
420        assert_eq!(Imm::int(i128::MIN, word).signed(word), i128::MIN);
421        assert_eq!(Imm::int(i128::MAX, word).signed(word), i128::MAX);
422        assert_eq!(Imm::int(-1, word).unsigned(), u128::MAX);
423    }
424
425    #[test]
426    fn a_one_bit_immediate_is_a_bit() {
427        let bit = Type::I1;
428        assert_eq!(Imm::int(1, bit).unsigned(), 1);
429        assert_eq!(Imm::int(3, bit).unsigned(), 1);
430        assert_eq!(Imm::int(2, bit).unsigned(), 0);
431        // The one bit is the sign bit, so the only two values are zero and minus one.
432        assert_eq!(Imm::int(1, bit).signed(bit), -1);
433    }
434
435    #[test]
436    fn a_floating_immediate_keeps_its_bits() {
437        let bits = f64::NAN.to_bits() | 0x7;
438        assert_eq!(Imm::from_bits(u128::from(bits)).bits(), u128::from(bits));
439    }
440
441    #[test]
442    fn an_instruction_with_no_results_yields_none() {
443        let inst = InstData::new(Opcode::Store);
444        assert_eq!(inst.results().count(), 0);
445    }
446
447    #[test]
448    fn results_follow_the_first_one() {
449        let mut inst = InstData::new(Opcode::SAddOverflow);
450        inst.first_result = Some(Value::new(4));
451        inst.results = 2;
452        let got: Vec<u32> = inst.results().map(Idx::raw).collect();
453        assert_eq!(got, [4, 5]);
454    }
455
456    #[test]
457    fn a_jump_says_where_it_goes() {
458        let mut inst = InstData::new(Opcode::Jump);
459        inst.extra = Extra::Targets(BlockCallList::new(Idx::new(0), Idx::new(1)));
460        assert_eq!(inst.targets().len(), 1);
461    }
462
463    #[test]
464    fn a_signature_is_built_by_saying_what_it_takes_and_returns() {
465        let sig = Signature::new()
466            .with_params(&[Type::int(32), Type::PTR])
467            .with_returns(&[Type::int(32)])
468            .variadic();
469        assert_eq!(sig.params, [Type::int(32), Type::PTR]);
470        assert_eq!(sig.returns, [Type::int(32)]);
471        assert!(sig.variadic);
472        assert_eq!(Signature::new(), Signature::default());
473    }
474
475    #[test]
476    fn an_instruction_stays_small() {
477        // Not a promise, a tripwire. Every function in the program is a run of these, and a
478        // change that doubles this should be a change somebody decided to make.
479        assert!(size_of::<InstData>() <= 32, "{}", size_of::<InstData>());
480        assert_eq!(size_of::<ValueData>(), 16);
481        assert_eq!(size_of::<Extra>(), 12);
482    }
483}