From f35332cf8985266d99392c077de74bd2cec5c8ff Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Mon, 9 Mar 2015 22:48:10 +0100 Subject: [PATCH] Enable simple optimizations. --- BitcodeEmitter.cpp | 19 +++++++++++++++++++ README.md | 5 +++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/BitcodeEmitter.cpp b/BitcodeEmitter.cpp index d8f548d..0d1c5a1 100644 --- a/BitcodeEmitter.cpp +++ b/BitcodeEmitter.cpp @@ -27,6 +27,9 @@ #include #include #include +#include +#include +#include #include #include @@ -54,6 +57,7 @@ struct BitcodeEmitter::Private { llvm::IRBuilder<> builder = llvm::IRBuilder<>(getGlobalContext()); Scope scope; + Pointer optimizer; }; static @@ -234,6 +238,17 @@ BitcodeEmitter::BitcodeEmitter() { new llvm::Module("monicelli", getGlobalContext()) ); d = new Private; + + d->optimizer = Pointer( + new llvm::legacy::FunctionPassManager(module.get()) + ); + + d->optimizer->add(llvm::createBasicAliasAnalysisPass()); + d->optimizer->add(llvm::createInstructionCombiningPass()); + d->optimizer->add(llvm::createReassociatePass()); + d->optimizer->add(llvm::createGVNPass()); + d->optimizer->add(llvm::createCFGSimplificationPass()); + d->optimizer->doInitialization(); } BitcodeEmitter::~BitcodeEmitter() { @@ -599,6 +614,8 @@ bool BitcodeEmitter::emit(Function const& node) { verifyFunction(*func); + d->optimizer->run(*func); + return true; } @@ -624,6 +641,8 @@ bool BitcodeEmitter::emit(Program const& program) { GUARDED(program.getMain()->emit(this)); } + verifyModule(*module); + return true; } diff --git a/README.md b/README.md index dcdee92..9a5efd5 100644 --- a/README.md +++ b/README.md @@ -76,13 +76,14 @@ Usually, the relevant package goes under the name `llvm`. A C compiler is used to simplify the assembling and linking step, but it could be skipped altogether with a small effort. If you want to try ;) -`mcc` does not optimize the emitted bytecode to ensure readibility when +`mcc` only performs minimal optimizations in order to ensure readibility when disassembling with `llvm-dis`. However, you might want to optimize the code using `opt` LLVM utility: $ opt example.bc | llc -o example.s -in place of the simple `llc` compilation step. +in place of the simple `llc` compilation step. See `opt` documentation for a +comprehensive list of optimizations available. ###C++ transpiler `mcc` can be configured as a source to source compiler, which reads Monicelli