Revert r354244 "[DAGCombiner] Eliminate dead stores to stack."
[llvm-complete.git] / lib / CodeGen / LLVMTargetMachine.cpp
blob1d10a6c9afd583bebc82dc1d7292e8b6277836f8
1 //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the LLVMTargetMachine class.
11 //===----------------------------------------------------------------------===//
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"
19 #include "llvm/IR/LegacyPassManager.h"
20 #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"
25 #include "llvm/MC/MCObjectWriter.h"
26 #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"
32 #include "llvm/Target/TargetLoweringObjectFile.h"
33 #include "llvm/Target/TargetMachine.h"
34 #include "llvm/Target/TargetOptions.h"
35 using namespace llvm;
37 static cl::opt<bool> EnableTrapUnreachable("trap-unreachable",
38 cl::Hidden, cl::ZeroOrMore, cl::init(false),
39 cl::desc("Enable generating trap for unreachable"));
41 void LLVMTargetMachine::initAsmInfo() {
42 MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str()));
43 MII.reset(TheTarget.createMCInstrInfo());
44 // 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.
48 STI.reset(TheTarget.createMCSubtargetInfo(
49 getTargetTriple().str(), getTargetCPU(), getTargetFeatureString()));
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!");
61 if (Options.DisableIntegratedAS)
62 TmpAsmInfo->setUseIntegratedAssembler(false);
64 TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments);
66 TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections);
68 TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations);
70 if (Options.ExceptionModel != ExceptionHandling::None)
71 TmpAsmInfo->setExceptionsType(Options.ExceptionModel);
73 AsmInfo.reset(TmpAsmInfo);
76 LLVMTargetMachine::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;
87 if (EnableTrapUnreachable)
88 this->Options.TrapUnreachable = true;
91 TargetTransformInfo
92 LLVMTargetMachine::getTargetTransformInfo(const Function &F) {
93 return TargetTransformInfo(BasicTTIImpl(this, F));
96 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
97 static TargetPassConfig *
98 addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM,
99 bool DisableVerify, MachineModuleInfo &MMI) {
100 // Targets may override createPassConfig to provide a target-specific
101 // subclass.
102 TargetPassConfig *PassConfig = TM.createPassConfig(PM);
103 // Set PassConfig options provided by TargetMachine.
104 PassConfig->setDisableVerify(DisableVerify);
105 PM.add(PassConfig);
106 PM.add(&MMI);
108 if (PassConfig->addISelPasses())
109 return nullptr;
110 PassConfig->addMachinePasses();
111 PassConfig->setInitialized();
112 return PassConfig;
115 bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM,
116 raw_pwrite_stream &Out,
117 raw_pwrite_stream *DwoOut,
118 CodeGenFileType FileType,
119 MCContext &Context) {
120 if (Options.MCOptions.MCSaveTempLabels)
121 Context.setAllowTemporaryLabels(false);
123 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
124 const MCAsmInfo &MAI = *getMCAsmInfo();
125 const MCRegisterInfo &MRI = *getMCRegisterInfo();
126 const MCInstrInfo &MII = *getMCInstrInfo();
128 std::unique_ptr<MCStreamer> AsmStreamer;
130 switch (FileType) {
131 case CGFT_AssemblyFile: {
132 MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter(
133 getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI);
135 // Create a code emitter if asked to show the encoding.
136 std::unique_ptr<MCCodeEmitter> MCE;
137 if (Options.MCOptions.ShowMCEncoding)
138 MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context));
140 std::unique_ptr<MCAsmBackend> MAB(
141 getTarget().createMCAsmBackend(STI, MRI, 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, std::move(MCE),
146 std::move(MAB), Options.MCOptions.ShowMCInst);
147 AsmStreamer.reset(S);
148 break;
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(STI, MRI, Options.MCOptions);
156 if (!MCE || !MAB)
157 return true;
159 // Don't waste memory on names of temp labels.
160 Context.setUseNamesOnTempLabels(false);
162 Triple T(getTargetTriple().str());
163 AsmStreamer.reset(getTarget().createMCObjectStreamer(
164 T, Context, std::unique_ptr<MCAsmBackend>(MAB),
165 DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut)
166 : MAB->createObjectWriter(Out),
167 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
168 Options.MCOptions.MCIncrementalLinkerCompatible,
169 /*DWARFMustBeAtTheEnd*/ true));
170 break;
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;
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;
185 PM.add(Printer);
186 return false;
189 bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM,
190 raw_pwrite_stream &Out,
191 raw_pwrite_stream *DwoOut,
192 CodeGenFileType FileType,
193 bool DisableVerify,
194 MachineModuleInfo *MMI) {
195 // Add common CodeGen passes.
196 if (!MMI)
197 MMI = new MachineModuleInfo(this);
198 TargetPassConfig *PassConfig =
199 addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
200 if (!PassConfig)
201 return true;
203 if (!TargetPassConfig::willCompleteCodeGenPipeline()) {
204 PM.add(createPrintMIRPass(Out));
205 } else if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext()))
206 return true;
208 PM.add(createFreeMachineFunctionPass());
209 return false;
212 /// addPassesToEmitMC - Add passes to the specified pass manager to get
213 /// machine code emitted with the MCJIT. This method returns true if machine
214 /// code is not supported. It fills the MCContext Ctx pointer which can be
215 /// used to build custom MCStreamer.
217 bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
218 raw_pwrite_stream &Out,
219 bool DisableVerify) {
220 // Add common CodeGen passes.
221 MachineModuleInfo *MMI = new MachineModuleInfo(this);
222 TargetPassConfig *PassConfig =
223 addPassesToGenerateCode(*this, PM, DisableVerify, *MMI);
224 if (!PassConfig)
225 return true;
226 assert(TargetPassConfig::willCompleteCodeGenPipeline() &&
227 "Cannot emit MC with limited codegen pipeline");
229 Ctx = &MMI->getContext();
230 if (Options.MCOptions.MCSaveTempLabels)
231 Ctx->setAllowTemporaryLabels(false);
233 // Create the code emitter for the target if it exists. If not, .o file
234 // emission fails.
235 const MCSubtargetInfo &STI = *getMCSubtargetInfo();
236 const MCRegisterInfo &MRI = *getMCRegisterInfo();
237 MCCodeEmitter *MCE =
238 getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx);
239 MCAsmBackend *MAB =
240 getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions);
241 if (!MCE || !MAB)
242 return true;
244 const Triple &T = getTargetTriple();
245 std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer(
246 T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out),
247 std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll,
248 Options.MCOptions.MCIncrementalLinkerCompatible,
249 /*DWARFMustBeAtTheEnd*/ true));
251 // Create the AsmPrinter, which takes ownership of AsmStreamer if successful.
252 FunctionPass *Printer =
253 getTarget().createAsmPrinter(*this, std::move(AsmStreamer));
254 if (!Printer)
255 return true;
257 PM.add(Printer);
258 PM.add(createFreeMachineFunctionPass());
260 return false; // success!