blob: 73f2902d9a060c0c5582ef33a412242c62e8090e [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Target/TargetMachine.h"
15#include "llvm/PassManager.h"
16#include "llvm/Pass.h"
17#include "llvm/Assembly/PrintModulePass.h"
18#include "llvm/Analysis/LoopPass.h"
19#include "llvm/CodeGen/Passes.h"
Gordon Henriksen36464772008-01-07 01:33:09 +000020#include "llvm/CodeGen/Collector.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesen85535762008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Transforms/Scalar.h"
24#include "llvm/Support/CommandLine.h"
25using namespace llvm;
26
27static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
28 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
29static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
30 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng77547212007-07-20 21:56:13 +000031static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
32 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen36464772008-01-07 01:33:09 +000033static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
34 cl::desc("Dump garbage collector data"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035
Chris Lattner39882262008-01-04 07:36:53 +000036// Hidden options to help debugging
37static cl::opt<bool>
38EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
39 cl::desc("Perform sinking on machine code"));
Bill Wendling4aab7ae2008-01-04 08:11:03 +000040static cl::opt<bool>
Evan Cheng14f8a502008-06-04 09:18:41 +000041EnableStackColoring("stack-coloring",
42 cl::init(true), cl::Hidden,
43 cl::desc("Perform stack slot coloring"));
Evan Cheng7e29ba02008-02-28 23:29:57 +000044static cl::opt<bool>
Evan Cheng14f8a502008-06-04 09:18:41 +000045EnableLICM("machine-licm",
46 cl::init(false), cl::Hidden,
47 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattner39882262008-01-04 07:36:53 +000048
Chris Lattnere06d8eb2008-01-14 19:00:06 +000049// When this works it will be on by default.
50static cl::opt<bool>
51DisablePostRAScheduler("disable-post-RA-scheduler",
52 cl::desc("Disable scheduling after register allocation"),
53 cl::init(true));
54
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000056LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 std::ostream &Out,
58 CodeGenFileType FileType,
59 bool Fast) {
60 // Standard LLVM-Level Passes.
61
62 // Run loop strength reduction before anything else.
63 if (!Fast) {
64 PM.add(createLoopStrengthReducePass(getTargetLowering()));
65 if (PrintLSR)
Dan Gohman35a54152008-03-25 21:38:12 +000066 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 }
68
Gordon Henriksen36464772008-01-07 01:33:09 +000069 PM.add(createGCLoweringPass());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070
Dale Johannesen85535762008-04-02 00:25:04 +000071 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +000072 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073
74 // Make sure that no unreachable blocks are instruction selected.
75 PM.add(createUnreachableBlockEliminationPass());
76
77 if (!Fast)
78 PM.add(createCodeGenPreparePass(getTargetLowering()));
79
80 if (PrintISelInput)
Dan Gohman35a54152008-03-25 21:38:12 +000081 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000082 &cerr));
83
84 // Ask the target for an isel.
85 if (addInstSelector(PM, Fast))
86 return FileModel::Error;
87
88 // Print the instruction selected machine code...
89 if (PrintMachineCode)
90 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +000091
Evan Cheng14f8a502008-06-04 09:18:41 +000092 if (EnableLICM)
Bill Wendling4aab7ae2008-01-04 08:11:03 +000093 PM.add(createMachineLICMPass());
Chris Lattner39882262008-01-04 07:36:53 +000094
95 if (EnableSinking)
96 PM.add(createMachineSinkingPass());
Bill Wendlingb958b0d2007-12-07 21:42:31 +000097
Anton Korobeynikov339d2452008-04-23 18:22:28 +000098 // Run pre-ra passes.
99 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
100 PM.add(createMachineFunctionPrinterPass(cerr));
101
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 // Perform register allocation to convert to a concrete x86 representation
103 PM.add(createRegisterAllocator());
104
Evan Cheng14f8a502008-06-04 09:18:41 +0000105 // Perform stack slot coloring.
106 if (EnableStackColoring)
107 PM.add(createStackSlotColoringPass());
108
109 if (PrintMachineCode) // Print the register-allocated code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 PM.add(createMachineFunctionPrinterPass(cerr));
Evan Cheng14f8a502008-06-04 09:18:41 +0000111
112 // Run post-ra passes.
113 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
114 PM.add(createMachineFunctionPrinterPass(cerr));
115
Christopher Lambed379732007-07-27 07:36:14 +0000116 PM.add(createLowerSubregsPass());
117
118 if (PrintMachineCode) // Print the subreg lowered code
119 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 // Insert prolog/epilog code. Eliminate abstract frame index references...
122 PM.add(createPrologEpilogCodeInserter());
123
Evan Cheng14f8a502008-06-04 09:18:41 +0000124 if (PrintMachineCode)
125 PM.add(createMachineFunctionPrinterPass(cerr));
126
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000127 // Second pass scheduler.
Chris Lattnere06d8eb2008-01-14 19:00:06 +0000128 if (!Fast && !DisablePostRAScheduler)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129 PM.add(createPostRAScheduler());
130
131 // Branch folding must be run after regalloc and prolog/epilog insertion.
132 if (!Fast)
133 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000134
Gordon Henriksen36464772008-01-07 01:33:09 +0000135 PM.add(createGCMachineCodeAnalysisPass());
136 if (PrintMachineCode)
137 PM.add(createMachineFunctionPrinterPass(cerr));
138
139 if (PrintGCInfo)
140 PM.add(createCollectorMetadataPrinter(*cerr));
141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 // Fold redundant debug labels.
143 PM.add(createDebugLabelFoldingPass());
144
145 if (PrintMachineCode) // Print the register-allocated code
146 PM.add(createMachineFunctionPrinterPass(cerr));
147
148 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
149 PM.add(createMachineFunctionPrinterPass(cerr));
150
Evan Cheng14f8a502008-06-04 09:18:41 +0000151 if (!Fast && !OptimizeForSize)
Evan Cheng7e29ba02008-02-28 23:29:57 +0000152 PM.add(createLoopAlignerPass());
153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 switch (FileType) {
155 default:
156 break;
157 case TargetMachine::AssemblyFile:
158 if (addAssemblyEmitter(PM, Fast, Out))
159 return FileModel::Error;
160 return FileModel::AsmFile;
161 case TargetMachine::ObjectFile:
162 if (getMachOWriterInfo())
163 return FileModel::MachOFile;
164 else if (getELFWriterInfo())
165 return FileModel::ElfFile;
166 }
167
168 return FileModel::Error;
169}
170
171/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
172/// be split up (e.g., to add an object writer pass), this method can be used to
173/// finish up adding passes to emit the file, if necessary.
Dan Gohmane34aa772008-03-11 22:29:46 +0000174bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 MachineCodeEmitter *MCE,
176 bool Fast) {
177 if (MCE)
Evan Cheng77547212007-07-20 21:56:13 +0000178 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen36464772008-01-07 01:33:09 +0000179
180 PM.add(createCollectorMetadataDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181
182 // Delete machine code for this function
183 PM.add(createMachineCodeDeleter());
184
185 return false; // success!
186}
187
188/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
189/// get machine code emitted. This uses a MachineCodeEmitter object to handle
190/// actually outputting the machine code and resolving things like the address
191/// of functions. This method should returns true if machine code emission is
192/// not supported.
193///
Dan Gohmane34aa772008-03-11 22:29:46 +0000194bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 MachineCodeEmitter &MCE,
196 bool Fast) {
197 // Standard LLVM-Level Passes.
198
199 // Run loop strength reduction before anything else.
200 if (!Fast) {
201 PM.add(createLoopStrengthReducePass(getTargetLowering()));
202 if (PrintLSR)
Dan Gohman35a54152008-03-25 21:38:12 +0000203 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 }
205
Gordon Henriksen36464772008-01-07 01:33:09 +0000206 PM.add(createGCLoweringPass());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207
Dale Johannesen85535762008-04-02 00:25:04 +0000208 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +0000209 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
211 // Make sure that no unreachable blocks are instruction selected.
212 PM.add(createUnreachableBlockEliminationPass());
213
214 if (!Fast)
215 PM.add(createCodeGenPreparePass(getTargetLowering()));
216
217 if (PrintISelInput)
Dan Gohman35a54152008-03-25 21:38:12 +0000218 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 &cerr));
220
221 // Ask the target for an isel.
222 if (addInstSelector(PM, Fast))
223 return true;
224
225 // Print the instruction selected machine code...
226 if (PrintMachineCode)
227 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000228
Evan Cheng14f8a502008-06-04 09:18:41 +0000229 if (EnableLICM)
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000230 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000231
232 if (EnableSinking)
233 PM.add(createMachineSinkingPass());
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000234
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000235 // Run pre-ra passes.
236 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
237 PM.add(createMachineFunctionPrinterPass(cerr));
238
Evan Cheng14f8a502008-06-04 09:18:41 +0000239 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000241
242 // Perform stack slot coloring.
243 if (EnableStackColoring)
244 PM.add(createStackSlotColoringPass());
245
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 if (PrintMachineCode)
247 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambed379732007-07-27 07:36:14 +0000248
Evan Cheng14f8a502008-06-04 09:18:41 +0000249 // Run post-ra passes.
250 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
251 PM.add(createMachineFunctionPrinterPass(cerr));
252
253 if (PrintMachineCode) // Print the register-allocated code
254 PM.add(createMachineFunctionPrinterPass(cerr));
255
Christopher Lambed379732007-07-27 07:36:14 +0000256 PM.add(createLowerSubregsPass());
257
258 if (PrintMachineCode) // Print the subreg lowered code
259 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 // Insert prolog/epilog code. Eliminate abstract frame index references...
262 PM.add(createPrologEpilogCodeInserter());
263
Evan Cheng14f8a502008-06-04 09:18:41 +0000264 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 PM.add(createMachineFunctionPrinterPass(cerr));
266
267 // Second pass scheduler.
268 if (!Fast)
269 PM.add(createPostRAScheduler());
270
271 // Branch folding must be run after regalloc and prolog/epilog insertion.
272 if (!Fast)
273 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000274
Gordon Henriksen36464772008-01-07 01:33:09 +0000275 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000276
Gordon Henriksen36464772008-01-07 01:33:09 +0000277 if (PrintMachineCode)
278 PM.add(createMachineFunctionPrinterPass(cerr));
279
280 if (PrintGCInfo)
281 PM.add(createCollectorMetadataPrinter(*cerr));
282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
284 PM.add(createMachineFunctionPrinterPass(cerr));
285
Evan Cheng77547212007-07-20 21:56:13 +0000286 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287
Gordon Henriksen36464772008-01-07 01:33:09 +0000288 PM.add(createCollectorMetadataDeleter());
289
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 // Delete machine code for this function
291 PM.add(createMachineCodeDeleter());
292
293 return false; // success!
294}