Mercurial > projects > ldc
annotate gen/optimizer.cpp @ 1287:6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
to generate asm for code with metadata globals.
This new pass is used as a workaround: it strips metadata from the module before
it reaches the code generator.
Obviously, this is disabled if LLVM doesn't support metadata.
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Sat, 02 May 2009 12:19:43 +0200 |
parents | 91d9386d4a5a |
children | e109e4031e8a |
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 | 6 #include "llvm/PassManager.h" |
7 #include "llvm/LinkAllPasses.h" | |
8 #include "llvm/Analysis/LoopPass.h" | |
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 | 14 |
15 using namespace llvm; | |
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 | 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 | 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 |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
48 static cl::opt<bool> |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
49 disableGCToStack("disable-gc2stack", |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
50 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
|
51 cl::ZeroOrMore); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
52 |
1287
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
53 // Not recommended; metadata currently triggers an assert in the backend... |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
54 static cl::opt<bool> |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
55 disableStripMetaData("disable-strip-metadata", |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
56 cl::desc("Disable default metadata stripping (not recommended)"), |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
57 cl::ZeroOrMore); |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
58 |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
59 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
|
60 enableInlining("inlining", |
1275
bedf0bfb8fdb
Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents:
1268
diff
changeset
|
61 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
|
62 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
|
63 |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
64 // 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
|
65 // optimization passes. |
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
66 // 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
|
67 bool doInline() { |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
68 return enableInlining == cl::BOU_TRUE |
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
69 || (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
|
70 } |
131 | 71 |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
72 // 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
|
73 int optLevel() { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
74 return optimizeLevel; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
75 } |
125 | 76 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
77 bool optimize() { |
1267 | 78 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
|
79 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
80 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
81 // 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
|
82 // 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
|
83 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
|
84 // -O1 |
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
85 if (optimizeLevel >= 1) |
131 | 86 { |
87 //pm.add(createStripDeadPrototypesPass()); | |
88 pm.add(createGlobalDCEPass()); | |
125 | 89 pm.add(createRaiseAllocationsPass()); |
90 pm.add(createCFGSimplificationPass()); | |
1219
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
91 if (optimizeLevel == 1) |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
92 pm.add(createPromoteMemoryToRegisterPass()); |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
93 else |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
94 pm.add(createScalarReplAggregatesPass()); |
129
8096ba7082db
[svn r133] Fixed some problems with inlining not happening :P
lindquist
parents:
125
diff
changeset
|
95 pm.add(createGlobalOptimizerPass()); |
8096ba7082db
[svn r133] Fixed some problems with inlining not happening :P
lindquist
parents:
125
diff
changeset
|
96 pm.add(createGlobalDCEPass()); |
125 | 97 } |
98 | |
131 | 99 // -O2 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
100 if (optimizeLevel >= 2) |
125 | 101 { |
102 pm.add(createIPConstantPropagationPass()); | |
103 pm.add(createDeadArgEliminationPass()); | |
104 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
|
105 pm.add(createCFGSimplificationPass()); |
125 | 106 pm.add(createPruneEHPass()); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
107 if (!disableLangSpecificPasses && !disableGCToStack) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
108 pm.add(createGarbageCollect2Stack()); |
125 | 109 } |
110 | |
131 | 111 // -inline |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
112 if (doInline()) { |
125 | 113 pm.add(createFunctionInliningPass()); |
1219
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
114 |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
115 if (optimizeLevel >= 2) { |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
116 // Run some optimizations to clean up after inlining. |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
117 pm.add(createScalarReplAggregatesPass()); |
1219
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
118 pm.add(createInstructionCombiningPass()); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
119 if (!disableLangSpecificPasses && !disableGCToStack) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
120 pm.add(createGarbageCollect2Stack()); |
1219
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
121 |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
122 // Inline again, to catch things like foreach delegates |
1220 | 123 // 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
|
124 // known during the first inliner pass. |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
125 pm.add(createFunctionInliningPass()); |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
126 |
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
127 // Run clean-up again. |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
128 pm.add(createScalarReplAggregatesPass()); |
1219
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
129 pm.add(createInstructionCombiningPass()); |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
130 if (!disableLangSpecificPasses && !disableGCToStack) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
131 pm.add(createGarbageCollect2Stack()); |
1219
a0844cc67840
Tweak some optimizations.
Frits van Bommel <fvbommel wxs.nl>
parents:
1175
diff
changeset
|
132 } |
125 | 133 } |
134 | |
1285
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
135 if (optimizeLevel >= 2 && !disableLangSpecificPasses) { |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
136 if (!disableSimplifyRuntimeCalls) |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
137 pm.add(createSimplifyDRuntimeCalls()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
138 |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
139 if (!disableGCToStack) { |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
140 // Run some clean-up after the last GC to stack promotion pass. |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
141 pm.add(createScalarReplAggregatesPass()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
142 pm.add(createInstructionCombiningPass()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
143 pm.add(createCFGSimplificationPass()); |
91d9386d4a5a
Implement another D-specific pass: -dgc2stack
Frits van Bommel <fvbommel wxs.nl>
parents:
1281
diff
changeset
|
144 } |
1275
bedf0bfb8fdb
Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents:
1268
diff
changeset
|
145 } |
1287
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
146 #ifdef USE_METADATA |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
147 if (!disableStripMetaData) { |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
148 // This one is purposely not disabled by disableLangSpecificPasses |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
149 // because the code generator will assert if it's not used. |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
150 pm.add(createStripMetaData()); |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
151 } |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
152 #endif |
1275
bedf0bfb8fdb
Implement first D-specific optimization pass: -simplify-drtcalls.
Frits van Bommel <fvbommel wxs.nl>
parents:
1268
diff
changeset
|
153 |
131 | 154 // -O3 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
155 if (optimizeLevel >= 3) |
125 | 156 { |
157 pm.add(createArgumentPromotionPass()); | |
158 pm.add(createTailDuplicationPass()); | |
159 pm.add(createInstructionCombiningPass()); | |
160 pm.add(createCFGSimplificationPass()); | |
161 pm.add(createScalarReplAggregatesPass()); | |
162 pm.add(createInstructionCombiningPass()); | |
163 pm.add(createCondPropagationPass()); | |
164 | |
165 pm.add(createTailCallEliminationPass()); | |
166 pm.add(createCFGSimplificationPass()); | |
167 pm.add(createReassociatePass()); | |
168 pm.add(createLoopRotatePass()); | |
169 pm.add(createLICMPass()); | |
170 pm.add(createLoopUnswitchPass()); | |
171 pm.add(createInstructionCombiningPass()); | |
172 pm.add(createIndVarSimplifyPass()); | |
173 pm.add(createLoopUnrollPass()); | |
174 pm.add(createInstructionCombiningPass()); | |
175 pm.add(createGVNPass()); | |
176 pm.add(createSCCPPass()); | |
177 | |
178 pm.add(createInstructionCombiningPass()); | |
179 pm.add(createCondPropagationPass()); | |
180 | |
181 pm.add(createDeadStoreEliminationPass()); | |
182 pm.add(createAggressiveDCEPass()); | |
183 pm.add(createCFGSimplificationPass()); | |
184 pm.add(createSimplifyLibCallsPass()); | |
185 pm.add(createDeadTypeEliminationPass()); | |
186 pm.add(createConstantMergePass()); | |
187 } | |
188 | |
131 | 189 // 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
|
190 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
191 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
192 ////////////////////////////////////////////////////////////////////////////////////////// |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
193 // 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
|
194 // 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
|
195 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
|
196 { |
1287
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
197 if (!optimize()) { |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
198 #ifdef USE_METADATA |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
199 if (!disableStripMetaData) { |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
200 // This one always needs to run if metadata is generated, because |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
201 // the code generator will assert if it's not used. |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
202 ModulePass* stripMD = createStripMetaData(); |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
203 stripMD->runOnModule(*m); |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
204 delete stripMD; |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
205 } |
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
206 #endif |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
207 return false; |
1287
6c8af78364f5
There's an issue with LLVM metadata support; it triggers an assert when trying
Frits van Bommel <fvbommel wxs.nl>
parents:
1285
diff
changeset
|
208 } |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
209 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
210 PassManager pm; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
211 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
|
212 |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
213 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
|
214 |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
215 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
|
216 ? optimizeLevel.getPosition() |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
217 : enableInlining.getPosition(); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
218 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
219 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
|
220 // 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
|
221 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
|
222 addPassesForOptLevel(pm); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
223 optimize = false; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
224 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
225 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
226 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
|
227 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
|
228 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
|
229 } else { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
230 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
|
231 if (arg) |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
232 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
|
233 else |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
234 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
|
235 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
|
236 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
237 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
238 // 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
|
239 if (optimize) |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
240 addPassesForOptLevel(pm); |
125 | 241 |
131 | 242 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
|
243 return true; |
125 | 244 } |