blob: 5f19660a26800a5de8e37b11e98dad86ff8011d3 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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"
17#include "llvm/Assembly/PrintModulePass.h"
18#include "llvm/Analysis/LoopPass.h"
19#include "llvm/CodeGen/Passes.h"
Gordon Henriksenf194af22008-08-17 12:56:54 +000020#include "llvm/CodeGen/GCStrategy.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/Target/TargetOptions.h"
Dale Johannesen85535762008-04-02 00:25:04 +000022#include "llvm/Target/TargetAsmInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include "llvm/Transforms/Scalar.h"
24#include "llvm/Support/CommandLine.h"
Owen Anderson847b99b2008-08-21 00:14:44 +000025#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026using namespace llvm;
27
Dan Gohman6a9b05f2008-09-25 01:14:49 +000028namespace llvm {
29 bool EnableFastISel;
30}
31
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Cheng77547212007-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 Henriksen36464772008-01-07 01:33:09 +000038static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
39 cl::desc("Dump garbage collector data"));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
Chris Lattnere06d8eb2008-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 Gohmane3769ef2008-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 Gohman6d7ee012008-10-07 23:00:56 +000050static cl::opt<cl::boolOrDefault>
Dan Gohmane3769ef2008-10-01 20:39:19 +000051EnableFastISelOption("fast-isel", cl::Hidden,
52 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman6a9b05f2008-09-25 01:14:49 +000053
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000055LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Anderson847b99b2008-08-21 00:14:44 +000056 raw_ostream &Out,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057 CodeGenFileType FileType,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000058 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +000059 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +000060 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061 return FileModel::Error;
62
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 // Fold redundant debug labels.
64 PM.add(createDebugLabelFoldingPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +000065
66 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000067 PM.add(createMachineFunctionPrinterPass(cerr));
68
Bill Wendling58ed5d22009-04-29 00:15:41 +000069 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 PM.add(createMachineFunctionPrinterPass(cerr));
71
Bill Wendling5ed22ac2009-04-29 23:29:43 +000072 if (OptLevel != CodeGenOpt::None)
Evan Cheng7e29ba02008-02-28 23:29:57 +000073 PM.add(createLoopAlignerPass());
74
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 switch (FileType) {
76 default:
77 break;
78 case TargetMachine::AssemblyFile:
Bill Wendling58ed5d22009-04-29 00:15:41 +000079 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Dan Gohmanf17a25c2007-07-18 16:29:46 +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;
87 }
88
89 return FileModel::Error;
90}
Dan Gohman7e71ccf2008-09-25 00:37:07 +000091
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Gohmane34aa772008-03-11 22:29:46 +000095bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 MachineCodeEmitter *MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000097 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098 if (MCE)
Bill Wendling58ed5d22009-04-29 00:15:41 +000099 addSimpleCodeEmitter(PM, OptLevel, PrintEmittedAsm, *MCE);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000100
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000101 PM.add(createGCInfoDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102
103 // Delete machine code for this function
104 PM.add(createMachineCodeDeleter());
105
106 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 Gohmane34aa772008-03-11 22:29:46 +0000115bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 MachineCodeEmitter &MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000117 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000118 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000119 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000120 return true;
121
Bill Wendling58ed5d22009-04-29 00:15:41 +0000122 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000123 PM.add(createMachineFunctionPrinterPass(cerr));
124
Bill Wendling58ed5d22009-04-29 00:15:41 +0000125 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, MCE);
Dan Gohman7e71ccf2008-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 Wendling5ed22ac2009-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 Gohman7e71ccf2008-09-25 00:37:07 +0000137///
Bill Wendling58ed5d22009-04-29 00:15:41 +0000138bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000139 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 // Standard LLVM-Level Passes.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000141
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 // Run loop strength reduction before anything else.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000143 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 PM.add(createLoopStrengthReducePass(getTargetLowering()));
145 if (PrintLSR)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000146 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 }
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000148
Gordon Henriksen36464772008-01-07 01:33:09 +0000149 PM.add(createGCLoweringPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000150
Dale Johannesen85535762008-04-02 00:25:04 +0000151 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +0000152 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 // Make sure that no unreachable blocks are instruction selected.
155 PM.add(createUnreachableBlockEliminationPass());
156
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000157 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158 PM.add(createCodeGenPreparePass(getTargetLowering()));
159
Bill Wendling3e13ce52008-11-13 01:02:14 +0000160 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendlingdac9f712008-11-04 02:10:20 +0000161
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 if (PrintISelInput)
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000163 PM.add(createPrintFunctionPass("\n\n"
164 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000165 &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000167 // Standard Lower-Level Passes.
168
Dan Gohmane3769ef2008-10-01 20:39:19 +0000169 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman6d7ee012008-10-07 23:00:56 +0000170 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000171 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmane3769ef2008-10-01 20:39:19 +0000172 EnableFastISel = true;
173
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 // Ask the target for an isel.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000175 if (addInstSelector(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 return true;
177
178 // Print the instruction selected machine code...
179 if (PrintMachineCode)
180 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000181
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000182 if (OptLevel != CodeGenOpt::None) {
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000183 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000184 PM.add(createMachineSinkingPass());
Evan Cheng23cf3d12009-02-09 08:45:39 +0000185 }
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000186
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000187 // Run pre-ra passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000188 if (addPreRegAlloc(PM, OptLevel) && PrintMachineCode)
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000189 PM.add(createMachineFunctionPrinterPass(cerr));
190
Evan Cheng14f8a502008-06-04 09:18:41 +0000191 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000193
194 // Perform stack slot coloring.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000195 if (OptLevel != CodeGenOpt::None)
Evan Chengb4093592009-05-05 20:30:36 +0000196 PM.add(createStackSlotColoringPass(OptLevel >= CodeGenOpt::Aggressive));
Evan Cheng14f8a502008-06-04 09:18:41 +0000197
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000198 if (PrintMachineCode) // Print the register-allocated code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000200
Evan Cheng14f8a502008-06-04 09:18:41 +0000201 // Run post-ra passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000202 if (addPostRegAlloc(PM, OptLevel) && PrintMachineCode)
Evan Cheng14f8a502008-06-04 09:18:41 +0000203 PM.add(createMachineFunctionPrinterPass(cerr));
204
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000205 if (PrintMachineCode)
Evan Cheng14f8a502008-06-04 09:18:41 +0000206 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000207
Christopher Lambed379732007-07-27 07:36:14 +0000208 PM.add(createLowerSubregsPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000209
Christopher Lambed379732007-07-27 07:36:14 +0000210 if (PrintMachineCode) // Print the subreg lowered code
211 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000212
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 // Insert prolog/epilog code. Eliminate abstract frame index references...
214 PM.add(createPrologEpilogCodeInserter());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000215
Evan Cheng14f8a502008-06-04 09:18:41 +0000216 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000217 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000218
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 // Second pass scheduler.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000220 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000221 PM.add(createPostRAScheduler());
222
Dan Gohmana2fa48e2008-11-20 19:54:21 +0000223 if (PrintMachineCode)
224 PM.add(createMachineFunctionPrinterPass(cerr));
225 }
226
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000227 // Branch folding must be run after regalloc and prolog/epilog insertion.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000228 if (OptLevel != CodeGenOpt::None)
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000229 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
230
231 if (PrintMachineCode)
232 PM.add(createMachineFunctionPrinterPass(cerr));
233
Gordon Henriksen36464772008-01-07 01:33:09 +0000234 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000235
Gordon Henriksen36464772008-01-07 01:33:09 +0000236 if (PrintMachineCode)
237 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000238
Gordon Henriksen36464772008-01-07 01:33:09 +0000239 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000240 PM.add(createGCInfoPrinter(*cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000242 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243}