Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
compiler_dataflow_example.cc
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
36#include <iostream>
37
38#include <Compiler_Dataflow.H>
39
40using namespace Aleph;
41
42namespace
43{
46 {
47 Compiler_IR_Function function;
48 function.name = "simplify";
49 function.entry_block = 0;
50 function.exit_block = 1;
51 function.next_value_id = 5;
52
54 slot.id = 0;
55 slot.kind = Compiler_IR_Slot_Kind::Local;
56 slot.name = "x";
57 function.local_slots.append(slot);
58
60 entry.id = 0;
61 entry.label = "entry";
62
64 c1.kind = Compiler_IR_Instruction_Kind::Constant;
65 c1.result_id = 1;
66 c1.text = "1";
67 entry.instructions.append(c1);
68
70 store1.kind = Compiler_IR_Instruction_Kind::Store;
71 store1.local_slot_id = 0;
72 store1.operands.append(1);
73 entry.instructions.append(store1);
74
76 cond.kind = Compiler_IR_Instruction_Kind::Constant;
77 cond.result_id = 2;
78 cond.text = "true";
79 cond.bool_value = true;
80 entry.instructions.append(cond);
81
82 entry.terminator.kind = Compiler_IR_Terminator_Kind::Branch;
86
88 exit.id = 1;
89 exit.label = "exit";
90 exit.terminator.kind = Compiler_IR_Terminator_Kind::Exit;
91 exit.predecessors.append(3);
92
94 then_block.id = 2;
95 then_block.label = "if.then.0";
96
98 c2.kind = Compiler_IR_Instruction_Kind::Constant;
99 c2.result_id = 3;
100 c2.text = "2";
101 then_block.instructions.append(c2);
102
104 store2.kind = Compiler_IR_Instruction_Kind::Store;
105 store2.local_slot_id = 0;
106 store2.operands.append(3);
107 then_block.instructions.append(store2);
108
109 then_block.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
110 then_block.terminator.successors.append(3);
111 then_block.predecessors.append(0);
112
114 join_block.id = 3;
115 join_block.label = "if.end.0";
116
118 load_value.kind = Compiler_IR_Instruction_Kind::Load;
119 load_value.result_id = 5;
120 load_value.local_slot_id = 0;
121 join_block.instructions.append(load_value);
122
123 join_block.terminator.kind = Compiler_IR_Terminator_Kind::Return;
124 join_block.terminator.return_value = 5;
125 join_block.terminator.successors.append(1);
126 join_block.predecessors.append(2);
127 join_block.predecessors.append(4);
128
130 else_block.id = 4;
131 else_block.label = "if.else.0";
132
134 c3.kind = Compiler_IR_Instruction_Kind::Constant;
135 c3.result_id = 4;
136 c3.text = "3";
137 else_block.instructions.append(c3);
138
140 store3.kind = Compiler_IR_Instruction_Kind::Store;
141 store3.local_slot_id = 0;
142 store3.operands.append(4);
143 else_block.instructions.append(store3);
144
145 else_block.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
146 else_block.terminator.successors.append(3);
147 else_block.predecessors.append(0);
148
149 function.blocks.append(entry);
150 function.blocks.append(exit);
151 function.blocks.append(then_block);
152 function.blocks.append(join_block);
153 function.blocks.append(else_block);
154 return function;
155 }
156}
157
158int main()
159{
160 const auto function = make_demo_function();
161 const auto analysis = analyze_dataflow_function(function);
163 const auto optimized = eliminate_dead_code(function);
165
166 std::cout << compiler_dump_dataflow_analysis(&function, analysis) << '\n';
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)
173 {
174 const auto & block = optimized.function.blocks.access(i);
175 std::cout << " Block " << i << " [" << block.label << "] -> "
177 }
178
179 if (not dataflow_report.warnings.is_empty() or not dce_report.warnings.is_empty())
180 {
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';
186 }
187
188 return dataflow_report.valid and dce_report.valid ? 0 : 1;
189}
Reusable dataflow analyses and dead-code elimination over Compiler_IR_Model.H.
T & append()
Allocate a new entry to the end of array.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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.
Definition ahDefs.C:125
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_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.
One storage slot used by the IR.
size_t id
Stable slot id within its storage space.
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.
std::string label
Deterministic debug label.
Definition SSA.H:165
Compiler_SSA_Terminator terminator
Explicit block terminator.
Definition SSA.H:169
Compiler_IR_Terminator_Kind kind
Terminator category.
Definition SSA.H:153