Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 1 | //===- MCSubtargetInfo.cpp - Subtarget Information ------------------------===// |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 10 | #include "llvm/MC/MCSubtargetInfo.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 11 | #include "llvm/ADT/ArrayRef.h" |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 12 | #include "llvm/ADT/StringRef.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 13 | #include "llvm/MC/MCInstrItineraries.h" |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCSchedule.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/MC/SubtargetFeature.h" |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
| 17 | #include <algorithm> |
Eugene Zelenko | 498b48e | 2017-02-08 22:23:19 +0000 | [diff] [blame] | 18 | #include <cassert> |
| 19 | #include <cstring> |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Duncan P. N. Exon Smith | c9978ba | 2015-07-10 22:52:15 +0000 | [diff] [blame] | 23 | static FeatureBitset getFeatures(StringRef CPU, StringRef FS, |
| 24 | ArrayRef<SubtargetFeatureKV> ProcDesc, |
| 25 | ArrayRef<SubtargetFeatureKV> ProcFeatures) { |
Andrew Trick | 34aadd6 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 26 | SubtargetFeatures Features(FS); |
Duncan P. N. Exon Smith | c9978ba | 2015-07-10 22:52:15 +0000 | [diff] [blame] | 27 | return Features.getFeatureBits(CPU, ProcDesc, ProcFeatures); |
| 28 | } |
| 29 | |
| 30 | void MCSubtargetInfo::InitMCProcessorInfo(StringRef CPU, StringRef FS) { |
| 31 | FeatureBits = getFeatures(CPU, FS, ProcDesc, ProcFeatures); |
Andrew Trick | 34aadd6 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 32 | if (!CPU.empty()) |
Duncan P. N. Exon Smith | d819fac | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 33 | CPUSchedModel = &getSchedModelForCPU(CPU); |
Andrew Trick | 34aadd6 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 34 | else |
Duncan P. N. Exon Smith | d819fac | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 35 | CPUSchedModel = &MCSchedModel::GetDefaultSchedModel(); |
Andrew Trick | 34aadd6 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Bradley Smith | 7c1b772 | 2015-11-16 11:10:19 +0000 | [diff] [blame] | 38 | void MCSubtargetInfo::setDefaultFeatures(StringRef CPU, StringRef FS) { |
| 39 | FeatureBits = getFeatures(CPU, FS, ProcDesc, ProcFeatures); |
Duncan P. N. Exon Smith | c9978ba | 2015-07-10 22:52:15 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Duncan P. N. Exon Smith | 16859aa | 2015-07-10 22:43:42 +0000 | [diff] [blame] | 42 | MCSubtargetInfo::MCSubtargetInfo( |
Daniel Sanders | 47b167d | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 43 | const Triple &TT, StringRef C, StringRef FS, |
Daniel Sanders | 4d13f31 | 2015-06-10 12:11:26 +0000 | [diff] [blame] | 44 | ArrayRef<SubtargetFeatureKV> PF, ArrayRef<SubtargetFeatureKV> PD, |
| 45 | const SubtargetInfoKV *ProcSched, const MCWriteProcResEntry *WPR, |
| 46 | const MCWriteLatencyEntry *WL, const MCReadAdvanceEntry *RA, |
Duncan P. N. Exon Smith | 16859aa | 2015-07-10 22:43:42 +0000 | [diff] [blame] | 47 | const InstrStage *IS, const unsigned *OC, const unsigned *FP) |
Daniel Sanders | 47b167d | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 48 | : TargetTriple(TT), CPU(C), ProcFeatures(PF), ProcDesc(PD), |
Duncan P. N. Exon Smith | 16859aa | 2015-07-10 22:43:42 +0000 | [diff] [blame] | 49 | ProcSchedModels(ProcSched), WriteProcResTable(WPR), WriteLatencyTable(WL), |
| 50 | ReadAdvanceTable(RA), Stages(IS), OperandCycles(OC), ForwardingPaths(FP) { |
Andrew Trick | 34aadd6 | 2012-09-18 05:33:15 +0000 | [diff] [blame] | 51 | InitMCProcessorInfo(CPU, FS); |
Evan Cheng | 0ddff1b | 2011-07-07 07:07:08 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Michael Kuperstein | d714fcf | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 54 | FeatureBitset MCSubtargetInfo::ToggleFeature(uint64_t FB) { |
| 55 | FeatureBits.flip(FB); |
| 56 | return FeatureBits; |
| 57 | } |
| 58 | |
| 59 | FeatureBitset MCSubtargetInfo::ToggleFeature(const FeatureBitset &FB) { |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 60 | FeatureBits ^= FB; |
| 61 | return FeatureBits; |
| 62 | } |
| 63 | |
Michael Kuperstein | d714fcf | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 64 | FeatureBitset MCSubtargetInfo::ToggleFeature(StringRef FS) { |
Artyom Skrobov | 176a9b2a | 2016-01-05 10:25:56 +0000 | [diff] [blame] | 65 | SubtargetFeatures::ToggleFeature(FeatureBits, FS, ProcFeatures); |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 66 | return FeatureBits; |
| 67 | } |
| 68 | |
John Brawn | c1c9bc1 | 2015-06-05 13:29:24 +0000 | [diff] [blame] | 69 | FeatureBitset MCSubtargetInfo::ApplyFeatureFlag(StringRef FS) { |
Artyom Skrobov | 176a9b2a | 2016-01-05 10:25:56 +0000 | [diff] [blame] | 70 | SubtargetFeatures::ApplyFeatureFlag(FeatureBits, FS, ProcFeatures); |
John Brawn | c1c9bc1 | 2015-06-05 13:29:24 +0000 | [diff] [blame] | 71 | return FeatureBits; |
| 72 | } |
Evan Cheng | ffc0e73 | 2011-07-09 05:47:46 +0000 | [diff] [blame] | 73 | |
Krzysztof Parzyszek | c84b859 | 2017-09-14 20:44:20 +0000 | [diff] [blame] | 74 | bool MCSubtargetInfo::checkFeatures(StringRef FS) const { |
| 75 | SubtargetFeatures T(FS); |
| 76 | FeatureBitset Set, All; |
| 77 | for (std::string F : T.getFeatures()) { |
| 78 | SubtargetFeatures::ApplyFeatureFlag(Set, F, ProcFeatures); |
| 79 | if (F[0] == '-') |
| 80 | F[0] = '+'; |
| 81 | SubtargetFeatures::ApplyFeatureFlag(All, F, ProcFeatures); |
| 82 | } |
| 83 | return (FeatureBits & All) == Set; |
| 84 | } |
| 85 | |
Duncan P. N. Exon Smith | d819fac | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 86 | const MCSchedModel &MCSubtargetInfo::getSchedModelForCPU(StringRef CPU) const { |
Andrew Trick | 72d048b | 2012-09-14 20:26:41 +0000 | [diff] [blame] | 87 | assert(ProcSchedModels && "Processor machine model not available!"); |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 88 | |
Craig Topper | 2e08513 | 2016-01-03 08:45:36 +0000 | [diff] [blame] | 89 | ArrayRef<SubtargetInfoKV> SchedModels(ProcSchedModels, ProcDesc.size()); |
| 90 | |
| 91 | assert(std::is_sorted(SchedModels.begin(), SchedModels.end(), |
Craig Topper | 8d6385d | 2015-10-17 16:37:11 +0000 | [diff] [blame] | 92 | [](const SubtargetInfoKV &LHS, const SubtargetInfoKV &RHS) { |
| 93 | return strcmp(LHS.Key, RHS.Key) < 0; |
| 94 | }) && |
| 95 | "Processor machine model table is not sorted"); |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 96 | |
| 97 | // Find entry |
Craig Topper | 2e08513 | 2016-01-03 08:45:36 +0000 | [diff] [blame] | 98 | auto Found = |
| 99 | std::lower_bound(SchedModels.begin(), SchedModels.end(), CPU); |
| 100 | if (Found == SchedModels.end() || StringRef(Found->Key) != CPU) { |
Craig Topper | 0a4630d | 2015-04-02 04:27:50 +0000 | [diff] [blame] | 101 | if (CPU != "help") // Don't error if the user asked for help. |
| 102 | errs() << "'" << CPU |
| 103 | << "' is not a recognized processor for this target" |
| 104 | << " (ignoring processor)\n"; |
Pete Cooper | 6de6c6a | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 105 | return MCSchedModel::GetDefaultSchedModel(); |
Artyom Skrobov | 85ae034 | 2014-01-25 16:56:18 +0000 | [diff] [blame] | 106 | } |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 107 | assert(Found->Value && "Missing processor SchedModel value"); |
Pete Cooper | 6de6c6a | 2014-09-02 17:43:54 +0000 | [diff] [blame] | 108 | return *(const MCSchedModel *)Found->Value; |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 109 | } |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 110 | |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 111 | InstrItineraryData |
| 112 | MCSubtargetInfo::getInstrItineraryForCPU(StringRef CPU) const { |
Krzysztof Parzyszek | 90ca67c | 2017-09-27 12:48:48 +0000 | [diff] [blame] | 113 | const MCSchedModel &SchedModel = getSchedModelForCPU(CPU); |
Andrew Trick | 2661b41 | 2012-07-07 04:00:00 +0000 | [diff] [blame] | 114 | return InstrItineraryData(SchedModel, Stages, OperandCycles, ForwardingPaths); |
Evan Cheng | 9421470 | 2011-07-01 20:45:01 +0000 | [diff] [blame] | 115 | } |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 116 | |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 117 | void MCSubtargetInfo::initInstrItins(InstrItineraryData &InstrItins) const { |
Duncan P. N. Exon Smith | d819fac | 2015-07-10 22:13:43 +0000 | [diff] [blame] | 118 | InstrItins = InstrItineraryData(getSchedModel(), Stages, OperandCycles, |
| 119 | ForwardingPaths); |
Andrew Trick | 99ab6c6 | 2012-09-14 20:26:46 +0000 | [diff] [blame] | 120 | } |