blob: e1dd46342d4fddd2c8073bcdb130b94e68833d4c [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 Lattner39882262008-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"));
Bill Wendling4aab7ae2008-01-04 08:11:03 +000045static cl::opt<bool>
Evan Cheng14f8a502008-06-04 09:18:41 +000046EnableLICM("machine-licm",
47 cl::init(false), cl::Hidden,
48 cl::desc("Perform loop-invariant code motion on machine code"));
Chris Lattner39882262008-01-04 07:36:53 +000049
Chris Lattnere06d8eb2008-01-14 19:00:06 +000050// When this works it will be on by default.
51static cl::opt<bool>
52DisablePostRAScheduler("disable-post-RA-scheduler",
53 cl::desc("Disable scheduling after register allocation"),
54 cl::init(true));
55
Dan Gohmane3769ef2008-10-01 20:39:19 +000056// Enable or disable FastISel. Both options are needed, because
57// FastISel is enabled by default with -fast, and we wish to be
58// able to enable or disable fast-isel independently from -fast.
Dan Gohman6d7ee012008-10-07 23:00:56 +000059static cl::opt<cl::boolOrDefault>
Dan Gohmane3769ef2008-10-01 20:39:19 +000060EnableFastISelOption("fast-isel", cl::Hidden,
61 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman6a9b05f2008-09-25 01:14:49 +000062
Bill Wendlingdac9f712008-11-04 02:10:20 +000063// Enable stack protectors.
64static cl::opt<int>
65EnableStackProtector("enable-stack-protector", cl::init(0),
66 cl::desc("Use ProPolice as a stack protection method."));
67
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068FileModel::Model
Dan Gohmane34aa772008-03-11 22:29:46 +000069LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Anderson847b99b2008-08-21 00:14:44 +000070 raw_ostream &Out,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071 CodeGenFileType FileType,
72 bool Fast) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +000073 // Add common CodeGen passes.
74 if (addCommonCodeGenPasses(PM, Fast))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 return FileModel::Error;
76
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 // Fold redundant debug labels.
78 PM.add(createDebugLabelFoldingPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +000079
80 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 PM.add(createMachineFunctionPrinterPass(cerr));
82
83 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
84 PM.add(createMachineFunctionPrinterPass(cerr));
85
Devang Patel93698d92008-10-01 23:18:38 +000086 if (!Fast)
Evan Cheng7e29ba02008-02-28 23:29:57 +000087 PM.add(createLoopAlignerPass());
88
Dan Gohmanf17a25c2007-07-18 16:29:46 +000089 switch (FileType) {
90 default:
91 break;
92 case TargetMachine::AssemblyFile:
93 if (addAssemblyEmitter(PM, Fast, Out))
94 return FileModel::Error;
95 return FileModel::AsmFile;
96 case TargetMachine::ObjectFile:
97 if (getMachOWriterInfo())
98 return FileModel::MachOFile;
99 else if (getELFWriterInfo())
100 return FileModel::ElfFile;
101 }
102
103 return FileModel::Error;
104}
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000105
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106/// addPassesToEmitFileFinish - If the passes to emit the specified file had to
107/// be split up (e.g., to add an object writer pass), this method can be used to
108/// finish up adding passes to emit the file, if necessary.
Dan Gohmane34aa772008-03-11 22:29:46 +0000109bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 MachineCodeEmitter *MCE,
111 bool Fast) {
112 if (MCE)
Evan Cheng77547212007-07-20 21:56:13 +0000113 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000114
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000115 PM.add(createGCInfoDeleter());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116
117 // Delete machine code for this function
118 PM.add(createMachineCodeDeleter());
119
120 return false; // success!
121}
122
123/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
124/// get machine code emitted. This uses a MachineCodeEmitter object to handle
125/// actually outputting the machine code and resolving things like the address
126/// of functions. This method should returns true if machine code emission is
127/// not supported.
128///
Dan Gohmane34aa772008-03-11 22:29:46 +0000129bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000130 MachineCodeEmitter &MCE,
131 bool Fast) {
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000132 // Add common CodeGen passes.
133 if (addCommonCodeGenPasses(PM, Fast))
134 return true;
135
136 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
137 PM.add(createMachineFunctionPrinterPass(cerr));
138
139 addCodeEmitter(PM, Fast, PrintEmittedAsm, MCE);
140
141 PM.add(createGCInfoDeleter());
142
143 // Delete machine code for this function
144 PM.add(createMachineCodeDeleter());
145
146 return false; // success!
147}
148
149/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for
150/// both emitting to assembly files or machine code output.
151///
152bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM, bool Fast) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 // Standard LLVM-Level Passes.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000154
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000155 // Run loop strength reduction before anything else.
156 if (!Fast) {
157 PM.add(createLoopStrengthReducePass(getTargetLowering()));
158 if (PrintLSR)
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000159 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 }
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000161
Gordon Henriksen36464772008-01-07 01:33:09 +0000162 PM.add(createGCLoweringPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000163
Dale Johannesen85535762008-04-02 00:25:04 +0000164 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesen748a85c2008-04-01 20:00:57 +0000165 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000166
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 // Make sure that no unreachable blocks are instruction selected.
168 PM.add(createUnreachableBlockEliminationPass());
169
170 if (!Fast)
171 PM.add(createCodeGenPreparePass(getTargetLowering()));
172
Bill Wendlingdac9f712008-11-04 02:10:20 +0000173 PM.add(createStackProtectorPass(EnableStackProtector));
174
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000175 if (PrintISelInput)
Daniel Dunbar1363a6d2008-10-21 23:33:38 +0000176 PM.add(createPrintFunctionPass("\n\n"
177 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b475e92008-10-22 03:25:22 +0000178 &errs()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000180 // Standard Lower-Level Passes.
181
Dan Gohmane3769ef2008-10-01 20:39:19 +0000182 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohman6d7ee012008-10-07 23:00:56 +0000183 if (EnableFastISelOption == cl::BOU_TRUE ||
184 (Fast && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmane3769ef2008-10-01 20:39:19 +0000185 EnableFastISel = true;
186
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 // Ask the target for an isel.
188 if (addInstSelector(PM, Fast))
189 return true;
190
191 // Print the instruction selected machine code...
192 if (PrintMachineCode)
193 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000194
Evan Cheng14f8a502008-06-04 09:18:41 +0000195 if (EnableLICM)
Bill Wendling4aab7ae2008-01-04 08:11:03 +0000196 PM.add(createMachineLICMPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000197
Chris Lattnera132dd42008-01-05 06:14:16 +0000198 if (EnableSinking)
199 PM.add(createMachineSinkingPass());
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000200
Anton Korobeynikov9cba34c2008-04-23 18:26:03 +0000201 // Run pre-ra passes.
202 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
203 PM.add(createMachineFunctionPrinterPass(cerr));
204
Evan Cheng14f8a502008-06-04 09:18:41 +0000205 // Perform register allocation.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 PM.add(createRegisterAllocator());
Evan Cheng14f8a502008-06-04 09:18:41 +0000207
208 // Perform stack slot coloring.
Evan Cheng2ea55502008-06-30 22:33:16 +0000209 if (!Fast)
210 PM.add(createStackSlotColoringPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000211
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000212 if (PrintMachineCode) // Print the register-allocated code
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000213 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000214
Evan Cheng14f8a502008-06-04 09:18:41 +0000215 // Run post-ra passes.
216 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
217 PM.add(createMachineFunctionPrinterPass(cerr));
218
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000219 if (PrintMachineCode)
Evan Cheng14f8a502008-06-04 09:18:41 +0000220 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000221
Christopher Lambed379732007-07-27 07:36:14 +0000222 PM.add(createLowerSubregsPass());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000223
Christopher Lambed379732007-07-27 07:36:14 +0000224 if (PrintMachineCode) // Print the subreg lowered code
225 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000227 // Insert prolog/epilog code. Eliminate abstract frame index references...
228 PM.add(createPrologEpilogCodeInserter());
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000229
Evan Cheng14f8a502008-06-04 09:18:41 +0000230 if (PrintMachineCode)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000232
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 // Second pass scheduler.
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000234 if (!Fast && !DisablePostRAScheduler)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235 PM.add(createPostRAScheduler());
236
237 // Branch folding must be run after regalloc and prolog/epilog insertion.
238 if (!Fast)
239 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendlingb958b0d2007-12-07 21:42:31 +0000240
Gordon Henriksen36464772008-01-07 01:33:09 +0000241 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng14f8a502008-06-04 09:18:41 +0000242
Gordon Henriksen36464772008-01-07 01:33:09 +0000243 if (PrintMachineCode)
244 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000245
Gordon Henriksen36464772008-01-07 01:33:09 +0000246 if (PrintGCInfo)
Gordon Henriksen1aed5992008-08-17 18:44:35 +0000247 PM.add(createGCInfoPrinter(*cerr));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000248
Dan Gohman7e71ccf2008-09-25 00:37:07 +0000249 return false;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250}