blob: 6a456dc0e6c28c0cc9896630f39d0b77655e291b [file] [log] [blame]
Chris Lattner47877052006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Lattner47877052006-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 Lattner31442f92007-03-31 00:24:43 +000017#include "llvm/Assembly/PrintModulePass.h"
Chris Lattner47877052006-09-04 04:16:09 +000018#include "llvm/CodeGen/Passes.h"
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000019#include "llvm/CodeGen/GCStrategy.h"
Dan Gohmanad2afc22009-07-31 18:16:33 +000020#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Chris Lattner47877052006-09-04 04:16:09 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesen1532f3d2008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Daniel Dunbar6d823cd2009-07-15 23:48:37 +000023#include "llvm/Target/TargetRegistry.h"
Chris Lattner47877052006-09-04 04:16:09 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner31442f92007-03-31 00:24:43 +000025#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000026#include "llvm/Support/FormattedStream.h"
Chris Lattner47877052006-09-04 04:16:09 +000027using namespace llvm;
28
Dan Gohman2c4bf112008-09-25 01:14:49 +000029namespace llvm {
30 bool EnableFastISel;
31}
32
Chris Lattner85ef2542007-06-19 05:47:49 +000033static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
34 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
35static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
36 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000037static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
38 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000039static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
40 cl::desc("Dump garbage collector data"));
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000041static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
42 cl::desc("Verify generated machine code"),
43 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Chris Lattner85ef2542007-06-19 05:47:49 +000044
Chris Lattner459525d2008-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 Gohmandc756852008-10-01 20:39:19 +000051// Enable or disable FastISel. Both options are needed, because
52// FastISel is enabled by default with -fast, and we wish to be
53// able to enable or disable fast-isel independently from -fast.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +000054static cl::opt<cl::boolOrDefault>
Dan Gohmandc756852008-10-01 20:39:19 +000055EnableFastISelOption("fast-isel", cl::Hidden,
56 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman2c4bf112008-09-25 01:14:49 +000057
Bill Wendling04523ea2007-02-08 01:36:53 +000058FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000059LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
David Greene71847812009-07-14 20:18:05 +000060 formatted_raw_ostream &Out,
Bill Wendling04523ea2007-02-08 01:36:53 +000061 CodeGenFileType FileType,
Bill Wendling98a366d2009-04-29 23:29:43 +000062 CodeGenOpt::Level OptLevel) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000063 // Add common CodeGen passes.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000064 if (addCommonCodeGenPasses(PM, OptLevel))
Bill Wendling04523ea2007-02-08 01:36:53 +000065 return FileModel::Error;
66
Jim Laskey9d4209f2006-11-07 19:33:46 +000067 // Fold redundant debug labels.
68 PM.add(createDebugLabelFoldingPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +000069
70 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000071 PM.add(createMachineFunctionPrinterPass(cerr));
72
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000073 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000074 PM.add(createMachineFunctionPrinterPass(cerr));
75
Bill Wendling98a366d2009-04-29 23:29:43 +000076 if (OptLevel != CodeGenOpt::None)
Evan Chengbbf1db72009-05-07 05:42:24 +000077 PM.add(createCodePlacementOptPass());
Evan Chengd703ed62008-02-28 23:29:57 +000078
Chris Lattner47877052006-09-04 04:16:09 +000079 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000080 default:
81 break;
82 case TargetMachine::AssemblyFile:
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000083 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Bill Wendling04523ea2007-02-08 01:36:53 +000084 return FileModel::Error;
85 return FileModel::AsmFile;
86 case TargetMachine::ObjectFile:
87 if (getMachOWriterInfo())
88 return FileModel::MachOFile;
89 else if (getELFWriterInfo())
90 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000091 }
Bill Wendling04523ea2007-02-08 01:36:53 +000092
93 return FileModel::Error;
94}
Dan Gohman02dae4b2008-09-25 00:37:07 +000095
Daniel Dunbar5d77cad2009-07-15 23:34:19 +000096bool LLVMTargetMachine::addAssemblyEmitter(PassManagerBase &PM,
97 CodeGenOpt::Level OptLevel,
98 bool Verbose,
99 formatted_raw_ostream &Out) {
100 FunctionPass *Printer = getTarget().createAsmPrinter(Out, *this, Verbose);
101 if (!Printer)
Daniel Dunbar36129db2009-07-15 23:54:01 +0000102 return true;
103
Daniel Dunbar5d77cad2009-07-15 23:34:19 +0000104 PM.add(Printer);
105 return false;
106}
107
Bill Wendling04523ea2007-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 Gohmanbfae8312008-03-11 22:29:46 +0000111bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000112 MachineCodeEmitter *MCE,
Bill Wendling98a366d2009-04-29 23:29:43 +0000113 CodeGenOpt::Level OptLevel) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000114 if (MCE)
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000115 addSimpleCodeEmitter(PM, OptLevel, *MCE);
116 if (PrintEmittedAsm)
117 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000118
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000119 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000120
Chris Lattner47877052006-09-04 04:16:09 +0000121 return false; // success!
122}
123
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000124/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
125/// be split up (e.g., to add an object writer pass), this method can be used to
126/// finish up adding passes to emit the file, if necessary.
127bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
128 JITCodeEmitter *JCE,
129 CodeGenOpt::Level OptLevel) {
130 if (JCE)
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000131 addSimpleCodeEmitter(PM, OptLevel, *JCE);
132 if (PrintEmittedAsm)
133 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000134
135 PM.add(createGCInfoDeleter());
136
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000137 return false; // success!
138}
139
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000140/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
141/// be split up (e.g., to add an object writer pass), this method can be used to
142/// finish up adding passes to emit the file, if necessary.
143bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
144 ObjectCodeEmitter *OCE,
145 CodeGenOpt::Level OptLevel) {
146 if (OCE)
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000147 addSimpleCodeEmitter(PM, OptLevel, *OCE);
148 if (PrintEmittedAsm)
149 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000150
151 PM.add(createGCInfoDeleter());
152
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000153 return false; // success!
154}
155
Chris Lattner47877052006-09-04 04:16:09 +0000156/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
157/// get machine code emitted. This uses a MachineCodeEmitter object to handle
158/// actually outputting the machine code and resolving things like the address
159/// of functions. This method should returns true if machine code emission is
160/// not supported.
161///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000162bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000163 MachineCodeEmitter &MCE,
Bill Wendling98a366d2009-04-29 23:29:43 +0000164 CodeGenOpt::Level OptLevel) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000165 // Add common CodeGen passes.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000166 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman02dae4b2008-09-25 00:37:07 +0000167 return true;
168
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000169 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman02dae4b2008-09-25 00:37:07 +0000170 PM.add(createMachineFunctionPrinterPass(cerr));
171
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000172 addCodeEmitter(PM, OptLevel, MCE);
173 if (PrintEmittedAsm)
174 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000175
176 PM.add(createGCInfoDeleter());
177
Dan Gohman02dae4b2008-09-25 00:37:07 +0000178 return false; // success!
179}
180
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000181/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
182/// get machine code emitted. This uses a MachineCodeEmitter object to handle
183/// actually outputting the machine code and resolving things like the address
184/// of functions. This method should returns true if machine code emission is
185/// not supported.
186///
187bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
188 JITCodeEmitter &JCE,
189 CodeGenOpt::Level OptLevel) {
190 // Add common CodeGen passes.
191 if (addCommonCodeGenPasses(PM, OptLevel))
192 return true;
193
194 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
195 PM.add(createMachineFunctionPrinterPass(cerr));
196
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000197 addCodeEmitter(PM, OptLevel, JCE);
198 if (PrintEmittedAsm)
199 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000200
201 PM.add(createGCInfoDeleter());
202
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000203 return false; // success!
204}
205
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000206static void printAndVerify(PassManagerBase &PM,
207 bool allowDoubleDefs = false) {
208 if (PrintMachineCode)
209 PM.add(createMachineFunctionPrinterPass(cerr));
210
211 if (VerifyMachineCode)
212 PM.add(createMachineVerifierPass(allowDoubleDefs));
213}
214
Bill Wendling98a366d2009-04-29 23:29:43 +0000215/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
216/// emitting to assembly files or machine code output.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000217///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000218bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000219 CodeGenOpt::Level OptLevel) {
Chris Lattner47877052006-09-04 04:16:09 +0000220 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000221
Chris Lattner47877052006-09-04 04:16:09 +0000222 // Run loop strength reduction before anything else.
Bill Wendling98a366d2009-04-29 23:29:43 +0000223 if (OptLevel != CodeGenOpt::None) {
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000224 PM.add(createLoopStrengthReducePass(getTargetLowering()));
225 if (PrintLSR)
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000226 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000227 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000228
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000229 // Turn exception handling constructs into something the code generators can
230 // handle.
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000231 switch (getTargetAsmInfo()->getExceptionHandlingType())
232 {
233 // SjLj piggy-backs on dwarf for this bit
234 case ExceptionHandling::SjLj:
235 case ExceptionHandling::Dwarf:
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000236 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000237 break;
238 case ExceptionHandling::None:
239 PM.add(createLowerInvokePass(getTargetLowering()));
240 break;
241 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000242
243 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000244
Chris Lattner47877052006-09-04 04:16:09 +0000245 // Make sure that no unreachable blocks are instruction selected.
246 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000247
Bill Wendling98a366d2009-04-29 23:29:43 +0000248 if (OptLevel != CodeGenOpt::None)
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000249 PM.add(createCodeGenPreparePass(getTargetLowering()));
250
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000251 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendling2b58ce52008-11-04 02:10:20 +0000252
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000253 if (PrintISelInput)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000254 PM.add(createPrintFunctionPass("\n\n"
255 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000256 &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000257
Dan Gohman02dae4b2008-09-25 00:37:07 +0000258 // Standard Lower-Level Passes.
259
Dan Gohmanad2afc22009-07-31 18:16:33 +0000260 // Set up a MachineFunction for the rest of CodeGen to work on.
261 PM.add(new MachineFunctionAnalysis(*this, OptLevel));
262
Dan Gohmandc756852008-10-01 20:39:19 +0000263 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000264 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling98a366d2009-04-29 23:29:43 +0000265 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmandc756852008-10-01 20:39:19 +0000266 EnableFastISel = true;
267
Chris Lattner47877052006-09-04 04:16:09 +0000268 // Ask the target for an isel.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000269 if (addInstSelector(PM, OptLevel))
Chris Lattner47877052006-09-04 04:16:09 +0000270 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000271
Chris Lattner47877052006-09-04 04:16:09 +0000272 // Print the instruction selected machine code...
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000273 printAndVerify(PM, /* allowDoubleDefs= */ true);
Bill Wendling0f940c92007-12-07 21:42:31 +0000274
Bill Wendling98a366d2009-04-29 23:29:43 +0000275 if (OptLevel != CodeGenOpt::None) {
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000276 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000277 PM.add(createMachineSinkingPass());
Evan Cheng8799dbe2009-07-13 23:44:01 +0000278 printAndVerify(PM, /* allowDoubleDefs= */ true);
Evan Cheng8f0d99e2009-02-09 08:45:39 +0000279 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000280
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000281 // Run pre-ra passes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000282 if (addPreRegAlloc(PM, OptLevel))
283 printAndVerify(PM);
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000284
Evan Cheng3f32d652008-06-04 09:18:41 +0000285 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000286 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000287
288 // Perform stack slot coloring.
Bill Wendling98a366d2009-04-29 23:29:43 +0000289 if (OptLevel != CodeGenOpt::None)
Evan Cheng6248fa42009-08-05 07:26:17 +0000290 // FIXME: Re-enable coloring with register when it's capable of adding
291 // kill markers.
292 PM.add(createStackSlotColoringPass(false));
Evan Cheng3f32d652008-06-04 09:18:41 +0000293
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000294 printAndVerify(PM); // Print the register-allocated code
Dan Gohman02dae4b2008-09-25 00:37:07 +0000295
Evan Cheng3f32d652008-06-04 09:18:41 +0000296 // Run post-ra passes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000297 if (addPostRegAlloc(PM, OptLevel))
298 printAndVerify(PM);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000299
Christopher Lambada779f2007-07-27 07:36:14 +0000300 PM.add(createLowerSubregsPass());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000301 printAndVerify(PM);
Bill Wendling04523ea2007-02-08 01:36:53 +0000302
Chris Lattner47877052006-09-04 04:16:09 +0000303 // Insert prolog/epilog code. Eliminate abstract frame index references...
304 PM.add(createPrologEpilogCodeInserter());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000305 printAndVerify(PM);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000306
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000307 // Second pass scheduler.
Bill Wendling98a366d2009-04-29 23:29:43 +0000308 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dale Johannesen72f15962007-07-13 17:31:29 +0000309 PM.add(createPostRAScheduler());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000310 printAndVerify(PM);
Dan Gohman5ce09732008-11-20 19:54:21 +0000311 }
312
Dan Gohman23b0d492008-12-18 01:36:42 +0000313 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000314 if (OptLevel != CodeGenOpt::None) {
Dan Gohman23b0d492008-12-18 01:36:42 +0000315 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000316 printAndVerify(PM);
317 }
Dan Gohman23b0d492008-12-18 01:36:42 +0000318
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000319 PM.add(createGCMachineCodeAnalysisPass());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000320 printAndVerify(PM);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000321
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000322 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000323 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000324
Dan Gohman02dae4b2008-09-25 00:37:07 +0000325 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000326}