@@ -45,6 +45,45 @@ impl IndexMut<Inst> for Insts {
4545 }
4646}
4747
48+ /// Storage for basic blocks within the DFG.
49+ #[ derive( Clone , PartialEq , Hash ) ]
50+ #[ cfg_attr( feature = "enable-serde" , derive( Serialize , Deserialize ) ) ]
51+ pub struct Blocks ( PrimaryMap < Block , BlockData > ) ;
52+
53+ impl Blocks {
54+ /// Create a new basic block.
55+ pub fn add ( & mut self ) -> Block {
56+ self . 0 . push ( BlockData :: new ( ) )
57+ }
58+
59+ /// Get the total number of basic blocks created in this function, whether they are
60+ /// currently inserted in the layout or not.
61+ ///
62+ /// This is intended for use with `SecondaryMap::with_capacity`.
63+ pub fn len ( & self ) -> usize {
64+ self . 0 . len ( )
65+ }
66+
67+ /// Returns `true` if the given block reference is valid.
68+ pub fn is_valid ( & self , block : Block ) -> bool {
69+ self . 0 . is_valid ( block)
70+ }
71+ }
72+
73+ impl Index < Block > for Blocks {
74+ type Output = BlockData ;
75+
76+ fn index ( & self , block : Block ) -> & BlockData {
77+ & self . 0 [ block]
78+ }
79+ }
80+
81+ impl IndexMut < Block > for Blocks {
82+ fn index_mut ( & mut self , block : Block ) -> & mut BlockData {
83+ & mut self . 0 [ block]
84+ }
85+ }
86+
4887/// A data flow graph defines all instructions and basic blocks in a function as well as
4988/// the data flow dependencies between them. The DFG also tracks values which can be either
5089/// instruction results or block parameters.
@@ -70,7 +109,7 @@ pub struct DataFlowGraph {
70109 ///
71110 /// This map is not in program order. That is handled by `Layout`, and so is the sequence of
72111 /// instructions contained in each block.
73- blocks : PrimaryMap < Block , BlockData > ,
112+ pub blocks : Blocks ,
74113
75114 /// Dynamic types created.
76115 pub dynamic_types : DynamicTypes ,
@@ -113,7 +152,7 @@ impl DataFlowGraph {
113152 Self {
114153 insts : Insts ( PrimaryMap :: new ( ) ) ,
115154 results : SecondaryMap :: new ( ) ,
116- blocks : PrimaryMap :: new ( ) ,
155+ blocks : Blocks ( PrimaryMap :: new ( ) ) ,
117156 dynamic_types : DynamicTypes :: new ( ) ,
118157 value_lists : ValueListPool :: new ( ) ,
119158 values : PrimaryMap :: new ( ) ,
@@ -130,7 +169,7 @@ impl DataFlowGraph {
130169 pub fn clear ( & mut self ) {
131170 self . insts . 0 . clear ( ) ;
132171 self . results . clear ( ) ;
133- self . blocks . clear ( ) ;
172+ self . blocks . 0 . clear ( ) ;
134173 self . dynamic_types . clear ( ) ;
135174 self . value_lists . clear ( ) ;
136175 self . values . clear ( ) ;
@@ -1084,17 +1123,17 @@ impl DataFlowGraph {
10841123impl DataFlowGraph {
10851124 /// Create a new basic block.
10861125 pub fn make_block ( & mut self ) -> Block {
1087- self . blocks . push ( BlockData :: new ( ) )
1126+ self . blocks . add ( )
10881127 }
10891128
10901129 /// Get the number of parameters on `block`.
10911130 pub fn num_block_params ( & self , block : Block ) -> usize {
1092- self . blocks [ block] . params . len ( & self . value_lists )
1131+ self . blocks [ block] . params ( & self . value_lists ) . len ( )
10931132 }
10941133
10951134 /// Get the parameters on `block`.
10961135 pub fn block_params ( & self , block : Block ) -> & [ Value ] {
1097- self . blocks [ block] . params . as_slice ( & self . value_lists )
1136+ self . blocks [ block] . params ( & self . value_lists )
10981137 }
10991138
11001139 /// Get the types of the parameters on `block`.
@@ -1250,7 +1289,7 @@ impl DataFlowGraph {
12501289/// match the function arguments.
12511290#[ derive( Clone , PartialEq , Hash ) ]
12521291#[ cfg_attr( feature = "enable-serde" , derive( Serialize , Deserialize ) ) ]
1253- struct BlockData {
1292+ pub struct BlockData {
12541293 /// List of parameters to this block.
12551294 params : ValueList ,
12561295}
@@ -1261,6 +1300,11 @@ impl BlockData {
12611300 params : ValueList :: new ( ) ,
12621301 }
12631302 }
1303+
1304+ /// Get the parameters on `block`.
1305+ pub fn params < ' a > ( & self , pool : & ' a ValueListPool ) -> & ' a [ Value ] {
1306+ self . params . as_slice ( pool)
1307+ }
12641308}
12651309
12661310/// Object that can display an instruction.
0 commit comments