blob: d3e8e4904fee2efc49a03614291728b2f5ee5dce [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 Henriksen93f96d02008-01-07 01:33:09 +000020#include "llvm/CodeGen/Collector.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"
Chris Lattner47877052006-09-04 04:16:09 +000025using namespace llvm;
26
Chris Lattner85ef2542007-06-19 05:47:49 +000027static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
28 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
29static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
30 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000031static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
32 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000033static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
34 cl::desc("Dump garbage collector data"));
Chris Lattner85ef2542007-06-19 05:47:49 +000035
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000036// Hidden options to help debugging
37static cl::opt<bool>
38EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
39 cl::desc("Perform sinking on machine code"));
Bill Wendlingcc8f6032008-01-04 08:11:03 +000040static cl::opt<bool>
Evan Cheng3f32d652008-06-04 09:18:41 +000041EnableStackColoring("stack-coloring",
Evan Cheng75bb7342008-06-04 18:09:20 +000042 cl::init(false), cl::Hidden,
Evan Cheng3f32d652008-06-04 09:18:41 +000043 cl::desc("Perform stack slot coloring"));
Evan Chengd703ed62008-02-28 23:29:57 +000044static cl::opt<bool>
Evan Cheng3f32d652008-06-04 09:18:41 +000045EnableLICM("machine-licm",
46 cl::init(false), cl::Hidden,
47 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000048
Chris Lattner459525d2008-01-14 19:00:06 +000049// When this works it will be on by default.
50static cl::opt<bool>
51DisablePostRAScheduler("disable-post-RA-scheduler",
52 cl::desc("Disable scheduling after register allocation"),
53 cl::init(true));
54
Bill Wendling04523ea2007-02-08 01:36:53 +000055FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000056LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +000057 std::ostream &Out,
58 CodeGenFileType FileType,
59 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000060 // Standard LLVM-Level Passes.
61
62 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000063 if (!Fast) {
64 PM.add(createLoopStrengthReducePass(getTargetLowering()));
65 if (PrintLSR)
Dan Gohman62c7b8c2008-03-25 21:38:12 +000066 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattner31442f92007-03-31 00:24:43 +000067 }
Chris Lattner47877052006-09-04 04:16:09 +000068
Gordon Henriksen93f96d02008-01-07 01:33:09 +000069 PM.add(createGCLoweringPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000070
Dale Johannesen1532f3d2008-04-02 00:25:04 +000071 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +000072 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000073
Chris Lattner47877052006-09-04 04:16:09 +000074 // Make sure that no unreachable blocks are instruction selected.
75 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000076
Chris Lattnerc8d288f2007-03-31 04:18:03 +000077 if (!Fast)
78 PM.add(createCodeGenPreparePass(getTargetLowering()));
79
80 if (PrintISelInput)
Dan Gohman62c7b8c2008-03-25 21:38:12 +000081 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerc8d288f2007-03-31 04:18:03 +000082 &cerr));
83
Chris Lattner47877052006-09-04 04:16:09 +000084 // Ask the target for an isel.
85 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000086 return FileModel::Error;
87
Chris Lattner47877052006-09-04 04:16:09 +000088 // Print the instruction selected machine code...
89 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000090 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000091
Evan Cheng3f32d652008-06-04 09:18:41 +000092 if (EnableLICM)
Bill Wendlingcc8f6032008-01-04 08:11:03 +000093 PM.add(createMachineLICMPass());
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000094
95 if (EnableSinking)
96 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +000097
Anton Korobeynikov769b4812008-04-23 18:22:28 +000098 // Run pre-ra passes.
99 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
100 PM.add(createMachineFunctionPrinterPass(cerr));
101
Chris Lattner47877052006-09-04 04:16:09 +0000102 // Perform register allocation to convert to a concrete x86 representation
103 PM.add(createRegisterAllocator());
104
Evan Cheng3f32d652008-06-04 09:18:41 +0000105 // Perform stack slot coloring.
106 if (EnableStackColoring)
107 PM.add(createStackSlotColoringPass());
108
109 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000110 PM.add(createMachineFunctionPrinterPass(cerr));
Evan Cheng3f32d652008-06-04 09:18:41 +0000111
112 // Run post-ra passes.
113 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
114 PM.add(createMachineFunctionPrinterPass(cerr));
115
Christopher Lambada779f2007-07-27 07:36:14 +0000116 PM.add(createLowerSubregsPass());
117
118 if (PrintMachineCode) // Print the subreg lowered code
119 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000120
Chris Lattner47877052006-09-04 04:16:09 +0000121 // Insert prolog/epilog code. Eliminate abstract frame index references...
122 PM.add(createPrologEpilogCodeInserter());
123
Evan Cheng3f32d652008-06-04 09:18:41 +0000124 if (PrintMachineCode)
125 PM.add(createMachineFunctionPrinterPass(cerr));
126
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000127 // Second pass scheduler.
Chris Lattner459525d2008-01-14 19:00:06 +0000128 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000129 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000130
Chris Lattner4a84ad72006-10-13 20:45:56 +0000131 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +0000132 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000133 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000134
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000135 PM.add(createGCMachineCodeAnalysisPass());
136 if (PrintMachineCode)
137 PM.add(createMachineFunctionPrinterPass(cerr));
138
139 if (PrintGCInfo)
140 PM.add(createCollectorMetadataPrinter(*cerr));
141
Jim Laskey9d4209f2006-11-07 19:33:46 +0000142 // Fold redundant debug labels.
143 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000144
Chris Lattner47877052006-09-04 04:16:09 +0000145 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000146 PM.add(createMachineFunctionPrinterPass(cerr));
147
Chris Lattner47877052006-09-04 04:16:09 +0000148 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000149 PM.add(createMachineFunctionPrinterPass(cerr));
150
Evan Cheng3f32d652008-06-04 09:18:41 +0000151 if (!Fast && !OptimizeForSize)
Evan Chengd703ed62008-02-28 23:29:57 +0000152 PM.add(createLoopAlignerPass());
153
Chris Lattner47877052006-09-04 04:16:09 +0000154 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000155 default:
156 break;
157 case TargetMachine::AssemblyFile:
158 if (addAssemblyEmitter(PM, Fast, Out))
159 return FileModel::Error;
160 return FileModel::AsmFile;
161 case TargetMachine::ObjectFile:
162 if (getMachOWriterInfo())
163 return FileModel::MachOFile;
164 else if (getELFWriterInfo())
165 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000166 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000167
168 return FileModel::Error;
169}
170
171/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
172/// be split up (e.g., to add an object writer pass), this method can be used to
173/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +0000174bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000175 MachineCodeEmitter *MCE,
176 bool Fast) {
177 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000178 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000179
180 PM.add(createCollectorMetadataDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000181
Chris Lattner47877052006-09-04 04:16:09 +0000182 // Delete machine code for this function
183 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000184
Chris Lattner47877052006-09-04 04:16:09 +0000185 return false; // success!
186}
187
188/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
189/// get machine code emitted. This uses a MachineCodeEmitter object to handle
190/// actually outputting the machine code and resolving things like the address
191/// of functions. This method should returns true if machine code emission is
192/// not supported.
193///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000194bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000195 MachineCodeEmitter &MCE,
196 bool Fast) {
197 // Standard LLVM-Level Passes.
198
199 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000200 if (!Fast) {
201 PM.add(createLoopStrengthReducePass(getTargetLowering()));
202 if (PrintLSR)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000203 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000204 }
Chris Lattner47877052006-09-04 04:16:09 +0000205
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000206 PM.add(createGCLoweringPass());
Chris Lattner47877052006-09-04 04:16:09 +0000207
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000208 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000209 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000210
211 // Make sure that no unreachable blocks are instruction selected.
212 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000213
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000214 if (!Fast)
215 PM.add(createCodeGenPreparePass(getTargetLowering()));
216
217 if (PrintISelInput)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000218 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000219 &cerr));
220
Chris Lattner47877052006-09-04 04:16:09 +0000221 // Ask the target for an isel.
222 if (addInstSelector(PM, Fast))
223 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000224
Chris Lattner47877052006-09-04 04:16:09 +0000225 // Print the instruction selected machine code...
226 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000227 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000228
Evan Cheng3f32d652008-06-04 09:18:41 +0000229 if (EnableLICM)
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000230 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000231
232 if (EnableSinking)
233 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000234
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000235 // Run pre-ra passes.
236 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
237 PM.add(createMachineFunctionPrinterPass(cerr));
238
Evan Cheng3f32d652008-06-04 09:18:41 +0000239 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000240 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000241
242 // Perform stack slot coloring.
243 if (EnableStackColoring)
244 PM.add(createStackSlotColoringPass());
245
Chris Lattner47877052006-09-04 04:16:09 +0000246 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000247 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000248
Evan Cheng3f32d652008-06-04 09:18:41 +0000249 // Run post-ra passes.
250 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
251 PM.add(createMachineFunctionPrinterPass(cerr));
252
253 if (PrintMachineCode) // Print the register-allocated code
254 PM.add(createMachineFunctionPrinterPass(cerr));
255
Christopher Lambada779f2007-07-27 07:36:14 +0000256 PM.add(createLowerSubregsPass());
257
258 if (PrintMachineCode) // Print the subreg lowered code
259 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000260
Chris Lattner47877052006-09-04 04:16:09 +0000261 // Insert prolog/epilog code. Eliminate abstract frame index references...
262 PM.add(createPrologEpilogCodeInserter());
263
Evan Cheng3f32d652008-06-04 09:18:41 +0000264 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000265 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000266
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000267 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000268 if (!Fast)
269 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000270
Chris Lattnere01eaa02006-11-16 01:00:07 +0000271 // Branch folding must be run after regalloc and prolog/epilog insertion.
272 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000273 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000274
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000275 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000276
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000277 if (PrintMachineCode)
278 PM.add(createMachineFunctionPrinterPass(cerr));
279
280 if (PrintGCInfo)
281 PM.add(createCollectorMetadataPrinter(*cerr));
282
Chris Lattner47877052006-09-04 04:16:09 +0000283 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000284 PM.add(createMachineFunctionPrinterPass(cerr));
285
Evan Cheng8bd60352007-07-20 21:56:13 +0000286 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000287
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000288 PM.add(createCollectorMetadataDeleter());
289
Chris Lattner47877052006-09-04 04:16:09 +0000290 // Delete machine code for this function
291 PM.add(createMachineCodeDeleter());
292
293 return false; // success!
294}