llvm-project/llvm/lib/Target/Lanai
Duncan P. N. Exon Smith 5c001c367f ADT: Give ilist<T>::reverse_iterator a handle to the current node
Reverse iterators to doubly-linked lists can be simpler (and cheaper)
than std::reverse_iterator.  Make it so.

In particular, change ilist<T>::reverse_iterator so that it is *never*
invalidated unless the node it references is deleted.  This matches the
guarantees of ilist<T>::iterator.

(Note: MachineBasicBlock::iterator is *not* an ilist iterator, but a
MachineInstrBundleIterator<MachineInstr>.  This commit does not change
MachineBasicBlock::reverse_iterator, but it does update
MachineBasicBlock::reverse_instr_iterator.  See note at end of commit
message for details on bundle iterators.)

Given the list (with the Sentinel showing twice for simplicity):

     [Sentinel] <-> A <-> B <-> [Sentinel]

the following is now true:
 1. begin() represents A.
 2. begin() holds the pointer for A.
 3. end() represents [Sentinel].
 4. end() holds the poitner for [Sentinel].
 5. rbegin() represents B.
 6. rbegin() holds the pointer for B.
 7. rend() represents [Sentinel].
 8. rend() holds the pointer for [Sentinel].

The changes are #6 and #8.  Here are some properties from the old
scheme (which used std::reverse_iterator):
- rbegin() held the pointer for [Sentinel] and rend() held the pointer
  for A;
- operator*() cost two dereferences instead of one;
- converting from a valid iterator to its valid reverse_iterator
  involved a confusing increment; and
- "RI++->erase()" left RI invalid.  The unintuitive replacement was
  "RI->erase(), RE = end()".

With vector-like data structures these properties are hard to avoid
(since past-the-beginning is not a valid pointer), and don't impose a
real cost (since there's still only one dereference, and all iterators
are invalidated on erase).  But with lists, this was a poor design.

Specifically, the following code (which obviously works with normal
iterators) now works with ilist::reverse_iterator as well:

    for (auto RI = L.rbegin(), RE = L.rend(); RI != RE;)
      fooThatMightRemoveArgFromList(*RI++);

Converting between iterator and reverse_iterator for the same node uses
the getReverse() function.

    reverse_iterator iterator::getReverse();
    iterator reverse_iterator::getReverse();

Why doesn't iterator <=> reverse_iterator conversion use constructors?

In order to catch and update old code, reverse_iterator does not even
have an explicit conversion from iterator.  It wouldn't be safe because
there would be no reasonable way to catch all the bugs from the changed
semantic (see the changes at call sites that are part of this patch).

Old code used this API:

    std::reverse_iterator::reverse_iterator(iterator);
    iterator std::reverse_iterator::base();

Here's how to update from old code to new (that incorporates the
semantic change), assuming I is an ilist<>::iterator and RI is an
ilist<>::reverse_iterator:

            [Old]         ==>          [New]
    reverse_iterator(I)       (--I).getReverse()
    reverse_iterator(I)         ++I.getReverse()
  --reverse_iterator(I)           I.getReverse()
    reverse_iterator(++I)         I.getReverse()
          RI.base()          (--RI).getReverse()
          RI.base()            ++RI.getReverse()
        --RI.base()              RI.getReverse()
      (++RI).base()              RI.getReverse()
  delete &*RI, RE = end()         delete &*RI++
  RI->erase(), RE = end()         RI++->erase()

=======================================
Note: bundle iterators are out of scope
=======================================

MachineBasicBlock::iterator, also known as
MachineInstrBundleIterator<MachineInstr>, is a wrapper to represent
MachineInstr bundles.  The idea is that each operator++ takes you to the
beginning of the next bundle.  Implementing a sane reverse iterator for
this is harder than ilist.  Here are the options:
- Use std::reverse_iterator<MBB::i>.  Store a handle to the beginning of
  the next bundle.  A call to operator*() runs a loop (usually
  operator--() will be called 1 time, for unbundled instructions).
  Increment/decrement just works.  This is the status quo.
