blob: 5611b03ba764f2bc3aea397f91da7700753e39d2 [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>
40PerformLICM("machine-licm",
41 cl::init(false), cl::Hidden,
42 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000043
Chris Lattner459525d2008-01-14 19:00:06 +000044// When this works it will be on by default.
45static cl::opt<bool>
46DisablePostRAScheduler("disable-post-RA-scheduler",
47 cl::desc("Disable scheduling after register allocation"),
48 cl::init(true));
49
Bill Wendling04523ea2007-02-08 01:36:53 +000050FileModel::Model
51LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
52 std::ostream &Out,
53 CodeGenFileType FileType,
54 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000055 // Standard LLVM-Level Passes.
56
57 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000058 if (!Fast) {
59 PM.add(createLoopStrengthReducePass(getTargetLowering()));
60 if (PrintLSR)
61 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
62 }
Chris Lattner47877052006-09-04 04:16:09 +000063
Gordon Henriksen93f96d02008-01-07 01:33:09 +000064 PM.add(createGCLoweringPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000065
Jim Laskeya4e7cd92007-02-22 16:22:15 +000066 if (!ExceptionHandling)
67 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000068
Chris Lattner47877052006-09-04 04:16:09 +000069 // Make sure that no unreachable blocks are instruction selected.
70 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000071
Chris Lattnerc8d288f2007-03-31 04:18:03 +000072 if (!Fast)
73 PM.add(createCodeGenPreparePass(getTargetLowering()));
74
75 if (PrintISelInput)
76 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
77 &cerr));
78
Chris Lattner47877052006-09-04 04:16:09 +000079 // Ask the target for an isel.
80 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000081 return FileModel::Error;
82
Chris Lattner47877052006-09-04 04:16:09 +000083 // Print the instruction selected machine code...
84 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000085 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000086
Bill Wendlingcc8f6032008-01-04 08:11:03 +000087 if (PerformLICM)
88 PM.add(createMachineLICMPass());
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000089
90 if (EnableSinking)
91 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +000092
Chris Lattner47877052006-09-04 04:16:09 +000093 // Perform register allocation to convert to a concrete x86 representation
94 PM.add(createRegisterAllocator());
95
96 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000097 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +000098
99 PM.add(createLowerSubregsPass());
100
101 if (PrintMachineCode) // Print the subreg lowered code
102 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000103
Chris Lattner47877052006-09-04 04:16:09 +0000104 // Run post-ra passes.
105 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000106 PM.add(createMachineFunctionPrinterPass(cerr));
107
Chris Lattner47877052006-09-04 04:16:09 +0000108 // Insert prolog/epilog code. Eliminate abstract frame index references...
109 PM.add(createPrologEpilogCodeInserter());
110
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000111 // Second pass scheduler.
Chris Lattner459525d2008-01-14 19:00:06 +0000112 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000113 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000114
Chris Lattner4a84ad72006-10-13 20:45:56 +0000115 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +0000116 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000117 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000118
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000119 PM.add(createGCMachineCodeAnalysisPass());
120 if (PrintMachineCode)
121 PM.add(createMachineFunctionPrinterPass(cerr));
122
123 if (PrintGCInfo)
124 PM.add(createCollectorMetadataPrinter(*cerr));
125
Jim Laskey9d4209f2006-11-07 19:33:46 +0000126 // Fold redundant debug labels.
127 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000128
Chris Lattner47877052006-09-04 04:16:09 +0000129 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000130 PM.add(createMachineFunctionPrinterPass(cerr));
131
Chris Lattner47877052006-09-04 04:16:09 +0000132 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000133 PM.add(createMachineFunctionPrinterPass(cerr));
134
Chris Lattner47877052006-09-04 04:16:09 +0000135 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000136 default:
137 break;
138 case TargetMachine::AssemblyFile:
139 if (addAssemblyEmitter(PM, Fast, Out))
140 return FileModel::Error;
141 return FileModel::AsmFile;
142 case TargetMachine::ObjectFile:
143 if (getMachOWriterInfo())
144 return FileModel::MachOFile;
145 else if (getELFWriterInfo())
146 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000147 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000148
149 return FileModel::Error;
150}
151
152/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
153/// be split up (e.g., to add an object writer pass), this method can be used to
154/// finish up adding passes to emit the file, if necessary.
155bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
156 MachineCodeEmitter *MCE,
157 bool Fast) {
158 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000159 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000160
161 PM.add(createCollectorMetadataDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000162
Chris Lattner47877052006-09-04 04:16:09 +0000163 // Delete machine code for this function
164 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000165
Chris Lattner47877052006-09-04 04:16:09 +0000166 return false; // success!
167}
168
169/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
170/// get machine code emitted. This uses a MachineCodeEmitter object to handle
171/// actually outputting the machine code and resolving things like the address
172/// of functions. This method should returns true if machine code emission is
173/// not supported.
174///
175bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
176 MachineCodeEmitter &MCE,
177 bool Fast) {
178 // Standard LLVM-Level Passes.
179
180 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000181 if (!Fast) {
182 PM.add(createLoopStrengthReducePass(getTargetLowering()));
183 if (PrintLSR)
184 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
185 }
Chris Lattner47877052006-09-04 04:16:09 +0000186
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000187 PM.add(createGCLoweringPass());
Chris Lattner47877052006-09-04 04:16:09 +0000188
Nicolas Geoffrayafe6c2b2008-02-13 18:39:37 +0000189 if (!ExceptionHandling)
190 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000191
192 // Make sure that no unreachable blocks are instruction selected.
193 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000194
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000195 if (!Fast)
196 PM.add(createCodeGenPreparePass(getTargetLowering()));
197
198 if (PrintISelInput)
199 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
200 &cerr));
201
Chris Lattner47877052006-09-04 04:16:09 +0000202 // Ask the target for an isel.
203 if (addInstSelector(PM, Fast))
204 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000205
Chris Lattner47877052006-09-04 04:16:09 +0000206 // Print the instruction selected machine code...
207 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000208 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000209
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000210 if (PerformLICM)
211 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000212
213 if (EnableSinking)
214 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000215
Chris Lattner47877052006-09-04 04:16:09 +0000216 // Perform register allocation to convert to a concrete x86 representation
217 PM.add(createRegisterAllocator());
218
219 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000220 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000221
222 PM.add(createLowerSubregsPass());
223
224 if (PrintMachineCode) // Print the subreg lowered code
225 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000226
Chris Lattner47877052006-09-04 04:16:09 +0000227 // Run post-ra passes.
228 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000229 PM.add(createMachineFunctionPrinterPass(cerr));
230
Chris Lattner47877052006-09-04 04:16:09 +0000231 // Insert prolog/epilog code. Eliminate abstract frame index references...
232 PM.add(createPrologEpilogCodeInserter());
233
234 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000235 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000236
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000237 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000238 if (!Fast)
239 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000240
Chris Lattnere01eaa02006-11-16 01:00:07 +0000241 // Branch folding must be run after regalloc and prolog/epilog insertion.
242 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000243 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000244
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000245 PM.add(createGCMachineCodeAnalysisPass());
246 if (PrintMachineCode)
247 PM.add(createMachineFunctionPrinterPass(cerr));
248
249 if (PrintGCInfo)
250 PM.add(createCollectorMetadataPrinter(*cerr));
251
Chris Lattner47877052006-09-04 04:16:09 +0000252 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000253 PM.add(createMachineFunctionPrinterPass(cerr));
254
Evan Cheng8bd60352007-07-20 21:56:13 +0000255 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000256
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000257 PM.add(createCollectorMetadataDeleter());
258
Chris Lattner47877052006-09-04 04:16:09 +0000259 // Delete machine code for this function
260 PM.add(createMachineCodeDeleter());
261
262 return false; // success!
263}