From 38cdab01cbf86a7377b95c515ba3a200bae9d6bf Mon Sep 17 00:00:00 2001 From: Sebastien Bourdeauducq Date: Mon, 30 Mar 2020 16:58:44 +0800 Subject: [PATCH] run LLVM passes --- src/main.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 72b7cbe9..7f8ea412 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,6 +21,7 @@ use inkwell::types; use inkwell::types::BasicType; use inkwell::values; use inkwell::{IntPredicate, FloatPredicate}; +use inkwell::passes; #[derive(Debug)] @@ -71,6 +72,7 @@ type CompileResult = Result; struct CodeGen<'ctx> { context: &'ctx Context, module: Module<'ctx>, + pass_manager: passes::PassManager>, builder: Builder<'ctx>, current_source_location: ast::Location, namespace: HashMap>, @@ -78,9 +80,21 @@ struct CodeGen<'ctx> { impl<'ctx> CodeGen<'ctx> { fn new(context: &'ctx Context) -> CodeGen<'ctx> { + let module = context.create_module("kernel"); + + let pass_manager = passes::PassManager::create(&module); + pass_manager.add_instruction_combining_pass(); + pass_manager.add_reassociate_pass(); + pass_manager.add_gvn_pass(); + pass_manager.add_cfg_simplification_pass(); + pass_manager.add_basic_alias_analysis_pass(); + pass_manager.add_promote_memory_to_register_pass(); + pass_manager.add_instruction_combining_pass(); + pass_manager.add_reassociate_pass(); + pass_manager.initialize(); + CodeGen { - context, - module: context.create_module("kernel"), + context, module, pass_manager, builder: context.create_builder(), current_source_location: ast::Location::default(), namespace: HashMap::new(), @@ -117,7 +131,7 @@ impl<'ctx> CodeGen<'ctx> { decorator_list: &[ast::Expression], returns: &Option, is_async: bool, - ) -> CompileResult<()> { + ) -> CompileResult> { if is_async { return Err(self.compile_error(CompileErrorKind::Unsupported("async functions"))) } @@ -174,7 +188,7 @@ impl<'ctx> CodeGen<'ctx> { for statement in body.iter() { self.compile_statement(statement, return_type)?; } - Ok(()) + Ok(function) } fn compile_expression( @@ -418,7 +432,9 @@ impl<'ctx> CodeGen<'ctx> { decorator_list, returns, } = &statement.node { - self.compile_function_def(name, args, body, decorator_list, returns, *is_async) + let function = self.compile_function_def(name, args, body, decorator_list, returns, *is_async)?; + self.pass_manager.run_on(&function); + Ok(()) } else { Err(self.compile_error(CompileErrorKind::Internal("top-level is not a function definition"))) }