blob: 6882ef72b95db1b4800f43d47d97cdbd7e4e1a51 [file] [log] [blame]
Eric Anholt5ba94202010-04-14 17:03:03 -07001/*
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 ir_function_inlining.cpp
26 *
27 * Moves constant branches of if statements out to the surrounding
28 * instruction stream.
29 */
30
Eric Anholt5ba94202010-04-14 17:03:03 -070031#include "ir.h"
Eric Anholt5ba94202010-04-14 17:03:03 -070032
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070033class ir_if_simplification_visitor : public ir_hierarchical_visitor {
Eric Anholt5ba94202010-04-14 17:03:03 -070034public:
35 ir_if_simplification_visitor()
36 {
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070037 this->made_progress = false;
Eric Anholt5ba94202010-04-14 17:03:03 -070038 }
39
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070040 ir_visitor_status visit_leave(ir_if *);
Eric Anholt5ba94202010-04-14 17:03:03 -070041
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070042 bool made_progress;
Eric Anholt5ba94202010-04-14 17:03:03 -070043};
44
45bool
46do_if_simplification(exec_list *instructions)
47{
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070048 ir_if_simplification_visitor v;
Eric Anholt5ba94202010-04-14 17:03:03 -070049
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070050 v.run(instructions);
51 return v.made_progress;
52}
Eric Anholt5ba94202010-04-14 17:03:03 -070053
Eric Anholt5ba94202010-04-14 17:03:03 -070054
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070055ir_visitor_status
56ir_if_simplification_visitor::visit_leave(ir_if *ir)
57{
58 /* FINISHME: Ideally there would be a way to note that the condition results
59 * FINISHME: in a constant before processing both of the other subtrees.
60 * FINISHME: This can probably be done with some flags, but it would take
61 * FINISHME: some work to get right.
62 */
63 ir_constant *condition_constant = ir->condition->constant_expression_value();
64 if (condition_constant) {
65 /* Move the contents of the one branch of the conditional
66 * that matters out.
67 */
68 if (condition_constant->value.b[0]) {
69 foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
70 ir_instruction *then_ir = (ir_instruction *)then_iter.get();
71 ir->insert_before(then_ir);
Eric Anholt5ba94202010-04-14 17:03:03 -070072 }
73 } else {
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070074 foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
75 ir_instruction *else_ir = (ir_instruction *)else_iter.get();
76 ir->insert_before(else_ir);
77 }
Eric Anholt5ba94202010-04-14 17:03:03 -070078 }
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070079 ir->remove();
80 this->made_progress = true;
Eric Anholt5ba94202010-04-14 17:03:03 -070081 }
82
Ian Romanicka0b4f3d2010-05-14 13:35:27 -070083 return visit_continue;
Eric Anholt5ba94202010-04-14 17:03:03 -070084}