blob: f29944c7c90b5d8f500eb91c31e0a521bc4051ab [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 Lattner459525d2008-01-14 19:00:06 +000041// When this works it will be on by default.
42static cl::opt<bool>
43DisablePostRAScheduler("disable-post-RA-scheduler",
44 cl::desc("Disable scheduling after register allocation"),
45 cl::init(true));
46
Dan Gohmandc756852008-10-01 20:39:19 +000047// Enable or disable FastISel. Both options are needed, because
48// FastISel is enabled by default with -fast, and we wish to be
49// able to enable or disable fast-isel independently from -fast.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +000050static cl::opt<cl::boolOrDefault>
Dan Gohmandc756852008-10-01 20:39:19 +000051EnableFastISelOption("fast-isel", cl::Hidden,
52 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman2c4bf112008-09-25 01:14:49 +000053
Bill Wendling04523ea2007-02-08 01:36:53 +000054FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000055LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000056 raw_ostream &Out,
Bill Wendling04523ea2007-02-08 01:36:53 +000057 CodeGenFileType FileType,
58 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000059 // Add common CodeGen passes.
60 if (addCommonCodeGenPasses(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000061 return FileModel::Error;
62
Jim Laskey9d4209f2006-11-07 19:33:46 +000063 // Fold redundant debug labels.
64 PM.add(createDebugLabelFoldingPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +000065
66 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000067 PM.add(createMachineFunctionPrinterPass(cerr));
68
Chris Lattner47877052006-09-04 04:16:09 +000069 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000070 PM.add(createMachineFunctionPrinterPass(cerr));
71
Devang Patel4ae641f2008-10-01 23:18:38 +000072 if (!Fast)
Evan Chengd703ed62008-02-28 23:29:57 +000073 PM.add(createLoopAlignerPass());
74
Chris Lattner47877052006-09-04 04:16:09 +000075 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000076 default:
77 break;
78 case TargetMachine::AssemblyFile:
79 if (addAssemblyEmitter(PM, Fast, Out))
80 return FileModel::Error;
81 return FileModel::AsmFile;
82 case TargetMachine::ObjectFile:
83 if (getMachOWriterInfo())
84 return FileModel::MachOFile;
85 else if (getELFWriterInfo())
86 return FileModel::ElfFile;
Chris Lattner47877052006-09-04 04:16:09 +000087 }
Bill Wendling04523ea2007-02-08 01:36:53 +000088
89 return FileModel::Error;
90}
Dan Gohman02dae4b2008-09-25 00:37:07 +000091
Bill Wendling04523ea2007-02-08 01:36:53 +000092/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
93/// be split up (e.g., to add an object writer pass), this method can be used to
94/// finish up adding passes to emit the file, if necessary.
Dan Gohmanbfae8312008-03-11 22:29:46 +000095bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +000096 MachineCodeEmitter *MCE,
97 bool Fast) {
98 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +000099 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000100
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000101 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000102
Chris Lattner47877052006-09-04 04:16:09 +0000103 // Delete machine code for this function
104 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000105
Chris Lattner47877052006-09-04 04:16:09 +0000106 return false; // success!
107}
108
109/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
110/// get machine code emitted. This uses a MachineCodeEmitter object to handle
111/// actually outputting the machine code and resolving things like the address
112/// of functions. This method should returns true if machine code emission is
113/// not supported.
114///
Dan Gohmanbfae8312008-03-11 22:29:46 +0000115bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000116 MachineCodeEmitter &MCE,
117 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000118 // Add common CodeGen passes.
119 if (addCommonCodeGenPasses(PM, Fast))
120 return true;
121
122 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
123 PM.add(createMachineFunctionPrinterPass(cerr));
124
125 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
126
127 PM.add(createGCInfoDeleter());
128
129 // Delete machine code for this function
130 PM.add(createMachineCodeDeleter());
131
132 return false; // success!
133}
134
135/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
136/// both emitting to assembly files or machine code output.
137///
138bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
Chris Lattner47877052006-09-04 04:16:09 +0000139 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000140
Chris Lattner47877052006-09-04 04:16:09 +0000141 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000142 if (!Fast) {
143 PM.add(createLoopStrengthReducePass(getTargetLowering()));
144 if (PrintLSR)
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000145 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000146 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000147
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000148 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000149
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000150 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000151 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000152
Chris Lattner47877052006-09-04 04:16:09 +0000153 // Make sure that no unreachable blocks are instruction selected.
154 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000155
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000156 if (!Fast)
157 PM.add(createCodeGenPreparePass(getTargetLowering()));
158
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000159 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendling2b58ce52008-11-04 02:10:20 +0000160
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000161 if (PrintISelInput)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000162 PM.add(createPrintFunctionPass("\n\n"
163 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000164 &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000165
Dan Gohman02dae4b2008-09-25 00:37:07 +0000166 // Standard Lower-Level Passes.
167
Dan Gohmandc756852008-10-01 20:39:19 +0000168 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000169 if (EnableFastISelOption == cl::BOU_TRUE ||
170 (Fast && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmandc756852008-10-01 20:39:19 +0000171 EnableFastISel = true;
172
Chris Lattner47877052006-09-04 04:16:09 +0000173 // Ask the target for an isel.
174 if (addInstSelector(PM, Fast))
175 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000176
Chris Lattner47877052006-09-04 04:16:09 +0000177 // Print the instruction selected machine code...
178 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000179 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000180
Evan Cheng8f0d99e2009-02-09 08:45:39 +0000181 if (!Fast) {
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000182 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000183 PM.add(createMachineSinkingPass());
Evan Cheng8f0d99e2009-02-09 08:45:39 +0000184 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000185
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000186 // Run pre-ra passes.
187 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
188 PM.add(createMachineFunctionPrinterPass(cerr));
189
Evan Cheng3f32d652008-06-04 09:18:41 +0000190 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000191 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000192
193 // Perform stack slot coloring.
Evan Cheng9ef4c532008-06-30 22:33:16 +0000194 if (!Fast)
195 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000196
Dan Gohman02dae4b2008-09-25 00:37:07 +0000197 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000198 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000199
Evan Cheng3f32d652008-06-04 09:18:41 +0000200 // Run post-ra passes.
201 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
202 PM.add(createMachineFunctionPrinterPass(cerr));
203
Dan Gohman02dae4b2008-09-25 00:37:07 +0000204 if (PrintMachineCode)
Evan Cheng3f32d652008-06-04 09:18:41 +0000205 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000206
Christopher Lambada779f2007-07-27 07:36:14 +0000207 PM.add(createLowerSubregsPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000208
Christopher Lambada779f2007-07-27 07:36:14 +0000209 if (PrintMachineCode) // Print the subreg lowered code
210 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000211
Chris Lattner47877052006-09-04 04:16:09 +0000212 // Insert prolog/epilog code. Eliminate abstract frame index references...
213 PM.add(createPrologEpilogCodeInserter());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000214
Evan Cheng3f32d652008-06-04 09:18:41 +0000215 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000216 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000217
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000218 // Second pass scheduler.
Dan Gohman5ce09732008-11-20 19:54:21 +0000219 if (!Fast && !DisablePostRAScheduler) {
Dale Johannesen72f15962007-07-13 17:31:29 +0000220 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000221
Dan Gohman5ce09732008-11-20 19:54:21 +0000222 if (PrintMachineCode)
223 PM.add(createMachineFunctionPrinterPass(cerr));
224 }
225
Dan Gohman23b0d492008-12-18 01:36:42 +0000226 // Branch folding must be run after regalloc and prolog/epilog insertion.
227 if (!Fast)
228 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
229
230 if (PrintMachineCode)
231 PM.add(createMachineFunctionPrinterPass(cerr));
232
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000233 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000234
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000235 if (PrintMachineCode)
236 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000237
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000238 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000239 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000240
Dan Gohman02dae4b2008-09-25 00:37:07 +0000241 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000242}