blob: 7def8fa61d2a3c57f825a4c7a656519e41b07b7c [file] [log] [blame]
Chris Lattnera916db12006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnera916db12006-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 Lattnerbafc8372007-03-31 00:24:43 +000017#include "llvm/Assembly/PrintModulePass.h"
Devang Patelb0743b52007-03-06 21:14:09 +000018#include "llvm/Analysis/LoopPass.h"
Chris Lattnera916db12006-09-04 04:16:09 +000019#include "llvm/CodeGen/Passes.h"
Gordon Henriksenbcef14d2008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.h"
Chris Lattnera916db12006-09-04 04:16:09 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesenfd967cf2008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnera916db12006-09-04 04:16:09 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000024#include "llvm/Support/CommandLine.h"
Owen Anderson93719642008-08-21 00:14:44 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattnera916db12006-09-04 04:16:09 +000026using namespace llvm;
27
Dan Gohmanb8e69f12008-09-25 01:14:49 +000028namespace llvm {
29 bool EnableFastISel;
30}
31
Chris Lattner37228f62007-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 Cheng9d5df0a2007-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 Henriksen2d684b12008-01-07 01:33:09 +000038static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39 cl::desc("Dump garbage collector data"));
Chris Lattner37228f62007-06-19 05:47:49 +000040
Bill Wendling5469ec12009-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 Lattner99471842008-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 Gohman3b88f102008-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 Gohman60ad1732008-10-07 23:00:56 +000055static cl::opt<cl::boolOrDefault>
Dan Gohman3b88f102008-10-01 20:39:19 +000056EnableFastISelOption("fast-isel", cl::Hidden,
57 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohmanb8e69f12008-09-25 01:14:49 +000058
Bill Wendling523048e2007-02-08 01:36:53 +000059FileModel::Model
Dan Gohman24570832008-03-11 22:29:46 +000060LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Anderson93719642008-08-21 00:14:44 +000061 raw_ostream &Out,
Bill Wendling523048e2007-02-08 01:36:53 +000062 CodeGenFileType FileType,
63 bool Fast) {
Dan Gohmanacb05542008-09-25 00:37:07 +000064 // Add common CodeGen passes.
65 if (addCommonCodeGenPasses(PM, Fast))
Bill Wendling523048e2007-02-08 01:36:53 +000066 return FileModel::Error;
67
Jim Laskey6ea4fae2006-11-07 19:33:46 +000068 // Fold redundant debug labels.
69 PM.add(createDebugLabelFoldingPass());
Dan Gohmanacb05542008-09-25 00:37:07 +000070
71 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +000072 PM.add(createMachineFunctionPrinterPass(cerr));
73
Chris Lattnera916db12006-09-04 04:16:09 +000074 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +000075 PM.add(createMachineFunctionPrinterPass(cerr));
76
Devang Patel1b76f2c2008-10-01 23:18:38 +000077 if (!Fast)
Evan Cheng95a7be42008-02-28 23:29:57 +000078 PM.add(createLoopAlignerPass());
79
Chris Lattnera916db12006-09-04 04:16:09 +000080 switch (FileType) {
Bill Wendling523048e2007-02-08 01:36:53 +000081 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;
Chris Lattnera916db12006-09-04 04:16:09 +000092 }
Bill Wendling523048e2007-02-08 01:36:53 +000093
94 return FileModel::Error;
95}
Dan Gohmanacb05542008-09-25 00:37:07 +000096
Bill Wendling523048e2007-02-08 01:36:53 +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 Gohman24570832008-03-11 22:29:46 +0000100bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling523048e2007-02-08 01:36:53 +0000101 MachineCodeEmitter *MCE,
102 bool Fast) {
103 if (MCE)
Evan Cheng9d5df0a2007-07-20 21:56:13 +0000104 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohmanacb05542008-09-25 00:37:07 +0000105
Gordon Henriksend930f912008-08-17 18:44:35 +0000106 PM.add(createGCInfoDeleter());
Bill Wendling523048e2007-02-08 01:36:53 +0000107
Chris Lattnera916db12006-09-04 04:16:09 +0000108 // Delete machine code for this function
109 PM.add(createMachineCodeDeleter());
Bill Wendling523048e2007-02-08 01:36:53 +0000110
Chris Lattnera916db12006-09-04 04:16:09 +0000111 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 Gohman24570832008-03-11 22:29:46 +0000120bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattnera916db12006-09-04 04:16:09 +0000121 MachineCodeEmitter &MCE,
122 bool Fast) {
Dan Gohmanacb05542008-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) {
Chris Lattnera916db12006-09-04 04:16:09 +0000144 // Standard LLVM-Level Passes.
Dan Gohmanacb05542008-09-25 00:37:07 +0000145
Chris Lattnera916db12006-09-04 04:16:09 +0000146 // Run loop strength reduction before anything else.
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000147 if (!Fast) {
148 PM.add(createLoopStrengthReducePass(getTargetLowering()));
149 if (PrintLSR)
Daniel Dunbar81b5fa52008-10-22 03:25:22 +0000150 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000151 }
Dan Gohmanacb05542008-09-25 00:37:07 +0000152
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000153 PM.add(createGCLoweringPass());
Dan Gohmanacb05542008-09-25 00:37:07 +0000154
Dale Johannesenfd967cf2008-04-02 00:25:04 +0000155 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen5e4e0512008-04-01 20:00:57 +0000156 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohmanacb05542008-09-25 00:37:07 +0000157
Chris Lattnera916db12006-09-04 04:16:09 +0000158 // Make sure that no unreachable blocks are instruction selected.
159 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling523048e2007-02-08 01:36:53 +0000160
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000161 if (!Fast)
162 PM.add(createCodeGenPreparePass(getTargetLowering()));
163
Bill Wendlingccb67a3d2008-11-13 01:02:14 +0000164 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendling05d84172008-11-04 02:10:20 +0000165
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000166 if (PrintISelInput)
Daniel Dunbar54d5b9e2008-10-21 23:33:38 +0000167 PM.add(createPrintFunctionPass("\n\n"
168 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar81b5fa52008-10-22 03:25:22 +0000169 &errs()));
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000170
Dan Gohmanacb05542008-09-25 00:37:07 +0000171 // Standard Lower-Level Passes.
172
Dan Gohman3b88f102008-10-01 20:39:19 +0000173 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman60ad1732008-10-07 23:00:56 +0000174 if (EnableFastISelOption == cl::BOU_TRUE ||
175 (Fast && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohman3b88f102008-10-01 20:39:19 +0000176 EnableFastISel = true;
177
Chris Lattnera916db12006-09-04 04:16:09 +0000178 // Ask the target for an isel.
179 if (addInstSelector(PM, Fast))
180 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000181
Chris Lattnera916db12006-09-04 04:16:09 +0000182 // Print the instruction selected machine code...
183 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000184 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000185
Bill Wendling5469ec12009-02-08 00:58:05 +0000186 if (!Fast)
Bill Wendling66470d02008-01-04 08:11:03 +0000187 PM.add(createMachineLICMPass());
Bill Wendling5469ec12009-02-08 00:58:05 +0000188
189 if (EnableSinking)
Chris Lattner276178e2008-01-05 06:14:16 +0000190 PM.add(createMachineSinkingPass());
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000191
Anton Korobeynikov0516b6f2008-04-23 18:26:03 +0000192 // Run pre-ra passes.
193 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
194 PM.add(createMachineFunctionPrinterPass(cerr));
195
Evan Cheng12a02222008-06-04 09:18:41 +0000196 // Perform register allocation.
Chris Lattnera916db12006-09-04 04:16:09 +0000197 PM.add(createRegisterAllocator());
Evan Cheng12a02222008-06-04 09:18:41 +0000198
199 // Perform stack slot coloring.
Evan Cheng6a323e12008-06-30 22:33:16 +0000200 if (!Fast)
201 PM.add(createStackSlotColoringPass());
Evan Cheng12a02222008-06-04 09:18:41 +0000202
Dan Gohmanacb05542008-09-25 00:37:07 +0000203 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling523048e2007-02-08 01:36:53 +0000204 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanacb05542008-09-25 00:37:07 +0000205
Evan Cheng12a02222008-06-04 09:18:41 +0000206 // Run post-ra passes.
207 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
208 PM.add(createMachineFunctionPrinterPass(cerr));
209
Dan Gohmanacb05542008-09-25 00:37:07 +0000210 if (PrintMachineCode)
Evan Cheng12a02222008-06-04 09:18:41 +0000211 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanacb05542008-09-25 00:37:07 +0000212
Christopher Lamb14bbb152007-07-27 07:36:14 +0000213 PM.add(createLowerSubregsPass());
Dan Gohmanacb05542008-09-25 00:37:07 +0000214
Christopher Lamb14bbb152007-07-27 07:36:14 +0000215 if (PrintMachineCode) // Print the subreg lowered code
216 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling523048e2007-02-08 01:36:53 +0000217
Chris Lattnera916db12006-09-04 04:16:09 +0000218 // Insert prolog/epilog code. Eliminate abstract frame index references...
219 PM.add(createPrologEpilogCodeInserter());
Dan Gohmanacb05542008-09-25 00:37:07 +0000220
Evan Cheng12a02222008-06-04 09:18:41 +0000221 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000222 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanacb05542008-09-25 00:37:07 +0000223
Dale Johannesen2182f062007-07-13 17:13:54 +0000224 // Second pass scheduler.
Dan Gohman06613bc2008-11-20 19:54:21 +0000225 if (!Fast && !DisablePostRAScheduler) {
Dale Johannesen4dc35db2007-07-13 17:31:29 +0000226 PM.add(createPostRAScheduler());
Dale Johannesen2182f062007-07-13 17:13:54 +0000227
Dan Gohman06613bc2008-11-20 19:54:21 +0000228 if (PrintMachineCode)
229 PM.add(createMachineFunctionPrinterPass(cerr));
230 }
231
Dan Gohmanb0ef9142008-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 Henriksen2d684b12008-01-07 01:33:09 +0000239 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng12a02222008-06-04 09:18:41 +0000240
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000241 if (PrintMachineCode)
242 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanacb05542008-09-25 00:37:07 +0000243
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000244 if (PrintGCInfo)
Gordon Henriksend930f912008-08-17 18:44:35 +0000245 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling523048e2007-02-08 01:36:53 +0000246
Dan Gohmanacb05542008-09-25 00:37:07 +0000247 return false;
Chris Lattnera916db12006-09-04 04:16:09 +0000248}