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