File: | llvm/lib/Target/AMDGPU/AMDGPUMachineCFGStructurizer.cpp |
Warning: | line 2276, column 27 Called C++ object pointer is null |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
1 | //===- AMDGPUMachineCFGStructurizer.cpp - Machine code if conversion pass. ===// | ||||
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 implements the machine instruction level CFG structurizer pass. | ||||
10 | // | ||||
11 | //===----------------------------------------------------------------------===// | ||||
12 | |||||
13 | #include "AMDGPU.h" | ||||
14 | #include "GCNSubtarget.h" | ||||
15 | #include "llvm/ADT/DenseSet.h" | ||||
16 | #include "llvm/ADT/PostOrderIterator.h" | ||||
17 | #include "llvm/ADT/SetVector.h" | ||||
18 | #include "llvm/ADT/SmallPtrSet.h" | ||||
19 | #include "llvm/CodeGen/MachineBasicBlock.h" | ||||
20 | #include "llvm/CodeGen/MachineInstrBuilder.h" | ||||
21 | #include "llvm/CodeGen/MachineRegionInfo.h" | ||||
22 | #include "llvm/CodeGen/MachineRegisterInfo.h" | ||||
23 | #include "llvm/CodeGen/TargetRegisterInfo.h" | ||||
24 | #include "llvm/InitializePasses.h" | ||||
25 | |||||
26 | using namespace llvm; | ||||
27 | |||||
28 | #define DEBUG_TYPE"amdgpucfgstructurizer" "amdgpucfgstructurizer" | ||||
29 | |||||
30 | namespace { | ||||
31 | |||||
32 | class PHILinearizeDestIterator; | ||||
33 | |||||
34 | class PHILinearize { | ||||
35 | friend class PHILinearizeDestIterator; | ||||
36 | |||||
37 | public: | ||||
38 | using PHISourceT = std::pair<unsigned, MachineBasicBlock *>; | ||||
39 | |||||
40 | private: | ||||
41 | using PHISourcesT = DenseSet<PHISourceT>; | ||||
42 | using PHIInfoElementT = struct { | ||||
43 | unsigned DestReg; | ||||
44 | DebugLoc DL; | ||||
45 | PHISourcesT Sources; | ||||
46 | }; | ||||
47 | using PHIInfoT = SmallPtrSet<PHIInfoElementT *, 2>; | ||||
48 | PHIInfoT PHIInfo; | ||||
49 | |||||
50 | static unsigned phiInfoElementGetDest(PHIInfoElementT *Info); | ||||
51 | static void phiInfoElementSetDef(PHIInfoElementT *Info, unsigned NewDef); | ||||
52 | static PHISourcesT &phiInfoElementGetSources(PHIInfoElementT *Info); | ||||
53 | static void phiInfoElementAddSource(PHIInfoElementT *Info, unsigned SourceReg, | ||||
54 | MachineBasicBlock *SourceMBB); | ||||
55 | static void phiInfoElementRemoveSource(PHIInfoElementT *Info, | ||||
56 | unsigned SourceReg, | ||||
57 | MachineBasicBlock *SourceMBB); | ||||
58 | PHIInfoElementT *findPHIInfoElement(unsigned DestReg); | ||||
59 | PHIInfoElementT *findPHIInfoElementFromSource(unsigned SourceReg, | ||||
60 | MachineBasicBlock *SourceMBB); | ||||
61 | |||||
62 | public: | ||||
63 | bool findSourcesFromMBB(MachineBasicBlock *SourceMBB, | ||||
64 | SmallVector<unsigned, 4> &Sources); | ||||
65 | void addDest(unsigned DestReg, const DebugLoc &DL); | ||||
66 | void replaceDef(unsigned OldDestReg, unsigned NewDestReg); | ||||
67 | void deleteDef(unsigned DestReg); | ||||
68 | void addSource(unsigned DestReg, unsigned SourceReg, | ||||
69 | MachineBasicBlock *SourceMBB); | ||||
70 | void removeSource(unsigned DestReg, unsigned SourceReg, | ||||
71 | MachineBasicBlock *SourceMBB = nullptr); | ||||
72 | bool findDest(unsigned SourceReg, MachineBasicBlock *SourceMBB, | ||||
73 | unsigned &DestReg); | ||||
74 | bool isSource(unsigned Reg, MachineBasicBlock *SourceMBB = nullptr); | ||||
75 | unsigned getNumSources(unsigned DestReg); | ||||
76 | void dump(MachineRegisterInfo *MRI); | ||||
77 | void clear(); | ||||
78 | |||||
79 | using source_iterator = PHISourcesT::iterator; | ||||
80 | using dest_iterator = PHILinearizeDestIterator; | ||||
81 | |||||
82 | dest_iterator dests_begin(); | ||||
83 | dest_iterator dests_end(); | ||||
84 | |||||
85 | source_iterator sources_begin(unsigned Reg); | ||||
86 | source_iterator sources_end(unsigned Reg); | ||||
87 | }; | ||||
88 | |||||
89 | class PHILinearizeDestIterator { | ||||
90 | private: | ||||
91 | PHILinearize::PHIInfoT::iterator Iter; | ||||
92 | |||||
93 | public: | ||||
94 | PHILinearizeDestIterator(PHILinearize::PHIInfoT::iterator I) : Iter(I) {} | ||||
95 | |||||
96 | unsigned operator*() { return PHILinearize::phiInfoElementGetDest(*Iter); } | ||||
97 | PHILinearizeDestIterator &operator++() { | ||||
98 | ++Iter; | ||||
99 | return *this; | ||||
100 | } | ||||
101 | bool operator==(const PHILinearizeDestIterator &I) const { | ||||
102 | return I.Iter == Iter; | ||||
103 | } | ||||
104 | bool operator!=(const PHILinearizeDestIterator &I) const { | ||||
105 | return I.Iter != Iter; | ||||
106 | } | ||||
107 | }; | ||||
108 | |||||
109 | } // end anonymous namespace | ||||
110 | |||||
111 | unsigned PHILinearize::phiInfoElementGetDest(PHIInfoElementT *Info) { | ||||
112 | return Info->DestReg; | ||||
113 | } | ||||
114 | |||||
115 | void PHILinearize::phiInfoElementSetDef(PHIInfoElementT *Info, | ||||
116 | unsigned NewDef) { | ||||
117 | Info->DestReg = NewDef; | ||||
118 | } | ||||
119 | |||||
120 | PHILinearize::PHISourcesT & | ||||
121 | PHILinearize::phiInfoElementGetSources(PHIInfoElementT *Info) { | ||||
122 | return Info->Sources; | ||||
123 | } | ||||
124 | |||||
125 | void PHILinearize::phiInfoElementAddSource(PHIInfoElementT *Info, | ||||
126 | unsigned SourceReg, | ||||
127 | MachineBasicBlock *SourceMBB) { | ||||
128 | // Assertion ensures we don't use the same SourceMBB for the | ||||
129 | // sources, because we cannot have different registers with | ||||
130 | // identical predecessors, but we can have the same register for | ||||
131 | // multiple predecessors. | ||||
132 | #if !defined(NDEBUG1) | ||||
133 | for (auto SI : phiInfoElementGetSources(Info)) { | ||||
134 | assert((SI.second != SourceMBB || SourceReg == SI.first))(static_cast<void> (0)); | ||||
135 | } | ||||
136 | #endif | ||||
137 | |||||
138 | phiInfoElementGetSources(Info).insert(PHISourceT(SourceReg, SourceMBB)); | ||||
139 | } | ||||
140 | |||||
141 | void PHILinearize::phiInfoElementRemoveSource(PHIInfoElementT *Info, | ||||
142 | unsigned SourceReg, | ||||
143 | MachineBasicBlock *SourceMBB) { | ||||
144 | auto &Sources = phiInfoElementGetSources(Info); | ||||
145 | SmallVector<PHISourceT, 4> ElimiatedSources; | ||||
146 | for (auto SI : Sources) { | ||||
147 | if (SI.first == SourceReg && | ||||
148 | (SI.second == nullptr || SI.second == SourceMBB)) { | ||||
149 | ElimiatedSources.push_back(PHISourceT(SI.first, SI.second)); | ||||
150 | } | ||||
151 | } | ||||
152 | |||||
153 | for (auto &Source : ElimiatedSources) { | ||||
154 | Sources.erase(Source); | ||||
155 | } | ||||
156 | } | ||||
157 | |||||
158 | PHILinearize::PHIInfoElementT * | ||||
159 | PHILinearize::findPHIInfoElement(unsigned DestReg) { | ||||
160 | for (auto I : PHIInfo) { | ||||
161 | if (phiInfoElementGetDest(I) == DestReg) { | ||||
162 | return I; | ||||
163 | } | ||||
164 | } | ||||
165 | return nullptr; | ||||
166 | } | ||||
167 | |||||
168 | PHILinearize::PHIInfoElementT * | ||||
169 | PHILinearize::findPHIInfoElementFromSource(unsigned SourceReg, | ||||
170 | MachineBasicBlock *SourceMBB) { | ||||
171 | for (auto I : PHIInfo) { | ||||
172 | for (auto SI : phiInfoElementGetSources(I)) { | ||||
173 | if (SI.first == SourceReg && | ||||
174 | (SI.second == nullptr || SI.second == SourceMBB)) { | ||||
175 | return I; | ||||
176 | } | ||||
177 | } | ||||
178 | } | ||||
179 | return nullptr; | ||||
180 | } | ||||
181 | |||||
182 | bool PHILinearize::findSourcesFromMBB(MachineBasicBlock *SourceMBB, | ||||
183 | SmallVector<unsigned, 4> &Sources) { | ||||
184 | bool FoundSource = false; | ||||
185 | for (auto I : PHIInfo) { | ||||
186 | for (auto SI : phiInfoElementGetSources(I)) { | ||||
187 | if (SI.second == SourceMBB) { | ||||
188 | FoundSource = true; | ||||
189 | Sources.push_back(SI.first); | ||||
190 | } | ||||
191 | } | ||||
192 | } | ||||
193 | return FoundSource; | ||||
194 | } | ||||
195 | |||||
196 | void PHILinearize::addDest(unsigned DestReg, const DebugLoc &DL) { | ||||
197 | assert(findPHIInfoElement(DestReg) == nullptr && "Dest already exsists")(static_cast<void> (0)); | ||||
198 | PHISourcesT EmptySet; | ||||
199 | PHIInfoElementT *NewElement = new PHIInfoElementT(); | ||||
200 | NewElement->DestReg = DestReg; | ||||
201 | NewElement->DL = DL; | ||||
202 | NewElement->Sources = EmptySet; | ||||
203 | PHIInfo.insert(NewElement); | ||||
204 | } | ||||
205 | |||||
206 | void PHILinearize::replaceDef(unsigned OldDestReg, unsigned NewDestReg) { | ||||
207 | phiInfoElementSetDef(findPHIInfoElement(OldDestReg), NewDestReg); | ||||
208 | } | ||||
209 | |||||
210 | void PHILinearize::deleteDef(unsigned DestReg) { | ||||
211 | PHIInfoElementT *InfoElement = findPHIInfoElement(DestReg); | ||||
212 | PHIInfo.erase(InfoElement); | ||||
213 | delete InfoElement; | ||||
214 | } | ||||
215 | |||||
216 | void PHILinearize::addSource(unsigned DestReg, unsigned SourceReg, | ||||
217 | MachineBasicBlock *SourceMBB) { | ||||
218 | phiInfoElementAddSource(findPHIInfoElement(DestReg), SourceReg, SourceMBB); | ||||
219 | } | ||||
220 | |||||
221 | void PHILinearize::removeSource(unsigned DestReg, unsigned SourceReg, | ||||
222 | MachineBasicBlock *SourceMBB) { | ||||
223 | phiInfoElementRemoveSource(findPHIInfoElement(DestReg), SourceReg, SourceMBB); | ||||
224 | } | ||||
225 | |||||
226 | bool PHILinearize::findDest(unsigned SourceReg, MachineBasicBlock *SourceMBB, | ||||
227 | unsigned &DestReg) { | ||||
228 | PHIInfoElementT *InfoElement = | ||||
229 | findPHIInfoElementFromSource(SourceReg, SourceMBB); | ||||
230 | if (InfoElement != nullptr) { | ||||
231 | DestReg = phiInfoElementGetDest(InfoElement); | ||||
232 | return true; | ||||
233 | } | ||||
234 | return false; | ||||
235 | } | ||||
236 | |||||
237 | bool PHILinearize::isSource(unsigned Reg, MachineBasicBlock *SourceMBB) { | ||||
238 | unsigned DestReg; | ||||
239 | return findDest(Reg, SourceMBB, DestReg); | ||||
240 | } | ||||
241 | |||||
242 | unsigned PHILinearize::getNumSources(unsigned DestReg) { | ||||
243 | return phiInfoElementGetSources(findPHIInfoElement(DestReg)).size(); | ||||
244 | } | ||||
245 | |||||
246 | #if !defined(NDEBUG1) || defined(LLVM_ENABLE_DUMP) | ||||
247 | LLVM_DUMP_METHOD__attribute__((noinline)) __attribute__((__used__)) void PHILinearize::dump(MachineRegisterInfo *MRI) { | ||||
248 | const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo(); | ||||
249 | dbgs() << "=PHIInfo Start=\n"; | ||||
250 | for (auto PII : this->PHIInfo) { | ||||
251 | PHIInfoElementT &Element = *PII; | ||||
252 | dbgs() << "Dest: " << printReg(Element.DestReg, TRI) | ||||
253 | << " Sources: {"; | ||||
254 | for (auto &SI : Element.Sources) { | ||||
255 | dbgs() << printReg(SI.first, TRI) << '(' << printMBBReference(*SI.second) | ||||
256 | << "),"; | ||||
257 | } | ||||
258 | dbgs() << "}\n"; | ||||
259 | } | ||||
260 | dbgs() << "=PHIInfo End=\n"; | ||||
261 | } | ||||
262 | #endif | ||||
263 | |||||
264 | void PHILinearize::clear() { PHIInfo = PHIInfoT(); } | ||||
265 | |||||
266 | PHILinearize::dest_iterator PHILinearize::dests_begin() { | ||||
267 | return PHILinearizeDestIterator(PHIInfo.begin()); | ||||
268 | } | ||||
269 | |||||
270 | PHILinearize::dest_iterator PHILinearize::dests_end() { | ||||
271 | return PHILinearizeDestIterator(PHIInfo.end()); | ||||
272 | } | ||||
273 | |||||
274 | PHILinearize::source_iterator PHILinearize::sources_begin(unsigned Reg) { | ||||
275 | auto InfoElement = findPHIInfoElement(Reg); | ||||
276 | return phiInfoElementGetSources(InfoElement).begin(); | ||||
277 | } | ||||
278 | |||||
279 | PHILinearize::source_iterator PHILinearize::sources_end(unsigned Reg) { | ||||
280 | auto InfoElement = findPHIInfoElement(Reg); | ||||
281 | return phiInfoElementGetSources(InfoElement).end(); | ||||
282 | } | ||||
283 | |||||
284 | static unsigned getPHINumInputs(MachineInstr &PHI) { | ||||
285 | assert(PHI.isPHI())(static_cast<void> (0)); | ||||
286 | return (PHI.getNumOperands() - 1) / 2; | ||||
287 | } | ||||
288 | |||||
289 | static MachineBasicBlock *getPHIPred(MachineInstr &PHI, unsigned Index) { | ||||
290 | assert(PHI.isPHI())(static_cast<void> (0)); | ||||
291 | return PHI.getOperand(Index * 2 + 2).getMBB(); | ||||
292 | } | ||||
293 | |||||
294 | static void setPhiPred(MachineInstr &PHI, unsigned Index, | ||||
295 | MachineBasicBlock *NewPred) { | ||||
296 | PHI.getOperand(Index * 2 + 2).setMBB(NewPred); | ||||
297 | } | ||||
298 | |||||
299 | static unsigned getPHISourceReg(MachineInstr &PHI, unsigned Index) { | ||||
300 | assert(PHI.isPHI())(static_cast<void> (0)); | ||||
301 | return PHI.getOperand(Index * 2 + 1).getReg(); | ||||
302 | } | ||||
303 | |||||
304 | static unsigned getPHIDestReg(MachineInstr &PHI) { | ||||
305 | assert(PHI.isPHI())(static_cast<void> (0)); | ||||
306 | return PHI.getOperand(0).getReg(); | ||||
307 | } | ||||
308 | |||||
309 | namespace { | ||||
310 | |||||
311 | class RegionMRT; | ||||
312 | class MBBMRT; | ||||
313 | |||||
314 | class LinearizedRegion { | ||||
315 | protected: | ||||
316 | MachineBasicBlock *Entry; | ||||
317 | // The exit block is part of the region, and is the last | ||||
318 | // merge block before exiting the region. | ||||
319 | MachineBasicBlock *Exit; | ||||
320 | DenseSet<unsigned> LiveOuts; | ||||
321 | SmallPtrSet<MachineBasicBlock *, 1> MBBs; | ||||
322 | bool HasLoop; | ||||
323 | LinearizedRegion *Parent; | ||||
324 | RegionMRT *RMRT; | ||||
325 | |||||
326 | void storeLiveOutReg(MachineBasicBlock *MBB, Register Reg, | ||||
327 | MachineInstr *DefInstr, const MachineRegisterInfo *MRI, | ||||
328 | const TargetRegisterInfo *TRI, PHILinearize &PHIInfo); | ||||
329 | |||||
330 | void storeLiveOutRegRegion(RegionMRT *Region, Register Reg, | ||||
331 | MachineInstr *DefInstr, | ||||
332 | const MachineRegisterInfo *MRI, | ||||
333 | const TargetRegisterInfo *TRI, | ||||
334 | PHILinearize &PHIInfo); | ||||
335 | |||||
336 | void storeMBBLiveOuts(MachineBasicBlock *MBB, const MachineRegisterInfo *MRI, | ||||
337 | const TargetRegisterInfo *TRI, PHILinearize &PHIInfo, | ||||
338 | RegionMRT *TopRegion); | ||||
339 | |||||
340 | void storeLiveOuts(MachineBasicBlock *MBB, const MachineRegisterInfo *MRI, | ||||
341 | const TargetRegisterInfo *TRI, PHILinearize &PHIInfo); | ||||
342 | |||||
343 | void storeLiveOuts(RegionMRT *Region, const MachineRegisterInfo *MRI, | ||||
344 | const TargetRegisterInfo *TRI, PHILinearize &PHIInfo, | ||||
345 | RegionMRT *TopRegion = nullptr); | ||||
346 | |||||
347 | public: | ||||
348 | LinearizedRegion(); | ||||
349 | LinearizedRegion(MachineBasicBlock *MBB, const MachineRegisterInfo *MRI, | ||||
350 | const TargetRegisterInfo *TRI, PHILinearize &PHIInfo); | ||||
351 | ~LinearizedRegion() = default; | ||||
352 | |||||
353 | void setRegionMRT(RegionMRT *Region) { RMRT = Region; } | ||||
354 | |||||
355 | RegionMRT *getRegionMRT() { return RMRT; } | ||||
356 | |||||
357 | void setParent(LinearizedRegion *P) { Parent = P; } | ||||
358 | |||||
359 | LinearizedRegion *getParent() { return Parent; } | ||||
360 | |||||
361 | void print(raw_ostream &OS, const TargetRegisterInfo *TRI = nullptr); | ||||
362 | |||||
363 | void setBBSelectRegIn(unsigned Reg); | ||||
364 | |||||
365 | unsigned getBBSelectRegIn(); | ||||
366 | |||||
367 | void setBBSelectRegOut(unsigned Reg, bool IsLiveOut); | ||||
368 | |||||
369 | unsigned getBBSelectRegOut(); | ||||
370 | |||||
371 | void setHasLoop(bool Value); | ||||
372 | |||||
373 | bool getHasLoop(); | ||||
374 | |||||
375 | void addLiveOut(unsigned VReg); | ||||
376 | |||||
377 | void removeLiveOut(unsigned Reg); | ||||
378 | |||||
379 | void replaceLiveOut(unsigned OldReg, unsigned NewReg); | ||||
380 | |||||
381 | void replaceRegister(unsigned Register, class Register NewRegister, | ||||
382 | MachineRegisterInfo *MRI, bool ReplaceInside, | ||||
383 | bool ReplaceOutside, bool IncludeLoopPHIs); | ||||
384 | |||||
385 | void replaceRegisterInsideRegion(unsigned Register, unsigned NewRegister, | ||||
386 | bool IncludeLoopPHIs, | ||||
387 | MachineRegisterInfo *MRI); | ||||
388 | |||||
389 | void replaceRegisterOutsideRegion(unsigned Register, unsigned NewRegister, | ||||
390 | bool IncludeLoopPHIs, | ||||
391 | MachineRegisterInfo *MRI); | ||||
392 | |||||
393 | DenseSet<unsigned> *getLiveOuts(); | ||||
394 | |||||
395 | void setEntry(MachineBasicBlock *NewEntry); | ||||
396 | |||||
397 | MachineBasicBlock *getEntry(); | ||||
398 | |||||
399 | void setExit(MachineBasicBlock *NewExit); | ||||
400 | |||||
401 | MachineBasicBlock *getExit(); | ||||
402 | |||||
403 | void addMBB(MachineBasicBlock *MBB); | ||||
404 | |||||
405 | void addMBBs(LinearizedRegion *InnerRegion); | ||||
406 | |||||
407 | bool contains(MachineBasicBlock *MBB); | ||||
408 | |||||
409 | bool isLiveOut(unsigned Reg); | ||||
410 | |||||
411 | bool hasNoDef(unsigned Reg, MachineRegisterInfo *MRI); | ||||
412 | |||||
413 | void removeFalseRegisterKills(MachineRegisterInfo *MRI); | ||||
414 | |||||
415 | void initLiveOut(RegionMRT *Region, const MachineRegisterInfo *MRI, | ||||
416 | const TargetRegisterInfo *TRI, PHILinearize &PHIInfo); | ||||
417 | }; | ||||
418 | |||||
419 | class MRT { | ||||
420 | protected: | ||||
421 | RegionMRT *Parent; | ||||
422 | unsigned BBSelectRegIn; | ||||
423 | unsigned BBSelectRegOut; | ||||
424 | |||||
425 | public: | ||||
426 | virtual ~MRT() = default; | ||||
427 | |||||
428 | unsigned getBBSelectRegIn() { return BBSelectRegIn; } | ||||
429 | |||||
430 | unsigned getBBSelectRegOut() { return BBSelectRegOut; } | ||||
431 | |||||
432 | void setBBSelectRegIn(unsigned Reg) { BBSelectRegIn = Reg; } | ||||
433 | |||||
434 | void setBBSelectRegOut(unsigned Reg) { BBSelectRegOut = Reg; } | ||||
435 | |||||
436 | virtual RegionMRT *getRegionMRT() { return nullptr; } | ||||
437 | |||||
438 | virtual MBBMRT *getMBBMRT() { return nullptr; } | ||||
439 | |||||
440 | bool isRegion() { return getRegionMRT() != nullptr; } | ||||
441 | |||||
442 | bool isMBB() { return getMBBMRT() != nullptr; } | ||||
443 | |||||
444 | bool isRoot() { return Parent == nullptr; } | ||||
445 | |||||
446 | void setParent(RegionMRT *Region) { Parent = Region; } | ||||
447 | |||||
448 | RegionMRT *getParent() { return Parent; } | ||||
449 | |||||
450 | static MachineBasicBlock * | ||||
451 | initializeMRT(MachineFunction &MF, const MachineRegionInfo *RegionInfo, | ||||
452 | DenseMap<MachineRegion *, RegionMRT *> &RegionMap); | ||||
453 | |||||
454 | static RegionMRT *buildMRT(MachineFunction &MF, | ||||
455 | const MachineRegionInfo *RegionInfo, | ||||
456 | const SIInstrInfo *TII, | ||||
457 | MachineRegisterInfo *MRI); | ||||
458 | |||||
459 | virtual void dump(const TargetRegisterInfo *TRI, int depth = 0) = 0; | ||||
460 | |||||
461 | void dumpDepth(int depth) { | ||||
462 | for (int i = depth; i > 0; --i) { | ||||
463 | dbgs() << " "; | ||||
464 | } | ||||
465 | } | ||||
466 | }; | ||||
467 | |||||
468 | class MBBMRT : public MRT { | ||||
469 | MachineBasicBlock *MBB; | ||||
470 | |||||
471 | public: | ||||
472 | MBBMRT(MachineBasicBlock *BB) : MBB(BB) { | ||||
473 | setParent(nullptr); | ||||
474 | setBBSelectRegOut(0); | ||||
475 | setBBSelectRegIn(0); | ||||
476 | } | ||||
477 | |||||
478 | MBBMRT *getMBBMRT() override { return this; } | ||||
479 | |||||
480 | MachineBasicBlock *getMBB() { return MBB; } | ||||
481 | |||||
482 | void dump(const TargetRegisterInfo *TRI, int depth = 0) override { | ||||
483 | dumpDepth(depth); | ||||
484 | dbgs() << "MBB: " << getMBB()->getNumber(); | ||||
485 | dbgs() << " In: " << printReg(getBBSelectRegIn(), TRI); | ||||
486 | dbgs() << ", Out: " << printReg(getBBSelectRegOut(), TRI) << "\n"; | ||||
487 | } | ||||
488 | }; | ||||
489 | |||||
490 | class RegionMRT : public MRT { | ||||
491 | protected: | ||||
492 | MachineRegion *Region; | ||||
493 | LinearizedRegion *LRegion = nullptr; | ||||
494 | MachineBasicBlock *Succ = nullptr; | ||||
495 | SetVector<MRT *> Children; | ||||
496 | |||||
497 | public: | ||||
498 | RegionMRT(MachineRegion *MachineRegion) : Region(MachineRegion) { | ||||
499 | setParent(nullptr); | ||||
500 | setBBSelectRegOut(0); | ||||
501 | setBBSelectRegIn(0); | ||||
502 | } | ||||
503 | |||||
504 | ~RegionMRT() override { | ||||
505 | if (LRegion) { | ||||
506 | delete LRegion; | ||||
507 | } | ||||
508 | |||||
509 | for (auto CI : Children) { | ||||
510 | delete &(*CI); | ||||
511 | } | ||||
512 | } | ||||
513 | |||||
514 | RegionMRT *getRegionMRT() override { return this; } | ||||
515 | |||||
516 | void setLinearizedRegion(LinearizedRegion *LinearizeRegion) { | ||||
517 | LRegion = LinearizeRegion; | ||||
518 | } | ||||
519 | |||||
520 | LinearizedRegion *getLinearizedRegion() { return LRegion; } | ||||
521 | |||||
522 | MachineRegion *getMachineRegion() { return Region; } | ||||
523 | |||||
524 | unsigned getInnerOutputRegister() { | ||||
525 | return (*(Children.begin()))->getBBSelectRegOut(); | ||||
526 | } | ||||
527 | |||||
528 | void addChild(MRT *Tree) { Children.insert(Tree); } | ||||
529 | |||||
530 | SetVector<MRT *> *getChildren() { return &Children; } | ||||
531 | |||||
532 | void dump(const TargetRegisterInfo *TRI, int depth = 0) override { | ||||
533 | dumpDepth(depth); | ||||
534 | dbgs() << "Region: " << (void *)Region; | ||||
535 | dbgs() << " In: " << printReg(getBBSelectRegIn(), TRI); | ||||
536 | dbgs() << ", Out: " << printReg(getBBSelectRegOut(), TRI) << "\n"; | ||||
537 | |||||
538 | dumpDepth(depth); | ||||
539 | if (getSucc()) | ||||
540 | dbgs() << "Succ: " << getSucc()->getNumber() << "\n"; | ||||
541 | else | ||||
542 | dbgs() << "Succ: none \n"; | ||||
543 | for (auto MRTI : Children) { | ||||
544 | MRTI->dump(TRI, depth + 1); | ||||
545 | } | ||||
546 | } | ||||
547 | |||||
548 | MRT *getEntryTree() { return Children.back(); } | ||||
549 | |||||
550 | MRT *getExitTree() { return Children.front(); } | ||||
551 | |||||
552 | MachineBasicBlock *getEntry() { | ||||
553 | MRT *Tree = Children.back(); | ||||
554 | return (Tree->isRegion()) ? Tree->getRegionMRT()->getEntry() | ||||
555 | : Tree->getMBBMRT()->getMBB(); | ||||
556 | } | ||||
557 | |||||
558 | MachineBasicBlock *getExit() { | ||||
559 | MRT *Tree = Children.front(); | ||||
560 | return (Tree->isRegion()) ? Tree->getRegionMRT()->getExit() | ||||
561 | : Tree->getMBBMRT()->getMBB(); | ||||
562 | } | ||||
563 | |||||
564 | void setSucc(MachineBasicBlock *MBB) { Succ = MBB; } | ||||
565 | |||||
566 | MachineBasicBlock *getSucc() { return Succ; } | ||||
567 | |||||
568 | bool contains(MachineBasicBlock *MBB) { | ||||
569 | for (auto CI : Children) { | ||||
570 | if (CI->isMBB()) { | ||||
571 | if (MBB == CI->getMBBMRT()->getMBB()) { | ||||
572 | return true; | ||||
573 | } | ||||
574 | } else { | ||||
575 | if (CI->getRegionMRT()->contains(MBB)) { | ||||
576 | return true; | ||||
577 | } else if (CI->getRegionMRT()->getLinearizedRegion() != nullptr && | ||||
578 | CI->getRegionMRT()->getLinearizedRegion()->contains(MBB)) { | ||||
579 | return true; | ||||
580 | } | ||||
581 | } | ||||
582 | } | ||||
583 | return false; | ||||
584 | } | ||||
585 | |||||
586 | void replaceLiveOutReg(unsigned Register, unsigned NewRegister) { | ||||
587 | LinearizedRegion *LRegion = getLinearizedRegion(); | ||||
588 | LRegion->replaceLiveOut(Register, NewRegister); | ||||
589 | for (auto &CI : Children) { | ||||
590 | if (CI->isRegion()) { | ||||
591 | CI->getRegionMRT()->replaceLiveOutReg(Register, NewRegister); | ||||
592 | } | ||||
593 | } | ||||
594 | } | ||||
595 | }; | ||||
596 | |||||
597 | } // end anonymous namespace | ||||
598 | |||||
599 | static unsigned createBBSelectReg(const SIInstrInfo *TII, | ||||
600 | MachineRegisterInfo *MRI) { | ||||
601 | return MRI->createVirtualRegister(TII->getPreferredSelectRegClass(32)); | ||||
602 | } | ||||
603 | |||||
604 | MachineBasicBlock * | ||||
605 | MRT::initializeMRT(MachineFunction &MF, const MachineRegionInfo *RegionInfo, | ||||
606 | DenseMap<MachineRegion *, RegionMRT *> &RegionMap) { | ||||
607 | for (auto &MFI : MF) { | ||||
608 | MachineBasicBlock *ExitMBB = &MFI; | ||||
609 | if (ExitMBB->succ_size() == 0) { | ||||
610 | return ExitMBB; | ||||
611 | } | ||||
612 | } | ||||
613 | llvm_unreachable("CFG has no exit block")__builtin_unreachable(); | ||||
614 | return nullptr; | ||||
615 | } | ||||
616 | |||||
617 | RegionMRT *MRT::buildMRT(MachineFunction &MF, | ||||
618 | const MachineRegionInfo *RegionInfo, | ||||
619 | const SIInstrInfo *TII, MachineRegisterInfo *MRI) { | ||||
620 | SmallPtrSet<MachineRegion *, 4> PlacedRegions; | ||||
621 | DenseMap<MachineRegion *, RegionMRT *> RegionMap; | ||||
622 | MachineRegion *TopLevelRegion = RegionInfo->getTopLevelRegion(); | ||||
623 | RegionMRT *Result = new RegionMRT(TopLevelRegion); | ||||
624 | RegionMap[TopLevelRegion] = Result; | ||||
625 | |||||
626 | // Insert the exit block first, we need it to be the merge node | ||||
627 | // for the top level region. | ||||
628 | MachineBasicBlock *Exit = initializeMRT(MF, RegionInfo, RegionMap); | ||||
629 | |||||
630 | unsigned BBSelectRegIn = createBBSelectReg(TII, MRI); | ||||
631 | MBBMRT *ExitMRT = new MBBMRT(Exit); | ||||
632 | RegionMap[RegionInfo->getRegionFor(Exit)]->addChild(ExitMRT); | ||||
633 | ExitMRT->setBBSelectRegIn(BBSelectRegIn); | ||||
634 | |||||
635 | for (auto MBBI : post_order(&(MF.front()))) { | ||||
636 | MachineBasicBlock *MBB = &(*MBBI); | ||||
637 | |||||
638 | // Skip Exit since we already added it | ||||
639 | if (MBB == Exit) { | ||||
640 | continue; | ||||
641 | } | ||||
642 | |||||
643 | LLVM_DEBUG(dbgs() << "Visiting " << printMBBReference(*MBB) << "\n")do { } while (false); | ||||
644 | MBBMRT *NewMBB = new MBBMRT(MBB); | ||||
645 | MachineRegion *Region = RegionInfo->getRegionFor(MBB); | ||||
646 | |||||
647 | // Ensure we have the MRT region | ||||
648 | if (RegionMap.count(Region) == 0) { | ||||
649 | RegionMRT *NewMRTRegion = new RegionMRT(Region); | ||||
650 | RegionMap[Region] = NewMRTRegion; | ||||
651 | |||||
652 | // Ensure all parents are in the RegionMap | ||||
653 | MachineRegion *Parent = Region->getParent(); | ||||
654 | while (RegionMap.count(Parent) == 0) { | ||||
655 | RegionMRT *NewMRTParent = new RegionMRT(Parent); | ||||
656 | NewMRTParent->addChild(NewMRTRegion); | ||||
657 | NewMRTRegion->setParent(NewMRTParent); | ||||
658 | RegionMap[Parent] = NewMRTParent; | ||||
659 | NewMRTRegion = NewMRTParent; | ||||
660 | Parent = Parent->getParent(); | ||||
661 | } | ||||
662 | RegionMap[Parent]->addChild(NewMRTRegion); | ||||
663 | NewMRTRegion->setParent(RegionMap[Parent]); | ||||
664 | } | ||||
665 | |||||
666 | // Add MBB to Region MRT | ||||
667 | RegionMap[Region]->addChild(NewMBB); | ||||
668 | NewMBB->setParent(RegionMap[Region]); | ||||
669 | RegionMap[Region]->setSucc(Region->getExit()); | ||||
670 | } | ||||
671 | return Result; | ||||
672 | } | ||||
673 | |||||
674 | void LinearizedRegion::storeLiveOutReg(MachineBasicBlock *MBB, Register Reg, | ||||
675 | MachineInstr *DefInstr, | ||||
676 | const MachineRegisterInfo *MRI, | ||||
677 | const TargetRegisterInfo *TRI, | ||||
678 | PHILinearize &PHIInfo) { | ||||
679 | if (Reg.isVirtual()) { | ||||
680 | LLVM_DEBUG(dbgs() << "Considering Register: " << printReg(Reg, TRI)do { } while (false) | ||||
681 | << "\n")do { } while (false); | ||||
682 | // If this is a source register to a PHI we are chaining, it | ||||
683 | // must be live out. | ||||
684 | if (PHIInfo.isSource(Reg)) { | ||||
685 | LLVM_DEBUG(dbgs() << "Add LiveOut (PHI): " << printReg(Reg, TRI) << "\n")do { } while (false); | ||||
686 | addLiveOut(Reg); | ||||
687 | } else { | ||||
688 | // If this is live out of the MBB | ||||
689 | for (auto &UI : MRI->use_operands(Reg)) { | ||||
690 | if (UI.getParent()->getParent() != MBB) { | ||||
691 | LLVM_DEBUG(dbgs() << "Add LiveOut (MBB " << printMBBReference(*MBB)do { } while (false) | ||||
692 | << "): " << printReg(Reg, TRI) << "\n")do { } while (false); | ||||
693 | addLiveOut(Reg); | ||||
694 | } else { | ||||
695 | // If the use is in the same MBB we have to make sure | ||||
696 | // it is after the def, otherwise it is live out in a loop | ||||
697 | MachineInstr *UseInstr = UI.getParent(); | ||||
698 | for (MachineBasicBlock::instr_iterator | ||||
699 | MII = UseInstr->getIterator(), | ||||
700 | MIE = UseInstr->getParent()->instr_end(); | ||||
701 | MII != MIE; ++MII) { | ||||
702 | if ((&(*MII)) == DefInstr) { | ||||
703 | LLVM_DEBUG(dbgs() << "Add LiveOut (Loop): " << printReg(Reg, TRI)do { } while (false) | ||||
704 | << "\n")do { } while (false); | ||||
705 | addLiveOut(Reg); | ||||
706 | } | ||||
707 | } | ||||
708 | } | ||||
709 | } | ||||
710 | } | ||||
711 | } | ||||
712 | } | ||||
713 | |||||
714 | void LinearizedRegion::storeLiveOutRegRegion(RegionMRT *Region, Register Reg, | ||||
715 | MachineInstr *DefInstr, | ||||
716 | const MachineRegisterInfo *MRI, | ||||
717 | const TargetRegisterInfo *TRI, | ||||
718 | PHILinearize &PHIInfo) { | ||||
719 | if (Reg.isVirtual()) { | ||||
720 | LLVM_DEBUG(dbgs() << "Considering Register: " << printReg(Reg, TRI)do { } while (false) | ||||
721 | << "\n")do { } while (false); | ||||
722 | for (auto &UI : MRI->use_operands(Reg)) { | ||||
723 | if (!Region->contains(UI.getParent()->getParent())) { | ||||
724 | LLVM_DEBUG(dbgs() << "Add LiveOut (Region " << (void *)Regiondo { } while (false) | ||||
725 | << "): " << printReg(Reg, TRI) << "\n")do { } while (false); | ||||
726 | addLiveOut(Reg); | ||||
727 | } | ||||
728 | } | ||||
729 | } | ||||
730 | } | ||||
731 | |||||
732 | void LinearizedRegion::storeLiveOuts(MachineBasicBlock *MBB, | ||||
733 | const MachineRegisterInfo *MRI, | ||||
734 | const TargetRegisterInfo *TRI, | ||||
735 | PHILinearize &PHIInfo) { | ||||
736 | LLVM_DEBUG(dbgs() << "-Store Live Outs Begin (" << printMBBReference(*MBB)do { } while (false) | ||||
737 | << ")-\n")do { } while (false); | ||||
738 | for (auto &II : *MBB) { | ||||
739 | for (auto &RI : II.defs()) { | ||||
740 | storeLiveOutReg(MBB, RI.getReg(), RI.getParent(), MRI, TRI, PHIInfo); | ||||
741 | } | ||||
742 | for (auto &IRI : II.implicit_operands()) { | ||||
743 | if (IRI.isDef()) { | ||||
744 | storeLiveOutReg(MBB, IRI.getReg(), IRI.getParent(), MRI, TRI, PHIInfo); | ||||
745 | } | ||||
746 | } | ||||
747 | } | ||||
748 | |||||
749 | // If we have a successor with a PHI, source coming from this MBB we have to | ||||
750 | // add the register as live out | ||||
751 | for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(), | ||||
752 | E = MBB->succ_end(); | ||||
753 | SI != E; ++SI) { | ||||
754 | for (auto &II : *(*SI)) { | ||||
755 | if (II.isPHI()) { | ||||
756 | MachineInstr &PHI = II; | ||||
757 | int numPreds = getPHINumInputs(PHI); | ||||
758 | for (int i = 0; i < numPreds; ++i) { | ||||
759 | if (getPHIPred(PHI, i) == MBB) { | ||||
760 | unsigned PHIReg = getPHISourceReg(PHI, i); | ||||
761 | LLVM_DEBUG(dbgs()do { } while (false) | ||||
762 | << "Add LiveOut (PhiSource " << printMBBReference(*MBB)do { } while (false) | ||||
763 | << " -> " << printMBBReference(*(*SI))do { } while (false) | ||||
764 | << "): " << printReg(PHIReg, TRI) << "\n")do { } while (false); | ||||
765 | addLiveOut(PHIReg); | ||||
766 | } | ||||
767 | } | ||||
768 | } | ||||
769 | } | ||||
770 | } | ||||
771 | |||||
772 | LLVM_DEBUG(dbgs() << "-Store Live Outs Endn-\n")do { } while (false); | ||||
773 | } | ||||
774 | |||||
775 | void LinearizedRegion::storeMBBLiveOuts(MachineBasicBlock *MBB, | ||||
776 | const MachineRegisterInfo *MRI, | ||||
777 | const TargetRegisterInfo *TRI, | ||||
778 | PHILinearize &PHIInfo, | ||||
779 | RegionMRT *TopRegion) { | ||||
780 | for (auto &II : *MBB) { | ||||
781 | for (auto &RI : II.defs()) { | ||||
782 | storeLiveOutRegRegion(TopRegion, RI.getReg(), RI.getParent(), MRI, TRI, | ||||
783 | PHIInfo); | ||||
784 | } | ||||
785 | for (auto &IRI : II.implicit_operands()) { | ||||
786 | if (IRI.isDef()) { | ||||
787 | storeLiveOutRegRegion(TopRegion, IRI.getReg(), IRI.getParent(), MRI, | ||||
788 | TRI, PHIInfo); | ||||
789 | } | ||||
790 | } | ||||
791 | } | ||||
792 | } | ||||
793 | |||||
794 | void LinearizedRegion::storeLiveOuts(RegionMRT *Region, | ||||
795 | const MachineRegisterInfo *MRI, | ||||
796 | const TargetRegisterInfo *TRI, | ||||
797 | PHILinearize &PHIInfo, | ||||
798 | RegionMRT *CurrentTopRegion) { | ||||
799 | MachineBasicBlock *Exit = Region->getSucc(); | ||||
800 | |||||
801 | RegionMRT *TopRegion = | ||||
802 | CurrentTopRegion == nullptr ? Region : CurrentTopRegion; | ||||
803 | |||||
804 | // Check if exit is end of function, if so, no live outs. | ||||
805 | if (Exit == nullptr) | ||||
806 | return; | ||||
807 | |||||
808 | auto Children = Region->getChildren(); | ||||
809 | for (auto CI : *Children) { | ||||
810 | if (CI->isMBB()) { | ||||
811 | auto MBB = CI->getMBBMRT()->getMBB(); | ||||
812 | storeMBBLiveOuts(MBB, MRI, TRI, PHIInfo, TopRegion); | ||||
813 | } else { | ||||
814 | LinearizedRegion *SubRegion = CI->getRegionMRT()->getLinearizedRegion(); | ||||
815 | // We should be limited to only store registers that are live out from the | ||||
816 | // lineaized region | ||||
817 | for (auto MBBI : SubRegion->MBBs) { | ||||
818 | storeMBBLiveOuts(MBBI, MRI, TRI, PHIInfo, TopRegion); | ||||
819 | } | ||||
820 | } | ||||
821 | } | ||||
822 | |||||
823 | if (CurrentTopRegion == nullptr) { | ||||
824 | auto Succ = Region->getSucc(); | ||||
825 | for (auto &II : *Succ) { | ||||
826 | if (II.isPHI()) { | ||||
827 | MachineInstr &PHI = II; | ||||
828 | int numPreds = getPHINumInputs(PHI); | ||||
829 | for (int i = 0; i < numPreds; ++i) { | ||||
830 | if (Region->contains(getPHIPred(PHI, i))) { | ||||
831 | unsigned PHIReg = getPHISourceReg(PHI, i); | ||||
832 | LLVM_DEBUG(dbgs() << "Add Region LiveOut (" << (void *)Regiondo { } while (false) | ||||
833 | << "): " << printReg(PHIReg, TRI) << "\n")do { } while (false); | ||||
834 | addLiveOut(PHIReg); | ||||
835 | } | ||||
836 | } | ||||
837 | } | ||||
838 | } | ||||
839 | } | ||||
840 | } | ||||
841 | |||||
842 | #ifndef NDEBUG1 | ||||
843 | void LinearizedRegion::print(raw_ostream &OS, const TargetRegisterInfo *TRI) { | ||||
844 | OS << "Linearized Region {"; | ||||
845 | bool IsFirst = true; | ||||
846 | for (auto MBB : MBBs) { | ||||
847 | if (IsFirst) { | ||||
848 | IsFirst = false; | ||||
849 | } else { | ||||
850 | OS << " ,"; | ||||
851 | } | ||||
852 | OS << MBB->getNumber(); | ||||
853 | } | ||||
854 | OS << "} (" << Entry->getNumber() << ", " | ||||
855 | << (Exit == nullptr ? -1 : Exit->getNumber()) | ||||
856 | << "): In:" << printReg(getBBSelectRegIn(), TRI) | ||||
857 | << " Out:" << printReg(getBBSelectRegOut(), TRI) << " {"; | ||||
858 | for (auto &LI : LiveOuts) { | ||||
859 | OS << printReg(LI, TRI) << " "; | ||||
860 | } | ||||
861 | OS << "} \n"; | ||||
862 | } | ||||
863 | #endif | ||||
864 | |||||
865 | unsigned LinearizedRegion::getBBSelectRegIn() { | ||||
866 | return getRegionMRT()->getBBSelectRegIn(); | ||||
867 | } | ||||
868 | |||||
869 | unsigned LinearizedRegion::getBBSelectRegOut() { | ||||
870 | return getRegionMRT()->getBBSelectRegOut(); | ||||
871 | } | ||||
872 | |||||
873 | void LinearizedRegion::setHasLoop(bool Value) { HasLoop = Value; } | ||||
874 | |||||
875 | bool LinearizedRegion::getHasLoop() { return HasLoop; } | ||||
876 | |||||
877 | void LinearizedRegion::addLiveOut(unsigned VReg) { LiveOuts.insert(VReg); } | ||||
878 | |||||
879 | void LinearizedRegion::removeLiveOut(unsigned Reg) { | ||||
880 | if (isLiveOut(Reg)) | ||||
881 | LiveOuts.erase(Reg); | ||||
882 | } | ||||
883 | |||||
884 | void LinearizedRegion::replaceLiveOut(unsigned OldReg, unsigned NewReg) { | ||||
885 | if (isLiveOut(OldReg)) { | ||||
886 | removeLiveOut(OldReg); | ||||
887 | addLiveOut(NewReg); | ||||
888 | } | ||||
889 | } | ||||
890 | |||||
891 | void LinearizedRegion::replaceRegister(unsigned Register, | ||||
892 | class Register NewRegister, | ||||
893 | MachineRegisterInfo *MRI, | ||||
894 | bool ReplaceInside, bool ReplaceOutside, | ||||
895 | bool IncludeLoopPHI) { | ||||
896 | assert(Register != NewRegister && "Cannot replace a reg with itself")(static_cast<void> (0)); | ||||
897 | |||||
898 | LLVM_DEBUG(do { } while (false) | ||||
899 | dbgs() << "Pepareing to replace register (region): "do { } while (false) | ||||
900 | << printReg(Register, MRI->getTargetRegisterInfo()) << " with "do { } while (false) | ||||
901 | << printReg(NewRegister, MRI->getTargetRegisterInfo()) << "\n")do { } while (false); | ||||
902 | |||||
903 | // If we are replacing outside, we also need to update the LiveOuts | ||||
904 | if (ReplaceOutside && | ||||
905 | (isLiveOut(Register) || this->getParent()->isLiveOut(Register))) { | ||||
906 | LinearizedRegion *Current = this; | ||||
907 | while (Current != nullptr && Current->getEntry() != nullptr) { | ||||
908 | LLVM_DEBUG(dbgs() << "Region before register replace\n")do { } while (false); | ||||
909 | LLVM_DEBUG(Current->print(dbgs(), MRI->getTargetRegisterInfo()))do { } while (false); | ||||
910 | Current->replaceLiveOut(Register, NewRegister); | ||||
911 | LLVM_DEBUG(dbgs() << "Region after register replace\n")do { } while (false); | ||||
912 | LLVM_DEBUG(Current->print(dbgs(), MRI->getTargetRegisterInfo()))do { } while (false); | ||||
913 | Current = Current->getParent(); | ||||
914 | } | ||||
915 | } | ||||
916 | |||||
917 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Register), | ||||
918 | E = MRI->reg_end(); | ||||
919 | I != E;) { | ||||
920 | MachineOperand &O = *I; | ||||
921 | ++I; | ||||
922 | |||||
923 | // We don't rewrite defs. | ||||
924 | if (O.isDef()) | ||||
925 | continue; | ||||
926 | |||||
927 | bool IsInside = contains(O.getParent()->getParent()); | ||||
928 | bool IsLoopPHI = IsInside && (O.getParent()->isPHI() && | ||||
929 | O.getParent()->getParent() == getEntry()); | ||||
930 | bool ShouldReplace = (IsInside && ReplaceInside) || | ||||
931 | (!IsInside && ReplaceOutside) || | ||||
932 | (IncludeLoopPHI && IsLoopPHI); | ||||
933 | if (ShouldReplace) { | ||||
934 | |||||
935 | if (NewRegister.isPhysical()) { | ||||
936 | LLVM_DEBUG(dbgs() << "Trying to substitute physical register: "do { } while (false) | ||||
937 | << printReg(NewRegister, MRI->getTargetRegisterInfo())do { } while (false) | ||||
938 | << "\n")do { } while (false); | ||||
939 | llvm_unreachable("Cannot substitute physical registers")__builtin_unreachable(); | ||||
940 | } else { | ||||
941 | LLVM_DEBUG(dbgs() << "Replacing register (region): "do { } while (false) | ||||
942 | << printReg(Register, MRI->getTargetRegisterInfo())do { } while (false) | ||||
943 | << " with "do { } while (false) | ||||
944 | << printReg(NewRegister, MRI->getTargetRegisterInfo())do { } while (false) | ||||
945 | << "\n")do { } while (false); | ||||
946 | O.setReg(NewRegister); | ||||
947 | } | ||||
948 | } | ||||
949 | } | ||||
950 | } | ||||
951 | |||||
952 | void LinearizedRegion::replaceRegisterInsideRegion(unsigned Register, | ||||
953 | unsigned NewRegister, | ||||
954 | bool IncludeLoopPHIs, | ||||
955 | MachineRegisterInfo *MRI) { | ||||
956 | replaceRegister(Register, NewRegister, MRI, true, false, IncludeLoopPHIs); | ||||
957 | } | ||||
958 | |||||
959 | void LinearizedRegion::replaceRegisterOutsideRegion(unsigned Register, | ||||
960 | unsigned NewRegister, | ||||
961 | bool IncludeLoopPHIs, | ||||
962 | MachineRegisterInfo *MRI) { | ||||
963 | replaceRegister(Register, NewRegister, MRI, false, true, IncludeLoopPHIs); | ||||
964 | } | ||||
965 | |||||
966 | DenseSet<unsigned> *LinearizedRegion::getLiveOuts() { return &LiveOuts; } | ||||
967 | |||||
968 | void LinearizedRegion::setEntry(MachineBasicBlock *NewEntry) { | ||||
969 | Entry = NewEntry; | ||||
970 | } | ||||
971 | |||||
972 | MachineBasicBlock *LinearizedRegion::getEntry() { return Entry; } | ||||
973 | |||||
974 | void LinearizedRegion::setExit(MachineBasicBlock *NewExit) { Exit = NewExit; } | ||||
975 | |||||
976 | MachineBasicBlock *LinearizedRegion::getExit() { return Exit; } | ||||
977 | |||||
978 | void LinearizedRegion::addMBB(MachineBasicBlock *MBB) { MBBs.insert(MBB); } | ||||
979 | |||||
980 | void LinearizedRegion::addMBBs(LinearizedRegion *InnerRegion) { | ||||
981 | for (auto MBB : InnerRegion->MBBs) { | ||||
982 | addMBB(MBB); | ||||
983 | } | ||||
984 | } | ||||
985 | |||||
986 | bool LinearizedRegion::contains(MachineBasicBlock *MBB) { | ||||
987 | return MBBs.contains(MBB); | ||||
988 | } | ||||
989 | |||||
990 | bool LinearizedRegion::isLiveOut(unsigned Reg) { | ||||
991 | return LiveOuts.contains(Reg); | ||||
992 | } | ||||
993 | |||||
994 | bool LinearizedRegion::hasNoDef(unsigned Reg, MachineRegisterInfo *MRI) { | ||||
995 | return MRI->def_begin(Reg) == MRI->def_end(); | ||||
996 | } | ||||
997 | |||||
998 | // After the code has been structurized, what was flagged as kills | ||||
999 | // before are no longer register kills. | ||||
1000 | void LinearizedRegion::removeFalseRegisterKills(MachineRegisterInfo *MRI) { | ||||
1001 | const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo(); | ||||
1002 | (void)TRI; // It's used by LLVM_DEBUG. | ||||
1003 | |||||
1004 | for (auto MBBI : MBBs) { | ||||
1005 | MachineBasicBlock *MBB = MBBI; | ||||
1006 | for (auto &II : *MBB) { | ||||
1007 | for (auto &RI : II.uses()) { | ||||
1008 | if (RI.isReg()) { | ||||
1009 | Register Reg = RI.getReg(); | ||||
1010 | if (Reg.isVirtual()) { | ||||
1011 | if (hasNoDef(Reg, MRI)) | ||||
1012 | continue; | ||||
1013 | if (!MRI->hasOneDef(Reg)) { | ||||
1014 | LLVM_DEBUG(this->getEntry()->getParent()->dump())do { } while (false); | ||||
1015 | LLVM_DEBUG(dbgs() << printReg(Reg, TRI) << "\n")do { } while (false); | ||||
1016 | } | ||||
1017 | |||||
1018 | if (MRI->def_begin(Reg) == MRI->def_end()) { | ||||
1019 | LLVM_DEBUG(dbgs() << "Register "do { } while (false) | ||||
1020 | << printReg(Reg, MRI->getTargetRegisterInfo())do { } while (false) | ||||
1021 | << " has NO defs\n")do { } while (false); | ||||
1022 | } else if (!MRI->hasOneDef(Reg)) { | ||||
1023 | LLVM_DEBUG(dbgs() << "Register "do { } while (false) | ||||
1024 | << printReg(Reg, MRI->getTargetRegisterInfo())do { } while (false) | ||||
1025 | << " has multiple defs\n")do { } while (false); | ||||
1026 | } | ||||
1027 | |||||
1028 | assert(MRI->hasOneDef(Reg) && "Register has multiple definitions")(static_cast<void> (0)); | ||||
1029 | MachineOperand *Def = &(*(MRI->def_begin(Reg))); | ||||
1030 | MachineOperand *UseOperand = &(RI); | ||||
1031 | bool UseIsOutsideDefMBB = Def->getParent()->getParent() != MBB; | ||||
1032 | if (UseIsOutsideDefMBB && UseOperand->isKill()) { | ||||
1033 | LLVM_DEBUG(dbgs() << "Removing kill flag on register: "do { } while (false) | ||||
1034 | << printReg(Reg, TRI) << "\n")do { } while (false); | ||||
1035 | UseOperand->setIsKill(false); | ||||
1036 | } | ||||
1037 | } | ||||
1038 | } | ||||
1039 | } | ||||
1040 | } | ||||
1041 | } | ||||
1042 | } | ||||
1043 | |||||
1044 | void LinearizedRegion::initLiveOut(RegionMRT *Region, | ||||
1045 | const MachineRegisterInfo *MRI, | ||||
1046 | const TargetRegisterInfo *TRI, | ||||
1047 | PHILinearize &PHIInfo) { | ||||
1048 | storeLiveOuts(Region, MRI, TRI, PHIInfo); | ||||
1049 | } | ||||
1050 | |||||
1051 | LinearizedRegion::LinearizedRegion(MachineBasicBlock *MBB, | ||||
1052 | const MachineRegisterInfo *MRI, | ||||
1053 | const TargetRegisterInfo *TRI, | ||||
1054 | PHILinearize &PHIInfo) { | ||||
1055 | setEntry(MBB); | ||||
1056 | setExit(MBB); | ||||
1057 | storeLiveOuts(MBB, MRI, TRI, PHIInfo); | ||||
1058 | MBBs.insert(MBB); | ||||
1059 | Parent = nullptr; | ||||
1060 | } | ||||
1061 | |||||
1062 | LinearizedRegion::LinearizedRegion() { | ||||
1063 | setEntry(nullptr); | ||||
1064 | setExit(nullptr); | ||||
1065 | Parent = nullptr; | ||||
1066 | } | ||||
1067 | |||||
1068 | namespace { | ||||
1069 | |||||
1070 | class AMDGPUMachineCFGStructurizer : public MachineFunctionPass { | ||||
1071 | private: | ||||
1072 | const MachineRegionInfo *Regions; | ||||
1073 | const SIInstrInfo *TII; | ||||
1074 | const TargetRegisterInfo *TRI; | ||||
1075 | MachineRegisterInfo *MRI; | ||||
1076 | unsigned BBSelectRegister; | ||||
1077 | PHILinearize PHIInfo; | ||||
1078 | DenseMap<MachineBasicBlock *, MachineBasicBlock *> FallthroughMap; | ||||
1079 | RegionMRT *RMRT; | ||||
1080 | |||||
1081 | void getPHIRegionIndices(RegionMRT *Region, MachineInstr &PHI, | ||||
1082 | SmallVector<unsigned, 2> &RegionIndices); | ||||
1083 | void getPHIRegionIndices(LinearizedRegion *Region, MachineInstr &PHI, | ||||
1084 | SmallVector<unsigned, 2> &RegionIndices); | ||||
1085 | void getPHINonRegionIndices(LinearizedRegion *Region, MachineInstr &PHI, | ||||
1086 | SmallVector<unsigned, 2> &PHINonRegionIndices); | ||||
1087 | |||||
1088 | void storePHILinearizationInfoDest( | ||||
1089 | unsigned LDestReg, MachineInstr &PHI, | ||||
1090 | SmallVector<unsigned, 2> *RegionIndices = nullptr); | ||||
1091 | |||||
1092 | unsigned storePHILinearizationInfo(MachineInstr &PHI, | ||||
1093 | SmallVector<unsigned, 2> *RegionIndices); | ||||
1094 | |||||
1095 | void extractKilledPHIs(MachineBasicBlock *MBB); | ||||
1096 | |||||
1097 | bool shrinkPHI(MachineInstr &PHI, SmallVector<unsigned, 2> &PHIIndices, | ||||
1098 | unsigned *ReplaceReg); | ||||
1099 | |||||
1100 | bool shrinkPHI(MachineInstr &PHI, unsigned CombinedSourceReg, | ||||
1101 | MachineBasicBlock *SourceMBB, | ||||
1102 | SmallVector<unsigned, 2> &PHIIndices, unsigned *ReplaceReg); | ||||
1103 | |||||
1104 | void replacePHI(MachineInstr &PHI, unsigned CombinedSourceReg, | ||||
1105 | MachineBasicBlock *LastMerge, | ||||
1106 | SmallVector<unsigned, 2> &PHIRegionIndices); | ||||
1107 | void replaceEntryPHI(MachineInstr &PHI, unsigned CombinedSourceReg, | ||||
1108 | MachineBasicBlock *IfMBB, | ||||
1109 | SmallVector<unsigned, 2> &PHIRegionIndices); | ||||
1110 | void replaceLiveOutRegs(MachineInstr &PHI, | ||||
1111 | SmallVector<unsigned, 2> &PHIRegionIndices, | ||||
1112 | unsigned CombinedSourceReg, | ||||
1113 | LinearizedRegion *LRegion); | ||||
1114 | void rewriteRegionExitPHI(RegionMRT *Region, MachineBasicBlock *LastMerge, | ||||
1115 | MachineInstr &PHI, LinearizedRegion *LRegion); | ||||
1116 | |||||
1117 | void rewriteRegionExitPHIs(RegionMRT *Region, MachineBasicBlock *LastMerge, | ||||
1118 | LinearizedRegion *LRegion); | ||||
1119 | void rewriteRegionEntryPHI(LinearizedRegion *Region, MachineBasicBlock *IfMBB, | ||||
1120 | MachineInstr &PHI); | ||||
1121 | void rewriteRegionEntryPHIs(LinearizedRegion *Region, | ||||
1122 | MachineBasicBlock *IfMBB); | ||||
1123 | |||||
1124 | bool regionIsSimpleIf(RegionMRT *Region); | ||||
1125 | |||||
1126 | void transformSimpleIfRegion(RegionMRT *Region); | ||||
1127 | |||||
1128 | void eliminateDeadBranchOperands(MachineBasicBlock::instr_iterator &II); | ||||
1129 | |||||
1130 | void insertUnconditionalBranch(MachineBasicBlock *MBB, | ||||
1131 | MachineBasicBlock *Dest, | ||||
1132 | const DebugLoc &DL = DebugLoc()); | ||||
1133 | |||||
1134 | MachineBasicBlock *createLinearizedExitBlock(RegionMRT *Region); | ||||
1135 | |||||
1136 | void insertMergePHI(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, | ||||
1137 | MachineBasicBlock *MergeBB, unsigned DestRegister, | ||||
1138 | unsigned IfSourceRegister, unsigned CodeSourceRegister, | ||||
1139 | bool IsUndefIfSource = false); | ||||
1140 | |||||
1141 | MachineBasicBlock *createIfBlock(MachineBasicBlock *MergeBB, | ||||
1142 | MachineBasicBlock *CodeBBStart, | ||||
1143 | MachineBasicBlock *CodeBBEnd, | ||||
1144 | MachineBasicBlock *SelectBB, unsigned IfReg, | ||||
1145 | bool InheritPreds); | ||||
1146 | |||||
1147 | void prunePHIInfo(MachineBasicBlock *MBB); | ||||
1148 | void createEntryPHI(LinearizedRegion *CurrentRegion, unsigned DestReg); | ||||
1149 | |||||
1150 | void createEntryPHIs(LinearizedRegion *CurrentRegion); | ||||
1151 | void resolvePHIInfos(MachineBasicBlock *FunctionEntry); | ||||
1152 | |||||
1153 | void replaceRegisterWith(unsigned Register, class Register NewRegister); | ||||
1154 | |||||
1155 | MachineBasicBlock *createIfRegion(MachineBasicBlock *MergeBB, | ||||
1156 | MachineBasicBlock *CodeBB, | ||||
1157 | LinearizedRegion *LRegion, | ||||
1158 | unsigned BBSelectRegIn, | ||||
1159 | unsigned BBSelectRegOut); | ||||
1160 | |||||
1161 | MachineBasicBlock * | ||||
1162 | createIfRegion(MachineBasicBlock *MergeMBB, LinearizedRegion *InnerRegion, | ||||
1163 | LinearizedRegion *CurrentRegion, MachineBasicBlock *SelectBB, | ||||
1164 | unsigned BBSelectRegIn, unsigned BBSelectRegOut); | ||||
1165 | void ensureCondIsNotKilled(SmallVector<MachineOperand, 1> Cond); | ||||
1166 | |||||
1167 | void rewriteCodeBBTerminator(MachineBasicBlock *CodeBB, | ||||
1168 | MachineBasicBlock *MergeBB, | ||||
1169 | unsigned BBSelectReg); | ||||
1170 | |||||
1171 | MachineInstr *getDefInstr(unsigned Reg); | ||||
1172 | void insertChainedPHI(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, | ||||
1173 | MachineBasicBlock *MergeBB, | ||||
1174 | LinearizedRegion *InnerRegion, unsigned DestReg, | ||||
1175 | unsigned SourceReg); | ||||
1176 | bool containsDef(MachineBasicBlock *MBB, LinearizedRegion *InnerRegion, | ||||
1177 | unsigned Register); | ||||
1178 | void rewriteLiveOutRegs(MachineBasicBlock *IfBB, MachineBasicBlock *CodeBB, | ||||
1179 | MachineBasicBlock *MergeBB, | ||||
1180 | LinearizedRegion *InnerRegion, | ||||
1181 | LinearizedRegion *LRegion); | ||||
1182 | |||||
1183 | void splitLoopPHI(MachineInstr &PHI, MachineBasicBlock *Entry, | ||||
1184 | MachineBasicBlock *EntrySucc, LinearizedRegion *LRegion); | ||||
1185 | void splitLoopPHIs(MachineBasicBlock *Entry, MachineBasicBlock *EntrySucc, | ||||
1186 | LinearizedRegion *LRegion); | ||||
1187 | |||||
1188 | MachineBasicBlock *splitExit(LinearizedRegion *LRegion); | ||||
1189 | |||||
1190 | MachineBasicBlock *splitEntry(LinearizedRegion *LRegion); | ||||
1191 | |||||
1192 | LinearizedRegion *initLinearizedRegion(RegionMRT *Region); | ||||
1193 | |||||
1194 | bool structurizeComplexRegion(RegionMRT *Region); | ||||
1195 | |||||
1196 | bool structurizeRegion(RegionMRT *Region); | ||||
1197 | |||||
1198 | bool structurizeRegions(RegionMRT *Region, bool isTopRegion); | ||||
1199 | |||||
1200 | public: | ||||
1201 | static char ID; | ||||
1202 | |||||
1203 | AMDGPUMachineCFGStructurizer() : MachineFunctionPass(ID) { | ||||
1204 | initializeAMDGPUMachineCFGStructurizerPass(*PassRegistry::getPassRegistry()); | ||||
1205 | } | ||||
1206 | |||||
1207 | void getAnalysisUsage(AnalysisUsage &AU) const override { | ||||
1208 | AU.addRequired<MachineRegionInfoPass>(); | ||||
1209 | MachineFunctionPass::getAnalysisUsage(AU); | ||||
1210 | } | ||||
1211 | |||||
1212 | void initFallthroughMap(MachineFunction &MF); | ||||
1213 | |||||
1214 | void createLinearizedRegion(RegionMRT *Region, unsigned SelectOut); | ||||
1215 | |||||
1216 | unsigned initializeSelectRegisters(MRT *MRT, unsigned ExistingExitReg, | ||||
1217 | MachineRegisterInfo *MRI, | ||||
1218 | const SIInstrInfo *TII); | ||||
1219 | |||||
1220 | void setRegionMRT(RegionMRT *RegionTree) { RMRT = RegionTree; } | ||||
1221 | |||||
1222 | RegionMRT *getRegionMRT() { return RMRT; } | ||||
1223 | |||||
1224 | bool runOnMachineFunction(MachineFunction &MF) override; | ||||
1225 | }; | ||||
1226 | |||||
1227 | } // end anonymous namespace | ||||
1228 | |||||
1229 | char AMDGPUMachineCFGStructurizer::ID = 0; | ||||
1230 | |||||
1231 | bool AMDGPUMachineCFGStructurizer::regionIsSimpleIf(RegionMRT *Region) { | ||||
1232 | MachineBasicBlock *Entry = Region->getEntry(); | ||||
1233 | MachineBasicBlock *Succ = Region->getSucc(); | ||||
1234 | bool FoundBypass = false; | ||||
1235 | bool FoundIf = false; | ||||
1236 | |||||
1237 | if (Entry->succ_size() != 2) { | ||||
1238 | return false; | ||||
1239 | } | ||||
1240 | |||||
1241 | for (MachineBasicBlock::const_succ_iterator SI = Entry->succ_begin(), | ||||
1242 | E = Entry->succ_end(); | ||||
1243 | SI != E; ++SI) { | ||||
1244 | MachineBasicBlock *Current = *SI; | ||||
1245 | |||||
1246 | if (Current == Succ) { | ||||
1247 | FoundBypass = true; | ||||
1248 | } else if ((Current->succ_size() == 1) && | ||||
1249 | *(Current->succ_begin()) == Succ) { | ||||
1250 | FoundIf = true; | ||||
1251 | } | ||||
1252 | } | ||||
1253 | |||||
1254 | return FoundIf && FoundBypass; | ||||
1255 | } | ||||
1256 | |||||
1257 | void AMDGPUMachineCFGStructurizer::transformSimpleIfRegion(RegionMRT *Region) { | ||||
1258 | MachineBasicBlock *Entry = Region->getEntry(); | ||||
1259 | MachineBasicBlock *Exit = Region->getExit(); | ||||
1260 | TII->convertNonUniformIfRegion(Entry, Exit); | ||||
1261 | } | ||||
1262 | |||||
1263 | static void fixMBBTerminator(MachineBasicBlock *MBB) { | ||||
1264 | if (MBB->succ_size() == 1) { | ||||
1265 | auto *Succ = *(MBB->succ_begin()); | ||||
1266 | for (auto &TI : MBB->terminators()) { | ||||
1267 | for (auto &UI : TI.uses()) { | ||||
1268 | if (UI.isMBB() && UI.getMBB() != Succ) { | ||||
1269 | UI.setMBB(Succ); | ||||
1270 | } | ||||
1271 | } | ||||
1272 | } | ||||
1273 | } | ||||
1274 | } | ||||
1275 | |||||
1276 | static void fixRegionTerminator(RegionMRT *Region) { | ||||
1277 | MachineBasicBlock *InternalSucc = nullptr; | ||||
1278 | MachineBasicBlock *ExternalSucc = nullptr; | ||||
1279 | LinearizedRegion *LRegion = Region->getLinearizedRegion(); | ||||
1280 | auto Exit = LRegion->getExit(); | ||||
1281 | |||||
1282 | SmallPtrSet<MachineBasicBlock *, 2> Successors; | ||||
1283 | for (MachineBasicBlock::const_succ_iterator SI = Exit->succ_begin(), | ||||
1284 | SE = Exit->succ_end(); | ||||
1285 | SI != SE; ++SI) { | ||||
1286 | MachineBasicBlock *Succ = *SI; | ||||
1287 | if (LRegion->contains(Succ)) { | ||||
1288 | // Do not allow re-assign | ||||
1289 | assert(InternalSucc == nullptr)(static_cast<void> (0)); | ||||
1290 | InternalSucc = Succ; | ||||
1291 | } else { | ||||
1292 | // Do not allow re-assign | ||||
1293 | assert(ExternalSucc == nullptr)(static_cast<void> (0)); | ||||
1294 | ExternalSucc = Succ; | ||||
1295 | } | ||||
1296 | } | ||||
1297 | |||||
1298 | for (auto &TI : Exit->terminators()) { | ||||
1299 | for (auto &UI : TI.uses()) { | ||||
1300 | if (UI.isMBB()) { | ||||
1301 | auto Target = UI.getMBB(); | ||||
1302 | if (Target != InternalSucc && Target != ExternalSucc) { | ||||
1303 | UI.setMBB(ExternalSucc); | ||||
1304 | } | ||||
1305 | } | ||||
1306 | } | ||||
1307 | } | ||||
1308 | } | ||||
1309 | |||||
1310 | // If a region region is just a sequence of regions (and the exit | ||||
1311 | // block in the case of the top level region), we can simply skip | ||||
1312 | // linearizing it, because it is already linear | ||||
1313 | bool regionIsSequence(RegionMRT *Region) { | ||||
1314 | auto Children = Region->getChildren(); | ||||
1315 | for (auto CI : *Children) { | ||||
1316 | if (!CI->isRegion()) { | ||||
1317 | if (CI->getMBBMRT()->getMBB()->succ_size() > 1) { | ||||
1318 | return false; | ||||
1319 | } | ||||
1320 | } | ||||
1321 | } | ||||
1322 | return true; | ||||
1323 | } | ||||
1324 | |||||
1325 | void fixupRegionExits(RegionMRT *Region) { | ||||
1326 | auto Children = Region->getChildren(); | ||||
1327 | for (auto CI : *Children) { | ||||
1328 | if (!CI->isRegion()) { | ||||
1329 | fixMBBTerminator(CI->getMBBMRT()->getMBB()); | ||||
1330 | } else { | ||||
1331 | fixRegionTerminator(CI->getRegionMRT()); | ||||
1332 | } | ||||
1333 | } | ||||
1334 | } | ||||
1335 | |||||
1336 | void AMDGPUMachineCFGStructurizer::getPHIRegionIndices( | ||||
1337 | RegionMRT *Region, MachineInstr &PHI, | ||||
1338 | SmallVector<unsigned, 2> &PHIRegionIndices) { | ||||
1339 | unsigned NumInputs = getPHINumInputs(PHI); | ||||
1340 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1341 | MachineBasicBlock *Pred = getPHIPred(PHI, i); | ||||
1342 | if (Region->contains(Pred)) { | ||||
1343 | PHIRegionIndices.push_back(i); | ||||
1344 | } | ||||
1345 | } | ||||
1346 | } | ||||
1347 | |||||
1348 | void AMDGPUMachineCFGStructurizer::getPHIRegionIndices( | ||||
1349 | LinearizedRegion *Region, MachineInstr &PHI, | ||||
1350 | SmallVector<unsigned, 2> &PHIRegionIndices) { | ||||
1351 | unsigned NumInputs = getPHINumInputs(PHI); | ||||
1352 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1353 | MachineBasicBlock *Pred = getPHIPred(PHI, i); | ||||
1354 | if (Region->contains(Pred)) { | ||||
1355 | PHIRegionIndices.push_back(i); | ||||
1356 | } | ||||
1357 | } | ||||
1358 | } | ||||
1359 | |||||
1360 | void AMDGPUMachineCFGStructurizer::getPHINonRegionIndices( | ||||
1361 | LinearizedRegion *Region, MachineInstr &PHI, | ||||
1362 | SmallVector<unsigned, 2> &PHINonRegionIndices) { | ||||
1363 | unsigned NumInputs = getPHINumInputs(PHI); | ||||
1364 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1365 | MachineBasicBlock *Pred = getPHIPred(PHI, i); | ||||
1366 | if (!Region->contains(Pred)) { | ||||
1367 | PHINonRegionIndices.push_back(i); | ||||
1368 | } | ||||
1369 | } | ||||
1370 | } | ||||
1371 | |||||
1372 | void AMDGPUMachineCFGStructurizer::storePHILinearizationInfoDest( | ||||
1373 | unsigned LDestReg, MachineInstr &PHI, | ||||
1374 | SmallVector<unsigned, 2> *RegionIndices) { | ||||
1375 | if (RegionIndices) { | ||||
1376 | for (auto i : *RegionIndices) { | ||||
1377 | PHIInfo.addSource(LDestReg, getPHISourceReg(PHI, i), getPHIPred(PHI, i)); | ||||
1378 | } | ||||
1379 | } else { | ||||
1380 | unsigned NumInputs = getPHINumInputs(PHI); | ||||
1381 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1382 | PHIInfo.addSource(LDestReg, getPHISourceReg(PHI, i), getPHIPred(PHI, i)); | ||||
1383 | } | ||||
1384 | } | ||||
1385 | } | ||||
1386 | |||||
1387 | unsigned AMDGPUMachineCFGStructurizer::storePHILinearizationInfo( | ||||
1388 | MachineInstr &PHI, SmallVector<unsigned, 2> *RegionIndices) { | ||||
1389 | unsigned DestReg = getPHIDestReg(PHI); | ||||
1390 | Register LinearizeDestReg = | ||||
1391 | MRI->createVirtualRegister(MRI->getRegClass(DestReg)); | ||||
1392 | PHIInfo.addDest(LinearizeDestReg, PHI.getDebugLoc()); | ||||
1393 | storePHILinearizationInfoDest(LinearizeDestReg, PHI, RegionIndices); | ||||
1394 | return LinearizeDestReg; | ||||
1395 | } | ||||
1396 | |||||
1397 | void AMDGPUMachineCFGStructurizer::extractKilledPHIs(MachineBasicBlock *MBB) { | ||||
1398 | // We need to create a new chain for the killed phi, but there is no | ||||
1399 | // need to do the renaming outside or inside the block. | ||||
1400 | SmallPtrSet<MachineInstr *, 2> PHIs; | ||||
1401 | for (MachineBasicBlock::instr_iterator I = MBB->instr_begin(), | ||||
1402 | E = MBB->instr_end(); | ||||
1403 | I != E; ++I) { | ||||
1404 | MachineInstr &Instr = *I; | ||||
1405 | if (Instr.isPHI()) { | ||||
1406 | unsigned PHIDestReg = getPHIDestReg(Instr); | ||||
1407 | LLVM_DEBUG(dbgs() << "Extractking killed phi:\n")do { } while (false); | ||||
1408 | LLVM_DEBUG(Instr.dump())do { } while (false); | ||||
1409 | PHIs.insert(&Instr); | ||||
1410 | PHIInfo.addDest(PHIDestReg, Instr.getDebugLoc()); | ||||
1411 | storePHILinearizationInfoDest(PHIDestReg, Instr); | ||||
1412 | } | ||||
1413 | } | ||||
1414 | |||||
1415 | for (auto PI : PHIs) { | ||||
1416 | PI->eraseFromParent(); | ||||
1417 | } | ||||
1418 | } | ||||
1419 | |||||
1420 | static bool isPHIRegionIndex(SmallVector<unsigned, 2> PHIRegionIndices, | ||||
1421 | unsigned Index) { | ||||
1422 | return llvm::is_contained(PHIRegionIndices, Index); | ||||
1423 | } | ||||
1424 | |||||
1425 | bool AMDGPUMachineCFGStructurizer::shrinkPHI(MachineInstr &PHI, | ||||
1426 | SmallVector<unsigned, 2> &PHIIndices, | ||||
1427 | unsigned *ReplaceReg) { | ||||
1428 | return shrinkPHI(PHI, 0, nullptr, PHIIndices, ReplaceReg); | ||||
1429 | } | ||||
1430 | |||||
1431 | bool AMDGPUMachineCFGStructurizer::shrinkPHI(MachineInstr &PHI, | ||||
1432 | unsigned CombinedSourceReg, | ||||
1433 | MachineBasicBlock *SourceMBB, | ||||
1434 | SmallVector<unsigned, 2> &PHIIndices, | ||||
1435 | unsigned *ReplaceReg) { | ||||
1436 | LLVM_DEBUG(dbgs() << "Shrink PHI: ")do { } while (false); | ||||
1437 | LLVM_DEBUG(PHI.dump())do { } while (false); | ||||
1438 | LLVM_DEBUG(dbgs() << " to " << printReg(getPHIDestReg(PHI), TRI)do { } while (false) | ||||
1439 | << " = PHI(")do { } while (false); | ||||
1440 | |||||
1441 | bool Replaced = false; | ||||
1442 | unsigned NumInputs = getPHINumInputs(PHI); | ||||
1443 | int SingleExternalEntryIndex = -1; | ||||
1444 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1445 | if (!isPHIRegionIndex(PHIIndices, i)) { | ||||
1446 | if (SingleExternalEntryIndex == -1) { | ||||
1447 | // Single entry | ||||
1448 | SingleExternalEntryIndex = i; | ||||
1449 | } else { | ||||
1450 | // Multiple entries | ||||
1451 | SingleExternalEntryIndex = -2; | ||||
1452 | } | ||||
1453 | } | ||||
1454 | } | ||||
1455 | |||||
1456 | if (SingleExternalEntryIndex > -1) { | ||||
1457 | *ReplaceReg = getPHISourceReg(PHI, SingleExternalEntryIndex); | ||||
1458 | // We should not rewrite the code, we should only pick up the single value | ||||
1459 | // that represents the shrunk PHI. | ||||
1460 | Replaced = true; | ||||
1461 | } else { | ||||
1462 | MachineBasicBlock *MBB = PHI.getParent(); | ||||
1463 | MachineInstrBuilder MIB = | ||||
1464 | BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), | ||||
1465 | getPHIDestReg(PHI)); | ||||
1466 | if (SourceMBB) { | ||||
1467 | MIB.addReg(CombinedSourceReg); | ||||
1468 | MIB.addMBB(SourceMBB); | ||||
1469 | LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", "do { } while (false) | ||||
1470 | << printMBBReference(*SourceMBB))do { } while (false); | ||||
1471 | } | ||||
1472 | |||||
1473 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1474 | if (isPHIRegionIndex(PHIIndices, i)) { | ||||
1475 | continue; | ||||
1476 | } | ||||
1477 | unsigned SourceReg = getPHISourceReg(PHI, i); | ||||
1478 | MachineBasicBlock *SourcePred = getPHIPred(PHI, i); | ||||
1479 | MIB.addReg(SourceReg); | ||||
1480 | MIB.addMBB(SourcePred); | ||||
1481 | LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", "do { } while (false) | ||||
1482 | << printMBBReference(*SourcePred))do { } while (false); | ||||
1483 | } | ||||
1484 | LLVM_DEBUG(dbgs() << ")\n")do { } while (false); | ||||
1485 | } | ||||
1486 | PHI.eraseFromParent(); | ||||
1487 | return Replaced; | ||||
1488 | } | ||||
1489 | |||||
1490 | void AMDGPUMachineCFGStructurizer::replacePHI( | ||||
1491 | MachineInstr &PHI, unsigned CombinedSourceReg, MachineBasicBlock *LastMerge, | ||||
1492 | SmallVector<unsigned, 2> &PHIRegionIndices) { | ||||
1493 | LLVM_DEBUG(dbgs() << "Replace PHI: ")do { } while (false); | ||||
1494 | LLVM_DEBUG(PHI.dump())do { } while (false); | ||||
1495 | LLVM_DEBUG(dbgs() << " with " << printReg(getPHIDestReg(PHI), TRI)do { } while (false) | ||||
1496 | << " = PHI(")do { } while (false); | ||||
1497 | |||||
1498 | bool HasExternalEdge = false; | ||||
1499 | unsigned NumInputs = getPHINumInputs(PHI); | ||||
1500 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1501 | if (!isPHIRegionIndex(PHIRegionIndices, i)) { | ||||
1502 | HasExternalEdge = true; | ||||
1503 | } | ||||
1504 | } | ||||
1505 | |||||
1506 | if (HasExternalEdge) { | ||||
1507 | MachineBasicBlock *MBB = PHI.getParent(); | ||||
1508 | MachineInstrBuilder MIB = | ||||
1509 | BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), | ||||
1510 | getPHIDestReg(PHI)); | ||||
1511 | MIB.addReg(CombinedSourceReg); | ||||
1512 | MIB.addMBB(LastMerge); | ||||
1513 | LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", "do { } while (false) | ||||
1514 | << printMBBReference(*LastMerge))do { } while (false); | ||||
1515 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1516 | if (isPHIRegionIndex(PHIRegionIndices, i)) { | ||||
1517 | continue; | ||||
1518 | } | ||||
1519 | unsigned SourceReg = getPHISourceReg(PHI, i); | ||||
1520 | MachineBasicBlock *SourcePred = getPHIPred(PHI, i); | ||||
1521 | MIB.addReg(SourceReg); | ||||
1522 | MIB.addMBB(SourcePred); | ||||
1523 | LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", "do { } while (false) | ||||
1524 | << printMBBReference(*SourcePred))do { } while (false); | ||||
1525 | } | ||||
1526 | LLVM_DEBUG(dbgs() << ")\n")do { } while (false); | ||||
1527 | } else { | ||||
1528 | replaceRegisterWith(getPHIDestReg(PHI), CombinedSourceReg); | ||||
1529 | } | ||||
1530 | PHI.eraseFromParent(); | ||||
1531 | } | ||||
1532 | |||||
1533 | void AMDGPUMachineCFGStructurizer::replaceEntryPHI( | ||||
1534 | MachineInstr &PHI, unsigned CombinedSourceReg, MachineBasicBlock *IfMBB, | ||||
1535 | SmallVector<unsigned, 2> &PHIRegionIndices) { | ||||
1536 | LLVM_DEBUG(dbgs() << "Replace entry PHI: ")do { } while (false); | ||||
1537 | LLVM_DEBUG(PHI.dump())do { } while (false); | ||||
1538 | LLVM_DEBUG(dbgs() << " with ")do { } while (false); | ||||
1539 | |||||
1540 | unsigned NumInputs = getPHINumInputs(PHI); | ||||
1541 | unsigned NumNonRegionInputs = NumInputs; | ||||
1542 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1543 | if (isPHIRegionIndex(PHIRegionIndices, i)) { | ||||
1544 | NumNonRegionInputs--; | ||||
1545 | } | ||||
1546 | } | ||||
1547 | |||||
1548 | if (NumNonRegionInputs == 0) { | ||||
1549 | auto DestReg = getPHIDestReg(PHI); | ||||
1550 | replaceRegisterWith(DestReg, CombinedSourceReg); | ||||
1551 | LLVM_DEBUG(dbgs() << " register " << printReg(CombinedSourceReg, TRI)do { } while (false) | ||||
1552 | << "\n")do { } while (false); | ||||
1553 | PHI.eraseFromParent(); | ||||
1554 | } else { | ||||
1555 | LLVM_DEBUG(dbgs() << printReg(getPHIDestReg(PHI), TRI) << " = PHI(")do { } while (false); | ||||
1556 | MachineBasicBlock *MBB = PHI.getParent(); | ||||
1557 | MachineInstrBuilder MIB = | ||||
1558 | BuildMI(*MBB, PHI, PHI.getDebugLoc(), TII->get(TargetOpcode::PHI), | ||||
1559 | getPHIDestReg(PHI)); | ||||
1560 | MIB.addReg(CombinedSourceReg); | ||||
1561 | MIB.addMBB(IfMBB); | ||||
1562 | LLVM_DEBUG(dbgs() << printReg(CombinedSourceReg, TRI) << ", "do { } while (false) | ||||
1563 | << printMBBReference(*IfMBB))do { } while (false); | ||||
1564 | unsigned NumInputs = getPHINumInputs(PHI); | ||||
1565 | for (unsigned i = 0; i < NumInputs; ++i) { | ||||
1566 | if (isPHIRegionIndex(PHIRegionIndices, i)) { | ||||
1567 | continue; | ||||
1568 | } | ||||
1569 | unsigned SourceReg = getPHISourceReg(PHI, i); | ||||
1570 | MachineBasicBlock *SourcePred = getPHIPred(PHI, i); | ||||
1571 | MIB.addReg(SourceReg); | ||||
1572 | MIB.addMBB(SourcePred); | ||||
1573 | LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", "do { } while (false) | ||||
1574 | << printMBBReference(*SourcePred))do { } while (false); | ||||
1575 | } | ||||
1576 | LLVM_DEBUG(dbgs() << ")\n")do { } while (false); | ||||
1577 | PHI.eraseFromParent(); | ||||
1578 | } | ||||
1579 | } | ||||
1580 | |||||
1581 | void AMDGPUMachineCFGStructurizer::replaceLiveOutRegs( | ||||
1582 | MachineInstr &PHI, SmallVector<unsigned, 2> &PHIRegionIndices, | ||||
1583 | unsigned CombinedSourceReg, LinearizedRegion *LRegion) { | ||||
1584 | bool WasLiveOut = false; | ||||
1585 | for (auto PII : PHIRegionIndices) { | ||||
1586 | unsigned Reg = getPHISourceReg(PHI, PII); | ||||
1587 | if (LRegion->isLiveOut(Reg)) { | ||||
1588 | bool IsDead = true; | ||||
1589 | |||||
1590 | // Check if register is live out of the basic block | ||||
1591 | MachineBasicBlock *DefMBB = getDefInstr(Reg)->getParent(); | ||||
1592 | for (auto UI = MRI->use_begin(Reg), E = MRI->use_end(); UI != E; ++UI) { | ||||
1593 | if ((*UI).getParent()->getParent() != DefMBB) { | ||||
1594 | IsDead = false; | ||||
1595 | } | ||||
1596 | } | ||||
1597 | |||||
1598 | LLVM_DEBUG(dbgs() << "Register " << printReg(Reg, TRI) << " is "do { } while (false) | ||||
1599 | << (IsDead ? "dead" : "alive")do { } while (false) | ||||
1600 | << " after PHI replace\n")do { } while (false); | ||||
1601 | if (IsDead) { | ||||
1602 | LRegion->removeLiveOut(Reg); | ||||
1603 | } | ||||
1604 | WasLiveOut = true; | ||||
1605 | } | ||||
1606 | } | ||||
1607 | |||||
1608 | if (WasLiveOut) | ||||
1609 | LRegion->addLiveOut(CombinedSourceReg); | ||||
1610 | } | ||||
1611 | |||||
1612 | void AMDGPUMachineCFGStructurizer::rewriteRegionExitPHI(RegionMRT *Region, | ||||
1613 | MachineBasicBlock *LastMerge, | ||||
1614 | MachineInstr &PHI, | ||||
1615 | LinearizedRegion *LRegion) { | ||||
1616 | SmallVector<unsigned, 2> PHIRegionIndices; | ||||
1617 | getPHIRegionIndices(Region, PHI, PHIRegionIndices); | ||||
1618 | unsigned LinearizedSourceReg = | ||||
1619 | storePHILinearizationInfo(PHI, &PHIRegionIndices); | ||||
1620 | |||||
1621 | replacePHI(PHI, LinearizedSourceReg, LastMerge, PHIRegionIndices); | ||||
1622 | replaceLiveOutRegs(PHI, PHIRegionIndices, LinearizedSourceReg, LRegion); | ||||
1623 | } | ||||
1624 | |||||
1625 | void AMDGPUMachineCFGStructurizer::rewriteRegionEntryPHI(LinearizedRegion *Region, | ||||
1626 | MachineBasicBlock *IfMBB, | ||||
1627 | MachineInstr &PHI) { | ||||
1628 | SmallVector<unsigned, 2> PHINonRegionIndices; | ||||
1629 | getPHINonRegionIndices(Region, PHI, PHINonRegionIndices); | ||||
1630 | unsigned LinearizedSourceReg = | ||||
1631 | storePHILinearizationInfo(PHI, &PHINonRegionIndices); | ||||
1632 | replaceEntryPHI(PHI, LinearizedSourceReg, IfMBB, PHINonRegionIndices); | ||||
1633 | } | ||||
1634 | |||||
1635 | static void collectPHIs(MachineBasicBlock *MBB, | ||||
1636 | SmallVector<MachineInstr *, 2> &PHIs) { | ||||
1637 | for (auto &BBI : *MBB) { | ||||
1638 | if (BBI.isPHI()) { | ||||
1639 | PHIs.push_back(&BBI); | ||||
1640 | } | ||||
1641 | } | ||||
1642 | } | ||||
1643 | |||||
1644 | void AMDGPUMachineCFGStructurizer::rewriteRegionExitPHIs(RegionMRT *Region, | ||||
1645 | MachineBasicBlock *LastMerge, | ||||
1646 | LinearizedRegion *LRegion) { | ||||
1647 | SmallVector<MachineInstr *, 2> PHIs; | ||||
1648 | auto Exit = Region->getSucc(); | ||||
1649 | if (Exit == nullptr) | ||||
1650 | return; | ||||
1651 | |||||
1652 | collectPHIs(Exit, PHIs); | ||||
1653 | |||||
1654 | for (auto PHII : PHIs) { | ||||
1655 | rewriteRegionExitPHI(Region, LastMerge, *PHII, LRegion); | ||||
1656 | } | ||||
1657 | } | ||||
1658 | |||||
1659 | void AMDGPUMachineCFGStructurizer::rewriteRegionEntryPHIs(LinearizedRegion *Region, | ||||
1660 | MachineBasicBlock *IfMBB) { | ||||
1661 | SmallVector<MachineInstr *, 2> PHIs; | ||||
1662 | auto Entry = Region->getEntry(); | ||||
1663 | |||||
1664 | collectPHIs(Entry, PHIs); | ||||
1665 | |||||
1666 | for (auto PHII : PHIs) { | ||||
1667 | rewriteRegionEntryPHI(Region, IfMBB, *PHII); | ||||
1668 | } | ||||
1669 | } | ||||
1670 | |||||
1671 | void AMDGPUMachineCFGStructurizer::insertUnconditionalBranch(MachineBasicBlock *MBB, | ||||
1672 | MachineBasicBlock *Dest, | ||||
1673 | const DebugLoc &DL) { | ||||
1674 | LLVM_DEBUG(dbgs() << "Inserting unconditional branch: " << MBB->getNumber()do { } while (false) | ||||
1675 | << " -> " << Dest->getNumber() << "\n")do { } while (false); | ||||
1676 | MachineBasicBlock::instr_iterator Terminator = MBB->getFirstInstrTerminator(); | ||||
1677 | bool HasTerminator = Terminator != MBB->instr_end(); | ||||
1678 | if (HasTerminator) { | ||||
1679 | TII->ReplaceTailWithBranchTo(Terminator, Dest); | ||||
1680 | } | ||||
1681 | if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(Dest)) { | ||||
1682 | TII->insertUnconditionalBranch(*MBB, Dest, DL); | ||||
1683 | } | ||||
1684 | } | ||||
1685 | |||||
1686 | static MachineBasicBlock *getSingleExitNode(MachineFunction &MF) { | ||||
1687 | MachineBasicBlock *result = nullptr; | ||||
1688 | for (auto &MFI : MF) { | ||||
1689 | if (MFI.succ_size() == 0) { | ||||
1690 | if (result == nullptr) { | ||||
1691 | result = &MFI; | ||||
1692 | } else { | ||||
1693 | return nullptr; | ||||
1694 | } | ||||
1695 | } | ||||
1696 | } | ||||
1697 | |||||
1698 | return result; | ||||
1699 | } | ||||
1700 | |||||
1701 | static bool hasOneExitNode(MachineFunction &MF) { | ||||
1702 | return getSingleExitNode(MF) != nullptr; | ||||
1703 | } | ||||
1704 | |||||
1705 | MachineBasicBlock * | ||||
1706 | AMDGPUMachineCFGStructurizer::createLinearizedExitBlock(RegionMRT *Region) { | ||||
1707 | auto Exit = Region->getSucc(); | ||||
1708 | |||||
1709 | // If the exit is the end of the function, we just use the existing | ||||
1710 | MachineFunction *MF = Region->getEntry()->getParent(); | ||||
1711 | if (Exit == nullptr && hasOneExitNode(*MF)) { | ||||
1712 | return &(*(--(Region->getEntry()->getParent()->end()))); | ||||
1713 | } | ||||
1714 | |||||
1715 | MachineBasicBlock *LastMerge = MF->CreateMachineBasicBlock(); | ||||
1716 | if (Exit == nullptr) { | ||||
1717 | MachineFunction::iterator ExitIter = MF->end(); | ||||
1718 | MF->insert(ExitIter, LastMerge); | ||||
1719 | } else { | ||||
1720 | MachineFunction::iterator ExitIter = Exit->getIterator(); | ||||
1721 | MF->insert(ExitIter, LastMerge); | ||||
1722 | LastMerge->addSuccessor(Exit); | ||||
1723 | insertUnconditionalBranch(LastMerge, Exit); | ||||
1724 | LLVM_DEBUG(dbgs() << "Created exit block: " << LastMerge->getNumber()do { } while (false) | ||||
1725 | << "\n")do { } while (false); | ||||
1726 | } | ||||
1727 | return LastMerge; | ||||
1728 | } | ||||
1729 | |||||
1730 | void AMDGPUMachineCFGStructurizer::insertMergePHI(MachineBasicBlock *IfBB, | ||||
1731 | MachineBasicBlock *CodeBB, | ||||
1732 | MachineBasicBlock *MergeBB, | ||||
1733 | unsigned DestRegister, | ||||
1734 | unsigned IfSourceRegister, | ||||
1735 | unsigned CodeSourceRegister, | ||||
1736 | bool IsUndefIfSource) { | ||||
1737 | // If this is the function exit block, we don't need a phi. | ||||
1738 | if (MergeBB->succ_begin() == MergeBB->succ_end()) { | ||||
1739 | return; | ||||
1740 | } | ||||
1741 | LLVM_DEBUG(dbgs() << "Merge PHI (" << printMBBReference(*MergeBB)do { } while (false) | ||||
1742 | << "): " << printReg(DestRegister, TRI) << " = PHI("do { } while (false) | ||||
1743 | << printReg(IfSourceRegister, TRI) << ", "do { } while (false) | ||||
1744 | << printMBBReference(*IfBB)do { } while (false) | ||||
1745 | << printReg(CodeSourceRegister, TRI) << ", "do { } while (false) | ||||
1746 | << printMBBReference(*CodeBB) << ")\n")do { } while (false); | ||||
1747 | const DebugLoc &DL = MergeBB->findDebugLoc(MergeBB->begin()); | ||||
1748 | MachineInstrBuilder MIB = BuildMI(*MergeBB, MergeBB->instr_begin(), DL, | ||||
1749 | TII->get(TargetOpcode::PHI), DestRegister); | ||||
1750 | if (IsUndefIfSource && false) { | ||||
1751 | MIB.addReg(IfSourceRegister, RegState::Undef); | ||||
1752 | } else { | ||||
1753 | MIB.addReg(IfSourceRegister); | ||||
1754 | } | ||||
1755 | MIB.addMBB(IfBB); | ||||
1756 | MIB.addReg(CodeSourceRegister); | ||||
1757 | MIB.addMBB(CodeBB); | ||||
1758 | } | ||||
1759 | |||||
1760 | static void removeExternalCFGSuccessors(MachineBasicBlock *MBB) { | ||||
1761 | for (MachineBasicBlock::succ_iterator PI = MBB->succ_begin(), | ||||
1762 | E = MBB->succ_end(); | ||||
1763 | PI != E; ++PI) { | ||||
1764 | if ((*PI) != MBB) { | ||||
1765 | (MBB)->removeSuccessor(*PI); | ||||
1766 | } | ||||
1767 | } | ||||
1768 | } | ||||
1769 | |||||
1770 | static void removeExternalCFGEdges(MachineBasicBlock *StartMBB, | ||||
1771 | MachineBasicBlock *EndMBB) { | ||||
1772 | |||||
1773 | // We have to check against the StartMBB successor becasuse a | ||||
1774 | // structurized region with a loop will have the entry block split, | ||||
1775 | // and the backedge will go to the entry successor. | ||||
1776 | DenseSet<std::pair<MachineBasicBlock *, MachineBasicBlock *>> Succs; | ||||
1777 | unsigned SuccSize = StartMBB->succ_size(); | ||||
1778 | if (SuccSize > 0) { | ||||
1779 | MachineBasicBlock *StartMBBSucc = *(StartMBB->succ_begin()); | ||||
1780 | for (MachineBasicBlock::succ_iterator PI = EndMBB->succ_begin(), | ||||
1781 | E = EndMBB->succ_end(); | ||||
1782 | PI != E; ++PI) { | ||||
1783 | // Either we have a back-edge to the entry block, or a back-edge to the | ||||
1784 | // successor of the entry block since the block may be split. | ||||
1785 | if ((*PI) != StartMBB && | ||||
1786 | !((*PI) == StartMBBSucc && StartMBB != EndMBB && SuccSize == 1)) { | ||||
1787 | Succs.insert( | ||||
1788 | std::pair<MachineBasicBlock *, MachineBasicBlock *>(EndMBB, *PI)); | ||||
1789 | } | ||||
1790 | } | ||||
1791 | } | ||||
1792 | |||||
1793 | for (MachineBasicBlock::pred_iterator PI = StartMBB->pred_begin(), | ||||
1794 | E = StartMBB->pred_end(); | ||||
1795 | PI != E; ++PI) { | ||||
1796 | if ((*PI) != EndMBB) { | ||||
1797 | Succs.insert( | ||||
1798 | std::pair<MachineBasicBlock *, MachineBasicBlock *>(*PI, StartMBB)); | ||||
1799 | } | ||||
1800 | } | ||||
1801 | |||||
1802 | for (auto SI : Succs) { | ||||
1803 | std::pair<MachineBasicBlock *, MachineBasicBlock *> Edge = SI; | ||||
1804 | LLVM_DEBUG(dbgs() << "Removing edge: " << printMBBReference(*Edge.first)do { } while (false) | ||||
1805 | << " -> " << printMBBReference(*Edge.second) << "\n")do { } while (false); | ||||
1806 | Edge.first->removeSuccessor(Edge.second); | ||||
1807 | } | ||||
1808 | } | ||||
1809 | |||||
1810 | MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfBlock( | ||||
1811 | MachineBasicBlock *MergeBB, MachineBasicBlock *CodeBBStart, | ||||
1812 | MachineBasicBlock *CodeBBEnd, MachineBasicBlock *SelectBB, unsigned IfReg, | ||||
1813 | bool InheritPreds) { | ||||
1814 | MachineFunction *MF = MergeBB->getParent(); | ||||
1815 | MachineBasicBlock *IfBB = MF->CreateMachineBasicBlock(); | ||||
1816 | |||||
1817 | if (InheritPreds) { | ||||
1818 | for (MachineBasicBlock::pred_iterator PI = CodeBBStart->pred_begin(), | ||||
1819 | E = CodeBBStart->pred_end(); | ||||
1820 | PI != E; ++PI) { | ||||
1821 | if ((*PI) != CodeBBEnd) { | ||||
1822 | MachineBasicBlock *Pred = (*PI); | ||||
1823 | Pred->addSuccessor(IfBB); | ||||
1824 | } | ||||
1825 | } | ||||
1826 | } | ||||
1827 | |||||
1828 | removeExternalCFGEdges(CodeBBStart, CodeBBEnd); | ||||
1829 | |||||
1830 | auto CodeBBStartI = CodeBBStart->getIterator(); | ||||
1831 | auto CodeBBEndI = CodeBBEnd->getIterator(); | ||||
1832 | auto MergeIter = MergeBB->getIterator(); | ||||
1833 | MF->insert(MergeIter, IfBB); | ||||
1834 | MF->splice(MergeIter, CodeBBStartI, ++CodeBBEndI); | ||||
1835 | IfBB->addSuccessor(MergeBB); | ||||
1836 | IfBB->addSuccessor(CodeBBStart); | ||||
1837 | |||||
1838 | LLVM_DEBUG(dbgs() << "Created If block: " << IfBB->getNumber() << "\n")do { } while (false); | ||||
1839 | // Ensure that the MergeBB is a successor of the CodeEndBB. | ||||
1840 | if (!CodeBBEnd->isSuccessor(MergeBB)) | ||||
1841 | CodeBBEnd->addSuccessor(MergeBB); | ||||
1842 | |||||
1843 | LLVM_DEBUG(dbgs() << "Moved " << printMBBReference(*CodeBBStart)do { } while (false) | ||||
1844 | << " through " << printMBBReference(*CodeBBEnd) << "\n")do { } while (false); | ||||
1845 | |||||
1846 | // If we have a single predecessor we can find a reasonable debug location | ||||
1847 | MachineBasicBlock *SinglePred = | ||||
1848 | CodeBBStart->pred_size() == 1 ? *(CodeBBStart->pred_begin()) : nullptr; | ||||
1849 | const DebugLoc &DL = SinglePred | ||||
1850 | ? SinglePred->findDebugLoc(SinglePred->getFirstTerminator()) | ||||
1851 | : DebugLoc(); | ||||
1852 | |||||
1853 | Register Reg = | ||||
1854 | TII->insertEQ(IfBB, IfBB->begin(), DL, IfReg, | ||||
1855 | SelectBB->getNumber() /* CodeBBStart->getNumber() */); | ||||
1856 | if (&(*(IfBB->getParent()->begin())) == IfBB) { | ||||
1857 | TII->materializeImmediate(*IfBB, IfBB->begin(), DL, IfReg, | ||||
1858 | CodeBBStart->getNumber()); | ||||
1859 | } | ||||
1860 | MachineOperand RegOp = MachineOperand::CreateReg(Reg, false, false, true); | ||||
1861 | ArrayRef<MachineOperand> Cond(RegOp); | ||||
1862 | TII->insertBranch(*IfBB, MergeBB, CodeBBStart, Cond, DL); | ||||
1863 | |||||
1864 | return IfBB; | ||||
1865 | } | ||||
1866 | |||||
1867 | void AMDGPUMachineCFGStructurizer::ensureCondIsNotKilled( | ||||
1868 | SmallVector<MachineOperand, 1> Cond) { | ||||
1869 | if (Cond.size() != 1) | ||||
1870 | return; | ||||
1871 | if (!Cond[0].isReg()) | ||||
1872 | return; | ||||
1873 | |||||
1874 | Register CondReg = Cond[0].getReg(); | ||||
1875 | for (auto UI = MRI->use_begin(CondReg), E = MRI->use_end(); UI != E; ++UI) { | ||||
1876 | (*UI).setIsKill(false); | ||||
1877 | } | ||||
1878 | } | ||||
1879 | |||||
1880 | void AMDGPUMachineCFGStructurizer::rewriteCodeBBTerminator(MachineBasicBlock *CodeBB, | ||||
1881 | MachineBasicBlock *MergeBB, | ||||
1882 | unsigned BBSelectReg) { | ||||
1883 | MachineBasicBlock *TrueBB = nullptr; | ||||
1884 | MachineBasicBlock *FalseBB = nullptr; | ||||
1885 | SmallVector<MachineOperand, 1> Cond; | ||||
1886 | MachineBasicBlock *FallthroughBB = FallthroughMap[CodeBB]; | ||||
1887 | TII->analyzeBranch(*CodeBB, TrueBB, FalseBB, Cond); | ||||
1888 | |||||
1889 | const DebugLoc &DL = CodeBB->findDebugLoc(CodeBB->getFirstTerminator()); | ||||
1890 | |||||
1891 | if (FalseBB == nullptr && TrueBB == nullptr && FallthroughBB == nullptr) { | ||||
1892 | // This is an exit block, hence no successors. We will assign the | ||||
1893 | // bb select register to the entry block. | ||||
1894 | TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, | ||||
1895 | BBSelectReg, | ||||
1896 | CodeBB->getParent()->begin()->getNumber()); | ||||
1897 | insertUnconditionalBranch(CodeBB, MergeBB, DL); | ||||
1898 | return; | ||||
1899 | } | ||||
1900 | |||||
1901 | if (FalseBB == nullptr && TrueBB == nullptr) { | ||||
1902 | TrueBB = FallthroughBB; | ||||
1903 | } else if (TrueBB != nullptr) { | ||||
1904 | FalseBB = | ||||
1905 | (FallthroughBB && (FallthroughBB != TrueBB)) ? FallthroughBB : FalseBB; | ||||
1906 | } | ||||
1907 | |||||
1908 | if ((TrueBB != nullptr && FalseBB == nullptr) || (TrueBB == FalseBB)) { | ||||
1909 | TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, | ||||
1910 | BBSelectReg, TrueBB->getNumber()); | ||||
1911 | } else { | ||||
1912 | const TargetRegisterClass *RegClass = MRI->getRegClass(BBSelectReg); | ||||
1913 | Register TrueBBReg = MRI->createVirtualRegister(RegClass); | ||||
1914 | Register FalseBBReg = MRI->createVirtualRegister(RegClass); | ||||
1915 | TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, | ||||
1916 | TrueBBReg, TrueBB->getNumber()); | ||||
1917 | TII->materializeImmediate(*CodeBB, CodeBB->getFirstTerminator(), DL, | ||||
1918 | FalseBBReg, FalseBB->getNumber()); | ||||
1919 | ensureCondIsNotKilled(Cond); | ||||
1920 | TII->insertVectorSelect(*CodeBB, CodeBB->getFirstTerminator(), DL, | ||||
1921 | BBSelectReg, Cond, TrueBBReg, FalseBBReg); | ||||
1922 | } | ||||
1923 | |||||
1924 | insertUnconditionalBranch(CodeBB, MergeBB, DL); | ||||
1925 | } | ||||
1926 | |||||
1927 | MachineInstr *AMDGPUMachineCFGStructurizer::getDefInstr(unsigned Reg) { | ||||
1928 | if (MRI->def_begin(Reg) == MRI->def_end()) { | ||||
1929 | LLVM_DEBUG(dbgs() << "Register "do { } while (false) | ||||
1930 | << printReg(Reg, MRI->getTargetRegisterInfo())do { } while (false) | ||||
1931 | << " has NO defs\n")do { } while (false); | ||||
1932 | } else if (!MRI->hasOneDef(Reg)) { | ||||
1933 | LLVM_DEBUG(dbgs() << "Register "do { } while (false) | ||||
1934 | << printReg(Reg, MRI->getTargetRegisterInfo())do { } while (false) | ||||
1935 | << " has multiple defs\n")do { } while (false); | ||||
1936 | LLVM_DEBUG(dbgs() << "DEFS BEGIN:\n")do { } while (false); | ||||
1937 | for (auto DI = MRI->def_begin(Reg), DE = MRI->def_end(); DI != DE; ++DI) { | ||||
1938 | LLVM_DEBUG(DI->getParent()->dump())do { } while (false); | ||||
1939 | } | ||||
1940 | LLVM_DEBUG(dbgs() << "DEFS END\n")do { } while (false); | ||||
1941 | } | ||||
1942 | |||||
1943 | assert(MRI->hasOneDef(Reg) && "Register has multiple definitions")(static_cast<void> (0)); | ||||
1944 | return (*(MRI->def_begin(Reg))).getParent(); | ||||
1945 | } | ||||
1946 | |||||
1947 | void AMDGPUMachineCFGStructurizer::insertChainedPHI(MachineBasicBlock *IfBB, | ||||
1948 | MachineBasicBlock *CodeBB, | ||||
1949 | MachineBasicBlock *MergeBB, | ||||
1950 | LinearizedRegion *InnerRegion, | ||||
1951 | unsigned DestReg, | ||||
1952 | unsigned SourceReg) { | ||||
1953 | // In this function we know we are part of a chain already, so we need | ||||
1954 | // to add the registers to the existing chain, and rename the register | ||||
1955 | // inside the region. | ||||
1956 | bool IsSingleBB = InnerRegion->getEntry() == InnerRegion->getExit(); | ||||
1957 | MachineInstr *DefInstr = getDefInstr(SourceReg); | ||||
1958 | if (DefInstr->isPHI() && DefInstr->getParent() == CodeBB && IsSingleBB) { | ||||
1959 | // Handle the case where the def is a PHI-def inside a basic | ||||
1960 | // block, then we only need to do renaming. Special care needs to | ||||
1961 | // be taken if the PHI-def is part of an existing chain, or if a | ||||
1962 | // new one needs to be created. | ||||
1963 | InnerRegion->replaceRegisterInsideRegion(SourceReg, DestReg, true, MRI); | ||||
1964 | |||||
1965 | // We collect all PHI Information, and if we are at the region entry, | ||||
1966 | // all PHIs will be removed, and then re-introduced if needed. | ||||
1967 | storePHILinearizationInfoDest(DestReg, *DefInstr); | ||||
1968 | // We have picked up all the information we need now and can remove | ||||
1969 | // the PHI | ||||
1970 | PHIInfo.removeSource(DestReg, SourceReg, CodeBB); | ||||
1971 | DefInstr->eraseFromParent(); | ||||
1972 | } else { | ||||
1973 | // If this is not a phi-def, or it is a phi-def but from a linearized region | ||||
1974 | if (IsSingleBB && DefInstr->getParent() == InnerRegion->getEntry()) { | ||||
1975 | // If this is a single BB and the definition is in this block we | ||||
1976 | // need to replace any uses outside the region. | ||||
1977 | InnerRegion->replaceRegisterOutsideRegion(SourceReg, DestReg, false, MRI); | ||||
1978 | } | ||||
1979 | const TargetRegisterClass *RegClass = MRI->getRegClass(DestReg); | ||||
1980 | Register NextDestReg = MRI->createVirtualRegister(RegClass); | ||||
1981 | bool IsLastDef = PHIInfo.getNumSources(DestReg) == 1; | ||||
1982 | LLVM_DEBUG(dbgs() << "Insert Chained PHI\n")do { } while (false); | ||||
1983 | insertMergePHI(IfBB, InnerRegion->getExit(), MergeBB, DestReg, NextDestReg, | ||||
1984 | SourceReg, IsLastDef); | ||||
1985 | |||||
1986 | PHIInfo.removeSource(DestReg, SourceReg, CodeBB); | ||||
1987 | if (IsLastDef) { | ||||
1988 | const DebugLoc &DL = IfBB->findDebugLoc(IfBB->getFirstTerminator()); | ||||
1989 | TII->materializeImmediate(*IfBB, IfBB->getFirstTerminator(), DL, | ||||
1990 | NextDestReg, 0); | ||||
1991 | PHIInfo.deleteDef(DestReg); | ||||
1992 | } else { | ||||
1993 | PHIInfo.replaceDef(DestReg, NextDestReg); | ||||
1994 | } | ||||
1995 | } | ||||
1996 | } | ||||
1997 | |||||
1998 | bool AMDGPUMachineCFGStructurizer::containsDef(MachineBasicBlock *MBB, | ||||
1999 | LinearizedRegion *InnerRegion, | ||||
2000 | unsigned Register) { | ||||
2001 | return getDefInstr(Register)->getParent() == MBB || | ||||
2002 | InnerRegion->contains(getDefInstr(Register)->getParent()); | ||||
2003 | } | ||||
2004 | |||||
2005 | void AMDGPUMachineCFGStructurizer::rewriteLiveOutRegs(MachineBasicBlock *IfBB, | ||||
2006 | MachineBasicBlock *CodeBB, | ||||
2007 | MachineBasicBlock *MergeBB, | ||||
2008 | LinearizedRegion *InnerRegion, | ||||
2009 | LinearizedRegion *LRegion) { | ||||
2010 | DenseSet<unsigned> *LiveOuts = InnerRegion->getLiveOuts(); | ||||
2011 | SmallVector<unsigned, 4> OldLiveOuts; | ||||
2012 | bool IsSingleBB = InnerRegion->getEntry() == InnerRegion->getExit(); | ||||
2013 | for (auto OLI : *LiveOuts) { | ||||
2014 | OldLiveOuts.push_back(OLI); | ||||
2015 | } | ||||
2016 | |||||
2017 | for (auto LI : OldLiveOuts) { | ||||
2018 | LLVM_DEBUG(dbgs() << "LiveOut: " << printReg(LI, TRI))do { } while (false); | ||||
2019 | if (!containsDef(CodeBB, InnerRegion, LI) || | ||||
2020 | (!IsSingleBB && (getDefInstr(LI)->getParent() == LRegion->getExit()))) { | ||||
2021 | // If the register simly lives through the CodeBB, we don't have | ||||
2022 | // to rewrite anything since the register is not defined in this | ||||
2023 | // part of the code. | ||||
2024 | LLVM_DEBUG(dbgs() << "- through")do { } while (false); | ||||
2025 | continue; | ||||
2026 | } | ||||
2027 | LLVM_DEBUG(dbgs() << "\n")do { } while (false); | ||||
2028 | unsigned Reg = LI; | ||||
2029 | if (/*!PHIInfo.isSource(Reg) &&*/ Reg != InnerRegion->getBBSelectRegOut()) { | ||||
2030 | // If the register is live out, we do want to create a phi, | ||||
2031 | // unless it is from the Exit block, becasuse in that case there | ||||
2032 | // is already a PHI, and no need to create a new one. | ||||
2033 | |||||
2034 | // If the register is just a live out def and not part of a phi | ||||
2035 | // chain, we need to create a PHI node to handle the if region, | ||||
2036 | // and replace all uses outside of the region with the new dest | ||||
2037 | // register, unless it is the outgoing BB select register. We have | ||||
2038 | // already creaed phi nodes for these. | ||||
2039 | const TargetRegisterClass *RegClass = MRI->getRegClass(Reg); | ||||
2040 | Register PHIDestReg = MRI->createVirtualRegister(RegClass); | ||||
2041 | Register IfSourceReg = MRI->createVirtualRegister(RegClass); | ||||
2042 | // Create initializer, this value is never used, but is needed | ||||
2043 | // to satisfy SSA. | ||||
2044 | LLVM_DEBUG(dbgs() << "Initializer for reg: " << printReg(Reg) << "\n")do { } while (false); | ||||
2045 | TII->materializeImmediate(*IfBB, IfBB->getFirstTerminator(), DebugLoc(), | ||||
2046 | IfSourceReg, 0); | ||||
2047 | |||||
2048 | InnerRegion->replaceRegisterOutsideRegion(Reg, PHIDestReg, true, MRI); | ||||
2049 | LLVM_DEBUG(dbgs() << "Insert Non-Chained Live out PHI\n")do { } while (false); | ||||
2050 | insertMergePHI(IfBB, InnerRegion->getExit(), MergeBB, PHIDestReg, | ||||
2051 | IfSourceReg, Reg, true); | ||||
2052 | } | ||||
2053 | } | ||||
2054 | |||||
2055 | // Handle the chained definitions in PHIInfo, checking if this basic block | ||||
2056 | // is a source block for a definition. | ||||
2057 | SmallVector<unsigned, 4> Sources; | ||||
2058 | if (PHIInfo.findSourcesFromMBB(CodeBB, Sources)) { | ||||
2059 | LLVM_DEBUG(dbgs() << "Inserting PHI Live Out from "do { } while (false) | ||||
2060 | << printMBBReference(*CodeBB) << "\n")do { } while (false); | ||||
2061 | for (auto SI : Sources) { | ||||
2062 | unsigned DestReg; | ||||
2063 | PHIInfo.findDest(SI, CodeBB, DestReg); | ||||
2064 | insertChainedPHI(IfBB, CodeBB, MergeBB, InnerRegion, DestReg, SI); | ||||
2065 | } | ||||
2066 | LLVM_DEBUG(dbgs() << "Insertion done.\n")do { } while (false); | ||||
2067 | } | ||||
2068 | |||||
2069 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2070 | } | ||||
2071 | |||||
2072 | void AMDGPUMachineCFGStructurizer::prunePHIInfo(MachineBasicBlock *MBB) { | ||||
2073 | LLVM_DEBUG(dbgs() << "Before PHI Prune\n")do { } while (false); | ||||
2074 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2075 | SmallVector<std::tuple<unsigned, unsigned, MachineBasicBlock *>, 4> | ||||
2076 | ElimiatedSources; | ||||
2077 | for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; | ||||
2078 | ++DRI) { | ||||
2079 | |||||
2080 | unsigned DestReg = *DRI; | ||||
2081 | auto SE = PHIInfo.sources_end(DestReg); | ||||
2082 | |||||
2083 | bool MBBContainsPHISource = false; | ||||
2084 | // Check if there is a PHI source in this MBB | ||||
2085 | for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { | ||||
2086 | unsigned SourceReg = (*SRI).first; | ||||
2087 | MachineOperand *Def = &(*(MRI->def_begin(SourceReg))); | ||||
2088 | if (Def->getParent()->getParent() == MBB) { | ||||
2089 | MBBContainsPHISource = true; | ||||
2090 | } | ||||
2091 | } | ||||
2092 | |||||
2093 | // If so, all other sources are useless since we know this block | ||||
2094 | // is always executed when the region is executed. | ||||
2095 | if (MBBContainsPHISource) { | ||||
2096 | for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { | ||||
2097 | PHILinearize::PHISourceT Source = *SRI; | ||||
2098 | unsigned SourceReg = Source.first; | ||||
2099 | MachineBasicBlock *SourceMBB = Source.second; | ||||
2100 | MachineOperand *Def = &(*(MRI->def_begin(SourceReg))); | ||||
2101 | if (Def->getParent()->getParent() != MBB) { | ||||
2102 | ElimiatedSources.push_back( | ||||
2103 | std::make_tuple(DestReg, SourceReg, SourceMBB)); | ||||
2104 | } | ||||
2105 | } | ||||
2106 | } | ||||
2107 | } | ||||
2108 | |||||
2109 | // Remove the PHI sources that are in the given MBB | ||||
2110 | for (auto &SourceInfo : ElimiatedSources) { | ||||
2111 | PHIInfo.removeSource(std::get<0>(SourceInfo), std::get<1>(SourceInfo), | ||||
2112 | std::get<2>(SourceInfo)); | ||||
2113 | } | ||||
2114 | LLVM_DEBUG(dbgs() << "After PHI Prune\n")do { } while (false); | ||||
2115 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2116 | } | ||||
2117 | |||||
2118 | void AMDGPUMachineCFGStructurizer::createEntryPHI(LinearizedRegion *CurrentRegion, | ||||
2119 | unsigned DestReg) { | ||||
2120 | MachineBasicBlock *Entry = CurrentRegion->getEntry(); | ||||
2121 | MachineBasicBlock *Exit = CurrentRegion->getExit(); | ||||
2122 | |||||
2123 | LLVM_DEBUG(dbgs() << "RegionExit: " << Exit->getNumber() << " Pred: "do { } while (false) | ||||
2124 | << (*(Entry->pred_begin()))->getNumber() << "\n")do { } while (false); | ||||
2125 | |||||
2126 | int NumSources = 0; | ||||
2127 | auto SE = PHIInfo.sources_end(DestReg); | ||||
2128 | |||||
2129 | for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { | ||||
2130 | NumSources++; | ||||
2131 | } | ||||
2132 | |||||
2133 | if (NumSources == 1) { | ||||
2134 | auto SRI = PHIInfo.sources_begin(DestReg); | ||||
2135 | unsigned SourceReg = (*SRI).first; | ||||
2136 | replaceRegisterWith(DestReg, SourceReg); | ||||
2137 | } else { | ||||
2138 | const DebugLoc &DL = Entry->findDebugLoc(Entry->begin()); | ||||
2139 | MachineInstrBuilder MIB = BuildMI(*Entry, Entry->instr_begin(), DL, | ||||
2140 | TII->get(TargetOpcode::PHI), DestReg); | ||||
2141 | LLVM_DEBUG(dbgs() << "Entry PHI " << printReg(DestReg, TRI) << " = PHI(")do { } while (false); | ||||
2142 | |||||
2143 | unsigned CurrentBackedgeReg = 0; | ||||
2144 | |||||
2145 | for (auto SRI = PHIInfo.sources_begin(DestReg); SRI != SE; ++SRI) { | ||||
2146 | unsigned SourceReg = (*SRI).first; | ||||
2147 | |||||
2148 | if (CurrentRegion->contains((*SRI).second)) { | ||||
2149 | if (CurrentBackedgeReg == 0) { | ||||
2150 | CurrentBackedgeReg = SourceReg; | ||||
2151 | } else { | ||||
2152 | MachineInstr *PHIDefInstr = getDefInstr(SourceReg); | ||||
2153 | MachineBasicBlock *PHIDefMBB = PHIDefInstr->getParent(); | ||||
2154 | const TargetRegisterClass *RegClass = | ||||
2155 | MRI->getRegClass(CurrentBackedgeReg); | ||||
2156 | Register NewBackedgeReg = MRI->createVirtualRegister(RegClass); | ||||
2157 | MachineInstrBuilder BackedgePHI = | ||||
2158 | BuildMI(*PHIDefMBB, PHIDefMBB->instr_begin(), DL, | ||||
2159 | TII->get(TargetOpcode::PHI), NewBackedgeReg); | ||||
2160 | BackedgePHI.addReg(CurrentBackedgeReg); | ||||
2161 | BackedgePHI.addMBB(getPHIPred(*PHIDefInstr, 0)); | ||||
2162 | BackedgePHI.addReg(getPHISourceReg(*PHIDefInstr, 1)); | ||||
2163 | BackedgePHI.addMBB((*SRI).second); | ||||
2164 | CurrentBackedgeReg = NewBackedgeReg; | ||||
2165 | LLVM_DEBUG(dbgs()do { } while (false) | ||||
2166 | << "Inserting backedge PHI: "do { } while (false) | ||||
2167 | << printReg(NewBackedgeReg, TRI) << " = PHI("do { } while (false) | ||||
2168 | << printReg(CurrentBackedgeReg, TRI) << ", "do { } while (false) | ||||
2169 | << printMBBReference(*getPHIPred(*PHIDefInstr, 0)) << ", "do { } while (false) | ||||
2170 | << printReg(getPHISourceReg(*PHIDefInstr, 1), TRI) << ", "do { } while (false) | ||||
2171 | << printMBBReference(*(*SRI).second))do { } while (false); | ||||
2172 | } | ||||
2173 | } else { | ||||
2174 | MIB.addReg(SourceReg); | ||||
2175 | MIB.addMBB((*SRI).second); | ||||
2176 | LLVM_DEBUG(dbgs() << printReg(SourceReg, TRI) << ", "do { } while (false) | ||||
2177 | << printMBBReference(*(*SRI).second) << ", ")do { } while (false); | ||||
2178 | } | ||||
2179 | } | ||||
2180 | |||||
2181 | // Add the final backedge register source to the entry phi | ||||
2182 | if (CurrentBackedgeReg != 0) { | ||||
2183 | MIB.addReg(CurrentBackedgeReg); | ||||
2184 | MIB.addMBB(Exit); | ||||
2185 | LLVM_DEBUG(dbgs() << printReg(CurrentBackedgeReg, TRI) << ", "do { } while (false) | ||||
2186 | << printMBBReference(*Exit) << ")\n")do { } while (false); | ||||
2187 | } else { | ||||
2188 | LLVM_DEBUG(dbgs() << ")\n")do { } while (false); | ||||
2189 | } | ||||
2190 | } | ||||
2191 | } | ||||
2192 | |||||
2193 | void AMDGPUMachineCFGStructurizer::createEntryPHIs(LinearizedRegion *CurrentRegion) { | ||||
2194 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2195 | |||||
2196 | for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; | ||||
2197 | ++DRI) { | ||||
2198 | |||||
2199 | unsigned DestReg = *DRI; | ||||
2200 | createEntryPHI(CurrentRegion, DestReg); | ||||
2201 | } | ||||
2202 | PHIInfo.clear(); | ||||
2203 | } | ||||
2204 | |||||
2205 | void AMDGPUMachineCFGStructurizer::replaceRegisterWith( | ||||
2206 | unsigned Register, class Register NewRegister) { | ||||
2207 | assert(Register != NewRegister && "Cannot replace a reg with itself")(static_cast<void> (0)); | ||||
2208 | |||||
2209 | for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(Register), | ||||
2210 | E = MRI->reg_end(); | ||||
2211 | I != E;) { | ||||
2212 | MachineOperand &O = *I; | ||||
2213 | ++I; | ||||
2214 | if (NewRegister.isPhysical()) { | ||||
2215 | LLVM_DEBUG(dbgs() << "Trying to substitute physical register: "do { } while (false) | ||||
2216 | << printReg(NewRegister, MRI->getTargetRegisterInfo())do { } while (false) | ||||
2217 | << "\n")do { } while (false); | ||||
2218 | llvm_unreachable("Cannot substitute physical registers")__builtin_unreachable(); | ||||
2219 | // We don't handle physical registers, but if we need to | ||||
2220 | // in the future This is how we do it: | ||||
2221 | // O.substPhysReg(NewRegister, *TRI); | ||||
2222 | } else { | ||||
2223 | LLVM_DEBUG(dbgs() << "Replacing register: "do { } while (false) | ||||
2224 | << printReg(Register, MRI->getTargetRegisterInfo())do { } while (false) | ||||
2225 | << " with "do { } while (false) | ||||
2226 | << printReg(NewRegister, MRI->getTargetRegisterInfo())do { } while (false) | ||||
2227 | << "\n")do { } while (false); | ||||
2228 | O.setReg(NewRegister); | ||||
2229 | } | ||||
2230 | } | ||||
2231 | PHIInfo.deleteDef(Register); | ||||
2232 | |||||
2233 | getRegionMRT()->replaceLiveOutReg(Register, NewRegister); | ||||
2234 | |||||
2235 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2236 | } | ||||
2237 | |||||
2238 | void AMDGPUMachineCFGStructurizer::resolvePHIInfos(MachineBasicBlock *FunctionEntry) { | ||||
2239 | LLVM_DEBUG(dbgs() << "Resolve PHI Infos\n")do { } while (false); | ||||
2240 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2241 | for (auto DRI = PHIInfo.dests_begin(), DE = PHIInfo.dests_end(); DRI != DE; | ||||
2242 | ++DRI) { | ||||
2243 | unsigned DestReg = *DRI; | ||||
2244 | LLVM_DEBUG(dbgs() << "DestReg: " << printReg(DestReg, TRI) << "\n")do { } while (false); | ||||
2245 | auto SRI = PHIInfo.sources_begin(DestReg); | ||||
2246 | unsigned SourceReg = (*SRI).first; | ||||
2247 | LLVM_DEBUG(dbgs() << "DestReg: " << printReg(DestReg, TRI)do { } while (false) | ||||
2248 | << " SourceReg: " << printReg(SourceReg, TRI) << "\n")do { } while (false); | ||||
2249 | |||||
2250 | assert(PHIInfo.sources_end(DestReg) == ++SRI &&(static_cast<void> (0)) | ||||
2251 | "More than one phi source in entry node")(static_cast<void> (0)); | ||||
2252 | replaceRegisterWith(DestReg, SourceReg); | ||||
2253 | } | ||||
2254 | } | ||||
2255 | |||||
2256 | static bool isFunctionEntryBlock(MachineBasicBlock *MBB) { | ||||
2257 | return ((&(*(MBB->getParent()->begin()))) == MBB); | ||||
2258 | } | ||||
2259 | |||||
2260 | MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfRegion( | ||||
2261 | MachineBasicBlock *MergeBB, MachineBasicBlock *CodeBB, | ||||
2262 | LinearizedRegion *CurrentRegion, unsigned BBSelectRegIn, | ||||
2263 | unsigned BBSelectRegOut) { | ||||
2264 | if (isFunctionEntryBlock(CodeBB) && !CurrentRegion->getHasLoop()) { | ||||
2265 | // Handle non-loop function entry block. | ||||
2266 | // We need to allow loops to the entry block and then | ||||
2267 | rewriteCodeBBTerminator(CodeBB, MergeBB, BBSelectRegOut); | ||||
2268 | resolvePHIInfos(CodeBB); | ||||
2269 | removeExternalCFGSuccessors(CodeBB); | ||||
2270 | CodeBB->addSuccessor(MergeBB); | ||||
2271 | CurrentRegion->addMBB(CodeBB); | ||||
2272 | return nullptr; | ||||
2273 | } | ||||
2274 | if (CurrentRegion->getEntry() == CodeBB && !CurrentRegion->getHasLoop()) { | ||||
2275 | // Handle non-loop region entry block. | ||||
2276 | MachineFunction *MF = MergeBB->getParent(); | ||||
| |||||
2277 | auto MergeIter = MergeBB->getIterator(); | ||||
2278 | auto CodeBBStartIter = CodeBB->getIterator(); | ||||
2279 | auto CodeBBEndIter = ++(CodeBB->getIterator()); | ||||
2280 | if (CodeBBEndIter != MergeIter) { | ||||
2281 | MF->splice(MergeIter, CodeBBStartIter, CodeBBEndIter); | ||||
2282 | } | ||||
2283 | rewriteCodeBBTerminator(CodeBB, MergeBB, BBSelectRegOut); | ||||
2284 | prunePHIInfo(CodeBB); | ||||
2285 | createEntryPHIs(CurrentRegion); | ||||
2286 | removeExternalCFGSuccessors(CodeBB); | ||||
2287 | CodeBB->addSuccessor(MergeBB); | ||||
2288 | CurrentRegion->addMBB(CodeBB); | ||||
2289 | return nullptr; | ||||
2290 | } else { | ||||
2291 | // Handle internal block. | ||||
2292 | const TargetRegisterClass *RegClass = MRI->getRegClass(BBSelectRegIn); | ||||
2293 | Register CodeBBSelectReg = MRI->createVirtualRegister(RegClass); | ||||
2294 | rewriteCodeBBTerminator(CodeBB, MergeBB, CodeBBSelectReg); | ||||
2295 | bool IsRegionEntryBB = CurrentRegion->getEntry() == CodeBB; | ||||
2296 | MachineBasicBlock *IfBB = createIfBlock(MergeBB, CodeBB, CodeBB, CodeBB, | ||||
2297 | BBSelectRegIn, IsRegionEntryBB); | ||||
2298 | CurrentRegion->addMBB(IfBB); | ||||
2299 | // If this is the entry block we need to make the If block the new | ||||
2300 | // linearized region entry. | ||||
2301 | if (IsRegionEntryBB
| ||||
2302 | CurrentRegion->setEntry(IfBB); | ||||
2303 | |||||
2304 | if (CurrentRegion->getHasLoop()) { | ||||
2305 | MachineBasicBlock *RegionExit = CurrentRegion->getExit(); | ||||
2306 | MachineBasicBlock *ETrueBB = nullptr; | ||||
2307 | MachineBasicBlock *EFalseBB = nullptr; | ||||
2308 | SmallVector<MachineOperand, 1> ECond; | ||||
2309 | |||||
2310 | const DebugLoc &DL = DebugLoc(); | ||||
2311 | TII->analyzeBranch(*RegionExit, ETrueBB, EFalseBB, ECond); | ||||
2312 | TII->removeBranch(*RegionExit); | ||||
2313 | |||||
2314 | // We need to create a backedge if there is a loop | ||||
2315 | Register Reg = TII->insertNE( | ||||
2316 | RegionExit, RegionExit->instr_end(), DL, | ||||
2317 | CurrentRegion->getRegionMRT()->getInnerOutputRegister(), | ||||
2318 | CurrentRegion->getRegionMRT()->getEntry()->getNumber()); | ||||
2319 | MachineOperand RegOp = | ||||
2320 | MachineOperand::CreateReg(Reg, false, false, true); | ||||
2321 | ArrayRef<MachineOperand> Cond(RegOp); | ||||
2322 | LLVM_DEBUG(dbgs() << "RegionExitReg: ")do { } while (false); | ||||
2323 | LLVM_DEBUG(Cond[0].print(dbgs(), TRI))do { } while (false); | ||||
2324 | LLVM_DEBUG(dbgs() << "\n")do { } while (false); | ||||
2325 | TII->insertBranch(*RegionExit, CurrentRegion->getEntry(), RegionExit, | ||||
2326 | Cond, DebugLoc()); | ||||
2327 | RegionExit->addSuccessor(CurrentRegion->getEntry()); | ||||
2328 | } | ||||
2329 | } | ||||
2330 | CurrentRegion->addMBB(CodeBB); | ||||
2331 | LinearizedRegion InnerRegion(CodeBB, MRI, TRI, PHIInfo); | ||||
2332 | |||||
2333 | InnerRegion.setParent(CurrentRegion); | ||||
2334 | LLVM_DEBUG(dbgs() << "Insert BB Select PHI (BB)\n")do { } while (false); | ||||
2335 | insertMergePHI(IfBB, CodeBB, MergeBB, BBSelectRegOut, BBSelectRegIn, | ||||
2336 | CodeBBSelectReg); | ||||
2337 | InnerRegion.addMBB(MergeBB); | ||||
2338 | |||||
2339 | LLVM_DEBUG(InnerRegion.print(dbgs(), TRI))do { } while (false); | ||||
2340 | rewriteLiveOutRegs(IfBB, CodeBB, MergeBB, &InnerRegion, CurrentRegion); | ||||
2341 | extractKilledPHIs(CodeBB); | ||||
2342 | if (IsRegionEntryBB
| ||||
2343 | createEntryPHIs(CurrentRegion); | ||||
2344 | } | ||||
2345 | return IfBB; | ||||
2346 | } | ||||
2347 | } | ||||
2348 | |||||
2349 | MachineBasicBlock *AMDGPUMachineCFGStructurizer::createIfRegion( | ||||
2350 | MachineBasicBlock *MergeBB, LinearizedRegion *InnerRegion, | ||||
2351 | LinearizedRegion *CurrentRegion, MachineBasicBlock *SelectBB, | ||||
2352 | unsigned BBSelectRegIn, unsigned BBSelectRegOut) { | ||||
2353 | unsigned CodeBBSelectReg = | ||||
2354 | InnerRegion->getRegionMRT()->getInnerOutputRegister(); | ||||
2355 | MachineBasicBlock *CodeEntryBB = InnerRegion->getEntry(); | ||||
2356 | MachineBasicBlock *CodeExitBB = InnerRegion->getExit(); | ||||
2357 | MachineBasicBlock *IfBB = createIfBlock(MergeBB, CodeEntryBB, CodeExitBB, | ||||
2358 | SelectBB, BBSelectRegIn, true); | ||||
2359 | CurrentRegion->addMBB(IfBB); | ||||
2360 | bool isEntry = CurrentRegion->getEntry() == InnerRegion->getEntry(); | ||||
2361 | if (isEntry) { | ||||
2362 | |||||
2363 | if (CurrentRegion->getHasLoop()) { | ||||
2364 | MachineBasicBlock *RegionExit = CurrentRegion->getExit(); | ||||
2365 | MachineBasicBlock *ETrueBB = nullptr; | ||||
2366 | MachineBasicBlock *EFalseBB = nullptr; | ||||
2367 | SmallVector<MachineOperand, 1> ECond; | ||||
2368 | |||||
2369 | const DebugLoc &DL = DebugLoc(); | ||||
2370 | TII->analyzeBranch(*RegionExit, ETrueBB, EFalseBB, ECond); | ||||
2371 | TII->removeBranch(*RegionExit); | ||||
2372 | |||||
2373 | // We need to create a backedge if there is a loop | ||||
2374 | Register Reg = | ||||
2375 | TII->insertNE(RegionExit, RegionExit->instr_end(), DL, | ||||
2376 | CurrentRegion->getRegionMRT()->getInnerOutputRegister(), | ||||
2377 | CurrentRegion->getRegionMRT()->getEntry()->getNumber()); | ||||
2378 | MachineOperand RegOp = MachineOperand::CreateReg(Reg, false, false, true); | ||||
2379 | ArrayRef<MachineOperand> Cond(RegOp); | ||||
2380 | LLVM_DEBUG(dbgs() << "RegionExitReg: ")do { } while (false); | ||||
2381 | LLVM_DEBUG(Cond[0].print(dbgs(), TRI))do { } while (false); | ||||
2382 | LLVM_DEBUG(dbgs() << "\n")do { } while (false); | ||||
2383 | TII->insertBranch(*RegionExit, CurrentRegion->getEntry(), RegionExit, | ||||
2384 | Cond, DebugLoc()); | ||||
2385 | RegionExit->addSuccessor(IfBB); | ||||
2386 | } | ||||
2387 | } | ||||
2388 | CurrentRegion->addMBBs(InnerRegion); | ||||
2389 | LLVM_DEBUG(dbgs() << "Insert BB Select PHI (region)\n")do { } while (false); | ||||
2390 | insertMergePHI(IfBB, CodeExitBB, MergeBB, BBSelectRegOut, BBSelectRegIn, | ||||
2391 | CodeBBSelectReg); | ||||
2392 | |||||
2393 | rewriteLiveOutRegs(IfBB, /* CodeEntryBB */ CodeExitBB, MergeBB, InnerRegion, | ||||
2394 | CurrentRegion); | ||||
2395 | |||||
2396 | rewriteRegionEntryPHIs(InnerRegion, IfBB); | ||||
2397 | |||||
2398 | if (isEntry) { | ||||
2399 | CurrentRegion->setEntry(IfBB); | ||||
2400 | } | ||||
2401 | |||||
2402 | if (isEntry) { | ||||
2403 | createEntryPHIs(CurrentRegion); | ||||
2404 | } | ||||
2405 | |||||
2406 | return IfBB; | ||||
2407 | } | ||||
2408 | |||||
2409 | void AMDGPUMachineCFGStructurizer::splitLoopPHI(MachineInstr &PHI, | ||||
2410 | MachineBasicBlock *Entry, | ||||
2411 | MachineBasicBlock *EntrySucc, | ||||
2412 | LinearizedRegion *LRegion) { | ||||
2413 | SmallVector<unsigned, 2> PHIRegionIndices; | ||||
2414 | getPHIRegionIndices(LRegion, PHI, PHIRegionIndices); | ||||
2415 | |||||
2416 | assert(PHIRegionIndices.size() == 1)(static_cast<void> (0)); | ||||
2417 | |||||
2418 | unsigned RegionIndex = PHIRegionIndices[0]; | ||||
2419 | unsigned RegionSourceReg = getPHISourceReg(PHI, RegionIndex); | ||||
2420 | MachineBasicBlock *RegionSourceMBB = getPHIPred(PHI, RegionIndex); | ||||
2421 | unsigned PHIDest = getPHIDestReg(PHI); | ||||
2422 | unsigned PHISource = PHIDest; | ||||
2423 | unsigned ReplaceReg; | ||||
2424 | |||||
2425 | if (shrinkPHI(PHI, PHIRegionIndices, &ReplaceReg)) { | ||||
2426 | PHISource = ReplaceReg; | ||||
2427 | } | ||||
2428 | |||||
2429 | const TargetRegisterClass *RegClass = MRI->getRegClass(PHIDest); | ||||
2430 | Register NewDestReg = MRI->createVirtualRegister(RegClass); | ||||
2431 | LRegion->replaceRegisterInsideRegion(PHIDest, NewDestReg, false, MRI); | ||||
2432 | MachineInstrBuilder MIB = | ||||
2433 | BuildMI(*EntrySucc, EntrySucc->instr_begin(), PHI.getDebugLoc(), | ||||
2434 | TII->get(TargetOpcode::PHI), NewDestReg); | ||||
2435 | LLVM_DEBUG(dbgs() << "Split Entry PHI " << printReg(NewDestReg, TRI)do { } while (false) | ||||
2436 | << " = PHI(")do { } while (false); | ||||
2437 | MIB.addReg(PHISource); | ||||
2438 | MIB.addMBB(Entry); | ||||
2439 | LLVM_DEBUG(dbgs() << printReg(PHISource, TRI) << ", "do { } while (false) | ||||
2440 | << printMBBReference(*Entry))do { } while (false); | ||||
2441 | MIB.addReg(RegionSourceReg); | ||||
2442 | MIB.addMBB(RegionSourceMBB); | ||||
2443 | LLVM_DEBUG(dbgs() << " ," << printReg(RegionSourceReg, TRI) << ", "do { } while (false) | ||||
2444 | << printMBBReference(*RegionSourceMBB) << ")\n")do { } while (false); | ||||
2445 | } | ||||
2446 | |||||
2447 | void AMDGPUMachineCFGStructurizer::splitLoopPHIs(MachineBasicBlock *Entry, | ||||
2448 | MachineBasicBlock *EntrySucc, | ||||
2449 | LinearizedRegion *LRegion) { | ||||
2450 | SmallVector<MachineInstr *, 2> PHIs; | ||||
2451 | collectPHIs(Entry, PHIs); | ||||
2452 | |||||
2453 | for (auto PHII : PHIs) { | ||||
2454 | splitLoopPHI(*PHII, Entry, EntrySucc, LRegion); | ||||
2455 | } | ||||
2456 | } | ||||
2457 | |||||
2458 | // Split the exit block so that we can insert a end control flow | ||||
2459 | MachineBasicBlock * | ||||
2460 | AMDGPUMachineCFGStructurizer::splitExit(LinearizedRegion *LRegion) { | ||||
2461 | auto MRTRegion = LRegion->getRegionMRT(); | ||||
2462 | auto Exit = LRegion->getExit(); | ||||
2463 | auto MF = Exit->getParent(); | ||||
2464 | auto Succ = MRTRegion->getSucc(); | ||||
2465 | |||||
2466 | auto NewExit = MF->CreateMachineBasicBlock(); | ||||
2467 | auto AfterExitIter = Exit->getIterator(); | ||||
2468 | AfterExitIter++; | ||||
2469 | MF->insert(AfterExitIter, NewExit); | ||||
2470 | Exit->removeSuccessor(Succ); | ||||
2471 | Exit->addSuccessor(NewExit); | ||||
2472 | NewExit->addSuccessor(Succ); | ||||
2473 | insertUnconditionalBranch(NewExit, Succ); | ||||
2474 | LRegion->addMBB(NewExit); | ||||
2475 | LRegion->setExit(NewExit); | ||||
2476 | |||||
2477 | LLVM_DEBUG(dbgs() << "Created new exit block: " << NewExit->getNumber()do { } while (false) | ||||
2478 | << "\n")do { } while (false); | ||||
2479 | |||||
2480 | // Replace any PHI Predecessors in the successor with NewExit | ||||
2481 | for (auto &II : *Succ) { | ||||
2482 | MachineInstr &Instr = II; | ||||
2483 | |||||
2484 | // If we are past the PHI instructions we are done | ||||
2485 | if (!Instr.isPHI()) | ||||
2486 | break; | ||||
2487 | |||||
2488 | int numPreds = getPHINumInputs(Instr); | ||||
2489 | for (int i = 0; i < numPreds; ++i) { | ||||
2490 | auto Pred = getPHIPred(Instr, i); | ||||
2491 | if (Pred == Exit) { | ||||
2492 | setPhiPred(Instr, i, NewExit); | ||||
2493 | } | ||||
2494 | } | ||||
2495 | } | ||||
2496 | |||||
2497 | return NewExit; | ||||
2498 | } | ||||
2499 | |||||
2500 | static MachineBasicBlock *split(MachineBasicBlock::iterator I) { | ||||
2501 | // Create the fall-through block. | ||||
2502 | MachineBasicBlock *MBB = (*I).getParent(); | ||||
2503 | MachineFunction *MF = MBB->getParent(); | ||||
2504 | MachineBasicBlock *SuccMBB = MF->CreateMachineBasicBlock(); | ||||
2505 | auto MBBIter = ++(MBB->getIterator()); | ||||
2506 | MF->insert(MBBIter, SuccMBB); | ||||
2507 | SuccMBB->transferSuccessorsAndUpdatePHIs(MBB); | ||||
2508 | MBB->addSuccessor(SuccMBB); | ||||
2509 | |||||
2510 | // Splice the code over. | ||||
2511 | SuccMBB->splice(SuccMBB->end(), MBB, I, MBB->end()); | ||||
2512 | |||||
2513 | return SuccMBB; | ||||
2514 | } | ||||
2515 | |||||
2516 | // Split the entry block separating PHI-nodes and the rest of the code | ||||
2517 | // This is needed to insert an initializer for the bb select register | ||||
2518 | // inloop regions. | ||||
2519 | |||||
2520 | MachineBasicBlock * | ||||
2521 | AMDGPUMachineCFGStructurizer::splitEntry(LinearizedRegion *LRegion) { | ||||
2522 | MachineBasicBlock *Entry = LRegion->getEntry(); | ||||
2523 | MachineBasicBlock *EntrySucc = split(Entry->getFirstNonPHI()); | ||||
2524 | MachineBasicBlock *Exit = LRegion->getExit(); | ||||
2525 | |||||
2526 | LLVM_DEBUG(dbgs() << "Split " << printMBBReference(*Entry) << " to "do { } while (false) | ||||
2527 | << printMBBReference(*Entry) << " -> "do { } while (false) | ||||
2528 | << printMBBReference(*EntrySucc) << "\n")do { } while (false); | ||||
2529 | LRegion->addMBB(EntrySucc); | ||||
2530 | |||||
2531 | // Make the backedge go to Entry Succ | ||||
2532 | if (Exit->isSuccessor(Entry)) { | ||||
2533 | Exit->removeSuccessor(Entry); | ||||
2534 | } | ||||
2535 | Exit->addSuccessor(EntrySucc); | ||||
2536 | MachineInstr &Branch = *(Exit->instr_rbegin()); | ||||
2537 | for (auto &UI : Branch.uses()) { | ||||
2538 | if (UI.isMBB() && UI.getMBB() == Entry) { | ||||
2539 | UI.setMBB(EntrySucc); | ||||
2540 | } | ||||
2541 | } | ||||
2542 | |||||
2543 | splitLoopPHIs(Entry, EntrySucc, LRegion); | ||||
2544 | |||||
2545 | return EntrySucc; | ||||
2546 | } | ||||
2547 | |||||
2548 | LinearizedRegion * | ||||
2549 | AMDGPUMachineCFGStructurizer::initLinearizedRegion(RegionMRT *Region) { | ||||
2550 | LinearizedRegion *LRegion = Region->getLinearizedRegion(); | ||||
2551 | LRegion->initLiveOut(Region, MRI, TRI, PHIInfo); | ||||
2552 | LRegion->setEntry(Region->getEntry()); | ||||
2553 | return LRegion; | ||||
2554 | } | ||||
2555 | |||||
2556 | static void removeOldExitPreds(RegionMRT *Region) { | ||||
2557 | MachineBasicBlock *Exit = Region->getSucc(); | ||||
2558 | if (Exit == nullptr) { | ||||
2559 | return; | ||||
2560 | } | ||||
2561 | for (MachineBasicBlock::pred_iterator PI = Exit->pred_begin(), | ||||
2562 | E = Exit->pred_end(); | ||||
2563 | PI != E; ++PI) { | ||||
2564 | if (Region->contains(*PI)) { | ||||
2565 | (*PI)->removeSuccessor(Exit); | ||||
2566 | } | ||||
2567 | } | ||||
2568 | } | ||||
2569 | |||||
2570 | static bool mbbHasBackEdge(MachineBasicBlock *MBB, | ||||
2571 | SmallPtrSet<MachineBasicBlock *, 8> &MBBs) { | ||||
2572 | for (auto SI = MBB->succ_begin(), SE = MBB->succ_end(); SI != SE; ++SI) { | ||||
2573 | if (MBBs.contains(*SI)) { | ||||
2574 | return true; | ||||
2575 | } | ||||
2576 | } | ||||
2577 | return false; | ||||
2578 | } | ||||
2579 | |||||
2580 | static bool containsNewBackedge(MRT *Tree, | ||||
2581 | SmallPtrSet<MachineBasicBlock *, 8> &MBBs) { | ||||
2582 | // Need to traverse this in reverse since it is in post order. | ||||
2583 | if (Tree == nullptr) | ||||
2584 | return false; | ||||
2585 | |||||
2586 | if (Tree->isMBB()) { | ||||
2587 | MachineBasicBlock *MBB = Tree->getMBBMRT()->getMBB(); | ||||
2588 | MBBs.insert(MBB); | ||||
2589 | if (mbbHasBackEdge(MBB, MBBs)) { | ||||
2590 | return true; | ||||
2591 | } | ||||
2592 | } else { | ||||
2593 | RegionMRT *Region = Tree->getRegionMRT(); | ||||
2594 | SetVector<MRT *> *Children = Region->getChildren(); | ||||
2595 | for (auto CI = Children->rbegin(), CE = Children->rend(); CI != CE; ++CI) { | ||||
2596 | if (containsNewBackedge(*CI, MBBs)) | ||||
2597 | return true; | ||||
2598 | } | ||||
2599 | } | ||||
2600 | return false; | ||||
2601 | } | ||||
2602 | |||||
2603 | static bool containsNewBackedge(RegionMRT *Region) { | ||||
2604 | SmallPtrSet<MachineBasicBlock *, 8> MBBs; | ||||
2605 | return containsNewBackedge(Region, MBBs); | ||||
2606 | } | ||||
2607 | |||||
2608 | bool AMDGPUMachineCFGStructurizer::structurizeComplexRegion(RegionMRT *Region) { | ||||
2609 | auto *LRegion = initLinearizedRegion(Region); | ||||
2610 | LRegion->setHasLoop(containsNewBackedge(Region)); | ||||
2611 | MachineBasicBlock *LastMerge = createLinearizedExitBlock(Region); | ||||
2612 | MachineBasicBlock *CurrentMerge = LastMerge; | ||||
2613 | LRegion->addMBB(LastMerge); | ||||
2614 | LRegion->setExit(LastMerge); | ||||
2615 | |||||
2616 | rewriteRegionExitPHIs(Region, LastMerge, LRegion); | ||||
2617 | removeOldExitPreds(Region); | ||||
2618 | |||||
2619 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2620 | |||||
2621 | SetVector<MRT *> *Children = Region->getChildren(); | ||||
2622 | LLVM_DEBUG(dbgs() << "===========If Region Start===============\n")do { } while (false); | ||||
2623 | if (LRegion->getHasLoop()) { | ||||
2624 | LLVM_DEBUG(dbgs() << "Has Backedge: Yes\n")do { } while (false); | ||||
2625 | } else { | ||||
2626 | LLVM_DEBUG(dbgs() << "Has Backedge: No\n")do { } while (false); | ||||
2627 | } | ||||
2628 | |||||
2629 | unsigned BBSelectRegIn; | ||||
2630 | unsigned BBSelectRegOut; | ||||
2631 | for (auto CI = Children->begin(), CE = Children->end(); CI != CE; ++CI) { | ||||
2632 | LLVM_DEBUG(dbgs() << "CurrentRegion: \n")do { } while (false); | ||||
2633 | LLVM_DEBUG(LRegion->print(dbgs(), TRI))do { } while (false); | ||||
2634 | |||||
2635 | auto CNI = CI; | ||||
2636 | ++CNI; | ||||
2637 | |||||
2638 | MRT *Child = (*CI); | ||||
2639 | |||||
2640 | if (Child->isRegion()) { | ||||
2641 | |||||
2642 | LinearizedRegion *InnerLRegion = | ||||
2643 | Child->getRegionMRT()->getLinearizedRegion(); | ||||
2644 | // We found the block is the exit of an inner region, we need | ||||
2645 | // to put it in the current linearized region. | ||||
2646 | |||||
2647 | LLVM_DEBUG(dbgs() << "Linearizing region: ")do { } while (false); | ||||
2648 | LLVM_DEBUG(InnerLRegion->print(dbgs(), TRI))do { } while (false); | ||||
2649 | LLVM_DEBUG(dbgs() << "\n")do { } while (false); | ||||
2650 | |||||
2651 | MachineBasicBlock *InnerEntry = InnerLRegion->getEntry(); | ||||
2652 | if ((&(*(InnerEntry->getParent()->begin()))) == InnerEntry) { | ||||
2653 | // Entry has already been linearized, no need to do this region. | ||||
2654 | unsigned OuterSelect = InnerLRegion->getBBSelectRegOut(); | ||||
2655 | unsigned InnerSelectReg = | ||||
2656 | InnerLRegion->getRegionMRT()->getInnerOutputRegister(); | ||||
2657 | replaceRegisterWith(InnerSelectReg, OuterSelect), | ||||
2658 | resolvePHIInfos(InnerEntry); | ||||
2659 | if (!InnerLRegion->getExit()->isSuccessor(CurrentMerge)) | ||||
2660 | InnerLRegion->getExit()->addSuccessor(CurrentMerge); | ||||
2661 | continue; | ||||
2662 | } | ||||
2663 | |||||
2664 | BBSelectRegOut = Child->getBBSelectRegOut(); | ||||
2665 | BBSelectRegIn = Child->getBBSelectRegIn(); | ||||
2666 | |||||
2667 | LLVM_DEBUG(dbgs() << "BBSelectRegIn: " << printReg(BBSelectRegIn, TRI)do { } while (false) | ||||
2668 | << "\n")do { } while (false); | ||||
2669 | LLVM_DEBUG(dbgs() << "BBSelectRegOut: " << printReg(BBSelectRegOut, TRI)do { } while (false) | ||||
2670 | << "\n")do { } while (false); | ||||
2671 | |||||
2672 | MachineBasicBlock *IfEnd = CurrentMerge; | ||||
2673 | CurrentMerge = createIfRegion(CurrentMerge, InnerLRegion, LRegion, | ||||
2674 | Child->getRegionMRT()->getEntry(), | ||||
2675 | BBSelectRegIn, BBSelectRegOut); | ||||
2676 | TII->convertNonUniformIfRegion(CurrentMerge, IfEnd); | ||||
2677 | } else { | ||||
2678 | MachineBasicBlock *MBB = Child->getMBBMRT()->getMBB(); | ||||
2679 | LLVM_DEBUG(dbgs() << "Linearizing block: " << MBB->getNumber() << "\n")do { } while (false); | ||||
2680 | |||||
2681 | if (MBB == getSingleExitNode(*(MBB->getParent()))) { | ||||
2682 | // If this is the exit block then we need to skip to the next. | ||||
2683 | // The "in" register will be transferred to "out" in the next | ||||
2684 | // iteration. | ||||
2685 | continue; | ||||
2686 | } | ||||
2687 | |||||
2688 | BBSelectRegOut = Child->getBBSelectRegOut(); | ||||
2689 | BBSelectRegIn = Child->getBBSelectRegIn(); | ||||
2690 | |||||
2691 | LLVM_DEBUG(dbgs() << "BBSelectRegIn: " << printReg(BBSelectRegIn, TRI)do { } while (false) | ||||
2692 | << "\n")do { } while (false); | ||||
2693 | LLVM_DEBUG(dbgs() << "BBSelectRegOut: " << printReg(BBSelectRegOut, TRI)do { } while (false) | ||||
2694 | << "\n")do { } while (false); | ||||
2695 | |||||
2696 | MachineBasicBlock *IfEnd = CurrentMerge; | ||||
2697 | // This is a basic block that is not part of an inner region, we | ||||
2698 | // need to put it in the current linearized region. | ||||
2699 | CurrentMerge = createIfRegion(CurrentMerge, MBB, LRegion, BBSelectRegIn, | ||||
2700 | BBSelectRegOut); | ||||
2701 | if (CurrentMerge) { | ||||
2702 | TII->convertNonUniformIfRegion(CurrentMerge, IfEnd); | ||||
2703 | } | ||||
2704 | |||||
2705 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2706 | } | ||||
2707 | } | ||||
2708 | |||||
2709 | LRegion->removeFalseRegisterKills(MRI); | ||||
2710 | |||||
2711 | if (LRegion->getHasLoop()) { | ||||
2712 | MachineBasicBlock *NewSucc = splitEntry(LRegion); | ||||
2713 | if (isFunctionEntryBlock(LRegion->getEntry())) { | ||||
2714 | resolvePHIInfos(LRegion->getEntry()); | ||||
2715 | } | ||||
2716 | const DebugLoc &DL = NewSucc->findDebugLoc(NewSucc->getFirstNonPHI()); | ||||
2717 | unsigned InReg = LRegion->getBBSelectRegIn(); | ||||
2718 | Register InnerSelectReg = | ||||
2719 | MRI->createVirtualRegister(MRI->getRegClass(InReg)); | ||||
2720 | Register NewInReg = MRI->createVirtualRegister(MRI->getRegClass(InReg)); | ||||
2721 | TII->materializeImmediate(*(LRegion->getEntry()), | ||||
2722 | LRegion->getEntry()->getFirstTerminator(), DL, | ||||
2723 | NewInReg, Region->getEntry()->getNumber()); | ||||
2724 | // Need to be careful about updating the registers inside the region. | ||||
2725 | LRegion->replaceRegisterInsideRegion(InReg, InnerSelectReg, false, MRI); | ||||
2726 | LLVM_DEBUG(dbgs() << "Loop BBSelect Merge PHI:\n")do { } while (false); | ||||
2727 | insertMergePHI(LRegion->getEntry(), LRegion->getExit(), NewSucc, | ||||
2728 | InnerSelectReg, NewInReg, | ||||
2729 | LRegion->getRegionMRT()->getInnerOutputRegister()); | ||||
2730 | splitExit(LRegion); | ||||
2731 | TII->convertNonUniformLoopRegion(NewSucc, LastMerge); | ||||
2732 | } | ||||
2733 | |||||
2734 | if (Region->isRoot()) { | ||||
2735 | TII->insertReturn(*LastMerge); | ||||
2736 | } | ||||
2737 | |||||
2738 | LLVM_DEBUG(Region->getEntry()->getParent()->dump())do { } while (false); | ||||
2739 | LLVM_DEBUG(LRegion->print(dbgs(), TRI))do { } while (false); | ||||
2740 | LLVM_DEBUG(PHIInfo.dump(MRI))do { } while (false); | ||||
2741 | |||||
2742 | LLVM_DEBUG(dbgs() << "===========If Region End===============\n")do { } while (false); | ||||
2743 | |||||
2744 | Region->setLinearizedRegion(LRegion); | ||||
2745 | return true; | ||||
2746 | } | ||||
2747 | |||||
2748 | bool AMDGPUMachineCFGStructurizer::structurizeRegion(RegionMRT *Region) { | ||||
2749 | if (false && regionIsSimpleIf(Region)) { | ||||
2750 | transformSimpleIfRegion(Region); | ||||
2751 | return true; | ||||
2752 | } else if (regionIsSequence(Region)) { | ||||
2753 | fixupRegionExits(Region); | ||||
2754 | return false; | ||||
2755 | } else { | ||||
2756 | structurizeComplexRegion(Region); | ||||
2757 | } | ||||
2758 | return false; | ||||
2759 | } | ||||
2760 | |||||
2761 | static int structurize_once = 0; | ||||
2762 | |||||
2763 | bool AMDGPUMachineCFGStructurizer::structurizeRegions(RegionMRT *Region, | ||||
2764 | bool isTopRegion) { | ||||
2765 | bool Changed = false; | ||||
2766 | |||||
2767 | auto Children = Region->getChildren(); | ||||
2768 | for (auto CI : *Children) { | ||||
2769 | if (CI->isRegion()) { | ||||
2770 | Changed |= structurizeRegions(CI->getRegionMRT(), false); | ||||
2771 | } | ||||
2772 | } | ||||
2773 | |||||
2774 | if (structurize_once < 2 || true) { | ||||
2775 | Changed |= structurizeRegion(Region); | ||||
2776 | structurize_once++; | ||||
2777 | } | ||||
2778 | return Changed; | ||||
2779 | } | ||||
2780 | |||||
2781 | void AMDGPUMachineCFGStructurizer::initFallthroughMap(MachineFunction &MF) { | ||||
2782 | LLVM_DEBUG(dbgs() << "Fallthrough Map:\n")do { } while (false); | ||||
2783 | for (auto &MBBI : MF) { | ||||
2784 | MachineBasicBlock *MBB = MBBI.getFallThrough(); | ||||
2785 | if (MBB != nullptr) { | ||||
2786 | LLVM_DEBUG(dbgs() << "Fallthrough: " << MBBI.getNumber() << " -> "do { } while (false) | ||||
2787 | << MBB->getNumber() << "\n")do { } while (false); | ||||
2788 | } | ||||
2789 | FallthroughMap[&MBBI] = MBB; | ||||
2790 | } | ||||
2791 | } | ||||
2792 | |||||
2793 | void AMDGPUMachineCFGStructurizer::createLinearizedRegion(RegionMRT *Region, | ||||
2794 | unsigned SelectOut) { | ||||
2795 | LinearizedRegion *LRegion = new LinearizedRegion(); | ||||
2796 | if (SelectOut) { | ||||
2797 | LRegion->addLiveOut(SelectOut); | ||||
2798 | LLVM_DEBUG(dbgs() << "Add LiveOut (BBSelect): " << printReg(SelectOut, TRI)do { } while (false) | ||||
2799 | << "\n")do { } while (false); | ||||
2800 | } | ||||
2801 | LRegion->setRegionMRT(Region); | ||||
2802 | Region->setLinearizedRegion(LRegion); | ||||
2803 | LRegion->setParent(Region->getParent() | ||||
2804 | ? Region->getParent()->getLinearizedRegion() | ||||
2805 | : nullptr); | ||||
2806 | } | ||||
2807 | |||||
2808 | unsigned | ||||
2809 | AMDGPUMachineCFGStructurizer::initializeSelectRegisters(MRT *MRT, unsigned SelectOut, | ||||
2810 | MachineRegisterInfo *MRI, | ||||
2811 | const SIInstrInfo *TII) { | ||||
2812 | if (MRT->isRegion()) { | ||||
2813 | RegionMRT *Region = MRT->getRegionMRT(); | ||||
2814 | Region->setBBSelectRegOut(SelectOut); | ||||
2815 | unsigned InnerSelectOut = createBBSelectReg(TII, MRI); | ||||
2816 | |||||
2817 | // Fixme: Move linearization creation to the original spot | ||||
2818 | createLinearizedRegion(Region, SelectOut); | ||||
2819 | |||||
2820 | for (auto CI = Region->getChildren()->begin(), | ||||
2821 | CE = Region->getChildren()->end(); | ||||
2822 | CI != CE; ++CI) { | ||||
2823 | InnerSelectOut = | ||||
2824 | initializeSelectRegisters((*CI), InnerSelectOut, MRI, TII); | ||||
2825 | } | ||||
2826 | MRT->setBBSelectRegIn(InnerSelectOut); | ||||
2827 | return InnerSelectOut; | ||||
2828 | } else { | ||||
2829 | MRT->setBBSelectRegOut(SelectOut); | ||||
2830 | unsigned NewSelectIn = createBBSelectReg(TII, MRI); | ||||
2831 | MRT->setBBSelectRegIn(NewSelectIn); | ||||
2832 | return NewSelectIn; | ||||
2833 | } | ||||
2834 | } | ||||
2835 | |||||
2836 | static void checkRegOnlyPHIInputs(MachineFunction &MF) { | ||||
2837 | for (auto &MBBI : MF) { | ||||
2838 | for (MachineBasicBlock::instr_iterator I = MBBI.instr_begin(), | ||||
2839 | E = MBBI.instr_end(); | ||||
2840 | I != E; ++I) { | ||||
2841 | MachineInstr &Instr = *I; | ||||
2842 | if (Instr.isPHI()) { | ||||
2843 | int numPreds = getPHINumInputs(Instr); | ||||
2844 | for (int i = 0; i < numPreds; ++i) { | ||||
2845 | assert(Instr.getOperand(i * 2 + 1).isReg() &&(static_cast<void> (0)) | ||||
2846 | "PHI Operand not a register")(static_cast<void> (0)); | ||||
2847 | } | ||||
2848 | } | ||||
2849 | } | ||||
2850 | } | ||||
2851 | } | ||||
2852 | |||||
2853 | bool AMDGPUMachineCFGStructurizer::runOnMachineFunction(MachineFunction &MF) { | ||||
2854 | const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>(); | ||||
2855 | const SIInstrInfo *TII = ST.getInstrInfo(); | ||||
2856 | TRI = ST.getRegisterInfo(); | ||||
2857 | MRI = &(MF.getRegInfo()); | ||||
2858 | initFallthroughMap(MF); | ||||
2859 | |||||
2860 | checkRegOnlyPHIInputs(MF); | ||||
2861 | LLVM_DEBUG(dbgs() << "----STRUCTURIZER START----\n")do { } while (false); | ||||
| |||||
2862 | LLVM_DEBUG(MF.dump())do { } while (false); | ||||
2863 | |||||
2864 | Regions = &(getAnalysis<MachineRegionInfoPass>().getRegionInfo()); | ||||
2865 | LLVM_DEBUG(Regions->dump())do { } while (false); | ||||
2866 | |||||
2867 | RegionMRT *RTree = MRT::buildMRT(MF, Regions, TII, MRI); | ||||
2868 | setRegionMRT(RTree); | ||||
2869 | initializeSelectRegisters(RTree, 0, MRI, TII); | ||||
2870 | LLVM_DEBUG(RTree->dump(TRI))do { } while (false); | ||||
2871 | bool result = structurizeRegions(RTree, true); | ||||
2872 | delete RTree; | ||||
2873 | LLVM_DEBUG(dbgs() << "----STRUCTURIZER END----\n")do { } while (false); | ||||
2874 | initFallthroughMap(MF); | ||||
2875 | return result; | ||||
2876 | } | ||||
2877 | |||||
2878 | char AMDGPUMachineCFGStructurizerID = AMDGPUMachineCFGStructurizer::ID; | ||||
2879 | |||||
2880 | INITIALIZE_PASS_BEGIN(AMDGPUMachineCFGStructurizer, "amdgpu-machine-cfg-structurizer",static void *initializeAMDGPUMachineCFGStructurizerPassOnce(PassRegistry &Registry) { | ||||
2881 | "AMDGPU Machine CFG Structurizer", false, false)static void *initializeAMDGPUMachineCFGStructurizerPassOnce(PassRegistry &Registry) { | ||||
2882 | INITIALIZE_PASS_DEPENDENCY(MachineRegionInfoPass)initializeMachineRegionInfoPassPass(Registry); | ||||
2883 | INITIALIZE_PASS_END(AMDGPUMachineCFGStructurizer, "amdgpu-machine-cfg-structurizer",PassInfo *PI = new PassInfo( "AMDGPU Machine CFG Structurizer" , "amdgpu-machine-cfg-structurizer", &AMDGPUMachineCFGStructurizer ::ID, PassInfo::NormalCtor_t(callDefaultCtor<AMDGPUMachineCFGStructurizer >), false, false); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeAMDGPUMachineCFGStructurizerPassFlag ; void llvm::initializeAMDGPUMachineCFGStructurizerPass(PassRegistry &Registry) { llvm::call_once(InitializeAMDGPUMachineCFGStructurizerPassFlag , initializeAMDGPUMachineCFGStructurizerPassOnce, std::ref(Registry )); } | ||||
2884 | "AMDGPU Machine CFG Structurizer", false, false)PassInfo *PI = new PassInfo( "AMDGPU Machine CFG Structurizer" , "amdgpu-machine-cfg-structurizer", &AMDGPUMachineCFGStructurizer ::ID, PassInfo::NormalCtor_t(callDefaultCtor<AMDGPUMachineCFGStructurizer >), false, false); Registry.registerPass(*PI, true); return PI; } static llvm::once_flag InitializeAMDGPUMachineCFGStructurizerPassFlag ; void llvm::initializeAMDGPUMachineCFGStructurizerPass(PassRegistry &Registry) { llvm::call_once(InitializeAMDGPUMachineCFGStructurizerPassFlag , initializeAMDGPUMachineCFGStructurizerPassOnce, std::ref(Registry )); } | ||||
2885 | |||||
2886 | FunctionPass *llvm::createAMDGPUMachineCFGStructurizerPass() { | ||||
2887 | return new AMDGPUMachineCFGStructurizer(); | ||||
2888 | } |
1 | // Iterators -*- C++ -*- |
2 | |
3 | // Copyright (C) 2001-2020 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /* |
26 | * |
27 | * Copyright (c) 1994 |
28 | * Hewlett-Packard Company |
29 | * |
30 | * Permission to use, copy, modify, distribute and sell this software |
31 | * and its documentation for any purpose is hereby granted without fee, |
32 | * provided that the above copyright notice appear in all copies and |
33 | * that both that copyright notice and this permission notice appear |
34 | * in supporting documentation. Hewlett-Packard Company makes no |
35 | * representations about the suitability of this software for any |
36 | * purpose. It is provided "as is" without express or implied warranty. |
37 | * |
38 | * |
39 | * Copyright (c) 1996-1998 |
40 | * Silicon Graphics Computer Systems, Inc. |
41 | * |
42 | * Permission to use, copy, modify, distribute and sell this software |
43 | * and its documentation for any purpose is hereby granted without fee, |
44 | * provided that the above copyright notice appear in all copies and |
45 | * that both that copyright notice and this permission notice appear |
46 | * in supporting documentation. Silicon Graphics makes no |
47 | * representations about the suitability of this software for any |
48 | * purpose. It is provided "as is" without express or implied warranty. |
49 | */ |
50 | |
51 | /** @file bits/stl_iterator.h |
52 | * This is an internal header file, included by other library headers. |
53 | * Do not attempt to use it directly. @headername{iterator} |
54 | * |
55 | * This file implements reverse_iterator, back_insert_iterator, |
56 | * front_insert_iterator, insert_iterator, __normal_iterator, and their |
57 | * supporting functions and overloaded operators. |
58 | */ |
59 | |
60 | #ifndef _STL_ITERATOR_H1 |
61 | #define _STL_ITERATOR_H1 1 |
62 | |
63 | #include <bits/cpp_type_traits.h> |
64 | #include <ext/type_traits.h> |
65 | #include <bits/move.h> |
66 | #include <bits/ptr_traits.h> |
67 | |
68 | #if __cplusplus201402L >= 201103L |
69 | # include <type_traits> |
70 | #endif |
71 | |
72 | #if __cplusplus201402L > 201703L |
73 | # define __cpp_lib_array_constexpr 201811L |
74 | # define __cpp_lib_constexpr_iterator 201811L |
75 | #elif __cplusplus201402L == 201703L |
76 | # define __cpp_lib_array_constexpr 201803L |
77 | #endif |
78 | |
79 | #if __cplusplus201402L > 201703L |
80 | # include <compare> |
81 | # include <new> |
82 | # include <bits/iterator_concepts.h> |
83 | #endif |
84 | |
85 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
86 | { |
87 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
88 | |
89 | /** |
90 | * @addtogroup iterators |
91 | * @{ |
92 | */ |
93 | |
94 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
95 | namespace __detail |
96 | { |
97 | // Weaken iterator_category _Cat to _Limit if it is derived from that, |
98 | // otherwise use _Otherwise. |
99 | template<typename _Cat, typename _Limit, typename _Otherwise = _Cat> |
100 | using __clamp_iter_cat |
101 | = conditional_t<derived_from<_Cat, _Limit>, _Limit, _Otherwise>; |
102 | } |
103 | #endif |
104 | |
105 | // 24.4.1 Reverse iterators |
106 | /** |
107 | * Bidirectional and random access iterators have corresponding reverse |
108 | * %iterator adaptors that iterate through the data structure in the |
109 | * opposite direction. They have the same signatures as the corresponding |
110 | * iterators. The fundamental relation between a reverse %iterator and its |
111 | * corresponding %iterator @c i is established by the identity: |
112 | * @code |
113 | * &*(reverse_iterator(i)) == &*(i - 1) |
114 | * @endcode |
115 | * |
116 | * <em>This mapping is dictated by the fact that while there is always a |
117 | * pointer past the end of an array, there might not be a valid pointer |
118 | * before the beginning of an array.</em> [24.4.1]/1,2 |
119 | * |
120 | * Reverse iterators can be tricky and surprising at first. Their |
121 | * semantics make sense, however, and the trickiness is a side effect of |
122 | * the requirement that the iterators must be safe. |
123 | */ |
124 | template<typename _Iterator> |
125 | class reverse_iterator |
126 | : public iterator<typename iterator_traits<_Iterator>::iterator_category, |
127 | typename iterator_traits<_Iterator>::value_type, |
128 | typename iterator_traits<_Iterator>::difference_type, |
129 | typename iterator_traits<_Iterator>::pointer, |
130 | typename iterator_traits<_Iterator>::reference> |
131 | { |
132 | protected: |
133 | _Iterator current; |
134 | |
135 | typedef iterator_traits<_Iterator> __traits_type; |
136 | |
137 | public: |
138 | typedef _Iterator iterator_type; |
139 | typedef typename __traits_type::difference_type difference_type; |
140 | typedef typename __traits_type::pointer pointer; |
141 | typedef typename __traits_type::reference reference; |
142 | |
143 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
144 | using iterator_concept |
145 | = conditional_t<random_access_iterator<_Iterator>, |
146 | random_access_iterator_tag, |
147 | bidirectional_iterator_tag>; |
148 | using iterator_category |
149 | = __detail::__clamp_iter_cat<typename __traits_type::iterator_category, |
150 | random_access_iterator_tag>; |
151 | #endif |
152 | |
153 | /** |
154 | * The default constructor value-initializes member @p current. |
155 | * If it is a pointer, that means it is zero-initialized. |
156 | */ |
157 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
158 | // 235 No specification of default ctor for reverse_iterator |
159 | // 1012. reverse_iterator default ctor should value initialize |
160 | _GLIBCXX17_CONSTEXPR |
161 | reverse_iterator() : current() { } |
162 | |
163 | /** |
164 | * This %iterator will move in the opposite direction that @p x does. |
165 | */ |
166 | explicit _GLIBCXX17_CONSTEXPR |
167 | reverse_iterator(iterator_type __x) : current(__x) { } |
168 | |
169 | /** |
170 | * The copy constructor is normal. |
171 | */ |
172 | _GLIBCXX17_CONSTEXPR |
173 | reverse_iterator(const reverse_iterator& __x) |
174 | : current(__x.current) { } |
175 | |
176 | #if __cplusplus201402L >= 201103L |
177 | reverse_iterator& operator=(const reverse_iterator&) = default; |
178 | #endif |
179 | |
180 | /** |
181 | * A %reverse_iterator across other types can be copied if the |
182 | * underlying %iterator can be converted to the type of @c current. |
183 | */ |
184 | template<typename _Iter> |
185 | _GLIBCXX17_CONSTEXPR |
186 | reverse_iterator(const reverse_iterator<_Iter>& __x) |
187 | : current(__x.base()) { } |
188 | |
189 | /** |
190 | * @return @c current, the %iterator used for underlying work. |
191 | */ |
192 | _GLIBCXX17_CONSTEXPR iterator_type |
193 | base() const |
194 | { return current; } |
195 | |
196 | /** |
197 | * @return A reference to the value at @c --current |
198 | * |
199 | * This requires that @c --current is dereferenceable. |
200 | * |
201 | * @warning This implementation requires that for an iterator of the |
202 | * underlying iterator type, @c x, a reference obtained by |
203 | * @c *x remains valid after @c x has been modified or |
204 | * destroyed. This is a bug: http://gcc.gnu.org/PR51823 |
205 | */ |
206 | _GLIBCXX17_CONSTEXPR reference |
207 | operator*() const |
208 | { |
209 | _Iterator __tmp = current; |
210 | return *--__tmp; |
211 | } |
212 | |
213 | /** |
214 | * @return A pointer to the value at @c --current |
215 | * |
216 | * This requires that @c --current is dereferenceable. |
217 | */ |
218 | _GLIBCXX17_CONSTEXPR pointer |
219 | operator->() const |
220 | #if __cplusplus201402L > 201703L && __cpp_concepts >= 201907L |
221 | requires is_pointer_v<_Iterator> |
222 | || requires(const _Iterator __i) { __i.operator->(); } |
223 | #endif |
224 | { |
225 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
226 | // 1052. operator-> should also support smart pointers |
227 | _Iterator __tmp = current; |
228 | --__tmp; |
229 | return _S_to_pointer(__tmp); |
230 | } |
231 | |
232 | /** |
233 | * @return @c *this |
234 | * |
235 | * Decrements the underlying iterator. |
236 | */ |
237 | _GLIBCXX17_CONSTEXPR reverse_iterator& |
238 | operator++() |
239 | { |
240 | --current; |
241 | return *this; |
242 | } |
243 | |
244 | /** |
245 | * @return The original value of @c *this |
246 | * |
247 | * Decrements the underlying iterator. |
248 | */ |
249 | _GLIBCXX17_CONSTEXPR reverse_iterator |
250 | operator++(int) |
251 | { |
252 | reverse_iterator __tmp = *this; |
253 | --current; |
254 | return __tmp; |
255 | } |
256 | |
257 | /** |
258 | * @return @c *this |
259 | * |
260 | * Increments the underlying iterator. |
261 | */ |
262 | _GLIBCXX17_CONSTEXPR reverse_iterator& |
263 | operator--() |
264 | { |
265 | ++current; |
266 | return *this; |
267 | } |
268 | |
269 | /** |
270 | * @return A reverse_iterator with the previous value of @c *this |
271 | * |
272 | * Increments the underlying iterator. |
273 | */ |
274 | _GLIBCXX17_CONSTEXPR reverse_iterator |
275 | operator--(int) |
276 | { |
277 | reverse_iterator __tmp = *this; |
278 | ++current; |
279 | return __tmp; |
280 | } |
281 | |
282 | /** |
283 | * @return A reverse_iterator that refers to @c current - @a __n |
284 | * |
285 | * The underlying iterator must be a Random Access Iterator. |
286 | */ |
287 | _GLIBCXX17_CONSTEXPR reverse_iterator |
288 | operator+(difference_type __n) const |
289 | { return reverse_iterator(current - __n); } |
290 | |
291 | /** |
292 | * @return *this |
293 | * |
294 | * Moves the underlying iterator backwards @a __n steps. |
295 | * The underlying iterator must be a Random Access Iterator. |
296 | */ |
297 | _GLIBCXX17_CONSTEXPR reverse_iterator& |
298 | operator+=(difference_type __n) |
299 | { |
300 | current -= __n; |
301 | return *this; |
302 | } |
303 | |
304 | /** |
305 | * @return A reverse_iterator that refers to @c current - @a __n |
306 | * |
307 | * The underlying iterator must be a Random Access Iterator. |
308 | */ |
309 | _GLIBCXX17_CONSTEXPR reverse_iterator |
310 | operator-(difference_type __n) const |
311 | { return reverse_iterator(current + __n); } |
312 | |
313 | /** |
314 | * @return *this |
315 | * |
316 | * Moves the underlying iterator forwards @a __n steps. |
317 | * The underlying iterator must be a Random Access Iterator. |
318 | */ |
319 | _GLIBCXX17_CONSTEXPR reverse_iterator& |
320 | operator-=(difference_type __n) |
321 | { |
322 | current += __n; |
323 | return *this; |
324 | } |
325 | |
326 | /** |
327 | * @return The value at @c current - @a __n - 1 |
328 | * |
329 | * The underlying iterator must be a Random Access Iterator. |
330 | */ |
331 | _GLIBCXX17_CONSTEXPR reference |
332 | operator[](difference_type __n) const |
333 | { return *(*this + __n); } |
334 | |
335 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
336 | friend constexpr iter_rvalue_reference_t<_Iterator> |
337 | iter_move(const reverse_iterator& __i) |
338 | noexcept(is_nothrow_copy_constructible_v<_Iterator> |
339 | && noexcept(ranges::iter_move(--std::declval<_Iterator&>()))) |
340 | { |
341 | auto __tmp = __i.base(); |
342 | return ranges::iter_move(--__tmp); |
343 | } |
344 | |
345 | template<indirectly_swappable<_Iterator> _Iter2> |
346 | friend constexpr void |
347 | iter_swap(const reverse_iterator& __x, |
348 | const reverse_iterator<_Iter2>& __y) |
349 | noexcept(is_nothrow_copy_constructible_v<_Iterator> |
350 | && is_nothrow_copy_constructible_v<_Iter2> |
351 | && noexcept(ranges::iter_swap(--std::declval<_Iterator&>(), |
352 | --std::declval<_Iter2&>()))) |
353 | { |
354 | auto __xtmp = __x.base(); |
355 | auto __ytmp = __y.base(); |
356 | ranges::iter_swap(--__xtmp, --__ytmp); |
357 | } |
358 | #endif |
359 | |
360 | private: |
361 | template<typename _Tp> |
362 | static _GLIBCXX17_CONSTEXPR _Tp* |
363 | _S_to_pointer(_Tp* __p) |
364 | { return __p; } |
365 | |
366 | template<typename _Tp> |
367 | static _GLIBCXX17_CONSTEXPR pointer |
368 | _S_to_pointer(_Tp __t) |
369 | { return __t.operator->(); } |
370 | }; |
371 | |
372 | //@{ |
373 | /** |
374 | * @param __x A %reverse_iterator. |
375 | * @param __y A %reverse_iterator. |
376 | * @return A simple bool. |
377 | * |
378 | * Reverse iterators forward comparisons to their underlying base() |
379 | * iterators. |
380 | * |
381 | */ |
382 | #if __cplusplus201402L <= 201703L || ! defined __cpp_lib_concepts |
383 | template<typename _Iterator> |
384 | inline _GLIBCXX17_CONSTEXPR bool |
385 | operator==(const reverse_iterator<_Iterator>& __x, |
386 | const reverse_iterator<_Iterator>& __y) |
387 | { return __x.base() == __y.base(); } |
388 | |
389 | template<typename _Iterator> |
390 | inline _GLIBCXX17_CONSTEXPR bool |
391 | operator<(const reverse_iterator<_Iterator>& __x, |
392 | const reverse_iterator<_Iterator>& __y) |
393 | { return __y.base() < __x.base(); } |
394 | |
395 | template<typename _Iterator> |
396 | inline _GLIBCXX17_CONSTEXPR bool |
397 | operator!=(const reverse_iterator<_Iterator>& __x, |
398 | const reverse_iterator<_Iterator>& __y) |
399 | { return !(__x == __y); } |
400 | |
401 | template<typename _Iterator> |
402 | inline _GLIBCXX17_CONSTEXPR bool |
403 | operator>(const reverse_iterator<_Iterator>& __x, |
404 | const reverse_iterator<_Iterator>& __y) |
405 | { return __y < __x; } |
406 | |
407 | template<typename _Iterator> |
408 | inline _GLIBCXX17_CONSTEXPR bool |
409 | operator<=(const reverse_iterator<_Iterator>& __x, |
410 | const reverse_iterator<_Iterator>& __y) |
411 | { return !(__y < __x); } |
412 | |
413 | template<typename _Iterator> |
414 | inline _GLIBCXX17_CONSTEXPR bool |
415 | operator>=(const reverse_iterator<_Iterator>& __x, |
416 | const reverse_iterator<_Iterator>& __y) |
417 | { return !(__x < __y); } |
418 | |
419 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
420 | // DR 280. Comparison of reverse_iterator to const reverse_iterator. |
421 | template<typename _IteratorL, typename _IteratorR> |
422 | inline _GLIBCXX17_CONSTEXPR bool |
423 | operator==(const reverse_iterator<_IteratorL>& __x, |
424 | const reverse_iterator<_IteratorR>& __y) |
425 | { return __x.base() == __y.base(); } |
426 | |
427 | template<typename _IteratorL, typename _IteratorR> |
428 | inline _GLIBCXX17_CONSTEXPR bool |
429 | operator<(const reverse_iterator<_IteratorL>& __x, |
430 | const reverse_iterator<_IteratorR>& __y) |
431 | { return __y.base() < __x.base(); } |
432 | |
433 | template<typename _IteratorL, typename _IteratorR> |
434 | inline _GLIBCXX17_CONSTEXPR bool |
435 | operator!=(const reverse_iterator<_IteratorL>& __x, |
436 | const reverse_iterator<_IteratorR>& __y) |
437 | { return !(__x == __y); } |
438 | |
439 | template<typename _IteratorL, typename _IteratorR> |
440 | inline _GLIBCXX17_CONSTEXPR bool |
441 | operator>(const reverse_iterator<_IteratorL>& __x, |
442 | const reverse_iterator<_IteratorR>& __y) |
443 | { return __y < __x; } |
444 | |
445 | template<typename _IteratorL, typename _IteratorR> |
446 | inline _GLIBCXX17_CONSTEXPR bool |
447 | operator<=(const reverse_iterator<_IteratorL>& __x, |
448 | const reverse_iterator<_IteratorR>& __y) |
449 | { return !(__y < __x); } |
450 | |
451 | template<typename _IteratorL, typename _IteratorR> |
452 | inline _GLIBCXX17_CONSTEXPR bool |
453 | operator>=(const reverse_iterator<_IteratorL>& __x, |
454 | const reverse_iterator<_IteratorR>& __y) |
455 | { return !(__x < __y); } |
456 | #else // C++20 |
457 | template<typename _IteratorL, typename _IteratorR> |
458 | constexpr bool |
459 | operator==(const reverse_iterator<_IteratorL>& __x, |
460 | const reverse_iterator<_IteratorR>& __y) |
461 | requires requires { { __x.base() == __y.base() } -> convertible_to<bool>; } |
462 | { return __x.base() == __y.base(); } |
463 | |
464 | template<typename _IteratorL, typename _IteratorR> |
465 | constexpr bool |
466 | operator!=(const reverse_iterator<_IteratorL>& __x, |
467 | const reverse_iterator<_IteratorR>& __y) |
468 | requires requires { { __x.base() != __y.base() } -> convertible_to<bool>; } |
469 | { return __x.base() != __y.base(); } |
470 | |
471 | template<typename _IteratorL, typename _IteratorR> |
472 | constexpr bool |
473 | operator<(const reverse_iterator<_IteratorL>& __x, |
474 | const reverse_iterator<_IteratorR>& __y) |
475 | requires requires { { __x.base() > __y.base() } -> convertible_to<bool>; } |
476 | { return __x.base() > __y.base(); } |
477 | |
478 | template<typename _IteratorL, typename _IteratorR> |
479 | constexpr bool |
480 | operator>(const reverse_iterator<_IteratorL>& __x, |
481 | const reverse_iterator<_IteratorR>& __y) |
482 | requires requires { { __x.base() < __y.base() } -> convertible_to<bool>; } |
483 | { return __x.base() < __y.base(); } |
484 | |
485 | template<typename _IteratorL, typename _IteratorR> |
486 | constexpr bool |
487 | operator<=(const reverse_iterator<_IteratorL>& __x, |
488 | const reverse_iterator<_IteratorR>& __y) |
489 | requires requires { { __x.base() >= __y.base() } -> convertible_to<bool>; } |
490 | { return __x.base() >= __y.base(); } |
491 | |
492 | template<typename _IteratorL, typename _IteratorR> |
493 | constexpr bool |
494 | operator>=(const reverse_iterator<_IteratorL>& __x, |
495 | const reverse_iterator<_IteratorR>& __y) |
496 | requires requires { { __x.base() <= __y.base() } -> convertible_to<bool>; } |
497 | { return __x.base() <= __y.base(); } |
498 | |
499 | template<typename _IteratorL, |
500 | three_way_comparable_with<_IteratorL> _IteratorR> |
501 | constexpr compare_three_way_result_t<_IteratorL, _IteratorR> |
502 | operator<=>(const reverse_iterator<_IteratorL>& __x, |
503 | const reverse_iterator<_IteratorR>& __y) |
504 | { return __y.base() <=> __x.base(); } |
505 | #endif // C++20 |
506 | //@} |
507 | |
508 | #if __cplusplus201402L < 201103L |
509 | template<typename _Iterator> |
510 | inline typename reverse_iterator<_Iterator>::difference_type |
511 | operator-(const reverse_iterator<_Iterator>& __x, |
512 | const reverse_iterator<_Iterator>& __y) |
513 | { return __y.base() - __x.base(); } |
514 | |
515 | template<typename _IteratorL, typename _IteratorR> |
516 | inline typename reverse_iterator<_IteratorL>::difference_type |
517 | operator-(const reverse_iterator<_IteratorL>& __x, |
518 | const reverse_iterator<_IteratorR>& __y) |
519 | { return __y.base() - __x.base(); } |
520 | #else |
521 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
522 | // DR 685. reverse_iterator/move_iterator difference has invalid signatures |
523 | template<typename _IteratorL, typename _IteratorR> |
524 | inline _GLIBCXX17_CONSTEXPR auto |
525 | operator-(const reverse_iterator<_IteratorL>& __x, |
526 | const reverse_iterator<_IteratorR>& __y) |
527 | -> decltype(__y.base() - __x.base()) |
528 | { return __y.base() - __x.base(); } |
529 | #endif |
530 | |
531 | template<typename _Iterator> |
532 | inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator> |
533 | operator+(typename reverse_iterator<_Iterator>::difference_type __n, |
534 | const reverse_iterator<_Iterator>& __x) |
535 | { return reverse_iterator<_Iterator>(__x.base() - __n); } |
536 | |
537 | #if __cplusplus201402L >= 201103L |
538 | // Same as C++14 make_reverse_iterator but used in C++11 mode too. |
539 | template<typename _Iterator> |
540 | inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator> |
541 | __make_reverse_iterator(_Iterator __i) |
542 | { return reverse_iterator<_Iterator>(__i); } |
543 | |
544 | # if __cplusplus201402L >= 201402L |
545 | # define __cpp_lib_make_reverse_iterator201402 201402 |
546 | |
547 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
548 | // DR 2285. make_reverse_iterator |
549 | /// Generator function for reverse_iterator. |
550 | template<typename _Iterator> |
551 | inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Iterator> |
552 | make_reverse_iterator(_Iterator __i) |
553 | { return reverse_iterator<_Iterator>(__i); } |
554 | |
555 | # if __cplusplus201402L > 201703L && defined __cpp_lib_concepts |
556 | template<typename _Iterator1, typename _Iterator2> |
557 | requires (!sized_sentinel_for<_Iterator1, _Iterator2>) |
558 | inline constexpr bool |
559 | disable_sized_sentinel_for<reverse_iterator<_Iterator1>, |
560 | reverse_iterator<_Iterator2>> = true; |
561 | # endif // C++20 |
562 | # endif // C++14 |
563 | |
564 | template<typename _Iterator> |
565 | _GLIBCXX20_CONSTEXPR |
566 | auto |
567 | __niter_base(reverse_iterator<_Iterator> __it) |
568 | -> decltype(__make_reverse_iterator(__niter_base(__it.base()))) |
569 | { return __make_reverse_iterator(__niter_base(__it.base())); } |
570 | |
571 | template<typename _Iterator> |
572 | struct __is_move_iterator<reverse_iterator<_Iterator> > |
573 | : __is_move_iterator<_Iterator> |
574 | { }; |
575 | |
576 | template<typename _Iterator> |
577 | _GLIBCXX20_CONSTEXPR |
578 | auto |
579 | __miter_base(reverse_iterator<_Iterator> __it) |
580 | -> decltype(__make_reverse_iterator(__miter_base(__it.base()))) |
581 | { return __make_reverse_iterator(__miter_base(__it.base())); } |
582 | #endif // C++11 |
583 | |
584 | // 24.4.2.2.1 back_insert_iterator |
585 | /** |
586 | * @brief Turns assignment into insertion. |
587 | * |
588 | * These are output iterators, constructed from a container-of-T. |
589 | * Assigning a T to the iterator appends it to the container using |
590 | * push_back. |
591 | * |
592 | * Tip: Using the back_inserter function to create these iterators can |
593 | * save typing. |
594 | */ |
595 | template<typename _Container> |
596 | class back_insert_iterator |
597 | : public iterator<output_iterator_tag, void, void, void, void> |
598 | { |
599 | protected: |
600 | _Container* container; |
601 | |
602 | public: |
603 | /// A nested typedef for the type of whatever container you used. |
604 | typedef _Container container_type; |
605 | #if __cplusplus201402L > 201703L |
606 | using difference_type = ptrdiff_t; |
607 | |
608 | constexpr back_insert_iterator() noexcept : container(nullptr) { } |
609 | #endif |
610 | |
611 | /// The only way to create this %iterator is with a container. |
612 | explicit _GLIBCXX20_CONSTEXPR |
613 | back_insert_iterator(_Container& __x) |
614 | : container(std::__addressof(__x)) { } |
615 | |
616 | /** |
617 | * @param __value An instance of whatever type |
618 | * container_type::const_reference is; presumably a |
619 | * reference-to-const T for container<T>. |
620 | * @return This %iterator, for chained operations. |
621 | * |
622 | * This kind of %iterator doesn't really have a @a position in the |
623 | * container (you can think of the position as being permanently at |
624 | * the end, if you like). Assigning a value to the %iterator will |
625 | * always append the value to the end of the container. |
626 | */ |
627 | #if __cplusplus201402L < 201103L |
628 | back_insert_iterator& |
629 | operator=(typename _Container::const_reference __value) |
630 | { |
631 | container->push_back(__value); |
632 | return *this; |
633 | } |
634 | #else |
635 | _GLIBCXX20_CONSTEXPR |
636 | back_insert_iterator& |
637 | operator=(const typename _Container::value_type& __value) |
638 | { |
639 | container->push_back(__value); |
640 | return *this; |
641 | } |
642 | |
643 | _GLIBCXX20_CONSTEXPR |
644 | back_insert_iterator& |
645 | operator=(typename _Container::value_type&& __value) |
646 | { |
647 | container->push_back(std::move(__value)); |
648 | return *this; |
649 | } |
650 | #endif |
651 | |
652 | /// Simply returns *this. |
653 | _GLIBCXX20_CONSTEXPR |
654 | back_insert_iterator& |
655 | operator*() |
656 | { return *this; } |
657 | |
658 | /// Simply returns *this. (This %iterator does not @a move.) |
659 | _GLIBCXX20_CONSTEXPR |
660 | back_insert_iterator& |
661 | operator++() |
662 | { return *this; } |
663 | |
664 | /// Simply returns *this. (This %iterator does not @a move.) |
665 | _GLIBCXX20_CONSTEXPR |
666 | back_insert_iterator |
667 | operator++(int) |
668 | { return *this; } |
669 | }; |
670 | |
671 | /** |
672 | * @param __x A container of arbitrary type. |
673 | * @return An instance of back_insert_iterator working on @p __x. |
674 | * |
675 | * This wrapper function helps in creating back_insert_iterator instances. |
676 | * Typing the name of the %iterator requires knowing the precise full |
677 | * type of the container, which can be tedious and impedes generic |
678 | * programming. Using this function lets you take advantage of automatic |
679 | * template parameter deduction, making the compiler match the correct |
680 | * types for you. |
681 | */ |
682 | template<typename _Container> |
683 | _GLIBCXX20_CONSTEXPR |
684 | inline back_insert_iterator<_Container> |
685 | back_inserter(_Container& __x) |
686 | { return back_insert_iterator<_Container>(__x); } |
687 | |
688 | /** |
689 | * @brief Turns assignment into insertion. |
690 | * |
691 | * These are output iterators, constructed from a container-of-T. |
692 | * Assigning a T to the iterator prepends it to the container using |
693 | * push_front. |
694 | * |
695 | * Tip: Using the front_inserter function to create these iterators can |
696 | * save typing. |
697 | */ |
698 | template<typename _Container> |
699 | class front_insert_iterator |
700 | : public iterator<output_iterator_tag, void, void, void, void> |
701 | { |
702 | protected: |
703 | _Container* container; |
704 | |
705 | public: |
706 | /// A nested typedef for the type of whatever container you used. |
707 | typedef _Container container_type; |
708 | #if __cplusplus201402L > 201703L |
709 | using difference_type = ptrdiff_t; |
710 | |
711 | constexpr front_insert_iterator() noexcept : container(nullptr) { } |
712 | #endif |
713 | |
714 | /// The only way to create this %iterator is with a container. |
715 | explicit _GLIBCXX20_CONSTEXPR |
716 | front_insert_iterator(_Container& __x) |
717 | : container(std::__addressof(__x)) { } |
718 | |
719 | /** |
720 | * @param __value An instance of whatever type |
721 | * container_type::const_reference is; presumably a |
722 | * reference-to-const T for container<T>. |
723 | * @return This %iterator, for chained operations. |
724 | * |
725 | * This kind of %iterator doesn't really have a @a position in the |
726 | * container (you can think of the position as being permanently at |
727 | * the front, if you like). Assigning a value to the %iterator will |
728 | * always prepend the value to the front of the container. |
729 | */ |
730 | #if __cplusplus201402L < 201103L |
731 | front_insert_iterator& |
732 | operator=(typename _Container::const_reference __value) |
733 | { |
734 | container->push_front(__value); |
735 | return *this; |
736 | } |
737 | #else |
738 | _GLIBCXX20_CONSTEXPR |
739 | front_insert_iterator& |
740 | operator=(const typename _Container::value_type& __value) |
741 | { |
742 | container->push_front(__value); |
743 | return *this; |
744 | } |
745 | |
746 | _GLIBCXX20_CONSTEXPR |
747 | front_insert_iterator& |
748 | operator=(typename _Container::value_type&& __value) |
749 | { |
750 | container->push_front(std::move(__value)); |
751 | return *this; |
752 | } |
753 | #endif |
754 | |
755 | /// Simply returns *this. |
756 | _GLIBCXX20_CONSTEXPR |
757 | front_insert_iterator& |
758 | operator*() |
759 | { return *this; } |
760 | |
761 | /// Simply returns *this. (This %iterator does not @a move.) |
762 | _GLIBCXX20_CONSTEXPR |
763 | front_insert_iterator& |
764 | operator++() |
765 | { return *this; } |
766 | |
767 | /// Simply returns *this. (This %iterator does not @a move.) |
768 | _GLIBCXX20_CONSTEXPR |
769 | front_insert_iterator |
770 | operator++(int) |
771 | { return *this; } |
772 | }; |
773 | |
774 | /** |
775 | * @param __x A container of arbitrary type. |
776 | * @return An instance of front_insert_iterator working on @p x. |
777 | * |
778 | * This wrapper function helps in creating front_insert_iterator instances. |
779 | * Typing the name of the %iterator requires knowing the precise full |
780 | * type of the container, which can be tedious and impedes generic |
781 | * programming. Using this function lets you take advantage of automatic |
782 | * template parameter deduction, making the compiler match the correct |
783 | * types for you. |
784 | */ |
785 | template<typename _Container> |
786 | _GLIBCXX20_CONSTEXPR |
787 | inline front_insert_iterator<_Container> |
788 | front_inserter(_Container& __x) |
789 | { return front_insert_iterator<_Container>(__x); } |
790 | |
791 | /** |
792 | * @brief Turns assignment into insertion. |
793 | * |
794 | * These are output iterators, constructed from a container-of-T. |
795 | * Assigning a T to the iterator inserts it in the container at the |
796 | * %iterator's position, rather than overwriting the value at that |
797 | * position. |
798 | * |
799 | * (Sequences will actually insert a @e copy of the value before the |
800 | * %iterator's position.) |
801 | * |
802 | * Tip: Using the inserter function to create these iterators can |
803 | * save typing. |
804 | */ |
805 | template<typename _Container> |
806 | class insert_iterator |
807 | : public iterator<output_iterator_tag, void, void, void, void> |
808 | { |
809 | #if __cplusplus201402L > 201703L && defined __cpp_lib_concepts |
810 | using _Iter = std::__detail::__range_iter_t<_Container>; |
811 | |
812 | protected: |
813 | _Container* container = nullptr; |
814 | _Iter iter = _Iter(); |
815 | #else |
816 | typedef typename _Container::iterator _Iter; |
817 | |
818 | protected: |
819 | _Container* container; |
820 | _Iter iter; |
821 | #endif |
822 | |
823 | public: |
824 | /// A nested typedef for the type of whatever container you used. |
825 | typedef _Container container_type; |
826 | |
827 | #if __cplusplus201402L > 201703L && defined __cpp_lib_concepts |
828 | using difference_type = ptrdiff_t; |
829 | |
830 | insert_iterator() = default; |
831 | #endif |
832 | |
833 | /** |
834 | * The only way to create this %iterator is with a container and an |
835 | * initial position (a normal %iterator into the container). |
836 | */ |
837 | _GLIBCXX20_CONSTEXPR |
838 | insert_iterator(_Container& __x, _Iter __i) |
839 | : container(std::__addressof(__x)), iter(__i) {} |
840 | |
841 | /** |
842 | * @param __value An instance of whatever type |
843 | * container_type::const_reference is; presumably a |
844 | * reference-to-const T for container<T>. |
845 | * @return This %iterator, for chained operations. |
846 | * |
847 | * This kind of %iterator maintains its own position in the |
848 | * container. Assigning a value to the %iterator will insert the |
849 | * value into the container at the place before the %iterator. |
850 | * |
851 | * The position is maintained such that subsequent assignments will |
852 | * insert values immediately after one another. For example, |
853 | * @code |
854 | * // vector v contains A and Z |
855 | * |
856 | * insert_iterator i (v, ++v.begin()); |
857 | * i = 1; |
858 | * i = 2; |
859 | * i = 3; |
860 | * |
861 | * // vector v contains A, 1, 2, 3, and Z |
862 | * @endcode |
863 | */ |
864 | #if __cplusplus201402L < 201103L |
865 | insert_iterator& |
866 | operator=(typename _Container::const_reference __value) |
867 | { |
868 | iter = container->insert(iter, __value); |
869 | ++iter; |
870 | return *this; |
871 | } |
872 | #else |
873 | _GLIBCXX20_CONSTEXPR |
874 | insert_iterator& |
875 | operator=(const typename _Container::value_type& __value) |
876 | { |
877 | iter = container->insert(iter, __value); |
878 | ++iter; |
879 | return *this; |
880 | } |
881 | |
882 | _GLIBCXX20_CONSTEXPR |
883 | insert_iterator& |
884 | operator=(typename _Container::value_type&& __value) |
885 | { |
886 | iter = container->insert(iter, std::move(__value)); |
887 | ++iter; |
888 | return *this; |
889 | } |
890 | #endif |
891 | |
892 | /// Simply returns *this. |
893 | _GLIBCXX20_CONSTEXPR |
894 | insert_iterator& |
895 | operator*() |
896 | { return *this; } |
897 | |
898 | /// Simply returns *this. (This %iterator does not @a move.) |
899 | _GLIBCXX20_CONSTEXPR |
900 | insert_iterator& |
901 | operator++() |
902 | { return *this; } |
903 | |
904 | /// Simply returns *this. (This %iterator does not @a move.) |
905 | _GLIBCXX20_CONSTEXPR |
906 | insert_iterator& |
907 | operator++(int) |
908 | { return *this; } |
909 | }; |
910 | |
911 | /** |
912 | * @param __x A container of arbitrary type. |
913 | * @param __i An iterator into the container. |
914 | * @return An instance of insert_iterator working on @p __x. |
915 | * |
916 | * This wrapper function helps in creating insert_iterator instances. |
917 | * Typing the name of the %iterator requires knowing the precise full |
918 | * type of the container, which can be tedious and impedes generic |
919 | * programming. Using this function lets you take advantage of automatic |
920 | * template parameter deduction, making the compiler match the correct |
921 | * types for you. |
922 | */ |
923 | #if __cplusplus201402L > 201703L && defined __cpp_lib_concepts |
924 | template<typename _Container> |
925 | constexpr insert_iterator<_Container> |
926 | inserter(_Container& __x, std::__detail::__range_iter_t<_Container> __i) |
927 | { return insert_iterator<_Container>(__x, __i); } |
928 | #else |
929 | template<typename _Container, typename _Iterator> |
930 | inline insert_iterator<_Container> |
931 | inserter(_Container& __x, _Iterator __i) |
932 | { |
933 | return insert_iterator<_Container>(__x, |
934 | typename _Container::iterator(__i)); |
935 | } |
936 | #endif |
937 | |
938 | // @} group iterators |
939 | |
940 | _GLIBCXX_END_NAMESPACE_VERSION |
941 | } // namespace |
942 | |
943 | namespace __gnu_cxx _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
944 | { |
945 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
946 | |
947 | // This iterator adapter is @a normal in the sense that it does not |
948 | // change the semantics of any of the operators of its iterator |
949 | // parameter. Its primary purpose is to convert an iterator that is |
950 | // not a class, e.g. a pointer, into an iterator that is a class. |
951 | // The _Container parameter exists solely so that different containers |
952 | // using this template can instantiate different types, even if the |
953 | // _Iterator parameter is the same. |
954 | template<typename _Iterator, typename _Container> |
955 | class __normal_iterator |
956 | { |
957 | protected: |
958 | _Iterator _M_current; |
959 | |
960 | typedef std::iterator_traits<_Iterator> __traits_type; |
961 | |
962 | public: |
963 | typedef _Iterator iterator_type; |
964 | typedef typename __traits_type::iterator_category iterator_category; |
965 | typedef typename __traits_type::value_type value_type; |
966 | typedef typename __traits_type::difference_type difference_type; |
967 | typedef typename __traits_type::reference reference; |
968 | typedef typename __traits_type::pointer pointer; |
969 | |
970 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
971 | using iterator_concept = std::__detail::__iter_concept<_Iterator>; |
972 | #endif |
973 | |
974 | _GLIBCXX_CONSTEXPRconstexpr __normal_iterator() _GLIBCXX_NOEXCEPTnoexcept |
975 | : _M_current(_Iterator()) { } |
976 | |
977 | explicit _GLIBCXX20_CONSTEXPR |
978 | __normal_iterator(const _Iterator& __i) _GLIBCXX_NOEXCEPTnoexcept |
979 | : _M_current(__i) { } |
980 | |
981 | // Allow iterator to const_iterator conversion |
982 | template<typename _Iter> |
983 | _GLIBCXX20_CONSTEXPR |
984 | __normal_iterator(const __normal_iterator<_Iter, |
985 | typename __enable_if< |
986 | (std::__are_same<_Iter, typename _Container::pointer>::__value), |
987 | _Container>::__type>& __i) _GLIBCXX_NOEXCEPTnoexcept |
988 | : _M_current(__i.base()) { } |
989 | |
990 | // Forward iterator requirements |
991 | _GLIBCXX20_CONSTEXPR |
992 | reference |
993 | operator*() const _GLIBCXX_NOEXCEPTnoexcept |
994 | { return *_M_current; } |
995 | |
996 | _GLIBCXX20_CONSTEXPR |
997 | pointer |
998 | operator->() const _GLIBCXX_NOEXCEPTnoexcept |
999 | { return _M_current; } |
1000 | |
1001 | _GLIBCXX20_CONSTEXPR |
1002 | __normal_iterator& |
1003 | operator++() _GLIBCXX_NOEXCEPTnoexcept |
1004 | { |
1005 | ++_M_current; |
1006 | return *this; |
1007 | } |
1008 | |
1009 | _GLIBCXX20_CONSTEXPR |
1010 | __normal_iterator |
1011 | operator++(int) _GLIBCXX_NOEXCEPTnoexcept |
1012 | { return __normal_iterator(_M_current++); } |
1013 | |
1014 | // Bidirectional iterator requirements |
1015 | _GLIBCXX20_CONSTEXPR |
1016 | __normal_iterator& |
1017 | operator--() _GLIBCXX_NOEXCEPTnoexcept |
1018 | { |
1019 | --_M_current; |
1020 | return *this; |
1021 | } |
1022 | |
1023 | _GLIBCXX20_CONSTEXPR |
1024 | __normal_iterator |
1025 | operator--(int) _GLIBCXX_NOEXCEPTnoexcept |
1026 | { return __normal_iterator(_M_current--); } |
1027 | |
1028 | // Random access iterator requirements |
1029 | _GLIBCXX20_CONSTEXPR |
1030 | reference |
1031 | operator[](difference_type __n) const _GLIBCXX_NOEXCEPTnoexcept |
1032 | { return _M_current[__n]; } |
1033 | |
1034 | _GLIBCXX20_CONSTEXPR |
1035 | __normal_iterator& |
1036 | operator+=(difference_type __n) _GLIBCXX_NOEXCEPTnoexcept |
1037 | { _M_current += __n; return *this; } |
1038 | |
1039 | _GLIBCXX20_CONSTEXPR |
1040 | __normal_iterator |
1041 | operator+(difference_type __n) const _GLIBCXX_NOEXCEPTnoexcept |
1042 | { return __normal_iterator(_M_current + __n); } |
1043 | |
1044 | _GLIBCXX20_CONSTEXPR |
1045 | __normal_iterator& |
1046 | operator-=(difference_type __n) _GLIBCXX_NOEXCEPTnoexcept |
1047 | { _M_current -= __n; return *this; } |
1048 | |
1049 | _GLIBCXX20_CONSTEXPR |
1050 | __normal_iterator |
1051 | operator-(difference_type __n) const _GLIBCXX_NOEXCEPTnoexcept |
1052 | { return __normal_iterator(_M_current - __n); } |
1053 | |
1054 | _GLIBCXX20_CONSTEXPR |
1055 | const _Iterator& |
1056 | base() const _GLIBCXX_NOEXCEPTnoexcept |
1057 | { return _M_current; } |
1058 | }; |
1059 | |
1060 | // Note: In what follows, the left- and right-hand-side iterators are |
1061 | // allowed to vary in types (conceptually in cv-qualification) so that |
1062 | // comparison between cv-qualified and non-cv-qualified iterators be |
1063 | // valid. However, the greedy and unfriendly operators in std::rel_ops |
1064 | // will make overload resolution ambiguous (when in scope) if we don't |
1065 | // provide overloads whose operands are of the same type. Can someone |
1066 | // remind me what generic programming is about? -- Gaby |
1067 | |
1068 | #if __cpp_lib_three_way_comparison |
1069 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1070 | requires requires (_IteratorL __lhs, _IteratorR __rhs) |
1071 | { { __lhs == __rhs } -> std::convertible_to<bool>; } |
1072 | constexpr bool |
1073 | operator==(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1074 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1075 | noexcept(noexcept(__lhs.base() == __rhs.base())) |
1076 | { return __lhs.base() == __rhs.base(); } |
1077 | |
1078 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1079 | constexpr std::__detail::__synth3way_t<_IteratorR, _IteratorL> |
1080 | operator<=>(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1081 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1082 | noexcept(noexcept(std::__detail::__synth3way(__lhs.base(), __rhs.base()))) |
1083 | { return std::__detail::__synth3way(__lhs.base(), __rhs.base()); } |
1084 | #else |
1085 | // Forward iterator requirements |
1086 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1087 | _GLIBCXX20_CONSTEXPR |
1088 | inline bool |
1089 | operator==(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1090 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1091 | _GLIBCXX_NOEXCEPTnoexcept |
1092 | { return __lhs.base() == __rhs.base(); } |
1093 | |
1094 | template<typename _Iterator, typename _Container> |
1095 | _GLIBCXX20_CONSTEXPR |
1096 | inline bool |
1097 | operator==(const __normal_iterator<_Iterator, _Container>& __lhs, |
1098 | const __normal_iterator<_Iterator, _Container>& __rhs) |
1099 | _GLIBCXX_NOEXCEPTnoexcept |
1100 | { return __lhs.base() == __rhs.base(); } |
1101 | |
1102 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1103 | _GLIBCXX20_CONSTEXPR |
1104 | inline bool |
1105 | operator!=(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1106 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1107 | _GLIBCXX_NOEXCEPTnoexcept |
1108 | { return __lhs.base() != __rhs.base(); } |
1109 | |
1110 | template<typename _Iterator, typename _Container> |
1111 | _GLIBCXX20_CONSTEXPR |
1112 | inline bool |
1113 | operator!=(const __normal_iterator<_Iterator, _Container>& __lhs, |
1114 | const __normal_iterator<_Iterator, _Container>& __rhs) |
1115 | _GLIBCXX_NOEXCEPTnoexcept |
1116 | { return __lhs.base() != __rhs.base(); } |
1117 | |
1118 | // Random access iterator requirements |
1119 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1120 | inline bool |
1121 | operator<(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1122 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1123 | _GLIBCXX_NOEXCEPTnoexcept |
1124 | { return __lhs.base() < __rhs.base(); } |
1125 | |
1126 | template<typename _Iterator, typename _Container> |
1127 | _GLIBCXX20_CONSTEXPR |
1128 | inline bool |
1129 | operator<(const __normal_iterator<_Iterator, _Container>& __lhs, |
1130 | const __normal_iterator<_Iterator, _Container>& __rhs) |
1131 | _GLIBCXX_NOEXCEPTnoexcept |
1132 | { return __lhs.base() < __rhs.base(); } |
1133 | |
1134 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1135 | inline bool |
1136 | operator>(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1137 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1138 | _GLIBCXX_NOEXCEPTnoexcept |
1139 | { return __lhs.base() > __rhs.base(); } |
1140 | |
1141 | template<typename _Iterator, typename _Container> |
1142 | _GLIBCXX20_CONSTEXPR |
1143 | inline bool |
1144 | operator>(const __normal_iterator<_Iterator, _Container>& __lhs, |
1145 | const __normal_iterator<_Iterator, _Container>& __rhs) |
1146 | _GLIBCXX_NOEXCEPTnoexcept |
1147 | { return __lhs.base() > __rhs.base(); } |
1148 | |
1149 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1150 | inline bool |
1151 | operator<=(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1152 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1153 | _GLIBCXX_NOEXCEPTnoexcept |
1154 | { return __lhs.base() <= __rhs.base(); } |
1155 | |
1156 | template<typename _Iterator, typename _Container> |
1157 | _GLIBCXX20_CONSTEXPR |
1158 | inline bool |
1159 | operator<=(const __normal_iterator<_Iterator, _Container>& __lhs, |
1160 | const __normal_iterator<_Iterator, _Container>& __rhs) |
1161 | _GLIBCXX_NOEXCEPTnoexcept |
1162 | { return __lhs.base() <= __rhs.base(); } |
1163 | |
1164 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1165 | inline bool |
1166 | operator>=(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1167 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1168 | _GLIBCXX_NOEXCEPTnoexcept |
1169 | { return __lhs.base() >= __rhs.base(); } |
1170 | |
1171 | template<typename _Iterator, typename _Container> |
1172 | _GLIBCXX20_CONSTEXPR |
1173 | inline bool |
1174 | operator>=(const __normal_iterator<_Iterator, _Container>& __lhs, |
1175 | const __normal_iterator<_Iterator, _Container>& __rhs) |
1176 | _GLIBCXX_NOEXCEPTnoexcept |
1177 | { return __lhs.base() >= __rhs.base(); } |
1178 | #endif // three-way comparison |
1179 | |
1180 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1181 | // According to the resolution of DR179 not only the various comparison |
1182 | // operators but also operator- must accept mixed iterator/const_iterator |
1183 | // parameters. |
1184 | template<typename _IteratorL, typename _IteratorR, typename _Container> |
1185 | #if __cplusplus201402L >= 201103L |
1186 | // DR 685. |
1187 | _GLIBCXX20_CONSTEXPR |
1188 | inline auto |
1189 | operator-(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1190 | const __normal_iterator<_IteratorR, _Container>& __rhs) noexcept |
1191 | -> decltype(__lhs.base() - __rhs.base()) |
1192 | #else |
1193 | inline typename __normal_iterator<_IteratorL, _Container>::difference_type |
1194 | operator-(const __normal_iterator<_IteratorL, _Container>& __lhs, |
1195 | const __normal_iterator<_IteratorR, _Container>& __rhs) |
1196 | #endif |
1197 | { return __lhs.base() - __rhs.base(); } |
1198 | |
1199 | template<typename _Iterator, typename _Container> |
1200 | _GLIBCXX20_CONSTEXPR |
1201 | inline typename __normal_iterator<_Iterator, _Container>::difference_type |
1202 | operator-(const __normal_iterator<_Iterator, _Container>& __lhs, |
1203 | const __normal_iterator<_Iterator, _Container>& __rhs) |
1204 | _GLIBCXX_NOEXCEPTnoexcept |
1205 | { return __lhs.base() - __rhs.base(); } |
1206 | |
1207 | template<typename _Iterator, typename _Container> |
1208 | _GLIBCXX20_CONSTEXPR |
1209 | inline __normal_iterator<_Iterator, _Container> |
1210 | operator+(typename __normal_iterator<_Iterator, _Container>::difference_type |
1211 | __n, const __normal_iterator<_Iterator, _Container>& __i) |
1212 | _GLIBCXX_NOEXCEPTnoexcept |
1213 | { return __normal_iterator<_Iterator, _Container>(__i.base() + __n); } |
1214 | |
1215 | _GLIBCXX_END_NAMESPACE_VERSION |
1216 | } // namespace |
1217 | |
1218 | namespace std _GLIBCXX_VISIBILITY(default)__attribute__ ((__visibility__ ("default"))) |
1219 | { |
1220 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
1221 | |
1222 | template<typename _Iterator, typename _Container> |
1223 | _GLIBCXX20_CONSTEXPR |
1224 | _Iterator |
1225 | __niter_base(__gnu_cxx::__normal_iterator<_Iterator, _Container> __it) |
1226 | _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)noexcept(std::is_nothrow_copy_constructible<_Iterator>:: value) |
1227 | { return __it.base(); } |
1228 | |
1229 | #if __cplusplus201402L >= 201103L |
1230 | /** |
1231 | * @addtogroup iterators |
1232 | * @{ |
1233 | */ |
1234 | |
1235 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1236 | template<semiregular _Sent> |
1237 | class move_sentinel |
1238 | { |
1239 | public: |
1240 | constexpr |
1241 | move_sentinel() |
1242 | noexcept(is_nothrow_default_constructible_v<_Sent>) |
1243 | : _M_last() { } |
1244 | |
1245 | constexpr explicit |
1246 | move_sentinel(_Sent __s) |
1247 | noexcept(is_nothrow_move_constructible_v<_Sent>) |
1248 | : _M_last(std::move(__s)) { } |
1249 | |
1250 | template<typename _S2> requires convertible_to<const _S2&, _Sent> |
1251 | constexpr |
1252 | move_sentinel(const move_sentinel<_S2>& __s) |
1253 | noexcept(is_nothrow_constructible_v<_Sent, const _S2&>) |
1254 | : _M_last(__s.base()) |
1255 | { } |
1256 | |
1257 | template<typename _S2> requires assignable_from<_Sent&, const _S2&> |
1258 | constexpr move_sentinel& |
1259 | operator=(const move_sentinel<_S2>& __s) |
1260 | noexcept(is_nothrow_assignable_v<_Sent, const _S2&>) |
1261 | { |
1262 | _M_last = __s.base(); |
1263 | return *this; |
1264 | } |
1265 | |
1266 | constexpr _Sent |
1267 | base() const |
1268 | noexcept(is_nothrow_copy_constructible_v<_Sent>) |
1269 | { return _M_last; } |
1270 | |
1271 | private: |
1272 | _Sent _M_last; |
1273 | }; |
1274 | #endif // C++20 |
1275 | |
1276 | // 24.4.3 Move iterators |
1277 | /** |
1278 | * Class template move_iterator is an iterator adapter with the same |
1279 | * behavior as the underlying iterator except that its dereference |
1280 | * operator implicitly converts the value returned by the underlying |
1281 | * iterator's dereference operator to an rvalue reference. Some |
1282 | * generic algorithms can be called with move iterators to replace |
1283 | * copying with moving. |
1284 | */ |
1285 | template<typename _Iterator> |
1286 | class move_iterator |
1287 | { |
1288 | _Iterator _M_current; |
1289 | |
1290 | using __traits_type = iterator_traits<_Iterator>; |
1291 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1292 | using __base_cat = typename __traits_type::iterator_category; |
1293 | #else |
1294 | using __base_ref = typename __traits_type::reference; |
1295 | #endif |
1296 | |
1297 | public: |
1298 | using iterator_type = _Iterator; |
1299 | |
1300 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1301 | using iterator_concept = input_iterator_tag; |
1302 | using iterator_category |
1303 | = __detail::__clamp_iter_cat<__base_cat, random_access_iterator_tag>; |
1304 | using value_type = iter_value_t<_Iterator>; |
1305 | using difference_type = iter_difference_t<_Iterator>; |
1306 | using pointer = _Iterator; |
1307 | using reference = iter_rvalue_reference_t<_Iterator>; |
1308 | #else |
1309 | typedef typename __traits_type::iterator_category iterator_category; |
1310 | typedef typename __traits_type::value_type value_type; |
1311 | typedef typename __traits_type::difference_type difference_type; |
1312 | // NB: DR 680. |
1313 | typedef _Iterator pointer; |
1314 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
1315 | // 2106. move_iterator wrapping iterators returning prvalues |
1316 | typedef typename conditional<is_reference<__base_ref>::value, |
1317 | typename remove_reference<__base_ref>::type&&, |
1318 | __base_ref>::type reference; |
1319 | #endif |
1320 | |
1321 | _GLIBCXX17_CONSTEXPR |
1322 | move_iterator() |
1323 | : _M_current() { } |
1324 | |
1325 | explicit _GLIBCXX17_CONSTEXPR |
1326 | move_iterator(iterator_type __i) |
1327 | : _M_current(std::move(__i)) { } |
1328 | |
1329 | template<typename _Iter> |
1330 | _GLIBCXX17_CONSTEXPR |
1331 | move_iterator(const move_iterator<_Iter>& __i) |
1332 | : _M_current(__i.base()) { } |
1333 | |
1334 | #if __cplusplus201402L <= 201703L |
1335 | _GLIBCXX17_CONSTEXPR iterator_type |
1336 | base() const |
1337 | { return _M_current; } |
1338 | #else |
1339 | constexpr iterator_type |
1340 | base() const & |
1341 | #if __cpp_lib_concepts |
1342 | requires copy_constructible<iterator_type> |
1343 | #endif |
1344 | { return _M_current; } |
1345 | |
1346 | constexpr iterator_type |
1347 | base() && |
1348 | { return std::move(_M_current); } |
1349 | #endif |
1350 | |
1351 | _GLIBCXX17_CONSTEXPR reference |
1352 | operator*() const |
1353 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1354 | { return ranges::iter_move(_M_current); } |
1355 | #else |
1356 | { return static_cast<reference>(*_M_current); } |
1357 | #endif |
1358 | |
1359 | _GLIBCXX17_CONSTEXPR pointer |
1360 | operator->() const |
1361 | { return _M_current; } |
1362 | |
1363 | _GLIBCXX17_CONSTEXPR move_iterator& |
1364 | operator++() |
1365 | { |
1366 | ++_M_current; |
1367 | return *this; |
1368 | } |
1369 | |
1370 | _GLIBCXX17_CONSTEXPR move_iterator |
1371 | operator++(int) |
1372 | { |
1373 | move_iterator __tmp = *this; |
1374 | ++_M_current; |
1375 | return __tmp; |
1376 | } |
1377 | |
1378 | #if __cpp_lib_concepts |
1379 | constexpr void |
1380 | operator++(int) requires (!forward_iterator<_Iterator>) |
1381 | { ++_M_current; } |
1382 | #endif |
1383 | |
1384 | _GLIBCXX17_CONSTEXPR move_iterator& |
1385 | operator--() |
1386 | { |
1387 | --_M_current; |
1388 | return *this; |
1389 | } |
1390 | |
1391 | _GLIBCXX17_CONSTEXPR move_iterator |
1392 | operator--(int) |
1393 | { |
1394 | move_iterator __tmp = *this; |
1395 | --_M_current; |
1396 | return __tmp; |
1397 | } |
1398 | |
1399 | _GLIBCXX17_CONSTEXPR move_iterator |
1400 | operator+(difference_type __n) const |
1401 | { return move_iterator(_M_current + __n); } |
1402 | |
1403 | _GLIBCXX17_CONSTEXPR move_iterator& |
1404 | operator+=(difference_type __n) |
1405 | { |
1406 | _M_current += __n; |
1407 | return *this; |
1408 | } |
1409 | |
1410 | _GLIBCXX17_CONSTEXPR move_iterator |
1411 | operator-(difference_type __n) const |
1412 | { return move_iterator(_M_current - __n); } |
1413 | |
1414 | _GLIBCXX17_CONSTEXPR move_iterator& |
1415 | operator-=(difference_type __n) |
1416 | { |
1417 | _M_current -= __n; |
1418 | return *this; |
1419 | } |
1420 | |
1421 | _GLIBCXX17_CONSTEXPR reference |
1422 | operator[](difference_type __n) const |
1423 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1424 | { return ranges::iter_move(_M_current + __n); } |
1425 | #else |
1426 | { return std::move(_M_current[__n]); } |
1427 | #endif |
1428 | |
1429 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1430 | template<sentinel_for<_Iterator> _Sent> |
1431 | friend constexpr bool |
1432 | operator==(const move_iterator& __x, const move_sentinel<_Sent>& __y) |
1433 | { return __x.base() == __y.base(); } |
1434 | |
1435 | template<sized_sentinel_for<_Iterator> _Sent> |
1436 | friend constexpr iter_difference_t<_Iterator> |
1437 | operator-(const move_sentinel<_Sent>& __x, const move_iterator& __y) |
1438 | { return __x.base() - __y.base(); } |
1439 | |
1440 | template<sized_sentinel_for<_Iterator> _Sent> |
1441 | friend constexpr iter_difference_t<_Iterator> |
1442 | operator-(const move_iterator& __x, const move_sentinel<_Sent>& __y) |
1443 | { return __x.base() - __y.base(); } |
1444 | |
1445 | friend constexpr iter_rvalue_reference_t<_Iterator> |
1446 | iter_move(const move_iterator& __i) |
1447 | noexcept(noexcept(ranges::iter_move(__i._M_current))) |
1448 | { return ranges::iter_move(__i._M_current); } |
1449 | |
1450 | template<indirectly_swappable<_Iterator> _Iter2> |
1451 | friend constexpr void |
1452 | iter_swap(const move_iterator& __x, const move_iterator<_Iter2>& __y) |
1453 | noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current))) |
1454 | { return ranges::iter_swap(__x._M_current, __y._M_current); } |
1455 | #endif // C++20 |
1456 | }; |
1457 | |
1458 | template<typename _IteratorL, typename _IteratorR> |
1459 | inline _GLIBCXX17_CONSTEXPR bool |
1460 | operator==(const move_iterator<_IteratorL>& __x, |
1461 | const move_iterator<_IteratorR>& __y) |
1462 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1463 | requires requires { { __x.base() == __y.base() } -> convertible_to<bool>; } |
1464 | #endif |
1465 | { return __x.base() == __y.base(); } |
1466 | |
1467 | #if __cpp_lib_three_way_comparison |
1468 | template<typename _IteratorL, |
1469 | three_way_comparable_with<_IteratorL> _IteratorR> |
1470 | constexpr compare_three_way_result_t<_IteratorL, _IteratorR> |
1471 | operator<=>(const move_iterator<_IteratorL>& __x, |
1472 | const move_iterator<_IteratorR>& __y) |
1473 | { return __x.base() <=> __y.base(); } |
1474 | #else |
1475 | template<typename _IteratorL, typename _IteratorR> |
1476 | inline _GLIBCXX17_CONSTEXPR bool |
1477 | operator!=(const move_iterator<_IteratorL>& __x, |
1478 | const move_iterator<_IteratorR>& __y) |
1479 | { return !(__x == __y); } |
1480 | #endif |
1481 | |
1482 | template<typename _IteratorL, typename _IteratorR> |
1483 | inline _GLIBCXX17_CONSTEXPR bool |
1484 | operator<(const move_iterator<_IteratorL>& __x, |
1485 | const move_iterator<_IteratorR>& __y) |
1486 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1487 | requires requires { { __x.base() < __y.base() } -> convertible_to<bool>; } |
1488 | #endif |
1489 | { return __x.base() < __y.base(); } |
1490 | |
1491 | template<typename _IteratorL, typename _IteratorR> |
1492 | inline _GLIBCXX17_CONSTEXPR bool |
1493 | operator<=(const move_iterator<_IteratorL>& __x, |
1494 | const move_iterator<_IteratorR>& __y) |
1495 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1496 | requires requires { { __y.base() < __x.base() } -> convertible_to<bool>; } |
1497 | #endif |
1498 | { return !(__y < __x); } |
1499 | |
1500 | template<typename _IteratorL, typename _IteratorR> |
1501 | inline _GLIBCXX17_CONSTEXPR bool |
1502 | operator>(const move_iterator<_IteratorL>& __x, |
1503 | const move_iterator<_IteratorR>& __y) |
1504 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1505 | requires requires { { __y.base() < __x.base() } -> convertible_to<bool>; } |
1506 | #endif |
1507 | { return __y < __x; } |
1508 | |
1509 | template<typename _IteratorL, typename _IteratorR> |
1510 | inline _GLIBCXX17_CONSTEXPR bool |
1511 | operator>=(const move_iterator<_IteratorL>& __x, |
1512 | const move_iterator<_IteratorR>& __y) |
1513 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1514 | requires requires { { __x.base() < __y.base() } -> convertible_to<bool>; } |
1515 | #endif |
1516 | { return !(__x < __y); } |
1517 | |
1518 | #if ! (__cplusplus201402L > 201703L && __cpp_lib_concepts) |
1519 | // Note: See __normal_iterator operators note from Gaby to understand |
1520 | // why we have these extra overloads for some move_iterator operators. |
1521 | |
1522 | // These extra overloads are not needed in C++20, because the ones above |
1523 | // are constrained with a requires-clause and so overload resolution will |
1524 | // prefer them to greedy unconstrained function templates. |
1525 | |
1526 | template<typename _Iterator> |
1527 | inline _GLIBCXX17_CONSTEXPR bool |
1528 | operator==(const move_iterator<_Iterator>& __x, |
1529 | const move_iterator<_Iterator>& __y) |
1530 | { return __x.base() == __y.base(); } |
1531 | |
1532 | template<typename _Iterator> |
1533 | inline _GLIBCXX17_CONSTEXPR bool |
1534 | operator!=(const move_iterator<_Iterator>& __x, |
1535 | const move_iterator<_Iterator>& __y) |
1536 | { return !(__x == __y); } |
1537 | |
1538 | template<typename _Iterator> |
1539 | inline _GLIBCXX17_CONSTEXPR bool |
1540 | operator<(const move_iterator<_Iterator>& __x, |
1541 | const move_iterator<_Iterator>& __y) |
1542 | { return __x.base() < __y.base(); } |
1543 | |
1544 | template<typename _Iterator> |
1545 | inline _GLIBCXX17_CONSTEXPR bool |
1546 | operator<=(const move_iterator<_Iterator>& __x, |
1547 | const move_iterator<_Iterator>& __y) |
1548 | { return !(__y < __x); } |
1549 | |
1550 | template<typename _Iterator> |
1551 | inline _GLIBCXX17_CONSTEXPR bool |
1552 | operator>(const move_iterator<_Iterator>& __x, |
1553 | const move_iterator<_Iterator>& __y) |
1554 | { return __y < __x; } |
1555 | |
1556 | template<typename _Iterator> |
1557 | inline _GLIBCXX17_CONSTEXPR bool |
1558 | operator>=(const move_iterator<_Iterator>& __x, |
1559 | const move_iterator<_Iterator>& __y) |
1560 | { return !(__x < __y); } |
1561 | #endif // ! C++20 |
1562 | |
1563 | // DR 685. |
1564 | template<typename _IteratorL, typename _IteratorR> |
1565 | inline _GLIBCXX17_CONSTEXPR auto |
1566 | operator-(const move_iterator<_IteratorL>& __x, |
1567 | const move_iterator<_IteratorR>& __y) |
1568 | -> decltype(__x.base() - __y.base()) |
1569 | { return __x.base() - __y.base(); } |
1570 | |
1571 | template<typename _Iterator> |
1572 | inline _GLIBCXX17_CONSTEXPR move_iterator<_Iterator> |
1573 | operator+(typename move_iterator<_Iterator>::difference_type __n, |
1574 | const move_iterator<_Iterator>& __x) |
1575 | { return __x + __n; } |
1576 | |
1577 | template<typename _Iterator> |
1578 | inline _GLIBCXX17_CONSTEXPR move_iterator<_Iterator> |
1579 | make_move_iterator(_Iterator __i) |
1580 | { return move_iterator<_Iterator>(std::move(__i)); } |
1581 | |
1582 | template<typename _Iterator, typename _ReturnType |
1583 | = typename conditional<__move_if_noexcept_cond |
1584 | <typename iterator_traits<_Iterator>::value_type>::value, |
1585 | _Iterator, move_iterator<_Iterator>>::type> |
1586 | inline _GLIBCXX17_CONSTEXPR _ReturnType |
1587 | __make_move_if_noexcept_iterator(_Iterator __i) |
1588 | { return _ReturnType(__i); } |
1589 | |
1590 | // Overload for pointers that matches std::move_if_noexcept more closely, |
1591 | // returning a constant iterator when we don't want to move. |
1592 | template<typename _Tp, typename _ReturnType |
1593 | = typename conditional<__move_if_noexcept_cond<_Tp>::value, |
1594 | const _Tp*, move_iterator<_Tp*>>::type> |
1595 | inline _GLIBCXX17_CONSTEXPR _ReturnType |
1596 | __make_move_if_noexcept_iterator(_Tp* __i) |
1597 | { return _ReturnType(__i); } |
1598 | |
1599 | #if __cplusplus201402L > 201703L && __cpp_lib_concepts |
1600 | // [iterators.common] Common iterators |
1601 | |
1602 | namespace __detail |
1603 | { |
1604 | template<typename _It> |
1605 | concept __common_iter_has_arrow = indirectly_readable<const _It> |
1606 | && (requires(const _It& __it) { __it.operator->(); } |
1607 | || is_reference_v<iter_reference_t<_It>> |
1608 | || constructible_from<iter_value_t<_It>, iter_reference_t<_It>>); |
1609 | |
1610 | } // namespace __detail |
1611 | |
1612 | /// An iterator/sentinel adaptor for representing a non-common range. |
1613 | template<input_or_output_iterator _It, sentinel_for<_It> _Sent> |
1614 | requires (!same_as<_It, _Sent>) && copyable<_It> |
1615 | class common_iterator |
1616 | { |
1617 | template<typename _Tp, typename _Up> |
1618 | static constexpr bool |
1619 | _S_noexcept1() |
1620 | { |
1621 | if constexpr (is_trivially_default_constructible_v<_Tp>) |
1622 | return is_nothrow_assignable_v<_Tp, _Up>; |
1623 | else |
1624 | return is_nothrow_constructible_v<_Tp, _Up>; |
1625 | } |
1626 | |
1627 | template<typename _It2, typename _Sent2> |
1628 | static constexpr bool |
1629 | _S_noexcept() |
1630 | { return _S_noexcept1<_It, _It2>() && _S_noexcept1<_Sent, _Sent2>(); } |
1631 | |
1632 | class _Proxy |
1633 | { |
1634 | iter_value_t<_It> _M_keep; |
1635 | |
1636 | _Proxy(iter_reference_t<_It>&& __x) |
1637 | : _M_keep(std::move(__x)) { } |
1638 | |
1639 | friend class common_iterator; |
1640 | |
1641 | public: |
1642 | const iter_value_t<_It>* |
1643 | operator->() const |
1644 | { return std::__addressof(_M_keep); } |
1645 | }; |
1646 | |
1647 | public: |
1648 | constexpr |
1649 | common_iterator() |
1650 | noexcept(is_nothrow_default_constructible_v<_It>) |
1651 | : _M_it(), _M_index(0) |
1652 | { } |
1653 | |
1654 | constexpr |
1655 | common_iterator(_It __i) |
1656 | noexcept(is_nothrow_move_constructible_v<_It>) |
1657 | : _M_it(std::move(__i)), _M_index(0) |
1658 | { } |
1659 | |
1660 | constexpr |
1661 | common_iterator(_Sent __s) |
1662 | noexcept(is_nothrow_move_constructible_v<_Sent>) |
1663 | : _M_sent(std::move(__s)), _M_index(1) |
1664 | { } |
1665 | |
1666 | template<typename _It2, typename _Sent2> |
1667 | requires convertible_to<const _It2&, _It> |
1668 | && convertible_to<const _Sent2&, _Sent> |
1669 | constexpr |
1670 | common_iterator(const common_iterator<_It2, _Sent2>& __x) |
1671 | noexcept(_S_noexcept<const _It2&, const _Sent2&>()) |
1672 | : _M_valueless(), _M_index(__x._M_index) |
1673 | { |
1674 | if (_M_index == 0) |
1675 | { |
1676 | if constexpr (is_trivially_default_constructible_v<_It>) |
1677 | _M_it = std::move(__x._M_it); |
1678 | else |
1679 | ::new((void*)std::__addressof(_M_it)) _It(__x._M_it); |
1680 | } |
1681 | else if (_M_index == 1) |
1682 | { |
1683 | if constexpr (is_trivially_default_constructible_v<_Sent>) |
1684 | _M_sent = std::move(__x._M_sent); |
1685 | else |
1686 | ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent); |
1687 | } |
1688 | } |
1689 | |
1690 | constexpr |
1691 | common_iterator(const common_iterator& __x) |
1692 | noexcept(_S_noexcept<const _It&, const _Sent&>()) |
1693 | : _M_valueless(), _M_index(__x._M_index) |
1694 | { |
1695 | if (_M_index == 0) |
1696 | { |
1697 | if constexpr (is_trivially_default_constructible_v<_It>) |
1698 | _M_it = std::move(__x._M_it); |
1699 | else |
1700 | ::new((void*)std::__addressof(_M_it)) _It(__x._M_it); |
1701 | } |
1702 | else if (_M_index == 1) |
1703 | { |
1704 | if constexpr (is_trivially_default_constructible_v<_Sent>) |
1705 | _M_sent = std::move(__x._M_sent); |
1706 | else |
1707 | ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent); |
1708 | } |
1709 | } |
1710 | |
1711 | common_iterator& |
1712 | operator=(const common_iterator& __x) |
1713 | noexcept(is_nothrow_copy_assignable_v<_It> |
1714 | && is_nothrow_copy_assignable_v<_Sent> |
1715 | && is_nothrow_copy_constructible_v<_It> |
1716 | && is_nothrow_copy_constructible_v<_Sent>) |
1717 | { |
1718 | return this->operator=<_It, _Sent>(__x); |
1719 | } |
1720 | |
1721 | template<typename _It2, typename _Sent2> |
1722 | requires convertible_to<const _It2&, _It> |
1723 | && convertible_to<const _Sent2&, _Sent> |
1724 | && assignable_from<_It&, const _It2&> |
1725 | && assignable_from<_Sent&, const _Sent2&> |
1726 | common_iterator& |
1727 | operator=(const common_iterator<_It2, _Sent2>& __x) |
1728 | noexcept(is_nothrow_constructible_v<_It, const _It2&> |
1729 | && is_nothrow_constructible_v<_Sent, const _Sent2&> |
1730 | && is_nothrow_assignable_v<_It, const _It2&> |
1731 | && is_nothrow_assignable_v<_Sent, const _Sent2&>) |
1732 | { |
1733 | switch(_M_index << 2 | __x._M_index) |
1734 | { |
1735 | case 0b0000: |
1736 | _M_it = __x._M_it; |
1737 | break; |
1738 | case 0b0101: |
1739 | _M_sent = __x._M_sent; |
1740 | break; |
1741 | case 0b0001: |
1742 | _M_it.~_It(); |
1743 | _M_index = -1; |
1744 | [[fallthrough]]; |
1745 | case 0b1001: |
1746 | ::new((void*)std::__addressof(_M_sent)) _Sent(__x._M_sent); |
1747 | _M_index = 1; |
1748 | break; |
1749 | case 0b0100: |
1750 | _M_sent.~_Sent(); |
1751 | _M_index = -1; |
1752 | [[fallthrough]]; |
1753 | case 0b1000: |
1754 | ::new((void*)std::__addressof(_M_it)) _It(__x._M_it); |
1755 | _M_index = 0; |
1756 | break; |
1757 | default: |
1758 | __glibcxx_assert(__x._M_has_value()); |
1759 | __builtin_unreachable(); |
1760 | } |
1761 | return *this; |
1762 | } |
1763 | |
1764 | ~common_iterator() |
1765 | { |
1766 | switch (_M_index) |
1767 | { |
1768 | case 0: |
1769 | _M_it.~_It(); |
1770 | break; |
1771 | case 1: |
1772 | _M_sent.~_Sent(); |
1773 | break; |
1774 | } |
1775 | } |
1776 | |
1777 | decltype(auto) |
1778 | operator*() |
1779 | { |
1780 | __glibcxx_assert(_M_index == 0); |
1781 | return *_M_it; |
1782 | } |
1783 | |
1784 | decltype(auto) |
1785 | operator*() const requires __detail::__dereferenceable<const _It> |
1786 | { |
1787 | __glibcxx_assert(_M_index == 0); |
1788 | return *_M_it; |
1789 | } |
1790 | |
1791 | decltype(auto) |
1792 | operator->() const requires __detail::__common_iter_has_arrow<_It> |
1793 | { |
1794 | __glibcxx_assert(_M_index == 0); |
1795 | if constexpr (is_pointer_v<_It> || requires { _M_it.operator->(); }) |
1796 | return _M_it; |
1797 | else if constexpr (is_reference_v<iter_reference_t<_It>>) |
1798 | { |
1799 | auto&& __tmp = *_M_it; |
1800 | return std::__addressof(__tmp); |
1801 | } |
1802 | else |
1803 | return _Proxy{*_M_it}; |
1804 | } |
1805 | |
1806 | common_iterator& |
1807 | operator++() |
1808 | { |
1809 | __glibcxx_assert(_M_index == 0); |
1810 | ++_M_it; |
1811 | return *this; |
1812 | } |
1813 | |
1814 | decltype(auto) |
1815 | operator++(int) |
1816 | { |
1817 | __glibcxx_assert(_M_index == 0); |
1818 | if constexpr (forward_iterator<_It>) |
1819 | { |
1820 | common_iterator __tmp = *this; |
1821 | ++*this; |
1822 | return __tmp; |
1823 | } |
1824 | else |
1825 | return _M_it++; |
1826 | } |
1827 | |
1828 | template<typename _It2, sentinel_for<_It> _Sent2> |
1829 | requires sentinel_for<_Sent, _It2> |
1830 | friend bool |
1831 | operator==(const common_iterator& __x, |
1832 | const common_iterator<_It2, _Sent2>& __y) |
1833 | { |
1834 | switch(__x._M_index << 2 | __y._M_index) |
1835 | { |
1836 | case 0b0000: |
1837 | case 0b0101: |
1838 | return true; |
1839 | case 0b0001: |
1840 | return __x._M_it == __y._M_sent; |
1841 | case 0b0100: |
1842 | return __x._M_sent == __y._M_it; |
1843 | default: |
1844 | __glibcxx_assert(__x._M_has_value()); |
1845 | __glibcxx_assert(__y._M_has_value()); |
1846 | __builtin_unreachable(); |
1847 | } |
1848 | } |
1849 | |
1850 | template<typename _It2, sentinel_for<_It> _Sent2> |
1851 | requires sentinel_for<_Sent, _It2> && equality_comparable_with<_It, _It2> |
1852 | friend bool |
1853 | operator==(const common_iterator& __x, |
1854 | const common_iterator<_It2, _Sent2>& __y) |
1855 | { |
1856 | switch(__x._M_index << 2 | __y._M_index) |
1857 | { |
1858 | case 0b0101: |
1859 | return true; |
1860 | case 0b0000: |
1861 | return __x._M_it == __y._M_it; |
1862 | case 0b0001: |
1863 | return __x._M_it == __y._M_sent; |
1864 | case 0b0100: |
1865 | return __x._M_sent == __y._M_it; |
1866 | default: |
1867 | __glibcxx_assert(__x._M_has_value()); |
1868 | __glibcxx_assert(__y._M_has_value()); |
1869 | __builtin_unreachable(); |
1870 | } |
1871 | } |
1872 | |
1873 | template<sized_sentinel_for<_It> _It2, sized_sentinel_for<_It> _Sent2> |
1874 | requires sized_sentinel_for<_Sent, _It2> |
1875 | friend iter_difference_t<_It2> |
1876 | operator-(const common_iterator& __x, |
1877 | const common_iterator<_It2, _Sent2>& __y) |
1878 | { |
1879 | switch(__x._M_index << 2 | __y._M_index) |
1880 | { |
1881 | case 0b0101: |
1882 | return 0; |
1883 | case 0b0000: |
1884 | return __x._M_it - __y._M_it; |
1885 | case 0b0001: |
1886 | return __x._M_it - __y._M_sent; |
1887 | case 0b0100: |
1888 | return __x._M_sent - __y._M_it; |
1889 | default: |
1890 | __glibcxx_assert(__x._M_has_value()); |
1891 | __glibcxx_assert(__y._M_has_value()); |
1892 | __builtin_unreachable(); |
1893 | } |
1894 | } |
1895 | |
1896 | friend iter_rvalue_reference_t<_It> |
1897 | iter_move(const common_iterator& __i) |
1898 | noexcept(noexcept(ranges::iter_move(std::declval<const _It&>()))) |
1899 | requires input_iterator<_It> |
1900 | { |
1901 | __glibcxx_assert(__i._M_index == 0); |
1902 | return ranges::iter_move(__i._M_it); |
1903 | } |
1904 | |
1905 | template<indirectly_swappable<_It> _It2, typename _Sent2> |
1906 | friend void |
1907 | iter_swap(const common_iterator& __x, |
1908 | const common_iterator<_It2, _Sent2>& __y) |
1909 | noexcept(noexcept(ranges::iter_swap(std::declval<const _It&>(), |
1910 | std::declval<const _It2&>()))) |
1911 | { |
1912 | __glibcxx_assert(__x._M_index == 0); |
1913 | __glibcxx_assert(__y._M_index == 0); |
1914 | return ranges::iter_swap(__x._M_it, __y._M_it); |
1915 | } |
1916 | |
1917 | private: |
1918 | template<input_or_output_iterator _It2, sentinel_for<_It2> _Sent2> |
1919 | friend class common_iterator; |
1920 | |
1921 | bool _M_has_value() const noexcept { return _M_index < 2; } |
1922 | |
1923 | union |
1924 | { |
1925 | _It _M_it; |
1926 | _Sent _M_sent; |
1927 | unsigned char _M_valueless; |
1928 | }; |
1929 | unsigned char _M_index; // 0==_M_it, 1==_M_sent, 2==valueless |
1930 | }; |
1931 | |
1932 | template<typename _It, typename _Sent> |
1933 | struct incrementable_traits<common_iterator<_It, _Sent>> |
1934 | { |
1935 | using difference_type = iter_difference_t<_It>; |
1936 | }; |
1937 | |
1938 | template<input_iterator _It, typename _Sent> |
1939 | struct iterator_traits<common_iterator<_It, _Sent>> |
1940 | { |
1941 | private: |
1942 | template<typename _Iter> |
1943 | struct __ptr |
1944 | { |
1945 | using type = void; |
1946 | }; |
1947 | |
1948 | template<typename _Iter> |
1949 | requires __detail::__common_iter_has_arrow<_Iter> |
1950 | struct __ptr<_Iter> |
1951 | { |
1952 | using _CIter = common_iterator<_Iter, _Sent>; |
1953 | using type = decltype(std::declval<const _CIter&>().operator->()); |
1954 | }; |
1955 | |
1956 | public: |
1957 | using iterator_concept = conditional_t<forward_iterator<_It>, |
1958 | forward_iterator_tag, input_iterator_tag>; |
1959 | using iterator_category = __detail::__clamp_iter_cat< |
1960 | typename iterator_traits<_It>::iterator_category, |
1961 | forward_iterator_tag, input_iterator_tag>; |
1962 | using value_type = iter_value_t<_It>; |
1963 | using difference_type = iter_difference_t<_It>; |
1964 | using pointer = typename __ptr<_It>::type; |
1965 | using reference = iter_reference_t<_It>; |
1966 | }; |
1967 | |
1968 | // [iterators.counted] Counted iterators |
1969 | |
1970 | /// An iterator adaptor that keeps track of the distance to the end. |
1971 | template<input_or_output_iterator _It> |
1972 | class counted_iterator |
1973 | { |
1974 | public: |
1975 | using iterator_type = _It; |
1976 | |
1977 | constexpr counted_iterator() = default; |
1978 | |
1979 | constexpr |
1980 | counted_iterator(_It __i, iter_difference_t<_It> __n) |
1981 | : _M_current(std::move(__i)), _M_length(__n) |
1982 | { __glibcxx_assert(__n >= 0); } |
1983 | |
1984 | template<typename _It2> |
1985 | requires convertible_to<const _It2&, _It> |
1986 | constexpr |
1987 | counted_iterator(const counted_iterator<_It2>& __x) |
1988 | : _M_current(__x._M_current), _M_length(__x._M_length) |
1989 | { } |
1990 | |
1991 | template<typename _It2> |
1992 | requires assignable_from<_It&, const _It2&> |
1993 | constexpr counted_iterator& |
1994 | operator=(const counted_iterator<_It2>& __x) |
1995 | { |
1996 | _M_current = __x._M_current; |
1997 | _M_length = __x._M_length; |
1998 | return *this; |
1999 | } |
2000 | |
2001 | constexpr _It |
2002 | base() const & |
2003 | noexcept(is_nothrow_copy_constructible_v<_It>) |
2004 | requires copy_constructible<_It> |
2005 | { return _M_current; } |
2006 | |
2007 | constexpr _It |
2008 | base() && |
2009 | noexcept(is_nothrow_move_constructible_v<_It>) |
2010 | { return std::move(_M_current); } |
2011 | |
2012 | constexpr iter_difference_t<_It> |
2013 | count() const noexcept { return _M_length; } |
2014 | |
2015 | constexpr decltype(auto) |
2016 | operator*() |
2017 | noexcept(noexcept(*_M_current)) |
2018 | { return *_M_current; } |
2019 | |
2020 | constexpr decltype(auto) |
2021 | operator*() const |
2022 | noexcept(noexcept(*_M_current)) |
2023 | requires __detail::__dereferenceable<const _It> |
2024 | { return *_M_current; } |
2025 | |
2026 | constexpr counted_iterator& |
2027 | operator++() |
2028 | { |
2029 | __glibcxx_assert(_M_length > 0); |
2030 | ++_M_current; |
2031 | --_M_length; |
2032 | return *this; |
2033 | } |
2034 | |
2035 | decltype(auto) |
2036 | operator++(int) |
2037 | { |
2038 | __glibcxx_assert(_M_length > 0); |
2039 | --_M_length; |
2040 | __tryif (true) |
2041 | { |
2042 | return _M_current++; |
2043 | } __catch(...)if (false) { |
2044 | ++_M_length; |
2045 | __throw_exception_again; |
2046 | } |
2047 | |
2048 | } |
2049 | |
2050 | constexpr counted_iterator |
2051 | operator++(int) requires forward_iterator<_It> |
2052 | { |
2053 | auto __tmp = *this; |
2054 | ++*this; |
2055 | return __tmp; |
2056 | } |
2057 | |
2058 | constexpr counted_iterator& |
2059 | operator--() requires bidirectional_iterator<_It> |
2060 | { |
2061 | --_M_current; |
2062 | ++_M_length; |
2063 | return *this; |
2064 | } |
2065 | |
2066 | constexpr counted_iterator |
2067 | operator--(int) requires bidirectional_iterator<_It> |
2068 | { |
2069 | auto __tmp = *this; |
2070 | --*this; |
2071 | return __tmp; |
2072 | } |
2073 | |
2074 | constexpr counted_iterator |
2075 | operator+(iter_difference_t<_It> __n) const |
2076 | requires random_access_iterator<_It> |
2077 | { return counted_iterator(_M_current + __n, _M_length - __n); } |
2078 | |
2079 | friend constexpr counted_iterator |
2080 | operator+(iter_difference_t<_It> __n, const counted_iterator& __x) |
2081 | requires random_access_iterator<_It> |
2082 | { return __x + __n; } |
2083 | |
2084 | constexpr counted_iterator& |
2085 | operator+=(iter_difference_t<_It> __n) |
2086 | requires random_access_iterator<_It> |
2087 | { |
2088 | __glibcxx_assert(__n <= _M_length); |
2089 | _M_current += __n; |
2090 | _M_length -= __n; |
2091 | return *this; |
2092 | } |
2093 | |
2094 | constexpr counted_iterator |
2095 | operator-(iter_difference_t<_It> __n) const |
2096 | requires random_access_iterator<_It> |
2097 | { return counted_iterator(_M_current - __n, _M_length + __n); } |
2098 | |
2099 | template<common_with<_It> _It2> |
2100 | friend constexpr iter_difference_t<_It2> |
2101 | operator-(const counted_iterator& __x, |
2102 | const counted_iterator<_It2>& __y) |
2103 | { return __y._M_length - __x._M_length; } |
2104 | |
2105 | friend constexpr iter_difference_t<_It> |
2106 | operator-(const counted_iterator& __x, default_sentinel_t) |
2107 | { return -__x._M_length; } |
2108 | |
2109 | friend constexpr iter_difference_t<_It> |
2110 | operator-(default_sentinel_t, const counted_iterator& __y) |
2111 | { return __y._M_length; } |
2112 | |
2113 | constexpr counted_iterator& |
2114 | operator-=(iter_difference_t<_It> __n) |
2115 | requires random_access_iterator<_It> |
2116 | { |
2117 | __glibcxx_assert(-__n <= _M_length); |
2118 | _M_current -= __n; |
2119 | _M_length += __n; |
2120 | return *this; |
2121 | } |
2122 | |
2123 | constexpr decltype(auto) |
2124 | operator[](iter_difference_t<_It> __n) const |
2125 | noexcept(noexcept(_M_current[__n])) |
2126 | requires random_access_iterator<_It> |
2127 | { |
2128 | __glibcxx_assert(__n < _M_length); |
2129 | return _M_current[__n]; |
2130 | } |
2131 | |
2132 | template<common_with<_It> _It2> |
2133 | friend constexpr bool |
2134 | operator==(const counted_iterator& __x, |
2135 | const counted_iterator<_It2>& __y) |
2136 | { return __x._M_length == __y._M_length; } |
2137 | |
2138 | friend constexpr bool |
2139 | operator==(const counted_iterator& __x, default_sentinel_t) |
2140 | { return __x._M_length == 0; } |
2141 | |
2142 | template<common_with<_It> _It2> |
2143 | friend constexpr strong_ordering |
2144 | operator<=>(const counted_iterator& __x, |
2145 | const counted_iterator<_It2>& __y) |
2146 | { return __y._M_length <=> __x._M_length; } |
2147 | |
2148 | friend constexpr iter_rvalue_reference_t<_It> |
2149 | iter_move(const counted_iterator& __i) |
2150 | noexcept(noexcept(ranges::iter_move(__i._M_current))) |
2151 | requires input_iterator<_It> |
2152 | { return ranges::iter_move(__i._M_current); } |
2153 | |
2154 | template<indirectly_swappable<_It> _It2> |
2155 | friend constexpr void |
2156 | iter_swap(const counted_iterator& __x, |
2157 | const counted_iterator<_It2>& __y) |
2158 | noexcept(noexcept(ranges::iter_swap(__x._M_current, __y._M_current))) |
2159 | { ranges::iter_swap(__x._M_current, __y._M_current); } |
2160 | |
2161 | private: |
2162 | template<input_or_output_iterator _It2> friend class counted_iterator; |
2163 | |
2164 | _It _M_current = _It(); |
2165 | iter_difference_t<_It> _M_length = 0; |
2166 | }; |
2167 | |
2168 | template<typename _It> |
2169 | struct incrementable_traits<counted_iterator<_It>> |
2170 | { |
2171 | using difference_type = iter_difference_t<_It>; |
2172 | }; |
2173 | |
2174 | template<input_iterator _It> |
2175 | struct iterator_traits<counted_iterator<_It>> : iterator_traits<_It> |
2176 | { |
2177 | using pointer = void; |
2178 | }; |
2179 | #endif // C++20 |
2180 | |
2181 | // @} group iterators |
2182 | |
2183 | template<typename _Iterator> |
2184 | auto |
2185 | __niter_base(move_iterator<_Iterator> __it) |
2186 | -> decltype(make_move_iterator(__niter_base(__it.base()))) |
2187 | { return make_move_iterator(__niter_base(__it.base())); } |
2188 | |
2189 | template<typename _Iterator> |
2190 | struct __is_move_iterator<move_iterator<_Iterator> > |
2191 | { |
2192 | enum { __value = 1 }; |
2193 | typedef __true_type __type; |
2194 | }; |
2195 | |
2196 | template<typename _Iterator> |
2197 | auto |
2198 | __miter_base(move_iterator<_Iterator> __it) |
2199 | -> decltype(__miter_base(__it.base())) |
2200 | { return __miter_base(__it.base()); } |
2201 | |
2202 | #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter)std::make_move_iterator(_Iter) std::make_move_iterator(_Iter) |
2203 | #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter)std::__make_move_if_noexcept_iterator(_Iter) \ |
2204 | std::__make_move_if_noexcept_iterator(_Iter) |
2205 | #else |
2206 | #define _GLIBCXX_MAKE_MOVE_ITERATOR(_Iter)std::make_move_iterator(_Iter) (_Iter) |
2207 | #define _GLIBCXX_MAKE_MOVE_IF_NOEXCEPT_ITERATOR(_Iter)std::__make_move_if_noexcept_iterator(_Iter) (_Iter) |
2208 | #endif // C++11 |
2209 | |
2210 | #if __cpp_deduction_guides >= 201606 |
2211 | // These helper traits are used for deduction guides |
2212 | // of associative containers. |
2213 | template<typename _InputIterator> |
2214 | using __iter_key_t = remove_const_t< |
2215 | typename iterator_traits<_InputIterator>::value_type::first_type>; |
2216 | |
2217 | template<typename _InputIterator> |
2218 | using __iter_val_t = |
2219 | typename iterator_traits<_InputIterator>::value_type::second_type; |
2220 | |
2221 | template<typename _T1, typename _T2> |
2222 | struct pair; |
2223 | |
2224 | template<typename _InputIterator> |
2225 | using __iter_to_alloc_t = |
2226 | pair<add_const_t<__iter_key_t<_InputIterator>>, |
2227 | __iter_val_t<_InputIterator>>; |
2228 | #endif // __cpp_deduction_guides |
2229 | |
2230 | _GLIBCXX_END_NAMESPACE_VERSION |
2231 | } // namespace |
2232 | |
2233 | #ifdef _GLIBCXX_DEBUG |
2234 | # include <debug/stl_iterator.h> |
2235 | #endif |
2236 | |
2237 | #endif |