blob: cd70792146d2b1b5fbc73ac25e6d90775817ba30 [file] [log] [blame]
Matthias Braunbb8507e2017-10-12 22:57:28 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Matthias Braunbb8507e2017-10-12 22:57:28 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the LLVMTargetMachine class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/Analysis/Passes.h"
14#include "llvm/CodeGen/AsmPrinter.h"
15#include "llvm/CodeGen/BasicTTIImpl.h"
16#include "llvm/CodeGen/MachineModuleInfo.h"
17#include "llvm/CodeGen/Passes.h"
18#include "llvm/CodeGen/TargetPassConfig.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000019#include "llvm/IR/LegacyPassManager.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000020#include "llvm/MC/MCAsmBackend.h"
21#include "llvm/MC/MCAsmInfo.h"
22#include "llvm/MC/MCCodeEmitter.h"
23#include "llvm/MC/MCContext.h"
24#include "llvm/MC/MCInstrInfo.h"
Peter Collingbournef7b81db2018-05-18 18:26:45 +000025#include "llvm/MC/MCObjectWriter.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000026#include "llvm/MC/MCStreamer.h"
27#include "llvm/MC/MCSubtargetInfo.h"
28#include "llvm/Support/CommandLine.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/FormattedStream.h"
31#include "llvm/Support/TargetRegistry.h"
David Blaikie6054e652018-03-23 23:58:19 +000032#include "llvm/Target/TargetLoweringObjectFile.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000033#include "llvm/Target/TargetMachine.h"
34#include "llvm/Target/TargetOptions.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000035using namespace llvm;
36
David Green6d9f8c92018-02-12 11:06:27 +000037static cl::opt<bool> EnableTrapUnreachable("trap-unreachable",
38 cl::Hidden, cl::ZeroOrMore, cl::init(false),
39 cl::desc("Enable generating trap for unreachable"));
40
Matthias Braunbb8507e2017-10-12 22:57:28 +000041void LLVMTargetMachine::initAsmInfo() {
Fangrui Song10a21622018-09-25 06:19:31 +000042 MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
43 MII.reset(TheTarget.createMCInstrInfo());
Matthias Braunbb8507e2017-10-12 22:57:28 +000044 // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
45 // to some backends having subtarget feature dependent module level
46 // code generation. This is similar to the hack in the AsmPrinter for
47 // module level assembly etc.
Fangrui Song10a21622018-09-25 06:19:31 +000048 STI.reset(TheTarget.createMCSubtargetInfo(
49 getTargetTriple().str(), getTargetCPU(), getTargetFeatureString()));
Matthias Braunbb8507e2017-10-12 22:57:28 +000050
51 MCAsmInfo *TmpAsmInfo =
52 TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
53 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
54 // and if the old one gets included then MCAsmInfo will be NULL and
55 // we'll crash later.
56 // Provide the user with a useful error message about what's wrong.
57 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
58 "Make sure you include the correct TargetSelect.h"
59 "and that InitializeAllTargetMCs() is being invoked!");
60
61 if (Options.DisableIntegratedAS)
62 TmpAsmInfo->setUseIntegratedAssembler(false);
63
64 TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
65
66 TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
67
68 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
69
70 if (Options.ExceptionModel != ExceptionHandling::None)
71 TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
72
Fangrui Song10a21622018-09-25 06:19:31 +000073 AsmInfo.reset(TmpAsmInfo);
Matthias Braunbb8507e2017-10-12 22:57:28 +000074}
75
76LLVMTargetMachine::LLVMTargetMachine(const Target &T,
77 StringRef DataLayoutString,
78 const Triple &TT, StringRef CPU,
79 StringRef FS, const TargetOptions &Options,
80 Reloc::Model RM, CodeModel::Model CM,
81 CodeGenOpt::Level OL)
82 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
83 this->RM = RM;
84 this->CMModel = CM;
85 this->OptLevel = OL;
David Green6d9f8c92018-02-12 11:06:27 +000086
87 if (EnableTrapUnreachable)
88 this->Options.TrapUnreachable = true;
Matthias Braunbb8507e2017-10-12 22:57:28 +000089}
90
Sanjoy Das26d11ca2017-12-22 18:21:59 +000091TargetTransformInfo
92LLVMTargetMachine::getTargetTransformInfo(const Function &F) {
93 return TargetTransformInfo(BasicTTIImpl(this, F));
Matthias Braunbb8507e2017-10-12 22:57:28 +000094}
95
96/// addPassesToX helper drives creation and initialization of TargetPassConfig.
Matthias Braune8f717a2018-11-02 01:31:50 +000097static TargetPassConfig *
98addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM,
99 bool DisableVerify, MachineModuleInfo &MMI) {
Matthias Braunbb8507e2017-10-12 22:57:28 +0000100 // Targets may override createPassConfig to provide a target-specific
101 // subclass.
Matthias Braune8f717a2018-11-02 01:31:50 +0000102 TargetPassConfig *PassConfig = TM.createPassConfig(PM);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000103 // Set PassConfig options provided by TargetMachine.
104 PassConfig->setDisableVerify(DisableVerify);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000105 PM.add(PassConfig);
Matthias Braune8f717a2018-11-02 01:31:50 +0000106 PM.add(&MMI);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000107
108 if (PassConfig->addISelPasses())
109 return nullptr;
110 PassConfig->addMachinePasses();
111 PassConfig->setInitialized();
Matthias Braune8f717a2018-11-02 01:31:50 +0000112 return PassConfig;
Matthias Braunbb8507e2017-10-12 22:57:28 +0000113}
114
115bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
Peter Collingbourne9a451142018-05-21 20:16:41 +0000116 raw_pwrite_stream &Out,
117 raw_pwrite_stream *DwoOut,
118 CodeGenFileType FileType,
119 MCContext &Context) {
Matthias Braunbb8507e2017-10-12 22:57:28 +0000120 if (Options.MCOptions.MCSaveTempLabels)
121 Context.setAllowTemporaryLabels(false);
122
123 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
124 const MCAsmInfo &MAI = *getMCAsmInfo();
125 const MCRegisterInfo &MRI = *getMCRegisterInfo();
126 const MCInstrInfo &MII = *getMCInstrInfo();
127
128 std::unique_ptr<MCStreamer> AsmStreamer;
129
130 switch (FileType) {
131 case CGFT_AssemblyFile: {
132 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
133 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
134
135 // Create a code emitter if asked to show the encoding.
Nirav Dave1b5533c2018-04-27 15:45:54 +0000136 std::unique_ptr<MCCodeEmitter> MCE;
Matthias Braunbb8507e2017-10-12 22:57:28 +0000137 if (Options.MCOptions.ShowMCEncoding)
Nirav Dave1b5533c2018-04-27 15:45:54 +0000138 MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context));
Matthias Braunbb8507e2017-10-12 22:57:28 +0000139
Nirav Dave1b5533c2018-04-27 15:45:54 +0000140 std::unique_ptr<MCAsmBackend> MAB(
141 getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions));
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000142 auto FOut = std::make_unique<formatted_raw_ostream>(Out);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000143 MCStreamer *S = getTarget().createAsmStreamer(
144 Context, std::move(FOut), Options.MCOptions.AsmVerbose,
Nirav Dave1b5533c2018-04-27 15:45:54 +0000145 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE),
146 std::move(MAB), Options.MCOptions.ShowMCInst);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000147 AsmStreamer.reset(S);
148 break;
149 }
150 case CGFT_ObjectFile: {
151 // Create the code emitter for the target if it exists. If not, .o file
152 // emission fails.
153 MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
154 MCAsmBackend *MAB =
Alex Bradburyb22f7512018-01-03 08:53:05 +0000155 getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000156 if (!MCE || !MAB)
157 return true;
158
159 // Don't waste memory on names of temp labels.
160 Context.setUseNamesOnTempLabels(false);
161
162 Triple T(getTargetTriple().str());
163 AsmStreamer.reset(getTarget().createMCObjectStreamer(
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000164 T, Context, std::unique_ptr<MCAsmBackend>(MAB),
Peter Collingbourne9a451142018-05-21 20:16:41 +0000165 DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut)
166 : MAB->createObjectWriter(Out),
167 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
Matthias Braunbb8507e2017-10-12 22:57:28 +0000168 Options.MCOptions.MCIncrementalLinkerCompatible,
169 /*DWARFMustBeAtTheEnd*/ true));
170 break;
171 }
172 case CGFT_Null:
173 // The Null output is intended for use for performance analysis and testing,
174 // not real users.
175 AsmStreamer.reset(getTarget().createNullStreamer(Context));
176 break;
177 }
178
179 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
180 FunctionPass *Printer =
181 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
182 if (!Printer)
183 return true;
184
185 PM.add(Printer);
186 return false;
187}
188
189bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
190 raw_pwrite_stream &Out,
Peter Collingbourne9a451142018-05-21 20:16:41 +0000191 raw_pwrite_stream *DwoOut,
Matthias Braunbb8507e2017-10-12 22:57:28 +0000192 CodeGenFileType FileType,
193 bool DisableVerify,
194 MachineModuleInfo *MMI) {
195 // Add common CodeGen passes.
Matthias Braune8f717a2018-11-02 01:31:50 +0000196 if (!MMI)
197 MMI = new MachineModuleInfo(this);
198 TargetPassConfig *PassConfig =
199 addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
200 if (!PassConfig)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000201 return true;
202
Matthias Braune8f717a2018-11-02 01:31:50 +0000203 if (!TargetPassConfig::willCompleteCodeGenPipeline()) {
Jason Liu60ec2482019-06-06 19:13:36 +0000204 if (this->getTargetTriple().isOSAIX()) {
205 // On AIX, we might manifest MCSymbols during SDAG lowering. For MIR
206 // testing to be meaningful, we need to ensure that the symbols created
207 // are MCSymbolXCOFF variants, which requires that
208 // the TargetLoweringObjectFile instance has been initialized.
209 MCContext &Ctx = MMI->getContext();
210 const_cast<TargetLoweringObjectFile &>(*this->getObjFileLowering())
211 .Initialize(Ctx, *this);
212 }
Matthias Braune8f717a2018-11-02 01:31:50 +0000213 PM.add(createPrintMIRPass(Out));
214 } else if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext()))
Matthias Braunbb8507e2017-10-12 22:57:28 +0000215 return true;
216
217 PM.add(createFreeMachineFunctionPass());
218 return false;
219}
220
221/// addPassesToEmitMC - Add passes to the specified pass manager to get
222/// machine code emitted with the MCJIT. This method returns true if machine
223/// code is not supported. It fills the MCContext Ctx pointer which can be
224/// used to build custom MCStreamer.
225///
226bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
227 raw_pwrite_stream &Out,
228 bool DisableVerify) {
229 // Add common CodeGen passes.
Matthias Braune8f717a2018-11-02 01:31:50 +0000230 MachineModuleInfo *MMI = new MachineModuleInfo(this);
231 TargetPassConfig *PassConfig =
232 addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
233 if (!PassConfig)
Matthias Braunbb8507e2017-10-12 22:57:28 +0000234 return true;
Matthias Braune8f717a2018-11-02 01:31:50 +0000235 assert(TargetPassConfig::willCompleteCodeGenPipeline() &&
236 "Cannot emit MC with limited codegen pipeline");
Matthias Braunbb8507e2017-10-12 22:57:28 +0000237
Matthias Braune8f717a2018-11-02 01:31:50 +0000238 Ctx = &MMI->getContext();
Matthias Braunbb8507e2017-10-12 22:57:28 +0000239 if (Options.MCOptions.MCSaveTempLabels)
240 Ctx->setAllowTemporaryLabels(false);
241
242 // Create the code emitter for the target if it exists. If not, .o file
243 // emission fails.
Alex Bradburyb22f7512018-01-03 08:53:05 +0000244 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
Matthias Braunbb8507e2017-10-12 22:57:28 +0000245 const MCRegisterInfo &MRI = *getMCRegisterInfo();
246 MCCodeEmitter *MCE =
247 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
248 MCAsmBackend *MAB =
Alex Bradburyb22f7512018-01-03 08:53:05 +0000249 getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
Matthias Braunbb8507e2017-10-12 22:57:28 +0000250 if (!MCE || !MAB)
251 return true;
252
253 const Triple &T = getTargetTriple();
Matthias Braunbb8507e2017-10-12 22:57:28 +0000254 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
Peter Collingbournef7b81db2018-05-18 18:26:45 +0000255 T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
Matthias Braunbb8507e2017-10-12 22:57:28 +0000256 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
257 Options.MCOptions.MCIncrementalLinkerCompatible,
258 /*DWARFMustBeAtTheEnd*/ true));
259
260 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
261 FunctionPass *Printer =
262 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
263 if (!Printer)
264 return true;
265
266 PM.add(Printer);
267 PM.add(createFreeMachineFunctionPass());
268
269 return false; // success!
270}