summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArthur Eubanks <aeubanks@google.com>2021-09-30 13:57:55 -0700
committerArthur Eubanks <aeubanks@google.com>2021-10-01 10:17:41 -0700
commita7b4ce9cfd110db56d89fa8d76b39023d038de11 (patch)
treed98b67823f4b047105d76f76694aab9f3ad485da
parent31c92d515dd21c49a2b3bcedd991fffb3b6dfd55 (diff)
[NFC][AttributeList] Replace index_begin/end with an iterator
We expose the fact that we rely on unsigned wrapping to iterate through all indexes. This can be confusing. Rather, keeping it as an implementation detail through an iterator is less confusing and is less code. Reviewed By: rnk Differential Revision: https://reviews.llvm.org/D110885
-rw-r--r--lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp3
-rw-r--r--llvm/include/llvm/IR/Attributes.h29
-rw-r--r--llvm/lib/Bitcode/Writer/BitcodeWriter.cpp2
-rw-r--r--llvm/lib/Bitcode/Writer/ValueEnumerator.cpp2
-rw-r--r--llvm/lib/IR/Attributes.cpp2
-rw-r--r--llvm/lib/Transforms/Utils/FunctionComparator.cpp2
-rw-r--r--llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp3
7 files changed, 32 insertions, 11 deletions
diff --git a/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp b/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp
index edfadc94debe..bb09b5e171f0 100644
--- a/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptx86ABIFixups.cpp
@@ -237,8 +237,7 @@ bool fixupRSAllocationStructByValCalls(llvm::Module &module) {
llvm::AttributeList call_attribs = call_inst->getAttributes();
// iterate over the argument attributes
- for (unsigned I = call_attribs.index_begin(); I != call_attribs.index_end();
- I++) {
+ for (unsigned I : call_attribs.indexes()) {
// if this argument is passed by val
if (call_attribs.hasAttributeAtIndex(I, llvm::Attribute::ByVal)) {
// strip away the byval attribute
diff --git a/llvm/include/llvm/IR/Attributes.h b/llvm/include/llvm/IR/Attributes.h
index 24588faf6ffa..6e830b3ebbbe 100644
--- a/llvm/include/llvm/IR/Attributes.h
+++ b/llvm/include/llvm/IR/Attributes.h
@@ -851,9 +851,32 @@ public:
unsigned getNumAttrSets() const;
- /// Use these to iterate over the valid attribute indices.
- unsigned index_begin() const { return AttributeList::FunctionIndex; }
- unsigned index_end() const { return getNumAttrSets() - 1; }
+ // Implementation of indexes(). Produces iterators that wrap an index. Mostly
+ // to hide the awkwardness of unsigned wrapping when iterating over valid
+ // indexes.
+ struct index_iterator {
+ unsigned NumAttrSets;
+ index_iterator(int NumAttrSets) : NumAttrSets(NumAttrSets) {}
+ struct int_wrapper {
+ int_wrapper(unsigned i) : i(i) {}
+ unsigned i;
+ unsigned operator*() { return i; }
+ bool operator!=(const int_wrapper &Other) { return i != Other.i; }
+ int_wrapper &operator++() {
+ // This is expected to undergo unsigned wrapping since FunctionIndex is
+ // ~0 and that's where we start.
+ ++i;
+ return *this;
+ }
+ };
+
+ int_wrapper begin() { return int_wrapper(AttributeList::FunctionIndex); }
+
+ int_wrapper end() { return int_wrapper(NumAttrSets - 1); }
+ };
+
+ /// Use this to iterate over the valid attribute indexes.
+ index_iterator indexes() const { return index_iterator(getNumAttrSets()); }
/// operator==/!= - Provide equality predicates.
bool operator==(const AttributeList &RHS) const { return pImpl == RHS.pImpl; }
diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index a915d478890e..23d6132f185f 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -835,7 +835,7 @@ void ModuleBitcodeWriter::writeAttributeTable() {
SmallVector<uint64_t, 64> Record;
for (unsigned i = 0, e = Attrs.size(); i != e; ++i) {
AttributeList AL = Attrs[i];
- for (unsigned i = AL.index_begin(), e = AL.index_end(); i != e; ++i) {
+ for (unsigned i : AL.indexes()) {
AttributeSet AS = AL.getAttributes(i);
if (AS.hasAttributes())
Record.push_back(VE.getAttributeGroupID({i, AS}));
diff --git a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
index 0c6e7462e124..9465a3b11c8f 100644
--- a/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
+++ b/llvm/lib/Bitcode/Writer/ValueEnumerator.cpp
@@ -1039,7 +1039,7 @@ void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {
}
// Do lookups for all attribute groups.
- for (unsigned i = PAL.index_begin(), e = PAL.index_end(); i != e; ++i) {
+ for (unsigned i : PAL.indexes()) {
AttributeSet AS = PAL.getAttributes(i);
if (!AS.hasAttributes())
continue;
diff --git a/llvm/lib/IR/Attributes.cpp b/llvm/lib/IR/Attributes.cpp
index 2abd1fefa206..c43be77e7ca4 100644
--- a/llvm/lib/IR/Attributes.cpp
+++ b/llvm/lib/IR/Attributes.cpp
@@ -1527,7 +1527,7 @@ unsigned AttributeList::getNumAttrSets() const {
void AttributeList::print(raw_ostream &O) const {
O << "AttributeList[\n";
- for (unsigned i = index_begin(), e = index_end(); i != e; ++i) {
+ for (unsigned i : indexes()) {
if (!getAttributes(i).hasAttributes())
continue;
O << " { ";
diff --git a/llvm/lib/Transforms/Utils/FunctionComparator.cpp b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
index 2696557a719f..326864803d7c 100644
--- a/llvm/lib/Transforms/Utils/FunctionComparator.cpp
+++ b/llvm/lib/Transforms/Utils/FunctionComparator.cpp
@@ -110,7 +110,7 @@ int FunctionComparator::cmpAttrs(const AttributeList L,
if (int Res = cmpNumbers(L.getNumAttrSets(), R.getNumAttrSets()))
return Res;
- for (unsigned i = L.index_begin(), e = L.index_end(); i != e; ++i) {
+ for (unsigned i : L.indexes()) {
AttributeSet LAS = L.getAttributes(i);
AttributeSet RAS = R.getAttributes(i);
AttributeSet::iterator LI = LAS.begin(), LE = LAS.end();
diff --git a/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp b/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp
index 223866ba52c2..74aa39f6c7e1 100644
--- a/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp
+++ b/llvm/tools/llvm-reduce/deltas/ReduceAttributes.cpp
@@ -84,8 +84,7 @@ public:
AttrPtrVecVecTy &AttributeSetsToPreserve) {
assert(AttributeSetsToPreserve.empty() && "Should not be sharing vectors.");
AttributeSetsToPreserve.reserve(AL.getNumAttrSets());
- for (unsigned SetIdx = AL.index_begin(), SetEndIdx = AL.index_end();
- SetIdx != SetEndIdx; ++SetIdx) {
+ for (unsigned SetIdx : AL.indexes()) {
AttrPtrIdxVecVecTy AttributesToPreserve;
AttributesToPreserve.first = SetIdx;
visitAttributeSet(AL.getAttributes(AttributesToPreserve.first),