Skip to content

Commit 7c57e82

Browse files
authored
[NFC][TableGen] Code cleanup in Record.h/cpp (#138876)
- Use range for loops in several places. - Change some variable names to conform to LLVM coding standard. - Use ListSeparator instead of manual code to generate comma interleaved strings. - Remove unnecessary copies in SETDAGOP evaluation in BinOpInit::Fold. - Eliminate duplicated code in the 2 overloads of RecordVal::setValue. - Use explicit type in some range for loops. - Tested by verifying that all the .inc files generated by building all the *CommonTableGen targets stay unchanged.
1 parent 2f2c327 commit 7c57e82

File tree

2 files changed

+151
-223
lines changed

2 files changed

+151
-223
lines changed

llvm/include/llvm/TableGen/Record.h

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/DenseSet.h"
2020
#include "llvm/ADT/FoldingSet.h"
2121
#include "llvm/ADT/PointerIntPair.h"
22+
#include "llvm/ADT/STLExtras.h"
2223
#include "llvm/ADT/SmallVector.h"
2324
#include "llvm/ADT/StringExtras.h"
2425
#include "llvm/ADT/StringRef.h"
@@ -244,7 +245,7 @@ class RecordRecTy final : public RecTy,
244245
RecordRecTy &operator=(const RecordRecTy &) = delete;
245246

246247
// Do not use sized deallocation due to trailing objects.
247-
void operator delete(void *p) { ::operator delete(p); }
248+
void operator delete(void *Ptr) { ::operator delete(Ptr); }
248249

249250
static bool classof(const RecTy *RT) {
250251
return RT->getRecTyKind() == RecordRecTyKind;
@@ -598,7 +599,7 @@ class BitsInit final : public TypedInit,
598599
BitsInit &operator=(const BitsInit &) = delete;
599600

600601
// Do not use sized deallocation due to trailing objects.
601-
void operator delete(void *p) { ::operator delete(p); }
602+
void operator delete(void *Ptr) { ::operator delete(Ptr); }
602603

603604
static bool classof(const Init *I) {
604605
return I->getKind() == IK_BitsInit;
@@ -615,18 +616,8 @@ class BitsInit final : public TypedInit,
615616
convertInitializerBitRange(ArrayRef<unsigned> Bits) const override;
616617
std::optional<int64_t> convertInitializerToInt() const;
617618

618-
bool isComplete() const override {
619-
for (unsigned i = 0; i != getNumBits(); ++i)
620-
if (!getBit(i)->isComplete()) return false;
621-
return true;
622-
}
623-
624-
bool allInComplete() const {
625-
for (unsigned i = 0; i != getNumBits(); ++i)
626-
if (getBit(i)->isComplete()) return false;
627-
return true;
628-
}
629-
619+
bool isComplete() const override;
620+
bool allInComplete() const;
630621
bool isConcrete() const override;
631622
std::string getAsString() const override;
632623

@@ -769,7 +760,7 @@ class ListInit final : public TypedInit,
769760
ListInit &operator=(const ListInit &) = delete;
770761

771762
// Do not use sized deallocation due to trailing objects.
772-
void operator delete(void *p) { ::operator delete(p); }
763+
void operator delete(void *Ptr) { ::operator delete(Ptr); }
773764

774765
static bool classof(const Init *I) {
775766
return I->getKind() == IK_ListInit;
@@ -782,13 +773,13 @@ class ListInit final : public TypedInit,
782773
return ArrayRef(getTrailingObjects(), NumValues);
783774
}
784775

785-
const Init *getElement(unsigned Index) const { return getValues()[Index]; }
776+
const Init *getElement(unsigned Idx) const { return getValues()[Idx]; }
786777

787778
const RecTy *getElementType() const {
788779
return cast<ListRecTy>(getType())->getElementType();
789780
}
790781

791-
const Record *getElementAsRecord(unsigned i) const;
782+
const Record *getElementAsRecord(unsigned Idx) const;
792783

793784
const Init *convertInitializerTo(const RecTy *Ty) const override;
794785

@@ -1052,6 +1043,8 @@ class CondOpInit final : public TypedInit,
10521043
return ArrayRef(getTrailingObjects() + NumConds, NumConds);
10531044
}
10541045

1046+
auto getCondAndVals() const { return zip_equal(getConds(), getVals()); }
1047+
10551048
const Init *Fold(const Record *CurRec) const;
10561049

10571050
const Init *resolveReferences(Resolver &R) const override;
@@ -1341,7 +1334,7 @@ class VarDefInit final
13411334
VarDefInit &operator=(const VarDefInit &) = delete;
13421335

13431336
// Do not use sized deallocation due to trailing objects.
1344-
void operator delete(void *p) { ::operator delete(p); }
1337+
void operator delete(void *Ptr) { ::operator delete(Ptr); }
13451338

13461339
static bool classof(const Init *I) {
13471340
return I->getKind() == IK_VarDefInit;
@@ -1449,7 +1442,7 @@ class DagInit final
14491442
ArrayRef<const StringInit *> NameRange);
14501443
static const DagInit *
14511444
get(const Init *V, const StringInit *VN,
1452-
ArrayRef<std::pair<const Init *, const StringInit *>> Args);
1445+
ArrayRef<std::pair<const Init *, const StringInit *>> ArgAndNames);
14531446

14541447
void Profile(FoldingSetNodeID &ID) const;
14551448

@@ -1487,6 +1480,15 @@ class DagInit final
14871480
return getTrailingObjects<const StringInit *>(NumArgs);
14881481
}
14891482

1483+
// Return a range of std::pair.
1484+
auto getArgAndNames() const {
1485+
auto Zip = llvm::zip_equal(getArgs(), getArgNames());
1486+
using EltTy = decltype(*adl_begin(Zip));
1487+
return llvm::map_range(Zip, [](const EltTy &E) {
1488+
return std::make_pair(std::get<0>(E), std::get<1>(E));
1489+
});
1490+
}
1491+
14901492
const Init *resolveReferences(Resolver &R) const override;
14911493

14921494
bool isConcrete() const override;
@@ -1790,12 +1792,11 @@ class Record {
17901792
}
17911793

17921794
void removeValue(const Init *Name) {
1793-
for (unsigned i = 0, e = Values.size(); i != e; ++i)
1794-
if (Values[i].getNameInit() == Name) {
1795-
Values.erase(Values.begin()+i);
1796-
return;
1797-
}
1798-
llvm_unreachable("Cannot remove an entry that does not exist!");
1795+
auto It = llvm::find_if(
1796+
Values, [Name](const RecordVal &V) { return V.getNameInit() == Name; });
1797+
if (It == Values.end())
1798+
llvm_unreachable("Cannot remove an entry that does not exist!");
1799+
Values.erase(It);
17991800
}
18001801

18011802
void removeValue(StringRef Name) {
@@ -2115,10 +2116,7 @@ struct LessRecordRegister {
21152116

21162117
size_t size() { return Parts.size(); }
21172118

2118-
std::pair<bool, StringRef> getPart(size_t i) {
2119-
assert (i < Parts.size() && "Invalid idx!");
2120-
return Parts[i];
2121-
}
2119+
std::pair<bool, StringRef> getPart(size_t Idx) { return Parts[Idx]; }
21222120
};
21232121

21242122
bool operator()(const Record *Rec1, const Record *Rec2) const {

0 commit comments

Comments
 (0)