48 function.
name =
"simplify";
55 slot.
kind = Compiler_IR_Slot_Kind::Local;
61 entry.
label =
"entry";
64 c1.
kind = Compiler_IR_Instruction_Kind::Constant;
70 store1.
kind = Compiler_IR_Instruction_Kind::Store;
76 cond.
kind = Compiler_IR_Instruction_Kind::Constant;
90 exit.terminator.kind = Compiler_IR_Terminator_Kind::Exit;
91 exit.predecessors.append(3);
95 then_block.
label =
"if.then.0";
98 c2.
kind = Compiler_IR_Instruction_Kind::Constant;
104 store2.
kind = Compiler_IR_Instruction_Kind::Store;
115 join_block.
label =
"if.end.0";
118 load_value.
kind = Compiler_IR_Instruction_Kind::Load;
131 else_block.
label =
"if.else.0";
134 c3.
kind = Compiler_IR_Instruction_Kind::Constant;
140 store3.
kind = Compiler_IR_Instruction_Kind::Store;
149 function.
blocks.append(entry);
151 function.
blocks.append(then_block);
152 function.
blocks.append(join_block);
153 function.
blocks.append(else_block);
160 const auto function = make_demo_function();
167 std::cout <<
"OptimizedSummary\n";
168 std::cout <<
" Blocks: " << optimized.function.blocks.size() <<
'\n';
169 std::cout <<
" RemovedBlocks: " << optimized.removed_blocks <<
'\n';
170 std::cout <<
" RemovedInstructions: " << optimized.removed_instructions <<
'\n';
171 std::cout <<
" FoldedBranches: " << optimized.folded_branches <<
'\n';
172 for (
size_t i = 0; i < optimized.function.blocks.size(); ++i)
174 const auto & block = optimized.function.blocks.access(i);
175 std::cout <<
" Block " << i <<
" [" << block.
label <<
"] -> "
179 if (not dataflow_report.warnings.is_empty() or not dce_report.warnings.is_empty())
181 std::cout <<
"\nWarnings:\n";
182 for (
size_t i = 0; i < dataflow_report.warnings.size(); ++i)
183 std::cout <<
"- " << dataflow_report.warnings.access(i) <<
'\n';
184 for (
size_t i = 0; i < dce_report.warnings.size(); ++i)
185 std::cout <<
"- " << dce_report.warnings.access(i) <<
'\n';
188 return dataflow_report.valid
and dce_report.valid ? 0 : 1;
Reusable dataflow analyses and dead-code elimination over Compiler_IR_Model.H.
T & append()
Allocate a new entry to the end of array.
Main namespace for Aleph-w library functions.
const char * compiler_ir_terminator_kind_name(const Compiler_IR_Terminator_Kind kind) noexcept
Stable debug name for one terminator kind.
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 exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
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.
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.
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_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.
std::string label
Deterministic debug label.
Compiler_SSA_Terminator terminator
Explicit block terminator.
Compiler_IR_Terminator_Kind kind
Terminator category.