blob: 15eac944a0e6e3fd494e777448c85827b0110a81 [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"
Devang Patel0f54dcb2007-03-06 21:14:09 +000018#include "llvm/Analysis/LoopPass.h"
Chris Lattner47877052006-09-04 04:16:09 +000019#include "llvm/CodeGen/Passes.h"
Gordon Henriksen5a29c9e2008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.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"
Chris Lattner47877052006-09-04 04:16:09 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattner31442f92007-03-31 00:24:43 +000024#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000025#include "llvm/Support/FormattedStream.h"
Chris Lattner47877052006-09-04 04:16:09 +000026using namespace llvm;
27
Dan Gohman2c4bf112008-09-25 01:14:49 +000028namespace llvm {
29 bool EnableFastISel;
30}
31
Chris Lattner85ef2542007-06-19 05:47:49 +000032static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
33 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
34static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
35 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000036static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
37 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000038static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39 cl::desc("Dump garbage collector data"));
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000040static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
41 cl::desc("Verify generated machine code"),
42 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Chris Lattner85ef2542007-06-19 05:47:49 +000043
Chris Lattner459525d2008-01-14 19:00:06 +000044// When this works it will be on by default.
45static cl::opt<bool>
46DisablePostRAScheduler("disable-post-RA-scheduler",
47 cl::desc("Disable scheduling after register allocation"),
48 cl::init(true));
49
Dan Gohmandc756852008-10-01 20:39:19 +000050// Enable or disable FastISel. Both options are needed, because
51// FastISel is enabled by default with -fast, and we wish to be
52// able to enable or disable fast-isel independently from -fast.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +000053static cl::opt<cl::boolOrDefault>
Dan Gohmandc756852008-10-01 20:39:19 +000054EnableFastISelOption("fast-isel", cl::Hidden,
55 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman2c4bf112008-09-25 01:14:49 +000056
Bill Wendling04523ea2007-02-08 01:36:53 +000057FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000058LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
David Greene71847812009-07-14 20:18:05 +000059 formatted_raw_ostream &Out,
Bill Wendling04523ea2007-02-08 01:36:53 +000060 CodeGenFileType FileType,
Bill Wendling98a366d2009-04-29 23:29:43 +000061 CodeGenOpt::Level OptLevel) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000062 // Add common CodeGen passes.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000063 if (addCommonCodeGenPasses(PM, OptLevel))
Bill Wendling04523ea2007-02-08 01:36:53 +000064 return FileModel::Error;
65
Jim Laskey9d4209f2006-11-07 19:33:46 +000066 // Fold redundant debug labels.
67 PM.add(createDebugLabelFoldingPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +000068
69 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000070 PM.add(createMachineFunctionPrinterPass(cerr));
71
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000072 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000073 PM.add(createMachineFunctionPrinterPass(cerr));
74
Bill Wendling98a366d2009-04-29 23:29:43 +000075 if (OptLevel != CodeGenOpt::None)
Evan Chengbbf1db72009-05-07 05:42:24 +000076 PM.add(createCodePlacementOptPass());
Evan Chengd703ed62008-02-28 23:29:57 +000077
Chris Lattner47877052006-09-04 04:16:09 +000078 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000079 default:
80 break;
81 case TargetMachine::AssemblyFile:
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000082 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Bill Wendling04523ea2007-02-08 01:36:53 +000083 return FileModel::Error;
84 return FileModel::AsmFile;
85 case TargetMachine::ObjectFile:
86 if (getMachOWriterInfo())
87 return FileModel::MachOFile;
88 else if (getELFWriterInfo())
89 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000090 }
Bill Wendling04523ea2007-02-08 01:36:53 +000091
92 return FileModel::Error;
93}
Dan Gohman02dae4b2008-09-25 00:37:07 +000094
Bill Wendling04523ea2007-02-08 01:36:53 +000095/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
96/// be split up (e.g., to add an object writer pass), this method can be used to
97/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +000098bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +000099 MachineCodeEmitter *MCE,
Bill Wendling98a366d2009-04-29 23:29:43 +0000100 CodeGenOpt::Level OptLevel) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000101 if (MCE)
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000102 addSimpleCodeEmitter(PM, OptLevel, *MCE);
103 if (PrintEmittedAsm)
104 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000105
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000106 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000107
Chris Lattner47877052006-09-04 04:16:09 +0000108 // Delete machine code for this function
109 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000110
Chris Lattner47877052006-09-04 04:16:09 +0000111 return false; // success!
112}
113
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000114/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
115/// be split up (e.g., to add an object writer pass), this method can be used to
116/// finish up adding passes to emit the file, if necessary.
117bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
118 JITCodeEmitter *JCE,
119 CodeGenOpt::Level OptLevel) {
120 if (JCE)
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000121 addSimpleCodeEmitter(PM, OptLevel, *JCE);
122 if (PrintEmittedAsm)
123 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000124
125 PM.add(createGCInfoDeleter());
126
127 // Delete machine code for this function
128 PM.add(createMachineCodeDeleter());
129
130 return false; // success!
131}
132
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000133/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
134/// be split up (e.g., to add an object writer pass), this method can be used to
135/// finish up adding passes to emit the file, if necessary.
136bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
137 ObjectCodeEmitter *OCE,
138 CodeGenOpt::Level OptLevel) {
139 if (OCE)
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000140 addSimpleCodeEmitter(PM, OptLevel, *OCE);
141 if (PrintEmittedAsm)
142 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesac57e6e2009-07-06 05:09:34 +0000143
144 PM.add(createGCInfoDeleter());
145
146 // Delete machine code for this function
147 PM.add(createMachineCodeDeleter());
148
149 return false; // success!
150}
151
Chris Lattner47877052006-09-04 04:16:09 +0000152/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
153/// get machine code emitted. This uses a MachineCodeEmitter object to handle
154/// actually outputting the machine code and resolving things like the address
155/// of functions. This method should returns true if machine code emission is
156/// not supported.
157///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000158bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000159 MachineCodeEmitter &MCE,
Bill Wendling98a366d2009-04-29 23:29:43 +0000160 CodeGenOpt::Level OptLevel) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000161 // Add common CodeGen passes.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000162 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman02dae4b2008-09-25 00:37:07 +0000163 return true;
164
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000165 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman02dae4b2008-09-25 00:37:07 +0000166 PM.add(createMachineFunctionPrinterPass(cerr));
167
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000168 addCodeEmitter(PM, OptLevel, MCE);
169 if (PrintEmittedAsm)
170 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000171
172 PM.add(createGCInfoDeleter());
173
174 // Delete machine code for this function
175 PM.add(createMachineCodeDeleter());
176
177 return false; // success!
178}
179
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000180/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
181/// get machine code emitted. This uses a MachineCodeEmitter object to handle
182/// actually outputting the machine code and resolving things like the address
183/// of functions. This method should returns true if machine code emission is
184/// not supported.
185///
186bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
187 JITCodeEmitter &JCE,
188 CodeGenOpt::Level OptLevel) {
189 // Add common CodeGen passes.
190 if (addCommonCodeGenPasses(PM, OptLevel))
191 return true;
192
193 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
194 PM.add(createMachineFunctionPrinterPass(cerr));
195
Daniel Dunbarcfe9a602009-07-15 22:33:19 +0000196 addCodeEmitter(PM, OptLevel, JCE);
197 if (PrintEmittedAsm)
198 addAssemblyEmitter(PM, OptLevel, true, ferrs());
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000199
200 PM.add(createGCInfoDeleter());
201
202 // Delete machine code for this function
203 PM.add(createMachineCodeDeleter());
204
205 return false; // success!
206}
207
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000208static void printAndVerify(PassManagerBase &PM,
209 bool allowDoubleDefs = false) {
210 if (PrintMachineCode)
211 PM.add(createMachineFunctionPrinterPass(cerr));
212
213 if (VerifyMachineCode)
214 PM.add(createMachineVerifierPass(allowDoubleDefs));
215}
216
Bill Wendling98a366d2009-04-29 23:29:43 +0000217/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
218/// emitting to assembly files or machine code output.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000219///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000220bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000221 CodeGenOpt::Level OptLevel) {
Chris Lattner47877052006-09-04 04:16:09 +0000222 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000223
Chris Lattner47877052006-09-04 04:16:09 +0000224 // Run loop strength reduction before anything else.
Bill Wendling98a366d2009-04-29 23:29:43 +0000225 if (OptLevel != CodeGenOpt::None) {
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000226 PM.add(createLoopStrengthReducePass(getTargetLowering()));
227 if (PrintLSR)
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000228 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000229 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000230
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000231 // Turn exception handling constructs into something the code generators can
232 // handle.
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000233 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000234 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000235 else
236 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
237
238 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000239
Chris Lattner47877052006-09-04 04:16:09 +0000240 // Make sure that no unreachable blocks are instruction selected.
241 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000242
Bill Wendling98a366d2009-04-29 23:29:43 +0000243 if (OptLevel != CodeGenOpt::None)
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000244 PM.add(createCodeGenPreparePass(getTargetLowering()));
245
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000246 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendling2b58ce52008-11-04 02:10:20 +0000247
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000248 if (PrintISelInput)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000249 PM.add(createPrintFunctionPass("\n\n"
250 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000251 &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000252
Dan Gohman02dae4b2008-09-25 00:37:07 +0000253 // Standard Lower-Level Passes.
254
Dan Gohmandc756852008-10-01 20:39:19 +0000255 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000256 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling98a366d2009-04-29 23:29:43 +0000257 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmandc756852008-10-01 20:39:19 +0000258 EnableFastISel = true;
259
Chris Lattner47877052006-09-04 04:16:09 +0000260 // Ask the target for an isel.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000261 if (addInstSelector(PM, OptLevel))
Chris Lattner47877052006-09-04 04:16:09 +0000262 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000263
Chris Lattner47877052006-09-04 04:16:09 +0000264 // Print the instruction selected machine code...
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000265 printAndVerify(PM, /* allowDoubleDefs= */ true);
Bill Wendling0f940c92007-12-07 21:42:31 +0000266
Bill Wendling98a366d2009-04-29 23:29:43 +0000267 if (OptLevel != CodeGenOpt::None) {
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000268 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000269 PM.add(createMachineSinkingPass());
Evan Cheng8799dbe2009-07-13 23:44:01 +0000270 printAndVerify(PM, /* allowDoubleDefs= */ true);
Evan Cheng8f0d99e2009-02-09 08:45:39 +0000271 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000272
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000273 // Run pre-ra passes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000274 if (addPreRegAlloc(PM, OptLevel))
275 printAndVerify(PM);
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000276
Evan Cheng3f32d652008-06-04 09:18:41 +0000277 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000278 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000279
280 // Perform stack slot coloring.
Bill Wendling98a366d2009-04-29 23:29:43 +0000281 if (OptLevel != CodeGenOpt::None)
Evan Cheng1ea73272009-05-12 18:31:57 +0000282 PM.add(createStackSlotColoringPass(OptLevel >= CodeGenOpt::Aggressive));
Evan Cheng3f32d652008-06-04 09:18:41 +0000283
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000284 printAndVerify(PM); // Print the register-allocated code
Dan Gohman02dae4b2008-09-25 00:37:07 +0000285
Evan Cheng3f32d652008-06-04 09:18:41 +0000286 // Run post-ra passes.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000287 if (addPostRegAlloc(PM, OptLevel))
288 printAndVerify(PM);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000289
Christopher Lambada779f2007-07-27 07:36:14 +0000290 PM.add(createLowerSubregsPass());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000291 printAndVerify(PM);
Bill Wendling04523ea2007-02-08 01:36:53 +0000292
Chris Lattner47877052006-09-04 04:16:09 +0000293 // Insert prolog/epilog code. Eliminate abstract frame index references...
294 PM.add(createPrologEpilogCodeInserter());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000295 printAndVerify(PM);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000296
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000297 // Second pass scheduler.
Bill Wendling98a366d2009-04-29 23:29:43 +0000298 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dale Johannesen72f15962007-07-13 17:31:29 +0000299 PM.add(createPostRAScheduler());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000300 printAndVerify(PM);
Dan Gohman5ce09732008-11-20 19:54:21 +0000301 }
302
Dan Gohman23b0d492008-12-18 01:36:42 +0000303 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000304 if (OptLevel != CodeGenOpt::None) {
Dan Gohman23b0d492008-12-18 01:36:42 +0000305 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000306 printAndVerify(PM);
307 }
Dan Gohman23b0d492008-12-18 01:36:42 +0000308
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000309 PM.add(createGCMachineCodeAnalysisPass());
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000310 printAndVerify(PM);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000311
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000312 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000313 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000314
Dan Gohman02dae4b2008-09-25 00:37:07 +0000315 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000316}