1use crate::alias_analysis::AliasAnalysis;
13use crate::dominator_tree::DominatorTree;
14use crate::egraph::EgraphPass;
15use crate::flowgraph::ControlFlowGraph;
16use crate::ir::Function;
17use crate::isa::TargetIsa;
18use crate::legalizer::simple_legalize;
19use crate::loop_analysis::LoopAnalysis;
20use crate::machinst::{CompiledCode, CompiledCodeStencil};
21use crate::nan_canonicalization::do_nan_canonicalization;
22use crate::remove_constant_phis::do_remove_constant_phis;
23use crate::result::{CodegenResult, CompileResult};
24use crate::settings::{FlagsOrIsa, OptLevel};
25use crate::trace;
26use crate::unreachable_code::eliminate_unreachable_code;
27use crate::verifier::{verify_context, VerifierErrors, VerifierResult};
28use crate::{timing, CompileError};
29#[cfg(feature = "souper-harvest")]
30use alloc::string::String;
31use alloc::vec::Vec;
32use cranelift_control::ControlPlane;
33use target_lexicon::Architecture;
34
35#[cfg(feature = "souper-harvest")]
36use crate::souper_harvest::do_souper_harvest;
37
38pub struct Context {
40 pub func: Function,
42
43 pub cfg: ControlFlowGraph,
45
46 pub domtree: DominatorTree,
48
49 pub loop_analysis: LoopAnalysis,
51
52 pub(crate) compiled_code: Option<CompiledCode>,
54
55 pub want_disasm: bool,
57}
58
59impl Context {
60 pub fn new() -> Self {
65 Self::for_function(Function::new())
66 }
67
68 pub fn for_function(func: Function) -> Self {
73 Self {
74 func,
75 cfg: ControlFlowGraph::new(),
76 domtree: DominatorTree::new(),
77 loop_analysis: LoopAnalysis::new(),
78 compiled_code: None,
79 want_disasm: false,
80 }
81 }
82
83 pub fn clear(&mut self) {
85 self.func.clear();
86 self.cfg.clear();
87 self.domtree.clear();
88 self.loop_analysis.clear();
89 self.compiled_code = None;
90 self.want_disasm = false;
91 }
92
93 pub fn compiled_code(&self) -> Option<&CompiledCode> {
96 self.compiled_code.as_ref()
97 }
98
99 pub fn take_compiled_code(&mut self) -> Option<CompiledCode> {
102 self.compiled_code.take()
103 }
104
105 pub fn set_disasm(&mut self, val: bool) {
108 self.want_disasm = val;
109 }
110
111 #[deprecated = "use Context::compile"]
113 pub fn compile_and_emit(
114 &mut self,
115 isa: &dyn TargetIsa,
116 mem: &mut Vec<u8>,
117 ctrl_plane: &mut ControlPlane,
118 ) -> CompileResult<&CompiledCode> {
119 let compiled_code = self.compile(isa, ctrl_plane)?;
120 mem.extend_from_slice(compiled_code.code_buffer());
121 Ok(compiled_code)
122 }
123
124 pub fn compile_stencil(
128 &mut self,
129 isa: &dyn TargetIsa,
130 ctrl_plane: &mut ControlPlane,
131 ) -> CodegenResult<CompiledCodeStencil> {
132 let _tt = timing::compile();
133
134 self.verify_if(isa)?;
135
136 self.optimize(isa, ctrl_plane)?;
137
138 isa.compile_function(&self.func, &self.domtree, self.want_disasm, ctrl_plane)
139 }
140
141 pub fn optimize(
147 &mut self,
148 isa: &dyn TargetIsa,
149 ctrl_plane: &mut ControlPlane,
150 ) -> CodegenResult<()> {
151 log::debug!(
152 "Number of CLIF instructions to optimize: {}",
153 self.func.dfg.num_insts()
154 );
155 log::debug!(
156 "Number of CLIF blocks to optimize: {}",
157 self.func.dfg.num_blocks()
158 );
159
160 let opt_level = isa.flags().opt_level();
161 crate::trace!(
162 "Optimizing (opt level {:?}):\n{}",
163 opt_level,
164 self.func.display()
165 );
166
167 self.compute_cfg();
168 if isa.flags().enable_nan_canonicalization() {
169 self.canonicalize_nans(isa)?;
170 }
171
172 self.legalize(isa)?;
173
174 self.compute_domtree();
175 self.eliminate_unreachable_code(isa)?;
176 self.remove_constant_phis(isa)?;
177
178 self.func.dfg.resolve_all_aliases();
179
180 if opt_level != OptLevel::None {
181 self.egraph_pass(isa, ctrl_plane)?;
182 }
183
184 Ok(())
185 }
186
187 pub fn compile(
200 &mut self,
201 isa: &dyn TargetIsa,
202 ctrl_plane: &mut ControlPlane,
203 ) -> CompileResult<&CompiledCode> {
204 let stencil = self
205 .compile_stencil(isa, ctrl_plane)
206 .map_err(|error| CompileError {
207 inner: error,
208 func: &self.func,
209 })?;
210 Ok(self
211 .compiled_code
212 .insert(stencil.apply_params(&self.func.params)))
213 }
214
215 #[deprecated = "use CompiledCode::get_code_bb_layout"]
219 pub fn get_code_bb_layout(&self) -> Option<(Vec<usize>, Vec<(usize, usize)>)> {
220 self.compiled_code().map(CompiledCode::get_code_bb_layout)
221 }
222
223 #[cfg(feature = "unwind")]
227 #[deprecated = "use CompiledCode::create_unwind_info"]
228 pub fn create_unwind_info(
229 &self,
230 isa: &dyn TargetIsa,
231 ) -> CodegenResult<Option<crate::isa::unwind::UnwindInfo>> {
232 self.compiled_code().unwrap().create_unwind_info(isa)
233 }
234
235 pub fn verify<'a, FOI: Into<FlagsOrIsa<'a>>>(&self, fisa: FOI) -> VerifierResult<()> {
241 let mut errors = VerifierErrors::default();
242 let _ = verify_context(&self.func, &self.cfg, &self.domtree, fisa, &mut errors);
243
244 if errors.is_empty() {
245 Ok(())
246 } else {
247 Err(errors)
248 }
249 }
250
251 pub fn verify_if<'a, FOI: Into<FlagsOrIsa<'a>>>(&self, fisa: FOI) -> CodegenResult<()> {
253 let fisa = fisa.into();
254 if fisa.flags.enable_verifier() {
255 self.verify(fisa)?;
256 }
257 Ok(())
258 }
259
260 pub fn remove_constant_phis<'a, FOI: Into<FlagsOrIsa<'a>>>(
262 &mut self,
263 fisa: FOI,
264 ) -> CodegenResult<()> {
265 do_remove_constant_phis(&mut self.func, &mut self.domtree);
266 self.verify_if(fisa)?;
267 Ok(())
268 }
269
270 pub fn canonicalize_nans(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> {
272 let has_vector_support = match isa.triple().architecture {
274 Architecture::Riscv64(_) => match isa.isa_flags().iter().find(|f| f.name == "has_v") {
275 Some(value) => value.as_bool().unwrap_or(false),
276 None => false,
277 },
278 _ => true,
279 };
280 do_nan_canonicalization(&mut self.func, has_vector_support);
281 self.verify_if(isa)
282 }
283
284 pub fn legalize(&mut self, isa: &dyn TargetIsa) -> CodegenResult<()> {
286 self.domtree.clear();
289 self.loop_analysis.clear();
290
291 simple_legalize(&mut self.func, isa);
293 self.verify_if(isa)
294 }
295
296 pub fn compute_cfg(&mut self) {
298 self.cfg.compute(&self.func)
299 }
300
301 pub fn compute_domtree(&mut self) {
303 self.domtree.compute(&self.func, &self.cfg)
304 }
305
306 pub fn compute_loop_analysis(&mut self) {
308 self.loop_analysis
309 .compute(&self.func, &self.cfg, &self.domtree)
310 }
311
312 pub fn flowgraph(&mut self) {
314 self.compute_cfg();
315 self.compute_domtree()
316 }
317
318 pub fn eliminate_unreachable_code<'a, FOI>(&mut self, fisa: FOI) -> CodegenResult<()>
320 where
321 FOI: Into<FlagsOrIsa<'a>>,
322 {
323 eliminate_unreachable_code(&mut self.func, &mut self.cfg, &self.domtree);
324 self.verify_if(fisa)
325 }
326
327 pub fn replace_redundant_loads(&mut self) -> CodegenResult<()> {
333 let mut analysis = AliasAnalysis::new(&self.func, &self.domtree);
334 analysis.compute_and_update_aliases(&mut self.func);
335 Ok(())
336 }
337
338 #[cfg(feature = "souper-harvest")]
340 pub fn souper_harvest(
341 &mut self,
342 out: &mut std::sync::mpsc::Sender<String>,
343 ) -> CodegenResult<()> {
344 do_souper_harvest(&self.func, out);
345 Ok(())
346 }
347
348 pub fn egraph_pass<'a, FOI>(
350 &mut self,
351 fisa: FOI,
352 ctrl_plane: &mut ControlPlane,
353 ) -> CodegenResult<()>
354 where
355 FOI: Into<FlagsOrIsa<'a>>,
356 {
357 let _tt = timing::egraph();
358
359 trace!(
360 "About to optimize with egraph phase:\n{}",
361 self.func.display()
362 );
363 let fisa = fisa.into();
364 self.compute_loop_analysis();
365 let mut alias_analysis = AliasAnalysis::new(&self.func, &self.domtree);
366 let mut pass = EgraphPass::new(
367 &mut self.func,
368 &self.domtree,
369 &self.loop_analysis,
370 &mut alias_analysis,
371 &fisa.flags,
372 ctrl_plane,
373 );
374 pass.run();
375 log::debug!("egraph stats: {:?}", pass.stats);
376 trace!("pinned_union_count: {}", pass.eclasses.pinned_union_count);
377 trace!("After egraph optimization:\n{}", self.func.display());
378
379 self.verify_if(fisa)
380 }
381}