cranelift_codegen/ir/
function.rs

1//! Intermediate representation of a function.
2//!
3//! The `Function` struct defined in this module owns all of its extended basic blocks and
4//! instructions.
5
6use crate::binemit::CodeOffset;
7use crate::entity::{PrimaryMap, SecondaryMap};
8use crate::ir;
9use crate::ir::{DataFlowGraph, ExternalName, Layout, Signature};
10use crate::ir::{
11    Ebb, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Heap, HeapData, Inst, JumpTable,
12    JumpTableData, SigRef, StackSlot, StackSlotData, Table, TableData,
13};
14use crate::ir::{EbbOffsets, FrameLayout, InstEncodings, SourceLocs, StackSlots, ValueLocations};
15use crate::ir::{JumpTableOffsets, JumpTables};
16use crate::isa::{CallConv, EncInfo, Encoding, Legalize, TargetIsa};
17use crate::regalloc::{EntryRegDiversions, RegDiversions};
18use crate::value_label::ValueLabelsRanges;
19use crate::write::write_function;
20use core::fmt;
21
22#[cfg(feature = "basic-blocks")]
23use crate::ir::Opcode;
24
25/// A function.
26///
27/// Functions can be cloned, but it is not a very fast operation.
28/// The clone will have all the same entity numbers as the original.
29#[derive(Clone)]
30pub struct Function {
31    /// Name of this function. Mostly used by `.clif` files.
32    pub name: ExternalName,
33
34    /// Signature of this function.
35    pub signature: Signature,
36
37    /// The old signature of this function, before the most recent legalization,
38    /// if any.
39    pub old_signature: Option<Signature>,
40
41    /// Stack slots allocated in this function.
42    pub stack_slots: StackSlots,
43
44    /// Global values referenced.
45    pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
46
47    /// Heaps referenced.
48    pub heaps: PrimaryMap<ir::Heap, ir::HeapData>,
49
50    /// Tables referenced.
51    pub tables: PrimaryMap<ir::Table, ir::TableData>,
52
53    /// Jump tables used in this function.
54    pub jump_tables: JumpTables,
55
56    /// Data flow graph containing the primary definition of all instructions, EBBs and values.
57    pub dfg: DataFlowGraph,
58
59    /// Layout of EBBs and instructions in the function body.
60    pub layout: Layout,
61
62    /// Encoding recipe and bits for the legal instructions.
63    /// Illegal instructions have the `Encoding::default()` value.
64    pub encodings: InstEncodings,
65
66    /// Location assigned to every value.
67    pub locations: ValueLocations,
68
69    /// Non-default locations assigned to value at the entry of basic blocks.
70    ///
71    /// At the entry of each basic block, we might have values which are not in their default
72    /// ValueLocation. This field records these register-to-register moves as Diversions.
73    pub entry_diversions: EntryRegDiversions,
74
75    /// Code offsets of the EBB headers.
76    ///
77    /// This information is only transiently available after the `binemit::relax_branches` function
78    /// computes it, and it can easily be recomputed by calling that function. It is not included
79    /// in the textual IR format.
80    pub offsets: EbbOffsets,
81
82    /// Code offsets of Jump Table headers.
83    pub jt_offsets: JumpTableOffsets,
84
85    /// Source locations.
86    ///
87    /// Track the original source location for each instruction. The source locations are not
88    /// interpreted by Cranelift, only preserved.
89    pub srclocs: SourceLocs,
90
91    /// Instruction that marks the end (inclusive) of the function's prologue.
92    ///
93    /// This is used for some calling conventions to track the end of unwind information.
94    pub prologue_end: Option<Inst>,
95
96    /// Frame layout for the instructions.
97    ///
98    /// The stack unwinding requires to have information about which registers and where they
99    /// are saved in the frame. This information is created during the prologue and epilogue
100    /// passes.
101    pub frame_layout: Option<FrameLayout>,
102}
103
104impl Function {
105    /// Create a function with the given name and signature.
106    pub fn with_name_signature(name: ExternalName, sig: Signature) -> Self {
107        Self {
108            name,
109            signature: sig,
110            old_signature: None,
111            stack_slots: StackSlots::new(),
112            global_values: PrimaryMap::new(),
113            heaps: PrimaryMap::new(),
114            tables: PrimaryMap::new(),
115            jump_tables: PrimaryMap::new(),
116            dfg: DataFlowGraph::new(),
117            layout: Layout::new(),
118            encodings: SecondaryMap::new(),
119            locations: SecondaryMap::new(),
120            entry_diversions: EntryRegDiversions::new(),
121            offsets: SecondaryMap::new(),
122            jt_offsets: SecondaryMap::new(),
123            srclocs: SecondaryMap::new(),
124            prologue_end: None,
125            frame_layout: None,
126        }
127    }
128
129    /// Clear all data structures in this function.
130    pub fn clear(&mut self) {
131        self.signature.clear(CallConv::Fast);
132        self.stack_slots.clear();
133        self.global_values.clear();
134        self.heaps.clear();
135        self.tables.clear();
136        self.jump_tables.clear();
137        self.dfg.clear();
138        self.layout.clear();
139        self.encodings.clear();
140        self.locations.clear();
141        self.entry_diversions.clear();
142        self.offsets.clear();
143        self.jt_offsets.clear();
144        self.srclocs.clear();
145        self.prologue_end = None;
146        self.frame_layout = None;
147    }
148
149    /// Create a new empty, anonymous function with a Fast calling convention.
150    pub fn new() -> Self {
151        Self::with_name_signature(ExternalName::default(), Signature::new(CallConv::Fast))
152    }
153
154    /// Creates a jump table in the function, to be used by `br_table` instructions.
155    pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
156        self.jump_tables.push(data)
157    }
158
159    /// Creates a stack slot in the function, to be used by `stack_load`, `stack_store` and
160    /// `stack_addr` instructions.
161    pub fn create_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
162        self.stack_slots.push(data)
163    }
164
165    /// Adds a signature which can later be used to declare an external function import.
166    pub fn import_signature(&mut self, signature: Signature) -> SigRef {
167        self.dfg.signatures.push(signature)
168    }
169
170    /// Declare an external function import.
171    pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
172        self.dfg.ext_funcs.push(data)
173    }
174
175    /// Declares a global value accessible to the function.
176    pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
177        self.global_values.push(data)
178    }
179
180    /// Declares a heap accessible to the function.
181    pub fn create_heap(&mut self, data: HeapData) -> Heap {
182        self.heaps.push(data)
183    }
184
185    /// Declares a table accessible to the function.
186    pub fn create_table(&mut self, data: TableData) -> Table {
187        self.tables.push(data)
188    }
189
190    /// Return an object that can display this function with correct ISA-specific annotations.
191    pub fn display<'a, I: Into<Option<&'a dyn TargetIsa>>>(
192        &'a self,
193        isa: I,
194    ) -> DisplayFunction<'a> {
195        DisplayFunction(self, isa.into().into())
196    }
197
198    /// Return an object that can display this function with correct ISA-specific annotations.
199    pub fn display_with<'a>(
200        &'a self,
201        annotations: DisplayFunctionAnnotations<'a>,
202    ) -> DisplayFunction<'a> {
203        DisplayFunction(self, annotations)
204    }
205
206    /// Find a presumed unique special-purpose function parameter value.
207    ///
208    /// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists.
209    pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
210        let entry = self.layout.entry_block().expect("Function is empty");
211        self.signature
212            .special_param_index(purpose)
213            .map(|i| self.dfg.ebb_params(entry)[i])
214    }
215
216    /// Get an iterator over the instructions in `ebb`, including offsets and encoded instruction
217    /// sizes.
218    ///
219    /// The iterator returns `(offset, inst, size)` tuples, where `offset` if the offset in bytes
220    /// from the beginning of the function to the instruction, and `size` is the size of the
221    /// instruction in bytes, or 0 for unencoded instructions.
222    ///
223    /// This function can only be used after the code layout has been computed by the
224    /// `binemit::relax_branches()` function.
225    pub fn inst_offsets<'a>(&'a self, ebb: Ebb, encinfo: &EncInfo) -> InstOffsetIter<'a> {
226        assert!(
227            !self.offsets.is_empty(),
228            "Code layout must be computed first"
229        );
230        let mut divert = RegDiversions::new();
231        divert.at_ebb(&self.entry_diversions, ebb);
232        InstOffsetIter {
233            encinfo: encinfo.clone(),
234            func: self,
235            divert,
236            encodings: &self.encodings,
237            offset: self.offsets[ebb],
238            iter: self.layout.ebb_insts(ebb),
239        }
240    }
241
242    /// Wrapper around `encode` which assigns `inst` the resulting encoding.
243    pub fn update_encoding(&mut self, inst: ir::Inst, isa: &dyn TargetIsa) -> Result<(), Legalize> {
244        self.encode(inst, isa).map(|e| self.encodings[inst] = e)
245    }
246
247    /// Wrapper around `TargetIsa::encode` for encoding an existing instruction
248    /// in the `Function`.
249    pub fn encode(&self, inst: ir::Inst, isa: &dyn TargetIsa) -> Result<Encoding, Legalize> {
250        isa.encode(&self, &self.dfg[inst], self.dfg.ctrl_typevar(inst))
251    }
252
253    /// Starts collection of debug information.
254    pub fn collect_debug_info(&mut self) {
255        self.dfg.collect_debug_info();
256        self.frame_layout = Some(FrameLayout::new());
257    }
258
259    /// Changes the destination of a jump or branch instruction.
260    /// Does nothing if called with a non-jump or non-branch instruction.
261    pub fn change_branch_destination(&mut self, inst: Inst, new_dest: Ebb) {
262        match self.dfg[inst].branch_destination_mut() {
263            None => (),
264            Some(inst_dest) => *inst_dest = new_dest,
265        }
266    }
267
268    /// Checks that the specified EBB can be encoded as a basic block.
269    ///
270    /// On error, returns the first invalid instruction and an error message.
271    #[cfg(feature = "basic-blocks")]
272    pub fn is_ebb_basic(&self, ebb: Ebb) -> Result<(), (Inst, &'static str)> {
273        let dfg = &self.dfg;
274        let inst_iter = self.layout.ebb_insts(ebb);
275
276        // Ignore all instructions prior to the first branch.
277        let mut inst_iter = inst_iter.skip_while(|&inst| !dfg[inst].opcode().is_branch());
278
279        // A conditional branch is permitted in a basic block only when followed
280        // by a terminal jump or fallthrough instruction.
281        if let Some(_branch) = inst_iter.next() {
282            if let Some(next) = inst_iter.next() {
283                match dfg[next].opcode() {
284                    Opcode::Fallthrough | Opcode::Jump => (),
285                    _ => return Err((next, "post-branch instruction not fallthrough or jump")),
286                }
287            }
288        }
289
290        Ok(())
291    }
292
293    /// Returns true if the function is function that doesn't call any other functions. This is not
294    /// to be confused with a "leaf function" in Windows terminology.
295    pub fn is_leaf(&self) -> bool {
296        // Conservative result: if there's at least one function signature referenced in this
297        // function, assume it may call.
298        !self.dfg.signatures.is_empty()
299    }
300}
301
302/// Additional annotations for function display.
303#[derive(Default)]
304pub struct DisplayFunctionAnnotations<'a> {
305    /// Enable ISA annotations.
306    pub isa: Option<&'a dyn TargetIsa>,
307
308    /// Enable value labels annotations.
309    pub value_ranges: Option<&'a ValueLabelsRanges>,
310}
311
312impl<'a> From<Option<&'a dyn TargetIsa>> for DisplayFunctionAnnotations<'a> {
313    fn from(isa: Option<&'a dyn TargetIsa>) -> DisplayFunctionAnnotations {
314        DisplayFunctionAnnotations {
315            isa,
316            value_ranges: None,
317        }
318    }
319}
320
321/// Wrapper type capable of displaying a `Function` with correct ISA annotations.
322pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>);
323
324impl<'a> fmt::Display for DisplayFunction<'a> {
325    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
326        write_function(fmt, self.0, &self.1)
327    }
328}
329
330impl fmt::Display for Function {
331    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
332        write_function(fmt, self, &DisplayFunctionAnnotations::default())
333    }
334}
335
336impl fmt::Debug for Function {
337    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
338        write_function(fmt, self, &DisplayFunctionAnnotations::default())
339    }
340}
341
342/// Iterator returning instruction offsets and sizes: `(offset, inst, size)`.
343pub struct InstOffsetIter<'a> {
344    encinfo: EncInfo,
345    divert: RegDiversions,
346    func: &'a Function,
347    encodings: &'a InstEncodings,
348    offset: CodeOffset,
349    iter: ir::layout::Insts<'a>,
350}
351
352impl<'a> Iterator for InstOffsetIter<'a> {
353    type Item = (CodeOffset, ir::Inst, CodeOffset);
354
355    fn next(&mut self) -> Option<Self::Item> {
356        self.iter.next().map(|inst| {
357            self.divert.apply(&self.func.dfg[inst]);
358            let byte_size =
359                self.encinfo
360                    .byte_size(self.encodings[inst], inst, &self.divert, self.func);
361            let offset = self.offset;
362            self.offset += byte_size;
363            (offset, inst, byte_size)
364        })
365    }
366}