Struct cranelift_codegen::ir::InsertBuilder
source · pub struct InsertBuilder<'f, IIB: InstInserterBase<'f>> { /* private fields */ }Expand description
Builder that inserts an instruction at the current position.
An InsertBuilder is a wrapper for an InstInserterBase that turns it into an instruction
builder with some additional facilities for creating instructions that reuse existing values as
their results.
Implementations§
source§impl<'f, IIB: InstInserterBase<'f>> InsertBuilder<'f, IIB>
impl<'f, IIB: InstInserterBase<'f>> InsertBuilder<'f, IIB>
sourcepub fn new(inserter: IIB) -> Self
pub fn new(inserter: IIB) -> Self
Create a new builder which inserts instructions at pos.
The dfg and pos.layout references should be from the same Function.
sourcepub fn with_results<Array>(
self,
reuse: Array
) -> InsertReuseBuilder<'f, IIB, Array>where
Array: AsRef<[Option<Value>]>,
pub fn with_results<Array>(
self,
reuse: Array
) -> InsertReuseBuilder<'f, IIB, Array>where
Array: AsRef<[Option<Value>]>,
Reuse result values in reuse.
Convert this builder into one that will reuse the provided result values instead of allocating new ones. The provided values for reuse must not be attached to anything. Any missing result values will be allocated as normal.
The reuse argument is expected to be an array of Option<Value>.
sourcepub fn with_result(
self,
v: Value
) -> InsertReuseBuilder<'f, IIB, [Option<Value>; 1]>
pub fn with_result(
self,
v: Value
) -> InsertReuseBuilder<'f, IIB, [Option<Value>; 1]>
Reuse a single result value.
Convert this into a builder that will reuse v as the single result value. The reused
result value v must not be attached to anything.
This method should only be used when building an instruction with exactly one result. Use
with_results() for the more general case.
Examples found in repository?
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
fn add_nan_canon_seq(pos: &mut FuncCursor, inst: Inst) {
// Select the instruction result, result type. Replace the instruction
// result and step forward before inserting the canonicalization sequence.
let val = pos.func.dfg.first_result(inst);
let val_type = pos.func.dfg.value_type(val);
let new_res = pos.func.dfg.replace_result(val, val_type);
let _next_inst = pos.next_inst().expect("block missing terminator!");
// Insert a comparison instruction, to check if `inst_res` is NaN. Select
// the canonical NaN value if `val` is NaN, assign the result to `inst`.
let is_nan = pos.ins().fcmp(FloatCC::NotEqual, new_res, new_res);
let scalar_select = |pos: &mut FuncCursor, canon_nan: Value| {
pos.ins()
.with_result(val)
.select(is_nan, canon_nan, new_res);
};
let vector_select = |pos: &mut FuncCursor, canon_nan: Value| {
pos.ins()
.with_result(val)
.vselect(is_nan, canon_nan, new_res);
};
match val_type {
types::F32 => {
let canon_nan = pos.ins().f32const(Ieee32::with_bits(CANON_32BIT_NAN));
scalar_select(pos, canon_nan);
}
types::F64 => {
let canon_nan = pos.ins().f64const(Ieee64::with_bits(CANON_64BIT_NAN));
scalar_select(pos, canon_nan);
}
types::F32X4 => {
let canon_nan = pos.ins().f32const(Ieee32::with_bits(CANON_32BIT_NAN));
let canon_nan = pos.ins().splat(types::F32X4, canon_nan);
vector_select(pos, canon_nan);
}
types::F64X2 => {
let canon_nan = pos.ins().f64const(Ieee64::with_bits(CANON_64BIT_NAN));
let canon_nan = pos.ins().splat(types::F64X2, canon_nan);
vector_select(pos, canon_nan);
}
_ => {
// Panic if the type given was not an IEEE floating point type.
panic!("Could not canonicalize NaN: Unexpected result type found.");
}
}
pos.prev_inst(); // Step backwards so the pass does not skip instructions.
}