13361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri/*
23361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * Copyright © 2010 Luca Barbieri
33361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri *
43361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * Permission is hereby granted, free of charge, to any person obtaining a
53361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * copy of this software and associated documentation files (the "Software"),
63361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * to deal in the Software without restriction, including without limitation
73361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * the rights to use, copy, modify, merge, publish, distribute, sublicense,
83361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * and/or sell copies of the Software, and to permit persons to whom the
93361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * Software is furnished to do so, subject to the following conditions:
103361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri *
113361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * The above copyright notice and this permission notice (including the next
123361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * paragraph) shall be included in all copies or substantial portions of the
133361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * Software.
143361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri *
153361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
163361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
173361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
183361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
193361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
203361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
213361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri * DEALINGS IN THE SOFTWARE.
223361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri */
233361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
243361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri/**
25df883eb1575a740bf91e01cbe2eaa4dbc1f9f154Chad Versace * \file lower_jumps.cpp
26d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *
27d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * This pass lowers jumps (break, continue, and return) to if/else structures.
28d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *
29d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * It can be asked to:
30d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * 1. Pull jumps out of ifs where possible
31d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * 2. Remove all "continue"s, replacing them with an "execute flag"
32d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * 3. Replace all "break" with a single conditional one at the end of the loop
33d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * 4. Replace all "return"s with a single return at the end of the function,
34d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *    for the main function and/or other functions
35d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *
36d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * Applying this pass gives several benefits:
37d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * 1. All functions can be inlined.
38d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * 2. nv40 and other pre-DX10 chips without "continue" can be supported
39d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * 3. nv30 and other pre-DX10 chips with no control flow at all are better
40d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *    supported
41d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *
42d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * Continues are lowered by adding a per-loop "execute flag", initialized to
43d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * true, that when cleared inhibits all execution until the end of the loop.
44d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *
45d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * Breaks are lowered to continues, plus setting a "break flag" that is checked
46d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * at the end of the loop, and trigger the unique "break".
47d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *
48d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * Returns are lowered to breaks/continues, plus adding a "return flag" that
49d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * causes loops to break again out of their enclosing loops until all the
50d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * loops are exited: then the "execute flag" logic will ignore everything
51d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * until the end of the function.
52d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke *
53d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * Note that "continue" and "return" can also be implemented by adding
54d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * a dummy loop and using break.
55d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * However, this is bad for hardware with limited nesting depth, and
56d2d7a273c51127677d394d1bb9484a2d85c5dcd2Kenneth Graunke * prevents further optimization, and thus is not currently performed.
573361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri */
583361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
593361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri#include "glsl_types.h"
603361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri#include <string.h>
613361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri#include "ir.h"
623361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
63e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry/**
64e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * Enum recording the result of analyzing how control flow might exit
65e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * an IR node.
66e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *
67e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * Each possible value of jump_strength indicates a strictly stronger
68e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * guarantee on control flow than the previous value.
69e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *
70e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * The ordering of strengths roughly reflects the way jumps are
71e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * lowered: jumps with higher strength tend to be lowered to jumps of
72e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * lower strength.  Accordingly, strength is used as a heuristic to
73e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * determine which lowering to perform first.
74e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *
75e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * This enum is also used by get_jump_strength() to categorize
76e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * instructions as either break, continue, return, or other.  When
77e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * used in this fashion, strength_always_clears_execute_flag is not
78e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * used.
79e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *
80e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * The control flow analysis made by this optimization pass makes two
81e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * simplifying assumptions:
82e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *
83e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * - It ignores discard instructions, since they are lowered by a
84e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *   separate pass (lower_discard.cpp).
85e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *
86e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * - It assumes it is always possible for control to flow from a loop
87e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *   to the instruction immediately following it.  Technically, this
88e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *   is not true (since all execution paths through the loop might
89e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *   jump back to the top, or return from the function).
90e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry *
91e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * Both of these simplifying assumtions are safe, since they can never
92e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * cause reachable code to be incorrectly classified as unreachable;
93e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry * they can only do the opposite.
94e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry */
953361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbierienum jump_strength
963361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri{
97e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry   /**
98e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * Analysis has produced no guarantee on how control flow might
99e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * exit this IR node.  It might fall out the bottom (with or
100e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * without clearing the execute flag, if present), or it might
101e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * continue to the top of the innermost enclosing loop, break out
102e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * of it, or return from the function.
103e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    */
1043361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   strength_none,
105e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
106e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry   /**
107e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * The only way control can fall out the bottom of this node is
108e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * through a code path that clears the execute flag.  It might also
109e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * continue to the top of the innermost enclosing loop, break out
110e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * of it, or return from the function.
111e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    */
1123361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   strength_always_clears_execute_flag,
113e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
114e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry   /**
115e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * Control cannot fall out the bottom of this node.  It might
116e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * continue to the top of the innermost enclosing loop, break out
117e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * of it, or return from the function.
118e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    */
1193361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   strength_continue,
120e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
121e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry   /**
122e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * Control cannot fall out the bottom of this node, or continue the
123e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * top of the innermost enclosing loop.  It can only break out of
124e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * it or return from the function.
125e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    */
1263361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   strength_break,
127e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
128e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry   /**
129e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * Control cannot fall out the bottom of this node, continue to the
130e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * top of the innermost enclosing loop, or break out of it.  It can
131e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * only return from the function.
132e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    */
1331b173fb3bad3b53411a4b16a385556b23a247d93Brian Paul   strength_return
1343361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri};
1353361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1363361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieristruct block_record
1373361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri{
1383361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   /* minimum jump strength (of lowered IR, not pre-lowering IR)
1393361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri    *
1403361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri    * If the block ends with a jump, must be the strength of the jump.
1413361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri    * Otherwise, the jump would be dead and have been deleted before)
1423361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri    *
1433361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri    * If the block doesn't end with a jump, it can be different than strength_none if all paths before it lead to some jump
1443361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri    * (e.g. an if with a return in one branch, and a break in the other, while not lowering them)
1453361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri    * Note that identical jumps are usually unified though.
1463361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri    */
1473361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   jump_strength min_strength;
1483361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1493361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   /* can anything clear the execute flag? */
1503361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool may_clear_execute_flag;
1513361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1523361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   block_record()
1533361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
1543361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->min_strength = strength_none;
1553361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->may_clear_execute_flag = false;
1563361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
1573361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri};
1583361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1593361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieristruct loop_record
1603361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri{
1613361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_function_signature* signature;
1623361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_loop* loop;
1633361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1643361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   /* used to avoid lowering the break used to represent lowered breaks */
1653361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   unsigned nesting_depth;
1663361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool in_if_at_the_end_of_the_loop;
1673361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1683361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool may_set_return_flag;
1693361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1703361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_variable* break_flag;
1713361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_variable* execute_flag; /* cleared to emulate continue */
1723361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1733361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   loop_record(ir_function_signature* p_signature = 0, ir_loop* p_loop = 0)
1743361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
1753361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->signature = p_signature;
1763361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->loop = p_loop;
1773361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->nesting_depth = 0;
1783361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->in_if_at_the_end_of_the_loop = false;
1793361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->may_set_return_flag = false;
1803361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->break_flag = 0;
1813361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->execute_flag = 0;
1823361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
1833361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1843361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_variable* get_execute_flag()
1853361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
1863361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      /* also supported for the "function loop" */
1873361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(!this->execute_flag) {
1883361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         exec_list& list = this->loop ? this->loop->body_instructions : signature->body;
1893361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->execute_flag = new(this->signature) ir_variable(glsl_type::bool_type, "execute_flag", ir_var_temporary);
1903361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         list.push_head(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(execute_flag), new(this->signature) ir_constant(true), 0));
1913361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         list.push_head(this->execute_flag);
1923361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
1933361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      return this->execute_flag;
1943361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
1953361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
1963361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_variable* get_break_flag()
1973361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
1983361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      assert(this->loop);
1993361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(!this->break_flag) {
2003361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->break_flag = new(this->signature) ir_variable(glsl_type::bool_type, "break_flag", ir_var_temporary);
2013361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->loop->insert_before(this->break_flag);
2023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->loop->insert_before(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(break_flag), new(this->signature) ir_constant(false), 0));
2033361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
2043361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      return this->break_flag;
2053361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
2063361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri};
2073361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2083361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieristruct function_record
2093361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri{
2103361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_function_signature* signature;
2113361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_variable* return_flag; /* used to break out of all loops and then jump to the return instruction */
2123361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_variable* return_value;
213dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry   bool lower_return;
2143361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   unsigned nesting_depth;
2153361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
216dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry   function_record(ir_function_signature* p_signature = 0,
217dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry                   bool lower_return = false)
2183361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
2193361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->signature = p_signature;
2203361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->return_flag = 0;
2213361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->return_value = 0;
2223361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->nesting_depth = 0;
223dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry      this->lower_return = lower_return;
2243361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
2253361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2263361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_variable* get_return_flag()
2273361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
2283361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(!this->return_flag) {
2293361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->return_flag = new(this->signature) ir_variable(glsl_type::bool_type, "return_flag", ir_var_temporary);
2303361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->signature->body.push_head(new(this->signature) ir_assignment(new(this->signature) ir_dereference_variable(return_flag), new(this->signature) ir_constant(false), 0));
2313361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->signature->body.push_head(this->return_flag);
2323361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
2333361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      return this->return_flag;
2343361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
2353361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2363361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_variable* get_return_value()
2373361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
2383361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(!this->return_value) {
2393361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         assert(!this->signature->return_type->is_void());
2403361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         return_value = new(this->signature) ir_variable(this->signature->return_type, "return_value", ir_var_temporary);
2413361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->signature->body.push_head(this->return_value);
2423361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
2433361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      return this->return_value;
2443361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
2453361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri};
2463361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2473361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieristruct ir_lower_jumps_visitor : public ir_control_flow_visitor {
248e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry   /* Postconditions: on exit of any visit() function:
249e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    *
250e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * ANALYSIS: this->block.min_strength,
251e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * this->block.may_clear_execute_flag, and
252e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * this->loop.may_set_return_flag are updated to reflect the
253e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * characteristics of the visited statement.
254e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    *
255e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * DEAD_CODE_ELIMINATION: If this->block.min_strength is not
256e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * strength_none, the visited node is at the end of its exec_list.
257e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * In other words, any unreachable statements that follow the
258e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * visited statement in its exec_list have been removed.
259e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    *
260e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * CONTAINED_JUMPS_LOWERED: If the visited statement contains other
261e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * statements, then should_lower_jump() is false for all of the
262e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * return, break, or continue statements it contains.
263e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    *
264e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * Note that visiting a jump does not lower it.  That is the
265e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * responsibility of the statement (or function signature) that
266e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    * contains the jump.
267e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry    */
268e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
2693361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool progress;
2703361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2713361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   struct function_record function;
2723361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   struct loop_record loop;
2733361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   struct block_record block;
2743361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2753361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool pull_out_jumps;
2763361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool lower_continue;
2773361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool lower_break;
2783361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool lower_sub_return;
2793361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool lower_main_return;
2803361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2813361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_lower_jumps_visitor()
2823361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
2833361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->progress = false;
2843361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
2853361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2863361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   void truncate_after_instruction(exec_node *ir)
2873361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
2883361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if (!ir)
2893361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         return;
2903361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2913361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      while (!ir->get_next()->is_tail_sentinel()) {
2923361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         ((ir_instruction *)ir->get_next())->remove();
2933361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->progress = true;
2943361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
2953361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
2963361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
2973361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   void move_outer_block_inside(ir_instruction *ir, exec_list *inner_block)
2983361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
2993361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      while (!ir->get_next()->is_tail_sentinel()) {
3003361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         ir_instruction *move_ir = (ir_instruction *)ir->get_next();
3013361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
3023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         move_ir->remove();
3033361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         inner_block->push_tail(move_ir);
3043361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
3053361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
3063361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
30703145ba655ad9173a74b853843eccaae78ff392fPaul Berry   /**
30803145ba655ad9173a74b853843eccaae78ff392fPaul Berry    * Insert the instructions necessary to lower a return statement,
30903145ba655ad9173a74b853843eccaae78ff392fPaul Berry    * before the given return instruction.
31003145ba655ad9173a74b853843eccaae78ff392fPaul Berry    */
31103145ba655ad9173a74b853843eccaae78ff392fPaul Berry   void insert_lowered_return(ir_return *ir)
31203145ba655ad9173a74b853843eccaae78ff392fPaul Berry   {
31303145ba655ad9173a74b853843eccaae78ff392fPaul Berry      ir_variable* return_flag = this->function.get_return_flag();
31403145ba655ad9173a74b853843eccaae78ff392fPaul Berry      if(!this->function.signature->return_type->is_void()) {
31503145ba655ad9173a74b853843eccaae78ff392fPaul Berry         ir_variable* return_value = this->function.get_return_value();
31603145ba655ad9173a74b853843eccaae78ff392fPaul Berry         ir->insert_before(
31703145ba655ad9173a74b853843eccaae78ff392fPaul Berry            new(ir) ir_assignment(
31803145ba655ad9173a74b853843eccaae78ff392fPaul Berry               new (ir) ir_dereference_variable(return_value),
31903145ba655ad9173a74b853843eccaae78ff392fPaul Berry               ir->value));
32003145ba655ad9173a74b853843eccaae78ff392fPaul Berry      }
32103145ba655ad9173a74b853843eccaae78ff392fPaul Berry      ir->insert_before(
32203145ba655ad9173a74b853843eccaae78ff392fPaul Berry         new(ir) ir_assignment(
32303145ba655ad9173a74b853843eccaae78ff392fPaul Berry            new (ir) ir_dereference_variable(return_flag),
32403145ba655ad9173a74b853843eccaae78ff392fPaul Berry            new (ir) ir_constant(true)));
32503145ba655ad9173a74b853843eccaae78ff392fPaul Berry      this->loop.may_set_return_flag = true;
32603145ba655ad9173a74b853843eccaae78ff392fPaul Berry   }
32703145ba655ad9173a74b853843eccaae78ff392fPaul Berry
32803145ba655ad9173a74b853843eccaae78ff392fPaul Berry   /**
32903145ba655ad9173a74b853843eccaae78ff392fPaul Berry    * If the given instruction is a return, lower it to instructions
33003145ba655ad9173a74b853843eccaae78ff392fPaul Berry    * that store the return value (if there is one), set the return
33103145ba655ad9173a74b853843eccaae78ff392fPaul Berry    * flag, and then break.
33203145ba655ad9173a74b853843eccaae78ff392fPaul Berry    *
33303145ba655ad9173a74b853843eccaae78ff392fPaul Berry    * It is safe to pass NULL to this function.
33403145ba655ad9173a74b853843eccaae78ff392fPaul Berry    */
33503145ba655ad9173a74b853843eccaae78ff392fPaul Berry   void lower_return_unconditionally(ir_instruction *ir)
33603145ba655ad9173a74b853843eccaae78ff392fPaul Berry   {
33703145ba655ad9173a74b853843eccaae78ff392fPaul Berry      if (get_jump_strength(ir) != strength_return) {
33803145ba655ad9173a74b853843eccaae78ff392fPaul Berry         return;
33903145ba655ad9173a74b853843eccaae78ff392fPaul Berry      }
34003145ba655ad9173a74b853843eccaae78ff392fPaul Berry      insert_lowered_return((ir_return*)ir);
34103145ba655ad9173a74b853843eccaae78ff392fPaul Berry      ir->replace_with(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
34203145ba655ad9173a74b853843eccaae78ff392fPaul Berry   }
34303145ba655ad9173a74b853843eccaae78ff392fPaul Berry
344067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   /**
345067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    * Create the necessary instruction to replace a break instruction.
346067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    */
347067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   ir_instruction *create_lowered_break()
348067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   {
349067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      void *ctx = this->function.signature;
350067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      return new(ctx) ir_assignment(
351067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          new(ctx) ir_dereference_variable(this->loop.get_break_flag()),
352067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          new(ctx) ir_constant(true),
353067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          0);
354067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   }
355067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry
356067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   /**
357067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    * If the given instruction is a break, lower it to an instruction
358067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    * that sets the break flag, without consulting
359067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    * should_lower_jump().
360067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    *
361067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    * It is safe to pass NULL to this function.
362067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    */
363067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   void lower_break_unconditionally(ir_instruction *ir)
364067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   {
365067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      if (get_jump_strength(ir) != strength_break) {
366067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry         return;
367067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      }
368067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      ir->replace_with(create_lowered_break());
369067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   }
370067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry
371067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   /**
372067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    * If the block ends in a conditional or unconditional break, lower
373067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    * it, even though should_lower_jump() says it needn't be lowered.
374067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry    */
375067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   void lower_final_breaks(exec_list *block)
376067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   {
377067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      ir_instruction *ir = (ir_instruction *) block->get_tail();
378067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      lower_break_unconditionally(ir);
379067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      ir_if *ir_if = ir->as_if();
380067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      if (ir_if) {
381067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          lower_break_unconditionally(
382067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry              (ir_instruction *) ir_if->then_instructions.get_tail());
383067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          lower_break_unconditionally(
384067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry              (ir_instruction *) ir_if->else_instructions.get_tail());
385067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry      }
386067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry   }
387067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry
3883361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   virtual void visit(class ir_loop_jump * ir)
3893361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
390e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Eliminate all instructions after each one, since they are
391e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * unreachable.  This satisfies the DEAD_CODE_ELIMINATION
392e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * postcondition.
393e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
3943361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      truncate_after_instruction(ir);
395e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
396e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Set this->block.min_strength based on this instruction.  This
397e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * satisfies the ANALYSIS postcondition.  It is not necessary to
398e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * update this->block.may_clear_execute_flag or
399e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * this->loop.may_set_return_flag, because an unlowered jump
400e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * instruction can't change any flags.
401e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
4023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->block.min_strength = ir->is_break() ? strength_break : strength_continue;
403e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
404e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* The CONTAINED_JUMPS_LOWERED postcondition is already
405e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * satisfied, because jump statements can't contain other
406e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * statements.
407e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
4083361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
4093361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
4103361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   virtual void visit(class ir_return * ir)
4113361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
412e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Eliminate all instructions after each one, since they are
413e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * unreachable.  This satisfies the DEAD_CODE_ELIMINATION
414e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * postcondition.
415e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
4163361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      truncate_after_instruction(ir);
417e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
418e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Set this->block.min_strength based on this instruction.  This
419e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * satisfies the ANALYSIS postcondition.  It is not necessary to
420e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * update this->block.may_clear_execute_flag or
421e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * this->loop.may_set_return_flag, because an unlowered return
422e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * instruction can't change any flags.
423e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
4243361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->block.min_strength = strength_return;
425e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
426e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* The CONTAINED_JUMPS_LOWERED postcondition is already
427e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * satisfied, because jump statements can't contain other
428e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * statements.
429e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
4303361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
4313361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
4323361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   virtual void visit(class ir_discard * ir)
4333361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
434e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Nothing needs to be done.  The ANALYSIS and
435e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * DEAD_CODE_ELIMINATION postconditions are already satisfied,
436e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * because discard statements are ignored by this optimization
437e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * pass.  The CONTAINED_JUMPS_LOWERED postcondition is already
438e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * satisfied, because discard statements can't contain other
439e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * statements.
440e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
4419a3bd5e0452c9c791ba94155d3c9ddba42abd114Ian Romanick      (void) ir;
4423361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
4433361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
4443361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   enum jump_strength get_jump_strength(ir_instruction* ir)
4453361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
4463361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(!ir)
4473361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         return strength_none;
4483361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      else if(ir->ir_type == ir_type_loop_jump) {
4493361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(((ir_loop_jump*)ir)->is_break())
4503361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            return strength_break;
4513361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         else
4523361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            return strength_continue;
4533361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      } else if(ir->ir_type == ir_type_return)
4543361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         return strength_return;
4553361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      else
4563361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         return strength_none;
4573361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
4583361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
4593361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   bool should_lower_jump(ir_jump* ir)
4603361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
4613361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      unsigned strength = get_jump_strength(ir);
4623361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      bool lower;
4633361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      switch(strength)
4643361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      {
4653361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      case strength_none:
4663361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         lower = false; /* don't change this, code relies on it */
4673361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         break;
4683361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      case strength_continue:
4693361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         lower = lower_continue;
4703361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         break;
4713361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      case strength_break:
4723361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         assert(this->loop.loop);
4733361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         /* never lower "canonical break" */
4743361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(ir->get_next()->is_tail_sentinel() && (this->loop.nesting_depth == 0
4753361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               || (this->loop.nesting_depth == 1 && this->loop.in_if_at_the_end_of_the_loop)))
4763361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            lower = false;
4773361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         else
4783361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            lower = lower_break;
4793361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         break;
4803361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      case strength_return:
4813361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         /* never lower return at the end of a this->function */
4823361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(this->function.nesting_depth == 0 && ir->get_next()->is_tail_sentinel())
4833361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            lower = false;
4843361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         else
485dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry            lower = this->function.lower_return;
4863361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         break;
4873361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
4883361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      return lower;
4893361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
4903361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
4913361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   block_record visit_block(exec_list* list)
4923361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
493382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry      /* Note: since visiting a node may change that node's next
494382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry       * pointer, we can't use visit_exec_list(), because
495382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry       * visit_exec_list() caches the node's next pointer before
496382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry       * visiting it.  So we use foreach_list() instead.
497382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry       *
498382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry       * foreach_list() isn't safe if the node being visited gets
499382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry       * removed, but fortunately this visitor doesn't do that.
500382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry       */
501382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry
5023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      block_record saved_block = this->block;
5033361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->block = block_record();
504382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry      foreach_list(node, list) {
505382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry         ((ir_instruction *) node)->accept(this);
506382cee91a4bbbee45897239e6802ccaa5a5ad9c3Paul Berry      }
5073361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      block_record ret = this->block;
5083361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->block = saved_block;
5093361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      return ret;
5103361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
5113361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
5123361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   virtual void visit(ir_if *ir)
5133361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
5143361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(this->loop.nesting_depth == 0 && ir->get_next()->is_tail_sentinel())
5153361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->loop.in_if_at_the_end_of_the_loop = true;
5163361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
5173361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      ++this->function.nesting_depth;
5183361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      ++this->loop.nesting_depth;
5193361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
5203361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      block_record block_records[2];
5213361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      ir_jump* jumps[2];
5223361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
523e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Recursively lower nested jumps.  This satisfies the
524e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * CONTAINED_JUMPS_LOWERED postcondition, except in the case of
525e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * unconditional jumps at the end of ir->then_instructions and
526e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * ir->else_instructions, which are handled below.
527e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
5283361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      block_records[0] = visit_block(&ir->then_instructions);
5293361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      block_records[1] = visit_block(&ir->else_instructions);
5303361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
5313361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieriretry: /* we get here if we put code after the if inside a branch */
5323361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
533e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Determine which of ir->then_instructions and
534e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * ir->else_instructions end with an unconditional jump.
535e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
536e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      for(unsigned i = 0; i < 2; ++i) {
537e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         exec_list& list = i ? ir->else_instructions : ir->then_instructions;
538e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         jumps[i] = 0;
539e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         if(!list.is_empty() && get_jump_strength((ir_instruction*)list.get_tail()))
540e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            jumps[i] = (ir_jump*)list.get_tail();
541e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      }
542e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
543e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Loop until we have satisfied the CONTAINED_JUMPS_LOWERED
544e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * postcondition by lowering jumps in both then_instructions and
545e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * else_instructions.
546e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
5473361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      for(;;) {
548e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         /* Determine the types of the jumps that terminate
549e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * ir->then_instructions and ir->else_instructions.
550e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          */
5513361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         jump_strength jump_strengths[2];
5523361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
5533361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         for(unsigned i = 0; i < 2; ++i) {
5543361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            if(jumps[i]) {
5553361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               jump_strengths[i] = block_records[i].min_strength;
5563361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               assert(jump_strengths[i] == get_jump_strength(jumps[i]));
5573361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            } else
5583361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               jump_strengths[i] = strength_none;
5593361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         }
5603361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
561e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         /* If both code paths end in a jump, and the jumps are the
562e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * same, and we are pulling out jumps, replace them with a
563e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * single jump that comes after the if instruction.  The new
564e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * jump will be visited next, and it will be lowered if
565e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * necessary by the loop or conditional that encloses it.
566e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          */
5673361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(pull_out_jumps && jump_strengths[0] == jump_strengths[1]) {
5683361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            bool unify = true;
5693361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            if(jump_strengths[0] == strength_continue)
5703361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               ir->insert_after(new(ir) ir_loop_jump(ir_loop_jump::jump_continue));
5713361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            else if(jump_strengths[0] == strength_break)
5723361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               ir->insert_after(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
5733361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            /* FINISHME: unify returns with identical expressions */
5743361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            else if(jump_strengths[0] == strength_return && this->function.signature->return_type->is_void())
5753361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               ir->insert_after(new(ir) ir_return(NULL));
5761802cb9bafc4125300870be51e8b22ddd795d61eKenneth Graunke	    else
5771802cb9bafc4125300870be51e8b22ddd795d61eKenneth Graunke	       unify = false;
5783361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
5793361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            if(unify) {
5803361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               jumps[0]->remove();
5813361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               jumps[1]->remove();
5823361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               this->progress = true;
5833361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
584e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry               /* Update jumps[] to reflect the fact that the jumps
585e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * are gone, and update block_records[] to reflect the
586e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * fact that control can now flow to the next
587e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * instruction.
588e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                */
5893361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               jumps[0] = 0;
5903361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               jumps[1] = 0;
5913361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               block_records[0].min_strength = strength_none;
5923361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               block_records[1].min_strength = strength_none;
593e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
594e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry               /* The CONTAINED_JUMPS_LOWERED postcondition is now
595e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * satisfied, so we can break out of the loop.
596e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                */
5973361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               break;
5983361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            }
5993361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         }
6003361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
6013361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         /* lower a jump: if both need to lowered, start with the strongest one, so that
6023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri          * we might later unify the lowered version with the other one
6033361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri          */
6043361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         bool should_lower[2];
6053361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         for(unsigned i = 0; i < 2; ++i)
6063361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            should_lower[i] = should_lower_jump(jumps[i]);
6073361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
6083361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         int lower;
6093361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(should_lower[1] && should_lower[0])
6103361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            lower = jump_strengths[1] > jump_strengths[0];
6113361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         else if(should_lower[0])
6123361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            lower = 0;
6133361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         else if(should_lower[1])
6143361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            lower = 1;
6153361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         else
616e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* Neither code path ends in a jump that needs to be
617e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * lowered, so the CONTAINED_JUMPS_LOWERED postcondition
618e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * is satisfied and we can break out of the loop.
619e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
6203361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            break;
6213361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
6223361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(jump_strengths[lower] == strength_return) {
623e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* To lower a return, we create a return flag (if the
624e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * function doesn't have one already) and add instructions
625e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * that: 1. store the return value (if this function has a
626e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * non-void return) and 2. set the return flag
627e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
62803145ba655ad9173a74b853843eccaae78ff392fPaul Berry            insert_lowered_return((ir_return*)jumps[lower]);
6293361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            if(this->loop.loop) {
630e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry               /* If we are in a loop, replace the return instruction
631e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * with a break instruction, and then loop so that the
632e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * break instruction can be lowered if necessary.
633e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                */
6343361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               ir_loop_jump* lowered = 0;
6353361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               lowered = new(ir) ir_loop_jump(ir_loop_jump::jump_break);
636e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry               /* Note: we must update block_records and jumps to
637e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * reflect the fact that the control path has been
638e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * altered from a return to a break.
639e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                */
6403361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               block_records[lower].min_strength = strength_break;
6413361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               jumps[lower]->replace_with(lowered);
6423361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               jumps[lower] = lowered;
643e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            } else {
644e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry               /* If we are not in a loop, we then proceed as we would
645e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * for a continue statement (set the execute flag to
646e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * false to prevent the rest of the function from
647e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * executing).
648e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                */
6493361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               goto lower_continue;
650e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            }
6513361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            this->progress = true;
6523361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         } else if(jump_strengths[lower] == strength_break) {
653e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* To lower a break, we create a break flag (if the loop
654e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * doesn't have one already) and add an instruction that
655e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * sets it.
6563361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri             *
657e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * Then we proceed as we would for a continue statement
658e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * (set the execute flag to false to prevent the rest of
659e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * the loop body from executing).
6603361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri             *
661e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * The visit() function for the loop will ensure that the
662e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * break flag is checked after executing the loop body.
6633361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri             */
664067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry            jumps[lower]->insert_before(create_lowered_break());
6653361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            goto lower_continue;
6663361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         } else if(jump_strengths[lower] == strength_continue) {
6673361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbierilower_continue:
668e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* To lower a continue, we create an execute flag (if the
669e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * loop doesn't have one already) and replace the continue
670e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * with an instruction that clears it.
671e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             *
672e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * Note that this code path gets exercised when lowering
673e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * return statements that are not inside a loop, so
674e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * this->loop must be initialized even outside of loops.
675e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
6763361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            ir_variable* execute_flag = this->loop.get_execute_flag();
6773361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            jumps[lower]->replace_with(new(ir) ir_assignment(new (ir) ir_dereference_variable(execute_flag), new (ir) ir_constant(false), 0));
678e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* Note: we must update block_records and jumps to reflect
679e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * the fact that the control path has been altered to an
680e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * instruction that clears the execute flag.
681e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
6823361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            jumps[lower] = 0;
6833361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            block_records[lower].min_strength = strength_always_clears_execute_flag;
6843361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            block_records[lower].may_clear_execute_flag = true;
6853361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            this->progress = true;
686e71b4ab8a64bf978b2036976a41e30996eebb0c8Paul Berry
687e71b4ab8a64bf978b2036976a41e30996eebb0c8Paul Berry            /* Let the loop run again, in case the other branch of the
688e71b4ab8a64bf978b2036976a41e30996eebb0c8Paul Berry             * if needs to be lowered too.
689e71b4ab8a64bf978b2036976a41e30996eebb0c8Paul Berry             */
6903361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         }
6913361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
6923361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
6933361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      /* move out a jump out if possible */
6943361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(pull_out_jumps) {
695e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         /* If one of the branches ends in a jump, and control cannot
696e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * fall out the bottom of the other branch, then we can move
697e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * the jump after the if.
698e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          *
699e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * Set move_out to the branch we are moving a jump out of.
700e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          */
7013361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         int move_out = -1;
7023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(jumps[0] && block_records[1].min_strength >= strength_continue)
7033361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            move_out = 0;
7043361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         else if(jumps[1] && block_records[0].min_strength >= strength_continue)
7053361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            move_out = 1;
7063361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
7073361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(move_out >= 0)
7083361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         {
7093361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            jumps[move_out]->remove();
7103361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            ir->insert_after(jumps[move_out]);
711e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* Note: we must update block_records and jumps to reflect
712e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * the fact that the jump has been moved out of the if.
713e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
7143361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            jumps[move_out] = 0;
7153361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            block_records[move_out].min_strength = strength_none;
7163361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            this->progress = true;
7173361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         }
7183361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
7193361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
720e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Now satisfy the ANALYSIS postcondition by setting
721e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * this->block.min_strength and
722e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * this->block.may_clear_execute_flag based on the
723e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * characteristics of the two branches.
724e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
7253361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(block_records[0].min_strength < block_records[1].min_strength)
7263361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->block.min_strength = block_records[0].min_strength;
7273361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      else
7283361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         this->block.min_strength = block_records[1].min_strength;
7293361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->block.may_clear_execute_flag = this->block.may_clear_execute_flag || block_records[0].may_clear_execute_flag || block_records[1].may_clear_execute_flag;
7303361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
731e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Now we need to clean up the instructions that follow the
732e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * if.
733e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       *
734e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * If those instructions are unreachable, then satisfy the
735e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * DEAD_CODE_ELIMINATION postcondition by eliminating them.
736e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * Otherwise that postcondition is already satisfied.
737e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
7383361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(this->block.min_strength)
7393361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         truncate_after_instruction(ir);
7403361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      else if(this->block.may_clear_execute_flag)
7413361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      {
742e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         /* If the "if" instruction might clear the execute flag, then
743e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * we need to guard any instructions that follow so that they
744e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * are only executed if the execute flag is set.
745e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          *
746e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * If one of the branches of the "if" always clears the
747e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * execute flag, and the other branch never clears it, then
748e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * this is easy: just move all the instructions following the
749e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * "if" into the branch that never clears it.
750e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          */
7513361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         int move_into = -1;
7523361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(block_records[0].min_strength && !block_records[1].may_clear_execute_flag)
7533361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            move_into = 1;
7543361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         else if(block_records[1].min_strength && !block_records[0].may_clear_execute_flag)
7553361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            move_into = 0;
7563361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
7573361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         if(move_into >= 0) {
7583361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            assert(!block_records[move_into].min_strength && !block_records[move_into].may_clear_execute_flag); /* otherwise, we just truncated */
7593361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
7603361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            exec_list* list = move_into ? &ir->else_instructions : &ir->then_instructions;
7613361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            exec_node* next = ir->get_next();
7623361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            if(!next->is_tail_sentinel()) {
7633361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               move_outer_block_inside(ir, list);
7643361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
765e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry               /* If any instructions moved, then we need to visit
766e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * them (since they are now inside the "if").  Since
767e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * block_records[move_into] is in its default state
768e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * (see assertion above), we can safely replace
769e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * block_records[move_into] with the result of this
770e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * analysis.
771e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                */
7723361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               exec_list list;
7733361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               list.head = next;
7743361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               block_records[move_into] = visit_block(&list);
7753361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
776e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry               /*
777e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * Then we need to re-start our jump lowering, since one
778e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * of the instructions we moved might be a jump that
779e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                * needs to be lowered.
780e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry                */
7813361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               this->progress = true;
7823361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               goto retry;
7833361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            }
7843361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         } else {
785e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* If we get here, then the simple case didn't apply; we
786e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * need to actually guard the instructions that follow.
787e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             *
788e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * To avoid creating unnecessarily-deep nesting, first
789e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * look through the instructions that follow and unwrap
790e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * any instructions that that are already wrapped in the
791e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * appropriate guard.
792e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
7933361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            ir_instruction* ir_after;
7943361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            for(ir_after = (ir_instruction*)ir->get_next(); !ir_after->is_tail_sentinel();)
7953361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            {
7963361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               ir_if* ir_if = ir_after->as_if();
7973361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               if(ir_if && ir_if->else_instructions.is_empty()) {
7983361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri                  ir_dereference_variable* ir_if_cond_deref = ir_if->condition->as_dereference_variable();
7993361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri                  if(ir_if_cond_deref && ir_if_cond_deref->var == this->loop.execute_flag) {
8003361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri                     ir_instruction* ir_next = (ir_instruction*)ir_after->get_next();
8013361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri                     ir_after->insert_before(&ir_if->then_instructions);
8023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri                     ir_after->remove();
8033361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri                     ir_after = ir_next;
8043361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri                     continue;
8053361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri                  }
8063361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               }
8073361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               ir_after = (ir_instruction*)ir_after->get_next();
8083361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
8093361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               /* only set this if we find any unprotected instruction */
8103361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               this->progress = true;
8113361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            }
8123361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
813e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* Then, wrap all the instructions that follow in a single
814e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * guard.
815e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
8163361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            if(!ir->get_next()->is_tail_sentinel()) {
8173361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               assert(this->loop.execute_flag);
8183361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               ir_if* if_execute = new(ir) ir_if(new(ir) ir_dereference_variable(this->loop.execute_flag));
8193361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               move_outer_block_inside(ir, &if_execute->then_instructions);
8203361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri               ir->insert_after(if_execute);
8213361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri            }
8223361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         }
8233361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
8243361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      --this->loop.nesting_depth;
8253361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      --this->function.nesting_depth;
8263361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
8273361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
8283361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   virtual void visit(ir_loop *ir)
8293361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
830e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Visit the body of the loop, with a fresh data structure in
831e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * this->loop so that the analysis we do here won't bleed into
832e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * enclosing loops.
833e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       *
834e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * We assume that all code after a loop is reachable from the
835e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * loop (see comments on enum jump_strength), so the
836e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * DEAD_CODE_ELIMINATION postcondition is automatically
837e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * satisfied, as is the block.min_strength portion of the
838e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * ANALYSIS postcondition.
839e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       *
840e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * The block.may_clear_execute_flag portion of the ANALYSIS
841e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * postcondition is automatically satisfied because execute
842e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * flags do not propagate outside of loops.
843e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       *
844e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * The loop.may_set_return_flag portion of the ANALYSIS
845e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * postcondition is handled below.
846e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
8473361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      ++this->function.nesting_depth;
8483361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      loop_record saved_loop = this->loop;
8493361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->loop = loop_record(this->function.signature, ir);
8503361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
851e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Recursively lower nested jumps.  This satisfies the
852e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * CONTAINED_JUMPS_LOWERED postcondition, except in the case of
853e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * an unconditional continue or return at the bottom of the
85403145ba655ad9173a74b853843eccaae78ff392fPaul Berry       * loop, which are handled below.
855e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
8563361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      block_record body = visit_block(&ir->body_instructions);
8573361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
85803145ba655ad9173a74b853843eccaae78ff392fPaul Berry      /* If the loop ends in an unconditional continue, eliminate it
85903145ba655ad9173a74b853843eccaae78ff392fPaul Berry       * because it is redundant.
86003145ba655ad9173a74b853843eccaae78ff392fPaul Berry       */
86103145ba655ad9173a74b853843eccaae78ff392fPaul Berry      ir_instruction *ir_last
86203145ba655ad9173a74b853843eccaae78ff392fPaul Berry         = (ir_instruction *) ir->body_instructions.get_tail();
86303145ba655ad9173a74b853843eccaae78ff392fPaul Berry      if (get_jump_strength(ir_last) == strength_continue) {
86403145ba655ad9173a74b853843eccaae78ff392fPaul Berry         ir_last->remove();
86503145ba655ad9173a74b853843eccaae78ff392fPaul Berry      }
86603145ba655ad9173a74b853843eccaae78ff392fPaul Berry
86703145ba655ad9173a74b853843eccaae78ff392fPaul Berry      /* If the loop ends in an unconditional return, and we are
86803145ba655ad9173a74b853843eccaae78ff392fPaul Berry       * lowering returns, lower it.
86903145ba655ad9173a74b853843eccaae78ff392fPaul Berry       */
87003145ba655ad9173a74b853843eccaae78ff392fPaul Berry      if (this->function.lower_return)
87103145ba655ad9173a74b853843eccaae78ff392fPaul Berry         lower_return_unconditionally(ir_last);
87203145ba655ad9173a74b853843eccaae78ff392fPaul Berry
8733361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(body.min_strength >= strength_break) {
874e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         /* FINISHME: If the min_strength of the loop body is
875e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * strength_break or strength_return, that means that it
876e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * isn't a loop at all, since control flow always leaves the
877e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * body of the loop via break or return.  In principle the
878e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * loop could be eliminated in this case.  This optimization
879e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * is not implemented yet.
880e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          */
8813361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
8823361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
8833361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(this->loop.break_flag) {
884067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry         /* We only get here if we are lowering breaks */
885067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry         assert (lower_break);
886067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry
887e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         /* If a break flag was generated while visiting the body of
888e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * the loop, then at least one break was lowered, so we need
889e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * to generate an if statement at the end of the loop that
890e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * does a "break" if the break flag is set.  The break we
891e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * generate won't violate the CONTAINED_JUMPS_LOWERED
892e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * postcondition, because should_lower_jump() always returns
893e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * false for a break that happens at the end of a loop.
894067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          *
895067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          * However, if the loop already ends in a conditional or
896067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          * unconditional break, then we need to lower that break,
897067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry          * because it won't be at the end of the loop anymore.
898e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          */
899067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry         lower_final_breaks(&ir->body_instructions);
900067c9d7bd776260298ceabda026425ed7e4eb161Paul Berry
9013361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         ir_if* break_if = new(ir) ir_if(new(ir) ir_dereference_variable(this->loop.break_flag));
9023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         break_if->then_instructions.push_tail(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
9033361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         ir->body_instructions.push_tail(break_if);
9043361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
9053361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
906e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* If the body of the loop may set the return flag, then at
907e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * least one return was lowered to a break, so we need to ensure
908e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * that the return flag is checked after the body of the loop is
909e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * executed.
910e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
9113361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(this->loop.may_set_return_flag) {
9123361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         assert(this->function.return_flag);
913e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         /* Generate the if statement to check the return flag */
9143361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         ir_if* return_if = new(ir) ir_if(new(ir) ir_dereference_variable(this->function.return_flag));
915e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry         /* Note: we also need to propagate the knowledge that the
916e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * return flag may get set to the outer context.  This
917e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * satisfies the loop.may_set_return_flag part of the
918e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          * ANALYSIS postcondition.
919e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry          */
920ef534f3838f23d757a40426728789183ed36c3bbFabian Bieler         saved_loop.may_set_return_flag = true;
921ef534f3838f23d757a40426728789183ed36c3bbFabian Bieler         if(saved_loop.loop)
922e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* If this loop is nested inside another one, then the if
923e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * statement that we generated should break out of that
924e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * loop if the return flag is set.  Caller will lower that
925e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * break statement if necessary.
926e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
927ef534f3838f23d757a40426728789183ed36c3bbFabian Bieler            return_if->then_instructions.push_tail(new(ir) ir_loop_jump(ir_loop_jump::jump_break));
928ef534f3838f23d757a40426728789183ed36c3bbFabian Bieler         else
929e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry            /* Otherwise, all we need to do is ensure that the
930e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * instructions that follow are only executed if the
931e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * return flag is clear.  We can do that by moving those
932e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * instructions into the else clause of the generated if
933e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             * statement.
934e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry             */
935ef534f3838f23d757a40426728789183ed36c3bbFabian Bieler            move_outer_block_inside(ir, &return_if->else_instructions);
9363361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         ir->insert_after(return_if);
9373361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      }
9383361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
9393361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->loop = saved_loop;
9403361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      --this->function.nesting_depth;
9413361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
9423361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
9433361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   virtual void visit(ir_function_signature *ir)
9443361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
9453361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      /* these are not strictly necessary */
9463361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      assert(!this->function.signature);
9473361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      assert(!this->loop.loop);
9483361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
949dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry      bool lower_return;
950dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry      if (strcmp(ir->function_name(), "main") == 0)
951dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry         lower_return = lower_main_return;
952dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry      else
953dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry         lower_return = lower_sub_return;
954dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry
9553361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      function_record saved_function = this->function;
9563361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      loop_record saved_loop = this->loop;
957dbaa2e627effbe1361e1a69c23cf247cf86f2709Paul Berry      this->function = function_record(ir, lower_return);
9583361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->loop = loop_record(ir);
9593361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
9603361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      assert(!this->loop.loop);
961e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry
962e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry      /* Visit the body of the function to lower any jumps that occur
963e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * in it, except possibly an unconditional return statement at
964e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       * the end of it.
965e2c748aec5363981a05f21f26a0c4d37ccf6419dPaul Berry       */
9663361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      visit_block(&ir->body);
9673361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
968afc9a50fba39df520046019c6993d7b7559329eaPaul Berry      /* If the body ended in an unconditional return of non-void,
969afc9a50fba39df520046019c6993d7b7559329eaPaul Berry       * then we don't need to lower it because it's the one canonical
970afc9a50fba39df520046019c6993d7b7559329eaPaul Berry       * return.
971afc9a50fba39df520046019c6993d7b7559329eaPaul Berry       *
972afc9a50fba39df520046019c6993d7b7559329eaPaul Berry       * If the body ended in a return of void, eliminate it because
973afc9a50fba39df520046019c6993d7b7559329eaPaul Berry       * it is redundant.
974afc9a50fba39df520046019c6993d7b7559329eaPaul Berry       */
975afc9a50fba39df520046019c6993d7b7559329eaPaul Berry      if (ir->return_type->is_void() &&
976afc9a50fba39df520046019c6993d7b7559329eaPaul Berry          get_jump_strength((ir_instruction *) ir->body.get_tail())) {
977afc9a50fba39df520046019c6993d7b7559329eaPaul Berry         ir_jump *jump = (ir_jump *) ir->body.get_tail();
978afc9a50fba39df520046019c6993d7b7559329eaPaul Berry         assert (jump->ir_type == ir_type_return);
979afc9a50fba39df520046019c6993d7b7559329eaPaul Berry         jump->remove();
980afc9a50fba39df520046019c6993d7b7559329eaPaul Berry      }
981afc9a50fba39df520046019c6993d7b7559329eaPaul Berry
9823361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      if(this->function.return_value)
9833361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri         ir->body.push_tail(new(ir) ir_return(new (ir) ir_dereference_variable(this->function.return_value)));
9843361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
9853361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->loop = saved_loop;
9863361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      this->function = saved_function;
9873361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
9883361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
9893361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   virtual void visit(class ir_function * ir)
9903361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   {
9913361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      visit_block(&ir->signatures);
9923361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   }
9933361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri};
9943361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
9953361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieribool
9963361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbierido_lower_jumps(exec_list *instructions, bool pull_out_jumps, bool lower_sub_return, bool lower_main_return, bool lower_continue, bool lower_break)
9973361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri{
9983361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   ir_lower_jumps_visitor v;
9993361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   v.pull_out_jumps = pull_out_jumps;
10003361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   v.lower_continue = lower_continue;
10013361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   v.lower_break = lower_break;
10023361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   v.lower_sub_return = lower_sub_return;
10033361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   v.lower_main_return = lower_main_return;
10043361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
10053361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   do {
10063361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      v.progress = false;
10073361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri      visit_exec_list(instructions, &v);
10083361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   } while (v.progress);
10093361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri
10103361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri   return v.progress;
10113361cbac2a883818efeb2b3e27405eeefce60f63Luca Barbieri}
1012