File indexing completed on 2026-05-10 08:44:36
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #ifndef LLVM_TARGET_TARGETMACHINE_H
0014 #define LLVM_TARGET_TARGETMACHINE_H
0015
0016 #include "llvm/ADT/StringRef.h"
0017 #include "llvm/IR/DataLayout.h"
0018 #include "llvm/IR/PassManager.h"
0019 #include "llvm/MC/MCStreamer.h"
0020 #include "llvm/Support/Allocator.h"
0021 #include "llvm/Support/CodeGen.h"
0022 #include "llvm/Support/Error.h"
0023 #include "llvm/Support/PGOOptions.h"
0024 #include "llvm/Target/CGPassBuilderOption.h"
0025 #include "llvm/Target/TargetOptions.h"
0026 #include "llvm/TargetParser/Triple.h"
0027 #include <optional>
0028 #include <string>
0029 #include <utility>
0030
0031 namespace llvm {
0032
0033 class AAManager;
0034 using ModulePassManager = PassManager<Module>;
0035
0036 class Function;
0037 class GlobalValue;
0038 class MachineModuleInfoWrapperPass;
0039 class Mangler;
0040 class MCAsmInfo;
0041 class MCContext;
0042 class MCInstrInfo;
0043 class MCRegisterInfo;
0044 class MCSubtargetInfo;
0045 class MCSymbol;
0046 class raw_pwrite_stream;
0047 class PassBuilder;
0048 class PassInstrumentationCallbacks;
0049 struct PerFunctionMIParsingState;
0050 class SMDiagnostic;
0051 class SMRange;
0052 class Target;
0053 class TargetIntrinsicInfo;
0054 class TargetIRAnalysis;
0055 class TargetTransformInfo;
0056 class TargetLoweringObjectFile;
0057 class TargetPassConfig;
0058 class TargetSubtargetInfo;
0059
0060
0061 namespace legacy {
0062 class PassManagerBase;
0063 }
0064 using legacy::PassManagerBase;
0065
0066 struct MachineFunctionInfo;
0067 namespace yaml {
0068 struct MachineFunctionInfo;
0069 }
0070
0071
0072
0073
0074
0075
0076
0077 class TargetMachine {
0078 protected:
0079 TargetMachine(const Target &T, StringRef DataLayoutString,
0080 const Triple &TargetTriple, StringRef CPU, StringRef FS,
0081 const TargetOptions &Options);
0082
0083
0084 const Target &TheTarget;
0085
0086
0087
0088
0089
0090
0091
0092 const DataLayout DL;
0093
0094
0095
0096 Triple TargetTriple;
0097 std::string TargetCPU;
0098 std::string TargetFS;
0099
0100 Reloc::Model RM = Reloc::Static;
0101 CodeModel::Model CMModel = CodeModel::Small;
0102 uint64_t LargeDataThreshold = 0;
0103 CodeGenOptLevel OptLevel = CodeGenOptLevel::Default;
0104
0105
0106 std::unique_ptr<const MCAsmInfo> AsmInfo;
0107 std::unique_ptr<const MCRegisterInfo> MRI;
0108 std::unique_ptr<const MCInstrInfo> MII;
0109 std::unique_ptr<const MCSubtargetInfo> STI;
0110
0111 unsigned RequireStructuredCFG : 1;
0112 unsigned O0WantsFastISel : 1;
0113
0114
0115 std::optional<PGOOptions> PGOOption;
0116
0117 public:
0118 mutable TargetOptions Options;
0119
0120 TargetMachine(const TargetMachine &) = delete;
0121 void operator=(const TargetMachine &) = delete;
0122 virtual ~TargetMachine();
0123
0124 const Target &getTarget() const { return TheTarget; }
0125
0126 const Triple &getTargetTriple() const { return TargetTriple; }
0127 StringRef getTargetCPU() const { return TargetCPU; }
0128 StringRef getTargetFeatureString() const { return TargetFS; }
0129 void setTargetFeatureString(StringRef FS) { TargetFS = std::string(FS); }
0130
0131
0132
0133 virtual const TargetSubtargetInfo *getSubtargetImpl(const Function &) const {
0134 return nullptr;
0135 }
0136 virtual TargetLoweringObjectFile *getObjFileLowering() const {
0137 return nullptr;
0138 }
0139
0140
0141 virtual MachineFunctionInfo *
0142 createMachineFunctionInfo(BumpPtrAllocator &Allocator, const Function &F,
0143 const TargetSubtargetInfo *STI) const {
0144 return nullptr;
0145 }
0146
0147
0148
0149 virtual yaml::MachineFunctionInfo *createDefaultFuncInfoYAML() const {
0150 return nullptr;
0151 }
0152
0153
0154
0155 virtual yaml::MachineFunctionInfo *
0156 convertFuncInfoToYAML(const MachineFunction &MF) const {
0157 return nullptr;
0158 }
0159
0160
0161 virtual bool parseMachineFunctionInfo(const yaml::MachineFunctionInfo &,
0162 PerFunctionMIParsingState &PFS,
0163 SMDiagnostic &Error,
0164 SMRange &SourceRange) const {
0165 return false;
0166 }
0167
0168
0169
0170
0171 template <typename STC> const STC &getSubtarget(const Function &F) const {
0172 return *static_cast<const STC*>(getSubtargetImpl(F));
0173 }
0174
0175
0176 const DataLayout createDataLayout() const { return DL; }
0177
0178
0179
0180
0181
0182
0183 bool isCompatibleDataLayout(const DataLayout &Candidate) const {
0184 return DL == Candidate;
0185 }
0186
0187
0188
0189
0190 unsigned getPointerSize(unsigned AS) const {
0191 return DL.getPointerSize(AS);
0192 }
0193
0194 unsigned getPointerSizeInBits(unsigned AS) const {
0195 return DL.getPointerSizeInBits(AS);
0196 }
0197
0198 unsigned getProgramPointerSize() const {
0199 return DL.getPointerSize(DL.getProgramAddressSpace());
0200 }
0201
0202 unsigned getAllocaPointerSize() const {
0203 return DL.getPointerSize(DL.getAllocaAddrSpace());
0204 }
0205
0206
0207
0208
0209 void resetTargetOptions(const Function &F) const;
0210
0211
0212 const MCAsmInfo *getMCAsmInfo() const { return AsmInfo.get(); }
0213
0214 const MCRegisterInfo *getMCRegisterInfo() const { return MRI.get(); }
0215 const MCInstrInfo *getMCInstrInfo() const { return MII.get(); }
0216 const MCSubtargetInfo *getMCSubtargetInfo() const { return STI.get(); }
0217
0218
0219 virtual const TargetIntrinsicInfo *getIntrinsicInfo() const {
0220 return nullptr;
0221 }
0222
0223 bool requiresStructuredCFG() const { return RequireStructuredCFG; }
0224 void setRequiresStructuredCFG(bool Value) { RequireStructuredCFG = Value; }
0225
0226
0227
0228 Reloc::Model getRelocationModel() const;
0229
0230
0231
0232 CodeModel::Model getCodeModel() const { return CMModel; }
0233
0234
0235 uint64_t getMaxCodeSize() const;
0236
0237
0238 void setCodeModel(CodeModel::Model CM) { CMModel = CM; }
0239
0240 void setLargeDataThreshold(uint64_t LDT) { LargeDataThreshold = LDT; }
0241 bool isLargeGlobalValue(const GlobalValue *GV) const;
0242
0243 bool isPositionIndependent() const;
0244
0245 bool shouldAssumeDSOLocal(const GlobalValue *GV) const;
0246
0247
0248 bool useEmulatedTLS() const;
0249
0250
0251 bool useTLSDESC() const;
0252
0253
0254 TLSModel::Model getTLSModel(const GlobalValue *GV) const;
0255
0256
0257 CodeGenOptLevel getOptLevel() const { return OptLevel; }
0258
0259
0260 void setOptLevel(CodeGenOptLevel Level) { OptLevel = Level; }
0261
0262 void setFastISel(bool Enable) { Options.EnableFastISel = Enable; }
0263 bool getO0WantsFastISel() { return O0WantsFastISel; }
0264 void setO0WantsFastISel(bool Enable) { O0WantsFastISel = Enable; }
0265 void setGlobalISel(bool Enable) { Options.EnableGlobalISel = Enable; }
0266 void setGlobalISelAbort(GlobalISelAbortMode Mode) {
0267 Options.GlobalISelAbort = Mode;
0268 }
0269 void setMachineOutliner(bool Enable) {
0270 Options.EnableMachineOutliner = Enable;
0271 }
0272 void setSupportsDefaultOutlining(bool Enable) {
0273 Options.SupportsDefaultOutlining = Enable;
0274 }
0275 void setSupportsDebugEntryValues(bool Enable) {
0276 Options.SupportsDebugEntryValues = Enable;
0277 }
0278
0279 void setCFIFixup(bool Enable) { Options.EnableCFIFixup = Enable; }
0280
0281 bool getAIXExtendedAltivecABI() const {
0282 return Options.EnableAIXExtendedAltivecABI;
0283 }
0284
0285 bool getUniqueSectionNames() const { return Options.UniqueSectionNames; }
0286
0287
0288 bool getUniqueBasicBlockSectionNames() const {
0289 return Options.UniqueBasicBlockSectionNames;
0290 }
0291
0292 bool getSeparateNamedSections() const {
0293 return Options.SeparateNamedSections;
0294 }
0295
0296
0297
0298 bool getDataSections() const {
0299 return Options.DataSections;
0300 }
0301
0302
0303
0304 bool getFunctionSections() const {
0305 return Options.FunctionSections;
0306 }
0307
0308
0309
0310 bool getIgnoreXCOFFVisibility() const {
0311 return Options.IgnoreXCOFFVisibility;
0312 }
0313
0314
0315
0316 bool getXCOFFTracebackTable() const { return Options.XCOFFTracebackTable; }
0317
0318
0319
0320 llvm::BasicBlockSection getBBSectionsType() const {
0321 return Options.BBSections;
0322 }
0323
0324
0325 const MemoryBuffer *getBBSectionsFuncListBuf() const {
0326 return Options.BBSectionsFuncListBuf.get();
0327 }
0328
0329
0330 virtual bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const {
0331 return false;
0332 }
0333
0334 void setPGOOption(std::optional<PGOOptions> PGOOpt) { PGOOption = PGOOpt; }
0335 const std::optional<PGOOptions> &getPGOOption() const { return PGOOption; }
0336
0337
0338
0339
0340
0341
0342
0343 virtual unsigned getAssumedAddrSpace(const Value *V) const { return -1; }
0344
0345
0346
0347
0348
0349
0350
0351
0352 virtual std::pair<const Value *, unsigned>
0353 getPredicatedAddrSpace(const Value *V) const {
0354 return std::make_pair(nullptr, -1);
0355 }
0356
0357
0358
0359
0360
0361
0362 TargetIRAnalysis getTargetIRAnalysis() const;
0363
0364
0365
0366
0367
0368 virtual TargetTransformInfo getTargetTransformInfo(const Function &F) const;
0369
0370
0371
0372 virtual void registerPassBuilderCallbacks(PassBuilder &) {}
0373
0374
0375
0376 virtual void registerDefaultAliasAnalyses(AAManager &) {}
0377
0378
0379
0380
0381
0382
0383
0384 virtual bool
0385 addPassesToEmitFile(PassManagerBase &, raw_pwrite_stream &,
0386 raw_pwrite_stream *, CodeGenFileType,
0387 bool = true,
0388 MachineModuleInfoWrapperPass *MMIWP = nullptr) {
0389 return true;
0390 }
0391
0392
0393
0394
0395
0396
0397 virtual bool addPassesToEmitMC(PassManagerBase &, MCContext *&,
0398 raw_pwrite_stream &,
0399 bool = true) {
0400 return true;
0401 }
0402
0403
0404
0405
0406
0407
0408 virtual bool targetSchedulesPostRAScheduling() const { return false; };
0409
0410 void getNameWithPrefix(SmallVectorImpl<char> &Name, const GlobalValue *GV,
0411 Mangler &Mang, bool MayAlwaysUsePrivate = false) const;
0412 MCSymbol *getSymbol(const GlobalValue *GV) const;
0413
0414
0415 static constexpr unsigned DefaultSjLjDataSize = 32;
0416 virtual unsigned getSjLjDataSize() const { return DefaultSjLjDataSize; }
0417
0418 static std::pair<int, int> parseBinutilsVersion(StringRef Version);
0419
0420
0421
0422 virtual unsigned getAddressSpaceForPseudoSourceKind(unsigned Kind) const {
0423 return 0;
0424 }
0425
0426
0427
0428
0429
0430
0431
0432 virtual bool splitModule(
0433 Module &M, unsigned NumParts,
0434 function_ref<void(std::unique_ptr<Module> MPart)> ModuleCallback) {
0435 return false;
0436 }
0437
0438
0439
0440 virtual TargetPassConfig *createPassConfig(PassManagerBase &PM) {
0441 return nullptr;
0442 }
0443
0444 virtual Error buildCodeGenPipeline(ModulePassManager &, raw_pwrite_stream &,
0445 raw_pwrite_stream *, CodeGenFileType,
0446 const CGPassBuilderOption &,
0447 PassInstrumentationCallbacks *) {
0448 return make_error<StringError>("buildCodeGenPipeline is not overridden",
0449 inconvertibleErrorCode());
0450 }
0451
0452
0453
0454
0455
0456 virtual bool isMachineVerifierClean() const { return true; }
0457
0458
0459
0460 virtual bool addAsmPrinter(PassManagerBase &PM, raw_pwrite_stream &Out,
0461 raw_pwrite_stream *DwoOut,
0462 CodeGenFileType FileType, MCContext &Context) {
0463 return false;
0464 }
0465
0466 virtual Expected<std::unique_ptr<MCStreamer>>
0467 createMCStreamer(raw_pwrite_stream &Out, raw_pwrite_stream *DwoOut,
0468 CodeGenFileType FileType, MCContext &Ctx) {
0469 return nullptr;
0470 }
0471
0472
0473
0474
0475
0476
0477
0478 virtual bool usesPhysRegsForValues() const { return true; }
0479
0480
0481
0482 virtual bool useIPRA() const { return false; }
0483
0484
0485
0486 virtual int unqualifiedInlineAsmVariant() const { return 0; }
0487
0488
0489 virtual void registerMachineRegisterInfoCallback(MachineFunction &MF) const {}
0490 };
0491
0492 }
0493
0494 #endif