blob: 6ba3ef1caef74903203ee265f688624a9064cc5f [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
Chandler Carruthed0881b2012-12-03 16:50:05 +000014#include "llvm/Target/TargetMachine.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000015#include "llvm/Analysis/Passes.h"
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +000016#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000017#include "llvm/CodeGen/BasicTTIImpl.h"
Andrew Trickf8ea1082012-02-04 02:56:59 +000018#include "llvm/CodeGen/MachineFunctionAnalysis.h"
19#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/CodeGen/Passes.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000021#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000022#include "llvm/IR/LegacyPassManager.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000023#include "llvm/IR/Verifier.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000024#include "llvm/MC/MCAsmInfo.h"
Andrew Trickde401d32012-02-04 02:56:48 +000025#include "llvm/MC/MCContext.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000026#include "llvm/MC/MCInstrInfo.h"
Chris Lattnerb0d44c32010-02-02 23:37:42 +000027#include "llvm/MC/MCStreamer.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000028#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000029#include "llvm/Support/CommandLine.h"
Andrew Trickde401d32012-02-04 02:56:48 +000030#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000032#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Target/TargetInstrInfo.h"
34#include "llvm/Target/TargetLowering.h"
35#include "llvm/Target/TargetLoweringObjectFile.h"
36#include "llvm/Target/TargetOptions.h"
37#include "llvm/Target/TargetRegisterInfo.h"
38#include "llvm/Target/TargetSubtargetInfo.h"
39#include "llvm/Transforms/Scalar.h"
Chris Lattnera916db12006-09-04 04:16:09 +000040using namespace llvm;
41
Andrew Trickf8ea1082012-02-04 02:56:59 +000042// Enable or disable FastISel. Both options are needed, because
43// FastISel is enabled by default with -fast, and we wish to be
44// able to enable or disable fast-isel independently from -O0.
45static cl::opt<cl::boolOrDefault>
46EnableFastISelOption("fast-isel", cl::Hidden,
47 cl::desc("Enable the \"fast\" instruction selector"));
48
Rafael Espindola227144c2013-05-13 01:16:13 +000049void LLVMTargetMachine::initAsmInfo() {
Eric Christopherd9134482014-08-04 21:25:23 +000050 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(
51 *getSubtargetImpl()->getRegisterInfo(), getTargetTriple());
Torok Edwinbe5020e2011-09-30 13:07:47 +000052 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
53 // and if the old one gets included then MCAsmInfo will be NULL and
54 // we'll crash later.
55 // Provide the user with a useful error message about what's wrong.
Daniel Sanders753e1762014-02-13 14:44:26 +000056 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
Jim Grosbacha40f8c42011-10-25 20:30:48 +000057 "Make sure you include the correct TargetSelect.h"
58 "and that InitializeAllTargetMCs() is being invoked!");
Daniel Sanders753e1762014-02-13 14:44:26 +000059
Rafael Espindola48fa6ed2014-02-21 03:13:54 +000060 if (Options.DisableIntegratedAS)
Daniel Sanders753e1762014-02-13 14:44:26 +000061 TmpAsmInfo->setUseIntegratedAssembler(false);
62
David Blaikie70bd1fd2014-03-27 20:45:41 +000063 if (Options.CompressDebugSections)
64 TmpAsmInfo->setCompressDebugSections(true);
65
Daniel Sanders753e1762014-02-13 14:44:26 +000066 AsmInfo = TmpAsmInfo;
Chris Lattner9a6cf912009-08-12 07:22:17 +000067}
68
Mehdi Amini93e1ea12015-03-12 00:07:24 +000069LLVMTargetMachine::LLVMTargetMachine(const Target &T,
70 StringRef DataLayoutString,
71 StringRef Triple, StringRef CPU,
72 StringRef FS, TargetOptions Options,
Rafael Espindola227144c2013-05-13 01:16:13 +000073 Reloc::Model RM, CodeModel::Model CM,
74 CodeGenOpt::Level OL)
Mehdi Amini93e1ea12015-03-12 00:07:24 +000075 : TargetMachine(T, DataLayoutString, Triple, CPU, FS, Options) {
Rafael Espindola227144c2013-05-13 01:16:13 +000076 CodeGenInfo = T.createMCCodeGenInfo(Triple, RM, CM, OL);
77}
78
Chandler Carruth8b04c0d2015-02-01 13:20:00 +000079TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
Chandler Carruthc956ab662015-02-01 14:22:17 +000080 return TargetIRAnalysis([this](Function &F) {
81 return TargetTransformInfo(BasicTTIImpl(this, F));
82 });
Chandler Carruth664e3542013-01-07 01:37:14 +000083}
84
Andrew Trickf8ea1082012-02-04 02:56:59 +000085/// addPassesToX helper drives creation and initialization of TargetPassConfig.
86static MCContext *addPassesToGenerateCode(LLVMTargetMachine *TM,
87 PassManagerBase &PM,
Bob Wilsoncac3b902012-07-02 19:48:45 +000088 bool DisableVerify,
89 AnalysisID StartAfter,
90 AnalysisID StopAfter) {
Tom Roeder44cb65f2014-06-05 19:29:43 +000091
Juergen Ributzka5fe955c2014-01-23 19:23:28 +000092 // Add internal analysis passes from the target machine.
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +000093 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
Juergen Ributzka5fe955c2014-01-23 19:23:28 +000094
Eric Christopher710c0ae2014-05-19 21:18:47 +000095 // Targets may override createPassConfig to provide a target-specific
96 // subclass.
Andrew Trickf8ea1082012-02-04 02:56:59 +000097 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilsoncac3b902012-07-02 19:48:45 +000098 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +000099
100 // Set PassConfig options provided by TargetMachine.
101 PassConfig->setDisableVerify(DisableVerify);
102
Andrew Trick34914912012-02-06 22:51:15 +0000103 PM.add(PassConfig);
104
Andrew Trickf8ea1082012-02-04 02:56:59 +0000105 PassConfig->addIRPasses();
106
Bill Wendlingc786b312012-11-30 22:08:55 +0000107 PassConfig->addCodeGenPrepare();
108
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000109 PassConfig->addPassesToHandleExceptions();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000110
111 PassConfig->addISelPrepare();
112
113 // Install a MachineModuleInfo class, which is an immutable pass that holds
114 // all the per-module stuff we're generating, including MCContext.
Eric Christopherd9134482014-08-04 21:25:23 +0000115 MachineModuleInfo *MMI = new MachineModuleInfo(
116 *TM->getMCAsmInfo(), *TM->getSubtargetImpl()->getRegisterInfo(),
Eric Christopher36fe0282015-02-03 07:22:52 +0000117 TM->getObjFileLowering());
Andrew Trickf8ea1082012-02-04 02:56:59 +0000118 PM.add(MMI);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000119
120 // Set up a MachineFunction for the rest of CodeGen to work on.
121 PM.add(new MachineFunctionAnalysis(*TM));
122
123 // Enable FastISel with -fast, but allow that to be overridden.
124 if (EnableFastISelOption == cl::BOU_TRUE ||
125 (TM->getOptLevel() == CodeGenOpt::None &&
126 EnableFastISelOption != cl::BOU_FALSE))
127 TM->setFastISel(true);
128
129 // Ask the target for an isel.
130 if (PassConfig->addInstSelector())
Craig Topperc0196b12014-04-14 00:51:57 +0000131 return nullptr;
Andrew Trickf8ea1082012-02-04 02:56:59 +0000132
133 PassConfig->addMachinePasses();
134
Andrew Trickdd37d522012-02-08 21:22:39 +0000135 PassConfig->setInitialized();
136
Bill Wendlingafc10362013-06-19 20:51:24 +0000137 return &MMI->getContext();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000138}
139
Chris Lattneredcf0652010-02-03 05:55:08 +0000140bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
141 formatted_raw_ostream &Out,
142 CodeGenFileType FileType,
Bob Wilsoncac3b902012-07-02 19:48:45 +0000143 bool DisableVerify,
144 AnalysisID StartAfter,
145 AnalysisID StopAfter) {
Dan Gohmanacb05542008-09-25 00:37:07 +0000146 // Add common CodeGen passes.
Bob Wilsoncac3b902012-07-02 19:48:45 +0000147 MCContext *Context = addPassesToGenerateCode(this, PM, DisableVerify,
148 StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000149 if (!Context)
Chris Lattneredcf0652010-02-03 05:55:08 +0000150 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000151
Bob Wilsoncac3b902012-07-02 19:48:45 +0000152 if (StopAfter) {
153 // FIXME: The intent is that this should eventually write out a YAML file,
154 // containing the LLVM IR, the machine-level IR (when stopping after a
155 // machine-level pass), and whatever other information is needed to
156 // deserialize the code and resume compilation. For now, just write the
157 // LLVM IR.
Chandler Carruth9d805132014-01-12 11:30:46 +0000158 PM.add(createPrintModulePass(Out));
Bob Wilsoncac3b902012-07-02 19:48:45 +0000159 return false;
160 }
161
Eric Christopherc21d3d52014-05-16 00:32:52 +0000162 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000163 Context->setAllowTemporaryLabels(false);
164
James Molloy4c493e82011-09-07 17:24:38 +0000165 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Eric Christopherd9134482014-08-04 21:25:23 +0000166 const MCAsmInfo &MAI = *getMCAsmInfo();
167 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
168 const MCInstrInfo &MII = *getSubtargetImpl()->getInstrInfo();
Ahmed Charles56440fd2014-03-06 05:51:42 +0000169 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000170
Chris Lattnera916db12006-09-04 04:16:09 +0000171 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000172 case CGFT_AssemblyFile: {
Chris Lattner249453f2010-02-03 00:29:55 +0000173 MCInstPrinter *InstPrinter =
Jim Grosbachfd93a592012-03-05 19:33:20 +0000174 getTarget().createMCInstPrinter(MAI.getAssemblerDialect(), MAI,
Bill Wendling551a6775d2013-06-18 06:07:26 +0000175 MII, MRI, STI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000176
177 // Create a code emitter if asked to show the encoding.
Craig Topperc0196b12014-04-14 00:51:57 +0000178 MCCodeEmitter *MCE = nullptr;
Eric Christopher5d376062014-05-15 23:27:49 +0000179 if (Options.MCOptions.ShowMCEncoding)
Eric Christopher0169e422015-03-10 22:03:14 +0000180 MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000181
Bill Wendling550c76d2013-09-09 19:48:37 +0000182 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
183 TargetCPU);
Eric Christopher5d376062014-05-15 23:27:49 +0000184 MCStreamer *S = getTarget().createAsmStreamer(
Eric Christophereb719722014-05-20 23:59:50 +0000185 *Context, Out, Options.MCOptions.AsmVerbose,
186 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
187 Options.MCOptions.ShowMCInst);
Rafael Espindolab58867c2010-11-19 02:26:16 +0000188 AsmStreamer.reset(S);
Chris Lattner03dc0f72010-02-02 19:14:27 +0000189 break;
Chris Lattner249453f2010-02-03 00:29:55 +0000190 }
Chris Lattner8856a672010-02-02 23:57:42 +0000191 case CGFT_ObjectFile: {
192 // Create the code emitter for the target if it exists. If not, .o file
193 // emission fails.
Eric Christopher0169e422015-03-10 22:03:14 +0000194 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000195 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
Eli Bendersky26e7efe2012-11-22 14:10:40 +0000196 TargetCPU);
Craig Topperc0196b12014-04-14 00:51:57 +0000197 if (!MCE || !MAB)
Chris Lattneredcf0652010-02-03 05:55:08 +0000198 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000199
Rafael Espindola7b61ddf2014-10-15 16:12:52 +0000200 AsmStreamer.reset(
201 getTarget()
202 .createMCObjectStreamer(getTargetTriple(), *Context, *MAB, Out, MCE,
203 STI, Options.MCOptions.MCRelaxAll));
Chris Lattner8856a672010-02-02 23:57:42 +0000204 break;
Chris Lattner2fdf5b52010-02-02 18:44:12 +0000205 }
Chris Lattneredcf0652010-02-03 05:55:08 +0000206 case CGFT_Null:
207 // The Null output is intended for use for performance analysis and testing,
208 // not real users.
Rafael Espindola1fc003e2014-06-20 13:11:28 +0000209 AsmStreamer.reset(getTarget().createNullStreamer(*Context));
Chris Lattneredcf0652010-02-03 05:55:08 +0000210 break;
Chris Lattnera916db12006-09-04 04:16:09 +0000211 }
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000212
Chris Lattnere468f882010-03-13 20:55:24 +0000213 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
David Blaikie94598322015-01-18 20:29:04 +0000214 FunctionPass *Printer =
215 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
Craig Topperc0196b12014-04-14 00:51:57 +0000216 if (!Printer)
Chris Lattneredcf0652010-02-03 05:55:08 +0000217 return true;
Jim Grosbachd1f44652010-08-13 16:55:08 +0000218
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000219 PM.add(Printer);
Jim Grosbachd1f44652010-08-13 16:55:08 +0000220
Chris Lattneredcf0652010-02-03 05:55:08 +0000221 return false;
Dan Gohmanacb05542008-09-25 00:37:07 +0000222}
223
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000224/// addPassesToEmitMC - Add passes to the specified pass manager to get
225/// machine code emitted with the MCJIT. This method returns true if machine
226/// code is not supported. It fills the MCContext Ctx pointer which can be
227/// used to build custom MCStreamer.
228///
229bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM,
230 MCContext *&Ctx,
Jim Grosbach7b162492011-03-18 22:48:41 +0000231 raw_ostream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000232 bool DisableVerify) {
233 // Add common CodeGen passes.
Craig Topperc0196b12014-04-14 00:51:57 +0000234 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000235 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000236 return true;
Jim Grosbach7b162492011-03-18 22:48:41 +0000237
Eric Christopherc21d3d52014-05-16 00:32:52 +0000238 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000239 Ctx->setAllowTemporaryLabels(false);
240
Jim Grosbach7b162492011-03-18 22:48:41 +0000241 // Create the code emitter for the target if it exists. If not, .o file
242 // emission fails.
Eric Christopherd9134482014-08-04 21:25:23 +0000243 const MCRegisterInfo &MRI = *getSubtargetImpl()->getRegisterInfo();
Evan Chengc5e6d2f2011-07-11 03:57:24 +0000244 const MCSubtargetInfo &STI = getSubtarget<MCSubtargetInfo>();
Eric Christopherd9134482014-08-04 21:25:23 +0000245 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(
Eric Christopher0169e422015-03-10 22:03:14 +0000246 *getSubtargetImpl()->getInstrInfo(), MRI, *Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000247 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
248 TargetCPU);
Craig Topperc0196b12014-04-14 00:51:57 +0000249 if (!MCE || !MAB)
Jim Grosbach7b162492011-03-18 22:48:41 +0000250 return true;
251
David Blaikie94598322015-01-18 20:29:04 +0000252 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
253 getTargetTriple(), *Ctx, *MAB, Out, MCE, STI,
254 Options.MCOptions.MCRelaxAll));
Jim Grosbach7b162492011-03-18 22:48:41 +0000255
256 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
David Blaikie94598322015-01-18 20:29:04 +0000257 FunctionPass *Printer =
258 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
Craig Topperc0196b12014-04-14 00:51:57 +0000259 if (!Printer)
Jim Grosbach7b162492011-03-18 22:48:41 +0000260 return true;
261
Jim Grosbach7b162492011-03-18 22:48:41 +0000262 PM.add(Printer);
263
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000264 return false; // success!
265}