blob: 17633e2114dcc59646960623d817d8786bba33fd [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"
Daniel Dunbar78945782009-08-13 23:48:47 +000016#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattner11d53c12010-03-13 20:55:24 +000017#include "llvm/CodeGen/Passes.h"
Andrew Trick061efcf2012-02-04 02:56:59 +000018#include "llvm/CodeGen/MachineFunctionAnalysis.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
20#include "llvm/Target/TargetInstrInfo.h"
21#include "llvm/Target/TargetLowering.h"
22#include "llvm/Target/TargetLoweringObjectFile.h"
23#include "llvm/Target/TargetMachine.h"
Chris Lattner47877052006-09-04 04:16:09 +000024#include "llvm/Target/TargetOptions.h"
Andrew Trick061efcf2012-02-04 02:56:59 +000025#include "llvm/Target/TargetSubtargetInfo.h"
26#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattneraf76e592009-08-22 20:48:53 +000027#include "llvm/MC/MCAsmInfo.h"
Andrew Trickd5422652012-02-04 02:56:48 +000028#include "llvm/MC/MCContext.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000029#include "llvm/MC/MCInstrInfo.h"
Chris Lattner56591ab2010-02-02 23:37:42 +000030#include "llvm/MC/MCStreamer.h"
Evan Cheng59ee62d2011-07-11 03:57:24 +000031#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattner6cafdcc2010-02-02 23:45:17 +000032#include "llvm/ADT/OwningPtr.h"
Chris Lattner31442f92007-03-31 00:24:43 +000033#include "llvm/Support/CommandLine.h"
David Greene71847812009-07-14 20:18:05 +000034#include "llvm/Support/FormattedStream.h"
Andrew Trickd5422652012-02-04 02:56:48 +000035#include "llvm/Support/ErrorHandling.h"
Evan Cheng3e74d6f2011-08-24 18:08:43 +000036#include "llvm/Support/TargetRegistry.h"
Chris Lattner47877052006-09-04 04:16:09 +000037using namespace llvm;
38
Andrew Trick061efcf2012-02-04 02:56:59 +000039// Enable or disable FastISel. Both options are needed, because
40// FastISel is enabled by default with -fast, and we wish to be
41// able to enable or disable fast-isel independently from -O0.
42static cl::opt<cl::boolOrDefault>
43EnableFastISelOption("fast-isel", cl::Hidden,
44 cl::desc("Enable the \"fast\" instruction selector"));
45
Daniel Dunbar3538c802010-05-18 17:22:19 +000046static cl::opt<bool> ShowMCEncoding("show-mc-encoding", cl::Hidden,
47 cl::desc("Show encoding in .s output"));
48static cl::opt<bool> ShowMCInst("show-mc-inst", cl::Hidden,
49 cl::desc("Show instruction structure in .s output"));
Chris Lattner85ef2542007-06-19 05:47:49 +000050
Chris Lattner1f219902010-02-02 22:54:51 +000051static cl::opt<cl::boolOrDefault>
52AsmVerbose("asm-verbose", cl::desc("Add comments to directives."),
53 cl::init(cl::BOU_UNSET));
54
Chris Lattnere16ad1a2010-02-02 22:58:13 +000055static bool getVerboseAsm() {
Chris Lattner1f219902010-02-02 22:54:51 +000056 switch (AsmVerbose) {
Chris Lattnere16ad1a2010-02-02 22:58:13 +000057 case cl::BOU_UNSET: return TargetMachine::getAsmVerbosityDefault();
58 case cl::BOU_TRUE: return true;
59 case cl::BOU_FALSE: return false;
Jim Grosbachc92bb502010-08-13 16:55:08 +000060 }
Chandler Carruth732f05c2012-01-10 18:08:01 +000061 llvm_unreachable("Invalid verbose asm state");
Chris Lattner1f219902010-02-02 22:54:51 +000062}
Evan Cheng7da9ecf2010-01-13 00:30:23 +000063
Evan Chengebdeeab2011-07-08 01:53:10 +000064LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
Evan Cheng43966132011-07-19 06:37:02 +000065 StringRef CPU, StringRef FS,
Nick Lewycky8a8d4792011-12-02 22:16:29 +000066 TargetOptions Options,
Evan Chengb95fc312011-11-16 08:38:26 +000067 Reloc::Model RM, CodeModel::Model CM,
68 CodeGenOpt::Level OL)
Nick Lewycky8a8d4792011-12-02 22:16:29 +000069 : TargetMachine(T, Triple, CPU, FS, Options) {
Evan Chengb95fc312011-11-16 08:38:26 +000070 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
Evan Cheng1abf2cb2011-07-14 23:50:31 +000071 AsmInfo = T.createMCAsmInfo(Triple);
Torok Edwind398bae2011-09-30 13:07:47 +000072 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
73 // and if the old one gets included then MCAsmInfo will be NULL and
74 // we'll crash later.
75 // Provide the user with a useful error message about what's wrong.
76 assert(AsmInfo && "MCAsmInfo not initialized."
Jim Grosbachca30f752011-10-25 20:30:48 +000077 "Make sure you include the correct TargetSelect.h"
78 "and that InitializeAllTargetMCs() is being invoked!");
Chris Lattnera7ac47c2009-08-12 07:22:17 +000079}
80
Andrew Trick061efcf2012-02-04 02:56:59 +000081/// Turn exception handling constructs into something the code generators can
82/// handle.
83static void addPassesToHandleExceptions(TargetMachine *TM,
84 PassManagerBase &PM) {
85 switch (TM->getMCAsmInfo()->getExceptionHandlingType()) {
86 case ExceptionHandling::SjLj:
87 // SjLj piggy-backs on dwarf for this bit. The cleanups done apply to both
88 // Dwarf EH prepare needs to be run after SjLj prepare. Otherwise,
89 // catch info can get misplaced when a selector ends up more than one block
90 // removed from the parent invoke(s). This could happen when a landing
91 // pad is shared by multiple invokes and is also a target of a normal
92 // edge from elsewhere.
93 PM.add(createSjLjEHPass(TM->getTargetLowering()));
94 // FALLTHROUGH
95 case ExceptionHandling::DwarfCFI:
96 case ExceptionHandling::ARM:
97 case ExceptionHandling::Win64:
98 PM.add(createDwarfEHPass(TM));
99 break;
100 case ExceptionHandling::None:
101 PM.add(createLowerInvokePass(TM->getTargetLowering()));
102
103 // The lower invoke pass may create unreachable code. Remove it.
104 PM.add(createUnreachableBlockEliminationPass());
105 break;
106 }
107}
108
109/// addPassesToX helper drives creation and initialization of TargetPassConfig.
110static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
111 PassManagerBase &PM,
112 bool DisableVerify) {
113 // Targets may override createPassConfig to provide a target-specific sublass.
114 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
115
116 // Set PassConfig options provided by TargetMachine.
117 PassConfig->setDisableVerify(DisableVerify);
118
Andrew Trick6939fde2012-02-06 22:51:15 +0000119 PM.add(PassConfig);
120
Andrew Trick061efcf2012-02-04 02:56:59 +0000121 PassConfig->addIRPasses();
122
123 addPassesToHandleExceptions(TM, PM);
124
125 PassConfig->addISelPrepare();
126
127 // Install a MachineModuleInfo class, which is an immutable pass that holds
128 // all the per-module stuff we're generating, including MCContext.
129 MachineModuleInfo *MMI =
130 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
131 &TM->getTargetLowering()->getObjFileLowering());
132 PM.add(MMI);
Jim Grosbach7842a742012-02-17 17:35:10 +0000133 MCContext *Context = &MMI->getContext(); // Return the MCContext by-ref.
Andrew Trick061efcf2012-02-04 02:56:59 +0000134
135 // Set up a MachineFunction for the rest of CodeGen to work on.
136 PM.add(new MachineFunctionAnalysis(*TM));
137
138 // Enable FastISel with -fast, but allow that to be overridden.
139 if (EnableFastISelOption == cl::BOU_TRUE ||
140 (TM->getOptLevel() == CodeGenOpt::None &&
141 EnableFastISelOption != cl::BOU_FALSE))
142 TM->setFastISel(true);
143
144 // Ask the target for an isel.
145 if (PassConfig->addInstSelector())
146 return NULL;
147
148 PassConfig->addMachinePasses();
149
Andrew Trickffea03f2012-02-08 21:22:39 +0000150 PassConfig->setInitialized();
151
Andrew Trick061efcf2012-02-04 02:56:59 +0000152 return Context;
153}
154
Chris Lattner5669e302010-02-03 05:55:08 +0000155bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
156 formatted_raw_ostream &Out,
157 CodeGenFileType FileType,
Dan Gohman8772f502010-02-28 00:41:59 +0000158 bool DisableVerify) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000159 // Add common CodeGen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000160 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify);
161 if (!Context)
Chris Lattner5669e302010-02-03 05:55:08 +0000162 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000163
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000164 if (hasMCSaveTempLabels())
165 Context->setAllowTemporaryLabels(false);
166
Chris Lattnerc18409a2010-03-11 22:53:35 +0000167 const MCAsmInfo &MAI = *getMCAsmInfo();
James Molloyb9505852011-09-07 17:24:38 +0000168 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000169 OwningPtr<MCStreamer> AsmStreamer;
170
Chris Lattner47877052006-09-04 04:16:09 +0000171 switch (FileType) {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000172 case CGFT_AssemblyFile: {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000173 MCInstPrinter *InstPrinter =
Jim Grosbachc6449b62012-03-05 19:33:20 +0000174 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
175 Context->getRegisterInfo(), STI);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000176
177 // Create a code emitter if asked to show the encoding.
Daniel Dunbar3538c802010-05-18 17:22:19 +0000178 MCCodeEmitter *MCE = 0;
Evan Cheng78c10ee2011-07-25 23:24:55 +0000179 MCAsmBackend *MAB = 0;
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000180 if (ShowMCEncoding) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000181 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Evan Cheng28c85a82011-07-26 00:42:34 +0000182 MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI, *Context);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000183 MAB = getTarget().createMCAsmBackend(getTargetTriple());
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000184 }
Daniel Dunbar3538c802010-05-18 17:22:19 +0000185
Rafael Espindola89b93722010-12-10 07:39:47 +0000186 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
187 getVerboseAsm(),
188 hasMCUseLoc(),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000189 hasMCUseCFI(),
Nick Lewycky44d798d2011-10-17 23:05:28 +0000190 hasMCUseDwarfDirectory(),
Rafael Espindola89b93722010-12-10 07:39:47 +0000191 InstPrinter,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000192 MCE, MAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +0000193 ShowMCInst);
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000194 AsmStreamer.reset(S);
Chris Lattnerb5c51602010-02-02 19:14:27 +0000195 break;
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000196 }
Chris Lattnerac7798e2010-02-02 23:57:42 +0000197 case CGFT_ObjectFile: {
198 // Create the code emitter for the target if it exists. If not, .o file
199 // emission fails.
Evan Cheng28c85a82011-07-26 00:42:34 +0000200 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI,
201 *Context);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000202 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
203 if (MCE == 0 || MAB == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000204 return true;
Daniel Dunbarcb8326d2010-05-26 21:48:55 +0000205
Evan Cheng28c85a82011-07-26 00:42:34 +0000206 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
207 *Context, *MAB, Out,
208 MCE, hasMCRelaxAll(),
209 hasMCNoExecStack()));
Rafael Espindolaff9dfed2010-10-08 19:37:38 +0000210 AsmStreamer.get()->InitSections();
Chris Lattnerac7798e2010-02-02 23:57:42 +0000211 break;
Chris Lattner0823d2a2010-02-02 18:44:12 +0000212 }
Chris Lattner5669e302010-02-03 05:55:08 +0000213 case CGFT_Null:
214 // The Null output is intended for use for performance analysis and testing,
215 // not real users.
216 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattner5669e302010-02-03 05:55:08 +0000217 break;
Chris Lattner47877052006-09-04 04:16:09 +0000218 }
Daniel Dunbarfdb5a862010-05-23 17:44:06 +0000219
Chris Lattner11d53c12010-03-13 20:55:24 +0000220 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerb23569a2010-04-04 08:18:47 +0000221 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000222 if (Printer == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000223 return true;
Jim Grosbachc92bb502010-08-13 16:55:08 +0000224
Chris Lattner11d53c12010-03-13 20:55:24 +0000225 // If successful, createAsmPrinter took ownership of AsmStreamer.
226 AsmStreamer.take();
Jim Grosbachc92bb502010-08-13 16:55:08 +0000227
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000228 PM.add(Printer);
Jim Grosbachc92bb502010-08-13 16:55:08 +0000229
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000230 PM.add(createGCInfoDeleter());
Chris Lattner5669e302010-02-03 05:55:08 +0000231 return false;
Dan Gohman02dae4b2008-09-25 00:37:07 +0000232}
233
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000234/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner3813d8a2010-02-02 22:31:11 +0000235/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000236/// actually outputting the machine code and resolving things like the address
237/// of functions. This method should returns true if machine code emission is
238/// not supported.
239///
240bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
241 JITCodeEmitter &JCE,
Dan Gohman8772f502010-02-28 00:41:59 +0000242 bool DisableVerify) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000243 // Add common CodeGen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000244 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify);
245 if (!Context)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000246 return true;
247
Evan Chengb95fc312011-11-16 08:38:26 +0000248 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000249 PM.add(createGCInfoDeleter());
250
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000251 return false; // success!
252}
253
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000254/// addPassesToEmitMC - Add passes to the specified pass manager to get
255/// machine code emitted with the MCJIT. This method returns true if machine
256/// code is not supported. It fills the MCContext Ctx pointer which can be
257/// used to build custom MCStreamer.
258///
259bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
260 MCContext *&Ctx,
Jim Grosbach31649e62011-03-18 22:48:41 +0000261 raw_ostream &Out,
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000262 bool DisableVerify) {
263 // Add common CodeGen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000264 Ctx = addPassesToGenerateCode(this, PM, DisableVerify);
265 if (!Ctx)
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000266 return true;
Jim Grosbach31649e62011-03-18 22:48:41 +0000267
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000268 if (hasMCSaveTempLabels())
269 Ctx->setAllowTemporaryLabels(false);
270
Jim Grosbach31649e62011-03-18 22:48:41 +0000271 // Create the code emitter for the target if it exists. If not, .o file
272 // emission fails.
Evan Cheng59ee62d2011-07-11 03:57:24 +0000273 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbach7842a742012-02-17 17:35:10 +0000274 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(),STI,
275 *Ctx);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000276 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
277 if (MCE == 0 || MAB == 0)
Jim Grosbach31649e62011-03-18 22:48:41 +0000278 return true;
279
280 OwningPtr<MCStreamer> AsmStreamer;
Evan Cheng28c85a82011-07-26 00:42:34 +0000281 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
282 *MAB, Out, MCE,
283 hasMCRelaxAll(),
284 hasMCNoExecStack()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000285 AsmStreamer.get()->InitSections();
286
287 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
288 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
289 if (Printer == 0)
290 return true;
291
292 // If successful, createAsmPrinter took ownership of AsmStreamer.
293 AsmStreamer.take();
294
295 PM.add(Printer);
296
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000297 return false; // success!
298}