blob: b7cac3ba381057b029d71965a1cc4a63e6ac5799 [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/TargetLoweringObjectFile.h"
34#include "llvm/Target/TargetOptions.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000035#include "llvm/Transforms/Scalar.h"
Chris Lattnera916db12006-09-04 04:16:09 +000036using namespace llvm;
37
Andrew Trickf8ea1082012-02-04 02:56:59 +000038// Enable or disable FastISel. Both options are needed, because
39// FastISel is enabled by default with -fast, and we wish to be
40// able to enable or disable fast-isel independently from -O0.
41static cl::opt<cl::boolOrDefault>
42EnableFastISelOption("fast-isel", cl::Hidden,
43 cl::desc("Enable the \"fast\" instruction selector"));
44
Rafael Espindola227144c2013-05-13 01:16:13 +000045void LLVMTargetMachine::initAsmInfo() {
Eric Christopher72e23a22015-03-19 22:36:32 +000046 MRI = TheTarget.createMCRegInfo(getTargetTriple());
47 MII = TheTarget.createMCInstrInfo();
Eric Christopher12cf76f2015-03-19 22:36:37 +000048 // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
49 // to some backends having subtarget feature dependent module level
50 // code generation. This is similar to the hack in the AsmPrinter for
51 // module level assembly etc.
52 STI = TheTarget.createMCSubtargetInfo(getTargetTriple(), getTargetCPU(),
53 getTargetFeatureString());
Eric Christopher72e23a22015-03-19 22:36:32 +000054
55 MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo(*MRI, getTargetTriple());
Torok Edwinbe5020e2011-09-30 13:07:47 +000056 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
57 // and if the old one gets included then MCAsmInfo will be NULL and
58 // we'll crash later.
59 // Provide the user with a useful error message about what's wrong.
Daniel Sanders753e1762014-02-13 14:44:26 +000060 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
Jim Grosbacha40f8c42011-10-25 20:30:48 +000061 "Make sure you include the correct TargetSelect.h"
62 "and that InitializeAllTargetMCs() is being invoked!");
Daniel Sanders753e1762014-02-13 14:44:26 +000063
Rafael Espindola48fa6ed2014-02-21 03:13:54 +000064 if (Options.DisableIntegratedAS)
Daniel Sanders753e1762014-02-13 14:44:26 +000065 TmpAsmInfo->setUseIntegratedAssembler(false);
66
David Blaikie70bd1fd2014-03-27 20:45:41 +000067 if (Options.CompressDebugSections)
68 TmpAsmInfo->setCompressDebugSections(true);
69
Daniel Sanders753e1762014-02-13 14:44:26 +000070 AsmInfo = TmpAsmInfo;
Chris Lattner9a6cf912009-08-12 07:22:17 +000071}
72
Mehdi Amini93e1ea12015-03-12 00:07:24 +000073LLVMTargetMachine::LLVMTargetMachine(const Target &T,
74 StringRef DataLayoutString,
Daniel Sanders3e5de882015-06-11 19:41:26 +000075 const Triple &TT, StringRef CPU,
Mehdi Amini93e1ea12015-03-12 00:07:24 +000076 StringRef FS, TargetOptions Options,
Rafael Espindola227144c2013-05-13 01:16:13 +000077 Reloc::Model RM, CodeModel::Model CM,
78 CodeGenOpt::Level OL)
Daniel Sanders3e5de882015-06-11 19:41:26 +000079 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
80 CodeGenInfo = T.createMCCodeGenInfo(TT.str(), RM, CM, OL);
Rafael Espindola227144c2013-05-13 01:16:13 +000081}
82
Chandler Carruth8b04c0d2015-02-01 13:20:00 +000083TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
Chandler Carruthc956ab662015-02-01 14:22:17 +000084 return TargetIRAnalysis([this](Function &F) {
85 return TargetTransformInfo(BasicTTIImpl(this, F));
86 });
Chandler Carruth664e3542013-01-07 01:37:14 +000087}
88
Andrew Trickf8ea1082012-02-04 02:56:59 +000089/// addPassesToX helper drives creation and initialization of TargetPassConfig.
Alex Lorenz735c47e2015-06-15 20:30:22 +000090static MCContext *
91addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
92 bool DisableVerify, AnalysisID StartAfter,
93 AnalysisID StopAfter,
94 MachineFunctionInitializer *MFInitializer = nullptr) {
Tom Roeder44cb65f2014-06-05 19:29:43 +000095
Juergen Ributzka5fe955c2014-01-23 19:23:28 +000096 // Add internal analysis passes from the target machine.
Chandler Carruth5ec2b1d2015-02-01 12:26:09 +000097 PM.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
Juergen Ributzka5fe955c2014-01-23 19:23:28 +000098
Eric Christopher710c0ae2014-05-19 21:18:47 +000099 // Targets may override createPassConfig to provide a target-specific
100 // subclass.
Andrew Trickf8ea1082012-02-04 02:56:59 +0000101 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Bob Wilsoncac3b902012-07-02 19:48:45 +0000102 PassConfig->setStartStopPasses(StartAfter, StopAfter);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000103
104 // Set PassConfig options provided by TargetMachine.
105 PassConfig->setDisableVerify(DisableVerify);
106
Andrew Trick34914912012-02-06 22:51:15 +0000107 PM.add(PassConfig);
108
Andrew Trickf8ea1082012-02-04 02:56:59 +0000109 PassConfig->addIRPasses();
110
Bill Wendlingc786b312012-11-30 22:08:55 +0000111 PassConfig->addCodeGenPrepare();
112
Bob Wilsonbbd38dd2012-07-02 19:48:31 +0000113 PassConfig->addPassesToHandleExceptions();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000114
115 PassConfig->addISelPrepare();
116
117 // Install a MachineModuleInfo class, which is an immutable pass that holds
118 // all the per-module stuff we're generating, including MCContext.
Eric Christopherd9134482014-08-04 21:25:23 +0000119 MachineModuleInfo *MMI = new MachineModuleInfo(
Eric Christopher72e23a22015-03-19 22:36:32 +0000120 *TM->getMCAsmInfo(), *TM->getMCRegisterInfo(), TM->getObjFileLowering());
Andrew Trickf8ea1082012-02-04 02:56:59 +0000121 PM.add(MMI);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000122
123 // Set up a MachineFunction for the rest of CodeGen to work on.
Alex Lorenz735c47e2015-06-15 20:30:22 +0000124 PM.add(new MachineFunctionAnalysis(*TM, MFInitializer));
Andrew Trickf8ea1082012-02-04 02:56:59 +0000125
126 // Enable FastISel with -fast, but allow that to be overridden.
127 if (EnableFastISelOption == cl::BOU_TRUE ||
128 (TM->getOptLevel() == CodeGenOpt::None &&
129 EnableFastISelOption != cl::BOU_FALSE))
130 TM->setFastISel(true);
131
132 // Ask the target for an isel.
133 if (PassConfig->addInstSelector())
Craig Topperc0196b12014-04-14 00:51:57 +0000134 return nullptr;
Andrew Trickf8ea1082012-02-04 02:56:59 +0000135
136 PassConfig->addMachinePasses();
137
Andrew Trickdd37d522012-02-08 21:22:39 +0000138 PassConfig->setInitialized();
139
Bill Wendlingafc10362013-06-19 20:51:24 +0000140 return &MMI->getContext();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000141}
142
Rafael Espindola5682ce22015-04-09 21:06:08 +0000143bool LLVMTargetMachine::addPassesToEmitFile(
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000144 PassManagerBase &PM, raw_pwrite_stream &Out, CodeGenFileType FileType,
Alex Lorenz735c47e2015-06-15 20:30:22 +0000145 bool DisableVerify, AnalysisID StartAfter, AnalysisID StopAfter,
146 MachineFunctionInitializer *MFInitializer) {
Dan Gohmanacb05542008-09-25 00:37:07 +0000147 // Add common CodeGen passes.
Alex Lorenz735c47e2015-06-15 20:30:22 +0000148 MCContext *Context = addPassesToGenerateCode(
149 this, PM, DisableVerify, StartAfter, StopAfter, MFInitializer);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000150 if (!Context)
Chris Lattneredcf0652010-02-03 05:55:08 +0000151 return true;
Bill Wendling523048e2007-02-08 01:36:53 +0000152
Bob Wilsoncac3b902012-07-02 19:48:45 +0000153 if (StopAfter) {
Alex Lorenz2bdb4e12015-05-27 18:02:19 +0000154 PM.add(createPrintMIRPass(outs()));
Bob Wilsoncac3b902012-07-02 19:48:45 +0000155 return false;
156 }
157
Eric Christopherc21d3d52014-05-16 00:32:52 +0000158 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000159 Context->setAllowTemporaryLabels(false);
160
Eric Christopher12cf76f2015-03-19 22:36:37 +0000161 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000162 const MCAsmInfo &MAI = *getMCAsmInfo();
Eric Christopher72e23a22015-03-19 22:36:32 +0000163 const MCRegisterInfo &MRI = *getMCRegisterInfo();
164 const MCInstrInfo &MII = *getMCInstrInfo();
165
Ahmed Charles56440fd2014-03-06 05:51:42 +0000166 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000167
Chris Lattnera916db12006-09-04 04:16:09 +0000168 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000169 case CGFT_AssemblyFile: {
Eric Christopher72e23a22015-03-19 22:36:32 +0000170 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
Eric Christopherf8019402015-03-31 00:10:04 +0000171 Triple(getTargetTriple()), MAI.getAssemblerDialect(), MAI, MII, MRI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000172
173 // Create a code emitter if asked to show the encoding.
Craig Topperc0196b12014-04-14 00:51:57 +0000174 MCCodeEmitter *MCE = nullptr;
Eric Christopher5d376062014-05-15 23:27:49 +0000175 if (Options.MCOptions.ShowMCEncoding)
Eric Christopher0169e422015-03-10 22:03:14 +0000176 MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000177
Bill Wendling550c76d2013-09-09 19:48:37 +0000178 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
179 TargetCPU);
Rafael Espindola5682ce22015-04-09 21:06:08 +0000180 auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
Eric Christopher5d376062014-05-15 23:27:49 +0000181 MCStreamer *S = getTarget().createAsmStreamer(
Rafael Espindola5682ce22015-04-09 21:06:08 +0000182 *Context, std::move(FOut), Options.MCOptions.AsmVerbose,
Rafael Espindolaee0dd4d2015-04-09 15:54:59 +0000183 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
184 Options.MCOptions.ShowMCInst);
Rafael Espindolab58867c2010-11-19 02:26:16 +0000185 AsmStreamer.reset(S);
Chris Lattner03dc0f72010-02-02 19:14:27 +0000186 break;
Chris Lattner249453f2010-02-03 00:29:55 +0000187 }
Chris Lattner8856a672010-02-02 23:57:42 +0000188 case CGFT_ObjectFile: {
189 // Create the code emitter for the target if it exists. If not, .o file
190 // emission fails.
Eric Christopher0169e422015-03-10 22:03:14 +0000191 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, *Context);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000192 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
Eli Bendersky26e7efe2012-11-22 14:10:40 +0000193 TargetCPU);
Craig Topperc0196b12014-04-14 00:51:57 +0000194 if (!MCE || !MAB)
Chris Lattneredcf0652010-02-03 05:55:08 +0000195 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000196
Duncan P. N. Exon Smithc177fec2015-05-06 21:34:34 +0000197 // Don't waste memory on names of temp labels.
198 Context->setUseNamesOnTempLabels(false);
199
Rafael Espindolaf696df12015-03-16 22:29:29 +0000200 Triple T(getTargetTriple());
201 AsmStreamer.reset(getTarget().createMCObjectStreamer(
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000202 T, *Context, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll,
203 /*DWARFMustBeAtTheEnd*/ true));
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///
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000229bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
230 raw_pwrite_stream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000231 bool DisableVerify) {
232 // Add common CodeGen passes.
Craig Topperc0196b12014-04-14 00:51:57 +0000233 Ctx = addPassesToGenerateCode(this, PM, DisableVerify, nullptr, nullptr);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000234 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000235 return true;
Jim Grosbach7b162492011-03-18 22:48:41 +0000236
Eric Christopherc21d3d52014-05-16 00:32:52 +0000237 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000238 Ctx->setAllowTemporaryLabels(false);
239
Jim Grosbach7b162492011-03-18 22:48:41 +0000240 // Create the code emitter for the target if it exists. If not, .o file
241 // emission fails.
Eric Christopher72e23a22015-03-19 22:36:32 +0000242 const MCRegisterInfo &MRI = *getMCRegisterInfo();
243 MCCodeEmitter *MCE =
244 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000245 MCAsmBackend *MAB = getTarget().createMCAsmBackend(MRI, getTargetTriple(),
246 TargetCPU);
Craig Topperc0196b12014-04-14 00:51:57 +0000247 if (!MCE || !MAB)
Jim Grosbach7b162492011-03-18 22:48:41 +0000248 return true;
249
Rafael Espindolaf696df12015-03-16 22:29:29 +0000250 Triple T(getTargetTriple());
Eric Christopher12cf76f2015-03-19 22:36:37 +0000251 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
David Blaikie94598322015-01-18 20:29:04 +0000252 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000253 T, *Ctx, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll,
254 /*DWARFMustBeAtTheEnd*/ true));
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}