blob: 33cf6b5489156e060b3c6a210b5f595b376a8fe0 [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"
22#include "llvm/Transforms/Scalar.h"
Chris Lattner31442f92007-03-31 00:24:43 +000023#include "llvm/Support/CommandLine.h"
Chris Lattner47877052006-09-04 04:16:09 +000024using namespace llvm;
25
Chris Lattner85ef2542007-06-19 05:47:49 +000026static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
27 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
28static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
29 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000030static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
31 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000032static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
33 cl::desc("Dump garbage collector data"));
Chris Lattner85ef2542007-06-19 05:47:49 +000034
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000035// Hidden options to help debugging
36static cl::opt<bool>
37EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
38 cl::desc("Perform sinking on machine code"));
Bill Wendlingcc8f6032008-01-04 08:11:03 +000039static cl::opt<bool>
Evan Chengd703ed62008-02-28 23:29:57 +000040AlignLoops("align-loops", cl::init(true), cl::Hidden,
41 cl::desc("Align loop headers"));
42static cl::opt<bool>
Bill Wendlingcc8f6032008-01-04 08:11:03 +000043PerformLICM("machine-licm",
44 cl::init(false), cl::Hidden,
45 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000046
Chris Lattner459525d2008-01-14 19:00:06 +000047// When this works it will be on by default.
48static cl::opt<bool>
49DisablePostRAScheduler("disable-post-RA-scheduler",
50 cl::desc("Disable scheduling after register allocation"),
51 cl::init(true));
52
Bill Wendling04523ea2007-02-08 01:36:53 +000053FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000054LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +000055 std::ostream &Out,
56 CodeGenFileType FileType,
57 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000058 // Standard LLVM-Level Passes.
59
60 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000061 if (!Fast) {
62 PM.add(createLoopStrengthReducePass(getTargetLowering()));
63 if (PrintLSR)
64 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
65 }
Chris Lattner47877052006-09-04 04:16:09 +000066
Gordon Henriksen93f96d02008-01-07 01:33:09 +000067 PM.add(createGCLoweringPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000068
Jim Laskeya4e7cd92007-02-22 16:22:15 +000069 if (!ExceptionHandling)
70 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000071
Chris Lattner47877052006-09-04 04:16:09 +000072 // Make sure that no unreachable blocks are instruction selected.
73 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000074
Chris Lattnerc8d288f2007-03-31 04:18:03 +000075 if (!Fast)
76 PM.add(createCodeGenPreparePass(getTargetLowering()));
77
78 if (PrintISelInput)
79 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
80 &cerr));
81
Chris Lattner47877052006-09-04 04:16:09 +000082 // Ask the target for an isel.
83 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000084 return FileModel::Error;
85
Chris Lattner47877052006-09-04 04:16:09 +000086 // Print the instruction selected machine code...
87 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000088 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000089
Bill Wendlingcc8f6032008-01-04 08:11:03 +000090 if (PerformLICM)
91 PM.add(createMachineLICMPass());
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000092
93 if (EnableSinking)
94 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +000095
Chris Lattner47877052006-09-04 04:16:09 +000096 // Perform register allocation to convert to a concrete x86 representation
97 PM.add(createRegisterAllocator());
98
99 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000100 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000101
102 PM.add(createLowerSubregsPass());
103
104 if (PrintMachineCode) // Print the subreg lowered code
105 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000106
Chris Lattner47877052006-09-04 04:16:09 +0000107 // Run post-ra passes.
108 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000109 PM.add(createMachineFunctionPrinterPass(cerr));
110
Chris Lattner47877052006-09-04 04:16:09 +0000111 // Insert prolog/epilog code. Eliminate abstract frame index references...
112 PM.add(createPrologEpilogCodeInserter());
113
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000114 // Second pass scheduler.
Chris Lattner459525d2008-01-14 19:00:06 +0000115 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000116 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000117
Chris Lattner4a84ad72006-10-13 20:45:56 +0000118 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +0000119 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000120 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000121
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000122 PM.add(createGCMachineCodeAnalysisPass());
123 if (PrintMachineCode)
124 PM.add(createMachineFunctionPrinterPass(cerr));
125
126 if (PrintGCInfo)
127 PM.add(createCollectorMetadataPrinter(*cerr));
128
Jim Laskey9d4209f2006-11-07 19:33:46 +0000129 // Fold redundant debug labels.
130 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000131
Chris Lattner47877052006-09-04 04:16:09 +0000132 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000133 PM.add(createMachineFunctionPrinterPass(cerr));
134
Chris Lattner47877052006-09-04 04:16:09 +0000135 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000136 PM.add(createMachineFunctionPrinterPass(cerr));
137
Evan Chengd703ed62008-02-28 23:29:57 +0000138 if (AlignLoops)
139 PM.add(createLoopAlignerPass());
140
Chris Lattner47877052006-09-04 04:16:09 +0000141 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000142 default:
143 break;
144 case TargetMachine::AssemblyFile:
145 if (addAssemblyEmitter(PM, Fast, Out))
146 return FileModel::Error;
147 return FileModel::AsmFile;
148 case TargetMachine::ObjectFile:
149 if (getMachOWriterInfo())
150 return FileModel::MachOFile;
151 else if (getELFWriterInfo())
152 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000153 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000154
155 return FileModel::Error;
156}
157
158/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
159/// be split up (e.g., to add an object writer pass), this method can be used to
160/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +0000161bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000162 MachineCodeEmitter *MCE,
163 bool Fast) {
164 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000165 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000166
167 PM.add(createCollectorMetadataDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000168
Chris Lattner47877052006-09-04 04:16:09 +0000169 // Delete machine code for this function
170 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000171
Chris Lattner47877052006-09-04 04:16:09 +0000172 return false; // success!
173}
174
175/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
176/// get machine code emitted. This uses a MachineCodeEmitter object to handle
177/// actually outputting the machine code and resolving things like the address
178/// of functions. This method should returns true if machine code emission is
179/// not supported.
180///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000181bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000182 MachineCodeEmitter &MCE,
183 bool Fast) {
184 // Standard LLVM-Level Passes.
185
186 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000187 if (!Fast) {
188 PM.add(createLoopStrengthReducePass(getTargetLowering()));
189 if (PrintLSR)
190 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
191 }
Chris Lattner47877052006-09-04 04:16:09 +0000192
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000193 PM.add(createGCLoweringPass());
Chris Lattner47877052006-09-04 04:16:09 +0000194
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000195 if (!ExceptionHandling)
196 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000197
198 // Make sure that no unreachable blocks are instruction selected.
199 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000200
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000201 if (!Fast)
202 PM.add(createCodeGenPreparePass(getTargetLowering()));
203
204 if (PrintISelInput)
205 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
206 &cerr));
207
Chris Lattner47877052006-09-04 04:16:09 +0000208 // Ask the target for an isel.
209 if (addInstSelector(PM, Fast))
210 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000211
Chris Lattner47877052006-09-04 04:16:09 +0000212 // Print the instruction selected machine code...
213 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000214 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000215
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000216 if (PerformLICM)
217 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000218
219 if (EnableSinking)
220 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000221
Chris Lattner47877052006-09-04 04:16:09 +0000222 // Perform register allocation to convert to a concrete x86 representation
223 PM.add(createRegisterAllocator());
224
225 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000226 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000227
228 PM.add(createLowerSubregsPass());
229
230 if (PrintMachineCode) // Print the subreg lowered code
231 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000232
Chris Lattner47877052006-09-04 04:16:09 +0000233 // Run post-ra passes.
234 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000235 PM.add(createMachineFunctionPrinterPass(cerr));
236
Chris Lattner47877052006-09-04 04:16:09 +0000237 // Insert prolog/epilog code. Eliminate abstract frame index references...
238 PM.add(createPrologEpilogCodeInserter());
239
240 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000241 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000242
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000243 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000244 if (!Fast)
245 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000246
Chris Lattnere01eaa02006-11-16 01:00:07 +0000247 // Branch folding must be run after regalloc and prolog/epilog insertion.
248 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000249 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000250
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000251 PM.add(createGCMachineCodeAnalysisPass());
252 if (PrintMachineCode)
253 PM.add(createMachineFunctionPrinterPass(cerr));
254
255 if (PrintGCInfo)
256 PM.add(createCollectorMetadataPrinter(*cerr));
257
Chris Lattner47877052006-09-04 04:16:09 +0000258 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000259 PM.add(createMachineFunctionPrinterPass(cerr));
260
Evan Cheng8bd60352007-07-20 21:56:13 +0000261 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000262
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000263 PM.add(createCollectorMetadataDeleter());
264
Chris Lattner47877052006-09-04 04:16:09 +0000265 // Delete machine code for this function
266 PM.add(createMachineCodeDeleter());
267
268 return false; // success!
269}