rucc_codegen/term.rs
1//! The IR as something a lowering rule can match against.
2//!
3//! Design: `spec/10-backend.md` section 10.2.
4//!
5//! A rule is written about a term and the compiler has no terms. It has a function full of
6//! instructions, and what a pattern is about is one of them together with whatever its operands
7//! were computed from. So this is the [`Subject`] the matcher asks its three questions of, and
8//! the answers come out of the IR: nothing is built and nothing is thrown away.
9//!
10//! # How an operand is shown
11//!
12//! The same IR value can be several different terms. `(add.i32 (value.i32 x) (iconst.i32 k))`
13//! and `(add.i32 (value.i32 x) (value.i32 y))` are two patterns over one instruction, and which
14//! one it is depends on whether the second operand is a constant and on whether the rule that
15//! wants a constant will take this one. `(add.i64 (value.i64 x) (mul.i64 (value.i64 y)
16//! (iconst.i64 4)))` is a third, and it is about two instructions rather than one.
17//!
18//! The matcher does not backtrack across alternatives for one node: [`Subject::head`] gives one
19//! answer and the walk believes it. So the choice is made before the walk rather than during it.
20//! A [`Plan`] says how each operand of the instruction is shown, the selector tries the plans in
21//! order, and the first that matches is the one that fires. There are at most three ways to show
22//! an operand and at most two operands in any pattern this rule set has, so the whole of the
23//! search is a handful of walks over a trie, each of which fails in its first node or two.
24//!
25//! # How deep it goes
26//!
27//! One level. An operand may be shown as the instruction that computed it, and that
28//! instruction's own operands are shown as a register or as a constant and never expanded
29//! again, which is as deep as any pattern in `x86-64.rules` reaches. A rule set that wants three
30//! levels needs this to grow a level, and it would be found by the rule failing to fire rather
31//! than by anything going wrong.
32
33use rucc_ir::{Def, Extra, Float, FloatPred, Func, Inst, IntPred, Opcode, Type, Value};
34
35use crate::select::Subject;
36
37/// How many operands of one instruction a plan can speak about.
38///
39/// Two is what every pattern in the rule set needs, and a third costs nothing to carry. An
40/// instruction with more operands than this is one no rule matches, which is the same answer it
41/// would get from a plan that could describe it.
42pub const MAX_ARGS: usize = 3;
43
44/// How one operand is shown to the matcher.
45#[derive(Clone, Copy, Debug, PartialEq, Eq)]
46pub enum Shown {
47 /// As a value sitting in a register, which is what `(value.iN x)` matches.
48 Reg,
49 /// As a constant the selector has in hand, which is what `(iconst.iN k)` matches.
50 Const,
51 /// As the instruction that computed it, so a rule can be about two instructions at once.
52 Expand,
53}
54
55/// How every operand of one instruction is shown.
56pub type Plan = [Shown; MAX_ARGS];
57
58/// Everything shown as a register, which is the plan that matches when no other does.
59pub const PLAIN: Plan = [Shown::Reg; MAX_ARGS];
60
61/// One node of the term the matcher is walking.
62///
63/// A position rather than a term, because the term does not exist. Two of these are values in
64/// their own right, and they are the two a pattern can bind: the register a `value` wraps and
65/// the number an `iconst` wraps.
66#[derive(Clone, Copy, Debug, PartialEq, Eq)]
67pub enum Term {
68 /// The instruction being selected.
69 Root,
70 /// Operand `i` of the root, shown the way the plan says to show it.
71 Arg(u8),
72 /// Operand `j` of the instruction that computed operand `i` of the root.
73 Deep(u8, u8),
74 /// A value in a register, which is what a pattern binds when it writes `(value.iN x)`.
75 Reg(Value),
76 /// A constant, which is what a pattern binds or tests inside an `(iconst.iN k)`.
77 Num(i128),
78}
79
80/// One instruction of a function, as the terms a rule could match.
81#[derive(Debug)]
82pub struct Terms<'a> {
83 func: &'a Func,
84 root: Inst,
85 plan: Plan,
86}
87
88impl<'a> Terms<'a> {
89 /// The instruction, shown the way the plan says.
90 #[must_use]
91 pub fn new(func: &'a Func, root: Inst, plan: Plan) -> Self {
92 Self { func, root, plan }
93 }
94
95 /// The instruction this is about.
96 #[must_use]
97 pub fn root(&self) -> Inst {
98 self.root
99 }
100
101 /// What the root, or an instruction one of its operands was expanded into, is called in a
102 /// rule file.
103 #[must_use]
104 pub fn name(&self, inst: Inst) -> Option<&'static str> {
105 head_of(self.func, inst)
106 }
107
108 /// The value operands of an instruction.
109 fn args(&self, inst: Inst) -> &[Value] {
110 &self.func[self.func[inst].args]
111 }
112
113 /// Operand `index` of the root, or nothing if it has no such operand.
114 fn arg_value(&self, index: u8) -> Option<Value> {
115 self.args(self.root).get(usize::from(index)).copied()
116 }
117
118 /// The instruction a value is the result of, or nothing for a block parameter.
119 fn def_of(&self, value: Value) -> Option<Inst> {
120 match self.func[value].def {
121 Def::Result { inst, .. } => Some(inst),
122 Def::Param { .. } => None,
123 }
124 }
125
126 /// What a value is, if it is a constant.
127 #[must_use]
128 pub fn constant(&self, value: Value) -> Option<i128> {
129 let inst = self.def_of(value)?;
130 let data = &self.func[inst];
131 if data.opcode != Opcode::IConst {
132 return None;
133 }
134 let Extra::Imm(imm) = data.extra else { return None };
135 let ty = self.func[value].ty;
136 if !ty.is_int() {
137 return None;
138 }
139 // One bit is read unsigned, and every other width is read signed. The sign bit of a one
140 // bit integer is the whole of it, so the signed reading of a true is minus one, and what
141 // a rule at that width means by the number it matched is the truth value rather than a
142 // bit pattern. Reading it signed would put a byte of ones in a register where the rest of
143 // the rule set expects a zero or a one.
144 if is_bit(ty) {
145 return Some(i128::try_from(self.func[imm].unsigned()).unwrap_or(0));
146 }
147 Some(self.func[imm].signed(ty))
148 }
149
150 /// The head of a value shown as a register or as a constant, which is a term of one
151 /// argument either way: the thing the pattern binds.
152 fn leaf_head(&self, value: Value, shown: Shown) -> Option<(&'static str, usize)> {
153 let ty = self.func[value].ty;
154 let name = match shown {
155 Shown::Reg => value_head(ty)?,
156 Shown::Const => iconst_head(ty)?,
157 // An expansion is not a leaf, and nothing asks this about one.
158 Shown::Expand => return None,
159 };
160 Some((name, 1))
161 }
162
163 /// What a value shown as a register or as a constant binds, which is the value itself or
164 /// the number it is.
165 fn leaf_arg(&self, value: Value, shown: Shown) -> Term {
166 match shown {
167 Shown::Const => self.constant(value).map_or(Term::Reg(value), Term::Num),
168 Shown::Reg | Shown::Expand => Term::Reg(value),
169 }
170 }
171
172 /// How an operand of an expanded operand is shown, which is as a constant when it is one
173 /// and as a register otherwise.
174 ///
175 /// There is no choice to make here. The reason to show a constant as a register is that no
176 /// rule would take it as an immediate, and the answer to that inside an expansion is to
177 /// stop expanding, which is a plan the selector tries anyway.
178 fn deep_shown(&self, value: Value) -> Shown {
179 if self.constant(value).is_some() { Shown::Const } else { Shown::Reg }
180 }
181
182 /// The instruction an expanded operand of the root was computed by, with its operands.
183 fn expansion(&self, index: u8) -> Option<(Inst, &[Value])> {
184 let value = self.arg_value(index)?;
185 let inst = self.def_of(value)?;
186 Some((inst, self.args(inst)))
187 }
188}
189
190impl Subject for Terms<'_> {
191 type Node = Term;
192
193 fn head(&self, node: Term) -> Option<(&str, usize)> {
194 match node {
195 Term::Root => {
196 let name = head_of(self.func, self.root)?;
197 let data = &self.func[self.root];
198 // A constant has no operands and its term has one, which is the constant, so it
199 // is the one instruction whose arity is not the length of its operand list.
200 let arity =
201 if data.opcode == Opcode::IConst { 1 } else { self.args(self.root).len() };
202 Some((name, arity))
203 }
204 Term::Arg(index) => {
205 let value = self.arg_value(index)?;
206 match self.plan[usize::from(index)] {
207 Shown::Expand => {
208 let (inst, args) = self.expansion(index)?;
209 Some((head_of(self.func, inst)?, args.len()))
210 }
211 shown => self.leaf_head(value, shown),
212 }
213 }
214 Term::Deep(outer, inner) => {
215 let (_, args) = self.expansion(outer)?;
216 let value = *args.get(usize::from(inner))?;
217 self.leaf_head(value, self.deep_shown(value))
218 }
219 Term::Reg(_) | Term::Num(_) => None,
220 }
221 }
222
223 fn arg(&self, node: Term, index: usize) -> Term {
224 let index = u8::try_from(index).unwrap_or(u8::MAX);
225 match node {
226 Term::Root => {
227 let data = &self.func[self.root];
228 if data.opcode == Opcode::IConst {
229 let value = data.first_result.expect("a constant has a result");
230 return self.leaf_arg(value, Shown::Const);
231 }
232 Term::Arg(index)
233 }
234 Term::Arg(outer) => match self.plan[usize::from(outer)] {
235 Shown::Expand => Term::Deep(outer, index),
236 shown => {
237 self.arg_value(outer).map_or(Term::Num(0), |value| self.leaf_arg(value, shown))
238 }
239 },
240 Term::Deep(outer, inner) => {
241 let value = self
242 .expansion(outer)
243 .and_then(|(_, args)| args.get(usize::from(inner)).copied());
244 value.map_or(Term::Num(0), |value| self.leaf_arg(value, self.deep_shown(value)))
245 }
246 // Neither has a head, so nothing asks either of them for an argument.
247 Term::Reg(_) | Term::Num(_) => node,
248 }
249 }
250
251 fn int(&self, node: Term) -> Option<i128> {
252 match node {
253 Term::Num(value) => Some(value),
254 _ => None,
255 }
256 }
257}
258
259/// What an instruction is called in a rule file, or nothing if the rules have no name for it.
260///
261/// The name carries the width, because a rule file that did not say how wide a term is would be
262/// a file whose reader has to look at the line above to find out. Which widths there are names
263/// for is the rule language's business and not this crate's: an instruction at a width nothing
264/// is written about has no name here, and the answer to it is that no rule matches.
265fn head_of(func: &Func, inst: Inst) -> Option<&'static str> {
266 let data = &func[inst];
267
268 // A store is the one instruction with a name here that computes nothing, so the width in
269 // its name is the width of what it is storing and has to come from an operand. That operand
270 // is the first one, which is the order `rucc_ir::Builder::store` puts them in and the order
271 // a pattern for one is written in.
272 //
273 // Nothing looks at the flags or the ordering, and both of those are worth saying out loud.
274 // A `volatile` access has to happen exactly once and must not move, and neither of those is
275 // something selection does: one IR load is one instruction whatever its flags say, and
276 // folding the address arithmetic into the addressing mode does not change how many times
277 // memory is touched. An ordering would be a different matter, because a store that releases
278 // is not a plain `mov` on any machine where it means anything, but an ordered access is
279 // `atomic_load` or `atomic_store` and those are different opcodes with no name here. The IR
280 // verifier is what makes that true rather than merely usual: it rejects an ordering on a
281 // plain access, so by the time anything is selected there is none to miss.
282 if data.opcode == Opcode::Store {
283 let value = *func[data.args].first()?;
284 return store_head(func[value].ty);
285 }
286
287 // A return is the other one, and the width comes from the operand for the same reason. A
288 // return of nothing has no name, and neither has a return of more than one value: a rule
289 // for either would have to say where each of them goes, and where a value goes is a fact
290 // about the convention rather than about a term, so the rule language has nothing to say
291 // about it. A return of nothing needs no rule at all, since the epilogue is the whole of it.
292 if data.opcode == Opcode::Return {
293 let [value] = &func[data.args] else { return None };
294 return ret_head(func[*value].ty);
295 }
296
297 // A conditional branch is the third instruction here that computes nothing. Where it goes is
298 // not part of its name and not part of any pattern: a machine IR block holds its own
299 // successors, so a rule for a branch never has to say a block, and what is left for it to say
300 // is what the branch is about, which is the condition.
301 if data.opcode == Opcode::BrIf {
302 let [cond] = &func[data.args] else { return None };
303 return (func[*cond].ty == Type::int(1)).then_some(BRIF);
304 }
305
306 let result = data.first_result?;
307 let ty = func[result].ty;
308 match data.opcode {
309 Opcode::IConst => iconst_head(ty),
310 Opcode::Load => load_head(ty),
311 Opcode::ICmp => {
312 let Extra::IntPred(pred) = data.extra else { return None };
313 Some(icmp_head(pred))
314 }
315 // A float comparison, whose name comes from the operands rather than from the result: the
316 // result is one bit either way and what tells the two instructions apart is the format.
317 Opcode::FCmp => {
318 let Extra::FloatPred(pred) = data.extra else { return None };
319 fcmp_head(pred, func[*func[data.args].first()?].ty)
320 }
321 Opcode::SExt | Opcode::ZExt | Opcode::Trunc => {
322 let from = func[*func[data.args].first()?].ty;
323 convert_head(data.opcode, from, ty)
324 }
325 // The conversions with a float on one side or both. A separate row because what is on
326 // each side is part of the name and a width alone would not say which register file the
327 // value is in, which is the whole difference between these and the three above.
328 Opcode::FPExt | Opcode::FPTrunc | Opcode::FPToSI | Opcode::SIToFP | Opcode::Bitcast => {
329 let from = func[*func[data.args].first()?].ty;
330 cross_head(data.opcode, from, ty)
331 }
332 // Address arithmetic is an add at the address width, which is all it is once both
333 // operands are in registers: the offset is already in bytes, which the IR guarantees and
334 // the front end is what did the multiplying. Calling it that is what lets every rule
335 // written about an add reach it, including the ones that fold it into an addressing mode,
336 // and there is nothing in any of them it could get wrong.
337 Opcode::PtrAdd => binary_head(Opcode::Add, ty),
338 opcode => binary_head(opcode, ty),
339 }
340}
341
342/// What a conditional branch is called, which carries the width of the condition and nothing
343/// else, since where the branch goes is on the block rather than in the term.
344///
345/// A constant rather than a literal in [`head_of`] because [`heads`] says it too, and a name
346/// written in two places is a name that can differ in one of them.
347const BRIF: &str = "brif.i1";
348
349/// Every name this module can give an instruction, with the opcode it gives it to.
350///
351/// This is what a rule file could be written about, so that [`crate::coverage`] can ask what one
352/// is written about and say where the difference is. It comes out of the same functions
353/// [`head_of`] asks rather than out of a list, because a list of names checked against another
354/// list of names is a test that both were typed the same way, which is not the question worth
355/// asking.
356///
357/// The sweep is over every type the compiler has, including the ones nothing here has a name for.
358/// A width with no name contributes nothing and costs nothing, and the day one of them gets a name
359/// it appears here without anybody remembering to add it, which is the property that makes this
360/// worth generating rather than writing down.
361pub(crate) fn heads() -> Vec<(Opcode, &'static str)> {
362 let types = [
363 Type::int(1),
364 Type::int(8),
365 Type::int(16),
366 Type::int(32),
367 Type::int(64),
368 Type::int(128),
369 Type::PTR,
370 Type::float(Float::F32),
371 Type::float(Float::F64),
372 Type::float(Float::F80),
373 Type::vector(Type::int(32), 4),
374 ];
375
376 let mut found = Vec::new();
377 for opcode in Opcode::all() {
378 // The names that come from one type, which is the result's for most of these and an
379 // operand's for the two that compute nothing. The arms are the ones `head_of` has, in the
380 // order it has them, so that a name reachable there is reachable here.
381 for &ty in &types {
382 let name = match opcode {
383 Opcode::Store => store_head(ty),
384 Opcode::Return => ret_head(ty),
385 Opcode::IConst => iconst_head(ty),
386 Opcode::Load => load_head(ty),
387 Opcode::PtrAdd => binary_head(Opcode::Add, ty),
388 _ => binary_head(opcode, ty),
389 };
390 if let Some(name) = name {
391 found.push((opcode, name));
392 }
393 }
394 // And the names that come from a predicate or from two types at once.
395 match opcode {
396 Opcode::BrIf => found.push((opcode, BRIF)),
397 Opcode::ICmp => found.extend(IntPred::all().map(|pred| (opcode, icmp_head(pred)))),
398 Opcode::FCmp => {
399 for pred in FloatPred::all() {
400 let named = types.iter().filter_map(|&ty| fcmp_head(pred, ty));
401 found.extend(named.map(|name| (opcode, name)));
402 }
403 }
404 Opcode::SExt | Opcode::ZExt | Opcode::Trunc => {
405 for &from in &types {
406 let named = types.iter().filter_map(|&to| convert_head(opcode, from, to));
407 found.extend(named.map(|name| (opcode, name)));
408 }
409 }
410 Opcode::FPExt | Opcode::FPTrunc | Opcode::FPToSI | Opcode::SIToFP | Opcode::Bitcast => {
411 for &from in &types {
412 let named = types.iter().filter_map(|&to| cross_head(opcode, from, to));
413 found.extend(named.map(|name| (opcode, name)));
414 }
415 }
416 _ => {}
417 }
418 }
419
420 found.sort_unstable();
421 found.dedup();
422 found
423}
424
425/// How wide an address is on the machine this lowers for.
426///
427/// The rule set has no term for a pointer and needs none. An address in a register is an integer
428/// of the machine's address width, every rule that could compute one is a rule about an integer
429/// of that width, and the only thing missing was a name. [`slot`] used to ask the type how wide
430/// it was, and a pointer answers nothing, because how wide an address is belongs to the target
431/// rather than to the IR. So this is where the target's answer is written down.
432///
433/// Sixty four, and it is a constant for the same reason the `x64.` prefix and the table in
434/// [`crate::select::x86_64`] are: this crate lowers for one machine. Every architecture
435/// `rucc_target::Arch` names is a sixty four bit one, so there is no target in the compiler that
436/// would want a different number, and a thirty two bit one would want more from this crate than
437/// a number.
438const ADDRESS: u32 = 64;
439
440/// Which of the four widths a type is, or nothing for a width no rule is written at.
441///
442/// A pointer is one of them, at [`ADDRESS`]. A vector is none of them however wide its lane is,
443/// because a rule at a width says nothing about how many lanes it acts on and lowering an add of
444/// four lanes to an add of one would be wrong rather than incomplete.
445pub(crate) fn slot(ty: Type) -> Option<usize> {
446 if !ty.is_scalar() {
447 return None;
448 }
449 let bits = if ty.is_ptr() { ADDRESS } else { ty.is_int().then(|| ty.bits())? };
450 match bits {
451 8 => Some(0),
452 16 => Some(1),
453 32 => Some(2),
454 64 => Some(3),
455 _ => None,
456 }
457}
458
459/// Which of the two float widths a type is, or nothing for anything that is not a float.
460///
461/// Two rather than [`slot`]'s four, and a table of its own rather than more entries in that one,
462/// because a `float` and an `int` of the same width are not the same term to any rule: they are in
463/// different register files and every instruction that touches them is a different instruction. A
464/// `long double` is none of them, since it is on the x87 stack rather than in a vector register
465/// and nothing here is written about that stack.
466pub(crate) fn float_slot(ty: Type) -> Option<usize> {
467 if !ty.is_scalar() || !ty.is_float() {
468 return None;
469 }
470 match ty.bits() {
471 32 => Some(0),
472 64 => Some(1),
473 _ => None,
474 }
475}
476
477/// Whether a type is the one bit a truth value comes in.
478///
479/// One bit is a width the rule set is written at and is not one of [`slot`]'s four, because it is
480/// not a width the machine computes in. There is no one bit register and no one bit instruction: a
481/// value of this width lives in a whole byte with the other seven bits zero, which is what a
482/// `setcc` leaves behind, and every rule written at one bit is a byte instruction chosen because
483/// it keeps that true. The model says the same thing from the other side, giving `setcc` a meaning
484/// one bit wide, so the abstraction is stated in both places rather than assumed in either.
485///
486/// What makes the invariant hold rather than merely be usual is that nothing else at this width
487/// has a name. A comparison is the only instruction that produces one, the bitwise operations
488/// below carry it through unchanged, and everything else at one bit reaches [`slot`] and gets
489/// nothing, so there is no rule that could put a byte here which is not a zero or a one.
490fn is_bit(ty: Type) -> bool {
491 ty.is_scalar() && ty.is_int() && ty.bits() == 1
492}
493
494/// What a value in a register is called at that width.
495fn value_head(ty: Type) -> Option<&'static str> {
496 if is_bit(ty) {
497 return Some("value.i1");
498 }
499 if let Some(at) = float_slot(ty) {
500 return Some(["value.f32", "value.f64"][at]);
501 }
502 Some(["value.i8", "value.i16", "value.i32", "value.i64"][slot(ty)?])
503}
504
505/// What a constant is called at that width.
506///
507/// An integer and not an address, unlike everything else here. What a pattern binds inside one of
508/// these is the number, and [`Terms::constant`] only has a number for an integer, so a term that
509/// named an address would be one a rule could match and then find nothing behind.
510fn iconst_head(ty: Type) -> Option<&'static str> {
511 if !ty.is_int() {
512 return None;
513 }
514 if is_bit(ty) {
515 return Some("iconst.i1");
516 }
517 Some(["iconst.i8", "iconst.i16", "iconst.i32", "iconst.i64"][slot(ty)?])
518}
519
520/// What a load is called, which is the width of the value it produced.
521fn load_head(ty: Type) -> Option<&'static str> {
522 if let Some(at) = float_slot(ty) {
523 return Some(["load.f32", "load.f64"][at]);
524 }
525 Some(["load.i8", "load.i16", "load.i32", "load.i64"][slot(ty)?])
526}
527
528/// What a store is called, which is the width of the value it writes, since it produces nothing
529/// to take a width from.
530fn store_head(ty: Type) -> Option<&'static str> {
531 if let Some(at) = float_slot(ty) {
532 return Some(["store.f32", "store.f64"][at]);
533 }
534 Some(["store.i8", "store.i16", "store.i32", "store.i64"][slot(ty)?])
535}
536
537/// What a return is called, which is the width of the value it gives back, for the same reason.
538fn ret_head(ty: Type) -> Option<&'static str> {
539 if let Some(at) = float_slot(ty) {
540 return Some(["ret.f32", "ret.f64"][at]);
541 }
542 Some(["ret.i8", "ret.i16", "ret.i32", "ret.i64"][slot(ty)?])
543}
544
545/// What a comparison is called, which does not carry the width of what it compared: the result
546/// is one bit whatever the operands were, and the operands say how wide they are themselves.
547fn icmp_head(pred: IntPred) -> &'static str {
548 match pred {
549 IntPred::Eq => "icmp_eq.i1",
550 IntPred::Ne => "icmp_ne.i1",
551 IntPred::Slt => "icmp_slt.i1",
552 IntPred::Sle => "icmp_sle.i1",
553 IntPred::Sgt => "icmp_sgt.i1",
554 IntPred::Sge => "icmp_sge.i1",
555 IntPred::Ult => "icmp_ult.i1",
556 IntPred::Ule => "icmp_ule.i1",
557 IntPred::Ugt => "icmp_ugt.i1",
558 IntPred::Uge => "icmp_uge.i1",
559 }
560}
561
562/// What a float comparison is called, which does carry the format of what it compared.
563///
564/// The difference from [`icmp_head`] is the whole reason this is a second function. A comparison
565/// of two integers is the same instruction whatever file they came from, because there is only one
566/// file they could have come from, so the width lives on the operands and the name says nothing
567/// about it. A comparison of two floats is a different instruction for a `float` and a `double`,
568/// and the operands are in registers that hold either, so the name has to say which.
569///
570/// The two predicates that read nothing have no name here. `false` and `true` do not look at their
571/// operands, so a rule for either would be a rule that computes a constant out of a comparison it
572/// did not make, and the front end writes neither: nothing in C spells them and nothing here folds
573/// a comparison into one yet.
574fn fcmp_head(pred: FloatPred, ty: Type) -> Option<&'static str> {
575 let at = float_slot(ty)?;
576 let names: [&'static str; 2] = match pred {
577 FloatPred::Oeq => ["fcmp_oeq.f32.i1", "fcmp_oeq.f64.i1"],
578 FloatPred::Ogt => ["fcmp_ogt.f32.i1", "fcmp_ogt.f64.i1"],
579 FloatPred::Oge => ["fcmp_oge.f32.i1", "fcmp_oge.f64.i1"],
580 FloatPred::Olt => ["fcmp_olt.f32.i1", "fcmp_olt.f64.i1"],
581 FloatPred::Ole => ["fcmp_ole.f32.i1", "fcmp_ole.f64.i1"],
582 FloatPred::One => ["fcmp_one.f32.i1", "fcmp_one.f64.i1"],
583 FloatPred::Ord => ["fcmp_ord.f32.i1", "fcmp_ord.f64.i1"],
584 FloatPred::Uno => ["fcmp_uno.f32.i1", "fcmp_uno.f64.i1"],
585 FloatPred::Ueq => ["fcmp_ueq.f32.i1", "fcmp_ueq.f64.i1"],
586 FloatPred::Ugt => ["fcmp_ugt.f32.i1", "fcmp_ugt.f64.i1"],
587 FloatPred::Uge => ["fcmp_uge.f32.i1", "fcmp_uge.f64.i1"],
588 FloatPred::Ult => ["fcmp_ult.f32.i1", "fcmp_ult.f64.i1"],
589 FloatPred::Ule => ["fcmp_ule.f32.i1", "fcmp_ule.f64.i1"],
590 FloatPred::Une => ["fcmp_une.f32.i1", "fcmp_une.f64.i1"],
591 FloatPred::False | FloatPred::True => return None,
592 };
593 Some(names[at])
594}
595
596/// What a conversion is called, which is the two widths it is between.
597///
598/// A widening from one bit is the one conversion this width has, and it is a row of its own rather
599/// than a fifth entry in the tables below. A five by five table would have a name for every
600/// conversion between one bit and every other width in both directions, and all but four of those
601/// are conversions nothing writes: a narrowing to one bit is a comparison against zero, which is a
602/// different opcode, and a sign extension from one bit is what an `unsigned` comparison result
603/// would need and there is none.
604fn convert_head(opcode: Opcode, from: Type, to: Type) -> Option<&'static str> {
605 if is_bit(from) {
606 if opcode != Opcode::ZExt {
607 return None;
608 }
609 return Some(["zext.i1.i8", "zext.i1.i16", "zext.i1.i32", "zext.i1.i64"][slot(to)?]);
610 }
611 let table: &[[Option<&'static str>; 4]; 4] = match opcode {
612 Opcode::SExt => &SEXT,
613 Opcode::ZExt => &ZEXT,
614 Opcode::Trunc => &TRUNC,
615 _ => return None,
616 };
617 table[slot(from)?][slot(to)?]
618}
619
620/// Which of the two integer widths a conversion to or from a float is written at, or nothing for
621/// any other width.
622///
623/// The machine converts at thirty two bits and at sixty four and at no width below them. A C
624/// program turning a `double` into a `short` is a conversion to `int` and a truncation after it,
625/// and the front end is what writes the truncation, so a narrower conversion arriving here has no
626/// name and is reported rather than lowered to an instruction that would round it in the wrong
627/// place.
628fn cross_slot(ty: Type) -> Option<usize> {
629 match slot(ty)? {
630 2 => Some(0),
631 3 => Some(1),
632 _ => None,
633 }
634}
635
636/// Whether that type is the integer the float at that index shares its width with.
637///
638/// A pointer is not, however wide it is. The IR has `ptrtoint` for turning an address into a
639/// number, and a `bitcast` that moved one through a vector register would be hiding that
640/// conversion rather than performing it, which is what the IR verifier says as well.
641fn paired_int(ty: Type, at: usize) -> bool {
642 ty.is_scalar() && ty.is_int() && ty.bits() == [32, 64][at]
643}
644
645/// What a conversion with a float on one side or both is called, which is what it goes between and
646/// which side each of them is on.
647///
648/// The name carries the format where an integer conversion carries a width, for the reason
649/// [`float_slot`] gives: a `float` and an `int` of the same width are in different register files
650/// and no rule written about one says anything about the other. So there is no name here that
651/// could be read as either, and a rule for `fptosi.f64.i32` cannot match anything but a `double`
652/// becoming an `int`.
653///
654/// The unsigned conversions have no name. The machine has no instruction for either below a
655/// register wider than anything this allocates, so each is several instructions and belongs in a
656/// pass that rewrites it into these rather than in a rule that would have to be several
657/// instructions long.
658fn cross_head(opcode: Opcode, from: Type, to: Type) -> Option<&'static str> {
659 match opcode {
660 // Between the two formats, one name each way. There is no third format with a name here,
661 // so these two are the whole of it rather than the first two of a table.
662 Opcode::FPExt => {
663 (float_slot(from)? == 0 && float_slot(to)? == 1).then_some("fpext.f32.f64")
664 }
665 Opcode::FPTrunc => {
666 (float_slot(from)? == 1 && float_slot(to)? == 0).then_some("fptrunc.f64.f32")
667 }
668 Opcode::FPToSI => Some(FPTOSI[float_slot(from)?][cross_slot(to)?]),
669 Opcode::SIToFP => Some(SITOFP[cross_slot(from)?][float_slot(to)?]),
670 // A reinterpretation, which is a `movd` or a `movq` between the two register files and is
671 // the one conversion here that changes no bit. Between two integers or between two floats
672 // it is nothing at all, since the IR keeps the width the same, so the four that cross the
673 // files are the four with a name.
674 Opcode::Bitcast => match (float_slot(from), float_slot(to)) {
675 (Some(at), None) if paired_int(to, at) => {
676 Some(["bitcast.f32.i32", "bitcast.f64.i64"][at])
677 }
678 (None, Some(at)) if paired_int(from, at) => {
679 Some(["bitcast.i32.f32", "bitcast.i64.f64"][at])
680 }
681 _ => None,
682 },
683 _ => None,
684 }
685}
686
687/// A float to a signed integer, from the format down the side to the width across the top.
688static FPTOSI: [[&str; 2]; 2] =
689 [["fptosi.f32.i32", "fptosi.f32.i64"], ["fptosi.f64.i32", "fptosi.f64.i64"]];
690
691/// A signed integer to a float, the other way round.
692static SITOFP: [[&str; 2]; 2] =
693 [["sitofp.i32.f32", "sitofp.i32.f64"], ["sitofp.i64.f32", "sitofp.i64.f64"]];
694
695/// What each of the binary operations is called at each width.
696///
697/// The three bitwise ones are the only ones with a name at one bit. They are what a `!=` between
698/// two truth values and a `&&` folded to one instruction become, and each of them takes two bytes
699/// that are a zero or a one to a byte that is a zero or a one. There is nothing to be gained by an
700/// add or a shift at this width and no front end writes one.
701fn binary_head(opcode: Opcode, ty: Type) -> Option<&'static str> {
702 if is_bit(ty) {
703 return match opcode {
704 Opcode::And => Some("and.i1"),
705 Opcode::Or => Some("or.i1"),
706 Opcode::Xor => Some("xor.i1"),
707 _ => None,
708 };
709 }
710 if let Some(at) = float_slot(ty) {
711 // The four the machine has one instruction each for. A remainder is not among them: there
712 // is no scalar instruction for it and what C means by `fmod` is a call, so an `frem` that
713 // reached here would find no rule and be reported rather than lowered to something else.
714 let names: &[&'static str; 2] = match opcode {
715 Opcode::FAdd => &["fadd.f32", "fadd.f64"],
716 Opcode::FSub => &["fsub.f32", "fsub.f64"],
717 Opcode::FMul => &["fmul.f32", "fmul.f64"],
718 Opcode::FDiv => &["fdiv.f32", "fdiv.f64"],
719 _ => return None,
720 };
721 return Some(names[at]);
722 }
723 let names: &[&'static str; 4] = match opcode {
724 Opcode::Add => &["add.i8", "add.i16", "add.i32", "add.i64"],
725 Opcode::Sub => &["sub.i8", "sub.i16", "sub.i32", "sub.i64"],
726 Opcode::Mul => &["mul.i8", "mul.i16", "mul.i32", "mul.i64"],
727 Opcode::SDiv => &["sdiv.i8", "sdiv.i16", "sdiv.i32", "sdiv.i64"],
728 Opcode::UDiv => &["udiv.i8", "udiv.i16", "udiv.i32", "udiv.i64"],
729 Opcode::SRem => &["srem.i8", "srem.i16", "srem.i32", "srem.i64"],
730 Opcode::URem => &["urem.i8", "urem.i16", "urem.i32", "urem.i64"],
731 Opcode::And => &["and.i8", "and.i16", "and.i32", "and.i64"],
732 Opcode::Or => &["or.i8", "or.i16", "or.i32", "or.i64"],
733 Opcode::Xor => &["xor.i8", "xor.i16", "xor.i32", "xor.i64"],
734 Opcode::Shl => &["shl.i8", "shl.i16", "shl.i32", "shl.i64"],
735 Opcode::LShr => &["lshr.i8", "lshr.i16", "lshr.i32", "lshr.i64"],
736 Opcode::AShr => &["ashr.i8", "ashr.i16", "ashr.i32", "ashr.i64"],
737 _ => return None,
738 };
739 Some(names[slot(ty)?])
740}
741
742/// The widening conversions, from the width down the side to the width across the top. The
743/// diagonal and everything below it is empty, because a sign extension to a width it already
744/// has is not an instruction and the IR does not have one.
745static SEXT: [[Option<&str>; 4]; 4] = [
746 [None, Some("sext.i8.i16"), Some("sext.i8.i32"), Some("sext.i8.i64")],
747 [None, None, Some("sext.i16.i32"), Some("sext.i16.i64")],
748 [None, None, None, Some("sext.i32.i64")],
749 [None, None, None, None],
750];
751
752static ZEXT: [[Option<&str>; 4]; 4] = [
753 [None, Some("zext.i8.i16"), Some("zext.i8.i32"), Some("zext.i8.i64")],
754 [None, None, Some("zext.i16.i32"), Some("zext.i16.i64")],
755 [None, None, None, Some("zext.i32.i64")],
756 [None, None, None, None],
757];
758
759/// The narrowing ones, which fill the other corner for the same reason.
760static TRUNC: [[Option<&str>; 4]; 4] = [
761 [None, None, None, None],
762 [Some("trunc.i16.i8"), None, None, None],
763 [Some("trunc.i32.i8"), Some("trunc.i32.i16"), None, None],
764 [Some("trunc.i64.i8"), Some("trunc.i64.i16"), Some("trunc.i64.i32"), None],
765];
766
767#[cfg(test)]
768mod tests {
769 use rucc_base::Interner;
770 use rucc_ir::{Builder, Flags, Signature};
771
772 use super::*;
773 use crate::select::Subject;
774
775 /// A function with one block, and the builder to put instructions in it.
776 fn func() -> (Func, rucc_ir::Block) {
777 let mut names = Interner::new();
778 let mut func = Func::new(names.intern("f"), Signature::new());
779 let block = func.create_block();
780 (func, block)
781 }
782
783 /// The instruction that computed a value, which every value in these tests has.
784 fn inst_of(func: &Func, value: Value) -> Inst {
785 match func[value].def {
786 Def::Result { inst, .. } => inst,
787 Def::Param { .. } => unreachable!(),
788 }
789 }
790
791 #[test]
792 fn an_instruction_is_the_term_the_rule_file_names_it_by() {
793 let (mut func, block) = func();
794 let i32 = Type::int(32);
795 let mut build = Builder::new(&mut func, block);
796 let k = build.iconst(i32, 7);
797 let x = build.iconst(i32, 3);
798 let sum = build.binary(Opcode::Add, x, k, Flags::default());
799 let add = inst_of(&func, sum);
800
801 let terms = Terms::new(&func, add, PLAIN);
802 assert_eq!(terms.head(Term::Root), Some(("add.i32", 2)));
803 assert_eq!(terms.head(Term::Arg(0)), Some(("value.i32", 1)));
804 assert_eq!(terms.arg(Term::Arg(0), 0), Term::Reg(x));
805 assert_eq!(terms.head(Term::Reg(x)), None);
806 assert_eq!(terms.int(Term::Reg(x)), None);
807 }
808
809 #[test]
810 fn an_operand_shown_as_a_constant_gives_the_number_up() {
811 let (mut func, block) = func();
812 let i32 = Type::int(32);
813 let mut build = Builder::new(&mut func, block);
814 let x = build.iconst(i32, 3);
815 let k = build.iconst(i32, -7);
816 let sum = build.binary(Opcode::Add, x, k, Flags::default());
817 let add = inst_of(&func, sum);
818
819 let terms = Terms::new(&func, add, [Shown::Reg, Shown::Const, Shown::Reg]);
820 assert_eq!(terms.head(Term::Arg(1)), Some(("iconst.i32", 1)));
821 assert_eq!(terms.arg(Term::Arg(1), 0), Term::Num(-7));
822 assert_eq!(terms.int(Term::Num(-7)), Some(-7));
823 // The same operand shown as a register is a register, and a guard asking what number it
824 // is gets no answer, which is what makes a rule about a number decline it.
825 let plain = Terms::new(&func, add, PLAIN);
826 assert_eq!(plain.head(Term::Arg(1)), Some(("value.i32", 1)));
827 assert_eq!(plain.int(plain.arg(Term::Arg(1), 0)), None);
828 }
829
830 #[test]
831 fn a_constant_is_a_term_of_one_argument_and_has_no_operands() {
832 let (mut func, block) = func();
833 let mut build = Builder::new(&mut func, block);
834 let k = build.iconst(Type::int(64), 12);
835 let inst = inst_of(&func, k);
836
837 let terms = Terms::new(&func, inst, PLAIN);
838 assert_eq!(terms.head(Term::Root), Some(("iconst.i64", 1)));
839 assert_eq!(terms.arg(Term::Root, 0), Term::Num(12));
840 }
841
842 #[test]
843 fn an_expanded_operand_is_the_instruction_that_computed_it() {
844 let (mut func, block) = func();
845 let i64 = Type::int(64);
846 // A parameter, because the point of the test is an operand that is not a constant.
847 let y = func.append_param(block, i64);
848 let mut build = Builder::new(&mut func, block);
849 let x = build.iconst(i64, 1);
850 let four = build.iconst(i64, 4);
851 let scaled = build.binary(Opcode::Mul, y, four, Flags::default());
852 let sum = build.binary(Opcode::Add, x, scaled, Flags::default());
853 let add = inst_of(&func, sum);
854
855 let terms = Terms::new(&func, add, [Shown::Reg, Shown::Expand, Shown::Reg]);
856 assert_eq!(terms.head(Term::Root), Some(("add.i64", 2)));
857 assert_eq!(terms.head(Term::Arg(1)), Some(("mul.i64", 2)));
858 assert_eq!(terms.head(Term::Deep(1, 0)), Some(("value.i64", 1)));
859 assert_eq!(terms.arg(Term::Deep(1, 0), 0), Term::Reg(y));
860 // The constant inside an expansion is shown as one without being asked to be.
861 assert_eq!(terms.head(Term::Deep(1, 1)), Some(("iconst.i64", 1)));
862 assert_eq!(terms.arg(Term::Deep(1, 1), 0), Term::Num(4));
863 }
864
865 #[test]
866 fn a_comparison_says_which_one_it_is_and_a_conversion_says_both_widths() {
867 let (mut func, block) = func();
868 let mut build = Builder::new(&mut func, block);
869 let x = build.iconst(Type::int(32), 1);
870 let y = build.iconst(Type::int(32), 2);
871 let less = build.icmp(IntPred::Slt, x, y);
872 let wide = build.unary(Opcode::SExt, x, Type::int(64));
873 let narrow = build.unary(Opcode::Trunc, x, Type::int(8));
874 let cmp = inst_of(&func, less);
875 assert_eq!(Terms::new(&func, cmp, PLAIN).head(Term::Root), Some(("icmp_slt.i1", 2)));
876 let sext = inst_of(&func, wide);
877 assert_eq!(Terms::new(&func, sext, PLAIN).head(Term::Root), Some(("sext.i32.i64", 1)));
878 let trunc = inst_of(&func, narrow);
879 assert_eq!(Terms::new(&func, trunc, PLAIN).head(Term::Root), Some(("trunc.i32.i8", 1)));
880 }
881
882 #[test]
883 fn a_width_no_rule_is_written_at_has_no_name() {
884 let (mut func, block) = func();
885 let mut build = Builder::new(&mut func, block);
886 let x = build.iconst(Type::int(128), 1);
887 let inst = inst_of(&func, x);
888 assert_eq!(Terms::new(&func, inst, PLAIN).head(Term::Root), None);
889 }
890
891 /// An address is an integer of the machine's width to every term here, which is what lets one
892 /// be loaded from, stored through, returned and added to by rules written about integers.
893 #[test]
894 fn an_address_is_an_integer_as_wide_as_the_machine_addresses() {
895 assert_eq!(value_head(Type::PTR), Some("value.i64"));
896 assert_eq!(load_head(Type::PTR), Some("load.i64"));
897 assert_eq!(store_head(Type::PTR), Some("store.i64"));
898 assert_eq!(ret_head(Type::PTR), Some("ret.i64"));
899 // Not a constant, since nothing writes an address down as one.
900 assert_eq!(iconst_head(Type::PTR), None);
901 }
902
903 /// One bit is a width with names of its own, and they are not the four the tables hold. What
904 /// has a name there is what a truth value is written with: a constant, the three bitwise
905 /// operations, and the widening that turns one into a number.
906 #[test]
907 fn one_bit_is_a_width_with_a_name_for_what_a_truth_value_is_written_with() {
908 let bit = Type::int(1);
909 assert_eq!(slot(bit), None);
910 assert_eq!(value_head(bit), Some("value.i1"));
911 assert_eq!(iconst_head(bit), Some("iconst.i1"));
912 assert_eq!(binary_head(Opcode::And, bit), Some("and.i1"));
913 assert_eq!(binary_head(Opcode::Or, bit), Some("or.i1"));
914 assert_eq!(binary_head(Opcode::Xor, bit), Some("xor.i1"));
915 assert_eq!(convert_head(Opcode::ZExt, bit, Type::int(8)), Some("zext.i1.i8"));
916 assert_eq!(convert_head(Opcode::ZExt, bit, Type::int(32)), Some("zext.i1.i32"));
917 assert_eq!(convert_head(Opcode::ZExt, bit, Type::int(64)), Some("zext.i1.i64"));
918 }
919
920 /// Everything else at one bit has no name, which is what keeps the byte holding one a zero or
921 /// a one: an add at this width would be an instruction that leaves something else there.
922 #[test]
923 fn nothing_else_at_one_bit_has_a_name() {
924 let bit = Type::int(1);
925 assert_eq!(binary_head(Opcode::Add, bit), None);
926 assert_eq!(binary_head(Opcode::Shl, bit), None);
927 assert_eq!(load_head(bit), None);
928 assert_eq!(store_head(bit), None);
929 assert_eq!(ret_head(bit), None);
930 // Not a sign extension either, which would be a truth value spread over every bit.
931 assert_eq!(convert_head(Opcode::SExt, bit, Type::int(32)), None);
932 // And not a narrowing to it, since what makes a number into a truth value is a
933 // comparison against zero and that is a different opcode.
934 assert_eq!(convert_head(Opcode::Trunc, Type::int(32), bit), None);
935 }
936
937 /// A one bit constant is the truth value it stands for. The signed reading of a one bit
938 /// integer turns a true into a minus one, which would put a byte of ones where every rule at
939 /// this width expects a one.
940 #[test]
941 fn a_one_bit_constant_is_a_zero_or_a_one_rather_than_a_zero_or_a_minus_one() {
942 let (mut func, block) = func();
943 let mut build = Builder::new(&mut func, block);
944 let bit = Type::int(1);
945 let no = build.iconst(bit, 0);
946 let yes = build.iconst(bit, 1);
947 let terms = Terms::new(&func, inst_of(&func, yes), PLAIN);
948 assert_eq!(terms.constant(no), Some(0));
949 assert_eq!(terms.constant(yes), Some(1));
950 assert_eq!(terms.head(Term::Root), Some(("iconst.i1", 1)));
951 assert_eq!(terms.arg(Term::Root, 0), Term::Num(1));
952 }
953
954 /// A float is a term of its own at each of the two widths the machine has instructions for.
955 /// The same width of integer is a different term, which is what keeps a rule about one from
956 /// ever firing on the other, and it has to be, because the two are in different register
957 /// files.
958 #[test]
959 fn a_float_is_a_term_of_its_own_at_each_width_the_machine_computes_in() {
960 let f32 = Type::float(Float::F32);
961 let f64 = Type::float(Float::F64);
962 assert_eq!(value_head(f32), Some("value.f32"));
963 assert_eq!(value_head(f64), Some("value.f64"));
964 assert_eq!(load_head(f32), Some("load.f32"));
965 assert_eq!(store_head(f64), Some("store.f64"));
966 assert_eq!(ret_head(f32), Some("ret.f32"));
967 assert_eq!(binary_head(Opcode::FAdd, f32), Some("fadd.f32"));
968 assert_eq!(binary_head(Opcode::FSub, f64), Some("fsub.f64"));
969 assert_eq!(binary_head(Opcode::FMul, f32), Some("fmul.f32"));
970 assert_eq!(binary_head(Opcode::FDiv, f64), Some("fdiv.f64"));
971 // Not one of the four widths an integer rule is written at, and not a constant either,
972 // since what a pattern binds inside an `iconst` is a number and a float is not one.
973 assert_eq!(slot(f32), None);
974 assert_eq!(slot(f64), None);
975 assert_eq!(iconst_head(f64), None);
976 // An integer add at thirty two bits is a different name from a float add at the same
977 // width, which is the whole of what keeps the two rule sets apart.
978 assert_ne!(binary_head(Opcode::Add, Type::int(32)), binary_head(Opcode::FAdd, f32));
979 }
980
981 /// What the machine has no scalar instruction for has no name, so it is reported rather than
982 /// lowered to something near it. A remainder is a call to `fmod` and a `long double` is on the
983 /// x87 stack, and neither is anything a rule in this set is written about.
984 #[test]
985 fn a_float_operation_the_machine_lacks_has_no_name() {
986 assert_eq!(binary_head(Opcode::FRem, Type::float(Float::F32)), None);
987 let long = Type::float(Float::F80);
988 assert_eq!(float_slot(long), None);
989 assert_eq!(value_head(long), None);
990 assert_eq!(binary_head(Opcode::FAdd, long), None);
991 assert_eq!(ret_head(long), None);
992 }
993
994 /// A lane count is not a width, so a rule written at a width does not get to answer for a
995 /// vector of that width. Nothing produces one yet and the day something does it should be
996 /// reported rather than lowered to an instruction that acts on one lane of it.
997 #[test]
998 fn a_vector_is_not_the_width_of_its_lane() {
999 let i32x4 = Type::vector(Type::int(32), 4);
1000 assert_eq!(slot(i32x4), None);
1001 assert_eq!(value_head(i32x4), None);
1002 assert_eq!(binary_head(Opcode::Add, i32x4), None);
1003 }
1004
1005 /// The sweep says the same thing about an instruction that looking the instruction up does,
1006 /// which is the only way it is worth anything: a list of names built beside the naming rather
1007 /// than out of it would be a second table to keep in step.
1008 #[test]
1009 fn the_names_the_sweep_finds_are_the_names_an_instruction_gets() {
1010 let (mut func, block) = func();
1011 let other = func.create_block();
1012 let mut build = Builder::new(&mut func, block);
1013 let cond = build.iconst(Type::int(1), 1);
1014 let x = build.iconst(Type::int(32), 1);
1015 let sum = build.binary(Opcode::Add, x, x, Flags::default());
1016 let branch = build.br_if(cond, other, &[], other, &[]);
1017
1018 let names = heads();
1019 for inst in [inst_of(&func, sum), inst_of(&func, x), branch] {
1020 let name = head_of(&func, inst).expect("all three have a name");
1021 let opcode = func[inst].opcode;
1022 assert!(
1023 names.contains(&(opcode, name)),
1024 "an instruction is called {name} and the sweep does not know that name"
1025 );
1026 }
1027 }
1028
1029 /// Every name is there once and belongs to one opcode. A name in the list twice would count
1030 /// twice in the coverage report, and the two instructions a name could belong to are the two
1031 /// the machine has one instruction for: an add of two numbers and an add of an address.
1032 #[test]
1033 fn a_name_is_listed_once_and_an_address_add_is_the_one_name_two_opcodes_share() {
1034 let names = heads();
1035 let mut once = names.clone();
1036 once.dedup();
1037 assert_eq!(names, once, "the sweep lists a name twice");
1038 assert!(names.contains(&(Opcode::Add, "add.i64")));
1039 assert!(names.contains(&(Opcode::PtrAdd, "add.i64")));
1040 }
1041
1042 /// A width nothing is written at contributes nothing, which is what makes the sweep safe to
1043 /// run over every type there is. These four are the widths that have no name today, and each
1044 /// is an issue rather than an oversight: one bit arithmetic, `__int128`, `long double` and a
1045 /// vector of any lane count.
1046 #[test]
1047 fn a_width_with_no_name_puts_nothing_in_the_sweep() {
1048 let named: Vec<&'static str> = heads().into_iter().map(|(_, name)| name).collect();
1049 for name in &named {
1050 assert!(!name.contains("i128"), "{name} is a width no rule is written at");
1051 assert!(!name.contains("f80"), "{name} is a width no rule is written at");
1052 }
1053 // One bit is the width with some names and not others, so it is checked from the other
1054 // side: what a truth value is written with, and nothing else. A comparison is in the list
1055 // because its result is one bit, whatever it compared.
1056 let mut bit: Vec<&'static str> =
1057 named.into_iter().filter(|name| name.ends_with(".i1")).collect();
1058 bit.sort_unstable();
1059 assert_eq!(
1060 bit,
1061 [
1062 "and.i1",
1063 "brif.i1",
1064 "fcmp_oeq.f32.i1",
1065 "fcmp_oeq.f64.i1",
1066 "fcmp_oge.f32.i1",
1067 "fcmp_oge.f64.i1",
1068 "fcmp_ogt.f32.i1",
1069 "fcmp_ogt.f64.i1",
1070 "fcmp_ole.f32.i1",
1071 "fcmp_ole.f64.i1",
1072 "fcmp_olt.f32.i1",
1073 "fcmp_olt.f64.i1",
1074 "fcmp_one.f32.i1",
1075 "fcmp_one.f64.i1",
1076 "fcmp_ord.f32.i1",
1077 "fcmp_ord.f64.i1",
1078 "fcmp_ueq.f32.i1",
1079 "fcmp_ueq.f64.i1",
1080 "fcmp_uge.f32.i1",
1081 "fcmp_uge.f64.i1",
1082 "fcmp_ugt.f32.i1",
1083 "fcmp_ugt.f64.i1",
1084 "fcmp_ule.f32.i1",
1085 "fcmp_ule.f64.i1",
1086 "fcmp_ult.f32.i1",
1087 "fcmp_ult.f64.i1",
1088 "fcmp_une.f32.i1",
1089 "fcmp_une.f64.i1",
1090 "fcmp_uno.f32.i1",
1091 "fcmp_uno.f64.i1",
1092 "icmp_eq.i1",
1093 "icmp_ne.i1",
1094 "icmp_sge.i1",
1095 "icmp_sgt.i1",
1096 "icmp_sle.i1",
1097 "icmp_slt.i1",
1098 "icmp_uge.i1",
1099 "icmp_ugt.i1",
1100 "icmp_ule.i1",
1101 "icmp_ult.i1",
1102 "iconst.i1",
1103 "or.i1",
1104 "xor.i1",
1105 ]
1106 );
1107 }
1108
1109 /// Address arithmetic is named as the add it is, which is what puts it in reach of every rule
1110 /// written about one, including the two below that fold it into an address.
1111 #[test]
1112 fn address_arithmetic_is_an_add_at_the_address_width() {
1113 let (mut func, block) = func();
1114 let base = func.append_param(block, Type::PTR);
1115 let mut build = Builder::new(&mut func, block);
1116 let step = build.iconst(Type::int(64), 4);
1117 let args = func.push_values(&[base, step]);
1118 let next = Builder::new(&mut func, block)
1119 .value(rucc_ir::InstData { args, ..rucc_ir::InstData::new(Opcode::PtrAdd) }, Type::PTR);
1120 let inst = inst_of(&func, next);
1121
1122 let terms = Terms::new(&func, inst, [Shown::Reg, Shown::Const, Shown::Reg]);
1123 assert_eq!(terms.head(Term::Root), Some(("add.i64", 2)));
1124 assert_eq!(terms.head(Term::Arg(0)), Some(("value.i64", 1)));
1125 assert_eq!(terms.head(Term::Arg(1)), Some(("iconst.i64", 1)));
1126 assert_eq!(terms.arg(Term::Arg(1), 0), Term::Num(4));
1127 }
1128}