blob: 7def8fa61d2a3c57f825a4c7a656519e41b07b7c [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 Henriksen5a29c9e2008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.h"
Chris Lattner47877052006-09-04 04:16:09 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesen1532f3d2008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Chris Lattner47877052006-09-04 04:16:09 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattner31442f92007-03-31 00:24:43 +000024#include "llvm/Support/CommandLine.h"
Owen Andersoncb371882008-08-21 00:14:44 +000025#include "llvm/Support/raw_ostream.h"
Chris Lattner47877052006-09-04 04:16:09 +000026using namespace llvm;
27
Dan Gohman2c4bf112008-09-25 01:14:49 +000028namespace llvm {
29 bool EnableFastISel;
30}
31
Chris Lattner85ef2542007-06-19 05:47:49 +000032static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
33 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
34static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
35 cl::desc("Print LLVM IR input to isel pass"));
Evan Cheng8bd60352007-07-20 21:56:13 +000036static cl::opt<bool> PrintEmittedAsm("print-emitted-asm", cl::Hidden,
37 cl::desc("Dump emitter generated instructions as assembly"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000038static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39 cl::desc("Dump garbage collector data"));
Chris Lattner85ef2542007-06-19 05:47:49 +000040
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000041// Hidden options to help debugging
42static cl::opt<bool>
43EnableSinking("enable-sinking", cl::init(false), cl::Hidden,
44 cl::desc("Perform sinking on machine code"));
Chris Lattnerc4ce73f2008-01-04 07:36:53 +000045
Chris Lattner459525d2008-01-14 19:00:06 +000046// When this works it will be on by default.
47static cl::opt<bool>
48DisablePostRAScheduler("disable-post-RA-scheduler",
49 cl::desc("Disable scheduling after register allocation"),
50 cl::init(true));
51
Dan Gohmandc756852008-10-01 20:39:19 +000052// Enable or disable FastISel. Both options are needed, because
53// FastISel is enabled by default with -fast, and we wish to be
54// able to enable or disable fast-isel independently from -fast.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +000055static cl::opt<cl::boolOrDefault>
Dan Gohmandc756852008-10-01 20:39:19 +000056EnableFastISelOption("fast-isel", cl::Hidden,
57 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman2c4bf112008-09-25 01:14:49 +000058
Bill Wendling04523ea2007-02-08 01:36:53 +000059FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000060LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000061 raw_ostream &Out,
Bill Wendling04523ea2007-02-08 01:36:53 +000062 CodeGenFileType FileType,
63 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000064 // Add common CodeGen passes.
65 if (addCommonCodeGenPasses(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000066 return FileModel::Error;
67
Jim Laskey9d4209f2006-11-07 19:33:46 +000068 // Fold redundant debug labels.
69 PM.add(createDebugLabelFoldingPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +000070
71 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000072 PM.add(createMachineFunctionPrinterPass(cerr));
73
Chris Lattner47877052006-09-04 04:16:09 +000074 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000075 PM.add(createMachineFunctionPrinterPass(cerr));
76
Devang Patel4ae641f2008-10-01 23:18:38 +000077 if (!Fast)
Evan Chengd703ed62008-02-28 23:29:57 +000078 PM.add(createLoopAlignerPass());
79
Chris Lattner47877052006-09-04 04:16:09 +000080 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000081 default:
82 break;
83 case TargetMachine::AssemblyFile:
84 if (addAssemblyEmitter(PM, Fast, Out))
85 return FileModel::Error;
86 return FileModel::AsmFile;
87 case TargetMachine::ObjectFile:
88 if (getMachOWriterInfo())
89 return FileModel::MachOFile;
90 else if (getELFWriterInfo())
91 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000092 }
Bill Wendling04523ea2007-02-08 01:36:53 +000093
94 return FileModel::Error;
95}
Dan Gohman02dae4b2008-09-25 00:37:07 +000096
Bill Wendling04523ea2007-02-08 01:36:53 +000097/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
98/// be split up (e.g., to add an object writer pass), this method can be used to
99/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +0000100bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000101 MachineCodeEmitter *MCE,
102 bool Fast) {
103 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000104 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000105
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000106 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000107
Chris Lattner47877052006-09-04 04:16:09 +0000108 // Delete machine code for this function
109 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000110
Chris Lattner47877052006-09-04 04:16:09 +0000111 return false; // success!
112}
113
114/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
115/// get machine code emitted. This uses a MachineCodeEmitter object to handle
116/// actually outputting the machine code and resolving things like the address
117/// of functions. This method should returns true if machine code emission is
118/// not supported.
119///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000120bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000121 MachineCodeEmitter &MCE,
122 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000123 // Add common CodeGen passes.
124 if (addCommonCodeGenPasses(PM, Fast))
125 return true;
126
127 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
128 PM.add(createMachineFunctionPrinterPass(cerr));
129
130 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
131
132 PM.add(createGCInfoDeleter());
133
134 // Delete machine code for this function
135 PM.add(createMachineCodeDeleter());
136
137 return false; // success!
138}
139
140/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
141/// both emitting to assembly files or machine code output.
142///
143bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +0000144 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000145
Chris Lattner47877052006-09-04 04:16:09 +0000146 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000147 if (!Fast) {
148 PM.add(createLoopStrengthReducePass(getTargetLowering()));
149 if (PrintLSR)
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000150 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000151 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000152
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000153 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000154
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000155 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000156 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000157
Chris Lattner47877052006-09-04 04:16:09 +0000158 // Make sure that no unreachable blocks are instruction selected.
159 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000160
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000161 if (!Fast)
162 PM.add(createCodeGenPreparePass(getTargetLowering()));
163
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000164 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendling2b58ce52008-11-04 02:10:20 +0000165
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000166 if (PrintISelInput)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000167 PM.add(createPrintFunctionPass("\n\n"
168 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000169 &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000170
Dan Gohman02dae4b2008-09-25 00:37:07 +0000171 // Standard Lower-Level Passes.
172
Dan Gohmandc756852008-10-01 20:39:19 +0000173 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000174 if (EnableFastISelOption == cl::BOU_TRUE ||
175 (Fast && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmandc756852008-10-01 20:39:19 +0000176 EnableFastISel = true;
177
Chris Lattner47877052006-09-04 04:16:09 +0000178 // Ask the target for an isel.
179 if (addInstSelector(PM, Fast))
180 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000181
Chris Lattner47877052006-09-04 04:16:09 +0000182 // Print the instruction selected machine code...
183 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000184 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000185
Evan Chengc5d1a4f2009-02-05 08:46:33 +0000186 if (!Fast)
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000187 PM.add(createMachineLICMPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000188
Chris Lattner3c42f122008-01-05 06:14:16 +0000189 if (EnableSinking)
190 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000191
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000192 // Run pre-ra passes.
193 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
194 PM.add(createMachineFunctionPrinterPass(cerr));
195
Evan Cheng3f32d652008-06-04 09:18:41 +0000196 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000197 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000198
199 // Perform stack slot coloring.
Evan Cheng9ef4c532008-06-30 22:33:16 +0000200 if (!Fast)
201 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000202
Dan Gohman02dae4b2008-09-25 00:37:07 +0000203 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000204 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000205
Evan Cheng3f32d652008-06-04 09:18:41 +0000206 // Run post-ra passes.
207 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
208 PM.add(createMachineFunctionPrinterPass(cerr));
209
Dan Gohman02dae4b2008-09-25 00:37:07 +0000210 if (PrintMachineCode)
Evan Cheng3f32d652008-06-04 09:18:41 +0000211 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000212
Christopher Lambada779f2007-07-27 07:36:14 +0000213 PM.add(createLowerSubregsPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000214
Christopher Lambada779f2007-07-27 07:36:14 +0000215 if (PrintMachineCode) // Print the subreg lowered code
216 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000217
Chris Lattner47877052006-09-04 04:16:09 +0000218 // Insert prolog/epilog code. Eliminate abstract frame index references...
219 PM.add(createPrologEpilogCodeInserter());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000220
Evan Cheng3f32d652008-06-04 09:18:41 +0000221 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000222 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000223
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000224 // Second pass scheduler.
Dan Gohman5ce09732008-11-20 19:54:21 +0000225 if (!Fast && !DisablePostRAScheduler) {
Dale Johannesen72f15962007-07-13 17:31:29 +0000226 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000227
Dan Gohman5ce09732008-11-20 19:54:21 +0000228 if (PrintMachineCode)
229 PM.add(createMachineFunctionPrinterPass(cerr));
230 }
231
Dan Gohman23b0d492008-12-18 01:36:42 +0000232 // Branch folding must be run after regalloc and prolog/epilog insertion.
233 if (!Fast)
234 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
235
236 if (PrintMachineCode)
237 PM.add(createMachineFunctionPrinterPass(cerr));
238
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000239 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000240
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000241 if (PrintMachineCode)
242 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000243
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000244 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000245 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000246
Dan Gohman02dae4b2008-09-25 00:37:07 +0000247 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000248}