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/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Analysis/Passes.h>
#include <cassert>
#include <string>
@ -54,6 +57,7 @@ struct BitcodeEmitter::Private {
llvm::IRBuilder<> builder = llvm::IRBuilder<>(getGlobalContext());
Scope<std::string, llvm::AllocaInst*> scope;
Pointer<llvm::legacy::FunctionPassManager> optimizer;
};
static
@ -234,6 +238,17 @@ BitcodeEmitter::BitcodeEmitter() {
new llvm::Module("monicelli", getGlobalContext())
);
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() {
@ -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;
}

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
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