blob: 7def8fa61d2a3c57f825a4c7a656519e41b07b7c [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"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
Bill Wendling7576d7b2009-02-08 00:58:05 +000041// Hidden options to help debugging
42static cl::opt<bool>
43EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
44 cl::desc("Perform sinking on machine code"));
45
Chris Lattnere06d8eb2008-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 Gohmane3769ef2008-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 Gohman6d7ee012008-10-07 23:00:56 +000055static cl::opt<cl::boolOrDefault>
Dan Gohmane3769ef2008-10-01 20:39:19 +000056EnableFastISelOption("fast-isel", cl::Hidden,
57 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman6a9b05f2008-09-25 01:14:49 +000058
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000060LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Anderson847b99b2008-08-21 00:14:44 +000061 raw_ostream &Out,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 CodeGenFileType FileType,
63 bool Fast) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +000064 // Add common CodeGen passes.
65 if (addCommonCodeGenPasses(PM, Fast))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 return FileModel::Error;
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 // Fold redundant debug labels.
69 PM.add(createDebugLabelFoldingPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +000070
71 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072 PM.add(createMachineFunctionPrinterPass(cerr));
73
74 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
75 PM.add(createMachineFunctionPrinterPass(cerr));
76
Devang Patel93698d92008-10-01 23:18:38 +000077 if (!Fast)
Evan Cheng7e29ba02008-02-28 23:29:57 +000078 PM.add(createLoopAlignerPass());
79
Dan Gohmanf17a25c2007-07-18 16:29:46 +000080 switch (FileType) {
81 default:
82 break;
83 case TargetMachine::AssemblyFile:
84 if (addAssemblyEmitter(PM, Fast, Out))
85 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;
92 }
93
94 return FileModel::Error;
95}
Dan Gohman7e71ccf2008-09-25 00:37:07 +000096
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
98/// be split up (e.g., to add an object writer pass), this method can be used to
99/// finish up adding passes to emit the file, if necessary.
Dan Gohmane34aa772008-03-11 22:29:46 +0000100bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 MachineCodeEmitter *MCE,
102 bool Fast) {
103 if (MCE)
Evan Cheng77547212007-07-20 21:56:13 +0000104 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000105
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000106 PM.add(createGCInfoDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107
108 // Delete machine code for this function
109 PM.add(createMachineCodeDeleter());
110
111 return false; // success!
112}
113
114/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
115/// get machine code emitted. This uses a MachineCodeEmitter object to handle
116/// actually outputting the machine code and resolving things like the address
117/// of functions. This method should returns true if machine code emission is
118/// not supported.
119///
Dan Gohmane34aa772008-03-11 22:29:46 +0000120bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 MachineCodeEmitter &MCE,
122 bool Fast) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000123 // Add common CodeGen passes.
124 if (addCommonCodeGenPasses(PM, Fast))
125 return true;
126
127 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
128 PM.add(createMachineFunctionPrinterPass(cerr));
129
130 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
131
132 PM.add(createGCInfoDeleter());
133
134 // Delete machine code for this function
135 PM.add(createMachineCodeDeleter());
136
137 return false; // success!
138}
139
140/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
141/// both emitting to assembly files or machine code output.
142///
143bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 // Standard LLVM-Level Passes.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000145
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000146 // Run loop strength reduction before anything else.
147 if (!Fast) {
148 PM.add(createLoopStrengthReducePass(getTargetLowering()));
149 if (PrintLSR)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000150 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000151 }
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000152
Gordon Henriksen36464772008-01-07 01:33:09 +0000153 PM.add(createGCLoweringPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000154
Dale Johannesen85535762008-04-02 00:25:04 +0000155 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +0000156 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000157
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 // Make sure that no unreachable blocks are instruction selected.
159 PM.add(createUnreachableBlockEliminationPass());
160
161 if (!Fast)
162 PM.add(createCodeGenPreparePass(getTargetLowering()));
163
Bill Wendling3e13ce52008-11-13 01:02:14 +0000164 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendlingdac9f712008-11-04 02:10:20 +0000165
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 if (PrintISelInput)
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000167 PM.add(createPrintFunctionPass("\n\n"
168 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000169 &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000171 // Standard Lower-Level Passes.
172
Dan Gohmane3769ef2008-10-01 20:39:19 +0000173 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman6d7ee012008-10-07 23:00:56 +0000174 if (EnableFastISelOption == cl::BOU_TRUE ||
175 (Fast && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmane3769ef2008-10-01 20:39:19 +0000176 EnableFastISel = true;
177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 // Ask the target for an isel.
179 if (addInstSelector(PM, Fast))
180 return true;
181
182 // Print the instruction selected machine code...
183 if (PrintMachineCode)
184 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000185
Bill Wendling7576d7b2009-02-08 00:58:05 +0000186 if (!Fast)
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000187 PM.add(createMachineLICMPass());
Bill Wendling7576d7b2009-02-08 00:58:05 +0000188
189 if (EnableSinking)
Chris Lattnera132dd42008-01-05 06:14:16 +0000190 PM.add(createMachineSinkingPass());
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000191
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000192 // Run pre-ra passes.
193 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
194 PM.add(createMachineFunctionPrinterPass(cerr));
195
Evan Cheng14f8a502008-06-04 09:18:41 +0000196 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000197 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000198
199 // Perform stack slot coloring.
Evan Cheng2ea55502008-06-30 22:33:16 +0000200 if (!Fast)
201 PM.add(createStackSlotColoringPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000202
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000203 if (PrintMachineCode) // Print the register-allocated code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000205
Evan Cheng14f8a502008-06-04 09:18:41 +0000206 // Run post-ra passes.
207 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
208 PM.add(createMachineFunctionPrinterPass(cerr));
209
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000210 if (PrintMachineCode)
Evan Cheng14f8a502008-06-04 09:18:41 +0000211 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000212
Christopher Lambed379732007-07-27 07:36:14 +0000213 PM.add(createLowerSubregsPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000214
Christopher Lambed379732007-07-27 07:36:14 +0000215 if (PrintMachineCode) // Print the subreg lowered code
216 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 // Insert prolog/epilog code. Eliminate abstract frame index references...
219 PM.add(createPrologEpilogCodeInserter());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000220
Evan Cheng14f8a502008-06-04 09:18:41 +0000221 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000223
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000224 // Second pass scheduler.
Dan Gohmana2fa48e2008-11-20 19:54:21 +0000225 if (!Fast && !DisablePostRAScheduler) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 PM.add(createPostRAScheduler());
227
Dan Gohmana2fa48e2008-11-20 19:54:21 +0000228 if (PrintMachineCode)
229 PM.add(createMachineFunctionPrinterPass(cerr));
230 }
231
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000232 // Branch folding must be run after regalloc and prolog/epilog insertion.
233 if (!Fast)
234 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
235
236 if (PrintMachineCode)
237 PM.add(createMachineFunctionPrinterPass(cerr));
238
Gordon Henriksen36464772008-01-07 01:33:09 +0000239 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000240
Gordon Henriksen36464772008-01-07 01:33:09 +0000241 if (PrintMachineCode)
242 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000243
Gordon Henriksen36464772008-01-07 01:33:09 +0000244 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000245 PM.add(createGCInfoPrinter(*cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000247 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248}