blob: de293651a655346810657e3de083fd6ead4aefb9 [file] [log] [blame]
Chris Lattnera916db12006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnera916db12006-09-04 04:16:09 +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"
Chris Lattnerbafc8372007-03-31 00:24:43 +000017#include "llvm/Assembly/PrintModulePass.h"
Devang Patelb0743b52007-03-06 21:14:09 +000018#include "llvm/Analysis/LoopPass.h"
Chris Lattnera916db12006-09-04 04:16:09 +000019#include "llvm/CodeGen/Passes.h"
Gordon Henriksenbcef14d2008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.h"
Chris Lattnera916db12006-09-04 04:16:09 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesenfd967cf2008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbarf060b4d2009-07-15 23:48:37 +000023#include "llvm/Target/TargetRegistry.h"
Chris Lattnera916db12006-09-04 04:16:09 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000025#include "llvm/Support/CommandLine.h"
Daniel Dunbarf060b4d2009-07-15 23:48:37 +000026#include "llvm/Support/ErrorHandling.h"
David Greenea31f96c2009-07-14 20:18:05 +000027#include "llvm/Support/FormattedStream.h"
Chris Lattnera916db12006-09-04 04:16:09 +000028using namespace llvm;
29
Dan Gohmanb8e69f12008-09-25 01:14:49 +000030namespace llvm {
31 bool EnableFastISel;
32}
33
Chris Lattner37228f62007-06-19 05:47:49 +000034static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
35 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
36static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
37 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng9d5df0a2007-07-20 21:56:13 +000038static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
39 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen2d684b12008-01-07 01:33:09 +000040static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
41 cl::desc("Dump garbage collector data"));
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +000042static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
43 cl::desc("Verify generated machine code"),
44 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Chris Lattner37228f62007-06-19 05:47:49 +000045
Chris Lattner99471842008-01-14 19:00:06 +000046// When this works it will be on by default.
47static cl::opt<bool>
48DisablePostRAScheduler("disable-post-RA-scheduler",
49 cl::desc("Disable scheduling after register allocation"),
50 cl::init(true));
51
Dan Gohman3b88f102008-10-01 20:39:19 +000052// Enable or disable FastISel. Both options are needed, because
53// FastISel is enabled by default with -fast, and we wish to be
54// able to enable or disable fast-isel independently from -fast.
Dan Gohman60ad1732008-10-07 23:00:56 +000055static cl::opt<cl::boolOrDefault>
Dan Gohman3b88f102008-10-01 20:39:19 +000056EnableFastISelOption("fast-isel", cl::Hidden,
57 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohmanb8e69f12008-09-25 01:14:49 +000058
Bill Wendling523048e2007-02-08 01:36:53 +000059FileModel::Model
Dan Gohman24570832008-03-11 22:29:46 +000060LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
David Greenea31f96c2009-07-14 20:18:05 +000061 formatted_raw_ostream &Out,
Bill Wendling523048e2007-02-08 01:36:53 +000062 CodeGenFileType FileType,
Bill Wendling026e5d72009-04-29 23:29:43 +000063 CodeGenOpt::Level OptLevel) {
Dan Gohmanacb05542008-09-25 00:37:07 +000064 // Add common CodeGen passes.
Bill Wendling084669a2009-04-29 00:15:41 +000065 if (addCommonCodeGenPasses(PM, OptLevel))
Bill Wendling523048e2007-02-08 01:36:53 +000066 return FileModel::Error;
67
Jim Laskey6ea4fae2006-11-07 19:33:46 +000068 // Fold redundant debug labels.
69 PM.add(createDebugLabelFoldingPass());
Dan Gohmanacb05542008-09-25 00:37:07 +000070
71 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +000072 PM.add(createMachineFunctionPrinterPass(cerr));
73
Bill Wendling084669a2009-04-29 00:15:41 +000074 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +000075 PM.add(createMachineFunctionPrinterPass(cerr));
76
Bill Wendling026e5d72009-04-29 23:29:43 +000077 if (OptLevel != CodeGenOpt::None)
Evan Chengf356a892009-05-07 05:42:24 +000078 PM.add(createCodePlacementOptPass());
Evan Cheng95a7be42008-02-28 23:29:57 +000079
Chris Lattnera916db12006-09-04 04:16:09 +000080 switch (FileType) {
Bill Wendling523048e2007-02-08 01:36:53 +000081 default:
82 break;
83 case TargetMachine::AssemblyFile:
Bill Wendling084669a2009-04-29 00:15:41 +000084 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Bill Wendling523048e2007-02-08 01:36:53 +000085 return FileModel::Error;
86 return FileModel::AsmFile;
87 case TargetMachine::ObjectFile:
88 if (getMachOWriterInfo())
89 return FileModel::MachOFile;
90 else if (getELFWriterInfo())
91 return FileModel::ElfFile;
Chris Lattnera916db12006-09-04 04:16:09 +000092 }
Bill Wendling523048e2007-02-08 01:36:53 +000093
94 return FileModel::Error;
95}
Dan Gohmanacb05542008-09-25 00:37:07 +000096
Daniel Dunbard97db682009-07-15 23:34:19 +000097bool LLVMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
98 CodeGenOpt::Level OptLevel,
99 bool Verbose,
100 formatted_raw_ostream &Out) {
101 FunctionPass *Printer = getTarget().createAsmPrinter(Out, *this, Verbose);
102 if (!Printer)
103 llvm_report_error("unable to create assembly printer");
104 PM.add(Printer);
105 return false;
106}
107
Bill Wendling523048e2007-02-08 01:36:53 +0000108/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
109/// be split up (e.g., to add an object writer pass), this method can be used to
110/// finish up adding passes to emit the file, if necessary.
Dan Gohman24570832008-03-11 22:29:46 +0000111bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling523048e2007-02-08 01:36:53 +0000112 MachineCodeEmitter *MCE,
Bill Wendling026e5d72009-04-29 23:29:43 +0000113 CodeGenOpt::Level OptLevel) {
Bill Wendling523048e2007-02-08 01:36:53 +0000114 if (MCE)
Daniel Dunbarc9013922009-07-15 22:33:19 +0000115 addSimpleCodeEmitter(PM, OptLevel, *MCE);
116 if (PrintEmittedAsm)
117 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohmanacb05542008-09-25 00:37:07 +0000118
Gordon Henriksend930f912008-08-17 18:44:35 +0000119 PM.add(createGCInfoDeleter());
Bill Wendling523048e2007-02-08 01:36:53 +0000120
Chris Lattnera916db12006-09-04 04:16:09 +0000121 // Delete machine code for this function
122 PM.add(createMachineCodeDeleter());
Bill Wendling523048e2007-02-08 01:36:53 +0000123
Chris Lattnera916db12006-09-04 04:16:09 +0000124 return false; // success!
125}
126
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000127/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
128/// be split up (e.g., to add an object writer pass), this method can be used to
129/// finish up adding passes to emit the file, if necessary.
130bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
131 JITCodeEmitter *JCE,
132 CodeGenOpt::Level OptLevel) {
133 if (JCE)
Daniel Dunbarc9013922009-07-15 22:33:19 +0000134 addSimpleCodeEmitter(PM, OptLevel, *JCE);
135 if (PrintEmittedAsm)
136 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000137
138 PM.add(createGCInfoDeleter());
139
140 // Delete machine code for this function
141 PM.add(createMachineCodeDeleter());
142
143 return false; // success!
144}
145
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000146/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
147/// be split up (e.g., to add an object writer pass), this method can be used to
148/// finish up adding passes to emit the file, if necessary.
149bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
150 ObjectCodeEmitter *OCE,
151 CodeGenOpt::Level OptLevel) {
152 if (OCE)
Daniel Dunbarc9013922009-07-15 22:33:19 +0000153 addSimpleCodeEmitter(PM, OptLevel, *OCE);
154 if (PrintEmittedAsm)
155 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopes5661ea62009-07-06 05:09:34 +0000156
157 PM.add(createGCInfoDeleter());
158
159 // Delete machine code for this function
160 PM.add(createMachineCodeDeleter());
161
162 return false; // success!
163}
164
Chris Lattnera916db12006-09-04 04:16:09 +0000165/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
166/// get machine code emitted. This uses a MachineCodeEmitter object to handle
167/// actually outputting the machine code and resolving things like the address
168/// of functions. This method should returns true if machine code emission is
169/// not supported.
170///
Dan Gohman24570832008-03-11 22:29:46 +0000171bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattnera916db12006-09-04 04:16:09 +0000172 MachineCodeEmitter &MCE,
Bill Wendling026e5d72009-04-29 23:29:43 +0000173 CodeGenOpt::Level OptLevel) {
Dan Gohmanacb05542008-09-25 00:37:07 +0000174 // Add common CodeGen passes.
Bill Wendling084669a2009-04-29 00:15:41 +0000175 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohmanacb05542008-09-25 00:37:07 +0000176 return true;
177
Bill Wendling084669a2009-04-29 00:15:41 +0000178 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohmanacb05542008-09-25 00:37:07 +0000179 PM.add(createMachineFunctionPrinterPass(cerr));
180
Daniel Dunbarc9013922009-07-15 22:33:19 +0000181 addCodeEmitter(PM, OptLevel, MCE);
182 if (PrintEmittedAsm)
183 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohmanacb05542008-09-25 00:37:07 +0000184
185 PM.add(createGCInfoDeleter());
186
187 // Delete machine code for this function
188 PM.add(createMachineCodeDeleter());
189
190 return false; // success!
191}
192
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000193/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
194/// get machine code emitted. This uses a MachineCodeEmitter object to handle
195/// actually outputting the machine code and resolving things like the address
196/// of functions. This method should returns true if machine code emission is
197/// not supported.
198///
199bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
200 JITCodeEmitter &JCE,
201 CodeGenOpt::Level OptLevel) {
202 // Add common CodeGen passes.
203 if (addCommonCodeGenPasses(PM, OptLevel))
204 return true;
205
206 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
207 PM.add(createMachineFunctionPrinterPass(cerr));
208
Daniel Dunbarc9013922009-07-15 22:33:19 +0000209 addCodeEmitter(PM, OptLevel, JCE);
210 if (PrintEmittedAsm)
211 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000212
213 PM.add(createGCInfoDeleter());
214
215 // Delete machine code for this function
216 PM.add(createMachineCodeDeleter());
217
218 return false; // success!
219}
220
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000221static void printAndVerify(PassManagerBase &PM,
222 bool allowDoubleDefs = false) {
223 if (PrintMachineCode)
224 PM.add(createMachineFunctionPrinterPass(cerr));
225
226 if (VerifyMachineCode)
227 PM.add(createMachineVerifierPass(allowDoubleDefs));
228}
229
Bill Wendling026e5d72009-04-29 23:29:43 +0000230/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
231/// emitting to assembly files or machine code output.
Dan Gohmanacb05542008-09-25 00:37:07 +0000232///
Bill Wendling084669a2009-04-29 00:15:41 +0000233bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling026e5d72009-04-29 23:29:43 +0000234 CodeGenOpt::Level OptLevel) {
Chris Lattnera916db12006-09-04 04:16:09 +0000235 // Standard LLVM-Level Passes.
Dan Gohmanacb05542008-09-25 00:37:07 +0000236
Chris Lattnera916db12006-09-04 04:16:09 +0000237 // Run loop strength reduction before anything else.
Bill Wendling026e5d72009-04-29 23:29:43 +0000238 if (OptLevel != CodeGenOpt::None) {
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000239 PM.add(createLoopStrengthReducePass(getTargetLowering()));
240 if (PrintLSR)
Daniel Dunbar81b5fa52008-10-22 03:25:22 +0000241 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000242 }
Dan Gohmanacb05542008-09-25 00:37:07 +0000243
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000244 // Turn exception handling constructs into something the code generators can
245 // handle.
Dale Johannesenfd967cf2008-04-02 00:25:04 +0000246 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen5e4e0512008-04-01 20:00:57 +0000247 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsd6fb6502009-05-22 20:36:31 +0000248 else
249 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
250
251 PM.add(createGCLoweringPass());
Dan Gohmanacb05542008-09-25 00:37:07 +0000252
Chris Lattnera916db12006-09-04 04:16:09 +0000253 // Make sure that no unreachable blocks are instruction selected.
254 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling523048e2007-02-08 01:36:53 +0000255
Bill Wendling026e5d72009-04-29 23:29:43 +0000256 if (OptLevel != CodeGenOpt::None)
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000257 PM.add(createCodeGenPreparePass(getTargetLowering()));
258
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000259 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendling05d84172008-11-04 02:10:20 +0000260
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000261 if (PrintISelInput)
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +0000262 PM.add(createPrintFunctionPass("\n\n"
263 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar81b5fa52008-10-22 03:25:22 +0000264 &errs()));
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000265
Dan Gohmanacb05542008-09-25 00:37:07 +0000266 // Standard Lower-Level Passes.
267
Dan Gohman3b88f102008-10-01 20:39:19 +0000268 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman60ad1732008-10-07 23:00:56 +0000269 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling026e5d72009-04-29 23:29:43 +0000270 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohman3b88f102008-10-01 20:39:19 +0000271 EnableFastISel = true;
272
Chris Lattnera916db12006-09-04 04:16:09 +0000273 // Ask the target for an isel.
Bill Wendling084669a2009-04-29 00:15:41 +0000274 if (addInstSelector(PM, OptLevel))
Chris Lattnera916db12006-09-04 04:16:09 +0000275 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000276
Chris Lattnera916db12006-09-04 04:16:09 +0000277 // Print the instruction selected machine code...
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000278 printAndVerify(PM, /* allowDoubleDefs= */ true);
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000279
Bill Wendling026e5d72009-04-29 23:29:43 +0000280 if (OptLevel != CodeGenOpt::None) {
Bill Wendling66470d02008-01-04 08:11:03 +0000281 PM.add(createMachineLICMPass());
Chris Lattner276178e2008-01-05 06:14:16 +0000282 PM.add(createMachineSinkingPass());
Evan Cheng6698ab92009-07-13 23:44:01 +0000283 printAndVerify(PM, /* allowDoubleDefs= */ true);
Evan Chengf736bd92009-02-09 08:45:39 +0000284 }
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000285
Anton Korobeynikov0516b6f2008-04-23 18:26:03 +0000286 // Run pre-ra passes.
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000287 if (addPreRegAlloc(PM, OptLevel))
288 printAndVerify(PM);
Anton Korobeynikov0516b6f2008-04-23 18:26:03 +0000289
Evan Cheng12a02222008-06-04 09:18:41 +0000290 // Perform register allocation.
Chris Lattnera916db12006-09-04 04:16:09 +0000291 PM.add(createRegisterAllocator());
Evan Cheng12a02222008-06-04 09:18:41 +0000292
293 // Perform stack slot coloring.
Bill Wendling026e5d72009-04-29 23:29:43 +0000294 if (OptLevel != CodeGenOpt::None)
Evan Chengc94de922009-05-12 18:31:57 +0000295 PM.add(createStackSlotColoringPass(OptLevel >= CodeGenOpt::Aggressive));
Evan Cheng12a02222008-06-04 09:18:41 +0000296
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000297 printAndVerify(PM); // Print the register-allocated code
Dan Gohmanacb05542008-09-25 00:37:07 +0000298
Evan Cheng12a02222008-06-04 09:18:41 +0000299 // Run post-ra passes.
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000300 if (addPostRegAlloc(PM, OptLevel))
301 printAndVerify(PM);
Dan Gohmanacb05542008-09-25 00:37:07 +0000302
Christopher Lamb14bbb152007-07-27 07:36:14 +0000303 PM.add(createLowerSubregsPass());
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000304 printAndVerify(PM);
Bill Wendling523048e2007-02-08 01:36:53 +0000305
Chris Lattnera916db12006-09-04 04:16:09 +0000306 // Insert prolog/epilog code. Eliminate abstract frame index references...
307 PM.add(createPrologEpilogCodeInserter());
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000308 printAndVerify(PM);
Dan Gohmanacb05542008-09-25 00:37:07 +0000309
Dale Johannesen2182f062007-07-13 17:13:54 +0000310 // Second pass scheduler.
Bill Wendling026e5d72009-04-29 23:29:43 +0000311 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dale Johannesen4dc35db2007-07-13 17:31:29 +0000312 PM.add(createPostRAScheduler());
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000313 printAndVerify(PM);
Dan Gohman06613bc2008-11-20 19:54:21 +0000314 }
315
Dan Gohmanb0ef9142008-12-18 01:36:42 +0000316 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000317 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanb0ef9142008-12-18 01:36:42 +0000318 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000319 printAndVerify(PM);
320 }
Dan Gohmanb0ef9142008-12-18 01:36:42 +0000321
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000322 PM.add(createGCMachineCodeAnalysisPass());
Jakob Stoklund Olesen36c027a2009-05-16 00:33:53 +0000323 printAndVerify(PM);
Dan Gohmanacb05542008-09-25 00:37:07 +0000324
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000325 if (PrintGCInfo)
Gordon Henriksend930f912008-08-17 18:44:35 +0000326 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling523048e2007-02-08 01:36:53 +0000327
Dan Gohmanacb05542008-09-25 00:37:07 +0000328 return false;
Chris Lattnera916db12006-09-04 04:16:09 +0000329}