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 6747ad3c4Slazypassion use crate::entity::{PrimaryMap, SecondaryMap}; 7747ad3c4Slazypassion use crate::ir; 82db3b5b9Sbjorn3 use crate::ir::JumpTables; 9747ad3c4Slazypassion use crate::ir::{ 109c43749dSSam Parker instructions::BranchInfo, Block, DynamicStackSlot, DynamicStackSlotData, DynamicType, 11*c0b587acSNick Fitzgerald ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Inst, InstructionData, JumpTable, 12*c0b587acSNick Fitzgerald JumpTableData, Opcode, SigRef, StackSlot, StackSlotData, Table, TableData, Type, 13747ad3c4Slazypassion }; 148a9b1a90SBenjamin Bouvier use crate::ir::{DataFlowGraph, Layout, Signature}; 159c43749dSSam Parker use crate::ir::{DynamicStackSlots, SourceLocs, StackSlots}; 1643a86f14SBenjamin Bouvier use crate::isa::CallConv; 178f95c517SYury Delendik use crate::value_label::ValueLabelsRanges; 18747ad3c4Slazypassion use crate::write::write_function; 198a9b1a90SBenjamin Bouvier use crate::HashMap; 20a0c2276eSbjorn3 #[cfg(feature = "enable-serde")] 21a0c2276eSbjorn3 use alloc::string::String; 22747ad3c4Slazypassion use core::fmt; 23747ad3c4Slazypassion 242fc964eaSbjorn3 #[cfg(feature = "enable-serde")] 25a0c2276eSbjorn3 use serde::de::{Deserializer, Error}; 26a0c2276eSbjorn3 #[cfg(feature = "enable-serde")] 27a0c2276eSbjorn3 use serde::ser::Serializer; 28a0c2276eSbjorn3 #[cfg(feature = "enable-serde")] 292fc964eaSbjorn3 use serde::{Deserialize, Serialize}; 302fc964eaSbjorn3 318a9b1a90SBenjamin Bouvier use super::entities::UserExternalNameRef; 328a9b1a90SBenjamin Bouvier use super::extname::UserFuncName; 338a9b1a90SBenjamin Bouvier use super::{RelSourceLoc, SourceLoc, UserExternalName}; 348a9b1a90SBenjamin Bouvier 35a0c2276eSbjorn3 /// A version marker used to ensure that serialized clif ir is never deserialized with a 36a0c2276eSbjorn3 /// different version of Cranelift. 378a9b1a90SBenjamin Bouvier #[derive(Copy, Clone, Debug, PartialEq, Hash)] 38a0c2276eSbjorn3 pub struct VersionMarker; 39a0c2276eSbjorn3 40a0c2276eSbjorn3 #[cfg(feature = "enable-serde")] 41a0c2276eSbjorn3 impl Serialize for VersionMarker { 42a0c2276eSbjorn3 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> 43a0c2276eSbjorn3 where 44a0c2276eSbjorn3 S: Serializer, 45a0c2276eSbjorn3 { 46a0c2276eSbjorn3 crate::VERSION.serialize(serializer) 47a0c2276eSbjorn3 } 48a0c2276eSbjorn3 } 49a0c2276eSbjorn3 50a0c2276eSbjorn3 #[cfg(feature = "enable-serde")] 51a0c2276eSbjorn3 impl<'de> Deserialize<'de> for VersionMarker { 52a0c2276eSbjorn3 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> 53a0c2276eSbjorn3 where 54a0c2276eSbjorn3 D: Deserializer<'de>, 55a0c2276eSbjorn3 { 56a0c2276eSbjorn3 let version = String::deserialize(deserializer)?; 57a0c2276eSbjorn3 if version != crate::VERSION { 58a0c2276eSbjorn3 return Err(D::Error::custom(&format!( 59a0c2276eSbjorn3 "Expected a clif ir function for version {}, found one for version {}", 60a0c2276eSbjorn3 crate::VERSION, 61a0c2276eSbjorn3 version, 62a0c2276eSbjorn3 ))); 63a0c2276eSbjorn3 } 64a0c2276eSbjorn3 Ok(VersionMarker) 65a0c2276eSbjorn3 } 66a0c2276eSbjorn3 } 67a0c2276eSbjorn3 688a9b1a90SBenjamin Bouvier /// Function parameters used when creating this function, and that will become applied after 698a9b1a90SBenjamin Bouvier /// compilation to materialize the final `CompiledCode`. 70747ad3c4Slazypassion #[derive(Clone)] 712fc964eaSbjorn3 #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] 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. 868a9b1a90SBenjamin 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`. 988a9b1a90SBenjamin 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. 1038a9b1a90SBenjamin 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. 1178a9b1a90SBenjamin 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. 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`. 1378a9b1a90SBenjamin Bouvier pub fn user_named_funcs(&self) -> &PrimaryMap<UserExternalNameRef, UserExternalName> { 1388a9b1a90SBenjamin Bouvier &self.user_named_funcs 1398a9b1a90SBenjamin Bouvier } 1408a9b1a90SBenjamin Bouvier 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)] 1538a9b1a90SBenjamin Bouvier #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] 1548a9b1a90SBenjamin Bouvier pub struct FunctionStencil { 155a0c2276eSbjorn3 /// A version marker used to ensure that serialized clif ir is never deserialized with a 156a0c2276eSbjorn3 /// different version of Cranelift. 157a0c2276eSbjorn3 // Note: This must be the first field to ensure that Serde will deserialize it before 158a0c2276eSbjorn3 // attempting to deserialize other fields that are potentially changed between versions. 159a0c2276eSbjorn3 pub version_marker: VersionMarker, 160a0c2276eSbjorn3 161747ad3c4Slazypassion /// Signature of this function. 162747ad3c4Slazypassion pub signature: Signature, 163747ad3c4Slazypassion 1649c43749dSSam Parker /// Sized stack slots allocated in this function. 1659c43749dSSam Parker pub sized_stack_slots: StackSlots, 1669c43749dSSam Parker 1679c43749dSSam Parker /// Dynamic stack slots allocated in this function. 1689c43749dSSam Parker pub dynamic_stack_slots: DynamicStackSlots, 169747ad3c4Slazypassion 170747ad3c4Slazypassion /// Global values referenced. 171747ad3c4Slazypassion pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>, 172747ad3c4Slazypassion 173747ad3c4Slazypassion /// Tables referenced. 174747ad3c4Slazypassion pub tables: PrimaryMap<ir::Table, ir::TableData>, 175747ad3c4Slazypassion 176747ad3c4Slazypassion /// Jump tables used in this function. 177747ad3c4Slazypassion pub jump_tables: JumpTables, 178747ad3c4Slazypassion 179832666c4SRyan Hunt /// Data flow graph containing the primary definition of all instructions, blocks and values. 180747ad3c4Slazypassion pub dfg: DataFlowGraph, 181747ad3c4Slazypassion 182832666c4SRyan Hunt /// Layout of blocks and instructions in the function body. 183747ad3c4Slazypassion pub layout: Layout, 184747ad3c4Slazypassion 185747ad3c4Slazypassion /// Source locations. 186747ad3c4Slazypassion /// 187747ad3c4Slazypassion /// Track the original source location for each instruction. The source locations are not 188747ad3c4Slazypassion /// interpreted by Cranelift, only preserved. 1892be12a51SChris Fallin pub srclocs: SourceLocs, 1908923bac7SPeter Huene 191c9a0ba81SAlex Crichton /// An optional global value which represents an expression evaluating to 192c9a0ba81SAlex Crichton /// the stack limit for this function. This `GlobalValue` will be 193c9a0ba81SAlex Crichton /// interpreted in the prologue, if necessary, to insert a stack check to 194c9a0ba81SAlex Crichton /// ensure that a trap happens if the stack pointer goes below the 195c9a0ba81SAlex Crichton /// threshold specified here. 196c9a0ba81SAlex Crichton pub stack_limit: Option<ir::GlobalValue>, 197747ad3c4Slazypassion } 198747ad3c4Slazypassion 1998a9b1a90SBenjamin Bouvier impl FunctionStencil { 2008a9b1a90SBenjamin Bouvier fn clear(&mut self) { 201747ad3c4Slazypassion self.signature.clear(CallConv::Fast); 2029c43749dSSam Parker self.sized_stack_slots.clear(); 2039c43749dSSam Parker self.dynamic_stack_slots.clear(); 204747ad3c4Slazypassion self.global_values.clear(); 205747ad3c4Slazypassion self.tables.clear(); 206747ad3c4Slazypassion self.jump_tables.clear(); 207747ad3c4Slazypassion self.dfg.clear(); 208747ad3c4Slazypassion self.layout.clear(); 209747ad3c4Slazypassion self.srclocs.clear(); 210c9a0ba81SAlex Crichton self.stack_limit = None; 211747ad3c4Slazypassion } 212747ad3c4Slazypassion 213747ad3c4Slazypassion /// Creates a jump table in the function, to be used by `br_table` instructions. 214747ad3c4Slazypassion pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable { 215747ad3c4Slazypassion self.jump_tables.push(data) 216747ad3c4Slazypassion } 217747ad3c4Slazypassion 2189c43749dSSam Parker /// Creates a sized stack slot in the function, to be used by `stack_load`, `stack_store` 2199c43749dSSam Parker /// and `stack_addr` instructions. 2209c43749dSSam Parker pub fn create_sized_stack_slot(&mut self, data: StackSlotData) -> StackSlot { 2219c43749dSSam Parker self.sized_stack_slots.push(data) 2229c43749dSSam Parker } 2239c43749dSSam Parker 2249c43749dSSam Parker /// Creates a dynamic stack slot in the function, to be used by `dynamic_stack_load`, 2259c43749dSSam Parker /// `dynamic_stack_store` and `dynamic_stack_addr` instructions. 2269c43749dSSam Parker pub fn create_dynamic_stack_slot(&mut self, data: DynamicStackSlotData) -> DynamicStackSlot { 2279c43749dSSam Parker self.dynamic_stack_slots.push(data) 228747ad3c4Slazypassion } 229747ad3c4Slazypassion 230747ad3c4Slazypassion /// Adds a signature which can later be used to declare an external function import. 231747ad3c4Slazypassion pub fn import_signature(&mut self, signature: Signature) -> SigRef { 232747ad3c4Slazypassion self.dfg.signatures.push(signature) 233747ad3c4Slazypassion } 234747ad3c4Slazypassion 235747ad3c4Slazypassion /// Declares a global value accessible to the function. 236747ad3c4Slazypassion pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue { 237747ad3c4Slazypassion self.global_values.push(data) 238747ad3c4Slazypassion } 239747ad3c4Slazypassion 2409c43749dSSam Parker /// Find the global dyn_scale value associated with given DynamicType 2419c43749dSSam Parker pub fn get_dyn_scale(&self, ty: DynamicType) -> GlobalValue { 2429c43749dSSam Parker self.dfg.dynamic_types.get(ty).unwrap().dynamic_scale 2439c43749dSSam Parker } 2449c43749dSSam Parker 2459c43749dSSam Parker /// Find the global dyn_scale for the given stack slot. 2469c43749dSSam Parker pub fn get_dynamic_slot_scale(&self, dss: DynamicStackSlot) -> GlobalValue { 2479c43749dSSam Parker let dyn_ty = self.dynamic_stack_slots.get(dss).unwrap().dyn_ty; 2489c43749dSSam Parker self.get_dyn_scale(dyn_ty) 2499c43749dSSam Parker } 2509c43749dSSam Parker 2519c43749dSSam Parker /// Get a concrete `Type` from a user defined `DynamicType`. 2529c43749dSSam Parker pub fn get_concrete_dynamic_ty(&self, ty: DynamicType) -> Option<Type> { 2539c43749dSSam Parker self.dfg 2549c43749dSSam Parker .dynamic_types 2559c43749dSSam Parker .get(ty) 2569c43749dSSam Parker .unwrap_or_else(|| panic!("Undeclared dynamic vector type: {}", ty)) 2579c43749dSSam Parker .concrete() 2589c43749dSSam Parker } 2599c43749dSSam Parker 260747ad3c4Slazypassion /// Declares a table accessible to the function. 261747ad3c4Slazypassion pub fn create_table(&mut self, data: TableData) -> Table { 262747ad3c4Slazypassion self.tables.push(data) 263747ad3c4Slazypassion } 264747ad3c4Slazypassion 265747ad3c4Slazypassion /// Find a presumed unique special-purpose function parameter value. 266747ad3c4Slazypassion /// 267747ad3c4Slazypassion /// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists. 268747ad3c4Slazypassion pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> { 269747ad3c4Slazypassion let entry = self.layout.entry_block().expect("Function is empty"); 270747ad3c4Slazypassion self.signature 271747ad3c4Slazypassion .special_param_index(purpose) 272832666c4SRyan Hunt .map(|i| self.dfg.block_params(entry)[i]) 273747ad3c4Slazypassion } 274747ad3c4Slazypassion 2758f95c517SYury Delendik /// Starts collection of debug information. 2768f95c517SYury Delendik pub fn collect_debug_info(&mut self) { 2778f95c517SYury Delendik self.dfg.collect_debug_info(); 2788f95c517SYury Delendik } 2798efaeec5SSean Stangl 280c7b4b98cSSean Stangl /// Changes the destination of a jump or branch instruction. 281c7b4b98cSSean Stangl /// Does nothing if called with a non-jump or non-branch instruction. 282855a6374SY-Nak /// 283855a6374SY-Nak /// Note that this method ignores multi-destination branches like `br_table`. 284832666c4SRyan Hunt pub fn change_branch_destination(&mut self, inst: Inst, new_dest: Block) { 285c7b4b98cSSean Stangl match self.dfg[inst].branch_destination_mut() { 286c7b4b98cSSean Stangl None => (), 287c7b4b98cSSean Stangl Some(inst_dest) => *inst_dest = new_dest, 288c7b4b98cSSean Stangl } 289c7b4b98cSSean Stangl } 290c7b4b98cSSean Stangl 291855a6374SY-Nak /// Rewrite the branch destination to `new_dest` if the destination matches `old_dest`. 292855a6374SY-Nak /// Does nothing if called with a non-jump or non-branch instruction. 293855a6374SY-Nak /// 2948a9b1a90SBenjamin Bouvier /// Unlike [change_branch_destination](FunctionStencil::change_branch_destination), this method 2958a9b1a90SBenjamin Bouvier /// rewrite the destinations of multi-destination branches like `br_table`. 296855a6374SY-Nak pub fn rewrite_branch_destination(&mut self, inst: Inst, old_dest: Block, new_dest: Block) { 297855a6374SY-Nak match self.dfg.analyze_branch(inst) { 298855a6374SY-Nak BranchInfo::SingleDest(dest, ..) => { 299855a6374SY-Nak if dest == old_dest { 300855a6374SY-Nak self.change_branch_destination(inst, new_dest); 301855a6374SY-Nak } 302855a6374SY-Nak } 303855a6374SY-Nak 304855a6374SY-Nak BranchInfo::Table(table, default_dest) => { 305855a6374SY-Nak self.jump_tables[table].iter_mut().for_each(|entry| { 306855a6374SY-Nak if *entry == old_dest { 307855a6374SY-Nak *entry = new_dest; 308855a6374SY-Nak } 309855a6374SY-Nak }); 310855a6374SY-Nak 311855a6374SY-Nak if default_dest == Some(old_dest) { 312855a6374SY-Nak match &mut self.dfg[inst] { 313855a6374SY-Nak InstructionData::BranchTable { destination, .. } => { 314855a6374SY-Nak *destination = new_dest; 315855a6374SY-Nak } 316855a6374SY-Nak _ => panic!( 317855a6374SY-Nak "Unexpected instruction {} having default destination", 31843a86f14SBenjamin Bouvier self.dfg.display_inst(inst) 319855a6374SY-Nak ), 320855a6374SY-Nak } 321855a6374SY-Nak } 322855a6374SY-Nak } 323855a6374SY-Nak 324855a6374SY-Nak BranchInfo::NotABranch => {} 325855a6374SY-Nak } 326855a6374SY-Nak } 327855a6374SY-Nak 328832666c4SRyan Hunt /// Checks that the specified block can be encoded as a basic block. 3298efaeec5SSean Stangl /// 3308efaeec5SSean Stangl /// On error, returns the first invalid instruction and an error message. 331832666c4SRyan Hunt pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> { 3328efaeec5SSean Stangl let dfg = &self.dfg; 333832666c4SRyan Hunt let inst_iter = self.layout.block_insts(block); 3348efaeec5SSean Stangl 3358efaeec5SSean Stangl // Ignore all instructions prior to the first branch. 3368efaeec5SSean Stangl let mut inst_iter = inst_iter.skip_while(|&inst| !dfg[inst].opcode().is_branch()); 3378efaeec5SSean Stangl 3388efaeec5SSean Stangl // A conditional branch is permitted in a basic block only when followed 3391fd491daSbjorn3 // by a terminal jump instruction. 3408efaeec5SSean Stangl if let Some(_branch) = inst_iter.next() { 3418efaeec5SSean Stangl if let Some(next) = inst_iter.next() { 3428efaeec5SSean Stangl match dfg[next].opcode() { 3431fd491daSbjorn3 Opcode::Jump => (), 3441fd491daSbjorn3 _ => return Err((next, "post-branch instruction not jump")), 3458efaeec5SSean Stangl } 3468efaeec5SSean Stangl } 3478efaeec5SSean Stangl } 3488efaeec5SSean Stangl 3498efaeec5SSean Stangl Ok(()) 3508efaeec5SSean Stangl } 351143cb014SBenjamin Bouvier 352143cb014SBenjamin Bouvier /// Returns true if the function is function that doesn't call any other functions. This is not 353143cb014SBenjamin Bouvier /// to be confused with a "leaf function" in Windows terminology. 354143cb014SBenjamin Bouvier pub fn is_leaf(&self) -> bool { 355143cb014SBenjamin Bouvier // Conservative result: if there's at least one function signature referenced in this 35658e5a62cSY-Nak // function, assume it is not a leaf. 35758e5a62cSY-Nak self.dfg.signatures.is_empty() 358143cb014SBenjamin Bouvier } 359090d1c2dSNick Fitzgerald 360090d1c2dSNick Fitzgerald /// Replace the `dst` instruction's data with the `src` instruction's data 361090d1c2dSNick Fitzgerald /// and then remove `src`. 362090d1c2dSNick Fitzgerald /// 363090d1c2dSNick Fitzgerald /// `src` and its result values should not be used at all, as any uses would 364090d1c2dSNick Fitzgerald /// be left dangling after calling this method. 365090d1c2dSNick Fitzgerald /// 366090d1c2dSNick Fitzgerald /// `src` and `dst` must have the same number of resulting values, and 367090d1c2dSNick Fitzgerald /// `src`'s i^th value must have the same type as `dst`'s i^th value. 368090d1c2dSNick Fitzgerald pub fn transplant_inst(&mut self, dst: Inst, src: Inst) { 369090d1c2dSNick Fitzgerald debug_assert_eq!( 370090d1c2dSNick Fitzgerald self.dfg.inst_results(dst).len(), 371090d1c2dSNick Fitzgerald self.dfg.inst_results(src).len() 372090d1c2dSNick Fitzgerald ); 373090d1c2dSNick Fitzgerald debug_assert!(self 374090d1c2dSNick Fitzgerald .dfg 375090d1c2dSNick Fitzgerald .inst_results(dst) 376090d1c2dSNick Fitzgerald .iter() 377090d1c2dSNick Fitzgerald .zip(self.dfg.inst_results(src)) 378090d1c2dSNick Fitzgerald .all(|(a, b)| self.dfg.value_type(*a) == self.dfg.value_type(*b))); 379090d1c2dSNick Fitzgerald 38003d77d4dSNick Fitzgerald self.dfg[dst] = self.dfg[src]; 381090d1c2dSNick Fitzgerald self.layout.remove_inst(src); 382090d1c2dSNick Fitzgerald } 3832776074dSAfonso Bordado 3842776074dSAfonso Bordado /// Size occupied by all stack slots associated with this function. 3852776074dSAfonso Bordado /// 3862776074dSAfonso Bordado /// Does not include any padding necessary due to offsets 3879c43749dSSam Parker pub fn fixed_stack_size(&self) -> u32 { 3889c43749dSSam Parker self.sized_stack_slots.values().map(|ss| ss.size).sum() 3892776074dSAfonso Bordado } 3908a9b1a90SBenjamin Bouvier 3918a9b1a90SBenjamin Bouvier /// Returns the list of relative source locations for this function. 3928a9b1a90SBenjamin Bouvier pub(crate) fn rel_srclocs(&self) -> &SecondaryMap<Inst, RelSourceLoc> { 3938a9b1a90SBenjamin Bouvier &self.srclocs 3948a9b1a90SBenjamin Bouvier } 3958a9b1a90SBenjamin Bouvier } 3968a9b1a90SBenjamin Bouvier 3978a9b1a90SBenjamin Bouvier /// Functions can be cloned, but it is not a very fast operation. 3988a9b1a90SBenjamin Bouvier /// The clone will have all the same entity numbers as the original. 3998a9b1a90SBenjamin Bouvier #[derive(Clone)] 4008a9b1a90SBenjamin Bouvier #[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))] 4018a9b1a90SBenjamin Bouvier pub struct Function { 4028a9b1a90SBenjamin Bouvier /// Name of this function. 4038a9b1a90SBenjamin Bouvier /// 4048a9b1a90SBenjamin Bouvier /// Mostly used by `.clif` files, only there for debugging / naming purposes. 4058a9b1a90SBenjamin Bouvier pub name: UserFuncName, 4068a9b1a90SBenjamin Bouvier 4078a9b1a90SBenjamin Bouvier /// All the fields required for compiling a function, independently of details irrelevant to 4088a9b1a90SBenjamin Bouvier /// compilation and that are stored in the `FunctionParameters` `params` field instead. 4098a9b1a90SBenjamin Bouvier pub stencil: FunctionStencil, 4108a9b1a90SBenjamin Bouvier 4118a9b1a90SBenjamin Bouvier /// All the parameters that can be applied onto the function stencil, that is, that don't 4128a9b1a90SBenjamin Bouvier /// matter when caching compilation artifacts. 4138a9b1a90SBenjamin Bouvier pub params: FunctionParameters, 4148a9b1a90SBenjamin Bouvier } 4158a9b1a90SBenjamin Bouvier 4168a9b1a90SBenjamin Bouvier impl core::ops::Deref for Function { 4178a9b1a90SBenjamin Bouvier type Target = FunctionStencil; 4188a9b1a90SBenjamin Bouvier 4198a9b1a90SBenjamin Bouvier fn deref(&self) -> &Self::Target { 4208a9b1a90SBenjamin Bouvier &self.stencil 4218a9b1a90SBenjamin Bouvier } 4228a9b1a90SBenjamin Bouvier } 4238a9b1a90SBenjamin Bouvier 4248a9b1a90SBenjamin Bouvier impl core::ops::DerefMut for Function { 4258a9b1a90SBenjamin Bouvier fn deref_mut(&mut self) -> &mut Self::Target { 4268a9b1a90SBenjamin Bouvier &mut self.stencil 4278a9b1a90SBenjamin Bouvier } 4288a9b1a90SBenjamin Bouvier } 4298a9b1a90SBenjamin Bouvier 4308a9b1a90SBenjamin Bouvier impl Function { 4318a9b1a90SBenjamin Bouvier /// Create a function with the given name and signature. 4328a9b1a90SBenjamin Bouvier pub fn with_name_signature(name: UserFuncName, sig: Signature) -> Self { 4338a9b1a90SBenjamin Bouvier Self { 4348a9b1a90SBenjamin Bouvier name, 4358a9b1a90SBenjamin Bouvier stencil: FunctionStencil { 4368a9b1a90SBenjamin Bouvier version_marker: VersionMarker, 4378a9b1a90SBenjamin Bouvier signature: sig, 4388a9b1a90SBenjamin Bouvier sized_stack_slots: StackSlots::new(), 4398a9b1a90SBenjamin Bouvier dynamic_stack_slots: DynamicStackSlots::new(), 4408a9b1a90SBenjamin Bouvier global_values: PrimaryMap::new(), 4418a9b1a90SBenjamin Bouvier tables: PrimaryMap::new(), 4428a9b1a90SBenjamin Bouvier jump_tables: PrimaryMap::new(), 4438a9b1a90SBenjamin Bouvier dfg: DataFlowGraph::new(), 4448a9b1a90SBenjamin Bouvier layout: Layout::new(), 4458a9b1a90SBenjamin Bouvier srclocs: SecondaryMap::new(), 4468a9b1a90SBenjamin Bouvier stack_limit: None, 4478a9b1a90SBenjamin Bouvier }, 4488a9b1a90SBenjamin Bouvier params: FunctionParameters::new(), 4498a9b1a90SBenjamin Bouvier } 4508a9b1a90SBenjamin Bouvier } 4518a9b1a90SBenjamin Bouvier 4528a9b1a90SBenjamin Bouvier /// Clear all data structures in this function. 4538a9b1a90SBenjamin Bouvier pub fn clear(&mut self) { 4548a9b1a90SBenjamin Bouvier self.stencil.clear(); 4558a9b1a90SBenjamin Bouvier self.params.clear(); 4568a9b1a90SBenjamin Bouvier self.name = UserFuncName::default(); 4578a9b1a90SBenjamin Bouvier } 4588a9b1a90SBenjamin Bouvier 4598a9b1a90SBenjamin Bouvier /// Create a new empty, anonymous function with a Fast calling convention. 4608a9b1a90SBenjamin Bouvier pub fn new() -> Self { 4618a9b1a90SBenjamin Bouvier Self::with_name_signature(Default::default(), Signature::new(CallConv::Fast)) 4628a9b1a90SBenjamin Bouvier } 4638a9b1a90SBenjamin Bouvier 4648a9b1a90SBenjamin Bouvier /// Return an object that can display this function with correct ISA-specific annotations. 4658a9b1a90SBenjamin Bouvier pub fn display(&self) -> DisplayFunction<'_> { 4668a9b1a90SBenjamin Bouvier DisplayFunction(self, Default::default()) 4678a9b1a90SBenjamin Bouvier } 4688a9b1a90SBenjamin Bouvier 4698a9b1a90SBenjamin Bouvier /// Return an object that can display this function with correct ISA-specific annotations. 4708a9b1a90SBenjamin Bouvier pub fn display_with<'a>( 4718a9b1a90SBenjamin Bouvier &'a self, 4728a9b1a90SBenjamin Bouvier annotations: DisplayFunctionAnnotations<'a>, 4738a9b1a90SBenjamin Bouvier ) -> DisplayFunction<'a> { 4748a9b1a90SBenjamin Bouvier DisplayFunction(self, annotations) 4758a9b1a90SBenjamin Bouvier } 4768a9b1a90SBenjamin Bouvier 4778a9b1a90SBenjamin Bouvier /// Sets an absolute source location for the given instruction. 4788a9b1a90SBenjamin Bouvier /// 4798a9b1a90SBenjamin Bouvier /// If no base source location has been set yet, records it at the same time. 4808a9b1a90SBenjamin Bouvier pub fn set_srcloc(&mut self, inst: Inst, srcloc: SourceLoc) { 4818a9b1a90SBenjamin Bouvier let base = self.params.ensure_base_srcloc(srcloc); 4828a9b1a90SBenjamin Bouvier self.stencil.srclocs[inst] = RelSourceLoc::from_base_offset(base, srcloc); 4838a9b1a90SBenjamin Bouvier } 4848a9b1a90SBenjamin Bouvier 4858a9b1a90SBenjamin Bouvier /// Returns an absolute source location for the given instruction. 4868a9b1a90SBenjamin Bouvier pub fn srcloc(&self, inst: Inst) -> SourceLoc { 4878a9b1a90SBenjamin Bouvier let base = self.params.base_srcloc(); 4888a9b1a90SBenjamin Bouvier self.stencil.srclocs[inst].expand(base) 4898a9b1a90SBenjamin Bouvier } 4908a9b1a90SBenjamin Bouvier 4918a9b1a90SBenjamin Bouvier /// Declare a user-defined external function import, to be referenced in `ExtFuncData::User` later. 4928a9b1a90SBenjamin Bouvier pub fn declare_imported_user_function( 4938a9b1a90SBenjamin Bouvier &mut self, 4948a9b1a90SBenjamin Bouvier name: UserExternalName, 4958a9b1a90SBenjamin Bouvier ) -> UserExternalNameRef { 4968a9b1a90SBenjamin Bouvier self.params.ensure_user_func_name(name) 4978a9b1a90SBenjamin Bouvier } 4988a9b1a90SBenjamin Bouvier 4998a9b1a90SBenjamin Bouvier /// Declare an external function import. 5008a9b1a90SBenjamin Bouvier pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef { 5018a9b1a90SBenjamin Bouvier self.stencil.dfg.ext_funcs.push(data) 5028a9b1a90SBenjamin Bouvier } 5038f95c517SYury Delendik } 5048f95c517SYury Delendik 5058f95c517SYury Delendik /// Additional annotations for function display. 506f856b124SMark McCaskey #[derive(Default)] 5078f95c517SYury Delendik pub struct DisplayFunctionAnnotations<'a> { 5088f95c517SYury Delendik /// Enable value labels annotations. 5098f95c517SYury Delendik pub value_ranges: Option<&'a ValueLabelsRanges>, 5108f95c517SYury Delendik } 5118f95c517SYury Delendik 512747ad3c4Slazypassion /// Wrapper type capable of displaying a `Function` with correct ISA annotations. 5138f95c517SYury Delendik pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>); 514747ad3c4Slazypassion 515747ad3c4Slazypassion impl<'a> fmt::Display for DisplayFunction<'a> { 516747ad3c4Slazypassion fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 51743a86f14SBenjamin Bouvier write_function(fmt, self.0) 518747ad3c4Slazypassion } 519747ad3c4Slazypassion } 520747ad3c4Slazypassion 521747ad3c4Slazypassion impl fmt::Display for Function { 522747ad3c4Slazypassion fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 52343a86f14SBenjamin Bouvier write_function(fmt, self) 524747ad3c4Slazypassion } 525747ad3c4Slazypassion } 526747ad3c4Slazypassion 527747ad3c4Slazypassion impl fmt::Debug for Function { 528747ad3c4Slazypassion fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 52943a86f14SBenjamin Bouvier write_function(fmt, self) 530747ad3c4Slazypassion } 531747ad3c4Slazypassion } 532