blob: b6554356eead47c020ed9f6d98abce9917b145c0 [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
Bill Wendling04523ea2007-02-08 01:36:53 +000044FileModel::Model
45LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
46 std::ostream &Out,
47 CodeGenFileType FileType,
48 bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +000049 // Standard LLVM-Level Passes.
50
51 // Run loop strength reduction before anything else.
Chris Lattner31442f92007-03-31 00:24:43 +000052 if (!Fast) {
53 PM.add(createLoopStrengthReducePass(getTargetLowering()));
54 if (PrintLSR)
55 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
56 }
Chris Lattner47877052006-09-04 04:16:09 +000057
Gordon Henriksen93f96d02008-01-07 01:33:09 +000058 PM.add(createGCLoweringPass());
Duncan Sandsc3751602007-07-11 16:59:20 +000059
Jim Laskeya4e7cd92007-02-22 16:22:15 +000060 if (!ExceptionHandling)
61 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsc3751602007-07-11 16:59:20 +000062
Chris Lattner47877052006-09-04 04:16:09 +000063 // Make sure that no unreachable blocks are instruction selected.
64 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +000065
Chris Lattnerc8d288f2007-03-31 04:18:03 +000066 if (!Fast)
67 PM.add(createCodeGenPreparePass(getTargetLowering()));
68
69 if (PrintISelInput)
70 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
71 &cerr));
72
Chris Lattner47877052006-09-04 04:16:09 +000073 // Ask the target for an isel.
74 if (addInstSelector(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000075 return FileModel::Error;
76
Chris Lattner47877052006-09-04 04:16:09 +000077 // Print the instruction selected machine code...
78 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000079 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +000080
Bill Wendlingcc8f6032008-01-04 08:11:03 +000081 if (PerformLICM)
82 PM.add(createMachineLICMPass());
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000083
84 if (EnableSinking)
85 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +000086
Chris Lattner47877052006-09-04 04:16:09 +000087 // Perform register allocation to convert to a concrete x86 representation
88 PM.add(createRegisterAllocator());
89
90 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000091 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +000092
93 PM.add(createLowerSubregsPass());
94
95 if (PrintMachineCode) // Print the subreg lowered code
96 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +000097
Chris Lattner47877052006-09-04 04:16:09 +000098 // Run post-ra passes.
99 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000100 PM.add(createMachineFunctionPrinterPass(cerr));
101
Chris Lattner47877052006-09-04 04:16:09 +0000102 // Insert prolog/epilog code. Eliminate abstract frame index references...
103 PM.add(createPrologEpilogCodeInserter());
104
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000105 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000106 if (!Fast)
107 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000108
Chris Lattner4a84ad72006-10-13 20:45:56 +0000109 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey62d07d62006-10-24 16:11:49 +0000110 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000111 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000112
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000113 PM.add(createGCMachineCodeAnalysisPass());
114 if (PrintMachineCode)
115 PM.add(createMachineFunctionPrinterPass(cerr));
116
117 if (PrintGCInfo)
118 PM.add(createCollectorMetadataPrinter(*cerr));
119
Jim Laskey9d4209f2006-11-07 19:33:46 +0000120 // Fold redundant debug labels.
121 PM.add(createDebugLabelFoldingPass());
Chris Lattner4a84ad72006-10-13 20:45:56 +0000122
Chris Lattner47877052006-09-04 04:16:09 +0000123 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000124 PM.add(createMachineFunctionPrinterPass(cerr));
125
Chris Lattner47877052006-09-04 04:16:09 +0000126 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000127 PM.add(createMachineFunctionPrinterPass(cerr));
128
Chris Lattner47877052006-09-04 04:16:09 +0000129 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +0000130 default:
131 break;
132 case TargetMachine::AssemblyFile:
133 if (addAssemblyEmitter(PM, Fast, Out))
134 return FileModel::Error;
135 return FileModel::AsmFile;
136 case TargetMachine::ObjectFile:
137 if (getMachOWriterInfo())
138 return FileModel::MachOFile;
139 else if (getELFWriterInfo())
140 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +0000141 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000142
143 return FileModel::Error;
144}
145
146/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
147/// be split up (e.g., to add an object writer pass), this method can be used to
148/// finish up adding passes to emit the file, if necessary.
149bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
150 MachineCodeEmitter *MCE,
151 bool Fast) {
152 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000153 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000154
155 PM.add(createCollectorMetadataDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000156
Chris Lattner47877052006-09-04 04:16:09 +0000157 // Delete machine code for this function
158 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000159
Chris Lattner47877052006-09-04 04:16:09 +0000160 return false; // success!
161}
162
163/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
164/// get machine code emitted. This uses a MachineCodeEmitter object to handle
165/// actually outputting the machine code and resolving things like the address
166/// of functions. This method should returns true if machine code emission is
167/// not supported.
168///
169bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
170 MachineCodeEmitter &MCE,
171 bool Fast) {
172 // Standard LLVM-Level Passes.
173
174 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000175 if (!Fast) {
176 PM.add(createLoopStrengthReducePass(getTargetLowering()));
177 if (PrintLSR)
178 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
179 }
Chris Lattner47877052006-09-04 04:16:09 +0000180
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000181 PM.add(createGCLoweringPass());
Chris Lattner47877052006-09-04 04:16:09 +0000182
183 // FIXME: Implement the invoke/unwind instructions!
Duraid Madina2a0013f2006-09-04 06:21:35 +0000184 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattner47877052006-09-04 04:16:09 +0000185
186 // Make sure that no unreachable blocks are instruction selected.
187 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000188
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000189 if (!Fast)
190 PM.add(createCodeGenPreparePass(getTargetLowering()));
191
192 if (PrintISelInput)
193 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
194 &cerr));
195
Chris Lattner47877052006-09-04 04:16:09 +0000196 // Ask the target for an isel.
197 if (addInstSelector(PM, Fast))
198 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000199
Chris Lattner47877052006-09-04 04:16:09 +0000200 // Print the instruction selected machine code...
201 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000202 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000203
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000204 if (PerformLICM)
205 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000206
207 if (EnableSinking)
208 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000209
Chris Lattner47877052006-09-04 04:16:09 +0000210 // Perform register allocation to convert to a concrete x86 representation
211 PM.add(createRegisterAllocator());
212
213 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000214 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambada779f2007-07-27 07:36:14 +0000215
216 PM.add(createLowerSubregsPass());
217
218 if (PrintMachineCode) // Print the subreg lowered code
219 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000220
Chris Lattner47877052006-09-04 04:16:09 +0000221 // Run post-ra passes.
222 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000223 PM.add(createMachineFunctionPrinterPass(cerr));
224
Chris Lattner47877052006-09-04 04:16:09 +0000225 // Insert prolog/epilog code. Eliminate abstract frame index references...
226 PM.add(createPrologEpilogCodeInserter());
227
228 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000229 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattner47877052006-09-04 04:16:09 +0000230
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000231 // Second pass scheduler.
Dale Johannesen72f15962007-07-13 17:31:29 +0000232 if (!Fast)
233 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000234
Chris Lattnere01eaa02006-11-16 01:00:07 +0000235 // Branch folding must be run after regalloc and prolog/epilog insertion.
236 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000237 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000238
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000239 PM.add(createGCMachineCodeAnalysisPass());
240 if (PrintMachineCode)
241 PM.add(createMachineFunctionPrinterPass(cerr));
242
243 if (PrintGCInfo)
244 PM.add(createCollectorMetadataPrinter(*cerr));
245
Chris Lattner47877052006-09-04 04:16:09 +0000246 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000247 PM.add(createMachineFunctionPrinterPass(cerr));
248
Evan Cheng8bd60352007-07-20 21:56:13 +0000249 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattner47877052006-09-04 04:16:09 +0000250
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000251 PM.add(createCollectorMetadataDeleter());
252
Chris Lattner47877052006-09-04 04:16:09 +0000253 // Delete machine code for this function
254 PM.add(createMachineCodeDeleter());
255
256 return false; // success!
257}