- Store a handle to the final node in the bundle.  A call to operator*()
  still runs a loop, but it iterates one time fewer (usually
  operator--() will be called 0 times, for unbundled instructions).
  Increment/decrement just works.
- Make the ilist_sentinel<MachineInstr> *always* store that it's the
  sentinel (instead of just in asserts mode).  Then the bundle iterator
  can sniff the sentinel bit in operator++().

I initially tried implementing the end() option as part of this commit,
but updating iterator/reverse_iterator conversion call sites was
error-prone.  I have a WIP series of patches that implements the final
option.

llvm-svn: 280032
2016-08-30 00:13:12 +00:00
..
AsmParser Change the target's name, s/LanaiMCTargetDesc/LanaiDesc/g. 2016-08-23 10:43:01 +00:00
Disassembler Change the target's name, s/LanaiMCTargetDesc/LanaiDesc/g. 2016-08-23 10:43:01 +00:00
InstPrinter [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
MCTargetDesc LLVMLanaDesc: Update libdesp. 2016-08-23 10:47:40 +00:00
TargetInfo
CMakeLists.txt [lanai] Use peephole optimizer to generate more conditional ALU operations. 2016-07-07 23:36:04 +00:00
LLVMBuild.txt Change the target's name, s/LanaiMCTargetDesc/LanaiDesc/g. 2016-08-23 10:43:01 +00:00
Lanai.h
Lanai.td
LanaiAluCode.h [lanai] Use const instead of constexpr 2016-08-23 14:36:53 +00:00
LanaiAsmPrinter.cpp [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiCallingConv.td
LanaiCondCode.h
LanaiDelaySlotFiller.cpp ADT: Give ilist<T>::reverse_iterator a handle to the current node 2016-08-30 00:13:12 +00:00
LanaiFrameLowering.cpp MachineFunction: Return reference for getFrameInfo(); NFC 2016-07-28 18:40:00 +00:00
LanaiFrameLowering.h [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiISelDAGToDAG.cpp [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiISelLowering.cpp MachineFunction: Return reference for getFrameInfo(); NFC 2016-07-28 18:40:00 +00:00
LanaiISelLowering.h [lanai] Use peephole optimizer to generate more conditional ALU operations. 2016-07-07 23:36:04 +00:00
LanaiInstrFormats.td
LanaiInstrInfo.cpp [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiInstrInfo.h Rename AnalyzeBranch* to analyzeBranch*. 2016-07-15 14:41:04 +00:00
LanaiInstrInfo.td [lanai] Treat .t as optional in assembly parser for RR operands and add predicate operand to ShiftRR 2016-07-09 18:26:04 +00:00
LanaiMCInstLower.cpp [lanai] Fix for LanaiDelaySlotFiller and LanaiMCInstLower.cpp 2016-04-03 00:49:27 +00:00
LanaiMCInstLower.h [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiMachineFunctionInfo.cpp
LanaiMachineFunctionInfo.h
LanaiMemAluCombiner.cpp MachineFunctionProperties/MIRParser: Rename AllVRegsAllocated->NoVRegs, compute it 2016-08-25 01:27:13 +00:00
LanaiRegisterInfo.cpp MachineFunction: Return reference for getFrameInfo(); NFC 2016-07-28 18:40:00 +00:00
LanaiRegisterInfo.h
LanaiRegisterInfo.td
LanaiSchedule.td Remove MinLatency in SchedMachineModel. NFC. 2016-04-26 00:37:46 +00:00
LanaiSelectionDAGInfo.cpp [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiSelectionDAGInfo.h Pass DebugLoc and SDLoc by const ref. 2016-06-12 15:39:02 +00:00
LanaiSubtarget.cpp [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiSubtarget.h [lanai] Use Optional<Reloc> in LanaiTargetMachine. 2016-05-20 03:21:37 +00:00
LanaiTargetMachine.cpp [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiTargetMachine.h [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiTargetObjectFile.cpp [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00
LanaiTargetObjectFile.h
LanaiTargetTransformInfo.h [lanai] Small cleanup: remove/comment out unused args 2016-07-15 22:38:32 +00:00