annotate gen/optimizer.cpp @ 1281:29d3861aa2da

Make sure this still compiles after LLVM r70437, which introduces a specialization for command-line option template parser<char> which does the wrong thing for us...
author Frits van Bommel <fvbommel wxs.nl>
date Thu, 30 Apr 2009 12:25:04 +0200
parents bedf0bfb8fdb
children 91d9386d4a5a
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"
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
9 #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
10 #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
11 #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
12
1172
b3887714b735 Small cleanup: remove mars.h #include
Frits van Bommel <fvbommel wxs.nl>
parents: 1171
diff changeset
13 #include "root.h" // error()
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
14
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
15 using namespace llvm;
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
16
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
17 // 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
18 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
19 passList(
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
20 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
21 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
22 );
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
23
1281
29d3861aa2da Make sure this still compiles after LLVM r70437, which introduces a
Frits van Bommel <fvbommel wxs.nl>
parents: 1275
diff changeset
24 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
25 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
26 cl::ZeroOrMore,
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
27 cl::values(
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
28 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
29 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
30 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
31 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
32 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
33 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
34 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
35 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
36 cl::init(0));
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
37
1275
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
38 static cl::opt<bool>
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
39 disableLangSpecificPasses("disable-d-passes",
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
40 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
41 cl::ZeroOrMore);
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
42
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
43 static cl::opt<bool>
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
44 disableSimplifyRuntimeCalls("disable-simplify-drtcalls",
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
45 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
46 cl::ZeroOrMore);
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
47
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
48 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
49 enableInlining("inlining",
1275
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
50 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
51 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
52
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
53 // 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
54 // optimization passes.
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
55 // 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
56 bool doInline() {
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
57 return enableInlining == cl::BOU_TRUE
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
58 || (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
59 }
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
60
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
61 // 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
62 int optLevel() {
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
63 return optimizeLevel;
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
64 }
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
65
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
66 bool optimize() {
1267
381c3ee0ca00 Fix a logic bug.
Frits van Bommel <fvbommel wxs.nl>
parents: 1220
diff changeset
67 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
68 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
69
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
70 // 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
71 // 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
72 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
73 // -O1
461a85f0db31 Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents: 1170
diff changeset
74 if (optimizeLevel >= 1)
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
75 {
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
76 //pm.add(createStripDeadPrototypesPass());
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
77 pm.add(createGlobalDCEPass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
78 pm.add(createRaiseAllocationsPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
79 pm.add(createCFGSimplificationPass());
1219
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
80 if (optimizeLevel == 1)
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
81 pm.add(createPromoteMemoryToRegisterPass());
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
82 else
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
83 pm.add(createScalarReplAggregatesPass());
129
8096ba7082db [svn r133] Fixed some problems with inlining not happening :P
lindquist
parents: 125
diff changeset
84 pm.add(createGlobalOptimizerPass());
8096ba7082db [svn r133] Fixed some problems with inlining not happening :P
lindquist
parents: 125
diff changeset
85 pm.add(createGlobalDCEPass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
86 }
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
87
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
88 // -O2
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
89 if (optimizeLevel >= 2)
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
90 {
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
91 pm.add(createIPConstantPropagationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
92 pm.add(createDeadArgEliminationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
93 pm.add(createInstructionCombiningPass());
607
9526b29ae342 Fixed the optimizer thing, since llvm PR 2800 is already fixed, users need to upgrade LLVM to latest svn.
Tomas Lindquist Olsen <tomas.l.olsen@gmail.com>
parents: 605
diff changeset
94 pm.add(createCFGSimplificationPass());
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
95 pm.add(createPruneEHPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
96 }
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
97
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
98 // -inline
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
99 if (doInline()) {
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
100 pm.add(createFunctionInliningPass());
1219
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
101
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
102 if (optimizeLevel >= 2) {
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
103 // Run some optimizations to clean up after inlining.
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
104 pm.add(createInstructionCombiningPass());
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
105 pm.add(createScalarReplAggregatesPass());
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
106
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
107 // Inline again, to catch things like foreach delegates
1220
e945d2a0999e Fix typo in comment
Frits van Bommel <fvbommel wxs.nl>
parents: 1219
diff changeset
108 // passed to inlined opApply's where the function wasn't
1219
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
109 // known during the first inliner pass.
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
110 pm.add(createFunctionInliningPass());
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
111
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
112 // Run clean-up again.
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
113 pm.add(createInstructionCombiningPass());
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
114 pm.add(createScalarReplAggregatesPass());
a0844cc67840 Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents: 1175
diff changeset
115 }
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
116 }
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
117
1275
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
118 if (optimizeLevel >= 2 && !disableLangSpecificPasses
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
119 && !disableSimplifyRuntimeCalls) {
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
120 pm.add(createSimplifyDRuntimeCalls());
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
121 }
bedf0bfb8fdb Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents: 1268
diff changeset
122
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
123 // -O3
1170
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
124 if (optimizeLevel >= 3)
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
125 {
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
126 pm.add(createArgumentPromotionPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
127 pm.add(createTailDuplicationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
128 pm.add(createInstructionCombiningPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
129 pm.add(createCFGSimplificationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
130 pm.add(createScalarReplAggregatesPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
131 pm.add(createInstructionCombiningPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
132 pm.add(createCondPropagationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
133
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
134 pm.add(createTailCallEliminationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
135 pm.add(createCFGSimplificationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
136 pm.add(createReassociatePass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
137 pm.add(createLoopRotatePass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
138 pm.add(createLICMPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
139 pm.add(createLoopUnswitchPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
140 pm.add(createInstructionCombiningPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
141 pm.add(createIndVarSimplifyPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
142 pm.add(createLoopUnrollPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
143 pm.add(createInstructionCombiningPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
144 pm.add(createGVNPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
145 pm.add(createSCCPPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
146
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
147 pm.add(createInstructionCombiningPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
148 pm.add(createCondPropagationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
149
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
150 pm.add(createDeadStoreEliminationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
151 pm.add(createAggressiveDCEPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
152 pm.add(createCFGSimplificationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
153 pm.add(createSimplifyLibCallsPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
154 pm.add(createDeadTypeEliminationPass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
155 pm.add(createConstantMergePass());
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
156 }
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
157
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
158 // 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
159 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
160
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
161 //////////////////////////////////////////////////////////////////////////////////////////
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
162 // 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
163 // 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
164 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
165 {
1268
4ae3b3ceea11 Remove a bit of code duplication.
Frits van Bommel <fvbommel wxs.nl>
parents: 1267
diff changeset
166 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
167 return false;
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
168
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
169 PassManager pm;
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
170 pm.add(new TargetData(m));
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
171
1175
cc1efa23030a Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents: 1172
diff changeset
172 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
173
1171
461a85f0db31 Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents: 1170
diff changeset
174 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
175 ? optimizeLevel.getPosition()
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
176 : enableInlining.getPosition();
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
177
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
178 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
179 // 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
180 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
181 addPassesForOptLevel(pm);
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
182 optimize = false;
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
183 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
184
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
185 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
186 if (PassInfo::NormalCtor_t ctor = pass->getNormalCtor()) {
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
187 pm.add(ctor());
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
188 } else {
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
189 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
190 if (arg)
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
191 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
192 else
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
193 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
194 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
195 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
196 }
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
197 // 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
198 if (optimize)
e40c65bd8c5d Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents: 663
diff changeset
199 addPassesForOptLevel(pm);
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
200
131
5825d48b27d1 [svn r135] * Merged DMD 1.025 *
lindquist
parents: 129
diff changeset
201 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
202 return true;
125
c42d245468ea [svn r129] Started AA literals.
lindquist
parents:
diff changeset
203 }