blob: 753db852263683935cc0d14a8b6cf95f8c44cf0d [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
Tom Roeder44cb65f2014-06-05 19:29:43 +000014#include "llvm/Analysis/Passes.h"
Daniel Dunbar9abdc6c2009-08-13 23:48:47 +000015#include "llvm/CodeGen/AsmPrinter.h"
Chandler Carruth93dcdc42015-01-31 11:17:59 +000016#include "llvm/CodeGen/BasicTTIImpl.h"
Andrew Trickf8ea1082012-02-04 02:56:59 +000017#include "llvm/CodeGen/MachineModuleInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/CodeGen/Passes.h"
Matthias Braun31d19d42016-05-10 03:21:59 +000019#include "llvm/CodeGen/TargetPassConfig.h"
Chandler Carruthb8ddc702014-01-12 11:10:32 +000020#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000021#include "llvm/IR/LegacyPassManager.h"
Tom Roeder44cb65f2014-06-05 19:29:43 +000022#include "llvm/IR/Verifier.h"
Chris Lattner7b26fce2009-08-22 20:48:53 +000023#include "llvm/MC/MCAsmInfo.h"
Andrew Trickde401d32012-02-04 02:56:48 +000024#include "llvm/MC/MCContext.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000025#include "llvm/MC/MCInstrInfo.h"
Chris Lattnerb0d44c32010-02-02 23:37:42 +000026#include "llvm/MC/MCStreamer.h"
Evan Chengc5e6d2f2011-07-11 03:57:24 +000027#include "llvm/MC/MCSubtargetInfo.h"
Chris Lattnerbafc8372007-03-31 00:24:43 +000028#include "llvm/Support/CommandLine.h"
Andrew Trickde401d32012-02-04 02:56:48 +000029#include "llvm/Support/ErrorHandling.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Support/FormattedStream.h"
Evan Cheng2bb40352011-08-24 18:08:43 +000031#include "llvm/Support/TargetRegistry.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000033#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#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
Rafael Espindola227144c2013-05-13 01:16:13 +000038void LLVMTargetMachine::initAsmInfo() {
Daniel Sanders335487a2015-06-16 13:15:50 +000039 MRI = TheTarget.createMCRegInfo(getTargetTriple().str());
Eric Christopher72e23a22015-03-19 22:36:32 +000040 MII = TheTarget.createMCInstrInfo();
Eric Christopher12cf76f2015-03-19 22:36:37 +000041 // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
42 // to some backends having subtarget feature dependent module level
43 // code generation. This is similar to the hack in the AsmPrinter for
44 // module level assembly etc.
Daniel Sanders335487a2015-06-16 13:15:50 +000045 STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(),
Eric Christopher12cf76f2015-03-19 22:36:37 +000046 getTargetFeatureString());
Eric Christopher72e23a22015-03-19 22:36:32 +000047
Daniel Sanders335487a2015-06-16 13:15:50 +000048 MCAsmInfo *TmpAsmInfo =
49 TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
Torok Edwinbe5020e2011-09-30 13:07:47 +000050 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
51 // and if the old one gets included then MCAsmInfo will be NULL and
52 // we'll crash later.
53 // Provide the user with a useful error message about what's wrong.
Daniel Sanders753e1762014-02-13 14:44:26 +000054 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
Jim Grosbacha40f8c42011-10-25 20:30:48 +000055 "Make sure you include the correct TargetSelect.h"
56 "and that InitializeAllTargetMCs() is being invoked!");
Daniel Sanders753e1762014-02-13 14:44:26 +000057
Rafael Espindola48fa6ed2014-02-21 03:13:54 +000058 if (Options.DisableIntegratedAS)
Daniel Sanders753e1762014-02-13 14:44:26 +000059 TmpAsmInfo->setUseIntegratedAssembler(false);
60
Nirav Dave53a72f42016-07-11 12:42:14 +000061 TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
62
Saleem Abdulrasool1f62f572017-06-09 00:40:19 +000063 TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
David Blaikie70bd1fd2014-03-27 20:45:41 +000064
Rafael Espindolafd82f052016-05-29 01:57:20 +000065 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
66
Saleem Abdulrasoold2f705d2016-05-31 01:48:07 +000067 if (Options.ExceptionModel != ExceptionHandling::None)
68 TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
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,
Benjamin Kramer061f4a52017-01-13 14:39:03 +000076 StringRef FS, const 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) {
Rafael Espindolad86e8bb2016-06-30 18:25:11 +000080 this->RM = RM;
81 this->CMModel = CM;
82 this->OptLevel = OL;
Rafael Espindola227144c2013-05-13 01:16:13 +000083}
84
Chandler Carruth8b04c0d2015-02-01 13:20:00 +000085TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
Eric Christophera4e5d3c2015-09-16 23:38:13 +000086 return TargetIRAnalysis([this](const Function &F) {
Chandler Carruthc956ab662015-02-01 14:22:17 +000087 return TargetTransformInfo(BasicTTIImpl(this, F));
88 });
Chandler Carruth664e3542013-01-07 01:37:14 +000089}
90
Andrew Trickf8ea1082012-02-04 02:56:59 +000091/// addPassesToX helper drives creation and initialization of TargetPassConfig.
Alex Lorenz735c47e2015-06-15 20:30:22 +000092static MCContext *
93addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
Quentin Colombet15f6ffb2017-07-31 18:24:07 +000094 bool DisableVerify, bool &WillCompleteCodeGenPipeline,
95 raw_pwrite_stream &Out, MachineModuleInfo *MMI) {
Eric Christopher710c0ae2014-05-19 21:18:47 +000096 // Targets may override createPassConfig to provide a target-specific
97 // subclass.
Andrew Trickf8ea1082012-02-04 02:56:59 +000098 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
Andrew Trickf8ea1082012-02-04 02:56:59 +000099 // Set PassConfig options provided by TargetMachine.
100 PassConfig->setDisableVerify(DisableVerify);
Quentin Colombet15f6ffb2017-07-31 18:24:07 +0000101 WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline();
Andrew Trick34914912012-02-06 22:51:15 +0000102 PM.add(PassConfig);
Quentin Colombet15f6ffb2017-07-31 18:24:07 +0000103 if (!MMI)
104 MMI = new MachineModuleInfo(TM);
Matthias Braunc3b2e802016-08-24 00:42:05 +0000105 PM.add(MMI);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000106
Matthias Braunc7c06f12017-06-06 00:26:13 +0000107 if (PassConfig->addISelPasses())
Craig Topperc0196b12014-04-14 00:51:57 +0000108 return nullptr;
Andrew Trickf8ea1082012-02-04 02:56:59 +0000109 PassConfig->addMachinePasses();
Andrew Trickdd37d522012-02-08 21:22:39 +0000110 PassConfig->setInitialized();
Quentin Colombet15f6ffb2017-07-31 18:24:07 +0000111 if (!WillCompleteCodeGenPipeline)
112 PM.add(createPrintMIRPass(Out));
Andrew Trickdd37d522012-02-08 21:22:39 +0000113
Matthias Braunc3b2e802016-08-24 00:42:05 +0000114 return &MMI->getContext();
Andrew Trickf8ea1082012-02-04 02:56:59 +0000115}
116
Matthias Braunc7c06f12017-06-06 00:26:13 +0000117bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
118 raw_pwrite_stream &Out, CodeGenFileType FileType,
119 MCContext &Context) {
Eric Christopherc21d3d52014-05-16 00:32:52 +0000120 if (Options.MCOptions.MCSaveTempLabels)
Matthias Braunc7c06f12017-06-06 00:26:13 +0000121 Context.setAllowTemporaryLabels(false);
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000122
Eric Christopher12cf76f2015-03-19 22:36:37 +0000123 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
Eric Christopherd9134482014-08-04 21:25:23 +0000124 const MCAsmInfo &MAI = *getMCAsmInfo();
Eric Christopher72e23a22015-03-19 22:36:32 +0000125 const MCRegisterInfo &MRI = *getMCRegisterInfo();
126 const MCInstrInfo &MII = *getMCInstrInfo();
127
Ahmed Charles56440fd2014-03-06 05:51:42 +0000128 std::unique_ptr<MCStreamer> AsmStreamer;
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000129
Chris Lattnera916db12006-09-04 04:16:09 +0000130 switch (FileType) {
Chris Lattner249453f2010-02-03 00:29:55 +0000131 case CGFT_AssemblyFile: {
Eric Christopher72e23a22015-03-19 22:36:32 +0000132 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
Daniel Sanders50f17232015-09-15 16:17:27 +0000133 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000134
135 // Create a code emitter if asked to show the encoding.
Craig Topperc0196b12014-04-14 00:51:57 +0000136 MCCodeEmitter *MCE = nullptr;
Eric Christopher5d376062014-05-15 23:27:49 +0000137 if (Options.MCOptions.ShowMCEncoding)
Matthias Braunc7c06f12017-06-06 00:26:13 +0000138 MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
Daniel Dunbar62bc96a2010-05-18 17:22:19 +0000139
Daniel Sanders335487a2015-06-16 13:15:50 +0000140 MCAsmBackend *MAB =
Joel Jones373d7d32016-07-25 17:18:28 +0000141 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
142 Options.MCOptions);
Rafael Espindola5682ce22015-04-09 21:06:08 +0000143 auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
Eric Christopher5d376062014-05-15 23:27:49 +0000144 MCStreamer *S = getTarget().createAsmStreamer(
Matthias Braunc7c06f12017-06-06 00:26:13 +0000145 Context, std::move(FOut), Options.MCOptions.AsmVerbose,
Rafael Espindolaee0dd4d2015-04-09 15:54:59 +0000146 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
147 Options.MCOptions.ShowMCInst);
Rafael Espindolab58867c2010-11-19 02:26:16 +0000148 AsmStreamer.reset(S);
Chris Lattner03dc0f72010-02-02 19:14:27 +0000149 break;
Chris Lattner249453f2010-02-03 00:29:55 +0000150 }
Chris Lattner8856a672010-02-02 23:57:42 +0000151 case CGFT_ObjectFile: {
152 // Create the code emitter for the target if it exists. If not, .o file
153 // emission fails.
Matthias Braunc7c06f12017-06-06 00:26:13 +0000154 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
Daniel Sanders335487a2015-06-16 13:15:50 +0000155 MCAsmBackend *MAB =
Joel Jones373d7d32016-07-25 17:18:28 +0000156 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
157 Options.MCOptions);
Craig Topperc0196b12014-04-14 00:51:57 +0000158 if (!MCE || !MAB)
Chris Lattneredcf0652010-02-03 05:55:08 +0000159 return true;
Daniel Dunbarb33dfbc2010-05-26 21:48:55 +0000160
Duncan P. N. Exon Smithc177fec2015-05-06 21:34:34 +0000161 // Don't waste memory on names of temp labels.
Matthias Braunc7c06f12017-06-06 00:26:13 +0000162 Context.setUseNamesOnTempLabels(false);
Duncan P. N. Exon Smithc177fec2015-05-06 21:34:34 +0000163
Daniel Sanders50f17232015-09-15 16:17:27 +0000164 Triple T(getTargetTriple().str());
Rafael Espindolaf696df12015-03-16 22:29:29 +0000165 AsmStreamer.reset(getTarget().createMCObjectStreamer(
Matthias Braunc7c06f12017-06-06 00:26:13 +0000166 T, Context, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll,
David Majnemer03e2cc32015-12-21 22:09:27 +0000167 Options.MCOptions.MCIncrementalLinkerCompatible,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000168 /*DWARFMustBeAtTheEnd*/ true));
Chris Lattner8856a672010-02-02 23:57:42 +0000169 break;
Chris Lattner2fdf5b52010-02-02 18:44:12 +0000170 }
Chris Lattneredcf0652010-02-03 05:55:08 +0000171 case CGFT_Null:
172 // The Null output is intended for use for performance analysis and testing,
173 // not real users.
Matthias Braunc7c06f12017-06-06 00:26:13 +0000174 AsmStreamer.reset(getTarget().createNullStreamer(Context));
Chris Lattneredcf0652010-02-03 05:55:08 +0000175 break;
Chris Lattnera916db12006-09-04 04:16:09 +0000176 }
Daniel Dunbar3ff1a062010-05-23 17:44:06 +0000177
Chris Lattnere468f882010-03-13 20:55:24 +0000178 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
David Blaikie94598322015-01-18 20:29:04 +0000179 FunctionPass *Printer =
180 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
Craig Topperc0196b12014-04-14 00:51:57 +0000181 if (!Printer)
Chris Lattneredcf0652010-02-03 05:55:08 +0000182 return true;
Jim Grosbachd1f44652010-08-13 16:55:08 +0000183
Chris Lattnerc49f8c72010-02-02 23:45:17 +0000184 PM.add(Printer);
Matthias Braunc7c06f12017-06-06 00:26:13 +0000185 return false;
186}
Jim Grosbachd1f44652010-08-13 16:55:08 +0000187
Quentin Colombet15f6ffb2017-07-31 18:24:07 +0000188bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
189 raw_pwrite_stream &Out,
190 CodeGenFileType FileType,
191 bool DisableVerify,
192 MachineModuleInfo *MMI) {
Matthias Braunc7c06f12017-06-06 00:26:13 +0000193 // Add common CodeGen passes.
Quentin Colombet15f6ffb2017-07-31 18:24:07 +0000194 bool WillCompleteCodeGenPipeline = true;
195 MCContext *Context = addPassesToGenerateCode(
196 this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI);
Matthias Braunc7c06f12017-06-06 00:26:13 +0000197 if (!Context)
198 return true;
199
Quentin Colombet15f6ffb2017-07-31 18:24:07 +0000200 if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context))
201 return true;
Matthias Braunc7c06f12017-06-06 00:26:13 +0000202
203 PM.add(createFreeMachineFunctionPass());
Chris Lattneredcf0652010-02-03 05:55:08 +0000204 return false;
Dan Gohmanacb05542008-09-25 00:37:07 +0000205}
206
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000207/// addPassesToEmitMC - Add passes to the specified pass manager to get
208/// machine code emitted with the MCJIT. This method returns true if machine
209/// code is not supported. It fills the MCContext Ctx pointer which can be
210/// used to build custom MCStreamer.
211///
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000212bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
213 raw_pwrite_stream &Out,
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000214 bool DisableVerify) {
215 // Add common CodeGen passes.
Quentin Colombet15f6ffb2017-07-31 18:24:07 +0000216 bool WillCompleteCodeGenPipeline = true;
217 Ctx = addPassesToGenerateCode(this, PM, DisableVerify,
218 WillCompleteCodeGenPipeline, Out,
219 /*MachineModuleInfo*/ nullptr);
Andrew Trickf8ea1082012-02-04 02:56:59 +0000220 if (!Ctx)
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000221 return true;
Quentin Colombet15f6ffb2017-07-31 18:24:07 +0000222 assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered");
Jim Grosbach7b162492011-03-18 22:48:41 +0000223
Eric Christopherc21d3d52014-05-16 00:32:52 +0000224 if (Options.MCOptions.MCSaveTempLabels)
Daniel Dunbar3e2b3352011-03-28 22:49:19 +0000225 Ctx->setAllowTemporaryLabels(false);
226
Jim Grosbach7b162492011-03-18 22:48:41 +0000227 // Create the code emitter for the target if it exists. If not, .o file
228 // emission fails.
Eric Christopher72e23a22015-03-19 22:36:32 +0000229 const MCRegisterInfo &MRI = *getMCRegisterInfo();
230 MCCodeEmitter *MCE =
231 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
Daniel Sanders335487a2015-06-16 13:15:50 +0000232 MCAsmBackend *MAB =
Joel Jones373d7d32016-07-25 17:18:28 +0000233 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
234 Options.MCOptions);
Craig Topperc0196b12014-04-14 00:51:57 +0000235 if (!MCE || !MAB)
Jim Grosbach7b162492011-03-18 22:48:41 +0000236 return true;
237
Daniel Sanders50f17232015-09-15 16:17:27 +0000238 const Triple &T = getTargetTriple();
Eric Christopher12cf76f2015-03-19 22:36:37 +0000239 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
David Blaikie94598322015-01-18 20:29:04 +0000240 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
Daniel Sanders50f17232015-09-15 16:17:27 +0000241 T, *Ctx, *MAB, Out, MCE, STI, Options.MCOptions.MCRelaxAll,
David Majnemer03e2cc32015-12-21 22:09:27 +0000242 Options.MCOptions.MCIncrementalLinkerCompatible,
Rafael Espindola36a15cb2015-03-20 20:00:01 +0000243 /*DWARFMustBeAtTheEnd*/ true));
Jim Grosbach7b162492011-03-18 22:48:41 +0000244
245 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
David Blaikie94598322015-01-18 20:29:04 +0000246 FunctionPass *Printer =
247 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
Craig Topperc0196b12014-04-14 00:51:57 +0000248 if (!Printer)
Jim Grosbach7b162492011-03-18 22:48:41 +0000249 return true;
250
Jim Grosbach7b162492011-03-18 22:48:41 +0000251 PM.add(Printer);
Matthias Braun733fe362016-08-24 01:52:46 +0000252 PM.add(createFreeMachineFunctionPass());
Jim Grosbach7b162492011-03-18 22:48:41 +0000253
Reid Klecknerd85e3c52010-07-22 05:58:53 +0000254 return false; // success!
255}