blob: d4533351447129ac4317fcd3c3c0306135516693 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
17#include "llvm/Assembly/PrintModulePass.h"
18#include "llvm/Analysis/LoopPass.h"
19#include "llvm/CodeGen/Passes.h"
20#include "llvm/Target/TargetOptions.h"
21#include "llvm/Transforms/Scalar.h"
22#include "llvm/Support/CommandLine.h"
23using namespace llvm;
24
25static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
26 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
27static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
28 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng77547212007-07-20 21:56:13 +000029static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
30 cl::desc("Dump emitter generated instructions as assembly"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031
Chris Lattner39882262008-01-04 07:36:53 +000032// Hidden options to help debugging
33static cl::opt<bool>
34EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
35 cl::desc("Perform sinking on machine code"));
Bill Wendling4aab7ae2008-01-04 08:11:03 +000036static cl::opt<bool>
37PerformLICM("machine-licm",
38 cl::init(false), cl::Hidden,
39 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattner39882262008-01-04 07:36:53 +000040
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041FileModel::Model
42LLVMTargetMachine::addPassesToEmitFile(FunctionPassManager &PM,
43 std::ostream &Out,
44 CodeGenFileType FileType,
45 bool Fast) {
46 // Standard LLVM-Level Passes.
47
48 // Run loop strength reduction before anything else.
49 if (!Fast) {
50 PM.add(createLoopStrengthReducePass(getTargetLowering()));
51 if (PrintLSR)
52 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
53 }
54
55 // FIXME: Implement efficient support for garbage collection intrinsics.
56 PM.add(createLowerGCPass());
57
58 if (!ExceptionHandling)
59 PM.add(createLowerInvokePass(getTargetLowering()));
60
61 // Make sure that no unreachable blocks are instruction selected.
62 PM.add(createUnreachableBlockEliminationPass());
63
64 if (!Fast)
65 PM.add(createCodeGenPreparePass(getTargetLowering()));
66
67 if (PrintISelInput)
68 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
69 &cerr));
70
71 // Ask the target for an isel.
72 if (addInstSelector(PM, Fast))
73 return FileModel::Error;
74
75 // Print the instruction selected machine code...
76 if (PrintMachineCode)
77 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +000078
Bill Wendling4aab7ae2008-01-04 08:11:03 +000079 if (PerformLICM)
80 PM.add(createMachineLICMPass());
Chris Lattner39882262008-01-04 07:36:53 +000081
82 if (EnableSinking)
83 PM.add(createMachineSinkingPass());
Bill Wendlingb958b0d2007-12-07 21:42:31 +000084
Dan Gohmanf17a25c2007-07-18 16:29:46 +000085 // Perform register allocation to convert to a concrete x86 representation
86 PM.add(createRegisterAllocator());
87
88 if (PrintMachineCode)
89 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambed379732007-07-27 07:36:14 +000090
91 PM.add(createLowerSubregsPass());
92
93 if (PrintMachineCode) // Print the subreg lowered code
94 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095
96 // Run post-ra passes.
97 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
98 PM.add(createMachineFunctionPrinterPass(cerr));
99
100 // Insert prolog/epilog code. Eliminate abstract frame index references...
101 PM.add(createPrologEpilogCodeInserter());
102
103 // Second pass scheduler.
104 if (!Fast)
105 PM.add(createPostRAScheduler());
106
107 // Branch folding must be run after regalloc and prolog/epilog insertion.
108 if (!Fast)
109 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000110
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 // Fold redundant debug labels.
112 PM.add(createDebugLabelFoldingPass());
113
114 if (PrintMachineCode) // Print the register-allocated code
115 PM.add(createMachineFunctionPrinterPass(cerr));
116
117 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
118 PM.add(createMachineFunctionPrinterPass(cerr));
119
120 switch (FileType) {
121 default:
122 break;
123 case TargetMachine::AssemblyFile:
124 if (addAssemblyEmitter(PM, Fast, Out))
125 return FileModel::Error;
126 return FileModel::AsmFile;
127 case TargetMachine::ObjectFile:
128 if (getMachOWriterInfo())
129 return FileModel::MachOFile;
130 else if (getELFWriterInfo())
131 return FileModel::ElfFile;
132 }
133
134 return FileModel::Error;
135}
136
137/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
138/// be split up (e.g., to add an object writer pass), this method can be used to
139/// finish up adding passes to emit the file, if necessary.
140bool LLVMTargetMachine::addPassesToEmitFileFinish(FunctionPassManager &PM,
141 MachineCodeEmitter *MCE,
142 bool Fast) {
143 if (MCE)
Evan Cheng77547212007-07-20 21:56:13 +0000144 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145
146 // Delete machine code for this function
147 PM.add(createMachineCodeDeleter());
148
149 return false; // success!
150}
151
152/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
153/// get machine code emitted. This uses a MachineCodeEmitter object to handle
154/// actually outputting the machine code and resolving things like the address
155/// of functions. This method should returns true if machine code emission is
156/// not supported.
157///
158bool LLVMTargetMachine::addPassesToEmitMachineCode(FunctionPassManager &PM,
159 MachineCodeEmitter &MCE,
160 bool Fast) {
161 // Standard LLVM-Level Passes.
162
163 // Run loop strength reduction before anything else.
164 if (!Fast) {
165 PM.add(createLoopStrengthReducePass(getTargetLowering()));
166 if (PrintLSR)
167 PM.add(new PrintFunctionPass("\n\n*** Code after LSR *** \n", &cerr));
168 }
169
170 // FIXME: Implement efficient support for garbage collection intrinsics.
171 PM.add(createLowerGCPass());
172
173 // FIXME: Implement the invoke/unwind instructions!
174 PM.add(createLowerInvokePass(getTargetLowering()));
175
176 // Make sure that no unreachable blocks are instruction selected.
177 PM.add(createUnreachableBlockEliminationPass());
178
179 if (!Fast)
180 PM.add(createCodeGenPreparePass(getTargetLowering()));
181
182 if (PrintISelInput)
183 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel *** \n",
184 &cerr));
185
186 // Ask the target for an isel.
187 if (addInstSelector(PM, Fast))
188 return true;
189
190 // Print the instruction selected machine code...
191 if (PrintMachineCode)
192 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000193
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000194 if (PerformLICM)
195 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000196
197 if (EnableSinking)
198 PM.add(createMachineSinkingPass());
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000199
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 // Perform register allocation to convert to a concrete x86 representation
201 PM.add(createRegisterAllocator());
202
203 if (PrintMachineCode)
204 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lambed379732007-07-27 07:36:14 +0000205
206 PM.add(createLowerSubregsPass());
207
208 if (PrintMachineCode) // Print the subreg lowered code
209 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210
211 // Run post-ra passes.
212 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
213 PM.add(createMachineFunctionPrinterPass(cerr));
214
215 // Insert prolog/epilog code. Eliminate abstract frame index references...
216 PM.add(createPrologEpilogCodeInserter());
217
218 if (PrintMachineCode) // Print the register-allocated code
219 PM.add(createMachineFunctionPrinterPass(cerr));
220
221 // Second pass scheduler.
222 if (!Fast)
223 PM.add(createPostRAScheduler());
224
225 // Branch folding must be run after regalloc and prolog/epilog insertion.
226 if (!Fast)
227 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000228
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
230 PM.add(createMachineFunctionPrinterPass(cerr));
231
Evan Cheng77547212007-07-20 21:56:13 +0000232 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233
234 // Delete machine code for this function
235 PM.add(createMachineCodeDeleter());
236
237 return false; // success!
238}