Mercurial > octave-nkf
annotate src/pt-jit.cc @ 14949:c57cfdb37f81
Add power to jit
author | Max Brister <max@2bass.com> |
---|---|
date | Mon, 11 Jun 2012 10:03:28 -0500 |
parents | 006570a76b90 |
children | 4c9fd3e31436 |
rev | line source |
---|---|
14899 | 1 /* |
2 | |
14901
516b4a15b775
doc: Copyright fix in pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14899
diff
changeset
|
3 Copyright (C) 2012 Max Brister <max@2bass.com> |
14899 | 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 #define __STDC_LIMIT_MACROS | |
24 #define __STDC_CONSTANT_MACROS | |
25 | |
26 #ifdef HAVE_CONFIG_H | |
27 #include <config.h> | |
28 #endif | |
29 | |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
30 #ifdef HAVE_LLVM |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
31 |
14899 | 32 #include "pt-jit.h" |
33 | |
34 #include <typeinfo> | |
35 | |
36 #include <llvm/LLVMContext.h> | |
37 #include <llvm/Module.h> | |
38 #include <llvm/Function.h> | |
39 #include <llvm/BasicBlock.h> | |
40 #include <llvm/Support/IRBuilder.h> | |
41 #include <llvm/ExecutionEngine/ExecutionEngine.h> | |
42 #include <llvm/ExecutionEngine/JIT.h> | |
43 #include <llvm/PassManager.h> | |
44 #include <llvm/Analysis/Verifier.h> | |
14903 | 45 #include <llvm/Analysis/CallGraph.h> |
14899 | 46 #include <llvm/Analysis/Passes.h> |
47 #include <llvm/Target/TargetData.h> | |
48 #include <llvm/Transforms/Scalar.h> | |
14903 | 49 #include <llvm/Transforms/IPO.h> |
14899 | 50 #include <llvm/Support/TargetSelect.h> |
51 #include <llvm/Support/raw_os_ostream.h> | |
52 | |
14903 | 53 #include "octave.h" |
14899 | 54 #include "ov-fcn-handle.h" |
55 #include "ov-usr-fcn.h" | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
56 #include "ov-scalar.h" |
14899 | 57 #include "pt-all.h" |
14949 | 58 #include "xpow.h" |
14899 | 59 |
14903 | 60 static llvm::IRBuilder<> builder (llvm::getGlobalContext ()); |
61 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
62 static llvm::LLVMContext& context = llvm::getGlobalContext (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
63 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
64 jit_typeinfo *jit_typeinfo::instance; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
65 |
14906 | 66 // thrown when we should give up on JIT and interpret |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
67 class jit_fail_exception : public std::runtime_error |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
68 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
69 public: |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
70 jit_fail_exception (void) : std::runtime_error ("unknown"), mknown (false) {} |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
71 jit_fail_exception (const std::string& reason) : std::runtime_error (reason), |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
72 mknown (true) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
73 {} |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
74 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
75 bool known (void) const { return mknown; } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
76 private: |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
77 bool mknown; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
78 }; |
14906 | 79 |
14940
5f05007ccc5f
Mark fail with GCC_ATTR_NORETURN
Max Brister <max@2bass.com>
parents:
14939
diff
changeset
|
80 static void fail (void) GCC_ATTR_NORETURN; |
5f05007ccc5f
Mark fail with GCC_ATTR_NORETURN
Max Brister <max@2bass.com>
parents:
14939
diff
changeset
|
81 static void fail (const std::string&) GCC_ATTR_NORETURN; |
5f05007ccc5f
Mark fail with GCC_ATTR_NORETURN
Max Brister <max@2bass.com>
parents:
14939
diff
changeset
|
82 |
14906 | 83 static void |
84 fail (void) | |
85 { | |
86 throw jit_fail_exception (); | |
87 } | |
88 | |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
89 #ifdef OCTAVE_JIT_DEBUG |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
90 static void |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
91 fail (const std::string& reason) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
92 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
93 throw jit_fail_exception (reason); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
94 } |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
95 #else |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
96 static void |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
97 fail (const std::string&) |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
98 { |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
99 throw jit_fail_exception (); |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
100 } |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
101 #endif // OCTAVE_JIT_DEBUG |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
102 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
103 std::ostream& jit_print (std::ostream& os, jit_type *atype) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
104 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
105 if (! atype) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
106 return os << "null"; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
107 return os << atype->name (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
108 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
109 |
14903 | 110 // function that jit code calls |
111 extern "C" void | |
112 octave_jit_print_any (const char *name, octave_base_value *obv) | |
113 { | |
114 obv->print_with_name (octave_stdout, name, true); | |
115 } | |
14899 | 116 |
117 extern "C" void | |
14903 | 118 octave_jit_print_double (const char *name, double value) |
14899 | 119 { |
120 // FIXME: We should avoid allocating a new octave_scalar each time | |
121 octave_value ov (value); | |
122 ov.print_with_name (octave_stdout, name); | |
123 } | |
124 | |
14903 | 125 extern "C" octave_base_value* |
126 octave_jit_binary_any_any (octave_value::binary_op op, octave_base_value *lhs, | |
127 octave_base_value *rhs) | |
128 { | |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
129 octave_value olhs (lhs); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
130 octave_value orhs (rhs); |
14903 | 131 octave_value result = do_binary_op (op, olhs, orhs); |
132 octave_base_value *rep = result.internal_rep (); | |
133 rep->grab (); | |
134 return rep; | |
135 } | |
136 | |
14936
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
137 extern "C" octave_idx_type |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
138 octave_jit_compute_nelem (double base, double limit, double inc) |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
139 { |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
140 Range rng = Range (base, limit, inc); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
141 return rng.nelem (); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
142 } |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
143 |
14903 | 144 extern "C" void |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
145 octave_jit_release_any (octave_base_value *obv) |
14903 | 146 { |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
147 obv->release (); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
148 } |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
149 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
150 extern "C" octave_base_value * |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
151 octave_jit_grab_any (octave_base_value *obv) |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
152 { |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
153 obv->grab (); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
154 return obv; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
155 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
156 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
157 extern "C" double |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
158 octave_jit_cast_scalar_any (octave_base_value *obv) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
159 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
160 double ret = obv->double_value (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
161 obv->release (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
162 return ret; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
163 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
164 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
165 extern "C" octave_base_value * |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
166 octave_jit_cast_any_scalar (double value) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
167 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
168 return new octave_scalar (value); |
14903 | 169 } |
170 | |
14943
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
171 extern "C" void |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
172 octave_jit_gripe_nan_to_logical_conversion (void) |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
173 { |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
174 try |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
175 { |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
176 gripe_nan_to_logical_conversion (); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
177 } |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
178 catch (const octave_execution_exception&) |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
179 { |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
180 gripe_library_execution_error (); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
181 } |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
182 } |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
183 |
14949 | 184 extern "C" octave_base_value * |
185 octave_jit_xpow (double a, double b) | |
186 { | |
187 octave_value ret = xpow (a, b); | |
188 octave_base_value *obv = ret.internal_rep (); | |
189 obv->grab (); | |
190 return obv; | |
191 } | |
192 | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
193 // -------------------- jit_range -------------------- |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
194 std::ostream& |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
195 operator<< (std::ostream& os, const jit_range& rng) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
196 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
197 return os << "Range[" << rng.base << ", " << rng.limit << ", " << rng.inc |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
198 << ", " << rng.nelem << "]"; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
199 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
200 |
14903 | 201 // -------------------- jit_type -------------------- |
202 llvm::Type * | |
203 jit_type::to_llvm_arg (void) const | |
204 { | |
205 return llvm_type ? llvm_type->getPointerTo () : 0; | |
206 } | |
207 | |
208 // -------------------- jit_function -------------------- | |
209 void | |
210 jit_function::add_overload (const overload& func, | |
211 const std::vector<jit_type*>& args) | |
212 { | |
213 if (args.size () >= overloads.size ()) | |
214 overloads.resize (args.size () + 1); | |
215 | |
216 Array<overload>& over = overloads[args.size ()]; | |
217 dim_vector dv (over.dims ()); | |
218 Array<octave_idx_type> idx = to_idx (args); | |
219 bool must_resize = false; | |
220 | |
221 if (dv.length () != idx.numel ()) | |
222 { | |
223 dv.resize (idx.numel ()); | |
224 must_resize = true; | |
225 } | |
226 | |
227 for (octave_idx_type i = 0; i < dv.length (); ++i) | |
228 if (dv(i) <= idx(i)) | |
229 { | |
230 must_resize = true; | |
231 dv(i) = idx(i) + 1; | |
232 } | |
233 | |
234 if (must_resize) | |
235 over.resize (dv); | |
236 | |
237 over(idx) = func; | |
238 } | |
239 | |
240 const jit_function::overload& | |
241 jit_function::get_overload (const std::vector<jit_type*>& types) const | |
242 { | |
243 // FIXME: We should search for the next best overload on failure | |
244 static overload null_overload; | |
245 if (types.size () >= overloads.size ()) | |
246 return null_overload; | |
247 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
248 for (size_t i =0; i < types.size (); ++i) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
249 if (! types[i]) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
250 return null_overload; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
251 |
14903 | 252 const Array<overload>& over = overloads[types.size ()]; |
253 dim_vector dv (over.dims ()); | |
254 Array<octave_idx_type> idx = to_idx (types); | |
255 for (octave_idx_type i = 0; i < dv.length (); ++i) | |
256 if (idx(i) >= dv(i)) | |
257 return null_overload; | |
258 | |
259 return over(idx); | |
260 } | |
261 | |
262 Array<octave_idx_type> | |
263 jit_function::to_idx (const std::vector<jit_type*>& types) const | |
264 { | |
265 octave_idx_type numel = types.size (); | |
266 if (numel == 1) | |
267 numel = 2; | |
268 | |
269 Array<octave_idx_type> idx (dim_vector (1, numel)); | |
270 for (octave_idx_type i = 0; i < static_cast<octave_idx_type> (types.size ()); | |
271 ++i) | |
272 idx(i) = types[i]->type_id (); | |
273 | |
274 if (types.size () == 1) | |
275 { | |
276 idx(1) = idx(0); | |
277 idx(0) = 0; | |
278 } | |
279 | |
280 return idx; | |
281 } | |
282 | |
283 // -------------------- jit_typeinfo -------------------- | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
284 void |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
285 jit_typeinfo::initialize (llvm::Module *m, llvm::ExecutionEngine *e) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
286 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
287 instance = new jit_typeinfo (m, e); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
288 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
289 |
14906 | 290 jit_typeinfo::jit_typeinfo (llvm::Module *m, llvm::ExecutionEngine *e) |
291 : module (m), engine (e), next_id (0) | |
14899 | 292 { |
14903 | 293 // FIXME: We should be registering types like in octave_value_typeinfo |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
294 ov_t = llvm::StructType::create (context, "octave_base_value"); |
14906 | 295 ov_t = ov_t->getPointerTo (); |
296 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
297 llvm::Type *dbl = llvm::Type::getDoubleTy (context); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
298 llvm::Type *bool_t = llvm::Type::getInt1Ty (context); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
299 llvm::Type *string_t = llvm::Type::getInt8Ty (context); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
300 string_t = string_t->getPointerTo (); |
14906 | 301 llvm::Type *index_t = 0; |
302 switch (sizeof(octave_idx_type)) | |
303 { | |
304 case 4: | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
305 index_t = llvm::Type::getInt32Ty (context); |
14906 | 306 break; |
307 case 8: | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
308 index_t = llvm::Type::getInt64Ty (context); |
14906 | 309 break; |
310 default: | |
311 assert (false && "Unrecognized index type size"); | |
312 } | |
313 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
314 llvm::StructType *range_t = llvm::StructType::create (context, "range"); |
14906 | 315 std::vector<llvm::Type *> range_contents (4, dbl); |
316 range_contents[3] = index_t; | |
317 range_t->setBody (range_contents); | |
318 | |
14903 | 319 // create types |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
320 any = new_type ("any", 0, ov_t); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
321 scalar = new_type ("scalar", any, dbl); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
322 range = new_type ("range", any, range_t); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
323 string = new_type ("string", any, string_t); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
324 boolean = new_type ("bool", any, bool_t); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
325 index = new_type ("index", any, index_t); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
326 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
327 casts.resize (next_id + 1); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
328 identities.resize (next_id + 1, 0); |
14903 | 329 |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
330 // bind global variables |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
331 lerror_state = new llvm::GlobalVariable (*module, bool_t, false, |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
332 llvm::GlobalValue::ExternalLinkage, |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
333 0, "error_state"); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
334 engine->addGlobalMapping (lerror_state, reinterpret_cast<void *> (&error_state)); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
335 |
14903 | 336 // any with anything is an any op |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
337 llvm::Function *fn; |
14903 | 338 llvm::Type *binary_op_type |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
339 = llvm::Type::getIntNTy (context, sizeof (octave_value::binary_op)); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
340 llvm::Function *any_binary = create_function ("octave_jit_binary_any_any", |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
341 any->to_llvm (), binary_op_type, |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
342 any->to_llvm (), any->to_llvm ()); |
14903 | 343 engine->addGlobalMapping (any_binary, |
344 reinterpret_cast<void*>(&octave_jit_binary_any_any)); | |
345 | |
346 binary_ops.resize (octave_value::num_binary_ops); | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
347 for (size_t i = 0; i < octave_value::num_binary_ops; ++i) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
348 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
349 octave_value::binary_op op = static_cast<octave_value::binary_op> (i); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
350 std::string op_name = octave_value::binary_op_as_string (op); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
351 binary_ops[i].stash_name ("binary" + op_name); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
352 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
353 |
14903 | 354 for (int op = 0; op < octave_value::num_binary_ops; ++op) |
355 { | |
356 llvm::Twine fn_name ("octave_jit_binary_any_any_"); | |
357 fn_name = fn_name + llvm::Twine (op); | |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
358 fn = create_function (fn_name, any, any, any); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
359 llvm::BasicBlock *block = llvm::BasicBlock::Create (context, "body", fn); |
14906 | 360 builder.SetInsertPoint (block); |
14903 | 361 llvm::APInt op_int(sizeof (octave_value::binary_op), op, |
362 std::numeric_limits<octave_value::binary_op>::is_signed); | |
363 llvm::Value *op_as_llvm = llvm::ConstantInt::get (binary_op_type, op_int); | |
14906 | 364 llvm::Value *ret = builder.CreateCall3 (any_binary, |
14903 | 365 op_as_llvm, |
366 fn->arg_begin (), | |
367 ++fn->arg_begin ()); | |
14906 | 368 builder.CreateRet (ret); |
14945 | 369 binary_ops[op].add_overload (fn, true, any, any, any); |
14903 | 370 } |
371 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
372 llvm::Type *void_t = llvm::Type::getVoidTy (context); |
14903 | 373 |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
374 // grab any |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
375 fn = create_function ("octave_jit_grab_any", any, any); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
376 |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
377 engine->addGlobalMapping (fn, reinterpret_cast<void*>(&octave_jit_grab_any)); |
14945 | 378 grab_fn.add_overload (fn, false, any, any); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
379 grab_fn.stash_name ("grab"); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
380 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
381 // grab scalar |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
382 fn = create_identity (scalar); |
14945 | 383 grab_fn.add_overload (fn, false, scalar, scalar); |
14903 | 384 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
385 // grab index |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
386 fn = create_identity (index); |
14945 | 387 grab_fn.add_overload (fn, false, index, index); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
388 |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
389 // release any |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
390 fn = create_function ("octave_jit_release_any", void_t, any->to_llvm ()); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
391 engine->addGlobalMapping (fn, reinterpret_cast<void*>(&octave_jit_release_any)); |
14945 | 392 release_fn.add_overload (fn, false, 0, any); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
393 release_fn.stash_name ("release"); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
394 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
395 // release scalar |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
396 fn = create_identity (scalar); |
14945 | 397 release_fn.add_overload (fn, false, 0, scalar); |
14903 | 398 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
399 // release index |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
400 fn = create_identity (index); |
14945 | 401 release_fn.add_overload (fn, false, 0, index); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
402 |
14903 | 403 // now for binary scalar operations |
404 // FIXME: Finish all operations | |
405 add_binary_op (scalar, octave_value::op_add, llvm::Instruction::FAdd); | |
406 add_binary_op (scalar, octave_value::op_sub, llvm::Instruction::FSub); | |
407 add_binary_op (scalar, octave_value::op_mul, llvm::Instruction::FMul); | |
408 add_binary_op (scalar, octave_value::op_el_mul, llvm::Instruction::FMul); | |
409 | |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
410 add_binary_fcmp (scalar, octave_value::op_lt, llvm::CmpInst::FCMP_ULT); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
411 add_binary_fcmp (scalar, octave_value::op_le, llvm::CmpInst::FCMP_ULE); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
412 add_binary_fcmp (scalar, octave_value::op_eq, llvm::CmpInst::FCMP_UEQ); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
413 add_binary_fcmp (scalar, octave_value::op_ge, llvm::CmpInst::FCMP_UGE); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
414 add_binary_fcmp (scalar, octave_value::op_gt, llvm::CmpInst::FCMP_UGT); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
415 add_binary_fcmp (scalar, octave_value::op_ne, llvm::CmpInst::FCMP_UNE); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
416 |
14949 | 417 llvm::Function *jxpow = create_function ("octave_jit_xpow", any, scalar, |
418 scalar); | |
419 engine->addGlobalMapping (jxpow, reinterpret_cast<void *> (&octave_jit_xpow)); | |
420 { | |
421 jit_function::overload ol (jxpow, false, any, scalar, scalar); | |
422 binary_ops[octave_value::op_pow].add_overload (ol); | |
423 binary_ops[octave_value::op_el_pow].add_overload (ol); | |
424 } | |
425 | |
14941 | 426 llvm::Function *gripe_div0 = create_function ("gripe_divide_by_zero", void_t); |
427 engine->addGlobalMapping (gripe_div0, | |
428 reinterpret_cast<void *> (&gripe_divide_by_zero)); | |
429 | |
430 // divide is annoying because it might error | |
431 fn = create_function ("octave_jit_div_scalar_scalar", scalar, scalar, scalar); | |
432 llvm::BasicBlock *body = llvm::BasicBlock::Create (context, "body", fn); | |
433 builder.SetInsertPoint (body); | |
434 { | |
435 llvm::BasicBlock *warn_block = llvm::BasicBlock::Create (context, "warn", fn); | |
436 llvm::BasicBlock *normal_block = llvm::BasicBlock::Create (context, "normal", fn); | |
437 | |
438 llvm::Value *zero = llvm::ConstantFP::get (dbl, 0); | |
439 llvm::Value *check = builder.CreateFCmpUEQ (zero, ++fn->arg_begin ()); | |
440 builder.CreateCondBr (check, warn_block, normal_block); | |
441 | |
442 builder.SetInsertPoint (warn_block); | |
443 builder.CreateCall (gripe_div0); | |
444 builder.CreateBr (normal_block); | |
445 | |
446 builder.SetInsertPoint (normal_block); | |
447 llvm::Value *ret = builder.CreateFDiv (fn->arg_begin (), ++fn->arg_begin ()); | |
448 builder.CreateRet (ret); | |
449 | |
14945 | 450 jit_function::overload ol (fn, true, scalar, scalar, scalar); |
14941 | 451 binary_ops[octave_value::op_div].add_overload (ol); |
452 binary_ops[octave_value::op_el_div].add_overload (ol); | |
453 } | |
454 llvm::verifyFunction (*fn); | |
455 | |
456 // ldiv is the same as div with the operators reversed | |
457 llvm::Function *div = fn; | |
458 fn = create_function ("octave_jit_ldiv_scalar_scalar", scalar, scalar, scalar); | |
459 body = llvm::BasicBlock::Create (context, "body", fn); | |
460 builder.SetInsertPoint (body); | |
461 { | |
462 llvm::Value *ret = builder.CreateCall2 (div, ++fn->arg_begin (), | |
463 fn->arg_begin ()); | |
464 builder.CreateRet (ret); | |
465 | |
14945 | 466 jit_function::overload ol (fn, true, scalar, scalar, scalar); |
14941 | 467 binary_ops[octave_value::op_ldiv].add_overload (ol); |
468 binary_ops[octave_value::op_el_ldiv].add_overload (ol); | |
469 } | |
470 llvm::verifyFunction (*fn); | |
471 | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
472 // now for binary index operators |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
473 add_binary_op (index, octave_value::op_add, llvm::Instruction::Add); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
474 |
14903 | 475 // now for printing functions |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
476 print_fn.stash_name ("print"); |
14903 | 477 add_print (any, reinterpret_cast<void*> (&octave_jit_print_any)); |
478 add_print (scalar, reinterpret_cast<void*> (&octave_jit_print_double)); | |
14906 | 479 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
480 // initialize for loop |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
481 for_init_fn.stash_name ("for_init"); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
482 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
483 fn = create_function ("octave_jit_for_range_init", index, range); |
14941 | 484 body = llvm::BasicBlock::Create (context, "body", fn); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
485 builder.SetInsertPoint (body); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
486 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
487 llvm::Value *zero = llvm::ConstantInt::get (index_t, 0); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
488 builder.CreateRet (zero); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
489 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
490 llvm::verifyFunction (*fn); |
14945 | 491 for_init_fn.add_overload (fn, false, index, range); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
492 |
14906 | 493 // bounds check for for loop |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
494 for_check_fn.stash_name ("for_check"); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
495 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
496 fn = create_function ("octave_jit_for_range_check", boolean, range, index); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
497 body = llvm::BasicBlock::Create (context, "body", fn); |
14906 | 498 builder.SetInsertPoint (body); |
499 { | |
500 llvm::Value *nelem | |
501 = builder.CreateExtractValue (fn->arg_begin (), 3); | |
502 llvm::Value *idx = ++fn->arg_begin (); | |
503 llvm::Value *ret = builder.CreateICmpULT (idx, nelem); | |
504 builder.CreateRet (ret); | |
505 } | |
506 llvm::verifyFunction (*fn); | |
14945 | 507 for_check_fn.add_overload (fn, false, boolean, range, index); |
14906 | 508 |
509 // index variabe for for loop | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
510 for_index_fn.stash_name ("for_index"); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
511 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
512 fn = create_function ("octave_jit_for_range_idx", scalar, range, index); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
513 body = llvm::BasicBlock::Create (context, "body", fn); |
14906 | 514 builder.SetInsertPoint (body); |
515 { | |
516 llvm::Value *idx = ++fn->arg_begin (); | |
517 llvm::Value *didx = builder.CreateUIToFP (idx, dbl); | |
518 llvm::Value *rng = fn->arg_begin (); | |
519 llvm::Value *base = builder.CreateExtractValue (rng, 0); | |
520 llvm::Value *inc = builder.CreateExtractValue (rng, 2); | |
521 | |
522 llvm::Value *ret = builder.CreateFMul (didx, inc); | |
523 ret = builder.CreateFAdd (base, ret); | |
524 builder.CreateRet (ret); | |
525 } | |
526 llvm::verifyFunction (*fn); | |
14945 | 527 for_index_fn.add_overload (fn, false, scalar, range, index); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
528 |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
529 // logically true |
14943
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
530 logically_true_fn.stash_name ("logically_true"); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
531 |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
532 llvm::Function *gripe_nantl = create_function ("octave_jit_gripe_nan_to_logical_conversion", void_t); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
533 engine->addGlobalMapping (gripe_nantl, reinterpret_cast<void *> (&octave_jit_gripe_nan_to_logical_conversion)); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
534 |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
535 |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
536 fn = create_function ("octave_jit_logically_true_scalar", boolean, scalar); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
537 body = llvm::BasicBlock::Create (context, "body", fn); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
538 builder.SetInsertPoint (body); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
539 { |
14943
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
540 llvm::BasicBlock *error_block = llvm::BasicBlock::Create (context, "error", fn); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
541 llvm::BasicBlock *normal_block = llvm::BasicBlock::Create (context, "normal", fn); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
542 |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
543 llvm::Value *check = builder.CreateFCmpUNE (fn->arg_begin (), fn->arg_begin ()); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
544 builder.CreateCondBr (check, error_block, normal_block); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
545 |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
546 builder.SetInsertPoint (error_block); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
547 builder.CreateCall (gripe_nantl); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
548 builder.CreateBr (normal_block); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
549 builder.SetInsertPoint (normal_block); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
550 |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
551 llvm::Value *zero = llvm::ConstantFP::get (dbl, 0); |
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
552 llvm::Value *ret = builder.CreateFCmpONE (fn->arg_begin (), zero); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
553 builder.CreateRet (ret); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
554 } |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
555 llvm::verifyFunction (*fn); |
14945 | 556 logically_true_fn.add_overload (fn, true, boolean, scalar); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
557 |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
558 fn = create_function ("octave_logically_true_bool", boolean, boolean); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
559 body = llvm::BasicBlock::Create (context, "body", fn); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
560 builder.SetInsertPoint (body); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
561 builder.CreateRet (fn->arg_begin ()); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
562 llvm::verifyFunction (*fn); |
14945 | 563 logically_true_fn.add_overload (fn, false, boolean, boolean); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
564 |
14936
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
565 // make_range |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
566 // FIXME: May be benificial to implement all in LLVM |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
567 make_range_fn.stash_name ("make_range"); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
568 llvm::Function *compute_nelem |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
569 = create_function ("octave_jit_compute_nelem", index, scalar, scalar, scalar); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
570 engine->addGlobalMapping (compute_nelem, |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
571 reinterpret_cast<void*> (&octave_jit_compute_nelem)); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
572 |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
573 fn = create_function ("octave_jit_make_range", range, scalar, scalar, scalar); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
574 body = llvm::BasicBlock::Create (context, "body", fn); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
575 builder.SetInsertPoint (body); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
576 { |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
577 llvm::Function::arg_iterator args = fn->arg_begin (); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
578 llvm::Value *base = args; |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
579 llvm::Value *limit = ++args; |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
580 llvm::Value *inc = ++args; |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
581 llvm::Value *nelem = builder.CreateCall3 (compute_nelem, base, limit, inc); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
582 |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
583 llvm::Value *dzero = llvm::ConstantFP::get (dbl, 0); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
584 llvm::Value *izero = llvm::ConstantInt::get (index_t, 0); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
585 llvm::Value *rng = llvm::ConstantStruct::get (range_t, dzero, dzero, dzero, |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
586 izero, NULL); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
587 rng = builder.CreateInsertValue (rng, base, 0); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
588 rng = builder.CreateInsertValue (rng, limit, 1); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
589 rng = builder.CreateInsertValue (rng, inc, 2); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
590 rng = builder.CreateInsertValue (rng, nelem, 3); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
591 builder.CreateRet (rng); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
592 } |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
593 llvm::verifyFunction (*fn); |
14945 | 594 make_range_fn.add_overload (fn, false, range, scalar, scalar, scalar); |
14936
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
595 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
596 casts[any->type_id ()].stash_name ("(any)"); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
597 casts[scalar->type_id ()].stash_name ("(scalar)"); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
598 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
599 // cast any <- scalar |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
600 fn = create_function ("octave_jit_cast_any_scalar", any, scalar); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
601 engine->addGlobalMapping (fn, reinterpret_cast<void*> (&octave_jit_cast_any_scalar)); |
14945 | 602 casts[any->type_id ()].add_overload (fn, false, any, scalar); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
603 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
604 // cast scalar <- any |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
605 fn = create_function ("octave_jit_cast_scalar_any", scalar, any); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
606 engine->addGlobalMapping (fn, reinterpret_cast<void*> (&octave_jit_cast_scalar_any)); |
14945 | 607 casts[scalar->type_id ()].add_overload (fn, false, scalar, any); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
608 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
609 // cast any <- any |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
610 fn = create_identity (any); |
14945 | 611 casts[any->type_id ()].add_overload (fn, false, any, any); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
612 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
613 // cast scalar <- scalar |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
614 fn = create_identity (scalar); |
14945 | 615 casts[scalar->type_id ()].add_overload (fn, false, scalar, scalar); |
14903 | 616 } |
617 | |
618 void | |
619 jit_typeinfo::add_print (jit_type *ty, void *call) | |
620 { | |
621 std::stringstream name; | |
622 name << "octave_jit_print_" << ty->name (); | |
623 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
624 llvm::Type *void_t = llvm::Type::getVoidTy (context); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
625 llvm::Function *fn = create_function (name.str (), void_t, |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
626 llvm::Type::getInt8PtrTy (context), |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
627 ty->to_llvm ()); |
14903 | 628 engine->addGlobalMapping (fn, call); |
629 | |
14945 | 630 jit_function::overload ol (fn, false, 0, string, ty); |
14903 | 631 print_fn.add_overload (ol); |
632 } | |
633 | |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
634 // FIXME: cp between add_binary_op, add_binary_icmp, and add_binary_fcmp |
14903 | 635 void |
636 jit_typeinfo::add_binary_op (jit_type *ty, int op, int llvm_op) | |
637 { | |
638 std::stringstream fname; | |
639 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); | |
640 fname << "octave_jit_" << octave_value::binary_op_as_string (ov_op) | |
641 << "_" << ty->name (); | |
14906 | 642 |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
643 llvm::Function *fn = create_function (fname.str (), ty, ty, ty); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
644 llvm::BasicBlock *block = llvm::BasicBlock::Create (context, "body", fn); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
645 builder.SetInsertPoint (block); |
14903 | 646 llvm::Instruction::BinaryOps temp |
647 = static_cast<llvm::Instruction::BinaryOps>(llvm_op); | |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
648 llvm::Value *ret = builder.CreateBinOp (temp, fn->arg_begin (), |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
649 ++fn->arg_begin ()); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
650 builder.CreateRet (ret); |
14903 | 651 llvm::verifyFunction (*fn); |
652 | |
14945 | 653 jit_function::overload ol(fn, false, ty, ty, ty); |
14903 | 654 binary_ops[op].add_overload (ol); |
655 } | |
656 | |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
657 void |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
658 jit_typeinfo::add_binary_icmp (jit_type *ty, int op, int llvm_op) |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
659 { |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
660 std::stringstream fname; |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
661 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
662 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
663 << "_" << ty->name (); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
664 |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
665 llvm::Function *fn = create_function (fname.str (), boolean, ty, ty); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
666 llvm::BasicBlock *block = llvm::BasicBlock::Create (context, "body", fn); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
667 builder.SetInsertPoint (block); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
668 llvm::CmpInst::Predicate temp |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
669 = static_cast<llvm::CmpInst::Predicate>(llvm_op); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
670 llvm::Value *ret = builder.CreateICmp (temp, fn->arg_begin (), |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
671 ++fn->arg_begin ()); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
672 builder.CreateRet (ret); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
673 llvm::verifyFunction (*fn); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
674 |
14945 | 675 jit_function::overload ol (fn, false, boolean, ty, ty); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
676 binary_ops[op].add_overload (ol); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
677 } |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
678 |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
679 void |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
680 jit_typeinfo::add_binary_fcmp (jit_type *ty, int op, int llvm_op) |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
681 { |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
682 std::stringstream fname; |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
683 octave_value::binary_op ov_op = static_cast<octave_value::binary_op>(op); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
684 fname << "octave_jit" << octave_value::binary_op_as_string (ov_op) |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
685 << "_" << ty->name (); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
686 |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
687 llvm::Function *fn = create_function (fname.str (), boolean, ty, ty); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
688 llvm::BasicBlock *block = llvm::BasicBlock::Create (context, "body", fn); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
689 builder.SetInsertPoint (block); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
690 llvm::CmpInst::Predicate temp |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
691 = static_cast<llvm::CmpInst::Predicate>(llvm_op); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
692 llvm::Value *ret = builder.CreateFCmp (temp, fn->arg_begin (), |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
693 ++fn->arg_begin ()); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
694 builder.CreateRet (ret); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
695 llvm::verifyFunction (*fn); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
696 |
14945 | 697 jit_function::overload ol (fn, false, boolean, ty, ty); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
698 binary_ops[op].add_overload (ol); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
699 } |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
700 |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
701 llvm::Function * |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
702 jit_typeinfo::create_function (const llvm::Twine& name, llvm::Type *ret, |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
703 const std::vector<llvm::Type *>& args) |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
704 { |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
705 llvm::FunctionType *ft = llvm::FunctionType::get (ret, args, false); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
706 llvm::Function *fn = llvm::Function::Create (ft, |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
707 llvm::Function::ExternalLinkage, |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
708 name, module); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
709 fn->addFnAttr (llvm::Attribute::AlwaysInline); |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
710 return fn; |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
711 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
712 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
713 llvm::Function * |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
714 jit_typeinfo::create_identity (jit_type *type) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
715 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
716 size_t id = type->type_id (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
717 if (id >= identities.size ()) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
718 identities.resize (id + 1, 0); |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
719 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
720 if (! identities[id]) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
721 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
722 llvm::Function *fn = create_function ("id", type, type); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
723 llvm::BasicBlock *body = llvm::BasicBlock::Create (context, "body", fn); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
724 builder.SetInsertPoint (body); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
725 builder.CreateRet (fn->arg_begin ()); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
726 llvm::verifyFunction (*fn); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
727 identities[id] = fn; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
728 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
729 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
730 return identities[id]; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
731 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
732 |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
733 llvm::Value * |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
734 jit_typeinfo::do_insert_error_check (void) |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
735 { |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
736 return builder.CreateLoad (lerror_state); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
737 } |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
738 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
739 jit_type * |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
740 jit_typeinfo::do_type_of (const octave_value &ov) const |
14903 | 741 { |
14924 | 742 if (ov.is_function ()) |
14903 | 743 return 0; |
744 | |
745 if (ov.is_double_type () && ov.is_real_scalar ()) | |
746 return get_scalar (); | |
747 | |
14906 | 748 if (ov.is_range ()) |
749 return get_range (); | |
750 | |
14903 | 751 return get_any (); |
752 } | |
753 | |
754 jit_type* | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
755 jit_typeinfo::new_type (const std::string& name, jit_type *parent, |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
756 llvm::Type *llvm_type) |
14903 | 757 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
758 jit_type *ret = new jit_type (name, parent, llvm_type, next_id++); |
14903 | 759 id_to_type.push_back (ret); |
760 return ret; | |
761 } | |
762 | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
763 // -------------------- jit_use -------------------- |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
764 jit_block * |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
765 jit_use::user_parent (void) const |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
766 { |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
767 return muser->parent (); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
768 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
769 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
770 // -------------------- jit_value -------------------- |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
771 jit_value::~jit_value (void) |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
772 {} |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
773 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
774 void |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
775 jit_value::replace_with (jit_value *value) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
776 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
777 while (first_use ()) |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
778 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
779 jit_instruction *user = first_use ()->user (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
780 size_t idx = first_use ()->index (); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
781 user->stash_argument (idx, value); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
782 } |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
783 } |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
784 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
785 #define JIT_METH(clname) \ |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
786 void \ |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
787 jit_ ## clname::accept (jit_ir_walker& walker) \ |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
788 { \ |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
789 walker.visit (*this); \ |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
790 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
791 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
792 JIT_VISIT_IR_NOTEMPLATE |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
793 #undef JIT_METH |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
794 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
795 std::ostream& |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
796 operator<< (std::ostream& os, const jit_value& value) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
797 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
798 return value.short_print (os); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
799 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
800 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
801 // -------------------- jit_instruction -------------------- |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
802 void |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
803 jit_instruction::remove (void) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
804 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
805 if (mparent) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
806 mparent->remove (mlocation); |
14945 | 807 resize_arguments (0); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
808 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
809 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
810 llvm::BasicBlock * |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
811 jit_instruction::parent_llvm (void) const |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
812 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
813 return mparent->to_llvm (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
814 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
815 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
816 std::ostream& |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
817 jit_instruction::short_print (std::ostream& os) const |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
818 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
819 if (type ()) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
820 jit_print (os, type ()) << ": "; |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
821 return os << "#" << mid; |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
822 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
823 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
824 // -------------------- jit_block -------------------- |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
825 void |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
826 jit_block::replace_with (jit_value *value) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
827 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
828 assert (isa<jit_block> (value)); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
829 jit_block *block = static_cast<jit_block *> (value); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
830 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
831 jit_value::replace_with (block); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
832 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
833 while (ILIST_T::first_use ()) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
834 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
835 jit_phi_incomming *incomming = ILIST_T::first_use (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
836 incomming->stash_value (block); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
837 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
838 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
839 |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
840 jit_block * |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
841 jit_block::maybe_merge () |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
842 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
843 if (successor_count () == 1 && successor (0) != this |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
844 && (successor (0)->use_count () == 1 || instructions.size () == 1)) |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
845 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
846 jit_block *to_merge = successor (0); |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
847 merge (*to_merge); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
848 return to_merge; |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
849 } |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
850 |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
851 return 0; |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
852 } |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
853 |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
854 void |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
855 jit_block::merge (jit_block& block) |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
856 { |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
857 // the merge block will contain a new terminator |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
858 jit_terminator *old_term = terminator (); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
859 if (old_term) |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
860 old_term->remove (); |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
861 |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
862 bool was_empty = end () == begin (); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
863 iterator merge_begin = end (); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
864 if (! was_empty) |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
865 --merge_begin; |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
866 |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
867 instructions.splice (end (), block.instructions); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
868 if (was_empty) |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
869 merge_begin = begin (); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
870 else |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
871 ++merge_begin; |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
872 |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
873 // now merge_begin points to the start of the new instructions, we must |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
874 // update their parent information |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
875 for (iterator iter = merge_begin; iter != end (); ++iter) |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
876 { |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
877 jit_instruction *instr = *iter; |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
878 instr->stash_parent (this, iter); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
879 } |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
880 |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
881 block.replace_with (this); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
882 } |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
883 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
884 jit_instruction * |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
885 jit_block::prepend (jit_instruction *instr) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
886 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
887 instructions.push_front (instr); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
888 instr->stash_parent (this, instructions.begin ()); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
889 return instr; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
890 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
891 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
892 jit_instruction * |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
893 jit_block::prepend_after_phi (jit_instruction *instr) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
894 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
895 // FIXME: Make this O(1) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
896 for (iterator iter = begin (); iter != end (); ++iter) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
897 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
898 jit_instruction *temp = *iter; |
14937
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
899 if (! isa<jit_phi> (temp)) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
900 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
901 insert_before (iter, instr); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
902 return instr; |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
903 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
904 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
905 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
906 return append (instr); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
907 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
908 |
14945 | 909 void |
910 jit_block::internal_append (jit_instruction *instr) | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
911 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
912 instructions.push_back (instr); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
913 instr->stash_parent (this, --instructions.end ()); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
914 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
915 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
916 jit_instruction * |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
917 jit_block::insert_before (iterator loc, jit_instruction *instr) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
918 { |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
919 iterator iloc = instructions.insert (loc, instr); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
920 instr->stash_parent (this, iloc); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
921 return instr; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
922 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
923 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
924 jit_instruction * |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
925 jit_block::insert_after (iterator loc, jit_instruction *instr) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
926 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
927 ++loc; |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
928 iterator iloc = instructions.insert (loc, instr); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
929 instr->stash_parent (this, iloc); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
930 return instr; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
931 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
932 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
933 jit_terminator * |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
934 jit_block::terminator (void) const |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
935 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
936 assert (this); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
937 if (instructions.empty ()) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
938 return 0; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
939 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
940 jit_instruction *last = instructions.back (); |
14937
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
941 return dynamic_cast<jit_terminator *> (last); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
942 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
943 |
14945 | 944 bool |
945 jit_block::branch_alive (jit_block *asucc) const | |
946 { | |
947 return terminator ()->alive (asucc); | |
948 } | |
949 | |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
950 jit_block * |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
951 jit_block::successor (size_t i) const |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
952 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
953 jit_terminator *term = terminator (); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
954 return term->successor (i); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
955 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
956 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
957 size_t |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
958 jit_block::successor_count (void) const |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
959 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
960 jit_terminator *term = terminator (); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
961 return term ? term->successor_count () : 0; |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
962 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
963 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
964 llvm::BasicBlock * |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
965 jit_block::branch_llvm (size_t idx) const |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
966 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
967 return terminator ()->branch_llvm (idx); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
968 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
969 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
970 llvm::BasicBlock * |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
971 jit_block::branch_llvm (jit_block *succ) const |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
972 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
973 return terminator ()->branch_llvm (succ); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
974 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
975 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
976 llvm::BasicBlock * |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
977 jit_block::to_llvm (void) const |
14903 | 978 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
979 return llvm::cast<llvm::BasicBlock> (llvm_value); |
14903 | 980 } |
981 | |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
982 std::ostream& |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
983 jit_block::print_dom (std::ostream& os) const |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
984 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
985 short_print (os); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
986 os << ":\n"; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
987 os << " mid: " << mid << std::endl; |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
988 os << " predecessors: "; |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
989 for (jit_use *use = first_use (); use; use = use->next ()) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
990 os << *use->user_parent () << " "; |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
991 os << std::endl; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
992 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
993 os << " successors: "; |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
994 for (size_t i = 0; i < successor_count (); ++i) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
995 os << *successor (i) << " "; |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
996 os << std::endl; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
997 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
998 os << " idom: "; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
999 if (idom) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1000 os << *idom; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1001 else |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1002 os << "NULL"; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1003 os << std::endl; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1004 os << " df: "; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1005 for (df_iterator iter = df_begin (); iter != df_end (); ++iter) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1006 os << **iter << " "; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1007 os << std::endl; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1008 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1009 os << " dom_succ: "; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1010 for (size_t i = 0; i < dom_succ.size (); ++i) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1011 os << *dom_succ[i] << " "; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1012 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1013 return os << std::endl; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1014 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1015 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1016 void |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1017 jit_block::compute_df (size_t visit_count) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1018 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1019 if (mvisit_count > visit_count) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1020 return; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1021 ++mvisit_count; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1022 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1023 if (use_count () >= 2) |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1024 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1025 for (jit_use *use = first_use (); use; use = use->next ()) |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1026 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1027 jit_block *runner = use->user_parent (); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1028 while (runner != idom) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1029 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1030 runner->mdf.insert (this); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1031 runner = runner->idom; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1032 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1033 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1034 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1035 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1036 for (size_t i = 0; i < successor_count (); ++i) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1037 successor (i)->compute_df (visit_count); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1038 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1039 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1040 bool |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1041 jit_block::update_idom (size_t visit_count) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1042 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1043 if (mvisit_count > visit_count) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1044 return false; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1045 ++mvisit_count; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1046 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1047 if (! use_count ()) |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1048 return false; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1049 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1050 bool changed = false; |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1051 for (jit_use *use = first_use (); use; use = use->next ()) |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1052 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1053 jit_block *pred = use->user_parent (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1054 changed = pred->update_idom (visit_count) || changed; |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1055 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1056 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1057 jit_use *use = first_use (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1058 jit_block *new_idom = use->user_parent (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1059 use = use->next (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1060 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1061 for (; use; use = use->next ()) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1062 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1063 jit_block *pred = use->user_parent (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1064 jit_block *pidom = pred->idom; |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1065 if (pidom) |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1066 new_idom = pidom->idom_intersect (new_idom); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1067 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1068 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1069 if (idom != new_idom) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1070 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1071 idom = new_idom; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1072 return true; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1073 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1074 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1075 return changed; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1076 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1077 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1078 void |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1079 jit_block::pop_all (void) |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1080 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1081 for (iterator iter = begin (); iter != end (); ++iter) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1082 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1083 jit_instruction *instr = *iter; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1084 instr->pop_variable (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1085 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1086 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1087 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1088 void |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1089 jit_block::create_dom_tree (size_t visit_count) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1090 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1091 if (mvisit_count > visit_count) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1092 return; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1093 ++mvisit_count; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1094 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1095 if (idom != this) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1096 idom->dom_succ.push_back (this); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1097 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1098 for (size_t i = 0; i < successor_count (); ++i) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1099 successor (i)->create_dom_tree (visit_count); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1100 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1101 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1102 jit_block * |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1103 jit_block::idom_intersect (jit_block *b) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1104 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1105 jit_block *i = this; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1106 jit_block *j = b; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1107 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1108 while (i != j) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1109 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1110 while (i->id () > j->id ()) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1111 i = i->idom; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1112 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1113 while (j->id () > i->id ()) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1114 j = j->idom; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1115 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1116 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1117 return i; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1118 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1119 |
14945 | 1120 // -------------------- jit_phi -------------------- |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1121 bool |
14945 | 1122 jit_phi::prune (void) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1123 { |
14945 | 1124 jit_block *p = parent (); |
1125 size_t new_idx = 0; | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1126 jit_value *unique = argument (1); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1127 |
14945 | 1128 for (size_t i = 0; i < argument_count (); ++i) |
1129 { | |
1130 jit_block *inc = incomming (i); | |
1131 if (inc->branch_alive (p)) | |
1132 { | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1133 if (unique != argument (i)) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1134 unique = 0; |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1135 |
14945 | 1136 if (new_idx != i) |
1137 { | |
1138 stash_argument (new_idx, argument (i)); | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1139 mincomming[new_idx].stash_value (inc); |
14945 | 1140 } |
1141 | |
1142 ++new_idx; | |
1143 } | |
1144 } | |
1145 | |
1146 if (new_idx != argument_count ()) | |
1147 { | |
1148 resize_arguments (new_idx); | |
1149 mincomming.resize (new_idx); | |
1150 } | |
1151 | |
1152 assert (argument_count () > 0); | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1153 if (unique) |
14945 | 1154 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1155 replace_with (unique); |
14945 | 1156 return true; |
1157 } | |
1158 | |
1159 return false; | |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1160 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1161 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1162 bool |
14945 | 1163 jit_phi::infer (void) |
1164 { | |
1165 jit_block *p = parent (); | |
1166 if (! p->alive ()) | |
1167 return false; | |
1168 | |
1169 jit_type *infered = 0; | |
1170 for (size_t i = 0; i < argument_count (); ++i) | |
1171 { | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1172 jit_block *inc = incomming (i); |
14945 | 1173 if (inc->branch_alive (p)) |
1174 infered = jit_typeinfo::join (infered, argument_type (i)); | |
1175 } | |
1176 | |
1177 if (infered != type ()) | |
1178 { | |
1179 stash_type (infered); | |
1180 return true; | |
1181 } | |
1182 | |
1183 return false; | |
1184 } | |
1185 | |
1186 // -------------------- jit_terminator -------------------- | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1187 size_t |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1188 jit_terminator::successor_index (const jit_block *asuccessor) const |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1189 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1190 size_t scount = successor_count (); |
14945 | 1191 for (size_t i = 0; i < scount; ++i) |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1192 if (successor (i) == asuccessor) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1193 return i; |
14945 | 1194 |
1195 panic_impossible (); | |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1196 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1197 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1198 void |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1199 jit_terminator::create_merge (llvm::Function *function, jit_block *asuccessor) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1200 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1201 size_t idx = successor_index (asuccessor); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1202 if (! mbranch_llvm[idx] && successor_count () > 1) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1203 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1204 assert (parent ()); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1205 assert (parent_llvm ()); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1206 llvm::BasicBlock *merge = llvm::BasicBlock::Create (context, "phi_merge", |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1207 function, |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1208 parent_llvm ()); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1209 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1210 // fix the predecessor jump if it has been created |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1211 if (has_llvm ()) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1212 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1213 llvm::TerminatorInst *branch = to_llvm (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1214 branch->setSuccessor (idx, merge); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1215 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1216 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1217 llvm::IRBuilder<> temp (merge); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1218 temp.CreateBr (successor_llvm (idx)); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1219 mbranch_llvm[idx] = merge; |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1220 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1221 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1222 |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1223 bool |
14945 | 1224 jit_terminator::infer (void) |
1225 { | |
1226 if (! parent ()->alive ()) | |
1227 return false; | |
1228 | |
1229 bool changed = false; | |
1230 for (size_t i = 0; i < malive.size (); ++i) | |
1231 if (! malive[i] && check_alive (i)) | |
1232 { | |
1233 changed = true; | |
1234 malive[i] = true; | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1235 successor (i)->mark_alive (); |
14945 | 1236 } |
1237 | |
1238 return changed; | |
1239 } | |
1240 | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1241 llvm::TerminatorInst * |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1242 jit_terminator::to_llvm (void) const |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1243 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1244 return llvm::cast<llvm::TerminatorInst> (jit_value::to_llvm ()); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1245 } |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1246 |
14945 | 1247 // -------------------- jit_call -------------------- |
1248 bool | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1249 jit_call::infer (void) |
14903 | 1250 { |
14922
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1251 // FIXME: explain algorithm |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1252 for (size_t i = 0; i < argument_count (); ++i) |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
1253 { |
14922
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1254 already_infered[i] = argument_type (i); |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1255 if (! already_infered[i]) |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1256 return false; |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
1257 } |
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
1258 |
14922
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1259 jit_type *infered = mfunction.get_result (already_infered); |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1260 if (! infered && use_count ()) |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
1261 { |
14922
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1262 std::stringstream ss; |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1263 ss << "Missing overload in type inference for "; |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1264 print (ss, 0); |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1265 fail (ss.str ()); |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1266 } |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1267 |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1268 if (infered != type ()) |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1269 { |
2e6f83b2f2b9
Cleanup of some type inference functions
Max Brister <max@2bass.com>
parents:
14920
diff
changeset
|
1270 stash_type (infered); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1271 return true; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1272 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1273 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1274 return false; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1275 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1276 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1277 // -------------------- jit_convert -------------------- |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1278 jit_convert::jit_convert (llvm::Module *module, tree &tee) |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1279 : iterator_count (0), breaking (false) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1280 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1281 jit_instruction::reset_ids (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1282 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1283 entry_block = create<jit_block> ("body"); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1284 final_block = create<jit_block> ("final"); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1285 add_block (entry_block); |
14945 | 1286 entry_block->mark_alive (); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1287 block = entry_block; |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1288 visit (tee); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1289 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1290 // FIXME: Remove if we no longer only compile loops |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1291 assert (! breaking); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1292 assert (breaks.empty ()); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1293 assert (continues.empty ()); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1294 |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1295 block->append (create<jit_break> (final_block)); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1296 add_block (final_block); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1297 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1298 for (vmap_t::iterator iter = vmap.begin (); iter != vmap.end (); ++iter) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1299 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1300 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1301 jit_variable *var = iter->second; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1302 const std::string& name = var->name (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1303 if (name.size () && name[0] != '#') |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1304 final_block->append (create<jit_store_argument> (var)); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1305 } |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1306 |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1307 construct_ssa (); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1308 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1309 // initialize the worklist to instructions derived from constants |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1310 for (std::list<jit_value *>::iterator iter = constants.begin (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1311 iter != constants.end (); ++iter) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1312 append_users (*iter); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1313 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1314 // FIXME: Describe algorithm here |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1315 while (worklist.size ()) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1316 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1317 jit_instruction *next = worklist.front (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1318 worklist.pop_front (); |
14946
3564bb141396
Only add items to worklist in type inferece if not already there
Max Brister <max@2bass.com>
parents:
14945
diff
changeset
|
1319 next->stash_in_worklist (false); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1320 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1321 if (next->infer ()) |
14945 | 1322 { |
1323 // terminators need to be handles specially | |
1324 if (jit_terminator *term = dynamic_cast<jit_terminator *> (next)) | |
1325 append_users_term (term); | |
1326 else | |
1327 append_users (next); | |
1328 } | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1329 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1330 |
14945 | 1331 remove_dead (); |
14946
3564bb141396
Only add items to worklist in type inferece if not already there
Max Brister <max@2bass.com>
parents:
14945
diff
changeset
|
1332 merge_blocks (); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1333 place_releases (); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1334 |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1335 #ifdef OCTAVE_JIT_DEBUG |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1336 std::cout << "-------------------- Compiling tree --------------------\n"; |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1337 std::cout << tee.str_print_code () << std::endl; |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1338 print_blocks ("octave jit ir"); |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1339 #endif |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1340 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1341 // for now just init arguments from entry, later we will have to do something |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1342 // more interesting |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1343 for (jit_block::iterator iter = entry_block->begin (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1344 iter != entry_block->end (); ++iter) |
14937
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
1345 if (jit_extract_argument *extract = dynamic_cast<jit_extract_argument *> (*iter)) |
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
1346 arguments.push_back (std::make_pair (extract->name (), true)); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1347 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1348 convert_llvm to_llvm; |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1349 function = to_llvm.convert (module, arguments, blocks, constants); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1350 |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1351 #ifdef OCTAVE_JIT_DEBUG |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1352 std::cout << "-------------------- llvm ir --------------------"; |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1353 llvm::raw_os_ostream llvm_cout (std::cout); |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1354 function->print (llvm_cout); |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1355 std::cout << std::endl; |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1356 llvm::verifyFunction (*function); |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
1357 #endif |
14906 | 1358 } |
1359 | |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1360 jit_convert::~jit_convert (void) |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1361 { |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1362 for (std::list<jit_value *>::iterator iter = all_values.begin (); |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1363 iter != all_values.end (); ++iter) |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1364 delete *iter; |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1365 } |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1366 |
14906 | 1367 void |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1368 jit_convert::visit_anon_fcn_handle (tree_anon_fcn_handle&) |
14903 | 1369 { |
1370 fail (); | |
1371 } | |
1372 | |
1373 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1374 jit_convert::visit_argument_list (tree_argument_list&) |
14903 | 1375 { |
1376 fail (); | |
1377 } | |
1378 | |
1379 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1380 jit_convert::visit_binary_expression (tree_binary_expression& be) |
14903 | 1381 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1382 if (be.op_type () >= octave_value::num_binary_ops) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1383 // this is the case for bool_or and bool_and |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1384 fail (); |
14903 | 1385 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1386 tree_expression *lhs = be.lhs (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1387 jit_value *lhsv = visit (lhs); |
14903 | 1388 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1389 tree_expression *rhs = be.rhs (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1390 jit_value *rhsv = visit (rhs); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1391 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1392 const jit_function& fn = jit_typeinfo::binary_op (be.op_type ()); |
14945 | 1393 jit_call *call = block->append (create<jit_call> (fn, lhsv, rhsv)); |
1394 | |
1395 jit_block *normal = create<jit_block> (block->name ()); | |
1396 block->append (create<jit_check_error> (call, normal, final_block)); | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1397 add_block (normal); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1398 block = normal; |
14945 | 1399 result = call; |
14903 | 1400 } |
1401 | |
1402 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1403 jit_convert::visit_break_command (tree_break_command&) |
14903 | 1404 { |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1405 breaks.push_back (block); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1406 breaking = true; |
14903 | 1407 } |
1408 | |
1409 void | |
14936
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1410 jit_convert::visit_colon_expression (tree_colon_expression& expr) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1411 { |
14936
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1412 // in the futher we need to add support for classes and deal with rvalues |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1413 jit_value *base = visit (expr.base ()); |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1414 jit_value *limit = visit (expr.limit ()); |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1415 jit_value *increment; |
14936
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1416 tree_expression *tinc = expr.increment (); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1417 |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1418 if (tinc) |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1419 increment = visit (tinc); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1420 else |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1421 increment = create<jit_const_scalar> (1); |
14936
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1422 |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1423 result = block->append (create<jit_call> (jit_typeinfo::make_range, base, |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1424 limit, increment)); |
32deb562ae77
Allow for construction of ranges during jit
Max Brister <max@2bass.com>
parents:
14935
diff
changeset
|
1425 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1426 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1427 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1428 void |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1429 jit_convert::visit_continue_command (tree_continue_command&) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1430 { |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1431 continues.push_back (block); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1432 breaking = true; |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1433 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1434 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1435 void |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1436 jit_convert::visit_global_command (tree_global_command&) |
14903 | 1437 { |
1438 fail (); | |
1439 } | |
1440 | |
1441 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1442 jit_convert::visit_persistent_command (tree_persistent_command&) |
14903 | 1443 { |
1444 fail (); | |
1445 } | |
1446 | |
1447 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1448 jit_convert::visit_decl_elt (tree_decl_elt&) |
14903 | 1449 { |
1450 fail (); | |
1451 } | |
1452 | |
1453 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1454 jit_convert::visit_decl_init_list (tree_decl_init_list&) |
14903 | 1455 { |
1456 fail (); | |
1457 } | |
1458 | |
1459 void | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1460 jit_convert::visit_simple_for_command (tree_simple_for_command& cmd) |
14903 | 1461 { |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1462 // Note we do an initial check to see if the loop will run atleast once. |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1463 // This allows us to get better type inference bounds on variables defined |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1464 // and used only inside the for loop (e.g. the index variable) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1465 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1466 // If we are a nested for loop we need to store the previous breaks |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1467 assert (! breaking); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1468 unwind_protect prot; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1469 prot.protect_var (breaks); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1470 prot.protect_var (continues); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1471 prot.protect_var (breaking); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1472 breaks.clear (); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1473 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1474 // FIXME: one of these days we will introduce proper lvalues... |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1475 tree_identifier *lhs = dynamic_cast<tree_identifier *>(cmd.left_hand_side ()); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1476 if (! lhs) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1477 fail (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1478 std::string lhs_name = lhs->name (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1479 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1480 // we need a variable for our iterator, because it is used in multiple blocks |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1481 std::stringstream ss; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1482 ss << "#iter" << iterator_count++; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1483 std::string iter_name = ss.str (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1484 jit_variable *iterator = create<jit_variable> (iter_name); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1485 vmap[iter_name] = iterator; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1486 |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1487 jit_block *body = create<jit_block> ("for_body"); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1488 add_block (body); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1489 |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1490 jit_block *tail = create<jit_block> ("for_tail"); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1491 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1492 // do control expression, iter init, and condition check in prev_block (block) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1493 jit_value *control = visit (cmd.control_expr ()); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1494 jit_call *init_iter = create<jit_call> (jit_typeinfo::for_init, control); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1495 block->append (init_iter); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1496 block->append (create<jit_assign> (iterator, init_iter)); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1497 |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1498 jit_value *check = block->append (create<jit_call> (jit_typeinfo::for_check, |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1499 control, iterator)); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1500 block->append (create<jit_cond_break> (check, body, tail)); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1501 block = body; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1502 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1503 // compute the syntactical iterator |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1504 jit_call *idx_rhs = create<jit_call> (jit_typeinfo::for_index, control, iterator); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1505 block->append (idx_rhs); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1506 do_assign (lhs_name, idx_rhs, false); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1507 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1508 // do loop |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1509 tree_statement_list *pt_body = cmd.body (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1510 pt_body->accept (*this); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1511 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1512 if (breaking && continues.empty ()) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1513 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1514 // WTF are you doing user? Every branch was a continue, why did you have |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1515 // a loop??? Users are silly people... |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1516 finish_breaks (tail, breaks); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1517 add_block (tail); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1518 block = tail; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1519 return; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1520 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1521 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1522 // check our condition, continues jump to this block |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1523 jit_block *check_block = create<jit_block> ("for_check"); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1524 add_block (check_block); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1525 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1526 if (! breaking) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1527 block->append (create<jit_break> (check_block)); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1528 finish_breaks (check_block, continues); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1529 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1530 block = check_block; |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1531 const jit_function& add_fn = jit_typeinfo::binary_op (octave_value::op_add); |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1532 jit_value *one = create<jit_const_index> (1); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1533 jit_call *iter_inc = create<jit_call> (add_fn, iterator, one); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1534 block->append (iter_inc); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1535 block->append (create<jit_assign> (iterator, iter_inc)); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1536 check = block->append (create<jit_call> (jit_typeinfo::for_check, control, |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1537 iterator)); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1538 block->append (create<jit_cond_break> (check, body, tail)); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1539 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1540 // breaks will go to our tail |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1541 add_block (tail); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1542 finish_breaks (tail, breaks); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1543 block = tail; |
14903 | 1544 } |
1545 | |
1546 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1547 jit_convert::visit_complex_for_command (tree_complex_for_command&) |
14903 | 1548 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1549 fail (); |
14903 | 1550 } |
1551 | |
1552 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1553 jit_convert::visit_octave_user_script (octave_user_script&) |
14903 | 1554 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1555 fail (); |
14903 | 1556 } |
1557 | |
14915
cba58541954c
Add if support and fix leak with any
Max Brister <max@2bass.com>
parents:
14913
diff
changeset
|
1558 void |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1559 jit_convert::visit_octave_user_function (octave_user_function&) |
14903 | 1560 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1561 fail (); |
14899 | 1562 } |
1563 | |
14903 | 1564 void |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1565 jit_convert::visit_octave_user_function_header (octave_user_function&) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1566 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1567 fail (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1568 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1569 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1570 void |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1571 jit_convert::visit_octave_user_function_trailer (octave_user_function&) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1572 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1573 fail (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1574 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1575 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1576 void |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1577 jit_convert::visit_function_def (tree_function_def&) |
14899 | 1578 { |
14903 | 1579 fail (); |
1580 } | |
14899 | 1581 |
14903 | 1582 void |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1583 jit_convert::visit_identifier (tree_identifier& ti) |
14903 | 1584 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1585 const jit_function& fn = jit_typeinfo::grab (); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1586 jit_value *decl = get_variable (ti.name ()); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1587 result = block->append (create<jit_call> (fn, decl)); |
14899 | 1588 } |
1589 | |
1590 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1591 jit_convert::visit_if_clause (tree_if_clause&) |
14903 | 1592 { |
1593 fail (); | |
14899 | 1594 } |
1595 | |
1596 void | |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1597 jit_convert::visit_if_command (tree_if_command& cmd) |
14903 | 1598 { |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1599 tree_if_command_list *lst = cmd.cmd_list (); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1600 assert (lst); // jwe: Can this be null? |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1601 lst->accept (*this); |
14903 | 1602 } |
1603 | |
1604 void | |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1605 jit_convert::visit_if_command_list (tree_if_command_list& lst) |
14903 | 1606 { |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1607 tree_if_clause *last = lst.back (); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1608 size_t last_else = static_cast<size_t> (last->is_else_clause ()); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1609 |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1610 // entry_blocks represents the block you need to enter in order to execute |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1611 // the condition check for the ith clause. For the else, it is simple the |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1612 // else body. If there is no else body, then it is padded with the tail |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1613 std::vector<jit_block *> entry_blocks (lst.size () + 1 - last_else); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1614 std::vector<jit_block *> branch_blocks (lst.size (), 0); // final blocks |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1615 entry_blocks[0] = block; |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1616 |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1617 // we need to construct blocks first, because they have jumps to eachother |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1618 tree_if_command_list::iterator iter = lst.begin (); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1619 ++iter; |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1620 for (size_t i = 1; iter != lst.end (); ++iter, ++i) |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1621 { |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1622 tree_if_clause *tic = *iter; |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1623 if (tic->is_else_clause ()) |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1624 entry_blocks[i] = create<jit_block> ("else"); |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1625 else |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1626 entry_blocks[i] = create<jit_block> ("ifelse_cond"); |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1627 } |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1628 |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1629 jit_block *tail = create<jit_block> ("if_tail"); |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1630 if (! last_else) |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1631 entry_blocks[entry_blocks.size () - 1] = tail; |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1632 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1633 size_t num_incomming = 0; // number of incomming blocks to our tail |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1634 iter = lst.begin (); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1635 for (size_t i = 0; iter != lst.end (); ++iter, ++i) |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1636 { |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1637 tree_if_clause *tic = *iter; |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1638 block = entry_blocks[i]; |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1639 assert (block); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1640 |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1641 if (i) // the first block is prev_block, so it has already been added |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1642 add_block (entry_blocks[i]); |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1643 |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1644 if (! tic->is_else_clause ()) |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1645 { |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1646 tree_expression *expr = tic->condition (); |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1647 jit_value *cond = visit (expr); |
14945 | 1648 jit_call *check = create<jit_call> (&jit_typeinfo::logically_true, cond); |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1649 block->append (check); |
14943
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
1650 |
14945 | 1651 jit_block *next = create<jit_block> (block->name ()); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1652 add_block (next); |
14945 | 1653 block->append (create<jit_check_error> (check, next, final_block)); |
14943
8efcaf5aa233
Prevent crash when using scalars as conditionals
Max Brister <max@2bass.com>
parents:
14941
diff
changeset
|
1654 block = next; |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1655 |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1656 jit_block *body = create<jit_block> (i == 0 ? "if_body" : "ifelse_body"); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1657 add_block (body); |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1658 |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1659 jit_instruction *br = create<jit_cond_break> (check, body, |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1660 entry_blocks[i + 1]); |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1661 block->append (br); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1662 block = body; |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1663 } |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1664 |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1665 tree_statement_list *stmt_lst = tic->commands (); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1666 assert (stmt_lst); // jwe: Can this be null? |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1667 stmt_lst->accept (*this); |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1668 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1669 if (breaking) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1670 breaking = false; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1671 else |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1672 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1673 ++num_incomming; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1674 block->append (create<jit_break> (tail)); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1675 } |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1676 } |
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1677 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1678 if (num_incomming || ! last_else) |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1679 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1680 add_block (tail); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1681 block = tail; |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1682 } |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1683 else |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1684 // every branch broke, so we don't have a tail |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1685 breaking = true; |
14903 | 1686 } |
1687 | |
1688 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1689 jit_convert::visit_index_expression (tree_index_expression&) |
14903 | 1690 { |
1691 fail (); | |
1692 } | |
1693 | |
1694 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1695 jit_convert::visit_matrix (tree_matrix&) |
14899 | 1696 { |
1697 fail (); | |
1698 } | |
1699 | |
1700 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1701 jit_convert::visit_cell (tree_cell&) |
14906 | 1702 { |
1703 fail (); | |
1704 } | |
1705 | |
1706 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1707 jit_convert::visit_multi_assignment (tree_multi_assignment&) |
14899 | 1708 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1709 fail (); |
14903 | 1710 } |
1711 | |
1712 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1713 jit_convert::visit_no_op_command (tree_no_op_command&) |
14906 | 1714 { |
1715 fail (); | |
1716 } | |
1717 | |
1718 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1719 jit_convert::visit_constant (tree_constant& tc) |
14903 | 1720 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1721 octave_value v = tc.rvalue1 (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1722 if (v.is_real_scalar () && v.is_double_type ()) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1723 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1724 double dv = v.double_value (); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1725 result = create<jit_const_scalar> (dv); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1726 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1727 else if (v.is_range ()) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1728 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1729 Range rv = v.range_value (); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1730 result = create<jit_const_range> (rv); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1731 } |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1732 else |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1733 fail ("Unknown constant"); |
14903 | 1734 } |
14899 | 1735 |
14903 | 1736 void |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1737 jit_convert::visit_fcn_handle (tree_fcn_handle&) |
14903 | 1738 { |
1739 fail (); | |
1740 } | |
14899 | 1741 |
14903 | 1742 void |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1743 jit_convert::visit_parameter_list (tree_parameter_list&) |
14906 | 1744 { |
1745 fail (); | |
1746 } | |
1747 | |
1748 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1749 jit_convert::visit_postfix_expression (tree_postfix_expression&) |
14903 | 1750 { |
1751 fail (); | |
1752 } | |
14899 | 1753 |
14903 | 1754 void |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1755 jit_convert::visit_prefix_expression (tree_prefix_expression&) |
14899 | 1756 { |
1757 fail (); | |
1758 } | |
1759 | |
1760 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1761 jit_convert::visit_return_command (tree_return_command&) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1762 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1763 fail (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1764 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1765 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1766 void |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1767 jit_convert::visit_return_list (tree_return_list&) |
14899 | 1768 { |
1769 fail (); | |
1770 } | |
1771 | |
1772 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1773 jit_convert::visit_simple_assignment (tree_simple_assignment& tsa) |
14899 | 1774 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1775 // resolve rhs |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1776 tree_expression *rhs = tsa.right_hand_side (); |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1777 jit_value *rhsv = visit (rhs); |
14899 | 1778 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1779 // resolve lhs |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1780 tree_expression *lhs = tsa.left_hand_side (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1781 if (! lhs->is_identifier ()) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1782 fail (); |
14899 | 1783 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1784 std::string lhs_name = lhs->name (); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1785 result = do_assign (lhs_name, rhsv, tsa.print_result ()); |
14899 | 1786 } |
1787 | |
1788 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1789 jit_convert::visit_statement (tree_statement& stmt) |
14899 | 1790 { |
1791 tree_command *cmd = stmt.command (); | |
1792 tree_expression *expr = stmt.expression (); | |
1793 | |
1794 if (cmd) | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1795 visit (cmd); |
14899 | 1796 else |
1797 { | |
1798 // stolen from tree_evaluator::visit_statement | |
1799 bool do_bind_ans = false; | |
1800 | |
1801 if (expr->is_identifier ()) | |
1802 { | |
1803 tree_identifier *id = dynamic_cast<tree_identifier *> (expr); | |
1804 | |
1805 do_bind_ans = (! id->is_variable ()); | |
1806 } | |
1807 else | |
1808 do_bind_ans = (! expr->is_assignment_expression ()); | |
1809 | |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1810 jit_value *expr_result = visit (expr); |
14899 | 1811 |
1812 if (do_bind_ans) | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1813 do_assign ("ans", expr_result, expr->print_result ()); |
14899 | 1814 else if (expr->is_identifier () && expr->print_result ()) |
1815 { | |
1816 // FIXME: ugly hack, we need to come up with a way to pass | |
1817 // nargout to visit_identifier | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1818 const jit_function& fn = jit_typeinfo::print_value (); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1819 jit_const_string *name = create<jit_const_string> (expr->name ()); |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1820 block->append (create<jit_call> (fn, name, expr_result)); |
14899 | 1821 } |
1822 } | |
1823 } | |
1824 | |
1825 void | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1826 jit_convert::visit_statement_list (tree_statement_list& lst) |
14906 | 1827 { |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1828 for (tree_statement_list::iterator iter = lst.begin (); iter != lst.end(); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1829 ++iter) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1830 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1831 tree_statement *elt = *iter; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1832 // jwe: Can this ever be null? |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1833 assert (elt); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1834 elt->accept (*this); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1835 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1836 if (breaking) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1837 break; |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1838 } |
14906 | 1839 } |
1840 | |
1841 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1842 jit_convert::visit_switch_case (tree_switch_case&) |
14906 | 1843 { |
1844 fail (); | |
1845 } | |
1846 | |
1847 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1848 jit_convert::visit_switch_case_list (tree_switch_case_list&) |
14899 | 1849 { |
1850 fail (); | |
1851 } | |
1852 | |
1853 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1854 jit_convert::visit_switch_command (tree_switch_command&) |
14906 | 1855 { |
1856 fail (); | |
1857 } | |
1858 | |
1859 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1860 jit_convert::visit_try_catch_command (tree_try_catch_command&) |
14899 | 1861 { |
1862 fail (); | |
1863 } | |
1864 | |
1865 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1866 jit_convert::visit_unwind_protect_command (tree_unwind_protect_command&) |
14899 | 1867 { |
1868 fail (); | |
1869 } | |
1870 | |
1871 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1872 jit_convert::visit_while_command (tree_while_command&) |
14906 | 1873 { |
1874 fail (); | |
1875 } | |
1876 | |
1877 void | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1878 jit_convert::visit_do_until_command (tree_do_until_command&) |
14899 | 1879 { |
1880 fail (); | |
1881 } | |
1882 | |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1883 jit_variable * |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1884 jit_convert::get_variable (const std::string& vname) |
14899 | 1885 { |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1886 vmap_t::iterator iter; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1887 iter = vmap.find (vname); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1888 if (iter != vmap.end ()) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1889 return iter->second; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1890 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1891 jit_variable *var = create<jit_variable> (vname); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1892 octave_value val = symbol_table::find (vname); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1893 jit_type *type = jit_typeinfo::type_of (val); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1894 jit_extract_argument *extract; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1895 extract = create<jit_extract_argument> (type, var); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1896 entry_block->prepend (extract); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1897 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1898 return vmap[vname] = var; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1899 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1900 |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1901 jit_value * |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1902 jit_convert::do_assign (const std::string& lhs, jit_value *rhs, |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1903 bool print) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1904 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1905 jit_variable *var = get_variable (lhs); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1906 block->append (create<jit_assign> (var, rhs)); |
14906 | 1907 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1908 if (print) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1909 { |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1910 const jit_function& print_fn = jit_typeinfo::print_value (); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
1911 jit_const_string *name = create<jit_const_string> (lhs); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1912 block->append (create<jit_call> (print_fn, name, var)); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1913 } |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1914 |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1915 return var; |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1916 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1917 |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1918 jit_value * |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1919 jit_convert::visit (tree& tee) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1920 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1921 result = 0; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1922 tee.accept (*this); |
14906 | 1923 |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
1924 jit_value *ret = result; |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1925 result = 0; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1926 return ret; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
1927 } |
14906 | 1928 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1929 void |
14945 | 1930 jit_convert::append_users_term (jit_terminator *term) |
1931 { | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1932 for (size_t i = 0; i < term->successor_count (); ++i) |
14945 | 1933 { |
1934 if (term->alive (i)) | |
1935 { | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1936 jit_block *succ = term->successor (i); |
14945 | 1937 for (jit_block::iterator iter = succ->begin (); iter != succ->end () |
1938 && isa<jit_phi> (*iter); ++iter) | |
14946
3564bb141396
Only add items to worklist in type inferece if not already there
Max Brister <max@2bass.com>
parents:
14945
diff
changeset
|
1939 push_worklist (*iter); |
3564bb141396
Only add items to worklist in type inferece if not already there
Max Brister <max@2bass.com>
parents:
14945
diff
changeset
|
1940 |
3564bb141396
Only add items to worklist in type inferece if not already there
Max Brister <max@2bass.com>
parents:
14945
diff
changeset
|
1941 jit_terminator *sterm = succ->terminator (); |
3564bb141396
Only add items to worklist in type inferece if not already there
Max Brister <max@2bass.com>
parents:
14945
diff
changeset
|
1942 if (sterm) |
3564bb141396
Only add items to worklist in type inferece if not already there
Max Brister <max@2bass.com>
parents:
14945
diff
changeset
|
1943 push_worklist (sterm); |
14945 | 1944 } |
1945 } | |
1946 } | |
1947 | |
1948 void | |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1949 jit_convert::merge_blocks (void) |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1950 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1951 std::vector<jit_block *> dead; |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1952 for (block_list::iterator iter = blocks.begin (); iter != blocks.end (); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1953 ++iter) |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1954 { |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1955 jit_block *b = *iter; |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1956 jit_block *merged = b->maybe_merge (); |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1957 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1958 if (merged) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1959 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1960 if (merged == final_block) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1961 final_block = b; |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1962 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1963 if (merged == entry_block) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1964 entry_block = b; |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1965 |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1966 dead.push_back (merged); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1967 } |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1968 } |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1969 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1970 for (size_t i = 0; i < dead.size (); ++i) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
1971 blocks.erase (dead[i]->location ()); |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1972 } |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1973 |
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1974 void |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
1975 jit_convert::construct_ssa (void) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1976 { |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1977 merge_blocks (); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1978 final_block->label (); |
14939
4488022820c9
No longer segfault when compiling nested for loops
Max Brister <max@2bass.com>
parents:
14938
diff
changeset
|
1979 final_block->compute_idom (entry_block); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1980 entry_block->compute_df (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1981 entry_block->create_dom_tree (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1982 |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
1983 // insert phi nodes where needed, this is done on a per variable basis |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1984 for (vmap_t::iterator iter = vmap.begin (); iter != vmap.end (); ++iter) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1985 { |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1986 jit_block::df_set visited, added_phi; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1987 std::list<jit_block *> ssa_worklist; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1988 iter->second->use_blocks (visited); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1989 ssa_worklist.insert (ssa_worklist.begin (), visited.begin (), visited.end ()); |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1990 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1991 while (ssa_worklist.size ()) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
1992 { |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1993 jit_block *b = ssa_worklist.front (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1994 ssa_worklist.pop_front (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1995 |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1996 for (jit_block::df_iterator diter = b->df_begin (); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1997 diter != b->df_end (); ++diter) |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
1998 { |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
1999 jit_block *dblock = *diter; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2000 if (! added_phi.count (dblock)) |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
2001 { |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2002 jit_phi *phi = create<jit_phi> (iter->second, |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2003 dblock->use_count ()); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2004 dblock->prepend (phi); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2005 added_phi.insert (dblock); |
14925
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
2006 } |
8697e3e9d77a
Properly cleanup the low level IR
Max Brister <max@2bass.com>
parents:
14924
diff
changeset
|
2007 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2008 if (! visited.count (dblock)) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2009 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2010 ssa_worklist.push_back (dblock); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2011 visited.insert (dblock); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2012 } |
14923
168cb10bb9c5
If, ifelse, and else statements JIT compile now
Max Brister <max@2bass.com>
parents:
14922
diff
changeset
|
2013 } |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2014 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2015 } |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2016 |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2017 entry_block->visit_dom (&jit_convert::do_construct_ssa, &jit_block::pop_all); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2018 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2019 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2020 void |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2021 jit_convert::do_construct_ssa (jit_block& block) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2022 { |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2023 // replace variables with their current SSA value |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2024 for (jit_block::iterator iter = block.begin (); iter != block.end (); ++iter) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2025 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2026 jit_instruction *instr = *iter; |
14937
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
2027 if (! isa<jit_phi> (instr)) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2028 { |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2029 for (size_t i = isa<jit_assign> (instr); i < instr->argument_count (); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2030 ++i) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2031 { |
14937
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
2032 jit_value *arg = instr->argument (i); |
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
2033 jit_variable *var = dynamic_cast<jit_variable *> (arg); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2034 if (var) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2035 instr->stash_argument (i, var->top ()); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2036 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2037 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2038 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2039 instr->push_variable (); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2040 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2041 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2042 // finish phi nodes of successors |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2043 for (size_t i = 0; i < block.successor_count (); ++i) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2044 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2045 jit_block *finish = block.successor (i); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2046 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2047 for (jit_block::iterator iter = finish->begin (); iter != finish->end () |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2048 && isa<jit_phi> (*iter);) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2049 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2050 jit_phi *phi = static_cast<jit_phi *> (*iter); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2051 jit_variable *var = phi->dest (); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2052 if (var->has_top ()) |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2053 { |
14945 | 2054 phi->add_incomming (&block, var->top ()); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2055 ++iter; |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2056 } |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2057 else |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2058 { |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2059 // temporaries may have extranious phi nodes which can be removed |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2060 assert (! phi->use_count ()); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2061 assert (var->name ().size () && var->name ()[0] == '#'); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2062 iter = finish->remove (iter); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2063 } |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2064 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2065 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2066 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2067 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2068 void |
14945 | 2069 jit_convert::remove_dead () |
2070 { | |
2071 block_list::iterator biter; | |
2072 for (biter = blocks.begin (); biter != blocks.end (); ++biter) | |
2073 { | |
2074 jit_block *b = *biter; | |
2075 if (b->alive ()) | |
2076 { | |
2077 for (jit_block::iterator iter = b->begin (); iter != b->end () | |
2078 && isa<jit_phi> (*iter);) | |
2079 { | |
2080 jit_phi *phi = static_cast<jit_phi *> (*iter); | |
2081 if (phi->prune ()) | |
2082 iter = b->remove (iter); | |
2083 else | |
2084 ++iter; | |
2085 } | |
2086 } | |
2087 } | |
2088 | |
2089 for (biter = blocks.begin (); biter != blocks.end ();) | |
2090 { | |
2091 jit_block *b = *biter; | |
2092 if (b->alive ()) | |
2093 { | |
2094 // FIXME: A special case for jit_check_error, if we generalize to | |
2095 // we will need to change! | |
2096 jit_terminator *term = b->terminator (); | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2097 if (term && term->successor_count () == 2 && ! term->alive (0)) |
14945 | 2098 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2099 jit_block *succ = term->successor (1); |
14945 | 2100 term->remove (); |
2101 jit_break *abreak = b->append (create<jit_break> (succ)); | |
2102 abreak->infer (); | |
2103 } | |
2104 | |
2105 ++biter; | |
2106 } | |
2107 else | |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2108 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2109 jit_terminator *term = b->terminator (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2110 if (term) |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2111 term->remove (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2112 biter = blocks.erase (biter); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2113 } |
14945 | 2114 } |
2115 } | |
2116 | |
2117 void | |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2118 jit_convert::place_releases (void) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2119 { |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2120 release_placer placer (*this); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2121 entry_block->visit_dom (placer, &jit_block::pop_all); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2122 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2123 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2124 void |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2125 jit_convert::finish_breaks (jit_block *dest, const block_list& lst) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2126 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2127 for (block_list::const_iterator iter = lst.begin (); iter != lst.end (); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2128 ++iter) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2129 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2130 jit_block *b = *iter; |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2131 b->append (create<jit_break> (dest)); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2132 } |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2133 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2134 |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2135 // -------------------- jit_convert::release_placer -------------------- |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2136 void |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2137 jit_convert::release_placer::operator() (jit_block& block) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2138 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2139 for (jit_block::iterator iter = block.begin (); iter != block.end (); ++iter) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2140 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2141 jit_instruction *instr = *iter; |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2142 instr->stash_last_use (instr); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2143 |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2144 for (size_t i = 0; i < instr->argument_count (); ++i) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2145 { |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2146 jit_value *arg = instr->argument (i); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2147 assert (arg); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2148 arg->stash_last_use (instr); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2149 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2150 |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2151 jit_assign *assign = dynamic_cast<jit_assign *> (instr); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2152 if (assign && assign->dest ()->has_top ()) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2153 { |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2154 jit_variable *var = assign->dest (); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2155 jit_instruction *last_use = var->last_use (); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2156 jit_call *release = convert.create<jit_call> (jit_typeinfo::release, |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2157 var->top ()); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2158 release->infer (); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2159 if (last_use && last_use->parent () == &block |
14937
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
2160 && ! isa<jit_phi> (last_use)) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2161 block.insert_after (last_use->location (), release); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2162 else |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2163 block.prepend_after_phi (release); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2164 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2165 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2166 instr->push_variable (); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2167 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2168 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2169 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2170 // -------------------- jit_convert::convert_llvm -------------------- |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2171 llvm::Function * |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2172 jit_convert::convert_llvm::convert (llvm::Module *module, |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2173 const std::vector<std::pair< std::string, bool> >& args, |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
2174 const std::list<jit_block *>& blocks, |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
2175 const std::list<jit_value *>& constants) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2176 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2177 jit_type *any = jit_typeinfo::get_any (); |
14899 | 2178 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2179 // argument is an array of octave_base_value*, or octave_base_value** |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2180 llvm::Type *arg_type = any->to_llvm (); // this is octave_base_value* |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2181 arg_type = arg_type->getPointerTo (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2182 llvm::FunctionType *ft = llvm::FunctionType::get (llvm::Type::getVoidTy (context), |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2183 arg_type, false); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2184 function = llvm::Function::Create (ft, llvm::Function::ExternalLinkage, |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2185 "foobar", module); |
14906 | 2186 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2187 try |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2188 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2189 llvm::BasicBlock *prelude = llvm::BasicBlock::Create (context, "prelude", |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2190 function); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2191 builder.SetInsertPoint (prelude); |
14906 | 2192 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2193 llvm::Value *arg = function->arg_begin (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2194 for (size_t i = 0; i < args.size (); ++i) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2195 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2196 llvm::Value *loaded_arg = builder.CreateConstInBoundsGEP1_32 (arg, i); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2197 arguments[args[i].first] = loaded_arg; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2198 } |
14906 | 2199 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2200 std::list<jit_block *>::const_iterator biter; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2201 for (biter = blocks.begin (); biter != blocks.end (); ++biter) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2202 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2203 jit_block *jblock = *biter; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2204 llvm::BasicBlock *block = llvm::BasicBlock::Create (context, jblock->name (), |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2205 function); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2206 jblock->stash_llvm (block); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2207 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2208 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2209 jit_block *first = *blocks.begin (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2210 builder.CreateBr (first->to_llvm ()); |
14906 | 2211 |
14944
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
2212 // constants aren't in the IR, we visit those first |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
2213 for (std::list<jit_value *>::const_iterator iter = constants.begin (); |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
2214 iter != constants.end (); ++iter) |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
2215 if (! isa<jit_instruction> (*iter)) |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
2216 visit (*iter); |
c0a5ab3b9278
jit_const no longer inherits from jit_instruction
Max Brister <max@2bass.com>
parents:
14943
diff
changeset
|
2217 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2218 // convert all instructions |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2219 for (biter = blocks.begin (); biter != blocks.end (); ++biter) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2220 visit (*biter); |
14906 | 2221 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2222 // now finish phi nodes |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2223 for (biter = blocks.begin (); biter != blocks.end (); ++biter) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2224 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2225 jit_block& block = **biter; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2226 for (jit_block::iterator piter = block.begin (); |
14937
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
2227 piter != block.end () && isa<jit_phi> (*piter); ++piter) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2228 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2229 jit_instruction *phi = *piter; |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2230 finish_phi (phi); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2231 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2232 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2233 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2234 jit_block *last = blocks.back (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2235 builder.SetInsertPoint (last->to_llvm ()); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2236 builder.CreateRetVoid (); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2237 } catch (const jit_fail_exception& e) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2238 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2239 function->eraseFromParent (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2240 throw; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2241 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2242 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2243 return function; |
14899 | 2244 } |
2245 | |
2246 void | |
14945 | 2247 jit_convert::convert_llvm::finish_phi (jit_instruction *aphi) |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2248 { |
14945 | 2249 jit_phi *phi = static_cast<jit_phi *> (aphi); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2250 llvm::PHINode *llvm_phi = llvm::cast<llvm::PHINode> (phi->to_llvm ()); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2251 |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2252 bool can_remove = ! phi->use_count (); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2253 if (! can_remove && llvm_phi->hasOneUse () && phi->use_count () == 1) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2254 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2255 jit_instruction *user = phi->first_use ()->user (); |
14937
78e1457c5bf5
Remove some macros from pt-jit.h and pt-jit.cc
Max Brister <max@2bass.com>
parents:
14936
diff
changeset
|
2256 can_remove = isa<jit_call> (user); // must be a remove |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2257 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2258 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2259 if (can_remove) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2260 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2261 // replace with releases along each incomming branch |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2262 while (! llvm_phi->use_empty ()) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2263 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2264 llvm::Instruction *llvm_instr; |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2265 llvm_instr = llvm::cast<llvm::Instruction> (llvm_phi->use_back ()); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2266 llvm_instr->eraseFromParent (); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2267 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2268 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2269 llvm_phi->eraseFromParent (); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2270 phi->stash_llvm (0); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2271 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2272 for (size_t i = 0; i < phi->argument_count (); ++i) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2273 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2274 jit_value *arg = phi->argument (i); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2275 if (arg->has_llvm () && phi->argument_type (i) != phi->type ()) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2276 { |
14945 | 2277 llvm::BasicBlock *pred = phi->incomming_llvm (i); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2278 builder.SetInsertPoint (--pred->end ()); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2279 const jit_function::overload& ol |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2280 = jit_typeinfo::get_release (phi->argument_type (i)); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2281 if (! ol.function) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2282 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2283 std::stringstream ss; |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2284 ss << "No release for phi(" << i << "): "; |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2285 phi->print (ss); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2286 fail (ss.str ()); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2287 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2288 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2289 builder.CreateCall (ol.function, phi->argument_llvm (i)); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2290 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2291 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2292 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2293 else |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2294 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2295 for (size_t i = 0; i < phi->argument_count (); ++i) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2296 { |
14945 | 2297 llvm::BasicBlock *pred = phi->incomming_llvm (i); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2298 if (phi->argument_type (i) == phi->type ()) |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2299 llvm_phi->addIncoming (phi->argument_llvm (i), pred); |
14935
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2300 else |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2301 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2302 // add cast right before pred terminator |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2303 builder.SetInsertPoint (--pred->end ()); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2304 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2305 const jit_function::overload& ol |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2306 = jit_typeinfo::cast (phi->type (), |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2307 phi->argument_type (i)); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2308 if (! ol.function) |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2309 { |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2310 std::stringstream ss; |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2311 ss << "No cast for phi(" << i << "): "; |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2312 phi->print (ss); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2313 fail (ss.str ()); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2314 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2315 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2316 llvm::Value *casted; |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2317 casted = builder.CreateCall (ol.function, |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2318 phi->argument_llvm (i)); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2319 llvm_phi->addIncoming (casted, pred); |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2320 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2321 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2322 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2323 } |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2324 |
5801e031a3b5
Place releases after last use and generalize dom visiting
Max Brister <max@2bass.com>
parents:
14932
diff
changeset
|
2325 void |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2326 jit_convert::convert_llvm::visit (jit_const_string& cs) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2327 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2328 cs.stash_llvm (builder.CreateGlobalStringPtr (cs.value ())); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2329 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2330 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2331 void |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2332 jit_convert::convert_llvm::visit (jit_const_scalar& cs) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2333 { |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2334 cs.stash_llvm (llvm::ConstantFP::get (cs.type_llvm (), cs.value ())); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2335 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2336 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2337 void jit_convert::convert_llvm::visit (jit_const_index& ci) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2338 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2339 ci.stash_llvm (llvm::ConstantInt::get (ci.type_llvm (), ci.value ())); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2340 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2341 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2342 void |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2343 jit_convert::convert_llvm::visit (jit_const_range& cr) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2344 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2345 llvm::StructType *stype = llvm::cast<llvm::StructType>(cr.type_llvm ()); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2346 llvm::Type *dbl = jit_typeinfo::get_scalar_llvm (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2347 llvm::Type *idx = jit_typeinfo::get_index_llvm (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2348 const jit_range& rng = cr.value (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2349 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2350 llvm::Constant *constants[4]; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2351 constants[0] = llvm::ConstantFP::get (dbl, rng.base); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2352 constants[1] = llvm::ConstantFP::get (dbl, rng.limit); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2353 constants[2] = llvm::ConstantFP::get (dbl, rng.inc); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2354 constants[3] = llvm::ConstantInt::get (idx, rng.nelem); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2355 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2356 llvm::Value *as_llvm; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2357 as_llvm = llvm::ConstantStruct::get (stype, |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2358 llvm::makeArrayRef (constants, 4)); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2359 cr.stash_llvm (as_llvm); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2360 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2361 |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2362 void |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2363 jit_convert::convert_llvm::visit (jit_block& b) |
14899 | 2364 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2365 llvm::BasicBlock *block = b.to_llvm (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2366 builder.SetInsertPoint (block); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2367 for (jit_block::iterator iter = b.begin (); iter != b.end (); ++iter) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2368 visit (*iter); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2369 } |
14903 | 2370 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2371 void |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2372 jit_convert::convert_llvm::visit (jit_break& b) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2373 { |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2374 b.stash_llvm (builder.CreateBr (b.successor_llvm ())); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2375 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2376 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2377 void |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2378 jit_convert::convert_llvm::visit (jit_cond_break& cb) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2379 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2380 llvm::Value *cond = cb.cond_llvm (); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2381 llvm::Value *br; |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2382 br = builder.CreateCondBr (cond, cb.successor_llvm (0), cb.successor_llvm (1)); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2383 cb.stash_llvm (br); |
14899 | 2384 } |
2385 | |
14913
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2386 void |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2387 jit_convert::convert_llvm::visit (jit_call& call) |
14913
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2388 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2389 const jit_function::overload& ol = call.overload (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2390 if (! ol.function) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2391 fail ("No overload for: " + call.print_string ()); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2392 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2393 std::vector<llvm::Value *> args (call.argument_count ()); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2394 for (size_t i = 0; i < call.argument_count (); ++i) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2395 args[i] = call.argument_llvm (i); |
14913
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2396 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2397 call.stash_llvm (builder.CreateCall (ol.function, args)); |
14913
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2398 } |
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2399 |
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2400 void |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2401 jit_convert::convert_llvm::visit (jit_extract_argument& extract) |
14913
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2402 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2403 const jit_function::overload& ol = extract.overload (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2404 if (! ol.function) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2405 fail (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2406 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2407 llvm::Value *arg = arguments[extract.name ()]; |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2408 assert (arg); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2409 arg = builder.CreateLoad (arg); |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2410 extract.stash_llvm (builder.CreateCall (ol.function, arg, extract.name ())); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2411 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2412 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2413 void |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2414 jit_convert::convert_llvm::visit (jit_store_argument& store) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2415 { |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2416 llvm::Value *arg_value = store.result_llvm (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2417 const jit_function::overload& ol = store.overload (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2418 if (! ol.function) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2419 fail (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2420 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2421 arg_value = builder.CreateCall (ol.function, arg_value); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2422 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2423 llvm::Value *arg = arguments[store.name ()]; |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2424 store.stash_llvm (builder.CreateStore (arg_value, arg)); |
14913
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2425 } |
c7071907a641
Use symbol_record_ref instead of names in JIT
Max Brister <max@2bass.com>
parents:
14911
diff
changeset
|
2426 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2427 void |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2428 jit_convert::convert_llvm::visit (jit_phi& phi) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2429 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2430 // we might not have converted all incoming branches, so we don't |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2431 // set incomming branches now |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2432 llvm::PHINode *node = llvm::PHINode::Create (phi.type_llvm (), |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2433 phi.argument_count ()); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2434 builder.Insert (node); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2435 phi.stash_llvm (node); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2436 |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2437 jit_block *pblock = phi.parent (); |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2438 for (size_t i = 0; i < phi.argument_count (); ++i) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2439 if (phi.argument_type (i) != phi.type ()) |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2440 { |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2441 jit_block *inc = phi.incomming (i); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2442 jit_terminator *term = inc->terminator (); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2443 term->create_merge (function, pblock); |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2444 } |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2445 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2446 |
14928
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2447 void |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2448 jit_convert::convert_llvm::visit (jit_variable&) |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2449 { |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2450 fail ("ERROR: SSA construction should remove all variables"); |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2451 } |
39d52aa37a08
Use standard SSA construction algorithm, and support break/continue
Max Brister <max@2bass.com>
parents:
14925
diff
changeset
|
2452 |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2453 void |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2454 jit_convert::convert_llvm::visit (jit_check_error& check) |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2455 { |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2456 llvm::Value *cond = jit_typeinfo::insert_error_check (); |
14948
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2457 llvm::Value *br = builder.CreateCondBr (cond, check.successor_llvm (0), |
006570a76b90
Cleanup and optimization of JIT
Max Brister <max@2bass.com>
parents:
14946
diff
changeset
|
2458 check.successor_llvm (1)); |
14938
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2459 check.stash_llvm (br); |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2460 } |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2461 |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2462 void |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2463 jit_convert::convert_llvm::visit (jit_assign&) |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2464 {} |
bab44e3ee291
Adding basic error support to JIT
Max Brister <max@2bass.com>
parents:
14937
diff
changeset
|
2465 |
14906 | 2466 // -------------------- tree_jit -------------------- |
2467 | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2468 tree_jit::tree_jit (void) : module (0), engine (0) |
14906 | 2469 { |
2470 } | |
2471 | |
2472 tree_jit::~tree_jit (void) | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2473 {} |
14906 | 2474 |
2475 bool | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2476 tree_jit::execute (tree_simple_for_command& cmd) |
14906 | 2477 { |
2478 if (! initialize ()) | |
2479 return false; | |
2480 | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2481 jit_info *info = cmd.get_info (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2482 if (! info || ! info->match ()) |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2483 { |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2484 delete info; |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2485 info = new jit_info (*this, cmd); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2486 cmd.stash_info (info); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2487 } |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2488 |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2489 return info->execute (); |
14906 | 2490 } |
2491 | |
2492 bool | |
2493 tree_jit::initialize (void) | |
14903 | 2494 { |
14906 | 2495 if (engine) |
2496 return true; | |
2497 | |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2498 if (! module) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2499 { |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2500 llvm::InitializeNativeTarget (); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2501 module = new llvm::Module ("octave", context); |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2502 } |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2503 |
14906 | 2504 // sometimes this fails pre main |
2505 engine = llvm::ExecutionEngine::createJIT (module); | |
2506 | |
2507 if (! engine) | |
2508 return false; | |
2509 | |
2510 module_pass_manager = new llvm::PassManager (); | |
2511 module_pass_manager->add (llvm::createAlwaysInlinerPass ()); | |
2512 | |
2513 pass_manager = new llvm::FunctionPassManager (module); | |
2514 pass_manager->add (new llvm::TargetData(*engine->getTargetData ())); | |
2515 pass_manager->add (llvm::createBasicAliasAnalysisPass ()); | |
2516 pass_manager->add (llvm::createPromoteMemoryToRegisterPass ()); | |
2517 pass_manager->add (llvm::createInstructionCombiningPass ()); | |
2518 pass_manager->add (llvm::createReassociatePass ()); | |
2519 pass_manager->add (llvm::createGVNPass ()); | |
2520 pass_manager->add (llvm::createCFGSimplificationPass ()); | |
2521 pass_manager->doInitialization (); | |
2522 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2523 jit_typeinfo::initialize (module, engine); |
14906 | 2524 |
2525 return true; | |
2526 } | |
2527 | |
2528 | |
2529 void | |
2530 tree_jit::optimize (llvm::Function *fn) | |
2531 { | |
2532 module_pass_manager->run (*module); | |
2533 pass_manager->run (*fn); | |
2534 } | |
2535 | |
2536 // -------------------- jit_info -------------------- | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2537 jit_info::jit_info (tree_jit& tjit, tree& tee) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2538 : engine (tjit.get_engine ()) |
14906 | 2539 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2540 llvm::Function *fun = 0; |
14903 | 2541 try |
2542 { | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2543 jit_convert conv (tjit.get_module (), tee); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2544 fun = conv.get_function (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2545 arguments = conv.get_arguments (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2546 bounds = conv.get_bounds (); |
14903 | 2547 } |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2548 catch (const jit_fail_exception& e) |
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2549 { |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2550 #ifdef OCTAVE_JIT_DEBUG |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2551 if (e.known ()) |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2552 std::cout << "jit fail: " << e.what () << std::endl; |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2553 #endif |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2554 } |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2555 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2556 if (! fun) |
14903 | 2557 { |
2558 function = 0; | |
2559 return; | |
2560 } | |
2561 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2562 tjit.optimize (fun); |
14906 | 2563 |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2564 #ifdef OCTAVE_JIT_DEBUG |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2565 std::cout << "-------------------- optimized llvm ir --------------------\n"; |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2566 llvm::raw_os_ostream llvm_cout (std::cout); |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2567 fun->print (llvm_cout); |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2568 std::cout << std::endl; |
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2569 #endif |
14903 | 2570 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2571 function = reinterpret_cast<jited_function>(engine->getPointerToFunction (fun)); |
14903 | 2572 } |
2573 | |
2574 bool | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2575 jit_info::execute (void) const |
14899 | 2576 { |
2577 if (! function) | |
2578 return false; | |
2579 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2580 std::vector<octave_base_value *> real_arguments (arguments.size ()); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2581 for (size_t i = 0; i < arguments.size (); ++i) |
14899 | 2582 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2583 if (arguments[i].second) |
14899 | 2584 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2585 octave_value current = symbol_table::varval (arguments[i].first); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2586 octave_base_value *obv = current.internal_rep (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2587 obv->grab (); |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2588 real_arguments[i] = obv; |
14899 | 2589 } |
2590 } | |
2591 | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2592 function (&real_arguments[0]); |
14899 | 2593 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2594 for (size_t i = 0; i < arguments.size (); ++i) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2595 symbol_table::varref (arguments[i].first) = real_arguments[i]; |
14910
a8f1e08de8fc
Simplified llvm::GenericValue creation
Max Brister <max@2bass.com>
parents:
14906
diff
changeset
|
2596 |
14899 | 2597 return true; |
2598 } | |
14903 | 2599 |
2600 bool | |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2601 jit_info::match (void) const |
14903 | 2602 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2603 if (! function) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2604 return true; |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2605 |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2606 for (size_t i = 0; i < bounds.size (); ++i) |
14903 | 2607 { |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2608 const std::string& arg_name = bounds[i].second; |
14920
51d4b1018efb
For loops compile with new IR
Max Brister <max@2bass.com>
parents:
14918
diff
changeset
|
2609 octave_value value = symbol_table::find (arg_name); |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2610 jit_type *type = jit_typeinfo::type_of (value); |
14906 | 2611 |
14917
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2612 // FIXME: Check for a parent relationship |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2613 if (type != bounds[i].first) |
232d8ab07932
Rewrite pt-jit.* adding new low level octave IR
Max Brister <max@2bass.com>
parents:
14915
diff
changeset
|
2614 return false; |
14903 | 2615 } |
2616 | |
2617 return true; | |
2618 } | |
14932
1f914446157d
Locate and link with LLVM properly
Max Brister <max@2bass.com>
parents:
14928
diff
changeset
|
2619 #endif |