1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
//! A linear IR for optimizations.
//!
//! This IR is designed such that it should be easy to combine multiple linear
//! optimizations into a single automata.
//!
//! See also `src/linearize.rs` for the AST to linear IR translation pass.
use crate::cc::ConditionCode;
use crate::integer_interner::{IntegerId, IntegerInterner};
use crate::r#type::{BitWidth, Type};
use crate::unquote::UnquoteOperator;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use std::hash::Hash;
use std::num::NonZeroU32;
/// A set of linear optimizations.
#[derive(Debug)]
pub struct Optimizations<TOperator>
where
TOperator: 'static + Copy + Debug + Eq + Hash,
{
/// The linear optimizations.
pub optimizations: Vec<Optimization<TOperator>>,
/// The integer literals referenced by these optimizations.
pub integers: IntegerInterner,
}
/// A linearized optimization.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Optimization<TOperator>
where
TOperator: 'static + Copy + Debug + Eq + Hash,
{
/// The chain of match operations and expected results for this
/// optimization.
pub matches: Vec<Match>,
/// Actions to perform, given that the operation resulted in the expected
/// value.
pub actions: Vec<Action<TOperator>>,
}
/// Match any value.
///
/// This can be used to create fallback, wildcard-style transitions between
/// states.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Else;
/// The result of evaluating a `MatchOp`.
///
/// This is either a specific non-zero `u32`, or a fallback that matches
/// everything.
pub type MatchResult = Result<NonZeroU32, Else>;
/// Convert a boolean to a `MatchResult`.
#[inline]
pub fn bool_to_match_result(b: bool) -> MatchResult {
let b = b as u32;
unsafe { Ok(NonZeroU32::new_unchecked(b + 1)) }
}
/// A partial match of an optimization's LHS.
///
/// An match is composed of a matching operation and the expected result of that
/// operation. Each match will basically become a state and a transition edge
/// out of that state in the final automata.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Match {
/// The matching operation to perform.
pub operation: MatchOp,
/// The expected result of our matching operation, that enables us to
/// continue to the next match, or `Else` for "don't care" wildcard-style
/// matching.
pub expected: MatchResult,
}
/// A matching operation to be performed on some Cranelift instruction as part
/// of determining whether an optimization is applicable.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Deserialize, Serialize)]
pub enum MatchOp {
/// Switch on the opcode of an instruction.
///
/// Upon successfully matching an instruction's opcode, bind each of its
/// operands to a LHS temporary.
Opcode(LhsId),
/// Does an instruction have a constant value?
IsConst(LhsId),
/// Is the constant value a power of two?
IsPowerOfTwo(LhsId),
/// Switch on the bit width of a value.
BitWidth(LhsId),
/// Does the value fit in our target architecture's native word size?
FitsInNativeWord(LhsId),
/// Are the instructions (or immediates) the same?
Eq(LhsId, LhsId),
/// Switch on the constant integer value of an instruction.
IntegerValue(LhsId),
/// Switch on the constant boolean value of an instruction.
BooleanValue(LhsId),
/// Switch on a condition code.
ConditionCode(LhsId),
/// No operation. Always evaluates to `Else`.
///
/// Never appears in real optimizations; nonetheless required to support
/// corner cases of the DSL, such as a LHS pattern that is nothing but a
/// variable.
Nop,
}
/// A canonicalized identifier for a left-hand side value that was bound in a
/// pattern.
///
/// These are defined in a pre-order traversal of the LHS pattern by successful
/// `MatchOp::Opcode` matches.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
pub struct LhsId(pub u16);
/// A canonicalized identifier for a right-hand side value.
///
/// These are defined by RHS actions.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RhsId(pub u16);
/// An action to perform when transitioning between states in the automata.
///
/// When evaluating actions, the `i^th` action implicitly defines the
/// `RhsId(i)`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Action<TOperator> {
/// Reuse something from the left-hand side.
GetLhs {
/// The left-hand side instruction or value.
lhs: LhsId,
},
/// Perform compile-time evaluation.
UnaryUnquote {
/// The unquote operator.
operator: UnquoteOperator,
/// The constant operand to this unquote.
operand: RhsId,
},
/// Perform compile-time evaluation.
BinaryUnquote {
/// The unquote operator.
operator: UnquoteOperator,
/// The constant operands to this unquote.
operands: [RhsId; 2],
},
/// Create an integer constant.
MakeIntegerConst {
/// The constant integer value.
value: IntegerId,
/// The bit width of this constant.
bit_width: BitWidth,
},
/// Create a boolean constant.
MakeBooleanConst {
/// The constant boolean value.
value: bool,
/// The bit width of this constant.
bit_width: BitWidth,
},
/// Create a condition code.
MakeConditionCode {
/// The condition code.
cc: ConditionCode,
},
/// Make a unary instruction.
MakeUnaryInst {
/// The operand for this instruction.
operand: RhsId,
/// The type of this instruction's result.
r#type: Type,
/// The operator for this instruction.
operator: TOperator,
},
/// Make a binary instruction.
MakeBinaryInst {
/// The opcode for this instruction.
operator: TOperator,
/// The type of this instruction's result.
r#type: Type,
/// The operands for this instruction.
operands: [RhsId; 2],
},
/// Make a ternary instruction.
MakeTernaryInst {
/// The opcode for this instruction.
operator: TOperator,
/// The type of this instruction's result.
r#type: Type,
/// The operands for this instruction.
operands: [RhsId; 3],
},
}
#[cfg(test)]
mod tests {
use super::*;
use peepmatic_test_operator::TestOperator;
// These types all end up in the automaton, so we should take care that they
// are small and don't fill up the data cache (or take up too much
// serialized size).
#[test]
fn match_result_size() {
assert_eq!(std::mem::size_of::<MatchResult>(), 4);
}
#[test]
fn match_op_size() {
assert_eq!(std::mem::size_of::<MatchOp>(), 6);
}
#[test]
fn action_size() {
assert_eq!(std::mem::size_of::<Action<TestOperator>>(), 16);
}
}