annotate src/pt-jit.cc @ 14958:4b98b3f66e46

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