1f4a2713aSLionel Sambuc //===-- MCSubtargetInfo.cpp - Subtarget Information -----------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc
10f4a2713aSLionel Sambuc #include "llvm/MC/MCSubtargetInfo.h"
11f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h"
12f4a2713aSLionel Sambuc #include "llvm/ADT/Triple.h"
13f4a2713aSLionel Sambuc #include "llvm/MC/MCInstrItineraries.h"
14f4a2713aSLionel Sambuc #include "llvm/MC/SubtargetFeature.h"
15f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
16f4a2713aSLionel Sambuc #include <algorithm>
17f4a2713aSLionel Sambuc
18f4a2713aSLionel Sambuc using namespace llvm;
19f4a2713aSLionel Sambuc
20f4a2713aSLionel Sambuc /// InitMCProcessorInfo - Set or change the CPU (optionally supplemented
21f4a2713aSLionel Sambuc /// with feature string). Recompute feature bits and scheduling model.
22f4a2713aSLionel Sambuc void
InitMCProcessorInfo(StringRef CPU,StringRef FS)23f4a2713aSLionel Sambuc MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef FS) {
24f4a2713aSLionel Sambuc SubtargetFeatures Features(FS);
25*0a6a1f1dSLionel Sambuc FeatureBits = Features.getFeatureBits(CPU, ProcDesc, ProcFeatures);
26f4a2713aSLionel Sambuc InitCPUSchedModel(CPU);
27f4a2713aSLionel Sambuc }
28f4a2713aSLionel Sambuc
29f4a2713aSLionel Sambuc void
InitCPUSchedModel(StringRef CPU)30f4a2713aSLionel Sambuc MCSubtargetInfo::InitCPUSchedModel(StringRef CPU) {
31f4a2713aSLionel Sambuc if (!CPU.empty())
32f4a2713aSLionel Sambuc CPUSchedModel = getSchedModelForCPU(CPU);
33f4a2713aSLionel Sambuc else
34*0a6a1f1dSLionel Sambuc CPUSchedModel = MCSchedModel::GetDefaultSchedModel();
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc
37f4a2713aSLionel Sambuc void
InitMCSubtargetInfo(StringRef TT,StringRef CPU,StringRef FS,ArrayRef<SubtargetFeatureKV> PF,ArrayRef<SubtargetFeatureKV> PD,const SubtargetInfoKV * ProcSched,const MCWriteProcResEntry * WPR,const MCWriteLatencyEntry * WL,const MCReadAdvanceEntry * RA,const InstrStage * IS,const unsigned * OC,const unsigned * FP)38f4a2713aSLionel Sambuc MCSubtargetInfo::InitMCSubtargetInfo(StringRef TT, StringRef CPU, StringRef FS,
39*0a6a1f1dSLionel Sambuc ArrayRef<SubtargetFeatureKV> PF,
40*0a6a1f1dSLionel Sambuc ArrayRef<SubtargetFeatureKV> PD,
41f4a2713aSLionel Sambuc const SubtargetInfoKV *ProcSched,
42f4a2713aSLionel Sambuc const MCWriteProcResEntry *WPR,
43f4a2713aSLionel Sambuc const MCWriteLatencyEntry *WL,
44f4a2713aSLionel Sambuc const MCReadAdvanceEntry *RA,
45f4a2713aSLionel Sambuc const InstrStage *IS,
46f4a2713aSLionel Sambuc const unsigned *OC,
47*0a6a1f1dSLionel Sambuc const unsigned *FP) {
48f4a2713aSLionel Sambuc TargetTriple = TT;
49f4a2713aSLionel Sambuc ProcFeatures = PF;
50f4a2713aSLionel Sambuc ProcDesc = PD;
51f4a2713aSLionel Sambuc ProcSchedModels = ProcSched;
52f4a2713aSLionel Sambuc WriteProcResTable = WPR;
53f4a2713aSLionel Sambuc WriteLatencyTable = WL;
54f4a2713aSLionel Sambuc ReadAdvanceTable = RA;
55f4a2713aSLionel Sambuc
56f4a2713aSLionel Sambuc Stages = IS;
57f4a2713aSLionel Sambuc OperandCycles = OC;
58f4a2713aSLionel Sambuc ForwardingPaths = FP;
59f4a2713aSLionel Sambuc
60f4a2713aSLionel Sambuc InitMCProcessorInfo(CPU, FS);
61f4a2713aSLionel Sambuc }
62f4a2713aSLionel Sambuc
63f4a2713aSLionel Sambuc /// ToggleFeature - Toggle a feature and returns the re-computed feature
64f4a2713aSLionel Sambuc /// bits. This version does not change the implied bits.
ToggleFeature(uint64_t FB)65f4a2713aSLionel Sambuc uint64_t MCSubtargetInfo::ToggleFeature(uint64_t FB) {
66f4a2713aSLionel Sambuc FeatureBits ^= FB;
67f4a2713aSLionel Sambuc return FeatureBits;
68f4a2713aSLionel Sambuc }
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc /// ToggleFeature - Toggle a feature and returns the re-computed feature
71f4a2713aSLionel Sambuc /// bits. This version will also change all implied bits.
ToggleFeature(StringRef FS)72f4a2713aSLionel Sambuc uint64_t MCSubtargetInfo::ToggleFeature(StringRef FS) {
73f4a2713aSLionel Sambuc SubtargetFeatures Features;
74*0a6a1f1dSLionel Sambuc FeatureBits = Features.ToggleFeature(FeatureBits, FS, ProcFeatures);
75f4a2713aSLionel Sambuc return FeatureBits;
76f4a2713aSLionel Sambuc }
77f4a2713aSLionel Sambuc
78f4a2713aSLionel Sambuc
79*0a6a1f1dSLionel Sambuc MCSchedModel
getSchedModelForCPU(StringRef CPU) const80f4a2713aSLionel Sambuc MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const {
81f4a2713aSLionel Sambuc assert(ProcSchedModels && "Processor machine model not available!");
82f4a2713aSLionel Sambuc
83*0a6a1f1dSLionel Sambuc unsigned NumProcs = ProcDesc.size();
84f4a2713aSLionel Sambuc #ifndef NDEBUG
85f4a2713aSLionel Sambuc for (size_t i = 1; i < NumProcs; i++) {
86f4a2713aSLionel Sambuc assert(strcmp(ProcSchedModels[i - 1].Key, ProcSchedModels[i].Key) < 0 &&
87f4a2713aSLionel Sambuc "Processor machine model table is not sorted");
88f4a2713aSLionel Sambuc }
89f4a2713aSLionel Sambuc #endif
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc // Find entry
92f4a2713aSLionel Sambuc const SubtargetInfoKV *Found =
93f4a2713aSLionel Sambuc std::lower_bound(ProcSchedModels, ProcSchedModels+NumProcs, CPU);
94f4a2713aSLionel Sambuc if (Found == ProcSchedModels+NumProcs || StringRef(Found->Key) != CPU) {
95f4a2713aSLionel Sambuc errs() << "'" << CPU
96f4a2713aSLionel Sambuc << "' is not a recognized processor for this target"
97f4a2713aSLionel Sambuc << " (ignoring processor)\n";
98*0a6a1f1dSLionel Sambuc return MCSchedModel::GetDefaultSchedModel();
99f4a2713aSLionel Sambuc }
100f4a2713aSLionel Sambuc assert(Found->Value && "Missing processor SchedModel value");
101*0a6a1f1dSLionel Sambuc return *(const MCSchedModel *)Found->Value;
102f4a2713aSLionel Sambuc }
103f4a2713aSLionel Sambuc
104f4a2713aSLionel Sambuc InstrItineraryData
getInstrItineraryForCPU(StringRef CPU) const105f4a2713aSLionel Sambuc MCSubtargetInfo::getInstrItineraryForCPU(StringRef CPU) const {
106*0a6a1f1dSLionel Sambuc const MCSchedModel SchedModel = getSchedModelForCPU(CPU);
107f4a2713aSLionel Sambuc return InstrItineraryData(SchedModel, Stages, OperandCycles, ForwardingPaths);
108f4a2713aSLionel Sambuc }
109f4a2713aSLionel Sambuc
110f4a2713aSLionel Sambuc /// Initialize an InstrItineraryData instance.
initInstrItins(InstrItineraryData & InstrItins) const111f4a2713aSLionel Sambuc void MCSubtargetInfo::initInstrItins(InstrItineraryData &InstrItins) const {
112f4a2713aSLionel Sambuc InstrItins =
113f4a2713aSLionel Sambuc InstrItineraryData(CPUSchedModel, Stages, OperandCycles, ForwardingPaths);
114f4a2713aSLionel Sambuc }
115