Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 1 | //===-- LLVMTargetMachine.cpp - Implement the LLVMTargetMachine class -----===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 6 | // |
| 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 Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 19 | #include "llvm/IR/LegacyPassManager.h" |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 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" |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCObjectWriter.h" |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 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" |
David Blaikie | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include "llvm/Target/TargetOptions.h" |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
David Green | 6d9f8c9 | 2018-02-12 11:06:27 +0000 | [diff] [blame] | 37 | static cl::opt<bool> EnableTrapUnreachable("trap-unreachable", |
| 38 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 39 | cl::desc("Enable generating trap for unreachable")); |
| 40 | |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 41 | void LLVMTargetMachine::initAsmInfo() { |
Fangrui Song | 10a2162 | 2018-09-25 06:19:31 +0000 | [diff] [blame] | 42 | MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str())); |
| 43 | MII.reset(TheTarget.createMCInstrInfo()); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 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. |
Fangrui Song | 10a2162 | 2018-09-25 06:19:31 +0000 | [diff] [blame] | 48 | STI.reset(TheTarget.createMCSubtargetInfo( |
| 49 | getTargetTriple().str(), getTargetCPU(), getTargetFeatureString())); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 50 | |
Mirko Brkusanin | 4b63ca1 | 2019-10-23 12:24:35 +0200 | [diff] [blame] | 51 | MCAsmInfo *TmpAsmInfo = TheTarget.createMCAsmInfo( |
| 52 | *MRI, getTargetTriple().str(), Options.MCOptions); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 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 Song | 10a2162 | 2018-09-25 06:19:31 +0000 | [diff] [blame] | 73 | AsmInfo.reset(TmpAsmInfo); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 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; |
David Green | 6d9f8c9 | 2018-02-12 11:06:27 +0000 | [diff] [blame] | 86 | |
| 87 | if (EnableTrapUnreachable) |
| 88 | this->Options.TrapUnreachable = true; |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Sanjoy Das | 26d11ca | 2017-12-22 18:21:59 +0000 | [diff] [blame] | 91 | TargetTransformInfo |
| 92 | LLVMTargetMachine::getTargetTransformInfo(const Function &F) { |
| 93 | return TargetTransformInfo(BasicTTIImpl(this, F)); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /// addPassesToX helper drives creation and initialization of TargetPassConfig. |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 97 | static TargetPassConfig * |
| 98 | addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM, |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 99 | bool DisableVerify, |
| 100 | MachineModuleInfoWrapperPass &MMIWP) { |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 101 | // Targets may override createPassConfig to provide a target-specific |
| 102 | // subclass. |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 103 | TargetPassConfig *PassConfig = TM.createPassConfig(PM); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 104 | // Set PassConfig options provided by TargetMachine. |
| 105 | PassConfig->setDisableVerify(DisableVerify); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 106 | PM.add(PassConfig); |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 107 | PM.add(&MMIWP); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 108 | |
| 109 | if (PassConfig->addISelPasses()) |
| 110 | return nullptr; |
| 111 | PassConfig->addMachinePasses(); |
| 112 | PassConfig->setInitialized(); |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 113 | return PassConfig; |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, |
Peter Collingbourne | 9a45114 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 117 | raw_pwrite_stream &Out, |
| 118 | raw_pwrite_stream *DwoOut, |
| 119 | CodeGenFileType FileType, |
| 120 | MCContext &Context) { |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 121 | if (Options.MCOptions.MCSaveTempLabels) |
| 122 | Context.setAllowTemporaryLabels(false); |
| 123 | |
| 124 | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
| 125 | const MCAsmInfo &MAI = *getMCAsmInfo(); |
| 126 | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
| 127 | const MCInstrInfo &MII = *getMCInstrInfo(); |
| 128 | |
| 129 | std::unique_ptr<MCStreamer> AsmStreamer; |
| 130 | |
| 131 | switch (FileType) { |
| 132 | case CGFT_AssemblyFile: { |
| 133 | MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( |
| 134 | getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); |
| 135 | |
| 136 | // Create a code emitter if asked to show the encoding. |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 137 | std::unique_ptr<MCCodeEmitter> MCE; |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 138 | if (Options.MCOptions.ShowMCEncoding) |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 139 | MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context)); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 140 | |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 141 | std::unique_ptr<MCAsmBackend> MAB( |
| 142 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions)); |
Jonas Devlieghere | 0eaee54 | 2019-08-15 15:54:37 +0000 | [diff] [blame] | 143 | auto FOut = std::make_unique<formatted_raw_ostream>(Out); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 144 | MCStreamer *S = getTarget().createAsmStreamer( |
| 145 | Context, std::move(FOut), Options.MCOptions.AsmVerbose, |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 146 | Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE), |
| 147 | std::move(MAB), Options.MCOptions.ShowMCInst); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 148 | AsmStreamer.reset(S); |
| 149 | break; |
| 150 | } |
| 151 | case CGFT_ObjectFile: { |
| 152 | // Create the code emitter for the target if it exists. If not, .o file |
| 153 | // emission fails. |
| 154 | MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); |
| 155 | MCAsmBackend *MAB = |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 156 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 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( |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 165 | T, Context, std::unique_ptr<MCAsmBackend>(MAB), |
Peter Collingbourne | 9a45114 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 166 | DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut) |
| 167 | : MAB->createObjectWriter(Out), |
| 168 | std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 169 | Options.MCOptions.MCIncrementalLinkerCompatible, |
| 170 | /*DWARFMustBeAtTheEnd*/ true)); |
| 171 | break; |
| 172 | } |
| 173 | case CGFT_Null: |
| 174 | // The Null output is intended for use for performance analysis and testing, |
| 175 | // not real users. |
| 176 | AsmStreamer.reset(getTarget().createNullStreamer(Context)); |
| 177 | break; |
| 178 | } |
| 179 | |
| 180 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
| 181 | FunctionPass *Printer = |
| 182 | getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); |
| 183 | if (!Printer) |
| 184 | return true; |
| 185 | |
| 186 | PM.add(Printer); |
| 187 | return false; |
| 188 | } |
| 189 | |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 190 | bool LLVMTargetMachine::addPassesToEmitFile( |
| 191 | PassManagerBase &PM, raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut, |
| 192 | CodeGenFileType FileType, bool DisableVerify, |
| 193 | MachineModuleInfoWrapperPass *MMIWP) { |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 194 | // Add common CodeGen passes. |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 195 | if (!MMIWP) |
| 196 | MMIWP = new MachineModuleInfoWrapperPass(this); |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 197 | TargetPassConfig *PassConfig = |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 198 | addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 199 | if (!PassConfig) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 200 | return true; |
| 201 | |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 202 | if (!TargetPassConfig::willCompleteCodeGenPipeline()) { |
Jason Liu | 60ec248 | 2019-06-06 19:13:36 +0000 | [diff] [blame] | 203 | if (this->getTargetTriple().isOSAIX()) { |
| 204 | // On AIX, we might manifest MCSymbols during SDAG lowering. For MIR |
| 205 | // testing to be meaningful, we need to ensure that the symbols created |
| 206 | // are MCSymbolXCOFF variants, which requires that |
| 207 | // the TargetLoweringObjectFile instance has been initialized. |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 208 | MCContext &Ctx = MMIWP->getMMI().getContext(); |
Jason Liu | 60ec248 | 2019-06-06 19:13:36 +0000 | [diff] [blame] | 209 | const_cast<TargetLoweringObjectFile &>(*this->getObjFileLowering()) |
| 210 | .Initialize(Ctx, *this); |
| 211 | } |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 212 | PM.add(createPrintMIRPass(Out)); |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 213 | } else if (addAsmPrinter(PM, Out, DwoOut, FileType, |
| 214 | MMIWP->getMMI().getContext())) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 215 | 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 | /// |
| 226 | bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, |
| 227 | raw_pwrite_stream &Out, |
| 228 | bool DisableVerify) { |
| 229 | // Add common CodeGen passes. |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 230 | MachineModuleInfoWrapperPass *MMIWP = new MachineModuleInfoWrapperPass(this); |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 231 | TargetPassConfig *PassConfig = |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 232 | addPassesToGenerateCode(*this, PM, DisableVerify, *MMIWP); |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 233 | if (!PassConfig) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 234 | return true; |
Matthias Braun | e8f717a | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 235 | assert(TargetPassConfig::willCompleteCodeGenPipeline() && |
| 236 | "Cannot emit MC with limited codegen pipeline"); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 237 | |
Yuanfang Chen | cc382cf | 2019-09-30 17:54:50 +0000 | [diff] [blame] | 238 | Ctx = &MMIWP->getMMI().getContext(); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 239 | 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 Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 244 | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 245 | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
| 246 | MCCodeEmitter *MCE = |
| 247 | getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); |
| 248 | MCAsmBackend *MAB = |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 249 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 250 | if (!MCE || !MAB) |
| 251 | return true; |
| 252 | |
| 253 | const Triple &T = getTargetTriple(); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 254 | std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 255 | T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out), |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 256 | 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 | } |