Mercurial > octave-nkf
annotate src/interp-core/jit-ir.cc @ 15191:ed4f4fb78586
Move type inference from jit_convert to jit_infer
* jit-ir.cc (jit_block_list::print, jit_block_list::print_dom): New function.
(operator<<): New overload.
* jit-ir.h (jit_block_list::print, jit_block_list::print_dom): New declaration.
(operator<<): New overload.
(jit_block::compute_idom): Change signature.
* pt-jit.cc (jit_convert::jit_convert): Remove type inference code.
(jit_convert::find_variable): vmap_t renamed to variable_map.
(jit_convert::append_users_term, jit_convert::construct_ssa,
jit_convert::do_construct_ssa, jit_convert::remove_dead,
jit_convert::place_releases, jit_convert::release_temp,
jit_convert::release_dead_phi, jit_convert::simplify_phi): Moved to jit_infer.
(jit_convert_llvm::jit_convert_llvm): Compute arguments.
(jit_info::jit_info): Update to use jit_infer.
(jit_info::initialize): Rename to jit_info::compile.
* pt-jit.h (jit_convert::append_users_term, jit_convert::construct_ssa,
jit_convert::do_construct_ssa, jit_convert::remove_dead,
jit_convert::place_releases, jit_convert::release_temp,
jit_convert::release_dead_phi, jit_convert::simplify_phi,
jit_convert::push_worklist, jit_convert::append_users): Moved to jit_infer.
(jit_convert::print_blocks, jit_convert::print_dom): Moved to jit_block_list.
(jit_convert_llvm::jit_convert_llvm): Do not take jit_convert as parameter.
(jit_convert_llvm::jit_convert_llvm): Remove arguments parameter.
(jit_convert_llvm::get_arguments): New function.
(jit_infer): New class.
(jit_info::initialize): Rename to jit_info::compile.
author | Max Brister <max@2bass.com> |
---|---|
date | Thu, 16 Aug 2012 18:51:30 -0500 |
parents | a7a56b436de2 |
children |
rev | line source |
---|---|
15016 | 1 /* |
2 | |
3 Copyright (C) 2012 Max Brister <max@2bass.com> | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 // defines required by llvm | |
24 #define __STDC_LIMIT_MACROS | |
25 #define __STDC_CONSTANT_MACROS | |
26 | |
27 #ifdef HAVE_CONFIG_H | |
28 #include <config.h> | |
29 #endif | |
30 | |
31 #ifdef HAVE_LLVM | |
32 | |
33 #include "jit-ir.h" | |
34 | |
35 #include <llvm/BasicBlock.h> | |
36 #include <llvm/Instructions.h> | |
37 | |
38 #include "error.h" | |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
39 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
40 // -------------------- jit_factory -------------------- |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
41 jit_factory::~jit_factory (void) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
42 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
43 for (value_list::iterator iter = all_values.begin (); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
44 iter != all_values.end (); ++iter) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
45 delete *iter; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
46 } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
47 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
48 void |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
49 jit_factory::track_value (jit_value *value) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
50 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
51 if (value->type ()) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
52 mconstants.push_back (value); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
53 all_values.push_back (value); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
54 } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
55 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
56 // -------------------- jit_block_list -------------------- |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
57 void |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
58 jit_block_list::insert_after (iterator iter, jit_block *ablock) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
59 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
60 ++iter; |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
61 insert_before (iter, ablock); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
62 } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
63 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
64 void |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
65 jit_block_list::insert_after (jit_block *loc, jit_block *ablock) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
66 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
67 insert_after (loc->location (), ablock); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
68 } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
69 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
70 void |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
71 jit_block_list::insert_before (iterator iter, jit_block *ablock) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
72 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
73 iter = mlist.insert (iter, ablock); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
74 ablock->stash_location (iter); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
75 } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
76 |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
77 void |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
78 jit_block_list::insert_before (jit_block *loc, jit_block *ablock) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
79 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
80 insert_before (loc->location (), ablock); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
81 } |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
82 |
15191
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
83 std::ostream& |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
84 jit_block_list::print (std::ostream& os, const std::string& header) const |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
85 { |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
86 os << "-------------------- " << header << " --------------------\n"; |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
87 return os << *this; |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
88 } |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
89 |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
90 std::ostream& |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
91 jit_block_list::print_dom (std::ostream& os) const |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
92 { |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
93 os << "-------------------- dom info --------------------\n"; |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
94 for (const_iterator iter = begin (); iter != end (); ++iter) |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
95 { |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
96 assert (*iter); |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
97 (*iter)->print_dom (os); |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
98 } |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
99 os << std::endl; |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
100 } |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
101 |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
102 void |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
103 jit_block_list::push_back (jit_block *b) |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
104 { |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
105 mlist.push_back (b); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
106 iterator iter = mlist.end (); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
107 b->stash_location (--iter); |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
108 } |
15016 | 109 |
15191
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
110 std::ostream& |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
111 operator<<(std::ostream& os, const jit_block_list& blocks) |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
112 { |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
113 for (jit_block_list::const_iterator iter = blocks.begin (); |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
114 iter != blocks.end (); ++iter) |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
115 { |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
116 assert (*iter); |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
117 (*iter)->print (os, 0); |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
118 } |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
119 return os << std::endl; |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
120 } |
ed4f4fb78586
Move type inference from jit_convert to jit_infer
Max Brister <max@2bass.com>
parents:
15182
diff
changeset
|
121 |
15016 | 122 // -------------------- jit_use -------------------- |
123 jit_block * | |
124 jit_use::user_parent (void) const | |
125 { | |
126 return muser->parent (); | |
127 } | |
128 | |
129 // -------------------- jit_value -------------------- | |
130 jit_value::~jit_value (void) | |
131 {} | |
132 | |
133 jit_block * | |
134 jit_value::first_use_block (void) | |
135 { | |
136 jit_use *use = first_use (); | |
137 while (use) | |
138 { | |
139 if (! isa<jit_error_check> (use->user ())) | |
140 return use->user_parent (); | |
141 | |
142 use = use->next (); | |
143 } | |
144 | |
145 return 0; | |
146 } | |
147 | |
148 void | |
149 jit_value::replace_with (jit_value *value) | |
150 { | |
151 while (first_use ()) | |
152 { | |
153 jit_instruction *user = first_use ()->user (); | |
154 size_t idx = first_use ()->index (); | |
155 user->stash_argument (idx, value); | |
156 } | |
157 } | |
158 | |
159 #define JIT_METH(clname) \ | |
160 void \ | |
161 jit_ ## clname::accept (jit_ir_walker& walker) \ | |
162 { \ | |
163 walker.visit (*this); \ | |
164 } | |
165 | |
166 JIT_VISIT_IR_NOTEMPLATE | |
167 #undef JIT_METH | |
168 | |
169 std::ostream& | |
170 operator<< (std::ostream& os, const jit_value& value) | |
171 { | |
172 return value.short_print (os); | |
173 } | |
174 | |
175 std::ostream& | |
176 jit_print (std::ostream& os, jit_value *avalue) | |
177 { | |
178 if (avalue) | |
179 return avalue->print (os); | |
180 return os << "NULL"; | |
181 } | |
182 | |
183 // -------------------- jit_instruction -------------------- | |
184 void | |
185 jit_instruction::remove (void) | |
186 { | |
187 if (mparent) | |
188 mparent->remove (mlocation); | |
189 resize_arguments (0); | |
190 } | |
191 | |
192 llvm::BasicBlock * | |
193 jit_instruction::parent_llvm (void) const | |
194 { | |
195 return mparent->to_llvm (); | |
196 } | |
197 | |
198 std::ostream& | |
199 jit_instruction::short_print (std::ostream& os) const | |
200 { | |
201 if (type ()) | |
202 jit_print (os, type ()) << ": "; | |
203 return os << "#" << mid; | |
204 } | |
205 | |
206 void | |
207 jit_instruction::do_construct_ssa (size_t start, size_t end) | |
208 { | |
209 for (size_t i = start; i < end; ++i) | |
210 { | |
211 jit_value *arg = argument (i); | |
212 jit_variable *var = dynamic_cast<jit_variable *> (arg); | |
213 if (var && var->has_top ()) | |
214 stash_argument (i, var->top ()); | |
215 } | |
216 } | |
217 | |
218 // -------------------- jit_block -------------------- | |
219 void | |
220 jit_block::replace_with (jit_value *value) | |
221 { | |
222 assert (isa<jit_block> (value)); | |
223 jit_block *block = static_cast<jit_block *> (value); | |
224 | |
225 jit_value::replace_with (block); | |
226 | |
227 while (ILIST_T::first_use ()) | |
228 { | |
229 jit_phi_incomming *incomming = ILIST_T::first_use (); | |
230 incomming->stash_value (block); | |
231 } | |
232 } | |
233 | |
234 void | |
235 jit_block::replace_in_phi (jit_block *ablock, jit_block *with) | |
236 { | |
237 jit_phi_incomming *node = ILIST_T::first_use (); | |
238 while (node) | |
239 { | |
240 jit_phi_incomming *prev = node; | |
241 node = node->next (); | |
242 | |
243 if (prev->user_parent () == ablock) | |
244 prev->stash_value (with); | |
245 } | |
246 } | |
247 | |
248 jit_block * | |
249 jit_block::maybe_merge () | |
250 { | |
251 if (successor_count () == 1 && successor (0) != this | |
252 && (successor (0)->use_count () == 1 || instructions.size () == 1)) | |
253 { | |
254 jit_block *to_merge = successor (0); | |
255 merge (*to_merge); | |
256 return to_merge; | |
257 } | |
258 | |
259 return 0; | |
260 } | |
261 | |
262 void | |
263 jit_block::merge (jit_block& block) | |
264 { | |
265 // the merge block will contain a new terminator | |
266 jit_terminator *old_term = terminator (); | |
267 if (old_term) | |
268 old_term->remove (); | |
269 | |
270 bool was_empty = end () == begin (); | |
271 iterator merge_begin = end (); | |
272 if (! was_empty) | |
273 --merge_begin; | |
274 | |
275 instructions.splice (end (), block.instructions); | |
276 if (was_empty) | |
277 merge_begin = begin (); | |
278 else | |
279 ++merge_begin; | |
280 | |
281 // now merge_begin points to the start of the new instructions, we must | |
282 // update their parent information | |
283 for (iterator iter = merge_begin; iter != end (); ++iter) | |
284 { | |
285 jit_instruction *instr = *iter; | |
286 instr->stash_parent (this, iter); | |
287 } | |
288 | |
289 block.replace_with (this); | |
290 } | |
291 | |
292 jit_instruction * | |
293 jit_block::prepend (jit_instruction *instr) | |
294 { | |
295 instructions.push_front (instr); | |
296 instr->stash_parent (this, instructions.begin ()); | |
297 return instr; | |
298 } | |
299 | |
300 jit_instruction * | |
301 jit_block::prepend_after_phi (jit_instruction *instr) | |
302 { | |
303 // FIXME: Make this O(1) | |
304 for (iterator iter = begin (); iter != end (); ++iter) | |
305 { | |
306 jit_instruction *temp = *iter; | |
307 if (! isa<jit_phi> (temp)) | |
308 { | |
309 insert_before (iter, instr); | |
310 return instr; | |
311 } | |
312 } | |
313 | |
314 return append (instr); | |
315 } | |
316 | |
317 void | |
318 jit_block::internal_append (jit_instruction *instr) | |
319 { | |
320 instructions.push_back (instr); | |
321 instr->stash_parent (this, --instructions.end ()); | |
322 } | |
323 | |
324 jit_instruction * | |
325 jit_block::insert_before (iterator loc, jit_instruction *instr) | |
326 { | |
327 iterator iloc = instructions.insert (loc, instr); | |
328 instr->stash_parent (this, iloc); | |
329 return instr; | |
330 } | |
331 | |
332 jit_instruction * | |
333 jit_block::insert_after (iterator loc, jit_instruction *instr) | |
334 { | |
335 ++loc; | |
336 iterator iloc = instructions.insert (loc, instr); | |
337 instr->stash_parent (this, iloc); | |
338 return instr; | |
339 } | |
340 | |
341 jit_terminator * | |
342 jit_block::terminator (void) const | |
343 { | |
344 assert (this); | |
345 if (instructions.empty ()) | |
346 return 0; | |
347 | |
348 jit_instruction *last = instructions.back (); | |
349 return dynamic_cast<jit_terminator *> (last); | |
350 } | |
351 | |
352 bool | |
353 jit_block::branch_alive (jit_block *asucc) const | |
354 { | |
355 return terminator ()->alive (asucc); | |
356 } | |
357 | |
358 jit_block * | |
359 jit_block::successor (size_t i) const | |
360 { | |
361 jit_terminator *term = terminator (); | |
362 return term->successor (i); | |
363 } | |
364 | |
365 size_t | |
366 jit_block::successor_count (void) const | |
367 { | |
368 jit_terminator *term = terminator (); | |
369 return term ? term->successor_count () : 0; | |
370 } | |
371 | |
372 llvm::BasicBlock * | |
373 jit_block::to_llvm (void) const | |
374 { | |
375 return llvm::cast<llvm::BasicBlock> (llvm_value); | |
376 } | |
377 | |
378 std::ostream& | |
379 jit_block::print_dom (std::ostream& os) const | |
380 { | |
381 short_print (os); | |
382 os << ":\n"; | |
383 os << " mid: " << mid << std::endl; | |
384 os << " predecessors: "; | |
385 for (jit_use *use = first_use (); use; use = use->next ()) | |
386 os << *use->user_parent () << " "; | |
387 os << std::endl; | |
388 | |
389 os << " successors: "; | |
390 for (size_t i = 0; i < successor_count (); ++i) | |
391 os << *successor (i) << " "; | |
392 os << std::endl; | |
393 | |
394 os << " idom: "; | |
395 if (idom) | |
396 os << *idom; | |
397 else | |
398 os << "NULL"; | |
399 os << std::endl; | |
400 os << " df: "; | |
401 for (df_iterator iter = df_begin (); iter != df_end (); ++iter) | |
402 os << **iter << " "; | |
403 os << std::endl; | |
404 | |
405 os << " dom_succ: "; | |
406 for (size_t i = 0; i < dom_succ.size (); ++i) | |
407 os << *dom_succ[i] << " "; | |
408 | |
409 return os << std::endl; | |
410 } | |
411 | |
412 void | |
413 jit_block::compute_df (size_t avisit_count) | |
414 { | |
415 if (visited (avisit_count)) | |
416 return; | |
417 | |
418 if (use_count () >= 2) | |
419 { | |
420 for (jit_use *use = first_use (); use; use = use->next ()) | |
421 { | |
422 jit_block *runner = use->user_parent (); | |
423 while (runner != idom) | |
424 { | |
425 runner->mdf.insert (this); | |
426 runner = runner->idom; | |
427 } | |
428 } | |
429 } | |
430 | |
431 for (size_t i = 0; i < successor_count (); ++i) | |
432 successor (i)->compute_df (avisit_count); | |
433 } | |
434 | |
435 bool | |
436 jit_block::update_idom (size_t avisit_count) | |
437 { | |
438 if (visited (avisit_count) || ! use_count ()) | |
439 return false; | |
440 | |
441 bool changed = false; | |
442 for (jit_use *use = first_use (); use; use = use->next ()) | |
443 { | |
444 jit_block *pred = use->user_parent (); | |
445 changed = pred->update_idom (avisit_count) || changed; | |
446 } | |
447 | |
448 jit_use *use = first_use (); | |
449 jit_block *new_idom = use->user_parent (); | |
450 use = use->next (); | |
451 | |
452 for (; use; use = use->next ()) | |
453 { | |
454 jit_block *pred = use->user_parent (); | |
455 jit_block *pidom = pred->idom; | |
456 if (pidom) | |
457 new_idom = idom_intersect (pidom, new_idom); | |
458 } | |
459 | |
460 if (idom != new_idom) | |
461 { | |
462 idom = new_idom; | |
463 return true; | |
464 } | |
465 | |
466 return changed; | |
467 } | |
468 | |
469 void | |
470 jit_block::pop_all (void) | |
471 { | |
472 for (iterator iter = begin (); iter != end (); ++iter) | |
473 { | |
474 jit_instruction *instr = *iter; | |
475 instr->pop_variable (); | |
476 } | |
477 } | |
478 | |
479 jit_block * | |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
480 jit_block::maybe_split (jit_factory& factory, jit_block_list& blocks, |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
481 jit_block *asuccessor) |
15016 | 482 { |
483 if (successor_count () > 1) | |
484 { | |
485 jit_terminator *term = terminator (); | |
486 size_t idx = term->successor_index (asuccessor); | |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
487 jit_block *split = factory.create<jit_block> ("phi_split", mvisit_count); |
15016 | 488 |
15171
7a19e8275d41
Do not simplify the CFG during type inference
Max Brister <max@2bass.com>
parents:
15124
diff
changeset
|
489 // place after this to ensure define before use in the blocks list |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
490 blocks.insert_after (this, split); |
15016 | 491 |
492 term->stash_argument (idx, split); | |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
493 jit_branch *br = split->append (factory.create<jit_branch> (asuccessor)); |
15016 | 494 replace_in_phi (asuccessor, split); |
495 | |
496 if (alive ()) | |
497 { | |
498 split->mark_alive (); | |
499 br->infer (); | |
500 } | |
501 | |
502 return split; | |
503 } | |
504 | |
505 return this; | |
506 } | |
507 | |
508 void | |
509 jit_block::create_dom_tree (size_t avisit_count) | |
510 { | |
511 if (visited (avisit_count)) | |
512 return; | |
513 | |
514 if (idom != this) | |
515 idom->dom_succ.push_back (this); | |
516 | |
517 for (size_t i = 0; i < successor_count (); ++i) | |
518 successor (i)->create_dom_tree (avisit_count); | |
519 } | |
520 | |
521 jit_block * | |
522 jit_block::idom_intersect (jit_block *i, jit_block *j) | |
523 { | |
524 while (i && j && i != j) | |
525 { | |
526 while (i && i->id () > j->id ()) | |
527 i = i->idom; | |
528 | |
529 while (i && j && j->id () > i->id ()) | |
530 j = j->idom; | |
531 } | |
532 | |
533 return i ? i : j; | |
534 } | |
535 | |
536 // -------------------- jit_phi_incomming -------------------- | |
537 | |
538 jit_block * | |
539 jit_phi_incomming::user_parent (void) const | |
540 { return muser->parent (); } | |
541 | |
542 // -------------------- jit_phi -------------------- | |
543 bool | |
544 jit_phi::prune (void) | |
545 { | |
546 jit_block *p = parent (); | |
547 size_t new_idx = 0; | |
548 jit_value *unique = argument (1); | |
549 | |
550 for (size_t i = 0; i < argument_count (); ++i) | |
551 { | |
552 jit_block *inc = incomming (i); | |
553 if (inc->branch_alive (p)) | |
554 { | |
555 if (unique != argument (i)) | |
556 unique = 0; | |
557 | |
558 if (new_idx != i) | |
559 { | |
560 stash_argument (new_idx, argument (i)); | |
561 mincomming[new_idx].stash_value (inc); | |
562 } | |
563 | |
564 ++new_idx; | |
565 } | |
566 } | |
567 | |
568 if (new_idx != argument_count ()) | |
569 { | |
570 resize_arguments (new_idx); | |
571 mincomming.resize (new_idx); | |
572 } | |
573 | |
574 assert (argument_count () > 0); | |
575 if (unique) | |
576 { | |
577 replace_with (unique); | |
578 return true; | |
579 } | |
580 | |
581 return false; | |
582 } | |
583 | |
584 bool | |
585 jit_phi::infer (void) | |
586 { | |
587 jit_block *p = parent (); | |
588 if (! p->alive ()) | |
589 return false; | |
590 | |
591 jit_type *infered = 0; | |
592 for (size_t i = 0; i < argument_count (); ++i) | |
593 { | |
594 jit_block *inc = incomming (i); | |
595 if (inc->branch_alive (p)) | |
596 infered = jit_typeinfo::join (infered, argument_type (i)); | |
597 } | |
598 | |
599 if (infered != type ()) | |
600 { | |
601 stash_type (infered); | |
602 return true; | |
603 } | |
604 | |
605 return false; | |
606 } | |
607 | |
608 llvm::PHINode * | |
609 jit_phi::to_llvm (void) const | |
610 { | |
611 return llvm::cast<llvm::PHINode> (jit_value::to_llvm ()); | |
612 } | |
613 | |
614 // -------------------- jit_terminator -------------------- | |
615 size_t | |
616 jit_terminator::successor_index (const jit_block *asuccessor) const | |
617 { | |
618 size_t scount = successor_count (); | |
619 for (size_t i = 0; i < scount; ++i) | |
620 if (successor (i) == asuccessor) | |
621 return i; | |
622 | |
623 panic_impossible (); | |
624 } | |
625 | |
626 bool | |
627 jit_terminator::infer (void) | |
628 { | |
629 if (! parent ()->alive ()) | |
630 return false; | |
631 | |
632 bool changed = false; | |
633 for (size_t i = 0; i < malive.size (); ++i) | |
634 if (! malive[i] && check_alive (i)) | |
635 { | |
636 changed = true; | |
637 malive[i] = true; | |
638 successor (i)->mark_alive (); | |
639 } | |
640 | |
641 return changed; | |
642 } | |
643 | |
644 llvm::TerminatorInst * | |
645 jit_terminator::to_llvm (void) const | |
646 { | |
647 return llvm::cast<llvm::TerminatorInst> (jit_value::to_llvm ()); | |
648 } | |
649 | |
650 // -------------------- jit_call -------------------- | |
651 bool | |
652 jit_call::infer (void) | |
653 { | |
654 // FIXME: explain algorithm | |
655 for (size_t i = 0; i < argument_count (); ++i) | |
656 { | |
657 already_infered[i] = argument_type (i); | |
658 if (! already_infered[i]) | |
659 return false; | |
660 } | |
661 | |
662 jit_type *infered = moperation.result (already_infered); | |
663 if (! infered && use_count ()) | |
664 { | |
665 std::stringstream ss; | |
666 ss << "Missing overload in type inference for "; | |
667 print (ss, 0); | |
668 throw jit_fail_exception (ss.str ()); | |
669 } | |
670 | |
671 if (infered != type ()) | |
672 { | |
673 stash_type (infered); | |
674 return true; | |
675 } | |
676 | |
677 return false; | |
678 } | |
679 | |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
680 // -------------------- jit_magic_end -------------------- |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
681 jit_magic_end::context::context (jit_factory& factory, jit_value *avalue, |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
682 size_t aindex, size_t acount) |
15182
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
683 : value (avalue), index (factory.create<jit_const_index> (aindex)), |
a7a56b436de2
Factor out jit_block_list and jit_factory from jit_convert
Max Brister <max@2bass.com>
parents:
15171
diff
changeset
|
684 count (factory.create<jit_const_index> (acount)) |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
685 {} |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
686 |
15067 | 687 jit_magic_end::jit_magic_end (const std::vector<context>& full_context) |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
688 : contexts (full_context) |
15067 | 689 { |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
690 resize_arguments (contexts.size ()); |
15067 | 691 |
692 size_t i; | |
693 std::vector<context>::const_iterator iter; | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
694 for (iter = contexts.begin (), i = 0; iter != contexts.end (); ++iter, ++i) |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
695 stash_argument (i, iter->value); |
15067 | 696 } |
697 | |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
698 jit_magic_end::context |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
699 jit_magic_end::resolve_context (void) const |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
700 { |
15124
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
701 size_t idx; |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
702 for (idx = 0; idx < contexts.size (); ++idx) |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
703 { |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
704 jit_type *ctx_type = contexts[idx].value->type (); |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
705 if (! ctx_type || ctx_type->skip_paren ()) |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
706 break; |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
707 } |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
708 |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
709 if (idx >= contexts.size ()) |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
710 idx = 0; |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
711 |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
712 context ret = contexts[idx]; |
0464e3ceb85b
Skip functions when resolving end context in JIT
Max Brister <max@2bass.com>
parents:
15102
diff
changeset
|
713 ret.value = argument (idx); |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
714 return ret; |
15056
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
715 } |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
716 |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
717 bool |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
718 jit_magic_end::infer (void) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
719 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
720 jit_type *new_type = overload ().result (); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
721 if (new_type != type ()) |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
722 { |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
723 stash_type (new_type); |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
724 return true; |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
725 } |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
726 |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
727 return false; |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
728 } |
bc32288f4a42
Support the end keyword for one dimentional indexing in JIT.
Max Brister <max@2bass.com>
parents:
15016
diff
changeset
|
729 |
15102
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
730 std::ostream& |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
731 jit_magic_end::print (std::ostream& os, size_t indent) const |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
732 { |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
733 context ctx = resolve_context (); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
734 short_print (print_indent (os, indent)) << " (" << *ctx.value << ", "; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
735 return os << *ctx.index << ", " << *ctx.count << ")"; |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
736 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
737 |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
738 const jit_function& |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
739 jit_magic_end::overload () const |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
740 { |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
741 const context& ctx = resolve_context (); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
742 return jit_typeinfo::end (ctx.value, ctx.index, ctx.count); |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
743 } |
d29f2583cf7b
Support end in multi indexing in JIT
Max Brister <max@2bass.com>
parents:
15096
diff
changeset
|
744 |
15016 | 745 #endif |