Skip to content

Commit 7641aa7

Browse files
authored
Merge pull request #16406 from jrose-apple/malfunction
[SIL] Avoid std::function in transform-iterators
2 parents 90ba980 + f6e85d1 commit 7641aa7

File tree

4 files changed

+51
-59
lines changed

4 files changed

+51
-59
lines changed

include/swift/SIL/SILBasicBlock.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,10 @@ public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock> {
163163

164164
ArrayRef<SILArgument *> getArguments() const { return ArgumentList; }
165165
using PHIArgumentArrayRefTy =
166-
TransformArrayRef<std::function<SILPHIArgument *(SILArgument *)>>;
166+
TransformArrayRef<SILPHIArgument *(*)(SILArgument *)>;
167167
PHIArgumentArrayRefTy getPHIArguments() const;
168168
using FunctionArgumentArrayRefTy =
169-
TransformArrayRef<std::function<SILFunctionArgument *(SILArgument *)>>;
169+
TransformArrayRef<SILFunctionArgument *(*)(SILArgument *)>;
170170
FunctionArgumentArrayRefTy getFunctionArguments() const;
171171

172172
unsigned getNumArguments() const { return ArgumentList.size(); }

include/swift/SIL/SILInstruction.h

Lines changed: 40 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ class SILInstructionResultArray {
171171
reverse_range getReversedValues() const;
172172

173173
using type_range = llvm::iterator_range<
174-
llvm::mapped_iterator<iterator, std::function<SILType(SILValue)>, SILType>>;
174+
llvm::mapped_iterator<iterator, SILType(*)(SILValue), SILType>>;
175175
type_range getTypes() const;
176176

177177
bool operator==(const SILInstructionResultArray &rhs);
@@ -197,18 +197,6 @@ class SILInstructionResultArray {
197197
/// Please do not use this outside of this class. It is only meant to speedup
198198
/// MultipleValueInstruction::getIndexOfResult(SILValue).
199199
const ValueBase *back() const;
200-
201-
/// Return the offset 1 past the end of the array or None if we are not
202-
/// actually storing anything.
203-
Optional<unsigned> getStartOffset() const {
204-
return empty() ? None : Optional<unsigned>(0);
205-
}
206-
207-
/// Return the offset 1 past the end of the array or None if we are not
208-
/// actually storing anything.
209-
Optional<unsigned> getEndOffset() const {
210-
return empty() ? None : Optional<unsigned>(size());
211-
}
212200
};
213201

214202
class SILInstructionResultArray::iterator {
@@ -220,7 +208,7 @@ class SILInstructionResultArray::iterator {
220208
SILInstructionResultArray Parent;
221209

222210
/// The index into the parent array.
223-
Optional<unsigned> Index;
211+
unsigned Index;
224212

225213
public:
226214
using difference_type = int;
@@ -230,34 +218,31 @@ class SILInstructionResultArray::iterator {
230218
using iterator_category = std::bidirectional_iterator_tag;
231219

232220
iterator() = default;
233-
iterator(const SILInstructionResultArray &Parent,
234-
Optional<unsigned> Index = 0)
221+
iterator(const SILInstructionResultArray &Parent, unsigned Index = 0)
235222
: Parent(Parent), Index(Index) {}
236223

237-
SILValue operator*() const { return Parent[Index.getValue()]; }
238-
SILValue operator*() { return Parent[Index.getValue()]; }
224+
SILValue operator*() const { return Parent[Index]; }
239225
SILValue operator->() const { return operator*(); }
240-
SILValue operator->() { return operator*(); }
241226

242227
iterator &operator++() {
243-
++Index.getValue();
228+
++Index;
244229
return *this;
245230
}
246231

247232
iterator operator++(int) {
248233
iterator copy = *this;
249-
++Index.getValue();
234+
++Index;
250235
return copy;
251236
}
252237

253238
iterator &operator--() {
254-
--Index.getValue();
239+
--Index;
255240
return *this;
256241
}
257242

258243
iterator operator--(int) {
259244
iterator copy = *this;
260-
--Index.getValue();
245+
--Index;
261246
return copy;
262247
}
263248

@@ -6646,29 +6631,35 @@ class TermInst : public NonValueInstruction {
66466631

66476632
using succblock_iterator =
66486633
TransformIterator<SILSuccessor *,
6649-
std::function<SILBasicBlock *(const SILSuccessor &)>>;
6634+
SILBasicBlock *(*)(const SILSuccessor &)>;
66506635
using const_succblock_iterator = TransformIterator<
66516636
const SILSuccessor *,
6652-
std::function<const SILBasicBlock *(const SILSuccessor &)>>;
6637+
const SILBasicBlock *(*)(const SILSuccessor &)>;
66536638
succblock_iterator succblock_begin() {
6654-
using FuncTy = std::function<SILBasicBlock *(const SILSuccessor &)>;
6655-
FuncTy F(&SILSuccessor::getBB);
6656-
return makeTransformIterator(getSuccessors().begin(), F);
6639+
return succblock_iterator(getSuccessors().begin(),
6640+
[](const SILSuccessor &succ) -> SILBasicBlock * {
6641+
return succ.getBB();
6642+
});
66576643
}
66586644
succblock_iterator succblock_end() {
6659-
using FuncTy = std::function<SILBasicBlock *(const SILSuccessor &)>;
6660-
FuncTy F(&SILSuccessor::getBB);
6661-
return makeTransformIterator(getSuccessors().end(), F);
6645+
return succblock_iterator(getSuccessors().end(),
6646+
[](const SILSuccessor &succ) -> SILBasicBlock * {
6647+
return succ.getBB();
6648+
});
66626649
}
66636650
const_succblock_iterator succblock_begin() const {
6664-
using FuncTy = std::function<const SILBasicBlock *(const SILSuccessor &)>;
6665-
FuncTy F(&SILSuccessor::getBB);
6666-
return makeTransformIterator(getSuccessors().begin(), F);
6651+
return const_succblock_iterator(
6652+
getSuccessors().begin(),
6653+
[](const SILSuccessor &succ) -> const SILBasicBlock * {
6654+
return succ.getBB();
6655+
});
66676656
}
66686657
const_succblock_iterator succblock_end() const {
6669-
using FuncTy = std::function<const SILBasicBlock *(const SILSuccessor &)>;
6670-
FuncTy F(&SILSuccessor::getBB);
6671-
return makeTransformIterator(getSuccessors().end(), F);
6658+
return const_succblock_iterator(
6659+
getSuccessors().end(),
6660+
[](const SILSuccessor &succ) -> const SILBasicBlock * {
6661+
return succ.getBB();
6662+
});
66726663
}
66736664

66746665
SILBasicBlock *getSingleSuccessorBlock() {
@@ -6691,23 +6682,26 @@ class TermInst : public NonValueInstruction {
66916682

66926683
using SuccessorBlockListTy =
66936684
TransformRange<SuccessorListTy,
6694-
std::function<SILBasicBlock *(const SILSuccessor &)>>;
6685+
SILBasicBlock *(*)(const SILSuccessor &)>;
66956686
using ConstSuccessorBlockListTy =
6696-
TransformRange<ConstSuccessorListTy, std::function<const SILBasicBlock *(
6697-
const SILSuccessor &)>>;
6687+
TransformRange<ConstSuccessorListTy,
6688+
const SILBasicBlock *(*)(const SILSuccessor &)>;
66986689

66996690
/// Return the range of SILBasicBlocks that are successors of this block.
67006691
SuccessorBlockListTy getSuccessorBlocks() {
6701-
using FuncTy = std::function<SILBasicBlock *(const SILSuccessor &)>;
6702-
FuncTy F(&SILSuccessor::getBB);
6703-
return makeTransformRange(getSuccessors(), F);
6692+
return SuccessorBlockListTy(getSuccessors(),
6693+
[](const SILSuccessor &succ) -> SILBasicBlock* {
6694+
return succ.getBB();
6695+
});
67046696
}
67056697

67066698
/// Return the range of SILBasicBlocks that are successors of this block.
67076699
ConstSuccessorBlockListTy getSuccessorBlocks() const {
6708-
using FuncTy = std::function<const SILBasicBlock *(const SILSuccessor &)>;
6709-
FuncTy F(&SILSuccessor::getBB);
6710-
return makeTransformRange(getSuccessors(), F);
6700+
return ConstSuccessorBlockListTy(
6701+
getSuccessors(),
6702+
[](const SILSuccessor &succ) -> const SILBasicBlock * {
6703+
return succ.getBB();
6704+
});
67116705
}
67126706

67136707
DEFINE_ABSTRACT_NON_VALUE_INST_BOILERPLATE(TermInst)

lib/SIL/SILBasicBlock.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -336,20 +336,18 @@ bool SILBasicBlock::isEntry() const {
336336
}
337337

338338
SILBasicBlock::PHIArgumentArrayRefTy SILBasicBlock::getPHIArguments() const {
339-
using FuncTy = std::function<SILPHIArgument *(SILArgument *)>;
340-
FuncTy F = [](SILArgument *A) -> SILPHIArgument * {
339+
return PHIArgumentArrayRefTy(getArguments(),
340+
[](SILArgument *A) -> SILPHIArgument * {
341341
return cast<SILPHIArgument>(A);
342-
};
343-
return makeTransformArrayRef(getArguments(), F);
342+
});
344343
}
345344

346345
SILBasicBlock::FunctionArgumentArrayRefTy
347346
SILBasicBlock::getFunctionArguments() const {
348-
using FuncTy = std::function<SILFunctionArgument *(SILArgument *)>;
349-
FuncTy F = [](SILArgument *A) -> SILFunctionArgument * {
347+
return FunctionArgumentArrayRefTy(getArguments(),
348+
[](SILArgument *A) -> SILFunctionArgument* {
350349
return cast<SILFunctionArgument>(A);
351-
};
352-
return makeTransformArrayRef(getArguments(), F);
350+
});
353351
}
354352

355353
/// Returns true if this block ends in an unreachable or an apply of a

lib/SIL/SILInstruction.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,18 +1354,18 @@ operator==(const SILInstructionResultArray &other) {
13541354

13551355
SILInstructionResultArray::type_range
13561356
SILInstructionResultArray::getTypes() const {
1357-
std::function<SILType(SILValue)> F = [](SILValue V) -> SILType {
1357+
SILType (*F)(SILValue) = [](SILValue V) -> SILType {
13581358
return V->getType();
13591359
};
13601360
return {llvm::map_iterator(begin(), F), llvm::map_iterator(end(), F)};
13611361
}
13621362

13631363
SILInstructionResultArray::iterator SILInstructionResultArray::begin() const {
1364-
return iterator(*this, getStartOffset());
1364+
return iterator(*this, 0);
13651365
}
13661366

13671367
SILInstructionResultArray::iterator SILInstructionResultArray::end() const {
1368-
return iterator(*this, getEndOffset());
1368+
return iterator(*this, size());
13691369
}
13701370

13711371
SILInstructionResultArray::reverse_iterator

0 commit comments

Comments
 (0)