Skip to content

Commit f2c0283

Browse files
committed
Simplify DebugUtils
With the changes in SILValue there is no need for template functions in DebugUtils anymore.
1 parent 74c0b11 commit f2c0283

11 files changed

+40
-47
lines changed

Diff for: include/swift/SIL/DebugUtils.h

+17-25
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ inline void deleteAllDebugUses(ValueBase *Inst) {
5959
}
6060

6161
/// This iterator filters out any debug (or non-debug) instructions from a range
62-
/// of uses, provided by the underlying use-iterator \p Base.
62+
/// of uses, provided by the underlying ValueBaseUseIterator.
6363
/// If \p nonDebugInsts is true, then the iterator provides a view to all non-
6464
/// debug instructions. Otherwise it provides a view ot all debug-instructions.
65-
template <typename Base, bool nonDebugInsts> class DebugUseIterator
65+
template <bool nonDebugInsts> class DebugUseIterator
6666
: public std::iterator<std::forward_iterator_tag, Operand *, ptrdiff_t> {
6767

68-
Base BaseIterator;
68+
ValueBaseUseIterator BaseIterator;
6969

7070
// Skip any debug or non-debug instructions (depending on the nonDebugInsts
7171
// template argument).
@@ -84,7 +84,8 @@ template <typename Base, bool nonDebugInsts> class DebugUseIterator
8484

8585
public:
8686

87-
DebugUseIterator(Base BaseIterator) : BaseIterator(BaseIterator) {
87+
DebugUseIterator(ValueBaseUseIterator BaseIterator) :
88+
BaseIterator(BaseIterator) {
8889
skipInsts();
8990
}
9091

@@ -116,52 +117,43 @@ template <typename Base, bool nonDebugInsts> class DebugUseIterator
116117
};
117118

118119
/// Iterator for iteration over debug instructions.
119-
template <typename V> using DUIterator =
120-
DebugUseIterator<typename V::use_iterator, false>;
120+
using DUIterator = DebugUseIterator<false>;
121121

122122
/// Iterator for iteration over non-debug instructions.
123-
template <typename V> using NonDUIterator =
124-
DebugUseIterator<typename V::use_iterator, true>;
123+
using NonDUIterator = DebugUseIterator<true>;
125124

126125

127126
/// Returns a range of all debug instructions in the uses of a value (e.g.
128127
/// SILValue or SILInstruction).
129-
template <typename V>
130-
inline iterator_range<DUIterator<V>> getDebugUses(const V &value) {
131-
return make_range(DUIterator<V>(value.use_begin()),
132-
DUIterator<V>(value.use_end()));
128+
inline iterator_range<DUIterator> getDebugUses(SILValue V) {
129+
return make_range(DUIterator(V.use_begin()), DUIterator(V.use_end()));
133130
}
134131

135132
/// Returns a range of all non-debug instructions in the uses of a value (e.g.
136133
/// SILValue or SILInstruction).
137-
template <typename V>
138-
inline iterator_range<NonDUIterator<V>> getNonDebugUses(const V &value) {
139-
return make_range(NonDUIterator<V>(value.use_begin()),
140-
NonDUIterator<V>(value.use_end()));
134+
inline iterator_range<NonDUIterator> getNonDebugUses(SILValue V) {
135+
return make_range(NonDUIterator(V.use_begin()), NonDUIterator(V.use_end()));
141136
}
142137

143138
/// Returns true if a value (e.g. SILInstruction) has no uses except debug
144139
/// instructions.
145-
template <typename V>
146-
inline bool hasNoUsesExceptDebug(V *I) {
147-
auto NonDebugUses = getNonDebugUses(*I);
140+
inline bool hasNoUsesExceptDebug(SILValue V) {
141+
auto NonDebugUses = getNonDebugUses(V);
148142
return NonDebugUses.begin() == NonDebugUses.end();
149143
}
150144

151145
/// Returns true if a value (e.g. SILInstruction) has exactly one use which is
152146
/// not a debug instruction.
153-
template <typename V>
154-
inline bool hasOneNonDebugUse(const V &value) {
155-
auto Range = getNonDebugUses(value);
147+
inline bool hasOneNonDebugUse(SILValue V) {
148+
auto Range = getNonDebugUses(V);
156149
auto I = Range.begin(), E = Range.end();
157150
if (I == E) return false;
158151
return ++I == E;
159152
}
160153

161154
// Returns the use if the value has only one non debug user.
162-
template <typename V>
163-
inline SILInstruction *getSingleNonDebugUser(const V &value) {
164-
auto Range = getNonDebugUses(value);
155+
inline SILInstruction *getSingleNonDebugUser(SILValue V) {
156+
auto Range = getNonDebugUses(V);
165157
auto I = Range.begin(), E = Range.end();
166158
if (I == E) return nullptr;
167159
if (std::next(I) != E)

Diff for: lib/SILOptimizer/Analysis/ArraySemantic.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ bool swift::ArraySemanticsCall::isValidSignature() {
9494
AllocFuncName != "swift_bufferAllocateOnStack")
9595
return false;
9696

97-
if (!hasOneNonDebugUse(*AllocBufferAI))
97+
if (!hasOneNonDebugUse(AllocBufferAI))
9898
return false;
9999
}
100100
return true;
@@ -474,7 +474,8 @@ void swift::ArraySemanticsCall::removeCall() {
474474

475475
// array.isNativeTypeChecked might be shared among several get_element
476476
// calls. The last user should delete it.
477-
if (IsNative && getSingleNonDebugUser(*IsNative) == SemanticsCall) {
477+
if (IsNative && getSingleNonDebugUser((ApplyInst *)IsNative) ==
478+
SemanticsCall) {
478479
deleteAllDebugUses(IsNative);
479480
(*IsNative).replaceAllUsesWithUndef();
480481
IsNative.removeCall();

Diff for: lib/SILOptimizer/IPO/FunctionSignatureOpts.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ getNonTrivialNonDebugReleaseUse(SILArgument *Arg) {
101101
}
102102

103103
// Otherwise add all non-debug uses of I to the worklist.
104-
for (Operand *I : getNonDebugUses(*U))
104+
for (Operand *I : getNonDebugUses(U))
105105
Worklist.push_back(I->getUser());
106106
}
107107

Diff for: lib/SILOptimizer/SILCombiner/SILCombine.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ SILInstruction *SILCombiner::eraseInstFromFunction(SILInstruction &I,
302302
}
303303
}
304304

305-
for (Operand *DU : getDebugUses(I))
305+
for (Operand *DU : getDebugUses(&I))
306306
Worklist.remove(DU->getUser());
307307

308308
Worklist.remove(&I);

Diff for: lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ static ApplyInst *optimizeCastThroughThinFunctionPointer(
10601060
static bool
10611061
hasOnlyRetainReleaseUsers(ApplyInst *AI, SILInstruction *IgnoreUser,
10621062
SmallVectorImpl<SILInstruction *> &Users) {
1063-
for (auto *Use : getNonDebugUses(*AI)) {
1063+
for (auto *Use : getNonDebugUses(AI)) {
10641064
if (Use->getUser() == IgnoreUser)
10651065
continue;
10661066

Diff for: lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ SILCombiner::visitUncheckedAddrCastInst(UncheckedAddrCastInst *UADCI) {
241241
return nullptr;
242242

243243
// For each user U of the unchecked_addr_cast...
244-
for (auto U : getNonDebugUses(*UADCI))
244+
for (auto U : getNonDebugUses(UADCI))
245245
// Check if it is load. If it is not a load, bail...
246246
if (!isa<LoadInst>(U->getUser()))
247247
return nullptr;
@@ -251,7 +251,7 @@ SILCombiner::visitUncheckedAddrCastInst(UncheckedAddrCastInst *UADCI) {
251251

252252
// Ok, we have all loads. Lets simplify this. Go back through the loads a
253253
// second time, rewriting them into a load + bitcast from our source.
254-
auto UsesRange = getNonDebugUses(*UADCI);
254+
auto UsesRange = getNonDebugUses(UADCI);
255255
for (auto UI = UsesRange.begin(), E = UsesRange.end(); UI != E;) {
256256
// Grab the original load.
257257
LoadInst *L = cast<LoadInst>(UI->getUser());

Diff for: lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@ SILCombiner::visitAllocExistentialBoxInst(AllocExistentialBoxInst *AEBI) {
4848
ProjectExistentialBoxInst *SingleProjection = nullptr;
4949

5050
// For each user U of the alloc_existential_box...
51-
for (auto U : getNonDebugUses(*AEBI)) {
51+
for (auto U : getNonDebugUses(AEBI)) {
5252

5353
if (auto *PEBI = dyn_cast<ProjectExistentialBoxInst>(U->getUser())) {
5454
if (SingleProjection) return nullptr;
5555
SingleProjection = PEBI;
56-
for (auto AddrUse : getNonDebugUses(*PEBI)) {
56+
for (auto AddrUse : getNonDebugUses(PEBI)) {
5757
// Record stores into the box.
5858
if (auto *SI = dyn_cast<StoreInst>(AddrUse->getUser())) {
5959
// If this is not the only store into the box then bail out.
@@ -226,7 +226,7 @@ struct AllocStackAnalyzer : SILInstructionVisitor<AllocStackAnalyzer> {
226226
// anything other than the init_existential_addr/open_existential_addr
227227
// container.
228228

229-
for (auto *Op : getNonDebugUses(*ASI)) {
229+
for (auto *Op : getNonDebugUses(ASI)) {
230230
visit(Op->getUser());
231231

232232
// If we found a non-legal user, bail early.
@@ -266,7 +266,7 @@ struct AllocStackAnalyzer : SILInstructionVisitor<AllocStackAnalyzer> {
266266

267267
// Make sure that the open_existential does not have any uses except
268268
// destroy_addr.
269-
for (auto *Use : getNonDebugUses(*I)) {
269+
for (auto *Use : getNonDebugUses(I)) {
270270
if (!isa<DestroyAddrInst>(Use->getUser())) {
271271
LegalUsers = false;
272272
return;
@@ -399,7 +399,7 @@ SILInstruction *SILCombiner::visitLoadInst(LoadInst *LI) {
399399
// Go through the loads uses and add any users that are projections to the
400400
// projection list.
401401
llvm::SmallVector<ProjInstPairTy, 8> Projections;
402-
for (auto *UI : getNonDebugUses(*LI)) {
402+
for (auto *UI : getNonDebugUses(LI)) {
403403
auto *User = UI->getUser();
404404

405405
// If we have any non SEI, TEI instruction, don't do anything here.
@@ -901,7 +901,7 @@ visitUncheckedTakeEnumDataAddrInst(UncheckedTakeEnumDataAddrInst *TEDAI) {
901901
return nullptr;
902902

903903
// For each user U of the take_enum_data_addr...
904-
for (auto U : getNonDebugUses(*TEDAI))
904+
for (auto U : getNonDebugUses(TEDAI))
905905
// Check if it is load. If it is not a load, bail...
906906
if (!isa<LoadInst>(U->getUser()))
907907
return nullptr;
@@ -916,7 +916,7 @@ visitUncheckedTakeEnumDataAddrInst(UncheckedTakeEnumDataAddrInst *TEDAI) {
916916
// Go back through a second time now that we know all of our users are
917917
// loads. Perform the transformation on each load.
918918
SmallVector<LoadInst*, 4> ToRemove;
919-
for (auto U : getNonDebugUses(*TEDAI)) {
919+
for (auto U : getNonDebugUses(TEDAI)) {
920920
// Grab the load.
921921
LoadInst *L = cast<LoadInst>(U->getUser());
922922

Diff for: lib/SILOptimizer/Transforms/ArrayElementValuePropagation.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ bool ArrayAllocation::mapInitializationStores() {
105105
if (!UnsafeMutablePointerExtract)
106106
return false;
107107
auto *PointerToAddress = dyn_cast_or_null<PointerToAddressInst>(
108-
getSingleNonDebugUser(*UnsafeMutablePointerExtract));
108+
getSingleNonDebugUser(UnsafeMutablePointerExtract));
109109
if (!PointerToAddress)
110110
return false;
111111

@@ -129,7 +129,7 @@ bool ArrayAllocation::mapInitializationStores() {
129129
auto *IndexAddr = dyn_cast<IndexAddrInst>(Inst);
130130
if (!IndexAddr)
131131
return false;
132-
SI = dyn_cast_or_null<StoreInst>(getSingleNonDebugUser(*IndexAddr));
132+
SI = dyn_cast_or_null<StoreInst>(getSingleNonDebugUser(IndexAddr));
133133
if (!SI || SI->getDest().getDef() != IndexAddr)
134134
return false;
135135
auto *Index = dyn_cast<IntegerLiteralInst>(IndexAddr->getIndex());

Diff for: lib/SILOptimizer/Transforms/DeadCodeElimination.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ void DCE::propagateLiveBlockArgument(SILArgument *Arg) {
334334
// Conceptually, the dependency from a debug instruction to its definition
335335
// is in reverse direction: Only if its definition (the Arg) is alive, also
336336
// the debug_value instruction is alive.
337-
for (Operand *DU : getDebugUses(*Arg))
337+
for (Operand *DU : getDebugUses(Arg))
338338
markValueLive(DU->getUser());
339339

340340
if (Arg->isFunctionArg())
@@ -357,7 +357,7 @@ void DCE::propagateLiveness(SILInstruction *I) {
357357
// Conceptually, the dependency from a debug instruction to its definition
358358
// is in reverse direction: Only if its definition is alive, also the
359359
// debug_value instruction is alive.
360-
for (Operand *DU : getDebugUses(*I))
360+
for (Operand *DU : getDebugUses(I))
361361
markValueLive(DU->getUser());
362362

363363
// Handle all other reverse-dependency instructions, like cond_fail and

Diff for: lib/SILOptimizer/Transforms/SILCodeMotion.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ static bool sinkArgument(SILBasicBlock *BB, unsigned ArgNum) {
369369
Clones.push_back(FirstPredArg);
370370

371371
// We only move instructions with a single use.
372-
if (!FSI || !hasOneNonDebugUse(*FSI))
372+
if (!FSI || !hasOneNonDebugUse(FSI))
373373
return false;
374374

375375
// Don't move instructions that are sensitive to their location.
@@ -398,7 +398,7 @@ static bool sinkArgument(SILBasicBlock *BB, unsigned ArgNum) {
398398
// Find the Nth argument passed to BB.
399399
SILValue Arg = TI->getOperand(ArgNum);
400400
SILInstruction *SI = dyn_cast<SILInstruction>(Arg);
401-
if (!SI || !hasOneNonDebugUse(*SI))
401+
if (!SI || !hasOneNonDebugUse(SI))
402402
return false;
403403
if (SI->isIdenticalTo(FSI)) {
404404
Clones.push_back(SI);

Diff for: lib/SILOptimizer/Utils/Local.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1935,7 +1935,7 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
19351935
bool isLegal = true;
19361936
// init_existential instruction used to initialize this alloc_stack.
19371937
InitExistentialAddrInst *FoundIEI = nullptr;
1938-
for (auto Use: getNonDebugUses(*ASI)) {
1938+
for (auto Use: getNonDebugUses(ASI)) {
19391939
auto *User = Use->getUser();
19401940
if (isa<ExistentialMetatypeInst>(User) ||
19411941
isa<DestroyAddrInst>(User) ||
@@ -1994,7 +1994,7 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
19941994
// Check if this alloc_stack is only initialized once by means of
19951995
// a single init_existential_ref.
19961996
bool isLegal = true;
1997-
for (auto Use: getNonDebugUses(*ASRI)) {
1997+
for (auto Use: getNonDebugUses(ASRI)) {
19981998
auto *User = Use->getUser();
19991999
if (isa<ExistentialMetatypeInst>(User) || isa<StrongReleaseInst>(User))
20002000
continue;

0 commit comments

Comments
 (0)