annotate gen/optimizer.cpp @ 1638:0de4525a9ed6

Apply workaround for #395 by klickverbot.
author Christian Kamm <kamm incasoftware de>
date Mon, 08 Mar 2010 20:06:08 +0100
parents 4551475bc6b6
children 40bd4a0d4870
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
1 #include "gen/optimizer.h"
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
2 #include "gen/cl_helpers.h"
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
3
1275
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
4 #include "gen/passes/Passes.h"
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
5
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
6 #include "llvm/PassManager.h"
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
7 #include "llvm/LinkAllPasses.h"
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
8 #include "llvm/Analysis/LoopPass.h"
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
9 #include "llvm/Analysis/Verifier.h"
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
10 #include "llvm/Target/TargetData.h"
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
11 #include "llvm/Support/CommandLine.h"
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
12 #include "llvm/Support/PassNameParser.h"
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
13
1172
b3887714b735 Small cleanup: remove mars.h #include
Frits van Bommel <fvbommel wxs.nl>
parents: 1171
diff changeset
14 #include "root.h" // error()
1482
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
15 #include <cstring> // strcmp();
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
16
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
17 using namespace llvm;
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
18
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
19 // Allow the user to specify specific optimizations to run.
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
20 static cl::list<const PassInfo*, bool, PassNameParser>
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
21 passList(
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
22 cl::desc("Running specific optimizations:"),
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
23 cl::Hidden // to clean up --help output
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
24 );
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
25
1281
29d3861aa2da Make sure this still compiles after LLVM r70437, which introduces a
Frits van Bommel <fvbommel wxs.nl>
parents: 1275
diff changeset
26 static cl::opt<unsigned char> optimizeLevel(
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
27 cl::desc("Setting the optimization level:"),
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
28 cl::ZeroOrMore,
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
29 cl::values(
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
30 clEnumValN(2, "O", "Equivalent to -O2"),
1171
461a85f0db31 Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents: 1170
diff changeset
31 clEnumValN(0, "O0", "No optimizations (default)"),
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
32 clEnumValN(1, "O1", "Simple optimizations"),
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
33 clEnumValN(2, "O2", "Good optimizations"),
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
34 clEnumValN(3, "O3", "Aggressive optimizations"),
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
35 clEnumValN(4, "O4", "Link-time optimization"), // not implemented?
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
36 clEnumValN(5, "O5", "Link-time optimization"), // not implemented?
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
37 clEnumValEnd),
1171
461a85f0db31 Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents: 1170
diff changeset
38 cl::init(0));
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
39
1275
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
40 static cl::opt<bool>
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
41 verifyEach("verify-each",
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
42 cl::desc("Run verifier after each optimization pass"),
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
43 cl::Hidden,
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
44 cl::ZeroOrMore);
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
45
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
46 static cl::opt<bool>
1275
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
47 disableLangSpecificPasses("disable-d-passes",
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
48 cl::desc("Disable D-specific passes in -O<N>"),
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
49 cl::ZeroOrMore);
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
50
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
51 static cl::opt<bool>
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
52 disableSimplifyRuntimeCalls("disable-simplify-drtcalls",
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
53 cl::desc("Disable simplification of runtime calls in -O<N>"),
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
54 cl::ZeroOrMore);
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
55
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents: 1281
diff changeset
56 static cl::opt<bool>
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents: 1281
diff changeset
57 disableGCToStack("disable-gc2stack",
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents: 1281
diff changeset
58 cl::desc("Disable promotion of GC allocations to stack memory in -O<N>"),
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents: 1281
diff changeset
59 cl::ZeroOrMore);
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents: 1281
diff changeset
60
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
61 static cl::opt<opts::BoolOrDefaultAdapter, false, opts::FlagParser>
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
62 enableInlining("inlining",
1275
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
63 cl::desc("(*) Enable function inlining in -O<N>"),
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
64 cl::ZeroOrMore);
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
65
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
66 // Determine whether or not to run the inliner as part of the default list of
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
67 // optimization passes.
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
68 // If not explicitly specified, treat as false for -O0-2, and true for -O3.
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
69 bool doInline() {
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
70 return enableInlining == cl::BOU_TRUE
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
71 || (enableInlining == cl::BOU_UNSET && optimizeLevel >= 3);
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
72 }
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
73
1482
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
74 // Determine whether the inliner will be run.
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
75 bool willInline() {
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
76 if (doInline())
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
77 return true;
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
78 // It may also have been specified explicitly on the command line as an explicit pass
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
79 typedef cl::list<const PassInfo*, bool, PassNameParser> PL;
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
80 for (PL::iterator I = passList.begin(), E = passList.end(); I != E; ++I) {
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
81 if (!std::strcmp((*I)->getPassArgument(), "inline"))
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
82 return true;
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
83 }
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
84 return false;
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
85 }
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
86
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
87 // Some extra accessors for the linker: (llvm-ld version only, currently unused?)
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
88 int optLevel() {
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
89 return optimizeLevel;
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
90 }
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
91
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
92 bool optimize() {
1267
381c3ee0ca00 Fix a logic bug.
Frits van Bommel <fvbommel wxs.nl>
parents: 1220
diff changeset
93 return optimizeLevel || doInline() || !passList.empty();
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
94 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
95
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
96 static void addPass(PassManager& pm, Pass* pass) {
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
97 pm.add(pass);
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
98
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
99 if (verifyEach) pm.add(createVerifierPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
100 }
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
101
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
102 // this function inserts some or all of the std-compile-opts passes depending on the
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
103 // optimization level given.
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
104 static void addPassesForOptLevel(PassManager& pm) {
1171
461a85f0db31 Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents: 1170
diff changeset
105 // -O1
461a85f0db31 Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents: 1170
diff changeset
106 if (optimizeLevel >= 1)
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
107 {
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
108 //addPass(pm, createStripDeadPrototypesPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
109 addPass(pm, createGlobalDCEPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
110 addPass(pm, createRaiseAllocationsPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
111 addPass(pm, createCFGSimplificationPass());
1219
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
112 if (optimizeLevel == 1)
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
113 addPass(pm, createPromoteMemoryToRegisterPass());
1219
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
114 else
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
115 addPass(pm, createScalarReplAggregatesPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
116 addPass(pm, createGlobalOptimizerPass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
117 }
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
118
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
119 // -O2
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
120 if (optimizeLevel >= 2)
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
121 {
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
122 addPass(pm, createIPConstantPropagationPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
123 addPass(pm, createDeadArgEliminationPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
124 addPass(pm, createInstructionCombiningPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
125 addPass(pm, createCFGSimplificationPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
126 addPass(pm, createPruneEHPass());
1423
42bd767ec5a4 * Add -functionattrs to default pass list so -dgc2stack is more effective
Frits van Bommel <fvbommel wxs.nl>
parents: 1296
diff changeset
127 addPass(pm, createFunctionAttrsPass());
1472
a28953f1eb43 Move `-tailcallelim` to a place in the optimization sequence where it seems to
Frits van Bommel <fvbommel wxs.nl>
parents: 1423
diff changeset
128 addPass(pm, createTailCallEliminationPass());
a28953f1eb43 Move `-tailcallelim` to a place in the optimization sequence where it seems to
Frits van Bommel <fvbommel wxs.nl>
parents: 1423
diff changeset
129 addPass(pm, createCFGSimplificationPass());
1506
76936858d1c6 Return `void*` from _d_allocclass so LLVM doesn't do weird things with it...
Frits van Bommel <fvbommel wxs.nl>
parents: 1492
diff changeset
130 addPass(pm, createGVNPass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
131 }
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
132
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
133 // -inline
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
134 if (doInline()) {
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
135 addPass(pm, createFunctionInliningPass());
1288
e109e4031e8a Fix build when USE_METADATA is off.
Matti Niemenmaa <matti.niemenmaa+hg@iki.fi>
parents: 1287
diff changeset
136
1219
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
137 if (optimizeLevel >= 2) {
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
138 // Run some optimizations to clean up after inlining.
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
139 addPass(pm, createScalarReplAggregatesPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
140 addPass(pm, createInstructionCombiningPass());
1506
76936858d1c6 Return `void*` from _d_allocclass so LLVM doesn't do weird things with it...
Frits van Bommel <fvbommel wxs.nl>
parents: 1492
diff changeset
141 // -instcombine + gvn == devirtualization :)
76936858d1c6 Return `void*` from _d_allocclass so LLVM doesn't do weird things with it...
Frits van Bommel <fvbommel wxs.nl>
parents: 1492
diff changeset
142 addPass(pm, createGVNPass());
1288
e109e4031e8a Fix build when USE_METADATA is off.
Matti Niemenmaa <matti.niemenmaa+hg@iki.fi>
parents: 1287
diff changeset
143
1506
76936858d1c6 Return `void*` from _d_allocclass so LLVM doesn't do weird things with it...
Frits van Bommel <fvbommel wxs.nl>
parents: 1492
diff changeset
144 // Inline again, to catch things like now nonvirtual
76936858d1c6 Return `void*` from _d_allocclass so LLVM doesn't do weird things with it...
Frits van Bommel <fvbommel wxs.nl>
parents: 1492
diff changeset
145 // function calls, foreach delegates passed to inlined
76936858d1c6 Return `void*` from _d_allocclass so LLVM doesn't do weird things with it...
Frits van Bommel <fvbommel wxs.nl>
parents: 1492
diff changeset
146 // opApply's, etc. where the actual function being called
76936858d1c6 Return `void*` from _d_allocclass so LLVM doesn't do weird things with it...
Frits van Bommel <fvbommel wxs.nl>
parents: 1492
diff changeset
147 // wasn't known during the first inliner pass.
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
148 addPass(pm, createFunctionInliningPass());
1219
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
149 }
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
150 }
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
151
1507
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
152 if (optimizeLevel >= 2) {
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
153 if (!disableLangSpecificPasses) {
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
154 if (!disableSimplifyRuntimeCalls)
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
155 addPass(pm, createSimplifyDRuntimeCalls());
1547
259b031f3d22 Some minor cleanups
Benjamin Kramer <benny.kra@gmail.com>
parents: 1533
diff changeset
156
1558
3adcb70700cb Added back option to disable metadata generation and users. Set USE_METADATA to OFF in ccmake.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents: 1547
diff changeset
157 #if USE_METADATA
1507
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
158 if (!disableGCToStack)
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
159 addPass(pm, createGarbageCollect2Stack());
1558
3adcb70700cb Added back option to disable metadata generation and users. Set USE_METADATA to OFF in ccmake.
Tomas Lindquist Olsen <tomas.l.olsen gmail com>
parents: 1547
diff changeset
160 #endif // USE_METADATA
1285
91d9386d4a5a Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents: 1281
diff changeset
161 }
1507
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
162 // Run some clean-up passes
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
163 addPass(pm, createInstructionCombiningPass());
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
164 addPass(pm, createScalarReplAggregatesPass());
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
165 addPass(pm, createCFGSimplificationPass());
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
166 addPass(pm, createInstructionCombiningPass());
1275
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
167 }
1507
f86fd3b77285 Eliminate comparisons between GC allocations and constants. This removes some
Frits van Bommel <fvbommel wxs.nl>
parents: 1506
diff changeset
168
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
169 // -O3
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
170 if (optimizeLevel >= 3)
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
171 {
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
172 addPass(pm, createArgumentPromotionPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
173 addPass(pm, createTailDuplicationPass());
1492
ef76f6e1693c Add some passes that `opt` runs to the default pass list.
Frits van Bommel <fvbommel wxs.nl>
parents: 1486
diff changeset
174 addPass(pm, createSimplifyLibCallsPass());
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
175 addPass(pm, createInstructionCombiningPass());
1492
ef76f6e1693c Add some passes that `opt` runs to the default pass list.
Frits van Bommel <fvbommel wxs.nl>
parents: 1486
diff changeset
176 addPass(pm, createJumpThreadingPass());
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
177 addPass(pm, createCFGSimplificationPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
178 addPass(pm, createScalarReplAggregatesPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
179 addPass(pm, createInstructionCombiningPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
180 addPass(pm, createCondPropagationPass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
181
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
182 addPass(pm, createReassociatePass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
183 addPass(pm, createLoopRotatePass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
184 addPass(pm, createLICMPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
185 addPass(pm, createLoopUnswitchPass());
1492
ef76f6e1693c Add some passes that `opt` runs to the default pass list.
Frits van Bommel <fvbommel wxs.nl>
parents: 1486
diff changeset
186 addPass(pm, createLoopIndexSplitPass());
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
187 addPass(pm, createInstructionCombiningPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
188 addPass(pm, createIndVarSimplifyPass());
1492
ef76f6e1693c Add some passes that `opt` runs to the default pass list.
Frits van Bommel <fvbommel wxs.nl>
parents: 1486
diff changeset
189 addPass(pm, createLoopDeletionPass());
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
190 addPass(pm, createLoopUnrollPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
191 addPass(pm, createInstructionCombiningPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
192 addPass(pm, createGVNPass());
1481
e0f03e11cdf8 Add `-memcpyopt` to the optimization pass list.
Frits van Bommel <fvbommel wxs.nl>
parents: 1472
diff changeset
193 addPass(pm, createMemCpyOptPass());
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
194 addPass(pm, createSCCPPass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
195
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
196 addPass(pm, createInstructionCombiningPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
197 addPass(pm, createCondPropagationPass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
198
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
199 addPass(pm, createDeadStoreEliminationPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
200 addPass(pm, createAggressiveDCEPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
201 addPass(pm, createCFGSimplificationPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
202 addPass(pm, createDeadTypeEliminationPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
203 addPass(pm, createConstantMergePass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
204 }
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
205
1482
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
206 if (optimizeLevel >= 1) {
1483
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents: 1482
diff changeset
207 addPass(pm, createStripExternalsPass());
1482
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
208 addPass(pm, createGlobalDCEPass());
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
209 }
d9c5f5a43403 Run `semantic3` on imported modules, and emit new symbols with
Frits van Bommel <fvbommel wxs.nl>
parents: 1481
diff changeset
210
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
211 // level -O4 and -O5 are linktime optimizations
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
212 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
213
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
214 //////////////////////////////////////////////////////////////////////////////////////////
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
215 // This function runs optimization passes based on command line arguments.
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
216 // Returns true if any optimization passes were invoked.
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
217 bool ldc_optimize_module(llvm::Module* m)
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
218 {
1576
4551475bc6b6 Kill off StripMetaData.
Benjamin Kramer <benny.kra@gmail.com>
parents: 1558
diff changeset
219 if (!optimize())
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
220 return false;
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
221
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
222 PassManager pm;
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
223
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
224 if (verifyEach) pm.add(createVerifierPass());
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
225
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
226 addPass(pm, new TargetData(m));
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
227
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
228 bool optimize = optimizeLevel != 0 || doInline();
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
229
1171
461a85f0db31 Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents: 1170
diff changeset
230 unsigned optPos = optimizeLevel != 0
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
231 ? optimizeLevel.getPosition()
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
232 : enableInlining.getPosition();
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
233
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
234 for (size_t i = 0; i < passList.size(); i++) {
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
235 // insert -O<N> / -enable-inlining in right position
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
236 if (optimize && optPos < passList.getPosition(i)) {
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
237 addPassesForOptLevel(pm);
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
238 optimize = false;
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
239 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
240
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
241 const PassInfo* pass = passList[i];
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
242 if (PassInfo::NormalCtor_t ctor = pass->getNormalCtor()) {
1296
79b201533cf8 Add -verify-each option to ease debugging
Frits van Bommel <fvbommel wxs.nl>
parents: 1294
diff changeset
243 addPass(pm, ctor());
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
244 } else {
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
245 const char* arg = pass->getPassArgument(); // may return null
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
246 if (arg)
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
247 error("Can't create pass '-%s' (%s)", arg, pass->getPassName());
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
248 else
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
249 error("Can't create pass (%s)", pass->getPassName());
1172
b3887714b735 Small cleanup: remove mars.h #include
Frits van Bommel <fvbommel wxs.nl>
parents: 1171
diff changeset
250 assert(0); // Should be unreachable; root.h:error() calls exit()
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
251 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
252 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
253 // insert -O<N> / -enable-inlining if specified at the end,
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
254 if (optimize)
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
255 addPassesForOptLevel(pm);
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
256
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
257 pm.run(*m);
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
258 return true;
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
259 }