blob: 67c0e4772a87dd779d5ada0aa0095e2b196d1cd0 [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"
22#include "llvm/Transforms/Scalar.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000023#include "llvm/Support/CommandLine.h"
Chris Lattnera916db12006-09-04 04:16:09 +000024using namespace llvm;
25
Chris Lattner37228f62007-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 Cheng9d5df0a2007-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 Henriksen2d684b12008-01-07 01:33:09 +000032static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
33 cl::desc("Dump garbage collector data"));
Chris Lattner37228f62007-06-19 05:47:49 +000034
Chris Lattnerf3edc092008-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 Wendling66470d02008-01-04 08:11:03 +000039static cl::opt<bool>
Evan Cheng95a7be42008-02-28 23:29:57 +000040AlignLoops("align-loops", cl::init(true), cl::Hidden,
41 cl::desc("Align loop headers"));
42static cl::opt<bool>
Bill Wendling66470d02008-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 Lattnerf3edc092008-01-04 07:36:53 +000046
Chris Lattner99471842008-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 Wendling523048e2007-02-08 01:36:53 +000053FileModel::Model
Dan Gohman24570832008-03-11 22:29:46 +000054LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Bill Wendling523048e2007-02-08 01:36:53 +000055 std::ostream &Out,
56 CodeGenFileType FileType,
57 bool Fast) {
Chris Lattnera916db12006-09-04 04:16:09 +000058 // Standard LLVM-Level Passes.
59
60 // Run loop strength reduction before anything else.
Chris Lattnerbafc8372007-03-31 00:24:43 +000061 if (!Fast) {
62 PM.add(createLoopStrengthReducePass(getTargetLowering()));
63 if (PrintLSR)
Dan Gohmana7ba51f2008-03-25 21:38:12 +000064 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerbafc8372007-03-31 00:24:43 +000065 }
Chris Lattnera916db12006-09-04 04:16:09 +000066
Gordon Henriksen2d684b12008-01-07 01:33:09 +000067 PM.add(createGCLoweringPass());
Duncan Sands03b274912007-07-11 16:59:20 +000068
Dale Johannesen7d02cf32008-03-31 23:40:23 +000069 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sands03b274912007-07-11 16:59:20 +000070
Chris Lattnera916db12006-09-04 04:16:09 +000071 // Make sure that no unreachable blocks are instruction selected.
72 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling523048e2007-02-08 01:36:53 +000073
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +000074 if (!Fast)
75 PM.add(createCodeGenPreparePass(getTargetLowering()));
76
77 if (PrintISelInput)
Dan Gohmana7ba51f2008-03-25 21:38:12 +000078 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +000079 &cerr));
80
Chris Lattnera916db12006-09-04 04:16:09 +000081 // Ask the target for an isel.
82 if (addInstSelector(PM, Fast))
Bill Wendling523048e2007-02-08 01:36:53 +000083 return FileModel::Error;
84
Chris Lattnera916db12006-09-04 04:16:09 +000085 // Print the instruction selected machine code...
86 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +000087 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingfb706bc2007-12-07 21:42:31 +000088
Bill Wendling66470d02008-01-04 08:11:03 +000089 if (PerformLICM)
90 PM.add(createMachineLICMPass());
Chris Lattnerf3edc092008-01-04 07:36:53 +000091
92 if (EnableSinking)
93 PM.add(createMachineSinkingPass());
Bill Wendlingfb706bc2007-12-07 21:42:31 +000094
Chris Lattnera916db12006-09-04 04:16:09 +000095 // Perform register allocation to convert to a concrete x86 representation
96 PM.add(createRegisterAllocator());
97
98 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +000099 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lamb14bbb152007-07-27 07:36:14 +0000100
101 PM.add(createLowerSubregsPass());
102
103 if (PrintMachineCode) // Print the subreg lowered code
104 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling523048e2007-02-08 01:36:53 +0000105
Chris Lattnera916db12006-09-04 04:16:09 +0000106 // Run post-ra passes.
107 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000108 PM.add(createMachineFunctionPrinterPass(cerr));
109
Chris Lattnera916db12006-09-04 04:16:09 +0000110 // Insert prolog/epilog code. Eliminate abstract frame index references...
111 PM.add(createPrologEpilogCodeInserter());
112
Dale Johannesen2182f062007-07-13 17:13:54 +0000113 // Second pass scheduler.
Chris Lattner99471842008-01-14 19:00:06 +0000114 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen4dc35db2007-07-13 17:31:29 +0000115 PM.add(createPostRAScheduler());
Dale Johannesen2182f062007-07-13 17:13:54 +0000116
Chris Lattner55ad08a2006-10-13 20:45:56 +0000117 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jim Laskey1941bfa2006-10-24 16:11:49 +0000118 if (!Fast)
Dale Johannesenf9cbdc62007-05-22 18:31:04 +0000119 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000120
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000121 PM.add(createGCMachineCodeAnalysisPass());
122 if (PrintMachineCode)
123 PM.add(createMachineFunctionPrinterPass(cerr));
124
125 if (PrintGCInfo)
126 PM.add(createCollectorMetadataPrinter(*cerr));
127
Jim Laskey6ea4fae2006-11-07 19:33:46 +0000128 // Fold redundant debug labels.
129 PM.add(createDebugLabelFoldingPass());
Chris Lattner55ad08a2006-10-13 20:45:56 +0000130
Chris Lattnera916db12006-09-04 04:16:09 +0000131 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling523048e2007-02-08 01:36:53 +0000132 PM.add(createMachineFunctionPrinterPass(cerr));
133
Chris Lattnera916db12006-09-04 04:16:09 +0000134 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000135 PM.add(createMachineFunctionPrinterPass(cerr));
136
Devang Patel72cfe842008-03-25 21:03:02 +0000137 if (AlignLoops && !OptimizeForSize)
Evan Cheng95a7be42008-02-28 23:29:57 +0000138 PM.add(createLoopAlignerPass());
139
Chris Lattnera916db12006-09-04 04:16:09 +0000140 switch (FileType) {
Bill Wendling523048e2007-02-08 01:36:53 +0000141 default:
142 break;
143 case TargetMachine::AssemblyFile:
144 if (addAssemblyEmitter(PM, Fast, Out))
145 return FileModel::Error;
146 return FileModel::AsmFile;
147 case TargetMachine::ObjectFile:
148 if (getMachOWriterInfo())
149 return FileModel::MachOFile;
150 else if (getELFWriterInfo())
151 return FileModel::ElfFile;
Chris Lattnera916db12006-09-04 04:16:09 +0000152 }
Bill Wendling523048e2007-02-08 01:36:53 +0000153
154 return FileModel::Error;
155}
156
157/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
158/// be split up (e.g., to add an object writer pass), this method can be used to
159/// finish up adding passes to emit the file, if necessary.
Dan Gohman24570832008-03-11 22:29:46 +0000160bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling523048e2007-02-08 01:36:53 +0000161 MachineCodeEmitter *MCE,
162 bool Fast) {
163 if (MCE)
Evan Cheng9d5df0a2007-07-20 21:56:13 +0000164 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000165
166 PM.add(createCollectorMetadataDeleter());
Bill Wendling523048e2007-02-08 01:36:53 +0000167
Chris Lattnera916db12006-09-04 04:16:09 +0000168 // Delete machine code for this function
169 PM.add(createMachineCodeDeleter());
Bill Wendling523048e2007-02-08 01:36:53 +0000170
Chris Lattnera916db12006-09-04 04:16:09 +0000171 return false; // success!
172}
173
174/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
175/// get machine code emitted. This uses a MachineCodeEmitter object to handle
176/// actually outputting the machine code and resolving things like the address
177/// of functions. This method should returns true if machine code emission is
178/// not supported.
179///
Dan Gohman24570832008-03-11 22:29:46 +0000180bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattnera916db12006-09-04 04:16:09 +0000181 MachineCodeEmitter &MCE,
182 bool Fast) {
183 // Standard LLVM-Level Passes.
184
185 // Run loop strength reduction before anything else.
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000186 if (!Fast) {
187 PM.add(createLoopStrengthReducePass(getTargetLowering()));
188 if (PrintLSR)
Dan Gohmana7ba51f2008-03-25 21:38:12 +0000189 PM.add(new PrintFunctionPass("\n\n*** Code after LSR ***\n", &cerr));
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000190 }
Chris Lattnera916db12006-09-04 04:16:09 +0000191
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000192 PM.add(createGCLoweringPass());
Chris Lattnera916db12006-09-04 04:16:09 +0000193
Dale Johannesen7d02cf32008-03-31 23:40:23 +0000194 PM.add(createLowerInvokePass(getTargetLowering()));
Chris Lattnera916db12006-09-04 04:16:09 +0000195
196 // Make sure that no unreachable blocks are instruction selected.
197 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling523048e2007-02-08 01:36:53 +0000198
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000199 if (!Fast)
200 PM.add(createCodeGenPreparePass(getTargetLowering()));
201
202 if (PrintISelInput)
Dan Gohmana7ba51f2008-03-25 21:38:12 +0000203 PM.add(new PrintFunctionPass("\n\n*** Final LLVM Code input to ISel ***\n",
Chris Lattnerf6a6d3c2007-03-31 04:18:03 +0000204 &cerr));
205
Chris Lattnera916db12006-09-04 04:16:09 +0000206 // Ask the target for an isel.
207 if (addInstSelector(PM, Fast))
208 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000209
Chris Lattnera916db12006-09-04 04:16:09 +0000210 // Print the instruction selected machine code...
211 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000212 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000213
Bill Wendling66470d02008-01-04 08:11:03 +0000214 if (PerformLICM)
215 PM.add(createMachineLICMPass());
Chris Lattner276178e2008-01-05 06:14:16 +0000216
217 if (EnableSinking)
218 PM.add(createMachineSinkingPass());
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000219
Chris Lattnera916db12006-09-04 04:16:09 +0000220 // Perform register allocation to convert to a concrete x86 representation
221 PM.add(createRegisterAllocator());
222
223 if (PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000224 PM.add(createMachineFunctionPrinterPass(cerr));
Christopher Lamb14bbb152007-07-27 07:36:14 +0000225
226 PM.add(createLowerSubregsPass());
227
228 if (PrintMachineCode) // Print the subreg lowered code
229 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling523048e2007-02-08 01:36:53 +0000230
Chris Lattnera916db12006-09-04 04:16:09 +0000231 // Run post-ra passes.
232 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000233 PM.add(createMachineFunctionPrinterPass(cerr));
234
Chris Lattnera916db12006-09-04 04:16:09 +0000235 // Insert prolog/epilog code. Eliminate abstract frame index references...
236 PM.add(createPrologEpilogCodeInserter());
237
238 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling523048e2007-02-08 01:36:53 +0000239 PM.add(createMachineFunctionPrinterPass(cerr));
Chris Lattnera916db12006-09-04 04:16:09 +0000240
Dale Johannesen2182f062007-07-13 17:13:54 +0000241 // Second pass scheduler.
Dale Johannesen4dc35db2007-07-13 17:31:29 +0000242 if (!Fast)
243 PM.add(createPostRAScheduler());
Dale Johannesen2182f062007-07-13 17:13:54 +0000244
Chris Lattnere0a70382006-11-16 01:00:07 +0000245 // Branch folding must be run after regalloc and prolog/epilog insertion.
246 if (!Fast)
Dale Johannesenf9cbdc62007-05-22 18:31:04 +0000247 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingfb706bc2007-12-07 21:42:31 +0000248
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000249 PM.add(createGCMachineCodeAnalysisPass());
250 if (PrintMachineCode)
251 PM.add(createMachineFunctionPrinterPass(cerr));
252
253 if (PrintGCInfo)
254 PM.add(createCollectorMetadataPrinter(*cerr));
255
Chris Lattnera916db12006-09-04 04:16:09 +0000256 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling523048e2007-02-08 01:36:53 +0000257 PM.add(createMachineFunctionPrinterPass(cerr));
258
Evan Cheng9d5df0a2007-07-20 21:56:13 +0000259 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
Chris Lattnera916db12006-09-04 04:16:09 +0000260
Gordon Henriksen2d684b12008-01-07 01:33:09 +0000261 PM.add(createCollectorMetadataDeleter());
262
Chris Lattnera916db12006-09-04 04:16:09 +0000263 // Delete machine code for this function
264 PM.add(createMachineCodeDeleter());
265
266 return false; // success!
267}