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::binemit::CodeOffset; 7747ad3c4Slazypassion use crate::entity::{PrimaryMap, SecondaryMap}; 8747ad3c4Slazypassion use crate::ir; 9747ad3c4Slazypassion use crate::ir::{ 10832666c4SRyan Hunt Block, ExtFuncData, FuncRef, GlobalValue, GlobalValueData, Heap, HeapData, Inst, JumpTable, 11c360007bSRyan Hunt JumpTableData, Opcode, SigRef, StackSlot, StackSlotData, Table, TableData, 12747ad3c4Slazypassion }; 13*f7e9f86bSPeter Huene use crate::ir::{BlockOffsets, InstEncodings, SourceLocs, StackSlots, ValueLocations}; 14832666c4SRyan Hunt use crate::ir::{DataFlowGraph, ExternalName, Layout, Signature}; 15747ad3c4Slazypassion use crate::ir::{JumpTableOffsets, JumpTables}; 16747ad3c4Slazypassion use crate::isa::{CallConv, EncInfo, Encoding, Legalize, TargetIsa}; 17bb87f1a5SNicolas B. Pierron use crate::regalloc::{EntryRegDiversions, RegDiversions}; 188f95c517SYury Delendik use crate::value_label::ValueLabelsRanges; 19747ad3c4Slazypassion use crate::write::write_function; 20*f7e9f86bSPeter Huene use alloc::vec::Vec; 21747ad3c4Slazypassion use core::fmt; 22747ad3c4Slazypassion 23747ad3c4Slazypassion /// A function. 24747ad3c4Slazypassion /// 25747ad3c4Slazypassion /// Functions can be cloned, but it is not a very fast operation. 26747ad3c4Slazypassion /// The clone will have all the same entity numbers as the original. 27747ad3c4Slazypassion #[derive(Clone)] 28747ad3c4Slazypassion pub struct Function { 29747ad3c4Slazypassion /// Name of this function. Mostly used by `.clif` files. 30747ad3c4Slazypassion pub name: ExternalName, 31747ad3c4Slazypassion 32747ad3c4Slazypassion /// Signature of this function. 33747ad3c4Slazypassion pub signature: Signature, 34747ad3c4Slazypassion 35a4948340SNick Fitzgerald /// The old signature of this function, before the most recent legalization, 36a4948340SNick Fitzgerald /// if any. 37a4948340SNick Fitzgerald pub old_signature: Option<Signature>, 38a4948340SNick Fitzgerald 39747ad3c4Slazypassion /// Stack slots allocated in this function. 40747ad3c4Slazypassion pub stack_slots: StackSlots, 41747ad3c4Slazypassion 42747ad3c4Slazypassion /// Global values referenced. 43747ad3c4Slazypassion pub global_values: PrimaryMap<ir::GlobalValue, ir::GlobalValueData>, 44747ad3c4Slazypassion 45747ad3c4Slazypassion /// Heaps referenced. 46747ad3c4Slazypassion pub heaps: PrimaryMap<ir::Heap, ir::HeapData>, 47747ad3c4Slazypassion 48747ad3c4Slazypassion /// Tables referenced. 49747ad3c4Slazypassion pub tables: PrimaryMap<ir::Table, ir::TableData>, 50747ad3c4Slazypassion 51747ad3c4Slazypassion /// Jump tables used in this function. 52747ad3c4Slazypassion pub jump_tables: JumpTables, 53747ad3c4Slazypassion 54832666c4SRyan Hunt /// Data flow graph containing the primary definition of all instructions, blocks and values. 55747ad3c4Slazypassion pub dfg: DataFlowGraph, 56747ad3c4Slazypassion 57832666c4SRyan Hunt /// Layout of blocks and instructions in the function body. 58747ad3c4Slazypassion pub layout: Layout, 59747ad3c4Slazypassion 60747ad3c4Slazypassion /// Encoding recipe and bits for the legal instructions. 61747ad3c4Slazypassion /// Illegal instructions have the `Encoding::default()` value. 62747ad3c4Slazypassion pub encodings: InstEncodings, 63747ad3c4Slazypassion 64747ad3c4Slazypassion /// Location assigned to every value. 65747ad3c4Slazypassion pub locations: ValueLocations, 66747ad3c4Slazypassion 67bb87f1a5SNicolas B. Pierron /// Non-default locations assigned to value at the entry of basic blocks. 68bb87f1a5SNicolas B. Pierron /// 69bb87f1a5SNicolas B. Pierron /// At the entry of each basic block, we might have values which are not in their default 70bb87f1a5SNicolas B. Pierron /// ValueLocation. This field records these register-to-register moves as Diversions. 71bb87f1a5SNicolas B. Pierron pub entry_diversions: EntryRegDiversions, 72bb87f1a5SNicolas B. Pierron 73832666c4SRyan Hunt /// Code offsets of the block headers. 74747ad3c4Slazypassion /// 75747ad3c4Slazypassion /// This information is only transiently available after the `binemit::relax_branches` function 76747ad3c4Slazypassion /// computes it, and it can easily be recomputed by calling that function. It is not included 77747ad3c4Slazypassion /// in the textual IR format. 78832666c4SRyan Hunt pub offsets: BlockOffsets, 79747ad3c4Slazypassion 80747ad3c4Slazypassion /// Code offsets of Jump Table headers. 81747ad3c4Slazypassion pub jt_offsets: JumpTableOffsets, 82747ad3c4Slazypassion 83747ad3c4Slazypassion /// Source locations. 84747ad3c4Slazypassion /// 85747ad3c4Slazypassion /// Track the original source location for each instruction. The source locations are not 86747ad3c4Slazypassion /// interpreted by Cranelift, only preserved. 87747ad3c4Slazypassion pub srclocs: SourceLocs, 888923bac7SPeter Huene 898923bac7SPeter Huene /// Instruction that marks the end (inclusive) of the function's prologue. 908923bac7SPeter Huene /// 91*f7e9f86bSPeter Huene /// This is used for some ABIs to generate unwind information. 928923bac7SPeter Huene pub prologue_end: Option<Inst>, 93d804ab8bSiximeow 94*f7e9f86bSPeter Huene /// The instructions that mark the start (inclusive) of an epilogue in the function. 95d804ab8bSiximeow /// 96*f7e9f86bSPeter Huene /// This is used for some ABIs to generate unwind information. 97*f7e9f86bSPeter Huene pub epilogues_start: Vec<Inst>, 98747ad3c4Slazypassion } 99747ad3c4Slazypassion 100747ad3c4Slazypassion impl Function { 101747ad3c4Slazypassion /// Create a function with the given name and signature. 102747ad3c4Slazypassion pub fn with_name_signature(name: ExternalName, sig: Signature) -> Self { 103747ad3c4Slazypassion Self { 104747ad3c4Slazypassion name, 105747ad3c4Slazypassion signature: sig, 106a4948340SNick Fitzgerald old_signature: None, 107747ad3c4Slazypassion stack_slots: StackSlots::new(), 108747ad3c4Slazypassion global_values: PrimaryMap::new(), 109747ad3c4Slazypassion heaps: PrimaryMap::new(), 110747ad3c4Slazypassion tables: PrimaryMap::new(), 111747ad3c4Slazypassion jump_tables: PrimaryMap::new(), 112747ad3c4Slazypassion dfg: DataFlowGraph::new(), 113747ad3c4Slazypassion layout: Layout::new(), 114747ad3c4Slazypassion encodings: SecondaryMap::new(), 115747ad3c4Slazypassion locations: SecondaryMap::new(), 116bb87f1a5SNicolas B. Pierron entry_diversions: EntryRegDiversions::new(), 117747ad3c4Slazypassion offsets: SecondaryMap::new(), 118747ad3c4Slazypassion jt_offsets: SecondaryMap::new(), 119747ad3c4Slazypassion srclocs: SecondaryMap::new(), 1208923bac7SPeter Huene prologue_end: None, 121*f7e9f86bSPeter Huene epilogues_start: Vec::new(), 122747ad3c4Slazypassion } 123747ad3c4Slazypassion } 124747ad3c4Slazypassion 125747ad3c4Slazypassion /// Clear all data structures in this function. 126747ad3c4Slazypassion pub fn clear(&mut self) { 127747ad3c4Slazypassion self.signature.clear(CallConv::Fast); 128747ad3c4Slazypassion self.stack_slots.clear(); 129747ad3c4Slazypassion self.global_values.clear(); 130747ad3c4Slazypassion self.heaps.clear(); 131747ad3c4Slazypassion self.tables.clear(); 132747ad3c4Slazypassion self.jump_tables.clear(); 133747ad3c4Slazypassion self.dfg.clear(); 134747ad3c4Slazypassion self.layout.clear(); 135747ad3c4Slazypassion self.encodings.clear(); 136747ad3c4Slazypassion self.locations.clear(); 137bb87f1a5SNicolas B. Pierron self.entry_diversions.clear(); 138747ad3c4Slazypassion self.offsets.clear(); 139ea9ee202SAndrew Brown self.jt_offsets.clear(); 140747ad3c4Slazypassion self.srclocs.clear(); 1418923bac7SPeter Huene self.prologue_end = None; 142*f7e9f86bSPeter Huene self.epilogues_start.clear(); 143747ad3c4Slazypassion } 144747ad3c4Slazypassion 145747ad3c4Slazypassion /// Create a new empty, anonymous function with a Fast calling convention. 146747ad3c4Slazypassion pub fn new() -> Self { 147747ad3c4Slazypassion Self::with_name_signature(ExternalName::default(), Signature::new(CallConv::Fast)) 148747ad3c4Slazypassion } 149747ad3c4Slazypassion 150747ad3c4Slazypassion /// Creates a jump table in the function, to be used by `br_table` instructions. 151747ad3c4Slazypassion pub fn create_jump_table(&mut self, data: JumpTableData) -> JumpTable { 152747ad3c4Slazypassion self.jump_tables.push(data) 153747ad3c4Slazypassion } 154747ad3c4Slazypassion 155747ad3c4Slazypassion /// Creates a stack slot in the function, to be used by `stack_load`, `stack_store` and 156747ad3c4Slazypassion /// `stack_addr` instructions. 157747ad3c4Slazypassion pub fn create_stack_slot(&mut self, data: StackSlotData) -> StackSlot { 158747ad3c4Slazypassion self.stack_slots.push(data) 159747ad3c4Slazypassion } 160747ad3c4Slazypassion 161747ad3c4Slazypassion /// Adds a signature which can later be used to declare an external function import. 162747ad3c4Slazypassion pub fn import_signature(&mut self, signature: Signature) -> SigRef { 163747ad3c4Slazypassion self.dfg.signatures.push(signature) 164747ad3c4Slazypassion } 165747ad3c4Slazypassion 166747ad3c4Slazypassion /// Declare an external function import. 167747ad3c4Slazypassion pub fn import_function(&mut self, data: ExtFuncData) -> FuncRef { 168747ad3c4Slazypassion self.dfg.ext_funcs.push(data) 169747ad3c4Slazypassion } 170747ad3c4Slazypassion 171747ad3c4Slazypassion /// Declares a global value accessible to the function. 172747ad3c4Slazypassion pub fn create_global_value(&mut self, data: GlobalValueData) -> GlobalValue { 173747ad3c4Slazypassion self.global_values.push(data) 174747ad3c4Slazypassion } 175747ad3c4Slazypassion 176747ad3c4Slazypassion /// Declares a heap accessible to the function. 177747ad3c4Slazypassion pub fn create_heap(&mut self, data: HeapData) -> Heap { 178747ad3c4Slazypassion self.heaps.push(data) 179747ad3c4Slazypassion } 180747ad3c4Slazypassion 181747ad3c4Slazypassion /// Declares a table accessible to the function. 182747ad3c4Slazypassion pub fn create_table(&mut self, data: TableData) -> Table { 183747ad3c4Slazypassion self.tables.push(data) 184747ad3c4Slazypassion } 185747ad3c4Slazypassion 186747ad3c4Slazypassion /// Return an object that can display this function with correct ISA-specific annotations. 187d7d48d5cSBenjamin Bouvier pub fn display<'a, I: Into<Option<&'a dyn TargetIsa>>>( 188d7d48d5cSBenjamin Bouvier &'a self, 189d7d48d5cSBenjamin Bouvier isa: I, 190d7d48d5cSBenjamin Bouvier ) -> DisplayFunction<'a> { 1918f95c517SYury Delendik DisplayFunction(self, isa.into().into()) 1928f95c517SYury Delendik } 1938f95c517SYury Delendik 1948f95c517SYury Delendik /// Return an object that can display this function with correct ISA-specific annotations. 1958f95c517SYury Delendik pub fn display_with<'a>( 1968f95c517SYury Delendik &'a self, 1978f95c517SYury Delendik annotations: DisplayFunctionAnnotations<'a>, 1988f95c517SYury Delendik ) -> DisplayFunction<'a> { 1998f95c517SYury Delendik DisplayFunction(self, annotations) 200747ad3c4Slazypassion } 201747ad3c4Slazypassion 202747ad3c4Slazypassion /// Find a presumed unique special-purpose function parameter value. 203747ad3c4Slazypassion /// 204747ad3c4Slazypassion /// Returns the value of the last `purpose` parameter, or `None` if no such parameter exists. 205747ad3c4Slazypassion pub fn special_param(&self, purpose: ir::ArgumentPurpose) -> Option<ir::Value> { 206747ad3c4Slazypassion let entry = self.layout.entry_block().expect("Function is empty"); 207747ad3c4Slazypassion self.signature 208747ad3c4Slazypassion .special_param_index(purpose) 209832666c4SRyan Hunt .map(|i| self.dfg.block_params(entry)[i]) 210747ad3c4Slazypassion } 211747ad3c4Slazypassion 212832666c4SRyan Hunt /// Get an iterator over the instructions in `block`, including offsets and encoded instruction 213747ad3c4Slazypassion /// sizes. 214747ad3c4Slazypassion /// 215747ad3c4Slazypassion /// The iterator returns `(offset, inst, size)` tuples, where `offset` if the offset in bytes 216747ad3c4Slazypassion /// from the beginning of the function to the instruction, and `size` is the size of the 217747ad3c4Slazypassion /// instruction in bytes, or 0 for unencoded instructions. 218747ad3c4Slazypassion /// 219747ad3c4Slazypassion /// This function can only be used after the code layout has been computed by the 220747ad3c4Slazypassion /// `binemit::relax_branches()` function. 221832666c4SRyan Hunt pub fn inst_offsets<'a>(&'a self, block: Block, encinfo: &EncInfo) -> InstOffsetIter<'a> { 222747ad3c4Slazypassion assert!( 223747ad3c4Slazypassion !self.offsets.is_empty(), 224747ad3c4Slazypassion "Code layout must be computed first" 225747ad3c4Slazypassion ); 226bb87f1a5SNicolas B. Pierron let mut divert = RegDiversions::new(); 227832666c4SRyan Hunt divert.at_block(&self.entry_diversions, block); 228747ad3c4Slazypassion InstOffsetIter { 229747ad3c4Slazypassion encinfo: encinfo.clone(), 230747ad3c4Slazypassion func: self, 231bb87f1a5SNicolas B. Pierron divert, 232747ad3c4Slazypassion encodings: &self.encodings, 233832666c4SRyan Hunt offset: self.offsets[block], 234832666c4SRyan Hunt iter: self.layout.block_insts(block), 235747ad3c4Slazypassion } 236747ad3c4Slazypassion } 237747ad3c4Slazypassion 238747ad3c4Slazypassion /// Wrapper around `encode` which assigns `inst` the resulting encoding. 239d7d48d5cSBenjamin Bouvier pub fn update_encoding(&mut self, inst: ir::Inst, isa: &dyn TargetIsa) -> Result<(), Legalize> { 24060990aeaSChris Fallin if isa.get_mach_backend().is_some() { 24160990aeaSChris Fallin Ok(()) 24260990aeaSChris Fallin } else { 243747ad3c4Slazypassion self.encode(inst, isa).map(|e| self.encodings[inst] = e) 244747ad3c4Slazypassion } 24560990aeaSChris Fallin } 246747ad3c4Slazypassion 247747ad3c4Slazypassion /// Wrapper around `TargetIsa::encode` for encoding an existing instruction 248747ad3c4Slazypassion /// in the `Function`. 249d7d48d5cSBenjamin Bouvier pub fn encode(&self, inst: ir::Inst, isa: &dyn TargetIsa) -> Result<Encoding, Legalize> { 25060990aeaSChris Fallin if isa.get_mach_backend().is_some() { 25160990aeaSChris Fallin Ok(Encoding::new(0, 0)) 25260990aeaSChris Fallin } else { 253747ad3c4Slazypassion isa.encode(&self, &self.dfg[inst], self.dfg.ctrl_typevar(inst)) 254747ad3c4Slazypassion } 25560990aeaSChris Fallin } 2568f95c517SYury Delendik 2578f95c517SYury Delendik /// Starts collection of debug information. 2588f95c517SYury Delendik pub fn collect_debug_info(&mut self) { 2598f95c517SYury Delendik self.dfg.collect_debug_info(); 2608f95c517SYury Delendik } 2618efaeec5SSean Stangl 262c7b4b98cSSean Stangl /// Changes the destination of a jump or branch instruction. 263c7b4b98cSSean Stangl /// Does nothing if called with a non-jump or non-branch instruction. 264832666c4SRyan Hunt pub fn change_branch_destination(&mut self, inst: Inst, new_dest: Block) { 265c7b4b98cSSean Stangl match self.dfg[inst].branch_destination_mut() { 266c7b4b98cSSean Stangl None => (), 267c7b4b98cSSean Stangl Some(inst_dest) => *inst_dest = new_dest, 268c7b4b98cSSean Stangl } 269c7b4b98cSSean Stangl } 270c7b4b98cSSean Stangl 271832666c4SRyan Hunt /// Checks that the specified block can be encoded as a basic block. 2728efaeec5SSean Stangl /// 2738efaeec5SSean Stangl /// On error, returns the first invalid instruction and an error message. 274832666c4SRyan Hunt pub fn is_block_basic(&self, block: Block) -> Result<(), (Inst, &'static str)> { 2758efaeec5SSean Stangl let dfg = &self.dfg; 276832666c4SRyan Hunt let inst_iter = self.layout.block_insts(block); 2778efaeec5SSean Stangl 2788efaeec5SSean Stangl // Ignore all instructions prior to the first branch. 2798efaeec5SSean Stangl let mut inst_iter = inst_iter.skip_while(|&inst| !dfg[inst].opcode().is_branch()); 2808efaeec5SSean Stangl 2818efaeec5SSean Stangl // A conditional branch is permitted in a basic block only when followed 2828efaeec5SSean Stangl // by a terminal jump or fallthrough instruction. 2838efaeec5SSean Stangl if let Some(_branch) = inst_iter.next() { 2848efaeec5SSean Stangl if let Some(next) = inst_iter.next() { 2858efaeec5SSean Stangl match dfg[next].opcode() { 2868efaeec5SSean Stangl Opcode::Fallthrough | Opcode::Jump => (), 2878efaeec5SSean Stangl _ => return Err((next, "post-branch instruction not fallthrough or jump")), 2888efaeec5SSean Stangl } 2898efaeec5SSean Stangl } 2908efaeec5SSean Stangl } 2918efaeec5SSean Stangl 2928efaeec5SSean Stangl Ok(()) 2938efaeec5SSean Stangl } 294143cb014SBenjamin Bouvier 295143cb014SBenjamin Bouvier /// Returns true if the function is function that doesn't call any other functions. This is not 296143cb014SBenjamin Bouvier /// to be confused with a "leaf function" in Windows terminology. 297143cb014SBenjamin Bouvier pub fn is_leaf(&self) -> bool { 298143cb014SBenjamin Bouvier // Conservative result: if there's at least one function signature referenced in this 29958e5a62cSY-Nak // function, assume it is not a leaf. 30058e5a62cSY-Nak self.dfg.signatures.is_empty() 301143cb014SBenjamin Bouvier } 3028f95c517SYury Delendik } 3038f95c517SYury Delendik 3048f95c517SYury Delendik /// Additional annotations for function display. 305f856b124SMark McCaskey #[derive(Default)] 3068f95c517SYury Delendik pub struct DisplayFunctionAnnotations<'a> { 3078f95c517SYury Delendik /// Enable ISA annotations. 308d7d48d5cSBenjamin Bouvier pub isa: Option<&'a dyn TargetIsa>, 3098f95c517SYury Delendik 3108f95c517SYury Delendik /// Enable value labels annotations. 3118f95c517SYury Delendik pub value_ranges: Option<&'a ValueLabelsRanges>, 3128f95c517SYury Delendik } 3138f95c517SYury Delendik 314d7d48d5cSBenjamin Bouvier impl<'a> From<Option<&'a dyn TargetIsa>> for DisplayFunctionAnnotations<'a> { 315d7d48d5cSBenjamin Bouvier fn from(isa: Option<&'a dyn TargetIsa>) -> DisplayFunctionAnnotations { 3168f95c517SYury Delendik DisplayFunctionAnnotations { 3178f95c517SYury Delendik isa, 3188f95c517SYury Delendik value_ranges: None, 3198f95c517SYury Delendik } 3208f95c517SYury Delendik } 321747ad3c4Slazypassion } 322747ad3c4Slazypassion 323747ad3c4Slazypassion /// Wrapper type capable of displaying a `Function` with correct ISA annotations. 3248f95c517SYury Delendik pub struct DisplayFunction<'a>(&'a Function, DisplayFunctionAnnotations<'a>); 325747ad3c4Slazypassion 326747ad3c4Slazypassion impl<'a> fmt::Display for DisplayFunction<'a> { 327747ad3c4Slazypassion fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 3288f95c517SYury Delendik write_function(fmt, self.0, &self.1) 329747ad3c4Slazypassion } 330747ad3c4Slazypassion } 331747ad3c4Slazypassion 332747ad3c4Slazypassion impl fmt::Display for Function { 333747ad3c4Slazypassion fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 3348f95c517SYury Delendik write_function(fmt, self, &DisplayFunctionAnnotations::default()) 335747ad3c4Slazypassion } 336747ad3c4Slazypassion } 337747ad3c4Slazypassion 338747ad3c4Slazypassion impl fmt::Debug for Function { 339747ad3c4Slazypassion fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { 3408f95c517SYury Delendik write_function(fmt, self, &DisplayFunctionAnnotations::default()) 341747ad3c4Slazypassion } 342747ad3c4Slazypassion } 343747ad3c4Slazypassion 344747ad3c4Slazypassion /// Iterator returning instruction offsets and sizes: `(offset, inst, size)`. 345747ad3c4Slazypassion pub struct InstOffsetIter<'a> { 346747ad3c4Slazypassion encinfo: EncInfo, 347747ad3c4Slazypassion divert: RegDiversions, 348747ad3c4Slazypassion func: &'a Function, 349747ad3c4Slazypassion encodings: &'a InstEncodings, 350747ad3c4Slazypassion offset: CodeOffset, 351747ad3c4Slazypassion iter: ir::layout::Insts<'a>, 352747ad3c4Slazypassion } 353747ad3c4Slazypassion 354747ad3c4Slazypassion impl<'a> Iterator for InstOffsetIter<'a> { 355747ad3c4Slazypassion type Item = (CodeOffset, ir::Inst, CodeOffset); 356747ad3c4Slazypassion 357747ad3c4Slazypassion fn next(&mut self) -> Option<Self::Item> { 358747ad3c4Slazypassion self.iter.next().map(|inst| { 359747ad3c4Slazypassion self.divert.apply(&self.func.dfg[inst]); 360747ad3c4Slazypassion let byte_size = 361747ad3c4Slazypassion self.encinfo 362747ad3c4Slazypassion .byte_size(self.encodings[inst], inst, &self.divert, self.func); 363747ad3c4Slazypassion let offset = self.offset; 364747ad3c4Slazypassion self.offset += byte_size; 365747ad3c4Slazypassion (offset, inst, byte_size) 366747ad3c4Slazypassion }) 367747ad3c4Slazypassion } 368747ad3c4Slazypassion } 369