Mercurial > projects > ldc
annotate gen/optimizer.cpp @ 1175:cc1efa23030a
Enable inlining by default for -O3+.
author | Frits van Bommel <fvbommel wxs.nl> |
---|---|
date | Sun, 29 Mar 2009 19:38:59 +0200 |
parents | b3887714b735 |
children | a0844cc67840 |
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 |
125 | 4 #include "llvm/PassManager.h" |
5 #include "llvm/LinkAllPasses.h" | |
6 #include "llvm/Analysis/LoopPass.h" | |
7 #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
|
8 #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
|
9 #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
|
10 |
1172
b3887714b735
Small cleanup: remove mars.h #include
Frits van Bommel <fvbommel wxs.nl>
parents:
1171
diff
changeset
|
11 #include "root.h" // error() |
125 | 12 |
13 using namespace llvm; | |
14 | |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
15 // 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
|
16 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
|
17 passList( |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
18 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
|
19 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
|
20 ); |
125 | 21 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
22 static cl::opt<char> optimizeLevel( |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
23 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
|
24 cl::ZeroOrMore, |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
25 cl::values( |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
26 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
|
27 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
|
28 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
|
29 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
|
30 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
|
31 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
|
32 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
|
33 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
|
34 cl::init(0)); |
125 | 35 |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
36 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
|
37 enableInlining("inlining", |
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
38 cl::desc("(*) Enable function inlining (in -O<N>, if given)"), |
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
39 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
|
40 |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
41 // 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
|
42 // optimization passes. |
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
43 // 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
|
44 bool doInline() { |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
45 return enableInlining == cl::BOU_TRUE |
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
46 || (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
|
47 } |
131 | 48 |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
49 // 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
|
50 int optLevel() { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
51 return optimizeLevel; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
52 } |
125 | 53 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
54 bool optimize() { |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
55 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
|
56 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
57 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
58 // 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
|
59 // 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
|
60 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
|
61 // -O1 |
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
62 if (optimizeLevel >= 1) |
131 | 63 { |
64 //pm.add(createStripDeadPrototypesPass()); | |
65 pm.add(createGlobalDCEPass()); | |
125 | 66 pm.add(createRaiseAllocationsPass()); |
67 pm.add(createCFGSimplificationPass()); | |
68 pm.add(createPromoteMemoryToRegisterPass()); | |
129
8096ba7082db
[svn r133] Fixed some problems with inlining not happening :P
lindquist
parents:
125
diff
changeset
|
69 pm.add(createGlobalOptimizerPass()); |
8096ba7082db
[svn r133] Fixed some problems with inlining not happening :P
lindquist
parents:
125
diff
changeset
|
70 pm.add(createGlobalDCEPass()); |
125 | 71 } |
72 | |
131 | 73 // -O2 |
1170
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
74 if (optimizeLevel >= 2) |
125 | 75 { |
76 pm.add(createIPConstantPropagationPass()); | |
77 pm.add(createDeadArgEliminationPass()); | |
78 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
|
79 pm.add(createCFGSimplificationPass()); |
125 | 80 pm.add(createPruneEHPass()); |
81 } | |
82 | |
131 | 83 // -inline |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
84 if (doInline()) { |
125 | 85 pm.add(createFunctionInliningPass()); |
86 } | |
87 | |
131 | 88 // -O3 |
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 >= 3) |
125 | 90 { |
91 pm.add(createArgumentPromotionPass()); | |
92 pm.add(createTailDuplicationPass()); | |
93 pm.add(createInstructionCombiningPass()); | |
94 pm.add(createCFGSimplificationPass()); | |
95 pm.add(createScalarReplAggregatesPass()); | |
96 pm.add(createInstructionCombiningPass()); | |
97 pm.add(createCondPropagationPass()); | |
98 | |
99 pm.add(createTailCallEliminationPass()); | |
100 pm.add(createCFGSimplificationPass()); | |
101 pm.add(createReassociatePass()); | |
102 pm.add(createLoopRotatePass()); | |
103 pm.add(createLICMPass()); | |
104 pm.add(createLoopUnswitchPass()); | |
105 pm.add(createInstructionCombiningPass()); | |
106 pm.add(createIndVarSimplifyPass()); | |
107 pm.add(createLoopUnrollPass()); | |
108 pm.add(createInstructionCombiningPass()); | |
109 pm.add(createGVNPass()); | |
110 pm.add(createSCCPPass()); | |
111 | |
112 pm.add(createInstructionCombiningPass()); | |
113 pm.add(createCondPropagationPass()); | |
114 | |
115 pm.add(createDeadStoreEliminationPass()); | |
116 pm.add(createAggressiveDCEPass()); | |
117 pm.add(createCFGSimplificationPass()); | |
118 pm.add(createSimplifyLibCallsPass()); | |
119 pm.add(createDeadTypeEliminationPass()); | |
120 pm.add(createConstantMergePass()); | |
121 } | |
122 | |
131 | 123 // 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
|
124 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
125 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
126 ////////////////////////////////////////////////////////////////////////////////////////// |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
127 // 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
|
128 // 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
|
129 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
|
130 { |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
131 if (!doInline() && optimizeLevel == 0 && 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
|
132 return false; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
133 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
134 PassManager pm; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
135 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
|
136 |
1175
cc1efa23030a
Enable inlining by default for -O3+.
Frits van Bommel <fvbommel wxs.nl>
parents:
1172
diff
changeset
|
137 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
|
138 |
1171
461a85f0db31
Change meaning of optimization levels: -O0 now means 'no optimization' like with
Frits van Bommel <fvbommel wxs.nl>
parents:
1170
diff
changeset
|
139 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
|
140 ? optimizeLevel.getPosition() |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
141 : enableInlining.getPosition(); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
142 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
143 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
|
144 // 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
|
145 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
|
146 addPassesForOptLevel(pm); |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
147 optimize = false; |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
148 } |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
149 |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
150 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
|
151 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
|
152 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
|
153 } else { |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
154 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
|
155 if (arg) |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
156 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
|
157 else |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
158 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
|
159 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
|
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 // 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
|
163 if (optimize) |
e40c65bd8c5d
Allow specific optimization passes to be requested from the command line.
Frits van Bommel <fvbommel wxs.nl>
parents:
663
diff
changeset
|
164 addPassesForOptLevel(pm); |
125 | 165 |
131 | 166 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
|
167 return true; |
125 | 168 } |