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, DynamicStackSlot, DynamicStackSlotData, DynamicType,
11     ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Inst, InstructionData, JumpTable,
12     JumpTableData, Opcode, SigRef, StackSlot, StackSlotData, Table, TableData, Type,
13 };
14 use crate::ir::{DataFlowGraph, Layout, Signature};
15 use crate::ir::{DynamicStackSlots, SourceLocs, StackSlots};
16 use crate::isa::CallConv;
17 use crate::value_label::ValueLabelsRanges;
18 use crate::write::write_function;
19 use crate::HashMap;
20 #[cfg(feature = "enable-serde")]
21 use alloc::string::String;
22 use core::fmt;
23 
24 #[cfg(feature = "enable-serde")]
25 use serde::de::{Deserializer, Error};
26 #[cfg(feature = "enable-serde")]
27 use serde::ser::Serializer;
28 #[cfg(feature = "enable-serde")]
29 use serde::{Deserialize, Serialize};
30 
31 use super::entities::UserExternalNameRef;
32 use super::extname::UserFuncName;
33 use super::{RelSourceLoc, SourceLoc, UserExternalName};
34 
35 /// A version marker used to ensure that serialized clif ir is never deserialized with a
36 /// different version of Cranelift.
37 #[derive(Copy, Clone, Debug, PartialEq, Hash)]
38 pub struct VersionMarker;
39 
40 #[cfg(feature = "enable-serde")]
41 impl Serialize for VersionMarker {
42     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
43     where
44         S: Serializer,
45     {
46         crate::VERSION.serialize(serializer)
47     }
48 }
49 
50 #[cfg(feature = "enable-serde")]
51 impl<'de> Deserialize<'de> for VersionMarker {
52     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
53     where
54         D: Deserializer<'de>,
55     {
56         let version = String::deserialize(deserializer)?;
57         if version != crate::VERSION {
58             return Err(D::Error::custom(&format!(
59                 "Expected a clif ir function for version {}, found one for version {}",
60                 crate::VERSION,
61                 version,
62             )));
63         }
64         Ok(VersionMarker)
65     }
66 }
67 
68 /// Function parameters used when creating this function, and that will become applied after
69 /// compilation to materialize the final `CompiledCode`.
70 #[derive(Clone)]
71 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
72 pub struct FunctionParameters {
73     /// The first `SourceLoc` appearing in the function, serving as a base for every relative
74     /// source loc in the function.
75     base_srcloc: Option<SourceLoc>,
76 
77     /// External user-defined function references.
78     user_named_funcs: PrimaryMap<UserExternalNameRef, UserExternalName>,
79 
80     /// Inverted mapping of `user_named_funcs`, to deduplicate internally.
81     user_ext_name_to_ref: HashMap<UserExternalName, UserExternalNameRef>,
82 }
83 
84 impl FunctionParameters {
85     /// Creates a new `FunctionParameters` with the given name.
86     pub fn new() -> Self {
87         Self {
88             base_srcloc: None,
89             user_named_funcs: Default::default(),
90             user_ext_name_to_ref: Default::default(),
91         }
92     }
93 
94     /// Returns the base `SourceLoc`.
95     ///
96     /// If it was never explicitly set with `ensure_base_srcloc`, will return an invalid
97     /// `SourceLoc`.
98     pub fn base_srcloc(&self) -> SourceLoc {
99         self.base_srcloc.unwrap_or_default()
100     }
101 
102     /// Sets the base `SourceLoc`, if not set yet, and returns the base value.
103     pub fn ensure_base_srcloc(&mut self, srcloc: SourceLoc) -> SourceLoc {
104         match self.base_srcloc {
105             Some(val) => val,
106             None => {
107                 self.base_srcloc = Some(srcloc);
108                 srcloc
109             }
110         }
111     }
112 
113     /// Retrieve a `UserExternalNameRef` for the given name, or add a new one.
114     ///
115     /// This method internally deduplicates same `UserExternalName` so they map to the same
116     /// reference.
117     pub fn ensure_user_func_name(&mut self, name: UserExternalName) -> UserExternalNameRef {
118         if let Some(reff) = self.user_ext_name_to_ref.get(&name) {
119             *reff
120         } else {
121             let reff = self.user_named_funcs.push(name.clone());
122             self.user_ext_name_to_ref.insert(name, reff);
123             reff
124         }
125     }
126 
127     /// Resets an already existing user function name to a new value.
128     pub fn reset_user_func_name(&mut self, index: UserExternalNameRef, name: UserExternalName) {
129         if let Some(prev_name) = self.user_named_funcs.get_mut(index) {
130             self.user_ext_name_to_ref.remove(prev_name);
131             *prev_name = name.clone();
132             self.user_ext_name_to_ref.insert(name, index);
133         }
134     }
135 
136     /// Returns the internal mapping of `UserExternalNameRef` to `UserExternalName`.
137     pub fn user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName> {
138         &self.user_named_funcs
139     }
140 
141     fn clear(&mut self) {
142         self.base_srcloc = None;
143         self.user_named_funcs.clear();
144         self.user_ext_name_to_ref.clear();
145     }
146 }
147 
148 /// Function fields needed when compiling a function.
149 ///
150 /// Additionally, these fields can be the same for two functions that would be compiled the same
151 /// way, and finalized by applying `FunctionParameters` onto their `CompiledCodeStencil`.
152 #[derive(Clone, PartialEq, Hash)]
153 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
154 pub struct FunctionStencil {
155     /// A version marker used to ensure that serialized clif ir is never deserialized with a
156     /// different version of Cranelift.
157     // Note: This must be the first field to ensure that Serde will deserialize it before
158     // attempting to deserialize other fields that are potentially changed between versions.
159     pub version_marker: VersionMarker,
160 
161     /// Signature of this function.
162     pub signature: Signature,
163 
164     /// Sized stack slots allocated in this function.
165     pub sized_stack_slots: StackSlots,
166 
167     /// Dynamic stack slots allocated in this function.
168     pub dynamic_stack_slots: DynamicStackSlots,
169 
170     /// Global values referenced.
171     pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
172 
173     /// Tables referenced.
174     pub tables: PrimaryMap<ir::Table, ir::TableData>,
175 
176     /// Jump tables used in this function.
177     pub jump_tables: JumpTables,
178 
179     /// Data flow graph containing the primary definition of all instructions, blocks and values.
180     pub dfg: DataFlowGraph,
181 
182     /// Layout of blocks and instructions in the function body.
183     pub layout: Layout,
184 
185     /// Source locations.
186     ///
187     /// Track the original source location for each instruction. The source locations are not
188     /// interpreted by Cranelift, only preserved.
189     pub srclocs: SourceLocs,
190 
191     /// An optional global value which represents an expression evaluating to
192     /// the stack limit for this function. This `GlobalValue` will be
193     /// interpreted in the prologue, if necessary, to insert a stack check to
194     /// ensure that a trap happens if the stack pointer goes below the
195     /// threshold specified here.
196     pub stack_limit: Option<ir::GlobalValue>,
197 }
198 
199 impl FunctionStencil {
200     fn clear(&mut self) {
201         self.signature.clear(CallConv::Fast);
202         self.sized_stack_slots.clear();
203         self.dynamic_stack_slots.clear();
204         self.global_values.clear();
205         self.tables.clear();
206         self.jump_tables.clear();
207         self.dfg.clear();
208         self.layout.clear();
209         self.srclocs.clear();
210         self.stack_limit = None;
211     }
212 
213     /// Creates a jump table in the function, to be used by `br_table` instructions.
214     pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
215         self.jump_tables.push(data)
216     }
217 
218     /// Creates a sized stack slot in the function, to be used by `stack_load`, `stack_store`
219     /// and `stack_addr` instructions.
220     pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
221         self.sized_stack_slots.push(data)
222     }
223 
224     /// Creates a dynamic stack slot in the function, to be used by `dynamic_stack_load`,
225     /// `dynamic_stack_store` and `dynamic_stack_addr` instructions.
226     pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {
227         self.dynamic_stack_slots.push(data)
228     }
229 
230     /// Adds a signature which can later be used to declare an external function import.
231     pub fn import_signature(&mut self, signature: Signature) -> SigRef {
232         self.dfg.signatures.push(signature)
233     }
234 
235     /// Declares a global value accessible to the function.
236     pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
237         self.global_values.push(data)
238     }
239 
240     /// Find the global dyn_scale value associated with given DynamicType
241     pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {
242         self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale
243     }
244 
245     /// Find the global dyn_scale for the given stack slot.
246     pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {
247         let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;
248         self.get_dyn_scale(dyn_ty)
249     }
250 
251     /// Get a concrete `Type` from a user defined `DynamicType`.
252     pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {
253         self.dfg
254             .dynamic_types
255             .get(ty)
256             .unwrap_or_else(|| panic!("Undeclared dynamic vector type: {}", ty))
257             .concrete()
258     }
259 
260     /// Declares a table accessible to the function.
261     pub fn create_table(&mut self, data: TableData) -> Table {
262         self.tables.push(data)
263     }
264 
265     /// Find a presumed unique special-purpose function parameter value.
266     ///
267     /// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists.
268     pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
269         let entry = self.layout.entry_block().expect("Function is empty");
270         self.signature
271             .special_param_index(purpose)
272             .map(|i| self.dfg.block_params(entry)[i])
273     }
274 
275     /// Starts collection of debug information.
276     pub fn collect_debug_info(&mut self) {
277         self.dfg.collect_debug_info();
278     }
279 
280     /// Changes the destination of a jump or branch instruction.
281     /// Does nothing if called with a non-jump or non-branch instruction.
282     ///
283     /// Note that this method ignores multi-destination branches like `br_table`.
284     pub fn change_branch_destination(&mut self, inst: Inst, new_dest: Block) {
285         match self.dfg.insts[inst].branch_destination_mut() {
286             None => (),
287             Some(inst_dest) => inst_dest.set_block(new_dest, &mut self.dfg.value_lists),
288         }
289     }
290 
291     /// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.
292     /// Does nothing if called with a non-jump or non-branch instruction.
293     ///
294     /// Unlike [change_branch_destination](FunctionStencil::change_branch_destination), this method
295     /// rewrite the destinations of multi-destination branches like `br_table`.
296     pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
297         match self.dfg.analyze_branch(inst) {
298             BranchInfo::SingleDest(dest) => {
299                 if dest.block(&self.dfg.value_lists) == old_dest {
300                     self.change_branch_destination(inst, new_dest);
301                 }
302             }
303 
304             BranchInfo::Table(table, default_dest) => {
305                 self.jump_tables[table].iter_mut().for_each(|entry| {
306                     if *entry == old_dest {
307                         *entry = new_dest;
308                     }
309                 });
310 
311                 if default_dest == Some(old_dest) {
312                     match &mut self.dfg.insts[inst] {
313                         InstructionData::BranchTable { destination, .. } => {
314                             *destination = new_dest;
315                         }
316                         _ => panic!(
317                             "Unexpected instruction {} having default destination",
318                             self.dfg.display_inst(inst)
319                         ),
320                     }
321                 }
322             }
323 
324             BranchInfo::NotABranch => {}
325         }
326     }
327 
328     /// Checks that the specified block can be encoded as a basic block.
329     ///
330     /// On error, returns the first invalid instruction and an error message.
331     pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
332         let dfg = &self.dfg;
333         let inst_iter = self.layout.block_insts(block);
334 
335         // Ignore all instructions prior to the first branch.
336         let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());
337 
338         // A conditional branch is permitted in a basic block only when followed
339         // by a terminal jump instruction.
340         if let Some(_branch) = inst_iter.next() {
341             if let Some(next) = inst_iter.next() {
342                 match dfg.insts[next].opcode() {
343                     Opcode::Jump => (),
344                     _ => return Err((next, "post-branch instruction not jump")),
345                 }
346             }
347         }
348 
349         Ok(())
350     }
351 
352     /// Returns true if the function is function that doesn't call any other functions. This is not
353     /// to be confused with a "leaf function" in Windows terminology.
354     pub fn is_leaf(&self) -> bool {
355         // Conservative result: if there's at least one function signature referenced in this
356         // function, assume it is not a leaf.
357         self.dfg.signatures.is_empty()
358     }
359 
360     /// Replace the `dst` instruction's data with the `src` instruction's data
361     /// and then remove `src`.
362     ///
363     /// `src` and its result values should not be used at all, as any uses would
364     /// be left dangling after calling this method.
365     ///
366     /// `src` and `dst` must have the same number of resulting values, and
367     /// `src`'s i^th value must have the same type as `dst`'s i^th value.
368     pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
369         debug_assert_eq!(
370             self.dfg.inst_results(dst).len(),
371             self.dfg.inst_results(src).len()
372         );
373         debug_assert!(self
374             .dfg
375             .inst_results(dst)
376             .iter()
377             .zip(self.dfg.inst_results(src))
378             .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b)));
379 
380         self.dfg.insts[dst] = self.dfg.insts[src];
381         self.layout.remove_inst(src);
382     }
383 
384     /// Size occupied by all stack slots associated with this function.
385     ///
386     /// Does not include any padding necessary due to offsets
387     pub fn fixed_stack_size(&self) -> u32 {
388         self.sized_stack_slots.values().map(|ss| ss.size).sum()
389     }
390 
391     /// Returns the list of relative source locations for this function.
392     pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {
393         &self.srclocs
394     }
395 }
396 
397 /// Functions can be cloned, but it is not a very fast operation.
398 /// The clone will have all the same entity numbers as the original.
399 #[derive(Clone)]
400 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
401 pub struct Function {
402     /// Name of this function.
403     ///
404     /// Mostly used by `.clif` files, only there for debugging / naming purposes.
405     pub name: UserFuncName,
406 
407     /// All the fields required for compiling a function, independently of details irrelevant to
408     /// compilation and that are stored in the `FunctionParameters` `params` field instead.
409     pub stencil: FunctionStencil,
410 
411     /// All the parameters that can be applied onto the function stencil, that is, that don't
412     /// matter when caching compilation artifacts.
413     pub params: FunctionParameters,
414 }
415 
416 impl core::ops::Deref for Function {
417     type Target = FunctionStencil;
418 
419     fn deref(&self) -> &Self::Target {
420         &self.stencil
421     }
422 }
423 
424 impl core::ops::DerefMut for Function {
425     fn deref_mut(&mut self) -> &mut Self::Target {
426         &mut self.stencil
427     }
428 }
429 
430 impl Function {
431     /// Create a function with the given name and signature.
432     pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {
433         Self {
434             name,
435             stencil: FunctionStencil {
436                 version_marker: VersionMarker,
437                 signature: sig,
438                 sized_stack_slots: StackSlots::new(),
439                 dynamic_stack_slots: DynamicStackSlots::new(),
440                 global_values: PrimaryMap::new(),
441                 tables: PrimaryMap::new(),
442                 jump_tables: PrimaryMap::new(),
443                 dfg: DataFlowGraph::new(),
444                 layout: Layout::new(),
445                 srclocs: SecondaryMap::new(),
446                 stack_limit: None,
447             },
448             params: FunctionParameters::new(),
449         }
450     }
451 
452     /// Clear all data structures in this function.
453     pub fn clear(&mut self) {
454         self.stencil.clear();
455         self.params.clear();
456         self.name = UserFuncName::default();
457     }
458 
459     /// Create a new empty, anonymous function with a Fast calling convention.
460     pub fn new() -> Self {
461         Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))
462     }
463 
464     /// Return an object that can display this function with correct ISA-specific annotations.
465     pub fn display(&self) -> DisplayFunction<'_> {
466         DisplayFunction(self, Default::default())
467     }
468 
469     /// Return an object that can display this function with correct ISA-specific annotations.
470     pub fn display_with<'a>(
471         &'a self,
472         annotations: DisplayFunctionAnnotations<'a>,
473     ) -> DisplayFunction<'a> {
474         DisplayFunction(self, annotations)
475     }
476 
477     /// Sets an absolute source location for the given instruction.
478     ///
479     /// If no base source location has been set yet, records it at the same time.
480     pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {
481         let base = self.params.ensure_base_srcloc(srcloc);
482         self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);
483     }
484 
485     /// Returns an absolute source location for the given instruction.
486     pub fn srcloc(&self, inst: Inst) -> SourceLoc {
487         let base = self.params.base_srcloc();
488         self.stencil.srclocs[inst].expand(base)
489     }
490 
491     /// Declare a user-defined external function import, to be referenced in `ExtFuncData::User` later.
492     pub fn declare_imported_user_function(
493         &mut self,
494         name: UserExternalName,
495     ) -> UserExternalNameRef {
496         self.params.ensure_user_func_name(name)
497     }
498 
499     /// Declare an external function import.
500     pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
501         self.stencil.dfg.ext_funcs.push(data)
502     }
503 }
504 
505 /// Additional annotations for function display.
506 #[derive(Default)]
507 pub struct DisplayFunctionAnnotations<'a> {
508     /// Enable value labels annotations.
509     pub value_ranges: Option<&'a ValueLabelsRanges>,
510 }
511 
512 /// Wrapper type capable of displaying a `Function` with correct ISA annotations.
513 pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>);
514 
515 impl<'a> fmt::Display for DisplayFunction<'a> {
516     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
517         write_function(fmt, self.0)
518     }
519 }
520 
521 impl fmt::Display for Function {
522     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
523         write_function(fmt, self)
524     }
525 }
526 
527 impl fmt::Debug for Function {
528     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
529         write_function(fmt, self)
530     }
531 }
532