Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ssa_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 <SSA.H>
39
40using namespace Aleph;
41
42namespace
43{
46 {
47 Compiler_IR_Function function;
48 function.name = "choose";
49 function.entry_block = 0;
50 function.exit_block = 1;
51
53 flag.id = 0;
54 flag.kind = Compiler_IR_Slot_Kind::Parameter;
55 flag.name = "flag";
56 function.local_slots.append(flag);
57
59 value.id = 1;
60 value.kind = Compiler_IR_Slot_Kind::Local;
61 value.name = "value";
62 function.local_slots.append(value);
63
65 entry.id = 0;
66 entry.label = "entry";
67
69 zero.kind = Compiler_IR_Instruction_Kind::Constant;
70 zero.result_id = 1;
71 zero.text = "0";
72 entry.instructions.append(zero);
73
75 init_store.kind = Compiler_IR_Instruction_Kind::Store;
76 init_store.local_slot_id = 1;
77 init_store.operands.append(1);
78 entry.instructions.append(init_store);
79
81 load_flag.kind = Compiler_IR_Instruction_Kind::Load;
82 load_flag.result_id = 2;
83 load_flag.local_slot_id = 0;
84 entry.instructions.append(load_flag);
85
86 entry.terminator.kind = Compiler_IR_Terminator_Kind::Branch;
90
92 exit.id = 1;
93 exit.label = "exit";
94 exit.terminator.kind = Compiler_IR_Terminator_Kind::Exit;
95 exit.predecessors.append(3);
96
98 then_block.id = 2;
99 then_block.label = "if.then.0";
100
102 one.kind = Compiler_IR_Instruction_Kind::Constant;
103 one.result_id = 3;
104 one.text = "1";
105 then_block.instructions.append(one);
106
108 then_store.kind = Compiler_IR_Instruction_Kind::Store;
109 then_store.local_slot_id = 1;
110 then_store.operands.append(3);
111 then_block.instructions.append(then_store);
112
113 then_block.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
114 then_block.terminator.successors.append(3);
115 then_block.predecessors.append(0);
116
118 join_block.id = 3;
119 join_block.label = "if.end.0";
120
122 load_value.kind = Compiler_IR_Instruction_Kind::Load;
123 load_value.result_id = 5;
124 load_value.local_slot_id = 1;
125 join_block.instructions.append(load_value);
126
127 join_block.terminator.kind = Compiler_IR_Terminator_Kind::Return;
128 join_block.terminator.return_value = 5;
129 join_block.terminator.successors.append(1);
130 join_block.predecessors.append(2);
131 join_block.predecessors.append(4);
132
134 else_block.id = 4;
135 else_block.label = "if.else.0";
136
138 two.kind = Compiler_IR_Instruction_Kind::Constant;
139 two.result_id = 4;
140 two.text = "2";
141 else_block.instructions.append(two);
142
144 else_store.kind = Compiler_IR_Instruction_Kind::Store;
145 else_store.local_slot_id = 1;
146 else_store.operands.append(4);
147 else_block.instructions.append(else_store);
148
149 else_block.terminator.kind = Compiler_IR_Terminator_Kind::Jump;
150 else_block.terminator.successors.append(3);
151 else_block.predecessors.append(0);
152
153 function.blocks.append(entry);
154 function.blocks.append(exit);
155 function.blocks.append(then_block);
156 function.blocks.append(join_block);
157 function.blocks.append(else_block);
158
159 function.next_value_id = 0;
160 for (size_t i = 0; i < function.blocks.size(); ++i)
161 {
162 const auto & block = function.blocks.access(i);
163 for (size_t j = 0; j < block.instructions.size(); ++j)
164 {
165 const auto & instruction = block.instructions.access(j);
166 if (instruction.result_id > function.next_value_id)
167 function.next_value_id = instruction.result_id;
168 }
169 }
170
171 return function;
172 }
173}
174
175int main()
176{
177 Compiler_SSA_Context ctx(1 << 16);
179
180 Compiler_IR_Module module;
181 auto function = make_demo_function();
182 module.functions.append(&function);
183
184 const auto * ssa = lowering.lower_module(&module);
185 const auto report = validate_ssa_module(*ssa);
186
187 std::cout << compiler_dump_ssa_module(ssa);
188 if (not report.warnings.is_empty())
189 {
190 std::cout << "\nWarnings:\n";
191 for (size_t i = 0; i < report.warnings.size(); ++i)
192 std::cout << "- " << report.warnings.access(i) << '\n';
193 }
194
195 return report.valid ? 0 : 1;
196}
Static single assignment form over Compiler_IR.H.
size_t size_t int32_t value
Definition ca-c-api.h:116
Arena-backed ownership context for SSA functions and modules.
Definition SSA.H:223
Lowers non-SSA IR to SSA form for all reachable blocks.
Definition SSA.H:823
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
Compiler_SSA_Block & block(Compiler_SSA_Function &function, const Compiler_SSA_Block_Id id)
Definition SSA.H:636
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Compiler_SSA_Validation_Report validate_ssa_module(const Compiler_SSA_Module &module)
Validates all functions in one SSA module.
Definition SSA.H:1231
void exit(const char *file, int line, const char *format,...)
Print a message and exit the program.
Definition ahDefs.C:125
std::string compiler_dump_ssa_module(const Compiler_SSA_Module *module, const Compiler_Type_Context *types=nullptr)
Dumps all SSA functions in one module deterministically.
Definition SSA.H:1410
int main()
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.
Compiler_IR_Instruction_Kind kind
Instruction category.
Compiler_IR_Value_Id result_id
Produced value id, or 0 for void instructions.
Lowered IR module with shared global slots and functions.
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.
DynArray< Compiler_SSA_Instruction > instructions
Linear SSA instruction list.
Definition SSA.H:168