pub struct Value(/* private fields */);Expand description
A handle to an SSA value inside a Function.
A value is the result of one instruction or one block parameter. It is named by
this small, copyable handle rather than by an expression position, which is what
lets a later pass refer to a result without holding a borrow of the function and
lets the IR stay flat. A handle is stable for the life of the function that
minted it and is dense (the handles of a function are 0..n), so it doubles as
an index into a side table a pass keeps alongside the IR.
Handles are scoped to the function they come from: a Value minted by one
function’s Builder is meaningless in another. The validator
catches a value used where its definition does not reach, but it cannot tell two
functions’ identically-numbered handles apart, so do not mix them.
§Examples
use ir_lang::{Builder, Type};
let mut b = Builder::new("id", &[Type::Int], Type::Int);
let entry = b.entry();
// The entry block's parameters are the function's parameters, as values.
let x = b.block_params(entry)[0];
b.ret(Some(x));
// A value handle is `Copy` and prints as `v<n>`.
assert_eq!(x.to_string(), "v0");
assert_eq!(x.index(), 0);Implementations§
Source§impl Value
impl Value
Sourcepub const fn index(self) -> usize
pub const fn index(self) -> usize
Returns the zero-based index of this value, for use as a key into a side table a pass keeps alongside the function.
The values of a function are numbered densely from zero in the order they
are created, so a Vec sized to the value count can be indexed directly by
value.index().
§Examples
use ir_lang::{Builder, Type};
let mut b = Builder::new("k", &[], Type::Int);
let a = b.iconst(1);
let c = b.iconst(2);
assert_eq!(a.index(), 0);
assert_eq!(c.index(), 1);