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     /// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.
281     /// Does nothing if called with a non-jump or non-branch instruction.
282     pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
283         match self.dfg.analyze_branch(inst) {
284             BranchInfo::SingleDest(dest) => {
285                 if dest.block(&self.dfg.value_lists) == old_dest {
286                     for block in self.dfg.insts[inst].branch_destination_mut() {
287                         block.set_block(new_dest, &mut self.dfg.value_lists)
288                     }
289                 }
290             }
291 
292             BranchInfo::Conditional(block_then, block_else) => {
293                 if block_then.block(&self.dfg.value_lists) == old_dest {
294                     if let InstructionData::Brif {
295                         blocks: [block_then, _],
296                         ..
297                     } = &mut self.dfg.insts[inst]
298                     {
299                         block_then.set_block(new_dest, &mut self.dfg.value_lists);
300                     } else {
301                         unreachable!();
302                     }
303                 }
304 
305                 if block_else.block(&self.dfg.value_lists) == old_dest {
306                     if let InstructionData::Brif {
307                         blocks: [_, block_else],
308                         ..
309                     } = &mut self.dfg.insts[inst]
310                     {
311                         block_else.set_block(new_dest, &mut self.dfg.value_lists);
312                     } else {
313                         unreachable!();
314                     }
315                 }
316             }
317 
318             BranchInfo::Table(table, default_dest) => {
319                 self.jump_tables[table].iter_mut().for_each(|entry| {
320                     if *entry == old_dest {
321                         *entry = new_dest;
322                     }
323                 });
324 
325                 if default_dest == old_dest {
326                     match &mut self.dfg.insts[inst] {
327                         InstructionData::BranchTable { destination, .. } => {
328                             *destination = new_dest;
329                         }
330                         _ => panic!(
331                             "Unexpected instruction {} having default destination",
332                             self.dfg.display_inst(inst)
333                         ),
334                     }
335                 }
336             }
337 
338             BranchInfo::NotABranch => {}
339         }
340     }
341 
342     /// Checks that the specified block can be encoded as a basic block.
343     ///
344     /// On error, returns the first invalid instruction and an error message.
345     pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
346         let dfg = &self.dfg;
347         let inst_iter = self.layout.block_insts(block);
348 
349         // Ignore all instructions prior to the first branch.
350         let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());
351 
352         // A conditional branch is permitted in a basic block only when followed
353         // by a terminal jump instruction.
354         if let Some(_branch) = inst_iter.next() {
355             if let Some(next) = inst_iter.next() {
356                 match dfg.insts[next].opcode() {
357                     Opcode::Jump => (),
358                     _ => return Err((next, "post-branch instruction not jump")),
359                 }
360             }
361         }
362 
363         Ok(())
364     }
365 
366     /// Returns true if the function is function that doesn't call any other functions. This is not
367     /// to be confused with a "leaf function" in Windows terminology.
368     pub fn is_leaf(&self) -> bool {
369         // Conservative result: if there's at least one function signature referenced in this
370         // function, assume it is not a leaf.
371         self.dfg.signatures.is_empty()
372     }
373 
374     /// Replace the `dst` instruction's data with the `src` instruction's data
375     /// and then remove `src`.
376     ///
377     /// `src` and its result values should not be used at all, as any uses would
378     /// be left dangling after calling this method.
379     ///
380     /// `src` and `dst` must have the same number of resulting values, and
381     /// `src`'s i^th value must have the same type as `dst`'s i^th value.
382     pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
383         debug_assert_eq!(
384             self.dfg.inst_results(dst).len(),
385             self.dfg.inst_results(src).len()
386         );
387         debug_assert!(self
388             .dfg
389             .inst_results(dst)
390             .iter()
391             .zip(self.dfg.inst_results(src))
392             .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b)));
393 
394         self.dfg.insts[dst] = self.dfg.insts[src];
395         self.layout.remove_inst(src);
396     }
397 
398     /// Size occupied by all stack slots associated with this function.
399     ///
400     /// Does not include any padding necessary due to offsets
401     pub fn fixed_stack_size(&self) -> u32 {
402         self.sized_stack_slots.values().map(|ss| ss.size).sum()
403     }
404 
405     /// Returns the list of relative source locations for this function.
406     pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {
407         &self.srclocs
408     }
409 }
410 
411 /// Functions can be cloned, but it is not a very fast operation.
412 /// The clone will have all the same entity numbers as the original.
413 #[derive(Clone)]
414 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
415 pub struct Function {
416     /// Name of this function.
417     ///
418     /// Mostly used by `.clif` files, only there for debugging / naming purposes.
419     pub name: UserFuncName,
420 
421     /// All the fields required for compiling a function, independently of details irrelevant to
422     /// compilation and that are stored in the `FunctionParameters` `params` field instead.
423     pub stencil: FunctionStencil,
424 
425     /// All the parameters that can be applied onto the function stencil, that is, that don't
426     /// matter when caching compilation artifacts.
427     pub params: FunctionParameters,
428 }
429 
430 impl core::ops::Deref for Function {
431     type Target = FunctionStencil;
432 
433     fn deref(&self) -> &Self::Target {
434         &self.stencil
435     }
436 }
437 
438 impl core::ops::DerefMut for Function {
439     fn deref_mut(&mut self) -> &mut Self::Target {
440         &mut self.stencil
441     }
442 }
443 
444 impl Function {
445     /// Create a function with the given name and signature.
446     pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {
447         Self {
448             name,
449             stencil: FunctionStencil {
450                 version_marker: VersionMarker,
451                 signature: sig,
452                 sized_stack_slots: StackSlots::new(),
453                 dynamic_stack_slots: DynamicStackSlots::new(),
454                 global_values: PrimaryMap::new(),
455                 tables: PrimaryMap::new(),
456                 jump_tables: PrimaryMap::new(),
457                 dfg: DataFlowGraph::new(),
458                 layout: Layout::new(),
459                 srclocs: SecondaryMap::new(),
460                 stack_limit: None,
461             },
462             params: FunctionParameters::new(),
463         }
464     }
465 
466     /// Clear all data structures in this function.
467     pub fn clear(&mut self) {
468         self.stencil.clear();
469         self.params.clear();
470         self.name = UserFuncName::default();
471     }
472 
473     /// Create a new empty, anonymous function with a Fast calling convention.
474     pub fn new() -> Self {
475         Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))
476     }
477 
478     /// Return an object that can display this function with correct ISA-specific annotations.
479     pub fn display(&self) -> DisplayFunction<'_> {
480         DisplayFunction(self, Default::default())
481     }
482 
483     /// Return an object that can display this function with correct ISA-specific annotations.
484     pub fn display_with<'a>(
485         &'a self,
486         annotations: DisplayFunctionAnnotations<'a>,
487     ) -> DisplayFunction<'a> {
488         DisplayFunction(self, annotations)
489     }
490 
491     /// Sets an absolute source location for the given instruction.
492     ///
493     /// If no base source location has been set yet, records it at the same time.
494     pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {
495         let base = self.params.ensure_base_srcloc(srcloc);
496         self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);
497     }
498 
499     /// Returns an absolute source location for the given instruction.
500     pub fn srcloc(&self, inst: Inst) -> SourceLoc {
501         let base = self.params.base_srcloc();
502         self.stencil.srclocs[inst].expand(base)
503     }
504 
505     /// Declare a user-defined external function import, to be referenced in `ExtFuncData::User` later.
506     pub fn declare_imported_user_function(
507         &mut self,
508         name: UserExternalName,
509     ) -> UserExternalNameRef {
510         self.params.ensure_user_func_name(name)
511     }
512 
513     /// Declare an external function import.
514     pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
515         self.stencil.dfg.ext_funcs.push(data)
516     }
517 }
518 
519 /// Additional annotations for function display.
520 #[derive(Default)]
521 pub struct DisplayFunctionAnnotations<'a> {
522     /// Enable value labels annotations.
523     pub value_ranges: Option<&'a ValueLabelsRanges>,
524 }
525 
526 /// Wrapper type capable of displaying a `Function` with correct ISA annotations.
527 pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>);
528 
529 impl<'a> fmt::Display for DisplayFunction<'a> {
530     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
531         write_function(fmt, self.0)
532     }
533 }
534 
535 impl fmt::Display for Function {
536     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
537         write_function(fmt, self)
538     }
539 }
540 
541 impl fmt::Debug for Function {
542     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
543         write_function(fmt, self)
544     }
545 }
546