Matthias Braun | bb8507e | 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 | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 20 | #include "llvm/IR/LegacyPassManager.h" |
Matthias Braun | bb8507e | 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 | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCObjectWriter.h" |
Matthias Braun | bb8507e | 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 | 6054e65 | 2018-03-23 23:58:19 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetMachine.h" |
| 35 | #include "llvm/Target/TargetOptions.h" |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
David Green | 6d9f8c9 | 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 | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 42 | void LLVMTargetMachine::initAsmInfo() { |
Fangrui Song | 10a2162 | 2018-09-25 06:19:31 +0000 | [diff] [blame^] | 43 | MRI.reset(TheTarget.createMCRegInfo(getTargetTriple().str())); |
| 44 | MII.reset(TheTarget.createMCInstrInfo()); |
Matthias Braun | bb8507e | 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 | 10a2162 | 2018-09-25 06:19:31 +0000 | [diff] [blame^] | 49 | STI.reset(TheTarget.createMCSubtargetInfo( |
| 50 | getTargetTriple().str(), getTargetCPU(), getTargetFeatureString())); |
Matthias Braun | bb8507e | 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 | 10a2162 | 2018-09-25 06:19:31 +0000 | [diff] [blame^] | 74 | AsmInfo.reset(TmpAsmInfo); |
Matthias Braun | bb8507e | 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 | 6d9f8c9 | 2018-02-12 11:06:27 +0000 | [diff] [blame] | 87 | |
| 88 | if (EnableTrapUnreachable) |
| 89 | this->Options.TrapUnreachable = true; |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Sanjoy Das | 26d11ca | 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 | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /// addPassesToX helper drives creation and initialization of TargetPassConfig. |
| 98 | static MCContext * |
| 99 | addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM, |
| 100 | bool DisableVerify, bool &WillCompleteCodeGenPipeline, |
| 101 | raw_pwrite_stream &Out, MachineModuleInfo *MMI) { |
| 102 | // Targets may override createPassConfig to provide a target-specific |
| 103 | // subclass. |
| 104 | TargetPassConfig *PassConfig = TM->createPassConfig(PM); |
| 105 | // Set PassConfig options provided by TargetMachine. |
| 106 | PassConfig->setDisableVerify(DisableVerify); |
| 107 | WillCompleteCodeGenPipeline = PassConfig->willCompleteCodeGenPipeline(); |
| 108 | PM.add(PassConfig); |
| 109 | if (!MMI) |
| 110 | MMI = new MachineModuleInfo(TM); |
| 111 | PM.add(MMI); |
| 112 | |
| 113 | if (PassConfig->addISelPasses()) |
| 114 | return nullptr; |
| 115 | PassConfig->addMachinePasses(); |
| 116 | PassConfig->setInitialized(); |
| 117 | if (!WillCompleteCodeGenPipeline) |
| 118 | PM.add(createPrintMIRPass(Out)); |
| 119 | |
| 120 | return &MMI->getContext(); |
| 121 | } |
| 122 | |
| 123 | bool LLVMTargetMachine::addAsmPrinter(PassManagerBase &PM, |
Peter Collingbourne | 9a45114 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 124 | raw_pwrite_stream &Out, |
| 125 | raw_pwrite_stream *DwoOut, |
| 126 | CodeGenFileType FileType, |
| 127 | MCContext &Context) { |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 128 | if (Options.MCOptions.MCSaveTempLabels) |
| 129 | Context.setAllowTemporaryLabels(false); |
| 130 | |
| 131 | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
| 132 | const MCAsmInfo &MAI = *getMCAsmInfo(); |
| 133 | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
| 134 | const MCInstrInfo &MII = *getMCInstrInfo(); |
| 135 | |
| 136 | std::unique_ptr<MCStreamer> AsmStreamer; |
| 137 | |
| 138 | switch (FileType) { |
| 139 | case CGFT_AssemblyFile: { |
| 140 | MCInstPrinter *InstPrinter = getTarget().createMCInstPrinter( |
| 141 | getTargetTriple(), MAI.getAssemblerDialect(), MAI, MII, MRI); |
| 142 | |
| 143 | // Create a code emitter if asked to show the encoding. |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 144 | std::unique_ptr<MCCodeEmitter> MCE; |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 145 | if (Options.MCOptions.ShowMCEncoding) |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 146 | MCE.reset(getTarget().createMCCodeEmitter(MII, MRI, Context)); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 147 | |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 148 | std::unique_ptr<MCAsmBackend> MAB( |
| 149 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions)); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 150 | auto FOut = llvm::make_unique<formatted_raw_ostream>(Out); |
| 151 | MCStreamer *S = getTarget().createAsmStreamer( |
| 152 | Context, std::move(FOut), Options.MCOptions.AsmVerbose, |
Nirav Dave | 1b5533c | 2018-04-27 15:45:54 +0000 | [diff] [blame] | 153 | Options.MCOptions.MCUseDwarfDirectory, InstPrinter, std::move(MCE), |
| 154 | std::move(MAB), Options.MCOptions.ShowMCInst); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 155 | AsmStreamer.reset(S); |
| 156 | break; |
| 157 | } |
| 158 | case CGFT_ObjectFile: { |
| 159 | // Create the code emitter for the target if it exists. If not, .o file |
| 160 | // emission fails. |
| 161 | MCCodeEmitter *MCE = getTarget().createMCCodeEmitter(MII, MRI, Context); |
| 162 | MCAsmBackend *MAB = |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 163 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 164 | if (!MCE || !MAB) |
| 165 | return true; |
| 166 | |
| 167 | // Don't waste memory on names of temp labels. |
| 168 | Context.setUseNamesOnTempLabels(false); |
| 169 | |
| 170 | Triple T(getTargetTriple().str()); |
| 171 | AsmStreamer.reset(getTarget().createMCObjectStreamer( |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 172 | T, Context, std::unique_ptr<MCAsmBackend>(MAB), |
Peter Collingbourne | 9a45114 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 173 | DwoOut ? MAB->createDwoObjectWriter(Out, *DwoOut) |
| 174 | : MAB->createObjectWriter(Out), |
| 175 | std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 176 | Options.MCOptions.MCIncrementalLinkerCompatible, |
| 177 | /*DWARFMustBeAtTheEnd*/ true)); |
| 178 | break; |
| 179 | } |
| 180 | case CGFT_Null: |
| 181 | // The Null output is intended for use for performance analysis and testing, |
| 182 | // not real users. |
| 183 | AsmStreamer.reset(getTarget().createNullStreamer(Context)); |
| 184 | break; |
| 185 | } |
| 186 | |
| 187 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
| 188 | FunctionPass *Printer = |
| 189 | getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); |
| 190 | if (!Printer) |
| 191 | return true; |
| 192 | |
| 193 | PM.add(Printer); |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | bool LLVMTargetMachine::addPassesToEmitFile(PassManagerBase &PM, |
| 198 | raw_pwrite_stream &Out, |
Peter Collingbourne | 9a45114 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 199 | raw_pwrite_stream *DwoOut, |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 200 | CodeGenFileType FileType, |
| 201 | bool DisableVerify, |
| 202 | MachineModuleInfo *MMI) { |
| 203 | // Add common CodeGen passes. |
| 204 | bool WillCompleteCodeGenPipeline = true; |
| 205 | MCContext *Context = addPassesToGenerateCode( |
| 206 | this, PM, DisableVerify, WillCompleteCodeGenPipeline, Out, MMI); |
| 207 | if (!Context) |
| 208 | return true; |
| 209 | |
Peter Collingbourne | 9a45114 | 2018-05-21 20:16:41 +0000 | [diff] [blame] | 210 | if (WillCompleteCodeGenPipeline && |
| 211 | addAsmPrinter(PM, Out, DwoOut, FileType, *Context)) |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 212 | return true; |
| 213 | |
| 214 | PM.add(createFreeMachineFunctionPass()); |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | /// addPassesToEmitMC - Add passes to the specified pass manager to get |
| 219 | /// machine code emitted with the MCJIT. This method returns true if machine |
| 220 | /// code is not supported. It fills the MCContext Ctx pointer which can be |
| 221 | /// used to build custom MCStreamer. |
| 222 | /// |
| 223 | bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx, |
| 224 | raw_pwrite_stream &Out, |
| 225 | bool DisableVerify) { |
| 226 | // Add common CodeGen passes. |
| 227 | bool WillCompleteCodeGenPipeline = true; |
| 228 | Ctx = addPassesToGenerateCode(this, PM, DisableVerify, |
| 229 | WillCompleteCodeGenPipeline, Out, |
| 230 | /*MachineModuleInfo*/ nullptr); |
| 231 | if (!Ctx) |
| 232 | return true; |
| 233 | assert(WillCompleteCodeGenPipeline && "CodeGen pipeline has been altered"); |
| 234 | |
| 235 | if (Options.MCOptions.MCSaveTempLabels) |
| 236 | Ctx->setAllowTemporaryLabels(false); |
| 237 | |
| 238 | // Create the code emitter for the target if it exists. If not, .o file |
| 239 | // emission fails. |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 240 | const MCSubtargetInfo &STI = *getMCSubtargetInfo(); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 241 | const MCRegisterInfo &MRI = *getMCRegisterInfo(); |
| 242 | MCCodeEmitter *MCE = |
| 243 | getTarget().createMCCodeEmitter(*getMCInstrInfo(), MRI, *Ctx); |
| 244 | MCAsmBackend *MAB = |
Alex Bradbury | b22f751 | 2018-01-03 08:53:05 +0000 | [diff] [blame] | 245 | getTarget().createMCAsmBackend(STI, MRI, Options.MCOptions); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 246 | if (!MCE || !MAB) |
| 247 | return true; |
| 248 | |
| 249 | const Triple &T = getTargetTriple(); |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 250 | std::unique_ptr<MCStreamer> AsmStreamer(getTarget().createMCObjectStreamer( |
Peter Collingbourne | f7b81db | 2018-05-18 18:26:45 +0000 | [diff] [blame] | 251 | T, *Ctx, std::unique_ptr<MCAsmBackend>(MAB), MAB->createObjectWriter(Out), |
Matthias Braun | bb8507e | 2017-10-12 22:57:28 +0000 | [diff] [blame] | 252 | std::unique_ptr<MCCodeEmitter>(MCE), STI, Options.MCOptions.MCRelaxAll, |
| 253 | Options.MCOptions.MCIncrementalLinkerCompatible, |
| 254 | /*DWARFMustBeAtTheEnd*/ true)); |
| 255 | |
| 256 | // Create the AsmPrinter, which takes ownership of AsmStreamer if successful. |
| 257 | FunctionPass *Printer = |
| 258 | getTarget().createAsmPrinter(*this, std::move(AsmStreamer)); |
| 259 | if (!Printer) |
| 260 | return true; |
| 261 | |
| 262 | PM.add(Printer); |
| 263 | PM.add(createFreeMachineFunctionPass()); |
| 264 | |
| 265 | return false; // success! |
| 266 | } |