22
22
#ifndef MLIR_IR_BLOCK_H
23
23
#define MLIR_IR_BLOCK_H
24
24
25
- #include " mlir/IR/Value .h"
25
+ #include " mlir/IR/BlockSupport .h"
26
26
#include " mlir/IR/Visitors.h"
27
- #include " llvm/ADT/PointerUnion.h"
28
- #include " llvm/ADT/ilist.h"
29
- #include " llvm/ADT/ilist_node.h"
30
-
31
- // ===----------------------------------------------------------------------===//
32
- // ilist_traits for Operation
33
- // ===----------------------------------------------------------------------===//
34
-
35
- namespace llvm {
36
- namespace ilist_detail {
37
- // Explicitly define the node access for the operation list so that we can
38
- // break the dependence on the Operation class in this header. This allows for
39
- // operations to have trailing Regions without a circular include
40
- // dependence.
41
- template <>
42
- struct SpecificNodeAccess <
43
- typename compute_node_options<::mlir::Operation>::type> : NodeAccess {
44
- protected:
45
- using OptionsT = typename compute_node_options<mlir::Operation>::type;
46
- using pointer = typename OptionsT::pointer;
47
- using const_pointer = typename OptionsT::const_pointer;
48
- using node_type = ilist_node_impl<OptionsT>;
49
-
50
- static node_type *getNodePtr (pointer N);
51
- static const node_type *getNodePtr (const_pointer N);
52
-
53
- static pointer getValuePtr (node_type *N);
54
- static const_pointer getValuePtr (const node_type *N);
55
- };
56
- } // end namespace ilist_detail
57
-
58
- template <> struct ilist_traits <::mlir::Operation> {
59
- using Operation = ::mlir::Operation;
60
- using op_iterator = simple_ilist<Operation>::iterator;
61
-
62
- static void deleteNode (Operation *op);
63
- void addNodeToList (Operation *op);
64
- void removeNodeFromList (Operation *op);
65
- void transferNodesFromList (ilist_traits<Operation> &otherList,
66
- op_iterator first, op_iterator last);
67
-
68
- private:
69
- mlir::Block *getContainingBlock ();
70
- };
71
- } // end namespace llvm
72
27
73
28
namespace mlir {
74
- using BlockOperand = IROperandImpl<Block>;
75
-
76
- class PredecessorIterator ;
77
- class SuccessorIterator ;
78
-
79
29
// / `Block` represents an ordered list of `Operation`s.
80
30
class Block : public IRObjectWithUseList ,
81
31
public llvm::ilist_node_with_parent<Block, Region> {
@@ -272,9 +222,13 @@ class Block : public IRObjectWithUseList,
272
222
273
223
// Predecessor iteration.
274
224
using pred_iterator = PredecessorIterator;
275
- pred_iterator pred_begin ();
276
- pred_iterator pred_end ();
277
- llvm::iterator_range<pred_iterator> getPredecessors ();
225
+ pred_iterator pred_begin () {
226
+ return pred_iterator ((BlockOperand *)getFirstUse ());
227
+ }
228
+ pred_iterator pred_end () { return pred_iterator (nullptr ); }
229
+ llvm::iterator_range<pred_iterator> getPredecessors () {
230
+ return {pred_begin (), pred_end ()};
231
+ }
278
232
279
233
// / Return true if this block has no predecessors.
280
234
bool hasNoPredecessors ();
@@ -292,10 +246,10 @@ class Block : public IRObjectWithUseList,
292
246
Block *getSuccessor (unsigned i);
293
247
294
248
// Successor iteration.
295
- using succ_iterator = SuccessorIterator ;
296
- succ_iterator succ_begin ();
297
- succ_iterator succ_end ();
298
- llvm::iterator_range<succ_iterator> getSuccessors ();
249
+ using succ_iterator = SuccessorRange::iterator ;
250
+ succ_iterator succ_begin () { return getSuccessors (). begin (); }
251
+ succ_iterator succ_end () { return getSuccessors (). end (); }
252
+ SuccessorRange getSuccessors () { return SuccessorRange ( this ); }
299
253
300
254
// ===--------------------------------------------------------------------===//
301
255
// Operation Walkers
@@ -381,105 +335,6 @@ class Block : public IRObjectWithUseList,
381
335
382
336
friend struct llvm ::ilist_traits<Block>;
383
337
};
384
-
385
- } // end namespace mlir
386
-
387
- // ===----------------------------------------------------------------------===//
388
- // ilist_traits for Block
389
- // ===----------------------------------------------------------------------===//
390
-
391
- namespace llvm {
392
-
393
- template <>
394
- struct ilist_traits <::mlir::Block> : public ilist_alloc_traits<::mlir::Block> {
395
- using Block = ::mlir::Block;
396
- using block_iterator = simple_ilist<::mlir::Block>::iterator;
397
-
398
- void addNodeToList (Block *block);
399
- void removeNodeFromList (Block *block);
400
- void transferNodesFromList (ilist_traits<Block> &otherList,
401
- block_iterator first, block_iterator last);
402
-
403
- private:
404
- mlir::Region *getParentRegion ();
405
- };
406
- } // end namespace llvm
407
-
408
- namespace mlir {
409
- // ===----------------------------------------------------------------------===//
410
- // Predecessors
411
- // ===----------------------------------------------------------------------===//
412
-
413
- // / Implement a predecessor iterator for blocks. This works by walking the use
414
- // / lists of the blocks. The entries on this list are the BlockOperands that
415
- // / are embedded into terminator operations. From the operand, we can get the
416
- // / terminator that contains it, and its parent block is the predecessor.
417
- class PredecessorIterator final
418
- : public llvm::mapped_iterator<ValueUseIterator<BlockOperand>,
419
- Block *(*)(BlockOperand &)> {
420
- static Block *unwrap (BlockOperand &value);
421
-
422
- public:
423
- using reference = Block *;
424
-
425
- // / Initializes the operand type iterator to the specified operand iterator.
426
- PredecessorIterator (ValueUseIterator<BlockOperand> it)
427
- : llvm::mapped_iterator<ValueUseIterator<BlockOperand>,
428
- Block *(*)(BlockOperand &)>(it, &unwrap) {}
429
- explicit PredecessorIterator (BlockOperand *operand)
430
- : PredecessorIterator(ValueUseIterator<BlockOperand>(operand)) {}
431
-
432
- // / Get the successor number in the predecessor terminator.
433
- unsigned getSuccessorIndex () const ;
434
- };
435
-
436
- inline auto Block::pred_begin () -> pred_iterator {
437
- return pred_iterator ((BlockOperand *)getFirstUse ());
438
- }
439
-
440
- inline auto Block::pred_end () -> pred_iterator {
441
- return pred_iterator (nullptr );
442
- }
443
-
444
- inline auto Block::getPredecessors () -> llvm::iterator_range<pred_iterator> {
445
- return {pred_begin (), pred_end ()};
446
- }
447
-
448
- // ===----------------------------------------------------------------------===//
449
- // Successors
450
- // ===----------------------------------------------------------------------===//
451
-
452
- // / This template implements the successor iterators for Block.
453
- class SuccessorIterator final
454
- : public indexed_accessor_iterator<SuccessorIterator, Block *, Block *,
455
- Block *, Block *> {
456
- public:
457
- // / Initializes the result iterator to the specified index.
458
- SuccessorIterator (Block *object, unsigned index)
459
- : indexed_accessor_iterator<SuccessorIterator, Block *, Block *, Block *,
460
- Block *>(object, index) {}
461
-
462
- SuccessorIterator (const SuccessorIterator &other)
463
- : SuccessorIterator(other.base, other.index) {}
464
-
465
- Block *operator *() const { return this ->base ->getSuccessor (this ->index ); }
466
-
467
- // / Get the successor number in the terminator.
468
- unsigned getSuccessorIndex () const { return this ->index ; }
469
- };
470
-
471
- inline auto Block::succ_begin () -> succ_iterator {
472
- return succ_iterator (this , 0 );
473
- }
474
-
475
- inline auto Block::succ_end () -> succ_iterator {
476
- return succ_iterator (this , getNumSuccessors ());
477
- }
478
-
479
- inline auto Block::getSuccessors () -> llvm::iterator_range<succ_iterator> {
480
- return {succ_begin (), succ_end ()};
481
- }
482
-
483
338
} // end namespace mlir
484
339
485
340
#endif // MLIR_IR_BLOCK_H
0 commit comments