blob: faaa41b68b6de0afbc162e29ddb126c2da448847 [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,
Bill Wendling98a366d2009-04-29 23:29:43 +000058 CodeGenOpt::Level OptLevel) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000059 // Add common CodeGen passes.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000060 if (addCommonCodeGenPasses(PM, OptLevel))
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
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000069 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000070 PM.add(createMachineFunctionPrinterPass(cerr));
71
Bill Wendling98a366d2009-04-29 23:29:43 +000072 if (OptLevel != CodeGenOpt::None)
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:
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000079 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Bill Wendling04523ea2007-02-08 01:36:53 +000080 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,
Bill Wendling98a366d2009-04-29 23:29:43 +000097 CodeGenOpt::Level OptLevel) {
Bill Wendling04523ea2007-02-08 01:36:53 +000098 if (MCE)
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +000099 addSimpleCodeEmitter(PM, OptLevel, 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,
Bill Wendling98a366d2009-04-29 23:29:43 +0000117 CodeGenOpt::Level OptLevel) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000118 // Add common CodeGen passes.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000119 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman02dae4b2008-09-25 00:37:07 +0000120 return true;
121
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000122 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman02dae4b2008-09-25 00:37:07 +0000123 PM.add(createMachineFunctionPrinterPass(cerr));
124
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000125 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, MCE);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000126
127 PM.add(createGCInfoDeleter());
128
129 // Delete machine code for this function
130 PM.add(createMachineCodeDeleter());
131
132 return false; // success!
133}
134
Bill Wendling98a366d2009-04-29 23:29:43 +0000135/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
136/// emitting to assembly files or machine code output.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000137///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000138bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling98a366d2009-04-29 23:29:43 +0000139 CodeGenOpt::Level OptLevel) {
Chris Lattner47877052006-09-04 04:16:09 +0000140 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000141
Chris Lattner47877052006-09-04 04:16:09 +0000142 // Run loop strength reduction before anything else.
Bill Wendling98a366d2009-04-29 23:29:43 +0000143 if (OptLevel != CodeGenOpt::None) {
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000144 PM.add(createLoopStrengthReducePass(getTargetLowering()));
145 if (PrintLSR)
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000146 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000147 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000148
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000149 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000150
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000151 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000152 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000153
Chris Lattner47877052006-09-04 04:16:09 +0000154 // Make sure that no unreachable blocks are instruction selected.
155 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000156
Bill Wendling98a366d2009-04-29 23:29:43 +0000157 if (OptLevel != CodeGenOpt::None)
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000158 PM.add(createCodeGenPreparePass(getTargetLowering()));
159
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000160 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendling2b58ce52008-11-04 02:10:20 +0000161
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000162 if (PrintISelInput)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000163 PM.add(createPrintFunctionPass("\n\n"
164 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000165 &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000166
Dan Gohman02dae4b2008-09-25 00:37:07 +0000167 // Standard Lower-Level Passes.
168
Dan Gohmandc756852008-10-01 20:39:19 +0000169 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000170 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling98a366d2009-04-29 23:29:43 +0000171 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmandc756852008-10-01 20:39:19 +0000172 EnableFastISel = true;
173
Chris Lattner47877052006-09-04 04:16:09 +0000174 // Ask the target for an isel.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000175 if (addInstSelector(PM, OptLevel))
Chris Lattner47877052006-09-04 04:16:09 +0000176 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000177
Chris Lattner47877052006-09-04 04:16:09 +0000178 // Print the instruction selected machine code...
179 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000180 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000181
Bill Wendling98a366d2009-04-29 23:29:43 +0000182 if (OptLevel != CodeGenOpt::None) {
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000183 PM.add(createMachineLICMPass());
Chris Lattner3c42f122008-01-05 06:14:16 +0000184 PM.add(createMachineSinkingPass());
Evan Cheng8f0d99e2009-02-09 08:45:39 +0000185 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000186
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000187 // Run pre-ra passes.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000188 if (addPreRegAlloc(PM, OptLevel) && PrintMachineCode)
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000189 PM.add(createMachineFunctionPrinterPass(cerr));
190
Evan Cheng3f32d652008-06-04 09:18:41 +0000191 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000192 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000193
194 // Perform stack slot coloring.
Bill Wendling98a366d2009-04-29 23:29:43 +0000195 if (OptLevel != CodeGenOpt::None)
Evan Cheng9ef4c532008-06-30 22:33:16 +0000196 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000197
Dan Gohman02dae4b2008-09-25 00:37:07 +0000198 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000199 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000200
Evan Cheng3f32d652008-06-04 09:18:41 +0000201 // Run post-ra passes.
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000202 if (addPostRegAlloc(PM, OptLevel) && PrintMachineCode)
Evan Cheng3f32d652008-06-04 09:18:41 +0000203 PM.add(createMachineFunctionPrinterPass(cerr));
204
Dan Gohman02dae4b2008-09-25 00:37:07 +0000205 if (PrintMachineCode)
Evan Cheng3f32d652008-06-04 09:18:41 +0000206 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000207
Christopher Lambada779f2007-07-27 07:36:14 +0000208 PM.add(createLowerSubregsPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000209
Christopher Lambada779f2007-07-27 07:36:14 +0000210 if (PrintMachineCode) // Print the subreg lowered code
211 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000212
Chris Lattner47877052006-09-04 04:16:09 +0000213 // Insert prolog/epilog code. Eliminate abstract frame index references...
214 PM.add(createPrologEpilogCodeInserter());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000215
Evan Cheng3f32d652008-06-04 09:18:41 +0000216 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000217 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000218
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000219 // Second pass scheduler.
Bill Wendling98a366d2009-04-29 23:29:43 +0000220 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dale Johannesen72f15962007-07-13 17:31:29 +0000221 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000222
Dan Gohman5ce09732008-11-20 19:54:21 +0000223 if (PrintMachineCode)
224 PM.add(createMachineFunctionPrinterPass(cerr));
225 }
226
Dan Gohman23b0d492008-12-18 01:36:42 +0000227 // Branch folding must be run after regalloc and prolog/epilog insertion.
Bill Wendling98a366d2009-04-29 23:29:43 +0000228 if (OptLevel != CodeGenOpt::None)
Dan Gohman23b0d492008-12-18 01:36:42 +0000229 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
230
231 if (PrintMachineCode)
232 PM.add(createMachineFunctionPrinterPass(cerr));
233
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000234 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000235
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000236 if (PrintMachineCode)
237 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000238
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000239 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000240 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000241
Dan Gohman02dae4b2008-09-25 00:37:07 +0000242 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000243}