blob: 4004cf1e1800a3e5b0f8eca818262089f6a64bf2 [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 Chengd703ed62008-02-28 23:29:57 +000041AlignLoops("align-loops", cl::init(true), cl::Hidden,
42 cl::desc("Align loop headers"));
43static cl::opt<bool>
Bill Wendlingcc8f6032008-01-04 08:11:03 +000044PerformLICM("machine-licm",
45 cl::init(false), cl::Hidden,
46 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000047
Chris Lattner459525d2008-01-14 19:00:06 +000048// When this works it will be on by default.
49static cl::opt<bool>
50DisablePostRAScheduler("disable-post-RA-scheduler",
51 cl::desc("Disable scheduling after register allocation"),
52 cl::init(true));
53
Bill Wendling04523ea2007-02-08 01:36:53 +000054FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000055LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +000056 std::ostream &Out,
57 CodeGenFileType FileType,
58 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000059 // Standard LLVM-Level Passes.
60
61 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000062 if (!Fast) {
63 PM.add(createLoopStrengthReducePass(getTargetLowering()));
64 if (PrintLSR)
Dan Gohman62c7b8c2008-03-25 21:38:12 +000065 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattner31442f92007-03-31 00:24:43 +000066 }
Chris Lattner47877052006-09-04 04:16:09 +000067
Gordon Henriksen93f96d02008-01-07 01:33:09 +000068 PM.add(createGCLoweringPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000069
Dale Johannesen1532f3d2008-04-02 00:25:04 +000070 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +000071 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000072
Chris Lattner47877052006-09-04 04:16:09 +000073 // Make sure that no unreachable blocks are instruction selected.
74 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000075
Chris Lattnerc8d288f2007-03-31 04:18:03 +000076 if (!Fast)
77 PM.add(createCodeGenPreparePass(getTargetLowering()));
78
79 if (PrintISelInput)
Dan Gohman62c7b8c2008-03-25 21:38:12 +000080 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerc8d288f2007-03-31 04:18:03 +000081 &cerr));
82
Chris Lattner47877052006-09-04 04:16:09 +000083 // Ask the target for an isel.
84 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000085 return FileModel::Error;
86
Chris Lattner47877052006-09-04 04:16:09 +000087 // Print the instruction selected machine code...
88 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000089 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000090
Bill Wendlingcc8f6032008-01-04 08:11:03 +000091 if (PerformLICM)
92 PM.add(createMachineLICMPass());
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000093
94 if (EnableSinking)
95 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +000096
Anton Korobeynikov769b4812008-04-23 18:22:28 +000097 // Run pre-ra passes.
98 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
99 PM.add(createMachineFunctionPrinterPass(cerr));
100
Chris Lattner47877052006-09-04 04:16:09 +0000101 // Perform register allocation to convert to a concrete x86 representation
102 PM.add(createRegisterAllocator());
103
104 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000105 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000106
107 PM.add(createLowerSubregsPass());
108
109 if (PrintMachineCode) // Print the subreg lowered code
110 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000111
Chris Lattner47877052006-09-04 04:16:09 +0000112 // Run post-ra passes.
113 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000114 PM.add(createMachineFunctionPrinterPass(cerr));
115
Chris Lattner47877052006-09-04 04:16:09 +0000116 // Insert prolog/epilog code. Eliminate abstract frame index references...
117 PM.add(createPrologEpilogCodeInserter());
118
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000119 // Second pass scheduler.
Chris Lattner459525d2008-01-14 19:00:06 +0000120 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000121 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000122
Chris Lattner4a84ad72006-10-13 20:45:56 +0000123 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +0000124 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000125 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000126
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000127 PM.add(createGCMachineCodeAnalysisPass());
128 if (PrintMachineCode)
129 PM.add(createMachineFunctionPrinterPass(cerr));
130
131 if (PrintGCInfo)
132 PM.add(createCollectorMetadataPrinter(*cerr));
133
Jim Laskey9d4209f2006-11-07 19:33:46 +0000134 // Fold redundant debug labels.
135 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000136
Chris Lattner47877052006-09-04 04:16:09 +0000137 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000138 PM.add(createMachineFunctionPrinterPass(cerr));
139
Chris Lattner47877052006-09-04 04:16:09 +0000140 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000141 PM.add(createMachineFunctionPrinterPass(cerr));
142
Devang Patel73ee9c32008-03-25 21:03:02 +0000143 if (AlignLoops && !OptimizeForSize)
Evan Chengd703ed62008-02-28 23:29:57 +0000144 PM.add(createLoopAlignerPass());
145
Chris Lattner47877052006-09-04 04:16:09 +0000146 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000147 default:
148 break;
149 case TargetMachine::AssemblyFile:
150 if (addAssemblyEmitter(PM, Fast, Out))
151 return FileModel::Error;
152 return FileModel::AsmFile;
153 case TargetMachine::ObjectFile:
154 if (getMachOWriterInfo())
155 return FileModel::MachOFile;
156 else if (getELFWriterInfo())
157 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000158 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000159
160 return FileModel::Error;
161}
162
163/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
164/// be split up (e.g., to add an object writer pass), this method can be used to
165/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +0000166bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000167 MachineCodeEmitter *MCE,
168 bool Fast) {
169 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000170 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000171
172 PM.add(createCollectorMetadataDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000173
Chris Lattner47877052006-09-04 04:16:09 +0000174 // Delete machine code for this function
175 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000176
Chris Lattner47877052006-09-04 04:16:09 +0000177 return false; // success!
178}
179
180/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
181/// get machine code emitted. This uses a MachineCodeEmitter object to handle
182/// actually outputting the machine code and resolving things like the address
183/// of functions. This method should returns true if machine code emission is
184/// not supported.
185///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000186bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000187 MachineCodeEmitter &MCE,
188 bool Fast) {
189 // Standard LLVM-Level Passes.
190
191 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000192 if (!Fast) {
193 PM.add(createLoopStrengthReducePass(getTargetLowering()));
194 if (PrintLSR)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000195 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000196 }
Chris Lattner47877052006-09-04 04:16:09 +0000197
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000198 PM.add(createGCLoweringPass());
Chris Lattner47877052006-09-04 04:16:09 +0000199
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000200 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000201 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000202
203 // Make sure that no unreachable blocks are instruction selected.
204 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000205
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000206 if (!Fast)
207 PM.add(createCodeGenPreparePass(getTargetLowering()));
208
209 if (PrintISelInput)
Dan Gohman62c7b8c2008-03-25 21:38:12 +0000210 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000211 &cerr));
212
Chris Lattner47877052006-09-04 04:16:09 +0000213 // Ask the target for an isel.
214 if (addInstSelector(PM, Fast))
215 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000216
Chris Lattner47877052006-09-04 04:16:09 +0000217 // Print the instruction selected machine code...
218 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000219 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000220
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000221 if (PerformLICM)
222 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000223
224 if (EnableSinking)
225 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000226
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000227 // Run pre-ra passes.
228 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
229 PM.add(createMachineFunctionPrinterPass(cerr));
230
Chris Lattner47877052006-09-04 04:16:09 +0000231 // Perform register allocation to convert to a concrete x86 representation
232 PM.add(createRegisterAllocator());
233
234 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000235 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000236
237 PM.add(createLowerSubregsPass());
238
239 if (PrintMachineCode) // Print the subreg lowered code
240 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000241
Chris Lattner47877052006-09-04 04:16:09 +0000242 // Run post-ra passes.
243 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000244 PM.add(createMachineFunctionPrinterPass(cerr));
245
Chris Lattner47877052006-09-04 04:16:09 +0000246 // Insert prolog/epilog code. Eliminate abstract frame index references...
247 PM.add(createPrologEpilogCodeInserter());
248
249 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000250 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000251
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000252 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000253 if (!Fast)
254 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000255
Chris Lattnere01eaa02006-11-16 01:00:07 +0000256 // Branch folding must be run after regalloc and prolog/epilog insertion.
257 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000258 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000259
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000260 PM.add(createGCMachineCodeAnalysisPass());
261 if (PrintMachineCode)
262 PM.add(createMachineFunctionPrinterPass(cerr));
263
264 if (PrintGCInfo)
265 PM.add(createCollectorMetadataPrinter(*cerr));
266
Chris Lattner47877052006-09-04 04:16:09 +0000267 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000268 PM.add(createMachineFunctionPrinterPass(cerr));
269
Evan Cheng8bd60352007-07-20 21:56:13 +0000270 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000271
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000272 PM.add(createCollectorMetadataDeleter());
273
Chris Lattner47877052006-09-04 04:16:09 +0000274 // Delete machine code for this function
275 PM.add(createMachineCodeDeleter());
276
277 return false; // success!
278}