1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
|
////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2012-2021 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// This file is part of Octave.
//
// Octave is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Octave is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Octave; see the file COPYING. If not, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////
// defines required by llvm
#define __STDC_LIMIT_MACROS
#define __STDC_CONSTANT_MACROS
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#if defined (HAVE_LLVM)
#include "jit-ir.h"
#if defined (HAVE_LLVM_IR_FUNCTION_H)
# include <llvm/IR/BasicBlock.h>
# include <llvm/IR/Instructions.h>
#else
# include <llvm/BasicBlock.h>
# include <llvm/Instructions.h>
#endif
#include "error.h"
namespace octave
{
// -------------------- jit_factory --------------------
jit_factory::~jit_factory (void)
{
for (auto iter = m_all_values.begin ();
iter != m_all_values.end (); ++iter)
delete *iter;
}
void
jit_factory::track_value (jit_value *value)
{
if (value->type ())
m_constants.push_back (value);
m_all_values.push_back (value);
}
// -------------------- jit_block_list --------------------
void
jit_block_list::insert_after (iterator iter, jit_block *ablock)
{
++iter;
insert_before (iter, ablock);
}
void
jit_block_list::insert_after (jit_block *loc, jit_block *ablock)
{
insert_after (loc->location (), ablock);
}
void
jit_block_list::insert_before (iterator iter, jit_block *ablock)
{
iter = m_list.insert (iter, ablock);
ablock->stash_location (iter);
}
void
jit_block_list::insert_before (jit_block *loc, jit_block *ablock)
{
insert_before (loc->location (), ablock);
}
void
jit_block_list::label (void)
{
if (m_list.size ())
{
jit_block *block = m_list.back ();
block->label ();
}
}
std::ostream&
jit_block_list::print (std::ostream& os, const std::string& header) const
{
os << "-------------------- " << header << " --------------------\n";
return os << *this;
}
std::ostream&
jit_block_list::print_dom (std::ostream& os) const
{
os << "-------------------- dom info --------------------\n";
for (auto iter = begin (); iter != end (); ++iter)
{
assert (*iter);
(*iter)->print_dom (os);
}
os << std::endl;
return os;
}
void
jit_block_list::push_back (jit_block *b)
{
m_list.push_back (b);
auto iter = m_list.end ();
b->stash_location (--iter);
}
std::ostream&
operator<<(std::ostream& os, const jit_block_list& blocks)
{
for (auto iter = blocks.begin (); iter != blocks.end (); ++iter)
{
assert (*iter);
(*iter)->print (os, 0);
}
return os << std::endl;
}
// -------------------- jit_use --------------------
jit_block *
jit_use::user_parent (void) const
{
return m_user->parent ();
}
// -------------------- jit_value --------------------
jit_value::~jit_value (void)
{ }
jit_block *
jit_value::first_use_block (void)
{
jit_use *use = first_use ();
while (use)
{
if (! isa<jit_error_check> (use->user ()))
return use->user_parent ();
use = use->next ();
}
return 0;
}
void
jit_value::replace_with (jit_value *value)
{
while (first_use ())
{
jit_instruction *user = first_use ()->user ();
size_t idx = first_use ()->index ();
user->stash_argument (idx, value);
}
}
#define JIT_METH(clname) \
void \
jit_ ## clname::accept (jit_ir_walker& walker) \
{ \
walker.visit (*this); \
}
JIT_VISIT_IR_NOTEMPLATE
#undef JIT_METH
std::ostream&
operator<< (std::ostream& os, const jit_value& value)
{
return value.short_print (os);
}
std::ostream&
jit_print (std::ostream& os, jit_value *avalue)
{
if (avalue)
return avalue->print (os);
return os << "NULL";
}
// -------------------- jit_instruction --------------------
void
jit_instruction::remove (void)
{
if (m_parent)
m_parent->remove (m_location);
resize_arguments (0);
}
llvm::BasicBlock *
jit_instruction::parent_llvm (void) const
{
return m_parent->to_llvm ();
}
std::ostream&
jit_instruction::short_print (std::ostream& os) const
{
if (type ())
jit_print (os, type ()) << ": ";
return os << '#' << m_id;
}
void
jit_instruction::do_construct_ssa (size_t start, size_t end)
{
for (size_t i = start; i < end; ++i)
{
jit_value *arg = argument (i);
jit_variable *var = dynamic_cast<jit_variable *> (arg);
if (var && var->has_top ())
stash_argument (i, var->top ());
}
}
// -------------------- jit_block --------------------
void
jit_block::replace_with (jit_value *value)
{
assert (isa<jit_block> (value));
jit_block *block = static_cast<jit_block *> (value);
jit_value::replace_with (block);
while (ILIST_T::first_use ())
{
jit_phi_incoming *incoming = ILIST_T::first_use ();
incoming->stash_value (block);
}
}
void
jit_block::replace_in_phi (jit_block *ablock, jit_block *with)
{
jit_phi_incoming *node = ILIST_T::first_use ();
while (node)
{
jit_phi_incoming *prev = node;
node = node->next ();
if (prev->user_parent () == ablock)
prev->stash_value (with);
}
}
jit_block *
jit_block::maybe_merge (void)
{
if (successor_count () == 1 && successor (0) != this
&& (successor (0)->use_count () == 1 || m_instructions.size () == 1))
{
jit_block *to_merge = successor (0);
merge (*to_merge);
return to_merge;
}
return 0;
}
void
jit_block::merge (jit_block& block)
{
// the merge block will contain a new terminator
jit_terminator *old_term = terminator ();
if (old_term)
old_term->remove ();
bool was_empty = end () == begin ();
auto merge_begin = end ();
if (! was_empty)
--merge_begin;
m_instructions.splice (end (), block.m_instructions);
if (was_empty)
merge_begin = begin ();
else
++merge_begin;
// now merge_begin points to the start of the new instructions, we must
// update their parent information
for (auto iter = merge_begin; iter != end (); ++iter)
{
jit_instruction *instr = *iter;
instr->stash_parent (this, iter);
}
block.replace_with (this);
}
jit_instruction *
jit_block::prepend (jit_instruction *instr)
{
m_instructions.push_front (instr);
instr->stash_parent (this, m_instructions.begin ());
return instr;
}
jit_instruction *
jit_block::prepend_after_phi (jit_instruction *instr)
{
// FIXME: Make this O(1)
for (auto iter = begin (); iter != end (); ++iter)
{
jit_instruction *temp = *iter;
if (! isa<jit_phi> (temp))
{
insert_before (iter, instr);
return instr;
}
}
return append (instr);
}
void
jit_block::internal_append (jit_instruction *instr)
{
m_instructions.push_back (instr);
instr->stash_parent (this, --m_instructions.end ());
}
jit_instruction *
jit_block::insert_before (iterator loc, jit_instruction *instr)
{
auto iloc = m_instructions.insert (loc, instr);
instr->stash_parent (this, iloc);
return instr;
}
jit_instruction *
jit_block::insert_after (iterator loc, jit_instruction *instr)
{
++loc;
auto iloc = m_instructions.insert (loc, instr);
instr->stash_parent (this, iloc);
return instr;
}
jit_terminator *
jit_block::terminator (void) const
{
if (m_instructions.empty ())
return nullptr;
jit_instruction *last = m_instructions.back ();
return dynamic_cast<jit_terminator *> (last);
}
bool
jit_block::branch_alive (jit_block *asucc) const
{
return terminator ()->alive (asucc);
}
jit_block *
jit_block::successor (size_t i) const
{
jit_terminator *term = terminator ();
return term->successor (i);
}
size_t
jit_block::successor_count (void) const
{
jit_terminator *term = terminator ();
return term ? term->successor_count () : 0;
}
llvm::BasicBlock *
jit_block::to_llvm (void) const
{
return llvm::cast<llvm::BasicBlock> (m_llvm_value);
}
std::ostream&
jit_block::print_dom (std::ostream& os) const
{
short_print (os);
os << ":\n";
os << " m_id: " << m_id << std::endl;
os << " predecessors: ";
for (jit_use *use = first_use (); use; use = use->next ())
os << *use->user_parent () << ' ';
os << std::endl;
os << " successors: ";
for (size_t i = 0; i < successor_count (); ++i)
os << *successor (i) << ' ';
os << std::endl;
os << " m_idom: ";
if (m_idom)
os << *m_idom;
else
os << "NULL";
os << std::endl;
os << " df: ";
for (auto iter = df_begin (); iter != df_end (); ++iter)
os << **iter << ' ';
os << std::endl;
os << " m_dom_succ: ";
for (size_t i = 0; i < m_dom_succ.size (); ++i)
os << *m_dom_succ[i] << ' ';
return os << std::endl;
}
void
jit_block::compute_df (size_t avisit_count)
{
if (visited (avisit_count))
return;
if (use_count () >= 2)
{
for (jit_use *use = first_use (); use; use = use->next ())
{
jit_block *runner = use->user_parent ();
while (runner != m_idom)
{
runner->m_df.insert (this);
runner = runner->m_idom;
}
}
}
for (size_t i = 0; i < successor_count (); ++i)
successor (i)->compute_df (avisit_count);
}
bool
jit_block::update_idom (size_t avisit_count)
{
if (visited (avisit_count) || ! use_count ())
return false;
bool changed = false;
for (jit_use *use = first_use (); use; use = use->next ())
{
jit_block *pred = use->user_parent ();
changed = pred->update_idom (avisit_count) || changed;
}
jit_use *use = first_use ();
jit_block *new_idom = use->user_parent ();
use = use->next ();
for (; use; use = use->next ())
{
jit_block *pred = use->user_parent ();
jit_block *pidom = pred->m_idom;
if (pidom)
new_idom = idom_intersect (pidom, new_idom);
}
if (m_idom != new_idom)
{
m_idom = new_idom;
return true;
}
return changed;
}
void
jit_block::label (size_t avisit_count, size_t& number)
{
if (visited (avisit_count))
return;
for (jit_use *use = first_use (); use; use = use->next ())
{
jit_block *pred = use->user_parent ();
pred->label (avisit_count, number);
}
m_id = number++;
}
void
jit_block::pop_all (void)
{
for (auto iter = begin (); iter != end (); ++iter)
{
jit_instruction *instr = *iter;
instr->pop_variable ();
}
}
std::ostream&
jit_block::print (std::ostream& os, size_t indent) const
{
print_indent (os, indent);
short_print (os) << ": %pred = ";
for (jit_use *use = first_use (); use; use = use->next ())
{
jit_block *pred = use->user_parent ();
os << *pred;
if (use->next ())
os << ", ";
}
os << std::endl;
for (auto iter = begin (); iter != end (); ++iter)
{
jit_instruction *instr = *iter;
instr->print (os, indent + 1) << std::endl;
}
return os;
}
jit_block *
jit_block::maybe_split (jit_factory& factory, jit_block_list& blocks,
jit_block *asuccessor)
{
if (successor_count () > 1)
{
jit_terminator *term = terminator ();
size_t idx = term->successor_index (asuccessor);
jit_block *split = factory.create<jit_block> ("phi_split", m_visit_count);
// place after this to ensure define before use in the blocks list
blocks.insert_after (this, split);
term->stash_argument (idx, split);
jit_branch *br = split->append (factory.create<jit_branch> (asuccessor));
replace_in_phi (asuccessor, split);
if (alive ())
{
split->mark_alive ();
br->infer ();
}
return split;
}
return this;
}
void
jit_block::create_dom_tree (size_t avisit_count)
{
if (visited (avisit_count))
return;
if (m_idom != this)
m_idom->m_dom_succ.push_back (this);
for (size_t i = 0; i < successor_count (); ++i)
successor (i)->create_dom_tree (avisit_count);
}
jit_block *
jit_block::idom_intersect (jit_block *i, jit_block *j)
{
while (i && j && i != j)
{
while (i && i->id () > j->id ())
i = i->m_idom;
while (i && j && j->id () > i->id ())
j = j->m_idom;
}
return i ? i : j;
}
// -------------------- jit_phi_incoming --------------------
jit_block *
jit_phi_incoming::user_parent (void) const
{ return m_user->parent (); }
// -------------------- jit_phi --------------------
bool
jit_phi::prune (void)
{
jit_block *p = parent ();
size_t new_idx = 0;
jit_value *unique = argument (1);
for (size_t i = 0; i < argument_count (); ++i)
{
jit_block *inc = incoming (i);
if (inc->branch_alive (p))
{
if (unique != argument (i))
unique = 0;
if (new_idx != i)
{
stash_argument (new_idx, argument (i));
m_incoming[new_idx].stash_value (inc);
}
++new_idx;
}
}
if (new_idx != argument_count ())
{
resize_arguments (new_idx);
m_incoming.resize (new_idx);
}
assert (argument_count () > 0);
if (unique)
{
replace_with (unique);
return true;
}
return false;
}
bool
jit_phi::infer (void)
{
jit_block *p = parent ();
if (! p->alive ())
return false;
jit_type *infered = nullptr;
for (size_t i = 0; i < argument_count (); ++i)
{
jit_block *inc = incoming (i);
if (inc->branch_alive (p))
infered = jit_type_join (infered, argument_type (i));
}
if (infered != type ())
{
stash_type (infered);
return true;
}
return false;
}
llvm::PHINode *
jit_phi::to_llvm (void) const
{
return llvm::cast<llvm::PHINode> (jit_value::to_llvm ());
}
// -------------------- jit_terminator --------------------
size_t
jit_terminator::successor_index (const jit_block *asuccessor) const
{
size_t scount = successor_count ();
for (size_t i = 0; i < scount; ++i)
if (successor (i) == asuccessor)
return i;
panic_impossible ();
}
bool
jit_terminator::infer (void)
{
if (! parent ()->alive ())
return false;
bool changed = false;
for (size_t i = 0; i < m_alive.size (); ++i)
if (! m_alive[i] && check_alive (i))
{
changed = true;
m_alive[i] = true;
successor (i)->mark_alive ();
}
return changed;
}
llvm::TerminatorInst *
jit_terminator::to_llvm (void) const
{
return llvm::cast<llvm::TerminatorInst> (jit_value::to_llvm ());
}
// -------------------- jit_call --------------------
bool
jit_call::needs_release (void) const
{
if (type () && jit_typeinfo::get_release (type ()).valid ())
{
for (jit_use *use = first_use (); use; use = use->next ())
{
jit_assign *assign = dynamic_cast<jit_assign *> (use->user ());
if (assign && assign->artificial ())
return false;
}
return true;
}
return false;
}
bool
jit_call::infer (void)
{
// FIXME: explain algorithm
for (size_t i = 0; i < argument_count (); ++i)
{
m_already_infered[i] = argument_type (i);
if (! m_already_infered[i])
return false;
}
jit_type *infered = m_operation.result (m_already_infered);
if (! infered && use_count ())
{
std::stringstream ss;
ss << "Missing overload in type inference for ";
print (ss, 0);
throw jit_fail_exception (ss.str ());
}
if (infered != type ())
{
stash_type (infered);
return true;
}
return false;
}
// -------------------- jit_error_check --------------------
std::string
jit_error_check::variable_to_string (variable v)
{
switch (v)
{
case var_error_state:
return "error_state";
case var_interrupt:
return "interrupt";
default:
panic_impossible ();
}
}
std::ostream&
jit_error_check::print (std::ostream& os, size_t indent) const
{
print_indent (os, indent) << "error_check " << variable_to_string (m_variable)
<< ", ";
if (has_check_for ())
os << "<for> " << *check_for () << ", ";
print_successor (os << "<normal> ", 1) << ", ";
return print_successor (os << "<error> ", 0);
}
// -------------------- jit_magic_end --------------------
jit_magic_end::context::context (jit_factory& factory, jit_value *avalue,
size_t aindex, size_t acount)
: m_value (avalue), m_index (factory.create<jit_const_index> (aindex)),
m_count (factory.create<jit_const_index> (acount))
{ }
jit_magic_end::jit_magic_end (const std::vector<context>& full_context)
: m_contexts (full_context)
{
resize_arguments (m_contexts.size ());
size_t i;
std::vector<context>::const_iterator iter;
for (iter = m_contexts.begin (), i = 0; iter != m_contexts.end (); ++iter, ++i)
stash_argument (i, iter->m_value);
}
jit_magic_end::context
jit_magic_end::resolve_context (void) const
{
size_t idx;
for (idx = 0; idx < m_contexts.size (); ++idx)
{
jit_type *ctx_type = m_contexts[idx].m_value->type ();
if (! ctx_type || ctx_type->skip_paren ())
break;
}
if (idx >= m_contexts.size ())
idx = 0;
context ret = m_contexts[idx];
ret.m_value = argument (idx);
return ret;
}
bool
jit_magic_end::infer (void)
{
jit_type *new_type = overload ().result ();
if (new_type != type ())
{
stash_type (new_type);
return true;
}
return false;
}
std::ostream&
jit_magic_end::print (std::ostream& os, size_t indent) const
{
context ctx = resolve_context ();
short_print (print_indent (os, indent)) << " (" << *ctx.m_value << ", ";
return os << *ctx.m_index << ", " << *ctx.m_count << ')';
}
const jit_function&
jit_magic_end::overload (void) const
{
const context& ctx = resolve_context ();
return jit_typeinfo::end (ctx.m_value, ctx.m_index, ctx.m_count);
}
}
#endif
|