114
114
#include " llvm/SandboxIR/Tracker.h"
115
115
#include " llvm/SandboxIR/Type.h"
116
116
#include " llvm/SandboxIR/Use.h"
117
+ #include " llvm/SandboxIR/User.h"
117
118
#include " llvm/SandboxIR/Value.h"
118
119
#include " llvm/Support/raw_ostream.h"
119
120
#include < iterator>
@@ -188,42 +189,6 @@ class CmpInst;
188
189
class ICmpInst ;
189
190
class FCmpInst ;
190
191
191
- // / Iterator for the `Use` edges of a User's operands.
192
- // / \Returns the operand `Use` when dereferenced.
193
- class OperandUseIterator {
194
- sandboxir::Use Use;
195
- // / Don't let the user create a non-empty OperandUseIterator.
196
- OperandUseIterator (const class Use &Use) : Use(Use) {}
197
- friend class User ; // For constructor
198
- #define DEF_INSTR (ID, OPC, CLASS ) friend class CLASS ; // For constructor
199
- #include " llvm/SandboxIR/SandboxIRValues.def"
200
-
201
- public:
202
- using difference_type = std::ptrdiff_t ;
203
- using value_type = sandboxir::Use;
204
- using pointer = value_type *;
205
- using reference = value_type &;
206
- using iterator_category = std::input_iterator_tag;
207
-
208
- OperandUseIterator () = default ;
209
- value_type operator *() const ;
210
- OperandUseIterator &operator ++();
211
- OperandUseIterator operator ++(int ) {
212
- auto Copy = *this ;
213
- this ->operator ++();
214
- return Copy;
215
- }
216
- bool operator ==(const OperandUseIterator &Other) const {
217
- return Use == Other.Use ;
218
- }
219
- bool operator !=(const OperandUseIterator &Other) const {
220
- return !(*this == Other);
221
- }
222
- OperandUseIterator operator +(unsigned Num) const ;
223
- OperandUseIterator operator -(unsigned Num) const ;
224
- int operator -(const OperandUseIterator &Other) const ;
225
- };
226
-
227
192
// / Argument of a sandboxir::Function.
228
193
class Argument : public sandboxir ::Value {
229
194
Argument (llvm::Argument *Arg, sandboxir::Context &Ctx)
@@ -243,97 +208,6 @@ class Argument : public sandboxir::Value {
243
208
#endif
244
209
};
245
210
246
- // / A sandboxir::User has operands.
247
- class User : public Value {
248
- protected:
249
- User (ClassID ID, llvm::Value *V, Context &Ctx) : Value(ID, V, Ctx) {}
250
-
251
- // / \Returns the Use edge that corresponds to \p OpIdx.
252
- // / Note: This is the default implementation that works for instructions that
253
- // / match the underlying LLVM instruction. All others should use a different
254
- // / implementation.
255
- Use getOperandUseDefault (unsigned OpIdx, bool Verify) const ;
256
- // / \Returns the Use for the \p OpIdx'th operand. This is virtual to allow
257
- // / instructions to deviate from the LLVM IR operands, which is a requirement
258
- // / for sandboxir Instructions that consist of more than one LLVM Instruction.
259
- virtual Use getOperandUseInternal (unsigned OpIdx, bool Verify) const = 0;
260
- friend class OperandUseIterator ; // for getOperandUseInternal()
261
-
262
- // / The default implementation works only for single-LLVMIR-instruction
263
- // / Users and only if they match exactly the LLVM instruction.
264
- unsigned getUseOperandNoDefault (const Use &Use) const {
265
- return Use.LLVMUse ->getOperandNo ();
266
- }
267
- // / \Returns the operand index of \p Use.
268
- virtual unsigned getUseOperandNo (const Use &Use) const = 0;
269
- friend unsigned Use::getOperandNo () const ; // For getUseOperandNo()
270
-
271
- void swapOperandsInternal (unsigned OpIdxA, unsigned OpIdxB) {
272
- assert (OpIdxA < getNumOperands () && " OpIdxA out of bounds!" );
273
- assert (OpIdxB < getNumOperands () && " OpIdxB out of bounds!" );
274
- auto UseA = getOperandUse (OpIdxA);
275
- auto UseB = getOperandUse (OpIdxB);
276
- UseA.swap (UseB);
277
- }
278
-
279
- #ifndef NDEBUG
280
- void verifyUserOfLLVMUse (const llvm::Use &Use) const ;
281
- #endif // NDEBUG
282
-
283
- public:
284
- // / For isa/dyn_cast.
285
- static bool classof (const Value *From);
286
- using op_iterator = OperandUseIterator;
287
- using const_op_iterator = OperandUseIterator;
288
- using op_range = iterator_range<op_iterator>;
289
- using const_op_range = iterator_range<const_op_iterator>;
290
-
291
- virtual op_iterator op_begin () {
292
- assert (isa<llvm::User>(Val) && " Expect User value!" );
293
- return op_iterator (getOperandUseInternal (0 , /* Verify=*/ false ));
294
- }
295
- virtual op_iterator op_end () {
296
- assert (isa<llvm::User>(Val) && " Expect User value!" );
297
- return op_iterator (
298
- getOperandUseInternal (getNumOperands (), /* Verify=*/ false ));
299
- }
300
- virtual const_op_iterator op_begin () const {
301
- return const_cast <User *>(this )->op_begin ();
302
- }
303
- virtual const_op_iterator op_end () const {
304
- return const_cast <User *>(this )->op_end ();
305
- }
306
-
307
- op_range operands () { return make_range<op_iterator>(op_begin (), op_end ()); }
308
- const_op_range operands () const {
309
- return make_range<const_op_iterator>(op_begin (), op_end ());
310
- }
311
- Value *getOperand (unsigned OpIdx) const { return getOperandUse (OpIdx).get (); }
312
- // / \Returns the operand edge for \p OpIdx. NOTE: This should also work for
313
- // / OpIdx == getNumOperands(), which is used for op_end().
314
- Use getOperandUse (unsigned OpIdx) const {
315
- return getOperandUseInternal (OpIdx, /* Verify=*/ true );
316
- }
317
- virtual unsigned getNumOperands () const {
318
- return isa<llvm::User>(Val) ? cast<llvm::User>(Val)->getNumOperands () : 0 ;
319
- }
320
-
321
- virtual void setOperand (unsigned OperandIdx, Value *Operand);
322
- // / Replaces any operands that match \p FromV with \p ToV. Returns whether any
323
- // / operands were replaced.
324
- bool replaceUsesOfWith (Value *FromV, Value *ToV);
325
-
326
- #ifndef NDEBUG
327
- void verify () const override {
328
- assert (isa<llvm::User>(Val) && " Expected User!" );
329
- }
330
- void dumpCommonHeader (raw_ostream &OS) const final ;
331
- void dumpOS (raw_ostream &OS) const override {
332
- // TODO: Remove this tmp implementation once we get the Instruction classes.
333
- }
334
- #endif
335
- };
336
-
337
211
class Constant : public sandboxir ::User {
338
212
protected:
339
213
Constant (llvm::Constant *C, sandboxir::Context &SBCtx)
0 commit comments