blob: 92edfb059ad6511fe280472e6d645808ddf6a062 [file] [log] [blame]
Matthias Braunbb8507e2017-10-12 22:57:28 +00001//===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LLVMTargetMachine class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/Passes.h"
15#include "llvm/CodeGen/AsmPrinter.h"
16#include "llvm/CodeGen/BasicTTIImpl.h"
17#include "llvm/CodeGen/MachineModuleInfo.h"
18#include "llvm/CodeGen/Passes.h"
David Blaikieb3bde2e2017-11-17 01:07:10 +000019#include "llvm/CodeGen/TargetLoweringObjectFile.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000020#include "llvm/CodeGen/TargetPassConfig.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000021#include "llvm/IR/LegacyPassManager.h"
Matthias Braunbb8507e2017-10-12 22:57:28 +000022#include "llvm/MC/MCAsmBackend.h"
23#include "llvm/MC/MCAsmInfo.h"
24#include "llvm/MC/MCCodeEmitter.h"
25#include "llvm/MC/MCContext.h"
26#include "llvm/MC/MCInstrInfo.h"
27#include "llvm/MC/MCStreamer.h"
28#include "llvm/MC/MCSubtargetInfo.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/FormattedStream.h"
32#include "llvm/Support/TargetRegistry.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
37void LLVMTargetMachine::initAsmInfo() {
38 MRI = TheTarget.createMCRegInfo(getTargetTriple().str());
39 MII = TheTarget.createMCInstrInfo();
40 // FIXME: Having an MCSubtargetInfo on the target machine is a hack due
41 // to some backends having subtarget feature dependent module level
42 // code generation. This is similar to the hack in the AsmPrinter for
43 // module level assembly etc.
44 STI = TheTarget.createMCSubtargetInfo(getTargetTriple().str(), getTargetCPU(),
45 getTargetFeatureString());
46
47 MCAsmInfo *TmpAsmInfo =
48 TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str());
49 // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0,
50 // and if the old one gets included then MCAsmInfo will be NULL and
51 // we'll crash later.
52 // Provide the user with a useful error message about what's wrong.
53 assert(TmpAsmInfo && "MCAsmInfo not initialized. "
54 "Make sure you include the correct TargetSelect.h"
55 "and that InitializeAllTargetMCs() is being invoked!");
56
57 if (Options.DisableIntegratedAS)
58 TmpAsmInfo->setUseIntegratedAssembler(false);
59
60 TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
61
62 TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
63
64 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
65
66 if (Options.ExceptionModel != ExceptionHandling::None)
67 TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
68
69 AsmInfo = TmpAsmInfo;
70}
71
72LLVMTargetMachine::LLVMTargetMachine(const Target &T,
73 StringRef DataLayoutString,
74 const Triple &TT, StringRef CPU,
75 StringRef FS, const TargetOptions &Options,
76 Reloc::Model RM, CodeModel::Model CM,
77 CodeGenOpt::Level OL)
78 : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) {
79 this->RM = RM;
80 this->CMModel = CM;
81 this->OptLevel = OL;
82}
83
84TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
85 return TargetIRAnalysis([this](const Function &F) {
86 return TargetTransformInfo(BasicTTIImpl(this, F));
87 });
88}
89
90/// addPassesToX helper drives creation and initialization of TargetPassConfig.
91static MCContext *
92addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
93 bool DisableVerify, bool &WillCompleteCodeGenPipeline,
94 raw_pwrite_stream &Out, MachineModuleInfo *MMI) {
95 // Targets may override createPassConfig to provide a target-specific
96 // subclass.
97 TargetPassConfig *PassConfig = TM->createPassConfig(PM);
98 // Set PassConfig options provided by TargetMachine.
99 PassConfig->setDisableVerify(DisableVerify);
100 WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline();
101 PM.add(PassConfig);
102 if (!MMI)
103 MMI = new MachineModuleInfo(TM);
104 PM.add(MMI);
105
106 if (PassConfig->addISelPasses())
107 return nullptr;
108 PassConfig->addMachinePasses();
109 PassConfig->setInitialized();
110 if (!WillCompleteCodeGenPipeline)
111 PM.add(createPrintMIRPass(Out));
112
113 return &MMI->getContext();
114}
115
116bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
117 raw_pwrite_stream &Out, CodeGenFileType FileType,
118 MCContext &Context) {
119 if (Options.MCOptions.MCSaveTempLabels)
120 Context.setAllowTemporaryLabels(false);
121
122 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
123 const MCAsmInfo &MAI = *getMCAsmInfo();
124 const MCRegisterInfo &MRI = *getMCRegisterInfo();
125 const MCInstrInfo &MII = *getMCInstrInfo();
126
127 std::unique_ptr<MCStreamer> AsmStreamer;
128
129 switch (FileType) {
130 case CGFT_AssemblyFile: {
131 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
132 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
133
134 // Create a code emitter if asked to show the encoding.
135 MCCodeEmitter *MCE = nullptr;
136 if (Options.MCOptions.ShowMCEncoding)
137 MCE = getTarget().createMCCodeEmitter(MII, MRI, Context);
138
139 MCAsmBackend *MAB =
140 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
141 Options.MCOptions);
142 auto FOut = llvm::make_unique<formatted_raw_ostream>(Out);
143 MCStreamer *S = getTarget().createAsmStreamer(
144 Context, std::move(FOut), Options.MCOptions.AsmVerbose,
145 Options.MCOptions.MCUseDwarfDirectory, InstPrinter, MCE, MAB,
146 Options.MCOptions.ShowMCInst);
147 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 =
155 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
156 Options.MCOptions);
157 if (!MCE || !MAB)
158 return true;
159
160 // Don't waste memory on names of temp labels.
161 Context.setUseNamesOnTempLabels(false);
162
163 Triple T(getTargetTriple().str());
164 AsmStreamer.reset(getTarget().createMCObjectStreamer(
165 T, Context, std::unique_ptr<MCAsmBackend>(MAB), Out,
166 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
167 Options.MCOptions.MCIncrementalLinkerCompatible,
168 /*DWARFMustBeAtTheEnd*/ true));
169 break;
170 }
171 case CGFT_Null:
172 // The Null output is intended for use for performance analysis and testing,
173 // not real users.
174 AsmStreamer.reset(getTarget().createNullStreamer(Context));
175 break;
176 }
177
178 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
179 FunctionPass *Printer =
180 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
181 if (!Printer)
182 return true;
183
184 PM.add(Printer);
185 return false;
186}
187
188bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
189 raw_pwrite_stream &Out,
190 CodeGenFileType FileType,
191 bool DisableVerify,
192 MachineModuleInfo *MMI) {
193 // Add common CodeGen passes.
194 bool WillCompleteCodeGenPipeline = true;
195 MCContext *Context = addPassesToGenerateCode(
196 this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI);
197 if (!Context)
198 return true;
199
200 if (WillCompleteCodeGenPipeline && addAsmPrinter(PM, Out, FileType, *Context))
201 return true;
202
203 PM.add(createFreeMachineFunctionPass());
204 return false;
205}
206
207/// 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///
212bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
213 raw_pwrite_stream &Out,
214 bool DisableVerify) {
215 // Add common CodeGen passes.
216 bool WillCompleteCodeGenPipeline = true;
217 Ctx = addPassesToGenerateCode(this, PM, DisableVerify,
218 WillCompleteCodeGenPipeline, Out,
219 /*MachineModuleInfo*/ nullptr);
220 if (!Ctx)
221 return true;
222 assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered");
223
224 if (Options.MCOptions.MCSaveTempLabels)
225 Ctx->setAllowTemporaryLabels(false);
226
227 // Create the code emitter for the target if it exists. If not, .o file
228 // emission fails.
229 const MCRegisterInfo &MRI = *getMCRegisterInfo();
230 MCCodeEmitter *MCE =
231 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
232 MCAsmBackend *MAB =
233 getTarget().createMCAsmBackend(MRI, getTargetTriple().str(), TargetCPU,
234 Options.MCOptions);
235 if (!MCE || !MAB)
236 return true;
237
238 const Triple &T = getTargetTriple();
239 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
240 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
241 T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), Out,
242 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
243 Options.MCOptions.MCIncrementalLinkerCompatible,
244 /*DWARFMustBeAtTheEnd*/ true));
245
246 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
247 FunctionPass *Printer =
248 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
249 if (!Printer)
250 return true;
251
252 PM.add(Printer);
253 PM.add(createFreeMachineFunctionPass());
254
255 return false; // success!
256}