File: | include/llvm/Support/Error.h |
Warning: | line 200, column 5 Potential leak of memory pointed to by 'Payload._M_t._M_head_impl' |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- GlobalISelEmitter.cpp - Generate an instruction selector -----------===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | /// \file | |||
10 | /// This tablegen backend emits code for use by the GlobalISel instruction | |||
11 | /// selector. See include/llvm/CodeGen/TargetGlobalISel.td. | |||
12 | /// | |||
13 | /// This file analyzes the patterns recognized by the SelectionDAGISel tablegen | |||
14 | /// backend, filters out the ones that are unsupported, maps | |||
15 | /// SelectionDAG-specific constructs to their GlobalISel counterpart | |||
16 | /// (when applicable: MVT to LLT; SDNode to generic Instruction). | |||
17 | /// | |||
18 | /// Not all patterns are supported: pass the tablegen invocation | |||
19 | /// "-warn-on-skipped-patterns" to emit a warning when a pattern is skipped, | |||
20 | /// as well as why. | |||
21 | /// | |||
22 | /// The generated file defines a single method: | |||
23 | /// bool <Target>InstructionSelector::selectImpl(MachineInstr &I) const; | |||
24 | /// intended to be used in InstructionSelector::select as the first-step | |||
25 | /// selector for the patterns that don't require complex C++. | |||
26 | /// | |||
27 | /// FIXME: We'll probably want to eventually define a base | |||
28 | /// "TargetGenInstructionSelector" class. | |||
29 | /// | |||
30 | //===----------------------------------------------------------------------===// | |||
31 | ||||
32 | #include "CodeGenDAGPatterns.h" | |||
33 | #include "SubtargetFeatureInfo.h" | |||
34 | #include "llvm/ADT/Optional.h" | |||
35 | #include "llvm/ADT/SmallSet.h" | |||
36 | #include "llvm/ADT/Statistic.h" | |||
37 | #include "llvm/Support/CodeGenCoverage.h" | |||
38 | #include "llvm/Support/CommandLine.h" | |||
39 | #include "llvm/Support/Error.h" | |||
40 | #include "llvm/Support/LowLevelTypeImpl.h" | |||
41 | #include "llvm/Support/MachineValueType.h" | |||
42 | #include "llvm/Support/ScopedPrinter.h" | |||
43 | #include "llvm/TableGen/Error.h" | |||
44 | #include "llvm/TableGen/Record.h" | |||
45 | #include "llvm/TableGen/TableGenBackend.h" | |||
46 | #include <numeric> | |||
47 | #include <string> | |||
48 | using namespace llvm; | |||
49 | ||||
50 | #define DEBUG_TYPE"gisel-emitter" "gisel-emitter" | |||
51 | ||||
52 | STATISTIC(NumPatternTotal, "Total number of patterns")static llvm::Statistic NumPatternTotal = {"gisel-emitter", "NumPatternTotal" , "Total number of patterns", {0}, {false}}; | |||
53 | STATISTIC(NumPatternImported, "Number of patterns imported from SelectionDAG")static llvm::Statistic NumPatternImported = {"gisel-emitter", "NumPatternImported", "Number of patterns imported from SelectionDAG" , {0}, {false}}; | |||
54 | STATISTIC(NumPatternImportsSkipped, "Number of SelectionDAG imports skipped")static llvm::Statistic NumPatternImportsSkipped = {"gisel-emitter" , "NumPatternImportsSkipped", "Number of SelectionDAG imports skipped" , {0}, {false}}; | |||
55 | STATISTIC(NumPatternsTested, "Number of patterns executed according to coverage information")static llvm::Statistic NumPatternsTested = {"gisel-emitter", "NumPatternsTested" , "Number of patterns executed according to coverage information" , {0}, {false}}; | |||
56 | STATISTIC(NumPatternEmitted, "Number of patterns emitted")static llvm::Statistic NumPatternEmitted = {"gisel-emitter", "NumPatternEmitted" , "Number of patterns emitted", {0}, {false}}; | |||
57 | ||||
58 | cl::OptionCategory GlobalISelEmitterCat("Options for -gen-global-isel"); | |||
59 | ||||
60 | static cl::opt<bool> WarnOnSkippedPatterns( | |||
61 | "warn-on-skipped-patterns", | |||
62 | cl::desc("Explain why a pattern was skipped for inclusion " | |||
63 | "in the GlobalISel selector"), | |||
64 | cl::init(false), cl::cat(GlobalISelEmitterCat)); | |||
65 | ||||
66 | static cl::opt<bool> GenerateCoverage( | |||
67 | "instrument-gisel-coverage", | |||
68 | cl::desc("Generate coverage instrumentation for GlobalISel"), | |||
69 | cl::init(false), cl::cat(GlobalISelEmitterCat)); | |||
70 | ||||
71 | static cl::opt<std::string> UseCoverageFile( | |||
72 | "gisel-coverage-file", cl::init(""), | |||
73 | cl::desc("Specify file to retrieve coverage information from"), | |||
74 | cl::cat(GlobalISelEmitterCat)); | |||
75 | ||||
76 | static cl::opt<bool> OptimizeMatchTable( | |||
77 | "optimize-match-table", | |||
78 | cl::desc("Generate an optimized version of the match table"), | |||
79 | cl::init(true), cl::cat(GlobalISelEmitterCat)); | |||
80 | ||||
81 | namespace { | |||
82 | //===- Helper functions ---------------------------------------------------===// | |||
83 | ||||
84 | /// Get the name of the enum value used to number the predicate function. | |||
85 | std::string getEnumNameForPredicate(const TreePredicateFn &Predicate) { | |||
86 | if (Predicate.hasGISelPredicateCode()) | |||
87 | return "GIPFP_MI_" + Predicate.getFnName(); | |||
88 | return "GIPFP_" + Predicate.getImmTypeIdentifier().str() + "_" + | |||
89 | Predicate.getFnName(); | |||
90 | } | |||
91 | ||||
92 | /// Get the opcode used to check this predicate. | |||
93 | std::string getMatchOpcodeForPredicate(const TreePredicateFn &Predicate) { | |||
94 | return "GIM_Check" + Predicate.getImmTypeIdentifier().str() + "ImmPredicate"; | |||
95 | } | |||
96 | ||||
97 | /// This class stands in for LLT wherever we want to tablegen-erate an | |||
98 | /// equivalent at compiler run-time. | |||
99 | class LLTCodeGen { | |||
100 | private: | |||
101 | LLT Ty; | |||
102 | ||||
103 | public: | |||
104 | LLTCodeGen() = default; | |||
105 | LLTCodeGen(const LLT &Ty) : Ty(Ty) {} | |||
106 | ||||
107 | std::string getCxxEnumValue() const { | |||
108 | std::string Str; | |||
109 | raw_string_ostream OS(Str); | |||
110 | ||||
111 | emitCxxEnumValue(OS); | |||
112 | return OS.str(); | |||
113 | } | |||
114 | ||||
115 | void emitCxxEnumValue(raw_ostream &OS) const { | |||
116 | if (Ty.isScalar()) { | |||
117 | OS << "GILLT_s" << Ty.getSizeInBits(); | |||
118 | return; | |||
119 | } | |||
120 | if (Ty.isVector()) { | |||
121 | OS << "GILLT_v" << Ty.getNumElements() << "s" << Ty.getScalarSizeInBits(); | |||
122 | return; | |||
123 | } | |||
124 | if (Ty.isPointer()) { | |||
125 | OS << "GILLT_p" << Ty.getAddressSpace(); | |||
126 | if (Ty.getSizeInBits() > 0) | |||
127 | OS << "s" << Ty.getSizeInBits(); | |||
128 | return; | |||
129 | } | |||
130 | llvm_unreachable("Unhandled LLT")::llvm::llvm_unreachable_internal("Unhandled LLT", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 130); | |||
131 | } | |||
132 | ||||
133 | void emitCxxConstructorCall(raw_ostream &OS) const { | |||
134 | if (Ty.isScalar()) { | |||
135 | OS << "LLT::scalar(" << Ty.getSizeInBits() << ")"; | |||
136 | return; | |||
137 | } | |||
138 | if (Ty.isVector()) { | |||
139 | OS << "LLT::vector(" << Ty.getNumElements() << ", " | |||
140 | << Ty.getScalarSizeInBits() << ")"; | |||
141 | return; | |||
142 | } | |||
143 | if (Ty.isPointer() && Ty.getSizeInBits() > 0) { | |||
144 | OS << "LLT::pointer(" << Ty.getAddressSpace() << ", " | |||
145 | << Ty.getSizeInBits() << ")"; | |||
146 | return; | |||
147 | } | |||
148 | llvm_unreachable("Unhandled LLT")::llvm::llvm_unreachable_internal("Unhandled LLT", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 148); | |||
149 | } | |||
150 | ||||
151 | const LLT &get() const { return Ty; } | |||
152 | ||||
153 | /// This ordering is used for std::unique() and llvm::sort(). There's no | |||
154 | /// particular logic behind the order but either A < B or B < A must be | |||
155 | /// true if A != B. | |||
156 | bool operator<(const LLTCodeGen &Other) const { | |||
157 | if (Ty.isValid() != Other.Ty.isValid()) | |||
158 | return Ty.isValid() < Other.Ty.isValid(); | |||
159 | if (!Ty.isValid()) | |||
160 | return false; | |||
161 | ||||
162 | if (Ty.isVector() != Other.Ty.isVector()) | |||
163 | return Ty.isVector() < Other.Ty.isVector(); | |||
164 | if (Ty.isScalar() != Other.Ty.isScalar()) | |||
165 | return Ty.isScalar() < Other.Ty.isScalar(); | |||
166 | if (Ty.isPointer() != Other.Ty.isPointer()) | |||
167 | return Ty.isPointer() < Other.Ty.isPointer(); | |||
168 | ||||
169 | if (Ty.isPointer() && Ty.getAddressSpace() != Other.Ty.getAddressSpace()) | |||
170 | return Ty.getAddressSpace() < Other.Ty.getAddressSpace(); | |||
171 | ||||
172 | if (Ty.isVector() && Ty.getNumElements() != Other.Ty.getNumElements()) | |||
173 | return Ty.getNumElements() < Other.Ty.getNumElements(); | |||
174 | ||||
175 | return Ty.getSizeInBits() < Other.Ty.getSizeInBits(); | |||
176 | } | |||
177 | ||||
178 | bool operator==(const LLTCodeGen &B) const { return Ty == B.Ty; } | |||
179 | }; | |||
180 | ||||
181 | // Track all types that are used so we can emit the corresponding enum. | |||
182 | std::set<LLTCodeGen> KnownTypes; | |||
183 | ||||
184 | class InstructionMatcher; | |||
185 | /// Convert an MVT to an equivalent LLT if possible, or the invalid LLT() for | |||
186 | /// MVTs that don't map cleanly to an LLT (e.g., iPTR, *any, ...). | |||
187 | static Optional<LLTCodeGen> MVTToLLT(MVT::SimpleValueType SVT) { | |||
188 | MVT VT(SVT); | |||
189 | ||||
190 | if (VT.isVector() && VT.getVectorNumElements() != 1) | |||
191 | return LLTCodeGen( | |||
192 | LLT::vector(VT.getVectorNumElements(), VT.getScalarSizeInBits())); | |||
193 | ||||
194 | if (VT.isInteger() || VT.isFloatingPoint()) | |||
195 | return LLTCodeGen(LLT::scalar(VT.getSizeInBits())); | |||
196 | return None; | |||
197 | } | |||
198 | ||||
199 | static std::string explainPredicates(const TreePatternNode *N) { | |||
200 | std::string Explanation = ""; | |||
201 | StringRef Separator = ""; | |||
202 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { | |||
203 | const TreePredicateFn &P = Call.Fn; | |||
204 | Explanation += | |||
205 | (Separator + P.getOrigPatFragRecord()->getRecord()->getName()).str(); | |||
206 | Separator = ", "; | |||
207 | ||||
208 | if (P.isAlwaysTrue()) | |||
209 | Explanation += " always-true"; | |||
210 | if (P.isImmediatePattern()) | |||
211 | Explanation += " immediate"; | |||
212 | ||||
213 | if (P.isUnindexed()) | |||
214 | Explanation += " unindexed"; | |||
215 | ||||
216 | if (P.isNonExtLoad()) | |||
217 | Explanation += " non-extload"; | |||
218 | if (P.isAnyExtLoad()) | |||
219 | Explanation += " extload"; | |||
220 | if (P.isSignExtLoad()) | |||
221 | Explanation += " sextload"; | |||
222 | if (P.isZeroExtLoad()) | |||
223 | Explanation += " zextload"; | |||
224 | ||||
225 | if (P.isNonTruncStore()) | |||
226 | Explanation += " non-truncstore"; | |||
227 | if (P.isTruncStore()) | |||
228 | Explanation += " truncstore"; | |||
229 | ||||
230 | if (Record *VT = P.getMemoryVT()) | |||
231 | Explanation += (" MemVT=" + VT->getName()).str(); | |||
232 | if (Record *VT = P.getScalarMemoryVT()) | |||
233 | Explanation += (" ScalarVT(MemVT)=" + VT->getName()).str(); | |||
234 | ||||
235 | if (P.isAtomicOrderingMonotonic()) | |||
236 | Explanation += " monotonic"; | |||
237 | if (P.isAtomicOrderingAcquire()) | |||
238 | Explanation += " acquire"; | |||
239 | if (P.isAtomicOrderingRelease()) | |||
240 | Explanation += " release"; | |||
241 | if (P.isAtomicOrderingAcquireRelease()) | |||
242 | Explanation += " acq_rel"; | |||
243 | if (P.isAtomicOrderingSequentiallyConsistent()) | |||
244 | Explanation += " seq_cst"; | |||
245 | if (P.isAtomicOrderingAcquireOrStronger()) | |||
246 | Explanation += " >=acquire"; | |||
247 | if (P.isAtomicOrderingWeakerThanAcquire()) | |||
248 | Explanation += " <acquire"; | |||
249 | if (P.isAtomicOrderingReleaseOrStronger()) | |||
250 | Explanation += " >=release"; | |||
251 | if (P.isAtomicOrderingWeakerThanRelease()) | |||
252 | Explanation += " <release"; | |||
253 | } | |||
254 | return Explanation; | |||
255 | } | |||
256 | ||||
257 | std::string explainOperator(Record *Operator) { | |||
258 | if (Operator->isSubClassOf("SDNode")) | |||
259 | return (" (" + Operator->getValueAsString("Opcode") + ")").str(); | |||
260 | ||||
261 | if (Operator->isSubClassOf("Intrinsic")) | |||
262 | return (" (Operator is an Intrinsic, " + Operator->getName() + ")").str(); | |||
263 | ||||
264 | if (Operator->isSubClassOf("ComplexPattern")) | |||
265 | return (" (Operator is an unmapped ComplexPattern, " + Operator->getName() + | |||
266 | ")") | |||
267 | .str(); | |||
268 | ||||
269 | if (Operator->isSubClassOf("SDNodeXForm")) | |||
270 | return (" (Operator is an unmapped SDNodeXForm, " + Operator->getName() + | |||
271 | ")") | |||
272 | .str(); | |||
273 | ||||
274 | return (" (Operator " + Operator->getName() + " not understood)").str(); | |||
275 | } | |||
276 | ||||
277 | /// Helper function to let the emitter report skip reason error messages. | |||
278 | static Error failedImport(const Twine &Reason) { | |||
279 | return make_error<StringError>(Reason, inconvertibleErrorCode()); | |||
280 | } | |||
281 | ||||
282 | static Error isTrivialOperatorNode(const TreePatternNode *N) { | |||
283 | std::string Explanation = ""; | |||
284 | std::string Separator = ""; | |||
285 | ||||
286 | bool HasUnsupportedPredicate = false; | |||
287 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { | |||
288 | const TreePredicateFn &Predicate = Call.Fn; | |||
289 | ||||
290 | if (Predicate.isAlwaysTrue()) | |||
291 | continue; | |||
292 | ||||
293 | if (Predicate.isImmediatePattern()) | |||
294 | continue; | |||
295 | ||||
296 | if (Predicate.isNonExtLoad() || Predicate.isAnyExtLoad() || | |||
297 | Predicate.isSignExtLoad() || Predicate.isZeroExtLoad()) | |||
298 | continue; | |||
299 | ||||
300 | if (Predicate.isNonTruncStore()) | |||
301 | continue; | |||
302 | ||||
303 | if (Predicate.isLoad() && Predicate.getMemoryVT()) | |||
304 | continue; | |||
305 | ||||
306 | if (Predicate.isLoad() || Predicate.isStore()) { | |||
307 | if (Predicate.isUnindexed()) | |||
308 | continue; | |||
309 | } | |||
310 | ||||
311 | if (Predicate.isAtomic() && Predicate.getMemoryVT()) | |||
312 | continue; | |||
313 | ||||
314 | if (Predicate.isAtomic() && | |||
315 | (Predicate.isAtomicOrderingMonotonic() || | |||
316 | Predicate.isAtomicOrderingAcquire() || | |||
317 | Predicate.isAtomicOrderingRelease() || | |||
318 | Predicate.isAtomicOrderingAcquireRelease() || | |||
319 | Predicate.isAtomicOrderingSequentiallyConsistent() || | |||
320 | Predicate.isAtomicOrderingAcquireOrStronger() || | |||
321 | Predicate.isAtomicOrderingWeakerThanAcquire() || | |||
322 | Predicate.isAtomicOrderingReleaseOrStronger() || | |||
323 | Predicate.isAtomicOrderingWeakerThanRelease())) | |||
324 | continue; | |||
325 | ||||
326 | if (Predicate.hasGISelPredicateCode()) | |||
327 | continue; | |||
328 | ||||
329 | HasUnsupportedPredicate = true; | |||
330 | Explanation = Separator + "Has a predicate (" + explainPredicates(N) + ")"; | |||
331 | Separator = ", "; | |||
332 | Explanation += (Separator + "first-failing:" + | |||
333 | Predicate.getOrigPatFragRecord()->getRecord()->getName()) | |||
334 | .str(); | |||
335 | break; | |||
336 | } | |||
337 | ||||
338 | if (!HasUnsupportedPredicate) | |||
339 | return Error::success(); | |||
340 | ||||
341 | return failedImport(Explanation); | |||
342 | } | |||
343 | ||||
344 | static Record *getInitValueAsRegClass(Init *V) { | |||
345 | if (DefInit *VDefInit = dyn_cast<DefInit>(V)) { | |||
346 | if (VDefInit->getDef()->isSubClassOf("RegisterOperand")) | |||
347 | return VDefInit->getDef()->getValueAsDef("RegClass"); | |||
348 | if (VDefInit->getDef()->isSubClassOf("RegisterClass")) | |||
349 | return VDefInit->getDef(); | |||
350 | } | |||
351 | return nullptr; | |||
352 | } | |||
353 | ||||
354 | std::string | |||
355 | getNameForFeatureBitset(const std::vector<Record *> &FeatureBitset) { | |||
356 | std::string Name = "GIFBS"; | |||
357 | for (const auto &Feature : FeatureBitset) | |||
358 | Name += ("_" + Feature->getName()).str(); | |||
359 | return Name; | |||
360 | } | |||
361 | ||||
362 | //===- MatchTable Helpers -------------------------------------------------===// | |||
363 | ||||
364 | class MatchTable; | |||
365 | ||||
366 | /// A record to be stored in a MatchTable. | |||
367 | /// | |||
368 | /// This class represents any and all output that may be required to emit the | |||
369 | /// MatchTable. Instances are most often configured to represent an opcode or | |||
370 | /// value that will be emitted to the table with some formatting but it can also | |||
371 | /// represent commas, comments, and other formatting instructions. | |||
372 | struct MatchTableRecord { | |||
373 | enum RecordFlagsBits { | |||
374 | MTRF_None = 0x0, | |||
375 | /// Causes EmitStr to be formatted as comment when emitted. | |||
376 | MTRF_Comment = 0x1, | |||
377 | /// Causes the record value to be followed by a comma when emitted. | |||
378 | MTRF_CommaFollows = 0x2, | |||
379 | /// Causes the record value to be followed by a line break when emitted. | |||
380 | MTRF_LineBreakFollows = 0x4, | |||
381 | /// Indicates that the record defines a label and causes an additional | |||
382 | /// comment to be emitted containing the index of the label. | |||
383 | MTRF_Label = 0x8, | |||
384 | /// Causes the record to be emitted as the index of the label specified by | |||
385 | /// LabelID along with a comment indicating where that label is. | |||
386 | MTRF_JumpTarget = 0x10, | |||
387 | /// Causes the formatter to add a level of indentation before emitting the | |||
388 | /// record. | |||
389 | MTRF_Indent = 0x20, | |||
390 | /// Causes the formatter to remove a level of indentation after emitting the | |||
391 | /// record. | |||
392 | MTRF_Outdent = 0x40, | |||
393 | }; | |||
394 | ||||
395 | /// When MTRF_Label or MTRF_JumpTarget is used, indicates a label id to | |||
396 | /// reference or define. | |||
397 | unsigned LabelID; | |||
398 | /// The string to emit. Depending on the MTRF_* flags it may be a comment, a | |||
399 | /// value, a label name. | |||
400 | std::string EmitStr; | |||
401 | ||||
402 | private: | |||
403 | /// The number of MatchTable elements described by this record. Comments are 0 | |||
404 | /// while values are typically 1. Values >1 may occur when we need to emit | |||
405 | /// values that exceed the size of a MatchTable element. | |||
406 | unsigned NumElements; | |||
407 | ||||
408 | public: | |||
409 | /// A bitfield of RecordFlagsBits flags. | |||
410 | unsigned Flags; | |||
411 | ||||
412 | /// The actual run-time value, if known | |||
413 | int64_t RawValue; | |||
414 | ||||
415 | MatchTableRecord(Optional<unsigned> LabelID_, StringRef EmitStr, | |||
416 | unsigned NumElements, unsigned Flags, | |||
417 | int64_t RawValue = std::numeric_limits<int64_t>::min()) | |||
418 | : LabelID(LabelID_.hasValue() ? LabelID_.getValue() : ~0u), | |||
419 | EmitStr(EmitStr), NumElements(NumElements), Flags(Flags), | |||
420 | RawValue(RawValue) { | |||
421 | ||||
422 | assert((!LabelID_.hasValue() || LabelID != ~0u) &&(((!LabelID_.hasValue() || LabelID != ~0u) && "This value is reserved for non-labels" ) ? static_cast<void> (0) : __assert_fail ("(!LabelID_.hasValue() || LabelID != ~0u) && \"This value is reserved for non-labels\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 423, __PRETTY_FUNCTION__)) | |||
423 | "This value is reserved for non-labels")(((!LabelID_.hasValue() || LabelID != ~0u) && "This value is reserved for non-labels" ) ? static_cast<void> (0) : __assert_fail ("(!LabelID_.hasValue() || LabelID != ~0u) && \"This value is reserved for non-labels\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 423, __PRETTY_FUNCTION__)); | |||
424 | } | |||
425 | MatchTableRecord(const MatchTableRecord &Other) = default; | |||
426 | MatchTableRecord(MatchTableRecord &&Other) = default; | |||
427 | ||||
428 | /// Useful if a Match Table Record gets optimized out | |||
429 | void turnIntoComment() { | |||
430 | Flags |= MTRF_Comment; | |||
431 | Flags &= ~MTRF_CommaFollows; | |||
432 | NumElements = 0; | |||
433 | } | |||
434 | ||||
435 | /// For Jump Table generation purposes | |||
436 | bool operator<(const MatchTableRecord &Other) const { | |||
437 | return RawValue < Other.RawValue; | |||
438 | } | |||
439 | int64_t getRawValue() const { return RawValue; } | |||
440 | ||||
441 | void emit(raw_ostream &OS, bool LineBreakNextAfterThis, | |||
442 | const MatchTable &Table) const; | |||
443 | unsigned size() const { return NumElements; } | |||
444 | }; | |||
445 | ||||
446 | class Matcher; | |||
447 | ||||
448 | /// Holds the contents of a generated MatchTable to enable formatting and the | |||
449 | /// necessary index tracking needed to support GIM_Try. | |||
450 | class MatchTable { | |||
451 | /// An unique identifier for the table. The generated table will be named | |||
452 | /// MatchTable${ID}. | |||
453 | unsigned ID; | |||
454 | /// The records that make up the table. Also includes comments describing the | |||
455 | /// values being emitted and line breaks to format it. | |||
456 | std::vector<MatchTableRecord> Contents; | |||
457 | /// The currently defined labels. | |||
458 | DenseMap<unsigned, unsigned> LabelMap; | |||
459 | /// Tracks the sum of MatchTableRecord::NumElements as the table is built. | |||
460 | unsigned CurrentSize = 0; | |||
461 | /// A unique identifier for a MatchTable label. | |||
462 | unsigned CurrentLabelID = 0; | |||
463 | /// Determines if the table should be instrumented for rule coverage tracking. | |||
464 | bool IsWithCoverage; | |||
465 | ||||
466 | public: | |||
467 | static MatchTableRecord LineBreak; | |||
468 | static MatchTableRecord Comment(StringRef Comment) { | |||
469 | return MatchTableRecord(None, Comment, 0, MatchTableRecord::MTRF_Comment); | |||
470 | } | |||
471 | static MatchTableRecord Opcode(StringRef Opcode, int IndentAdjust = 0) { | |||
472 | unsigned ExtraFlags = 0; | |||
473 | if (IndentAdjust > 0) | |||
474 | ExtraFlags |= MatchTableRecord::MTRF_Indent; | |||
475 | if (IndentAdjust < 0) | |||
476 | ExtraFlags |= MatchTableRecord::MTRF_Outdent; | |||
477 | ||||
478 | return MatchTableRecord(None, Opcode, 1, | |||
479 | MatchTableRecord::MTRF_CommaFollows | ExtraFlags); | |||
480 | } | |||
481 | static MatchTableRecord NamedValue(StringRef NamedValue) { | |||
482 | return MatchTableRecord(None, NamedValue, 1, | |||
483 | MatchTableRecord::MTRF_CommaFollows); | |||
484 | } | |||
485 | static MatchTableRecord NamedValue(StringRef NamedValue, int64_t RawValue) { | |||
486 | return MatchTableRecord(None, NamedValue, 1, | |||
487 | MatchTableRecord::MTRF_CommaFollows, RawValue); | |||
488 | } | |||
489 | static MatchTableRecord NamedValue(StringRef Namespace, | |||
490 | StringRef NamedValue) { | |||
491 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, | |||
492 | MatchTableRecord::MTRF_CommaFollows); | |||
493 | } | |||
494 | static MatchTableRecord NamedValue(StringRef Namespace, StringRef NamedValue, | |||
495 | int64_t RawValue) { | |||
496 | return MatchTableRecord(None, (Namespace + "::" + NamedValue).str(), 1, | |||
497 | MatchTableRecord::MTRF_CommaFollows, RawValue); | |||
498 | } | |||
499 | static MatchTableRecord IntValue(int64_t IntValue) { | |||
500 | return MatchTableRecord(None, llvm::to_string(IntValue), 1, | |||
501 | MatchTableRecord::MTRF_CommaFollows); | |||
502 | } | |||
503 | static MatchTableRecord Label(unsigned LabelID) { | |||
504 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 0, | |||
505 | MatchTableRecord::MTRF_Label | | |||
506 | MatchTableRecord::MTRF_Comment | | |||
507 | MatchTableRecord::MTRF_LineBreakFollows); | |||
508 | } | |||
509 | static MatchTableRecord JumpTarget(unsigned LabelID) { | |||
510 | return MatchTableRecord(LabelID, "Label " + llvm::to_string(LabelID), 1, | |||
511 | MatchTableRecord::MTRF_JumpTarget | | |||
512 | MatchTableRecord::MTRF_Comment | | |||
513 | MatchTableRecord::MTRF_CommaFollows); | |||
514 | } | |||
515 | ||||
516 | static MatchTable buildTable(ArrayRef<Matcher *> Rules, bool WithCoverage); | |||
517 | ||||
518 | MatchTable(bool WithCoverage, unsigned ID = 0) | |||
519 | : ID(ID), IsWithCoverage(WithCoverage) {} | |||
520 | ||||
521 | bool isWithCoverage() const { return IsWithCoverage; } | |||
522 | ||||
523 | void push_back(const MatchTableRecord &Value) { | |||
524 | if (Value.Flags & MatchTableRecord::MTRF_Label) | |||
525 | defineLabel(Value.LabelID); | |||
526 | Contents.push_back(Value); | |||
527 | CurrentSize += Value.size(); | |||
528 | } | |||
529 | ||||
530 | unsigned allocateLabelID() { return CurrentLabelID++; } | |||
531 | ||||
532 | void defineLabel(unsigned LabelID) { | |||
533 | LabelMap.insert(std::make_pair(LabelID, CurrentSize)); | |||
534 | } | |||
535 | ||||
536 | unsigned getLabelIndex(unsigned LabelID) const { | |||
537 | const auto I = LabelMap.find(LabelID); | |||
538 | assert(I != LabelMap.end() && "Use of undeclared label")((I != LabelMap.end() && "Use of undeclared label") ? static_cast<void> (0) : __assert_fail ("I != LabelMap.end() && \"Use of undeclared label\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 538, __PRETTY_FUNCTION__)); | |||
539 | return I->second; | |||
540 | } | |||
541 | ||||
542 | void emitUse(raw_ostream &OS) const { OS << "MatchTable" << ID; } | |||
543 | ||||
544 | void emitDeclaration(raw_ostream &OS) const { | |||
545 | unsigned Indentation = 4; | |||
546 | OS << " constexpr static int64_t MatchTable" << ID << "[] = {"; | |||
547 | LineBreak.emit(OS, true, *this); | |||
548 | OS << std::string(Indentation, ' '); | |||
549 | ||||
550 | for (auto I = Contents.begin(), E = Contents.end(); I != E; | |||
551 | ++I) { | |||
552 | bool LineBreakIsNext = false; | |||
553 | const auto &NextI = std::next(I); | |||
554 | ||||
555 | if (NextI != E) { | |||
556 | if (NextI->EmitStr == "" && | |||
557 | NextI->Flags == MatchTableRecord::MTRF_LineBreakFollows) | |||
558 | LineBreakIsNext = true; | |||
559 | } | |||
560 | ||||
561 | if (I->Flags & MatchTableRecord::MTRF_Indent) | |||
562 | Indentation += 2; | |||
563 | ||||
564 | I->emit(OS, LineBreakIsNext, *this); | |||
565 | if (I->Flags & MatchTableRecord::MTRF_LineBreakFollows) | |||
566 | OS << std::string(Indentation, ' '); | |||
567 | ||||
568 | if (I->Flags & MatchTableRecord::MTRF_Outdent) | |||
569 | Indentation -= 2; | |||
570 | } | |||
571 | OS << "};\n"; | |||
572 | } | |||
573 | }; | |||
574 | ||||
575 | MatchTableRecord MatchTable::LineBreak = { | |||
576 | None, "" /* Emit String */, 0 /* Elements */, | |||
577 | MatchTableRecord::MTRF_LineBreakFollows}; | |||
578 | ||||
579 | void MatchTableRecord::emit(raw_ostream &OS, bool LineBreakIsNextAfterThis, | |||
580 | const MatchTable &Table) const { | |||
581 | bool UseLineComment = | |||
582 | LineBreakIsNextAfterThis | (Flags & MTRF_LineBreakFollows); | |||
583 | if (Flags & (MTRF_JumpTarget | MTRF_CommaFollows)) | |||
584 | UseLineComment = false; | |||
585 | ||||
586 | if (Flags & MTRF_Comment) | |||
587 | OS << (UseLineComment ? "// " : "/*"); | |||
588 | ||||
589 | OS << EmitStr; | |||
590 | if (Flags & MTRF_Label) | |||
591 | OS << ": @" << Table.getLabelIndex(LabelID); | |||
592 | ||||
593 | if (Flags & MTRF_Comment && !UseLineComment) | |||
594 | OS << "*/"; | |||
595 | ||||
596 | if (Flags & MTRF_JumpTarget) { | |||
597 | if (Flags & MTRF_Comment) | |||
598 | OS << " "; | |||
599 | OS << Table.getLabelIndex(LabelID); | |||
600 | } | |||
601 | ||||
602 | if (Flags & MTRF_CommaFollows) { | |||
603 | OS << ","; | |||
604 | if (!LineBreakIsNextAfterThis && !(Flags & MTRF_LineBreakFollows)) | |||
605 | OS << " "; | |||
606 | } | |||
607 | ||||
608 | if (Flags & MTRF_LineBreakFollows) | |||
609 | OS << "\n"; | |||
610 | } | |||
611 | ||||
612 | MatchTable &operator<<(MatchTable &Table, const MatchTableRecord &Value) { | |||
613 | Table.push_back(Value); | |||
614 | return Table; | |||
615 | } | |||
616 | ||||
617 | //===- Matchers -----------------------------------------------------------===// | |||
618 | ||||
619 | class OperandMatcher; | |||
620 | class MatchAction; | |||
621 | class PredicateMatcher; | |||
622 | class RuleMatcher; | |||
623 | ||||
624 | class Matcher { | |||
625 | public: | |||
626 | virtual ~Matcher() = default; | |||
627 | virtual void optimize() {} | |||
628 | virtual void emit(MatchTable &Table) = 0; | |||
629 | ||||
630 | virtual bool hasFirstCondition() const = 0; | |||
631 | virtual const PredicateMatcher &getFirstCondition() const = 0; | |||
632 | virtual std::unique_ptr<PredicateMatcher> popFirstCondition() = 0; | |||
633 | }; | |||
634 | ||||
635 | MatchTable MatchTable::buildTable(ArrayRef<Matcher *> Rules, | |||
636 | bool WithCoverage) { | |||
637 | MatchTable Table(WithCoverage); | |||
638 | for (Matcher *Rule : Rules) | |||
639 | Rule->emit(Table); | |||
640 | ||||
641 | return Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; | |||
642 | } | |||
643 | ||||
644 | class GroupMatcher final : public Matcher { | |||
645 | /// Conditions that form a common prefix of all the matchers contained. | |||
646 | SmallVector<std::unique_ptr<PredicateMatcher>, 1> Conditions; | |||
647 | ||||
648 | /// All the nested matchers, sharing a common prefix. | |||
649 | std::vector<Matcher *> Matchers; | |||
650 | ||||
651 | /// An owning collection for any auxiliary matchers created while optimizing | |||
652 | /// nested matchers contained. | |||
653 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; | |||
654 | ||||
655 | public: | |||
656 | /// Add a matcher to the collection of nested matchers if it meets the | |||
657 | /// requirements, and return true. If it doesn't, do nothing and return false. | |||
658 | /// | |||
659 | /// Expected to preserve its argument, so it could be moved out later on. | |||
660 | bool addMatcher(Matcher &Candidate); | |||
661 | ||||
662 | /// Mark the matcher as fully-built and ensure any invariants expected by both | |||
663 | /// optimize() and emit(...) methods. Generally, both sequences of calls | |||
664 | /// are expected to lead to a sensible result: | |||
665 | /// | |||
666 | /// addMatcher(...)*; finalize(); optimize(); emit(...); and | |||
667 | /// addMatcher(...)*; finalize(); emit(...); | |||
668 | /// | |||
669 | /// or generally | |||
670 | /// | |||
671 | /// addMatcher(...)*; finalize(); { optimize()*; emit(...); }* | |||
672 | /// | |||
673 | /// Multiple calls to optimize() are expected to be handled gracefully, though | |||
674 | /// optimize() is not expected to be idempotent. Multiple calls to finalize() | |||
675 | /// aren't generally supported. emit(...) is expected to be non-mutating and | |||
676 | /// producing the exact same results upon repeated calls. | |||
677 | /// | |||
678 | /// addMatcher() calls after the finalize() call are not supported. | |||
679 | /// | |||
680 | /// finalize() and optimize() are both allowed to mutate the contained | |||
681 | /// matchers, so moving them out after finalize() is not supported. | |||
682 | void finalize(); | |||
683 | void optimize() override; | |||
684 | void emit(MatchTable &Table) override; | |||
685 | ||||
686 | /// Could be used to move out the matchers added previously, unless finalize() | |||
687 | /// has been already called. If any of the matchers are moved out, the group | |||
688 | /// becomes safe to destroy, but not safe to re-use for anything else. | |||
689 | iterator_range<std::vector<Matcher *>::iterator> matchers() { | |||
690 | return make_range(Matchers.begin(), Matchers.end()); | |||
691 | } | |||
692 | size_t size() const { return Matchers.size(); } | |||
693 | bool empty() const { return Matchers.empty(); } | |||
694 | ||||
695 | std::unique_ptr<PredicateMatcher> popFirstCondition() override { | |||
696 | assert(!Conditions.empty() &&((!Conditions.empty() && "Trying to pop a condition from a condition-less group" ) ? static_cast<void> (0) : __assert_fail ("!Conditions.empty() && \"Trying to pop a condition from a condition-less group\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 697, __PRETTY_FUNCTION__)) | |||
697 | "Trying to pop a condition from a condition-less group")((!Conditions.empty() && "Trying to pop a condition from a condition-less group" ) ? static_cast<void> (0) : __assert_fail ("!Conditions.empty() && \"Trying to pop a condition from a condition-less group\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 697, __PRETTY_FUNCTION__)); | |||
698 | std::unique_ptr<PredicateMatcher> P = std::move(Conditions.front()); | |||
699 | Conditions.erase(Conditions.begin()); | |||
700 | return P; | |||
701 | } | |||
702 | const PredicateMatcher &getFirstCondition() const override { | |||
703 | assert(!Conditions.empty() &&((!Conditions.empty() && "Trying to get a condition from a condition-less group" ) ? static_cast<void> (0) : __assert_fail ("!Conditions.empty() && \"Trying to get a condition from a condition-less group\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 704, __PRETTY_FUNCTION__)) | |||
704 | "Trying to get a condition from a condition-less group")((!Conditions.empty() && "Trying to get a condition from a condition-less group" ) ? static_cast<void> (0) : __assert_fail ("!Conditions.empty() && \"Trying to get a condition from a condition-less group\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 704, __PRETTY_FUNCTION__)); | |||
705 | return *Conditions.front(); | |||
706 | } | |||
707 | bool hasFirstCondition() const override { return !Conditions.empty(); } | |||
708 | ||||
709 | private: | |||
710 | /// See if a candidate matcher could be added to this group solely by | |||
711 | /// analyzing its first condition. | |||
712 | bool candidateConditionMatches(const PredicateMatcher &Predicate) const; | |||
713 | }; | |||
714 | ||||
715 | class SwitchMatcher : public Matcher { | |||
716 | /// All the nested matchers, representing distinct switch-cases. The first | |||
717 | /// conditions (as Matcher::getFirstCondition() reports) of all the nested | |||
718 | /// matchers must share the same type and path to a value they check, in other | |||
719 | /// words, be isIdenticalDownToValue, but have different values they check | |||
720 | /// against. | |||
721 | std::vector<Matcher *> Matchers; | |||
722 | ||||
723 | /// The representative condition, with a type and a path (InsnVarID and OpIdx | |||
724 | /// in most cases) shared by all the matchers contained. | |||
725 | std::unique_ptr<PredicateMatcher> Condition = nullptr; | |||
726 | ||||
727 | /// Temporary set used to check that the case values don't repeat within the | |||
728 | /// same switch. | |||
729 | std::set<MatchTableRecord> Values; | |||
730 | ||||
731 | /// An owning collection for any auxiliary matchers created while optimizing | |||
732 | /// nested matchers contained. | |||
733 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; | |||
734 | ||||
735 | public: | |||
736 | bool addMatcher(Matcher &Candidate); | |||
737 | ||||
738 | void finalize(); | |||
739 | void emit(MatchTable &Table) override; | |||
740 | ||||
741 | iterator_range<std::vector<Matcher *>::iterator> matchers() { | |||
742 | return make_range(Matchers.begin(), Matchers.end()); | |||
743 | } | |||
744 | size_t size() const { return Matchers.size(); } | |||
745 | bool empty() const { return Matchers.empty(); } | |||
746 | ||||
747 | std::unique_ptr<PredicateMatcher> popFirstCondition() override { | |||
748 | // SwitchMatcher doesn't have a common first condition for its cases, as all | |||
749 | // the cases only share a kind of a value (a type and a path to it) they | |||
750 | // match, but deliberately differ in the actual value they match. | |||
751 | llvm_unreachable("Trying to pop a condition from a condition-less group")::llvm::llvm_unreachable_internal("Trying to pop a condition from a condition-less group" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 751); | |||
752 | } | |||
753 | const PredicateMatcher &getFirstCondition() const override { | |||
754 | llvm_unreachable("Trying to pop a condition from a condition-less group")::llvm::llvm_unreachable_internal("Trying to pop a condition from a condition-less group" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 754); | |||
755 | } | |||
756 | bool hasFirstCondition() const override { return false; } | |||
757 | ||||
758 | private: | |||
759 | /// See if the predicate type has a Switch-implementation for it. | |||
760 | static bool isSupportedPredicateType(const PredicateMatcher &Predicate); | |||
761 | ||||
762 | bool candidateConditionMatches(const PredicateMatcher &Predicate) const; | |||
763 | ||||
764 | /// emit()-helper | |||
765 | static void emitPredicateSpecificOpcodes(const PredicateMatcher &P, | |||
766 | MatchTable &Table); | |||
767 | }; | |||
768 | ||||
769 | /// Generates code to check that a match rule matches. | |||
770 | class RuleMatcher : public Matcher { | |||
771 | public: | |||
772 | using ActionList = std::list<std::unique_ptr<MatchAction>>; | |||
773 | using action_iterator = ActionList::iterator; | |||
774 | ||||
775 | protected: | |||
776 | /// A list of matchers that all need to succeed for the current rule to match. | |||
777 | /// FIXME: This currently supports a single match position but could be | |||
778 | /// extended to support multiple positions to support div/rem fusion or | |||
779 | /// load-multiple instructions. | |||
780 | using MatchersTy = std::vector<std::unique_ptr<InstructionMatcher>> ; | |||
781 | MatchersTy Matchers; | |||
782 | ||||
783 | /// A list of actions that need to be taken when all predicates in this rule | |||
784 | /// have succeeded. | |||
785 | ActionList Actions; | |||
786 | ||||
787 | using DefinedInsnVariablesMap = std::map<InstructionMatcher *, unsigned>; | |||
788 | ||||
789 | /// A map of instruction matchers to the local variables | |||
790 | DefinedInsnVariablesMap InsnVariableIDs; | |||
791 | ||||
792 | using MutatableInsnSet = SmallPtrSet<InstructionMatcher *, 4>; | |||
793 | ||||
794 | // The set of instruction matchers that have not yet been claimed for mutation | |||
795 | // by a BuildMI. | |||
796 | MutatableInsnSet MutatableInsns; | |||
797 | ||||
798 | /// A map of named operands defined by the matchers that may be referenced by | |||
799 | /// the renderers. | |||
800 | StringMap<OperandMatcher *> DefinedOperands; | |||
801 | ||||
802 | /// ID for the next instruction variable defined with implicitlyDefineInsnVar() | |||
803 | unsigned NextInsnVarID; | |||
804 | ||||
805 | /// ID for the next output instruction allocated with allocateOutputInsnID() | |||
806 | unsigned NextOutputInsnID; | |||
807 | ||||
808 | /// ID for the next temporary register ID allocated with allocateTempRegID() | |||
809 | unsigned NextTempRegID; | |||
810 | ||||
811 | std::vector<Record *> RequiredFeatures; | |||
812 | std::vector<std::unique_ptr<PredicateMatcher>> EpilogueMatchers; | |||
813 | ||||
814 | ArrayRef<SMLoc> SrcLoc; | |||
815 | ||||
816 | typedef std::tuple<Record *, unsigned, unsigned> | |||
817 | DefinedComplexPatternSubOperand; | |||
818 | typedef StringMap<DefinedComplexPatternSubOperand> | |||
819 | DefinedComplexPatternSubOperandMap; | |||
820 | /// A map of Symbolic Names to ComplexPattern sub-operands. | |||
821 | DefinedComplexPatternSubOperandMap ComplexSubOperands; | |||
822 | ||||
823 | uint64_t RuleID; | |||
824 | static uint64_t NextRuleID; | |||
825 | ||||
826 | public: | |||
827 | RuleMatcher(ArrayRef<SMLoc> SrcLoc) | |||
828 | : Matchers(), Actions(), InsnVariableIDs(), MutatableInsns(), | |||
829 | DefinedOperands(), NextInsnVarID(0), NextOutputInsnID(0), | |||
830 | NextTempRegID(0), SrcLoc(SrcLoc), ComplexSubOperands(), | |||
831 | RuleID(NextRuleID++) {} | |||
832 | RuleMatcher(RuleMatcher &&Other) = default; | |||
833 | RuleMatcher &operator=(RuleMatcher &&Other) = default; | |||
834 | ||||
835 | uint64_t getRuleID() const { return RuleID; } | |||
836 | ||||
837 | InstructionMatcher &addInstructionMatcher(StringRef SymbolicName); | |||
838 | void addRequiredFeature(Record *Feature); | |||
839 | const std::vector<Record *> &getRequiredFeatures() const; | |||
840 | ||||
841 | template <class Kind, class... Args> Kind &addAction(Args &&... args); | |||
842 | template <class Kind, class... Args> | |||
843 | action_iterator insertAction(action_iterator InsertPt, Args &&... args); | |||
844 | ||||
845 | /// Define an instruction without emitting any code to do so. | |||
846 | unsigned implicitlyDefineInsnVar(InstructionMatcher &Matcher); | |||
847 | ||||
848 | unsigned getInsnVarID(InstructionMatcher &InsnMatcher) const; | |||
849 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_begin() const { | |||
850 | return InsnVariableIDs.begin(); | |||
851 | } | |||
852 | DefinedInsnVariablesMap::const_iterator defined_insn_vars_end() const { | |||
853 | return InsnVariableIDs.end(); | |||
854 | } | |||
855 | iterator_range<typename DefinedInsnVariablesMap::const_iterator> | |||
856 | defined_insn_vars() const { | |||
857 | return make_range(defined_insn_vars_begin(), defined_insn_vars_end()); | |||
858 | } | |||
859 | ||||
860 | MutatableInsnSet::const_iterator mutatable_insns_begin() const { | |||
861 | return MutatableInsns.begin(); | |||
862 | } | |||
863 | MutatableInsnSet::const_iterator mutatable_insns_end() const { | |||
864 | return MutatableInsns.end(); | |||
865 | } | |||
866 | iterator_range<typename MutatableInsnSet::const_iterator> | |||
867 | mutatable_insns() const { | |||
868 | return make_range(mutatable_insns_begin(), mutatable_insns_end()); | |||
869 | } | |||
870 | void reserveInsnMatcherForMutation(InstructionMatcher *InsnMatcher) { | |||
871 | bool R = MutatableInsns.erase(InsnMatcher); | |||
872 | assert(R && "Reserving a mutatable insn that isn't available")((R && "Reserving a mutatable insn that isn't available" ) ? static_cast<void> (0) : __assert_fail ("R && \"Reserving a mutatable insn that isn't available\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 872, __PRETTY_FUNCTION__)); | |||
873 | (void)R; | |||
874 | } | |||
875 | ||||
876 | action_iterator actions_begin() { return Actions.begin(); } | |||
877 | action_iterator actions_end() { return Actions.end(); } | |||
878 | iterator_range<action_iterator> actions() { | |||
879 | return make_range(actions_begin(), actions_end()); | |||
880 | } | |||
881 | ||||
882 | void defineOperand(StringRef SymbolicName, OperandMatcher &OM); | |||
883 | ||||
884 | Error defineComplexSubOperand(StringRef SymbolicName, Record *ComplexPattern, | |||
885 | unsigned RendererID, unsigned SubOperandID) { | |||
886 | if (ComplexSubOperands.count(SymbolicName)) | |||
887 | return failedImport( | |||
888 | "Complex suboperand referenced more than once (Operand: " + | |||
889 | SymbolicName + ")"); | |||
890 | ||||
891 | ComplexSubOperands[SymbolicName] = | |||
892 | std::make_tuple(ComplexPattern, RendererID, SubOperandID); | |||
893 | ||||
894 | return Error::success(); | |||
895 | } | |||
896 | ||||
897 | Optional<DefinedComplexPatternSubOperand> | |||
898 | getComplexSubOperand(StringRef SymbolicName) const { | |||
899 | const auto &I = ComplexSubOperands.find(SymbolicName); | |||
900 | if (I == ComplexSubOperands.end()) | |||
901 | return None; | |||
902 | return I->second; | |||
903 | } | |||
904 | ||||
905 | InstructionMatcher &getInstructionMatcher(StringRef SymbolicName) const; | |||
906 | const OperandMatcher &getOperandMatcher(StringRef Name) const; | |||
907 | ||||
908 | void optimize() override; | |||
909 | void emit(MatchTable &Table) override; | |||
910 | ||||
911 | /// Compare the priority of this object and B. | |||
912 | /// | |||
913 | /// Returns true if this object is more important than B. | |||
914 | bool isHigherPriorityThan(const RuleMatcher &B) const; | |||
915 | ||||
916 | /// Report the maximum number of temporary operands needed by the rule | |||
917 | /// matcher. | |||
918 | unsigned countRendererFns() const; | |||
919 | ||||
920 | std::unique_ptr<PredicateMatcher> popFirstCondition() override; | |||
921 | const PredicateMatcher &getFirstCondition() const override; | |||
922 | LLTCodeGen getFirstConditionAsRootType(); | |||
923 | bool hasFirstCondition() const override; | |||
924 | unsigned getNumOperands() const; | |||
925 | StringRef getOpcode() const; | |||
926 | ||||
927 | // FIXME: Remove this as soon as possible | |||
928 | InstructionMatcher &insnmatchers_front() const { return *Matchers.front(); } | |||
929 | ||||
930 | unsigned allocateOutputInsnID() { return NextOutputInsnID++; } | |||
931 | unsigned allocateTempRegID() { return NextTempRegID++; } | |||
932 | ||||
933 | iterator_range<MatchersTy::iterator> insnmatchers() { | |||
934 | return make_range(Matchers.begin(), Matchers.end()); | |||
935 | } | |||
936 | bool insnmatchers_empty() const { return Matchers.empty(); } | |||
937 | void insnmatchers_pop_front() { Matchers.erase(Matchers.begin()); } | |||
938 | }; | |||
939 | ||||
940 | uint64_t RuleMatcher::NextRuleID = 0; | |||
941 | ||||
942 | using action_iterator = RuleMatcher::action_iterator; | |||
943 | ||||
944 | template <class PredicateTy> class PredicateListMatcher { | |||
945 | private: | |||
946 | /// Template instantiations should specialize this to return a string to use | |||
947 | /// for the comment emitted when there are no predicates. | |||
948 | std::string getNoPredicateComment() const; | |||
949 | ||||
950 | protected: | |||
951 | using PredicatesTy = std::deque<std::unique_ptr<PredicateTy>>; | |||
952 | PredicatesTy Predicates; | |||
953 | ||||
954 | /// Track if the list of predicates was manipulated by one of the optimization | |||
955 | /// methods. | |||
956 | bool Optimized = false; | |||
957 | ||||
958 | public: | |||
959 | /// Construct a new predicate and add it to the matcher. | |||
960 | template <class Kind, class... Args> | |||
961 | Optional<Kind *> addPredicate(Args &&... args); | |||
962 | ||||
963 | typename PredicatesTy::iterator predicates_begin() { | |||
964 | return Predicates.begin(); | |||
965 | } | |||
966 | typename PredicatesTy::iterator predicates_end() { | |||
967 | return Predicates.end(); | |||
968 | } | |||
969 | iterator_range<typename PredicatesTy::iterator> predicates() { | |||
970 | return make_range(predicates_begin(), predicates_end()); | |||
971 | } | |||
972 | typename PredicatesTy::size_type predicates_size() const { | |||
973 | return Predicates.size(); | |||
974 | } | |||
975 | bool predicates_empty() const { return Predicates.empty(); } | |||
976 | ||||
977 | std::unique_ptr<PredicateTy> predicates_pop_front() { | |||
978 | std::unique_ptr<PredicateTy> Front = std::move(Predicates.front()); | |||
979 | Predicates.pop_front(); | |||
980 | Optimized = true; | |||
981 | return Front; | |||
982 | } | |||
983 | ||||
984 | void prependPredicate(std::unique_ptr<PredicateTy> &&Predicate) { | |||
985 | Predicates.push_front(std::move(Predicate)); | |||
986 | } | |||
987 | ||||
988 | void eraseNullPredicates() { | |||
989 | const auto NewEnd = | |||
990 | std::stable_partition(Predicates.begin(), Predicates.end(), | |||
991 | std::logical_not<std::unique_ptr<PredicateTy>>()); | |||
992 | if (NewEnd != Predicates.begin()) { | |||
993 | Predicates.erase(Predicates.begin(), NewEnd); | |||
994 | Optimized = true; | |||
995 | } | |||
996 | } | |||
997 | ||||
998 | /// Emit MatchTable opcodes that tests whether all the predicates are met. | |||
999 | template <class... Args> | |||
1000 | void emitPredicateListOpcodes(MatchTable &Table, Args &&... args) { | |||
1001 | if (Predicates.empty() && !Optimized) { | |||
1002 | Table << MatchTable::Comment(getNoPredicateComment()) | |||
1003 | << MatchTable::LineBreak; | |||
1004 | return; | |||
1005 | } | |||
1006 | ||||
1007 | for (const auto &Predicate : predicates()) | |||
1008 | Predicate->emitPredicateOpcodes(Table, std::forward<Args>(args)...); | |||
1009 | } | |||
1010 | }; | |||
1011 | ||||
1012 | class PredicateMatcher { | |||
1013 | public: | |||
1014 | /// This enum is used for RTTI and also defines the priority that is given to | |||
1015 | /// the predicate when generating the matcher code. Kinds with higher priority | |||
1016 | /// must be tested first. | |||
1017 | /// | |||
1018 | /// The relative priority of OPM_LLT, OPM_RegBank, and OPM_MBB do not matter | |||
1019 | /// but OPM_Int must have priority over OPM_RegBank since constant integers | |||
1020 | /// are represented by a virtual register defined by a G_CONSTANT instruction. | |||
1021 | /// | |||
1022 | /// Note: The relative priority between IPM_ and OPM_ does not matter, they | |||
1023 | /// are currently not compared between each other. | |||
1024 | enum PredicateKind { | |||
1025 | IPM_Opcode, | |||
1026 | IPM_NumOperands, | |||
1027 | IPM_ImmPredicate, | |||
1028 | IPM_AtomicOrderingMMO, | |||
1029 | IPM_MemoryLLTSize, | |||
1030 | IPM_MemoryVsLLTSize, | |||
1031 | IPM_GenericPredicate, | |||
1032 | OPM_SameOperand, | |||
1033 | OPM_ComplexPattern, | |||
1034 | OPM_IntrinsicID, | |||
1035 | OPM_Instruction, | |||
1036 | OPM_Int, | |||
1037 | OPM_LiteralInt, | |||
1038 | OPM_LLT, | |||
1039 | OPM_PointerToAny, | |||
1040 | OPM_RegBank, | |||
1041 | OPM_MBB, | |||
1042 | }; | |||
1043 | ||||
1044 | protected: | |||
1045 | PredicateKind Kind; | |||
1046 | unsigned InsnVarID; | |||
1047 | unsigned OpIdx; | |||
1048 | ||||
1049 | public: | |||
1050 | PredicateMatcher(PredicateKind Kind, unsigned InsnVarID, unsigned OpIdx = ~0) | |||
1051 | : Kind(Kind), InsnVarID(InsnVarID), OpIdx(OpIdx) {} | |||
1052 | ||||
1053 | unsigned getInsnVarID() const { return InsnVarID; } | |||
1054 | unsigned getOpIdx() const { return OpIdx; } | |||
1055 | ||||
1056 | virtual ~PredicateMatcher() = default; | |||
1057 | /// Emit MatchTable opcodes that check the predicate for the given operand. | |||
1058 | virtual void emitPredicateOpcodes(MatchTable &Table, | |||
1059 | RuleMatcher &Rule) const = 0; | |||
1060 | ||||
1061 | PredicateKind getKind() const { return Kind; } | |||
1062 | ||||
1063 | virtual bool isIdentical(const PredicateMatcher &B) const { | |||
1064 | return B.getKind() == getKind() && InsnVarID == B.InsnVarID && | |||
1065 | OpIdx == B.OpIdx; | |||
1066 | } | |||
1067 | ||||
1068 | virtual bool isIdenticalDownToValue(const PredicateMatcher &B) const { | |||
1069 | return hasValue() && PredicateMatcher::isIdentical(B); | |||
1070 | } | |||
1071 | ||||
1072 | virtual MatchTableRecord getValue() const { | |||
1073 | assert(hasValue() && "Can not get a value of a value-less predicate!")((hasValue() && "Can not get a value of a value-less predicate!" ) ? static_cast<void> (0) : __assert_fail ("hasValue() && \"Can not get a value of a value-less predicate!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 1073, __PRETTY_FUNCTION__)); | |||
1074 | llvm_unreachable("Not implemented yet")::llvm::llvm_unreachable_internal("Not implemented yet", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 1074); | |||
1075 | } | |||
1076 | virtual bool hasValue() const { return false; } | |||
1077 | ||||
1078 | /// Report the maximum number of temporary operands needed by the predicate | |||
1079 | /// matcher. | |||
1080 | virtual unsigned countRendererFns() const { return 0; } | |||
1081 | }; | |||
1082 | ||||
1083 | /// Generates code to check a predicate of an operand. | |||
1084 | /// | |||
1085 | /// Typical predicates include: | |||
1086 | /// * Operand is a particular register. | |||
1087 | /// * Operand is assigned a particular register bank. | |||
1088 | /// * Operand is an MBB. | |||
1089 | class OperandPredicateMatcher : public PredicateMatcher { | |||
1090 | public: | |||
1091 | OperandPredicateMatcher(PredicateKind Kind, unsigned InsnVarID, | |||
1092 | unsigned OpIdx) | |||
1093 | : PredicateMatcher(Kind, InsnVarID, OpIdx) {} | |||
1094 | virtual ~OperandPredicateMatcher() {} | |||
1095 | ||||
1096 | /// Compare the priority of this object and B. | |||
1097 | /// | |||
1098 | /// Returns true if this object is more important than B. | |||
1099 | virtual bool isHigherPriorityThan(const OperandPredicateMatcher &B) const; | |||
1100 | }; | |||
1101 | ||||
1102 | template <> | |||
1103 | std::string | |||
1104 | PredicateListMatcher<OperandPredicateMatcher>::getNoPredicateComment() const { | |||
1105 | return "No operand predicates"; | |||
1106 | } | |||
1107 | ||||
1108 | /// Generates code to check that a register operand is defined by the same exact | |||
1109 | /// one as another. | |||
1110 | class SameOperandMatcher : public OperandPredicateMatcher { | |||
1111 | std::string MatchingName; | |||
1112 | ||||
1113 | public: | |||
1114 | SameOperandMatcher(unsigned InsnVarID, unsigned OpIdx, StringRef MatchingName) | |||
1115 | : OperandPredicateMatcher(OPM_SameOperand, InsnVarID, OpIdx), | |||
1116 | MatchingName(MatchingName) {} | |||
1117 | ||||
1118 | static bool classof(const PredicateMatcher *P) { | |||
1119 | return P->getKind() == OPM_SameOperand; | |||
1120 | } | |||
1121 | ||||
1122 | void emitPredicateOpcodes(MatchTable &Table, | |||
1123 | RuleMatcher &Rule) const override; | |||
1124 | ||||
1125 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1126 | return OperandPredicateMatcher::isIdentical(B) && | |||
1127 | MatchingName == cast<SameOperandMatcher>(&B)->MatchingName; | |||
1128 | } | |||
1129 | }; | |||
1130 | ||||
1131 | /// Generates code to check that an operand is a particular LLT. | |||
1132 | class LLTOperandMatcher : public OperandPredicateMatcher { | |||
1133 | protected: | |||
1134 | LLTCodeGen Ty; | |||
1135 | ||||
1136 | public: | |||
1137 | static std::map<LLTCodeGen, unsigned> TypeIDValues; | |||
1138 | ||||
1139 | static void initTypeIDValuesMap() { | |||
1140 | TypeIDValues.clear(); | |||
1141 | ||||
1142 | unsigned ID = 0; | |||
1143 | for (const LLTCodeGen LLTy : KnownTypes) | |||
1144 | TypeIDValues[LLTy] = ID++; | |||
1145 | } | |||
1146 | ||||
1147 | LLTOperandMatcher(unsigned InsnVarID, unsigned OpIdx, const LLTCodeGen &Ty) | |||
1148 | : OperandPredicateMatcher(OPM_LLT, InsnVarID, OpIdx), Ty(Ty) { | |||
1149 | KnownTypes.insert(Ty); | |||
1150 | } | |||
1151 | ||||
1152 | static bool classof(const PredicateMatcher *P) { | |||
1153 | return P->getKind() == OPM_LLT; | |||
1154 | } | |||
1155 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1156 | return OperandPredicateMatcher::isIdentical(B) && | |||
1157 | Ty == cast<LLTOperandMatcher>(&B)->Ty; | |||
1158 | } | |||
1159 | MatchTableRecord getValue() const override { | |||
1160 | const auto VI = TypeIDValues.find(Ty); | |||
1161 | if (VI == TypeIDValues.end()) | |||
1162 | return MatchTable::NamedValue(getTy().getCxxEnumValue()); | |||
1163 | return MatchTable::NamedValue(getTy().getCxxEnumValue(), VI->second); | |||
1164 | } | |||
1165 | bool hasValue() const override { | |||
1166 | if (TypeIDValues.size() != KnownTypes.size()) | |||
1167 | initTypeIDValuesMap(); | |||
1168 | return TypeIDValues.count(Ty); | |||
1169 | } | |||
1170 | ||||
1171 | LLTCodeGen getTy() const { return Ty; } | |||
1172 | ||||
1173 | void emitPredicateOpcodes(MatchTable &Table, | |||
1174 | RuleMatcher &Rule) const override { | |||
1175 | Table << MatchTable::Opcode("GIM_CheckType") << MatchTable::Comment("MI") | |||
1176 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") | |||
1177 | << MatchTable::IntValue(OpIdx) << MatchTable::Comment("Type") | |||
1178 | << getValue() << MatchTable::LineBreak; | |||
1179 | } | |||
1180 | }; | |||
1181 | ||||
1182 | std::map<LLTCodeGen, unsigned> LLTOperandMatcher::TypeIDValues; | |||
1183 | ||||
1184 | /// Generates code to check that an operand is a pointer to any address space. | |||
1185 | /// | |||
1186 | /// In SelectionDAG, the types did not describe pointers or address spaces. As a | |||
1187 | /// result, iN is used to describe a pointer of N bits to any address space and | |||
1188 | /// PatFrag predicates are typically used to constrain the address space. There's | |||
1189 | /// no reliable means to derive the missing type information from the pattern so | |||
1190 | /// imported rules must test the components of a pointer separately. | |||
1191 | /// | |||
1192 | /// If SizeInBits is zero, then the pointer size will be obtained from the | |||
1193 | /// subtarget. | |||
1194 | class PointerToAnyOperandMatcher : public OperandPredicateMatcher { | |||
1195 | protected: | |||
1196 | unsigned SizeInBits; | |||
1197 | ||||
1198 | public: | |||
1199 | PointerToAnyOperandMatcher(unsigned InsnVarID, unsigned OpIdx, | |||
1200 | unsigned SizeInBits) | |||
1201 | : OperandPredicateMatcher(OPM_PointerToAny, InsnVarID, OpIdx), | |||
1202 | SizeInBits(SizeInBits) {} | |||
1203 | ||||
1204 | static bool classof(const OperandPredicateMatcher *P) { | |||
1205 | return P->getKind() == OPM_PointerToAny; | |||
1206 | } | |||
1207 | ||||
1208 | void emitPredicateOpcodes(MatchTable &Table, | |||
1209 | RuleMatcher &Rule) const override { | |||
1210 | Table << MatchTable::Opcode("GIM_CheckPointerToAny") | |||
1211 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1212 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) | |||
1213 | << MatchTable::Comment("SizeInBits") | |||
1214 | << MatchTable::IntValue(SizeInBits) << MatchTable::LineBreak; | |||
1215 | } | |||
1216 | }; | |||
1217 | ||||
1218 | /// Generates code to check that an operand is a particular target constant. | |||
1219 | class ComplexPatternOperandMatcher : public OperandPredicateMatcher { | |||
1220 | protected: | |||
1221 | const OperandMatcher &Operand; | |||
1222 | const Record &TheDef; | |||
1223 | ||||
1224 | unsigned getAllocatedTemporariesBaseID() const; | |||
1225 | ||||
1226 | public: | |||
1227 | bool isIdentical(const PredicateMatcher &B) const override { return false; } | |||
1228 | ||||
1229 | ComplexPatternOperandMatcher(unsigned InsnVarID, unsigned OpIdx, | |||
1230 | const OperandMatcher &Operand, | |||
1231 | const Record &TheDef) | |||
1232 | : OperandPredicateMatcher(OPM_ComplexPattern, InsnVarID, OpIdx), | |||
1233 | Operand(Operand), TheDef(TheDef) {} | |||
1234 | ||||
1235 | static bool classof(const PredicateMatcher *P) { | |||
1236 | return P->getKind() == OPM_ComplexPattern; | |||
1237 | } | |||
1238 | ||||
1239 | void emitPredicateOpcodes(MatchTable &Table, | |||
1240 | RuleMatcher &Rule) const override { | |||
1241 | unsigned ID = getAllocatedTemporariesBaseID(); | |||
1242 | Table << MatchTable::Opcode("GIM_CheckComplexPattern") | |||
1243 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1244 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) | |||
1245 | << MatchTable::Comment("Renderer") << MatchTable::IntValue(ID) | |||
1246 | << MatchTable::NamedValue(("GICP_" + TheDef.getName()).str()) | |||
1247 | << MatchTable::LineBreak; | |||
1248 | } | |||
1249 | ||||
1250 | unsigned countRendererFns() const override { | |||
1251 | return 1; | |||
1252 | } | |||
1253 | }; | |||
1254 | ||||
1255 | /// Generates code to check that an operand is in a particular register bank. | |||
1256 | class RegisterBankOperandMatcher : public OperandPredicateMatcher { | |||
1257 | protected: | |||
1258 | const CodeGenRegisterClass &RC; | |||
1259 | ||||
1260 | public: | |||
1261 | RegisterBankOperandMatcher(unsigned InsnVarID, unsigned OpIdx, | |||
1262 | const CodeGenRegisterClass &RC) | |||
1263 | : OperandPredicateMatcher(OPM_RegBank, InsnVarID, OpIdx), RC(RC) {} | |||
1264 | ||||
1265 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1266 | return OperandPredicateMatcher::isIdentical(B) && | |||
1267 | RC.getDef() == cast<RegisterBankOperandMatcher>(&B)->RC.getDef(); | |||
1268 | } | |||
1269 | ||||
1270 | static bool classof(const PredicateMatcher *P) { | |||
1271 | return P->getKind() == OPM_RegBank; | |||
1272 | } | |||
1273 | ||||
1274 | void emitPredicateOpcodes(MatchTable &Table, | |||
1275 | RuleMatcher &Rule) const override { | |||
1276 | Table << MatchTable::Opcode("GIM_CheckRegBankForClass") | |||
1277 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1278 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) | |||
1279 | << MatchTable::Comment("RC") | |||
1280 | << MatchTable::NamedValue(RC.getQualifiedName() + "RegClassID") | |||
1281 | << MatchTable::LineBreak; | |||
1282 | } | |||
1283 | }; | |||
1284 | ||||
1285 | /// Generates code to check that an operand is a basic block. | |||
1286 | class MBBOperandMatcher : public OperandPredicateMatcher { | |||
1287 | public: | |||
1288 | MBBOperandMatcher(unsigned InsnVarID, unsigned OpIdx) | |||
1289 | : OperandPredicateMatcher(OPM_MBB, InsnVarID, OpIdx) {} | |||
1290 | ||||
1291 | static bool classof(const PredicateMatcher *P) { | |||
1292 | return P->getKind() == OPM_MBB; | |||
1293 | } | |||
1294 | ||||
1295 | void emitPredicateOpcodes(MatchTable &Table, | |||
1296 | RuleMatcher &Rule) const override { | |||
1297 | Table << MatchTable::Opcode("GIM_CheckIsMBB") << MatchTable::Comment("MI") | |||
1298 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Op") | |||
1299 | << MatchTable::IntValue(OpIdx) << MatchTable::LineBreak; | |||
1300 | } | |||
1301 | }; | |||
1302 | ||||
1303 | /// Generates code to check that an operand is a G_CONSTANT with a particular | |||
1304 | /// int. | |||
1305 | class ConstantIntOperandMatcher : public OperandPredicateMatcher { | |||
1306 | protected: | |||
1307 | int64_t Value; | |||
1308 | ||||
1309 | public: | |||
1310 | ConstantIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value) | |||
1311 | : OperandPredicateMatcher(OPM_Int, InsnVarID, OpIdx), Value(Value) {} | |||
1312 | ||||
1313 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1314 | return OperandPredicateMatcher::isIdentical(B) && | |||
1315 | Value == cast<ConstantIntOperandMatcher>(&B)->Value; | |||
1316 | } | |||
1317 | ||||
1318 | static bool classof(const PredicateMatcher *P) { | |||
1319 | return P->getKind() == OPM_Int; | |||
1320 | } | |||
1321 | ||||
1322 | void emitPredicateOpcodes(MatchTable &Table, | |||
1323 | RuleMatcher &Rule) const override { | |||
1324 | Table << MatchTable::Opcode("GIM_CheckConstantInt") | |||
1325 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1326 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) | |||
1327 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; | |||
1328 | } | |||
1329 | }; | |||
1330 | ||||
1331 | /// Generates code to check that an operand is a raw int (where MO.isImm() or | |||
1332 | /// MO.isCImm() is true). | |||
1333 | class LiteralIntOperandMatcher : public OperandPredicateMatcher { | |||
1334 | protected: | |||
1335 | int64_t Value; | |||
1336 | ||||
1337 | public: | |||
1338 | LiteralIntOperandMatcher(unsigned InsnVarID, unsigned OpIdx, int64_t Value) | |||
1339 | : OperandPredicateMatcher(OPM_LiteralInt, InsnVarID, OpIdx), | |||
1340 | Value(Value) {} | |||
1341 | ||||
1342 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1343 | return OperandPredicateMatcher::isIdentical(B) && | |||
1344 | Value == cast<LiteralIntOperandMatcher>(&B)->Value; | |||
1345 | } | |||
1346 | ||||
1347 | static bool classof(const PredicateMatcher *P) { | |||
1348 | return P->getKind() == OPM_LiteralInt; | |||
1349 | } | |||
1350 | ||||
1351 | void emitPredicateOpcodes(MatchTable &Table, | |||
1352 | RuleMatcher &Rule) const override { | |||
1353 | Table << MatchTable::Opcode("GIM_CheckLiteralInt") | |||
1354 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1355 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) | |||
1356 | << MatchTable::IntValue(Value) << MatchTable::LineBreak; | |||
1357 | } | |||
1358 | }; | |||
1359 | ||||
1360 | /// Generates code to check that an operand is an intrinsic ID. | |||
1361 | class IntrinsicIDOperandMatcher : public OperandPredicateMatcher { | |||
1362 | protected: | |||
1363 | const CodeGenIntrinsic *II; | |||
1364 | ||||
1365 | public: | |||
1366 | IntrinsicIDOperandMatcher(unsigned InsnVarID, unsigned OpIdx, | |||
1367 | const CodeGenIntrinsic *II) | |||
1368 | : OperandPredicateMatcher(OPM_IntrinsicID, InsnVarID, OpIdx), II(II) {} | |||
1369 | ||||
1370 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1371 | return OperandPredicateMatcher::isIdentical(B) && | |||
1372 | II == cast<IntrinsicIDOperandMatcher>(&B)->II; | |||
1373 | } | |||
1374 | ||||
1375 | static bool classof(const PredicateMatcher *P) { | |||
1376 | return P->getKind() == OPM_IntrinsicID; | |||
1377 | } | |||
1378 | ||||
1379 | void emitPredicateOpcodes(MatchTable &Table, | |||
1380 | RuleMatcher &Rule) const override { | |||
1381 | Table << MatchTable::Opcode("GIM_CheckIntrinsicID") | |||
1382 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1383 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) | |||
1384 | << MatchTable::NamedValue("Intrinsic::" + II->EnumName) | |||
1385 | << MatchTable::LineBreak; | |||
1386 | } | |||
1387 | }; | |||
1388 | ||||
1389 | /// Generates code to check that a set of predicates match for a particular | |||
1390 | /// operand. | |||
1391 | class OperandMatcher : public PredicateListMatcher<OperandPredicateMatcher> { | |||
1392 | protected: | |||
1393 | InstructionMatcher &Insn; | |||
1394 | unsigned OpIdx; | |||
1395 | std::string SymbolicName; | |||
1396 | ||||
1397 | /// The index of the first temporary variable allocated to this operand. The | |||
1398 | /// number of allocated temporaries can be found with | |||
1399 | /// countRendererFns(). | |||
1400 | unsigned AllocatedTemporariesBaseID; | |||
1401 | ||||
1402 | public: | |||
1403 | OperandMatcher(InstructionMatcher &Insn, unsigned OpIdx, | |||
1404 | const std::string &SymbolicName, | |||
1405 | unsigned AllocatedTemporariesBaseID) | |||
1406 | : Insn(Insn), OpIdx(OpIdx), SymbolicName(SymbolicName), | |||
1407 | AllocatedTemporariesBaseID(AllocatedTemporariesBaseID) {} | |||
1408 | ||||
1409 | bool hasSymbolicName() const { return !SymbolicName.empty(); } | |||
1410 | const StringRef getSymbolicName() const { return SymbolicName; } | |||
1411 | void setSymbolicName(StringRef Name) { | |||
1412 | assert(SymbolicName.empty() && "Operand already has a symbolic name")((SymbolicName.empty() && "Operand already has a symbolic name" ) ? static_cast<void> (0) : __assert_fail ("SymbolicName.empty() && \"Operand already has a symbolic name\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 1412, __PRETTY_FUNCTION__)); | |||
1413 | SymbolicName = Name; | |||
1414 | } | |||
1415 | ||||
1416 | /// Construct a new operand predicate and add it to the matcher. | |||
1417 | template <class Kind, class... Args> | |||
1418 | Optional<Kind *> addPredicate(Args &&... args) { | |||
1419 | if (isSameAsAnotherOperand()) | |||
1420 | return None; | |||
1421 | Predicates.emplace_back(llvm::make_unique<Kind>( | |||
1422 | getInsnVarID(), getOpIdx(), std::forward<Args>(args)...)); | |||
1423 | return static_cast<Kind *>(Predicates.back().get()); | |||
1424 | } | |||
1425 | ||||
1426 | unsigned getOpIdx() const { return OpIdx; } | |||
1427 | unsigned getInsnVarID() const; | |||
1428 | ||||
1429 | std::string getOperandExpr(unsigned InsnVarID) const { | |||
1430 | return "State.MIs[" + llvm::to_string(InsnVarID) + "]->getOperand(" + | |||
1431 | llvm::to_string(OpIdx) + ")"; | |||
1432 | } | |||
1433 | ||||
1434 | InstructionMatcher &getInstructionMatcher() const { return Insn; } | |||
1435 | ||||
1436 | Error addTypeCheckPredicate(const TypeSetByHwMode &VTy, | |||
1437 | bool OperandIsAPointer); | |||
1438 | ||||
1439 | /// Emit MatchTable opcodes that test whether the instruction named in | |||
1440 | /// InsnVarID matches all the predicates and all the operands. | |||
1441 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) { | |||
1442 | if (!Optimized) { | |||
1443 | std::string Comment; | |||
1444 | raw_string_ostream CommentOS(Comment); | |||
1445 | CommentOS << "MIs[" << getInsnVarID() << "] "; | |||
1446 | if (SymbolicName.empty()) | |||
1447 | CommentOS << "Operand " << OpIdx; | |||
1448 | else | |||
1449 | CommentOS << SymbolicName; | |||
1450 | Table << MatchTable::Comment(CommentOS.str()) << MatchTable::LineBreak; | |||
1451 | } | |||
1452 | ||||
1453 | emitPredicateListOpcodes(Table, Rule); | |||
1454 | } | |||
1455 | ||||
1456 | /// Compare the priority of this object and B. | |||
1457 | /// | |||
1458 | /// Returns true if this object is more important than B. | |||
1459 | bool isHigherPriorityThan(OperandMatcher &B) { | |||
1460 | // Operand matchers involving more predicates have higher priority. | |||
1461 | if (predicates_size() > B.predicates_size()) | |||
1462 | return true; | |||
1463 | if (predicates_size() < B.predicates_size()) | |||
1464 | return false; | |||
1465 | ||||
1466 | // This assumes that predicates are added in a consistent order. | |||
1467 | for (auto &&Predicate : zip(predicates(), B.predicates())) { | |||
1468 | if (std::get<0>(Predicate)->isHigherPriorityThan(*std::get<1>(Predicate))) | |||
1469 | return true; | |||
1470 | if (std::get<1>(Predicate)->isHigherPriorityThan(*std::get<0>(Predicate))) | |||
1471 | return false; | |||
1472 | } | |||
1473 | ||||
1474 | return false; | |||
1475 | }; | |||
1476 | ||||
1477 | /// Report the maximum number of temporary operands needed by the operand | |||
1478 | /// matcher. | |||
1479 | unsigned countRendererFns() { | |||
1480 | return std::accumulate( | |||
1481 | predicates().begin(), predicates().end(), 0, | |||
1482 | [](unsigned A, | |||
1483 | const std::unique_ptr<OperandPredicateMatcher> &Predicate) { | |||
1484 | return A + Predicate->countRendererFns(); | |||
1485 | }); | |||
1486 | } | |||
1487 | ||||
1488 | unsigned getAllocatedTemporariesBaseID() const { | |||
1489 | return AllocatedTemporariesBaseID; | |||
1490 | } | |||
1491 | ||||
1492 | bool isSameAsAnotherOperand() { | |||
1493 | for (const auto &Predicate : predicates()) | |||
1494 | if (isa<SameOperandMatcher>(Predicate)) | |||
1495 | return true; | |||
1496 | return false; | |||
1497 | } | |||
1498 | }; | |||
1499 | ||||
1500 | Error OperandMatcher::addTypeCheckPredicate(const TypeSetByHwMode &VTy, | |||
1501 | bool OperandIsAPointer) { | |||
1502 | if (!VTy.isMachineValueType()) | |||
1503 | return failedImport("unsupported typeset"); | |||
1504 | ||||
1505 | if (VTy.getMachineValueType() == MVT::iPTR && OperandIsAPointer) { | |||
1506 | addPredicate<PointerToAnyOperandMatcher>(0); | |||
1507 | return Error::success(); | |||
1508 | } | |||
1509 | ||||
1510 | auto OpTyOrNone = MVTToLLT(VTy.getMachineValueType().SimpleTy); | |||
1511 | if (!OpTyOrNone) | |||
1512 | return failedImport("unsupported type"); | |||
1513 | ||||
1514 | if (OperandIsAPointer) | |||
1515 | addPredicate<PointerToAnyOperandMatcher>(OpTyOrNone->get().getSizeInBits()); | |||
1516 | else if (VTy.isPointer()) | |||
1517 | addPredicate<LLTOperandMatcher>(LLT::pointer(VTy.getPtrAddrSpace(), | |||
1518 | OpTyOrNone->get().getSizeInBits())); | |||
1519 | else | |||
1520 | addPredicate<LLTOperandMatcher>(*OpTyOrNone); | |||
1521 | return Error::success(); | |||
1522 | } | |||
1523 | ||||
1524 | unsigned ComplexPatternOperandMatcher::getAllocatedTemporariesBaseID() const { | |||
1525 | return Operand.getAllocatedTemporariesBaseID(); | |||
1526 | } | |||
1527 | ||||
1528 | /// Generates code to check a predicate on an instruction. | |||
1529 | /// | |||
1530 | /// Typical predicates include: | |||
1531 | /// * The opcode of the instruction is a particular value. | |||
1532 | /// * The nsw/nuw flag is/isn't set. | |||
1533 | class InstructionPredicateMatcher : public PredicateMatcher { | |||
1534 | public: | |||
1535 | InstructionPredicateMatcher(PredicateKind Kind, unsigned InsnVarID) | |||
1536 | : PredicateMatcher(Kind, InsnVarID) {} | |||
1537 | virtual ~InstructionPredicateMatcher() {} | |||
1538 | ||||
1539 | /// Compare the priority of this object and B. | |||
1540 | /// | |||
1541 | /// Returns true if this object is more important than B. | |||
1542 | virtual bool | |||
1543 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const { | |||
1544 | return Kind < B.Kind; | |||
1545 | }; | |||
1546 | }; | |||
1547 | ||||
1548 | template <> | |||
1549 | std::string | |||
1550 | PredicateListMatcher<PredicateMatcher>::getNoPredicateComment() const { | |||
1551 | return "No instruction predicates"; | |||
1552 | } | |||
1553 | ||||
1554 | /// Generates code to check the opcode of an instruction. | |||
1555 | class InstructionOpcodeMatcher : public InstructionPredicateMatcher { | |||
1556 | protected: | |||
1557 | const CodeGenInstruction *I; | |||
1558 | ||||
1559 | static DenseMap<const CodeGenInstruction *, unsigned> OpcodeValues; | |||
1560 | ||||
1561 | public: | |||
1562 | static void initOpcodeValuesMap(const CodeGenTarget &Target) { | |||
1563 | OpcodeValues.clear(); | |||
1564 | ||||
1565 | unsigned OpcodeValue = 0; | |||
1566 | for (const CodeGenInstruction *I : Target.getInstructionsByEnumValue()) | |||
1567 | OpcodeValues[I] = OpcodeValue++; | |||
1568 | } | |||
1569 | ||||
1570 | InstructionOpcodeMatcher(unsigned InsnVarID, const CodeGenInstruction *I) | |||
1571 | : InstructionPredicateMatcher(IPM_Opcode, InsnVarID), I(I) {} | |||
1572 | ||||
1573 | static bool classof(const PredicateMatcher *P) { | |||
1574 | return P->getKind() == IPM_Opcode; | |||
1575 | } | |||
1576 | ||||
1577 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1578 | return InstructionPredicateMatcher::isIdentical(B) && | |||
1579 | I == cast<InstructionOpcodeMatcher>(&B)->I; | |||
1580 | } | |||
1581 | MatchTableRecord getValue() const override { | |||
1582 | const auto VI = OpcodeValues.find(I); | |||
1583 | if (VI != OpcodeValues.end()) | |||
1584 | return MatchTable::NamedValue(I->Namespace, I->TheDef->getName(), | |||
1585 | VI->second); | |||
1586 | return MatchTable::NamedValue(I->Namespace, I->TheDef->getName()); | |||
1587 | } | |||
1588 | bool hasValue() const override { return OpcodeValues.count(I); } | |||
1589 | ||||
1590 | void emitPredicateOpcodes(MatchTable &Table, | |||
1591 | RuleMatcher &Rule) const override { | |||
1592 | Table << MatchTable::Opcode("GIM_CheckOpcode") << MatchTable::Comment("MI") | |||
1593 | << MatchTable::IntValue(InsnVarID) << getValue() | |||
1594 | << MatchTable::LineBreak; | |||
1595 | } | |||
1596 | ||||
1597 | /// Compare the priority of this object and B. | |||
1598 | /// | |||
1599 | /// Returns true if this object is more important than B. | |||
1600 | bool | |||
1601 | isHigherPriorityThan(const InstructionPredicateMatcher &B) const override { | |||
1602 | if (InstructionPredicateMatcher::isHigherPriorityThan(B)) | |||
1603 | return true; | |||
1604 | if (B.InstructionPredicateMatcher::isHigherPriorityThan(*this)) | |||
1605 | return false; | |||
1606 | ||||
1607 | // Prioritize opcodes for cosmetic reasons in the generated source. Although | |||
1608 | // this is cosmetic at the moment, we may want to drive a similar ordering | |||
1609 | // using instruction frequency information to improve compile time. | |||
1610 | if (const InstructionOpcodeMatcher *BO = | |||
1611 | dyn_cast<InstructionOpcodeMatcher>(&B)) | |||
1612 | return I->TheDef->getName() < BO->I->TheDef->getName(); | |||
1613 | ||||
1614 | return false; | |||
1615 | }; | |||
1616 | ||||
1617 | bool isConstantInstruction() const { | |||
1618 | return I->TheDef->getName() == "G_CONSTANT"; | |||
1619 | } | |||
1620 | ||||
1621 | StringRef getOpcode() const { return I->TheDef->getName(); } | |||
1622 | unsigned getNumOperands() const { return I->Operands.size(); } | |||
1623 | ||||
1624 | StringRef getOperandType(unsigned OpIdx) const { | |||
1625 | return I->Operands[OpIdx].OperandType; | |||
1626 | } | |||
1627 | }; | |||
1628 | ||||
1629 | DenseMap<const CodeGenInstruction *, unsigned> | |||
1630 | InstructionOpcodeMatcher::OpcodeValues; | |||
1631 | ||||
1632 | class InstructionNumOperandsMatcher final : public InstructionPredicateMatcher { | |||
1633 | unsigned NumOperands = 0; | |||
1634 | ||||
1635 | public: | |||
1636 | InstructionNumOperandsMatcher(unsigned InsnVarID, unsigned NumOperands) | |||
1637 | : InstructionPredicateMatcher(IPM_NumOperands, InsnVarID), | |||
1638 | NumOperands(NumOperands) {} | |||
1639 | ||||
1640 | static bool classof(const PredicateMatcher *P) { | |||
1641 | return P->getKind() == IPM_NumOperands; | |||
1642 | } | |||
1643 | ||||
1644 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1645 | return InstructionPredicateMatcher::isIdentical(B) && | |||
1646 | NumOperands == cast<InstructionNumOperandsMatcher>(&B)->NumOperands; | |||
1647 | } | |||
1648 | ||||
1649 | void emitPredicateOpcodes(MatchTable &Table, | |||
1650 | RuleMatcher &Rule) const override { | |||
1651 | Table << MatchTable::Opcode("GIM_CheckNumOperands") | |||
1652 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1653 | << MatchTable::Comment("Expected") | |||
1654 | << MatchTable::IntValue(NumOperands) << MatchTable::LineBreak; | |||
1655 | } | |||
1656 | }; | |||
1657 | ||||
1658 | /// Generates code to check that this instruction is a constant whose value | |||
1659 | /// meets an immediate predicate. | |||
1660 | /// | |||
1661 | /// Immediates are slightly odd since they are typically used like an operand | |||
1662 | /// but are represented as an operator internally. We typically write simm8:$src | |||
1663 | /// in a tablegen pattern, but this is just syntactic sugar for | |||
1664 | /// (imm:i32)<<P:Predicate_simm8>>:$imm which more directly describes the nodes | |||
1665 | /// that will be matched and the predicate (which is attached to the imm | |||
1666 | /// operator) that will be tested. In SelectionDAG this describes a | |||
1667 | /// ConstantSDNode whose internal value will be tested using the simm8 predicate. | |||
1668 | /// | |||
1669 | /// The corresponding GlobalISel representation is %1 = G_CONSTANT iN Value. In | |||
1670 | /// this representation, the immediate could be tested with an | |||
1671 | /// InstructionMatcher, InstructionOpcodeMatcher, OperandMatcher, and a | |||
1672 | /// OperandPredicateMatcher-subclass to check the Value meets the predicate but | |||
1673 | /// there are two implementation issues with producing that matcher | |||
1674 | /// configuration from the SelectionDAG pattern: | |||
1675 | /// * ImmLeaf is a PatFrag whose root is an InstructionMatcher. This means that | |||
1676 | /// were we to sink the immediate predicate to the operand we would have to | |||
1677 | /// have two partial implementations of PatFrag support, one for immediates | |||
1678 | /// and one for non-immediates. | |||
1679 | /// * At the point we handle the predicate, the OperandMatcher hasn't been | |||
1680 | /// created yet. If we were to sink the predicate to the OperandMatcher we | |||
1681 | /// would also have to complicate (or duplicate) the code that descends and | |||
1682 | /// creates matchers for the subtree. | |||
1683 | /// Overall, it's simpler to handle it in the place it was found. | |||
1684 | class InstructionImmPredicateMatcher : public InstructionPredicateMatcher { | |||
1685 | protected: | |||
1686 | TreePredicateFn Predicate; | |||
1687 | ||||
1688 | public: | |||
1689 | InstructionImmPredicateMatcher(unsigned InsnVarID, | |||
1690 | const TreePredicateFn &Predicate) | |||
1691 | : InstructionPredicateMatcher(IPM_ImmPredicate, InsnVarID), | |||
1692 | Predicate(Predicate) {} | |||
1693 | ||||
1694 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1695 | return InstructionPredicateMatcher::isIdentical(B) && | |||
1696 | Predicate.getOrigPatFragRecord() == | |||
1697 | cast<InstructionImmPredicateMatcher>(&B) | |||
1698 | ->Predicate.getOrigPatFragRecord(); | |||
1699 | } | |||
1700 | ||||
1701 | static bool classof(const PredicateMatcher *P) { | |||
1702 | return P->getKind() == IPM_ImmPredicate; | |||
1703 | } | |||
1704 | ||||
1705 | void emitPredicateOpcodes(MatchTable &Table, | |||
1706 | RuleMatcher &Rule) const override { | |||
1707 | Table << MatchTable::Opcode(getMatchOpcodeForPredicate(Predicate)) | |||
1708 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1709 | << MatchTable::Comment("Predicate") | |||
1710 | << MatchTable::NamedValue(getEnumNameForPredicate(Predicate)) | |||
1711 | << MatchTable::LineBreak; | |||
1712 | } | |||
1713 | }; | |||
1714 | ||||
1715 | /// Generates code to check that a memory instruction has a atomic ordering | |||
1716 | /// MachineMemoryOperand. | |||
1717 | class AtomicOrderingMMOPredicateMatcher : public InstructionPredicateMatcher { | |||
1718 | public: | |||
1719 | enum AOComparator { | |||
1720 | AO_Exactly, | |||
1721 | AO_OrStronger, | |||
1722 | AO_WeakerThan, | |||
1723 | }; | |||
1724 | ||||
1725 | protected: | |||
1726 | StringRef Order; | |||
1727 | AOComparator Comparator; | |||
1728 | ||||
1729 | public: | |||
1730 | AtomicOrderingMMOPredicateMatcher(unsigned InsnVarID, StringRef Order, | |||
1731 | AOComparator Comparator = AO_Exactly) | |||
1732 | : InstructionPredicateMatcher(IPM_AtomicOrderingMMO, InsnVarID), | |||
1733 | Order(Order), Comparator(Comparator) {} | |||
1734 | ||||
1735 | static bool classof(const PredicateMatcher *P) { | |||
1736 | return P->getKind() == IPM_AtomicOrderingMMO; | |||
1737 | } | |||
1738 | ||||
1739 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1740 | if (!InstructionPredicateMatcher::isIdentical(B)) | |||
1741 | return false; | |||
1742 | const auto &R = *cast<AtomicOrderingMMOPredicateMatcher>(&B); | |||
1743 | return Order == R.Order && Comparator == R.Comparator; | |||
1744 | } | |||
1745 | ||||
1746 | void emitPredicateOpcodes(MatchTable &Table, | |||
1747 | RuleMatcher &Rule) const override { | |||
1748 | StringRef Opcode = "GIM_CheckAtomicOrdering"; | |||
1749 | ||||
1750 | if (Comparator == AO_OrStronger) | |||
1751 | Opcode = "GIM_CheckAtomicOrderingOrStrongerThan"; | |||
1752 | if (Comparator == AO_WeakerThan) | |||
1753 | Opcode = "GIM_CheckAtomicOrderingWeakerThan"; | |||
1754 | ||||
1755 | Table << MatchTable::Opcode(Opcode) << MatchTable::Comment("MI") | |||
1756 | << MatchTable::IntValue(InsnVarID) << MatchTable::Comment("Order") | |||
1757 | << MatchTable::NamedValue(("(int64_t)AtomicOrdering::" + Order).str()) | |||
1758 | << MatchTable::LineBreak; | |||
1759 | } | |||
1760 | }; | |||
1761 | ||||
1762 | /// Generates code to check that the size of an MMO is exactly N bytes. | |||
1763 | class MemorySizePredicateMatcher : public InstructionPredicateMatcher { | |||
1764 | protected: | |||
1765 | unsigned MMOIdx; | |||
1766 | uint64_t Size; | |||
1767 | ||||
1768 | public: | |||
1769 | MemorySizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, unsigned Size) | |||
1770 | : InstructionPredicateMatcher(IPM_MemoryLLTSize, InsnVarID), | |||
1771 | MMOIdx(MMOIdx), Size(Size) {} | |||
1772 | ||||
1773 | static bool classof(const PredicateMatcher *P) { | |||
1774 | return P->getKind() == IPM_MemoryLLTSize; | |||
1775 | } | |||
1776 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1777 | return InstructionPredicateMatcher::isIdentical(B) && | |||
1778 | MMOIdx == cast<MemorySizePredicateMatcher>(&B)->MMOIdx && | |||
1779 | Size == cast<MemorySizePredicateMatcher>(&B)->Size; | |||
1780 | } | |||
1781 | ||||
1782 | void emitPredicateOpcodes(MatchTable &Table, | |||
1783 | RuleMatcher &Rule) const override { | |||
1784 | Table << MatchTable::Opcode("GIM_CheckMemorySizeEqualTo") | |||
1785 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1786 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) | |||
1787 | << MatchTable::Comment("Size") << MatchTable::IntValue(Size) | |||
1788 | << MatchTable::LineBreak; | |||
1789 | } | |||
1790 | }; | |||
1791 | ||||
1792 | /// Generates code to check that the size of an MMO is less-than, equal-to, or | |||
1793 | /// greater than a given LLT. | |||
1794 | class MemoryVsLLTSizePredicateMatcher : public InstructionPredicateMatcher { | |||
1795 | public: | |||
1796 | enum RelationKind { | |||
1797 | GreaterThan, | |||
1798 | EqualTo, | |||
1799 | LessThan, | |||
1800 | }; | |||
1801 | ||||
1802 | protected: | |||
1803 | unsigned MMOIdx; | |||
1804 | RelationKind Relation; | |||
1805 | unsigned OpIdx; | |||
1806 | ||||
1807 | public: | |||
1808 | MemoryVsLLTSizePredicateMatcher(unsigned InsnVarID, unsigned MMOIdx, | |||
1809 | enum RelationKind Relation, | |||
1810 | unsigned OpIdx) | |||
1811 | : InstructionPredicateMatcher(IPM_MemoryVsLLTSize, InsnVarID), | |||
1812 | MMOIdx(MMOIdx), Relation(Relation), OpIdx(OpIdx) {} | |||
1813 | ||||
1814 | static bool classof(const PredicateMatcher *P) { | |||
1815 | return P->getKind() == IPM_MemoryVsLLTSize; | |||
1816 | } | |||
1817 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1818 | return InstructionPredicateMatcher::isIdentical(B) && | |||
1819 | MMOIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->MMOIdx && | |||
1820 | Relation == cast<MemoryVsLLTSizePredicateMatcher>(&B)->Relation && | |||
1821 | OpIdx == cast<MemoryVsLLTSizePredicateMatcher>(&B)->OpIdx; | |||
1822 | } | |||
1823 | ||||
1824 | void emitPredicateOpcodes(MatchTable &Table, | |||
1825 | RuleMatcher &Rule) const override { | |||
1826 | Table << MatchTable::Opcode(Relation == EqualTo | |||
1827 | ? "GIM_CheckMemorySizeEqualToLLT" | |||
1828 | : Relation == GreaterThan | |||
1829 | ? "GIM_CheckMemorySizeGreaterThanLLT" | |||
1830 | : "GIM_CheckMemorySizeLessThanLLT") | |||
1831 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1832 | << MatchTable::Comment("MMO") << MatchTable::IntValue(MMOIdx) | |||
1833 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) | |||
1834 | << MatchTable::LineBreak; | |||
1835 | } | |||
1836 | }; | |||
1837 | ||||
1838 | /// Generates code to check an arbitrary C++ instruction predicate. | |||
1839 | class GenericInstructionPredicateMatcher : public InstructionPredicateMatcher { | |||
1840 | protected: | |||
1841 | TreePredicateFn Predicate; | |||
1842 | ||||
1843 | public: | |||
1844 | GenericInstructionPredicateMatcher(unsigned InsnVarID, | |||
1845 | TreePredicateFn Predicate) | |||
1846 | : InstructionPredicateMatcher(IPM_GenericPredicate, InsnVarID), | |||
1847 | Predicate(Predicate) {} | |||
1848 | ||||
1849 | static bool classof(const InstructionPredicateMatcher *P) { | |||
1850 | return P->getKind() == IPM_GenericPredicate; | |||
1851 | } | |||
1852 | bool isIdentical(const PredicateMatcher &B) const override { | |||
1853 | return InstructionPredicateMatcher::isIdentical(B) && | |||
1854 | Predicate == | |||
1855 | static_cast<const GenericInstructionPredicateMatcher &>(B) | |||
1856 | .Predicate; | |||
1857 | } | |||
1858 | void emitPredicateOpcodes(MatchTable &Table, | |||
1859 | RuleMatcher &Rule) const override { | |||
1860 | Table << MatchTable::Opcode("GIM_CheckCxxInsnPredicate") | |||
1861 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
1862 | << MatchTable::Comment("FnId") | |||
1863 | << MatchTable::NamedValue(getEnumNameForPredicate(Predicate)) | |||
1864 | << MatchTable::LineBreak; | |||
1865 | } | |||
1866 | }; | |||
1867 | ||||
1868 | /// Generates code to check that a set of predicates and operands match for a | |||
1869 | /// particular instruction. | |||
1870 | /// | |||
1871 | /// Typical predicates include: | |||
1872 | /// * Has a specific opcode. | |||
1873 | /// * Has an nsw/nuw flag or doesn't. | |||
1874 | class InstructionMatcher final : public PredicateListMatcher<PredicateMatcher> { | |||
1875 | protected: | |||
1876 | typedef std::vector<std::unique_ptr<OperandMatcher>> OperandVec; | |||
1877 | ||||
1878 | RuleMatcher &Rule; | |||
1879 | ||||
1880 | /// The operands to match. All rendered operands must be present even if the | |||
1881 | /// condition is always true. | |||
1882 | OperandVec Operands; | |||
1883 | bool NumOperandsCheck = true; | |||
1884 | ||||
1885 | std::string SymbolicName; | |||
1886 | unsigned InsnVarID; | |||
1887 | ||||
1888 | public: | |||
1889 | InstructionMatcher(RuleMatcher &Rule, StringRef SymbolicName) | |||
1890 | : Rule(Rule), SymbolicName(SymbolicName) { | |||
1891 | // We create a new instruction matcher. | |||
1892 | // Get a new ID for that instruction. | |||
1893 | InsnVarID = Rule.implicitlyDefineInsnVar(*this); | |||
1894 | } | |||
1895 | ||||
1896 | /// Construct a new instruction predicate and add it to the matcher. | |||
1897 | template <class Kind, class... Args> | |||
1898 | Optional<Kind *> addPredicate(Args &&... args) { | |||
1899 | Predicates.emplace_back( | |||
1900 | llvm::make_unique<Kind>(getInsnVarID(), std::forward<Args>(args)...)); | |||
1901 | return static_cast<Kind *>(Predicates.back().get()); | |||
1902 | } | |||
1903 | ||||
1904 | RuleMatcher &getRuleMatcher() const { return Rule; } | |||
1905 | ||||
1906 | unsigned getInsnVarID() const { return InsnVarID; } | |||
1907 | ||||
1908 | /// Add an operand to the matcher. | |||
1909 | OperandMatcher &addOperand(unsigned OpIdx, const std::string &SymbolicName, | |||
1910 | unsigned AllocatedTemporariesBaseID) { | |||
1911 | Operands.emplace_back(new OperandMatcher(*this, OpIdx, SymbolicName, | |||
1912 | AllocatedTemporariesBaseID)); | |||
1913 | if (!SymbolicName.empty()) | |||
1914 | Rule.defineOperand(SymbolicName, *Operands.back()); | |||
1915 | ||||
1916 | return *Operands.back(); | |||
1917 | } | |||
1918 | ||||
1919 | OperandMatcher &getOperand(unsigned OpIdx) { | |||
1920 | auto I = std::find_if(Operands.begin(), Operands.end(), | |||
1921 | [&OpIdx](const std::unique_ptr<OperandMatcher> &X) { | |||
1922 | return X->getOpIdx() == OpIdx; | |||
1923 | }); | |||
1924 | if (I != Operands.end()) | |||
1925 | return **I; | |||
1926 | llvm_unreachable("Failed to lookup operand")::llvm::llvm_unreachable_internal("Failed to lookup operand", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 1926); | |||
1927 | } | |||
1928 | ||||
1929 | StringRef getSymbolicName() const { return SymbolicName; } | |||
1930 | unsigned getNumOperands() const { return Operands.size(); } | |||
1931 | OperandVec::iterator operands_begin() { return Operands.begin(); } | |||
1932 | OperandVec::iterator operands_end() { return Operands.end(); } | |||
1933 | iterator_range<OperandVec::iterator> operands() { | |||
1934 | return make_range(operands_begin(), operands_end()); | |||
1935 | } | |||
1936 | OperandVec::const_iterator operands_begin() const { return Operands.begin(); } | |||
1937 | OperandVec::const_iterator operands_end() const { return Operands.end(); } | |||
1938 | iterator_range<OperandVec::const_iterator> operands() const { | |||
1939 | return make_range(operands_begin(), operands_end()); | |||
1940 | } | |||
1941 | bool operands_empty() const { return Operands.empty(); } | |||
1942 | ||||
1943 | void pop_front() { Operands.erase(Operands.begin()); } | |||
1944 | ||||
1945 | void optimize(); | |||
1946 | ||||
1947 | /// Emit MatchTable opcodes that test whether the instruction named in | |||
1948 | /// InsnVarName matches all the predicates and all the operands. | |||
1949 | void emitPredicateOpcodes(MatchTable &Table, RuleMatcher &Rule) { | |||
1950 | if (NumOperandsCheck) | |||
1951 | InstructionNumOperandsMatcher(InsnVarID, getNumOperands()) | |||
1952 | .emitPredicateOpcodes(Table, Rule); | |||
1953 | ||||
1954 | emitPredicateListOpcodes(Table, Rule); | |||
1955 | ||||
1956 | for (const auto &Operand : Operands) | |||
1957 | Operand->emitPredicateOpcodes(Table, Rule); | |||
1958 | } | |||
1959 | ||||
1960 | /// Compare the priority of this object and B. | |||
1961 | /// | |||
1962 | /// Returns true if this object is more important than B. | |||
1963 | bool isHigherPriorityThan(InstructionMatcher &B) { | |||
1964 | // Instruction matchers involving more operands have higher priority. | |||
1965 | if (Operands.size() > B.Operands.size()) | |||
1966 | return true; | |||
1967 | if (Operands.size() < B.Operands.size()) | |||
1968 | return false; | |||
1969 | ||||
1970 | for (auto &&P : zip(predicates(), B.predicates())) { | |||
1971 | auto L = static_cast<InstructionPredicateMatcher *>(std::get<0>(P).get()); | |||
1972 | auto R = static_cast<InstructionPredicateMatcher *>(std::get<1>(P).get()); | |||
1973 | if (L->isHigherPriorityThan(*R)) | |||
1974 | return true; | |||
1975 | if (R->isHigherPriorityThan(*L)) | |||
1976 | return false; | |||
1977 | } | |||
1978 | ||||
1979 | for (const auto &Operand : zip(Operands, B.Operands)) { | |||
1980 | if (std::get<0>(Operand)->isHigherPriorityThan(*std::get<1>(Operand))) | |||
1981 | return true; | |||
1982 | if (std::get<1>(Operand)->isHigherPriorityThan(*std::get<0>(Operand))) | |||
1983 | return false; | |||
1984 | } | |||
1985 | ||||
1986 | return false; | |||
1987 | }; | |||
1988 | ||||
1989 | /// Report the maximum number of temporary operands needed by the instruction | |||
1990 | /// matcher. | |||
1991 | unsigned countRendererFns() { | |||
1992 | return std::accumulate( | |||
1993 | predicates().begin(), predicates().end(), 0, | |||
1994 | [](unsigned A, | |||
1995 | const std::unique_ptr<PredicateMatcher> &Predicate) { | |||
1996 | return A + Predicate->countRendererFns(); | |||
1997 | }) + | |||
1998 | std::accumulate( | |||
1999 | Operands.begin(), Operands.end(), 0, | |||
2000 | [](unsigned A, const std::unique_ptr<OperandMatcher> &Operand) { | |||
2001 | return A + Operand->countRendererFns(); | |||
2002 | }); | |||
2003 | } | |||
2004 | ||||
2005 | InstructionOpcodeMatcher &getOpcodeMatcher() { | |||
2006 | for (auto &P : predicates()) | |||
2007 | if (auto *OpMatcher = dyn_cast<InstructionOpcodeMatcher>(P.get())) | |||
2008 | return *OpMatcher; | |||
2009 | llvm_unreachable("Didn't find an opcode matcher")::llvm::llvm_unreachable_internal("Didn't find an opcode matcher" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2009); | |||
2010 | } | |||
2011 | ||||
2012 | bool isConstantInstruction() { | |||
2013 | return getOpcodeMatcher().isConstantInstruction(); | |||
2014 | } | |||
2015 | ||||
2016 | StringRef getOpcode() { return getOpcodeMatcher().getOpcode(); } | |||
2017 | }; | |||
2018 | ||||
2019 | StringRef RuleMatcher::getOpcode() const { | |||
2020 | return Matchers.front()->getOpcode(); | |||
2021 | } | |||
2022 | ||||
2023 | unsigned RuleMatcher::getNumOperands() const { | |||
2024 | return Matchers.front()->getNumOperands(); | |||
2025 | } | |||
2026 | ||||
2027 | LLTCodeGen RuleMatcher::getFirstConditionAsRootType() { | |||
2028 | InstructionMatcher &InsnMatcher = *Matchers.front(); | |||
2029 | if (!InsnMatcher.predicates_empty()) | |||
2030 | if (const auto *TM = | |||
2031 | dyn_cast<LLTOperandMatcher>(&**InsnMatcher.predicates_begin())) | |||
2032 | if (TM->getInsnVarID() == 0 && TM->getOpIdx() == 0) | |||
2033 | return TM->getTy(); | |||
2034 | return {}; | |||
2035 | } | |||
2036 | ||||
2037 | /// Generates code to check that the operand is a register defined by an | |||
2038 | /// instruction that matches the given instruction matcher. | |||
2039 | /// | |||
2040 | /// For example, the pattern: | |||
2041 | /// (set $dst, (G_MUL (G_ADD $src1, $src2), $src3)) | |||
2042 | /// would use an InstructionOperandMatcher for operand 1 of the G_MUL to match | |||
2043 | /// the: | |||
2044 | /// (G_ADD $src1, $src2) | |||
2045 | /// subpattern. | |||
2046 | class InstructionOperandMatcher : public OperandPredicateMatcher { | |||
2047 | protected: | |||
2048 | std::unique_ptr<InstructionMatcher> InsnMatcher; | |||
2049 | ||||
2050 | public: | |||
2051 | InstructionOperandMatcher(unsigned InsnVarID, unsigned OpIdx, | |||
2052 | RuleMatcher &Rule, StringRef SymbolicName) | |||
2053 | : OperandPredicateMatcher(OPM_Instruction, InsnVarID, OpIdx), | |||
2054 | InsnMatcher(new InstructionMatcher(Rule, SymbolicName)) {} | |||
2055 | ||||
2056 | static bool classof(const PredicateMatcher *P) { | |||
2057 | return P->getKind() == OPM_Instruction; | |||
2058 | } | |||
2059 | ||||
2060 | InstructionMatcher &getInsnMatcher() const { return *InsnMatcher; } | |||
2061 | ||||
2062 | void emitCaptureOpcodes(MatchTable &Table, RuleMatcher &Rule) const { | |||
2063 | const unsigned NewInsnVarID = InsnMatcher->getInsnVarID(); | |||
2064 | Table << MatchTable::Opcode("GIM_RecordInsn") | |||
2065 | << MatchTable::Comment("DefineMI") | |||
2066 | << MatchTable::IntValue(NewInsnVarID) << MatchTable::Comment("MI") | |||
2067 | << MatchTable::IntValue(getInsnVarID()) | |||
2068 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(getOpIdx()) | |||
2069 | << MatchTable::Comment("MIs[" + llvm::to_string(NewInsnVarID) + "]") | |||
2070 | << MatchTable::LineBreak; | |||
2071 | } | |||
2072 | ||||
2073 | void emitPredicateOpcodes(MatchTable &Table, | |||
2074 | RuleMatcher &Rule) const override { | |||
2075 | emitCaptureOpcodes(Table, Rule); | |||
2076 | InsnMatcher->emitPredicateOpcodes(Table, Rule); | |||
2077 | } | |||
2078 | ||||
2079 | bool isHigherPriorityThan(const OperandPredicateMatcher &B) const override { | |||
2080 | if (OperandPredicateMatcher::isHigherPriorityThan(B)) | |||
2081 | return true; | |||
2082 | if (B.OperandPredicateMatcher::isHigherPriorityThan(*this)) | |||
2083 | return false; | |||
2084 | ||||
2085 | if (const InstructionOperandMatcher *BP = | |||
2086 | dyn_cast<InstructionOperandMatcher>(&B)) | |||
2087 | if (InsnMatcher->isHigherPriorityThan(*BP->InsnMatcher)) | |||
2088 | return true; | |||
2089 | return false; | |||
2090 | } | |||
2091 | }; | |||
2092 | ||||
2093 | void InstructionMatcher::optimize() { | |||
2094 | SmallVector<std::unique_ptr<PredicateMatcher>, 8> Stash; | |||
2095 | const auto &OpcMatcher = getOpcodeMatcher(); | |||
2096 | ||||
2097 | Stash.push_back(predicates_pop_front()); | |||
2098 | if (Stash.back().get() == &OpcMatcher) { | |||
2099 | if (NumOperandsCheck && OpcMatcher.getNumOperands() < getNumOperands()) | |||
2100 | Stash.emplace_back( | |||
2101 | new InstructionNumOperandsMatcher(InsnVarID, getNumOperands())); | |||
2102 | NumOperandsCheck = false; | |||
2103 | ||||
2104 | for (auto &OM : Operands) | |||
2105 | for (auto &OP : OM->predicates()) | |||
2106 | if (isa<IntrinsicIDOperandMatcher>(OP)) { | |||
2107 | Stash.push_back(std::move(OP)); | |||
2108 | OM->eraseNullPredicates(); | |||
2109 | break; | |||
2110 | } | |||
2111 | } | |||
2112 | ||||
2113 | if (InsnVarID > 0) { | |||
2114 | assert(!Operands.empty() && "Nested instruction is expected to def a vreg")((!Operands.empty() && "Nested instruction is expected to def a vreg" ) ? static_cast<void> (0) : __assert_fail ("!Operands.empty() && \"Nested instruction is expected to def a vreg\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2114, __PRETTY_FUNCTION__)); | |||
2115 | for (auto &OP : Operands[0]->predicates()) | |||
2116 | OP.reset(); | |||
2117 | Operands[0]->eraseNullPredicates(); | |||
2118 | } | |||
2119 | for (auto &OM : Operands) { | |||
2120 | for (auto &OP : OM->predicates()) | |||
2121 | if (isa<LLTOperandMatcher>(OP)) | |||
2122 | Stash.push_back(std::move(OP)); | |||
2123 | OM->eraseNullPredicates(); | |||
2124 | } | |||
2125 | while (!Stash.empty()) | |||
2126 | prependPredicate(Stash.pop_back_val()); | |||
2127 | } | |||
2128 | ||||
2129 | //===- Actions ------------------------------------------------------------===// | |||
2130 | class OperandRenderer { | |||
2131 | public: | |||
2132 | enum RendererKind { | |||
2133 | OR_Copy, | |||
2134 | OR_CopyOrAddZeroReg, | |||
2135 | OR_CopySubReg, | |||
2136 | OR_CopyConstantAsImm, | |||
2137 | OR_CopyFConstantAsFPImm, | |||
2138 | OR_Imm, | |||
2139 | OR_Register, | |||
2140 | OR_TempRegister, | |||
2141 | OR_ComplexPattern, | |||
2142 | OR_Custom | |||
2143 | }; | |||
2144 | ||||
2145 | protected: | |||
2146 | RendererKind Kind; | |||
2147 | ||||
2148 | public: | |||
2149 | OperandRenderer(RendererKind Kind) : Kind(Kind) {} | |||
2150 | virtual ~OperandRenderer() {} | |||
2151 | ||||
2152 | RendererKind getKind() const { return Kind; } | |||
2153 | ||||
2154 | virtual void emitRenderOpcodes(MatchTable &Table, | |||
2155 | RuleMatcher &Rule) const = 0; | |||
2156 | }; | |||
2157 | ||||
2158 | /// A CopyRenderer emits code to copy a single operand from an existing | |||
2159 | /// instruction to the one being built. | |||
2160 | class CopyRenderer : public OperandRenderer { | |||
2161 | protected: | |||
2162 | unsigned NewInsnID; | |||
2163 | /// The name of the operand. | |||
2164 | const StringRef SymbolicName; | |||
2165 | ||||
2166 | public: | |||
2167 | CopyRenderer(unsigned NewInsnID, StringRef SymbolicName) | |||
2168 | : OperandRenderer(OR_Copy), NewInsnID(NewInsnID), | |||
2169 | SymbolicName(SymbolicName) { | |||
2170 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source")((!SymbolicName.empty() && "Cannot copy from an unspecified source" ) ? static_cast<void> (0) : __assert_fail ("!SymbolicName.empty() && \"Cannot copy from an unspecified source\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2170, __PRETTY_FUNCTION__)); | |||
2171 | } | |||
2172 | ||||
2173 | static bool classof(const OperandRenderer *R) { | |||
2174 | return R->getKind() == OR_Copy; | |||
2175 | } | |||
2176 | ||||
2177 | const StringRef getSymbolicName() const { return SymbolicName; } | |||
2178 | ||||
2179 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2180 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); | |||
2181 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); | |||
2182 | Table << MatchTable::Opcode("GIR_Copy") << MatchTable::Comment("NewInsnID") | |||
2183 | << MatchTable::IntValue(NewInsnID) << MatchTable::Comment("OldInsnID") | |||
2184 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") | |||
2185 | << MatchTable::IntValue(Operand.getOpIdx()) | |||
2186 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; | |||
2187 | } | |||
2188 | }; | |||
2189 | ||||
2190 | /// A CopyOrAddZeroRegRenderer emits code to copy a single operand from an | |||
2191 | /// existing instruction to the one being built. If the operand turns out to be | |||
2192 | /// a 'G_CONSTANT 0' then it replaces the operand with a zero register. | |||
2193 | class CopyOrAddZeroRegRenderer : public OperandRenderer { | |||
2194 | protected: | |||
2195 | unsigned NewInsnID; | |||
2196 | /// The name of the operand. | |||
2197 | const StringRef SymbolicName; | |||
2198 | const Record *ZeroRegisterDef; | |||
2199 | ||||
2200 | public: | |||
2201 | CopyOrAddZeroRegRenderer(unsigned NewInsnID, | |||
2202 | StringRef SymbolicName, Record *ZeroRegisterDef) | |||
2203 | : OperandRenderer(OR_CopyOrAddZeroReg), NewInsnID(NewInsnID), | |||
2204 | SymbolicName(SymbolicName), ZeroRegisterDef(ZeroRegisterDef) { | |||
2205 | assert(!SymbolicName.empty() && "Cannot copy from an unspecified source")((!SymbolicName.empty() && "Cannot copy from an unspecified source" ) ? static_cast<void> (0) : __assert_fail ("!SymbolicName.empty() && \"Cannot copy from an unspecified source\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2205, __PRETTY_FUNCTION__)); | |||
2206 | } | |||
2207 | ||||
2208 | static bool classof(const OperandRenderer *R) { | |||
2209 | return R->getKind() == OR_CopyOrAddZeroReg; | |||
2210 | } | |||
2211 | ||||
2212 | const StringRef getSymbolicName() const { return SymbolicName; } | |||
2213 | ||||
2214 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2215 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); | |||
2216 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); | |||
2217 | Table << MatchTable::Opcode("GIR_CopyOrAddZeroReg") | |||
2218 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) | |||
2219 | << MatchTable::Comment("OldInsnID") | |||
2220 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") | |||
2221 | << MatchTable::IntValue(Operand.getOpIdx()) | |||
2222 | << MatchTable::NamedValue( | |||
2223 | (ZeroRegisterDef->getValue("Namespace") | |||
2224 | ? ZeroRegisterDef->getValueAsString("Namespace") | |||
2225 | : ""), | |||
2226 | ZeroRegisterDef->getName()) | |||
2227 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; | |||
2228 | } | |||
2229 | }; | |||
2230 | ||||
2231 | /// A CopyConstantAsImmRenderer emits code to render a G_CONSTANT instruction to | |||
2232 | /// an extended immediate operand. | |||
2233 | class CopyConstantAsImmRenderer : public OperandRenderer { | |||
2234 | protected: | |||
2235 | unsigned NewInsnID; | |||
2236 | /// The name of the operand. | |||
2237 | const std::string SymbolicName; | |||
2238 | bool Signed; | |||
2239 | ||||
2240 | public: | |||
2241 | CopyConstantAsImmRenderer(unsigned NewInsnID, StringRef SymbolicName) | |||
2242 | : OperandRenderer(OR_CopyConstantAsImm), NewInsnID(NewInsnID), | |||
2243 | SymbolicName(SymbolicName), Signed(true) {} | |||
2244 | ||||
2245 | static bool classof(const OperandRenderer *R) { | |||
2246 | return R->getKind() == OR_CopyConstantAsImm; | |||
2247 | } | |||
2248 | ||||
2249 | const StringRef getSymbolicName() const { return SymbolicName; } | |||
2250 | ||||
2251 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2252 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); | |||
2253 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); | |||
2254 | Table << MatchTable::Opcode(Signed ? "GIR_CopyConstantAsSImm" | |||
2255 | : "GIR_CopyConstantAsUImm") | |||
2256 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) | |||
2257 | << MatchTable::Comment("OldInsnID") | |||
2258 | << MatchTable::IntValue(OldInsnVarID) | |||
2259 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; | |||
2260 | } | |||
2261 | }; | |||
2262 | ||||
2263 | /// A CopyFConstantAsFPImmRenderer emits code to render a G_FCONSTANT | |||
2264 | /// instruction to an extended immediate operand. | |||
2265 | class CopyFConstantAsFPImmRenderer : public OperandRenderer { | |||
2266 | protected: | |||
2267 | unsigned NewInsnID; | |||
2268 | /// The name of the operand. | |||
2269 | const std::string SymbolicName; | |||
2270 | ||||
2271 | public: | |||
2272 | CopyFConstantAsFPImmRenderer(unsigned NewInsnID, StringRef SymbolicName) | |||
2273 | : OperandRenderer(OR_CopyFConstantAsFPImm), NewInsnID(NewInsnID), | |||
2274 | SymbolicName(SymbolicName) {} | |||
2275 | ||||
2276 | static bool classof(const OperandRenderer *R) { | |||
2277 | return R->getKind() == OR_CopyFConstantAsFPImm; | |||
2278 | } | |||
2279 | ||||
2280 | const StringRef getSymbolicName() const { return SymbolicName; } | |||
2281 | ||||
2282 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2283 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); | |||
2284 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); | |||
2285 | Table << MatchTable::Opcode("GIR_CopyFConstantAsFPImm") | |||
2286 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) | |||
2287 | << MatchTable::Comment("OldInsnID") | |||
2288 | << MatchTable::IntValue(OldInsnVarID) | |||
2289 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; | |||
2290 | } | |||
2291 | }; | |||
2292 | ||||
2293 | /// A CopySubRegRenderer emits code to copy a single register operand from an | |||
2294 | /// existing instruction to the one being built and indicate that only a | |||
2295 | /// subregister should be copied. | |||
2296 | class CopySubRegRenderer : public OperandRenderer { | |||
2297 | protected: | |||
2298 | unsigned NewInsnID; | |||
2299 | /// The name of the operand. | |||
2300 | const StringRef SymbolicName; | |||
2301 | /// The subregister to extract. | |||
2302 | const CodeGenSubRegIndex *SubReg; | |||
2303 | ||||
2304 | public: | |||
2305 | CopySubRegRenderer(unsigned NewInsnID, StringRef SymbolicName, | |||
2306 | const CodeGenSubRegIndex *SubReg) | |||
2307 | : OperandRenderer(OR_CopySubReg), NewInsnID(NewInsnID), | |||
2308 | SymbolicName(SymbolicName), SubReg(SubReg) {} | |||
2309 | ||||
2310 | static bool classof(const OperandRenderer *R) { | |||
2311 | return R->getKind() == OR_CopySubReg; | |||
2312 | } | |||
2313 | ||||
2314 | const StringRef getSymbolicName() const { return SymbolicName; } | |||
2315 | ||||
2316 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2317 | const OperandMatcher &Operand = Rule.getOperandMatcher(SymbolicName); | |||
2318 | unsigned OldInsnVarID = Rule.getInsnVarID(Operand.getInstructionMatcher()); | |||
2319 | Table << MatchTable::Opcode("GIR_CopySubReg") | |||
2320 | << MatchTable::Comment("NewInsnID") << MatchTable::IntValue(NewInsnID) | |||
2321 | << MatchTable::Comment("OldInsnID") | |||
2322 | << MatchTable::IntValue(OldInsnVarID) << MatchTable::Comment("OpIdx") | |||
2323 | << MatchTable::IntValue(Operand.getOpIdx()) | |||
2324 | << MatchTable::Comment("SubRegIdx") | |||
2325 | << MatchTable::IntValue(SubReg->EnumValue) | |||
2326 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; | |||
2327 | } | |||
2328 | }; | |||
2329 | ||||
2330 | /// Adds a specific physical register to the instruction being built. | |||
2331 | /// This is typically useful for WZR/XZR on AArch64. | |||
2332 | class AddRegisterRenderer : public OperandRenderer { | |||
2333 | protected: | |||
2334 | unsigned InsnID; | |||
2335 | const Record *RegisterDef; | |||
2336 | ||||
2337 | public: | |||
2338 | AddRegisterRenderer(unsigned InsnID, const Record *RegisterDef) | |||
2339 | : OperandRenderer(OR_Register), InsnID(InsnID), RegisterDef(RegisterDef) { | |||
2340 | } | |||
2341 | ||||
2342 | static bool classof(const OperandRenderer *R) { | |||
2343 | return R->getKind() == OR_Register; | |||
2344 | } | |||
2345 | ||||
2346 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2347 | Table << MatchTable::Opcode("GIR_AddRegister") | |||
2348 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2349 | << MatchTable::NamedValue( | |||
2350 | (RegisterDef->getValue("Namespace") | |||
2351 | ? RegisterDef->getValueAsString("Namespace") | |||
2352 | : ""), | |||
2353 | RegisterDef->getName()) | |||
2354 | << MatchTable::LineBreak; | |||
2355 | } | |||
2356 | }; | |||
2357 | ||||
2358 | /// Adds a specific temporary virtual register to the instruction being built. | |||
2359 | /// This is used to chain instructions together when emitting multiple | |||
2360 | /// instructions. | |||
2361 | class TempRegRenderer : public OperandRenderer { | |||
2362 | protected: | |||
2363 | unsigned InsnID; | |||
2364 | unsigned TempRegID; | |||
2365 | bool IsDef; | |||
2366 | ||||
2367 | public: | |||
2368 | TempRegRenderer(unsigned InsnID, unsigned TempRegID, bool IsDef = false) | |||
2369 | : OperandRenderer(OR_Register), InsnID(InsnID), TempRegID(TempRegID), | |||
2370 | IsDef(IsDef) {} | |||
2371 | ||||
2372 | static bool classof(const OperandRenderer *R) { | |||
2373 | return R->getKind() == OR_TempRegister; | |||
2374 | } | |||
2375 | ||||
2376 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2377 | Table << MatchTable::Opcode("GIR_AddTempRegister") | |||
2378 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2379 | << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID) | |||
2380 | << MatchTable::Comment("TempRegFlags"); | |||
2381 | if (IsDef) | |||
2382 | Table << MatchTable::NamedValue("RegState::Define"); | |||
2383 | else | |||
2384 | Table << MatchTable::IntValue(0); | |||
2385 | Table << MatchTable::LineBreak; | |||
2386 | } | |||
2387 | }; | |||
2388 | ||||
2389 | /// Adds a specific immediate to the instruction being built. | |||
2390 | class ImmRenderer : public OperandRenderer { | |||
2391 | protected: | |||
2392 | unsigned InsnID; | |||
2393 | int64_t Imm; | |||
2394 | ||||
2395 | public: | |||
2396 | ImmRenderer(unsigned InsnID, int64_t Imm) | |||
2397 | : OperandRenderer(OR_Imm), InsnID(InsnID), Imm(Imm) {} | |||
2398 | ||||
2399 | static bool classof(const OperandRenderer *R) { | |||
2400 | return R->getKind() == OR_Imm; | |||
2401 | } | |||
2402 | ||||
2403 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2404 | Table << MatchTable::Opcode("GIR_AddImm") << MatchTable::Comment("InsnID") | |||
2405 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Imm") | |||
2406 | << MatchTable::IntValue(Imm) << MatchTable::LineBreak; | |||
2407 | } | |||
2408 | }; | |||
2409 | ||||
2410 | /// Adds operands by calling a renderer function supplied by the ComplexPattern | |||
2411 | /// matcher function. | |||
2412 | class RenderComplexPatternOperand : public OperandRenderer { | |||
2413 | private: | |||
2414 | unsigned InsnID; | |||
2415 | const Record &TheDef; | |||
2416 | /// The name of the operand. | |||
2417 | const StringRef SymbolicName; | |||
2418 | /// The renderer number. This must be unique within a rule since it's used to | |||
2419 | /// identify a temporary variable to hold the renderer function. | |||
2420 | unsigned RendererID; | |||
2421 | /// When provided, this is the suboperand of the ComplexPattern operand to | |||
2422 | /// render. Otherwise all the suboperands will be rendered. | |||
2423 | Optional<unsigned> SubOperand; | |||
2424 | ||||
2425 | unsigned getNumOperands() const { | |||
2426 | return TheDef.getValueAsDag("Operands")->getNumArgs(); | |||
2427 | } | |||
2428 | ||||
2429 | public: | |||
2430 | RenderComplexPatternOperand(unsigned InsnID, const Record &TheDef, | |||
2431 | StringRef SymbolicName, unsigned RendererID, | |||
2432 | Optional<unsigned> SubOperand = None) | |||
2433 | : OperandRenderer(OR_ComplexPattern), InsnID(InsnID), TheDef(TheDef), | |||
2434 | SymbolicName(SymbolicName), RendererID(RendererID), | |||
2435 | SubOperand(SubOperand) {} | |||
2436 | ||||
2437 | static bool classof(const OperandRenderer *R) { | |||
2438 | return R->getKind() == OR_ComplexPattern; | |||
2439 | } | |||
2440 | ||||
2441 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2442 | Table << MatchTable::Opcode(SubOperand.hasValue() ? "GIR_ComplexSubOperandRenderer" | |||
2443 | : "GIR_ComplexRenderer") | |||
2444 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2445 | << MatchTable::Comment("RendererID") | |||
2446 | << MatchTable::IntValue(RendererID); | |||
2447 | if (SubOperand.hasValue()) | |||
2448 | Table << MatchTable::Comment("SubOperand") | |||
2449 | << MatchTable::IntValue(SubOperand.getValue()); | |||
2450 | Table << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; | |||
2451 | } | |||
2452 | }; | |||
2453 | ||||
2454 | class CustomRenderer : public OperandRenderer { | |||
2455 | protected: | |||
2456 | unsigned InsnID; | |||
2457 | const Record &Renderer; | |||
2458 | /// The name of the operand. | |||
2459 | const std::string SymbolicName; | |||
2460 | ||||
2461 | public: | |||
2462 | CustomRenderer(unsigned InsnID, const Record &Renderer, | |||
2463 | StringRef SymbolicName) | |||
2464 | : OperandRenderer(OR_Custom), InsnID(InsnID), Renderer(Renderer), | |||
2465 | SymbolicName(SymbolicName) {} | |||
2466 | ||||
2467 | static bool classof(const OperandRenderer *R) { | |||
2468 | return R->getKind() == OR_Custom; | |||
2469 | } | |||
2470 | ||||
2471 | void emitRenderOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2472 | InstructionMatcher &InsnMatcher = Rule.getInstructionMatcher(SymbolicName); | |||
2473 | unsigned OldInsnVarID = Rule.getInsnVarID(InsnMatcher); | |||
2474 | Table << MatchTable::Opcode("GIR_CustomRenderer") | |||
2475 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2476 | << MatchTable::Comment("OldInsnID") | |||
2477 | << MatchTable::IntValue(OldInsnVarID) | |||
2478 | << MatchTable::Comment("Renderer") | |||
2479 | << MatchTable::NamedValue( | |||
2480 | "GICR_" + Renderer.getValueAsString("RendererFn").str()) | |||
2481 | << MatchTable::Comment(SymbolicName) << MatchTable::LineBreak; | |||
2482 | } | |||
2483 | }; | |||
2484 | ||||
2485 | /// An action taken when all Matcher predicates succeeded for a parent rule. | |||
2486 | /// | |||
2487 | /// Typical actions include: | |||
2488 | /// * Changing the opcode of an instruction. | |||
2489 | /// * Adding an operand to an instruction. | |||
2490 | class MatchAction { | |||
2491 | public: | |||
2492 | virtual ~MatchAction() {} | |||
2493 | ||||
2494 | /// Emit the MatchTable opcodes to implement the action. | |||
2495 | virtual void emitActionOpcodes(MatchTable &Table, | |||
2496 | RuleMatcher &Rule) const = 0; | |||
2497 | }; | |||
2498 | ||||
2499 | /// Generates a comment describing the matched rule being acted upon. | |||
2500 | class DebugCommentAction : public MatchAction { | |||
2501 | private: | |||
2502 | std::string S; | |||
2503 | ||||
2504 | public: | |||
2505 | DebugCommentAction(StringRef S) : S(S) {} | |||
2506 | ||||
2507 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2508 | Table << MatchTable::Comment(S) << MatchTable::LineBreak; | |||
2509 | } | |||
2510 | }; | |||
2511 | ||||
2512 | /// Generates code to build an instruction or mutate an existing instruction | |||
2513 | /// into the desired instruction when this is possible. | |||
2514 | class BuildMIAction : public MatchAction { | |||
2515 | private: | |||
2516 | unsigned InsnID; | |||
2517 | const CodeGenInstruction *I; | |||
2518 | InstructionMatcher *Matched; | |||
2519 | std::vector<std::unique_ptr<OperandRenderer>> OperandRenderers; | |||
2520 | ||||
2521 | /// True if the instruction can be built solely by mutating the opcode. | |||
2522 | bool canMutate(RuleMatcher &Rule, const InstructionMatcher *Insn) const { | |||
2523 | if (!Insn) | |||
2524 | return false; | |||
2525 | ||||
2526 | if (OperandRenderers.size() != Insn->getNumOperands()) | |||
2527 | return false; | |||
2528 | ||||
2529 | for (const auto &Renderer : enumerate(OperandRenderers)) { | |||
2530 | if (const auto *Copy = dyn_cast<CopyRenderer>(&*Renderer.value())) { | |||
2531 | const OperandMatcher &OM = Rule.getOperandMatcher(Copy->getSymbolicName()); | |||
2532 | if (Insn != &OM.getInstructionMatcher() || | |||
2533 | OM.getOpIdx() != Renderer.index()) | |||
2534 | return false; | |||
2535 | } else | |||
2536 | return false; | |||
2537 | } | |||
2538 | ||||
2539 | return true; | |||
2540 | } | |||
2541 | ||||
2542 | public: | |||
2543 | BuildMIAction(unsigned InsnID, const CodeGenInstruction *I) | |||
2544 | : InsnID(InsnID), I(I), Matched(nullptr) {} | |||
2545 | ||||
2546 | unsigned getInsnID() const { return InsnID; } | |||
2547 | const CodeGenInstruction *getCGI() const { return I; } | |||
2548 | ||||
2549 | void chooseInsnToMutate(RuleMatcher &Rule) { | |||
2550 | for (auto *MutateCandidate : Rule.mutatable_insns()) { | |||
2551 | if (canMutate(Rule, MutateCandidate)) { | |||
2552 | // Take the first one we're offered that we're able to mutate. | |||
2553 | Rule.reserveInsnMatcherForMutation(MutateCandidate); | |||
2554 | Matched = MutateCandidate; | |||
2555 | return; | |||
2556 | } | |||
2557 | } | |||
2558 | } | |||
2559 | ||||
2560 | template <class Kind, class... Args> | |||
2561 | Kind &addRenderer(Args&&... args) { | |||
2562 | OperandRenderers.emplace_back( | |||
2563 | llvm::make_unique<Kind>(InsnID, std::forward<Args>(args)...)); | |||
2564 | return *static_cast<Kind *>(OperandRenderers.back().get()); | |||
2565 | } | |||
2566 | ||||
2567 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2568 | if (Matched) { | |||
2569 | assert(canMutate(Rule, Matched) &&((canMutate(Rule, Matched) && "Arranged to mutate an insn that isn't mutatable" ) ? static_cast<void> (0) : __assert_fail ("canMutate(Rule, Matched) && \"Arranged to mutate an insn that isn't mutatable\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2570, __PRETTY_FUNCTION__)) | |||
2570 | "Arranged to mutate an insn that isn't mutatable")((canMutate(Rule, Matched) && "Arranged to mutate an insn that isn't mutatable" ) ? static_cast<void> (0) : __assert_fail ("canMutate(Rule, Matched) && \"Arranged to mutate an insn that isn't mutatable\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2570, __PRETTY_FUNCTION__)); | |||
2571 | ||||
2572 | unsigned RecycleInsnID = Rule.getInsnVarID(*Matched); | |||
2573 | Table << MatchTable::Opcode("GIR_MutateOpcode") | |||
2574 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2575 | << MatchTable::Comment("RecycleInsnID") | |||
2576 | << MatchTable::IntValue(RecycleInsnID) | |||
2577 | << MatchTable::Comment("Opcode") | |||
2578 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) | |||
2579 | << MatchTable::LineBreak; | |||
2580 | ||||
2581 | if (!I->ImplicitDefs.empty() || !I->ImplicitUses.empty()) { | |||
2582 | for (auto Def : I->ImplicitDefs) { | |||
2583 | auto Namespace = Def->getValue("Namespace") | |||
2584 | ? Def->getValueAsString("Namespace") | |||
2585 | : ""; | |||
2586 | Table << MatchTable::Opcode("GIR_AddImplicitDef") | |||
2587 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2588 | << MatchTable::NamedValue(Namespace, Def->getName()) | |||
2589 | << MatchTable::LineBreak; | |||
2590 | } | |||
2591 | for (auto Use : I->ImplicitUses) { | |||
2592 | auto Namespace = Use->getValue("Namespace") | |||
2593 | ? Use->getValueAsString("Namespace") | |||
2594 | : ""; | |||
2595 | Table << MatchTable::Opcode("GIR_AddImplicitUse") | |||
2596 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2597 | << MatchTable::NamedValue(Namespace, Use->getName()) | |||
2598 | << MatchTable::LineBreak; | |||
2599 | } | |||
2600 | } | |||
2601 | return; | |||
2602 | } | |||
2603 | ||||
2604 | // TODO: Simple permutation looks like it could be almost as common as | |||
2605 | // mutation due to commutative operations. | |||
2606 | ||||
2607 | Table << MatchTable::Opcode("GIR_BuildMI") << MatchTable::Comment("InsnID") | |||
2608 | << MatchTable::IntValue(InsnID) << MatchTable::Comment("Opcode") | |||
2609 | << MatchTable::NamedValue(I->Namespace, I->TheDef->getName()) | |||
2610 | << MatchTable::LineBreak; | |||
2611 | for (const auto &Renderer : OperandRenderers) | |||
2612 | Renderer->emitRenderOpcodes(Table, Rule); | |||
2613 | ||||
2614 | if (I->mayLoad || I->mayStore) { | |||
2615 | Table << MatchTable::Opcode("GIR_MergeMemOperands") | |||
2616 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2617 | << MatchTable::Comment("MergeInsnID's"); | |||
2618 | // Emit the ID's for all the instructions that are matched by this rule. | |||
2619 | // TODO: Limit this to matched instructions that mayLoad/mayStore or have | |||
2620 | // some other means of having a memoperand. Also limit this to | |||
2621 | // emitted instructions that expect to have a memoperand too. For | |||
2622 | // example, (G_SEXT (G_LOAD x)) that results in separate load and | |||
2623 | // sign-extend instructions shouldn't put the memoperand on the | |||
2624 | // sign-extend since it has no effect there. | |||
2625 | std::vector<unsigned> MergeInsnIDs; | |||
2626 | for (const auto &IDMatcherPair : Rule.defined_insn_vars()) | |||
2627 | MergeInsnIDs.push_back(IDMatcherPair.second); | |||
2628 | llvm::sort(MergeInsnIDs); | |||
2629 | for (const auto &MergeInsnID : MergeInsnIDs) | |||
2630 | Table << MatchTable::IntValue(MergeInsnID); | |||
2631 | Table << MatchTable::NamedValue("GIU_MergeMemOperands_EndOfList") | |||
2632 | << MatchTable::LineBreak; | |||
2633 | } | |||
2634 | ||||
2635 | // FIXME: This is a hack but it's sufficient for ISel. We'll need to do | |||
2636 | // better for combines. Particularly when there are multiple match | |||
2637 | // roots. | |||
2638 | if (InsnID == 0) | |||
2639 | Table << MatchTable::Opcode("GIR_EraseFromParent") | |||
2640 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2641 | << MatchTable::LineBreak; | |||
2642 | } | |||
2643 | }; | |||
2644 | ||||
2645 | /// Generates code to constrain the operands of an output instruction to the | |||
2646 | /// register classes specified by the definition of that instruction. | |||
2647 | class ConstrainOperandsToDefinitionAction : public MatchAction { | |||
2648 | unsigned InsnID; | |||
2649 | ||||
2650 | public: | |||
2651 | ConstrainOperandsToDefinitionAction(unsigned InsnID) : InsnID(InsnID) {} | |||
2652 | ||||
2653 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2654 | Table << MatchTable::Opcode("GIR_ConstrainSelectedInstOperands") | |||
2655 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2656 | << MatchTable::LineBreak; | |||
2657 | } | |||
2658 | }; | |||
2659 | ||||
2660 | /// Generates code to constrain the specified operand of an output instruction | |||
2661 | /// to the specified register class. | |||
2662 | class ConstrainOperandToRegClassAction : public MatchAction { | |||
2663 | unsigned InsnID; | |||
2664 | unsigned OpIdx; | |||
2665 | const CodeGenRegisterClass &RC; | |||
2666 | ||||
2667 | public: | |||
2668 | ConstrainOperandToRegClassAction(unsigned InsnID, unsigned OpIdx, | |||
2669 | const CodeGenRegisterClass &RC) | |||
2670 | : InsnID(InsnID), OpIdx(OpIdx), RC(RC) {} | |||
2671 | ||||
2672 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2673 | Table << MatchTable::Opcode("GIR_ConstrainOperandRC") | |||
2674 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2675 | << MatchTable::Comment("Op") << MatchTable::IntValue(OpIdx) | |||
2676 | << MatchTable::Comment("RC " + RC.getName()) | |||
2677 | << MatchTable::IntValue(RC.EnumValue) << MatchTable::LineBreak; | |||
2678 | } | |||
2679 | }; | |||
2680 | ||||
2681 | /// Generates code to create a temporary register which can be used to chain | |||
2682 | /// instructions together. | |||
2683 | class MakeTempRegisterAction : public MatchAction { | |||
2684 | private: | |||
2685 | LLTCodeGen Ty; | |||
2686 | unsigned TempRegID; | |||
2687 | ||||
2688 | public: | |||
2689 | MakeTempRegisterAction(const LLTCodeGen &Ty, unsigned TempRegID) | |||
2690 | : Ty(Ty), TempRegID(TempRegID) {} | |||
2691 | ||||
2692 | void emitActionOpcodes(MatchTable &Table, RuleMatcher &Rule) const override { | |||
2693 | Table << MatchTable::Opcode("GIR_MakeTempReg") | |||
2694 | << MatchTable::Comment("TempRegID") << MatchTable::IntValue(TempRegID) | |||
2695 | << MatchTable::Comment("TypeID") | |||
2696 | << MatchTable::NamedValue(Ty.getCxxEnumValue()) | |||
2697 | << MatchTable::LineBreak; | |||
2698 | } | |||
2699 | }; | |||
2700 | ||||
2701 | InstructionMatcher &RuleMatcher::addInstructionMatcher(StringRef SymbolicName) { | |||
2702 | Matchers.emplace_back(new InstructionMatcher(*this, SymbolicName)); | |||
2703 | MutatableInsns.insert(Matchers.back().get()); | |||
2704 | return *Matchers.back(); | |||
2705 | } | |||
2706 | ||||
2707 | void RuleMatcher::addRequiredFeature(Record *Feature) { | |||
2708 | RequiredFeatures.push_back(Feature); | |||
2709 | } | |||
2710 | ||||
2711 | const std::vector<Record *> &RuleMatcher::getRequiredFeatures() const { | |||
2712 | return RequiredFeatures; | |||
2713 | } | |||
2714 | ||||
2715 | // Emplaces an action of the specified Kind at the end of the action list. | |||
2716 | // | |||
2717 | // Returns a reference to the newly created action. | |||
2718 | // | |||
2719 | // Like std::vector::emplace_back(), may invalidate all iterators if the new | |||
2720 | // size exceeds the capacity. Otherwise, only invalidates the past-the-end | |||
2721 | // iterator. | |||
2722 | template <class Kind, class... Args> | |||
2723 | Kind &RuleMatcher::addAction(Args &&... args) { | |||
2724 | Actions.emplace_back(llvm::make_unique<Kind>(std::forward<Args>(args)...)); | |||
2725 | return *static_cast<Kind *>(Actions.back().get()); | |||
2726 | } | |||
2727 | ||||
2728 | // Emplaces an action of the specified Kind before the given insertion point. | |||
2729 | // | |||
2730 | // Returns an iterator pointing at the newly created instruction. | |||
2731 | // | |||
2732 | // Like std::vector::insert(), may invalidate all iterators if the new size | |||
2733 | // exceeds the capacity. Otherwise, only invalidates the iterators from the | |||
2734 | // insertion point onwards. | |||
2735 | template <class Kind, class... Args> | |||
2736 | action_iterator RuleMatcher::insertAction(action_iterator InsertPt, | |||
2737 | Args &&... args) { | |||
2738 | return Actions.emplace(InsertPt, | |||
2739 | llvm::make_unique<Kind>(std::forward<Args>(args)...)); | |||
2740 | } | |||
2741 | ||||
2742 | unsigned RuleMatcher::implicitlyDefineInsnVar(InstructionMatcher &Matcher) { | |||
2743 | unsigned NewInsnVarID = NextInsnVarID++; | |||
2744 | InsnVariableIDs[&Matcher] = NewInsnVarID; | |||
2745 | return NewInsnVarID; | |||
2746 | } | |||
2747 | ||||
2748 | unsigned RuleMatcher::getInsnVarID(InstructionMatcher &InsnMatcher) const { | |||
2749 | const auto &I = InsnVariableIDs.find(&InsnMatcher); | |||
2750 | if (I != InsnVariableIDs.end()) | |||
2751 | return I->second; | |||
2752 | llvm_unreachable("Matched Insn was not captured in a local variable")::llvm::llvm_unreachable_internal("Matched Insn was not captured in a local variable" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2752); | |||
2753 | } | |||
2754 | ||||
2755 | void RuleMatcher::defineOperand(StringRef SymbolicName, OperandMatcher &OM) { | |||
2756 | if (DefinedOperands.find(SymbolicName) == DefinedOperands.end()) { | |||
2757 | DefinedOperands[SymbolicName] = &OM; | |||
2758 | return; | |||
2759 | } | |||
2760 | ||||
2761 | // If the operand is already defined, then we must ensure both references in | |||
2762 | // the matcher have the exact same node. | |||
2763 | OM.addPredicate<SameOperandMatcher>(OM.getSymbolicName()); | |||
2764 | } | |||
2765 | ||||
2766 | InstructionMatcher & | |||
2767 | RuleMatcher::getInstructionMatcher(StringRef SymbolicName) const { | |||
2768 | for (const auto &I : InsnVariableIDs) | |||
2769 | if (I.first->getSymbolicName() == SymbolicName) | |||
2770 | return *I.first; | |||
2771 | llvm_unreachable(::llvm::llvm_unreachable_internal(("Failed to lookup instruction " + SymbolicName).str().c_str(), "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2772) | |||
2772 | ("Failed to lookup instruction " + SymbolicName).str().c_str())::llvm::llvm_unreachable_internal(("Failed to lookup instruction " + SymbolicName).str().c_str(), "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2772); | |||
2773 | } | |||
2774 | ||||
2775 | const OperandMatcher & | |||
2776 | RuleMatcher::getOperandMatcher(StringRef Name) const { | |||
2777 | const auto &I = DefinedOperands.find(Name); | |||
2778 | ||||
2779 | if (I == DefinedOperands.end()) | |||
2780 | PrintFatalError(SrcLoc, "Operand " + Name + " was not declared in matcher"); | |||
2781 | ||||
2782 | return *I->second; | |||
2783 | } | |||
2784 | ||||
2785 | void RuleMatcher::emit(MatchTable &Table) { | |||
2786 | if (Matchers.empty()) | |||
2787 | llvm_unreachable("Unexpected empty matcher!")::llvm::llvm_unreachable_internal("Unexpected empty matcher!" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2787); | |||
2788 | ||||
2789 | // The representation supports rules that require multiple roots such as: | |||
2790 | // %ptr(p0) = ... | |||
2791 | // %elt0(s32) = G_LOAD %ptr | |||
2792 | // %1(p0) = G_ADD %ptr, 4 | |||
2793 | // %elt1(s32) = G_LOAD p0 %1 | |||
2794 | // which could be usefully folded into: | |||
2795 | // %ptr(p0) = ... | |||
2796 | // %elt0(s32), %elt1(s32) = TGT_LOAD_PAIR %ptr | |||
2797 | // on some targets but we don't need to make use of that yet. | |||
2798 | assert(Matchers.size() == 1 && "Cannot handle multi-root matchers yet")((Matchers.size() == 1 && "Cannot handle multi-root matchers yet" ) ? static_cast<void> (0) : __assert_fail ("Matchers.size() == 1 && \"Cannot handle multi-root matchers yet\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2798, __PRETTY_FUNCTION__)); | |||
2799 | ||||
2800 | unsigned LabelID = Table.allocateLabelID(); | |||
2801 | Table << MatchTable::Opcode("GIM_Try", +1) | |||
2802 | << MatchTable::Comment("On fail goto") | |||
2803 | << MatchTable::JumpTarget(LabelID) | |||
2804 | << MatchTable::Comment(("Rule ID " + Twine(RuleID) + " //").str()) | |||
2805 | << MatchTable::LineBreak; | |||
2806 | ||||
2807 | if (!RequiredFeatures.empty()) { | |||
2808 | Table << MatchTable::Opcode("GIM_CheckFeatures") | |||
2809 | << MatchTable::NamedValue(getNameForFeatureBitset(RequiredFeatures)) | |||
2810 | << MatchTable::LineBreak; | |||
2811 | } | |||
2812 | ||||
2813 | Matchers.front()->emitPredicateOpcodes(Table, *this); | |||
2814 | ||||
2815 | // We must also check if it's safe to fold the matched instructions. | |||
2816 | if (InsnVariableIDs.size() >= 2) { | |||
2817 | // Invert the map to create stable ordering (by var names) | |||
2818 | SmallVector<unsigned, 2> InsnIDs; | |||
2819 | for (const auto &Pair : InsnVariableIDs) { | |||
2820 | // Skip the root node since it isn't moving anywhere. Everything else is | |||
2821 | // sinking to meet it. | |||
2822 | if (Pair.first == Matchers.front().get()) | |||
2823 | continue; | |||
2824 | ||||
2825 | InsnIDs.push_back(Pair.second); | |||
2826 | } | |||
2827 | llvm::sort(InsnIDs); | |||
2828 | ||||
2829 | for (const auto &InsnID : InsnIDs) { | |||
2830 | // Reject the difficult cases until we have a more accurate check. | |||
2831 | Table << MatchTable::Opcode("GIM_CheckIsSafeToFold") | |||
2832 | << MatchTable::Comment("InsnID") << MatchTable::IntValue(InsnID) | |||
2833 | << MatchTable::LineBreak; | |||
2834 | ||||
2835 | // FIXME: Emit checks to determine it's _actually_ safe to fold and/or | |||
2836 | // account for unsafe cases. | |||
2837 | // | |||
2838 | // Example: | |||
2839 | // MI1--> %0 = ... | |||
2840 | // %1 = ... %0 | |||
2841 | // MI0--> %2 = ... %0 | |||
2842 | // It's not safe to erase MI1. We currently handle this by not | |||
2843 | // erasing %0 (even when it's dead). | |||
2844 | // | |||
2845 | // Example: | |||
2846 | // MI1--> %0 = load volatile @a | |||
2847 | // %1 = load volatile @a | |||
2848 | // MI0--> %2 = ... %0 | |||
2849 | // It's not safe to sink %0's def past %1. We currently handle | |||
2850 | // this by rejecting all loads. | |||
2851 | // | |||
2852 | // Example: | |||
2853 | // MI1--> %0 = load @a | |||
2854 | // %1 = store @a | |||
2855 | // MI0--> %2 = ... %0 | |||
2856 | // It's not safe to sink %0's def past %1. We currently handle | |||
2857 | // this by rejecting all loads. | |||
2858 | // | |||
2859 | // Example: | |||
2860 | // G_CONDBR %cond, @BB1 | |||
2861 | // BB0: | |||
2862 | // MI1--> %0 = load @a | |||
2863 | // G_BR @BB1 | |||
2864 | // BB1: | |||
2865 | // MI0--> %2 = ... %0 | |||
2866 | // It's not always safe to sink %0 across control flow. In this | |||
2867 | // case it may introduce a memory fault. We currentl handle this | |||
2868 | // by rejecting all loads. | |||
2869 | } | |||
2870 | } | |||
2871 | ||||
2872 | for (const auto &PM : EpilogueMatchers) | |||
2873 | PM->emitPredicateOpcodes(Table, *this); | |||
2874 | ||||
2875 | for (const auto &MA : Actions) | |||
2876 | MA->emitActionOpcodes(Table, *this); | |||
2877 | ||||
2878 | if (Table.isWithCoverage()) | |||
2879 | Table << MatchTable::Opcode("GIR_Coverage") << MatchTable::IntValue(RuleID) | |||
2880 | << MatchTable::LineBreak; | |||
2881 | else | |||
2882 | Table << MatchTable::Comment(("GIR_Coverage, " + Twine(RuleID) + ",").str()) | |||
2883 | << MatchTable::LineBreak; | |||
2884 | ||||
2885 | Table << MatchTable::Opcode("GIR_Done", -1) << MatchTable::LineBreak | |||
2886 | << MatchTable::Label(LabelID); | |||
2887 | ++NumPatternEmitted; | |||
2888 | } | |||
2889 | ||||
2890 | bool RuleMatcher::isHigherPriorityThan(const RuleMatcher &B) const { | |||
2891 | // Rules involving more match roots have higher priority. | |||
2892 | if (Matchers.size() > B.Matchers.size()) | |||
2893 | return true; | |||
2894 | if (Matchers.size() < B.Matchers.size()) | |||
2895 | return false; | |||
2896 | ||||
2897 | for (const auto &Matcher : zip(Matchers, B.Matchers)) { | |||
2898 | if (std::get<0>(Matcher)->isHigherPriorityThan(*std::get<1>(Matcher))) | |||
2899 | return true; | |||
2900 | if (std::get<1>(Matcher)->isHigherPriorityThan(*std::get<0>(Matcher))) | |||
2901 | return false; | |||
2902 | } | |||
2903 | ||||
2904 | return false; | |||
2905 | } | |||
2906 | ||||
2907 | unsigned RuleMatcher::countRendererFns() const { | |||
2908 | return std::accumulate( | |||
2909 | Matchers.begin(), Matchers.end(), 0, | |||
2910 | [](unsigned A, const std::unique_ptr<InstructionMatcher> &Matcher) { | |||
2911 | return A + Matcher->countRendererFns(); | |||
2912 | }); | |||
2913 | } | |||
2914 | ||||
2915 | bool OperandPredicateMatcher::isHigherPriorityThan( | |||
2916 | const OperandPredicateMatcher &B) const { | |||
2917 | // Generally speaking, an instruction is more important than an Int or a | |||
2918 | // LiteralInt because it can cover more nodes but theres an exception to | |||
2919 | // this. G_CONSTANT's are less important than either of those two because they | |||
2920 | // are more permissive. | |||
2921 | ||||
2922 | const InstructionOperandMatcher *AOM = | |||
2923 | dyn_cast<InstructionOperandMatcher>(this); | |||
2924 | const InstructionOperandMatcher *BOM = | |||
2925 | dyn_cast<InstructionOperandMatcher>(&B); | |||
2926 | bool AIsConstantInsn = AOM && AOM->getInsnMatcher().isConstantInstruction(); | |||
2927 | bool BIsConstantInsn = BOM && BOM->getInsnMatcher().isConstantInstruction(); | |||
2928 | ||||
2929 | if (AOM && BOM) { | |||
2930 | // The relative priorities between a G_CONSTANT and any other instruction | |||
2931 | // don't actually matter but this code is needed to ensure a strict weak | |||
2932 | // ordering. This is particularly important on Windows where the rules will | |||
2933 | // be incorrectly sorted without it. | |||
2934 | if (AIsConstantInsn != BIsConstantInsn) | |||
2935 | return AIsConstantInsn < BIsConstantInsn; | |||
2936 | return false; | |||
2937 | } | |||
2938 | ||||
2939 | if (AOM && AIsConstantInsn && (B.Kind == OPM_Int || B.Kind == OPM_LiteralInt)) | |||
2940 | return false; | |||
2941 | if (BOM && BIsConstantInsn && (Kind == OPM_Int || Kind == OPM_LiteralInt)) | |||
2942 | return true; | |||
2943 | ||||
2944 | return Kind < B.Kind; | |||
2945 | } | |||
2946 | ||||
2947 | void SameOperandMatcher::emitPredicateOpcodes(MatchTable &Table, | |||
2948 | RuleMatcher &Rule) const { | |||
2949 | const OperandMatcher &OtherOM = Rule.getOperandMatcher(MatchingName); | |||
2950 | unsigned OtherInsnVarID = Rule.getInsnVarID(OtherOM.getInstructionMatcher()); | |||
2951 | assert(OtherInsnVarID == OtherOM.getInstructionMatcher().getInsnVarID())((OtherInsnVarID == OtherOM.getInstructionMatcher().getInsnVarID ()) ? static_cast<void> (0) : __assert_fail ("OtherInsnVarID == OtherOM.getInstructionMatcher().getInsnVarID()" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 2951, __PRETTY_FUNCTION__)); | |||
2952 | ||||
2953 | Table << MatchTable::Opcode("GIM_CheckIsSameOperand") | |||
2954 | << MatchTable::Comment("MI") << MatchTable::IntValue(InsnVarID) | |||
2955 | << MatchTable::Comment("OpIdx") << MatchTable::IntValue(OpIdx) | |||
2956 | << MatchTable::Comment("OtherMI") | |||
2957 | << MatchTable::IntValue(OtherInsnVarID) | |||
2958 | << MatchTable::Comment("OtherOpIdx") | |||
2959 | << MatchTable::IntValue(OtherOM.getOpIdx()) | |||
2960 | << MatchTable::LineBreak; | |||
2961 | } | |||
2962 | ||||
2963 | //===- GlobalISelEmitter class --------------------------------------------===// | |||
2964 | ||||
2965 | class GlobalISelEmitter { | |||
2966 | public: | |||
2967 | explicit GlobalISelEmitter(RecordKeeper &RK); | |||
2968 | void run(raw_ostream &OS); | |||
2969 | ||||
2970 | private: | |||
2971 | const RecordKeeper &RK; | |||
2972 | const CodeGenDAGPatterns CGP; | |||
2973 | const CodeGenTarget &Target; | |||
2974 | CodeGenRegBank CGRegs; | |||
2975 | ||||
2976 | /// Keep track of the equivalence between SDNodes and Instruction by mapping | |||
2977 | /// SDNodes to the GINodeEquiv mapping. We need to map to the GINodeEquiv to | |||
2978 | /// check for attributes on the relation such as CheckMMOIsNonAtomic. | |||
2979 | /// This is defined using 'GINodeEquiv' in the target description. | |||
2980 | DenseMap<Record *, Record *> NodeEquivs; | |||
2981 | ||||
2982 | /// Keep track of the equivalence between ComplexPattern's and | |||
2983 | /// GIComplexOperandMatcher. Map entries are specified by subclassing | |||
2984 | /// GIComplexPatternEquiv. | |||
2985 | DenseMap<const Record *, const Record *> ComplexPatternEquivs; | |||
2986 | ||||
2987 | /// Keep track of the equivalence between SDNodeXForm's and | |||
2988 | /// GICustomOperandRenderer. Map entries are specified by subclassing | |||
2989 | /// GISDNodeXFormEquiv. | |||
2990 | DenseMap<const Record *, const Record *> SDNodeXFormEquivs; | |||
2991 | ||||
2992 | /// Keep track of Scores of PatternsToMatch similar to how the DAG does. | |||
2993 | /// This adds compatibility for RuleMatchers to use this for ordering rules. | |||
2994 | DenseMap<uint64_t, int> RuleMatcherScores; | |||
2995 | ||||
2996 | // Map of predicates to their subtarget features. | |||
2997 | SubtargetFeatureInfoMap SubtargetFeatures; | |||
2998 | ||||
2999 | // Rule coverage information. | |||
3000 | Optional<CodeGenCoverage> RuleCoverage; | |||
3001 | ||||
3002 | void gatherOpcodeValues(); | |||
3003 | void gatherTypeIDValues(); | |||
3004 | void gatherNodeEquivs(); | |||
3005 | ||||
3006 | Record *findNodeEquiv(Record *N) const; | |||
3007 | const CodeGenInstruction *getEquivNode(Record &Equiv, | |||
3008 | const TreePatternNode *N) const; | |||
3009 | ||||
3010 | Error importRulePredicates(RuleMatcher &M, ArrayRef<Predicate> Predicates); | |||
3011 | Expected<InstructionMatcher &> | |||
3012 | createAndImportSelDAGMatcher(RuleMatcher &Rule, | |||
3013 | InstructionMatcher &InsnMatcher, | |||
3014 | const TreePatternNode *Src, unsigned &TempOpIdx); | |||
3015 | Error importComplexPatternOperandMatcher(OperandMatcher &OM, Record *R, | |||
3016 | unsigned &TempOpIdx) const; | |||
3017 | Error importChildMatcher(RuleMatcher &Rule, InstructionMatcher &InsnMatcher, | |||
3018 | const TreePatternNode *SrcChild, | |||
3019 | bool OperandIsAPointer, unsigned OpIdx, | |||
3020 | unsigned &TempOpIdx); | |||
3021 | ||||
3022 | Expected<BuildMIAction &> | |||
3023 | createAndImportInstructionRenderer(RuleMatcher &M, | |||
3024 | const TreePatternNode *Dst); | |||
3025 | Expected<action_iterator> createAndImportSubInstructionRenderer( | |||
3026 | action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst, | |||
3027 | unsigned TempReg); | |||
3028 | Expected<action_iterator> | |||
3029 | createInstructionRenderer(action_iterator InsertPt, RuleMatcher &M, | |||
3030 | const TreePatternNode *Dst); | |||
3031 | void importExplicitDefRenderers(BuildMIAction &DstMIBuilder); | |||
3032 | Expected<action_iterator> | |||
3033 | importExplicitUseRenderers(action_iterator InsertPt, RuleMatcher &M, | |||
3034 | BuildMIAction &DstMIBuilder, | |||
3035 | const llvm::TreePatternNode *Dst); | |||
3036 | Expected<action_iterator> | |||
3037 | importExplicitUseRenderer(action_iterator InsertPt, RuleMatcher &Rule, | |||
3038 | BuildMIAction &DstMIBuilder, | |||
3039 | TreePatternNode *DstChild); | |||
3040 | Error importDefaultOperandRenderers(action_iterator InsertPt, RuleMatcher &M, | |||
3041 | BuildMIAction &DstMIBuilder, | |||
3042 | DagInit *DefaultOps) const; | |||
3043 | Error | |||
3044 | importImplicitDefRenderers(BuildMIAction &DstMIBuilder, | |||
3045 | const std::vector<Record *> &ImplicitDefs) const; | |||
3046 | ||||
3047 | void emitCxxPredicateFns(raw_ostream &OS, StringRef CodeFieldName, | |||
3048 | StringRef TypeIdentifier, StringRef ArgType, | |||
3049 | StringRef ArgName, StringRef AdditionalDeclarations, | |||
3050 | std::function<bool(const Record *R)> Filter); | |||
3051 | void emitImmPredicateFns(raw_ostream &OS, StringRef TypeIdentifier, | |||
3052 | StringRef ArgType, | |||
3053 | std::function<bool(const Record *R)> Filter); | |||
3054 | void emitMIPredicateFns(raw_ostream &OS); | |||
3055 | ||||
3056 | /// Analyze pattern \p P, returning a matcher for it if possible. | |||
3057 | /// Otherwise, return an Error explaining why we don't support it. | |||
3058 | Expected<RuleMatcher> runOnPattern(const PatternToMatch &P); | |||
3059 | ||||
3060 | void declareSubtargetFeature(Record *Predicate); | |||
3061 | ||||
3062 | MatchTable buildMatchTable(MutableArrayRef<RuleMatcher> Rules, bool Optimize, | |||
3063 | bool WithCoverage); | |||
3064 | ||||
3065 | public: | |||
3066 | /// Takes a sequence of \p Rules and group them based on the predicates | |||
3067 | /// they share. \p MatcherStorage is used as a memory container | |||
3068 | /// for the group that are created as part of this process. | |||
3069 | /// | |||
3070 | /// What this optimization does looks like if GroupT = GroupMatcher: | |||
3071 | /// Output without optimization: | |||
3072 | /// \verbatim | |||
3073 | /// # R1 | |||
3074 | /// # predicate A | |||
3075 | /// # predicate B | |||
3076 | /// ... | |||
3077 | /// # R2 | |||
3078 | /// # predicate A // <-- effectively this is going to be checked twice. | |||
3079 | /// // Once in R1 and once in R2. | |||
3080 | /// # predicate C | |||
3081 | /// \endverbatim | |||
3082 | /// Output with optimization: | |||
3083 | /// \verbatim | |||
3084 | /// # Group1_2 | |||
3085 | /// # predicate A // <-- Check is now shared. | |||
3086 | /// # R1 | |||
3087 | /// # predicate B | |||
3088 | /// # R2 | |||
3089 | /// # predicate C | |||
3090 | /// \endverbatim | |||
3091 | template <class GroupT> | |||
3092 | static std::vector<Matcher *> optimizeRules( | |||
3093 | ArrayRef<Matcher *> Rules, | |||
3094 | std::vector<std::unique_ptr<Matcher>> &MatcherStorage); | |||
3095 | }; | |||
3096 | ||||
3097 | void GlobalISelEmitter::gatherOpcodeValues() { | |||
3098 | InstructionOpcodeMatcher::initOpcodeValuesMap(Target); | |||
3099 | } | |||
3100 | ||||
3101 | void GlobalISelEmitter::gatherTypeIDValues() { | |||
3102 | LLTOperandMatcher::initTypeIDValuesMap(); | |||
3103 | } | |||
3104 | ||||
3105 | void GlobalISelEmitter::gatherNodeEquivs() { | |||
3106 | assert(NodeEquivs.empty())((NodeEquivs.empty()) ? static_cast<void> (0) : __assert_fail ("NodeEquivs.empty()", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 3106, __PRETTY_FUNCTION__)); | |||
3107 | for (Record *Equiv : RK.getAllDerivedDefinitions("GINodeEquiv")) | |||
3108 | NodeEquivs[Equiv->getValueAsDef("Node")] = Equiv; | |||
3109 | ||||
3110 | assert(ComplexPatternEquivs.empty())((ComplexPatternEquivs.empty()) ? static_cast<void> (0) : __assert_fail ("ComplexPatternEquivs.empty()", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 3110, __PRETTY_FUNCTION__)); | |||
3111 | for (Record *Equiv : RK.getAllDerivedDefinitions("GIComplexPatternEquiv")) { | |||
3112 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); | |||
3113 | if (!SelDAGEquiv) | |||
3114 | continue; | |||
3115 | ComplexPatternEquivs[SelDAGEquiv] = Equiv; | |||
3116 | } | |||
3117 | ||||
3118 | assert(SDNodeXFormEquivs.empty())((SDNodeXFormEquivs.empty()) ? static_cast<void> (0) : __assert_fail ("SDNodeXFormEquivs.empty()", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 3118, __PRETTY_FUNCTION__)); | |||
3119 | for (Record *Equiv : RK.getAllDerivedDefinitions("GISDNodeXFormEquiv")) { | |||
3120 | Record *SelDAGEquiv = Equiv->getValueAsDef("SelDAGEquivalent"); | |||
3121 | if (!SelDAGEquiv) | |||
3122 | continue; | |||
3123 | SDNodeXFormEquivs[SelDAGEquiv] = Equiv; | |||
3124 | } | |||
3125 | } | |||
3126 | ||||
3127 | Record *GlobalISelEmitter::findNodeEquiv(Record *N) const { | |||
3128 | return NodeEquivs.lookup(N); | |||
3129 | } | |||
3130 | ||||
3131 | const CodeGenInstruction * | |||
3132 | GlobalISelEmitter::getEquivNode(Record &Equiv, const TreePatternNode *N) const { | |||
3133 | for (const TreePredicateCall &Call : N->getPredicateCalls()) { | |||
3134 | const TreePredicateFn &Predicate = Call.Fn; | |||
3135 | if (!Equiv.isValueUnset("IfSignExtend") && Predicate.isLoad() && | |||
3136 | Predicate.isSignExtLoad()) | |||
3137 | return &Target.getInstruction(Equiv.getValueAsDef("IfSignExtend")); | |||
3138 | if (!Equiv.isValueUnset("IfZeroExtend") && Predicate.isLoad() && | |||
3139 | Predicate.isZeroExtLoad()) | |||
3140 | return &Target.getInstruction(Equiv.getValueAsDef("IfZeroExtend")); | |||
3141 | } | |||
3142 | return &Target.getInstruction(Equiv.getValueAsDef("I")); | |||
3143 | } | |||
3144 | ||||
3145 | GlobalISelEmitter::GlobalISelEmitter(RecordKeeper &RK) | |||
3146 | : RK(RK), CGP(RK), Target(CGP.getTargetInfo()), | |||
3147 | CGRegs(RK, Target.getHwModes()) {} | |||
3148 | ||||
3149 | //===- Emitter ------------------------------------------------------------===// | |||
3150 | ||||
3151 | Error | |||
3152 | GlobalISelEmitter::importRulePredicates(RuleMatcher &M, | |||
3153 | ArrayRef<Predicate> Predicates) { | |||
3154 | for (const Predicate &P : Predicates) { | |||
3155 | if (!P.Def) | |||
3156 | continue; | |||
3157 | declareSubtargetFeature(P.Def); | |||
3158 | M.addRequiredFeature(P.Def); | |||
3159 | } | |||
3160 | ||||
3161 | return Error::success(); | |||
3162 | } | |||
3163 | ||||
3164 | Expected<InstructionMatcher &> GlobalISelEmitter::createAndImportSelDAGMatcher( | |||
3165 | RuleMatcher &Rule, InstructionMatcher &InsnMatcher, | |||
3166 | const TreePatternNode *Src, unsigned &TempOpIdx) { | |||
3167 | Record *SrcGIEquivOrNull = nullptr; | |||
3168 | const CodeGenInstruction *SrcGIOrNull = nullptr; | |||
3169 | ||||
3170 | // Start with the defined operands (i.e., the results of the root operator). | |||
3171 | if (Src->getExtTypes().size() > 1) | |||
3172 | return failedImport("Src pattern has multiple results"); | |||
3173 | ||||
3174 | if (Src->isLeaf()) { | |||
3175 | Init *SrcInit = Src->getLeafValue(); | |||
3176 | if (isa<IntInit>(SrcInit)) { | |||
3177 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>( | |||
3178 | &Target.getInstruction(RK.getDef("G_CONSTANT"))); | |||
3179 | } else | |||
3180 | return failedImport( | |||
3181 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); | |||
3182 | } else { | |||
3183 | SrcGIEquivOrNull = findNodeEquiv(Src->getOperator()); | |||
3184 | if (!SrcGIEquivOrNull) | |||
3185 | return failedImport("Pattern operator lacks an equivalent Instruction" + | |||
3186 | explainOperator(Src->getOperator())); | |||
3187 | SrcGIOrNull = getEquivNode(*SrcGIEquivOrNull, Src); | |||
3188 | ||||
3189 | // The operators look good: match the opcode | |||
3190 | InsnMatcher.addPredicate<InstructionOpcodeMatcher>(SrcGIOrNull); | |||
3191 | } | |||
3192 | ||||
3193 | unsigned OpIdx = 0; | |||
3194 | for (const TypeSetByHwMode &VTy : Src->getExtTypes()) { | |||
3195 | // Results don't have a name unless they are the root node. The caller will | |||
3196 | // set the name if appropriate. | |||
3197 | OperandMatcher &OM = InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); | |||
3198 | if (auto Error = OM.addTypeCheckPredicate(VTy, false /* OperandIsAPointer */)) | |||
3199 | return failedImport(toString(std::move(Error)) + | |||
3200 | " for result of Src pattern operator"); | |||
3201 | } | |||
3202 | ||||
3203 | for (const TreePredicateCall &Call : Src->getPredicateCalls()) { | |||
3204 | const TreePredicateFn &Predicate = Call.Fn; | |||
3205 | if (Predicate.isAlwaysTrue()) | |||
3206 | continue; | |||
3207 | ||||
3208 | if (Predicate.isImmediatePattern()) { | |||
3209 | InsnMatcher.addPredicate<InstructionImmPredicateMatcher>(Predicate); | |||
3210 | continue; | |||
3211 | } | |||
3212 | ||||
3213 | // G_LOAD is used for both non-extending and any-extending loads. | |||
3214 | if (Predicate.isLoad() && Predicate.isNonExtLoad()) { | |||
3215 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( | |||
3216 | 0, MemoryVsLLTSizePredicateMatcher::EqualTo, 0); | |||
3217 | continue; | |||
3218 | } | |||
3219 | if (Predicate.isLoad() && Predicate.isAnyExtLoad()) { | |||
3220 | InsnMatcher.addPredicate<MemoryVsLLTSizePredicateMatcher>( | |||
3221 | 0, MemoryVsLLTSizePredicateMatcher::LessThan, 0); | |||
3222 | continue; | |||
3223 | } | |||
3224 | ||||
3225 | // No check required. We already did it by swapping the opcode. | |||
3226 | if (!SrcGIEquivOrNull->isValueUnset("IfSignExtend") && | |||
3227 | Predicate.isSignExtLoad()) | |||
3228 | continue; | |||
3229 | ||||
3230 | // No check required. We already did it by swapping the opcode. | |||
3231 | if (!SrcGIEquivOrNull->isValueUnset("IfZeroExtend") && | |||
3232 | Predicate.isZeroExtLoad()) | |||
3233 | continue; | |||
3234 | ||||
3235 | // No check required. G_STORE by itself is a non-extending store. | |||
3236 | if (Predicate.isNonTruncStore()) | |||
3237 | continue; | |||
3238 | ||||
3239 | if (Predicate.isLoad() || Predicate.isStore() || Predicate.isAtomic()) { | |||
3240 | if (Predicate.getMemoryVT() != nullptr) { | |||
3241 | Optional<LLTCodeGen> MemTyOrNone = | |||
3242 | MVTToLLT(getValueType(Predicate.getMemoryVT())); | |||
3243 | ||||
3244 | if (!MemTyOrNone) | |||
3245 | return failedImport("MemVT could not be converted to LLT"); | |||
3246 | ||||
3247 | // MMO's work in bytes so we must take care of unusual types like i1 | |||
3248 | // don't round down. | |||
3249 | unsigned MemSizeInBits = | |||
3250 | llvm::alignTo(MemTyOrNone->get().getSizeInBits(), 8); | |||
3251 | ||||
3252 | InsnMatcher.addPredicate<MemorySizePredicateMatcher>( | |||
3253 | 0, MemSizeInBits / 8); | |||
3254 | continue; | |||
3255 | } | |||
3256 | } | |||
3257 | ||||
3258 | if (Predicate.isLoad() || Predicate.isStore()) { | |||
3259 | // No check required. A G_LOAD/G_STORE is an unindexed load. | |||
3260 | if (Predicate.isUnindexed()) | |||
3261 | continue; | |||
3262 | } | |||
3263 | ||||
3264 | if (Predicate.isAtomic()) { | |||
3265 | if (Predicate.isAtomicOrderingMonotonic()) { | |||
3266 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( | |||
3267 | "Monotonic"); | |||
3268 | continue; | |||
3269 | } | |||
3270 | if (Predicate.isAtomicOrderingAcquire()) { | |||
3271 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Acquire"); | |||
3272 | continue; | |||
3273 | } | |||
3274 | if (Predicate.isAtomicOrderingRelease()) { | |||
3275 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("Release"); | |||
3276 | continue; | |||
3277 | } | |||
3278 | if (Predicate.isAtomicOrderingAcquireRelease()) { | |||
3279 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( | |||
3280 | "AcquireRelease"); | |||
3281 | continue; | |||
3282 | } | |||
3283 | if (Predicate.isAtomicOrderingSequentiallyConsistent()) { | |||
3284 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( | |||
3285 | "SequentiallyConsistent"); | |||
3286 | continue; | |||
3287 | } | |||
3288 | ||||
3289 | if (Predicate.isAtomicOrderingAcquireOrStronger()) { | |||
3290 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( | |||
3291 | "Acquire", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); | |||
3292 | continue; | |||
3293 | } | |||
3294 | if (Predicate.isAtomicOrderingWeakerThanAcquire()) { | |||
3295 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( | |||
3296 | "Acquire", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan); | |||
3297 | continue; | |||
3298 | } | |||
3299 | ||||
3300 | if (Predicate.isAtomicOrderingReleaseOrStronger()) { | |||
3301 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( | |||
3302 | "Release", AtomicOrderingMMOPredicateMatcher::AO_OrStronger); | |||
3303 | continue; | |||
3304 | } | |||
3305 | if (Predicate.isAtomicOrderingWeakerThanRelease()) { | |||
3306 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>( | |||
3307 | "Release", AtomicOrderingMMOPredicateMatcher::AO_WeakerThan); | |||
3308 | continue; | |||
3309 | } | |||
3310 | } | |||
3311 | ||||
3312 | if (Predicate.hasGISelPredicateCode()) { | |||
3313 | InsnMatcher.addPredicate<GenericInstructionPredicateMatcher>(Predicate); | |||
3314 | continue; | |||
3315 | } | |||
3316 | ||||
3317 | return failedImport("Src pattern child has predicate (" + | |||
3318 | explainPredicates(Src) + ")"); | |||
3319 | } | |||
3320 | if (SrcGIEquivOrNull && SrcGIEquivOrNull->getValueAsBit("CheckMMOIsNonAtomic")) | |||
3321 | InsnMatcher.addPredicate<AtomicOrderingMMOPredicateMatcher>("NotAtomic"); | |||
3322 | ||||
3323 | if (Src->isLeaf()) { | |||
3324 | Init *SrcInit = Src->getLeafValue(); | |||
3325 | if (IntInit *SrcIntInit = dyn_cast<IntInit>(SrcInit)) { | |||
3326 | OperandMatcher &OM = | |||
3327 | InsnMatcher.addOperand(OpIdx++, Src->getName(), TempOpIdx); | |||
3328 | OM.addPredicate<LiteralIntOperandMatcher>(SrcIntInit->getValue()); | |||
3329 | } else | |||
3330 | return failedImport( | |||
3331 | "Unable to deduce gMIR opcode to handle Src (which is a leaf)"); | |||
3332 | } else { | |||
3333 | assert(SrcGIOrNull &&((SrcGIOrNull && "Expected to have already found an equivalent Instruction" ) ? static_cast<void> (0) : __assert_fail ("SrcGIOrNull && \"Expected to have already found an equivalent Instruction\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 3334, __PRETTY_FUNCTION__)) | |||
3334 | "Expected to have already found an equivalent Instruction")((SrcGIOrNull && "Expected to have already found an equivalent Instruction" ) ? static_cast<void> (0) : __assert_fail ("SrcGIOrNull && \"Expected to have already found an equivalent Instruction\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 3334, __PRETTY_FUNCTION__)); | |||
3335 | if (SrcGIOrNull->TheDef->getName() == "G_CONSTANT" || | |||
3336 | SrcGIOrNull->TheDef->getName() == "G_FCONSTANT") { | |||
3337 | // imm/fpimm still have operands but we don't need to do anything with it | |||
3338 | // here since we don't support ImmLeaf predicates yet. However, we still | |||
3339 | // need to note the hidden operand to get GIM_CheckNumOperands correct. | |||
3340 | InsnMatcher.addOperand(OpIdx++, "", TempOpIdx); | |||
3341 | return InsnMatcher; | |||
3342 | } | |||
3343 | ||||
3344 | // Match the used operands (i.e. the children of the operator). | |||
3345 | for (unsigned i = 0, e = Src->getNumChildren(); i != e; ++i) { | |||
3346 | TreePatternNode *SrcChild = Src->getChild(i); | |||
3347 | ||||
3348 | // SelectionDAG allows pointers to be represented with iN since it doesn't | |||
3349 | // distinguish between pointers and integers but they are different types in GlobalISel. | |||
3350 | // Coerce integers to pointers to address space 0 if the context indicates a pointer. | |||
3351 | bool OperandIsAPointer = SrcGIOrNull->isOperandAPointer(i); | |||
3352 | ||||
3353 | // For G_INTRINSIC/G_INTRINSIC_W_SIDE_EFFECTS, the operand immediately | |||
3354 | // following the defs is an intrinsic ID. | |||
3355 | if ((SrcGIOrNull->TheDef->getName() == "G_INTRINSIC" || | |||
3356 | SrcGIOrNull->TheDef->getName() == "G_INTRINSIC_W_SIDE_EFFECTS") && | |||
3357 | i == 0) { | |||
3358 | if (const CodeGenIntrinsic *II = Src->getIntrinsicInfo(CGP)) { | |||
3359 | OperandMatcher &OM = | |||
3360 | InsnMatcher.addOperand(OpIdx++, SrcChild->getName(), TempOpIdx); | |||
3361 | OM.addPredicate<IntrinsicIDOperandMatcher>(II); | |||
3362 | continue; | |||
3363 | } | |||
3364 | ||||
3365 | return failedImport("Expected IntInit containing instrinsic ID)"); | |||
3366 | } | |||
3367 | ||||
3368 | if (auto Error = | |||
3369 | importChildMatcher(Rule, InsnMatcher, SrcChild, OperandIsAPointer, | |||
3370 | OpIdx++, TempOpIdx)) | |||
3371 | return std::move(Error); | |||
3372 | } | |||
3373 | } | |||
3374 | ||||
3375 | return InsnMatcher; | |||
3376 | } | |||
3377 | ||||
3378 | Error GlobalISelEmitter::importComplexPatternOperandMatcher( | |||
3379 | OperandMatcher &OM, Record *R, unsigned &TempOpIdx) const { | |||
3380 | const auto &ComplexPattern = ComplexPatternEquivs.find(R); | |||
3381 | if (ComplexPattern == ComplexPatternEquivs.end()) | |||
3382 | return failedImport("SelectionDAG ComplexPattern (" + R->getName() + | |||
3383 | ") not mapped to GlobalISel"); | |||
3384 | ||||
3385 | OM.addPredicate<ComplexPatternOperandMatcher>(OM, *ComplexPattern->second); | |||
3386 | TempOpIdx++; | |||
3387 | return Error::success(); | |||
3388 | } | |||
3389 | ||||
3390 | Error GlobalISelEmitter::importChildMatcher(RuleMatcher &Rule, | |||
3391 | InstructionMatcher &InsnMatcher, | |||
3392 | const TreePatternNode *SrcChild, | |||
3393 | bool OperandIsAPointer, | |||
3394 | unsigned OpIdx, | |||
3395 | unsigned &TempOpIdx) { | |||
3396 | OperandMatcher &OM = | |||
3397 | InsnMatcher.addOperand(OpIdx, SrcChild->getName(), TempOpIdx); | |||
3398 | if (OM.isSameAsAnotherOperand()) | |||
3399 | return Error::success(); | |||
3400 | ||||
3401 | ArrayRef<TypeSetByHwMode> ChildTypes = SrcChild->getExtTypes(); | |||
3402 | if (ChildTypes.size() != 1) | |||
3403 | return failedImport("Src pattern child has multiple results"); | |||
3404 | ||||
3405 | // Check MBB's before the type check since they are not a known type. | |||
3406 | if (!SrcChild->isLeaf()) { | |||
3407 | if (SrcChild->getOperator()->isSubClassOf("SDNode")) { | |||
3408 | auto &ChildSDNI = CGP.getSDNodeInfo(SrcChild->getOperator()); | |||
3409 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { | |||
3410 | OM.addPredicate<MBBOperandMatcher>(); | |||
3411 | return Error::success(); | |||
3412 | } | |||
3413 | } | |||
3414 | } | |||
3415 | ||||
3416 | if (auto Error = | |||
3417 | OM.addTypeCheckPredicate(ChildTypes.front(), OperandIsAPointer)) | |||
3418 | return failedImport(toString(std::move(Error)) + " for Src operand (" + | |||
3419 | to_string(*SrcChild) + ")"); | |||
3420 | ||||
3421 | // Check for nested instructions. | |||
3422 | if (!SrcChild->isLeaf()) { | |||
3423 | if (SrcChild->getOperator()->isSubClassOf("ComplexPattern")) { | |||
3424 | // When a ComplexPattern is used as an operator, it should do the same | |||
3425 | // thing as when used as a leaf. However, the children of the operator | |||
3426 | // name the sub-operands that make up the complex operand and we must | |||
3427 | // prepare to reference them in the renderer too. | |||
3428 | unsigned RendererID = TempOpIdx; | |||
3429 | if (auto Error = importComplexPatternOperandMatcher( | |||
3430 | OM, SrcChild->getOperator(), TempOpIdx)) | |||
3431 | return Error; | |||
3432 | ||||
3433 | for (unsigned i = 0, e = SrcChild->getNumChildren(); i != e; ++i) { | |||
3434 | auto *SubOperand = SrcChild->getChild(i); | |||
3435 | if (!SubOperand->getName().empty()) { | |||
3436 | if (auto Error = Rule.defineComplexSubOperand(SubOperand->getName(), | |||
3437 | SrcChild->getOperator(), | |||
3438 | RendererID, i)) | |||
3439 | return Error; | |||
3440 | } | |||
3441 | } | |||
3442 | ||||
3443 | return Error::success(); | |||
3444 | } | |||
3445 | ||||
3446 | auto MaybeInsnOperand = OM.addPredicate<InstructionOperandMatcher>( | |||
3447 | InsnMatcher.getRuleMatcher(), SrcChild->getName()); | |||
3448 | if (!MaybeInsnOperand.hasValue()) { | |||
3449 | // This isn't strictly true. If the user were to provide exactly the same | |||
3450 | // matchers as the original operand then we could allow it. However, it's | |||
3451 | // simpler to not permit the redundant specification. | |||
3452 | return failedImport("Nested instruction cannot be the same as another operand"); | |||
3453 | } | |||
3454 | ||||
3455 | // Map the node to a gMIR instruction. | |||
3456 | InstructionOperandMatcher &InsnOperand = **MaybeInsnOperand; | |||
3457 | auto InsnMatcherOrError = createAndImportSelDAGMatcher( | |||
3458 | Rule, InsnOperand.getInsnMatcher(), SrcChild, TempOpIdx); | |||
3459 | if (auto Error = InsnMatcherOrError.takeError()) | |||
3460 | return Error; | |||
3461 | ||||
3462 | return Error::success(); | |||
3463 | } | |||
3464 | ||||
3465 | if (SrcChild->hasAnyPredicate()) | |||
3466 | return failedImport("Src pattern child has unsupported predicate"); | |||
3467 | ||||
3468 | // Check for constant immediates. | |||
3469 | if (auto *ChildInt = dyn_cast<IntInit>(SrcChild->getLeafValue())) { | |||
3470 | OM.addPredicate<ConstantIntOperandMatcher>(ChildInt->getValue()); | |||
3471 | return Error::success(); | |||
3472 | } | |||
3473 | ||||
3474 | // Check for def's like register classes or ComplexPattern's. | |||
3475 | if (auto *ChildDefInit = dyn_cast<DefInit>(SrcChild->getLeafValue())) { | |||
3476 | auto *ChildRec = ChildDefInit->getDef(); | |||
3477 | ||||
3478 | // Check for register classes. | |||
3479 | if (ChildRec->isSubClassOf("RegisterClass") || | |||
3480 | ChildRec->isSubClassOf("RegisterOperand")) { | |||
3481 | OM.addPredicate<RegisterBankOperandMatcher>( | |||
3482 | Target.getRegisterClass(getInitValueAsRegClass(ChildDefInit))); | |||
3483 | return Error::success(); | |||
3484 | } | |||
3485 | ||||
3486 | // Check for ValueType. | |||
3487 | if (ChildRec->isSubClassOf("ValueType")) { | |||
3488 | // We already added a type check as standard practice so this doesn't need | |||
3489 | // to do anything. | |||
3490 | return Error::success(); | |||
3491 | } | |||
3492 | ||||
3493 | // Check for ComplexPattern's. | |||
3494 | if (ChildRec->isSubClassOf("ComplexPattern")) | |||
3495 | return importComplexPatternOperandMatcher(OM, ChildRec, TempOpIdx); | |||
3496 | ||||
3497 | if (ChildRec->isSubClassOf("ImmLeaf")) { | |||
3498 | return failedImport( | |||
3499 | "Src pattern child def is an unsupported tablegen class (ImmLeaf)"); | |||
3500 | } | |||
3501 | ||||
3502 | return failedImport( | |||
3503 | "Src pattern child def is an unsupported tablegen class"); | |||
3504 | } | |||
3505 | ||||
3506 | return failedImport("Src pattern child is an unsupported kind"); | |||
3507 | } | |||
3508 | ||||
3509 | Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderer( | |||
3510 | action_iterator InsertPt, RuleMatcher &Rule, BuildMIAction &DstMIBuilder, | |||
3511 | TreePatternNode *DstChild) { | |||
3512 | ||||
3513 | const auto &SubOperand = Rule.getComplexSubOperand(DstChild->getName()); | |||
3514 | if (SubOperand.hasValue()) { | |||
3515 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( | |||
3516 | *std::get<0>(*SubOperand), DstChild->getName(), | |||
3517 | std::get<1>(*SubOperand), std::get<2>(*SubOperand)); | |||
3518 | return InsertPt; | |||
3519 | } | |||
3520 | ||||
3521 | if (!DstChild->isLeaf()) { | |||
3522 | ||||
3523 | if (DstChild->getOperator()->isSubClassOf("SDNodeXForm")) { | |||
3524 | auto Child = DstChild->getChild(0); | |||
3525 | auto I = SDNodeXFormEquivs.find(DstChild->getOperator()); | |||
3526 | if (I != SDNodeXFormEquivs.end()) { | |||
3527 | DstMIBuilder.addRenderer<CustomRenderer>(*I->second, Child->getName()); | |||
3528 | return InsertPt; | |||
3529 | } | |||
3530 | return failedImport("SDNodeXForm " + Child->getName() + | |||
3531 | " has no custom renderer"); | |||
3532 | } | |||
3533 | ||||
3534 | // We accept 'bb' here. It's an operator because BasicBlockSDNode isn't | |||
3535 | // inline, but in MI it's just another operand. | |||
3536 | if (DstChild->getOperator()->isSubClassOf("SDNode")) { | |||
3537 | auto &ChildSDNI = CGP.getSDNodeInfo(DstChild->getOperator()); | |||
3538 | if (ChildSDNI.getSDClassName() == "BasicBlockSDNode") { | |||
3539 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); | |||
3540 | return InsertPt; | |||
3541 | } | |||
3542 | } | |||
3543 | ||||
3544 | // Similarly, imm is an operator in TreePatternNode's view but must be | |||
3545 | // rendered as operands. | |||
3546 | // FIXME: The target should be able to choose sign-extended when appropriate | |||
3547 | // (e.g. on Mips). | |||
3548 | if (DstChild->getOperator()->getName() == "imm") { | |||
3549 | DstMIBuilder.addRenderer<CopyConstantAsImmRenderer>(DstChild->getName()); | |||
3550 | return InsertPt; | |||
3551 | } else if (DstChild->getOperator()->getName() == "fpimm") { | |||
3552 | DstMIBuilder.addRenderer<CopyFConstantAsFPImmRenderer>( | |||
3553 | DstChild->getName()); | |||
3554 | return InsertPt; | |||
3555 | } | |||
3556 | ||||
3557 | if (DstChild->getOperator()->isSubClassOf("Instruction")) { | |||
3558 | ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes(); | |||
3559 | if (ChildTypes.size() != 1) | |||
3560 | return failedImport("Dst pattern child has multiple results"); | |||
3561 | ||||
3562 | Optional<LLTCodeGen> OpTyOrNone = None; | |||
3563 | if (ChildTypes.front().isMachineValueType()) | |||
3564 | OpTyOrNone = | |||
3565 | MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy); | |||
3566 | if (!OpTyOrNone) | |||
3567 | return failedImport("Dst operand has an unsupported type"); | |||
3568 | ||||
3569 | unsigned TempRegID = Rule.allocateTempRegID(); | |||
3570 | InsertPt = Rule.insertAction<MakeTempRegisterAction>( | |||
3571 | InsertPt, OpTyOrNone.getValue(), TempRegID); | |||
3572 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID); | |||
3573 | ||||
3574 | auto InsertPtOrError = createAndImportSubInstructionRenderer( | |||
3575 | ++InsertPt, Rule, DstChild, TempRegID); | |||
3576 | if (auto Error = InsertPtOrError.takeError()) | |||
3577 | return std::move(Error); | |||
3578 | return InsertPtOrError.get(); | |||
3579 | } | |||
3580 | ||||
3581 | return failedImport("Dst pattern child isn't a leaf node or an MBB" + llvm::to_string(*DstChild)); | |||
3582 | } | |||
3583 | ||||
3584 | // It could be a specific immediate in which case we should just check for | |||
3585 | // that immediate. | |||
3586 | if (const IntInit *ChildIntInit = | |||
3587 | dyn_cast<IntInit>(DstChild->getLeafValue())) { | |||
3588 | DstMIBuilder.addRenderer<ImmRenderer>(ChildIntInit->getValue()); | |||
3589 | return InsertPt; | |||
3590 | } | |||
3591 | ||||
3592 | // Otherwise, we're looking for a bog-standard RegisterClass operand. | |||
3593 | if (auto *ChildDefInit = dyn_cast<DefInit>(DstChild->getLeafValue())) { | |||
3594 | auto *ChildRec = ChildDefInit->getDef(); | |||
3595 | ||||
3596 | ArrayRef<TypeSetByHwMode> ChildTypes = DstChild->getExtTypes(); | |||
3597 | if (ChildTypes.size() != 1) | |||
3598 | return failedImport("Dst pattern child has multiple results"); | |||
3599 | ||||
3600 | Optional<LLTCodeGen> OpTyOrNone = None; | |||
3601 | if (ChildTypes.front().isMachineValueType()) | |||
3602 | OpTyOrNone = MVTToLLT(ChildTypes.front().getMachineValueType().SimpleTy); | |||
3603 | if (!OpTyOrNone) | |||
3604 | return failedImport("Dst operand has an unsupported type"); | |||
3605 | ||||
3606 | if (ChildRec->isSubClassOf("Register")) { | |||
3607 | DstMIBuilder.addRenderer<AddRegisterRenderer>(ChildRec); | |||
3608 | return InsertPt; | |||
3609 | } | |||
3610 | ||||
3611 | if (ChildRec->isSubClassOf("RegisterClass") || | |||
3612 | ChildRec->isSubClassOf("RegisterOperand") || | |||
3613 | ChildRec->isSubClassOf("ValueType")) { | |||
3614 | if (ChildRec->isSubClassOf("RegisterOperand") && | |||
3615 | !ChildRec->isValueUnset("GIZeroRegister")) { | |||
3616 | DstMIBuilder.addRenderer<CopyOrAddZeroRegRenderer>( | |||
3617 | DstChild->getName(), ChildRec->getValueAsDef("GIZeroRegister")); | |||
3618 | return InsertPt; | |||
3619 | } | |||
3620 | ||||
3621 | DstMIBuilder.addRenderer<CopyRenderer>(DstChild->getName()); | |||
3622 | return InsertPt; | |||
3623 | } | |||
3624 | ||||
3625 | if (ChildRec->isSubClassOf("ComplexPattern")) { | |||
3626 | const auto &ComplexPattern = ComplexPatternEquivs.find(ChildRec); | |||
3627 | if (ComplexPattern == ComplexPatternEquivs.end()) | |||
3628 | return failedImport( | |||
3629 | "SelectionDAG ComplexPattern not mapped to GlobalISel"); | |||
3630 | ||||
3631 | const OperandMatcher &OM = Rule.getOperandMatcher(DstChild->getName()); | |||
3632 | DstMIBuilder.addRenderer<RenderComplexPatternOperand>( | |||
3633 | *ComplexPattern->second, DstChild->getName(), | |||
3634 | OM.getAllocatedTemporariesBaseID()); | |||
3635 | return InsertPt; | |||
3636 | } | |||
3637 | ||||
3638 | return failedImport( | |||
3639 | "Dst pattern child def is an unsupported tablegen class"); | |||
3640 | } | |||
3641 | ||||
3642 | return failedImport("Dst pattern child is an unsupported kind"); | |||
3643 | } | |||
3644 | ||||
3645 | Expected<BuildMIAction &> GlobalISelEmitter::createAndImportInstructionRenderer( | |||
3646 | RuleMatcher &M, const TreePatternNode *Dst) { | |||
3647 | auto InsertPtOrError = createInstructionRenderer(M.actions_end(), M, Dst); | |||
3648 | if (auto Error = InsertPtOrError.takeError()) | |||
3649 | return std::move(Error); | |||
3650 | ||||
3651 | action_iterator InsertPt = InsertPtOrError.get(); | |||
3652 | BuildMIAction &DstMIBuilder = *static_cast<BuildMIAction *>(InsertPt->get()); | |||
3653 | ||||
3654 | importExplicitDefRenderers(DstMIBuilder); | |||
3655 | ||||
3656 | if (auto Error = importExplicitUseRenderers(InsertPt, M, DstMIBuilder, Dst) | |||
3657 | .takeError()) | |||
3658 | return std::move(Error); | |||
3659 | ||||
3660 | return DstMIBuilder; | |||
3661 | } | |||
3662 | ||||
3663 | Expected<action_iterator> | |||
3664 | GlobalISelEmitter::createAndImportSubInstructionRenderer( | |||
3665 | const action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst, | |||
3666 | unsigned TempRegID) { | |||
3667 | auto InsertPtOrError = createInstructionRenderer(InsertPt, M, Dst); | |||
| ||||
3668 | ||||
3669 | // TODO: Assert there's exactly one result. | |||
3670 | ||||
3671 | if (auto Error = InsertPtOrError.takeError()) | |||
3672 | return std::move(Error); | |||
3673 | ||||
3674 | BuildMIAction &DstMIBuilder = | |||
3675 | *static_cast<BuildMIAction *>(InsertPtOrError.get()->get()); | |||
3676 | ||||
3677 | // Assign the result to TempReg. | |||
3678 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID, true); | |||
3679 | ||||
3680 | InsertPtOrError = | |||
3681 | importExplicitUseRenderers(InsertPtOrError.get(), M, DstMIBuilder, Dst); | |||
3682 | if (auto Error = InsertPtOrError.takeError()) | |||
3683 | return std::move(Error); | |||
3684 | ||||
3685 | M.insertAction<ConstrainOperandsToDefinitionAction>(InsertPt, | |||
3686 | DstMIBuilder.getInsnID()); | |||
3687 | return InsertPtOrError.get(); | |||
3688 | } | |||
3689 | ||||
3690 | Expected<action_iterator> GlobalISelEmitter::createInstructionRenderer( | |||
3691 | action_iterator InsertPt, RuleMatcher &M, const TreePatternNode *Dst) { | |||
3692 | Record *DstOp = Dst->getOperator(); | |||
3693 | if (!DstOp->isSubClassOf("Instruction")) { | |||
3694 | if (DstOp->isSubClassOf("ValueType")) | |||
3695 | return failedImport( | |||
3696 | "Pattern operator isn't an instruction (it's a ValueType)"); | |||
3697 | return failedImport("Pattern operator isn't an instruction"); | |||
3698 | } | |||
3699 | CodeGenInstruction *DstI = &Target.getInstruction(DstOp); | |||
3700 | ||||
3701 | // COPY_TO_REGCLASS is just a copy with a ConstrainOperandToRegClassAction | |||
3702 | // attached. Similarly for EXTRACT_SUBREG except that's a subregister copy. | |||
3703 | if (DstI->TheDef->getName() == "COPY_TO_REGCLASS") | |||
3704 | DstI = &Target.getInstruction(RK.getDef("COPY")); | |||
3705 | else if (DstI->TheDef->getName() == "EXTRACT_SUBREG") | |||
3706 | DstI = &Target.getInstruction(RK.getDef("COPY")); | |||
3707 | else if (DstI->TheDef->getName() == "REG_SEQUENCE") | |||
3708 | return failedImport("Unable to emit REG_SEQUENCE"); | |||
3709 | ||||
3710 | return M.insertAction<BuildMIAction>(InsertPt, M.allocateOutputInsnID(), | |||
3711 | DstI); | |||
3712 | } | |||
3713 | ||||
3714 | void GlobalISelEmitter::importExplicitDefRenderers( | |||
3715 | BuildMIAction &DstMIBuilder) { | |||
3716 | const CodeGenInstruction *DstI = DstMIBuilder.getCGI(); | |||
3717 | for (unsigned I = 0; I < DstI->Operands.NumDefs; ++I) { | |||
3718 | const CGIOperandList::OperandInfo &DstIOperand = DstI->Operands[I]; | |||
3719 | DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name); | |||
3720 | } | |||
3721 | } | |||
3722 | ||||
3723 | Expected<action_iterator> GlobalISelEmitter::importExplicitUseRenderers( | |||
3724 | action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder, | |||
3725 | const llvm::TreePatternNode *Dst) { | |||
3726 | const CodeGenInstruction *DstI = DstMIBuilder.getCGI(); | |||
3727 | CodeGenInstruction *OrigDstI = &Target.getInstruction(Dst->getOperator()); | |||
3728 | ||||
3729 | // EXTRACT_SUBREG needs to use a subregister COPY. | |||
3730 | if (OrigDstI->TheDef->getName() == "EXTRACT_SUBREG") { | |||
3731 | if (!Dst->getChild(0)->isLeaf()) | |||
3732 | return failedImport("EXTRACT_SUBREG child #1 is not a leaf"); | |||
3733 | ||||
3734 | if (DefInit *SubRegInit = | |||
3735 | dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue())) { | |||
3736 | Record *RCDef = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); | |||
3737 | if (!RCDef) | |||
3738 | return failedImport("EXTRACT_SUBREG child #0 could not " | |||
3739 | "be coerced to a register class"); | |||
3740 | ||||
3741 | CodeGenRegisterClass *RC = CGRegs.getRegClass(RCDef); | |||
3742 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); | |||
3743 | ||||
3744 | const auto &SrcRCDstRCPair = | |||
3745 | RC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx); | |||
3746 | if (SrcRCDstRCPair.hasValue()) { | |||
3747 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass")((SrcRCDstRCPair->second && "Couldn't find a matching subclass" ) ? static_cast<void> (0) : __assert_fail ("SrcRCDstRCPair->second && \"Couldn't find a matching subclass\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 3747, __PRETTY_FUNCTION__)); | |||
3748 | if (SrcRCDstRCPair->first != RC) | |||
3749 | return failedImport("EXTRACT_SUBREG requires an additional COPY"); | |||
3750 | } | |||
3751 | ||||
3752 | DstMIBuilder.addRenderer<CopySubRegRenderer>(Dst->getChild(0)->getName(), | |||
3753 | SubIdx); | |||
3754 | return InsertPt; | |||
3755 | } | |||
3756 | ||||
3757 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); | |||
3758 | } | |||
3759 | ||||
3760 | // Render the explicit uses. | |||
3761 | unsigned DstINumUses = OrigDstI->Operands.size() - OrigDstI->Operands.NumDefs; | |||
3762 | unsigned ExpectedDstINumUses = Dst->getNumChildren(); | |||
3763 | if (OrigDstI->TheDef->getName() == "COPY_TO_REGCLASS") { | |||
3764 | DstINumUses--; // Ignore the class constraint. | |||
3765 | ExpectedDstINumUses--; | |||
3766 | } | |||
3767 | ||||
3768 | unsigned Child = 0; | |||
3769 | unsigned NumDefaultOps = 0; | |||
3770 | for (unsigned I = 0; I != DstINumUses; ++I) { | |||
3771 | const CGIOperandList::OperandInfo &DstIOperand = | |||
3772 | DstI->Operands[DstI->Operands.NumDefs + I]; | |||
3773 | ||||
3774 | // If the operand has default values, introduce them now. | |||
3775 | // FIXME: Until we have a decent test case that dictates we should do | |||
3776 | // otherwise, we're going to assume that operands with default values cannot | |||
3777 | // be specified in the patterns. Therefore, adding them will not cause us to | |||
3778 | // end up with too many rendered operands. | |||
3779 | if (DstIOperand.Rec->isSubClassOf("OperandWithDefaultOps")) { | |||
3780 | DagInit *DefaultOps = DstIOperand.Rec->getValueAsDag("DefaultOps"); | |||
3781 | if (auto Error = importDefaultOperandRenderers( | |||
3782 | InsertPt, M, DstMIBuilder, DefaultOps)) | |||
3783 | return std::move(Error); | |||
3784 | ++NumDefaultOps; | |||
3785 | continue; | |||
3786 | } | |||
3787 | ||||
3788 | auto InsertPtOrError = importExplicitUseRenderer(InsertPt, M, DstMIBuilder, | |||
3789 | Dst->getChild(Child)); | |||
3790 | if (auto Error = InsertPtOrError.takeError()) | |||
3791 | return std::move(Error); | |||
3792 | InsertPt = InsertPtOrError.get(); | |||
3793 | ++Child; | |||
3794 | } | |||
3795 | ||||
3796 | if (NumDefaultOps + ExpectedDstINumUses != DstINumUses) | |||
3797 | return failedImport("Expected " + llvm::to_string(DstINumUses) + | |||
3798 | " used operands but found " + | |||
3799 | llvm::to_string(ExpectedDstINumUses) + | |||
3800 | " explicit ones and " + llvm::to_string(NumDefaultOps) + | |||
3801 | " default ones"); | |||
3802 | ||||
3803 | return InsertPt; | |||
3804 | } | |||
3805 | ||||
3806 | Error GlobalISelEmitter::importDefaultOperandRenderers( | |||
3807 | action_iterator InsertPt, RuleMatcher &M, BuildMIAction &DstMIBuilder, | |||
3808 | DagInit *DefaultOps) const { | |||
3809 | for (const auto *DefaultOp : DefaultOps->getArgs()) { | |||
3810 | Optional<LLTCodeGen> OpTyOrNone = None; | |||
3811 | ||||
3812 | // Look through ValueType operators. | |||
3813 | if (const DagInit *DefaultDagOp = dyn_cast<DagInit>(DefaultOp)) { | |||
3814 | if (const DefInit *DefaultDagOperator = | |||
3815 | dyn_cast<DefInit>(DefaultDagOp->getOperator())) { | |||
3816 | if (DefaultDagOperator->getDef()->isSubClassOf("ValueType")) { | |||
3817 | OpTyOrNone = MVTToLLT(getValueType( | |||
3818 | DefaultDagOperator->getDef())); | |||
3819 | DefaultOp = DefaultDagOp->getArg(0); | |||
3820 | } | |||
3821 | } | |||
3822 | } | |||
3823 | ||||
3824 | if (const DefInit *DefaultDefOp = dyn_cast<DefInit>(DefaultOp)) { | |||
3825 | auto Def = DefaultDefOp->getDef(); | |||
3826 | if (Def->getName() == "undef_tied_input") { | |||
3827 | unsigned TempRegID = M.allocateTempRegID(); | |||
3828 | M.insertAction<MakeTempRegisterAction>( | |||
3829 | InsertPt, OpTyOrNone.getValue(), TempRegID); | |||
3830 | InsertPt = M.insertAction<BuildMIAction>( | |||
3831 | InsertPt, M.allocateOutputInsnID(), | |||
3832 | &Target.getInstruction(RK.getDef("IMPLICIT_DEF"))); | |||
3833 | BuildMIAction &IDMIBuilder = *static_cast<BuildMIAction *>( | |||
3834 | InsertPt->get()); | |||
3835 | IDMIBuilder.addRenderer<TempRegRenderer>(TempRegID); | |||
3836 | DstMIBuilder.addRenderer<TempRegRenderer>(TempRegID); | |||
3837 | } else { | |||
3838 | DstMIBuilder.addRenderer<AddRegisterRenderer>(Def); | |||
3839 | } | |||
3840 | continue; | |||
3841 | } | |||
3842 | ||||
3843 | if (const IntInit *DefaultIntOp = dyn_cast<IntInit>(DefaultOp)) { | |||
3844 | DstMIBuilder.addRenderer<ImmRenderer>(DefaultIntOp->getValue()); | |||
3845 | continue; | |||
3846 | } | |||
3847 | ||||
3848 | return failedImport("Could not add default op"); | |||
3849 | } | |||
3850 | ||||
3851 | return Error::success(); | |||
3852 | } | |||
3853 | ||||
3854 | Error GlobalISelEmitter::importImplicitDefRenderers( | |||
3855 | BuildMIAction &DstMIBuilder, | |||
3856 | const std::vector<Record *> &ImplicitDefs) const { | |||
3857 | if (!ImplicitDefs.empty()) | |||
3858 | return failedImport("Pattern defines a physical register"); | |||
3859 | return Error::success(); | |||
3860 | } | |||
3861 | ||||
3862 | Expected<RuleMatcher> GlobalISelEmitter::runOnPattern(const PatternToMatch &P) { | |||
3863 | // Keep track of the matchers and actions to emit. | |||
3864 | int Score = P.getPatternComplexity(CGP); | |||
3865 | RuleMatcher M(P.getSrcRecord()->getLoc()); | |||
3866 | RuleMatcherScores[M.getRuleID()] = Score; | |||
3867 | M.addAction<DebugCommentAction>(llvm::to_string(*P.getSrcPattern()) + | |||
3868 | " => " + | |||
3869 | llvm::to_string(*P.getDstPattern())); | |||
3870 | ||||
3871 | if (auto Error = importRulePredicates(M, P.getPredicates())) | |||
3872 | return std::move(Error); | |||
3873 | ||||
3874 | // Next, analyze the pattern operators. | |||
3875 | TreePatternNode *Src = P.getSrcPattern(); | |||
3876 | TreePatternNode *Dst = P.getDstPattern(); | |||
3877 | ||||
3878 | // If the root of either pattern isn't a simple operator, ignore it. | |||
3879 | if (auto Err = isTrivialOperatorNode(Dst)) | |||
3880 | return failedImport("Dst pattern root isn't a trivial operator (" + | |||
3881 | toString(std::move(Err)) + ")"); | |||
3882 | if (auto Err = isTrivialOperatorNode(Src)) | |||
3883 | return failedImport("Src pattern root isn't a trivial operator (" + | |||
3884 | toString(std::move(Err)) + ")"); | |||
3885 | ||||
3886 | // The different predicates and matchers created during | |||
3887 | // addInstructionMatcher use the RuleMatcher M to set up their | |||
3888 | // instruction ID (InsnVarID) that are going to be used when | |||
3889 | // M is going to be emitted. | |||
3890 | // However, the code doing the emission still relies on the IDs | |||
3891 | // returned during that process by the RuleMatcher when issuing | |||
3892 | // the recordInsn opcodes. | |||
3893 | // Because of that: | |||
3894 | // 1. The order in which we created the predicates | |||
3895 | // and such must be the same as the order in which we emit them, | |||
3896 | // and | |||
3897 | // 2. We need to reset the generation of the IDs in M somewhere between | |||
3898 | // addInstructionMatcher and emit | |||
3899 | // | |||
3900 | // FIXME: Long term, we don't want to have to rely on this implicit | |||
3901 | // naming being the same. One possible solution would be to have | |||
3902 | // explicit operator for operation capture and reference those. | |||
3903 | // The plus side is that it would expose opportunities to share | |||
3904 | // the capture accross rules. The downside is that it would | |||
3905 | // introduce a dependency between predicates (captures must happen | |||
3906 | // before their first use.) | |||
3907 | InstructionMatcher &InsnMatcherTemp = M.addInstructionMatcher(Src->getName()); | |||
3908 | unsigned TempOpIdx = 0; | |||
3909 | auto InsnMatcherOrError = | |||
3910 | createAndImportSelDAGMatcher(M, InsnMatcherTemp, Src, TempOpIdx); | |||
3911 | if (auto Error = InsnMatcherOrError.takeError()) | |||
3912 | return std::move(Error); | |||
3913 | InstructionMatcher &InsnMatcher = InsnMatcherOrError.get(); | |||
3914 | ||||
3915 | if (Dst->isLeaf()) { | |||
3916 | Record *RCDef = getInitValueAsRegClass(Dst->getLeafValue()); | |||
3917 | ||||
3918 | const CodeGenRegisterClass &RC = Target.getRegisterClass(RCDef); | |||
3919 | if (RCDef) { | |||
3920 | // We need to replace the def and all its uses with the specified | |||
3921 | // operand. However, we must also insert COPY's wherever needed. | |||
3922 | // For now, emit a copy and let the register allocator clean up. | |||
3923 | auto &DstI = Target.getInstruction(RK.getDef("COPY")); | |||
3924 | const auto &DstIOperand = DstI.Operands[0]; | |||
3925 | ||||
3926 | OperandMatcher &OM0 = InsnMatcher.getOperand(0); | |||
3927 | OM0.setSymbolicName(DstIOperand.Name); | |||
3928 | M.defineOperand(OM0.getSymbolicName(), OM0); | |||
3929 | OM0.addPredicate<RegisterBankOperandMatcher>(RC); | |||
3930 | ||||
3931 | auto &DstMIBuilder = | |||
3932 | M.addAction<BuildMIAction>(M.allocateOutputInsnID(), &DstI); | |||
3933 | DstMIBuilder.addRenderer<CopyRenderer>(DstIOperand.Name); | |||
3934 | DstMIBuilder.addRenderer<CopyRenderer>(Dst->getName()); | |||
3935 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, RC); | |||
3936 | ||||
3937 | // We're done with this pattern! It's eligible for GISel emission; return | |||
3938 | // it. | |||
3939 | ++NumPatternImported; | |||
3940 | return std::move(M); | |||
3941 | } | |||
3942 | ||||
3943 | return failedImport("Dst pattern root isn't a known leaf"); | |||
3944 | } | |||
3945 | ||||
3946 | // Start with the defined operands (i.e., the results of the root operator). | |||
3947 | Record *DstOp = Dst->getOperator(); | |||
3948 | if (!DstOp->isSubClassOf("Instruction")) | |||
3949 | return failedImport("Pattern operator isn't an instruction"); | |||
3950 | ||||
3951 | auto &DstI = Target.getInstruction(DstOp); | |||
3952 | if (DstI.Operands.NumDefs != Src->getExtTypes().size()) | |||
3953 | return failedImport("Src pattern results and dst MI defs are different (" + | |||
3954 | to_string(Src->getExtTypes().size()) + " def(s) vs " + | |||
3955 | to_string(DstI.Operands.NumDefs) + " def(s))"); | |||
3956 | ||||
3957 | // The root of the match also has constraints on the register bank so that it | |||
3958 | // matches the result instruction. | |||
3959 | unsigned OpIdx = 0; | |||
3960 | for (const TypeSetByHwMode &VTy : Src->getExtTypes()) { | |||
3961 | (void)VTy; | |||
3962 | ||||
3963 | const auto &DstIOperand = DstI.Operands[OpIdx]; | |||
3964 | Record *DstIOpRec = DstIOperand.Rec; | |||
3965 | if (DstI.TheDef->getName() == "COPY_TO_REGCLASS") { | |||
3966 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); | |||
3967 | ||||
3968 | if (DstIOpRec == nullptr) | |||
3969 | return failedImport( | |||
3970 | "COPY_TO_REGCLASS operand #1 isn't a register class"); | |||
3971 | } else if (DstI.TheDef->getName() == "EXTRACT_SUBREG") { | |||
3972 | if (!Dst->getChild(0)->isLeaf()) | |||
3973 | return failedImport("EXTRACT_SUBREG operand #0 isn't a leaf"); | |||
3974 | ||||
3975 | // We can assume that a subregister is in the same bank as it's super | |||
3976 | // register. | |||
3977 | DstIOpRec = getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); | |||
3978 | ||||
3979 | if (DstIOpRec == nullptr) | |||
3980 | return failedImport( | |||
3981 | "EXTRACT_SUBREG operand #0 isn't a register class"); | |||
3982 | } else if (DstIOpRec->isSubClassOf("RegisterOperand")) | |||
3983 | DstIOpRec = DstIOpRec->getValueAsDef("RegClass"); | |||
3984 | else if (!DstIOpRec->isSubClassOf("RegisterClass")) | |||
3985 | return failedImport("Dst MI def isn't a register class" + | |||
3986 | to_string(*Dst)); | |||
3987 | ||||
3988 | OperandMatcher &OM = InsnMatcher.getOperand(OpIdx); | |||
3989 | OM.setSymbolicName(DstIOperand.Name); | |||
3990 | M.defineOperand(OM.getSymbolicName(), OM); | |||
3991 | OM.addPredicate<RegisterBankOperandMatcher>( | |||
3992 | Target.getRegisterClass(DstIOpRec)); | |||
3993 | ++OpIdx; | |||
3994 | } | |||
3995 | ||||
3996 | auto DstMIBuilderOrError = createAndImportInstructionRenderer(M, Dst); | |||
3997 | if (auto Error = DstMIBuilderOrError.takeError()) | |||
3998 | return std::move(Error); | |||
3999 | BuildMIAction &DstMIBuilder = DstMIBuilderOrError.get(); | |||
4000 | ||||
4001 | // Render the implicit defs. | |||
4002 | // These are only added to the root of the result. | |||
4003 | if (auto Error = importImplicitDefRenderers(DstMIBuilder, P.getDstRegs())) | |||
4004 | return std::move(Error); | |||
4005 | ||||
4006 | DstMIBuilder.chooseInsnToMutate(M); | |||
4007 | ||||
4008 | // Constrain the registers to classes. This is normally derived from the | |||
4009 | // emitted instruction but a few instructions require special handling. | |||
4010 | if (DstI.TheDef->getName() == "COPY_TO_REGCLASS") { | |||
4011 | // COPY_TO_REGCLASS does not provide operand constraints itself but the | |||
4012 | // result is constrained to the class given by the second child. | |||
4013 | Record *DstIOpRec = | |||
4014 | getInitValueAsRegClass(Dst->getChild(1)->getLeafValue()); | |||
4015 | ||||
4016 | if (DstIOpRec == nullptr) | |||
4017 | return failedImport("COPY_TO_REGCLASS operand #1 isn't a register class"); | |||
4018 | ||||
4019 | M.addAction<ConstrainOperandToRegClassAction>( | |||
4020 | 0, 0, Target.getRegisterClass(DstIOpRec)); | |||
4021 | ||||
4022 | // We're done with this pattern! It's eligible for GISel emission; return | |||
4023 | // it. | |||
4024 | ++NumPatternImported; | |||
4025 | return std::move(M); | |||
4026 | } | |||
4027 | ||||
4028 | if (DstI.TheDef->getName() == "EXTRACT_SUBREG") { | |||
4029 | // EXTRACT_SUBREG selects into a subregister COPY but unlike most | |||
4030 | // instructions, the result register class is controlled by the | |||
4031 | // subregisters of the operand. As a result, we must constrain the result | |||
4032 | // class rather than check that it's already the right one. | |||
4033 | if (!Dst->getChild(0)->isLeaf()) | |||
4034 | return failedImport("EXTRACT_SUBREG child #1 is not a leaf"); | |||
4035 | ||||
4036 | DefInit *SubRegInit = dyn_cast<DefInit>(Dst->getChild(1)->getLeafValue()); | |||
4037 | if (!SubRegInit) | |||
4038 | return failedImport("EXTRACT_SUBREG child #1 is not a subreg index"); | |||
4039 | ||||
4040 | // Constrain the result to the same register bank as the operand. | |||
4041 | Record *DstIOpRec = | |||
4042 | getInitValueAsRegClass(Dst->getChild(0)->getLeafValue()); | |||
4043 | ||||
4044 | if (DstIOpRec == nullptr) | |||
4045 | return failedImport("EXTRACT_SUBREG operand #1 isn't a register class"); | |||
4046 | ||||
4047 | CodeGenSubRegIndex *SubIdx = CGRegs.getSubRegIdx(SubRegInit->getDef()); | |||
4048 | CodeGenRegisterClass *SrcRC = CGRegs.getRegClass(DstIOpRec); | |||
4049 | ||||
4050 | // It would be nice to leave this constraint implicit but we're required | |||
4051 | // to pick a register class so constrain the result to a register class | |||
4052 | // that can hold the correct MVT. | |||
4053 | // | |||
4054 | // FIXME: This may introduce an extra copy if the chosen class doesn't | |||
4055 | // actually contain the subregisters. | |||
4056 | assert(Src->getExtTypes().size() == 1 &&((Src->getExtTypes().size() == 1 && "Expected Src of EXTRACT_SUBREG to have one result type" ) ? static_cast<void> (0) : __assert_fail ("Src->getExtTypes().size() == 1 && \"Expected Src of EXTRACT_SUBREG to have one result type\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4057, __PRETTY_FUNCTION__)) | |||
4057 | "Expected Src of EXTRACT_SUBREG to have one result type")((Src->getExtTypes().size() == 1 && "Expected Src of EXTRACT_SUBREG to have one result type" ) ? static_cast<void> (0) : __assert_fail ("Src->getExtTypes().size() == 1 && \"Expected Src of EXTRACT_SUBREG to have one result type\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4057, __PRETTY_FUNCTION__)); | |||
4058 | ||||
4059 | const auto &SrcRCDstRCPair = | |||
4060 | SrcRC->getMatchingSubClassWithSubRegs(CGRegs, SubIdx); | |||
4061 | assert(SrcRCDstRCPair->second && "Couldn't find a matching subclass")((SrcRCDstRCPair->second && "Couldn't find a matching subclass" ) ? static_cast<void> (0) : __assert_fail ("SrcRCDstRCPair->second && \"Couldn't find a matching subclass\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4061, __PRETTY_FUNCTION__)); | |||
4062 | M.addAction<ConstrainOperandToRegClassAction>(0, 0, *SrcRCDstRCPair->second); | |||
4063 | M.addAction<ConstrainOperandToRegClassAction>(0, 1, *SrcRCDstRCPair->first); | |||
4064 | ||||
4065 | // We're done with this pattern! It's eligible for GISel emission; return | |||
4066 | // it. | |||
4067 | ++NumPatternImported; | |||
4068 | return std::move(M); | |||
4069 | } | |||
4070 | ||||
4071 | M.addAction<ConstrainOperandsToDefinitionAction>(0); | |||
4072 | ||||
4073 | // We're done with this pattern! It's eligible for GISel emission; return it. | |||
4074 | ++NumPatternImported; | |||
4075 | return std::move(M); | |||
4076 | } | |||
4077 | ||||
4078 | // Emit imm predicate table and an enum to reference them with. | |||
4079 | // The 'Predicate_' part of the name is redundant but eliminating it is more | |||
4080 | // trouble than it's worth. | |||
4081 | void GlobalISelEmitter::emitCxxPredicateFns( | |||
4082 | raw_ostream &OS, StringRef CodeFieldName, StringRef TypeIdentifier, | |||
4083 | StringRef ArgType, StringRef ArgName, StringRef AdditionalDeclarations, | |||
4084 | std::function<bool(const Record *R)> Filter) { | |||
4085 | std::vector<const Record *> MatchedRecords; | |||
4086 | const auto &Defs = RK.getAllDerivedDefinitions("PatFrag"); | |||
4087 | std::copy_if(Defs.begin(), Defs.end(), std::back_inserter(MatchedRecords), | |||
4088 | [&](Record *Record) { | |||
4089 | return !Record->getValueAsString(CodeFieldName).empty() && | |||
4090 | Filter(Record); | |||
4091 | }); | |||
4092 | ||||
4093 | if (!MatchedRecords.empty()) { | |||
4094 | OS << "// PatFrag predicates.\n" | |||
4095 | << "enum {\n"; | |||
4096 | std::string EnumeratorSeparator = | |||
4097 | (" = GIPFP_" + TypeIdentifier + "_Invalid + 1,\n").str(); | |||
4098 | for (const auto *Record : MatchedRecords) { | |||
4099 | OS << " GIPFP_" << TypeIdentifier << "_Predicate_" << Record->getName() | |||
4100 | << EnumeratorSeparator; | |||
4101 | EnumeratorSeparator = ",\n"; | |||
4102 | } | |||
4103 | OS << "};\n"; | |||
4104 | } | |||
4105 | ||||
4106 | OS << "bool " << Target.getName() << "InstructionSelector::test" << ArgName | |||
4107 | << "Predicate_" << TypeIdentifier << "(unsigned PredicateID, " << ArgType << " " | |||
4108 | << ArgName << ") const {\n" | |||
4109 | << AdditionalDeclarations; | |||
4110 | if (!AdditionalDeclarations.empty()) | |||
4111 | OS << "\n"; | |||
4112 | if (!MatchedRecords.empty()) | |||
4113 | OS << " switch (PredicateID) {\n"; | |||
4114 | for (const auto *Record : MatchedRecords) { | |||
4115 | OS << " case GIPFP_" << TypeIdentifier << "_Predicate_" | |||
4116 | << Record->getName() << ": {\n" | |||
4117 | << " " << Record->getValueAsString(CodeFieldName) << "\n" | |||
4118 | << " llvm_unreachable(\"" << CodeFieldName | |||
4119 | << " should have returned\");\n" | |||
4120 | << " return false;\n" | |||
4121 | << " }\n"; | |||
4122 | } | |||
4123 | if (!MatchedRecords.empty()) | |||
4124 | OS << " }\n"; | |||
4125 | OS << " llvm_unreachable(\"Unknown predicate\");\n" | |||
4126 | << " return false;\n" | |||
4127 | << "}\n"; | |||
4128 | } | |||
4129 | ||||
4130 | void GlobalISelEmitter::emitImmPredicateFns( | |||
4131 | raw_ostream &OS, StringRef TypeIdentifier, StringRef ArgType, | |||
4132 | std::function<bool(const Record *R)> Filter) { | |||
4133 | return emitCxxPredicateFns(OS, "ImmediateCode", TypeIdentifier, ArgType, | |||
4134 | "Imm", "", Filter); | |||
4135 | } | |||
4136 | ||||
4137 | void GlobalISelEmitter::emitMIPredicateFns(raw_ostream &OS) { | |||
4138 | return emitCxxPredicateFns( | |||
4139 | OS, "GISelPredicateCode", "MI", "const MachineInstr &", "MI", | |||
4140 | " const MachineFunction &MF = *MI.getParent()->getParent();\n" | |||
4141 | " const MachineRegisterInfo &MRI = MF.getRegInfo();\n" | |||
4142 | " (void)MRI;", | |||
4143 | [](const Record *R) { return true; }); | |||
4144 | } | |||
4145 | ||||
4146 | template <class GroupT> | |||
4147 | std::vector<Matcher *> GlobalISelEmitter::optimizeRules( | |||
4148 | ArrayRef<Matcher *> Rules, | |||
4149 | std::vector<std::unique_ptr<Matcher>> &MatcherStorage) { | |||
4150 | ||||
4151 | std::vector<Matcher *> OptRules; | |||
4152 | std::unique_ptr<GroupT> CurrentGroup = make_unique<GroupT>(); | |||
4153 | assert(CurrentGroup->empty() && "Newly created group isn't empty!")((CurrentGroup->empty() && "Newly created group isn't empty!" ) ? static_cast<void> (0) : __assert_fail ("CurrentGroup->empty() && \"Newly created group isn't empty!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4153, __PRETTY_FUNCTION__)); | |||
4154 | unsigned NumGroups = 0; | |||
4155 | ||||
4156 | auto ProcessCurrentGroup = [&]() { | |||
4157 | if (CurrentGroup->empty()) | |||
4158 | // An empty group is good to be reused: | |||
4159 | return; | |||
4160 | ||||
4161 | // If the group isn't large enough to provide any benefit, move all the | |||
4162 | // added rules out of it and make sure to re-create the group to properly | |||
4163 | // re-initialize it: | |||
4164 | if (CurrentGroup->size() < 2) | |||
4165 | for (Matcher *M : CurrentGroup->matchers()) | |||
4166 | OptRules.push_back(M); | |||
4167 | else { | |||
4168 | CurrentGroup->finalize(); | |||
4169 | OptRules.push_back(CurrentGroup.get()); | |||
4170 | MatcherStorage.emplace_back(std::move(CurrentGroup)); | |||
4171 | ++NumGroups; | |||
4172 | } | |||
4173 | CurrentGroup = make_unique<GroupT>(); | |||
4174 | }; | |||
4175 | for (Matcher *Rule : Rules) { | |||
4176 | // Greedily add as many matchers as possible to the current group: | |||
4177 | if (CurrentGroup->addMatcher(*Rule)) | |||
4178 | continue; | |||
4179 | ||||
4180 | ProcessCurrentGroup(); | |||
4181 | assert(CurrentGroup->empty() && "A group wasn't properly re-initialized")((CurrentGroup->empty() && "A group wasn't properly re-initialized" ) ? static_cast<void> (0) : __assert_fail ("CurrentGroup->empty() && \"A group wasn't properly re-initialized\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4181, __PRETTY_FUNCTION__)); | |||
4182 | ||||
4183 | // Try to add the pending matcher to a newly created empty group: | |||
4184 | if (!CurrentGroup->addMatcher(*Rule)) | |||
4185 | // If we couldn't add the matcher to an empty group, that group type | |||
4186 | // doesn't support that kind of matchers at all, so just skip it: | |||
4187 | OptRules.push_back(Rule); | |||
4188 | } | |||
4189 | ProcessCurrentGroup(); | |||
4190 | ||||
4191 | LLVM_DEBUG(dbgs() << "NumGroups: " << NumGroups << "\n")do { if (::llvm::DebugFlag && ::llvm::isCurrentDebugType ("gisel-emitter")) { dbgs() << "NumGroups: " << NumGroups << "\n"; } } while (false); | |||
4192 | assert(CurrentGroup->empty() && "The last group wasn't properly processed")((CurrentGroup->empty() && "The last group wasn't properly processed" ) ? static_cast<void> (0) : __assert_fail ("CurrentGroup->empty() && \"The last group wasn't properly processed\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4192, __PRETTY_FUNCTION__)); | |||
4193 | return OptRules; | |||
4194 | } | |||
4195 | ||||
4196 | MatchTable | |||
4197 | GlobalISelEmitter::buildMatchTable(MutableArrayRef<RuleMatcher> Rules, | |||
4198 | bool Optimize, bool WithCoverage) { | |||
4199 | std::vector<Matcher *> InputRules; | |||
4200 | for (Matcher &Rule : Rules) | |||
4201 | InputRules.push_back(&Rule); | |||
4202 | ||||
4203 | if (!Optimize) | |||
4204 | return MatchTable::buildTable(InputRules, WithCoverage); | |||
4205 | ||||
4206 | unsigned CurrentOrdering = 0; | |||
4207 | StringMap<unsigned> OpcodeOrder; | |||
4208 | for (RuleMatcher &Rule : Rules) { | |||
4209 | const StringRef Opcode = Rule.getOpcode(); | |||
4210 | assert(!Opcode.empty() && "Didn't expect an undefined opcode")((!Opcode.empty() && "Didn't expect an undefined opcode" ) ? static_cast<void> (0) : __assert_fail ("!Opcode.empty() && \"Didn't expect an undefined opcode\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4210, __PRETTY_FUNCTION__)); | |||
4211 | if (OpcodeOrder.count(Opcode) == 0) | |||
4212 | OpcodeOrder[Opcode] = CurrentOrdering++; | |||
4213 | } | |||
4214 | ||||
4215 | std::stable_sort(InputRules.begin(), InputRules.end(), | |||
4216 | [&OpcodeOrder](const Matcher *A, const Matcher *B) { | |||
4217 | auto *L = static_cast<const RuleMatcher *>(A); | |||
4218 | auto *R = static_cast<const RuleMatcher *>(B); | |||
4219 | return std::make_tuple(OpcodeOrder[L->getOpcode()], | |||
4220 | L->getNumOperands()) < | |||
4221 | std::make_tuple(OpcodeOrder[R->getOpcode()], | |||
4222 | R->getNumOperands()); | |||
4223 | }); | |||
4224 | ||||
4225 | for (Matcher *Rule : InputRules) | |||
4226 | Rule->optimize(); | |||
4227 | ||||
4228 | std::vector<std::unique_ptr<Matcher>> MatcherStorage; | |||
4229 | std::vector<Matcher *> OptRules = | |||
4230 | optimizeRules<GroupMatcher>(InputRules, MatcherStorage); | |||
4231 | ||||
4232 | for (Matcher *Rule : OptRules) | |||
4233 | Rule->optimize(); | |||
4234 | ||||
4235 | OptRules = optimizeRules<SwitchMatcher>(OptRules, MatcherStorage); | |||
4236 | ||||
4237 | return MatchTable::buildTable(OptRules, WithCoverage); | |||
4238 | } | |||
4239 | ||||
4240 | void GroupMatcher::optimize() { | |||
4241 | // Make sure we only sort by a specific predicate within a range of rules that | |||
4242 | // all have that predicate checked against a specific value (not a wildcard): | |||
4243 | auto F = Matchers.begin(); | |||
4244 | auto T = F; | |||
4245 | auto E = Matchers.end(); | |||
4246 | while (T != E) { | |||
4247 | while (T != E) { | |||
4248 | auto *R = static_cast<RuleMatcher *>(*T); | |||
4249 | if (!R->getFirstConditionAsRootType().get().isValid()) | |||
4250 | break; | |||
4251 | ++T; | |||
4252 | } | |||
4253 | std::stable_sort(F, T, [](Matcher *A, Matcher *B) { | |||
4254 | auto *L = static_cast<RuleMatcher *>(A); | |||
4255 | auto *R = static_cast<RuleMatcher *>(B); | |||
4256 | return L->getFirstConditionAsRootType() < | |||
4257 | R->getFirstConditionAsRootType(); | |||
4258 | }); | |||
4259 | if (T != E) | |||
4260 | F = ++T; | |||
4261 | } | |||
4262 | GlobalISelEmitter::optimizeRules<GroupMatcher>(Matchers, MatcherStorage) | |||
4263 | .swap(Matchers); | |||
4264 | GlobalISelEmitter::optimizeRules<SwitchMatcher>(Matchers, MatcherStorage) | |||
4265 | .swap(Matchers); | |||
4266 | } | |||
4267 | ||||
4268 | void GlobalISelEmitter::run(raw_ostream &OS) { | |||
4269 | if (!UseCoverageFile.empty()) { | |||
4270 | RuleCoverage = CodeGenCoverage(); | |||
4271 | auto RuleCoverageBufOrErr = MemoryBuffer::getFile(UseCoverageFile); | |||
4272 | if (!RuleCoverageBufOrErr) { | |||
4273 | PrintWarning(SMLoc(), "Missing rule coverage data"); | |||
4274 | RuleCoverage = None; | |||
4275 | } else { | |||
4276 | if (!RuleCoverage->parse(*RuleCoverageBufOrErr.get(), Target.getName())) { | |||
4277 | PrintWarning(SMLoc(), "Ignoring invalid or missing rule coverage data"); | |||
4278 | RuleCoverage = None; | |||
4279 | } | |||
4280 | } | |||
4281 | } | |||
4282 | ||||
4283 | // Track the run-time opcode values | |||
4284 | gatherOpcodeValues(); | |||
4285 | // Track the run-time LLT ID values | |||
4286 | gatherTypeIDValues(); | |||
4287 | ||||
4288 | // Track the GINodeEquiv definitions. | |||
4289 | gatherNodeEquivs(); | |||
4290 | ||||
4291 | emitSourceFileHeader(("Global Instruction Selector for the " + | |||
4292 | Target.getName() + " target").str(), OS); | |||
4293 | std::vector<RuleMatcher> Rules; | |||
4294 | // Look through the SelectionDAG patterns we found, possibly emitting some. | |||
4295 | for (const PatternToMatch &Pat : CGP.ptms()) { | |||
4296 | ++NumPatternTotal; | |||
4297 | ||||
4298 | auto MatcherOrErr = runOnPattern(Pat); | |||
4299 | ||||
4300 | // The pattern analysis can fail, indicating an unsupported pattern. | |||
4301 | // Report that if we've been asked to do so. | |||
4302 | if (auto Err = MatcherOrErr.takeError()) { | |||
4303 | if (WarnOnSkippedPatterns) { | |||
4304 | PrintWarning(Pat.getSrcRecord()->getLoc(), | |||
4305 | "Skipped pattern: " + toString(std::move(Err))); | |||
4306 | } else { | |||
4307 | consumeError(std::move(Err)); | |||
4308 | } | |||
4309 | ++NumPatternImportsSkipped; | |||
4310 | continue; | |||
4311 | } | |||
4312 | ||||
4313 | if (RuleCoverage) { | |||
4314 | if (RuleCoverage->isCovered(MatcherOrErr->getRuleID())) | |||
4315 | ++NumPatternsTested; | |||
4316 | else | |||
4317 | PrintWarning(Pat.getSrcRecord()->getLoc(), | |||
4318 | "Pattern is not covered by a test"); | |||
4319 | } | |||
4320 | Rules.push_back(std::move(MatcherOrErr.get())); | |||
4321 | } | |||
4322 | ||||
4323 | // Comparison function to order records by name. | |||
4324 | auto orderByName = [](const Record *A, const Record *B) { | |||
4325 | return A->getName() < B->getName(); | |||
4326 | }; | |||
4327 | ||||
4328 | std::vector<Record *> ComplexPredicates = | |||
4329 | RK.getAllDerivedDefinitions("GIComplexOperandMatcher"); | |||
4330 | llvm::sort(ComplexPredicates, orderByName); | |||
4331 | ||||
4332 | std::vector<Record *> CustomRendererFns = | |||
4333 | RK.getAllDerivedDefinitions("GICustomOperandRenderer"); | |||
4334 | llvm::sort(CustomRendererFns, orderByName); | |||
4335 | ||||
4336 | unsigned MaxTemporaries = 0; | |||
4337 | for (const auto &Rule : Rules) | |||
4338 | MaxTemporaries = std::max(MaxTemporaries, Rule.countRendererFns()); | |||
4339 | ||||
4340 | OS << "#ifdef GET_GLOBALISEL_PREDICATE_BITSET\n" | |||
4341 | << "const unsigned MAX_SUBTARGET_PREDICATES = " << SubtargetFeatures.size() | |||
4342 | << ";\n" | |||
4343 | << "using PredicateBitset = " | |||
4344 | "llvm::PredicateBitsetImpl<MAX_SUBTARGET_PREDICATES>;\n" | |||
4345 | << "#endif // ifdef GET_GLOBALISEL_PREDICATE_BITSET\n\n"; | |||
4346 | ||||
4347 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n" | |||
4348 | << " mutable MatcherState State;\n" | |||
4349 | << " typedef " | |||
4350 | "ComplexRendererFns(" | |||
4351 | << Target.getName() | |||
4352 | << "InstructionSelector::*ComplexMatcherMemFn)(MachineOperand &) const;\n" | |||
4353 | ||||
4354 | << " typedef void(" << Target.getName() | |||
4355 | << "InstructionSelector::*CustomRendererFn)(MachineInstrBuilder &, const " | |||
4356 | "MachineInstr&) " | |||
4357 | "const;\n" | |||
4358 | << " const ISelInfoTy<PredicateBitset, ComplexMatcherMemFn, " | |||
4359 | "CustomRendererFn> " | |||
4360 | "ISelInfo;\n"; | |||
4361 | OS << " static " << Target.getName() | |||
4362 | << "InstructionSelector::ComplexMatcherMemFn ComplexPredicateFns[];\n" | |||
4363 | << " static " << Target.getName() | |||
4364 | << "InstructionSelector::CustomRendererFn CustomRenderers[];\n" | |||
4365 | << " bool testImmPredicate_I64(unsigned PredicateID, int64_t Imm) const " | |||
4366 | "override;\n" | |||
4367 | << " bool testImmPredicate_APInt(unsigned PredicateID, const APInt &Imm) " | |||
4368 | "const override;\n" | |||
4369 | << " bool testImmPredicate_APFloat(unsigned PredicateID, const APFloat " | |||
4370 | "&Imm) const override;\n" | |||
4371 | << " const int64_t *getMatchTable() const override;\n" | |||
4372 | << " bool testMIPredicate_MI(unsigned PredicateID, const MachineInstr &MI) " | |||
4373 | "const override;\n" | |||
4374 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_DECL\n\n"; | |||
4375 | ||||
4376 | OS << "#ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n" | |||
4377 | << ", State(" << MaxTemporaries << "),\n" | |||
4378 | << "ISelInfo(TypeObjects, NumTypeObjects, FeatureBitsets" | |||
4379 | << ", ComplexPredicateFns, CustomRenderers)\n" | |||
4380 | << "#endif // ifdef GET_GLOBALISEL_TEMPORARIES_INIT\n\n"; | |||
4381 | ||||
4382 | OS << "#ifdef GET_GLOBALISEL_IMPL\n"; | |||
4383 | SubtargetFeatureInfo::emitSubtargetFeatureBitEnumeration(SubtargetFeatures, | |||
4384 | OS); | |||
4385 | ||||
4386 | // Separate subtarget features by how often they must be recomputed. | |||
4387 | SubtargetFeatureInfoMap ModuleFeatures; | |||
4388 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), | |||
4389 | std::inserter(ModuleFeatures, ModuleFeatures.end()), | |||
4390 | [](const SubtargetFeatureInfoMap::value_type &X) { | |||
4391 | return !X.second.mustRecomputePerFunction(); | |||
4392 | }); | |||
4393 | SubtargetFeatureInfoMap FunctionFeatures; | |||
4394 | std::copy_if(SubtargetFeatures.begin(), SubtargetFeatures.end(), | |||
4395 | std::inserter(FunctionFeatures, FunctionFeatures.end()), | |||
4396 | [](const SubtargetFeatureInfoMap::value_type &X) { | |||
4397 | return X.second.mustRecomputePerFunction(); | |||
4398 | }); | |||
4399 | ||||
4400 | SubtargetFeatureInfo::emitComputeAvailableFeatures( | |||
4401 | Target.getName(), "InstructionSelector", "computeAvailableModuleFeatures", | |||
4402 | ModuleFeatures, OS); | |||
4403 | SubtargetFeatureInfo::emitComputeAvailableFeatures( | |||
4404 | Target.getName(), "InstructionSelector", | |||
4405 | "computeAvailableFunctionFeatures", FunctionFeatures, OS, | |||
4406 | "const MachineFunction *MF"); | |||
4407 | ||||
4408 | // Emit a table containing the LLT objects needed by the matcher and an enum | |||
4409 | // for the matcher to reference them with. | |||
4410 | std::vector<LLTCodeGen> TypeObjects; | |||
4411 | for (const auto &Ty : KnownTypes) | |||
4412 | TypeObjects.push_back(Ty); | |||
4413 | llvm::sort(TypeObjects); | |||
4414 | OS << "// LLT Objects.\n" | |||
4415 | << "enum {\n"; | |||
4416 | for (const auto &TypeObject : TypeObjects) { | |||
4417 | OS << " "; | |||
4418 | TypeObject.emitCxxEnumValue(OS); | |||
4419 | OS << ",\n"; | |||
4420 | } | |||
4421 | OS << "};\n"; | |||
4422 | OS << "const static size_t NumTypeObjects = " << TypeObjects.size() << ";\n" | |||
4423 | << "const static LLT TypeObjects[] = {\n"; | |||
4424 | for (const auto &TypeObject : TypeObjects) { | |||
4425 | OS << " "; | |||
4426 | TypeObject.emitCxxConstructorCall(OS); | |||
4427 | OS << ",\n"; | |||
4428 | } | |||
4429 | OS << "};\n\n"; | |||
4430 | ||||
4431 | // Emit a table containing the PredicateBitsets objects needed by the matcher | |||
4432 | // and an enum for the matcher to reference them with. | |||
4433 | std::vector<std::vector<Record *>> FeatureBitsets; | |||
4434 | for (auto &Rule : Rules) | |||
4435 | FeatureBitsets.push_back(Rule.getRequiredFeatures()); | |||
4436 | llvm::sort(FeatureBitsets, [&](const std::vector<Record *> &A, | |||
4437 | const std::vector<Record *> &B) { | |||
4438 | if (A.size() < B.size()) | |||
4439 | return true; | |||
4440 | if (A.size() > B.size()) | |||
4441 | return false; | |||
4442 | for (const auto &Pair : zip(A, B)) { | |||
4443 | if (std::get<0>(Pair)->getName() < std::get<1>(Pair)->getName()) | |||
4444 | return true; | |||
4445 | if (std::get<0>(Pair)->getName() > std::get<1>(Pair)->getName()) | |||
4446 | return false; | |||
4447 | } | |||
4448 | return false; | |||
4449 | }); | |||
4450 | FeatureBitsets.erase( | |||
4451 | std::unique(FeatureBitsets.begin(), FeatureBitsets.end()), | |||
4452 | FeatureBitsets.end()); | |||
4453 | OS << "// Feature bitsets.\n" | |||
4454 | << "enum {\n" | |||
4455 | << " GIFBS_Invalid,\n"; | |||
4456 | for (const auto &FeatureBitset : FeatureBitsets) { | |||
4457 | if (FeatureBitset.empty()) | |||
4458 | continue; | |||
4459 | OS << " " << getNameForFeatureBitset(FeatureBitset) << ",\n"; | |||
4460 | } | |||
4461 | OS << "};\n" | |||
4462 | << "const static PredicateBitset FeatureBitsets[] {\n" | |||
4463 | << " {}, // GIFBS_Invalid\n"; | |||
4464 | for (const auto &FeatureBitset : FeatureBitsets) { | |||
4465 | if (FeatureBitset.empty()) | |||
4466 | continue; | |||
4467 | OS << " {"; | |||
4468 | for (const auto &Feature : FeatureBitset) { | |||
4469 | const auto &I = SubtargetFeatures.find(Feature); | |||
4470 | assert(I != SubtargetFeatures.end() && "Didn't import predicate?")((I != SubtargetFeatures.end() && "Didn't import predicate?" ) ? static_cast<void> (0) : __assert_fail ("I != SubtargetFeatures.end() && \"Didn't import predicate?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4470, __PRETTY_FUNCTION__)); | |||
4471 | OS << I->second.getEnumBitName() << ", "; | |||
4472 | } | |||
4473 | OS << "},\n"; | |||
4474 | } | |||
4475 | OS << "};\n\n"; | |||
4476 | ||||
4477 | // Emit complex predicate table and an enum to reference them with. | |||
4478 | OS << "// ComplexPattern predicates.\n" | |||
4479 | << "enum {\n" | |||
4480 | << " GICP_Invalid,\n"; | |||
4481 | for (const auto &Record : ComplexPredicates) | |||
4482 | OS << " GICP_" << Record->getName() << ",\n"; | |||
4483 | OS << "};\n" | |||
4484 | << "// See constructor for table contents\n\n"; | |||
4485 | ||||
4486 | emitImmPredicateFns(OS, "I64", "int64_t", [](const Record *R) { | |||
4487 | bool Unset; | |||
4488 | return !R->getValueAsBitOrUnset("IsAPFloat", Unset) && | |||
4489 | !R->getValueAsBit("IsAPInt"); | |||
4490 | }); | |||
4491 | emitImmPredicateFns(OS, "APFloat", "const APFloat &", [](const Record *R) { | |||
4492 | bool Unset; | |||
4493 | return R->getValueAsBitOrUnset("IsAPFloat", Unset); | |||
4494 | }); | |||
4495 | emitImmPredicateFns(OS, "APInt", "const APInt &", [](const Record *R) { | |||
4496 | return R->getValueAsBit("IsAPInt"); | |||
4497 | }); | |||
4498 | emitMIPredicateFns(OS); | |||
4499 | OS << "\n"; | |||
4500 | ||||
4501 | OS << Target.getName() << "InstructionSelector::ComplexMatcherMemFn\n" | |||
4502 | << Target.getName() << "InstructionSelector::ComplexPredicateFns[] = {\n" | |||
4503 | << " nullptr, // GICP_Invalid\n"; | |||
4504 | for (const auto &Record : ComplexPredicates) | |||
4505 | OS << " &" << Target.getName() | |||
4506 | << "InstructionSelector::" << Record->getValueAsString("MatcherFn") | |||
4507 | << ", // " << Record->getName() << "\n"; | |||
4508 | OS << "};\n\n"; | |||
4509 | ||||
4510 | OS << "// Custom renderers.\n" | |||
4511 | << "enum {\n" | |||
4512 | << " GICR_Invalid,\n"; | |||
4513 | for (const auto &Record : CustomRendererFns) | |||
4514 | OS << " GICR_" << Record->getValueAsString("RendererFn") << ", \n"; | |||
4515 | OS << "};\n"; | |||
4516 | ||||
4517 | OS << Target.getName() << "InstructionSelector::CustomRendererFn\n" | |||
4518 | << Target.getName() << "InstructionSelector::CustomRenderers[] = {\n" | |||
4519 | << " nullptr, // GICP_Invalid\n"; | |||
4520 | for (const auto &Record : CustomRendererFns) | |||
4521 | OS << " &" << Target.getName() | |||
4522 | << "InstructionSelector::" << Record->getValueAsString("RendererFn") | |||
4523 | << ", // " << Record->getName() << "\n"; | |||
4524 | OS << "};\n\n"; | |||
4525 | ||||
4526 | llvm::stable_sort(Rules, [&](const RuleMatcher &A, const RuleMatcher &B) { | |||
4527 | int ScoreA = RuleMatcherScores[A.getRuleID()]; | |||
4528 | int ScoreB = RuleMatcherScores[B.getRuleID()]; | |||
4529 | if (ScoreA > ScoreB) | |||
4530 | return true; | |||
4531 | if (ScoreB > ScoreA) | |||
4532 | return false; | |||
4533 | if (A.isHigherPriorityThan(B)) { | |||
4534 | assert(!B.isHigherPriorityThan(A) && "Cannot be more important "((!B.isHigherPriorityThan(A) && "Cannot be more important " "and less important at " "the same time") ? static_cast<void > (0) : __assert_fail ("!B.isHigherPriorityThan(A) && \"Cannot be more important \" \"and less important at \" \"the same time\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4536, __PRETTY_FUNCTION__)) | |||
4535 | "and less important at "((!B.isHigherPriorityThan(A) && "Cannot be more important " "and less important at " "the same time") ? static_cast<void > (0) : __assert_fail ("!B.isHigherPriorityThan(A) && \"Cannot be more important \" \"and less important at \" \"the same time\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4536, __PRETTY_FUNCTION__)) | |||
4536 | "the same time")((!B.isHigherPriorityThan(A) && "Cannot be more important " "and less important at " "the same time") ? static_cast<void > (0) : __assert_fail ("!B.isHigherPriorityThan(A) && \"Cannot be more important \" \"and less important at \" \"the same time\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4536, __PRETTY_FUNCTION__)); | |||
4537 | return true; | |||
4538 | } | |||
4539 | return false; | |||
4540 | }); | |||
4541 | ||||
4542 | OS << "bool " << Target.getName() | |||
4543 | << "InstructionSelector::selectImpl(MachineInstr &I, CodeGenCoverage " | |||
4544 | "&CoverageInfo) const {\n" | |||
4545 | << " MachineFunction &MF = *I.getParent()->getParent();\n" | |||
4546 | << " MachineRegisterInfo &MRI = MF.getRegInfo();\n" | |||
4547 | << " // FIXME: This should be computed on a per-function basis rather " | |||
4548 | "than per-insn.\n" | |||
4549 | << " AvailableFunctionFeatures = computeAvailableFunctionFeatures(&STI, " | |||
4550 | "&MF);\n" | |||
4551 | << " const PredicateBitset AvailableFeatures = getAvailableFeatures();\n" | |||
4552 | << " NewMIVector OutMIs;\n" | |||
4553 | << " State.MIs.clear();\n" | |||
4554 | << " State.MIs.push_back(&I);\n\n" | |||
4555 | << " if (executeMatchTable(*this, OutMIs, State, ISelInfo" | |||
4556 | << ", getMatchTable(), TII, MRI, TRI, RBI, AvailableFeatures" | |||
4557 | << ", CoverageInfo)) {\n" | |||
4558 | << " return true;\n" | |||
4559 | << " }\n\n" | |||
4560 | << " return false;\n" | |||
4561 | << "}\n\n"; | |||
4562 | ||||
4563 | const MatchTable Table = | |||
4564 | buildMatchTable(Rules, OptimizeMatchTable, GenerateCoverage); | |||
4565 | OS << "const int64_t *" << Target.getName() | |||
4566 | << "InstructionSelector::getMatchTable() const {\n"; | |||
4567 | Table.emitDeclaration(OS); | |||
4568 | OS << " return "; | |||
4569 | Table.emitUse(OS); | |||
4570 | OS << ";\n}\n"; | |||
4571 | OS << "#endif // ifdef GET_GLOBALISEL_IMPL\n"; | |||
4572 | ||||
4573 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_DECL\n" | |||
4574 | << "PredicateBitset AvailableModuleFeatures;\n" | |||
4575 | << "mutable PredicateBitset AvailableFunctionFeatures;\n" | |||
4576 | << "PredicateBitset getAvailableFeatures() const {\n" | |||
4577 | << " return AvailableModuleFeatures | AvailableFunctionFeatures;\n" | |||
4578 | << "}\n" | |||
4579 | << "PredicateBitset\n" | |||
4580 | << "computeAvailableModuleFeatures(const " << Target.getName() | |||
4581 | << "Subtarget *Subtarget) const;\n" | |||
4582 | << "PredicateBitset\n" | |||
4583 | << "computeAvailableFunctionFeatures(const " << Target.getName() | |||
4584 | << "Subtarget *Subtarget,\n" | |||
4585 | << " const MachineFunction *MF) const;\n" | |||
4586 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_DECL\n"; | |||
4587 | ||||
4588 | OS << "#ifdef GET_GLOBALISEL_PREDICATES_INIT\n" | |||
4589 | << "AvailableModuleFeatures(computeAvailableModuleFeatures(&STI)),\n" | |||
4590 | << "AvailableFunctionFeatures()\n" | |||
4591 | << "#endif // ifdef GET_GLOBALISEL_PREDICATES_INIT\n"; | |||
4592 | } | |||
4593 | ||||
4594 | void GlobalISelEmitter::declareSubtargetFeature(Record *Predicate) { | |||
4595 | if (SubtargetFeatures.count(Predicate) == 0) | |||
4596 | SubtargetFeatures.emplace( | |||
4597 | Predicate, SubtargetFeatureInfo(Predicate, SubtargetFeatures.size())); | |||
4598 | } | |||
4599 | ||||
4600 | void RuleMatcher::optimize() { | |||
4601 | for (auto &Item : InsnVariableIDs) { | |||
4602 | InstructionMatcher &InsnMatcher = *Item.first; | |||
4603 | for (auto &OM : InsnMatcher.operands()) { | |||
4604 | // Complex Patterns are usually expensive and they relatively rarely fail | |||
4605 | // on their own: more often we end up throwing away all the work done by a | |||
4606 | // matching part of a complex pattern because some other part of the | |||
4607 | // enclosing pattern didn't match. All of this makes it beneficial to | |||
4608 | // delay complex patterns until the very end of the rule matching, | |||
4609 | // especially for targets having lots of complex patterns. | |||
4610 | for (auto &OP : OM->predicates()) | |||
4611 | if (isa<ComplexPatternOperandMatcher>(OP)) | |||
4612 | EpilogueMatchers.emplace_back(std::move(OP)); | |||
4613 | OM->eraseNullPredicates(); | |||
4614 | } | |||
4615 | InsnMatcher.optimize(); | |||
4616 | } | |||
4617 | llvm::sort(EpilogueMatchers, [](const std::unique_ptr<PredicateMatcher> &L, | |||
4618 | const std::unique_ptr<PredicateMatcher> &R) { | |||
4619 | return std::make_tuple(L->getKind(), L->getInsnVarID(), L->getOpIdx()) < | |||
4620 | std::make_tuple(R->getKind(), R->getInsnVarID(), R->getOpIdx()); | |||
4621 | }); | |||
4622 | } | |||
4623 | ||||
4624 | bool RuleMatcher::hasFirstCondition() const { | |||
4625 | if (insnmatchers_empty()) | |||
4626 | return false; | |||
4627 | InstructionMatcher &Matcher = insnmatchers_front(); | |||
4628 | if (!Matcher.predicates_empty()) | |||
4629 | return true; | |||
4630 | for (auto &OM : Matcher.operands()) | |||
4631 | for (auto &OP : OM->predicates()) | |||
4632 | if (!isa<InstructionOperandMatcher>(OP)) | |||
4633 | return true; | |||
4634 | return false; | |||
4635 | } | |||
4636 | ||||
4637 | const PredicateMatcher &RuleMatcher::getFirstCondition() const { | |||
4638 | assert(!insnmatchers_empty() &&((!insnmatchers_empty() && "Trying to get a condition from an empty RuleMatcher" ) ? static_cast<void> (0) : __assert_fail ("!insnmatchers_empty() && \"Trying to get a condition from an empty RuleMatcher\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4639, __PRETTY_FUNCTION__)) | |||
4639 | "Trying to get a condition from an empty RuleMatcher")((!insnmatchers_empty() && "Trying to get a condition from an empty RuleMatcher" ) ? static_cast<void> (0) : __assert_fail ("!insnmatchers_empty() && \"Trying to get a condition from an empty RuleMatcher\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4639, __PRETTY_FUNCTION__)); | |||
4640 | ||||
4641 | InstructionMatcher &Matcher = insnmatchers_front(); | |||
4642 | if (!Matcher.predicates_empty()) | |||
4643 | return **Matcher.predicates_begin(); | |||
4644 | // If there is no more predicate on the instruction itself, look at its | |||
4645 | // operands. | |||
4646 | for (auto &OM : Matcher.operands()) | |||
4647 | for (auto &OP : OM->predicates()) | |||
4648 | if (!isa<InstructionOperandMatcher>(OP)) | |||
4649 | return *OP; | |||
4650 | ||||
4651 | llvm_unreachable("Trying to get a condition from an InstructionMatcher with "::llvm::llvm_unreachable_internal("Trying to get a condition from an InstructionMatcher with " "no conditions", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4652) | |||
4652 | "no conditions")::llvm::llvm_unreachable_internal("Trying to get a condition from an InstructionMatcher with " "no conditions", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4652); | |||
4653 | } | |||
4654 | ||||
4655 | std::unique_ptr<PredicateMatcher> RuleMatcher::popFirstCondition() { | |||
4656 | assert(!insnmatchers_empty() &&((!insnmatchers_empty() && "Trying to pop a condition from an empty RuleMatcher" ) ? static_cast<void> (0) : __assert_fail ("!insnmatchers_empty() && \"Trying to pop a condition from an empty RuleMatcher\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4657, __PRETTY_FUNCTION__)) | |||
4657 | "Trying to pop a condition from an empty RuleMatcher")((!insnmatchers_empty() && "Trying to pop a condition from an empty RuleMatcher" ) ? static_cast<void> (0) : __assert_fail ("!insnmatchers_empty() && \"Trying to pop a condition from an empty RuleMatcher\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4657, __PRETTY_FUNCTION__)); | |||
4658 | ||||
4659 | InstructionMatcher &Matcher = insnmatchers_front(); | |||
4660 | if (!Matcher.predicates_empty()) | |||
4661 | return Matcher.predicates_pop_front(); | |||
4662 | // If there is no more predicate on the instruction itself, look at its | |||
4663 | // operands. | |||
4664 | for (auto &OM : Matcher.operands()) | |||
4665 | for (auto &OP : OM->predicates()) | |||
4666 | if (!isa<InstructionOperandMatcher>(OP)) { | |||
4667 | std::unique_ptr<PredicateMatcher> Result = std::move(OP); | |||
4668 | OM->eraseNullPredicates(); | |||
4669 | return Result; | |||
4670 | } | |||
4671 | ||||
4672 | llvm_unreachable("Trying to pop a condition from an InstructionMatcher with "::llvm::llvm_unreachable_internal("Trying to pop a condition from an InstructionMatcher with " "no conditions", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4673) | |||
4673 | "no conditions")::llvm::llvm_unreachable_internal("Trying to pop a condition from an InstructionMatcher with " "no conditions", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4673); | |||
4674 | } | |||
4675 | ||||
4676 | bool GroupMatcher::candidateConditionMatches( | |||
4677 | const PredicateMatcher &Predicate) const { | |||
4678 | ||||
4679 | if (empty()) { | |||
4680 | // Sharing predicates for nested instructions is not supported yet as we | |||
4681 | // currently don't hoist the GIM_RecordInsn's properly, therefore we can | |||
4682 | // only work on the original root instruction (InsnVarID == 0): | |||
4683 | if (Predicate.getInsnVarID() != 0) | |||
4684 | return false; | |||
4685 | // ... otherwise an empty group can handle any predicate with no specific | |||
4686 | // requirements: | |||
4687 | return true; | |||
4688 | } | |||
4689 | ||||
4690 | const Matcher &Representative = **Matchers.begin(); | |||
4691 | const auto &RepresentativeCondition = Representative.getFirstCondition(); | |||
4692 | // ... if not empty, the group can only accomodate matchers with the exact | |||
4693 | // same first condition: | |||
4694 | return Predicate.isIdentical(RepresentativeCondition); | |||
4695 | } | |||
4696 | ||||
4697 | bool GroupMatcher::addMatcher(Matcher &Candidate) { | |||
4698 | if (!Candidate.hasFirstCondition()) | |||
4699 | return false; | |||
4700 | ||||
4701 | const PredicateMatcher &Predicate = Candidate.getFirstCondition(); | |||
4702 | if (!candidateConditionMatches(Predicate)) | |||
4703 | return false; | |||
4704 | ||||
4705 | Matchers.push_back(&Candidate); | |||
4706 | return true; | |||
4707 | } | |||
4708 | ||||
4709 | void GroupMatcher::finalize() { | |||
4710 | assert(Conditions.empty() && "Already finalized?")((Conditions.empty() && "Already finalized?") ? static_cast <void> (0) : __assert_fail ("Conditions.empty() && \"Already finalized?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4710, __PRETTY_FUNCTION__)); | |||
4711 | if (empty()) | |||
4712 | return; | |||
4713 | ||||
4714 | Matcher &FirstRule = **Matchers.begin(); | |||
4715 | for (;;) { | |||
4716 | // All the checks are expected to succeed during the first iteration: | |||
4717 | for (const auto &Rule : Matchers) | |||
4718 | if (!Rule->hasFirstCondition()) | |||
4719 | return; | |||
4720 | const auto &FirstCondition = FirstRule.getFirstCondition(); | |||
4721 | for (unsigned I = 1, E = Matchers.size(); I < E; ++I) | |||
4722 | if (!Matchers[I]->getFirstCondition().isIdentical(FirstCondition)) | |||
4723 | return; | |||
4724 | ||||
4725 | Conditions.push_back(FirstRule.popFirstCondition()); | |||
4726 | for (unsigned I = 1, E = Matchers.size(); I < E; ++I) | |||
4727 | Matchers[I]->popFirstCondition(); | |||
4728 | } | |||
4729 | } | |||
4730 | ||||
4731 | void GroupMatcher::emit(MatchTable &Table) { | |||
4732 | unsigned LabelID = ~0U; | |||
4733 | if (!Conditions.empty()) { | |||
4734 | LabelID = Table.allocateLabelID(); | |||
4735 | Table << MatchTable::Opcode("GIM_Try", +1) | |||
4736 | << MatchTable::Comment("On fail goto") | |||
4737 | << MatchTable::JumpTarget(LabelID) << MatchTable::LineBreak; | |||
4738 | } | |||
4739 | for (auto &Condition : Conditions) | |||
4740 | Condition->emitPredicateOpcodes( | |||
4741 | Table, *static_cast<RuleMatcher *>(*Matchers.begin())); | |||
4742 | ||||
4743 | for (const auto &M : Matchers) | |||
4744 | M->emit(Table); | |||
4745 | ||||
4746 | // Exit the group | |||
4747 | if (!Conditions.empty()) | |||
4748 | Table << MatchTable::Opcode("GIM_Reject", -1) << MatchTable::LineBreak | |||
4749 | << MatchTable::Label(LabelID); | |||
4750 | } | |||
4751 | ||||
4752 | bool SwitchMatcher::isSupportedPredicateType(const PredicateMatcher &P) { | |||
4753 | return isa<InstructionOpcodeMatcher>(P) || isa<LLTOperandMatcher>(P); | |||
4754 | } | |||
4755 | ||||
4756 | bool SwitchMatcher::candidateConditionMatches( | |||
4757 | const PredicateMatcher &Predicate) const { | |||
4758 | ||||
4759 | if (empty()) { | |||
4760 | // Sharing predicates for nested instructions is not supported yet as we | |||
4761 | // currently don't hoist the GIM_RecordInsn's properly, therefore we can | |||
4762 | // only work on the original root instruction (InsnVarID == 0): | |||
4763 | if (Predicate.getInsnVarID() != 0) | |||
4764 | return false; | |||
4765 | // ... while an attempt to add even a root matcher to an empty SwitchMatcher | |||
4766 | // could fail as not all the types of conditions are supported: | |||
4767 | if (!isSupportedPredicateType(Predicate)) | |||
4768 | return false; | |||
4769 | // ... or the condition might not have a proper implementation of | |||
4770 | // getValue() / isIdenticalDownToValue() yet: | |||
4771 | if (!Predicate.hasValue()) | |||
4772 | return false; | |||
4773 | // ... otherwise an empty Switch can accomodate the condition with no | |||
4774 | // further requirements: | |||
4775 | return true; | |||
4776 | } | |||
4777 | ||||
4778 | const Matcher &CaseRepresentative = **Matchers.begin(); | |||
4779 | const auto &RepresentativeCondition = CaseRepresentative.getFirstCondition(); | |||
4780 | // Switch-cases must share the same kind of condition and path to the value it | |||
4781 | // checks: | |||
4782 | if (!Predicate.isIdenticalDownToValue(RepresentativeCondition)) | |||
4783 | return false; | |||
4784 | ||||
4785 | const auto Value = Predicate.getValue(); | |||
4786 | // ... but be unique with respect to the actual value they check: | |||
4787 | return Values.count(Value) == 0; | |||
4788 | } | |||
4789 | ||||
4790 | bool SwitchMatcher::addMatcher(Matcher &Candidate) { | |||
4791 | if (!Candidate.hasFirstCondition()) | |||
4792 | return false; | |||
4793 | ||||
4794 | const PredicateMatcher &Predicate = Candidate.getFirstCondition(); | |||
4795 | if (!candidateConditionMatches(Predicate)) | |||
4796 | return false; | |||
4797 | const auto Value = Predicate.getValue(); | |||
4798 | Values.insert(Value); | |||
4799 | ||||
4800 | Matchers.push_back(&Candidate); | |||
4801 | return true; | |||
4802 | } | |||
4803 | ||||
4804 | void SwitchMatcher::finalize() { | |||
4805 | assert(Condition == nullptr && "Already finalized")((Condition == nullptr && "Already finalized") ? static_cast <void> (0) : __assert_fail ("Condition == nullptr && \"Already finalized\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4805, __PRETTY_FUNCTION__)); | |||
4806 | assert(Values.size() == Matchers.size() && "Broken SwitchMatcher")((Values.size() == Matchers.size() && "Broken SwitchMatcher" ) ? static_cast<void> (0) : __assert_fail ("Values.size() == Matchers.size() && \"Broken SwitchMatcher\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4806, __PRETTY_FUNCTION__)); | |||
4807 | if (empty()) | |||
4808 | return; | |||
4809 | ||||
4810 | std::stable_sort(Matchers.begin(), Matchers.end(), | |||
4811 | [](const Matcher *L, const Matcher *R) { | |||
4812 | return L->getFirstCondition().getValue() < | |||
4813 | R->getFirstCondition().getValue(); | |||
4814 | }); | |||
4815 | Condition = Matchers[0]->popFirstCondition(); | |||
4816 | for (unsigned I = 1, E = Values.size(); I < E; ++I) | |||
4817 | Matchers[I]->popFirstCondition(); | |||
4818 | } | |||
4819 | ||||
4820 | void SwitchMatcher::emitPredicateSpecificOpcodes(const PredicateMatcher &P, | |||
4821 | MatchTable &Table) { | |||
4822 | assert(isSupportedPredicateType(P) && "Predicate type is not supported")((isSupportedPredicateType(P) && "Predicate type is not supported" ) ? static_cast<void> (0) : __assert_fail ("isSupportedPredicateType(P) && \"Predicate type is not supported\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4822, __PRETTY_FUNCTION__)); | |||
4823 | ||||
4824 | if (const auto *Condition = dyn_cast<InstructionOpcodeMatcher>(&P)) { | |||
4825 | Table << MatchTable::Opcode("GIM_SwitchOpcode") << MatchTable::Comment("MI") | |||
4826 | << MatchTable::IntValue(Condition->getInsnVarID()); | |||
4827 | return; | |||
4828 | } | |||
4829 | if (const auto *Condition = dyn_cast<LLTOperandMatcher>(&P)) { | |||
4830 | Table << MatchTable::Opcode("GIM_SwitchType") << MatchTable::Comment("MI") | |||
4831 | << MatchTable::IntValue(Condition->getInsnVarID()) | |||
4832 | << MatchTable::Comment("Op") | |||
4833 | << MatchTable::IntValue(Condition->getOpIdx()); | |||
4834 | return; | |||
4835 | } | |||
4836 | ||||
4837 | llvm_unreachable("emitPredicateSpecificOpcodes is broken: can not handle a "::llvm::llvm_unreachable_internal("emitPredicateSpecificOpcodes is broken: can not handle a " "predicate type that is claimed to be supported", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4838) | |||
4838 | "predicate type that is claimed to be supported")::llvm::llvm_unreachable_internal("emitPredicateSpecificOpcodes is broken: can not handle a " "predicate type that is claimed to be supported", "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4838); | |||
4839 | } | |||
4840 | ||||
4841 | void SwitchMatcher::emit(MatchTable &Table) { | |||
4842 | assert(Values.size() == Matchers.size() && "Broken SwitchMatcher")((Values.size() == Matchers.size() && "Broken SwitchMatcher" ) ? static_cast<void> (0) : __assert_fail ("Values.size() == Matchers.size() && \"Broken SwitchMatcher\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4842, __PRETTY_FUNCTION__)); | |||
4843 | if (empty()) | |||
4844 | return; | |||
4845 | assert(Condition != nullptr &&((Condition != nullptr && "Broken SwitchMatcher, hasn't been finalized?" ) ? static_cast<void> (0) : __assert_fail ("Condition != nullptr && \"Broken SwitchMatcher, hasn't been finalized?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4846, __PRETTY_FUNCTION__)) | |||
4846 | "Broken SwitchMatcher, hasn't been finalized?")((Condition != nullptr && "Broken SwitchMatcher, hasn't been finalized?" ) ? static_cast<void> (0) : __assert_fail ("Condition != nullptr && \"Broken SwitchMatcher, hasn't been finalized?\"" , "/build/llvm-toolchain-snapshot-9~svn362543/utils/TableGen/GlobalISelEmitter.cpp" , 4846, __PRETTY_FUNCTION__)); | |||
4847 | ||||
4848 | std::vector<unsigned> LabelIDs(Values.size()); | |||
4849 | std::generate(LabelIDs.begin(), LabelIDs.end(), | |||
4850 | [&Table]() { return Table.allocateLabelID(); }); | |||
4851 | const unsigned Default = Table.allocateLabelID(); | |||
4852 | ||||
4853 | const int64_t LowerBound = Values.begin()->getRawValue(); | |||
4854 | const int64_t UpperBound = Values.rbegin()->getRawValue() + 1; | |||
4855 | ||||
4856 | emitPredicateSpecificOpcodes(*Condition, Table); | |||
4857 | ||||
4858 | Table << MatchTable::Comment("[") << MatchTable::IntValue(LowerBound) | |||
4859 | << MatchTable::IntValue(UpperBound) << MatchTable::Comment(")") | |||
4860 | << MatchTable::Comment("default:") << MatchTable::JumpTarget(Default); | |||
4861 | ||||
4862 | int64_t J = LowerBound; | |||
4863 | auto VI = Values.begin(); | |||
4864 | for (unsigned I = 0, E = Values.size(); I < E; ++I) { | |||
4865 | auto V = *VI++; | |||
4866 | while (J++ < V.getRawValue()) | |||
4867 | Table << MatchTable::IntValue(0); | |||
4868 | V.turnIntoComment(); | |||
4869 | Table << MatchTable::LineBreak << V << MatchTable::JumpTarget(LabelIDs[I]); | |||
4870 | } | |||
4871 | Table << MatchTable::LineBreak; | |||
4872 | ||||
4873 | for (unsigned I = 0, E = Values.size(); I < E; ++I) { | |||
4874 | Table << MatchTable::Label(LabelIDs[I]); | |||
4875 | Matchers[I]->emit(Table); | |||
4876 | Table << MatchTable::Opcode("GIM_Reject") << MatchTable::LineBreak; | |||
4877 | } | |||
4878 | Table << MatchTable::Label(Default); | |||
4879 | } | |||
4880 | ||||
4881 | unsigned OperandMatcher::getInsnVarID() const { return Insn.getInsnVarID(); } | |||
4882 | ||||
4883 | } // end anonymous namespace | |||
4884 | ||||
4885 | //===----------------------------------------------------------------------===// | |||
4886 | ||||
4887 | namespace llvm { | |||
4888 | void EmitGlobalISel(RecordKeeper &RK, raw_ostream &OS) { | |||
4889 | GlobalISelEmitter(RK).run(OS); | |||
4890 | } | |||
4891 | } // End llvm namespace |
1 | //===- llvm/Support/Error.h - Recoverable error handling --------*- C++ -*-===// | |||
2 | // | |||
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | |||
4 | // See https://llvm.org/LICENSE.txt for license information. | |||
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | |||
6 | // | |||
7 | //===----------------------------------------------------------------------===// | |||
8 | // | |||
9 | // This file defines an API used to report recoverable errors. | |||
10 | // | |||
11 | //===----------------------------------------------------------------------===// | |||
12 | ||||
13 | #ifndef LLVM_SUPPORT_ERROR_H | |||
14 | #define LLVM_SUPPORT_ERROR_H | |||
15 | ||||
16 | #include "llvm-c/Error.h" | |||
17 | #include "llvm/ADT/STLExtras.h" | |||
18 | #include "llvm/ADT/SmallVector.h" | |||
19 | #include "llvm/ADT/StringExtras.h" | |||
20 | #include "llvm/ADT/Twine.h" | |||
21 | #include "llvm/Config/abi-breaking.h" | |||
22 | #include "llvm/Support/AlignOf.h" | |||
23 | #include "llvm/Support/Compiler.h" | |||
24 | #include "llvm/Support/Debug.h" | |||
25 | #include "llvm/Support/ErrorHandling.h" | |||
26 | #include "llvm/Support/ErrorOr.h" | |||
27 | #include "llvm/Support/Format.h" | |||
28 | #include "llvm/Support/raw_ostream.h" | |||
29 | #include <algorithm> | |||
30 | #include <cassert> | |||
31 | #include <cstdint> | |||
32 | #include <cstdlib> | |||
33 | #include <functional> | |||
34 | #include <memory> | |||
35 | #include <new> | |||
36 | #include <string> | |||
37 | #include <system_error> | |||
38 | #include <type_traits> | |||
39 | #include <utility> | |||
40 | #include <vector> | |||
41 | ||||
42 | namespace llvm { | |||
43 | ||||
44 | class ErrorSuccess; | |||
45 | ||||
46 | /// Base class for error info classes. Do not extend this directly: Extend | |||
47 | /// the ErrorInfo template subclass instead. | |||
48 | class ErrorInfoBase { | |||
49 | public: | |||
50 | virtual ~ErrorInfoBase() = default; | |||
51 | ||||
52 | /// Print an error message to an output stream. | |||
53 | virtual void log(raw_ostream &OS) const = 0; | |||
54 | ||||
55 | /// Return the error message as a string. | |||
56 | virtual std::string message() const { | |||
57 | std::string Msg; | |||
58 | raw_string_ostream OS(Msg); | |||
59 | log(OS); | |||
60 | return OS.str(); | |||
61 | } | |||
62 | ||||
63 | /// Convert this error to a std::error_code. | |||
64 | /// | |||
65 | /// This is a temporary crutch to enable interaction with code still | |||
66 | /// using std::error_code. It will be removed in the future. | |||
67 | virtual std::error_code convertToErrorCode() const = 0; | |||
68 | ||||
69 | // Returns the class ID for this type. | |||
70 | static const void *classID() { return &ID; } | |||
71 | ||||
72 | // Returns the class ID for the dynamic type of this ErrorInfoBase instance. | |||
73 | virtual const void *dynamicClassID() const = 0; | |||
74 | ||||
75 | // Check whether this instance is a subclass of the class identified by | |||
76 | // ClassID. | |||
77 | virtual bool isA(const void *const ClassID) const { | |||
78 | return ClassID == classID(); | |||
79 | } | |||
80 | ||||
81 | // Check whether this instance is a subclass of ErrorInfoT. | |||
82 | template <typename ErrorInfoT> bool isA() const { | |||
83 | return isA(ErrorInfoT::classID()); | |||
84 | } | |||
85 | ||||
86 | private: | |||
87 | virtual void anchor(); | |||
88 | ||||
89 | static char ID; | |||
90 | }; | |||
91 | ||||
92 | /// Lightweight error class with error context and mandatory checking. | |||
93 | /// | |||
94 | /// Instances of this class wrap a ErrorInfoBase pointer. Failure states | |||
95 | /// are represented by setting the pointer to a ErrorInfoBase subclass | |||
96 | /// instance containing information describing the failure. Success is | |||
97 | /// represented by a null pointer value. | |||
98 | /// | |||
99 | /// Instances of Error also contains a 'Checked' flag, which must be set | |||
100 | /// before the destructor is called, otherwise the destructor will trigger a | |||
101 | /// runtime error. This enforces at runtime the requirement that all Error | |||
102 | /// instances be checked or returned to the caller. | |||
103 | /// | |||
104 | /// There are two ways to set the checked flag, depending on what state the | |||
105 | /// Error instance is in. For Error instances indicating success, it | |||
106 | /// is sufficient to invoke the boolean conversion operator. E.g.: | |||
107 | /// | |||
108 | /// @code{.cpp} | |||
109 | /// Error foo(<...>); | |||
110 | /// | |||
111 | /// if (auto E = foo(<...>)) | |||
112 | /// return E; // <- Return E if it is in the error state. | |||
113 | /// // We have verified that E was in the success state. It can now be safely | |||
114 | /// // destroyed. | |||
115 | /// @endcode | |||
116 | /// | |||
117 | /// A success value *can not* be dropped. For example, just calling 'foo(<...>)' | |||
118 | /// without testing the return value will raise a runtime error, even if foo | |||
119 | /// returns success. | |||
120 | /// | |||
121 | /// For Error instances representing failure, you must use either the | |||
122 | /// handleErrors or handleAllErrors function with a typed handler. E.g.: | |||
123 | /// | |||
124 | /// @code{.cpp} | |||
125 | /// class MyErrorInfo : public ErrorInfo<MyErrorInfo> { | |||
126 | /// // Custom error info. | |||
127 | /// }; | |||
128 | /// | |||
129 | /// Error foo(<...>) { return make_error<MyErrorInfo>(...); } | |||
130 | /// | |||
131 | /// auto E = foo(<...>); // <- foo returns failure with MyErrorInfo. | |||
132 | /// auto NewE = | |||
133 | /// handleErrors(E, | |||
134 | /// [](const MyErrorInfo &M) { | |||
135 | /// // Deal with the error. | |||
136 | /// }, | |||
137 | /// [](std::unique_ptr<OtherError> M) -> Error { | |||
138 | /// if (canHandle(*M)) { | |||
139 | /// // handle error. | |||
140 | /// return Error::success(); | |||
141 | /// } | |||
142 | /// // Couldn't handle this error instance. Pass it up the stack. | |||
143 | /// return Error(std::move(M)); | |||
144 | /// ); | |||
145 | /// // Note - we must check or return NewE in case any of the handlers | |||
146 | /// // returned a new error. | |||
147 | /// @endcode | |||
148 | /// | |||
149 | /// The handleAllErrors function is identical to handleErrors, except | |||
150 | /// that it has a void return type, and requires all errors to be handled and | |||
151 | /// no new errors be returned. It prevents errors (assuming they can all be | |||
152 | /// handled) from having to be bubbled all the way to the top-level. | |||
153 | /// | |||
154 | /// *All* Error instances must be checked before destruction, even if | |||
155 | /// they're moved-assigned or constructed from Success values that have already | |||
156 | /// been checked. This enforces checking through all levels of the call stack. | |||
157 | class LLVM_NODISCARD[[clang::warn_unused_result]] Error { | |||
158 | // Both ErrorList and FileError need to be able to yank ErrorInfoBase | |||
159 | // pointers out of this class to add to the error list. | |||
160 | friend class ErrorList; | |||
161 | friend class FileError; | |||
162 | ||||
163 | // handleErrors needs to be able to set the Checked flag. | |||
164 | template <typename... HandlerTs> | |||
165 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); | |||
166 | ||||
167 | // Expected<T> needs to be able to steal the payload when constructed from an | |||
168 | // error. | |||
169 | template <typename T> friend class Expected; | |||
170 | ||||
171 | // wrap needs to be able to steal the payload. | |||
172 | friend LLVMErrorRef wrap(Error); | |||
173 | ||||
174 | protected: | |||
175 | /// Create a success value. Prefer using 'Error::success()' for readability | |||
176 | Error() { | |||
177 | setPtr(nullptr); | |||
178 | setChecked(false); | |||
179 | } | |||
180 | ||||
181 | public: | |||
182 | /// Create a success value. | |||
183 | static ErrorSuccess success(); | |||
184 | ||||
185 | // Errors are not copy-constructable. | |||
186 | Error(const Error &Other) = delete; | |||
187 | ||||
188 | /// Move-construct an error value. The newly constructed error is considered | |||
189 | /// unchecked, even if the source error had been checked. The original error | |||
190 | /// becomes a checked Success value, regardless of its original state. | |||
191 | Error(Error &&Other) { | |||
192 | setChecked(true); | |||
193 | *this = std::move(Other); | |||
194 | } | |||
195 | ||||
196 | /// Create an error value. Prefer using the 'make_error' function, but | |||
197 | /// this constructor can be useful when "re-throwing" errors from handlers. | |||
198 | Error(std::unique_ptr<ErrorInfoBase> Payload) { | |||
199 | setPtr(Payload.release()); | |||
200 | setChecked(false); | |||
| ||||
201 | } | |||
202 | ||||
203 | // Errors are not copy-assignable. | |||
204 | Error &operator=(const Error &Other) = delete; | |||
205 | ||||
206 | /// Move-assign an error value. The current error must represent success, you | |||
207 | /// you cannot overwrite an unhandled error. The current error is then | |||
208 | /// considered unchecked. The source error becomes a checked success value, | |||
209 | /// regardless of its original state. | |||
210 | Error &operator=(Error &&Other) { | |||
211 | // Don't allow overwriting of unchecked values. | |||
212 | assertIsChecked(); | |||
213 | setPtr(Other.getPtr()); | |||
214 | ||||
215 | // This Error is unchecked, even if the source error was checked. | |||
216 | setChecked(false); | |||
217 | ||||
218 | // Null out Other's payload and set its checked bit. | |||
219 | Other.setPtr(nullptr); | |||
220 | Other.setChecked(true); | |||
221 | ||||
222 | return *this; | |||
223 | } | |||
224 | ||||
225 | /// Destroy a Error. Fails with a call to abort() if the error is | |||
226 | /// unchecked. | |||
227 | ~Error() { | |||
228 | assertIsChecked(); | |||
229 | delete getPtr(); | |||
230 | } | |||
231 | ||||
232 | /// Bool conversion. Returns true if this Error is in a failure state, | |||
233 | /// and false if it is in an accept state. If the error is in a Success state | |||
234 | /// it will be considered checked. | |||
235 | explicit operator bool() { | |||
236 | setChecked(getPtr() == nullptr); | |||
237 | return getPtr() != nullptr; | |||
238 | } | |||
239 | ||||
240 | /// Check whether one error is a subclass of another. | |||
241 | template <typename ErrT> bool isA() const { | |||
242 | return getPtr() && getPtr()->isA(ErrT::classID()); | |||
243 | } | |||
244 | ||||
245 | /// Returns the dynamic class id of this error, or null if this is a success | |||
246 | /// value. | |||
247 | const void* dynamicClassID() const { | |||
248 | if (!getPtr()) | |||
249 | return nullptr; | |||
250 | return getPtr()->dynamicClassID(); | |||
251 | } | |||
252 | ||||
253 | private: | |||
254 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
255 | // assertIsChecked() happens very frequently, but under normal circumstances | |||
256 | // is supposed to be a no-op. So we want it to be inlined, but having a bunch | |||
257 | // of debug prints can cause the function to be too large for inlining. So | |||
258 | // it's important that we define this function out of line so that it can't be | |||
259 | // inlined. | |||
260 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) | |||
261 | void fatalUncheckedError() const; | |||
262 | #endif | |||
263 | ||||
264 | void assertIsChecked() { | |||
265 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
266 | if (LLVM_UNLIKELY(!getChecked() || getPtr())__builtin_expect((bool)(!getChecked() || getPtr()), false)) | |||
267 | fatalUncheckedError(); | |||
268 | #endif | |||
269 | } | |||
270 | ||||
271 | ErrorInfoBase *getPtr() const { | |||
272 | return reinterpret_cast<ErrorInfoBase*>( | |||
273 | reinterpret_cast<uintptr_t>(Payload) & | |||
274 | ~static_cast<uintptr_t>(0x1)); | |||
275 | } | |||
276 | ||||
277 | void setPtr(ErrorInfoBase *EI) { | |||
278 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
279 | Payload = reinterpret_cast<ErrorInfoBase*>( | |||
280 | (reinterpret_cast<uintptr_t>(EI) & | |||
281 | ~static_cast<uintptr_t>(0x1)) | | |||
282 | (reinterpret_cast<uintptr_t>(Payload) & 0x1)); | |||
283 | #else | |||
284 | Payload = EI; | |||
285 | #endif | |||
286 | } | |||
287 | ||||
288 | bool getChecked() const { | |||
289 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
290 | return (reinterpret_cast<uintptr_t>(Payload) & 0x1) == 0; | |||
291 | #else | |||
292 | return true; | |||
293 | #endif | |||
294 | } | |||
295 | ||||
296 | void setChecked(bool V) { | |||
297 | Payload = reinterpret_cast<ErrorInfoBase*>( | |||
298 | (reinterpret_cast<uintptr_t>(Payload) & | |||
299 | ~static_cast<uintptr_t>(0x1)) | | |||
300 | (V ? 0 : 1)); | |||
301 | } | |||
302 | ||||
303 | std::unique_ptr<ErrorInfoBase> takePayload() { | |||
304 | std::unique_ptr<ErrorInfoBase> Tmp(getPtr()); | |||
305 | setPtr(nullptr); | |||
306 | setChecked(true); | |||
307 | return Tmp; | |||
308 | } | |||
309 | ||||
310 | friend raw_ostream &operator<<(raw_ostream &OS, const Error &E) { | |||
311 | if (auto P = E.getPtr()) | |||
312 | P->log(OS); | |||
313 | else | |||
314 | OS << "success"; | |||
315 | return OS; | |||
316 | } | |||
317 | ||||
318 | ErrorInfoBase *Payload = nullptr; | |||
319 | }; | |||
320 | ||||
321 | /// Subclass of Error for the sole purpose of identifying the success path in | |||
322 | /// the type system. This allows to catch invalid conversion to Expected<T> at | |||
323 | /// compile time. | |||
324 | class ErrorSuccess final : public Error {}; | |||
325 | ||||
326 | inline ErrorSuccess Error::success() { return ErrorSuccess(); } | |||
327 | ||||
328 | /// Make a Error instance representing failure using the given error info | |||
329 | /// type. | |||
330 | template <typename ErrT, typename... ArgTs> Error make_error(ArgTs &&... Args) { | |||
331 | return Error(llvm::make_unique<ErrT>(std::forward<ArgTs>(Args)...)); | |||
332 | } | |||
333 | ||||
334 | /// Base class for user error types. Users should declare their error types | |||
335 | /// like: | |||
336 | /// | |||
337 | /// class MyError : public ErrorInfo<MyError> { | |||
338 | /// .... | |||
339 | /// }; | |||
340 | /// | |||
341 | /// This class provides an implementation of the ErrorInfoBase::kind | |||
342 | /// method, which is used by the Error RTTI system. | |||
343 | template <typename ThisErrT, typename ParentErrT = ErrorInfoBase> | |||
344 | class ErrorInfo : public ParentErrT { | |||
345 | public: | |||
346 | using ParentErrT::ParentErrT; // inherit constructors | |||
347 | ||||
348 | static const void *classID() { return &ThisErrT::ID; } | |||
349 | ||||
350 | const void *dynamicClassID() const override { return &ThisErrT::ID; } | |||
351 | ||||
352 | bool isA(const void *const ClassID) const override { | |||
353 | return ClassID == classID() || ParentErrT::isA(ClassID); | |||
354 | } | |||
355 | }; | |||
356 | ||||
357 | /// Special ErrorInfo subclass representing a list of ErrorInfos. | |||
358 | /// Instances of this class are constructed by joinError. | |||
359 | class ErrorList final : public ErrorInfo<ErrorList> { | |||
360 | // handleErrors needs to be able to iterate the payload list of an | |||
361 | // ErrorList. | |||
362 | template <typename... HandlerTs> | |||
363 | friend Error handleErrors(Error E, HandlerTs &&... Handlers); | |||
364 | ||||
365 | // joinErrors is implemented in terms of join. | |||
366 | friend Error joinErrors(Error, Error); | |||
367 | ||||
368 | public: | |||
369 | void log(raw_ostream &OS) const override { | |||
370 | OS << "Multiple errors:\n"; | |||
371 | for (auto &ErrPayload : Payloads) { | |||
372 | ErrPayload->log(OS); | |||
373 | OS << "\n"; | |||
374 | } | |||
375 | } | |||
376 | ||||
377 | std::error_code convertToErrorCode() const override; | |||
378 | ||||
379 | // Used by ErrorInfo::classID. | |||
380 | static char ID; | |||
381 | ||||
382 | private: | |||
383 | ErrorList(std::unique_ptr<ErrorInfoBase> Payload1, | |||
384 | std::unique_ptr<ErrorInfoBase> Payload2) { | |||
385 | assert(!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() &&((!Payload1->isA<ErrorList>() && !Payload2-> isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors" ) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 386, __PRETTY_FUNCTION__)) | |||
386 | "ErrorList constructor payloads should be singleton errors")((!Payload1->isA<ErrorList>() && !Payload2-> isA<ErrorList>() && "ErrorList constructor payloads should be singleton errors" ) ? static_cast<void> (0) : __assert_fail ("!Payload1->isA<ErrorList>() && !Payload2->isA<ErrorList>() && \"ErrorList constructor payloads should be singleton errors\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 386, __PRETTY_FUNCTION__)); | |||
387 | Payloads.push_back(std::move(Payload1)); | |||
388 | Payloads.push_back(std::move(Payload2)); | |||
389 | } | |||
390 | ||||
391 | static Error join(Error E1, Error E2) { | |||
392 | if (!E1) | |||
393 | return E2; | |||
394 | if (!E2) | |||
395 | return E1; | |||
396 | if (E1.isA<ErrorList>()) { | |||
397 | auto &E1List = static_cast<ErrorList &>(*E1.getPtr()); | |||
398 | if (E2.isA<ErrorList>()) { | |||
399 | auto E2Payload = E2.takePayload(); | |||
400 | auto &E2List = static_cast<ErrorList &>(*E2Payload); | |||
401 | for (auto &Payload : E2List.Payloads) | |||
402 | E1List.Payloads.push_back(std::move(Payload)); | |||
403 | } else | |||
404 | E1List.Payloads.push_back(E2.takePayload()); | |||
405 | ||||
406 | return E1; | |||
407 | } | |||
408 | if (E2.isA<ErrorList>()) { | |||
409 | auto &E2List = static_cast<ErrorList &>(*E2.getPtr()); | |||
410 | E2List.Payloads.insert(E2List.Payloads.begin(), E1.takePayload()); | |||
411 | return E2; | |||
412 | } | |||
413 | return Error(std::unique_ptr<ErrorList>( | |||
414 | new ErrorList(E1.takePayload(), E2.takePayload()))); | |||
415 | } | |||
416 | ||||
417 | std::vector<std::unique_ptr<ErrorInfoBase>> Payloads; | |||
418 | }; | |||
419 | ||||
420 | /// Concatenate errors. The resulting Error is unchecked, and contains the | |||
421 | /// ErrorInfo(s), if any, contained in E1, followed by the | |||
422 | /// ErrorInfo(s), if any, contained in E2. | |||
423 | inline Error joinErrors(Error E1, Error E2) { | |||
424 | return ErrorList::join(std::move(E1), std::move(E2)); | |||
425 | } | |||
426 | ||||
427 | /// Tagged union holding either a T or a Error. | |||
428 | /// | |||
429 | /// This class parallels ErrorOr, but replaces error_code with Error. Since | |||
430 | /// Error cannot be copied, this class replaces getError() with | |||
431 | /// takeError(). It also adds an bool errorIsA<ErrT>() method for testing the | |||
432 | /// error class type. | |||
433 | template <class T> class LLVM_NODISCARD[[clang::warn_unused_result]] Expected { | |||
434 | template <class T1> friend class ExpectedAsOutParameter; | |||
435 | template <class OtherT> friend class Expected; | |||
436 | ||||
437 | static const bool isRef = std::is_reference<T>::value; | |||
438 | ||||
439 | using wrap = std::reference_wrapper<typename std::remove_reference<T>::type>; | |||
440 | ||||
441 | using error_type = std::unique_ptr<ErrorInfoBase>; | |||
442 | ||||
443 | public: | |||
444 | using storage_type = typename std::conditional<isRef, wrap, T>::type; | |||
445 | using value_type = T; | |||
446 | ||||
447 | private: | |||
448 | using reference = typename std::remove_reference<T>::type &; | |||
449 | using const_reference = const typename std::remove_reference<T>::type &; | |||
450 | using pointer = typename std::remove_reference<T>::type *; | |||
451 | using const_pointer = const typename std::remove_reference<T>::type *; | |||
452 | ||||
453 | public: | |||
454 | /// Create an Expected<T> error value from the given Error. | |||
455 | Expected(Error Err) | |||
456 | : HasError(true) | |||
457 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
458 | // Expected is unchecked upon construction in Debug builds. | |||
459 | , Unchecked(true) | |||
460 | #endif | |||
461 | { | |||
462 | assert(Err && "Cannot create Expected<T> from Error success value.")((Err && "Cannot create Expected<T> from Error success value." ) ? static_cast<void> (0) : __assert_fail ("Err && \"Cannot create Expected<T> from Error success value.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 462, __PRETTY_FUNCTION__)); | |||
463 | new (getErrorStorage()) error_type(Err.takePayload()); | |||
464 | } | |||
465 | ||||
466 | /// Forbid to convert from Error::success() implicitly, this avoids having | |||
467 | /// Expected<T> foo() { return Error::success(); } which compiles otherwise | |||
468 | /// but triggers the assertion above. | |||
469 | Expected(ErrorSuccess) = delete; | |||
470 | ||||
471 | /// Create an Expected<T> success value from the given OtherT value, which | |||
472 | /// must be convertible to T. | |||
473 | template <typename OtherT> | |||
474 | Expected(OtherT &&Val, | |||
475 | typename std::enable_if<std::is_convertible<OtherT, T>::value>::type | |||
476 | * = nullptr) | |||
477 | : HasError(false) | |||
478 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
479 | // Expected is unchecked upon construction in Debug builds. | |||
480 | , Unchecked(true) | |||
481 | #endif | |||
482 | { | |||
483 | new (getStorage()) storage_type(std::forward<OtherT>(Val)); | |||
484 | } | |||
485 | ||||
486 | /// Move construct an Expected<T> value. | |||
487 | Expected(Expected &&Other) { moveConstruct(std::move(Other)); } | |||
488 | ||||
489 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT | |||
490 | /// must be convertible to T. | |||
491 | template <class OtherT> | |||
492 | Expected(Expected<OtherT> &&Other, | |||
493 | typename std::enable_if<std::is_convertible<OtherT, T>::value>::type | |||
494 | * = nullptr) { | |||
495 | moveConstruct(std::move(Other)); | |||
496 | } | |||
497 | ||||
498 | /// Move construct an Expected<T> value from an Expected<OtherT>, where OtherT | |||
499 | /// isn't convertible to T. | |||
500 | template <class OtherT> | |||
501 | explicit Expected( | |||
502 | Expected<OtherT> &&Other, | |||
503 | typename std::enable_if<!std::is_convertible<OtherT, T>::value>::type * = | |||
504 | nullptr) { | |||
505 | moveConstruct(std::move(Other)); | |||
506 | } | |||
507 | ||||
508 | /// Move-assign from another Expected<T>. | |||
509 | Expected &operator=(Expected &&Other) { | |||
510 | moveAssign(std::move(Other)); | |||
511 | return *this; | |||
512 | } | |||
513 | ||||
514 | /// Destroy an Expected<T>. | |||
515 | ~Expected() { | |||
516 | assertIsChecked(); | |||
517 | if (!HasError) | |||
518 | getStorage()->~storage_type(); | |||
519 | else | |||
520 | getErrorStorage()->~error_type(); | |||
521 | } | |||
522 | ||||
523 | /// Return false if there is an error. | |||
524 | explicit operator bool() { | |||
525 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
526 | Unchecked = HasError; | |||
527 | #endif | |||
528 | return !HasError; | |||
529 | } | |||
530 | ||||
531 | /// Returns a reference to the stored T value. | |||
532 | reference get() { | |||
533 | assertIsChecked(); | |||
534 | return *getStorage(); | |||
535 | } | |||
536 | ||||
537 | /// Returns a const reference to the stored T value. | |||
538 | const_reference get() const { | |||
539 | assertIsChecked(); | |||
540 | return const_cast<Expected<T> *>(this)->get(); | |||
541 | } | |||
542 | ||||
543 | /// Check that this Expected<T> is an error of type ErrT. | |||
544 | template <typename ErrT> bool errorIsA() const { | |||
545 | return HasError && (*getErrorStorage())->template isA<ErrT>(); | |||
546 | } | |||
547 | ||||
548 | /// Take ownership of the stored error. | |||
549 | /// After calling this the Expected<T> is in an indeterminate state that can | |||
550 | /// only be safely destructed. No further calls (beside the destructor) should | |||
551 | /// be made on the Expected<T> vaule. | |||
552 | Error takeError() { | |||
553 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
554 | Unchecked = false; | |||
555 | #endif | |||
556 | return HasError ? Error(std::move(*getErrorStorage())) : Error::success(); | |||
557 | } | |||
558 | ||||
559 | /// Returns a pointer to the stored T value. | |||
560 | pointer operator->() { | |||
561 | assertIsChecked(); | |||
562 | return toPointer(getStorage()); | |||
563 | } | |||
564 | ||||
565 | /// Returns a const pointer to the stored T value. | |||
566 | const_pointer operator->() const { | |||
567 | assertIsChecked(); | |||
568 | return toPointer(getStorage()); | |||
569 | } | |||
570 | ||||
571 | /// Returns a reference to the stored T value. | |||
572 | reference operator*() { | |||
573 | assertIsChecked(); | |||
574 | return *getStorage(); | |||
575 | } | |||
576 | ||||
577 | /// Returns a const reference to the stored T value. | |||
578 | const_reference operator*() const { | |||
579 | assertIsChecked(); | |||
580 | return *getStorage(); | |||
581 | } | |||
582 | ||||
583 | private: | |||
584 | template <class T1> | |||
585 | static bool compareThisIfSameType(const T1 &a, const T1 &b) { | |||
586 | return &a == &b; | |||
587 | } | |||
588 | ||||
589 | template <class T1, class T2> | |||
590 | static bool compareThisIfSameType(const T1 &a, const T2 &b) { | |||
591 | return false; | |||
592 | } | |||
593 | ||||
594 | template <class OtherT> void moveConstruct(Expected<OtherT> &&Other) { | |||
595 | HasError = Other.HasError; | |||
596 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
597 | Unchecked = true; | |||
598 | Other.Unchecked = false; | |||
599 | #endif | |||
600 | ||||
601 | if (!HasError) | |||
602 | new (getStorage()) storage_type(std::move(*Other.getStorage())); | |||
603 | else | |||
604 | new (getErrorStorage()) error_type(std::move(*Other.getErrorStorage())); | |||
605 | } | |||
606 | ||||
607 | template <class OtherT> void moveAssign(Expected<OtherT> &&Other) { | |||
608 | assertIsChecked(); | |||
609 | ||||
610 | if (compareThisIfSameType(*this, Other)) | |||
611 | return; | |||
612 | ||||
613 | this->~Expected(); | |||
614 | new (this) Expected(std::move(Other)); | |||
615 | } | |||
616 | ||||
617 | pointer toPointer(pointer Val) { return Val; } | |||
618 | ||||
619 | const_pointer toPointer(const_pointer Val) const { return Val; } | |||
620 | ||||
621 | pointer toPointer(wrap *Val) { return &Val->get(); } | |||
622 | ||||
623 | const_pointer toPointer(const wrap *Val) const { return &Val->get(); } | |||
624 | ||||
625 | storage_type *getStorage() { | |||
626 | assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!" ) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 626, __PRETTY_FUNCTION__)); | |||
627 | return reinterpret_cast<storage_type *>(TStorage.buffer); | |||
628 | } | |||
629 | ||||
630 | const storage_type *getStorage() const { | |||
631 | assert(!HasError && "Cannot get value when an error exists!")((!HasError && "Cannot get value when an error exists!" ) ? static_cast<void> (0) : __assert_fail ("!HasError && \"Cannot get value when an error exists!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 631, __PRETTY_FUNCTION__)); | |||
632 | return reinterpret_cast<const storage_type *>(TStorage.buffer); | |||
633 | } | |||
634 | ||||
635 | error_type *getErrorStorage() { | |||
636 | assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!" ) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 636, __PRETTY_FUNCTION__)); | |||
637 | return reinterpret_cast<error_type *>(ErrorStorage.buffer); | |||
638 | } | |||
639 | ||||
640 | const error_type *getErrorStorage() const { | |||
641 | assert(HasError && "Cannot get error when a value exists!")((HasError && "Cannot get error when a value exists!" ) ? static_cast<void> (0) : __assert_fail ("HasError && \"Cannot get error when a value exists!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 641, __PRETTY_FUNCTION__)); | |||
642 | return reinterpret_cast<const error_type *>(ErrorStorage.buffer); | |||
643 | } | |||
644 | ||||
645 | // Used by ExpectedAsOutParameter to reset the checked flag. | |||
646 | void setUnchecked() { | |||
647 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
648 | Unchecked = true; | |||
649 | #endif | |||
650 | } | |||
651 | ||||
652 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
653 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) | |||
654 | LLVM_ATTRIBUTE_NOINLINE__attribute__((noinline)) | |||
655 | void fatalUncheckedExpected() const { | |||
656 | dbgs() << "Expected<T> must be checked before access or destruction.\n"; | |||
657 | if (HasError) { | |||
658 | dbgs() << "Unchecked Expected<T> contained error:\n"; | |||
659 | (*getErrorStorage())->log(dbgs()); | |||
660 | } else | |||
661 | dbgs() << "Expected<T> value was in success state. (Note: Expected<T> " | |||
662 | "values in success mode must still be checked prior to being " | |||
663 | "destroyed).\n"; | |||
664 | abort(); | |||
665 | } | |||
666 | #endif | |||
667 | ||||
668 | void assertIsChecked() { | |||
669 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
670 | if (LLVM_UNLIKELY(Unchecked)__builtin_expect((bool)(Unchecked), false)) | |||
671 | fatalUncheckedExpected(); | |||
672 | #endif | |||
673 | } | |||
674 | ||||
675 | union { | |||
676 | AlignedCharArrayUnion<storage_type> TStorage; | |||
677 | AlignedCharArrayUnion<error_type> ErrorStorage; | |||
678 | }; | |||
679 | bool HasError : 1; | |||
680 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 | |||
681 | bool Unchecked : 1; | |||
682 | #endif | |||
683 | }; | |||
684 | ||||
685 | /// Report a serious error, calling any installed error handler. See | |||
686 | /// ErrorHandling.h. | |||
687 | LLVM_ATTRIBUTE_NORETURN__attribute__((noreturn)) void report_fatal_error(Error Err, | |||
688 | bool gen_crash_diag = true); | |||
689 | ||||
690 | /// Report a fatal error if Err is a failure value. | |||
691 | /// | |||
692 | /// This function can be used to wrap calls to fallible functions ONLY when it | |||
693 | /// is known that the Error will always be a success value. E.g. | |||
694 | /// | |||
695 | /// @code{.cpp} | |||
696 | /// // foo only attempts the fallible operation if DoFallibleOperation is | |||
697 | /// // true. If DoFallibleOperation is false then foo always returns | |||
698 | /// // Error::success(). | |||
699 | /// Error foo(bool DoFallibleOperation); | |||
700 | /// | |||
701 | /// cantFail(foo(false)); | |||
702 | /// @endcode | |||
703 | inline void cantFail(Error Err, const char *Msg = nullptr) { | |||
704 | if (Err) { | |||
705 | if (!Msg) | |||
706 | Msg = "Failure value returned from cantFail wrapped call"; | |||
707 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 707); | |||
708 | } | |||
709 | } | |||
710 | ||||
711 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and | |||
712 | /// returns the contained value. | |||
713 | /// | |||
714 | /// This function can be used to wrap calls to fallible functions ONLY when it | |||
715 | /// is known that the Error will always be a success value. E.g. | |||
716 | /// | |||
717 | /// @code{.cpp} | |||
718 | /// // foo only attempts the fallible operation if DoFallibleOperation is | |||
719 | /// // true. If DoFallibleOperation is false then foo always returns an int. | |||
720 | /// Expected<int> foo(bool DoFallibleOperation); | |||
721 | /// | |||
722 | /// int X = cantFail(foo(false)); | |||
723 | /// @endcode | |||
724 | template <typename T> | |||
725 | T cantFail(Expected<T> ValOrErr, const char *Msg = nullptr) { | |||
726 | if (ValOrErr) | |||
727 | return std::move(*ValOrErr); | |||
728 | else { | |||
729 | if (!Msg) | |||
730 | Msg = "Failure value returned from cantFail wrapped call"; | |||
731 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 731); | |||
732 | } | |||
733 | } | |||
734 | ||||
735 | /// Report a fatal error if ValOrErr is a failure value, otherwise unwraps and | |||
736 | /// returns the contained reference. | |||
737 | /// | |||
738 | /// This function can be used to wrap calls to fallible functions ONLY when it | |||
739 | /// is known that the Error will always be a success value. E.g. | |||
740 | /// | |||
741 | /// @code{.cpp} | |||
742 | /// // foo only attempts the fallible operation if DoFallibleOperation is | |||
743 | /// // true. If DoFallibleOperation is false then foo always returns a Bar&. | |||
744 | /// Expected<Bar&> foo(bool DoFallibleOperation); | |||
745 | /// | |||
746 | /// Bar &X = cantFail(foo(false)); | |||
747 | /// @endcode | |||
748 | template <typename T> | |||
749 | T& cantFail(Expected<T&> ValOrErr, const char *Msg = nullptr) { | |||
750 | if (ValOrErr) | |||
751 | return *ValOrErr; | |||
752 | else { | |||
753 | if (!Msg) | |||
754 | Msg = "Failure value returned from cantFail wrapped call"; | |||
755 | llvm_unreachable(Msg)::llvm::llvm_unreachable_internal(Msg, "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 755); | |||
756 | } | |||
757 | } | |||
758 | ||||
759 | /// Helper for testing applicability of, and applying, handlers for | |||
760 | /// ErrorInfo types. | |||
761 | template <typename HandlerT> | |||
762 | class ErrorHandlerTraits | |||
763 | : public ErrorHandlerTraits<decltype( | |||
764 | &std::remove_reference<HandlerT>::type::operator())> {}; | |||
765 | ||||
766 | // Specialization functions of the form 'Error (const ErrT&)'. | |||
767 | template <typename ErrT> class ErrorHandlerTraits<Error (&)(ErrT &)> { | |||
768 | public: | |||
769 | static bool appliesTo(const ErrorInfoBase &E) { | |||
770 | return E.template isA<ErrT>(); | |||
771 | } | |||
772 | ||||
773 | template <typename HandlerT> | |||
774 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { | |||
775 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 775, __PRETTY_FUNCTION__)); | |||
776 | return H(static_cast<ErrT &>(*E)); | |||
777 | } | |||
778 | }; | |||
779 | ||||
780 | // Specialization functions of the form 'void (const ErrT&)'. | |||
781 | template <typename ErrT> class ErrorHandlerTraits<void (&)(ErrT &)> { | |||
782 | public: | |||
783 | static bool appliesTo(const ErrorInfoBase &E) { | |||
784 | return E.template isA<ErrT>(); | |||
785 | } | |||
786 | ||||
787 | template <typename HandlerT> | |||
788 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { | |||
789 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 789, __PRETTY_FUNCTION__)); | |||
790 | H(static_cast<ErrT &>(*E)); | |||
791 | return Error::success(); | |||
792 | } | |||
793 | }; | |||
794 | ||||
795 | /// Specialization for functions of the form 'Error (std::unique_ptr<ErrT>)'. | |||
796 | template <typename ErrT> | |||
797 | class ErrorHandlerTraits<Error (&)(std::unique_ptr<ErrT>)> { | |||
798 | public: | |||
799 | static bool appliesTo(const ErrorInfoBase &E) { | |||
800 | return E.template isA<ErrT>(); | |||
801 | } | |||
802 | ||||
803 | template <typename HandlerT> | |||
804 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { | |||
805 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 805, __PRETTY_FUNCTION__)); | |||
806 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); | |||
807 | return H(std::move(SubE)); | |||
808 | } | |||
809 | }; | |||
810 | ||||
811 | /// Specialization for functions of the form 'void (std::unique_ptr<ErrT>)'. | |||
812 | template <typename ErrT> | |||
813 | class ErrorHandlerTraits<void (&)(std::unique_ptr<ErrT>)> { | |||
814 | public: | |||
815 | static bool appliesTo(const ErrorInfoBase &E) { | |||
816 | return E.template isA<ErrT>(); | |||
817 | } | |||
818 | ||||
819 | template <typename HandlerT> | |||
820 | static Error apply(HandlerT &&H, std::unique_ptr<ErrorInfoBase> E) { | |||
821 | assert(appliesTo(*E) && "Applying incorrect handler")((appliesTo(*E) && "Applying incorrect handler") ? static_cast <void> (0) : __assert_fail ("appliesTo(*E) && \"Applying incorrect handler\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 821, __PRETTY_FUNCTION__)); | |||
822 | std::unique_ptr<ErrT> SubE(static_cast<ErrT *>(E.release())); | |||
823 | H(std::move(SubE)); | |||
824 | return Error::success(); | |||
825 | } | |||
826 | }; | |||
827 | ||||
828 | // Specialization for member functions of the form 'RetT (const ErrT&)'. | |||
829 | template <typename C, typename RetT, typename ErrT> | |||
830 | class ErrorHandlerTraits<RetT (C::*)(ErrT &)> | |||
831 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; | |||
832 | ||||
833 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. | |||
834 | template <typename C, typename RetT, typename ErrT> | |||
835 | class ErrorHandlerTraits<RetT (C::*)(ErrT &) const> | |||
836 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; | |||
837 | ||||
838 | // Specialization for member functions of the form 'RetT (const ErrT&)'. | |||
839 | template <typename C, typename RetT, typename ErrT> | |||
840 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &)> | |||
841 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; | |||
842 | ||||
843 | // Specialization for member functions of the form 'RetT (const ErrT&) const'. | |||
844 | template <typename C, typename RetT, typename ErrT> | |||
845 | class ErrorHandlerTraits<RetT (C::*)(const ErrT &) const> | |||
846 | : public ErrorHandlerTraits<RetT (&)(ErrT &)> {}; | |||
847 | ||||
848 | /// Specialization for member functions of the form | |||
849 | /// 'RetT (std::unique_ptr<ErrT>)'. | |||
850 | template <typename C, typename RetT, typename ErrT> | |||
851 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>)> | |||
852 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; | |||
853 | ||||
854 | /// Specialization for member functions of the form | |||
855 | /// 'RetT (std::unique_ptr<ErrT>) const'. | |||
856 | template <typename C, typename RetT, typename ErrT> | |||
857 | class ErrorHandlerTraits<RetT (C::*)(std::unique_ptr<ErrT>) const> | |||
858 | : public ErrorHandlerTraits<RetT (&)(std::unique_ptr<ErrT>)> {}; | |||
859 | ||||
860 | inline Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload) { | |||
861 | return Error(std::move(Payload)); | |||
862 | } | |||
863 | ||||
864 | template <typename HandlerT, typename... HandlerTs> | |||
865 | Error handleErrorImpl(std::unique_ptr<ErrorInfoBase> Payload, | |||
866 | HandlerT &&Handler, HandlerTs &&... Handlers) { | |||
867 | if (ErrorHandlerTraits<HandlerT>::appliesTo(*Payload)) | |||
868 | return ErrorHandlerTraits<HandlerT>::apply(std::forward<HandlerT>(Handler), | |||
869 | std::move(Payload)); | |||
870 | return handleErrorImpl(std::move(Payload), | |||
871 | std::forward<HandlerTs>(Handlers)...); | |||
872 | } | |||
873 | ||||
874 | /// Pass the ErrorInfo(s) contained in E to their respective handlers. Any | |||
875 | /// unhandled errors (or Errors returned by handlers) are re-concatenated and | |||
876 | /// returned. | |||
877 | /// Because this function returns an error, its result must also be checked | |||
878 | /// or returned. If you intend to handle all errors use handleAllErrors | |||
879 | /// (which returns void, and will abort() on unhandled errors) instead. | |||
880 | template <typename... HandlerTs> | |||
881 | Error handleErrors(Error E, HandlerTs &&... Hs) { | |||
882 | if (!E) | |||
883 | return Error::success(); | |||
884 | ||||
885 | std::unique_ptr<ErrorInfoBase> Payload = E.takePayload(); | |||
886 | ||||
887 | if (Payload->isA<ErrorList>()) { | |||
888 | ErrorList &List = static_cast<ErrorList &>(*Payload); | |||
889 | Error R; | |||
890 | for (auto &P : List.Payloads) | |||
891 | R = ErrorList::join( | |||
892 | std::move(R), | |||
893 | handleErrorImpl(std::move(P), std::forward<HandlerTs>(Hs)...)); | |||
894 | return R; | |||
895 | } | |||
896 | ||||
897 | return handleErrorImpl(std::move(Payload), std::forward<HandlerTs>(Hs)...); | |||
898 | } | |||
899 | ||||
900 | /// Behaves the same as handleErrors, except that by contract all errors | |||
901 | /// *must* be handled by the given handlers (i.e. there must be no remaining | |||
902 | /// errors after running the handlers, or llvm_unreachable is called). | |||
903 | template <typename... HandlerTs> | |||
904 | void handleAllErrors(Error E, HandlerTs &&... Handlers) { | |||
905 | cantFail(handleErrors(std::move(E), std::forward<HandlerTs>(Handlers)...)); | |||
906 | } | |||
907 | ||||
908 | /// Check that E is a non-error, then drop it. | |||
909 | /// If E is an error, llvm_unreachable will be called. | |||
910 | inline void handleAllErrors(Error E) { | |||
911 | cantFail(std::move(E)); | |||
912 | } | |||
913 | ||||
914 | /// Handle any errors (if present) in an Expected<T>, then try a recovery path. | |||
915 | /// | |||
916 | /// If the incoming value is a success value it is returned unmodified. If it | |||
917 | /// is a failure value then it the contained error is passed to handleErrors. | |||
918 | /// If handleErrors is able to handle the error then the RecoveryPath functor | |||
919 | /// is called to supply the final result. If handleErrors is not able to | |||
920 | /// handle all errors then the unhandled errors are returned. | |||
921 | /// | |||
922 | /// This utility enables the follow pattern: | |||
923 | /// | |||
924 | /// @code{.cpp} | |||
925 | /// enum FooStrategy { Aggressive, Conservative }; | |||
926 | /// Expected<Foo> foo(FooStrategy S); | |||
927 | /// | |||
928 | /// auto ResultOrErr = | |||
929 | /// handleExpected( | |||
930 | /// foo(Aggressive), | |||
931 | /// []() { return foo(Conservative); }, | |||
932 | /// [](AggressiveStrategyError&) { | |||
933 | /// // Implicitly conusme this - we'll recover by using a conservative | |||
934 | /// // strategy. | |||
935 | /// }); | |||
936 | /// | |||
937 | /// @endcode | |||
938 | template <typename T, typename RecoveryFtor, typename... HandlerTs> | |||
939 | Expected<T> handleExpected(Expected<T> ValOrErr, RecoveryFtor &&RecoveryPath, | |||
940 | HandlerTs &&... Handlers) { | |||
941 | if (ValOrErr) | |||
942 | return ValOrErr; | |||
943 | ||||
944 | if (auto Err = handleErrors(ValOrErr.takeError(), | |||
945 | std::forward<HandlerTs>(Handlers)...)) | |||
946 | return std::move(Err); | |||
947 | ||||
948 | return RecoveryPath(); | |||
949 | } | |||
950 | ||||
951 | /// Log all errors (if any) in E to OS. If there are any errors, ErrorBanner | |||
952 | /// will be printed before the first one is logged. A newline will be printed | |||
953 | /// after each error. | |||
954 | /// | |||
955 | /// This function is compatible with the helpers from Support/WithColor.h. You | |||
956 | /// can pass any of them as the OS. Please consider using them instead of | |||
957 | /// including 'error: ' in the ErrorBanner. | |||
958 | /// | |||
959 | /// This is useful in the base level of your program to allow clean termination | |||
960 | /// (allowing clean deallocation of resources, etc.), while reporting error | |||
961 | /// information to the user. | |||
962 | void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {}); | |||
963 | ||||
964 | /// Write all error messages (if any) in E to a string. The newline character | |||
965 | /// is used to separate error messages. | |||
966 | inline std::string toString(Error E) { | |||
967 | SmallVector<std::string, 2> Errors; | |||
968 | handleAllErrors(std::move(E), [&Errors](const ErrorInfoBase &EI) { | |||
969 | Errors.push_back(EI.message()); | |||
970 | }); | |||
971 | return join(Errors.begin(), Errors.end(), "\n"); | |||
972 | } | |||
973 | ||||
974 | /// Consume a Error without doing anything. This method should be used | |||
975 | /// only where an error can be considered a reasonable and expected return | |||
976 | /// value. | |||
977 | /// | |||
978 | /// Uses of this method are potentially indicative of design problems: If it's | |||
979 | /// legitimate to do nothing while processing an "error", the error-producer | |||
980 | /// might be more clearly refactored to return an Optional<T>. | |||
981 | inline void consumeError(Error Err) { | |||
982 | handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {}); | |||
983 | } | |||
984 | ||||
985 | /// Helper for converting an Error to a bool. | |||
986 | /// | |||
987 | /// This method returns true if Err is in an error state, or false if it is | |||
988 | /// in a success state. Puts Err in a checked state in both cases (unlike | |||
989 | /// Error::operator bool(), which only does this for success states). | |||
990 | inline bool errorToBool(Error Err) { | |||
991 | bool IsError = static_cast<bool>(Err); | |||
992 | if (IsError) | |||
993 | consumeError(std::move(Err)); | |||
994 | return IsError; | |||
995 | } | |||
996 | ||||
997 | /// Helper for Errors used as out-parameters. | |||
998 | /// | |||
999 | /// This helper is for use with the Error-as-out-parameter idiom, where an error | |||
1000 | /// is passed to a function or method by reference, rather than being returned. | |||
1001 | /// In such cases it is helpful to set the checked bit on entry to the function | |||
1002 | /// so that the error can be written to (unchecked Errors abort on assignment) | |||
1003 | /// and clear the checked bit on exit so that clients cannot accidentally forget | |||
1004 | /// to check the result. This helper performs these actions automatically using | |||
1005 | /// RAII: | |||
1006 | /// | |||
1007 | /// @code{.cpp} | |||
1008 | /// Result foo(Error &Err) { | |||
1009 | /// ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set | |||
1010 | /// // <body of foo> | |||
1011 | /// // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed. | |||
1012 | /// } | |||
1013 | /// @endcode | |||
1014 | /// | |||
1015 | /// ErrorAsOutParameter takes an Error* rather than Error& so that it can be | |||
1016 | /// used with optional Errors (Error pointers that are allowed to be null). If | |||
1017 | /// ErrorAsOutParameter took an Error reference, an instance would have to be | |||
1018 | /// created inside every condition that verified that Error was non-null. By | |||
1019 | /// taking an Error pointer we can just create one instance at the top of the | |||
1020 | /// function. | |||
1021 | class ErrorAsOutParameter { | |||
1022 | public: | |||
1023 | ErrorAsOutParameter(Error *Err) : Err(Err) { | |||
1024 | // Raise the checked bit if Err is success. | |||
1025 | if (Err) | |||
1026 | (void)!!*Err; | |||
1027 | } | |||
1028 | ||||
1029 | ~ErrorAsOutParameter() { | |||
1030 | // Clear the checked bit. | |||
1031 | if (Err && !*Err) | |||
1032 | *Err = Error::success(); | |||
1033 | } | |||
1034 | ||||
1035 | private: | |||
1036 | Error *Err; | |||
1037 | }; | |||
1038 | ||||
1039 | /// Helper for Expected<T>s used as out-parameters. | |||
1040 | /// | |||
1041 | /// See ErrorAsOutParameter. | |||
1042 | template <typename T> | |||
1043 | class ExpectedAsOutParameter { | |||
1044 | public: | |||
1045 | ExpectedAsOutParameter(Expected<T> *ValOrErr) | |||
1046 | : ValOrErr(ValOrErr) { | |||
1047 | if (ValOrErr) | |||
1048 | (void)!!*ValOrErr; | |||
1049 | } | |||
1050 | ||||
1051 | ~ExpectedAsOutParameter() { | |||
1052 | if (ValOrErr) | |||
1053 | ValOrErr->setUnchecked(); | |||
1054 | } | |||
1055 | ||||
1056 | private: | |||
1057 | Expected<T> *ValOrErr; | |||
1058 | }; | |||
1059 | ||||
1060 | /// This class wraps a std::error_code in a Error. | |||
1061 | /// | |||
1062 | /// This is useful if you're writing an interface that returns a Error | |||
1063 | /// (or Expected) and you want to call code that still returns | |||
1064 | /// std::error_codes. | |||
1065 | class ECError : public ErrorInfo<ECError> { | |||
1066 | friend Error errorCodeToError(std::error_code); | |||
1067 | ||||
1068 | virtual void anchor() override; | |||
1069 | ||||
1070 | public: | |||
1071 | void setErrorCode(std::error_code EC) { this->EC = EC; } | |||
1072 | std::error_code convertToErrorCode() const override { return EC; } | |||
1073 | void log(raw_ostream &OS) const override { OS << EC.message(); } | |||
1074 | ||||
1075 | // Used by ErrorInfo::classID. | |||
1076 | static char ID; | |||
1077 | ||||
1078 | protected: | |||
1079 | ECError() = default; | |||
1080 | ECError(std::error_code EC) : EC(EC) {} | |||
1081 | ||||
1082 | std::error_code EC; | |||
1083 | }; | |||
1084 | ||||
1085 | /// The value returned by this function can be returned from convertToErrorCode | |||
1086 | /// for Error values where no sensible translation to std::error_code exists. | |||
1087 | /// It should only be used in this situation, and should never be used where a | |||
1088 | /// sensible conversion to std::error_code is available, as attempts to convert | |||
1089 | /// to/from this error will result in a fatal error. (i.e. it is a programmatic | |||
1090 | ///error to try to convert such a value). | |||
1091 | std::error_code inconvertibleErrorCode(); | |||
1092 | ||||
1093 | /// Helper for converting an std::error_code to a Error. | |||
1094 | Error errorCodeToError(std::error_code EC); | |||
1095 | ||||
1096 | /// Helper for converting an ECError to a std::error_code. | |||
1097 | /// | |||
1098 | /// This method requires that Err be Error() or an ECError, otherwise it | |||
1099 | /// will trigger a call to abort(). | |||
1100 | std::error_code errorToErrorCode(Error Err); | |||
1101 | ||||
1102 | /// Convert an ErrorOr<T> to an Expected<T>. | |||
1103 | template <typename T> Expected<T> errorOrToExpected(ErrorOr<T> &&EO) { | |||
1104 | if (auto EC = EO.getError()) | |||
1105 | return errorCodeToError(EC); | |||
1106 | return std::move(*EO); | |||
1107 | } | |||
1108 | ||||
1109 | /// Convert an Expected<T> to an ErrorOr<T>. | |||
1110 | template <typename T> ErrorOr<T> expectedToErrorOr(Expected<T> &&E) { | |||
1111 | if (auto Err = E.takeError()) | |||
1112 | return errorToErrorCode(std::move(Err)); | |||
1113 | return std::move(*E); | |||
1114 | } | |||
1115 | ||||
1116 | /// This class wraps a string in an Error. | |||
1117 | /// | |||
1118 | /// StringError is useful in cases where the client is not expected to be able | |||
1119 | /// to consume the specific error message programmatically (for example, if the | |||
1120 | /// error message is to be presented to the user). | |||
1121 | /// | |||
1122 | /// StringError can also be used when additional information is to be printed | |||
1123 | /// along with a error_code message. Depending on the constructor called, this | |||
1124 | /// class can either display: | |||
1125 | /// 1. the error_code message (ECError behavior) | |||
1126 | /// 2. a string | |||
1127 | /// 3. the error_code message and a string | |||
1128 | /// | |||
1129 | /// These behaviors are useful when subtyping is required; for example, when a | |||
1130 | /// specific library needs an explicit error type. In the example below, | |||
1131 | /// PDBError is derived from StringError: | |||
1132 | /// | |||
1133 | /// @code{.cpp} | |||
1134 | /// Expected<int> foo() { | |||
1135 | /// return llvm::make_error<PDBError>(pdb_error_code::dia_failed_loading, | |||
1136 | /// "Additional information"); | |||
1137 | /// } | |||
1138 | /// @endcode | |||
1139 | /// | |||
1140 | class StringError : public ErrorInfo<StringError> { | |||
1141 | public: | |||
1142 | static char ID; | |||
1143 | ||||
1144 | // Prints EC + S and converts to EC | |||
1145 | StringError(std::error_code EC, const Twine &S = Twine()); | |||
1146 | ||||
1147 | // Prints S and converts to EC | |||
1148 | StringError(const Twine &S, std::error_code EC); | |||
1149 | ||||
1150 | void log(raw_ostream &OS) const override; | |||
1151 | std::error_code convertToErrorCode() const override; | |||
1152 | ||||
1153 | const std::string &getMessage() const { return Msg; } | |||
1154 | ||||
1155 | private: | |||
1156 | std::string Msg; | |||
1157 | std::error_code EC; | |||
1158 | const bool PrintMsgOnly = false; | |||
1159 | }; | |||
1160 | ||||
1161 | /// Create formatted StringError object. | |||
1162 | template <typename... Ts> | |||
1163 | Error createStringError(std::error_code EC, char const *Fmt, | |||
1164 | const Ts &... Vals) { | |||
1165 | std::string Buffer; | |||
1166 | raw_string_ostream Stream(Buffer); | |||
1167 | Stream << format(Fmt, Vals...); | |||
1168 | return make_error<StringError>(Stream.str(), EC); | |||
1169 | } | |||
1170 | ||||
1171 | Error createStringError(std::error_code EC, char const *Msg); | |||
1172 | ||||
1173 | /// This class wraps a filename and another Error. | |||
1174 | /// | |||
1175 | /// In some cases, an error needs to live along a 'source' name, in order to | |||
1176 | /// show more detailed information to the user. | |||
1177 | class FileError final : public ErrorInfo<FileError> { | |||
1178 | ||||
1179 | friend Error createFileError(const Twine &, Error); | |||
1180 | friend Error createFileError(const Twine &, size_t, Error); | |||
1181 | ||||
1182 | public: | |||
1183 | void log(raw_ostream &OS) const override { | |||
1184 | assert(Err && !FileName.empty() && "Trying to log after takeError().")((Err && !FileName.empty() && "Trying to log after takeError()." ) ? static_cast<void> (0) : __assert_fail ("Err && !FileName.empty() && \"Trying to log after takeError().\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 1184, __PRETTY_FUNCTION__)); | |||
1185 | OS << "'" << FileName << "': "; | |||
1186 | if (Line.hasValue()) | |||
1187 | OS << "line " << Line.getValue() << ": "; | |||
1188 | Err->log(OS); | |||
1189 | } | |||
1190 | ||||
1191 | Error takeError() { return Error(std::move(Err)); } | |||
1192 | ||||
1193 | std::error_code convertToErrorCode() const override; | |||
1194 | ||||
1195 | // Used by ErrorInfo::classID. | |||
1196 | static char ID; | |||
1197 | ||||
1198 | private: | |||
1199 | FileError(const Twine &F, Optional<size_t> LineNum, | |||
1200 | std::unique_ptr<ErrorInfoBase> E) { | |||
1201 | assert(E && "Cannot create FileError from Error success value.")((E && "Cannot create FileError from Error success value." ) ? static_cast<void> (0) : __assert_fail ("E && \"Cannot create FileError from Error success value.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 1201, __PRETTY_FUNCTION__)); | |||
1202 | assert(!F.isTriviallyEmpty() &&((!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? static_cast<void> (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 1203, __PRETTY_FUNCTION__)) | |||
1203 | "The file name provided to FileError must not be empty.")((!F.isTriviallyEmpty() && "The file name provided to FileError must not be empty." ) ? static_cast<void> (0) : __assert_fail ("!F.isTriviallyEmpty() && \"The file name provided to FileError must not be empty.\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/Support/Error.h" , 1203, __PRETTY_FUNCTION__)); | |||
1204 | FileName = F.str(); | |||
1205 | Err = std::move(E); | |||
1206 | Line = std::move(LineNum); | |||
1207 | } | |||
1208 | ||||
1209 | static Error build(const Twine &F, Optional<size_t> Line, Error E) { | |||
1210 | return Error( | |||
1211 | std::unique_ptr<FileError>(new FileError(F, Line, E.takePayload()))); | |||
1212 | } | |||
1213 | ||||
1214 | std::string FileName; | |||
1215 | Optional<size_t> Line; | |||
1216 | std::unique_ptr<ErrorInfoBase> Err; | |||
1217 | }; | |||
1218 | ||||
1219 | /// Concatenate a source file path and/or name with an Error. The resulting | |||
1220 | /// Error is unchecked. | |||
1221 | inline Error createFileError(const Twine &F, Error E) { | |||
1222 | return FileError::build(F, Optional<size_t>(), std::move(E)); | |||
1223 | } | |||
1224 | ||||
1225 | /// Concatenate a source file path and/or name with line number and an Error. | |||
1226 | /// The resulting Error is unchecked. | |||
1227 | inline Error createFileError(const Twine &F, size_t Line, Error E) { | |||
1228 | return FileError::build(F, Optional<size_t>(Line), std::move(E)); | |||
1229 | } | |||
1230 | ||||
1231 | /// Concatenate a source file path and/or name with a std::error_code | |||
1232 | /// to form an Error object. | |||
1233 | inline Error createFileError(const Twine &F, std::error_code EC) { | |||
1234 | return createFileError(F, errorCodeToError(EC)); | |||
1235 | } | |||
1236 | ||||
1237 | /// Concatenate a source file path and/or name with line number and | |||
1238 | /// std::error_code to form an Error object. | |||
1239 | inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) { | |||
1240 | return createFileError(F, Line, errorCodeToError(EC)); | |||
1241 | } | |||
1242 | ||||
1243 | Error createFileError(const Twine &F, ErrorSuccess) = delete; | |||
1244 | ||||
1245 | /// Helper for check-and-exit error handling. | |||
1246 | /// | |||
1247 | /// For tool use only. NOT FOR USE IN LIBRARY CODE. | |||
1248 | /// | |||
1249 | class ExitOnError { | |||
1250 | public: | |||
1251 | /// Create an error on exit helper. | |||
1252 | ExitOnError(std::string Banner = "", int DefaultErrorExitCode = 1) | |||
1253 | : Banner(std::move(Banner)), | |||
1254 | GetExitCode([=](const Error &) { return DefaultErrorExitCode; }) {} | |||
1255 | ||||
1256 | /// Set the banner string for any errors caught by operator(). | |||
1257 | void setBanner(std::string Banner) { this->Banner = std::move(Banner); } | |||
1258 | ||||
1259 | /// Set the exit-code mapper function. | |||
1260 | void setExitCodeMapper(std::function<int(const Error &)> GetExitCode) { | |||
1261 | this->GetExitCode = std::move(GetExitCode); | |||
1262 | } | |||
1263 | ||||
1264 | /// Check Err. If it's in a failure state log the error(s) and exit. | |||
1265 | void operator()(Error Err) const { checkError(std::move(Err)); } | |||
1266 | ||||
1267 | /// Check E. If it's in a success state then return the contained value. If | |||
1268 | /// it's in a failure state log the error(s) and exit. | |||
1269 | template <typename T> T operator()(Expected<T> &&E) const { | |||
1270 | checkError(E.takeError()); | |||
1271 | return std::move(*E); | |||
1272 | } | |||
1273 | ||||
1274 | /// Check E. If it's in a success state then return the contained reference. If | |||
1275 | /// it's in a failure state log the error(s) and exit. | |||
1276 | template <typename T> T& operator()(Expected<T&> &&E) const { | |||
1277 | checkError(E.takeError()); | |||
1278 | return *E; | |||
1279 | } | |||
1280 | ||||
1281 | private: | |||
1282 | void checkError(Error Err) const { | |||
1283 | if (Err) { | |||
1284 | int ExitCode = GetExitCode(Err); | |||
1285 | logAllUnhandledErrors(std::move(Err), errs(), Banner); | |||
1286 | exit(ExitCode); | |||
1287 | } | |||
1288 | } | |||
1289 | ||||
1290 | std::string Banner; | |||
1291 | std::function<int(const Error &)> GetExitCode; | |||
1292 | }; | |||
1293 | ||||
1294 | /// Conversion from Error to LLVMErrorRef for C error bindings. | |||
1295 | inline LLVMErrorRef wrap(Error Err) { | |||
1296 | return reinterpret_cast<LLVMErrorRef>(Err.takePayload().release()); | |||
1297 | } | |||
1298 | ||||
1299 | /// Conversion from LLVMErrorRef to Error for C error bindings. | |||
1300 | inline Error unwrap(LLVMErrorRef ErrRef) { | |||
1301 | return Error(std::unique_ptr<ErrorInfoBase>( | |||
1302 | reinterpret_cast<ErrorInfoBase *>(ErrRef))); | |||
1303 | } | |||
1304 | ||||
1305 | } // end namespace llvm | |||
1306 | ||||
1307 | #endif // LLVM_SUPPORT_ERROR_H |
1 | //===- llvm/ADT/STLExtras.h - Useful STL related functions ------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file contains some templates that are useful if you are working with the |
10 | // STL at all. |
11 | // |
12 | // No library is required when using these functions. |
13 | // |
14 | //===----------------------------------------------------------------------===// |
15 | |
16 | #ifndef LLVM_ADT_STLEXTRAS_H |
17 | #define LLVM_ADT_STLEXTRAS_H |
18 | |
19 | #include "llvm/ADT/Optional.h" |
20 | #include "llvm/ADT/SmallVector.h" |
21 | #include "llvm/ADT/iterator.h" |
22 | #include "llvm/ADT/iterator_range.h" |
23 | #include "llvm/Config/abi-breaking.h" |
24 | #include "llvm/Support/ErrorHandling.h" |
25 | #include <algorithm> |
26 | #include <cassert> |
27 | #include <cstddef> |
28 | #include <cstdint> |
29 | #include <cstdlib> |
30 | #include <functional> |
31 | #include <initializer_list> |
32 | #include <iterator> |
33 | #include <limits> |
34 | #include <memory> |
35 | #include <tuple> |
36 | #include <type_traits> |
37 | #include <utility> |
38 | |
39 | #ifdef EXPENSIVE_CHECKS |
40 | #include <random> // for std::mt19937 |
41 | #endif |
42 | |
43 | namespace llvm { |
44 | |
45 | // Only used by compiler if both template types are the same. Useful when |
46 | // using SFINAE to test for the existence of member functions. |
47 | template <typename T, T> struct SameType; |
48 | |
49 | namespace detail { |
50 | |
51 | template <typename RangeT> |
52 | using IterOfRange = decltype(std::begin(std::declval<RangeT &>())); |
53 | |
54 | template <typename RangeT> |
55 | using ValueOfRange = typename std::remove_reference<decltype( |
56 | *std::begin(std::declval<RangeT &>()))>::type; |
57 | |
58 | } // end namespace detail |
59 | |
60 | //===----------------------------------------------------------------------===// |
61 | // Extra additions to <type_traits> |
62 | //===----------------------------------------------------------------------===// |
63 | |
64 | template <typename T> |
65 | struct negation : std::integral_constant<bool, !bool(T::value)> {}; |
66 | |
67 | template <typename...> struct conjunction : std::true_type {}; |
68 | template <typename B1> struct conjunction<B1> : B1 {}; |
69 | template <typename B1, typename... Bn> |
70 | struct conjunction<B1, Bn...> |
71 | : std::conditional<bool(B1::value), conjunction<Bn...>, B1>::type {}; |
72 | |
73 | template <typename T> struct make_const_ptr { |
74 | using type = |
75 | typename std::add_pointer<typename std::add_const<T>::type>::type; |
76 | }; |
77 | |
78 | template <typename T> struct make_const_ref { |
79 | using type = typename std::add_lvalue_reference< |
80 | typename std::add_const<T>::type>::type; |
81 | }; |
82 | |
83 | //===----------------------------------------------------------------------===// |
84 | // Extra additions to <functional> |
85 | //===----------------------------------------------------------------------===// |
86 | |
87 | template <class Ty> struct identity { |
88 | using argument_type = Ty; |
89 | |
90 | Ty &operator()(Ty &self) const { |
91 | return self; |
92 | } |
93 | const Ty &operator()(const Ty &self) const { |
94 | return self; |
95 | } |
96 | }; |
97 | |
98 | template <class Ty> struct less_ptr { |
99 | bool operator()(const Ty* left, const Ty* right) const { |
100 | return *left < *right; |
101 | } |
102 | }; |
103 | |
104 | template <class Ty> struct greater_ptr { |
105 | bool operator()(const Ty* left, const Ty* right) const { |
106 | return *right < *left; |
107 | } |
108 | }; |
109 | |
110 | /// An efficient, type-erasing, non-owning reference to a callable. This is |
111 | /// intended for use as the type of a function parameter that is not used |
112 | /// after the function in question returns. |
113 | /// |
114 | /// This class does not own the callable, so it is not in general safe to store |
115 | /// a function_ref. |
116 | template<typename Fn> class function_ref; |
117 | |
118 | template<typename Ret, typename ...Params> |
119 | class function_ref<Ret(Params...)> { |
120 | Ret (*callback)(intptr_t callable, Params ...params) = nullptr; |
121 | intptr_t callable; |
122 | |
123 | template<typename Callable> |
124 | static Ret callback_fn(intptr_t callable, Params ...params) { |
125 | return (*reinterpret_cast<Callable*>(callable))( |
126 | std::forward<Params>(params)...); |
127 | } |
128 | |
129 | public: |
130 | function_ref() = default; |
131 | function_ref(std::nullptr_t) {} |
132 | |
133 | template <typename Callable> |
134 | function_ref(Callable &&callable, |
135 | typename std::enable_if< |
136 | !std::is_same<typename std::remove_reference<Callable>::type, |
137 | function_ref>::value>::type * = nullptr) |
138 | : callback(callback_fn<typename std::remove_reference<Callable>::type>), |
139 | callable(reinterpret_cast<intptr_t>(&callable)) {} |
140 | |
141 | Ret operator()(Params ...params) const { |
142 | return callback(callable, std::forward<Params>(params)...); |
143 | } |
144 | |
145 | operator bool() const { return callback; } |
146 | }; |
147 | |
148 | // deleter - Very very very simple method that is used to invoke operator |
149 | // delete on something. It is used like this: |
150 | // |
151 | // for_each(V.begin(), B.end(), deleter<Interval>); |
152 | template <class T> |
153 | inline void deleter(T *Ptr) { |
154 | delete Ptr; |
155 | } |
156 | |
157 | //===----------------------------------------------------------------------===// |
158 | // Extra additions to <iterator> |
159 | //===----------------------------------------------------------------------===// |
160 | |
161 | namespace adl_detail { |
162 | |
163 | using std::begin; |
164 | |
165 | template <typename ContainerTy> |
166 | auto adl_begin(ContainerTy &&container) |
167 | -> decltype(begin(std::forward<ContainerTy>(container))) { |
168 | return begin(std::forward<ContainerTy>(container)); |
169 | } |
170 | |
171 | using std::end; |
172 | |
173 | template <typename ContainerTy> |
174 | auto adl_end(ContainerTy &&container) |
175 | -> decltype(end(std::forward<ContainerTy>(container))) { |
176 | return end(std::forward<ContainerTy>(container)); |
177 | } |
178 | |
179 | using std::swap; |
180 | |
181 | template <typename T> |
182 | void adl_swap(T &&lhs, T &&rhs) noexcept(noexcept(swap(std::declval<T>(), |
183 | std::declval<T>()))) { |
184 | swap(std::forward<T>(lhs), std::forward<T>(rhs)); |
185 | } |
186 | |
187 | } // end namespace adl_detail |
188 | |
189 | template <typename ContainerTy> |
190 | auto adl_begin(ContainerTy &&container) |
191 | -> decltype(adl_detail::adl_begin(std::forward<ContainerTy>(container))) { |
192 | return adl_detail::adl_begin(std::forward<ContainerTy>(container)); |
193 | } |
194 | |
195 | template <typename ContainerTy> |
196 | auto adl_end(ContainerTy &&container) |
197 | -> decltype(adl_detail::adl_end(std::forward<ContainerTy>(container))) { |
198 | return adl_detail::adl_end(std::forward<ContainerTy>(container)); |
199 | } |
200 | |
201 | template <typename T> |
202 | void adl_swap(T &&lhs, T &&rhs) noexcept( |
203 | noexcept(adl_detail::adl_swap(std::declval<T>(), std::declval<T>()))) { |
204 | adl_detail::adl_swap(std::forward<T>(lhs), std::forward<T>(rhs)); |
205 | } |
206 | |
207 | /// Test whether \p RangeOrContainer is empty. Similar to C++17 std::empty. |
208 | template <typename T> |
209 | constexpr bool empty(const T &RangeOrContainer) { |
210 | return adl_begin(RangeOrContainer) == adl_end(RangeOrContainer); |
211 | } |
212 | |
213 | // mapped_iterator - This is a simple iterator adapter that causes a function to |
214 | // be applied whenever operator* is invoked on the iterator. |
215 | |
216 | template <typename ItTy, typename FuncTy, |
217 | typename FuncReturnTy = |
218 | decltype(std::declval<FuncTy>()(*std::declval<ItTy>()))> |
219 | class mapped_iterator |
220 | : public iterator_adaptor_base< |
221 | mapped_iterator<ItTy, FuncTy>, ItTy, |
222 | typename std::iterator_traits<ItTy>::iterator_category, |
223 | typename std::remove_reference<FuncReturnTy>::type> { |
224 | public: |
225 | mapped_iterator(ItTy U, FuncTy F) |
226 | : mapped_iterator::iterator_adaptor_base(std::move(U)), F(std::move(F)) {} |
227 | |
228 | ItTy getCurrent() { return this->I; } |
229 | |
230 | FuncReturnTy operator*() { return F(*this->I); } |
231 | |
232 | private: |
233 | FuncTy F; |
234 | }; |
235 | |
236 | // map_iterator - Provide a convenient way to create mapped_iterators, just like |
237 | // make_pair is useful for creating pairs... |
238 | template <class ItTy, class FuncTy> |
239 | inline mapped_iterator<ItTy, FuncTy> map_iterator(ItTy I, FuncTy F) { |
240 | return mapped_iterator<ItTy, FuncTy>(std::move(I), std::move(F)); |
241 | } |
242 | |
243 | /// Helper to determine if type T has a member called rbegin(). |
244 | template <typename Ty> class has_rbegin_impl { |
245 | using yes = char[1]; |
246 | using no = char[2]; |
247 | |
248 | template <typename Inner> |
249 | static yes& test(Inner *I, decltype(I->rbegin()) * = nullptr); |
250 | |
251 | template <typename> |
252 | static no& test(...); |
253 | |
254 | public: |
255 | static const bool value = sizeof(test<Ty>(nullptr)) == sizeof(yes); |
256 | }; |
257 | |
258 | /// Metafunction to determine if T& or T has a member called rbegin(). |
259 | template <typename Ty> |
260 | struct has_rbegin : has_rbegin_impl<typename std::remove_reference<Ty>::type> { |
261 | }; |
262 | |
263 | // Returns an iterator_range over the given container which iterates in reverse. |
264 | // Note that the container must have rbegin()/rend() methods for this to work. |
265 | template <typename ContainerTy> |
266 | auto reverse(ContainerTy &&C, |
267 | typename std::enable_if<has_rbegin<ContainerTy>::value>::type * = |
268 | nullptr) -> decltype(make_range(C.rbegin(), C.rend())) { |
269 | return make_range(C.rbegin(), C.rend()); |
270 | } |
271 | |
272 | // Returns a std::reverse_iterator wrapped around the given iterator. |
273 | template <typename IteratorTy> |
274 | std::reverse_iterator<IteratorTy> make_reverse_iterator(IteratorTy It) { |
275 | return std::reverse_iterator<IteratorTy>(It); |
276 | } |
277 | |
278 | // Returns an iterator_range over the given container which iterates in reverse. |
279 | // Note that the container must have begin()/end() methods which return |
280 | // bidirectional iterators for this to work. |
281 | template <typename ContainerTy> |
282 | auto reverse( |
283 | ContainerTy &&C, |
284 | typename std::enable_if<!has_rbegin<ContainerTy>::value>::type * = nullptr) |
285 | -> decltype(make_range(llvm::make_reverse_iterator(std::end(C)), |
286 | llvm::make_reverse_iterator(std::begin(C)))) { |
287 | return make_range(llvm::make_reverse_iterator(std::end(C)), |
288 | llvm::make_reverse_iterator(std::begin(C))); |
289 | } |
290 | |
291 | /// An iterator adaptor that filters the elements of given inner iterators. |
292 | /// |
293 | /// The predicate parameter should be a callable object that accepts the wrapped |
294 | /// iterator's reference type and returns a bool. When incrementing or |
295 | /// decrementing the iterator, it will call the predicate on each element and |
296 | /// skip any where it returns false. |
297 | /// |
298 | /// \code |
299 | /// int A[] = { 1, 2, 3, 4 }; |
300 | /// auto R = make_filter_range(A, [](int N) { return N % 2 == 1; }); |
301 | /// // R contains { 1, 3 }. |
302 | /// \endcode |
303 | /// |
304 | /// Note: filter_iterator_base implements support for forward iteration. |
305 | /// filter_iterator_impl exists to provide support for bidirectional iteration, |
306 | /// conditional on whether the wrapped iterator supports it. |
307 | template <typename WrappedIteratorT, typename PredicateT, typename IterTag> |
308 | class filter_iterator_base |
309 | : public iterator_adaptor_base< |
310 | filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>, |
311 | WrappedIteratorT, |
312 | typename std::common_type< |
313 | IterTag, typename std::iterator_traits< |
314 | WrappedIteratorT>::iterator_category>::type> { |
315 | using BaseT = iterator_adaptor_base< |
316 | filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>, |
317 | WrappedIteratorT, |
318 | typename std::common_type< |
319 | IterTag, typename std::iterator_traits< |
320 | WrappedIteratorT>::iterator_category>::type>; |
321 | |
322 | protected: |
323 | WrappedIteratorT End; |
324 | PredicateT Pred; |
325 | |
326 | void findNextValid() { |
327 | while (this->I != End && !Pred(*this->I)) |
328 | BaseT::operator++(); |
329 | } |
330 | |
331 | // Construct the iterator. The begin iterator needs to know where the end |
332 | // is, so that it can properly stop when it gets there. The end iterator only |
333 | // needs the predicate to support bidirectional iteration. |
334 | filter_iterator_base(WrappedIteratorT Begin, WrappedIteratorT End, |
335 | PredicateT Pred) |
336 | : BaseT(Begin), End(End), Pred(Pred) { |
337 | findNextValid(); |
338 | } |
339 | |
340 | public: |
341 | using BaseT::operator++; |
342 | |
343 | filter_iterator_base &operator++() { |
344 | BaseT::operator++(); |
345 | findNextValid(); |
346 | return *this; |
347 | } |
348 | }; |
349 | |
350 | /// Specialization of filter_iterator_base for forward iteration only. |
351 | template <typename WrappedIteratorT, typename PredicateT, |
352 | typename IterTag = std::forward_iterator_tag> |
353 | class filter_iterator_impl |
354 | : public filter_iterator_base<WrappedIteratorT, PredicateT, IterTag> { |
355 | using BaseT = filter_iterator_base<WrappedIteratorT, PredicateT, IterTag>; |
356 | |
357 | public: |
358 | filter_iterator_impl(WrappedIteratorT Begin, WrappedIteratorT End, |
359 | PredicateT Pred) |
360 | : BaseT(Begin, End, Pred) {} |
361 | }; |
362 | |
363 | /// Specialization of filter_iterator_base for bidirectional iteration. |
364 | template <typename WrappedIteratorT, typename PredicateT> |
365 | class filter_iterator_impl<WrappedIteratorT, PredicateT, |
366 | std::bidirectional_iterator_tag> |
367 | : public filter_iterator_base<WrappedIteratorT, PredicateT, |
368 | std::bidirectional_iterator_tag> { |
369 | using BaseT = filter_iterator_base<WrappedIteratorT, PredicateT, |
370 | std::bidirectional_iterator_tag>; |
371 | void findPrevValid() { |
372 | while (!this->Pred(*this->I)) |
373 | BaseT::operator--(); |
374 | } |
375 | |
376 | public: |
377 | using BaseT::operator--; |
378 | |
379 | filter_iterator_impl(WrappedIteratorT Begin, WrappedIteratorT End, |
380 | PredicateT Pred) |
381 | : BaseT(Begin, End, Pred) {} |
382 | |
383 | filter_iterator_impl &operator--() { |
384 | BaseT::operator--(); |
385 | findPrevValid(); |
386 | return *this; |
387 | } |
388 | }; |
389 | |
390 | namespace detail { |
391 | |
392 | template <bool is_bidirectional> struct fwd_or_bidi_tag_impl { |
393 | using type = std::forward_iterator_tag; |
394 | }; |
395 | |
396 | template <> struct fwd_or_bidi_tag_impl<true> { |
397 | using type = std::bidirectional_iterator_tag; |
398 | }; |
399 | |
400 | /// Helper which sets its type member to forward_iterator_tag if the category |
401 | /// of \p IterT does not derive from bidirectional_iterator_tag, and to |
402 | /// bidirectional_iterator_tag otherwise. |
403 | template <typename IterT> struct fwd_or_bidi_tag { |
404 | using type = typename fwd_or_bidi_tag_impl<std::is_base_of< |
405 | std::bidirectional_iterator_tag, |
406 | typename std::iterator_traits<IterT>::iterator_category>::value>::type; |
407 | }; |
408 | |
409 | } // namespace detail |
410 | |
411 | /// Defines filter_iterator to a suitable specialization of |
412 | /// filter_iterator_impl, based on the underlying iterator's category. |
413 | template <typename WrappedIteratorT, typename PredicateT> |
414 | using filter_iterator = filter_iterator_impl< |
415 | WrappedIteratorT, PredicateT, |
416 | typename detail::fwd_or_bidi_tag<WrappedIteratorT>::type>; |
417 | |
418 | /// Convenience function that takes a range of elements and a predicate, |
419 | /// and return a new filter_iterator range. |
420 | /// |
421 | /// FIXME: Currently if RangeT && is a rvalue reference to a temporary, the |
422 | /// lifetime of that temporary is not kept by the returned range object, and the |
423 | /// temporary is going to be dropped on the floor after the make_iterator_range |
424 | /// full expression that contains this function call. |
425 | template <typename RangeT, typename PredicateT> |
426 | iterator_range<filter_iterator<detail::IterOfRange<RangeT>, PredicateT>> |
427 | make_filter_range(RangeT &&Range, PredicateT Pred) { |
428 | using FilterIteratorT = |
429 | filter_iterator<detail::IterOfRange<RangeT>, PredicateT>; |
430 | return make_range( |
431 | FilterIteratorT(std::begin(std::forward<RangeT>(Range)), |
432 | std::end(std::forward<RangeT>(Range)), Pred), |
433 | FilterIteratorT(std::end(std::forward<RangeT>(Range)), |
434 | std::end(std::forward<RangeT>(Range)), Pred)); |
435 | } |
436 | |
437 | /// A pseudo-iterator adaptor that is designed to implement "early increment" |
438 | /// style loops. |
439 | /// |
440 | /// This is *not a normal iterator* and should almost never be used directly. It |
441 | /// is intended primarily to be used with range based for loops and some range |
442 | /// algorithms. |
443 | /// |
444 | /// The iterator isn't quite an `OutputIterator` or an `InputIterator` but |
445 | /// somewhere between them. The constraints of these iterators are: |
446 | /// |
447 | /// - On construction or after being incremented, it is comparable and |
448 | /// dereferencable. It is *not* incrementable. |
449 | /// - After being dereferenced, it is neither comparable nor dereferencable, it |
450 | /// is only incrementable. |
451 | /// |
452 | /// This means you can only dereference the iterator once, and you can only |
453 | /// increment it once between dereferences. |
454 | template <typename WrappedIteratorT> |
455 | class early_inc_iterator_impl |
456 | : public iterator_adaptor_base<early_inc_iterator_impl<WrappedIteratorT>, |
457 | WrappedIteratorT, std::input_iterator_tag> { |
458 | using BaseT = |
459 | iterator_adaptor_base<early_inc_iterator_impl<WrappedIteratorT>, |
460 | WrappedIteratorT, std::input_iterator_tag>; |
461 | |
462 | using PointerT = typename std::iterator_traits<WrappedIteratorT>::pointer; |
463 | |
464 | protected: |
465 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
466 | bool IsEarlyIncremented = false; |
467 | #endif |
468 | |
469 | public: |
470 | early_inc_iterator_impl(WrappedIteratorT I) : BaseT(I) {} |
471 | |
472 | using BaseT::operator*; |
473 | typename BaseT::reference operator*() { |
474 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
475 | assert(!IsEarlyIncremented && "Cannot dereference twice!")((!IsEarlyIncremented && "Cannot dereference twice!") ? static_cast<void> (0) : __assert_fail ("!IsEarlyIncremented && \"Cannot dereference twice!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 475, __PRETTY_FUNCTION__)); |
476 | IsEarlyIncremented = true; |
477 | #endif |
478 | return *(this->I)++; |
479 | } |
480 | |
481 | using BaseT::operator++; |
482 | early_inc_iterator_impl &operator++() { |
483 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
484 | assert(IsEarlyIncremented && "Cannot increment before dereferencing!")((IsEarlyIncremented && "Cannot increment before dereferencing!" ) ? static_cast<void> (0) : __assert_fail ("IsEarlyIncremented && \"Cannot increment before dereferencing!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 484, __PRETTY_FUNCTION__)); |
485 | IsEarlyIncremented = false; |
486 | #endif |
487 | return *this; |
488 | } |
489 | |
490 | using BaseT::operator==; |
491 | bool operator==(const early_inc_iterator_impl &RHS) const { |
492 | #if LLVM_ENABLE_ABI_BREAKING_CHECKS1 |
493 | assert(!IsEarlyIncremented && "Cannot compare after dereferencing!")((!IsEarlyIncremented && "Cannot compare after dereferencing!" ) ? static_cast<void> (0) : __assert_fail ("!IsEarlyIncremented && \"Cannot compare after dereferencing!\"" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 493, __PRETTY_FUNCTION__)); |
494 | #endif |
495 | return BaseT::operator==(RHS); |
496 | } |
497 | }; |
498 | |
499 | /// Make a range that does early increment to allow mutation of the underlying |
500 | /// range without disrupting iteration. |
501 | /// |
502 | /// The underlying iterator will be incremented immediately after it is |
503 | /// dereferenced, allowing deletion of the current node or insertion of nodes to |
504 | /// not disrupt iteration provided they do not invalidate the *next* iterator -- |
505 | /// the current iterator can be invalidated. |
506 | /// |
507 | /// This requires a very exact pattern of use that is only really suitable to |
508 | /// range based for loops and other range algorithms that explicitly guarantee |
509 | /// to dereference exactly once each element, and to increment exactly once each |
510 | /// element. |
511 | template <typename RangeT> |
512 | iterator_range<early_inc_iterator_impl<detail::IterOfRange<RangeT>>> |
513 | make_early_inc_range(RangeT &&Range) { |
514 | using EarlyIncIteratorT = |
515 | early_inc_iterator_impl<detail::IterOfRange<RangeT>>; |
516 | return make_range(EarlyIncIteratorT(std::begin(std::forward<RangeT>(Range))), |
517 | EarlyIncIteratorT(std::end(std::forward<RangeT>(Range)))); |
518 | } |
519 | |
520 | // forward declarations required by zip_shortest/zip_first/zip_longest |
521 | template <typename R, typename UnaryPredicate> |
522 | bool all_of(R &&range, UnaryPredicate P); |
523 | template <typename R, typename UnaryPredicate> |
524 | bool any_of(R &&range, UnaryPredicate P); |
525 | |
526 | template <size_t... I> struct index_sequence; |
527 | |
528 | template <class... Ts> struct index_sequence_for; |
529 | |
530 | namespace detail { |
531 | |
532 | using std::declval; |
533 | |
534 | // We have to alias this since inlining the actual type at the usage site |
535 | // in the parameter list of iterator_facade_base<> below ICEs MSVC 2017. |
536 | template<typename... Iters> struct ZipTupleType { |
537 | using type = std::tuple<decltype(*declval<Iters>())...>; |
538 | }; |
539 | |
540 | template <typename ZipType, typename... Iters> |
541 | using zip_traits = iterator_facade_base< |
542 | ZipType, typename std::common_type<std::bidirectional_iterator_tag, |
543 | typename std::iterator_traits< |
544 | Iters>::iterator_category...>::type, |
545 | // ^ TODO: Implement random access methods. |
546 | typename ZipTupleType<Iters...>::type, |
547 | typename std::iterator_traits<typename std::tuple_element< |
548 | 0, std::tuple<Iters...>>::type>::difference_type, |
549 | // ^ FIXME: This follows boost::make_zip_iterator's assumption that all |
550 | // inner iterators have the same difference_type. It would fail if, for |
551 | // instance, the second field's difference_type were non-numeric while the |
552 | // first is. |
553 | typename ZipTupleType<Iters...>::type *, |
554 | typename ZipTupleType<Iters...>::type>; |
555 | |
556 | template <typename ZipType, typename... Iters> |
557 | struct zip_common : public zip_traits<ZipType, Iters...> { |
558 | using Base = zip_traits<ZipType, Iters...>; |
559 | using value_type = typename Base::value_type; |
560 | |
561 | std::tuple<Iters...> iterators; |
562 | |
563 | protected: |
564 | template <size_t... Ns> value_type deref(index_sequence<Ns...>) const { |
565 | return value_type(*std::get<Ns>(iterators)...); |
566 | } |
567 | |
568 | template <size_t... Ns> |
569 | decltype(iterators) tup_inc(index_sequence<Ns...>) const { |
570 | return std::tuple<Iters...>(std::next(std::get<Ns>(iterators))...); |
571 | } |
572 | |
573 | template <size_t... Ns> |
574 | decltype(iterators) tup_dec(index_sequence<Ns...>) const { |
575 | return std::tuple<Iters...>(std::prev(std::get<Ns>(iterators))...); |
576 | } |
577 | |
578 | public: |
579 | zip_common(Iters &&... ts) : iterators(std::forward<Iters>(ts)...) {} |
580 | |
581 | value_type operator*() { return deref(index_sequence_for<Iters...>{}); } |
582 | |
583 | const value_type operator*() const { |
584 | return deref(index_sequence_for<Iters...>{}); |
585 | } |
586 | |
587 | ZipType &operator++() { |
588 | iterators = tup_inc(index_sequence_for<Iters...>{}); |
589 | return *reinterpret_cast<ZipType *>(this); |
590 | } |
591 | |
592 | ZipType &operator--() { |
593 | static_assert(Base::IsBidirectional, |
594 | "All inner iterators must be at least bidirectional."); |
595 | iterators = tup_dec(index_sequence_for<Iters...>{}); |
596 | return *reinterpret_cast<ZipType *>(this); |
597 | } |
598 | }; |
599 | |
600 | template <typename... Iters> |
601 | struct zip_first : public zip_common<zip_first<Iters...>, Iters...> { |
602 | using Base = zip_common<zip_first<Iters...>, Iters...>; |
603 | |
604 | bool operator==(const zip_first<Iters...> &other) const { |
605 | return std::get<0>(this->iterators) == std::get<0>(other.iterators); |
606 | } |
607 | |
608 | zip_first(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {} |
609 | }; |
610 | |
611 | template <typename... Iters> |
612 | class zip_shortest : public zip_common<zip_shortest<Iters...>, Iters...> { |
613 | template <size_t... Ns> |
614 | bool test(const zip_shortest<Iters...> &other, index_sequence<Ns...>) const { |
615 | return all_of(std::initializer_list<bool>{std::get<Ns>(this->iterators) != |
616 | std::get<Ns>(other.iterators)...}, |
617 | identity<bool>{}); |
618 | } |
619 | |
620 | public: |
621 | using Base = zip_common<zip_shortest<Iters...>, Iters...>; |
622 | |
623 | zip_shortest(Iters &&... ts) : Base(std::forward<Iters>(ts)...) {} |
624 | |
625 | bool operator==(const zip_shortest<Iters...> &other) const { |
626 | return !test(other, index_sequence_for<Iters...>{}); |
627 | } |
628 | }; |
629 | |
630 | template <template <typename...> class ItType, typename... Args> class zippy { |
631 | public: |
632 | using iterator = ItType<decltype(std::begin(std::declval<Args>()))...>; |
633 | using iterator_category = typename iterator::iterator_category; |
634 | using value_type = typename iterator::value_type; |
635 | using difference_type = typename iterator::difference_type; |
636 | using pointer = typename iterator::pointer; |
637 | using reference = typename iterator::reference; |
638 | |
639 | private: |
640 | std::tuple<Args...> ts; |
641 | |
642 | template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) const { |
643 | return iterator(std::begin(std::get<Ns>(ts))...); |
644 | } |
645 | template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) const { |
646 | return iterator(std::end(std::get<Ns>(ts))...); |
647 | } |
648 | |
649 | public: |
650 | zippy(Args &&... ts_) : ts(std::forward<Args>(ts_)...) {} |
651 | |
652 | iterator begin() const { return begin_impl(index_sequence_for<Args...>{}); } |
653 | iterator end() const { return end_impl(index_sequence_for<Args...>{}); } |
654 | }; |
655 | |
656 | } // end namespace detail |
657 | |
658 | /// zip iterator for two or more iteratable types. |
659 | template <typename T, typename U, typename... Args> |
660 | detail::zippy<detail::zip_shortest, T, U, Args...> zip(T &&t, U &&u, |
661 | Args &&... args) { |
662 | return detail::zippy<detail::zip_shortest, T, U, Args...>( |
663 | std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); |
664 | } |
665 | |
666 | /// zip iterator that, for the sake of efficiency, assumes the first iteratee to |
667 | /// be the shortest. |
668 | template <typename T, typename U, typename... Args> |
669 | detail::zippy<detail::zip_first, T, U, Args...> zip_first(T &&t, U &&u, |
670 | Args &&... args) { |
671 | return detail::zippy<detail::zip_first, T, U, Args...>( |
672 | std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); |
673 | } |
674 | |
675 | namespace detail { |
676 | template <typename Iter> |
677 | static Iter next_or_end(const Iter &I, const Iter &End) { |
678 | if (I == End) |
679 | return End; |
680 | return std::next(I); |
681 | } |
682 | |
683 | template <typename Iter> |
684 | static auto deref_or_none(const Iter &I, const Iter &End) |
685 | -> llvm::Optional<typename std::remove_const< |
686 | typename std::remove_reference<decltype(*I)>::type>::type> { |
687 | if (I == End) |
688 | return None; |
689 | return *I; |
690 | } |
691 | |
692 | template <typename Iter> struct ZipLongestItemType { |
693 | using type = |
694 | llvm::Optional<typename std::remove_const<typename std::remove_reference< |
695 | decltype(*std::declval<Iter>())>::type>::type>; |
696 | }; |
697 | |
698 | template <typename... Iters> struct ZipLongestTupleType { |
699 | using type = std::tuple<typename ZipLongestItemType<Iters>::type...>; |
700 | }; |
701 | |
702 | template <typename... Iters> |
703 | class zip_longest_iterator |
704 | : public iterator_facade_base< |
705 | zip_longest_iterator<Iters...>, |
706 | typename std::common_type< |
707 | std::forward_iterator_tag, |
708 | typename std::iterator_traits<Iters>::iterator_category...>::type, |
709 | typename ZipLongestTupleType<Iters...>::type, |
710 | typename std::iterator_traits<typename std::tuple_element< |
711 | 0, std::tuple<Iters...>>::type>::difference_type, |
712 | typename ZipLongestTupleType<Iters...>::type *, |
713 | typename ZipLongestTupleType<Iters...>::type> { |
714 | public: |
715 | using value_type = typename ZipLongestTupleType<Iters...>::type; |
716 | |
717 | private: |
718 | std::tuple<Iters...> iterators; |
719 | std::tuple<Iters...> end_iterators; |
720 | |
721 | template <size_t... Ns> |
722 | bool test(const zip_longest_iterator<Iters...> &other, |
723 | index_sequence<Ns...>) const { |
724 | return llvm::any_of( |
725 | std::initializer_list<bool>{std::get<Ns>(this->iterators) != |
726 | std::get<Ns>(other.iterators)...}, |
727 | identity<bool>{}); |
728 | } |
729 | |
730 | template <size_t... Ns> value_type deref(index_sequence<Ns...>) const { |
731 | return value_type( |
732 | deref_or_none(std::get<Ns>(iterators), std::get<Ns>(end_iterators))...); |
733 | } |
734 | |
735 | template <size_t... Ns> |
736 | decltype(iterators) tup_inc(index_sequence<Ns...>) const { |
737 | return std::tuple<Iters...>( |
738 | next_or_end(std::get<Ns>(iterators), std::get<Ns>(end_iterators))...); |
739 | } |
740 | |
741 | public: |
742 | zip_longest_iterator(std::pair<Iters &&, Iters &&>... ts) |
743 | : iterators(std::forward<Iters>(ts.first)...), |
744 | end_iterators(std::forward<Iters>(ts.second)...) {} |
745 | |
746 | value_type operator*() { return deref(index_sequence_for<Iters...>{}); } |
747 | |
748 | value_type operator*() const { return deref(index_sequence_for<Iters...>{}); } |
749 | |
750 | zip_longest_iterator<Iters...> &operator++() { |
751 | iterators = tup_inc(index_sequence_for<Iters...>{}); |
752 | return *this; |
753 | } |
754 | |
755 | bool operator==(const zip_longest_iterator<Iters...> &other) const { |
756 | return !test(other, index_sequence_for<Iters...>{}); |
757 | } |
758 | }; |
759 | |
760 | template <typename... Args> class zip_longest_range { |
761 | public: |
762 | using iterator = |
763 | zip_longest_iterator<decltype(adl_begin(std::declval<Args>()))...>; |
764 | using iterator_category = typename iterator::iterator_category; |
765 | using value_type = typename iterator::value_type; |
766 | using difference_type = typename iterator::difference_type; |
767 | using pointer = typename iterator::pointer; |
768 | using reference = typename iterator::reference; |
769 | |
770 | private: |
771 | std::tuple<Args...> ts; |
772 | |
773 | template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) const { |
774 | return iterator(std::make_pair(adl_begin(std::get<Ns>(ts)), |
775 | adl_end(std::get<Ns>(ts)))...); |
776 | } |
777 | |
778 | template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) const { |
779 | return iterator(std::make_pair(adl_end(std::get<Ns>(ts)), |
780 | adl_end(std::get<Ns>(ts)))...); |
781 | } |
782 | |
783 | public: |
784 | zip_longest_range(Args &&... ts_) : ts(std::forward<Args>(ts_)...) {} |
785 | |
786 | iterator begin() const { return begin_impl(index_sequence_for<Args...>{}); } |
787 | iterator end() const { return end_impl(index_sequence_for<Args...>{}); } |
788 | }; |
789 | } // namespace detail |
790 | |
791 | /// Iterate over two or more iterators at the same time. Iteration continues |
792 | /// until all iterators reach the end. The llvm::Optional only contains a value |
793 | /// if the iterator has not reached the end. |
794 | template <typename T, typename U, typename... Args> |
795 | detail::zip_longest_range<T, U, Args...> zip_longest(T &&t, U &&u, |
796 | Args &&... args) { |
797 | return detail::zip_longest_range<T, U, Args...>( |
798 | std::forward<T>(t), std::forward<U>(u), std::forward<Args>(args)...); |
799 | } |
800 | |
801 | /// Iterator wrapper that concatenates sequences together. |
802 | /// |
803 | /// This can concatenate different iterators, even with different types, into |
804 | /// a single iterator provided the value types of all the concatenated |
805 | /// iterators expose `reference` and `pointer` types that can be converted to |
806 | /// `ValueT &` and `ValueT *` respectively. It doesn't support more |
807 | /// interesting/customized pointer or reference types. |
808 | /// |
809 | /// Currently this only supports forward or higher iterator categories as |
810 | /// inputs and always exposes a forward iterator interface. |
811 | template <typename ValueT, typename... IterTs> |
812 | class concat_iterator |
813 | : public iterator_facade_base<concat_iterator<ValueT, IterTs...>, |
814 | std::forward_iterator_tag, ValueT> { |
815 | using BaseT = typename concat_iterator::iterator_facade_base; |
816 | |
817 | /// We store both the current and end iterators for each concatenated |
818 | /// sequence in a tuple of pairs. |
819 | /// |
820 | /// Note that something like iterator_range seems nice at first here, but the |
821 | /// range properties are of little benefit and end up getting in the way |
822 | /// because we need to do mutation on the current iterators. |
823 | std::tuple<IterTs...> Begins; |
824 | std::tuple<IterTs...> Ends; |
825 | |
826 | /// Attempts to increment a specific iterator. |
827 | /// |
828 | /// Returns true if it was able to increment the iterator. Returns false if |
829 | /// the iterator is already at the end iterator. |
830 | template <size_t Index> bool incrementHelper() { |
831 | auto &Begin = std::get<Index>(Begins); |
832 | auto &End = std::get<Index>(Ends); |
833 | if (Begin == End) |
834 | return false; |
835 | |
836 | ++Begin; |
837 | return true; |
838 | } |
839 | |
840 | /// Increments the first non-end iterator. |
841 | /// |
842 | /// It is an error to call this with all iterators at the end. |
843 | template <size_t... Ns> void increment(index_sequence<Ns...>) { |
844 | // Build a sequence of functions to increment each iterator if possible. |
845 | bool (concat_iterator::*IncrementHelperFns[])() = { |
846 | &concat_iterator::incrementHelper<Ns>...}; |
847 | |
848 | // Loop over them, and stop as soon as we succeed at incrementing one. |
849 | for (auto &IncrementHelperFn : IncrementHelperFns) |
850 | if ((this->*IncrementHelperFn)()) |
851 | return; |
852 | |
853 | llvm_unreachable("Attempted to increment an end concat iterator!")::llvm::llvm_unreachable_internal("Attempted to increment an end concat iterator!" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 853); |
854 | } |
855 | |
856 | /// Returns null if the specified iterator is at the end. Otherwise, |
857 | /// dereferences the iterator and returns the address of the resulting |
858 | /// reference. |
859 | template <size_t Index> ValueT *getHelper() const { |
860 | auto &Begin = std::get<Index>(Begins); |
861 | auto &End = std::get<Index>(Ends); |
862 | if (Begin == End) |
863 | return nullptr; |
864 | |
865 | return &*Begin; |
866 | } |
867 | |
868 | /// Finds the first non-end iterator, dereferences, and returns the resulting |
869 | /// reference. |
870 | /// |
871 | /// It is an error to call this with all iterators at the end. |
872 | template <size_t... Ns> ValueT &get(index_sequence<Ns...>) const { |
873 | // Build a sequence of functions to get from iterator if possible. |
874 | ValueT *(concat_iterator::*GetHelperFns[])() const = { |
875 | &concat_iterator::getHelper<Ns>...}; |
876 | |
877 | // Loop over them, and return the first result we find. |
878 | for (auto &GetHelperFn : GetHelperFns) |
879 | if (ValueT *P = (this->*GetHelperFn)()) |
880 | return *P; |
881 | |
882 | llvm_unreachable("Attempted to get a pointer from an end concat iterator!")::llvm::llvm_unreachable_internal("Attempted to get a pointer from an end concat iterator!" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 882); |
883 | } |
884 | |
885 | public: |
886 | /// Constructs an iterator from a squence of ranges. |
887 | /// |
888 | /// We need the full range to know how to switch between each of the |
889 | /// iterators. |
890 | template <typename... RangeTs> |
891 | explicit concat_iterator(RangeTs &&... Ranges) |
892 | : Begins(std::begin(Ranges)...), Ends(std::end(Ranges)...) {} |
893 | |
894 | using BaseT::operator++; |
895 | |
896 | concat_iterator &operator++() { |
897 | increment(index_sequence_for<IterTs...>()); |
898 | return *this; |
899 | } |
900 | |
901 | ValueT &operator*() const { return get(index_sequence_for<IterTs...>()); } |
902 | |
903 | bool operator==(const concat_iterator &RHS) const { |
904 | return Begins == RHS.Begins && Ends == RHS.Ends; |
905 | } |
906 | }; |
907 | |
908 | namespace detail { |
909 | |
910 | /// Helper to store a sequence of ranges being concatenated and access them. |
911 | /// |
912 | /// This is designed to facilitate providing actual storage when temporaries |
913 | /// are passed into the constructor such that we can use it as part of range |
914 | /// based for loops. |
915 | template <typename ValueT, typename... RangeTs> class concat_range { |
916 | public: |
917 | using iterator = |
918 | concat_iterator<ValueT, |
919 | decltype(std::begin(std::declval<RangeTs &>()))...>; |
920 | |
921 | private: |
922 | std::tuple<RangeTs...> Ranges; |
923 | |
924 | template <size_t... Ns> iterator begin_impl(index_sequence<Ns...>) { |
925 | return iterator(std::get<Ns>(Ranges)...); |
926 | } |
927 | template <size_t... Ns> iterator end_impl(index_sequence<Ns...>) { |
928 | return iterator(make_range(std::end(std::get<Ns>(Ranges)), |
929 | std::end(std::get<Ns>(Ranges)))...); |
930 | } |
931 | |
932 | public: |
933 | concat_range(RangeTs &&... Ranges) |
934 | : Ranges(std::forward<RangeTs>(Ranges)...) {} |
935 | |
936 | iterator begin() { return begin_impl(index_sequence_for<RangeTs...>{}); } |
937 | iterator end() { return end_impl(index_sequence_for<RangeTs...>{}); } |
938 | }; |
939 | |
940 | } // end namespace detail |
941 | |
942 | /// Concatenated range across two or more ranges. |
943 | /// |
944 | /// The desired value type must be explicitly specified. |
945 | template <typename ValueT, typename... RangeTs> |
946 | detail::concat_range<ValueT, RangeTs...> concat(RangeTs &&... Ranges) { |
947 | static_assert(sizeof...(RangeTs) > 1, |
948 | "Need more than one range to concatenate!"); |
949 | return detail::concat_range<ValueT, RangeTs...>( |
950 | std::forward<RangeTs>(Ranges)...); |
951 | } |
952 | |
953 | //===----------------------------------------------------------------------===// |
954 | // Extra additions to <utility> |
955 | //===----------------------------------------------------------------------===// |
956 | |
957 | /// Function object to check whether the first component of a std::pair |
958 | /// compares less than the first component of another std::pair. |
959 | struct less_first { |
960 | template <typename T> bool operator()(const T &lhs, const T &rhs) const { |
961 | return lhs.first < rhs.first; |
962 | } |
963 | }; |
964 | |
965 | /// Function object to check whether the second component of a std::pair |
966 | /// compares less than the second component of another std::pair. |
967 | struct less_second { |
968 | template <typename T> bool operator()(const T &lhs, const T &rhs) const { |
969 | return lhs.second < rhs.second; |
970 | } |
971 | }; |
972 | |
973 | /// \brief Function object to apply a binary function to the first component of |
974 | /// a std::pair. |
975 | template<typename FuncTy> |
976 | struct on_first { |
977 | FuncTy func; |
978 | |
979 | template <typename T> |
980 | auto operator()(const T &lhs, const T &rhs) const |
981 | -> decltype(func(lhs.first, rhs.first)) { |
982 | return func(lhs.first, rhs.first); |
983 | } |
984 | }; |
985 | |
986 | // A subset of N3658. More stuff can be added as-needed. |
987 | |
988 | /// Represents a compile-time sequence of integers. |
989 | template <class T, T... I> struct integer_sequence { |
990 | using value_type = T; |
991 | |
992 | static constexpr size_t size() { return sizeof...(I); } |
993 | }; |
994 | |
995 | /// Alias for the common case of a sequence of size_ts. |
996 | template <size_t... I> |
997 | struct index_sequence : integer_sequence<std::size_t, I...> {}; |
998 | |
999 | template <std::size_t N, std::size_t... I> |
1000 | struct build_index_impl : build_index_impl<N - 1, N - 1, I...> {}; |
1001 | template <std::size_t... I> |
1002 | struct build_index_impl<0, I...> : index_sequence<I...> {}; |
1003 | |
1004 | /// Creates a compile-time integer sequence for a parameter pack. |
1005 | template <class... Ts> |
1006 | struct index_sequence_for : build_index_impl<sizeof...(Ts)> {}; |
1007 | |
1008 | /// Utility type to build an inheritance chain that makes it easy to rank |
1009 | /// overload candidates. |
1010 | template <int N> struct rank : rank<N - 1> {}; |
1011 | template <> struct rank<0> {}; |
1012 | |
1013 | /// traits class for checking whether type T is one of any of the given |
1014 | /// types in the variadic list. |
1015 | template <typename T, typename... Ts> struct is_one_of { |
1016 | static const bool value = false; |
1017 | }; |
1018 | |
1019 | template <typename T, typename U, typename... Ts> |
1020 | struct is_one_of<T, U, Ts...> { |
1021 | static const bool value = |
1022 | std::is_same<T, U>::value || is_one_of<T, Ts...>::value; |
1023 | }; |
1024 | |
1025 | /// traits class for checking whether type T is a base class for all |
1026 | /// the given types in the variadic list. |
1027 | template <typename T, typename... Ts> struct are_base_of { |
1028 | static const bool value = true; |
1029 | }; |
1030 | |
1031 | template <typename T, typename U, typename... Ts> |
1032 | struct are_base_of<T, U, Ts...> { |
1033 | static const bool value = |
1034 | std::is_base_of<T, U>::value && are_base_of<T, Ts...>::value; |
1035 | }; |
1036 | |
1037 | //===----------------------------------------------------------------------===// |
1038 | // Extra additions for arrays |
1039 | //===----------------------------------------------------------------------===// |
1040 | |
1041 | /// Find the length of an array. |
1042 | template <class T, std::size_t N> |
1043 | constexpr inline size_t array_lengthof(T (&)[N]) { |
1044 | return N; |
1045 | } |
1046 | |
1047 | /// Adapt std::less<T> for array_pod_sort. |
1048 | template<typename T> |
1049 | inline int array_pod_sort_comparator(const void *P1, const void *P2) { |
1050 | if (std::less<T>()(*reinterpret_cast<const T*>(P1), |
1051 | *reinterpret_cast<const T*>(P2))) |
1052 | return -1; |
1053 | if (std::less<T>()(*reinterpret_cast<const T*>(P2), |
1054 | *reinterpret_cast<const T*>(P1))) |
1055 | return 1; |
1056 | return 0; |
1057 | } |
1058 | |
1059 | /// get_array_pod_sort_comparator - This is an internal helper function used to |
1060 | /// get type deduction of T right. |
1061 | template<typename T> |
1062 | inline int (*get_array_pod_sort_comparator(const T &)) |
1063 | (const void*, const void*) { |
1064 | return array_pod_sort_comparator<T>; |
1065 | } |
1066 | |
1067 | /// array_pod_sort - This sorts an array with the specified start and end |
1068 | /// extent. This is just like std::sort, except that it calls qsort instead of |
1069 | /// using an inlined template. qsort is slightly slower than std::sort, but |
1070 | /// most sorts are not performance critical in LLVM and std::sort has to be |
1071 | /// template instantiated for each type, leading to significant measured code |
1072 | /// bloat. This function should generally be used instead of std::sort where |
1073 | /// possible. |
1074 | /// |
1075 | /// This function assumes that you have simple POD-like types that can be |
1076 | /// compared with std::less and can be moved with memcpy. If this isn't true, |
1077 | /// you should use std::sort. |
1078 | /// |
1079 | /// NOTE: If qsort_r were portable, we could allow a custom comparator and |
1080 | /// default to std::less. |
1081 | template<class IteratorTy> |
1082 | inline void array_pod_sort(IteratorTy Start, IteratorTy End) { |
1083 | // Don't inefficiently call qsort with one element or trigger undefined |
1084 | // behavior with an empty sequence. |
1085 | auto NElts = End - Start; |
1086 | if (NElts <= 1) return; |
1087 | #ifdef EXPENSIVE_CHECKS |
1088 | std::mt19937 Generator(std::random_device{}()); |
1089 | std::shuffle(Start, End, Generator); |
1090 | #endif |
1091 | qsort(&*Start, NElts, sizeof(*Start), get_array_pod_sort_comparator(*Start)); |
1092 | } |
1093 | |
1094 | template <class IteratorTy> |
1095 | inline void array_pod_sort( |
1096 | IteratorTy Start, IteratorTy End, |
1097 | int (*Compare)( |
1098 | const typename std::iterator_traits<IteratorTy>::value_type *, |
1099 | const typename std::iterator_traits<IteratorTy>::value_type *)) { |
1100 | // Don't inefficiently call qsort with one element or trigger undefined |
1101 | // behavior with an empty sequence. |
1102 | auto NElts = End - Start; |
1103 | if (NElts <= 1) return; |
1104 | #ifdef EXPENSIVE_CHECKS |
1105 | std::mt19937 Generator(std::random_device{}()); |
1106 | std::shuffle(Start, End, Generator); |
1107 | #endif |
1108 | qsort(&*Start, NElts, sizeof(*Start), |
1109 | reinterpret_cast<int (*)(const void *, const void *)>(Compare)); |
1110 | } |
1111 | |
1112 | // Provide wrappers to std::sort which shuffle the elements before sorting |
1113 | // to help uncover non-deterministic behavior (PR35135). |
1114 | template <typename IteratorTy> |
1115 | inline void sort(IteratorTy Start, IteratorTy End) { |
1116 | #ifdef EXPENSIVE_CHECKS |
1117 | std::mt19937 Generator(std::random_device{}()); |
1118 | std::shuffle(Start, End, Generator); |
1119 | #endif |
1120 | std::sort(Start, End); |
1121 | } |
1122 | |
1123 | template <typename Container> inline void sort(Container &&C) { |
1124 | llvm::sort(adl_begin(C), adl_end(C)); |
1125 | } |
1126 | |
1127 | template <typename IteratorTy, typename Compare> |
1128 | inline void sort(IteratorTy Start, IteratorTy End, Compare Comp) { |
1129 | #ifdef EXPENSIVE_CHECKS |
1130 | std::mt19937 Generator(std::random_device{}()); |
1131 | std::shuffle(Start, End, Generator); |
1132 | #endif |
1133 | std::sort(Start, End, Comp); |
1134 | } |
1135 | |
1136 | template <typename Container, typename Compare> |
1137 | inline void sort(Container &&C, Compare Comp) { |
1138 | llvm::sort(adl_begin(C), adl_end(C), Comp); |
1139 | } |
1140 | |
1141 | //===----------------------------------------------------------------------===// |
1142 | // Extra additions to <algorithm> |
1143 | //===----------------------------------------------------------------------===// |
1144 | |
1145 | /// For a container of pointers, deletes the pointers and then clears the |
1146 | /// container. |
1147 | template<typename Container> |
1148 | void DeleteContainerPointers(Container &C) { |
1149 | for (auto V : C) |
1150 | delete V; |
1151 | C.clear(); |
1152 | } |
1153 | |
1154 | /// In a container of pairs (usually a map) whose second element is a pointer, |
1155 | /// deletes the second elements and then clears the container. |
1156 | template<typename Container> |
1157 | void DeleteContainerSeconds(Container &C) { |
1158 | for (auto &V : C) |
1159 | delete V.second; |
1160 | C.clear(); |
1161 | } |
1162 | |
1163 | /// Get the size of a range. This is a wrapper function around std::distance |
1164 | /// which is only enabled when the operation is O(1). |
1165 | template <typename R> |
1166 | auto size(R &&Range, typename std::enable_if< |
1167 | std::is_same<typename std::iterator_traits<decltype( |
1168 | Range.begin())>::iterator_category, |
1169 | std::random_access_iterator_tag>::value, |
1170 | void>::type * = nullptr) |
1171 | -> decltype(std::distance(Range.begin(), Range.end())) { |
1172 | return std::distance(Range.begin(), Range.end()); |
1173 | } |
1174 | |
1175 | /// Provide wrappers to std::for_each which take ranges instead of having to |
1176 | /// pass begin/end explicitly. |
1177 | template <typename R, typename UnaryPredicate> |
1178 | UnaryPredicate for_each(R &&Range, UnaryPredicate P) { |
1179 | return std::for_each(adl_begin(Range), adl_end(Range), P); |
1180 | } |
1181 | |
1182 | /// Provide wrappers to std::all_of which take ranges instead of having to pass |
1183 | /// begin/end explicitly. |
1184 | template <typename R, typename UnaryPredicate> |
1185 | bool all_of(R &&Range, UnaryPredicate P) { |
1186 | return std::all_of(adl_begin(Range), adl_end(Range), P); |
1187 | } |
1188 | |
1189 | /// Provide wrappers to std::any_of which take ranges instead of having to pass |
1190 | /// begin/end explicitly. |
1191 | template <typename R, typename UnaryPredicate> |
1192 | bool any_of(R &&Range, UnaryPredicate P) { |
1193 | return std::any_of(adl_begin(Range), adl_end(Range), P); |
1194 | } |
1195 | |
1196 | /// Provide wrappers to std::none_of which take ranges instead of having to pass |
1197 | /// begin/end explicitly. |
1198 | template <typename R, typename UnaryPredicate> |
1199 | bool none_of(R &&Range, UnaryPredicate P) { |
1200 | return std::none_of(adl_begin(Range), adl_end(Range), P); |
1201 | } |
1202 | |
1203 | /// Provide wrappers to std::find which take ranges instead of having to pass |
1204 | /// begin/end explicitly. |
1205 | template <typename R, typename T> |
1206 | auto find(R &&Range, const T &Val) -> decltype(adl_begin(Range)) { |
1207 | return std::find(adl_begin(Range), adl_end(Range), Val); |
1208 | } |
1209 | |
1210 | /// Provide wrappers to std::find_if which take ranges instead of having to pass |
1211 | /// begin/end explicitly. |
1212 | template <typename R, typename UnaryPredicate> |
1213 | auto find_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) { |
1214 | return std::find_if(adl_begin(Range), adl_end(Range), P); |
1215 | } |
1216 | |
1217 | template <typename R, typename UnaryPredicate> |
1218 | auto find_if_not(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) { |
1219 | return std::find_if_not(adl_begin(Range), adl_end(Range), P); |
1220 | } |
1221 | |
1222 | /// Provide wrappers to std::remove_if which take ranges instead of having to |
1223 | /// pass begin/end explicitly. |
1224 | template <typename R, typename UnaryPredicate> |
1225 | auto remove_if(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) { |
1226 | return std::remove_if(adl_begin(Range), adl_end(Range), P); |
1227 | } |
1228 | |
1229 | /// Provide wrappers to std::copy_if which take ranges instead of having to |
1230 | /// pass begin/end explicitly. |
1231 | template <typename R, typename OutputIt, typename UnaryPredicate> |
1232 | OutputIt copy_if(R &&Range, OutputIt Out, UnaryPredicate P) { |
1233 | return std::copy_if(adl_begin(Range), adl_end(Range), Out, P); |
1234 | } |
1235 | |
1236 | template <typename R, typename OutputIt> |
1237 | OutputIt copy(R &&Range, OutputIt Out) { |
1238 | return std::copy(adl_begin(Range), adl_end(Range), Out); |
1239 | } |
1240 | |
1241 | /// Wrapper function around std::find to detect if an element exists |
1242 | /// in a container. |
1243 | template <typename R, typename E> |
1244 | bool is_contained(R &&Range, const E &Element) { |
1245 | return std::find(adl_begin(Range), adl_end(Range), Element) != adl_end(Range); |
1246 | } |
1247 | |
1248 | /// Wrapper function around std::count to count the number of times an element |
1249 | /// \p Element occurs in the given range \p Range. |
1250 | template <typename R, typename E> |
1251 | auto count(R &&Range, const E &Element) -> |
1252 | typename std::iterator_traits<decltype(adl_begin(Range))>::difference_type { |
1253 | return std::count(adl_begin(Range), adl_end(Range), Element); |
1254 | } |
1255 | |
1256 | /// Wrapper function around std::count_if to count the number of times an |
1257 | /// element satisfying a given predicate occurs in a range. |
1258 | template <typename R, typename UnaryPredicate> |
1259 | auto count_if(R &&Range, UnaryPredicate P) -> |
1260 | typename std::iterator_traits<decltype(adl_begin(Range))>::difference_type { |
1261 | return std::count_if(adl_begin(Range), adl_end(Range), P); |
1262 | } |
1263 | |
1264 | /// Wrapper function around std::transform to apply a function to a range and |
1265 | /// store the result elsewhere. |
1266 | template <typename R, typename OutputIt, typename UnaryPredicate> |
1267 | OutputIt transform(R &&Range, OutputIt d_first, UnaryPredicate P) { |
1268 | return std::transform(adl_begin(Range), adl_end(Range), d_first, P); |
1269 | } |
1270 | |
1271 | /// Provide wrappers to std::partition which take ranges instead of having to |
1272 | /// pass begin/end explicitly. |
1273 | template <typename R, typename UnaryPredicate> |
1274 | auto partition(R &&Range, UnaryPredicate P) -> decltype(adl_begin(Range)) { |
1275 | return std::partition(adl_begin(Range), adl_end(Range), P); |
1276 | } |
1277 | |
1278 | /// Provide wrappers to std::lower_bound which take ranges instead of having to |
1279 | /// pass begin/end explicitly. |
1280 | template <typename R, typename T> |
1281 | auto lower_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range)) { |
1282 | return std::lower_bound(adl_begin(Range), adl_end(Range), |
1283 | std::forward<T>(Value)); |
1284 | } |
1285 | |
1286 | template <typename R, typename T, typename Compare> |
1287 | auto lower_bound(R &&Range, T &&Value, Compare C) |
1288 | -> decltype(adl_begin(Range)) { |
1289 | return std::lower_bound(adl_begin(Range), adl_end(Range), |
1290 | std::forward<T>(Value), C); |
1291 | } |
1292 | |
1293 | /// Provide wrappers to std::upper_bound which take ranges instead of having to |
1294 | /// pass begin/end explicitly. |
1295 | template <typename R, typename T> |
1296 | auto upper_bound(R &&Range, T &&Value) -> decltype(adl_begin(Range)) { |
1297 | return std::upper_bound(adl_begin(Range), adl_end(Range), |
1298 | std::forward<T>(Value)); |
1299 | } |
1300 | |
1301 | template <typename R, typename T, typename Compare> |
1302 | auto upper_bound(R &&Range, T &&Value, Compare C) |
1303 | -> decltype(adl_begin(Range)) { |
1304 | return std::upper_bound(adl_begin(Range), adl_end(Range), |
1305 | std::forward<T>(Value), C); |
1306 | } |
1307 | |
1308 | template <typename R> |
1309 | void stable_sort(R &&Range) { |
1310 | std::stable_sort(adl_begin(Range), adl_end(Range)); |
1311 | } |
1312 | |
1313 | template <typename R, typename Compare> |
1314 | void stable_sort(R &&Range, Compare C) { |
1315 | std::stable_sort(adl_begin(Range), adl_end(Range), C); |
1316 | } |
1317 | |
1318 | /// Binary search for the first index where a predicate is true. |
1319 | /// Returns the first I in [Lo, Hi) where C(I) is true, or Hi if it never is. |
1320 | /// Requires that C is always false below some limit, and always true above it. |
1321 | /// |
1322 | /// Example: |
1323 | /// size_t DawnModernEra = bsearch(1776, 2050, [](size_t Year){ |
1324 | /// return Presidents.for(Year).twitterHandle() != None; |
1325 | /// }); |
1326 | /// |
1327 | /// Note the return value differs from std::binary_search! |
1328 | template <typename Predicate> |
1329 | size_t bsearch(size_t Lo, size_t Hi, Predicate P) { |
1330 | while (Lo != Hi) { |
1331 | assert(Hi > Lo)((Hi > Lo) ? static_cast<void> (0) : __assert_fail ( "Hi > Lo", "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 1331, __PRETTY_FUNCTION__)); |
1332 | size_t Mid = Lo + (Hi - Lo) / 2; |
1333 | if (P(Mid)) |
1334 | Hi = Mid; |
1335 | else |
1336 | Lo = Mid + 1; |
1337 | } |
1338 | return Hi; |
1339 | } |
1340 | |
1341 | /// Binary search for the first iterator where a predicate is true. |
1342 | /// Returns the first I in [Lo, Hi) where C(*I) is true, or Hi if it never is. |
1343 | /// Requires that C is always false below some limit, and always true above it. |
1344 | template <typename It, typename Predicate, |
1345 | typename Val = decltype(*std::declval<It>())> |
1346 | It bsearch(It Lo, It Hi, Predicate P) { |
1347 | return std::lower_bound(Lo, Hi, 0u, |
1348 | [&](const Val &V, unsigned) { return !P(V); }); |
1349 | } |
1350 | |
1351 | /// Binary search for the first iterator in a range where a predicate is true. |
1352 | /// Requires that C is always false below some limit, and always true above it. |
1353 | template <typename R, typename Predicate> |
1354 | auto bsearch(R &&Range, Predicate P) -> decltype(adl_begin(Range)) { |
1355 | return bsearch(adl_begin(Range), adl_end(Range), P); |
1356 | } |
1357 | |
1358 | /// Wrapper function around std::equal to detect if all elements |
1359 | /// in a container are same. |
1360 | template <typename R> |
1361 | bool is_splat(R &&Range) { |
1362 | size_t range_size = size(Range); |
1363 | return range_size != 0 && (range_size == 1 || |
1364 | std::equal(adl_begin(Range) + 1, adl_end(Range), adl_begin(Range))); |
1365 | } |
1366 | |
1367 | /// Given a range of type R, iterate the entire range and return a |
1368 | /// SmallVector with elements of the vector. This is useful, for example, |
1369 | /// when you want to iterate a range and then sort the results. |
1370 | template <unsigned Size, typename R> |
1371 | SmallVector<typename std::remove_const<detail::ValueOfRange<R>>::type, Size> |
1372 | to_vector(R &&Range) { |
1373 | return {adl_begin(Range), adl_end(Range)}; |
1374 | } |
1375 | |
1376 | /// Provide a container algorithm similar to C++ Library Fundamentals v2's |
1377 | /// `erase_if` which is equivalent to: |
1378 | /// |
1379 | /// C.erase(remove_if(C, pred), C.end()); |
1380 | /// |
1381 | /// This version works for any container with an erase method call accepting |
1382 | /// two iterators. |
1383 | template <typename Container, typename UnaryPredicate> |
1384 | void erase_if(Container &C, UnaryPredicate P) { |
1385 | C.erase(remove_if(C, P), C.end()); |
1386 | } |
1387 | |
1388 | //===----------------------------------------------------------------------===// |
1389 | // Extra additions to <memory> |
1390 | //===----------------------------------------------------------------------===// |
1391 | |
1392 | // Implement make_unique according to N3656. |
1393 | |
1394 | /// Constructs a `new T()` with the given args and returns a |
1395 | /// `unique_ptr<T>` which owns the object. |
1396 | /// |
1397 | /// Example: |
1398 | /// |
1399 | /// auto p = make_unique<int>(); |
1400 | /// auto p = make_unique<std::tuple<int, int>>(0, 1); |
1401 | template <class T, class... Args> |
1402 | typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type |
1403 | make_unique(Args &&... args) { |
1404 | return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); |
1405 | } |
1406 | |
1407 | /// Constructs a `new T[n]` with the given args and returns a |
1408 | /// `unique_ptr<T[]>` which owns the object. |
1409 | /// |
1410 | /// \param n size of the new array. |
1411 | /// |
1412 | /// Example: |
1413 | /// |
1414 | /// auto p = make_unique<int[]>(2); // value-initializes the array with 0's. |
1415 | template <class T> |
1416 | typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0, |
1417 | std::unique_ptr<T>>::type |
1418 | make_unique(size_t n) { |
1419 | return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]()); |
1420 | } |
1421 | |
1422 | /// This function isn't used and is only here to provide better compile errors. |
1423 | template <class T, class... Args> |
1424 | typename std::enable_if<std::extent<T>::value != 0>::type |
1425 | make_unique(Args &&...) = delete; |
1426 | |
1427 | struct FreeDeleter { |
1428 | void operator()(void* v) { |
1429 | ::free(v); |
1430 | } |
1431 | }; |
1432 | |
1433 | template<typename First, typename Second> |
1434 | struct pair_hash { |
1435 | size_t operator()(const std::pair<First, Second> &P) const { |
1436 | return std::hash<First>()(P.first) * 31 + std::hash<Second>()(P.second); |
1437 | } |
1438 | }; |
1439 | |
1440 | /// A functor like C++14's std::less<void> in its absence. |
1441 | struct less { |
1442 | template <typename A, typename B> bool operator()(A &&a, B &&b) const { |
1443 | return std::forward<A>(a) < std::forward<B>(b); |
1444 | } |
1445 | }; |
1446 | |
1447 | /// A functor like C++14's std::equal<void> in its absence. |
1448 | struct equal { |
1449 | template <typename A, typename B> bool operator()(A &&a, B &&b) const { |
1450 | return std::forward<A>(a) == std::forward<B>(b); |
1451 | } |
1452 | }; |
1453 | |
1454 | /// Binary functor that adapts to any other binary functor after dereferencing |
1455 | /// operands. |
1456 | template <typename T> struct deref { |
1457 | T func; |
1458 | |
1459 | // Could be further improved to cope with non-derivable functors and |
1460 | // non-binary functors (should be a variadic template member function |
1461 | // operator()). |
1462 | template <typename A, typename B> |
1463 | auto operator()(A &lhs, B &rhs) const -> decltype(func(*lhs, *rhs)) { |
1464 | assert(lhs)((lhs) ? static_cast<void> (0) : __assert_fail ("lhs", "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 1464, __PRETTY_FUNCTION__)); |
1465 | assert(rhs)((rhs) ? static_cast<void> (0) : __assert_fail ("rhs", "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 1465, __PRETTY_FUNCTION__)); |
1466 | return func(*lhs, *rhs); |
1467 | } |
1468 | }; |
1469 | |
1470 | namespace detail { |
1471 | |
1472 | template <typename R> class enumerator_iter; |
1473 | |
1474 | template <typename R> struct result_pair { |
1475 | friend class enumerator_iter<R>; |
1476 | |
1477 | result_pair() = default; |
1478 | result_pair(std::size_t Index, IterOfRange<R> Iter) |
1479 | : Index(Index), Iter(Iter) {} |
1480 | |
1481 | result_pair<R> &operator=(const result_pair<R> &Other) { |
1482 | Index = Other.Index; |
1483 | Iter = Other.Iter; |
1484 | return *this; |
1485 | } |
1486 | |
1487 | std::size_t index() const { return Index; } |
1488 | const ValueOfRange<R> &value() const { return *Iter; } |
1489 | ValueOfRange<R> &value() { return *Iter; } |
1490 | |
1491 | private: |
1492 | std::size_t Index = std::numeric_limits<std::size_t>::max(); |
1493 | IterOfRange<R> Iter; |
1494 | }; |
1495 | |
1496 | template <typename R> |
1497 | class enumerator_iter |
1498 | : public iterator_facade_base< |
1499 | enumerator_iter<R>, std::forward_iterator_tag, result_pair<R>, |
1500 | typename std::iterator_traits<IterOfRange<R>>::difference_type, |
1501 | typename std::iterator_traits<IterOfRange<R>>::pointer, |
1502 | typename std::iterator_traits<IterOfRange<R>>::reference> { |
1503 | using result_type = result_pair<R>; |
1504 | |
1505 | public: |
1506 | explicit enumerator_iter(IterOfRange<R> EndIter) |
1507 | : Result(std::numeric_limits<size_t>::max(), EndIter) {} |
1508 | |
1509 | enumerator_iter(std::size_t Index, IterOfRange<R> Iter) |
1510 | : Result(Index, Iter) {} |
1511 | |
1512 | result_type &operator*() { return Result; } |
1513 | const result_type &operator*() const { return Result; } |
1514 | |
1515 | enumerator_iter<R> &operator++() { |
1516 | assert(Result.Index != std::numeric_limits<size_t>::max())((Result.Index != std::numeric_limits<size_t>::max()) ? static_cast<void> (0) : __assert_fail ("Result.Index != std::numeric_limits<size_t>::max()" , "/build/llvm-toolchain-snapshot-9~svn362543/include/llvm/ADT/STLExtras.h" , 1516, __PRETTY_FUNCTION__)); |
1517 | ++Result.Iter; |
1518 | ++Result.Index; |
1519 | return *this; |
1520 | } |
1521 | |
1522 | bool operator==(const enumerator_iter<R> &RHS) const { |
1523 | // Don't compare indices here, only iterators. It's possible for an end |
1524 | // iterator to have different indices depending on whether it was created |
1525 | // by calling std::end() versus incrementing a valid iterator. |
1526 | return Result.Iter == RHS.Result.Iter; |
1527 | } |
1528 | |
1529 | enumerator_iter<R> &operator=(const enumerator_iter<R> &Other) { |
1530 | Result = Other.Result; |
1531 | return *this; |
1532 | } |
1533 | |
1534 | private: |
1535 | result_type Result; |
1536 | }; |
1537 | |
1538 | template <typename R> class enumerator { |
1539 | public: |
1540 | explicit enumerator(R &&Range) : TheRange(std::forward<R>(Range)) {} |
1541 | |
1542 | enumerator_iter<R> begin() { |
1543 | return enumerator_iter<R>(0, std::begin(TheRange)); |
1544 | } |
1545 | |
1546 | enumerator_iter<R> end() { |
1547 | return enumerator_iter<R>(std::end(TheRange)); |
1548 | } |
1549 | |
1550 | private: |
1551 | R TheRange; |
1552 | }; |
1553 | |
1554 | } // end namespace detail |
1555 | |
1556 | /// Given an input range, returns a new range whose values are are pair (A,B) |
1557 | /// such that A is the 0-based index of the item in the sequence, and B is |
1558 | /// the value from the original sequence. Example: |
1559 | /// |
1560 | /// std::vector<char> Items = {'A', 'B', 'C', 'D'}; |
1561 | /// for (auto X : enumerate(Items)) { |
1562 | /// printf("Item %d - %c\n", X.index(), X.value()); |
1563 | /// } |
1564 | /// |
1565 | /// Output: |
1566 | /// Item 0 - A |
1567 | /// Item 1 - B |
1568 | /// Item 2 - C |
1569 | /// Item 3 - D |
1570 | /// |
1571 | template <typename R> detail::enumerator<R> enumerate(R &&TheRange) { |
1572 | return detail::enumerator<R>(std::forward<R>(TheRange)); |
1573 | } |
1574 | |
1575 | namespace detail { |
1576 | |
1577 | template <typename F, typename Tuple, std::size_t... I> |
1578 | auto apply_tuple_impl(F &&f, Tuple &&t, index_sequence<I...>) |
1579 | -> decltype(std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...)) { |
1580 | return std::forward<F>(f)(std::get<I>(std::forward<Tuple>(t))...); |
1581 | } |
1582 | |
1583 | } // end namespace detail |
1584 | |
1585 | /// Given an input tuple (a1, a2, ..., an), pass the arguments of the |
1586 | /// tuple variadically to f as if by calling f(a1, a2, ..., an) and |
1587 | /// return the result. |
1588 | template <typename F, typename Tuple> |
1589 | auto apply_tuple(F &&f, Tuple &&t) -> decltype(detail::apply_tuple_impl( |
1590 | std::forward<F>(f), std::forward<Tuple>(t), |
1591 | build_index_impl< |
1592 | std::tuple_size<typename std::decay<Tuple>::type>::value>{})) { |
1593 | using Indices = build_index_impl< |
1594 | std::tuple_size<typename std::decay<Tuple>::type>::value>; |
1595 | |
1596 | return detail::apply_tuple_impl(std::forward<F>(f), std::forward<Tuple>(t), |
1597 | Indices{}); |
1598 | } |
1599 | |
1600 | /// Return true if the sequence [Begin, End) has exactly N items. Runs in O(N) |
1601 | /// time. Not meant for use with random-access iterators. |
1602 | template <typename IterTy> |
1603 | bool hasNItems( |
1604 | IterTy &&Begin, IterTy &&End, unsigned N, |
1605 | typename std::enable_if< |
1606 | !std::is_same< |
1607 | typename std::iterator_traits<typename std::remove_reference< |
1608 | decltype(Begin)>::type>::iterator_category, |
1609 | std::random_access_iterator_tag>::value, |
1610 | void>::type * = nullptr) { |
1611 | for (; N; --N, ++Begin) |
1612 | if (Begin == End) |
1613 | return false; // Too few. |
1614 | return Begin == End; |
1615 | } |
1616 | |
1617 | /// Return true if the sequence [Begin, End) has N or more items. Runs in O(N) |
1618 | /// time. Not meant for use with random-access iterators. |
1619 | template <typename IterTy> |
1620 | bool hasNItemsOrMore( |
1621 | IterTy &&Begin, IterTy &&End, unsigned N, |
1622 | typename std::enable_if< |
1623 | !std::is_same< |
1624 | typename std::iterator_traits<typename std::remove_reference< |
1625 | decltype(Begin)>::type>::iterator_category, |
1626 | std::random_access_iterator_tag>::value, |
1627 | void>::type * = nullptr) { |
1628 | for (; N; --N, ++Begin) |
1629 | if (Begin == End) |
1630 | return false; // Too few. |
1631 | return true; |
1632 | } |
1633 | |
1634 | /// Returns a raw pointer that represents the same address as the argument. |
1635 | /// |
1636 | /// The late bound return should be removed once we move to C++14 to better |
1637 | /// align with the C++20 declaration. Also, this implementation can be removed |
1638 | /// once we move to C++20 where it's defined as std::to_addres() |
1639 | /// |
1640 | /// The std::pointer_traits<>::to_address(p) variations of these overloads has |
1641 | /// not been implemented. |
1642 | template <class Ptr> auto to_address(const Ptr &P) -> decltype(P.operator->()) { |
1643 | return P.operator->(); |
1644 | } |
1645 | template <class T> constexpr T *to_address(T *P) { return P; } |
1646 | |
1647 | } // end namespace llvm |
1648 | |
1649 | #endif // LLVM_ADT_STLEXTRAS_H |