pn2adg: handle dynamic control variables in generated AST
[ppn.git] / for_normalize.cc
blob2d1bd79c67592df1e3c2306f02db3c243b652e18
1 /* file "for_normalize.cc" */
3 /*
5 Copyright (C) 1994 Stanford University
7 All rights reserved.
9 NOTICE: This software is provided ``as is'', without any
10 warranty, including any implied warranty for merchantability or
11 fitness for a particular purpose. Under no circumstances shall
12 Stanford University or its agents be liable for any use of, misuse
13 of, or inability to use this software, including incidental and
14 consequential damages.
16 License is hereby given to use, modify, and redistribute this
17 software, in whole or in part, for any purpose, commercial or
18 non-commercial, provided that the user agrees to the terms of this
19 copyright notice, including disclaimer of warranty, and provided
20 that this copyright notice, including disclaimer of warranty, is
21 preserved in the source code and documentation of anything derived
22 from this software. Any redistributor of this software or
23 anything derived from this software assumes responsibility for
24 ensuring that any parties to whom such a redistribution is made
25 are fully aware of the terms of this license and disclaimer.
27 "SUIF" is a trademark of Stanford University.
32 /* code to normalize for loops to have zero lower bound and step size
33 * of one for the porky program for SUIF */
35 #include "for_normalize.h"
37 static instruction_list *normalized;
39 static void normalize_fors_on_node(tree_node *the_node, void *);
41 extern instruction_list *normalized_fors_on_proc(tree_proc *the_proc)
43 normalized = new instruction_list;
44 the_proc->map(&normalize_fors_on_node, NULL);
45 if (normalized->is_empty()) {
46 delete normalized;
47 normalized = NULL;
49 return normalized;
52 static void normalize_fors_on_node(tree_node *the_node, void *)
54 if (!the_node->is_for())
55 return;
57 tree_for *the_for = (tree_for *)the_node;
59 int step_int;
60 boolean step_is_int = operand_is_int_const(the_for->step_op(), &step_int);
62 if (step_is_int && (step_int == 1))
63 return;
65 guard_for(the_for);
67 if (!operand_is_int_const(the_for->lb_op()))
68 make_lb_temp(the_for);
69 if (!operand_is_int_const(the_for->step_op()))
70 make_step_temp(the_for);
72 operand old_lb_op = the_for->lb_op();
73 operand old_step_op = the_for->step_op();
75 if (!operand_is_int_const(the_for->ub_op()))
76 make_ub_temp(the_for);
78 tree_for_test new_test;
79 switch (the_for->test())
81 case FOR_SGTE:
82 case FOR_SLTE:
83 case FOR_SGT:
84 case FOR_SLT:
85 new_test = FOR_SLTE;
86 break;
87 case FOR_UGTE:
88 case FOR_ULTE:
89 case FOR_UGT:
90 case FOR_ULT:
91 new_test = FOR_ULTE;
92 break;
93 default:
94 return;
97 var_sym *old_index = the_for->index();
98 type_node *index_type = old_index->type()->unqual();
99 var_sym *new_index = the_for->scope()->new_unique_var(index_type);
101 operand new_ub_op = cast_op(iteration_count(the_for) - 1, index_type);
103 old_lb_op.remove();
104 old_step_op.remove();
106 operand old_ub_op = the_for->ub_op();
107 old_ub_op.remove();
108 if (old_ub_op.is_expr())
109 delete old_ub_op.instr();
111 the_for->set_lb_op(operand_int(index_type, 0));
112 the_for->set_ub_op(new_ub_op);
113 the_for->set_step_op(operand_int(index_type, 1));
115 the_for->set_test(new_test);
117 the_for->set_index(new_index);
119 operand old_ind_op =
120 #ifndef DEAL_WITH_SOLARIS_BRAIN_DAMAGE
121 cast_op((new_index * old_step_op) + old_lb_op, index_type);
122 #else
123 cast_op(fold_mul(new_index, old_step_op) + old_lb_op, index_type);
124 #endif
125 tree_node *assign_node = assign(old_index, old_ind_op);
126 tree_node *assign_node2 = assign_node->clone();
127 the_for->body()->push(assign_node2);
128 the_for->parent()->insert_after(assign_node, the_for->list_e());
129 normalized->push(((tree_instr *)assign_node)->instr());
130 normalized->push(((tree_instr *)assign_node2)->instr());
133 /*----------------------------------------------------------------------*
134 End Private Function Implementations
135 *----------------------------------------------------------------------*/