1 /*
2 * Copyright © 2010 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file opt_redundant_jumps.cpp
26 * Remove certain types of redundant jumps
27 */
28
29 #include "ir.h"
30
31 namespace {
32
33 class redundant_jumps_visitor : public ir_hierarchical_visitor {
34 public:
redundant_jumps_visitor()35 redundant_jumps_visitor()
36 {
37 this->progress = false;
38 }
39
40 virtual ir_visitor_status visit_leave(ir_if *);
41 virtual ir_visitor_status visit_leave(ir_loop *);
42 virtual ir_visitor_status visit_enter(ir_assignment *);
43
44 bool progress;
45 };
46
47 } /* unnamed namespace */
48
49 /* We only care about the top level instructions, so don't descend
50 * into expressions.
51 */
52 ir_visitor_status
visit_enter(ir_assignment * ir)53 redundant_jumps_visitor::visit_enter(ir_assignment *ir)
54 {
55 return visit_continue_with_parent;
56 }
57
58 ir_visitor_status
visit_leave(ir_if * ir)59 redundant_jumps_visitor::visit_leave(ir_if *ir)
60 {
61 /* If the last instruction in both branches is a 'break' or a 'continue',
62 * pull it out of the branches and insert it after the if-statment. Note
63 * that both must be the same type (either 'break' or 'continue').
64 */
65 ir_instruction *const last_then =
66 (ir_instruction *) ir->then_instructions.get_tail();
67 ir_instruction *const last_else =
68 (ir_instruction *) ir->else_instructions.get_tail();
69
70 if ((last_then == NULL) || (last_else == NULL))
71 return visit_continue;
72
73 if ((last_then->ir_type != ir_type_loop_jump)
74 || (last_else->ir_type != ir_type_loop_jump))
75 return visit_continue;
76
77 ir_loop_jump *const then_jump = (ir_loop_jump *) last_then;
78 ir_loop_jump *const else_jump = (ir_loop_jump *) last_else;
79
80 if (then_jump->mode != else_jump->mode)
81 return visit_continue;
82
83 then_jump->remove();
84 else_jump->remove();
85 this->progress = true;
86
87 ir->insert_after(then_jump);
88
89 /* If both branchs of the if-statement are now empty, remove the
90 * if-statement.
91 */
92 if (ir->then_instructions.is_empty() && ir->else_instructions.is_empty())
93 ir->remove();
94
95 return visit_continue;
96 }
97
98
99 ir_visitor_status
visit_leave(ir_loop * ir)100 redundant_jumps_visitor::visit_leave(ir_loop *ir)
101 {
102 /* If the last instruction of a loop body is a 'continue', remove it.
103 */
104 ir_instruction *const last =
105 (ir_instruction *) ir->body_instructions.get_tail();
106
107 if (last && (last->ir_type == ir_type_loop_jump)
108 && (((ir_loop_jump *) last)->mode == ir_loop_jump::jump_continue)) {
109 last->remove();
110 this->progress = true;
111 }
112
113 return visit_continue;
114 }
115
116
117 bool
optimize_redundant_jumps(exec_list * instructions)118 optimize_redundant_jumps(exec_list *instructions)
119 {
120 redundant_jumps_visitor v;
121
122 v.run(instructions);
123 return v.progress;
124 }
125