blob: e1dd46342d4fddd2c8073bcdb130b94e68833d4c [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 Lattnerc4ce73f2008-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 Wendlingcc8f6032008-01-04 08:11:03 +000045static cl::opt<bool>
Evan Cheng3f32d652008-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 Lattnerc4ce73f2008-01-04 07:36:53 +000049
Chris Lattner459525d2008-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 Gohmandc756852008-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 Gohmaneb0d6ab2008-10-07 23:00:56 +000059static cl::opt<cl::boolOrDefault>
Dan Gohmandc756852008-10-01 20:39:19 +000060EnableFastISelOption("fast-isel", cl::Hidden,
61 cl::desc("Enable the experimental \"fast\" instruction selector"));
Dan Gohman2c4bf112008-09-25 01:14:49 +000062
Bill Wendling2b58ce52008-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
Bill Wendling04523ea2007-02-08 01:36:53 +000068FileModel::Model
Dan Gohmanbfae8312008-03-11 22:29:46 +000069LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
Owen Andersoncb371882008-08-21 00:14:44 +000070 raw_ostream &Out,
Bill Wendling04523ea2007-02-08 01:36:53 +000071 CodeGenFileType FileType,
72 bool Fast) {
Dan Gohman02dae4b2008-09-25 00:37:07 +000073 // Add common CodeGen passes.
74 if (addCommonCodeGenPasses(PM, Fast))
Bill Wendling04523ea2007-02-08 01:36:53 +000075 return FileModel::Error;
76
Jim Laskey9d4209f2006-11-07 19:33:46 +000077 // Fold redundant debug labels.
78 PM.add(createDebugLabelFoldingPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +000079
80 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000081 PM.add(createMachineFunctionPrinterPass(cerr));
82
Chris Lattner47877052006-09-04 04:16:09 +000083 if (addPreEmitPass(PM, Fast) && PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +000084 PM.add(createMachineFunctionPrinterPass(cerr));
85
Devang Patel4ae641f2008-10-01 23:18:38 +000086 if (!Fast)
Evan Chengd703ed62008-02-28 23:29:57 +000087 PM.add(createLoopAlignerPass());
88
Chris Lattner47877052006-09-04 04:16:09 +000089 switch (FileType) {
Bill Wendling04523ea2007-02-08 01:36:53 +000090 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;
Chris Lattner47877052006-09-04 04:16:09 +0000101 }
Bill Wendling04523ea2007-02-08 01:36:53 +0000102
103 return FileModel::Error;
104}
Dan Gohman02dae4b2008-09-25 00:37:07 +0000105
Bill Wendling04523ea2007-02-08 01:36:53 +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 Gohmanbfae8312008-03-11 22:29:46 +0000109bool LLVMTargetMachine::addPassesToEmitFileFinish(PassManagerBase &PM,
Bill Wendling04523ea2007-02-08 01:36:53 +0000110 MachineCodeEmitter *MCE,
111 bool Fast) {
112 if (MCE)
Evan Cheng8bd60352007-07-20 21:56:13 +0000113 addSimpleCodeEmitter(PM, Fast, PrintEmittedAsm, *MCE);
Dan Gohman02dae4b2008-09-25 00:37:07 +0000114
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000115 PM.add(createGCInfoDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000116
Chris Lattner47877052006-09-04 04:16:09 +0000117 // Delete machine code for this function
118 PM.add(createMachineCodeDeleter());
Bill Wendling04523ea2007-02-08 01:36:53 +0000119
Chris Lattner47877052006-09-04 04:16:09 +0000120 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 Gohmanbfae8312008-03-11 22:29:46 +0000129bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
Chris Lattner47877052006-09-04 04:16:09 +0000130 MachineCodeEmitter &MCE,
131 bool Fast) {
Dan Gohman02dae4b2008-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) {
Chris Lattner47877052006-09-04 04:16:09 +0000153 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000154
Chris Lattner47877052006-09-04 04:16:09 +0000155 // Run loop strength reduction before anything else.
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000156 if (!Fast) {
157 PM.add(createLoopStrengthReducePass(getTargetLowering()));
158 if (PrintLSR)
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000159 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000160 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000161
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000162 PM.add(createGCLoweringPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000163
Dale Johannesen1532f3d2008-04-02 00:25:04 +0000164 if (!getTargetAsmInfo()->doesSupportExceptionHandling())
Dale Johannesenb6d5b142008-04-01 20:00:57 +0000165 PM.add(createLowerInvokePass(getTargetLowering()));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000166
Chris Lattner47877052006-09-04 04:16:09 +0000167 // Make sure that no unreachable blocks are instruction selected.
168 PM.add(createUnreachableBlockEliminationPass());
Bill Wendling04523ea2007-02-08 01:36:53 +0000169
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000170 if (!Fast)
171 PM.add(createCodeGenPreparePass(getTargetLowering()));
172
Bill Wendling2b58ce52008-11-04 02:10:20 +0000173 PM.add(createStackProtectorPass(EnableStackProtector));
174
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000175 if (PrintISelInput)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000176 PM.add(createPrintFunctionPass("\n\n"
177 "*** Final LLVM Code input to ISel ***\n",
Daniel Dunbar3b0da262008-10-22 03:25:22 +0000178 &errs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000179
Dan Gohman02dae4b2008-09-25 00:37:07 +0000180 // Standard Lower-Level Passes.
181
Dan Gohmandc756852008-10-01 20:39:19 +0000182 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000183 if (EnableFastISelOption == cl::BOU_TRUE ||
184 (Fast && EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmandc756852008-10-01 20:39:19 +0000185 EnableFastISel = true;
186
Chris Lattner47877052006-09-04 04:16:09 +0000187 // Ask the target for an isel.
188 if (addInstSelector(PM, Fast))
189 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000190
Chris Lattner47877052006-09-04 04:16:09 +0000191 // Print the instruction selected machine code...
192 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000193 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling0f940c92007-12-07 21:42:31 +0000194
Evan Cheng3f32d652008-06-04 09:18:41 +0000195 if (EnableLICM)
Bill Wendlingcc8f6032008-01-04 08:11:03 +0000196 PM.add(createMachineLICMPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000197
Chris Lattner3c42f122008-01-05 06:14:16 +0000198 if (EnableSinking)
199 PM.add(createMachineSinkingPass());
Bill Wendling0f940c92007-12-07 21:42:31 +0000200
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000201 // Run pre-ra passes.
202 if (addPreRegAlloc(PM, Fast) && PrintMachineCode)
203 PM.add(createMachineFunctionPrinterPass(cerr));
204
Evan Cheng3f32d652008-06-04 09:18:41 +0000205 // Perform register allocation.
Chris Lattner47877052006-09-04 04:16:09 +0000206 PM.add(createRegisterAllocator());
Evan Cheng3f32d652008-06-04 09:18:41 +0000207
208 // Perform stack slot coloring.
Evan Cheng9ef4c532008-06-30 22:33:16 +0000209 if (!Fast)
210 PM.add(createStackSlotColoringPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000211
Dan Gohman02dae4b2008-09-25 00:37:07 +0000212 if (PrintMachineCode) // Print the register-allocated code
Bill Wendling04523ea2007-02-08 01:36:53 +0000213 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000214
Evan Cheng3f32d652008-06-04 09:18:41 +0000215 // Run post-ra passes.
216 if (addPostRegAlloc(PM, Fast) && PrintMachineCode)
217 PM.add(createMachineFunctionPrinterPass(cerr));
218
Dan Gohman02dae4b2008-09-25 00:37:07 +0000219 if (PrintMachineCode)
Evan Cheng3f32d652008-06-04 09:18:41 +0000220 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000221
Christopher Lambada779f2007-07-27 07:36:14 +0000222 PM.add(createLowerSubregsPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000223
Christopher Lambada779f2007-07-27 07:36:14 +0000224 if (PrintMachineCode) // Print the subreg lowered code
225 PM.add(createMachineFunctionPrinterPass(cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000226
Chris Lattner47877052006-09-04 04:16:09 +0000227 // Insert prolog/epilog code. Eliminate abstract frame index references...
228 PM.add(createPrologEpilogCodeInserter());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000229
Evan Cheng3f32d652008-06-04 09:18:41 +0000230 if (PrintMachineCode)
Bill Wendling04523ea2007-02-08 01:36:53 +0000231 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000232
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000233 // Second pass scheduler.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000234 if (!Fast && !DisablePostRAScheduler)
Dale Johannesen72f15962007-07-13 17:31:29 +0000235 PM.add(createPostRAScheduler());
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000236
Chris Lattnere01eaa02006-11-16 01:00:07 +0000237 // Branch folding must be run after regalloc and prolog/epilog insertion.
238 if (!Fast)
Dale Johannesene6e43542007-05-22 18:31:04 +0000239 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Bill Wendling0f940c92007-12-07 21:42:31 +0000240
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000241 PM.add(createGCMachineCodeAnalysisPass());
Evan Cheng3f32d652008-06-04 09:18:41 +0000242
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000243 if (PrintMachineCode)
244 PM.add(createMachineFunctionPrinterPass(cerr));
Dan Gohman02dae4b2008-09-25 00:37:07 +0000245
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000246 if (PrintGCInfo)
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000247 PM.add(createGCInfoPrinter(*cerr));
Bill Wendling04523ea2007-02-08 01:36:53 +0000248
Dan Gohman02dae4b2008-09-25 00:37:07 +0000249 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000250}