1 //! Test command for testing the alias analysis pass.
2 //!
3 //! The `alias-analysis` test command runs each function through GVN
4 //! and then alias analysis after ensuring that all instructions are
5 //! legal for the target.
6 //!
7 //! The resulting function is sent to `filecheck`.
8 
9 use crate::subtest::{run_filecheck, Context, SubTest};
10 use cranelift_codegen;
11 use cranelift_codegen::ir::Function;
12 use cranelift_reader::TestCommand;
13 use std::borrow::Cow;
14 
15 struct TestAliasAnalysis;
16 
17 pub fn subtest(parsed: &TestCommand) -> anyhow::Result<Box<dyn SubTest>> {
18     assert_eq!(parsed.command, "alias-analysis");
19     if !parsed.options.is_empty() {
20         anyhow::bail!("No options allowed on {}", parsed);
21     }
22     Ok(Box::new(TestAliasAnalysis))
23 }
24 
25 impl SubTest for TestAliasAnalysis {
26     fn name(&self) -> &'static str {
27         "alias-analysis"
28     }
29 
30     fn is_mutating(&self) -> bool {
31         true
32     }
33 
34     fn run(&self, func: Cow<Function>, context: &Context) -> anyhow::Result<()> {
35         let mut comp_ctx = cranelift_codegen::Context::for_function(func.into_owned());
36 
37         comp_ctx.flowgraph();
38         comp_ctx
39             .simple_gvn(context.flags_or_isa())
40             .map_err(|e| crate::pretty_anyhow_error(&comp_ctx.func, Into::into(e)))?;
41         comp_ctx
42             .replace_redundant_loads()
43             .map_err(|e| crate::pretty_anyhow_error(&comp_ctx.func, Into::into(e)))?;
44 
45         let text = comp_ctx.func.display().to_string();
46         run_filecheck(&text, context)
47     }
48 }
49