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