blob: 03b5693a6a7d92d81b58a40134270a72aa5b8fc2 [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"
Chris Lattner402d9d92011-05-22 00:13:44 +000016#include "llvm/Analysis/Passes.h"
Dan Gohman572645c2010-02-12 10:34:29 +000017#include "llvm/Analysis/Verifier.h"
Chris Lattner31442f92007-03-31 00:24:43 +000018#include "llvm/Assembly/PrintModulePass.h"
Daniel Dunbar78945782009-08-13 23:48:47 +000019#include "llvm/CodeGen/AsmPrinter.h"
Dan Gohmanad2afc22009-07-31 18:16:33 +000020#include "llvm/CodeGen/MachineFunctionAnalysis.h"
Chris Lattner11d53c12010-03-13 20:55:24 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
22#include "llvm/CodeGen/GCStrategy.h"
23#include "llvm/CodeGen/Passes.h"
Rafael Espindola195a0ce2010-11-19 02:26:16 +000024#include "llvm/Target/TargetLowering.h"
Chris Lattner47877052006-09-04 04:16:09 +000025#include "llvm/Target/TargetOptions.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000026#include "llvm/MC/MCAsmInfo.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000027#include "llvm/MC/MCInstrInfo.h"
Chris Lattner56591ab2010-02-02 23:37:42 +000028#include "llvm/MC/MCStreamer.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000029#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner56591ab2010-02-02 23:37:42 +000030#include "llvm/Target/TargetData.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000031#include "llvm/Target/TargetInstrInfo.h"
Evan Cheng203576a2011-07-20 19:50:42 +000032#include "llvm/Target/TargetLowering.h"
33#include "llvm/Target/TargetLoweringObjectFile.h"
Evan Chenge76a33b2011-07-20 05:58:47 +000034#include "llvm/Target/TargetRegisterInfo.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000035#include "llvm/Target/TargetSubtargetInfo.h"
Chris Lattner47877052006-09-04 04:16:09 +000036#include "llvm/Transforms/Scalar.h"
Chris Lattner6cafdcc2010-02-02 23:45:17 +000037#include "llvm/ADT/OwningPtr.h"
Chris Lattner31442f92007-03-31 00:24:43 +000038#include "llvm/Support/CommandLine.h"
David Greene2800eb12010-01-04 22:33:16 +000039#include "llvm/Support/Debug.h"
David Greene71847812009-07-14 20:18:05 +000040#include "llvm/Support/FormattedStream.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000041#include "llvm/Support/TargetRegistry.h"
Chris Lattner47877052006-09-04 04:16:09 +000042using namespace llvm;
43
Dan Gohman2c4bf112008-09-25 01:14:49 +000044namespace llvm {
45 bool EnableFastISel;
46}
47
Eric Christopher522c01a2009-11-04 19:57:50 +000048static cl::opt<bool> DisablePostRA("disable-post-ra", cl::Hidden,
49 cl::desc("Disable Post Regalloc"));
50static cl::opt<bool> DisableBranchFold("disable-branch-fold", cl::Hidden,
51 cl::desc("Disable branch folding"));
Bob Wilson15acadd2009-11-26 00:32:21 +000052static cl::opt<bool> DisableTailDuplicate("disable-tail-duplicate", cl::Hidden,
53 cl::desc("Disable tail duplication"));
Bob Wilson516ab962010-01-16 00:29:50 +000054static cl::opt<bool> DisableEarlyTailDup("disable-early-taildup", cl::Hidden,
55 cl::desc("Disable pre-register allocation tail duplication"));
Chandler Carruthdb350872011-10-21 06:46:38 +000056static cl::opt<bool> EnableBlockPlacement("enable-block-placement",
57 cl::Hidden, cl::desc("Enable probability-driven block placement"));
Chandler Carruth37efc9f2011-11-02 07:17:12 +000058static cl::opt<bool> EnableBlockPlacementStats("enable-block-placement-stats",
59 cl::Hidden, cl::desc("Collect probability-driven block placement stats"));
Eric Christopher522c01a2009-11-04 19:57:50 +000060static cl::opt<bool> DisableCodePlace("disable-code-place", cl::Hidden,
61 cl::desc("Disable code placement"));
62static cl::opt<bool> DisableSSC("disable-ssc", cl::Hidden,
63 cl::desc("Disable Stack Slot Coloring"));
Jakob Stoklund Olesen61b2d7f2011-08-19 02:05:35 +000064static cl::opt<bool> DisableMachineDCE("disable-machine-dce", cl::Hidden,
65 cl::desc("Disable Machine Dead Code Elimination"));
Eric Christopher522c01a2009-11-04 19:57:50 +000066static cl::opt<bool> DisableMachineLICM("disable-machine-licm", cl::Hidden,
67 cl::desc("Disable Machine LICM"));
Jakob Stoklund Olesen61b2d7f2011-08-19 02:05:35 +000068static cl::opt<bool> DisableMachineCSE("disable-machine-cse", cl::Hidden,
69 cl::desc("Disable Machine Common Subexpression Elimination"));
Evan Chengf2b91092010-04-17 06:47:47 +000070static cl::opt<bool> DisablePostRAMachineLICM("disable-postra-machine-licm",
71 cl::Hidden,
72 cl::desc("Disable Machine LICM"));
Eric Christopher522c01a2009-11-04 19:57:50 +000073static cl::opt<bool> DisableMachineSink("disable-machine-sink", cl::Hidden,
74 cl::desc("Disable Machine Sinking"));
75static cl::opt<bool> DisableLSR("disable-lsr", cl::Hidden,
76 cl::desc("Disable Loop Strength Reduction Pass"));
77static cl::opt<bool> DisableCGP("disable-cgp", cl::Hidden,
78 cl::desc("Disable Codegen Prepare"));
Chris Lattner85ef2542007-06-19 05:47:49 +000079static cl::opt<bool> PrintLSR("print-lsr-output", cl::Hidden,
80 cl::desc("Print LLVM IR produced by the loop-reduce pass"));
81static cl::opt<bool> PrintISelInput("print-isel-input", cl::Hidden,
82 cl::desc("Print LLVM IR input to isel pass"));
Gordon Henriksen93f96d02008-01-07 01:33:09 +000083static cl::opt<bool> PrintGCInfo("print-gc", cl::Hidden,
84 cl::desc("Dump garbage collector data"));
Daniel Dunbar3538c802010-05-18 17:22:19 +000085static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
86 cl::desc("Show encoding in .s output"));
87static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
88 cl::desc("Show instruction structure in .s output"));
Daniel Dunbarfdb5a862010-05-23 17:44:06 +000089static cl::opt<bool> EnableMCLogging("enable-mc-api-logging", cl::Hidden,
90 cl::desc("Enable MC API logging"));
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +000091static cl::opt<bool> VerifyMachineCode("verify-machineinstrs", cl::Hidden,
92 cl::desc("Verify generated machine code"),
93 cl::init(getenv("LLVM_VERIFY_MACHINEINSTRS")!=NULL));
Chris Lattner85ef2542007-06-19 05:47:49 +000094
Chris Lattner1f219902010-02-02 22:54:51 +000095static cl::opt<cl::boolOrDefault>
96AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
97 cl::init(cl::BOU_UNSET));
98
Chris Lattnere16ad1a2010-02-02 22:58:13 +000099static bool getVerboseAsm() {
Chris Lattner1f219902010-02-02 22:54:51 +0000100 switch (AsmVerbose) {
Chris Lattnere16ad1a2010-02-02 22:58:13 +0000101 default:
102 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
103 case cl::BOU_TRUE: return true;
104 case cl::BOU_FALSE: return false;
Jim Grosbachc92bb502010-08-13 16:55:08 +0000105 }
Chris Lattner1f219902010-02-02 22:54:51 +0000106}
Evan Cheng7da9ecf2010-01-13 00:30:23 +0000107
Dan Gohmandc756852008-10-01 20:39:19 +0000108// Enable or disable FastISel. Both options are needed, because
109// FastISel is enabled by default with -fast, and we wish to be
Dan Gohmane29fea42009-08-26 15:57:57 +0000110// able to enable or disable fast-isel independently from -O0.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000111static cl::opt<cl::boolOrDefault>
Dan Gohmandc756852008-10-01 20:39:19 +0000112EnableFastISelOption("fast-isel", cl::Hidden,
Dan Gohmane29fea42009-08-26 15:57:57 +0000113 cl::desc("Enable the \"fast\" instruction selector"));
Dan Gohman2c4bf112008-09-25 01:14:49 +0000114
Evan Chengebdeeab2011-07-08 01:53:10 +0000115LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
Evan Cheng43966132011-07-19 06:37:02 +0000116 StringRef CPU, StringRef FS,
Evan Chengb95fc312011-11-16 08:38:26 +0000117 Reloc::Model RM, CodeModel::Model CM,
118 CodeGenOpt::Level OL)
Evan Chengebdeeab2011-07-08 01:53:10 +0000119 : TargetMachine(T, Triple, CPU, FS) {
Evan Chengb95fc312011-11-16 08:38:26 +0000120 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
Evan Cheng1abf2cb2011-07-14 23:50:31 +0000121 AsmInfo = T.createMCAsmInfo(Triple);
Torok Edwind398bae2011-09-30 13:07:47 +0000122 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
123 // and if the old one gets included then MCAsmInfo will be NULL and
124 // we'll crash later.
125 // Provide the user with a useful error message about what's wrong.
126 assert(AsmInfo && "MCAsmInfo not initialized."
Jim Grosbachca30f752011-10-25 20:30:48 +0000127 "Make sure you include the correct TargetSelect.h"
128 "and that InitializeAllTargetMCs() is being invoked!");
Chris Lattnera7ac47c2009-08-12 07:22:17 +0000129}
130
Chris Lattner5669e302010-02-03 05:55:08 +0000131bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
132 formatted_raw_ostream &Out,
133 CodeGenFileType FileType,
Dan Gohman8772f502010-02-28 00:41:59 +0000134 bool DisableVerify) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000135 // Add common CodeGen passes.
Chris Lattner11d53c12010-03-13 20:55:24 +0000136 MCContext *Context = 0;
Evan Chengb95fc312011-11-16 08:38:26 +0000137 if (addCommonCodeGenPasses(PM, DisableVerify, Context))
Chris Lattner5669e302010-02-03 05:55:08 +0000138 return true;
Chris Lattner11d53c12010-03-13 20:55:24 +0000139 assert(Context != 0 && "Failed to get MCContext");
Bill Wendling04523ea2007-02-08 01:36:53 +0000140
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000141 if (hasMCSaveTempLabels())
142 Context->setAllowTemporaryLabels(false);
143
Chris Lattnerc18409a2010-03-11 22:53:35 +0000144 const MCAsmInfo &MAI = *getMCAsmInfo();
James Molloyb9505852011-09-07 17:24:38 +0000145 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000146 OwningPtr<MCStreamer> AsmStreamer;
147
Chris Lattner47877052006-09-04 04:16:09 +0000148 switch (FileType) {
Chris Lattner5669e302010-02-03 05:55:08 +0000149 default: return true;
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000150 case CGFT_AssemblyFile: {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000151 MCInstPrinter *InstPrinter =
James Molloyb9505852011-09-07 17:24:38 +0000152 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000153
154 // Create a code emitter if asked to show the encoding.
Daniel Dunbar3538c802010-05-18 17:22:19 +0000155 MCCodeEmitter *MCE = 0;
Evan Cheng78c10ee2011-07-25 23:24:55 +0000156 MCAsmBackend *MAB = 0;
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000157 if (ShowMCEncoding) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000158 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Evan Cheng28c85a82011-07-26 00:42:34 +0000159 MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI, *Context);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000160 MAB = getTarget().createMCAsmBackend(getTargetTriple());
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000161 }
Daniel Dunbar3538c802010-05-18 17:22:19 +0000162
Rafael Espindola89b93722010-12-10 07:39:47 +0000163 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
164 getVerboseAsm(),
165 hasMCUseLoc(),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000166 hasMCUseCFI(),
Nick Lewycky44d798d2011-10-17 23:05:28 +0000167 hasMCUseDwarfDirectory(),
Rafael Espindola89b93722010-12-10 07:39:47 +0000168 InstPrinter,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000169 MCE, MAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +0000170 ShowMCInst);
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000171 AsmStreamer.reset(S);
Chris Lattnerb5c51602010-02-02 19:14:27 +0000172 break;
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000173 }
Chris Lattnerac7798e2010-02-02 23:57:42 +0000174 case CGFT_ObjectFile: {
175 // Create the code emitter for the target if it exists. If not, .o file
176 // emission fails.
Evan Cheng28c85a82011-07-26 00:42:34 +0000177 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI,
178 *Context);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000179 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
180 if (MCE == 0 || MAB == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000181 return true;
Daniel Dunbarcb8326d2010-05-26 21:48:55 +0000182
Evan Cheng28c85a82011-07-26 00:42:34 +0000183 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
184 *Context, *MAB, Out,
185 MCE, hasMCRelaxAll(),
186 hasMCNoExecStack()));
Rafael Espindolaff9dfed2010-10-08 19:37:38 +0000187 AsmStreamer.get()->InitSections();
Chris Lattnerac7798e2010-02-02 23:57:42 +0000188 break;
Chris Lattner0823d2a2010-02-02 18:44:12 +0000189 }
Chris Lattner5669e302010-02-03 05:55:08 +0000190 case CGFT_Null:
191 // The Null output is intended for use for performance analysis and testing,
192 // not real users.
193 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattner5669e302010-02-03 05:55:08 +0000194 break;
Chris Lattner47877052006-09-04 04:16:09 +0000195 }
Daniel Dunbarfdb5a862010-05-23 17:44:06 +0000196
197 if (EnableMCLogging)
198 AsmStreamer.reset(createLoggingStreamer(AsmStreamer.take(), errs()));
199
Chris Lattner11d53c12010-03-13 20:55:24 +0000200 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerb23569a2010-04-04 08:18:47 +0000201 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000202 if (Printer == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000203 return true;
Jim Grosbachc92bb502010-08-13 16:55:08 +0000204
Chris Lattner11d53c12010-03-13 20:55:24 +0000205 // If successful, createAsmPrinter took ownership of AsmStreamer.
206 AsmStreamer.take();
Jim Grosbachc92bb502010-08-13 16:55:08 +0000207
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000208 PM.add(Printer);
Jim Grosbachc92bb502010-08-13 16:55:08 +0000209
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000210 PM.add(createGCInfoDeleter());
Chris Lattner5669e302010-02-03 05:55:08 +0000211 return false;
Dan Gohman02dae4b2008-09-25 00:37:07 +0000212}
213
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000214/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner3813d8a2010-02-02 22:31:11 +0000215/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000216/// actually outputting the machine code and resolving things like the address
217/// of functions. This method should returns true if machine code emission is
218/// not supported.
219///
220bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
221 JITCodeEmitter &JCE,
Dan Gohman8772f502010-02-28 00:41:59 +0000222 bool DisableVerify) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000223 // Add common CodeGen passes.
Chris Lattner11d53c12010-03-13 20:55:24 +0000224 MCContext *Ctx = 0;
Evan Chengb95fc312011-11-16 08:38:26 +0000225 if (addCommonCodeGenPasses(PM, DisableVerify, Ctx))
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000226 return true;
227
Evan Chengb95fc312011-11-16 08:38:26 +0000228 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000229 PM.add(createGCInfoDeleter());
230
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000231 return false; // success!
232}
233
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000234/// addPassesToEmitMC - Add passes to the specified pass manager to get
235/// machine code emitted with the MCJIT. This method returns true if machine
236/// code is not supported. It fills the MCContext Ctx pointer which can be
237/// used to build custom MCStreamer.
238///
239bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
240 MCContext *&Ctx,
Jim Grosbach31649e62011-03-18 22:48:41 +0000241 raw_ostream &Out,
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000242 bool DisableVerify) {
243 // Add common CodeGen passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000244 if (addCommonCodeGenPasses(PM, DisableVerify, Ctx))
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000245 return true;
Jim Grosbach31649e62011-03-18 22:48:41 +0000246
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000247 if (hasMCSaveTempLabels())
248 Ctx->setAllowTemporaryLabels(false);
249
Jim Grosbach31649e62011-03-18 22:48:41 +0000250 // Create the code emitter for the target if it exists. If not, .o file
251 // emission fails.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000252 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Evan Cheng28c85a82011-07-26 00:42:34 +0000253 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(),STI, *Ctx);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000254 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
255 if (MCE == 0 || MAB == 0)
Jim Grosbach31649e62011-03-18 22:48:41 +0000256 return true;
257
258 OwningPtr<MCStreamer> AsmStreamer;
Evan Cheng28c85a82011-07-26 00:42:34 +0000259 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
260 *MAB, Out, MCE,
261 hasMCRelaxAll(),
262 hasMCNoExecStack()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000263 AsmStreamer.get()->InitSections();
264
265 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
266 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
267 if (Printer == 0)
268 return true;
269
270 // If successful, createAsmPrinter took ownership of AsmStreamer.
271 AsmStreamer.take();
272
273 PM.add(Printer);
274
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000275 return false; // success!
276}
277
Chris Lattner11d53c12010-03-13 20:55:24 +0000278static void printNoVerify(PassManagerBase &PM, const char *Banner) {
Jakob Stoklund Olesena76e8162010-03-05 21:49:13 +0000279 if (PrintMachineCode)
280 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
281}
282
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000283static void printAndVerify(PassManagerBase &PM,
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000284 const char *Banner) {
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000285 if (PrintMachineCode)
David Greene2800eb12010-01-04 22:33:16 +0000286 PM.add(createMachineFunctionPrinterPass(dbgs(), Banner));
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000287
288 if (VerifyMachineCode)
Jakob Stoklund Olesen89cab932010-12-18 00:06:56 +0000289 PM.add(createMachineVerifierPass(Banner));
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000290}
291
Bill Wendling98a366d2009-04-29 23:29:43 +0000292/// addCommonCodeGenPasses - Add standard LLVM codegen passes used for both
293/// emitting to assembly files or machine code output.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000294///
Bill Wendlingbe8cc2a2009-04-29 00:15:41 +0000295bool LLVMTargetMachine::addCommonCodeGenPasses(PassManagerBase &PM,
Chris Lattner11d53c12010-03-13 20:55:24 +0000296 bool DisableVerify,
297 MCContext *&OutContext) {
Chris Lattner47877052006-09-04 04:16:09 +0000298 // Standard LLVM-Level Passes.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000299
Dan Gohmanc1be92f2010-10-18 18:04:47 +0000300 // Basic AliasAnalysis support.
Chris Lattner402d9d92011-05-22 00:13:44 +0000301 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
302 // BasicAliasAnalysis wins if they disagree. This is intended to help
303 // support "obvious" type-punning idioms.
304 PM.add(createTypeBasedAliasAnalysisPass());
305 PM.add(createBasicAliasAnalysisPass());
Dan Gohmanc1be92f2010-10-18 18:04:47 +0000306
Dan Gohman8772f502010-02-28 00:41:59 +0000307 // Before running any passes, run the verifier to determine if the input
308 // coming from the front-end and/or optimizer is valid.
309 if (!DisableVerify)
310 PM.add(createVerifierPass());
311
Chris Lattner47877052006-09-04 04:16:09 +0000312 // Run loop strength reduction before anything else.
Evan Chengb95fc312011-11-16 08:38:26 +0000313 if (getOptLevel() != CodeGenOpt::None && !DisableLSR) {
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000314 PM.add(createLoopStrengthReducePass(getTargetLowering()));
315 if (PrintLSR)
David Greene2800eb12010-01-04 22:33:16 +0000316 PM.add(createPrintFunctionPass("\n\n*** Code after LSR ***\n", &dbgs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000317 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000318
Bill Wendling2464d612010-08-04 21:44:13 +0000319 PM.add(createGCLoweringPass());
320
321 // Make sure that no unreachable blocks are instruction selected.
322 PM.add(createUnreachableBlockEliminationPass());
323
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000324 // Turn exception handling constructs into something the code generators can
325 // handle.
Chris Lattner11d53c12010-03-13 20:55:24 +0000326 switch (getMCAsmInfo()->getExceptionHandlingType()) {
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000327 case ExceptionHandling::SjLj:
Jim Grosbach8b818d72009-08-17 16:41:22 +0000328 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
Jim Grosbach86e69fa2010-01-14 21:38:31 +0000329 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
330 // catch info can get misplaced when a selector ends up more than one block
331 // removed from the parent invoke(s). This could happen when a landing
332 // pad is shared by multiple invokes and is also a target of a normal
333 // edge from elsewhere.
Bill Wendling4b6736b2011-09-28 03:07:34 +0000334 PM.add(createSjLjEHPass(getTargetLowering()));
Bill Wendlingaf33b7b2010-08-04 23:36:02 +0000335 // FALLTHROUGH
Anton Korobeynikov3965b5e2011-01-14 21:58:08 +0000336 case ExceptionHandling::DwarfCFI:
Anton Korobeynikovb5e16af2011-03-05 18:43:15 +0000337 case ExceptionHandling::ARM:
Charles Davisd652b132011-05-27 23:47:32 +0000338 case ExceptionHandling::Win64:
Duncan Sands22efc182010-08-31 09:05:06 +0000339 PM.add(createDwarfEHPass(this));
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000340 break;
341 case ExceptionHandling::None:
342 PM.add(createLowerInvokePass(getTargetLowering()));
Bill Wendlingaf33b7b2010-08-04 23:36:02 +0000343
344 // The lower invoke pass may create unreachable code. Remove it.
345 PM.add(createUnreachableBlockEliminationPass());
Jim Grosbach1b747ad2009-08-11 00:09:57 +0000346 break;
347 }
Duncan Sandsb0f1e172009-05-22 20:36:31 +0000348
Evan Chengb95fc312011-11-16 08:38:26 +0000349 if (getOptLevel() != CodeGenOpt::None && !DisableCGP)
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000350 PM.add(createCodeGenPreparePass(getTargetLowering()));
351
Bill Wendlinge9e6bdf2008-11-13 01:02:14 +0000352 PM.add(createStackProtectorPass(getTargetLowering()));
Bill Wendling2b58ce52008-11-04 02:10:20 +0000353
Evan Chengb95fc312011-11-16 08:38:26 +0000354 addPreISel(PM);
Anton Korobeynikov8c64b972010-07-24 20:48:54 +0000355
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000356 if (PrintISelInput)
Daniel Dunbarf4db3a52008-10-21 23:33:38 +0000357 PM.add(createPrintFunctionPass("\n\n"
358 "*** Final LLVM Code input to ISel ***\n",
David Greene2800eb12010-01-04 22:33:16 +0000359 &dbgs()));
Chris Lattnerc8d288f2007-03-31 04:18:03 +0000360
Dan Gohman8772f502010-02-28 00:41:59 +0000361 // All passes which modify the LLVM IR are now complete; run the verifier
362 // to ensure that the IR is valid.
363 if (!DisableVerify)
364 PM.add(createVerifierPass());
365
Dan Gohman02dae4b2008-09-25 00:37:07 +0000366 // Standard Lower-Level Passes.
Jim Grosbachc92bb502010-08-13 16:55:08 +0000367
Chris Lattner11d53c12010-03-13 20:55:24 +0000368 // Install a MachineModuleInfo class, which is an immutable pass that holds
369 // all the per-module stuff we're generating, including MCContext.
Evan Cheng0e6a0522011-07-18 20:57:22 +0000370 MachineModuleInfo *MMI = new MachineModuleInfo(*getMCAsmInfo(),
Evan Chenge76a33b2011-07-20 05:58:47 +0000371 *getRegisterInfo(),
Evan Cheng203576a2011-07-20 19:50:42 +0000372 &getTargetLowering()->getObjFileLowering());
Chris Lattner11d53c12010-03-13 20:55:24 +0000373 PM.add(MMI);
374 OutContext = &MMI->getContext(); // Return the MCContext specifically by-ref.
Dan Gohman02dae4b2008-09-25 00:37:07 +0000375
Dan Gohmanad2afc22009-07-31 18:16:33 +0000376 // Set up a MachineFunction for the rest of CodeGen to work on.
Evan Chengb95fc312011-11-16 08:38:26 +0000377 PM.add(new MachineFunctionAnalysis(*this));
Dan Gohmanad2afc22009-07-31 18:16:33 +0000378
Dan Gohmandc756852008-10-01 20:39:19 +0000379 // Enable FastISel with -fast, but allow that to be overridden.
Dan Gohmaneb0d6ab2008-10-07 23:00:56 +0000380 if (EnableFastISelOption == cl::BOU_TRUE ||
Evan Chengb95fc312011-11-16 08:38:26 +0000381 (getOptLevel() == CodeGenOpt::None &&
382 EnableFastISelOption != cl::BOU_FALSE))
Dan Gohmandc756852008-10-01 20:39:19 +0000383 EnableFastISel = true;
384
Chris Lattner47877052006-09-04 04:16:09 +0000385 // Ask the target for an isel.
Evan Chengb95fc312011-11-16 08:38:26 +0000386 if (addInstSelector(PM))
Chris Lattner47877052006-09-04 04:16:09 +0000387 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000388
Chris Lattner47877052006-09-04 04:16:09 +0000389 // Print the instruction selected machine code...
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000390 printAndVerify(PM, "After Instruction Selection");
Bill Wendling0f940c92007-12-07 21:42:31 +0000391
Dan Gohmanbd77d392010-11-18 18:48:28 +0000392 // Expand pseudo-instructions emitted by ISel.
393 PM.add(createExpandISelPseudosPass());
Dan Gohman668ac2f2010-11-16 21:02:37 +0000394
Rafael Espindola25483912011-07-04 04:54:22 +0000395 // Pre-ra tail duplication.
Evan Chengb95fc312011-11-16 08:38:26 +0000396 if (getOptLevel() != CodeGenOpt::None && !DisableEarlyTailDup) {
Rafael Espindola25483912011-07-04 04:54:22 +0000397 PM.add(createTailDuplicatePass(true));
398 printAndVerify(PM, "After Pre-RegAlloc TailDuplicate");
399 }
400
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000401 // Optimize PHIs before DCE: removing dead PHI cycles may make more
402 // instructions dead.
Evan Chengb95fc312011-11-16 08:38:26 +0000403 if (getOptLevel() != CodeGenOpt::None)
Bob Wilsonbf9b2212010-02-13 00:31:44 +0000404 PM.add(createOptimizePHIsPass());
Evan Cheng00a99a32010-02-06 09:07:11 +0000405
Jim Grosbacha2734422010-08-24 19:05:43 +0000406 // If the target requests it, assign local variables to stack slots relative
407 // to one another and simplify frame index references where possible.
408 PM.add(createLocalStackSlotAllocationPass());
Jim Grosbach3d723672010-08-14 00:15:52 +0000409
Evan Chengb95fc312011-11-16 08:38:26 +0000410 if (getOptLevel() != CodeGenOpt::None) {
Dan Gohman84023e02010-07-10 09:00:22 +0000411 // With optimization, dead code should already be eliminated. However
412 // there is one known exception: lowered code for arguments that are only
413 // used by tail calls, where the tail calls reuse the incoming stack
414 // arguments directly (see t11 in test/CodeGen/X86/sibcall.ll).
Jakob Stoklund Olesen61b2d7f2011-08-19 02:05:35 +0000415 if (!DisableMachineDCE)
416 PM.add(createDeadMachineInstructionElimPass());
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000417 printAndVerify(PM, "After codegen DCE pass");
Dan Gohman84023e02010-07-10 09:00:22 +0000418
Eric Christopher522c01a2009-11-04 19:57:50 +0000419 if (!DisableMachineLICM)
420 PM.add(createMachineLICMPass());
Jakob Stoklund Olesen61b2d7f2011-08-19 02:05:35 +0000421 if (!DisableMachineCSE)
422 PM.add(createMachineCSEPass());
Eric Christopher522c01a2009-11-04 19:57:50 +0000423 if (!DisableMachineSink)
424 PM.add(createMachineSinkingPass());
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000425 printAndVerify(PM, "After Machine LICM, CSE and Sinking passes");
Evan Chengc4af4632010-11-17 20:13:28 +0000426
427 PM.add(createPeepholeOptimizerPass());
428 printAndVerify(PM, "After codegen peephole optimization pass");
Evan Cheng8f0d99e2009-02-09 08:45:39 +0000429 }
Bill Wendling0f940c92007-12-07 21:42:31 +0000430
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000431 // Run pre-ra passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000432 if (addPreRegAlloc(PM))
Jakob Stoklund Olesen73cf7092010-08-05 18:59:59 +0000433 printAndVerify(PM, "After PreRegAlloc passes");
Anton Korobeynikovb013f502008-04-23 18:26:03 +0000434
Evan Cheng3f32d652008-06-04 09:18:41 +0000435 // Perform register allocation.
Evan Chengb95fc312011-11-16 08:38:26 +0000436 PM.add(createRegisterAllocator(getOptLevel()));
Dan Gohman499a9372009-10-31 20:17:39 +0000437 printAndVerify(PM, "After Register Allocation");
Evan Cheng3f32d652008-06-04 09:18:41 +0000438
Evan Chengf2b91092010-04-17 06:47:47 +0000439 // Perform stack slot coloring and post-ra machine LICM.
Evan Chengb95fc312011-11-16 08:38:26 +0000440 if (getOptLevel() != CodeGenOpt::None) {
Evan Cheng6248fa42009-08-05 07:26:17 +0000441 // FIXME: Re-enable coloring with register when it's capable of adding
442 // kill markers.
Evan Chengf2b91092010-04-17 06:47:47 +0000443 if (!DisableSSC)
444 PM.add(createStackSlotColoringPass(false));
Evan Chengd94671a2010-04-07 00:41:17 +0000445
446 // Run post-ra machine LICM to hoist reloads / remats.
Evan Chengf2b91092010-04-17 06:47:47 +0000447 if (!DisablePostRAMachineLICM)
448 PM.add(createMachineLICMPass(false));
449
450 printAndVerify(PM, "After StackSlotColoring and postra Machine LICM");
Dan Gohman499a9372009-10-31 20:17:39 +0000451 }
Dan Gohman02dae4b2008-09-25 00:37:07 +0000452
Evan Cheng3f32d652008-06-04 09:18:41 +0000453 // Run post-ra passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000454 if (addPostRegAlloc(PM))
Dan Gohman499a9372009-10-31 20:17:39 +0000455 printAndVerify(PM, "After PostRegAlloc passes");
Dan Gohman02dae4b2008-09-25 00:37:07 +0000456
Jakob Stoklund Olesen74e2d6e2011-09-25 16:46:08 +0000457 PM.add(createExpandPostRAPseudosPass());
458 printAndVerify(PM, "After ExpandPostRAPseudos");
Bill Wendling04523ea2007-02-08 01:36:53 +0000459
Chris Lattner47877052006-09-04 04:16:09 +0000460 // Insert prolog/epilog code. Eliminate abstract frame index references...
461 PM.add(createPrologEpilogCodeInserter());
Dan Gohman499a9372009-10-31 20:17:39 +0000462 printAndVerify(PM, "After PrologEpilogCodeInserter");
Dan Gohman02dae4b2008-09-25 00:37:07 +0000463
Evan Cheng629adde2009-09-30 08:49:50 +0000464 // Run pre-sched2 passes.
Evan Chengb95fc312011-11-16 08:38:26 +0000465 if (addPreSched2(PM))
Dan Gohman499a9372009-10-31 20:17:39 +0000466 printAndVerify(PM, "After PreSched2 passes");
Evan Cheng629adde2009-09-30 08:49:50 +0000467
Dale Johannesene7e7d0d2007-07-13 17:13:54 +0000468 // Second pass scheduler.
Evan Chengb95fc312011-11-16 08:38:26 +0000469 if (getOptLevel() != CodeGenOpt::None && !DisablePostRA) {
470 PM.add(createPostRAScheduler(getOptLevel()));
Dan Gohman499a9372009-10-31 20:17:39 +0000471 printAndVerify(PM, "After PostRAScheduler");
Dan Gohman5ce09732008-11-20 19:54:21 +0000472 }
473
Dan Gohman23b0d492008-12-18 01:36:42 +0000474 // Branch folding must be run after regalloc and prolog/epilog insertion.
Evan Chengb95fc312011-11-16 08:38:26 +0000475 if (getOptLevel() != CodeGenOpt::None && !DisableBranchFold) {
Bob Wilsona5971032009-10-28 20:46:46 +0000476 PM.add(createBranchFoldingPass(getEnableTailMergeDefault()));
Jakob Stoklund Olesena76e8162010-03-05 21:49:13 +0000477 printNoVerify(PM, "After BranchFolding");
Jakob Stoklund Olesen48872e02009-05-16 00:33:53 +0000478 }
Dan Gohman23b0d492008-12-18 01:36:42 +0000479
Bob Wilson15acadd2009-11-26 00:32:21 +0000480 // Tail duplication.
Evan Chengb95fc312011-11-16 08:38:26 +0000481 if (getOptLevel() != CodeGenOpt::None && !DisableTailDuplicate) {
Evan Cheng79fc6f42009-12-04 09:42:45 +0000482 PM.add(createTailDuplicatePass(false));
Jakob Stoklund Olesena76e8162010-03-05 21:49:13 +0000483 printNoVerify(PM, "After TailDuplicate");
Bob Wilson15acadd2009-11-26 00:32:21 +0000484 }
485
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000486 PM.add(createGCMachineCodeAnalysisPass());
Dan Gohman02dae4b2008-09-25 00:37:07 +0000487
Gordon Henriksen93f96d02008-01-07 01:33:09 +0000488 if (PrintGCInfo)
David Greene2800eb12010-01-04 22:33:16 +0000489 PM.add(createGCInfoPrinter(dbgs()));
Bill Wendling04523ea2007-02-08 01:36:53 +0000490
Evan Chengb95fc312011-11-16 08:38:26 +0000491 if (getOptLevel() != CodeGenOpt::None && !DisableCodePlace) {
Chandler Carruthdb350872011-10-21 06:46:38 +0000492 if (EnableBlockPlacement) {
493 // MachineBlockPlacement is an experimental pass which is disabled by
494 // default currently. Eventually it should subsume CodePlacementOpt, so
495 // when enabled, the other is disabled.
496 PM.add(createMachineBlockPlacementPass());
497 printNoVerify(PM, "After MachineBlockPlacement");
498 } else {
499 PM.add(createCodePlacementOptPass());
500 printNoVerify(PM, "After CodePlacementOpt");
501 }
Chandler Carruth37efc9f2011-11-02 07:17:12 +0000502
503 // Run a separate pass to collect block placement statistics.
504 if (EnableBlockPlacementStats) {
505 PM.add(createMachineBlockPlacementStatsPass());
506 printNoVerify(PM, "After MachineBlockPlacementStats");
507 }
Dan Gohman499a9372009-10-31 20:17:39 +0000508 }
509
Evan Chengb95fc312011-11-16 08:38:26 +0000510 if (addPreEmitPass(PM))
Jakob Stoklund Olesena76e8162010-03-05 21:49:13 +0000511 printNoVerify(PM, "After PreEmit passes");
Evan Cheng517e2552009-11-05 01:16:59 +0000512
Dan Gohman02dae4b2008-09-25 00:37:07 +0000513 return false;
Chris Lattner47877052006-09-04 04:16:09 +0000514}