Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Compiler_Dataflow.H
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
51#ifndef COMPILER_DATAFLOW_H
52#define COMPILER_DATAFLOW_H
53
54#include <cerrno>
55#include <climits>
56#include <cstdlib>
57#include <sstream>
58#include <string>
59#include <utility>
60
61#include <Compiler_IR_Model.H>
62#include <ah-errors.H>
63#include <tpl_dynArray.H>
64
65namespace Aleph
66{
74 {
76
84 void resize(const size_t count, const bool value = false)
85 {
86 bits.clear();
87 for (size_t i = 0; i < count; ++i)
88 bits.append(value ? 1u : 0u);
89 }
90
96 size_t size() const noexcept
97 {
98 return bits.size();
99 }
100
108 bool test(const size_t index) const noexcept
109 {
110 return index < bits.size() and bits.access(index) != 0;
111 }
112
121 bool set(const size_t index, const bool value = true)
122 {
124 << "Compiler_Dataflow_Bit_Set::set(): index out of bounds";
125
126 const auto encoded = static_cast<unsigned char>(value ? 1u : 0u);
127 if (bits.access(index) == encoded)
128 return false;
129 bits.access(index) = encoded;
130 return true;
131 }
132 };
133
136 {
137 Unknown,
138 Undefined,
139 Integer,
140 Boolean,
141 Unit
142 };
143
145 inline const char *
147 {
148 switch (kind)
149 {
151 return "Unknown";
153 return "Undefined";
155 return "Integer";
157 return "Boolean";
159 return "Unit";
160 }
161
162 return "Unknown";
163 }
164
181
190
205
213
222
223 namespace Compiler_Dataflow_Detail
224 {
227 {
228 return {};
229 }
230
239
241 integer_constant(const long long value)
242 {
245 constant.integer_value = value;
246 constant.text = std::to_string(value);
247 return constant;
248 }
249
252 {
255 constant.bool_value = value;
256 constant.text = value ? "true" : "false";
257 return constant;
258 }
259
262 {
265 constant.text = "unit";
266 return constant;
267 }
268
269 inline bool
271 const Compiler_Dataflow_Constant & rhs) noexcept
272 {
273 if (lhs.kind != rhs.kind)
274 return false;
275
276 switch (lhs.kind)
277 {
279 return lhs.integer_value == rhs.integer_value;
281 return lhs.bool_value == rhs.bool_value;
285 return true;
286 }
287
288 return false;
289 }
290
291 inline std::string
293 {
294 switch (constant.kind)
295 {
297 return "?";
299 return "undef";
301 return constant.text.empty() ? std::to_string(constant.integer_value)
302 : constant.text;
304 return constant.bool_value ? "true" : "false";
306 return "unit";
307 }
308
309 return "?";
310 }
311
312 inline bool
313 parse_integer_constant(const std::string & text,
314 long long & value) noexcept
315 {
316 if (text.empty())
317 return false;
318
319 char * end = nullptr;
320 errno = 0;
321 const long long result = std::strtoll(text.c_str(), &end, 10);
322 if (end == text.c_str() or *end != '\0')
323 return false;
324 if (errno == ERANGE)
325 return false;
326 value = result;
327 return true;
328 }
329
332 {
334 return unknown_constant();
335
336 if (inst.text == "unit")
337 return unit_constant();
338 if (inst.text == "true")
339 return boolean_constant(true);
340 if (inst.text == "false")
341 return boolean_constant(false);
342
343 long long value = 0;
345 return integer_constant(value);
346
347 return unknown_constant();
348 }
349
367
369 make_bit_set(const size_t count, const bool value = false)
370 {
372 set.resize(count, value);
373 return set;
374 }
375
379 {
381 for (size_t i = 0; i < count; ++i)
382 values.append(fill);
383 return values;
384 }
385
386 inline bool
388 const Compiler_Dataflow_Bit_Set & rhs) noexcept
389 {
390 if (lhs.size() != rhs.size())
391 return false;
392
393 for (size_t i = 0; i < lhs.size(); ++i)
394 if (lhs.bits.access(i) != rhs.bits.access(i))
395 return false;
396 return true;
397 }
398
399 inline bool
401 const DynArray<Compiler_Dataflow_Constant> & rhs) noexcept
402 {
403 if (lhs.size() != rhs.size())
404 return false;
405
406 for (size_t i = 0; i < lhs.size(); ++i)
407 if (not same_constant(lhs.access(i), rhs.access(i)))
408 return false;
409 return true;
410 }
411
414 const Compiler_Dataflow_Bit_Set & rhs)
415 {
416 ah_runtime_error_unless(lhs.size() == rhs.size())
417 << "Compiler_Dataflow: union requires sets with the same size";
418
419 auto result = make_bit_set(lhs.size(), false);
420 for (size_t i = 0; i < lhs.size(); ++i)
421 result.bits.access(i) = static_cast<unsigned char>(lhs.test(i) or rhs.test(i));
422 return result;
423 }
424
427 const Compiler_Dataflow_Bit_Set & rhs)
428 {
429 ah_runtime_error_unless(lhs.size() == rhs.size())
430 << "Compiler_Dataflow: intersection requires sets with the same size";
431
432 auto result = make_bit_set(lhs.size(), false);
433 for (size_t i = 0; i < lhs.size(); ++i)
434 result.bits.access(i) = static_cast<unsigned char>(lhs.test(i) and rhs.test(i));
435 return result;
436 }
437
440 const Compiler_Dataflow_Bit_Set & rhs)
441 {
442 ah_runtime_error_unless(lhs.size() == rhs.size())
443 << "Compiler_Dataflow: subtraction requires sets with the same size";
444
445 auto result = make_bit_set(lhs.size(), false);
446 for (size_t i = 0; i < lhs.size(); ++i)
447 result.bits.access(i) = static_cast<unsigned char>(lhs.test(i) and not rhs.test(i));
448 return result;
449 }
450
453 {
454 auto assigned = make_bit_set(function.local_slots.size(), false);
455 for (size_t i = 0; i < function.local_slots.size(); ++i)
456 if (function.local_slots.access(i).kind == Compiler_IR_Slot_Kind::Parameter)
457 assigned.set(i, true);
458 return assigned;
459 }
460
463 {
464 auto constants = make_constant_vector(function.local_slots.size(),
466 for (size_t i = 0; i < function.local_slots.size(); ++i)
467 if (function.local_slots.access(i).kind == Compiler_IR_Slot_Kind::Parameter)
468 constants.access(i) = unknown_constant();
469 return constants;
470 }
471
474 const Compiler_IR_Value_Id id)
475 {
476 if (id == 0 or id >= values.size())
477 return unknown_constant();
478 return values.access(id);
479 }
480
481 inline void
483 const Compiler_IR_Value_Id id)
484 {
485 if (id != 0 and id < used_values.size())
486 used_values.access(id) = 1u;
487 }
488
489 inline bool
491 const Compiler_IR_Value_Id id) noexcept
492 {
493 return id != 0 and id < used_values.size() and used_values.access(id) != 0;
494 }
495
505 inline bool
507 {
509 return inst.op == Compiler_Operator_Kind::Minus;
510
512 return false;
513
514 switch (inst.op)
515 {
521 return true;
522 default:
523 return false;
524 }
525 }
526
527 // arithmetic that can stop the program stays even if its value is dead,
528 // as in the engines without the optimizer
529 inline bool
531 {
536 and not can_stop_program(inst))
538 }
539
542 const Compiler_Dataflow_Constant & operand)
543 {
544 switch (op)
545 {
548 return integer_constant(operand.integer_value);
549 break;
550
553 {
554 if (operand.integer_value == LLONG_MIN)
555 break; // -LLONG_MIN overflows; do not fold
556 return integer_constant(-operand.integer_value);
557 }
558 break;
559
562 return boolean_constant(not operand.bool_value);
563 break;
564
565 default:
566 break;
567 }
568
569 return unknown_constant();
570 }
571
574 const Compiler_Dataflow_Constant & lhs,
575 const Compiler_Dataflow_Constant & rhs)
576 {
579 switch (op)
580 {
582 {
583 long long result = 0;
584#if defined(__has_builtin) && __has_builtin(__builtin_add_overflow)
585 if (__builtin_add_overflow(lhs.integer_value, rhs.integer_value, &result))
586 return unknown_constant();
587#else
588 // Portable signed-addition overflow check via unsigned arithmetic.
589 const auto ul = static_cast<unsigned long long>(lhs.integer_value);
590 const auto ur = static_cast<unsigned long long>(rhs.integer_value);
591 result = static_cast<long long>(ul + ur);
592 if ((lhs.integer_value > 0 and rhs.integer_value > 0 and result < 0)
593 or (lhs.integer_value < 0 and rhs.integer_value < 0 and result >= 0))
594 return unknown_constant();
595#endif
596 return integer_constant(result);
597 }
599 {
600 long long result = 0;
601#if defined(__has_builtin) && __has_builtin(__builtin_sub_overflow)
602 if (__builtin_sub_overflow(lhs.integer_value, rhs.integer_value, &result))
603 return unknown_constant();
604#else
605 // Portable signed-subtraction overflow check via unsigned arithmetic.
606 const auto ul = static_cast<unsigned long long>(lhs.integer_value);
607 const auto ur = static_cast<unsigned long long>(rhs.integer_value);
608 result = static_cast<long long>(ul - ur);
609 if ((rhs.integer_value < 0 and lhs.integer_value > 0 and result < 0)
610 or (rhs.integer_value > 0 and lhs.integer_value < 0 and result >= 0))
611 return unknown_constant();
612#endif
613 return integer_constant(result);
614 }
616 {
617 long long result = 0;
618#if defined(__has_builtin) && __has_builtin(__builtin_mul_overflow)
619 if (__builtin_mul_overflow(lhs.integer_value, rhs.integer_value, &result))
620 return unknown_constant();
621#else
622 // Portable signed-multiplication overflow check.
623 if (lhs.integer_value == 0 or rhs.integer_value == 0)
624 {
625 result = 0;
626 }
627 else
628 {
629 const auto ul = static_cast<unsigned long long>(
630 lhs.integer_value < 0 ? -(unsigned long long)lhs.integer_value
631 : (unsigned long long)lhs.integer_value);
632 const auto ur = static_cast<unsigned long long>(
633 rhs.integer_value < 0 ? -(unsigned long long)rhs.integer_value
634 : (unsigned long long)rhs.integer_value);
635 const unsigned long long uresult = ul * ur;
636 const bool neg = (lhs.integer_value < 0) != (rhs.integer_value < 0);
637 if (ul != 0 and uresult / ul != ur)
638 return unknown_constant();
639 if (neg and uresult > static_cast<unsigned long long>(LLONG_MAX) + 1ULL)
640 return unknown_constant();
641 if (not neg and uresult > static_cast<unsigned long long>(LLONG_MAX))
642 return unknown_constant();
643 if (neg and uresult == static_cast<unsigned long long>(LLONG_MAX) + 1ULL)
644 result = LLONG_MIN;
645 else
646 result = neg ? -static_cast<long long>(uresult)
647 : static_cast<long long>(uresult);
648 }
649#endif
650 return integer_constant(result);
651 }
653 if (rhs.integer_value == 0)
654 return unknown_constant();
655 if (lhs.integer_value == LLONG_MIN and rhs.integer_value == -1)
656 return unknown_constant();
659 if (rhs.integer_value == 0)
660 return unknown_constant();
661 if (lhs.integer_value == LLONG_MIN and rhs.integer_value == -1)
662 return unknown_constant();
676 default:
677 break;
678 }
679
682 switch (op)
683 {
687 return boolean_constant(lhs.bool_value or rhs.bool_value);
689 return boolean_constant(lhs.bool_value == rhs.bool_value);
691 return boolean_constant(lhs.bool_value != rhs.bool_value);
692 default:
693 break;
694 }
695
698 switch (op)
699 {
701 return boolean_constant(true);
703 return boolean_constant(false);
704 default:
705 break;
706 }
707
708 return unknown_constant();
709 }
710
716
717 inline Block_Use_Def
719 const Compiler_IR_Block & block)
720 {
721 Block_Use_Def info;
722 info.use = make_bit_set(function.local_slots.size(), false);
723 info.def = make_bit_set(function.local_slots.size(), false);
724
725 for (size_t i = 0; i < block.instructions.size(); ++i)
726 {
727 const auto & inst = block.instructions.access(i);
729 and inst.local_slot_id != compiler_ir_invalid_id()
730 and inst.local_slot_id < function.local_slots.size()
731 and not info.def.test(inst.local_slot_id))
732 info.use.set(inst.local_slot_id, true);
733
735 and inst.local_slot_id != compiler_ir_invalid_id()
736 and inst.local_slot_id < function.local_slots.size())
737 info.def.set(inst.local_slot_id, true);
738 }
739
740 return info;
741 }
742
743 inline DynArray<bool>
745 {
746 DynArray<bool> reachable;
747 for (size_t i = 0; i < function.blocks.size(); ++i)
748 reachable.append(false);
749
750 if (function.blocks.is_empty() or function.entry_block >= function.blocks.size())
751 return reachable;
752
754 worklist.append(function.entry_block);
755 while (not worklist.is_empty())
756 {
757 const auto current = worklist.pop();
758 if (current >= function.blocks.size() or reachable.access(current))
759 continue;
760
761 reachable.access(current) = true;
762 const auto & block = function.blocks.access(current);
763 for (size_t i = 0; i < block.terminator.successors.size(); ++i)
764 {
765 const auto succ = block.terminator.successors.access(i);
766 if (succ < function.blocks.size() and not reachable.access(succ))
767 worklist.append(succ);
768 }
769 }
770
771 return reachable;
772 }
773
781
782 inline Block_Simulation
784 const Compiler_IR_Block & block,
785 const Compiler_Dataflow_Bit_Set & assigned_in,
786 const DynArray<Compiler_Dataflow_Constant> & constants_in,
788 {
789 Block_Simulation simulation;
790 simulation.assigned_out = assigned_in;
791 simulation.constant_out = constants_in;
792 simulation.value_constants = make_constant_vector(function.next_value_id + 1,
794 simulation.branch_constant = unknown_constant();
795
796 for (size_t i = 0; i < block.instructions.size(); ++i)
797 {
798 const auto & inst = block.instructions.access(i);
800
801 switch (inst.kind)
802 {
804 result = constant_from_instruction(inst);
805 break;
806
808 if (inst.local_slot_id != compiler_ir_invalid_id()
809 and inst.local_slot_id < simulation.constant_out.size())
810 {
811 if (not simulation.assigned_out.test(inst.local_slot_id)
812 and findings != nullptr)
813 findings->append({block.id, i, inst.local_slot_id, inst.span});
814
815 result = simulation.assigned_out.test(inst.local_slot_id)
816 ? simulation.constant_out.access(inst.local_slot_id)
818 }
819 else
820 result = unknown_constant();
821 break;
822
824 result = evaluate_unary(inst.op,
826 inst.operands.is_empty()
827 ? 0
828 : inst.operands.access(0)));
829 break;
830
832 result = evaluate_binary(inst.op,
834 inst.operands.size() > 0
835 ? inst.operands.access(0)
836 : 0),
838 inst.operands.size() > 1
839 ? inst.operands.access(1)
840 : 0));
841 break;
842
845 result = unknown_constant();
846 break;
847
849 if (inst.local_slot_id != compiler_ir_invalid_id()
850 and inst.local_slot_id < simulation.constant_out.size())
851 {
852 simulation.assigned_out.set(inst.local_slot_id, true);
853 simulation.constant_out.access(inst.local_slot_id) =
855 inst.operands.is_empty()
856 ? 0
857 : inst.operands.access(0));
858 }
859 continue;
860 }
861
862 if (inst.result_id != 0 and inst.result_id < simulation.value_constants.size())
863 simulation.value_constants.access(inst.result_id) = result;
864 }
865
867 simulation.branch_constant =
870
871 return simulation;
872 }
873
874 inline std::string
876 const Compiler_Dataflow_Bit_Set & set)
877 {
878 std::ostringstream out;
879 bool first = true;
880 for (size_t i = 0; i < set.size(); ++i)
881 if (set.test(i))
882 {
883 if (not first)
884 out << ", ";
885 out << Compiler_IR_Detail::local_slot_name(function.local_slots.access(i).id);
886 first = false;
887 }
888
889 return first ? "<none>" : out.str();
890 }
891
892 inline std::string
894 const DynArray<Compiler_Dataflow_Constant> & constants)
895 {
896 std::ostringstream out;
897 for (size_t i = 0; i < constants.size(); ++i)
898 {
899 if (i > 0)
900 out << ", ";
901 out << Compiler_IR_Detail::local_slot_name(function.local_slots.access(i).id)
902 << '=' << constant_to_string(constants.access(i));
903 }
904
905 return constants.is_empty() ? "<none>" : out.str();
906 }
907
908 inline void
910 {
911 for (size_t i = 0; i < function.blocks.size(); ++i)
912 function.blocks.access(i).predecessors.clear();
913
914 for (size_t i = 0; i < function.blocks.size(); ++i)
915 for (size_t j = 0; j < function.blocks.access(i).terminator.successors.size(); ++j)
916 {
917 const auto succ = function.blocks.access(i).terminator.successors.access(j);
918 if (succ < function.blocks.size())
919 function.blocks.access(succ).predecessors.append(i);
920 }
921 }
922
925 {
927 for (size_t i = 0; i < function.blocks.size(); ++i)
929
930 for (size_t i = 0; i < function.blocks.size(); ++i)
931 for (size_t j = 0; j < function.blocks.access(i).terminator.successors.size(); ++j)
932 {
933 const auto succ = function.blocks.access(i).terminator.successors.access(j);
934 if (succ < function.blocks.size())
935 predecessors.access(succ).append(i);
936 }
937
938 return predecessors;
939 }
940
941 inline size_t
943 {
944 size_t total = 0;
945 for (size_t i = 0; i < function.blocks.size(); ++i)
946 total += function.blocks.access(i).instructions.size();
947 return total;
948 }
949
950 inline bool
952 const Compiler_Dataflow_Bit_Set & live_after,
953 const DynArray<unsigned char> & used_values,
954 const Compiler_IR_Function & function) noexcept
955 {
957 and inst.local_slot_id != compiler_ir_invalid_id()
958 and inst.local_slot_id < function.local_slots.size())
959 return not live_after.test(inst.local_slot_id);
960
961 if (is_pure_instruction(inst) and inst.result_id != 0)
962 return not is_value_used(used_values, inst.result_id);
963
964 return false;
965 }
966 }
967
979 {
981 analysis.local_slot_count = function.local_slots.size();
982
983 const auto reachable = Compiler_Dataflow_Detail::compute_reachability(function);
984 const auto predecessors =
986 analysis.reachable_blocks = reachable;
987
989 for (size_t i = 0; i < function.blocks.size(); ++i)
991 function.blocks.access(i)));
992
993 for (size_t i = 0; i < function.blocks.size(); ++i)
994 {
995 analysis.live_in_slots.append(Compiler_Dataflow_Detail::make_bit_set(function.local_slots.size(), false));
996 analysis.live_out_slots.append(Compiler_Dataflow_Detail::make_bit_set(function.local_slots.size(), false));
997 analysis.assigned_in_slots.append(Compiler_Dataflow_Detail::make_bit_set(function.local_slots.size(), false));
998 analysis.assigned_out_slots.append(Compiler_Dataflow_Detail::make_bit_set(function.local_slots.size(), false));
1003 }
1004
1005 bool changed = true;
1006 while (changed)
1007 {
1008 changed = false;
1009 for (size_t index = function.blocks.size(); index > 0; --index)
1010 {
1011 const auto block_id = index - 1;
1012 if (block_id >= reachable.size() or not reachable.access(block_id))
1013 continue;
1014
1015 auto live_out = Compiler_Dataflow_Detail::make_bit_set(function.local_slots.size(), false);
1016 const auto & block = function.blocks.access(block_id);
1017 for (size_t i = 0; i < block.terminator.successors.size(); ++i)
1018 {
1019 const auto succ = block.terminator.successors.access(i);
1020 if (succ >= function.blocks.size() or not reachable.access(succ))
1021 continue;
1022 live_out = Compiler_Dataflow_Detail::bit_set_union(live_out,
1023 analysis.live_in_slots.access(succ));
1024 }
1025
1026 const auto live_in =
1027 Compiler_Dataflow_Detail::bit_set_union(block_sets.access(block_id).use,
1029 live_out,
1030 block_sets.access(block_id).def));
1031
1033 analysis.live_out_slots.access(block_id)))
1034 {
1035 analysis.live_out_slots.access(block_id) = live_out;
1036 changed = true;
1037 }
1039 analysis.live_in_slots.access(block_id)))
1040 {
1041 analysis.live_in_slots.access(block_id) = live_in;
1042 changed = true;
1043 }
1044 }
1045 }
1046
1047 const auto initial_assigned = Compiler_Dataflow_Detail::initial_assigned_state(function);
1048 changed = true;
1049 while (changed)
1050 {
1051 changed = false;
1052 for (size_t block_id = 0; block_id < function.blocks.size(); ++block_id)
1053 {
1054 if (block_id >= reachable.size() or not reachable.access(block_id))
1055 continue;
1056
1057 Compiler_Dataflow_Bit_Set assigned_in;
1058 if (block_id == function.entry_block)
1059 assigned_in = initial_assigned;
1060 else
1061 {
1062 bool have_predecessor = false;
1063 assigned_in = Compiler_Dataflow_Detail::make_bit_set(function.local_slots.size(), true);
1064 const auto & block_predecessors = predecessors.access(block_id);
1065 for (size_t i = 0; i < block_predecessors.size(); ++i)
1066 {
1067 const auto pred = block_predecessors.access(i);
1068 if (pred >= function.blocks.size() or not reachable.access(pred))
1069 continue;
1070 if (not have_predecessor)
1071 {
1072 assigned_in = analysis.assigned_out_slots.access(pred);
1073 have_predecessor = true;
1074 }
1075 else
1077 assigned_in,
1078 analysis.assigned_out_slots.access(pred));
1079 }
1080
1081 if (not have_predecessor)
1082 assigned_in = Compiler_Dataflow_Detail::make_bit_set(function.local_slots.size(), false);
1083 }
1084
1085 auto simulation =
1087 function.blocks.access(block_id),
1088 assigned_in,
1090 function.local_slots.size(),
1092
1093 if (not Compiler_Dataflow_Detail::bit_set_equals(assigned_in,
1094 analysis.assigned_in_slots.access(block_id)))
1095 {
1096 analysis.assigned_in_slots.access(block_id) = assigned_in;
1097 changed = true;
1098 }
1099 if (not Compiler_Dataflow_Detail::bit_set_equals(simulation.assigned_out,
1100 analysis.assigned_out_slots.access(block_id)))
1101 {
1102 analysis.assigned_out_slots.access(block_id) = simulation.assigned_out;
1103 changed = true;
1104 }
1105 }
1106 }
1107
1108 const auto initial_constants = Compiler_Dataflow_Detail::initial_constant_state(function);
1109 changed = true;
1110 while (changed)
1111 {
1112 changed = false;
1113 for (size_t block_id = 0; block_id < function.blocks.size(); ++block_id)
1114 {
1115 if (block_id >= reachable.size() or not reachable.access(block_id))
1116 continue;
1117
1119 if (block_id == function.entry_block)
1120 constant_in = initial_constants;
1121 else
1122 {
1123 bool have_predecessor = false;
1125 function.local_slots.size(),
1127
1128 const auto & block_predecessors = predecessors.access(block_id);
1129 for (size_t i = 0; i < block_predecessors.size(); ++i)
1130 {
1131 const auto pred = block_predecessors.access(i);
1132 if (pred >= function.blocks.size() or not reachable.access(pred))
1133 continue;
1134
1135 if (not have_predecessor)
1136 {
1137 constant_in = analysis.constant_out_slots.access(pred);
1138 have_predecessor = true;
1139 continue;
1140 }
1141
1142 for (size_t slot = 0; slot < constant_in.size(); ++slot)
1143 constant_in.access(slot) =
1145 analysis.constant_out_slots.access(pred).access(slot));
1146 }
1147
1148 if (not have_predecessor)
1150 function.local_slots.size(),
1152 }
1153
1154 const auto simulation =
1156 function.blocks.access(block_id),
1157 analysis.assigned_in_slots.access(block_id),
1158 constant_in);
1159
1161 analysis.constant_in_slots.access(block_id)))
1162 {
1163 analysis.constant_in_slots.access(block_id) = constant_in;
1164 changed = true;
1165 }
1166 if (not Compiler_Dataflow_Detail::constant_vector_equals(simulation.constant_out,
1167 analysis.constant_out_slots.access(block_id)))
1168 {
1169 analysis.constant_out_slots.access(block_id) = simulation.constant_out;
1170 changed = true;
1171 }
1172 }
1173 }
1174
1175 for (size_t block_id = 0; block_id < function.blocks.size(); ++block_id)
1176 {
1177 if (block_id >= reachable.size() or not reachable.access(block_id))
1178 continue;
1179
1181 const auto simulation =
1183 function.blocks.access(block_id),
1184 analysis.assigned_in_slots.access(block_id),
1185 analysis.constant_in_slots.access(block_id),
1186 &findings);
1187 for (size_t i = 0; i < findings.size(); ++i)
1188 analysis.uninitialized_reads.append(findings.access(i));
1189
1190 if (simulation.branch_constant.kind == Compiler_Dataflow_Constant_Kind::Boolean)
1191 ++analysis.foldable_branch_count;
1192 }
1193
1194 return analysis;
1195 }
1196
1207 const Compiler_Dataflow_Function_Analysis & analysis)
1208 {
1210
1211 auto fail = [&report](const std::string & message)
1212 {
1213 report.valid = false;
1214 report.errors.append(message);
1215 };
1216
1217 if (analysis.reachable_blocks.size() != function.blocks.size())
1218 fail("Dataflow analysis for '" + function.name
1219 + "' has mismatched reachability size");
1220 if (analysis.live_in_slots.size() != function.blocks.size()
1221 or analysis.live_out_slots.size() != function.blocks.size()
1222 or analysis.assigned_in_slots.size() != function.blocks.size()
1223 or analysis.assigned_out_slots.size() != function.blocks.size()
1224 or analysis.constant_in_slots.size() != function.blocks.size()
1225 or analysis.constant_out_slots.size() != function.blocks.size())
1226 fail("Dataflow analysis for '" + function.name
1227 + "' has mismatched per-block arrays");
1228
1229 if (not report.valid)
1230 return report;
1231
1232 for (size_t block_id = 0; block_id < function.blocks.size(); ++block_id)
1233 {
1234 const auto check_set = [&fail, &function, block_id](const char * label,
1235 const Compiler_Dataflow_Bit_Set & set)
1236 {
1237 if (set.size() != function.local_slots.size())
1238 fail(std::string("Dataflow ") + label + " for "
1240 + " in function '" + function.name + "' has wrong slot-domain size");
1241 };
1242
1243 check_set("live-in", analysis.live_in_slots.access(block_id));
1244 check_set("live-out", analysis.live_out_slots.access(block_id));
1245 check_set("assigned-in", analysis.assigned_in_slots.access(block_id));
1246 check_set("assigned-out", analysis.assigned_out_slots.access(block_id));
1247
1248 if (analysis.constant_in_slots.access(block_id).size() != function.local_slots.size())
1249 fail("Dataflow constant-in state for "
1251 + " in function '" + function.name + "' has wrong slot-domain size");
1252 if (analysis.constant_out_slots.access(block_id).size() != function.local_slots.size())
1253 fail("Dataflow constant-out state for "
1255 + " in function '" + function.name + "' has wrong slot-domain size");
1256 }
1257
1258 if (function.entry_block < analysis.reachable_blocks.size()
1259 and not analysis.reachable_blocks.access(function.entry_block))
1260 fail("Dataflow analysis for '" + function.name
1261 + "' does not mark the entry block as reachable");
1262
1263 for (size_t i = 0; i < analysis.uninitialized_reads.size(); ++i)
1264 {
1265 const auto & finding = analysis.uninitialized_reads.access(i);
1266 if (finding.block_id >= function.blocks.size())
1267 fail("Dataflow analysis for '" + function.name
1268 + "' contains an uninitialized-read finding with an invalid block id");
1269 else if (finding.instruction_index
1270 >= function.blocks.access(finding.block_id).instructions.size())
1271 fail("Dataflow analysis for '" + function.name
1272 + "' contains an uninitialized-read finding with an invalid instruction index");
1273
1274 if (finding.slot_id >= function.local_slots.size())
1275 fail("Dataflow analysis for '" + function.name
1276 + "' contains an uninitialized-read finding with an invalid slot id");
1277 }
1278
1279 return report;
1280 }
1281
1293 inline std::string
1295 const Compiler_Dataflow_Function_Analysis & analysis,
1296 const Compiler_Type_Context * types = nullptr)
1297 {
1298 std::ostringstream out;
1299 if (function == nullptr)
1300 {
1301 out << "<null-dataflow-function>\n";
1302 return out.str();
1303 }
1304
1305 out << "Dataflow(" << function->name << ")\n";
1306 if (function->local_slots.is_empty())
1307 out << " LocalSlots: <none>\n";
1308 else
1309 {
1310 out << " LocalSlots:\n";
1311 for (size_t i = 0; i < function->local_slots.size(); ++i)
1312 {
1313 const auto & slot = function->local_slots.access(i);
1314 out << " " << Compiler_IR_Detail::local_slot_name(slot.id)
1315 << " [" << compiler_ir_slot_kind_name(slot.kind) << "] "
1316 << slot.name;
1317 if (types != nullptr and slot.type_id != 0)
1318 out << ": " << types->to_string(slot.type_id);
1319 out << '\n';
1320 }
1321 }
1322
1323 out << " ReachableBlocks:";
1324 bool first = true;
1325 for (size_t i = 0; i < analysis.reachable_blocks.size(); ++i)
1326 if (analysis.reachable_blocks.access(i))
1327 {
1328 out << (first ? " " : ", ");
1330 first = false;
1331 }
1332 if (first)
1333 out << " <none>";
1334 out << '\n';
1335
1336 out << " FoldableBranches: " << analysis.foldable_branch_count << '\n';
1337 out << " UninitializedReads:";
1338 if (analysis.uninitialized_reads.is_empty())
1339 out << " <none>\n";
1340 else
1341 {
1342 out << '\n';
1343 for (size_t i = 0; i < analysis.uninitialized_reads.size(); ++i)
1344 {
1345 const auto & finding = analysis.uninitialized_reads.access(i);
1346 out << " " << Compiler_IR_Detail::block_name(finding.block_id)
1347 << "/I" << finding.instruction_index
1348 << " -> " << Compiler_IR_Detail::local_slot_name(finding.slot_id)
1349 << '\n';
1350 }
1351 }
1352
1353 for (size_t block_id = 0; block_id < function->blocks.size(); ++block_id)
1354 {
1355 const auto & block = function->blocks.access(block_id);
1356 out << " Block " << Compiler_IR_Detail::block_name(block_id)
1357 << " [" << block.label << "]\n";
1358 out << " Reachable: "
1359 << ((block_id < analysis.reachable_blocks.size()
1360 and analysis.reachable_blocks.access(block_id))
1361 ? "yes"
1362 : "no")
1363 << '\n';
1364 out << " LiveIn: "
1366 analysis.live_in_slots.access(block_id))
1367 << '\n';
1368 out << " LiveOut: "
1370 analysis.live_out_slots.access(block_id))
1371 << '\n';
1372 out << " AssignedIn: "
1374 analysis.assigned_in_slots.access(block_id))
1375 << '\n';
1376 out << " AssignedOut: "
1378 analysis.assigned_out_slots.access(block_id))
1379 << '\n';
1380 out << " ConstantsIn: "
1382 analysis.constant_in_slots.access(block_id))
1383 << '\n';
1384 out << " ConstantsOut: "
1386 analysis.constant_out_slots.access(block_id))
1387 << '\n';
1388 }
1389
1390 return out.str();
1391 }
1392
1410 {
1412
1413 const auto original_instruction_count =
1415
1416 Compiler_IR_Function provisional = function;
1417
1418 // Iterate to a fixpoint: repeat analysis+fold+DCE until a full pass
1419 // produces no new folded branches and no removed instructions/blocks.
1420 for (;;)
1421 {
1422 const auto analysis = analyze_dataflow_function(provisional);
1423 size_t pass_folded = 0;
1424 size_t pass_removed_instructions = 0;
1425
1426 for (size_t block_id = 0; block_id < provisional.blocks.size(); ++block_id)
1427 {
1428 provisional.blocks.access(block_id).predecessors.clear();
1429
1430 if (block_id >= analysis.reachable_blocks.size()
1431 or not analysis.reachable_blocks.access(block_id))
1432 continue;
1433
1434 const auto simulation =
1436 provisional.blocks.access(block_id),
1437 analysis.assigned_in_slots.access(block_id),
1438 analysis.constant_in_slots.access(block_id));
1439
1440 auto & block = provisional.blocks.access(block_id);
1441 DynArray<unsigned char> used_values;
1442 for (size_t i = 0; i <= provisional.next_value_id; ++i)
1443 used_values.append(0u);
1444
1445 auto live_slots = analysis.live_out_slots.access(block_id);
1446 auto terminator = block.terminator;
1447 if (terminator.kind == Compiler_IR_Terminator_Kind::Branch
1448 and simulation.branch_constant.kind == Compiler_Dataflow_Constant_Kind::Boolean
1449 and terminator.successors.size() == 2)
1450 {
1451 const auto chosen = simulation.branch_constant.bool_value
1452 ? terminator.successors.access(0)
1453 : terminator.successors.access(1);
1454 terminator.kind = Compiler_IR_Terminator_Kind::Jump;
1455 terminator.condition_value = 0;
1456 terminator.successors.clear();
1457 terminator.successors.append(chosen);
1458 ++pass_folded;
1459 ++result.folded_branches;
1460
1461 // Recompute live_slots from the post-fold successor so the DCE
1462 // pass below uses liveness that matches the new control flow.
1463 live_slots = Compiler_Dataflow_Detail::make_bit_set(provisional.local_slots.size(), false);
1464 if (chosen < analysis.live_in_slots.size())
1465 live_slots = analysis.live_in_slots.access(chosen);
1466 }
1467
1468 if (terminator.kind == Compiler_IR_Terminator_Kind::Branch)
1470 terminator.condition_value);
1471 else if (terminator.kind == Compiler_IR_Terminator_Kind::Return)
1473 terminator.return_value);
1474
1475 DynArray<unsigned char> keep_flags;
1476 for (size_t i = 0; i < block.instructions.size(); ++i)
1477 keep_flags.append(1u);
1478
1479 for (size_t index = block.instructions.size(); index > 0; --index)
1480 {
1481 const auto instruction_index = index - 1;
1482 const auto & inst = block.instructions.access(instruction_index);
1483
1484 bool keep = true;
1486 and inst.local_slot_id != compiler_ir_invalid_id()
1487 and inst.local_slot_id < provisional.local_slots.size()
1488 and not live_slots.test(inst.local_slot_id))
1489 keep = false;
1491 and inst.result_id != 0
1493 inst.result_id))
1494 keep = false;
1495
1496 keep_flags.access(instruction_index) = static_cast<unsigned char>(keep ? 1u : 0u);
1497 if (not keep)
1498 {
1499 ++pass_removed_instructions;
1500 continue;
1501 }
1502
1503 for (size_t operand = 0; operand < inst.operands.size(); ++operand)
1505 inst.operands.access(operand));
1506
1508 and inst.local_slot_id != compiler_ir_invalid_id()
1509 and inst.local_slot_id < provisional.local_slots.size())
1510 live_slots.set(inst.local_slot_id, false);
1511 else if (inst.kind == Compiler_IR_Instruction_Kind::Load
1512 and inst.local_slot_id != compiler_ir_invalid_id()
1513 and inst.local_slot_id < provisional.local_slots.size()
1514 and inst.result_id != 0)
1515 live_slots.set(inst.local_slot_id, true);
1516 }
1517
1519 for (size_t i = 0; i < block.instructions.size(); ++i)
1520 if (keep_flags.access(i) != 0)
1521 instructions.append(block.instructions.access(i));
1522 block.instructions = instructions;
1523 block.terminator = terminator;
1524 }
1525
1526 // Remove unreachable blocks created by this pass before the next iteration.
1527 const auto reachable_now =
1529 size_t pass_removed_blocks = 0;
1530 Compiler_IR_Function compacted;
1531 compacted = provisional;
1532 compacted.blocks.clear();
1534 for (size_t i = 0; i < provisional.blocks.size(); ++i)
1535 pass_remap.append(compiler_ir_invalid_id());
1536 for (size_t block_id = 0; block_id < provisional.blocks.size(); ++block_id)
1537 {
1538 const bool keep_exit_block = block_id == provisional.exit_block;
1539 if (not keep_exit_block
1540 and (block_id >= reachable_now.size() or not reachable_now.access(block_id)))
1541 {
1542 ++pass_removed_blocks;
1543 continue;
1544 }
1545 pass_remap.access(block_id) = compacted.blocks.size();
1546 auto blk = provisional.blocks.access(block_id);
1547 blk.id = compacted.blocks.size();
1548 blk.predecessors.clear();
1549 compacted.blocks.append(std::move(blk));
1550 }
1551 for (size_t block_id = 0; block_id < compacted.blocks.size(); ++block_id)
1552 {
1553 auto & blk = compacted.blocks.access(block_id);
1555 for (size_t i = 0; i < blk.terminator.successors.size(); ++i)
1556 {
1557 const auto old_s = blk.terminator.successors.access(i);
1558 if (old_s < pass_remap.size()
1559 and pass_remap.access(old_s) != compiler_ir_invalid_id())
1560 succs.append(pass_remap.access(old_s));
1561 }
1562 blk.terminator.successors = succs;
1563 }
1564 compacted.entry_block =
1565 provisional.entry_block < pass_remap.size()
1566 ? pass_remap.access(provisional.entry_block)
1568 compacted.exit_block =
1569 provisional.exit_block < pass_remap.size()
1570 ? pass_remap.access(provisional.exit_block)
1573 provisional = std::move(compacted);
1574
1575 if (pass_folded == 0 and pass_removed_instructions == 0 and pass_removed_blocks == 0)
1576 break;
1577 }
1578
1579 // The fixpoint loop already compacted provisional after every pass.
1580 result.function = std::move(provisional);
1581 result.removed_blocks = function.blocks.size() - result.function.blocks.size();
1582 result.removed_instructions =
1583 original_instruction_count - Compiler_Dataflow_Detail::count_instructions(result.function);
1584 return result;
1585 }
1586
1602 const Compiler_IR_Module * module = nullptr)
1603 {
1605
1606 auto fail = [&report](const std::string & message)
1607 {
1608 report.valid = false;
1609 report.errors.append(message);
1610 };
1611
1612 const auto ir_report = validate_ir_function(result.function, module);
1613 if (not ir_report.valid)
1614 {
1615 report.valid = false;
1616 for (size_t i = 0; i < ir_report.errors.size(); ++i)
1617 report.errors.append(ir_report.errors.access(i));
1618 }
1619
1620 const auto reachable = Compiler_Dataflow_Detail::compute_reachability(result.function);
1621 const auto expected_exit_unreachable_warning =
1622 result.function.exit_block < result.function.blocks.size()
1623 and result.function.exit_block < reachable.size()
1624 and not reachable.access(result.function.exit_block)
1625 ? "IR block " + Compiler_IR_Detail::block_name(result.function.exit_block)
1626 + " in function '" + result.function.name + "' is unreachable from entry"
1627 : std::string{};
1628
1629 for (size_t i = 0; i < ir_report.warnings.size(); ++i)
1630 if (ir_report.warnings.access(i).find("unreachable") != std::string::npos)
1631 {
1632 if (not expected_exit_unreachable_warning.empty()
1633 and ir_report.warnings.access(i) == expected_exit_unreachable_warning)
1634 continue;
1635
1636 fail("DCE result for '" + result.function.name
1637 + "' still contains unreachable blocks");
1638 }
1639 else
1640 report.warnings.append(ir_report.warnings.access(i));
1641
1642 const auto analysis = analyze_dataflow_function(result.function);
1643 if (analysis.foldable_branch_count != 0)
1644 fail("DCE result for '" + result.function.name
1645 + "' still contains foldable branches");
1646
1647 for (size_t block_id = 0; block_id < result.function.blocks.size(); ++block_id)
1648 {
1649 if (block_id >= analysis.reachable_blocks.size()
1650 or not analysis.reachable_blocks.access(block_id))
1651 continue;
1652
1653 const auto & block = result.function.blocks.access(block_id);
1654 auto live_slots = analysis.live_out_slots.access(block_id);
1655 DynArray<unsigned char> used_values;
1656 for (size_t i = 0; i <= result.function.next_value_id; ++i)
1657 used_values.append(0u);
1658
1664 block.terminator.return_value);
1665
1666 for (size_t index = block.instructions.size(); index > 0; --index)
1667 {
1668 const auto instruction_index = index - 1;
1669 const auto & inst = block.instructions.access(instruction_index);
1670
1672 live_slots,
1673 used_values,
1674 result.function))
1675 fail("DCE result for '" + result.function.name
1676 + "' still contains removable code in "
1678 + " at instruction #" + std::to_string(instruction_index));
1679
1680 for (size_t operand = 0; operand < inst.operands.size(); ++operand)
1682 inst.operands.access(operand));
1683
1685 and inst.local_slot_id != compiler_ir_invalid_id()
1686 and inst.local_slot_id < result.function.local_slots.size())
1687 live_slots.set(inst.local_slot_id, false);
1688 else if (inst.kind == Compiler_IR_Instruction_Kind::Load
1689 and inst.local_slot_id != compiler_ir_invalid_id()
1690 and inst.local_slot_id < result.function.local_slots.size()
1691 and inst.result_id != 0)
1692 live_slots.set(inst.local_slot_id, true);
1693 }
1694 }
1695
1696 return report;
1697 }
1698}
1699
1700#endif
Reusable explicit-value IR model, validation, and deterministic dumps.
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_unless(C)
Throws std::runtime_error if condition does NOT hold.
Definition ah-errors.H:399
size_t size_t int32_t value
Definition ca-c-api.h:116
size_t size_t int32_t * out
Definition ca-c-api.h:120
Context owning all compiler type nodes.
void clear() noexcept
Empties the container.
size_t size() const noexcept
Return the current dimension of array.
T & access(const size_t i) noexcept
Fast access without checking allocation and bound_min_clock checking.
T pop()
Remove the last item of array (as if this was a stack)
T & append()
Allocate a new entry to the end of array.
bool is_empty() const noexcept
Return true if the array is empty.
Freq_Node * pred
Predecessor node in level-order traversal.
Compiler_Dataflow_Bit_Set initial_assigned_state(const Compiler_IR_Function &function)
DynArray< bool > compute_reachability(const Compiler_IR_Function &function)
Compiler_Dataflow_Bit_Set bit_set_union(const Compiler_Dataflow_Bit_Set &lhs, const Compiler_Dataflow_Bit_Set &rhs)
std::string format_slot_set(const Compiler_IR_Function &function, const Compiler_Dataflow_Bit_Set &set)
Compiler_Dataflow_Bit_Set make_bit_set(const size_t count, const bool value=false)
void mark_value_used(DynArray< unsigned char > &used_values, const Compiler_IR_Value_Id id)
Compiler_Dataflow_Constant integer_constant(const long long value)
size_t count_instructions(const Compiler_IR_Function &function)
Compiler_Dataflow_Constant constant_from_instruction(const Compiler_IR_Instruction &inst)
DynArray< Compiler_Dataflow_Constant > make_constant_vector(const size_t count, const Compiler_Dataflow_Constant &fill)
Compiler_Dataflow_Constant unknown_constant()
Compiler_Dataflow_Constant evaluate_binary(const Compiler_Operator_Kind op, const Compiler_Dataflow_Constant &lhs, const Compiler_Dataflow_Constant &rhs)
Compiler_Dataflow_Bit_Set bit_set_subtract(const Compiler_Dataflow_Bit_Set &lhs, const Compiler_Dataflow_Bit_Set &rhs)
void rebuild_predecessors(Compiler_IR_Function &function)
std::string format_slot_constants(const Compiler_IR_Function &function, const DynArray< Compiler_Dataflow_Constant > &constants)
Compiler_Dataflow_Constant unit_constant()
Compiler_Dataflow_Constant undefined_constant()
Compiler_Dataflow_Constant evaluate_unary(const Compiler_Operator_Kind op, const Compiler_Dataflow_Constant &operand)
Block_Use_Def compute_block_use_def(const Compiler_IR_Function &function, const Compiler_IR_Block &block)
DynArray< Compiler_Dataflow_Constant > initial_constant_state(const Compiler_IR_Function &function)
bool can_stop_program(const Compiler_IR_Instruction &inst) noexcept
Returns whether inst is integer arithmetic that can stop the program.
bool is_pure_instruction(const Compiler_IR_Instruction &inst) noexcept
Block_Simulation simulate_block(const Compiler_IR_Function &function, const Compiler_IR_Block &block, const Compiler_Dataflow_Bit_Set &assigned_in, const DynArray< Compiler_Dataflow_Constant > &constants_in, DynArray< Compiler_Dataflow_Uninitialized_Read > *findings=nullptr)
Compiler_Dataflow_Constant meet_constants(const Compiler_Dataflow_Constant &lhs, const Compiler_Dataflow_Constant &rhs)
DynArray< DynArray< Compiler_IR_Block_Id > > compute_predecessor_lists(const Compiler_IR_Function &function)
bool instruction_is_trivially_dead(const Compiler_IR_Instruction &inst, const Compiler_Dataflow_Bit_Set &live_after, const DynArray< unsigned char > &used_values, const Compiler_IR_Function &function) noexcept
Compiler_Dataflow_Constant boolean_constant(const bool value)
bool bit_set_equals(const Compiler_Dataflow_Bit_Set &lhs, const Compiler_Dataflow_Bit_Set &rhs) noexcept
Compiler_Dataflow_Bit_Set bit_set_intersection(const Compiler_Dataflow_Bit_Set &lhs, const Compiler_Dataflow_Bit_Set &rhs)
std::string constant_to_string(const Compiler_Dataflow_Constant &constant)
bool constant_vector_equals(const DynArray< Compiler_Dataflow_Constant > &lhs, const DynArray< Compiler_Dataflow_Constant > &rhs) noexcept
bool same_constant(const Compiler_Dataflow_Constant &lhs, const Compiler_Dataflow_Constant &rhs) noexcept
bool parse_integer_constant(const std::string &text, long long &value) noexcept
bool is_value_used(const DynArray< unsigned char > &used_values, const Compiler_IR_Value_Id id) noexcept
Compiler_Dataflow_Constant value_constant(const DynArray< Compiler_Dataflow_Constant > &values, const Compiler_IR_Value_Id id)
std::string local_slot_name(const Compiler_IR_Local_Slot_Id id)
std::string block_name(const Compiler_IR_Block_Id id)
Main namespace for Aleph-w library functions.
Compiler_Dataflow_Constant_Kind
Constant-lattice states used by local constant propagation.
@ Unknown
A value exists but is not known to be constant.
@ Undefined
A slot has not been definitely initialized yet.
void message(const char *file, int line, const char *format,...)
Print an informational message with file and line info.
Definition ahDefs.C:95
Compiler_Dataflow_Validation_Report validate_dead_code_elimination(const Compiler_Dead_Code_Elimination_Result &result, const Compiler_IR_Module *module=nullptr)
Validates the result of dead-code elimination.
Compiler_Dataflow_Function_Analysis analyze_dataflow_function(const Compiler_IR_Function &function)
Computes reachability, liveness, definite assignment, and constant propagation for one IR function.
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
Definition ahAlgo.H:707
const char * compiler_dataflow_constant_kind_name(const Compiler_Dataflow_Constant_Kind kind) noexcept
Returns a stable debug name for one constant-lattice kind.
size_t Compiler_IR_Value_Id
constexpr size_t compiler_ir_invalid_id() noexcept
Returns the sentinel invalid IR id.
and
Check uniqueness with explicit hash + equality functors.
Compiler_Dead_Code_Elimination_Result eliminate_dead_code(const Compiler_IR_Function &function)
Eliminates unreachable blocks, dead pure instructions, dead local stores, and folds constant-conditio...
Compiler_Dataflow_Validation_Report validate_dataflow_analysis(const Compiler_IR_Function &function, const Compiler_Dataflow_Function_Analysis &analysis)
Validates structural invariants of a dataflow result against its source function.
size_t Compiler_IR_Block_Id
const char * compiler_ir_slot_kind_name(const Compiler_IR_Slot_Kind kind) noexcept
Stable debug name for one slot kind.
Compiler_IR_Validation_Report validate_ir_function(const Compiler_IR_Function &function, const Compiler_IR_Module *module=nullptr)
Validates one lowered IR function structurally.
std::string compiler_dump_dataflow_analysis(const Compiler_IR_Function *function, const Compiler_Dataflow_Function_Analysis &analysis, const Compiler_Type_Context *types=nullptr)
Produces a deterministic, human-readable dump of a dataflow result.
Field< int > Integer
Definition ahField.H:136
Compiler_Operator_Kind
Stable operator kinds shared by reusable compiler layers.
size_t Compiler_IR_Local_Slot_Id
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
Small reusable bit-set for slot-domain dataflow analyses.
bool test(const size_t index) const noexcept
Returns whether bit index is set.
size_t size() const noexcept
Returns the number of tracked bits.
bool set(const size_t index, const bool value=true)
Sets or clears bit index.
void resize(const size_t count, const bool value=false)
Clears all existing bits and resizes the set to count elements.
DynArray< unsigned char > bits
One byte per tracked element.
One propagated constant value in the local-slot lattice.
Compiler_Dataflow_Constant_Kind kind
Lattice kind.
bool is_known() const noexcept
Returns whether the lattice state represents a known constant.
long long integer_value
Integer payload when kind == Integer.
bool bool_value
Boolean payload when kind == Boolean.
std::string text
Stable textual spelling for deterministic dumps.
DynArray< Compiler_Dataflow_Constant > constant_out
DynArray< Compiler_Dataflow_Constant > value_constants
Full dataflow result for one IR function or top-level body.
DynArray< Compiler_Dataflow_Bit_Set > assigned_in_slots
Local slots definitely assigned at block entry.
DynArray< Compiler_Dataflow_Bit_Set > assigned_out_slots
Local slots definitely assigned at block exit.
size_t local_slot_count
Number of tracked local slots.
DynArray< Compiler_Dataflow_Bit_Set > live_out_slots
Local slots live at block exit.
DynArray< DynArray< Compiler_Dataflow_Constant > > constant_in_slots
Per-block constant state at entry.
DynArray< bool > reachable_blocks
Reachability from entry for each block id.
DynArray< DynArray< Compiler_Dataflow_Constant > > constant_out_slots
Per-block constant state at exit.
DynArray< Compiler_Dataflow_Bit_Set > live_in_slots
Local slots live at block entry.
DynArray< Compiler_Dataflow_Uninitialized_Read > uninitialized_reads
Local reads that are not definitely assigned.
size_t foldable_branch_count
Reachable branches whose condition is statically known.
One definite-assignment finding for an uninitialized local read.
Source_Span span
Source span associated with the load.
Compiler_IR_Local_Slot_Id slot_id
Read local slot.
Compiler_IR_Block_Id block_id
Block containing the read.
size_t instruction_index
Zero-based instruction index inside the block.
Validation report for analysis and optimization passes.
bool valid
Whether the checked invariants hold.
DynArray< std::string > warnings
Non-fatal observations.
DynArray< std::string > errors
Hard invariant violations.
Result of applying dead-code elimination to one IR function.
size_t removed_instructions
Number of removed instructions.
Compiler_IR_Function function
Optimized function copy.
size_t removed_blocks
Number of removed blocks.
size_t folded_branches
Number of branches folded to jumps.
One basic block of IR instructions.
Lowered IR for one function or top-level body.
Compiler_IR_Value_Id next_value_id
Next value id to allocate while lowering.
DynArray< Compiler_IR_Block > blocks
Basic blocks in deterministic id order.
std::string name
Debug or source-level name.
Compiler_IR_Block_Id exit_block
Canonical exit block.
Compiler_IR_Block_Id entry_block
Canonical entry block.
DynArray< Compiler_IR_Slot > local_slots
Parameter/local slots in stable order.
One instruction producing an optional explicit result value.
std::string text
Constant spelling or debug payload.
Compiler_IR_Instruction_Kind kind
Instruction category.
Lowered IR module with shared global slots and functions.
Compiler_SSA_Block_Id id
Dense SSA block id.
Definition SSA.H:163
DynArray< Compiler_SSA_Instruction > instructions
Linear SSA instruction list.
Definition SSA.H:168
std::string label
Deterministic debug label.
Definition SSA.H:165
Compiler_SSA_Terminator terminator
Explicit block terminator.
Definition SSA.H:169
Compiler_SSA_Value_Id return_value
Return value, when relevant.
Definition SSA.H:156
DynArray< Compiler_SSA_Block_Id > successors
Successors in deterministic order.
Definition SSA.H:157
Compiler_IR_Terminator_Kind kind
Terminator category.
Definition SSA.H:153
Compiler_SSA_Value_Id condition_value
Branch condition, when relevant.
Definition SSA.H:155
Half-open byte range inside a source file.
Definition ah-source.H:100
Lazy and scalable dynamic array implementation.