changeset 125:c42d245468ea trunk

[svn r129] Started AA literals. Fixed #15, passing -O will now invoke the optimizer before writing bitcode.
author lindquist
date Wed, 28 Nov 2007 04:52:35 +0100
parents a939ec89fc72
children a2c2c3c1a73d
files gen/optimizer.cpp gen/toir.cpp gen/toobj.cpp llvmdc.kdevelop llvmdc.kdevelop.filelist premake.lua
diffstat 6 files changed, 137 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gen/optimizer.cpp	Wed Nov 28 04:52:35 2007 +0100
@@ -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);
+}
--- a/gen/toir.cpp	Wed Nov 28 03:34:37 2007 +0100
+++ b/gen/toir.cpp	Wed Nov 28 04:52:35 2007 +0100
@@ -2574,6 +2574,29 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////
 
+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; i<n; ++i)
+    {
+        Expression* ekey = (Expression*)keys->data[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(HaltExp);
 STUB(RemoveExp);
 //STUB(ArrayLiteralExp);
-STUB(AssocArrayLiteralExp);
+//STUB(AssocArrayLiteralExp);
 //STUB(StructLiteralExp);
 STUB(TupleExp);
 
--- a/gen/toobj.cpp	Wed Nov 28 03:34:37 2007 +0100
+++ b/gen/toobj.cpp	Wed Nov 28 04:52:35 2007 +0100
@@ -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 @@
     // 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 @@
         }
     }
 
-    // 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 @@
     }
 
     delete ir.module;
+    gTargetData = 0;
     gIR = NULL;
 }
 
--- a/llvmdc.kdevelop	Wed Nov 28 03:34:37 2007 +0100
+++ b/llvmdc.kdevelop	Wed Nov 28 04:52:35 2007 +0100
@@ -14,8 +14,8 @@
     <projectname>llvmdc</projectname>
     <projectdirectory>.</projectdirectory>
     <absoluteprojectpath>false</absoluteprojectpath>
-    <description/>
-    <defaultencoding/>
+    <description></description>
+    <defaultencoding></defaultencoding>
   </general>
   <kdevautoproject>
     <general/>
@@ -147,7 +147,7 @@
       <namespaceAliases>std=_GLIBCXX_STD;__gnu_cxx=std</namespaceAliases>
       <processPrimaryTypes>true</processPrimaryTypes>
       <processFunctionArguments>false</processFunctionArguments>
-      <preProcessAllHeaders>false</preProcessAllHeaders>
+      <preProcessAllHeaders>true</preProcessAllHeaders>
       <parseMissingHeadersExperimental>false</parseMissingHeadersExperimental>
       <resolveIncludePathsUsingMakeExperimental>false</resolveIncludePathsUsingMakeExperimental>
       <alwaysParseInBackground>true</alwaysParseInBackground>
@@ -156,7 +156,7 @@
       <includePaths>.;</includePaths>
     </codecompletion>
     <creategettersetter>
-      <prefixGet/>
+      <prefixGet></prefixGet>
       <prefixSet>set</prefixSet>
       <prefixVariable>m_,_</prefixVariable>
       <parameterName>theValue</parameterName>
@@ -174,8 +174,8 @@
     <run>
       <directoryradio>executable</directoryradio>
       <mainprogram>/home/tomas/kdevprojects/llvmdc</mainprogram>
-      <programargs/>
-      <globaldebugarguments/>
+      <programargs></programargs>
+      <globaldebugarguments></globaldebugarguments>
       <globalcwd>/home/tomas/kdevprojects/llvmdc</globalcwd>
       <useglobalprogram>false</useglobalprogram>
       <terminal>false</terminal>
@@ -398,13 +398,13 @@
     </blacklist>
     <build>
       <buildtool>make</buildtool>
-      <builddir/>
+      <builddir></builddir>
     </build>
     <other>
       <prio>0</prio>
-      <otherbin/>
-      <defaulttarget/>
-      <otheroptions/>
+      <otherbin></otherbin>
+      <defaulttarget></defaulttarget>
+      <otheroptions></otheroptions>
       <selectedenvironment>default</selectedenvironment>
       <environments>
         <default/>
@@ -415,9 +415,9 @@
       <numberofjobs>0</numberofjobs>
       <prio>0</prio>
       <dontact>false</dontact>
-      <makebin/>
-      <defaulttarget/>
-      <makeoptions/>
+      <makebin></makebin>
+      <defaulttarget></defaulttarget>
+      <makeoptions></makeoptions>
       <selectedenvironment>default</selectedenvironment>
       <environments>
         <default/>
@@ -432,11 +432,11 @@
   </cppsupportpart>
   <kdevdebugger>
     <general>
-      <gdbpath/>
-      <dbgshell/>
-      <configGdbScript/>
-      <runShellScript/>
-      <runGdbScript/>
+      <gdbpath></gdbpath>
+      <dbgshell></dbgshell>
+      <configGdbScript></configGdbScript>
+      <runShellScript></runShellScript>
+      <runGdbScript></runGdbScript>
       <breakonloadinglibs>true</breakonloadinglibs>
       <separatetty>false</separatetty>
       <floatingtoolbar>false</floatingtoolbar>
--- a/llvmdc.kdevelop.filelist	Wed Nov 28 03:34:37 2007 +0100
+++ b/llvmdc.kdevelop.filelist	Wed Nov 28 04:52:35 2007 +0100
@@ -120,6 +120,7 @@
 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/aa3.d
 test/aa4.d
 test/aa5.d
+test/aa6.d
 test/alignment.d
 test/alloca1.d
 test/arrayinit.d
--- a/premake.lua	Wed Nov 28 03:34:37 2007 +0100
+++ b/premake.lua	Wed Nov 28 04:52:35 2007 +0100
@@ -24,7 +24,7 @@
 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" }