blob: c65a6b9a795278a32878deb54d7a262e7908f9ed [file] [log] [blame]
Chris Lattnera916db12006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnera916db12006-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 Lattnerbafc8372007-03-31 00:24:43 +000017#include "llvm/Assembly/PrintModulePass.h"
Devang Patelb0743b52007-03-06 21:14:09 +000018#include "llvm/Analysis/LoopPass.h"
Chris Lattnera916db12006-09-04 04:16:09 +000019#include "llvm/CodeGen/Passes.h"
Gordon Henriksen2d684b12008-01-07 01:33:09 +000020#include "llvm/CodeGen/Collector.h"
Chris Lattnera916db12006-09-04 04:16:09 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesenfd967cf2008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Chris Lattnera916db12006-09-04 04:16:09 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000024#include "llvm/Support/CommandLine.h"
Chris Lattnera916db12006-09-04 04:16:09 +000025using namespace llvm;
26
Chris Lattner37228f62007-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 Cheng9d5df0a2007-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 Henriksen2d684b12008-01-07 01:33:09 +000033static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
34 cl::desc("Dump garbage collector data"));
Chris Lattner37228f62007-06-19 05:47:49 +000035
Chris Lattnerf3edc092008-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 Wendling66470d02008-01-04 08:11:03 +000040static cl::opt<bool>
Evan Cheng95a7be42008-02-28 23:29:57 +000041AlignLoops("align-loops", cl::init(true), cl::Hidden,
42 cl::desc("Align loop headers"));
43static cl::opt<bool>
Bill Wendling66470d02008-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 Lattnerf3edc092008-01-04 07:36:53 +000047
Chris Lattner99471842008-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 Wendling523048e2007-02-08 01:36:53 +000054FileModel::Model
Dan Gohman24570832008-03-11 22:29:46 +000055LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Bill Wendling523048e2007-02-08 01:36:53 +000056 std::ostream &Out,
57 CodeGenFileType FileType,
58 bool Fast) {
Chris Lattnera916db12006-09-04 04:16:09 +000059 // Standard LLVM-Level Passes.
60
61 // Run loop strength reduction before anything else.
Chris Lattnerbafc8372007-03-31 00:24:43 +000062 if (!Fast) {
63 PM.add(createLoopStrengthReducePass(getTargetLowering()));
64 if (PrintLSR)
Dan Gohmana7ba51f2008-03-25 21:38:12 +000065 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerbafc8372007-03-31 00:24:43 +000066 }
Chris Lattnera916db12006-09-04 04:16:09 +000067
Gordon Henriksen2d684b12008-01-07 01:33:09 +000068 PM.add(createGCLoweringPass());
Duncan Sands03b274912007-07-11 16:59:20 +000069
Dale Johannesenfd967cf2008-04-02 00:25:04 +000070 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen5e4e0512008-04-01 20:00:57 +000071 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sands03b274912007-07-11 16:59:20 +000072
Chris Lattnera916db12006-09-04 04:16:09 +000073 // Make sure that no unreachable blocks are instruction selected.
74 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling523048e2007-02-08 01:36:53 +000075
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +000076 if (!Fast)
77 PM.add(createCodeGenPreparePass(getTargetLowering()));
78
79 if (PrintISelInput)
Dan Gohmana7ba51f2008-03-25 21:38:12 +000080 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +000081 &cerr));
82
Chris Lattnera916db12006-09-04 04:16:09 +000083 // Ask the target for an isel.
84 if (addInstSelector(PM, Fast))
Bill Wendling523048e2007-02-08 01:36:53 +000085 return FileModel::Error;
86
Chris Lattnera916db12006-09-04 04:16:09 +000087 // Print the instruction selected machine code...
88 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +000089 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingfb706bc2007-12-07 21:42:31 +000090
Bill Wendling66470d02008-01-04 08:11:03 +000091 if (PerformLICM)
92 PM.add(createMachineLICMPass());
Chris Lattnerf3edc092008-01-04 07:36:53 +000093
94 if (EnableSinking)
95 PM.add(createMachineSinkingPass());
Bill Wendlingfb706bc2007-12-07 21:42:31 +000096
Chris Lattnera916db12006-09-04 04:16:09 +000097 // Perform register allocation to convert to a concrete x86 representation
98 PM.add(createRegisterAllocator());
99
100 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000101 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lamb14bbb152007-07-27 07:36:14 +0000102
103 PM.add(createLowerSubregsPass());
104
105 if (PrintMachineCode) // Print the subreg lowered code
106 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling523048e2007-02-08 01:36:53 +0000107
Chris Lattnera916db12006-09-04 04:16:09 +0000108 // Run post-ra passes.
109 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000110 PM.add(createMachineFunctionPrinterPass(cerr));
111
Chris Lattnera916db12006-09-04 04:16:09 +0000112 // Insert prolog/epilog code. Eliminate abstract frame index references...
113 PM.add(createPrologEpilogCodeInserter());
114
Dale Johannesen2182f062007-07-13 17:13:54 +0000115 // Second pass scheduler.
Chris Lattner99471842008-01-14 19:00:06 +0000116 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen4dc35db2007-07-13 17:31:29 +0000117 PM.add(createPostRAScheduler());
Dale Johannesen2182f062007-07-13 17:13:54 +0000118
Chris Lattner55ad08a2006-10-13 20:45:56 +0000119 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey1941bfa2006-10-24 16:11:49 +0000120 if (!Fast)
Dale Johannesenf9cbdc62007-05-22 18:31:04 +0000121 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000122
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000123 PM.add(createGCMachineCodeAnalysisPass());
124 if (PrintMachineCode)
125 PM.add(createMachineFunctionPrinterPass(cerr));
126
127 if (PrintGCInfo)
128 PM.add(createCollectorMetadataPrinter(*cerr));
129
Jim Laskey6ea4fae2006-11-07 19:33:46 +0000130 // Fold redundant debug labels.
131 PM.add(createDebugLabelFoldingPass());
Chris Lattner55ad08a2006-10-13 20:45:56 +0000132
Chris Lattnera916db12006-09-04 04:16:09 +0000133 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling523048e2007-02-08 01:36:53 +0000134 PM.add(createMachineFunctionPrinterPass(cerr));
135
Chris Lattnera916db12006-09-04 04:16:09 +0000136 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000137 PM.add(createMachineFunctionPrinterPass(cerr));
138
Devang Patel72cfe842008-03-25 21:03:02 +0000139 if (AlignLoops && !OptimizeForSize)
Evan Cheng95a7be42008-02-28 23:29:57 +0000140 PM.add(createLoopAlignerPass());
141
Chris Lattnera916db12006-09-04 04:16:09 +0000142 switch (FileType) {
Bill Wendling523048e2007-02-08 01:36:53 +0000143 default:
144 break;
145 case TargetMachine::AssemblyFile:
146 if (addAssemblyEmitter(PM, Fast, Out))
147 return FileModel::Error;
148 return FileModel::AsmFile;
149 case TargetMachine::ObjectFile:
150 if (getMachOWriterInfo())
151 return FileModel::MachOFile;
152 else if (getELFWriterInfo())
153 return FileModel::ElfFile;
Chris Lattnera916db12006-09-04 04:16:09 +0000154 }
Bill Wendling523048e2007-02-08 01:36:53 +0000155
156 return FileModel::Error;
157}
158
159/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
160/// be split up (e.g., to add an object writer pass), this method can be used to
161/// finish up adding passes to emit the file, if necessary.
Dan Gohman24570832008-03-11 22:29:46 +0000162bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling523048e2007-02-08 01:36:53 +0000163 MachineCodeEmitter *MCE,
164 bool Fast) {
165 if (MCE)
Evan Cheng9d5df0a2007-07-20 21:56:13 +0000166 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000167
168 PM.add(createCollectorMetadataDeleter());
Bill Wendling523048e2007-02-08 01:36:53 +0000169
Chris Lattnera916db12006-09-04 04:16:09 +0000170 // Delete machine code for this function
171 PM.add(createMachineCodeDeleter());
Bill Wendling523048e2007-02-08 01:36:53 +0000172
Chris Lattnera916db12006-09-04 04:16:09 +0000173 return false; // success!
174}
175
176/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
177/// get machine code emitted. This uses a MachineCodeEmitter object to handle
178/// actually outputting the machine code and resolving things like the address
179/// of functions. This method should returns true if machine code emission is
180/// not supported.
181///
Dan Gohman24570832008-03-11 22:29:46 +0000182bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattnera916db12006-09-04 04:16:09 +0000183 MachineCodeEmitter &MCE,
184 bool Fast) {
185 // Standard LLVM-Level Passes.
186
187 // Run loop strength reduction before anything else.
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000188 if (!Fast) {
189 PM.add(createLoopStrengthReducePass(getTargetLowering()));
190 if (PrintLSR)
Dan Gohmana7ba51f2008-03-25 21:38:12 +0000191 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000192 }
Chris Lattnera916db12006-09-04 04:16:09 +0000193
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000194 PM.add(createGCLoweringPass());
Chris Lattnera916db12006-09-04 04:16:09 +0000195
Dale Johannesenfd967cf2008-04-02 00:25:04 +0000196 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen5e4e0512008-04-01 20:00:57 +0000197 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattnera916db12006-09-04 04:16:09 +0000198
199 // Make sure that no unreachable blocks are instruction selected.
200 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling523048e2007-02-08 01:36:53 +0000201
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000202 if (!Fast)
203 PM.add(createCodeGenPreparePass(getTargetLowering()));
204
205 if (PrintISelInput)
Dan Gohmana7ba51f2008-03-25 21:38:12 +0000206 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000207 &cerr));
208
Chris Lattnera916db12006-09-04 04:16:09 +0000209 // Ask the target for an isel.
210 if (addInstSelector(PM, Fast))
211 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000212
Chris Lattnera916db12006-09-04 04:16:09 +0000213 // Print the instruction selected machine code...
214 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000215 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000216
Bill Wendling66470d02008-01-04 08:11:03 +0000217 if (PerformLICM)
218 PM.add(createMachineLICMPass());
Chris Lattner276178e2008-01-05 06:14:16 +0000219
220 if (EnableSinking)
221 PM.add(createMachineSinkingPass());
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000222
Chris Lattnera916db12006-09-04 04:16:09 +0000223 // Perform register allocation to convert to a concrete x86 representation
224 PM.add(createRegisterAllocator());
225
226 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000227 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lamb14bbb152007-07-27 07:36:14 +0000228
229 PM.add(createLowerSubregsPass());
230
231 if (PrintMachineCode) // Print the subreg lowered code
232 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling523048e2007-02-08 01:36:53 +0000233
Chris Lattnera916db12006-09-04 04:16:09 +0000234 // Run post-ra passes.
235 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000236 PM.add(createMachineFunctionPrinterPass(cerr));
237
Chris Lattnera916db12006-09-04 04:16:09 +0000238 // Insert prolog/epilog code. Eliminate abstract frame index references...
239 PM.add(createPrologEpilogCodeInserter());
240
241 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling523048e2007-02-08 01:36:53 +0000242 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattnera916db12006-09-04 04:16:09 +0000243
Dale Johannesen2182f062007-07-13 17:13:54 +0000244 // Second pass scheduler.
Dale Johannesen4dc35db2007-07-13 17:31:29 +0000245 if (!Fast)
246 PM.add(createPostRAScheduler());
Dale Johannesen2182f062007-07-13 17:13:54 +0000247
Chris Lattnere0a70382006-11-16 01:00:07 +0000248 // Branch folding must be run after regalloc and prolog/epilog insertion.
249 if (!Fast)
Dale Johannesenf9cbdc62007-05-22 18:31:04 +0000250 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000251
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000252 PM.add(createGCMachineCodeAnalysisPass());
253 if (PrintMachineCode)
254 PM.add(createMachineFunctionPrinterPass(cerr));
255
256 if (PrintGCInfo)
257 PM.add(createCollectorMetadataPrinter(*cerr));
258
Chris Lattnera916db12006-09-04 04:16:09 +0000259 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000260 PM.add(createMachineFunctionPrinterPass(cerr));
261
Evan Cheng9d5df0a2007-07-20 21:56:13 +0000262 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattnera916db12006-09-04 04:16:09 +0000263
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000264 PM.add(createCollectorMetadataDeleter());
265
Chris Lattnera916db12006-09-04 04:16:09 +0000266 // Delete machine code for this function
267 PM.add(createMachineCodeDeleter());
268
269 return false; // success!
270}