Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 1 | //===-- 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" |
| 19 | #include "llvm/CodeGen/TargetPassConfig.h" |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LegacyPassManager.h" |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCAsmBackend.h" |
| 22 | #include "llvm/MC/MCAsmInfo.h" |
| 23 | #include "llvm/MC/MCCodeEmitter.h" |
| 24 | #include "llvm/MC/MCContext.h" |
| 25 | #include "llvm/MC/MCInstrInfo.h" |
Peter Collingbourne | 17a9814 | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCObjectWriter.h" |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 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" |
David Blaikie | fe42bd5 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetMachine.h" |
| 35 | #include "llvm/Target/TargetOptions.h" |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
David Green | 78a2fed | 2018-02-12 11:06:27 +0000 | [diff] [blame] | 38 | static cl::opt<bool> EnableTrapUnreachable("trap-unreachable", |
| 39 | cl::Hidden, cl::ZeroOrMore, cl::init(false), |
| 40 | cl::desc("Enable generating trap for unreachable")); |
| 41 | |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 42 | void LLVMTargetMachine::initAsmInfo() { |
Fangrui Song | ae4ef7d | 2018-09-25 06:19:31 +0000 | [diff] [blame] | 43 | MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str())); |
| 44 | MII.reset(TheTarget.createMCInstrInfo()); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 45 | // FIXME: Having an MCSubtargetInfo on the target machine is a hack due |
| 46 | // to some backends having subtarget feature dependent module level |
| 47 | // code generation. This is similar to the hack in the AsmPrinter for |
| 48 | // module level assembly etc. |
Fangrui Song | ae4ef7d | 2018-09-25 06:19:31 +0000 | [diff] [blame] | 49 | STI.reset(TheTarget.createMCSubtargetInfo( |
| 50 | getTargetTriple().str(), getTargetCPU(), getTargetFeatureString())); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 51 | |
| 52 | MCAsmInfo *TmpAsmInfo = |
| 53 | TheTarget.createMCAsmInfo(*MRI, getTargetTriple().str()); |
| 54 | // TargetSelect.h moved to a different directory between LLVM 2.9 and 3.0, |
| 55 | // and if the old one gets included then MCAsmInfo will be NULL and |
| 56 | // we'll crash later. |
| 57 | // Provide the user with a useful error message about what's wrong. |
| 58 | assert(TmpAsmInfo && "MCAsmInfo not initialized. " |
| 59 | "Make sure you include the correct TargetSelect.h" |
| 60 | "and that InitializeAllTargetMCs() is being invoked!"); |
| 61 | |
| 62 | if (Options.DisableIntegratedAS) |
| 63 | TmpAsmInfo->setUseIntegratedAssembler(false); |
| 64 | |
| 65 | TmpAsmInfo->setPreserveAsmComments(Options.MCOptions.PreserveAsmComments); |
| 66 | |
| 67 | TmpAsmInfo->setCompressDebugSections(Options.CompressDebugSections); |
| 68 | |
| 69 | TmpAsmInfo->setRelaxELFRelocations(Options.RelaxELFRelocations); |
| 70 | |
| 71 | if (Options.ExceptionModel != ExceptionHandling::None) |
| 72 | TmpAsmInfo->setExceptionsType(Options.ExceptionModel); |
| 73 | |
Fangrui Song | ae4ef7d | 2018-09-25 06:19:31 +0000 | [diff] [blame] | 74 | AsmInfo.reset(TmpAsmInfo); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | LLVMTargetMachine::LLVMTargetMachine(const Target &T, |
| 78 | StringRef DataLayoutString, |
| 79 | const Triple &TT, StringRef CPU, |
| 80 | StringRef FS, const TargetOptions &Options, |
| 81 | Reloc::Model RM, CodeModel::Model CM, |
| 82 | CodeGenOpt::Level OL) |
| 83 | : TargetMachine(T, DataLayoutString, TT, CPU, FS, Options) { |
| 84 | this->RM = RM; |
| 85 | this->CMModel = CM; |
| 86 | this->OptLevel = OL; |
David Green | 78a2fed | 2018-02-12 11:06:27 +0000 | [diff] [blame] | 87 | |
| 88 | if (EnableTrapUnreachable) |
| 89 | this->Options.TrapUnreachable = true; |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Sanjoy Das | b166e67 | 2017-12-22 18:21:59 +0000 | [diff] [blame] | 92 | TargetTransformInfo |
| 93 | LLVMTargetMachine::getTargetTransformInfo(const Function &F) { |
| 94 | return TargetTransformInfo(BasicTTIImpl(this, F)); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /// addPassesToX helper drives creation and initialization of TargetPassConfig. |
Matthias Braun | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 98 | static TargetPassConfig * |
| 99 | addPassesToGenerateCode(LLVMTargetMachine &TM, PassManagerBase &PM, |
| 100 | bool DisableVerify, MachineModuleInfo &MMI) { |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 101 | // Targets may override createPassConfig to provide a target-specific |
| 102 | // subclass. |
Matthias Braun | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 103 | TargetPassConfig *PassConfig = TM.createPassConfig(PM); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 104 | // Set PassConfig options provided by TargetMachine. |
| 105 | PassConfig->setDisableVerify(DisableVerify); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 106 | PM.add(PassConfig); |
Matthias Braun | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 107 | PM.add(&MMI); |
Matthias Braun | 9385cf1 | 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 | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 113 | return PassConfig; |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, |
Peter Collingbourne | 9ffe073 | 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 | 9385cf1 | 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 | 80e97ed | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 137 | std::unique_ptr<MCCodeEmitter> MCE; |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 138 | if (Options.MCOptions.ShowMCEncoding) |
Nirav Dave | 80e97ed | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 139 | MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context)); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 140 | |
Nirav Dave | 80e97ed | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 141 | std::unique_ptr<MCAsmBackend> MAB( |
| 142 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions)); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 143 | auto FOut = llvm::make_unique<formatted_raw_ostream>(Out); |
| 144 | MCStreamer *S = getTarget().createAsmStreamer( |
| 145 | Context, std::move(FOut), Options.MCOptions.AsmVerbose, |
Nirav Dave | 80e97ed | 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 | 9385cf1 | 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 | d32868d | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 156 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); |
Matthias Braun | 9385cf1 | 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 | 17a9814 | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 165 | T, Context, std::unique_ptr<MCAsmBackend>(MAB), |
Peter Collingbourne | 9ffe073 | 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 | 9385cf1 | 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 | |
| 190 | bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, |
| 191 | raw_pwrite_stream &Out, |
Peter Collingbourne | 9ffe073 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 192 | raw_pwrite_stream *DwoOut, |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 193 | CodeGenFileType FileType, |
| 194 | bool DisableVerify, |
| 195 | MachineModuleInfo *MMI) { |
| 196 | // Add common CodeGen passes. |
Matthias Braun | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 197 | if (!MMI) |
| 198 | MMI = new MachineModuleInfo(this); |
| 199 | TargetPassConfig *PassConfig = |
| 200 | addPassesToGenerateCode(*this, PM, DisableVerify, *MMI); |
| 201 | if (!PassConfig) |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 202 | return true; |
| 203 | |
Matthias Braun | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 204 | if (!TargetPassConfig::willCompleteCodeGenPipeline()) { |
| 205 | PM.add(createPrintMIRPass(Out)); |
| 206 | } else if (addAsmPrinter(PM, Out, DwoOut, FileType, MMI->getContext())) |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 207 | return true; |
| 208 | |
| 209 | PM.add(createFreeMachineFunctionPass()); |
| 210 | return false; |
| 211 | } |
| 212 | |
| 213 | /// addPassesToEmitMC - Add passes to the specified pass manager to get |
| 214 | /// machine code emitted with the MCJIT. This method returns true if machine |
| 215 | /// code is not supported. It fills the MCContext Ctx pointer which can be |
| 216 | /// used to build custom MCStreamer. |
| 217 | /// |
| 218 | bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, |
| 219 | raw_pwrite_stream &Out, |
| 220 | bool DisableVerify) { |
| 221 | // Add common CodeGen passes. |
Matthias Braun | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 222 | MachineModuleInfo *MMI = new MachineModuleInfo(this); |
| 223 | TargetPassConfig *PassConfig = |
| 224 | addPassesToGenerateCode(*this, PM, DisableVerify, *MMI); |
| 225 | if (!PassConfig) |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 226 | return true; |
Matthias Braun | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 227 | assert(TargetPassConfig::willCompleteCodeGenPipeline() && |
| 228 | "Cannot emit MC with limited codegen pipeline"); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 229 | |
Matthias Braun | 94f7fc2 | 2018-11-02 01:31:50 +0000 | [diff] [blame] | 230 | Ctx = &MMI->getContext(); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 231 | if (Options.MCOptions.MCSaveTempLabels) |
| 232 | Ctx->setAllowTemporaryLabels(false); |
| 233 | |
| 234 | // Create the code emitter for the target if it exists. If not, .o file |
| 235 | // emission fails. |
Alex Bradbury | d32868d | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 236 | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 237 | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
| 238 | MCCodeEmitter *MCE = |
| 239 | getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); |
| 240 | MCAsmBackend *MAB = |
Alex Bradbury | d32868d | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 241 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 242 | if (!MCE || !MAB) |
| 243 | return true; |
| 244 | |
| 245 | const Triple &T = getTargetTriple(); |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 246 | std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( |
Peter Collingbourne | 17a9814 | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 247 | T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out), |
Matthias Braun | 9385cf1 | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 248 | std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, |
| 249 | Options.MCOptions.MCIncrementalLinkerCompatible, |
| 250 | /*DWARFMustBeAtTheEnd*/ true)); |
| 251 | |
| 252 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
| 253 | FunctionPass *Printer = |
| 254 | getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); |
| 255 | if (!Printer) |
| 256 | return true; |
| 257 | |
| 258 | PM.add(Printer); |
| 259 | PM.add(createFreeMachineFunctionPass()); |
| 260 | |
| 261 | return false; // success! |
| 262 | } |