1 //! Intermediate representation of a function.
2 //!
3 //! The `Function` struct defined in this module owns all of its basic blocks and
4 //! instructions.
5 
6 use crate::entity::{PrimaryMap, SecondaryMap};
7 use crate::ir;
8 use crate::ir::JumpTables;
9 use crate::ir::{
10     instructions::BranchInfo, Block, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Heap,
11     HeapData, Inst, InstructionData, JumpTable, JumpTableData, Opcode, SigRef, StackSlot,
12     StackSlotData, Table, TableData,
13 };
14 use crate::ir::{DataFlowGraph, ExternalName, Layout, Signature};
15 use crate::ir::{SourceLocs, StackSlots};
16 use crate::isa::CallConv;
17 use crate::value_label::ValueLabelsRanges;
18 use crate::write::write_function;
19 #[cfg(feature = "enable-serde")]
20 use alloc::string::String;
21 use core::fmt;
22 
23 #[cfg(feature = "enable-serde")]
24 use serde::de::{Deserializer, Error};
25 #[cfg(feature = "enable-serde")]
26 use serde::ser::Serializer;
27 #[cfg(feature = "enable-serde")]
28 use serde::{Deserialize, Serialize};
29 
30 /// A version marker used to ensure that serialized clif ir is never deserialized with a
31 /// different version of Cranelift.
32 #[derive(Copy, Clone, Debug)]
33 pub struct VersionMarker;
34 
35 #[cfg(feature = "enable-serde")]
36 impl Serialize for VersionMarker {
37     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
38     where
39         S: Serializer,
40     {
41         crate::VERSION.serialize(serializer)
42     }
43 }
44 
45 #[cfg(feature = "enable-serde")]
46 impl<'de> Deserialize<'de> for VersionMarker {
47     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
48     where
49         D: Deserializer<'de>,
50     {
51         let version = String::deserialize(deserializer)?;
52         if version != crate::VERSION {
53             return Err(D::Error::custom(&format!(
54                 "Expected a clif ir function for version {}, found one for version {}",
55                 crate::VERSION,
56                 version,
57             )));
58         }
59         Ok(VersionMarker)
60     }
61 }
62 
63 ///
64 /// Functions can be cloned, but it is not a very fast operation.
65 /// The clone will have all the same entity numbers as the original.
66 #[derive(Clone)]
67 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
68 pub struct Function {
69     /// A version marker used to ensure that serialized clif ir is never deserialized with a
70     /// different version of Cranelift.
71     // Note: This must be the first field to ensure that Serde will deserialize it before
72     // attempting to deserialize other fields that are potentially changed between versions.
73     pub version_marker: VersionMarker,
74 
75     /// Name of this function. Mostly used by `.clif` files.
76     pub name: ExternalName,
77 
78     /// Signature of this function.
79     pub signature: Signature,
80 
81     /// Stack slots allocated in this function.
82     pub stack_slots: StackSlots,
83 
84     /// Global values referenced.
85     pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
86 
87     /// Heaps referenced.
88     pub heaps: PrimaryMap<ir::Heap, ir::HeapData>,
89 
90     /// Tables referenced.
91     pub tables: PrimaryMap<ir::Table, ir::TableData>,
92 
93     /// Jump tables used in this function.
94     pub jump_tables: JumpTables,
95 
96     /// Data flow graph containing the primary definition of all instructions, blocks and values.
97     pub dfg: DataFlowGraph,
98 
99     /// Layout of blocks and instructions in the function body.
100     pub layout: Layout,
101 
102     /// Source locations.
103     ///
104     /// Track the original source location for each instruction. The source locations are not
105     /// interpreted by Cranelift, only preserved.
106     pub srclocs: SourceLocs,
107 
108     /// An optional global value which represents an expression evaluating to
109     /// the stack limit for this function. This `GlobalValue` will be
110     /// interpreted in the prologue, if necessary, to insert a stack check to
111     /// ensure that a trap happens if the stack pointer goes below the
112     /// threshold specified here.
113     pub stack_limit: Option<ir::GlobalValue>,
114 }
115 
116 impl Function {
117     /// Create a function with the given name and signature.
118     pub fn with_name_signature(name: ExternalName, sig: Signature) -> Self {
119         Self {
120             version_marker: VersionMarker,
121             name,
122             signature: sig,
123             stack_slots: StackSlots::new(),
124             global_values: PrimaryMap::new(),
125             heaps: PrimaryMap::new(),
126             tables: PrimaryMap::new(),
127             jump_tables: PrimaryMap::new(),
128             dfg: DataFlowGraph::new(),
129             layout: Layout::new(),
130             srclocs: SecondaryMap::new(),
131             stack_limit: None,
132         }
133     }
134 
135     /// Clear all data structures in this function.
136     pub fn clear(&mut self) {
137         self.signature.clear(CallConv::Fast);
138         self.stack_slots.clear();
139         self.global_values.clear();
140         self.heaps.clear();
141         self.tables.clear();
142         self.jump_tables.clear();
143         self.dfg.clear();
144         self.layout.clear();
145         self.srclocs.clear();
146         self.stack_limit = 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(&self) -> DisplayFunction<'_> {
192         DisplayFunction(self, Default::default())
193     }
194 
195     /// Return an object that can display this function with correct ISA-specific annotations.
196     pub fn display_with<'a>(
197         &'a self,
198         annotations: DisplayFunctionAnnotations<'a>,
199     ) -> DisplayFunction<'a> {
200         DisplayFunction(self, annotations)
201     }
202 
203     /// Find a presumed unique special-purpose function parameter value.
204     ///
205     /// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists.
206     pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
207         let entry = self.layout.entry_block().expect("Function is empty");
208         self.signature
209             .special_param_index(purpose)
210             .map(|i| self.dfg.block_params(entry)[i])
211     }
212 
213     /// Starts collection of debug information.
214     pub fn collect_debug_info(&mut self) {
215         self.dfg.collect_debug_info();
216     }
217 
218     /// Changes the destination of a jump or branch instruction.
219     /// Does nothing if called with a non-jump or non-branch instruction.
220     ///
221     /// Note that this method ignores multi-destination branches like `br_table`.
222     pub fn change_branch_destination(&mut self, inst: Inst, new_dest: Block) {
223         match self.dfg[inst].branch_destination_mut() {
224             None => (),
225             Some(inst_dest) => *inst_dest = new_dest,
226         }
227     }
228 
229     /// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.
230     /// Does nothing if called with a non-jump or non-branch instruction.
231     ///
232     /// Unlike [change_branch_destination](Function::change_branch_destination), this method rewrite the destinations of
233     /// multi-destination branches like `br_table`.
234     pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
235         match self.dfg.analyze_branch(inst) {
236             BranchInfo::SingleDest(dest, ..) => {
237                 if dest == old_dest {
238                     self.change_branch_destination(inst, new_dest);
239                 }
240             }
241 
242             BranchInfo::Table(table, default_dest) => {
243                 self.jump_tables[table].iter_mut().for_each(|entry| {
244                     if *entry == old_dest {
245                         *entry = new_dest;
246                     }
247                 });
248 
249                 if default_dest == Some(old_dest) {
250                     match &mut self.dfg[inst] {
251                         InstructionData::BranchTable { destination, .. } => {
252                             *destination = new_dest;
253                         }
254                         _ => panic!(
255                             "Unexpected instruction {} having default destination",
256                             self.dfg.display_inst(inst)
257                         ),
258                     }
259                 }
260             }
261 
262             BranchInfo::NotABranch => {}
263         }
264     }
265 
266     /// Checks that the specified block can be encoded as a basic block.
267     ///
268     /// On error, returns the first invalid instruction and an error message.
269     pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
270         let dfg = &self.dfg;
271         let inst_iter = self.layout.block_insts(block);
272 
273         // Ignore all instructions prior to the first branch.
274         let mut inst_iter = inst_iter.skip_while(|&inst| !dfg[inst].opcode().is_branch());
275 
276         // A conditional branch is permitted in a basic block only when followed
277         // by a terminal jump instruction.
278         if let Some(_branch) = inst_iter.next() {
279             if let Some(next) = inst_iter.next() {
280                 match dfg[next].opcode() {
281                     Opcode::Jump => (),
282                     _ => return Err((next, "post-branch instruction not jump")),
283                 }
284             }
285         }
286 
287         Ok(())
288     }
289 
290     /// Returns true if the function is function that doesn't call any other functions. This is not
291     /// to be confused with a "leaf function" in Windows terminology.
292     pub fn is_leaf(&self) -> bool {
293         // Conservative result: if there's at least one function signature referenced in this
294         // function, assume it is not a leaf.
295         self.dfg.signatures.is_empty()
296     }
297 
298     /// Replace the `dst` instruction's data with the `src` instruction's data
299     /// and then remove `src`.
300     ///
301     /// `src` and its result values should not be used at all, as any uses would
302     /// be left dangling after calling this method.
303     ///
304     /// `src` and `dst` must have the same number of resulting values, and
305     /// `src`'s i^th value must have the same type as `dst`'s i^th value.
306     pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
307         debug_assert_eq!(
308             self.dfg.inst_results(dst).len(),
309             self.dfg.inst_results(src).len()
310         );
311         debug_assert!(self
312             .dfg
313             .inst_results(dst)
314             .iter()
315             .zip(self.dfg.inst_results(src))
316             .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b)));
317 
318         self.dfg[dst] = self.dfg[src].clone();
319         self.layout.remove_inst(src);
320     }
321 
322     /// Size occupied by all stack slots associated with this function.
323     ///
324     /// Does not include any padding necessary due to offsets
325     pub fn stack_size(&self) -> u32 {
326         self.stack_slots.values().map(|ss| ss.size).sum()
327     }
328 }
329 
330 /// Additional annotations for function display.
331 #[derive(Default)]
332 pub struct DisplayFunctionAnnotations<'a> {
333     /// Enable value labels annotations.
334     pub value_ranges: Option<&'a ValueLabelsRanges>,
335 }
336 
337 /// Wrapper type capable of displaying a `Function` with correct ISA annotations.
338 pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>);
339 
340 impl<'a> fmt::Display for DisplayFunction<'a> {
341     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
342         write_function(fmt, self.0)
343     }
344 }
345 
346 impl fmt::Display for Function {
347     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
348         write_function(fmt, self)
349     }
350 }
351 
352 impl fmt::Debug for Function {
353     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
354         write_function(fmt, self)
355     }
356 }
357