blob: 5129caa977493d402bedac319ab59bc6608a3db7 [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/// addPassesToX helper drives creation and initialization of TargetPassConfig.
82static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
83 PassManagerBase &PM,
84 bool DisableVerify) {
85 // Targets may override createPassConfig to provide a target-specific sublass.
86 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
87
88 // Set PassConfig options provided by TargetMachine.
89 PassConfig->setDisableVerify(DisableVerify);
90
Andrew Trick6939fde2012-02-06 22:51:15 +000091 PM.add(PassConfig);
92
Andrew Trick061efcf2012-02-04 02:56:59 +000093 PassConfig->addIRPasses();
94
Bob Wilson564fbf62012-07-02 19:48:31 +000095 PassConfig->addPassesToHandleExceptions();
Andrew Trick061efcf2012-02-04 02:56:59 +000096
97 PassConfig->addISelPrepare();
98
99 // Install a MachineModuleInfo class, which is an immutable pass that holds
100 // all the per-module stuff we're generating, including MCContext.
101 MachineModuleInfo *MMI =
102 new MachineModuleInfo(*TM->getMCAsmInfo(), *TM->getRegisterInfo(),
103 &TM->getTargetLowering()->getObjFileLowering());
104 PM.add(MMI);
Jim Grosbach7842a742012-02-17 17:35:10 +0000105 MCContext *Context = &MMI->getContext(); // Return the MCContext by-ref.
Andrew Trick061efcf2012-02-04 02:56:59 +0000106
107 // Set up a MachineFunction for the rest of CodeGen to work on.
108 PM.add(new MachineFunctionAnalysis(*TM));
109
110 // Enable FastISel with -fast, but allow that to be overridden.
111 if (EnableFastISelOption == cl::BOU_TRUE ||
112 (TM->getOptLevel() == CodeGenOpt::None &&
113 EnableFastISelOption != cl::BOU_FALSE))
114 TM->setFastISel(true);
115
116 // Ask the target for an isel.
117 if (PassConfig->addInstSelector())
118 return NULL;
119
120 PassConfig->addMachinePasses();
121
Andrew Trickffea03f2012-02-08 21:22:39 +0000122 PassConfig->setInitialized();
123
Andrew Trick061efcf2012-02-04 02:56:59 +0000124 return Context;
125}
126
Chris Lattner5669e302010-02-03 05:55:08 +0000127bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
128 formatted_raw_ostream &Out,
129 CodeGenFileType FileType,
Dan Gohman8772f502010-02-28 00:41:59 +0000130 bool DisableVerify) {
Dan Gohman02dae4b2008-09-25 00:37:07 +0000131 // Add common CodeGen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000132 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify);
133 if (!Context)
Chris Lattner5669e302010-02-03 05:55:08 +0000134 return true;
Bill Wendling04523ea2007-02-08 01:36:53 +0000135
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000136 if (hasMCSaveTempLabels())
137 Context->setAllowTemporaryLabels(false);
138
Chris Lattnerc18409a2010-03-11 22:53:35 +0000139 const MCAsmInfo &MAI = *getMCAsmInfo();
Benjamin Kramer79485312012-05-20 11:24:27 +0000140 const MCRegisterInfo &MRI = *getRegisterInfo();
James Molloyb9505852011-09-07 17:24:38 +0000141 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000142 OwningPtr<MCStreamer> AsmStreamer;
143
Chris Lattner47877052006-09-04 04:16:09 +0000144 switch (FileType) {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000145 case CGFT_AssemblyFile: {
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000146 MCInstPrinter *InstPrinter =
Jim Grosbachc6449b62012-03-05 19:33:20 +0000147 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Craig Topper17463b32012-04-02 06:09:36 +0000148 *getInstrInfo(),
Jim Grosbachc6449b62012-03-05 19:33:20 +0000149 Context->getRegisterInfo(), STI);
Daniel Dunbar3538c802010-05-18 17:22:19 +0000150
151 // Create a code emitter if asked to show the encoding.
Daniel Dunbar3538c802010-05-18 17:22:19 +0000152 MCCodeEmitter *MCE = 0;
Evan Cheng78c10ee2011-07-25 23:24:55 +0000153 MCAsmBackend *MAB = 0;
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000154 if (ShowMCEncoding) {
Evan Cheng59ee62d2011-07-11 03:57:24 +0000155 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbach918f55f2012-05-15 17:35:52 +0000156 MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI, STI,
157 *Context);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000158 MAB = getTarget().createMCAsmBackend(getTargetTriple());
Daniel Dunbar745dacc2010-12-16 03:05:59 +0000159 }
Daniel Dunbar3538c802010-05-18 17:22:19 +0000160
Rafael Espindola89b93722010-12-10 07:39:47 +0000161 MCStreamer *S = getTarget().createAsmStreamer(*Context, Out,
162 getVerboseAsm(),
163 hasMCUseLoc(),
Rafael Espindolaf1a5c7e2011-04-30 03:44:37 +0000164 hasMCUseCFI(),
Nick Lewycky44d798d2011-10-17 23:05:28 +0000165 hasMCUseDwarfDirectory(),
Rafael Espindola89b93722010-12-10 07:39:47 +0000166 InstPrinter,
Evan Cheng78c10ee2011-07-25 23:24:55 +0000167 MCE, MAB,
Bill Wendlinge266ce62011-06-17 20:55:01 +0000168 ShowMCInst);
Rafael Espindola195a0ce2010-11-19 02:26:16 +0000169 AsmStreamer.reset(S);
Chris Lattnerb5c51602010-02-02 19:14:27 +0000170 break;
Chris Lattner6c8d6ec2010-02-03 00:29:55 +0000171 }
Chris Lattnerac7798e2010-02-02 23:57:42 +0000172 case CGFT_ObjectFile: {
173 // Create the code emitter for the target if it exists. If not, .o file
174 // emission fails.
Jim Grosbach918f55f2012-05-15 17:35:52 +0000175 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
176 STI, *Context);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000177 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
178 if (MCE == 0 || MAB == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000179 return true;
Daniel Dunbarcb8326d2010-05-26 21:48:55 +0000180
Evan Cheng28c85a82011-07-26 00:42:34 +0000181 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(),
182 *Context, *MAB, Out,
183 MCE, hasMCRelaxAll(),
184 hasMCNoExecStack()));
Rafael Espindolaff9dfed2010-10-08 19:37:38 +0000185 AsmStreamer.get()->InitSections();
Chris Lattnerac7798e2010-02-02 23:57:42 +0000186 break;
Chris Lattner0823d2a2010-02-02 18:44:12 +0000187 }
Chris Lattner5669e302010-02-03 05:55:08 +0000188 case CGFT_Null:
189 // The Null output is intended for use for performance analysis and testing,
190 // not real users.
191 AsmStreamer.reset(createNullStreamer(*Context));
Chris Lattner5669e302010-02-03 05:55:08 +0000192 break;
Chris Lattner47877052006-09-04 04:16:09 +0000193 }
Daniel Dunbarfdb5a862010-05-23 17:44:06 +0000194
Chris Lattner11d53c12010-03-13 20:55:24 +0000195 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
Chris Lattnerb23569a2010-04-04 08:18:47 +0000196 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000197 if (Printer == 0)
Chris Lattner5669e302010-02-03 05:55:08 +0000198 return true;
Jim Grosbachc92bb502010-08-13 16:55:08 +0000199
Chris Lattner11d53c12010-03-13 20:55:24 +0000200 // If successful, createAsmPrinter took ownership of AsmStreamer.
201 AsmStreamer.take();
Jim Grosbachc92bb502010-08-13 16:55:08 +0000202
Chris Lattner6cafdcc2010-02-02 23:45:17 +0000203 PM.add(Printer);
Jim Grosbachc92bb502010-08-13 16:55:08 +0000204
Gordon Henriksen5eca0752008-08-17 18:44:35 +0000205 PM.add(createGCInfoDeleter());
Chris Lattner5669e302010-02-03 05:55:08 +0000206 return false;
Dan Gohman02dae4b2008-09-25 00:37:07 +0000207}
208
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000209/// addPassesToEmitMachineCode - Add passes to the specified pass manager to
Chris Lattner3813d8a2010-02-02 22:31:11 +0000210/// get machine code emitted. This uses a JITCodeEmitter object to handle
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000211/// actually outputting the machine code and resolving things like the address
212/// of functions. This method should returns true if machine code emission is
213/// not supported.
214///
215bool LLVMTargetMachine::addPassesToEmitMachineCode(PassManagerBase &PM,
216 JITCodeEmitter &JCE,
Dan Gohman8772f502010-02-28 00:41:59 +0000217 bool DisableVerify) {
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000218 // Add common CodeGen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000219 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify);
220 if (!Context)
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000221 return true;
222
Evan Chengb95fc312011-11-16 08:38:26 +0000223 addCodeEmitter(PM, JCE);
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000224 PM.add(createGCInfoDeleter());
225
Bruno Cardoso Lopesa3f99f92009-05-30 20:51:52 +0000226 return false; // success!
227}
228
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000229/// addPassesToEmitMC - Add passes to the specified pass manager to get
230/// machine code emitted with the MCJIT. This method returns true if machine
231/// code is not supported. It fills the MCContext Ctx pointer which can be
232/// used to build custom MCStreamer.
233///
234bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
235 MCContext *&Ctx,
Jim Grosbach31649e62011-03-18 22:48:41 +0000236 raw_ostream &Out,
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000237 bool DisableVerify) {
238 // Add common CodeGen passes.
Andrew Trick061efcf2012-02-04 02:56:59 +0000239 Ctx = addPassesToGenerateCode(this, PM, DisableVerify);
240 if (!Ctx)
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000241 return true;
Jim Grosbach31649e62011-03-18 22:48:41 +0000242
Daniel Dunbara7b8c2b2011-03-28 22:49:19 +0000243 if (hasMCSaveTempLabels())
244 Ctx->setAllowTemporaryLabels(false);
245
Jim Grosbach31649e62011-03-18 22:48:41 +0000246 // Create the code emitter for the target if it exists. If not, .o file
247 // emission fails.
Benjamin Kramer65145512012-05-20 17:24:08 +0000248 const MCRegisterInfo &MRI = *getRegisterInfo();
Evan Cheng59ee62d2011-07-11 03:57:24 +0000249 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Jim Grosbach918f55f2012-05-15 17:35:52 +0000250 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(*getInstrInfo(), MRI,
251 STI, *Ctx);
Evan Cheng78c10ee2011-07-25 23:24:55 +0000252 MCAsmBackend *MAB = getTarget().createMCAsmBackend(getTargetTriple());
253 if (MCE == 0 || MAB == 0)
Jim Grosbach31649e62011-03-18 22:48:41 +0000254 return true;
255
256 OwningPtr<MCStreamer> AsmStreamer;
Evan Cheng28c85a82011-07-26 00:42:34 +0000257 AsmStreamer.reset(getTarget().createMCObjectStreamer(getTargetTriple(), *Ctx,
258 *MAB, Out, MCE,
259 hasMCRelaxAll(),
260 hasMCNoExecStack()));
Jim Grosbach31649e62011-03-18 22:48:41 +0000261 AsmStreamer.get()->InitSections();
262
263 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
264 FunctionPass *Printer = getTarget().createAsmPrinter(*this, *AsmStreamer);
265 if (Printer == 0)
266 return true;
267
268 // If successful, createAsmPrinter took ownership of AsmStreamer.
269 AsmStreamer.take();
270
271 PM.add(Printer);
272
Reid Klecknerc96a82a2010-07-22 05:58:53 +0000273 return false; // success!
274}