Enable simple optimizations.

This commit is contained in:
Stefano Sanfilippo 2015-03-09 22:48:10 +01:00
parent f28172e9f2
commit f35332cf89
2 changed files with 22 additions and 2 deletions

View File

@ -27,6 +27,9 @@
#include <llvm/IR/IRBuilder.h> #include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h> #include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h> #include <llvm/IR/Module.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Analysis/Passes.h>
#include <cassert> #include <cassert>
#include <string> #include <string>
@ -54,6 +57,7 @@ struct BitcodeEmitter::Private {
llvm::IRBuilder<> builder = llvm::IRBuilder<>(getGlobalContext()); llvm::IRBuilder<> builder = llvm::IRBuilder<>(getGlobalContext());
Scope<std::string, llvm::AllocaInst*> scope; Scope<std::string, llvm::AllocaInst*> scope;
Pointer<llvm::legacy::FunctionPassManager> optimizer;
}; };
static static
@ -234,6 +238,17 @@ BitcodeEmitter::BitcodeEmitter() {
new llvm::Module("monicelli", getGlobalContext()) new llvm::Module("monicelli", getGlobalContext())
); );
d = new Private; d = new Private;
d->optimizer = Pointer<llvm::legacy::FunctionPassManager>(
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() { BitcodeEmitter::~BitcodeEmitter() {
@ -599,6 +614,8 @@ bool BitcodeEmitter::emit(Function const& node) {
verifyFunction(*func); verifyFunction(*func);
d->optimizer->run(*func);
return true; return true;
} }
@ -624,6 +641,8 @@ bool BitcodeEmitter::emit(Program const& program) {
GUARDED(program.getMain()->emit(this)); GUARDED(program.getMain()->emit(this));
} }
verifyModule(*module);
return true; return true;
} }

View File

@ -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 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 ;) 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 disassembling with `llvm-dis`. However, you might want to optimize the code
using `opt` LLVM utility: using `opt` LLVM utility:
$ opt example.bc | llc -o example.s $ 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 ###C++ transpiler
`mcc` can be configured as a source to source compiler, which reads Monicelli `mcc` can be configured as a source to source compiler, which reads Monicelli