From 5ab2166b057d1490a94946d92b88966ff278ff99 Mon Sep 17 00:00:00 2001 From: Tomas Lindquist Olsen Date: Wed, 28 Nov 2007 04:52:35 +0100 Subject: [PATCH] [svn r129] Started AA literals. Fixed #15, passing -O will now invoke the optimizer before writing bitcode. --- gen/optimizer.cpp | 81 ++++++++++++++++++++++++++++++++++++++++ gen/toir.cpp | 25 ++++++++++++- gen/toobj.cpp | 17 ++++++--- llvmdc.kdevelop | 36 +++++++++--------- llvmdc.kdevelop.filelist | 2 + premake.lua | 2 +- 6 files changed, 137 insertions(+), 26 deletions(-) create mode 100644 gen/optimizer.cpp diff --git a/gen/optimizer.cpp b/gen/optimizer.cpp new file mode 100644 index 00000000..69e70dcb --- /dev/null +++ b/gen/optimizer.cpp @@ -0,0 +1,81 @@ +#include "llvm/PassManager.h" +#include "llvm/LinkAllPasses.h" +#include "llvm/Analysis/LoopPass.h" +#include "llvm/Target/TargetData.h" + +using namespace llvm; + +////////////////////////////////////////////////////////////////////////////////////////// + +// this function runs some or all of the std-compile-opts passes depending on the +// optimization level given. + +void llvmdc_optimize_module(Module* m, char lvl, bool doinline) +{ + assert(lvl >= 0 && lvl <= 5); + if (lvl == 0) + return; + + PassManager pm; + pm.add(new TargetData(m)); + + if (lvl >= 1) + { + pm.add(createRaiseAllocationsPass()); + pm.add(createCFGSimplificationPass()); + pm.add(createPromoteMemoryToRegisterPass()); + } + + if (lvl >= 2) + { + pm.add(createGlobalOptimizerPass()); + pm.add(createGlobalDCEPass()); + pm.add(createIPConstantPropagationPass()); + pm.add(createDeadArgEliminationPass()); + pm.add(createInstructionCombiningPass()); + pm.add(createCFGSimplificationPass()); + pm.add(createPruneEHPass()); + } + + if (doinline) { + pm.add(createFunctionInliningPass()); + } + + if (lvl >= 3) + { + pm.add(createArgumentPromotionPass()); + pm.add(createTailDuplicationPass()); + pm.add(createInstructionCombiningPass()); + pm.add(createCFGSimplificationPass()); + pm.add(createScalarReplAggregatesPass()); + pm.add(createInstructionCombiningPass()); + pm.add(createCondPropagationPass()); + + pm.add(createTailCallEliminationPass()); + pm.add(createCFGSimplificationPass()); + pm.add(createReassociatePass()); + pm.add(createLoopRotatePass()); + pm.add(createLICMPass()); + pm.add(createLoopUnswitchPass()); + pm.add(createInstructionCombiningPass()); + pm.add(createIndVarSimplifyPass()); + pm.add(createLoopUnrollPass()); + pm.add(createInstructionCombiningPass()); + pm.add(createGVNPass()); + pm.add(createSCCPPass()); + + pm.add(createInstructionCombiningPass()); + pm.add(createCondPropagationPass()); + + pm.add(createDeadStoreEliminationPass()); + pm.add(createAggressiveDCEPass()); + pm.add(createCFGSimplificationPass()); + pm.add(createSimplifyLibCallsPass()); + pm.add(createDeadTypeEliminationPass()); + pm.add(createConstantMergePass()); + } + + // level 4 and 5 are linktime optimizations + + pm.run(*m); +} diff --git a/gen/toir.cpp b/gen/toir.cpp index 64c24bfc..2b59ef8e 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -2574,6 +2574,29 @@ DValue* InExp::toElem(IRState* p) ////////////////////////////////////////////////////////////////////////////////////////// +DValue* AssocArrayLiteralExp::toElem(IRState* p) +{ + Logger::print("AssocArrayLiteralExp::toElem: %s | %s\n", toChars(), type->toChars()); + LOG_SCOPE; + + assert(keys); + assert(values); + assert(keys->dim == values->dim); + + const size_t n = keys->dim; + for (size_t i=0; idata[i]; + Expression* eval = (Expression*)values->data[i]; + + Logger::println("(%u) aa[%s] = %s", i, ekey->toChars(), eval->toChars()); + } + + assert(0); +} + +////////////////////////////////////////////////////////////////////////////////////////// + #define STUB(x) DValue *x::toElem(IRState * p) {error("Exp type "#x" not implemented: %s", toChars()); fatal(); return 0; } //STUB(IdentityExp); //STUB(CondExp); @@ -2645,7 +2668,7 @@ STUB(BoolExp); //STUB(HaltExp); STUB(RemoveExp); //STUB(ArrayLiteralExp); -STUB(AssocArrayLiteralExp); +//STUB(AssocArrayLiteralExp); //STUB(StructLiteralExp); STUB(TupleExp); diff --git a/gen/toobj.cpp b/gen/toobj.cpp index d77ac2e3..7dcab58c 100644 --- a/gen/toobj.cpp +++ b/gen/toobj.cpp @@ -43,8 +43,12 @@ ////////////////////////////////////////////////////////////////////////////////////////// -void -Module::genobjfile() +// in gen/optimize.cpp +void llvmdc_optimize_module(llvm::Module* m, char lvl, bool doinline); + +////////////////////////////////////////////////////////////////////////////////////////// + +void Module::genobjfile() { Logger::cout() << "Generating module: " << (md ? md->toChars() : toChars()) << '\n'; LOG_SCOPE; @@ -112,8 +116,6 @@ Module::genobjfile() // do this again as moduleinfo might have pulled something in! DtoEmptyAllLists(); - gTargetData = 0; - // emit the llvm main function if necessary if (ir.emitMain) { DtoMain(); @@ -134,8 +136,10 @@ Module::genobjfile() } } - // run passes - // TODO + // run optimizer + if (global.params.optimize) { + llvmdc_optimize_module(ir.module, global.params.optimizeLevel, global.params.useInline); + } // write bytecode { @@ -152,6 +156,7 @@ Module::genobjfile() } delete ir.module; + gTargetData = 0; gIR = NULL; } diff --git a/llvmdc.kdevelop b/llvmdc.kdevelop index aad6bdd4..325df896 100644 --- a/llvmdc.kdevelop +++ b/llvmdc.kdevelop @@ -14,8 +14,8 @@ llvmdc . false - - + + @@ -147,7 +147,7 @@ std=_GLIBCXX_STD;__gnu_cxx=std true false - false + true false false true @@ -156,7 +156,7 @@ .; - + set m_,_ theValue @@ -174,8 +174,8 @@ executable /home/tomas/kdevprojects/llvmdc - - + + /home/tomas/kdevprojects/llvmdc false false @@ -398,13 +398,13 @@ make - + 0 - - - + + + default @@ -415,9 +415,9 @@ 0 0 false - - - + + + default @@ -432,11 +432,11 @@ - - - - - + + + + + true false false diff --git a/llvmdc.kdevelop.filelist b/llvmdc.kdevelop.filelist index f99f8615..f01bf2d5 100644 --- a/llvmdc.kdevelop.filelist +++ b/llvmdc.kdevelop.filelist @@ -120,6 +120,7 @@ gen/irstate.h gen/llvm.h gen/logger.cpp gen/logger.h +gen/optimizer.cpp gen/runtime.cpp gen/runtime.h gen/statements.cpp @@ -239,6 +240,7 @@ test/aa2.d test/aa3.d test/aa4.d test/aa5.d +test/aa6.d test/alignment.d test/alloca1.d test/arrayinit.d diff --git a/premake.lua b/premake.lua index 5f2f9191..29fe09b8 100644 --- a/premake.lua +++ b/premake.lua @@ -24,7 +24,7 @@ package.language = "c++" package.files = { matchfiles("dmd/*.c"), matchfiles("gen/*.cpp") } package.excludes = { "dmd/idgen.c", "dmd/impcnvgen.c" } package.buildoptions = { "-x c++", "`llvm-config --cxxflags`" } -package.linkoptions = { "`llvm-config --libs native bitwriter bitreader`", "`llvm-config --ldflags`" } +package.linkoptions = { "`llvm-config --libs all`", "`llvm-config --ldflags`" } package.defines = { "IN_LLVM", "_DH" } package.config.Release.defines = { "LLVMD_NO_LOGGER" } package.config.Debug.buildoptions = { "-g -O0" }