Home | History | Annotate | Download | only in nir
      1 /*
      2  * Copyright  2014 Intel Corporation
      3  *
      4  * Permission is hereby granted, free of charge, to any person obtaining a
      5  * copy of this software and associated documentation files (the "Software"),
      6  * to deal in the Software without restriction, including without limitation
      7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      8  * and/or sell copies of the Software, and to permit persons to whom the
      9  * Software is furnished to do so, subject to the following conditions:
     10  *
     11  * The above copyright notice and this permission notice (including the next
     12  * paragraph) shall be included in all copies or substantial portions of the
     13  * Software.
     14  *
     15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     21  * IN THE SOFTWARE.
     22  *
     23  * Authors:
     24  *    Connor Abbott (cwabbott0 (at) gmail.com)
     25  *
     26  */
     27 
     28 #include "nir.h"
     29 
     30 #pragma once
     31 
     32 #ifdef __cplusplus
     33 extern "C" {
     34 #endif
     35 
     36 /** NIR Control Flow Modification
     37  *
     38  * This file contains various API's that make modifying control flow in NIR,
     39  * while maintaining the invariants checked by the validator, much easier.
     40  * There are two parts to this:
     41  *
     42  * 1. Inserting control flow (if's and loops) in various places, for creating
     43  *    IR either from scratch or as part of some lowering pass.
     44  * 2. Taking existing pieces of the IR and either moving them around or
     45  *    deleting them.
     46  */
     47 
     48 /** Control flow insertion. */
     49 
     50 /** puts a control flow node where the cursor is */
     51 void nir_cf_node_insert(nir_cursor cursor, nir_cf_node *node);
     52 
     53 /** puts a control flow node immediately after another control flow node */
     54 static inline void
     55 nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after)
     56 {
     57    nir_cf_node_insert(nir_after_cf_node(node), after);
     58 }
     59 
     60 /** puts a control flow node immediately before another control flow node */
     61 static inline void
     62 nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before)
     63 {
     64    nir_cf_node_insert(nir_before_cf_node(node), before);
     65 }
     66 
     67 /** puts a control flow node at the beginning of a list from an if, loop, or function */
     68 static inline void
     69 nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node)
     70 {
     71    nir_cf_node_insert(nir_before_cf_list(list), node);
     72 }
     73 
     74 /** puts a control flow node at the end of a list from an if, loop, or function */
     75 static inline void
     76 nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node)
     77 {
     78    nir_cf_node_insert(nir_after_cf_list(list), node);
     79 }
     80 
     81 
     82 /** Control flow motion.
     83  *
     84  * These functions let you take a part of a control flow list (basically
     85  * equivalent to a series of statement in GLSL) and "extract" it from the IR,
     86  * so that it's a free-floating piece of IR that can be either re-inserted
     87  * somewhere else or deleted entirely. A few notes on using it:
     88  *
     89  * 1. Phi nodes are considered attached to the piece of control flow that
     90  *    their sources come from. There are three places where phi nodes can
     91  *    occur, which are the three places where a block can have multiple
     92  *    predecessors:
     93  *
     94  *    1) After an if statement, if neither branch ends in a jump.
     95  *    2) After a loop, if there are multiple break's.
     96  *    3) At the beginning of a loop.
     97  *
     98  *    For #1, the phi node is considered to be part of the if, and for #2 and
     99  *    #3 the phi node is considered to be part of the loop. This allows us to
    100  *    keep phi's intact, but it means that phi nodes cannot be separated from
    101  *    the control flow they come from. For example, extracting an if without
    102  *    extracting all the phi nodes after it is not allowed, and neither is
    103  *    extracting only some of the phi nodes at the beginning of a block. It
    104  *    also means that extracting from the beginning of a basic block actually
    105  *    means extracting from the first non-phi instruction, since there's no
    106  *    situation where extracting phi nodes without extracting what comes
    107  *    before them makes any sense.
    108  *
    109  * 2. Phi node sources are guaranteed to remain valid, meaning that they still
    110  *    correspond one-to-one with the predecessors of the basic block they're
    111  *    part of. In addition, the original sources will be preserved unless they
    112  *    correspond to a break or continue that was deleted. However, no attempt
    113  *    is made to ensure that SSA form is maintained. In particular, it is
    114  *    *not* guaranteed that definitions of SSA values will dominate all their
    115  *    uses after all is said and done. Either the caller must ensure that this
    116  *    is the case, or it must insert extra phi nodes to restore SSA.
    117  *
    118  * 3. It is invalid to move a piece of IR with a break/continue outside of the
    119  *    loop it references. Doing this will result in invalid
    120  *    successors/predecessors and phi node sources.
    121  *
    122  * 4. It is invalid to move a piece of IR from one function implementation to
    123  *    another.
    124  *
    125  * 5. Extracting a control flow list will leave lots of dangling references to
    126  *    and from other pieces of the IR. It also leaves things in a not 100%
    127  *    consistent state. This means that some things (e.g. inserting
    128  *    instructions) might not work reliably on the extracted control flow. It
    129  *    also means that extracting control flow without re-inserting it or
    130  *    deleting it is a Bad Thing (tm).
    131  */
    132 
    133 typedef struct {
    134    struct exec_list list;
    135    nir_function_impl *impl; /* for cleaning up if the list is deleted */
    136 } nir_cf_list;
    137 
    138 void nir_cf_extract(nir_cf_list *extracted, nir_cursor begin, nir_cursor end);
    139 
    140 void nir_cf_reinsert(nir_cf_list *cf_list, nir_cursor cursor);
    141 
    142 void nir_cf_delete(nir_cf_list *cf_list);
    143 
    144 void nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent,
    145                        struct hash_table *remap_table);
    146 
    147 static inline void
    148 nir_cf_list_extract(nir_cf_list *extracted, struct exec_list *cf_list)
    149 {
    150    nir_cf_extract(extracted, nir_before_cf_list(cf_list),
    151                   nir_after_cf_list(cf_list));
    152 }
    153 
    154 /** removes a control flow node, doing any cleanup necessary */
    155 static inline void
    156 nir_cf_node_remove(nir_cf_node *node)
    157 {
    158    nir_cf_list list;
    159    nir_cf_extract(&list, nir_before_cf_node(node), nir_after_cf_node(node));
    160    nir_cf_delete(&list);
    161 }
    162 
    163 #ifdef __cplusplus
    164 }
    165 #endif
    166