# HG changeset patch # User Max Brister # Date 1340858586 18000 # Node ID f649b66ef1afddb287d5c1edf733affb594cc279 # Parent d3f9801b1f29e1e706116487429690bcfe1d2d3e Add short circult operators to JIT * src/pt-jit.cc (jit_convert::jit_convert): Initialize short_count. (jit_convert::visit_binary_expression): Add support for short circut operators. (jit_convert::visit_if_command_list): Remove duplicate check append. (jit_convert::visit_simple_assignment): Store result. (jit_convert::convert_llvm::visit): New overload. * src/pt-jit.h (jit_const_bool): New specialization of jit_const. (jit_convert::short_count): New variable. diff -r d3f9801b1f29 -r f649b66ef1af src/pt-jit.cc --- a/src/pt-jit.cc Wed Jun 27 18:50:59 2012 -0500 +++ b/src/pt-jit.cc Wed Jun 27 23:43:06 2012 -0500 @@ -1836,7 +1836,7 @@ // -------------------- jit_convert -------------------- jit_convert::jit_convert (llvm::Module *module, tree &tee) - : iterator_count (0), breaking (false) + : iterator_count (0), short_count (0), breaking (false) { jit_instruction::reset_ids (); @@ -1942,18 +1942,65 @@ void jit_convert::visit_binary_expression (tree_binary_expression& be) { - // this is the case for bool_or and bool_and if (be.op_type () >= octave_value::num_binary_ops) - fail ("Unsupported binary operator"); - - tree_expression *lhs = be.lhs (); - jit_value *lhsv = visit (lhs); - - tree_expression *rhs = be.rhs (); - jit_value *rhsv = visit (rhs); - - const jit_function& fn = jit_typeinfo::binary_op (be.op_type ()); - result = create_checked (fn, lhsv, rhsv); + { + tree_boolean_expression *boole; + boole = dynamic_cast (&be); + assert (boole); + bool is_and = boole->op_type () == tree_boolean_expression::bool_and; + + std::stringstream ss; + ss << "#short_result" << short_count++; + + std::string short_name = ss.str (); + jit_variable *short_result = create (short_name); + vmap[short_name] = short_result; + + jit_block *done = create (block->name ()); + tree_expression *lhs = be.lhs (); + jit_value *lhsv = visit (lhs); + lhsv = create_checked (&jit_typeinfo::logically_true, lhsv); + + jit_block *short_early = create ("short_early"); + append (short_early); + + jit_block *short_cont = create ("short_cont"); + + if (is_and) + block->append (create (lhsv, short_cont, short_early)); + else + block->append (create (lhsv, short_early, short_cont)); + + block = short_early; + + jit_value *early_result = create (! is_and); + block->append (create (short_result, early_result)); + block->append (create (done)); + + append (short_cont); + block = short_cont; + + tree_expression *rhs = be.rhs (); + jit_value *rhsv = visit (rhs); + rhsv = create_checked (&jit_typeinfo::logically_true, rhsv); + block->append (create (short_result, rhsv)); + block->append (create (done)); + + append (done); + block = done; + result = short_result; + } + else + { + tree_expression *lhs = be.lhs (); + jit_value *lhsv = visit (lhs); + + tree_expression *rhs = be.rhs (); + jit_value *rhsv = visit (rhs); + + const jit_function& fn = jit_typeinfo::binary_op (be.op_type ()); + result = create_checked (fn, lhsv, rhsv); + } } void @@ -2196,8 +2243,6 @@ jit_value *cond = visit (expr); jit_call *check = create_checked (&jit_typeinfo::logically_true, cond); - block->append (check); - jit_block *body = create (i == 0 ? "if_body" : "ifelse_body"); append (body); @@ -2329,7 +2374,7 @@ tree_expression *rhs = tsa.right_hand_side (); jit_value *rhsv = visit (rhs); - do_assign (tsa.left_hand_side (), rhsv); + result = do_assign (tsa.left_hand_side (), rhsv); } void @@ -2980,6 +3025,12 @@ } void +jit_convert::convert_llvm::visit (jit_const_bool& cb) +{ + cb.stash_llvm (llvm::ConstantInt::get (cb.type_llvm (), cb.value ())); +} + +void jit_convert::convert_llvm::visit (jit_const_scalar& cs) { cs.stash_llvm (llvm::ConstantFP::get (cs.type_llvm (), cs.value ())); diff -r d3f9801b1f29 -r f649b66ef1af src/pt-jit.h --- a/src/pt-jit.h Wed Jun 27 18:50:59 2012 -0500 +++ b/src/pt-jit.h Wed Jun 27 23:43:06 2012 -0500 @@ -775,6 +775,7 @@ JIT_METH(argument) #define JIT_VISIT_IR_CONST \ + JIT_METH(const_bool); \ JIT_METH(const_scalar); \ JIT_METH(const_index); \ JIT_METH(const_string); \ @@ -802,6 +803,7 @@ bool QUOTE=false> class jit_const; +typedef jit_const jit_const_bool; typedef jit_const jit_const_scalar; typedef jit_const jit_const_index; @@ -2223,6 +2225,7 @@ std::list all_values; size_t iterator_count; + size_t short_count; typedef std::map vmap_t; vmap_t vmap;