blob: 41a047780e299b3aa347d05d45a9110a6917abd4 [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"));
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +000040static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
41 cl::desc("Verify generated machine code"),
42 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043
Chris Lattnere06d8eb2008-01-14 19:00:06 +000044// When this works it will be on by default.
45static cl::opt<bool>
46DisablePostRAScheduler("disable-post-RA-scheduler",
47 cl::desc("Disable scheduling after register allocation"),
48 cl::init(true));
49
Dan Gohmane3769ef2008-10-01 20:39:19 +000050// Enable or disable FastISel. Both options are needed, because
51// FastISel is enabled by default with -fast, and we wish to be
52// able to enable or disable fast-isel independently from -fast.
Dan Gohman6d7ee012008-10-07 23:00:56 +000053static cl::opt<cl::boolOrDefault>
Dan Gohmane3769ef2008-10-01 20:39:19 +000054EnableFastISelOption("fast-isel", cl::Hidden,
55 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman6a9b05f2008-09-25 01:14:49 +000056
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000058LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Anderson847b99b2008-08-21 00:14:44 +000059 raw_ostream &Out,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 CodeGenFileType FileType,
Bill Wendling5ed22ac2009-04-29 23:29:43 +000061 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +000062 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +000063 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 return FileModel::Error;
65
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 // Fold redundant debug labels.
67 PM.add(createDebugLabelFoldingPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +000068
69 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000070 PM.add(createMachineFunctionPrinterPass(cerr));
71
Bill Wendling58ed5d22009-04-29 00:15:41 +000072 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 PM.add(createMachineFunctionPrinterPass(cerr));
74
Bill Wendling5ed22ac2009-04-29 23:29:43 +000075 if (OptLevel != CodeGenOpt::None)
Evan Cheng02482c82009-05-07 05:42:24 +000076 PM.add(createCodePlacementOptPass());
Evan Cheng7e29ba02008-02-28 23:29:57 +000077
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078 switch (FileType) {
79 default:
80 break;
81 case TargetMachine::AssemblyFile:
Bill Wendling58ed5d22009-04-29 00:15:41 +000082 if (addAssemblyEmitter(PM, OptLevel, getAsmVerbosityDefault(), Out))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 return FileModel::Error;
84 return FileModel::AsmFile;
85 case TargetMachine::ObjectFile:
86 if (getMachOWriterInfo())
87 return FileModel::MachOFile;
88 else if (getELFWriterInfo())
89 return FileModel::ElfFile;
90 }
91
92 return FileModel::Error;
93}
Dan Gohman7e71ccf2008-09-25 00:37:07 +000094
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
96/// be split up (e.g., to add an object writer pass), this method can be used to
97/// finish up adding passes to emit the file, if necessary.
Dan Gohmane34aa772008-03-11 22:29:46 +000098bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 MachineCodeEmitter *MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000100 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000101 if (MCE)
Bill Wendling58ed5d22009-04-29 00:15:41 +0000102 addSimpleCodeEmitter(PM, OptLevel, PrintEmittedAsm, *MCE);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000103
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000104 PM.add(createGCInfoDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000105
106 // Delete machine code for this function
107 PM.add(createMachineCodeDeleter());
108
109 return false; // success!
110}
111
112/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
113/// get machine code emitted. This uses a MachineCodeEmitter object to handle
114/// actually outputting the machine code and resolving things like the address
115/// of functions. This method should returns true if machine code emission is
116/// not supported.
117///
Dan Gohmane34aa772008-03-11 22:29:46 +0000118bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 MachineCodeEmitter &MCE,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000120 CodeGenOpt::Level OptLevel) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000121 // Add common CodeGen passes.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000122 if (addCommonCodeGenPasses(PM, OptLevel))
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000123 return true;
124
Bill Wendling58ed5d22009-04-29 00:15:41 +0000125 if (addPreEmitPass(PM, OptLevel) && PrintMachineCode)
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000126 PM.add(createMachineFunctionPrinterPass(cerr));
127
Bill Wendling58ed5d22009-04-29 00:15:41 +0000128 addCodeEmitter(PM, OptLevel, PrintEmittedAsm, MCE);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000129
130 PM.add(createGCInfoDeleter());
131
132 // Delete machine code for this function
133 PM.add(createMachineCodeDeleter());
134
135 return false; // success!
136}
137
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000138static void printAndVerify(PassManagerBase &PM,
139 bool allowDoubleDefs = false) {
140 if (PrintMachineCode)
141 PM.add(createMachineFunctionPrinterPass(cerr));
142
143 if (VerifyMachineCode)
144 PM.add(createMachineVerifierPass(allowDoubleDefs));
145}
146
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000147/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
148/// emitting to assembly files or machine code output.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000149///
Bill Wendling58ed5d22009-04-29 00:15:41 +0000150bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000151 CodeGenOpt::Level OptLevel) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 // Standard LLVM-Level Passes.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000153
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 // Run loop strength reduction before anything else.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000155 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 PM.add(createLoopStrengthReducePass(getTargetLowering()));
157 if (PrintLSR)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000158 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 }
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000160
Duncan Sandsf325c482009-05-22 20:36:31 +0000161 // Turn exception handling constructs into something the code generators can
162 // handle.
Dale Johannesen85535762008-04-02 00:25:04 +0000163 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +0000164 PM.add(createLowerInvokePass(getTargetLowering()));
Duncan Sandsf325c482009-05-22 20:36:31 +0000165 else
166 PM.add(createDwarfEHPass(getTargetLowering(), OptLevel==CodeGenOpt::None));
167
168 PM.add(createGCLoweringPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 // Make sure that no unreachable blocks are instruction selected.
171 PM.add(createUnreachableBlockEliminationPass());
172
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000173 if (OptLevel != CodeGenOpt::None)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000174 PM.add(createCodeGenPreparePass(getTargetLowering()));
175
Bill Wendling3e13ce52008-11-13 01:02:14 +0000176 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendlingdac9f712008-11-04 02:10:20 +0000177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 if (PrintISelInput)
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000179 PM.add(createPrintFunctionPass("\n\n"
180 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000181 &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000183 // Standard Lower-Level Passes.
184
Dan Gohmane3769ef2008-10-01 20:39:19 +0000185 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman6d7ee012008-10-07 23:00:56 +0000186 if (EnableFastISelOption == cl::BOU_TRUE ||
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000187 (OptLevel == CodeGenOpt::None && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmane3769ef2008-10-01 20:39:19 +0000188 EnableFastISel = true;
189
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000190 // Ask the target for an isel.
Bill Wendling58ed5d22009-04-29 00:15:41 +0000191 if (addInstSelector(PM, OptLevel))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192 return true;
193
194 // Print the instruction selected machine code...
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000195 printAndVerify(PM, /* allowDoubleDefs= */ true);
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000196
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000197 if (OptLevel != CodeGenOpt::None) {
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000198 PM.add(createMachineLICMPass());
Chris Lattnera132dd42008-01-05 06:14:16 +0000199 PM.add(createMachineSinkingPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000200 printAndVerify(PM, /* allowDoubleDefs= */ true);
Evan Cheng23cf3d12009-02-09 08:45:39 +0000201 }
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000202
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000203 // Run pre-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000204 if (addPreRegAlloc(PM, OptLevel))
205 printAndVerify(PM);
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000206
Evan Cheng14f8a502008-06-04 09:18:41 +0000207 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000209
210 // Perform stack slot coloring.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000211 if (OptLevel != CodeGenOpt::None)
Evan Cheng4f2dfd22009-05-12 18:31:57 +0000212 PM.add(createStackSlotColoringPass(OptLevel >= CodeGenOpt::Aggressive));
Evan Cheng14f8a502008-06-04 09:18:41 +0000213
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000214 printAndVerify(PM); // Print the register-allocated code
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000215
Evan Cheng14f8a502008-06-04 09:18:41 +0000216 // Run post-ra passes.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000217 if (addPostRegAlloc(PM, OptLevel))
218 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000219
Christopher Lambed379732007-07-27 07:36:14 +0000220 PM.add(createLowerSubregsPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000221 printAndVerify(PM);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 // Insert prolog/epilog code. Eliminate abstract frame index references...
224 PM.add(createPrologEpilogCodeInserter());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000225 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 // Second pass scheduler.
Bill Wendling5ed22ac2009-04-29 23:29:43 +0000228 if (OptLevel != CodeGenOpt::None && !DisablePostRAScheduler) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 PM.add(createPostRAScheduler());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000230 printAndVerify(PM);
Dan Gohmana2fa48e2008-11-20 19:54:21 +0000231 }
232
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000233 // Branch folding must be run after regalloc and prolog/epilog insertion.
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000234 if (OptLevel != CodeGenOpt::None) {
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000235 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000236 printAndVerify(PM);
237 }
Dan Gohmanb8ef5442008-12-18 01:36:42 +0000238
Gordon Henriksen36464772008-01-07 01:33:09 +0000239 PM.add(createGCMachineCodeAnalysisPass());
Jakob Stoklund Olesenac32dd92009-05-16 00:33:53 +0000240 printAndVerify(PM);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000241
Gordon Henriksen36464772008-01-07 01:33:09 +0000242 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000243 PM.add(createGCInfoPrinter(*cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000245 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246}