From a75b7a08186de693f2fe7ef96d950c71f73542bc Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Wed, 21 Apr 2021 22:48:55 -0700 Subject: [PATCH] add optimization transforms --- compiler.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/compiler.c b/compiler.c index 9849d26..b6278a7 100644 --- a/compiler.c +++ b/compiler.c @@ -5,6 +5,10 @@ #include #include #include +#include +#include +#include +#include #include "y.tab.h" #include "ast.h" @@ -643,9 +647,24 @@ int main(int argc, char *argv[]) LLVMVerifyModule(module, LLVMAbortProcessAction, &error); LLVMDisposeMessage(error); + LLVMPassManagerRef passManager = LLVMCreatePassManager(); + LLVMAddInstructionCombiningPass(passManager); + LLVMAddCFGSimplificationPass(passManager); + LLVMAddReassociatePass(passManager); + LLVMAddPromoteMemoryToRegisterPass(passManager); + + LLVMPassManagerBuilderRef passManagerBuilder = LLVMPassManagerBuilderCreate(); + LLVMPassManagerBuilderSetOptLevel(passManagerBuilder, 3); + LLVMPassManagerBuilderPopulateModulePassManager(passManagerBuilder, passManager); + + LLVMRunPassManager(passManager, module); + if (LLVMWriteBitcodeToFile(module, "test.bc") != 0) { fprintf(stderr, "error writing bitcode to file\n"); } + LLVMPassManagerBuilderDispose(passManagerBuilder); + LLVMDisposePassManager(passManager); + LLVMDisposeModule(module); return 0; }