blob: 938e1ae9cc1a09d2f94615634b884a60e2bb2bf2 [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 Henriksen5a29c9e2008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.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 +000041EnableLICM("machine-licm",
42 cl::init(false), cl::Hidden,
43 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000044
Chris Lattner459525d2008-01-14 19:00:06 +000045// When this works it will be on by default.
46static cl::opt<bool>
47DisablePostRAScheduler("disable-post-RA-scheduler",
48 cl::desc("Disable scheduling after register allocation"),
49 cl::init(true));
50
Bill Wendling04523ea2007-02-08 01:36:53 +000051FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000052LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +000053 std::ostream &Out,
54 CodeGenFileType FileType,
55 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000056 // Standard LLVM-Level Passes.
57
58 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000059 if (!Fast) {
60 PM.add(createLoopStrengthReducePass(getTargetLowering()));
61 if (PrintLSR)
Dan Gohman62c7b8c2008-03-25 21:38:12 +000062 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattner31442f92007-03-31 00:24:43 +000063 }
Chris Lattner47877052006-09-04 04:16:09 +000064
Gordon Henriksen93f96d02008-01-07 01:33:09 +000065 PM.add(createGCLoweringPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000066
Dale Johannesen1532f3d2008-04-02 00:25:04 +000067 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +000068 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000069
Chris Lattner47877052006-09-04 04:16:09 +000070 // Make sure that no unreachable blocks are instruction selected.
71 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000072
Chris Lattnerc8d288f2007-03-31 04:18:03 +000073 if (!Fast)
74 PM.add(createCodeGenPreparePass(getTargetLowering()));
75
76 if (PrintISelInput)
Dan Gohman62c7b8c2008-03-25 21:38:12 +000077 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerc8d288f2007-03-31 04:18:03 +000078 &cerr));
79
Chris Lattner47877052006-09-04 04:16:09 +000080 // Ask the target for an isel.
81 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000082 return FileModel::Error;
83
Chris Lattner47877052006-09-04 04:16:09 +000084 // Print the instruction selected machine code...
85 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000086 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000087
Evan Cheng3f32d652008-06-04 09:18:41 +000088 if (EnableLICM)
Bill Wendlingcc8f6032008-01-04 08:11:03 +000089 PM.add(createMachineLICMPass());
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000090
91 if (EnableSinking)
92 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +000093
Anton Korobeynikov769b4812008-04-23 18:22:28 +000094 // Run pre-ra passes.
95 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
96 PM.add(createMachineFunctionPrinterPass(cerr));
97
Chris Lattner47877052006-09-04 04:16:09 +000098 // Perform register allocation to convert to a concrete x86 representation
99 PM.add(createRegisterAllocator());
100
Evan Cheng3f32d652008-06-04 09:18:41 +0000101 // Perform stack slot coloring.
Evan Cheng9ef4c532008-06-30 22:33:16 +0000102 if (!Fast)
103 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000104
105 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000106 PM.add(createMachineFunctionPrinterPass(cerr));
Evan Cheng3f32d652008-06-04 09:18:41 +0000107
108 // Run post-ra passes.
109 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
110 PM.add(createMachineFunctionPrinterPass(cerr));
111
Christopher Lambada779f2007-07-27 07:36:14 +0000112 PM.add(createLowerSubregsPass());
113
114 if (PrintMachineCode) // Print the subreg lowered code
115 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000116
Chris Lattner47877052006-09-04 04:16:09 +0000117 // Insert prolog/epilog code. Eliminate abstract frame index references...
118 PM.add(createPrologEpilogCodeInserter());
119
Evan Cheng3f32d652008-06-04 09:18:41 +0000120 if (PrintMachineCode)
121 PM.add(createMachineFunctionPrinterPass(cerr));
122
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000123 // Second pass scheduler.
Chris Lattner459525d2008-01-14 19:00:06 +0000124 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000125 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000126
Chris Lattner4a84ad72006-10-13 20:45:56 +0000127 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +0000128 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000129 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000130
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000131 PM.add(createGCMachineCodeAnalysisPass());
132 if (PrintMachineCode)
133 PM.add(createMachineFunctionPrinterPass(cerr));
134
135 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000136 PM.add(createGCInfoPrinter(*cerr));
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000137
Jim Laskey9d4209f2006-11-07 19:33:46 +0000138 // Fold redundant debug labels.
139 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000140
Chris Lattner47877052006-09-04 04:16:09 +0000141 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000142 PM.add(createMachineFunctionPrinterPass(cerr));
143
Chris Lattner47877052006-09-04 04:16:09 +0000144 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000145 PM.add(createMachineFunctionPrinterPass(cerr));
146
Evan Cheng3f32d652008-06-04 09:18:41 +0000147 if (!Fast && !OptimizeForSize)
Evan Chengd703ed62008-02-28 23:29:57 +0000148 PM.add(createLoopAlignerPass());
149
Chris Lattner47877052006-09-04 04:16:09 +0000150 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000151 default:
152 break;
153 case TargetMachine::AssemblyFile:
154 if (addAssemblyEmitter(PM, Fast, Out))
155 return FileModel::Error;
156 return FileModel::AsmFile;
157 case TargetMachine::ObjectFile:
158 if (getMachOWriterInfo())
159 return FileModel::MachOFile;
160 else if (getELFWriterInfo())
161 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000162 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000163
164 return FileModel::Error;
165}
166
167/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
168/// be split up (e.g., to add an object writer pass), this method can be used to
169/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +0000170bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000171 MachineCodeEmitter *MCE,
172 bool Fast) {
173 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000174 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000175
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000176 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000177
Chris Lattner47877052006-09-04 04:16:09 +0000178 // Delete machine code for this function
179 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000180
Chris Lattner47877052006-09-04 04:16:09 +0000181 return false; // success!
182}
183
184/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
185/// get machine code emitted. This uses a MachineCodeEmitter object to handle
186/// actually outputting the machine code and resolving things like the address
187/// of functions. This method should returns true if machine code emission is
188/// not supported.
189///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000190bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000191 MachineCodeEmitter &MCE,
192 bool Fast) {
193 // Standard LLVM-Level Passes.
194
195 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000196 if (!Fast) {
197 PM.add(createLoopStrengthReducePass(getTargetLowering()));
198 if (PrintLSR)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000199 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000200 }
Chris Lattner47877052006-09-04 04:16:09 +0000201
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000202 PM.add(createGCLoweringPass());
Chris Lattner47877052006-09-04 04:16:09 +0000203
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000204 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000205 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000206
207 // Make sure that no unreachable blocks are instruction selected.
208 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000209
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000210 if (!Fast)
211 PM.add(createCodeGenPreparePass(getTargetLowering()));
212
213 if (PrintISelInput)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000214 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000215 &cerr));
216
Chris Lattner47877052006-09-04 04:16:09 +0000217 // Ask the target for an isel.
218 if (addInstSelector(PM, Fast))
219 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000220
Chris Lattner47877052006-09-04 04:16:09 +0000221 // Print the instruction selected machine code...
222 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000223 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000224
Evan Cheng3f32d652008-06-04 09:18:41 +0000225 if (EnableLICM)
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000226 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000227
228 if (EnableSinking)
229 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000230
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000231 // Run pre-ra passes.
232 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
233 PM.add(createMachineFunctionPrinterPass(cerr));
234
Evan Cheng3f32d652008-06-04 09:18:41 +0000235 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000236 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000237
238 // Perform stack slot coloring.
Evan Cheng9ef4c532008-06-30 22:33:16 +0000239 if (!Fast)
240 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000241
Chris Lattner47877052006-09-04 04:16:09 +0000242 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000243 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000244
Evan Cheng3f32d652008-06-04 09:18:41 +0000245 // Run post-ra passes.
246 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
247 PM.add(createMachineFunctionPrinterPass(cerr));
248
249 if (PrintMachineCode) // Print the register-allocated code
250 PM.add(createMachineFunctionPrinterPass(cerr));
251
Christopher Lambada779f2007-07-27 07:36:14 +0000252 PM.add(createLowerSubregsPass());
253
254 if (PrintMachineCode) // Print the subreg lowered code
255 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000256
Chris Lattner47877052006-09-04 04:16:09 +0000257 // Insert prolog/epilog code. Eliminate abstract frame index references...
258 PM.add(createPrologEpilogCodeInserter());
259
Evan Cheng3f32d652008-06-04 09:18:41 +0000260 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000261 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000262
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000263 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000264 if (!Fast)
265 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000266
Chris Lattnere01eaa02006-11-16 01:00:07 +0000267 // Branch folding must be run after regalloc and prolog/epilog insertion.
268 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000269 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000270
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000271 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000272
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000273 if (PrintMachineCode)
274 PM.add(createMachineFunctionPrinterPass(cerr));
275
276 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000277 PM.add(createGCInfoPrinter(*cerr));
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000278
Chris Lattner47877052006-09-04 04:16:09 +0000279 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000280 PM.add(createMachineFunctionPrinterPass(cerr));
281
Evan Cheng8bd60352007-07-20 21:56:13 +0000282 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000283
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000284 PM.add(createGCInfoDeleter());
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000285
Chris Lattner47877052006-09-04 04:16:09 +0000286 // Delete machine code for this function
287 PM.add(createMachineCodeDeleter());
288
289 return false; // success!
290}