blob: 938e1ae9cc1a09d2f94615634b884a60e2bb2bf2 [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 Henriksenf194af22008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.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 +000041EnableLICM("machine-licm",
42 cl::init(false), cl::Hidden,
43 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattner39882262008-01-04 07:36:53 +000044
Chris Lattnere06d8eb2008-01-14 19:00:06 +000045// When this works it will be on by default.
46static cl::opt<bool>
47DisablePostRAScheduler("disable-post-RA-scheduler",
48 cl::desc("Disable scheduling after register allocation"),
49 cl::init(true));
50
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000052LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 std::ostream &Out,
54 CodeGenFileType FileType,
55 bool Fast) {
56 // Standard LLVM-Level Passes.
57
58 // Run loop strength reduction before anything else.
59 if (!Fast) {
60 PM.add(createLoopStrengthReducePass(getTargetLowering()));
61 if (PrintLSR)
Dan Gohman35a54152008-03-25 21:38:12 +000062 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 }
64
Gordon Henriksen36464772008-01-07 01:33:09 +000065 PM.add(createGCLoweringPass());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066
Dale Johannesen85535762008-04-02 00:25:04 +000067 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +000068 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000069
70 // Make sure that no unreachable blocks are instruction selected.
71 PM.add(createUnreachableBlockEliminationPass());
72
73 if (!Fast)
74 PM.add(createCodeGenPreparePass(getTargetLowering()));
75
76 if (PrintISelInput)
Dan Gohman35a54152008-03-25 21:38:12 +000077 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 &cerr));
79
80 // Ask the target for an isel.
81 if (addInstSelector(PM, Fast))
82 return FileModel::Error;
83
84 // Print the instruction selected machine code...
85 if (PrintMachineCode)
86 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +000087
Evan Cheng14f8a502008-06-04 09:18:41 +000088 if (EnableLICM)
Bill Wendling4aab7ae2008-01-04 08:11:03 +000089 PM.add(createMachineLICMPass());
Chris Lattner39882262008-01-04 07:36:53 +000090
91 if (EnableSinking)
92 PM.add(createMachineSinkingPass());
Bill Wendlingb958b0d2007-12-07 21:42:31 +000093
Anton Korobeynikov339d2452008-04-23 18:22:28 +000094 // Run pre-ra passes.
95 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
96 PM.add(createMachineFunctionPrinterPass(cerr));
97
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 // Perform register allocation to convert to a concrete x86 representation
99 PM.add(createRegisterAllocator());
100
Evan Cheng14f8a502008-06-04 09:18:41 +0000101 // Perform stack slot coloring.
Evan Cheng2ea55502008-06-30 22:33:16 +0000102 if (!Fast)
103 PM.add(createStackSlotColoringPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000104
105 if (PrintMachineCode) // Print the register-allocated code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 PM.add(createMachineFunctionPrinterPass(cerr));
Evan Cheng14f8a502008-06-04 09:18:41 +0000107
108 // Run post-ra passes.
109 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
110 PM.add(createMachineFunctionPrinterPass(cerr));
111
Christopher Lambed379732007-07-27 07:36:14 +0000112 PM.add(createLowerSubregsPass());
113
114 if (PrintMachineCode) // Print the subreg lowered code
115 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 // Insert prolog/epilog code. Eliminate abstract frame index references...
118 PM.add(createPrologEpilogCodeInserter());
119
Evan Cheng14f8a502008-06-04 09:18:41 +0000120 if (PrintMachineCode)
121 PM.add(createMachineFunctionPrinterPass(cerr));
122
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123 // Second pass scheduler.
Chris Lattnere06d8eb2008-01-14 19:00:06 +0000124 if (!Fast && !DisablePostRAScheduler)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000125 PM.add(createPostRAScheduler());
126
127 // Branch folding must be run after regalloc and prolog/epilog insertion.
128 if (!Fast)
129 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000130
Gordon Henriksen36464772008-01-07 01:33:09 +0000131 PM.add(createGCMachineCodeAnalysisPass());
132 if (PrintMachineCode)
133 PM.add(createMachineFunctionPrinterPass(cerr));
134
135 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000136 PM.add(createGCInfoPrinter(*cerr));
Gordon Henriksen36464772008-01-07 01:33:09 +0000137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 // Fold redundant debug labels.
139 PM.add(createDebugLabelFoldingPass());
140
141 if (PrintMachineCode) // Print the register-allocated code
142 PM.add(createMachineFunctionPrinterPass(cerr));
143
144 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
145 PM.add(createMachineFunctionPrinterPass(cerr));
146
Evan Cheng14f8a502008-06-04 09:18:41 +0000147 if (!Fast && !OptimizeForSize)
Evan Cheng7e29ba02008-02-28 23:29:57 +0000148 PM.add(createLoopAlignerPass());
149
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 switch (FileType) {
151 default:
152 break;
153 case TargetMachine::AssemblyFile:
154 if (addAssemblyEmitter(PM, Fast, Out))
155 return FileModel::Error;
156 return FileModel::AsmFile;
157 case TargetMachine::ObjectFile:
158 if (getMachOWriterInfo())
159 return FileModel::MachOFile;
160 else if (getELFWriterInfo())
161 return FileModel::ElfFile;
162 }
163
164 return FileModel::Error;
165}
166
167/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
168/// be split up (e.g., to add an object writer pass), this method can be used to
169/// finish up adding passes to emit the file, if necessary.
Dan Gohmane34aa772008-03-11 22:29:46 +0000170bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000171 MachineCodeEmitter *MCE,
172 bool Fast) {
173 if (MCE)
Evan Cheng77547212007-07-20 21:56:13 +0000174 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen36464772008-01-07 01:33:09 +0000175
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000176 PM.add(createGCInfoDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177
178 // Delete machine code for this function
179 PM.add(createMachineCodeDeleter());
180
181 return false; // success!
182}
183
184/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
185/// get machine code emitted. This uses a MachineCodeEmitter object to handle
186/// actually outputting the machine code and resolving things like the address
187/// of functions. This method should returns true if machine code emission is
188/// not supported.
189///
Dan Gohmane34aa772008-03-11 22:29:46 +0000190bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 MachineCodeEmitter &MCE,
192 bool Fast) {
193 // Standard LLVM-Level Passes.
194
195 // Run loop strength reduction before anything else.
196 if (!Fast) {
197 PM.add(createLoopStrengthReducePass(getTargetLowering()));
198 if (PrintLSR)
Dan Gohman35a54152008-03-25 21:38:12 +0000199 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 }
201
Gordon Henriksen36464772008-01-07 01:33:09 +0000202 PM.add(createGCLoweringPass());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203
Dale Johannesen85535762008-04-02 00:25:04 +0000204 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +0000205 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206
207 // Make sure that no unreachable blocks are instruction selected.
208 PM.add(createUnreachableBlockEliminationPass());
209
210 if (!Fast)
211 PM.add(createCodeGenPreparePass(getTargetLowering()));
212
213 if (PrintISelInput)
Dan Gohman35a54152008-03-25 21:38:12 +0000214 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 &cerr));
216
217 // Ask the target for an isel.
218 if (addInstSelector(PM, Fast))
219 return true;
220
221 // Print the instruction selected machine code...
222 if (PrintMachineCode)
223 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000224
Evan Cheng14f8a502008-06-04 09:18:41 +0000225 if (EnableLICM)
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000226 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000227
228 if (EnableSinking)
229 PM.add(createMachineSinkingPass());
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000230
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000231 // Run pre-ra passes.
232 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
233 PM.add(createMachineFunctionPrinterPass(cerr));
234
Evan Cheng14f8a502008-06-04 09:18:41 +0000235 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000236 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000237
238 // Perform stack slot coloring.
Evan Cheng2ea55502008-06-30 22:33:16 +0000239 if (!Fast)
240 PM.add(createStackSlotColoringPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000241
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 if (PrintMachineCode)
243 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambed379732007-07-27 07:36:14 +0000244
Evan Cheng14f8a502008-06-04 09:18:41 +0000245 // Run post-ra passes.
246 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
247 PM.add(createMachineFunctionPrinterPass(cerr));
248
249 if (PrintMachineCode) // Print the register-allocated code
250 PM.add(createMachineFunctionPrinterPass(cerr));
251
Christopher Lambed379732007-07-27 07:36:14 +0000252 PM.add(createLowerSubregsPass());
253
254 if (PrintMachineCode) // Print the subreg lowered code
255 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 // Insert prolog/epilog code. Eliminate abstract frame index references...
258 PM.add(createPrologEpilogCodeInserter());
259
Evan Cheng14f8a502008-06-04 09:18:41 +0000260 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000261 PM.add(createMachineFunctionPrinterPass(cerr));
262
263 // Second pass scheduler.
264 if (!Fast)
265 PM.add(createPostRAScheduler());
266
267 // Branch folding must be run after regalloc and prolog/epilog insertion.
268 if (!Fast)
269 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000270
Gordon Henriksen36464772008-01-07 01:33:09 +0000271 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000272
Gordon Henriksen36464772008-01-07 01:33:09 +0000273 if (PrintMachineCode)
274 PM.add(createMachineFunctionPrinterPass(cerr));
275
276 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000277 PM.add(createGCInfoPrinter(*cerr));
Gordon Henriksen36464772008-01-07 01:33:09 +0000278
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
280 PM.add(createMachineFunctionPrinterPass(cerr));
281
Evan Cheng77547212007-07-20 21:56:13 +0000282 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000284 PM.add(createGCInfoDeleter());
Gordon Henriksen36464772008-01-07 01:33:09 +0000285
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000286 // Delete machine code for this function
287 PM.add(createMachineCodeDeleter());
288
289 return false; // success!
290}