annotate src/pt-jit.cc @ 14952:e3696f2c6da6

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