This is an archive of the discontinued LLVM Phabricator instance.

[MC] Merge MC[Sub,Super]RegIterator with mc_[sub,super]_reg_iterator
ClosedPublic

Authored by barannikov88 on Jun 11 2023, 12:36 PM.

Details

Summary

Turn MC*RegIterator into fully qualified iterators by deriving them from
iterator_adaptor_base. This makes mc_*_reg iterators redundant.

Diff Detail

Event Timeline

barannikov88 created this revision.Jun 11 2023, 12:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 11 2023, 12:36 PM

Pull in post-increment operator++

Herald added a project: Restricted Project. · View Herald TranscriptJun 11 2023, 1:27 PM

Move inline from declaration to definition

Drop an unrelated change

Comment default constructors, remove isValid

This is an NFC to make D152098 simpler.

There is a bit of boilerplate in the form of overridden operator++ because forward iterators
require operator* to return by reference ( C++ <= 17 AFAIK). concat_range relies on this.
operator++ caches the value so that operator* can return a reference.

Add const so that operator-> can be used

dblaikie added inline comments.Jun 14 2023, 3:24 PM
llvm/include/llvm/MC/MCRegisterInfo.h
542

You could still expose isValid through MCSubRegIterator & avoid needing to add the second iterator here? No big deal either way, I guess.

barannikov88 added inline comments.Jun 14 2023, 4:51 PM
llvm/include/llvm/MC/MCRegisterInfo.h
542

I tried to make them look like the usual iterators, that don't usually provide such kind of methods. But I guess having this method here won't hurt, will add.

It would be nice if we had an adaptation of default_sentinel_t from C++20, then this method could be rutned into operator==.

barannikov88 added inline comments.Jun 14 2023, 5:21 PM
llvm/include/llvm/MC/MCRegisterInfo.h
513

@dblaikie Could you comment on this?
This requires type type to be default constructible. In D152098 I add MCRegUnit that does not have a good default value. Maybe I should instead return a proxy object from operator* that will hold the value? Would that be correct w.r.t. iterator requirements? Probably factor the proxy out into a common (templated) base class, too.

Add isValid method

barannikov88 marked an inline comment as done.Jun 14 2023, 5:28 PM
barannikov88 added inline comments.Jun 14 2023, 5:37 PM
llvm/include/llvm/MC/MCRegisterInfo.h
513

Ah I guess this won't change a thing, I would still need to be able to construct the proxy from some value, which I don't have in case of the end iterator.

foad added a comment.Jun 15 2023, 1:45 AM

Sounds good to me but I don't have enough C++ expertise to review it properly.

dblaikie accepted this revision.Jun 15 2023, 2:19 PM
dblaikie added inline comments.
llvm/include/llvm/MC/MCRegisterInfo.h
513

If you need something that isn't default constructible/can't just have an instance hanging around as a member like this, the next best thing would be std::optional<T> so it can be non-constructed. If the overhead of the extra bool/padding in std::optional is significant, you could maybe use a raw aligned buffer and /maybe/ conditionalize on the validity of the underlying iterator to decide whether there's an object in that buffer or not. But ensuring the code doesn't leak/fail to construct/destroy properly would be subtle.

This revision is now accepted and ready to land.Jun 15 2023, 2:19 PM