38#include <gtest/gtest.h>
57 make_manual_uninitialized_ir()
60 function.
name =
"manual";
67 slot.
kind = Compiler_IR_Slot_Kind::Local;
73 entry.
label =
"entry";
76 load.
kind = Compiler_IR_Instruction_Kind::Load;
82 constant.
kind = Compiler_IR_Instruction_Kind::Constant;
88 store.
kind = Compiler_IR_Instruction_Kind::Store;
97 auto exit = make_exit_block();
98 exit.predecessors.append(0);
100 function.
blocks.append(entry);
109 function.
name =
"choose";
116 flag.
kind = Compiler_IR_Slot_Kind::Parameter;
122 value.kind = Compiler_IR_Slot_Kind::Local;
123 value.name =
"value";
128 entry.
label =
"entry";
131 c0.
kind = Compiler_IR_Instruction_Kind::Constant;
137 store0.
kind = Compiler_IR_Instruction_Kind::Store;
143 load_flag.
kind = Compiler_IR_Instruction_Kind::Load;
153 auto exit = make_exit_block();
154 exit.predecessors.append(3);
158 then_block.
label =
"if.then.0";
161 c1.
kind = Compiler_IR_Instruction_Kind::Constant;
167 store1.
kind = Compiler_IR_Instruction_Kind::Store;
178 join_block.
label =
"if.end.0";
181 load_value.
kind = Compiler_IR_Instruction_Kind::Load;
194 else_block.
label =
"if.else.0";
197 c2.
kind = Compiler_IR_Instruction_Kind::Constant;
203 store2.
kind = Compiler_IR_Instruction_Kind::Store;
212 function.
blocks.append(entry);
214 function.
blocks.append(then_block);
215 function.
blocks.append(join_block);
216 function.
blocks.append(else_block);
221 make_constant_branch_ir()
224 function.
name =
"simplify";
231 slot.
kind = Compiler_IR_Slot_Kind::Local;
237 entry.
label =
"entry";
240 c1.
kind = Compiler_IR_Instruction_Kind::Constant;
246 store1.
kind = Compiler_IR_Instruction_Kind::Store;
252 cond.
kind = Compiler_IR_Instruction_Kind::Constant;
263 auto exit = make_exit_block();
264 exit.predecessors.append(3);
268 then_block.
label =
"if.then.0";
271 c2.
kind = Compiler_IR_Instruction_Kind::Constant;
277 store2.
kind = Compiler_IR_Instruction_Kind::Store;
288 join_block.
label =
"if.end.0";
291 load_value.
kind = Compiler_IR_Instruction_Kind::Load;
304 else_block.
label =
"if.else.0";
307 c3.
kind = Compiler_IR_Instruction_Kind::Constant;
313 store3.
kind = Compiler_IR_Instruction_Kind::Store;
322 function.
blocks.append(entry);
324 function.
blocks.append(then_block);
325 function.
blocks.append(join_block);
326 function.
blocks.append(else_block);
331 make_stale_predecessor_ir()
334 function.
name =
"stale_preds";
341 slot.
kind = Compiler_IR_Slot_Kind::Local;
347 entry.
label =
"entry";
350 cond.
kind = Compiler_IR_Instruction_Kind::Constant;
361 auto exit = make_exit_block();
362 exit.predecessors.append(4);
366 then_block.
label =
"then";
370 c1.
kind = Compiler_IR_Instruction_Kind::Constant;
376 s1.
kind = Compiler_IR_Instruction_Kind::Store;
386 else_block.
label =
"else";
390 c2.
kind = Compiler_IR_Instruction_Kind::Constant;
396 s2.
kind = Compiler_IR_Instruction_Kind::Store;
406 join_block.
label =
"join";
410 load_x.
kind = Compiler_IR_Instruction_Kind::Load;
419 function.
blocks.append(entry);
421 function.
blocks.append(then_block);
422 function.
blocks.append(else_block);
423 function.
blocks.append(join_block);
428 make_infinite_loop_ir()
431 function.
name =
"spin";
437 entry.
label =
"entry";
441 auto exit = make_exit_block();
451 function.
blocks.append(entry);
453 function.
blocks.append(loop);
473 make_branch_fold_liveout_ir(
bool flag_value)
476 function.
name =
"branch_fold";
483 slot.
kind = Compiler_IR_Slot_Kind::Local;
489 entry.
label =
"entry";
492 c0.
kind = Compiler_IR_Instruction_Kind::Constant;
498 s0.
kind = Compiler_IR_Instruction_Kind::Store;
504 cond.
kind = Compiler_IR_Instruction_Kind::Constant;
506 cond.
text = flag_value ?
"true" :
"false";
517 exit_blk.
label =
"exit";
523 then_block.
label =
"if.then.0";
527 c99.
kind = Compiler_IR_Instruction_Kind::Constant;
533 s99.
kind = Compiler_IR_Instruction_Kind::Store;
543 join_block.
label =
"if.end.0";
548 load_x.
kind = Compiler_IR_Instruction_Kind::Load;
559 else_block.
label =
"if.else.0";
563 cm1.
kind = Compiler_IR_Instruction_Kind::Constant;
569 sm1.
kind = Compiler_IR_Instruction_Kind::Store;
577 function.
blocks.append(entry);
578 function.
blocks.append(exit_blk);
579 function.
blocks.append(then_block);
580 function.
blocks.append(join_block);
581 function.
blocks.append(else_block);
594 bool test_parse_int(
const std::string &text,
long long &
value)
603 operand.
kind = Compiler_Dataflow_Constant_Kind::Integer;
612 lhs.
kind = rhs.
kind = Compiler_Dataflow_Constant_Kind::Integer;
619TEST(CompilerDataflowConstant, ParsesLLONG_MAX)
622 EXPECT_TRUE(test_parse_int(std::to_string(LLONG_MAX),
value));
623 EXPECT_EQ(
value, LLONG_MAX);
626TEST(CompilerDataflowConstant, ParsesLLONG_MINString)
629 EXPECT_TRUE(test_parse_int(std::to_string(LLONG_MIN),
value));
630 EXPECT_EQ(
value, LLONG_MIN);
633TEST(CompilerDataflowConstant, RejectsOverflowAboveLLONG_MAX)
636 EXPECT_FALSE(test_parse_int(
"9223372036854775808",
value));
639TEST(CompilerDataflowConstant, RejectsEmptyString)
642 EXPECT_FALSE(test_parse_int(
"",
value));
649TEST(CompilerDataflowConstant, UnaryMinusLLONG_MINDoesNotFold)
651 const auto result = test_unary(Compiler_Operator_Kind::Minus, LLONG_MIN);
652 EXPECT_EQ(result.kind, Compiler_Dataflow_Constant_Kind::Unknown);
655TEST(CompilerDataflowConstant, UnaryMinusNormalIntegerFolds)
657 const auto result = test_unary(Compiler_Operator_Kind::Minus, 42);
658 EXPECT_EQ(result.kind, Compiler_Dataflow_Constant_Kind::Integer);
659 EXPECT_EQ(result.integer_value, -42);
666TEST(CompilerDataflowConstant, BinaryDivLLONG_MINByMinusOneDoesNotFold)
668 const auto result = test_binary(Compiler_Operator_Kind::Slash, LLONG_MIN, -1);
669 EXPECT_EQ(result.kind, Compiler_Dataflow_Constant_Kind::Unknown);
672TEST(CompilerDataflowConstant, BinaryModLLONG_MINByMinusOneDoesNotFold)
674 const auto result = test_binary(Compiler_Operator_Kind::Percent, LLONG_MIN, -1);
675 EXPECT_EQ(result.kind, Compiler_Dataflow_Constant_Kind::Unknown);
678TEST(CompilerDataflowConstant, BinaryAddOverflowDoesNotFold)
680 const auto result = test_binary(Compiler_Operator_Kind::Plus, LLONG_MAX, 1);
681 EXPECT_EQ(result.kind, Compiler_Dataflow_Constant_Kind::Unknown);
684TEST(CompilerDataflowConstant, BinaryMulLLONG_MINByOneFoldsToLLONG_MIN)
686 const auto result = test_binary(Compiler_Operator_Kind::Star, LLONG_MIN, 1);
687 EXPECT_EQ(result.kind, Compiler_Dataflow_Constant_Kind::Integer);
688 EXPECT_EQ(result.integer_value, LLONG_MIN);
691TEST(CompilerDataflowConstant, BinaryMulOverflowDoesNotFold)
694 auto r = test_binary(Compiler_Operator_Kind::Star, LLONG_MAX, 2);
695 EXPECT_EQ(
r.kind, Compiler_Dataflow_Constant_Kind::Unknown);
698 r = test_binary(Compiler_Operator_Kind::Star, LLONG_MIN, 2);
699 EXPECT_EQ(
r.kind, Compiler_Dataflow_Constant_Kind::Unknown);
702 r = test_binary(Compiler_Operator_Kind::Star, LLONG_MAX, -2);
703 EXPECT_EQ(
r.kind, Compiler_Dataflow_Constant_Kind::Unknown);
710TEST(CompilerDataflow, BranchFoldPreservesLiveOutForTakenArm)
715 const auto function = make_branch_fold_liveout_ir(
true);
717 ASSERT_TRUE(ir_report.valid);
721 ASSERT_TRUE(report.valid);
723 EXPECT_EQ(optimized.folded_branches, 1u);
724 EXPECT_GT(optimized.removed_blocks, 0u);
727 bool found_join =
false;
728 for (
size_t i = 0; i < optimized.function.blocks.size(); ++i)
730 const auto &blk = optimized.function.blocks.access(i);
731 if (blk.label ==
"if.end.0")
734 bool has_load =
false;
735 for (
size_t j = 0; j < blk.instructions.size(); ++j)
736 if (blk.instructions.access(j).kind == Compiler_IR_Instruction_Kind::Load)
738 EXPECT_TRUE(has_load) <<
"load of x must survive in join block";
741 EXPECT_TRUE(found_join) <<
"join block must survive branch fold";
744 for (
size_t i = 0; i < optimized.function.blocks.size(); ++i)
745 EXPECT_NE(optimized.function.blocks.access(i).label,
"if.else.0")
746 <<
"dead else block must be removed";
749TEST(CompilerDataflow, BranchFoldFalseTakesElseArm)
752 const auto function = make_branch_fold_liveout_ir(
false);
754 ASSERT_TRUE(ir_report.valid);
759 EXPECT_EQ(optimized.folded_branches, 1u);
761 for (
size_t i = 0; i < optimized.function.blocks.size(); ++i)
762 EXPECT_NE(optimized.function.blocks.access(i).label,
"if.then.0")
763 <<
"dead then block must be removed";
766TEST(CompilerDataflow, ReportsUninitializedLocalLoads)
768 const auto function = make_manual_uninitialized_ir();
770 ASSERT_TRUE(ir_report.valid);
774 ASSERT_TRUE(report.valid);
775 ASSERT_EQ(analysis.uninitialized_reads.size(), 1u);
776 EXPECT_EQ(analysis.uninitialized_reads.access(0).block_id, 0u);
777 EXPECT_EQ(analysis.uninitialized_reads.access(0).instruction_index, 0u);
778 EXPECT_EQ(analysis.uninitialized_reads.access(0).slot_id, 0u);
781 EXPECT_NE(dump.find(
"UninitializedReads:"), std::string::npos);
782 EXPECT_NE(dump.find(
"B0/I0 -> s0"), std::string::npos);
785TEST(CompilerDataflow, ComputesReachabilityLivenessAndConstants)
787 const auto function = make_branching_ir();
789 ASSERT_TRUE(ir_report.valid);
793 ASSERT_TRUE(report.valid);
795 ASSERT_EQ(function.
blocks.size(), 5u);
796 EXPECT_TRUE(analysis.reachable_blocks.access(0));
797 EXPECT_TRUE(analysis.reachable_blocks.access(1));
798 EXPECT_TRUE(analysis.reachable_blocks.access(2));
799 EXPECT_TRUE(analysis.reachable_blocks.access(3));
800 EXPECT_TRUE(analysis.reachable_blocks.access(4));
801 EXPECT_EQ(analysis.foldable_branch_count, 0u);
802 EXPECT_TRUE(analysis.uninitialized_reads.is_empty());
805 EXPECT_TRUE(analysis.assigned_in_slots.access(0).test(0));
806 EXPECT_FALSE(analysis.assigned_in_slots.access(0).test(1));
807 EXPECT_EQ(analysis.constant_out_slots.access(0).access(1).kind,
808 Compiler_Dataflow_Constant_Kind::Integer);
809 EXPECT_EQ(analysis.constant_out_slots.access(0).access(1).integer_value, 0);
810 EXPECT_TRUE(analysis.live_out_slots.access(2).test(1));
811 EXPECT_TRUE(analysis.live_out_slots.access(4).test(1));
814TEST(CompilerDataflow, RebuildsPredecessorsForForwardMeets)
816 const auto function = make_stale_predecessor_ir();
819 ASSERT_TRUE(report.valid);
821 ASSERT_EQ(analysis.assigned_in_slots.size(), function.
blocks.size());
822 EXPECT_TRUE(analysis.assigned_in_slots.access(4).test(0));
823 EXPECT_TRUE(analysis.assigned_out_slots.access(4).test(0));
826TEST(CompilerDataflow, EliminatesDeadStoresAndConstantBranches)
828 const auto function = make_constant_branch_ir();
830 ASSERT_TRUE(ir_report.valid);
834 ASSERT_TRUE(report.valid);
835 EXPECT_EQ(optimized.folded_branches, 1u);
836 EXPECT_GT(optimized.removed_blocks, 0u);
837 EXPECT_GT(optimized.removed_instructions, 0u);
838 ASSERT_EQ(optimized.function.blocks.size(), 4u);
839 EXPECT_EQ(optimized.function.blocks.access(0).terminator.kind,
840 Compiler_IR_Terminator_Kind::Jump);
841 EXPECT_EQ(optimized.function.blocks.access(0).terminator.successors.size(), 1u);
842 EXPECT_EQ(optimized.function.blocks.access(2).label,
"if.then.0");
843 EXPECT_EQ(optimized.function.blocks.access(3).label,
"if.end.0");
844 for (
size_t i = 0; i < optimized.function.blocks.size(); ++i)
845 EXPECT_NE(optimized.function.blocks.access(i).label,
"if.else.0");
848TEST(CompilerDataflow, PreservesExitBlockWhenCompactingInfiniteLoop)
850 const auto function = make_infinite_loop_ir();
852 ASSERT_TRUE(ir_report.valid);
856 ASSERT_TRUE(report.valid);
859 ASSERT_LT(optimized.function.exit_block, optimized.function.blocks.size());
860 EXPECT_EQ(optimized.function.blocks.access(optimized.function.exit_block).terminator.kind,
861 Compiler_IR_Terminator_Kind::Exit);
867TEST(CompilerDataflow, KeepsDeadArithmeticThatCanStopTheProgram)
870 function.
name =
"dead_values";
874 for (
const char * name : {
"x",
"y"})
878 parameter.
kind = Compiler_IR_Slot_Kind::Parameter;
879 parameter.
name = name;
885 entry.
label =
"entry";
899 inst.
text = kind == Compiler_IR_Instruction_Kind::Constant ?
"0" :
"";
906 const auto x = append(Kind::Load, Op::Invalid, {}, 0);
907 const auto y = append(Kind::Load, Op::Invalid, {}, 1);
908 for (
const Op op : {Op::Plus, Op::Minus, Op::Star, Op::Slash, Op::Percent,
909 Op::Less, Op::EqEq, Op::Amp, Op::Pipe, Op::Caret})
910 (
void) append(Kind::Binary, op, {x,
y});
911 for (
const Op op : {Op::Minus, Op::Plus, Op::Tilde})
912 (
void) append(Kind::Unary, op, {x});
918 auto exit = make_exit_block();
919 exit.predecessors.append(0);
920 function.
blocks.append(entry);
928 const auto & instructions = optimized.function.blocks.access(0).instructions;
929 for (
size_t i = 0; i < instructions.size(); ++i)
931 const auto & inst = instructions.access(i);
932 if (inst.kind == Kind::Unary or inst.kind == Kind::Binary)
933 kept += std::string(inst.kind == Kind::Unary ?
" unary " :
" ")
936 EXPECT_EQ(kept,
" Plus Minus Star Slash Percent unary Minus");
937 EXPECT_EQ(optimized.removed_instructions, 7u);
Reusable dataflow analyses and dead-code elimination over Compiler_IR_Model.H.
size_t size_t int32_t value
void clear() noexcept
Empties the container.
T & append()
Allocate a new entry to the end of array.
Compiler_Dataflow_Constant evaluate_binary(const Compiler_Operator_Kind op, const Compiler_Dataflow_Constant &lhs, const Compiler_Dataflow_Constant &rhs)
Compiler_Dataflow_Constant evaluate_unary(const Compiler_Operator_Kind op, const Compiler_Dataflow_Constant &operand)
bool parse_integer_constant(const std::string &text, long long &value) noexcept
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
Main namespace for Aleph-w library functions.
Compiler_IR_Instruction_Kind
Instruction kinds supported by the MVP IR.
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.
const char * compiler_operator_name(const Compiler_Operator_Kind kind) noexcept
Returns a stable debug name for one operator kind.
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 exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
constexpr size_t compiler_ir_invalid_id() noexcept
Returns the sentinel invalid IR id.
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.
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.
Compiler_Operator_Kind
Stable operator kinds shared by reusable compiler layers.
size_t Compiler_IR_Local_Slot_Id
One propagated constant value in the local-slot lattice.
Compiler_Dataflow_Constant_Kind kind
Lattice kind.
long long integer_value
Integer payload when kind == Integer.
One basic block of IR instructions.
std::string label
Deterministic debug label.
Compiler_IR_Terminator terminator
Explicit block terminator.
DynArray< Compiler_IR_Block_Id > predecessors
Predecessor block ids.
DynArray< Compiler_IR_Instruction > instructions
Linear instruction list.
Compiler_IR_Block_Id id
Stable block id.
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.
bool bool_value
Decoded boolean payload for bool constants.
Compiler_IR_Instruction_Kind kind
Instruction category.
Compiler_IR_Value_Id result_id
Produced value id, or 0 for void instructions.
DynArray< Compiler_IR_Value_Id > operands
Input values in deterministic order.
Compiler_Operator_Kind op
Unary/binary operator kind, when relevant.
Compiler_IR_Local_Slot_Id local_slot_id
Referenced local/parameter slot, when relevant.
One storage slot used by the IR.
std::string name
Debug name of the slot.
size_t id
Stable slot id within its storage space.
Compiler_IR_Slot_Kind kind
Slot storage category.
Compiler_IR_Terminator_Kind kind
Terminator category.
DynArray< Compiler_IR_Block_Id > successors
Successor blocks in deterministic order.
Compiler_IR_Value_Id condition_value
Branch condition value, if any.
Compiler_IR_Value_Id return_value
Return value, if any.
Compiler_SSA_Block_Id id
Dense SSA block id.
std::string label
Deterministic debug label.
Compiler_SSA_Terminator terminator
Explicit block terminator.
Compiler_IR_Terminator_Kind kind
Terminator category.