blob: d7fde5f90a90a78a5d86b27cd9757d0ec5c6fb7d [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"
Owen Anderson847b99b2008-08-21 00:14:44 +000025#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026using namespace llvm;
27
Dan Gohman6a9b05f2008-09-25 01:14:49 +000028namespace llvm {
29 bool EnableFastISel;
30}
31
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Cheng77547212007-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 Henriksen36464772008-01-07 01:33:09 +000038static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39 cl::desc("Dump garbage collector data"));
Jakob Stoklund Olesenac32dd92009-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));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043
Chris Lattnere06d8eb2008-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 Gohmane3769ef2008-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 Gohman6d7ee012008-10-07 23:00:56 +000053static cl::opt<cl::boolOrDefault>
Dan Gohmane3769ef2008-10-01 20:39:19 +000054EnableFastISelOption("fast-isel", cl::Hidden,
55 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman6a9b05f2008-09-25 01:14:49 +000056
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000058LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Anderson847b99b2008-08-21 00:14:44 +000059 raw_ostream &Out,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 CodeGenFileType FileType,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000061 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +000062 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +000063 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 return FileModel::Error;
65
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 // Fold redundant debug labels.
67 PM.add(createDebugLabelFoldingPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +000068
69 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 PM.add(createMachineFunctionPrinterPass(cerr));
71
Bill Wendling58ed5d22009-04-29 00:15:41 +000072 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 PM.add(createMachineFunctionPrinterPass(cerr));
74
Bill Wendling5ed22ac2009-04-29 23:29:43 +000075 if (OptLevel != CodeGenOpt::None)
Evan Cheng02482c82009-05-07 05:42:24 +000076 PM.add(createCodePlacementOptPass());
Evan Cheng7e29ba02008-02-28 23:29:57 +000077
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 switch (FileType) {
79 default:
80 break;
81 case TargetMachine::AssemblyFile:
Bill Wendling58ed5d22009-04-29 00:15:41 +000082 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
90 }
91
92 return FileModel::Error;
93}
Dan Gohman7e71ccf2008-09-25 00:37:07 +000094
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohmane34aa772008-03-11 22:29:46 +000098bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 MachineCodeEmitter *MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000100 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 if (MCE)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000102 addSimpleCodeEmitter(PM, OptLevel, PrintEmittedAsm, *MCE);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000103
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000104 PM.add(createGCInfoDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
106 // Delete machine code for this function
107 PM.add(createMachineCodeDeleter());
108
109 return false; // success!
110}
111
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000112/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
113/// be split up (e.g., to add an object writer pass), this method can be used to
114/// finish up adding passes to emit the file, if necessary.
115bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
116 JITCodeEmitter *JCE,
117 CodeGenOpt::Level OptLevel) {
118 if (JCE)
119 addSimpleCodeEmitter(PM, OptLevel, PrintEmittedAsm, *JCE);
120
121 PM.add(createGCInfoDeleter());
122
123 // Delete machine code for this function
124 PM.add(createMachineCodeDeleter());
125
126 return false; // success!
127}
128
Bruno Cardoso Lopesaabb9a52009-07-06 05:09:34 +0000129/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
130/// be split up (e.g., to add an object writer pass), this method can be used to
131/// finish up adding passes to emit the file, if necessary.
132bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
133 ObjectCodeEmitter *OCE,
134 CodeGenOpt::Level OptLevel) {
135 if (OCE)
136 addSimpleCodeEmitter(PM, OptLevel, PrintEmittedAsm, *OCE);
137
138 PM.add(createGCInfoDeleter());
139
140 // Delete machine code for this function
141 PM.add(createMachineCodeDeleter());
142
143 return false; // success!
144}
145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
147/// get machine code emitted. This uses a MachineCodeEmitter object to handle
148/// actually outputting the machine code and resolving things like the address
149/// of functions. This method should returns true if machine code emission is
150/// not supported.
151///
Dan Gohmane34aa772008-03-11 22:29:46 +0000152bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 MachineCodeEmitter &MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000154 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000155 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000156 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000157 return true;
158
Bill Wendling58ed5d22009-04-29 00:15:41 +0000159 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000160 PM.add(createMachineFunctionPrinterPass(cerr));
161
Bill Wendling58ed5d22009-04-29 00:15:41 +0000162 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, MCE);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000163
164 PM.add(createGCInfoDeleter());
165
166 // Delete machine code for this function
167 PM.add(createMachineCodeDeleter());
168
169 return false; // success!
170}
171
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000172/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
173/// get machine code emitted. This uses a MachineCodeEmitter object to handle
174/// actually outputting the machine code and resolving things like the address
175/// of functions. This method should returns true if machine code emission is
176/// not supported.
177///
178bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
179 JITCodeEmitter &JCE,
180 CodeGenOpt::Level OptLevel) {
181 // Add common CodeGen passes.
182 if (addCommonCodeGenPasses(PM, OptLevel))
183 return true;
184
185 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
186 PM.add(createMachineFunctionPrinterPass(cerr));
187
188 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, JCE);
189
190 PM.add(createGCInfoDeleter());
191
192 // Delete machine code for this function
193 PM.add(createMachineCodeDeleter());
194
195 return false; // success!
196}
197
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000198static void printAndVerify(PassManagerBase &PM,
199 bool allowDoubleDefs = false) {
200 if (PrintMachineCode)
201 PM.add(createMachineFunctionPrinterPass(cerr));
202
203 if (VerifyMachineCode)
204 PM.add(createMachineVerifierPass(allowDoubleDefs));
205}
206
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000207/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
208/// emitting to assembly files or machine code output.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000209///
Bill Wendling58ed5d22009-04-29 00:15:41 +0000210bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000211 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212 // Standard LLVM-Level Passes.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000213
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 // Run loop strength reduction before anything else.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000215 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216 PM.add(createLoopStrengthReducePass(getTargetLowering()));
217 if (PrintLSR)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000218 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 }
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000220
Duncan Sandsf325c482009-05-22 20:36:31 +0000221 // Turn exception handling constructs into something the code generators can
222 // handle.
Dale Johannesen85535762008-04-02 00:25:04 +0000223 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +0000224 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsf325c482009-05-22 20:36:31 +0000225 else
226 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
227
228 PM.add(createGCLoweringPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000229
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 // Make sure that no unreachable blocks are instruction selected.
231 PM.add(createUnreachableBlockEliminationPass());
232
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000233 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 PM.add(createCodeGenPreparePass(getTargetLowering()));
235
Bill Wendling3e13ce52008-11-13 01:02:14 +0000236 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendlingdac9f712008-11-04 02:10:20 +0000237
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000238 if (PrintISelInput)
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000239 PM.add(createPrintFunctionPass("\n\n"
240 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000241 &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000243 // Standard Lower-Level Passes.
244
Dan Gohmane3769ef2008-10-01 20:39:19 +0000245 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman6d7ee012008-10-07 23:00:56 +0000246 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000247 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmane3769ef2008-10-01 20:39:19 +0000248 EnableFastISel = true;
249
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 // Ask the target for an isel.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000251 if (addInstSelector(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000252 return true;
253
254 // Print the instruction selected machine code...
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000255 printAndVerify(PM, /* allowDoubleDefs= */ true);
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000256
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000257 if (OptLevel != CodeGenOpt::None) {
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000258 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000259 PM.add(createMachineSinkingPass());
Evan Cheng66c7e262009-07-13 23:44:01 +0000260 printAndVerify(PM, /* allowDoubleDefs= */ true);
Evan Cheng23cf3d12009-02-09 08:45:39 +0000261 }
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000262
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000263 // Run pre-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000264 if (addPreRegAlloc(PM, OptLevel))
265 printAndVerify(PM);
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000266
Evan Cheng14f8a502008-06-04 09:18:41 +0000267 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000269
270 // Perform stack slot coloring.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000271 if (OptLevel != CodeGenOpt::None)
Evan Cheng4f2dfd22009-05-12 18:31:57 +0000272 PM.add(createStackSlotColoringPass(OptLevel >= CodeGenOpt::Aggressive));
Evan Cheng14f8a502008-06-04 09:18:41 +0000273
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000274 printAndVerify(PM); // Print the register-allocated code
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000275
Evan Cheng14f8a502008-06-04 09:18:41 +0000276 // Run post-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000277 if (addPostRegAlloc(PM, OptLevel))
278 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000279
Christopher Lambed379732007-07-27 07:36:14 +0000280 PM.add(createLowerSubregsPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000281 printAndVerify(PM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000282
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000283 // Insert prolog/epilog code. Eliminate abstract frame index references...
284 PM.add(createPrologEpilogCodeInserter());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000285 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 // Second pass scheduler.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000288 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289 PM.add(createPostRAScheduler());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000290 printAndVerify(PM);
Dan Gohmana2fa48e2008-11-20 19:54:21 +0000291 }
292
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000293 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000294 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000295 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000296 printAndVerify(PM);
297 }
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000298
Gordon Henriksen36464772008-01-07 01:33:09 +0000299 PM.add(createGCMachineCodeAnalysisPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000300 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000301
Gordon Henriksen36464772008-01-07 01:33:09 +0000302 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000303 PM.add(createGCInfoPrinter(*cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000305 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306}