1 //! Cranelift compilation context and main entry point. 2 //! 3 //! When compiling many small functions, it is important to avoid repeatedly allocating and 4 //! deallocating the data structures needed for compilation. The `Context` struct is used to hold 5 //! on to memory allocations between function compilations. 6 //! 7 //! The context does not hold a `TargetIsa` instance which has to be provided as an argument 8 //! instead. This is because an ISA instance is immutable and can be used by multiple compilation 9 //! contexts concurrently. Typically, you would have one context per compilation thread and only a 10 //! single ISA instance. 11 12 use crate::binemit::{ 13 relax_branches, shrink_instructions, CodeInfo, MemoryCodeSink, RelocSink, StackMapSink, 14 TrapSink, 15 }; 16 use crate::dce::do_dce; 17 use crate::dominator_tree::DominatorTree; 18 use crate::flowgraph::ControlFlowGraph; 19 use crate::ir::Function; 20 use crate::isa::TargetIsa; 21 use crate::legalize_function; 22 use crate::legalizer::simple_legalize; 23 use crate::licm::do_licm; 24 use crate::loop_analysis::LoopAnalysis; 25 use crate::machinst::{MachCompileResult, MachStackMap}; 26 use crate::nan_canonicalization::do_nan_canonicalization; 27 use crate::postopt::do_postopt; 28 use crate::redundant_reload_remover::RedundantReloadRemover; 29 use crate::regalloc; 30 use crate::remove_constant_phis::do_remove_constant_phis; 31 use crate::result::CodegenResult; 32 use crate::settings::{FlagsOrIsa, OptLevel}; 33 use crate::simple_gvn::do_simple_gvn; 34 use crate::simple_preopt::do_preopt; 35 use crate::timing; 36 use crate::unreachable_code::eliminate_unreachable_code; 37 use crate::value_label::{build_value_labels_ranges, ComparableSourceLoc, ValueLabelsRanges}; 38 use crate::verifier::{verify_context, verify_locations, VerifierErrors, VerifierResult}; 39 #[cfg(feature = "souper-harvest")] 40 use alloc::string::String; 41 use alloc::vec::Vec; 42 use log::debug; 43 44 #[cfg(feature = "souper-harvest")] 45 use crate::souper_harvest::do_souper_harvest; 46 47 /// Persistent data structures and compilation pipeline. 48 pub struct Context { 49 /// The function we're compiling. 50 pub func: Function, 51 52 /// The control flow graph of `func`. 53 pub cfg: ControlFlowGraph, 54 55 /// Dominator tree for `func`. 56 pub domtree: DominatorTree, 57 58 /// Register allocation context. 59 pub regalloc: regalloc::Context, 60 61 /// Loop analysis of `func`. 62 pub loop_analysis: LoopAnalysis, 63 64 /// Redundant-reload remover context. 65 pub redundant_reload_remover: RedundantReloadRemover, 66 67 /// Result of MachBackend compilation, if computed. 68 pub mach_compile_result: Option<MachCompileResult>, 69 70 /// Flag: do we want a disassembly with the MachCompileResult? 71 pub want_disasm: bool, 72 } 73 74 impl Context { 75 /// Allocate a new compilation context. 76 /// 77 /// The returned instance should be reused for compiling multiple functions in order to avoid 78 /// needless allocator thrashing. 79 pub fn new() -> Self { 80 Self::for_function(Function::new()) 81 } 82 83 /// Allocate a new compilation context with an existing Function. 84 /// 85 /// The returned instance should be reused for compiling multiple functions in order to avoid 86 /// needless allocator thrashing. 87 pub fn for_function(func: Function) -> Self { 88 Self { 89 func, 90 cfg: ControlFlowGraph::new(), 91 domtree: DominatorTree::new(), 92 regalloc: regalloc::Context::new(), 93 loop_analysis: LoopAnalysis::new(), 94 redundant_reload_remover: RedundantReloadRemover::new(), 95 mach_compile_result: None, 96 want_disasm: false, 97 } 98 } 99 100 /// Clear all data structures in this context. 101 pub fn clear(&mut self) { 102 self.func.clear(); 103 self.cfg.clear(); 104 self.domtree.clear(); 105 self.regalloc.clear(); 106 self.loop_analysis.clear(); 107 self.redundant_reload_remover.clear(); 108 self.mach_compile_result = None; 109 self.want_disasm = false; 110 } 111 112 /// Set the flag to request a disassembly when compiling with a 113 /// `MachBackend` backend. 114 pub fn set_disasm(&mut self, val: bool) { 115 self.want_disasm = val; 116 } 117 118 /// Compile the function, and emit machine code into a `Vec<u8>`. 119 /// 120 /// Run the function through all the passes necessary to generate code for the target ISA 121 /// represented by `isa`, as well as the final step of emitting machine code into a 122 /// `Vec<u8>`. The machine code is not relocated. Instead, any relocations are emitted 123 /// into `relocs`. 124 /// 125 /// This function calls `compile` and `emit_to_memory`, taking care to resize `mem` as 126 /// needed, so it provides a safe interface. 127 /// 128 /// Returns information about the function's code and read-only data. 129 pub fn compile_and_emit( 130 &mut self, 131 isa: &dyn TargetIsa, 132 mem: &mut Vec<u8>, 133 relocs: &mut dyn RelocSink, 134 traps: &mut dyn TrapSink, 135 stack_maps: &mut dyn StackMapSink, 136 ) -> CodegenResult<CodeInfo> { 137 let info = self.compile(isa)?; 138 let old_len = mem.len(); 139 mem.resize(old_len + info.total_size as usize, 0); 140 let new_info = unsafe { 141 self.emit_to_memory( 142 isa, 143 mem.as_mut_ptr().add(old_len), 144 relocs, 145 traps, 146 stack_maps, 147 ) 148 }; 149 debug_assert!(new_info == info); 150 Ok(info) 151 } 152 153 /// Compile the function. 154 /// 155 /// Run the function through all the passes necessary to generate code for the target ISA 156 /// represented by `isa`. This does not include the final step of emitting machine code into a 157 /// code sink. 158 /// 159 /// Returns information about the function's code and read-only data. 160 pub fn compile(&mut self, isa: &dyn TargetIsa) -> CodegenResult<CodeInfo> { 161 let _tt = timing::compile(); 162 self.verify_if(isa)?; 163 164 let opt_level = isa.flags().opt_level(); 165 debug!( 166 "Compiling (opt level {:?}):\n{}", 167 opt_level, 168 self.func.display(isa) 169 ); 170 171 self.compute_cfg(); 172 if opt_level != OptLevel::None { 173 self.preopt(isa)?; 174 } 175 if isa.flags().enable_nan_canonicalization() { 176 self.canonicalize_nans(isa)?; 177 } 178 179 self.legalize(isa)?; 180 if opt_level != OptLevel::None { 181 self.postopt(isa)?; 182 self.compute_domtree(); 183 self.compute_loop_analysis(); 184 self.licm(isa)?; 185 self.simple_gvn(isa)?; 186 } 187 188 self.compute_domtree(); 189 self.eliminate_unreachable_code(isa)?; 190 if opt_level != OptLevel::None { 191 self.dce(isa)?; 192 } 193 194 self.remove_constant_phis(isa)?; 195 196 if let Some(backend) = isa.get_mach_backend() { 197 let result = backend.compile_function(&self.func, self.want_disasm)?; 198 let info = result.code_info(); 199 self.mach_compile_result = Some(result); 200 Ok(info) 201 } else { 202 self.regalloc(isa)?; 203 self.prologue_epilogue(isa)?; 204 if opt_level == OptLevel::Speed || opt_level == OptLevel::SpeedAndSize { 205 self.redundant_reload_remover(isa)?; 206 } 207 if opt_level == OptLevel::SpeedAndSize { 208 self.shrink_instructions(isa)?; 209 } 210 let result = self.relax_branches(isa); 211 212 debug!("Compiled:\n{}", self.func.display(isa)); 213 result 214 } 215 } 216 217 /// Emit machine code directly into raw memory. 218 /// 219 /// Write all of the function's machine code to the memory at `mem`. The size of the machine 220 /// code is returned by `compile` above. 221 /// 222 /// The machine code is not relocated. Instead, any relocations are emitted into `relocs`. 223 /// 224 /// # Safety 225 /// 226 /// This function is unsafe since it does not perform bounds checking on the memory buffer, 227 /// and it can't guarantee that the `mem` pointer is valid. 228 /// 229 /// Returns information about the emitted code and data. 230 pub unsafe fn emit_to_memory( 231 &self, 232 isa: &dyn TargetIsa, 233 mem: *mut u8, 234 relocs: &mut dyn RelocSink, 235 traps: &mut dyn TrapSink, 236 stack_maps: &mut dyn StackMapSink, 237 ) -> CodeInfo { 238 let _tt = timing::binemit(); 239 let mut sink = MemoryCodeSink::new(mem, relocs, traps, stack_maps); 240 if let Some(ref result) = &self.mach_compile_result { 241 result.buffer.emit(&mut sink); 242 let info = sink.info; 243 // New backends do not emit StackMaps through the `CodeSink` because its interface 244 // requires `Value`s; instead, the `StackMap` objects are directly accessible via 245 // `result.buffer.stack_maps()`. 246 for &MachStackMap { 247 offset_end, 248 ref stack_map, 249 .. 250 } in result.buffer.stack_maps() 251 { 252 stack_maps.add_stack_map(offset_end, stack_map.clone()); 253 } 254 info 255 } else { 256 isa.emit_function_to_memory(&self.func, &mut sink); 257 sink.info 258 } 259 } 260 261 /// Creates unwind information for the function. 262 /// 263 /// Returns `None` if the function has no unwind information. 264 #[cfg(feature = "unwind")] 265 pub fn create_unwind_info( 266 &self, 267 isa: &dyn TargetIsa, 268 ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> { 269 if let Some(backend) = isa.get_mach_backend() { 270 let unwind_info_kind = isa.unwind_info_kind(); 271 let result = self.mach_compile_result.as_ref().unwrap(); 272 return backend.emit_unwind_info(result, unwind_info_kind); 273 } 274 isa.create_unwind_info(&self.func) 275 } 276 277 /// Run the verifier on the function. 278 /// 279 /// Also check that the dominator tree and control flow graph are consistent with the function. 280 pub fn verify<'a, FOI: Into<FlagsOrIsa<'a>>>(&self, fisa: FOI) -> VerifierResult<()> { 281 let mut errors = VerifierErrors::default(); 282 let _ = verify_context(&self.func, &self.cfg, &self.domtree, fisa, &mut errors); 283 284 if errors.is_empty() { 285 Ok(()) 286 } else { 287 Err(errors) 288 } 289 } 290 291 /// Run the verifier only if the `enable_verifier` setting is true. 292 pub fn verify_if<'a, FOI: Into<FlagsOrIsa<'a>>>(&self, fisa: FOI) -> CodegenResult<()> { 293 let fisa = fisa.into(); 294 if fisa.flags.enable_verifier() { 295 self.verify(fisa)?; 296 } 297 Ok(()) 298 } 299 300 /// Run the locations verifier on the function. 301 pub fn verify_locations(&self, isa: &dyn TargetIsa) -> VerifierResult<()> { 302 let mut errors = VerifierErrors::default(); 303 let _ = verify_locations(isa, &self.func, &self.cfg, None, &mut errors); 304 305 if errors.is_empty() { 306 Ok(()) 307 } else { 308 Err(errors) 309 } 310 } 311 312 /// Run the locations verifier only if the `enable_verifier` setting is true. 313 pub fn verify_locations_if(&self, isa: &dyn TargetIsa) -> CodegenResult<()> { 314 if isa.flags().enable_verifier() { 315 self.verify_locations(isa)?; 316 } 317 Ok(()) 318 } 319 320 /// Perform dead-code elimination on the function. 321 pub fn dce<'a, FOI: Into<FlagsOrIsa<'a>>>(&mut self, fisa: FOI) -> CodegenResult<()> { 322 do_dce(&mut self.func, &mut self.domtree); 323 self.verify_if(fisa)?; 324 Ok(()) 325 } 326 327 /// Perform constant-phi removal on the function. 328 pub fn remove_constant_phis<'a, FOI: Into<FlagsOrIsa<'a>>>( 329 &mut self, 330 fisa: FOI, 331 ) -> CodegenResult<()> { 332 do_remove_constant_phis(&mut self.func, &mut self.domtree); 333 self.verify_if(fisa)?; 334 Ok(()) 335 } 336 337 /// Perform pre-legalization rewrites on the function. 338 pub fn preopt(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 339 do_preopt(&mut self.func, &mut self.cfg, isa); 340 self.verify_if(isa)?; 341 Ok(()) 342 } 343 344 /// Perform NaN canonicalizing rewrites on the function. 345 pub fn canonicalize_nans(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 346 do_nan_canonicalization(&mut self.func); 347 self.verify_if(isa) 348 } 349 350 /// Run the legalizer for `isa` on the function. 351 pub fn legalize(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 352 // Legalization invalidates the domtree and loop_analysis by mutating the CFG. 353 // TODO: Avoid doing this when legalization doesn't actually mutate the CFG. 354 self.domtree.clear(); 355 self.loop_analysis.clear(); 356 if isa.get_mach_backend().is_some() { 357 // Run some specific legalizations only. 358 simple_legalize(&mut self.func, &mut self.cfg, isa); 359 self.verify_if(isa) 360 } else { 361 legalize_function(&mut self.func, &mut self.cfg, isa); 362 debug!("Legalized:\n{}", self.func.display(isa)); 363 self.verify_if(isa) 364 } 365 } 366 367 /// Perform post-legalization rewrites on the function. 368 pub fn postopt(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 369 do_postopt(&mut self.func, isa); 370 self.verify_if(isa)?; 371 Ok(()) 372 } 373 374 /// Compute the control flow graph. 375 pub fn compute_cfg(&mut self) { 376 self.cfg.compute(&self.func) 377 } 378 379 /// Compute dominator tree. 380 pub fn compute_domtree(&mut self) { 381 self.domtree.compute(&self.func, &self.cfg) 382 } 383 384 /// Compute the loop analysis. 385 pub fn compute_loop_analysis(&mut self) { 386 self.loop_analysis 387 .compute(&self.func, &self.cfg, &self.domtree) 388 } 389 390 /// Compute the control flow graph and dominator tree. 391 pub fn flowgraph(&mut self) { 392 self.compute_cfg(); 393 self.compute_domtree() 394 } 395 396 /// Perform simple GVN on the function. 397 pub fn simple_gvn<'a, FOI: Into<FlagsOrIsa<'a>>>(&mut self, fisa: FOI) -> CodegenResult<()> { 398 do_simple_gvn(&mut self.func, &mut self.domtree); 399 self.verify_if(fisa) 400 } 401 402 /// Perform LICM on the function. 403 pub fn licm(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 404 do_licm( 405 isa, 406 &mut self.func, 407 &mut self.cfg, 408 &mut self.domtree, 409 &mut self.loop_analysis, 410 ); 411 self.verify_if(isa) 412 } 413 414 /// Perform unreachable code elimination. 415 pub fn eliminate_unreachable_code<'a, FOI>(&mut self, fisa: FOI) -> CodegenResult<()> 416 where 417 FOI: Into<FlagsOrIsa<'a>>, 418 { 419 eliminate_unreachable_code(&mut self.func, &mut self.cfg, &self.domtree); 420 self.verify_if(fisa) 421 } 422 423 /// Run the register allocator. 424 pub fn regalloc(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 425 self.regalloc 426 .run(isa, &mut self.func, &mut self.cfg, &mut self.domtree) 427 } 428 429 /// Insert prologue and epilogues after computing the stack frame layout. 430 pub fn prologue_epilogue(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 431 isa.prologue_epilogue(&mut self.func)?; 432 self.verify_if(isa)?; 433 self.verify_locations_if(isa)?; 434 Ok(()) 435 } 436 437 /// Do redundant-reload removal after allocation of both registers and stack slots. 438 pub fn redundant_reload_remover(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 439 self.redundant_reload_remover 440 .run(isa, &mut self.func, &self.cfg); 441 self.verify_if(isa)?; 442 Ok(()) 443 } 444 445 /// Run the instruction shrinking pass. 446 pub fn shrink_instructions(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> { 447 shrink_instructions(&mut self.func, isa); 448 self.verify_if(isa)?; 449 self.verify_locations_if(isa)?; 450 Ok(()) 451 } 452 453 /// Run the branch relaxation pass and return information about the function's code and 454 /// read-only data. 455 pub fn relax_branches(&mut self, isa: &dyn TargetIsa) -> CodegenResult<CodeInfo> { 456 let info = relax_branches(&mut self.func, &mut self.cfg, &mut self.domtree, isa)?; 457 self.verify_if(isa)?; 458 self.verify_locations_if(isa)?; 459 Ok(info) 460 } 461 462 /// Builds ranges and location for specified value labels. 463 pub fn build_value_labels_ranges( 464 &self, 465 isa: &dyn TargetIsa, 466 ) -> CodegenResult<ValueLabelsRanges> { 467 Ok(build_value_labels_ranges::<ComparableSourceLoc>( 468 &self.func, 469 &self.regalloc, 470 self.mach_compile_result.as_ref(), 471 isa, 472 )) 473 } 474 475 /// Harvest candidate left-hand sides for superoptimization with Souper. 476 #[cfg(feature = "souper-harvest")] 477 pub fn souper_harvest( 478 &mut self, 479 out: &mut std::sync::mpsc::Sender<String>, 480 ) -> CodegenResult<()> { 481 do_souper_harvest(&self.func, out); 482 Ok(()) 483 } 484 } 485