blob: b3c60e63932f4daca538744ada93ce226a32f16e [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
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000129/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
130/// get machine code emitted. This uses a MachineCodeEmitter object to handle
131/// actually outputting the machine code and resolving things like the address
132/// of functions. This method should returns true if machine code emission is
133/// not supported.
134///
Dan Gohmane34aa772008-03-11 22:29:46 +0000135bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 MachineCodeEmitter &MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000137 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000138 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000139 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000140 return true;
141
Bill Wendling58ed5d22009-04-29 00:15:41 +0000142 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000143 PM.add(createMachineFunctionPrinterPass(cerr));
144
Bill Wendling58ed5d22009-04-29 00:15:41 +0000145 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, MCE);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000146
147 PM.add(createGCInfoDeleter());
148
149 // Delete machine code for this function
150 PM.add(createMachineCodeDeleter());
151
152 return false; // success!
153}
154
Bruno Cardoso Lopes1ea31ff2009-05-30 20:51:52 +0000155/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
156/// get machine code emitted. This uses a MachineCodeEmitter object to handle
157/// actually outputting the machine code and resolving things like the address
158/// of functions. This method should returns true if machine code emission is
159/// not supported.
160///
161bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
162 JITCodeEmitter &JCE,
163 CodeGenOpt::Level OptLevel) {
164 // Add common CodeGen passes.
165 if (addCommonCodeGenPasses(PM, OptLevel))
166 return true;
167
168 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
169 PM.add(createMachineFunctionPrinterPass(cerr));
170
171 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, JCE);
172
173 PM.add(createGCInfoDeleter());
174
175 // Delete machine code for this function
176 PM.add(createMachineCodeDeleter());
177
178 return false; // success!
179}
180
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000181static void printAndVerify(PassManagerBase &PM,
182 bool allowDoubleDefs = false) {
183 if (PrintMachineCode)
184 PM.add(createMachineFunctionPrinterPass(cerr));
185
186 if (VerifyMachineCode)
187 PM.add(createMachineVerifierPass(allowDoubleDefs));
188}
189
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000190/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
191/// emitting to assembly files or machine code output.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000192///
Bill Wendling58ed5d22009-04-29 00:15:41 +0000193bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000194 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 // Standard LLVM-Level Passes.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000196
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 // Run loop strength reduction before anything else.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000198 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 PM.add(createLoopStrengthReducePass(getTargetLowering()));
200 if (PrintLSR)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000201 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 }
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000203
Duncan Sandsf325c482009-05-22 20:36:31 +0000204 // Turn exception handling constructs into something the code generators can
205 // handle.
Dale Johannesen85535762008-04-02 00:25:04 +0000206 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +0000207 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsf325c482009-05-22 20:36:31 +0000208 else
209 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
210
211 PM.add(createGCLoweringPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000212
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 // Make sure that no unreachable blocks are instruction selected.
214 PM.add(createUnreachableBlockEliminationPass());
215
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000216 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 PM.add(createCodeGenPreparePass(getTargetLowering()));
218
Bill Wendling3e13ce52008-11-13 01:02:14 +0000219 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendlingdac9f712008-11-04 02:10:20 +0000220
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 if (PrintISelInput)
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000222 PM.add(createPrintFunctionPass("\n\n"
223 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000224 &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000226 // Standard Lower-Level Passes.
227
Dan Gohmane3769ef2008-10-01 20:39:19 +0000228 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman6d7ee012008-10-07 23:00:56 +0000229 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000230 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmane3769ef2008-10-01 20:39:19 +0000231 EnableFastISel = true;
232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 // Ask the target for an isel.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000234 if (addInstSelector(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 return true;
236
237 // Print the instruction selected machine code...
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000238 printAndVerify(PM, /* allowDoubleDefs= */ true);
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000239
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000240 if (OptLevel != CodeGenOpt::None) {
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000241 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000242 PM.add(createMachineSinkingPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000243 printAndVerify(PM, /* allowDoubleDefs= */ true);
Evan Cheng23cf3d12009-02-09 08:45:39 +0000244 }
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000245
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000246 // Run pre-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000247 if (addPreRegAlloc(PM, OptLevel))
248 printAndVerify(PM);
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000249
Evan Cheng14f8a502008-06-04 09:18:41 +0000250 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000251 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000252
253 // Perform stack slot coloring.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000254 if (OptLevel != CodeGenOpt::None)
Evan Cheng4f2dfd22009-05-12 18:31:57 +0000255 PM.add(createStackSlotColoringPass(OptLevel >= CodeGenOpt::Aggressive));
Evan Cheng14f8a502008-06-04 09:18:41 +0000256
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000257 printAndVerify(PM); // Print the register-allocated code
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000258
Evan Cheng14f8a502008-06-04 09:18:41 +0000259 // Run post-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000260 if (addPostRegAlloc(PM, OptLevel))
261 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000262
Christopher Lambed379732007-07-27 07:36:14 +0000263 PM.add(createLowerSubregsPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000264 printAndVerify(PM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000266 // Insert prolog/epilog code. Eliminate abstract frame index references...
267 PM.add(createPrologEpilogCodeInserter());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000268 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000269
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 // Second pass scheduler.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000271 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000272 PM.add(createPostRAScheduler());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000273 printAndVerify(PM);
Dan Gohmana2fa48e2008-11-20 19:54:21 +0000274 }
275
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000276 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000277 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000278 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000279 printAndVerify(PM);
280 }
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000281
Gordon Henriksen36464772008-01-07 01:33:09 +0000282 PM.add(createGCMachineCodeAnalysisPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000283 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000284
Gordon Henriksen36464772008-01-07 01:33:09 +0000285 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000286 PM.add(createGCInfoPrinter(*cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000288 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000289}