1747ad3c4Slazypassion //! Intermediate representation of a function.
2747ad3c4Slazypassion //!
3832666c4SRyan Hunt //! The `Function` struct defined in this module owns all of its basic blocks and
4747ad3c4Slazypassion //! instructions.
5747ad3c4Slazypassion 
690ac295eSAlex Crichton use crate::HashMap;
7747ad3c4Slazypassion use crate::entity::{PrimaryMap, SecondaryMap};
8a3d6e407SChris Fallin use crate::ir::DebugTags;
9747ad3c4Slazypassion use crate::ir::{
1090ac295eSAlex Crichton     self, Block, DataFlowGraph, DynamicStackSlot, DynamicStackSlotData, DynamicStackSlots,
1190ac295eSAlex Crichton     DynamicType, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Inst, JumpTable,
12*2f7dbd61SChris Fallin     JumpTableData, Layout, SigRef, Signature, SourceLocs, StackSlot, StackSlotData, StackSlots,
13*2f7dbd61SChris Fallin     Type,
14747ad3c4Slazypassion };
1543a86f14SBenjamin Bouvier use crate::isa::CallConv;
163da7fc8eSSingleAccretion use crate::write::{write_function, write_function_spec};
17a0c2276eSbjorn3 #[cfg(feature = "enable-serde")]
18a0c2276eSbjorn3 use alloc::string::String;
19747ad3c4Slazypassion use core::fmt;
20747ad3c4Slazypassion 
212fc964eaSbjorn3 #[cfg(feature = "enable-serde")]
22a0c2276eSbjorn3 use serde::de::{Deserializer, Error};
23a0c2276eSbjorn3 #[cfg(feature = "enable-serde")]
24a0c2276eSbjorn3 use serde::ser::Serializer;
25a0c2276eSbjorn3 #[cfg(feature = "enable-serde")]
262fc964eaSbjorn3 use serde::{Deserialize, Serialize};
272fc964eaSbjorn3 
288a9b1a90SBenjamin Bouvier use super::entities::UserExternalNameRef;
298a9b1a90SBenjamin Bouvier use super::extname::UserFuncName;
308a9b1a90SBenjamin Bouvier use super::{RelSourceLoc, SourceLoc, UserExternalName};
318a9b1a90SBenjamin Bouvier 
32a0c2276eSbjorn3 /// A version marker used to ensure that serialized clif ir is never deserialized with a
33a0c2276eSbjorn3 /// different version of Cranelift.
3491d1d246Sbjorn3 #[derive(Default, Copy, Clone, Debug, PartialEq, Hash)]
35a0c2276eSbjorn3 pub struct VersionMarker;
36a0c2276eSbjorn3 
37a0c2276eSbjorn3 #[cfg(feature = "enable-serde")]
38a0c2276eSbjorn3 impl Serialize for VersionMarker {
serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,39a0c2276eSbjorn3     fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
40a0c2276eSbjorn3     where
41a0c2276eSbjorn3         S: Serializer,
42a0c2276eSbjorn3     {
43a0c2276eSbjorn3         crate::VERSION.serialize(serializer)
44a0c2276eSbjorn3     }
45a0c2276eSbjorn3 }
46a0c2276eSbjorn3 
47a0c2276eSbjorn3 #[cfg(feature = "enable-serde")]
48a0c2276eSbjorn3 impl<'de> Deserialize<'de> for VersionMarker {
deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de>,49a0c2276eSbjorn3     fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
50a0c2276eSbjorn3     where
51a0c2276eSbjorn3         D: Deserializer<'de>,
52a0c2276eSbjorn3     {
53a0c2276eSbjorn3         let version = String::deserialize(deserializer)?;
54a0c2276eSbjorn3         if version != crate::VERSION {
55a0c2276eSbjorn3             return Err(D::Error::custom(&format!(
56a0c2276eSbjorn3                 "Expected a clif ir function for version {}, found one for version {}",
57a0c2276eSbjorn3                 crate::VERSION,
58a0c2276eSbjorn3                 version,
59a0c2276eSbjorn3             )));
60a0c2276eSbjorn3         }
61a0c2276eSbjorn3         Ok(VersionMarker)
62a0c2276eSbjorn3     }
63a0c2276eSbjorn3 }
64a0c2276eSbjorn3 
658a9b1a90SBenjamin Bouvier /// Function parameters used when creating this function, and that will become applied after
668a9b1a90SBenjamin Bouvier /// compilation to materialize the final `CompiledCode`.
67a9cda5afSAfonso Bordado #[derive(Clone, PartialEq)]
689ec02f9dSChristopher Serr #[cfg_attr(
699ec02f9dSChristopher Serr     feature = "enable-serde",
709ec02f9dSChristopher Serr     derive(serde_derive::Serialize, serde_derive::Deserialize)
719ec02f9dSChristopher Serr )]
728a9b1a90SBenjamin Bouvier pub struct FunctionParameters {
738a9b1a90SBenjamin Bouvier     /// The first `SourceLoc` appearing in the function, serving as a base for every relative
748a9b1a90SBenjamin Bouvier     /// source loc in the function.
758a9b1a90SBenjamin Bouvier     base_srcloc: Option<SourceLoc>,
768a9b1a90SBenjamin Bouvier 
778a9b1a90SBenjamin Bouvier     /// External user-defined function references.
788a9b1a90SBenjamin Bouvier     user_named_funcs: PrimaryMap<UserExternalNameRef, UserExternalName>,
798a9b1a90SBenjamin Bouvier 
808a9b1a90SBenjamin Bouvier     /// Inverted mapping of `user_named_funcs`, to deduplicate internally.
818a9b1a90SBenjamin Bouvier     user_ext_name_to_ref: HashMap<UserExternalName, UserExternalNameRef>,
828a9b1a90SBenjamin Bouvier }
838a9b1a90SBenjamin Bouvier 
848a9b1a90SBenjamin Bouvier impl FunctionParameters {
858a9b1a90SBenjamin Bouvier     /// Creates a new `FunctionParameters` with the given name.
new() -> Self868a9b1a90SBenjamin Bouvier     pub fn new() -> Self {
878a9b1a90SBenjamin Bouvier         Self {
888a9b1a90SBenjamin Bouvier             base_srcloc: None,
898a9b1a90SBenjamin Bouvier             user_named_funcs: Default::default(),
908a9b1a90SBenjamin Bouvier             user_ext_name_to_ref: Default::default(),
918a9b1a90SBenjamin Bouvier         }
928a9b1a90SBenjamin Bouvier     }
938a9b1a90SBenjamin Bouvier 
948a9b1a90SBenjamin Bouvier     /// Returns the base `SourceLoc`.
958a9b1a90SBenjamin Bouvier     ///
968a9b1a90SBenjamin Bouvier     /// If it was never explicitly set with `ensure_base_srcloc`, will return an invalid
978a9b1a90SBenjamin Bouvier     /// `SourceLoc`.
base_srcloc(&self) -> SourceLoc988a9b1a90SBenjamin Bouvier     pub fn base_srcloc(&self) -> SourceLoc {
998a9b1a90SBenjamin Bouvier         self.base_srcloc.unwrap_or_default()
1008a9b1a90SBenjamin Bouvier     }
1018a9b1a90SBenjamin Bouvier 
1028a9b1a90SBenjamin Bouvier     /// Sets the base `SourceLoc`, if not set yet, and returns the base value.
ensure_base_srcloc(&mut self, srcloc: SourceLoc) -> SourceLoc1038a9b1a90SBenjamin Bouvier     pub fn ensure_base_srcloc(&mut self, srcloc: SourceLoc) -> SourceLoc {
1048a9b1a90SBenjamin Bouvier         match self.base_srcloc {
1058a9b1a90SBenjamin Bouvier             Some(val) => val,
1068a9b1a90SBenjamin Bouvier             None => {
1078a9b1a90SBenjamin Bouvier                 self.base_srcloc = Some(srcloc);
1088a9b1a90SBenjamin Bouvier                 srcloc
1098a9b1a90SBenjamin Bouvier             }
1108a9b1a90SBenjamin Bouvier         }
1118a9b1a90SBenjamin Bouvier     }
1128a9b1a90SBenjamin Bouvier 
1138a9b1a90SBenjamin Bouvier     /// Retrieve a `UserExternalNameRef` for the given name, or add a new one.
1148a9b1a90SBenjamin Bouvier     ///
1158a9b1a90SBenjamin Bouvier     /// This method internally deduplicates same `UserExternalName` so they map to the same
1168a9b1a90SBenjamin Bouvier     /// reference.
ensure_user_func_name(&mut self, name: UserExternalName) -> UserExternalNameRef1178a9b1a90SBenjamin Bouvier     pub fn ensure_user_func_name(&mut self, name: UserExternalName) -> UserExternalNameRef {
1188a9b1a90SBenjamin Bouvier         if let Some(reff) = self.user_ext_name_to_ref.get(&name) {
1198a9b1a90SBenjamin Bouvier             *reff
1208a9b1a90SBenjamin Bouvier         } else {
1218a9b1a90SBenjamin Bouvier             let reff = self.user_named_funcs.push(name.clone());
1228a9b1a90SBenjamin Bouvier             self.user_ext_name_to_ref.insert(name, reff);
1238a9b1a90SBenjamin Bouvier             reff
1248a9b1a90SBenjamin Bouvier         }
1258a9b1a90SBenjamin Bouvier     }
1268a9b1a90SBenjamin Bouvier 
1278a9b1a90SBenjamin Bouvier     /// Resets an already existing user function name to a new value.
reset_user_func_name(&mut self, index: UserExternalNameRef, name: UserExternalName)1288a9b1a90SBenjamin Bouvier     pub fn reset_user_func_name(&mut self, index: UserExternalNameRef, name: UserExternalName) {
1298a9b1a90SBenjamin Bouvier         if let Some(prev_name) = self.user_named_funcs.get_mut(index) {
1308a9b1a90SBenjamin Bouvier             self.user_ext_name_to_ref.remove(prev_name);
1318a9b1a90SBenjamin Bouvier             *prev_name = name.clone();
1328a9b1a90SBenjamin Bouvier             self.user_ext_name_to_ref.insert(name, index);
1338a9b1a90SBenjamin Bouvier         }
1348a9b1a90SBenjamin Bouvier     }
1358a9b1a90SBenjamin Bouvier 
1368a9b1a90SBenjamin Bouvier     /// Returns the internal mapping of `UserExternalNameRef` to `UserExternalName`.
user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName>1378a9b1a90SBenjamin Bouvier     pub fn user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName> {
1388a9b1a90SBenjamin Bouvier         &self.user_named_funcs
1398a9b1a90SBenjamin Bouvier     }
1408a9b1a90SBenjamin Bouvier 
clear(&mut self)1418a9b1a90SBenjamin Bouvier     fn clear(&mut self) {
1428a9b1a90SBenjamin Bouvier         self.base_srcloc = None;
1438a9b1a90SBenjamin Bouvier         self.user_named_funcs.clear();
1448a9b1a90SBenjamin Bouvier         self.user_ext_name_to_ref.clear();
1458a9b1a90SBenjamin Bouvier     }
1468a9b1a90SBenjamin Bouvier }
1478a9b1a90SBenjamin Bouvier 
1488a9b1a90SBenjamin Bouvier /// Function fields needed when compiling a function.
1498a9b1a90SBenjamin Bouvier ///
1508a9b1a90SBenjamin Bouvier /// Additionally, these fields can be the same for two functions that would be compiled the same
1518a9b1a90SBenjamin Bouvier /// way, and finalized by applying `FunctionParameters` onto their `CompiledCodeStencil`.
1528a9b1a90SBenjamin Bouvier #[derive(Clone, PartialEq, Hash)]
1539ec02f9dSChristopher Serr #[cfg_attr(
1549ec02f9dSChristopher Serr     feature = "enable-serde",
1559ec02f9dSChristopher Serr     derive(serde_derive::Serialize, serde_derive::Deserialize)
1569ec02f9dSChristopher Serr )]
1578a9b1a90SBenjamin Bouvier pub struct FunctionStencil {
158a0c2276eSbjorn3     /// A version marker used to ensure that serialized clif ir is never deserialized with a
159a0c2276eSbjorn3     /// different version of Cranelift.
160a0c2276eSbjorn3     // Note: This must be the first field to ensure that Serde will deserialize it before
161a0c2276eSbjorn3     // attempting to deserialize other fields that are potentially changed between versions.
162a0c2276eSbjorn3     pub version_marker: VersionMarker,
163a0c2276eSbjorn3 
164747ad3c4Slazypassion     /// Signature of this function.
165747ad3c4Slazypassion     pub signature: Signature,
166747ad3c4Slazypassion 
1679c43749dSSam Parker     /// Sized stack slots allocated in this function.
1689c43749dSSam Parker     pub sized_stack_slots: StackSlots,
1699c43749dSSam Parker 
1709c43749dSSam Parker     /// Dynamic stack slots allocated in this function.
1719c43749dSSam Parker     pub dynamic_stack_slots: DynamicStackSlots,
172747ad3c4Slazypassion 
173747ad3c4Slazypassion     /// Global values referenced.
174747ad3c4Slazypassion     pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>,
175747ad3c4Slazypassion 
176832666c4SRyan Hunt     /// Data flow graph containing the primary definition of all instructions, blocks and values.
177747ad3c4Slazypassion     pub dfg: DataFlowGraph,
178747ad3c4Slazypassion 
179832666c4SRyan Hunt     /// Layout of blocks and instructions in the function body.
180747ad3c4Slazypassion     pub layout: Layout,
181747ad3c4Slazypassion 
182747ad3c4Slazypassion     /// Source locations.
183747ad3c4Slazypassion     ///
184747ad3c4Slazypassion     /// Track the original source location for each instruction. The source locations are not
185747ad3c4Slazypassion     /// interpreted by Cranelift, only preserved.
1862be12a51SChris Fallin     pub srclocs: SourceLocs,
1878923bac7SPeter Huene 
188a3d6e407SChris Fallin     /// Opaque debug-info tags on sequence-point and call
189a3d6e407SChris Fallin     /// instructions.
190a3d6e407SChris Fallin     ///
191a3d6e407SChris Fallin     /// These tags are not interpreted by Cranelift, and are passed
192a3d6e407SChris Fallin     /// through to compilation-result metadata. The only semantic
193a3d6e407SChris Fallin     /// structure that Cranelift imposes is that when inlining, it
194a3d6e407SChris Fallin     /// prepends the callsite call instruction's tags to the tags on
195a3d6e407SChris Fallin     /// inlined instructions.
196a3d6e407SChris Fallin     ///
197a3d6e407SChris Fallin     /// In order to ensure clarity around guaranteed compiler
198a3d6e407SChris Fallin     /// behavior, tags are only permitted on instructions whose
199a3d6e407SChris Fallin     /// presence and sequence will remain the same in the compiled
200a3d6e407SChris Fallin     /// output: namely, `sequence_point` instructions and ordinary
201a3d6e407SChris Fallin     /// call instructions.
202a3d6e407SChris Fallin     pub debug_tags: DebugTags,
203a3d6e407SChris Fallin 
204c9a0ba81SAlex Crichton     /// An optional global value which represents an expression evaluating to
205c9a0ba81SAlex Crichton     /// the stack limit for this function. This `GlobalValue` will be
206c9a0ba81SAlex Crichton     /// interpreted in the prologue, if necessary, to insert a stack check to
207c9a0ba81SAlex Crichton     /// ensure that a trap happens if the stack pointer goes below the
208c9a0ba81SAlex Crichton     /// threshold specified here.
209c9a0ba81SAlex Crichton     pub stack_limit: Option<ir::GlobalValue>,
210747ad3c4Slazypassion }
211747ad3c4Slazypassion 
2128a9b1a90SBenjamin Bouvier impl FunctionStencil {
clear(&mut self)2138a9b1a90SBenjamin Bouvier     fn clear(&mut self) {
214747ad3c4Slazypassion         self.signature.clear(CallConv::Fast);
2159c43749dSSam Parker         self.sized_stack_slots.clear();
2169c43749dSSam Parker         self.dynamic_stack_slots.clear();
217747ad3c4Slazypassion         self.global_values.clear();
218747ad3c4Slazypassion         self.dfg.clear();
219747ad3c4Slazypassion         self.layout.clear();
220747ad3c4Slazypassion         self.srclocs.clear();
221a3d6e407SChris Fallin         self.debug_tags.clear();
222c9a0ba81SAlex Crichton         self.stack_limit = None;
223747ad3c4Slazypassion     }
224747ad3c4Slazypassion 
225747ad3c4Slazypassion     /// Creates a jump table in the function, to be used by `br_table` instructions.
create_jump_table(&mut self, data: JumpTableData) -> JumpTable226747ad3c4Slazypassion     pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable {
227b0b3f67cSTrevor Elliott         self.dfg.jump_tables.push(data)
228747ad3c4Slazypassion     }
229747ad3c4Slazypassion 
2309c43749dSSam Parker     /// Creates a sized stack slot in the function, to be used by `stack_load`, `stack_store`
2319c43749dSSam Parker     /// and `stack_addr` instructions.
create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot2329c43749dSSam Parker     pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot {
2339c43749dSSam Parker         self.sized_stack_slots.push(data)
2349c43749dSSam Parker     }
2359c43749dSSam Parker 
2369c43749dSSam Parker     /// Creates a dynamic stack slot in the function, to be used by `dynamic_stack_load`,
2379c43749dSSam Parker     /// `dynamic_stack_store` and `dynamic_stack_addr` instructions.
create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot2389c43749dSSam Parker     pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot {
2399c43749dSSam Parker         self.dynamic_stack_slots.push(data)
240747ad3c4Slazypassion     }
241747ad3c4Slazypassion 
242747ad3c4Slazypassion     /// Adds a signature which can later be used to declare an external function import.
import_signature(&mut self, signature: Signature) -> SigRef243747ad3c4Slazypassion     pub fn import_signature(&mut self, signature: Signature) -> SigRef {
244747ad3c4Slazypassion         self.dfg.signatures.push(signature)
245747ad3c4Slazypassion     }
246747ad3c4Slazypassion 
247747ad3c4Slazypassion     /// Declares a global value accessible to the function.
create_global_value(&mut self, data: GlobalValueData) -> GlobalValue248747ad3c4Slazypassion     pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue {
249747ad3c4Slazypassion         self.global_values.push(data)
250747ad3c4Slazypassion     }
251747ad3c4Slazypassion 
2524053ae9eSkevaundray     /// Find the global dyn_scale value associated with given DynamicType.
get_dyn_scale(&self, ty: DynamicType) -> GlobalValue2539c43749dSSam Parker     pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue {
2549c43749dSSam Parker         self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale
2559c43749dSSam Parker     }
2569c43749dSSam Parker 
2579c43749dSSam Parker     /// Find the global dyn_scale for the given stack slot.
get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue2589c43749dSSam Parker     pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue {
2599c43749dSSam Parker         let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty;
2609c43749dSSam Parker         self.get_dyn_scale(dyn_ty)
2619c43749dSSam Parker     }
2629c43749dSSam Parker 
2639c43749dSSam Parker     /// Get a concrete `Type` from a user defined `DynamicType`.
get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type>2649c43749dSSam Parker     pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> {
2659c43749dSSam Parker         self.dfg
2669c43749dSSam Parker             .dynamic_types
2679c43749dSSam Parker             .get(ty)
268a0442ea0SHamir Mahal             .unwrap_or_else(|| panic!("Undeclared dynamic vector type: {ty}"))
2699c43749dSSam Parker             .concrete()
2709c43749dSSam Parker     }
2719c43749dSSam Parker 
272747ad3c4Slazypassion     /// Find a presumed unique special-purpose function parameter value.
273747ad3c4Slazypassion     ///
274747ad3c4Slazypassion     /// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists.
special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value>275747ad3c4Slazypassion     pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> {
276747ad3c4Slazypassion         let entry = self.layout.entry_block().expect("Function is empty");
277747ad3c4Slazypassion         self.signature
278747ad3c4Slazypassion             .special_param_index(purpose)
279832666c4SRyan Hunt             .map(|i| self.dfg.block_params(entry)[i])
280747ad3c4Slazypassion     }
281747ad3c4Slazypassion 
2828f95c517SYury Delendik     /// Starts collection of debug information.
collect_debug_info(&mut self)2838f95c517SYury Delendik     pub fn collect_debug_info(&mut self) {
2848f95c517SYury Delendik         self.dfg.collect_debug_info();
2858f95c517SYury Delendik     }
2868efaeec5SSean Stangl 
287855a6374SY-Nak     /// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`.
288855a6374SY-Nak     /// Does nothing if called with a non-jump or non-branch instruction.
rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block)289855a6374SY-Nak     pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) {
29094ec88eaSChris Fallin         for dest in self.dfg.insts[inst]
29194ec88eaSChris Fallin             .branch_destination_mut(&mut self.dfg.jump_tables, &mut self.dfg.exception_tables)
29294ec88eaSChris Fallin         {
2931e6c13d8STrevor Elliott             if dest.block(&self.dfg.value_lists) == old_dest {
2942c842599STrevor Elliott                 dest.set_block(new_dest, &mut self.dfg.value_lists)
295b58a197dSTrevor Elliott             }
296b58a197dSTrevor Elliott         }
297855a6374SY-Nak     }
298855a6374SY-Nak 
299832666c4SRyan Hunt     /// Checks that the specified block can be encoded as a basic block.
3008efaeec5SSean Stangl     ///
3018efaeec5SSean Stangl     /// On error, returns the first invalid instruction and an error message.
is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)>302832666c4SRyan Hunt     pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> {
3038efaeec5SSean Stangl         let dfg = &self.dfg;
304832666c4SRyan Hunt         let inst_iter = self.layout.block_insts(block);
3058efaeec5SSean Stangl 
3068efaeec5SSean Stangl         // Ignore all instructions prior to the first branch.
30725bf8e0eSTrevor Elliott         let mut inst_iter = inst_iter.skip_while(|&inst| !dfg.insts[inst].opcode().is_branch());
3088efaeec5SSean Stangl 
3098efaeec5SSean Stangl         if let Some(_branch) = inst_iter.next() {
3108efaeec5SSean Stangl             if let Some(next) = inst_iter.next() {
3113c089804Sbjorn3                 return Err((next, "post-terminator instruction"));
3128efaeec5SSean Stangl             }
3138efaeec5SSean Stangl         }
3148efaeec5SSean Stangl 
3158efaeec5SSean Stangl         Ok(())
3168efaeec5SSean Stangl     }
317143cb014SBenjamin Bouvier 
318f763f0e7SNick Fitzgerald     /// Returns an iterator over the blocks succeeding the given block.
block_successors(&self, block: Block) -> impl DoubleEndedIterator<Item = Block> + '_319f763f0e7SNick Fitzgerald     pub fn block_successors(&self, block: Block) -> impl DoubleEndedIterator<Item = Block> + '_ {
320f763f0e7SNick Fitzgerald         self.layout.last_inst(block).into_iter().flat_map(|inst| {
321f763f0e7SNick Fitzgerald             self.dfg.insts[inst]
32294ec88eaSChris Fallin                 .branch_destination(&self.dfg.jump_tables, &self.dfg.exception_tables)
323f763f0e7SNick Fitzgerald                 .iter()
324f763f0e7SNick Fitzgerald                 .map(|block| block.block(&self.dfg.value_lists))
325f763f0e7SNick Fitzgerald         })
326f763f0e7SNick Fitzgerald     }
327f763f0e7SNick Fitzgerald 
328090d1c2dSNick Fitzgerald     /// Replace the `dst` instruction's data with the `src` instruction's data
329090d1c2dSNick Fitzgerald     /// and then remove `src`.
330090d1c2dSNick Fitzgerald     ///
331090d1c2dSNick Fitzgerald     /// `src` and its result values should not be used at all, as any uses would
332090d1c2dSNick Fitzgerald     /// be left dangling after calling this method.
333090d1c2dSNick Fitzgerald     ///
334090d1c2dSNick Fitzgerald     /// `src` and `dst` must have the same number of resulting values, and
335090d1c2dSNick Fitzgerald     /// `src`'s i^th value must have the same type as `dst`'s i^th value.
transplant_inst(&mut self, dst: Inst, src: Inst)336090d1c2dSNick Fitzgerald     pub fn transplant_inst(&mut self, dst: Inst, src: Inst) {
337090d1c2dSNick Fitzgerald         debug_assert_eq!(
338090d1c2dSNick Fitzgerald             self.dfg.inst_results(dst).len(),
339090d1c2dSNick Fitzgerald             self.dfg.inst_results(src).len()
340090d1c2dSNick Fitzgerald         );
34190ac295eSAlex Crichton         debug_assert!(
34290ac295eSAlex Crichton             self.dfg
343090d1c2dSNick Fitzgerald                 .inst_results(dst)
344090d1c2dSNick Fitzgerald                 .iter()
345090d1c2dSNick Fitzgerald                 .zip(self.dfg.inst_results(src))
34690ac295eSAlex Crichton                 .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b))
34790ac295eSAlex Crichton         );
348090d1c2dSNick Fitzgerald 
34925bf8e0eSTrevor Elliott         self.dfg.insts[dst] = self.dfg.insts[src];
350090d1c2dSNick Fitzgerald         self.layout.remove_inst(src);
351090d1c2dSNick Fitzgerald     }
3522776074dSAfonso Bordado 
3532776074dSAfonso Bordado     /// Size occupied by all stack slots associated with this function.
3542776074dSAfonso Bordado     ///
3552776074dSAfonso Bordado     /// Does not include any padding necessary due to offsets
fixed_stack_size(&self) -> u323569c43749dSSam Parker     pub fn fixed_stack_size(&self) -> u32 {
3579c43749dSSam Parker         self.sized_stack_slots.values().map(|ss| ss.size).sum()
3582776074dSAfonso Bordado     }
3598a9b1a90SBenjamin Bouvier 
3608a9b1a90SBenjamin Bouvier     /// Returns the list of relative source locations for this function.
rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc>3618a9b1a90SBenjamin Bouvier     pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> {
3628a9b1a90SBenjamin Bouvier         &self.srclocs
3638a9b1a90SBenjamin Bouvier     }
3648a9b1a90SBenjamin Bouvier }
3658a9b1a90SBenjamin Bouvier 
3668a9b1a90SBenjamin Bouvier /// Functions can be cloned, but it is not a very fast operation.
3678a9b1a90SBenjamin Bouvier /// The clone will have all the same entity numbers as the original.
368a9cda5afSAfonso Bordado #[derive(Clone, PartialEq)]
3698a9b1a90SBenjamin Bouvier #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
3708a9b1a90SBenjamin Bouvier pub struct Function {
3718a9b1a90SBenjamin Bouvier     /// Name of this function.
3728a9b1a90SBenjamin Bouvier     ///
3738a9b1a90SBenjamin Bouvier     /// Mostly used by `.clif` files, only there for debugging / naming purposes.
3748a9b1a90SBenjamin Bouvier     pub name: UserFuncName,
3758a9b1a90SBenjamin Bouvier 
3768a9b1a90SBenjamin Bouvier     /// All the fields required for compiling a function, independently of details irrelevant to
3778a9b1a90SBenjamin Bouvier     /// compilation and that are stored in the `FunctionParameters` `params` field instead.
3788a9b1a90SBenjamin Bouvier     pub stencil: FunctionStencil,
3798a9b1a90SBenjamin Bouvier 
3808a9b1a90SBenjamin Bouvier     /// All the parameters that can be applied onto the function stencil, that is, that don't
3818a9b1a90SBenjamin Bouvier     /// matter when caching compilation artifacts.
3828a9b1a90SBenjamin Bouvier     pub params: FunctionParameters,
3838a9b1a90SBenjamin Bouvier }
3848a9b1a90SBenjamin Bouvier 
3858a9b1a90SBenjamin Bouvier impl core::ops::Deref for Function {
3868a9b1a90SBenjamin Bouvier     type Target = FunctionStencil;
3878a9b1a90SBenjamin Bouvier 
deref(&self) -> &Self::Target3888a9b1a90SBenjamin Bouvier     fn deref(&self) -> &Self::Target {
3898a9b1a90SBenjamin Bouvier         &self.stencil
3908a9b1a90SBenjamin Bouvier     }
3918a9b1a90SBenjamin Bouvier }
3928a9b1a90SBenjamin Bouvier 
3938a9b1a90SBenjamin Bouvier impl core::ops::DerefMut for Function {
deref_mut(&mut self) -> &mut Self::Target3948a9b1a90SBenjamin Bouvier     fn deref_mut(&mut self) -> &mut Self::Target {
3958a9b1a90SBenjamin Bouvier         &mut self.stencil
3968a9b1a90SBenjamin Bouvier     }
3978a9b1a90SBenjamin Bouvier }
3988a9b1a90SBenjamin Bouvier 
3998a9b1a90SBenjamin Bouvier impl Function {
4008a9b1a90SBenjamin Bouvier     /// Create a function with the given name and signature.
with_name_signature(name: UserFuncName, sig: Signature) -> Self4018a9b1a90SBenjamin Bouvier     pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self {
4028a9b1a90SBenjamin Bouvier         Self {
4038a9b1a90SBenjamin Bouvier             name,
4048a9b1a90SBenjamin Bouvier             stencil: FunctionStencil {
4058a9b1a90SBenjamin Bouvier                 version_marker: VersionMarker,
4068a9b1a90SBenjamin Bouvier                 signature: sig,
4078a9b1a90SBenjamin Bouvier                 sized_stack_slots: StackSlots::new(),
4088a9b1a90SBenjamin Bouvier                 dynamic_stack_slots: DynamicStackSlots::new(),
4098a9b1a90SBenjamin Bouvier                 global_values: PrimaryMap::new(),
4108a9b1a90SBenjamin Bouvier                 dfg: DataFlowGraph::new(),
4118a9b1a90SBenjamin Bouvier                 layout: Layout::new(),
4128a9b1a90SBenjamin Bouvier                 srclocs: SecondaryMap::new(),
4138a9b1a90SBenjamin Bouvier                 stack_limit: None,
414a3d6e407SChris Fallin                 debug_tags: DebugTags::default(),
4158a9b1a90SBenjamin Bouvier             },
4168a9b1a90SBenjamin Bouvier             params: FunctionParameters::new(),
4178a9b1a90SBenjamin Bouvier         }
4188a9b1a90SBenjamin Bouvier     }
4198a9b1a90SBenjamin Bouvier 
4208a9b1a90SBenjamin Bouvier     /// Clear all data structures in this function.
clear(&mut self)4218a9b1a90SBenjamin Bouvier     pub fn clear(&mut self) {
4228a9b1a90SBenjamin Bouvier         self.stencil.clear();
4238a9b1a90SBenjamin Bouvier         self.params.clear();
4248a9b1a90SBenjamin Bouvier         self.name = UserFuncName::default();
4258a9b1a90SBenjamin Bouvier     }
4268a9b1a90SBenjamin Bouvier 
4278a9b1a90SBenjamin Bouvier     /// Create a new empty, anonymous function with a Fast calling convention.
new() -> Self4288a9b1a90SBenjamin Bouvier     pub fn new() -> Self {
4298a9b1a90SBenjamin Bouvier         Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast))
4308a9b1a90SBenjamin Bouvier     }
4318a9b1a90SBenjamin Bouvier 
4328a9b1a90SBenjamin Bouvier     /// Return an object that can display this function with correct ISA-specific annotations.
display(&self) -> DisplayFunction<'_>4338a9b1a90SBenjamin Bouvier     pub fn display(&self) -> DisplayFunction<'_> {
434729e2640Sbjorn3         DisplayFunction(self)
4358a9b1a90SBenjamin Bouvier     }
4368a9b1a90SBenjamin Bouvier 
4373da7fc8eSSingleAccretion     /// Return an object that can display this function's name and signature.
display_spec(&self) -> DisplayFunctionSpec<'_>4383da7fc8eSSingleAccretion     pub fn display_spec(&self) -> DisplayFunctionSpec<'_> {
4393da7fc8eSSingleAccretion         DisplayFunctionSpec(self)
4403da7fc8eSSingleAccretion     }
4413da7fc8eSSingleAccretion 
4428a9b1a90SBenjamin Bouvier     /// Sets an absolute source location for the given instruction.
4438a9b1a90SBenjamin Bouvier     ///
4448a9b1a90SBenjamin Bouvier     /// If no base source location has been set yet, records it at the same time.
set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc)4458a9b1a90SBenjamin Bouvier     pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) {
4468a9b1a90SBenjamin Bouvier         let base = self.params.ensure_base_srcloc(srcloc);
4478a9b1a90SBenjamin Bouvier         self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc);
4488a9b1a90SBenjamin Bouvier     }
4498a9b1a90SBenjamin Bouvier 
4508a9b1a90SBenjamin Bouvier     /// Returns an absolute source location for the given instruction.
srcloc(&self, inst: Inst) -> SourceLoc4518a9b1a90SBenjamin Bouvier     pub fn srcloc(&self, inst: Inst) -> SourceLoc {
4528a9b1a90SBenjamin Bouvier         let base = self.params.base_srcloc();
4538a9b1a90SBenjamin Bouvier         self.stencil.srclocs[inst].expand(base)
4548a9b1a90SBenjamin Bouvier     }
4558a9b1a90SBenjamin Bouvier 
4568a9b1a90SBenjamin Bouvier     /// Declare a user-defined external function import, to be referenced in `ExtFuncData::User` later.
declare_imported_user_function( &mut self, name: UserExternalName, ) -> UserExternalNameRef4578a9b1a90SBenjamin Bouvier     pub fn declare_imported_user_function(
4588a9b1a90SBenjamin Bouvier         &mut self,
4598a9b1a90SBenjamin Bouvier         name: UserExternalName,
4608a9b1a90SBenjamin Bouvier     ) -> UserExternalNameRef {
4618a9b1a90SBenjamin Bouvier         self.params.ensure_user_func_name(name)
4628a9b1a90SBenjamin Bouvier     }
4638a9b1a90SBenjamin Bouvier 
4648a9b1a90SBenjamin Bouvier     /// Declare an external function import.
import_function(&mut self, data: ExtFuncData) -> FuncRef4658a9b1a90SBenjamin Bouvier     pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef {
4668a9b1a90SBenjamin Bouvier         self.stencil.dfg.ext_funcs.push(data)
4678a9b1a90SBenjamin Bouvier     }
4688f95c517SYury Delendik }
4698f95c517SYury Delendik 
470729e2640Sbjorn3 /// Wrapper type capable of displaying a `Function`.
471729e2640Sbjorn3 pub struct DisplayFunction<'a>(&'a Function);
472747ad3c4Slazypassion 
473747ad3c4Slazypassion impl<'a> fmt::Display for DisplayFunction<'a> {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result474747ad3c4Slazypassion     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
47543a86f14SBenjamin Bouvier         write_function(fmt, self.0)
476747ad3c4Slazypassion     }
477747ad3c4Slazypassion }
478747ad3c4Slazypassion 
479747ad3c4Slazypassion impl fmt::Display for Function {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result480747ad3c4Slazypassion     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
48143a86f14SBenjamin Bouvier         write_function(fmt, self)
482747ad3c4Slazypassion     }
483747ad3c4Slazypassion }
484747ad3c4Slazypassion 
485747ad3c4Slazypassion impl fmt::Debug for Function {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result486747ad3c4Slazypassion     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
48743a86f14SBenjamin Bouvier         write_function(fmt, self)
488747ad3c4Slazypassion     }
489747ad3c4Slazypassion }
4903da7fc8eSSingleAccretion 
4913da7fc8eSSingleAccretion /// Wrapper type capable of displaying a 'Function's name and signature.
4923da7fc8eSSingleAccretion pub struct DisplayFunctionSpec<'a>(&'a Function);
4933da7fc8eSSingleAccretion 
4943da7fc8eSSingleAccretion impl<'a> fmt::Display for DisplayFunctionSpec<'a> {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result4953da7fc8eSSingleAccretion     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
4963da7fc8eSSingleAccretion         write_function_spec(fmt, self.0)
4973da7fc8eSSingleAccretion     }
4983da7fc8eSSingleAccretion }
4993da7fc8eSSingleAccretion 
5003da7fc8eSSingleAccretion impl<'a> fmt::Debug for DisplayFunctionSpec<'a> {
fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result5013da7fc8eSSingleAccretion     fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
5023da7fc8eSSingleAccretion         write_function_spec(fmt, self.0)
5033da7fc8eSSingleAccretion     }
5043da7fc8eSSingleAccretion }
505