Wiki Roadmap Timeline Tickets New Ticket Source Search Help / Guide About Trac Login

Changeset 1170:e40c65bd8c5d

Show
Ignore:
Timestamp:
03/29/09 09:46:55 (3 years ago)
Author:
Frits van Bommel <fvbommel wxs.nl>
branch:
default
Message:

Allow specific optimization passes to be requested from the command line.
Now you can run "ldc test.d -c -mem2reg -simplifycfg" if you feel the urge.
The -O<N> options are still supported, and are inserted in the passes list in
the position where they appear on the command line.
(so -simplifycfg -O1 -instcombine does the "right thing")

One small change: -inline is renamed to -enable-inlining due to a naming
conflict with the option to add the -inline pass. -inline now inserts the
inlining pass in the position specified, not in the middle of -O<N>.
(ldmd has been updated to translate -inline to -enable-inlining)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • bin/ldmd

    r1060 r1170  
    1818for arg; do 
    1919    case "$arg" in 
     20    -C*) 
     21        # turn -Cfoo into -foo. 
     22        # Useful for passing -inline to ldc, for instance. 
     23        arg="-${arg:2}" 
     24        ;; 
    2025    -debug|-debug=*|-version=*) 
    2126        arg="-d$arg" 
     27        ;; 
     28    -inline) 
     29        arg="-enable-inlining" 
    2230        ;; 
    2331    -fPIC) 
  • dmd/mars.h

    r1124 r1170  
    139139    bool verbose;       // verbose compile 
    140140    char symdebug;  // insert debug symbolic information 
     141#if !IN_LLVM 
     142    // LDC uses a different mechanism 
    141143    bool optimize;      // run optimizer 
    142144    char optimizeLevel; // optimization level 
     145#endif 
    143146    ARCH cpu;       // target CPU 
    144147    OS   os;        // target OS 
     
    211214    OUTPUTFLAG output_s; 
    212215    OUTPUTFLAG output_o; 
    213     bool llvmInline; 
    214216    bool llvmAnnotate; 
    215217    bool useInlineAsm; 
  • dmd2/mars.h

    r1064 r1170  
    7474    bool verbose;       // verbose compile 
    7575    char symdebug;  // insert debug symbolic information 
     76#if !IN_LLVM 
     77    // LDC uses a different mechanism 
    7678    bool optimize;      // run optimizer 
    7779    char optimizeLevel; // optimization level 
     80#endif 
    7881    ARCH cpu;       // target CPU 
    7982    OS   os;        // target OS 
     
    146149    OUTPUTFLAG output_s; 
    147150    OUTPUTFLAG output_o; 
    148     bool llvmInline; 
    149151    bool llvmAnnotate; 
    150152    bool useInlineAsm; 
  • gen/cl_options.cpp

    r1067 r1170  
    1919    cl::Positional, 
    2020    cl::PositionalEatsArgs); 
    21  
    22  
    23  
    24 // TODO: Replace this with a proper PassNameParser-based solution 
    25 static cl::opt<bool, true> doInline("inline", 
    26     cl::desc("Do function inlining"), 
    27     cl::location(global.params.llvmInline), 
    28     cl::ZeroOrMore, 
    29     cl::init(false)); 
    30  
    3121 
    3222 
     
    5949    cl::ZeroOrMore, 
    6050    cl::location(global.params.warnings)); 
    61  
    62  
    63 static cl::opt<char, true> optimizeLevel( 
    64     cl::desc("Setting the optimization level:"), 
    65     cl::ZeroOrMore, 
    66     cl::values( 
    67         clEnumValN(2, "O",  "Equivalent to -O2"), 
    68         clEnumValN(0, "O0", "Trivial optimizations only"), 
    69         clEnumValN(1, "O1", "Simple optimizations"), 
    70         clEnumValN(2, "O2", "Good optimizations"), 
    71         clEnumValN(3, "O3", "Aggressive optimizations"), 
    72         clEnumValN(4, "O4", "Link-time optimization"), //  not implemented? 
    73         clEnumValN(5, "O5", "Link-time optimization"), //  not implemented? 
    74         clEnumValEnd), 
    75     cl::location(global.params.optimizeLevel), 
    76     cl::init(-1)); 
    7751 
    7852static cl::opt<char, true> debugInfo( 
  • gen/linker.cpp

    r1026 r1170  
    1414#include "gen/logger.h" 
    1515#include "gen/cl_options.h" 
     16#include "gen/optimizer.h" 
    1617 
    1718////////////////////////////////////////////////////////////////////////////// 
     
    112113 
    113114    // optimization level 
    114     if (!global.params.optimize
     115    if (!optimize()
    115116        args.push_back("-disable-opt"); 
    116117    else 
    117118    { 
    118119        const char* s = 0; 
    119         switch(global.params.optimizeLevel
     120        switch(optLevel()
    120121        { 
    121122        case 0: 
     
    139140 
    140141    // inlining 
    141     if (!(global.params.useInline || global.params.llvmInline)) 
     142    if (!(global.params.useInline || doInline())) 
    142143    { 
    143144        args.push_back("-disable-inlining"); 
  • gen/main.cpp

    r1149 r1170  
    192192    cl::SetVersionPrinter(&printVersion); 
    193193    cl::ParseCommandLineOptions(final_args.size(), (char**)&final_args[0], "LLVM-based D Compiler\n", true); 
    194  
    195     global.params.optimize = (global.params.optimizeLevel >= 0); 
    196194 
    197195    // Negated options 
  • gen/optimizer.cpp

    r663 r1170  
     1#include "gen/optimizer.h" 
     2 
    13#include "llvm/PassManager.h" 
    24#include "llvm/LinkAllPasses.h" 
    35#include "llvm/Analysis/LoopPass.h" 
    46#include "llvm/Target/TargetData.h" 
     7#include "llvm/Support/CommandLine.h" 
     8#include "llvm/Support/PassNameParser.h" 
     9 
     10#include "root.h"       // error() & fatal() 
     11#include "mars.h"       // global flags 
    512 
    613using namespace llvm; 
    714 
    8 ////////////////////////////////////////////////////////////////////////////////////////// 
     15// Allow the user to specify specific optimizations to run. 
     16static cl::list<const PassInfo*, bool, PassNameParser> 
     17    passList( 
     18        cl::desc("Running specific optimizations:"), 
     19        cl::Hidden      // to clean up --help output 
     20    ); 
    921 
    10 // this function runs some or all of the std-compile-opts passes depending on the 
     22static cl::opt<char> optimizeLevel( 
     23    cl::desc("Setting the optimization level:"), 
     24    cl::ZeroOrMore, 
     25    cl::values( 
     26        clEnumValN(2, "O",  "Equivalent to -O2"), 
     27        clEnumValN(0, "O0", "Trivial optimizations only"), 
     28        clEnumValN(1, "O1", "Simple optimizations"), 
     29        clEnumValN(2, "O2", "Good optimizations"), 
     30        clEnumValN(3, "O3", "Aggressive optimizations"), 
     31        clEnumValN(4, "O4", "Link-time optimization"), //  not implemented? 
     32        clEnumValN(5, "O5", "Link-time optimization"), //  not implemented? 
     33        clEnumValEnd), 
     34    cl::init(-1)); 
     35 
     36static cl::opt<bool> enableInlining("enable-inlining", 
     37    cl::desc("Enable function inlining (in -O<N>, if given)"), 
     38    cl::ZeroOrMore, 
     39    cl::init(false)); 
     40 
     41// Some accessors for the linker: (llvm-ld version only, currently unused?) 
     42bool doInline() { 
     43    return enableInlining; 
     44
     45 
     46int optLevel() { 
     47    return optimizeLevel; 
     48
     49 
     50bool optimize() { 
     51    return (optimizeLevel != -1) || enableInlining || passList.empty(); 
     52
     53 
     54// this function inserts some or all of the std-compile-opts passes depending on the 
    1155// optimization level given. 
    12  
    13 void ldc_optimize_module(Module* m, char lvl, bool doinline) 
    14 
    15     if (!doinline && lvl < 0) 
    16         return; 
    17  
    18     PassManager pm; 
    19     pm.add(new TargetData(m)); 
    20  
     56static void addPassesForOptLevel(PassManager& pm) { 
    2157    // -O0 
    22     if (lvl >= 0) 
     58    if (optimizeLevel >= 0) 
    2359    { 
    2460        //pm.add(createStripDeadPrototypesPass()); 
     
    2763 
    2864    // -O1 
    29     if (lvl >= 1) 
     65    if (optimizeLevel >= 1) 
    3066    { 
    3167        pm.add(createRaiseAllocationsPass()); 
     
    3773 
    3874    // -O2 
    39     if (lvl >= 2) 
     75    if (optimizeLevel >= 2) 
    4076    { 
    4177        pm.add(createIPConstantPropagationPass()); 
     
    4783 
    4884    // -inline 
    49     if (doinline) { 
     85    if (enableInlining) { 
    5086        pm.add(createFunctionInliningPass()); 
    5187    } 
    5288 
    5389    // -O3 
    54     if (lvl >= 3) 
     90    if (optimizeLevel >= 3) 
    5591    { 
    5692        pm.add(createArgumentPromotionPass()); 
     
    87123 
    88124    // level -O4 and -O5 are linktime optimizations 
     125} 
     126 
     127////////////////////////////////////////////////////////////////////////////////////////// 
     128// This function runs optimization passes based on command line arguments. 
     129// Returns true if any optimization passes were invoked. 
     130bool ldc_optimize_module(llvm::Module* m) 
     131{ 
     132    if (!enableInlining && optimizeLevel == -1 && passList.empty()) 
     133        return false; 
     134 
     135    PassManager pm; 
     136    pm.add(new TargetData(m)); 
     137 
     138    bool optimize = (optimizeLevel != -1) || enableInlining; 
     139 
     140    unsigned optPos = optimizeLevel != -1 
     141                    ? optimizeLevel.getPosition() 
     142                    : enableInlining.getPosition(); 
     143 
     144    for (size_t i = 0; i < passList.size(); i++) { 
     145        // insert -O<N> / -enable-inlining in right position 
     146        if (optimize && optPos < passList.getPosition(i)) { 
     147            addPassesForOptLevel(pm); 
     148            optimize = false; 
     149        } 
     150 
     151        const PassInfo* pass = passList[i]; 
     152        if (PassInfo::NormalCtor_t ctor = pass->getNormalCtor()) { 
     153            pm.add(ctor()); 
     154        } else { 
     155            const char* arg = pass->getPassArgument(); // may return null 
     156            if (arg) 
     157                error("Can't create pass '-%s' (%s)", arg, pass->getPassName()); 
     158            else 
     159                error("Can't create pass (%s)", pass->getPassName()); 
     160            fatal(); 
     161        } 
     162    } 
     163    // insert -O<N> / -enable-inlining if specified at the end, 
     164    if (optimize) 
     165        addPassesForOptLevel(pm); 
    89166 
    90167    pm.run(*m); 
     168    return true; 
    91169} 
  • gen/toobj.cpp

    r1163 r1170  
    5050#include "gen/abi.h" 
    5151#include "gen/cl_options.h" 
     52#include "gen/optimizer.h" 
    5253 
    5354#include "ir/irvar.h" 
     
    6162 
    6263////////////////////////////////////////////////////////////////////////////////////////// 
    63  
    64 // in gen/optimize.cpp 
    65 void ldc_optimize_module(llvm::Module* m, char lvl, bool doinline); 
    6664 
    6765// fwd decl 
     
    190188{ 
    191189    // run optimizer 
    192     ldc_optimize_module(m, global.params.optimizeLevel, global.params.llvmInline); 
     190    bool reverify = ldc_optimize_module(m); 
    193191 
    194192    // verify the llvm 
    195     if (!noVerify && (global.params.optimizeLevel >= 0 || global.params.llvmInline)) { 
     193    if (!noVerify && reverify) { 
    196194        std::string verifyErr; 
    197195        Logger::println("Verifying module... again..."); 
Copyright © 2008, LDC Development Team.