blob: de69526a1eb5cb3e90b7d452c3238747ec3769be [file] [log] [blame]
Chris Lattnera916db12006-09-04 04:16:09 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnera916db12006-09-04 04:16:09 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
Andrew Trickf8ea1082012-02-04 02:56:59 +000014#include "llvm/Transforms/Scalar.h"
Chris Lattnera916db12006-09-04 04:16:09 +000015#include "llvm/PassManager.h"
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +000016#include "llvm/CodeGen/AsmPrinter.h"
Chris Lattnere468f882010-03-13 20:55:24 +000017#include "llvm/CodeGen/Passes.h"
Andrew Trickf8ea1082012-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 Lattnera916db12006-09-04 04:16:09 +000024#include "llvm/Target/TargetOptions.h"
Andrew Trickf8ea1082012-02-04 02:56:59 +000025#include "llvm/Target/TargetSubtargetInfo.h"
26#include "llvm/Target/TargetRegisterInfo.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000027#include "llvm/MC/MCAsmInfo.h"
Andrew Trickde401d32012-02-04 02:56:48 +000028#include "llvm/MC/MCContext.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000029#include "llvm/MC/MCInstrInfo.h"
Chris Lattnerb0d44c32010-02-02 23:37:42 +000030#include "llvm/MC/MCStreamer.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000031#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerc49f8c72010-02-02 23:45:17 +000032#include "llvm/ADT/OwningPtr.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000033#include "llvm/Support/CommandLine.h"
David Greenea31f96c2009-07-14 20:18:05 +000034#include "llvm/Support/FormattedStream.h"
Andrew Trickde401d32012-02-04 02:56:48 +000035#include "llvm/Support/ErrorHandling.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000036#include "llvm/Support/TargetRegistry.h"
Chris Lattnera916db12006-09-04 04:16:09 +000037using namespace llvm;
38
Andrew Trickf8ea1082012-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 Dunbar62bc96a2010-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 Lattner37228f62007-06-19 05:47:49 +000050
Chris Lattner32445d32010-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 Lattner530c72a2010-02-02 22:58:13 +000055static bool getVerboseAsm() {
Chris Lattner32445d32010-02-02 22:54:51 +000056 switch (AsmVerbose) {
Chris Lattner530c72a2010-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 Grosbachd1f44652010-08-13 16:55:08 +000060 }
Chandler Carruthf3e85022012-01-10 18:08:01 +000061 llvm_unreachable("Invalid verbose asm state");
Chris Lattner32445d32010-02-02 22:54:51 +000062}
Evan Cheng30bebff2010-01-13 00:30:23 +000063
Evan Cheng4d1ca962011-07-08 01:53:10 +000064LLVMTargetMachine::LLVMTargetMachine(const Target &T, StringRef Triple,
Evan Cheng2129f592011-07-19 06:37:02 +000065 StringRef CPU, StringRef FS,
Nick Lewycky50f02cb2011-12-02 22:16:29 +000066 TargetOptions Options,
Evan Chengecb29082011-11-16 08:38:26 +000067 Reloc::Model RM, CodeModel::Model CM,
68 CodeGenOpt::Level OL)
Nick Lewycky50f02cb2011-12-02 22:16:29 +000069 : TargetMachine(T, Triple, CPU, FS, Options) {
Evan Chengecb29082011-11-16 08:38:26 +000070 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
Evan Cheng1705ab02011-07-14 23:50:31 +000071 AsmInfo = T.createMCAsmInfo(Triple);
Torok Edwinbe5020e2011-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 Grosbacha40f8c42011-10-25 20:30:48 +000077 "Make sure you include the correct TargetSelect.h"
78 "and that InitializeAllTargetMCs() is being invoked!");
Chris Lattner9a6cf912009-08-12 07:22:17 +000079}
80
Andrew Trickf8ea1082012-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
119 PassConfig->addIRPasses();
120
121 addPassesToHandleExceptions(TM, PM);
122
123 PassConfig->addISelPrepare();
124
125 // Install a MachineModuleInfo class, which is an immutable pass that holds
126 // all the per-module stuff we're generating, including MCContext.
127 MachineModuleInfo *MMI =
128 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
129 &TM->getTargetLowering()->getObjFileLowering());
130 PM.add(MMI);
131 MCContext *Context = &MMI->getContext(); // Return the MCContext specifically by-ref.
132
133 // Set up a MachineFunction for the rest of CodeGen to work on.
134 PM.add(new MachineFunctionAnalysis(*TM));
135
136 // Enable FastISel with -fast, but allow that to be overridden.
137 if (EnableFastISelOption == cl::BOU_TRUE ||
138 (TM->getOptLevel() == CodeGenOpt::None &&
139 EnableFastISelOption != cl::BOU_FALSE))
140 TM->setFastISel(true);
141
142 // Ask the target for an isel.
143 if (PassConfig->addInstSelector())
144 return NULL;
145
146 PassConfig->addMachinePasses();
147
148 return Context;
149}
150
Chris Lattneredcf0652010-02-03 05:55:08 +0000151bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
152 formatted_raw_ostream &Out,
153 CodeGenFileType FileType,
Dan Gohman0d8a9af2010-02-28 00:41:59 +0000154 bool DisableVerify) {
Dan Gohmanacb05542008-09-25 00:37:07 +0000155 // Add common CodeGen passes.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000156 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify);
157 if (!Context)
Chris Lattneredcf0652010-02-03 05:55:08 +0000158 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000159
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000160 if (hasMCSaveTempLabels())
161 Context->setAllowTemporaryLabels(false);
162
Chris Lattner768ea2a2010-03-11 22:53:35 +0000163 const MCAsmInfo &MAI = *getMCAsmInfo();
James Molloy4c493e82011-09-07 17:24:38 +0000164 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000165 OwningPtr<MCStreamer> AsmStreamer;
166
Chris Lattnera916db12006-09-04 04:16:09 +0000167 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000168 case CGFT_AssemblyFile: {
Chris Lattner249453f2010-02-03 00:29:55 +0000169 MCInstPrinter *InstPrinter =
James Molloy4c493e82011-09-07 17:24:38 +0000170 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI, STI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000171
172 // Create a code emitter if asked to show the encoding.
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000173 MCCodeEmitter *MCE = 0;
Evan Cheng5928e692011-07-25 23:24:55 +0000174 MCAsmBackend *MAB = 0;
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000175 if (ShowMCEncoding) {
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000176 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Evan Cheng3a792252011-07-26 00:42:34 +0000177 MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI, *Context);
Evan Cheng5928e692011-07-25 23:24:55 +0000178 MAB = getTarget().createMCAsmBackend(getTargetTriple());
Daniel Dunbarecd0c8a2010-12-16 03:05:59 +0000179 }
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000180
Rafael Espindola0a017a62010-12-10 07:39:47 +0000181 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
182 getVerboseAsm(),
183 hasMCUseLoc(),
Rafael Espindolaa3181d12011-04-30 03:44:37 +0000184 hasMCUseCFI(),
Nick Lewycky40f8f2f2011-10-17 23:05:28 +0000185 hasMCUseDwarfDirectory(),
Rafael Espindola0a017a62010-12-10 07:39:47 +0000186 InstPrinter,
Evan Cheng5928e692011-07-25 23:24:55 +0000187 MCE, MAB,
Bill Wendlingb74b9de2011-06-17 20:55:01 +0000188 ShowMCInst);
Rafael Espindolab58867c2010-11-19 02:26:16 +0000189 AsmStreamer.reset(S);
Chris Lattner03dc0f72010-02-02 19:14:27 +0000190 break;
Chris Lattner249453f2010-02-03 00:29:55 +0000191 }
Chris Lattner8856a672010-02-02 23:57:42 +0000192 case CGFT_ObjectFile: {
193 // Create the code emitter for the target if it exists. If not, .o file
194 // emission fails.
Evan Cheng3a792252011-07-26 00:42:34 +0000195 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), STI,
196 *Context);
Evan Cheng5928e692011-07-25 23:24:55 +0000197 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
198 if (MCE == 0 || MAB == 0)
Chris Lattneredcf0652010-02-03 05:55:08 +0000199 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000200
Evan Cheng3a792252011-07-26 00:42:34 +0000201 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
202 *Context, *MAB, Out,
203 MCE, hasMCRelaxAll(),
204 hasMCNoExecStack()));
Rafael Espindolaaf8b4872010-10-08 19:37:38 +0000205 AsmStreamer.get()->InitSections();
Chris Lattner8856a672010-02-02 23:57:42 +0000206 break;
Chris Lattner2fdf5b52010-02-02 18:44:12 +0000207 }
Chris Lattneredcf0652010-02-03 05:55:08 +0000208 case CGFT_Null:
209 // The Null output is intended for use for performance analysis and testing,
210 // not real users.
211 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattneredcf0652010-02-03 05:55:08 +0000212 break;
Chris Lattnera916db12006-09-04 04:16:09 +0000213 }
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000214
Chris Lattnere468f882010-03-13 20:55:24 +0000215 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerd20699b2010-04-04 08:18:47 +0000216 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000217 if (Printer == 0)
Chris Lattneredcf0652010-02-03 05:55:08 +0000218 return true;
Jim Grosbachd1f44652010-08-13 16:55:08 +0000219
Chris Lattnere468f882010-03-13 20:55:24 +0000220 // If successful, createAsmPrinter took ownership of AsmStreamer.
221 AsmStreamer.take();
Jim Grosbachd1f44652010-08-13 16:55:08 +0000222
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000223 PM.add(Printer);
Jim Grosbachd1f44652010-08-13 16:55:08 +0000224
Gordon Henriksend930f912008-08-17 18:44:35 +0000225 PM.add(createGCInfoDeleter());
Chris Lattneredcf0652010-02-03 05:55:08 +0000226 return false;
Dan Gohmanacb05542008-09-25 00:37:07 +0000227}
228
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000229/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner919b9742010-02-02 22:31:11 +0000230/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000231/// actually outputting the machine code and resolving things like the address
232/// of functions. This method should returns true if machine code emission is
233/// not supported.
234///
235bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
236 JITCodeEmitter &JCE,
Dan Gohman0d8a9af2010-02-28 00:41:59 +0000237 bool DisableVerify) {
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000238 // Add common CodeGen passes.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000239 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify);
240 if (!Context)
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000241 return true;
242
Evan Chengecb29082011-11-16 08:38:26 +0000243 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000244 PM.add(createGCInfoDeleter());
245
Bruno Cardoso Lopesa194c3a2009-05-30 20:51:52 +0000246 return false; // success!
247}
248
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000249/// addPassesToEmitMC - Add passes to the specified pass manager to get
250/// machine code emitted with the MCJIT. This method returns true if machine
251/// code is not supported. It fills the MCContext Ctx pointer which can be
252/// used to build custom MCStreamer.
253///
254bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
255 MCContext *&Ctx,
Jim Grosbach7b162492011-03-18 22:48:41 +0000256 raw_ostream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000257 bool DisableVerify) {
258 // Add common CodeGen passes.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000259 Ctx = addPassesToGenerateCode(this, PM, DisableVerify);
260 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000261 return true;
Jim Grosbach7b162492011-03-18 22:48:41 +0000262
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000263 if (hasMCSaveTempLabels())
264 Ctx->setAllowTemporaryLabels(false);
265
Jim Grosbach7b162492011-03-18 22:48:41 +0000266 // Create the code emitter for the target if it exists. If not, .o file
267 // emission fails.
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000268 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Evan Cheng3a792252011-07-26 00:42:34 +0000269 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(),STI, *Ctx);
Evan Cheng5928e692011-07-25 23:24:55 +0000270 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
271 if (MCE == 0 || MAB == 0)
Jim Grosbach7b162492011-03-18 22:48:41 +0000272 return true;
273
274 OwningPtr<MCStreamer> AsmStreamer;
Evan Cheng3a792252011-07-26 00:42:34 +0000275 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
276 *MAB, Out, MCE,
277 hasMCRelaxAll(),
278 hasMCNoExecStack()));
Jim Grosbach7b162492011-03-18 22:48:41 +0000279 AsmStreamer.get()->InitSections();
280
281 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
282 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
283 if (Printer == 0)
284 return true;
285
286 // If successful, createAsmPrinter took ownership of AsmStreamer.
287 AsmStreamer.take();
288
289 PM.add(Printer);
290
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000291 return false; // success!
292}