Skip to content

Commit 07b568d

Browse files
authored
Merge pull request #36914 from gottesmm/pr-50f9fe327da8081f9050c5c892eb164aec334530
[ownership] Refactor out the composition type LoadOperation from CanonicalizeInstruction into OwnershipOptUtils.
2 parents 367305b + 5019e15 commit 07b568d

File tree

2 files changed

+59
-48
lines changed

2 files changed

+59
-48
lines changed

include/swift/SILOptimizer/Utils/OwnershipOptUtils.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,64 @@ class OwnershipReplaceSingleUseHelper {
177177
SILBasicBlock::iterator perform();
178178
};
179179

180+
/// An abstraction over LoadInst/LoadBorrowInst so one can handle both types of
181+
/// load using common code.
182+
struct LoadOperation {
183+
llvm::PointerUnion<LoadInst *, LoadBorrowInst *> value;
184+
185+
LoadOperation() : value() {}
186+
LoadOperation(SILInstruction *input) : value(nullptr) {
187+
if (auto *li = dyn_cast<LoadInst>(input)) {
188+
value = li;
189+
return;
190+
}
191+
192+
if (auto *lbi = dyn_cast<LoadBorrowInst>(input)) {
193+
value = lbi;
194+
return;
195+
}
196+
}
197+
198+
explicit operator bool() const { return !value.isNull(); }
199+
200+
SingleValueInstruction *operator*() const {
201+
if (value.is<LoadInst *>())
202+
return value.get<LoadInst *>();
203+
return value.get<LoadBorrowInst *>();
204+
}
205+
206+
const SingleValueInstruction *operator->() const {
207+
if (value.is<LoadInst *>())
208+
return value.get<LoadInst *>();
209+
return value.get<LoadBorrowInst *>();
210+
}
211+
212+
SingleValueInstruction *operator->() {
213+
if (value.is<LoadInst *>())
214+
return value.get<LoadInst *>();
215+
return value.get<LoadBorrowInst *>();
216+
}
217+
218+
SILValue getOperand() const {
219+
if (value.is<LoadInst *>())
220+
return value.get<LoadInst *>()->getOperand();
221+
return value.get<LoadBorrowInst *>()->getOperand();
222+
}
223+
224+
/// Return the ownership qualifier of the underlying load if we have a load or
225+
/// None if we have a load_borrow.
226+
///
227+
/// TODO: Rather than use an optional here, we should include an invalid
228+
/// representation in LoadOwnershipQualifier.
229+
Optional<LoadOwnershipQualifier> getOwnershipQualifier() const {
230+
if (auto *lbi = value.dyn_cast<LoadBorrowInst *>()) {
231+
return None;
232+
}
233+
234+
return value.get<LoadInst *>()->getOwnershipQualifier();
235+
}
236+
};
237+
180238
} // namespace swift
181239

182240
#endif

lib/SILOptimizer/Utils/CanonicalizeInstruction.cpp

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "swift/SIL/SILInstruction.h"
2828
#include "swift/SILOptimizer/Analysis/SimplifyInstruction.h"
2929
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
30+
#include "swift/SILOptimizer/Utils/OwnershipOptUtils.h"
3031
#include "llvm/ADT/Statistic.h"
3132
#include "llvm/Support/Debug.h"
3233

@@ -98,54 +99,6 @@ simplifyAndReplace(SILInstruction *inst, CanonicalizeInstruction &pass) {
9899
// Canonicalize Memory Operations
99100
//===----------------------------------------------------------------------===//
100101

101-
namespace {
102-
103-
struct LoadOperation {
104-
llvm::PointerUnion<LoadInst *, LoadBorrowInst *> value;
105-
106-
LoadOperation(SILInstruction *input) : value(nullptr) {
107-
if (auto *li = dyn_cast<LoadInst>(input)) {
108-
value = li;
109-
return;
110-
}
111-
112-
if (auto *lbi = dyn_cast<LoadBorrowInst>(input)) {
113-
value = lbi;
114-
return;
115-
}
116-
}
117-
118-
operator bool() const { return !value.isNull(); }
119-
120-
SingleValueInstruction *operator*() const {
121-
if (value.is<LoadInst *>())
122-
return value.get<LoadInst *>();
123-
return value.get<LoadBorrowInst *>();
124-
}
125-
126-
const SingleValueInstruction *operator->() const {
127-
if (value.is<LoadInst *>())
128-
return value.get<LoadInst *>();
129-
return value.get<LoadBorrowInst *>();
130-
}
131-
132-
SingleValueInstruction *operator->() {
133-
if (value.is<LoadInst *>())
134-
return value.get<LoadInst *>();
135-
return value.get<LoadBorrowInst *>();
136-
}
137-
138-
Optional<LoadOwnershipQualifier> getOwnershipQualifier() const {
139-
if (auto *lbi = value.dyn_cast<LoadBorrowInst *>()) {
140-
return None;
141-
}
142-
143-
return value.get<LoadInst *>()->getOwnershipQualifier();
144-
}
145-
};
146-
147-
} // anonymous namespace
148-
149102
// Replace all uses of an original struct or tuple extract instruction with the
150103
// given load instruction. The caller ensures that the load only loads the
151104
// extracted field.

0 commit comments

Comments
 (0)