blob: cac0c83bcac2b571adea83459fae84f4c7b29d50 [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
Andrew Trick061efcf2012-02-04 02:56:59 +000014#include "llvm/Transforms/Scalar.h"
Chris Lattner47877052006-09-04 04:16:09 +000015#include "llvm/PassManager.h"
Bob Wilson30a507a2012-07-02 19:48:45 +000016#include "llvm/Assembly/PrintModulePass.h"
Daniel Dunbar78945782009-08-13 23:48:47 +000017#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattner11d53c12010-03-13 20:55:24 +000018#include "llvm/CodeGen/Passes.h"
Andrew Trick061efcf2012-02-04 02:56:59 +000019#include "llvm/CodeGen/MachineFunctionAnalysis.h"
20#include "llvm/CodeGen/MachineModuleInfo.h"
21#include "llvm/Target/TargetInstrInfo.h"
22#include "llvm/Target/TargetLowering.h"
23#include "llvm/Target/TargetLoweringObjectFile.h"
24#include "llvm/Target/TargetMachine.h"
Chris Lattner47877052006-09-04 04:16:09 +000025#include "llvm/Target/TargetOptions.h"
Andrew Trick061efcf2012-02-04 02:56:59 +000026#include "llvm/Target/TargetSubtargetInfo.h"
27#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000028#include "llvm/MC/MCAsmInfo.h"
Andrew Trickd5422652012-02-04 02:56:48 +000029#include "llvm/MC/MCContext.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000030#include "llvm/MC/MCInstrInfo.h"
Chris Lattner56591ab2010-02-02 23:37:42 +000031#include "llvm/MC/MCStreamer.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000032#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner6cafdcc2010-02-02 23:45:17 +000033#include "llvm/ADT/OwningPtr.h"
Chris Lattner31442f92007-03-31 00:24:43 +000034#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000035#include "llvm/Support/FormattedStream.h"
Andrew Trickd5422652012-02-04 02:56:48 +000036#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000037#include "llvm/Support/TargetRegistry.h"
Chris Lattner47877052006-09-04 04:16:09 +000038using namespace llvm;
39
Andrew Trick061efcf2012-02-04 02:56:59 +000040// Enable or disable FastISel. Both options are needed, because
41// FastISel is enabled by default with -fast, and we wish to be
42// able to enable or disable fast-isel independently from -O0.
43static cl::opt<cl::boolOrDefault>
44EnableFastISelOption("fast-isel", cl::Hidden,
45 cl::desc("Enable the \"fast\" instruction selector"));
46
Daniel Dunbar3538c802010-05-18 17:22:19 +000047static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
48 cl::desc("Show encoding in .s output"));
49static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
50 cl::desc("Show instruction structure in .s output"));
Chris Lattner85ef2542007-06-19 05:47:49 +000051
Chris Lattner1f219902010-02-02 22:54:51 +000052static cl::opt<cl::boolOrDefault>
53AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
54 cl::init(cl::BOU_UNSET));
55
Chris Lattnere16ad1a2010-02-02 22:58:13 +000056static bool getVerboseAsm() {
Chris Lattner1f219902010-02-02 22:54:51 +000057 switch (AsmVerbose) {
Chris Lattnere16ad1a2010-02-02 22:58:13 +000058 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
59 case cl::BOU_TRUE: return true;
60 case cl::BOU_FALSE: return false;
Jim Grosbachc92bb502010-08-13 16:55:08 +000061 }
Chandler Carruth732f05c2012-01-10 18:08:01 +000062 llvm_unreachable("Invalid verbose asm state");
Chris Lattner1f219902010-02-02 22:54:51 +000063}
Evan Cheng7da9ecf2010-01-13 00:30:23 +000064
Evan Chengebdeeab2011-07-08 01:53:10 +000065LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
Evan Cheng43966132011-07-19 06:37:02 +000066 StringRef CPU, StringRef FS,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000067 TargetOptions Options,
Evan Chengb95fc312011-11-16 08:38:26 +000068 Reloc::Model RM, CodeModel::Model CM,
69 CodeGenOpt::Level OL)
Nick Lewycky8a8d4792011-12-02 22:16:29 +000070 : TargetMachine(T, Triple, CPU, FS, Options) {
Evan Chengb95fc312011-11-16 08:38:26 +000071 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
Evan Cheng1abf2cb2011-07-14 23:50:31 +000072 AsmInfo = T.createMCAsmInfo(Triple);
Torok Edwind398bae2011-09-30 13:07:47 +000073 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
74 // and if the old one gets included then MCAsmInfo will be NULL and
75 // we'll crash later.
76 // Provide the user with a useful error message about what's wrong.
77 assert(AsmInfo && "MCAsmInfo not initialized."
Jim Grosbachca30f752011-10-25 20:30:48 +000078 "Make sure you include the correct TargetSelect.h"
79 "and that InitializeAllTargetMCs() is being invoked!");
Chris Lattnera7ac47c2009-08-12 07:22:17 +000080}
81
Andrew Trick061efcf2012-02-04 02:56:59 +000082/// addPassesToX helper drives creation and initialization of TargetPassConfig.
83static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
84 PassManagerBase &PM,
Bob Wilson30a507a2012-07-02 19:48:45 +000085 bool DisableVerify,
86 AnalysisID StartAfter,
87 AnalysisID StopAfter) {
Andrew Trick061efcf2012-02-04 02:56:59 +000088 // Targets may override createPassConfig to provide a target-specific sublass.
89 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilson30a507a2012-07-02 19:48:45 +000090 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trick061efcf2012-02-04 02:56:59 +000091
92 // Set PassConfig options provided by TargetMachine.
93 PassConfig->setDisableVerify(DisableVerify);
94
Andrew Trick6939fde2012-02-06 22:51:15 +000095 PM.add(PassConfig);
96
Andrew Trick061efcf2012-02-04 02:56:59 +000097 PassConfig->addIRPasses();
98
Bob Wilson564fbf62012-07-02 19:48:31 +000099 PassConfig->addPassesToHandleExceptions();
Andrew Trick061efcf2012-02-04 02:56:59 +0000100
101 PassConfig->addISelPrepare();
102
103 // Install a MachineModuleInfo class, which is an immutable pass that holds
104 // all the per-module stuff we're generating, including MCContext.
105 MachineModuleInfo *MMI =
106 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
107 &TM->getTargetLowering()->getObjFileLowering());
108 PM.add(MMI);
Jim Grosbach7842a742012-02-17 17:35:10 +0000109 MCContext *Context = &MMI->getContext(); // Return the MCContext by-ref.
Andrew Trick061efcf2012-02-04 02:56:59 +0000110
111 // Set up a MachineFunction for the rest of CodeGen to work on.
112 PM.add(new MachineFunctionAnalysis(*TM));
113
114 // Enable FastISel with -fast, but allow that to be overridden.
115 if (EnableFastISelOption == cl::BOU_TRUE ||
116 (TM->getOptLevel() == CodeGenOpt::None &&
117 EnableFastISelOption != cl::BOU_FALSE))
118 TM->setFastISel(true);
119
120 // Ask the target for an isel.
121 if (PassConfig->addInstSelector())
122 return NULL;
123
124 PassConfig->addMachinePasses();
125
Andrew Trickffea03f2012-02-08 21:22:39 +0000126 PassConfig->setInitialized();
127
Andrew Trick061efcf2012-02-04 02:56:59 +0000128 return Context;
129}
130
Chris Lattner5669e302010-02-03 05:55:08 +0000131bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
132 formatted_raw_ostream &Out,
133 CodeGenFileType FileType,
Bob Wilson30a507a2012-07-02 19:48:45 +0000134 bool DisableVerify,
135 AnalysisID StartAfter,
136 AnalysisID StopAfter) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000137 // Add common CodeGen passes.
Bob Wilson30a507a2012-07-02 19:48:45 +0000138 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
139 StartAfter, StopAfter);
Andrew Trick061efcf2012-02-04 02:56:59 +0000140 if (!Context)
Chris Lattner5669e302010-02-03 05:55:08 +0000141 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000142
Bob Wilson30a507a2012-07-02 19:48:45 +0000143 if (StopAfter) {
144 // FIXME: The intent is that this should eventually write out a YAML file,
145 // containing the LLVM IR, the machine-level IR (when stopping after a
146 // machine-level pass), and whatever other information is needed to
147 // deserialize the code and resume compilation. For now, just write the
148 // LLVM IR.
149 PM.add(createPrintModulePass(&Out));
150 return false;
151 }
152
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000153 if (hasMCSaveTempLabels())
154 Context->setAllowTemporaryLabels(false);
155
Chris Lattnerc18409a2010-03-11 22:53:35 +0000156 const MCAsmInfo &MAI = *getMCAsmInfo();
Benjamin Kramer79485312012-05-20 11:24:27 +0000157 const MCRegisterInfo &MRI = *getRegisterInfo();
James Molloyb9505852011-09-07 17:24:38 +0000158 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000159 OwningPtr<MCStreamer> AsmStreamer;
160
Chris Lattner47877052006-09-04 04:16:09 +0000161 switch (FileType) {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000162 case CGFT_AssemblyFile: {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000163 MCInstPrinter *InstPrinter =
Jim Grosbachc6449b62012-03-05 19:33:20 +0000164 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Craig Topper17463b32012-04-02 06:09:36 +0000165 *getInstrInfo(),
Jim Grosbachc6449b62012-03-05 19:33:20 +0000166 Context->getRegisterInfo(), STI);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000167
168 // Create a code emitter if asked to show the encoding.
Daniel Dunbar3538c802010-05-18 17:22:19 +0000169 MCCodeEmitter *MCE = 0;
Evan Cheng78c10ee2011-07-25 23:24:55 +0000170 MCAsmBackend *MAB = 0;
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000171 if (ShowMCEncoding) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000172 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbach918f55f2012-05-15 17:35:52 +0000173 MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI, STI,
174 *Context);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000175 MAB = getTarget().createMCAsmBackend(getTargetTriple());
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000176 }
Daniel Dunbar3538c802010-05-18 17:22:19 +0000177
Rafael Espindola89b93722010-12-10 07:39:47 +0000178 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
179 getVerboseAsm(),
180 hasMCUseLoc(),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000181 hasMCUseCFI(),
Nick Lewycky44d798d2011-10-17 23:05:28 +0000182 hasMCUseDwarfDirectory(),
Rafael Espindola89b93722010-12-10 07:39:47 +0000183 InstPrinter,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000184 MCE, MAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +0000185 ShowMCInst);
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000186 AsmStreamer.reset(S);
Chris Lattnerb5c51602010-02-02 19:14:27 +0000187 break;
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000188 }
Chris Lattnerac7798e2010-02-02 23:57:42 +0000189 case CGFT_ObjectFile: {
190 // Create the code emitter for the target if it exists. If not, .o file
191 // emission fails.
Jim Grosbach918f55f2012-05-15 17:35:52 +0000192 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
193 STI, *Context);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000194 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
195 if (MCE == 0 || MAB == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000196 return true;
Daniel Dunbarcb8326d2010-05-26 21:48:55 +0000197
Evan Cheng28c85a82011-07-26 00:42:34 +0000198 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
199 *Context, *MAB, Out,
200 MCE, hasMCRelaxAll(),
201 hasMCNoExecStack()));
Rafael Espindolaff9dfed2010-10-08 19:37:38 +0000202 AsmStreamer.get()->InitSections();
Chris Lattnerac7798e2010-02-02 23:57:42 +0000203 break;
Chris Lattner0823d2a2010-02-02 18:44:12 +0000204 }
Chris Lattner5669e302010-02-03 05:55:08 +0000205 case CGFT_Null:
206 // The Null output is intended for use for performance analysis and testing,
207 // not real users.
208 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattner5669e302010-02-03 05:55:08 +0000209 break;
Chris Lattner47877052006-09-04 04:16:09 +0000210 }
Daniel Dunbarfdb5a862010-05-23 17:44:06 +0000211
Chris Lattner11d53c12010-03-13 20:55:24 +0000212 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerb23569a2010-04-04 08:18:47 +0000213 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000214 if (Printer == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000215 return true;
Jim Grosbachc92bb502010-08-13 16:55:08 +0000216
Chris Lattner11d53c12010-03-13 20:55:24 +0000217 // If successful, createAsmPrinter took ownership of AsmStreamer.
218 AsmStreamer.take();
Jim Grosbachc92bb502010-08-13 16:55:08 +0000219
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000220 PM.add(Printer);
Jim Grosbachc92bb502010-08-13 16:55:08 +0000221
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000222 PM.add(createGCInfoDeleter());
Chris Lattner5669e302010-02-03 05:55:08 +0000223 return false;
Dan Gohman02dae4b2008-09-25 00:37:07 +0000224}
225
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000226/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner3813d8a2010-02-02 22:31:11 +0000227/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000228/// actually outputting the machine code and resolving things like the address
229/// of functions. This method should returns true if machine code emission is
230/// not supported.
231///
232bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
233 JITCodeEmitter &JCE,
Dan Gohman8772f502010-02-28 00:41:59 +0000234 bool DisableVerify) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000235 // Add common CodeGen passes.
Bob Wilson30a507a2012-07-02 19:48:45 +0000236 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
Andrew Trick061efcf2012-02-04 02:56:59 +0000237 if (!Context)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000238 return true;
239
Evan Chengb95fc312011-11-16 08:38:26 +0000240 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000241 PM.add(createGCInfoDeleter());
242
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000243 return false; // success!
244}
245
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000246/// addPassesToEmitMC - Add passes to the specified pass manager to get
247/// machine code emitted with the MCJIT. This method returns true if machine
248/// code is not supported. It fills the MCContext Ctx pointer which can be
249/// used to build custom MCStreamer.
250///
251bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
252 MCContext *&Ctx,
Jim Grosbach31649e62011-03-18 22:48:41 +0000253 raw_ostream &Out,
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000254 bool DisableVerify) {
255 // Add common CodeGen passes.
Bob Wilson30a507a2012-07-02 19:48:45 +0000256 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, 0, 0);
Andrew Trick061efcf2012-02-04 02:56:59 +0000257 if (!Ctx)
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000258 return true;
Jim Grosbach31649e62011-03-18 22:48:41 +0000259
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000260 if (hasMCSaveTempLabels())
261 Ctx->setAllowTemporaryLabels(false);
262
Jim Grosbach31649e62011-03-18 22:48:41 +0000263 // Create the code emitter for the target if it exists. If not, .o file
264 // emission fails.
Benjamin Kramer65145512012-05-20 17:24:08 +0000265 const MCRegisterInfo &MRI = *getRegisterInfo();
Evan Cheng59ee62d2011-07-11 03:57:24 +0000266 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbach918f55f2012-05-15 17:35:52 +0000267 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
268 STI, *Ctx);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000269 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
270 if (MCE == 0 || MAB == 0)
Jim Grosbach31649e62011-03-18 22:48:41 +0000271 return true;
272
273 OwningPtr<MCStreamer> AsmStreamer;
Evan Cheng28c85a82011-07-26 00:42:34 +0000274 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
275 *MAB, Out, MCE,
276 hasMCRelaxAll(),
277 hasMCNoExecStack()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000278 AsmStreamer.get()->InitSections();
279
280 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
281 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
282 if (Printer == 0)
283 return true;
284
285 // If successful, createAsmPrinter took ownership of AsmStreamer.
286 AsmStreamer.take();
287
288 PM.add(Printer);
289
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000290 return false; // success!
291}