commit 4b035e71c821cbbc8b39676aa1d3707f934e3d4d
parent 2b05fa5fc81752451f2d77c65d1576cbab6dd4a3
Author: erai <erai@omiltem.net>
Date: Wed, 5 Feb 2025 14:38:25 +0000
move comments around
Diffstat:
M | cc0.c | | | 60 | +++++++++++++++++++++++++++++++++++------------------------- |
M | ir.om | | | 50 | ++++++++++++++++++++++---------------------------- |
2 files changed, 57 insertions(+), 53 deletions(-)
diff --git a/cc0.c b/cc0.c
@@ -188,6 +188,7 @@ struct my_irvar {
struct my_irvar* my_right;
struct my_type* my_t;
unsigned long my_n;
+ unsigned long my_reify;
unsigned long my_offset;
unsigned long my_mark;
};
@@ -770,7 +771,6 @@ void( my_iraddarg)(struct my_irfunc* my_ic,unsigned char* my_name,struct my_type
void( my_iraddop)(struct my_irfunc* my_ic,struct my_irop* my_o);
void( my_iraddvar)(struct my_irfunc* my_ic,unsigned char* my_name,struct my_type* my_t);
void( my_irblock_dead_expr)(struct my_irfunc* my_ic,struct my_irblock* my_b);
-void( my_irblock_flow)(struct my_irfunc* my_ic);
void( my_irblock_flow2)(struct my_irfunc* my_ic,struct my_irblock* my_ib);
void( my_irblock_flow3)(struct my_irfunc* my_ic,struct my_irblock* my_ib);
void( my_irblock_fold)(struct my_irfunc* my_ic,struct my_irblock* my_b);
@@ -779,6 +779,9 @@ struct my_irop*( my_ircall)(struct my_irfunc* my_ic,struct my_node* my_n);
struct my_irop*( my_irexpr_fold)(struct my_irfunc* my_ic,struct my_irop* my_o);
struct my_irblock*( my_irfind_block)(struct my_irfunc* my_ic,unsigned char* my_name,unsigned long my_make);
struct my_irvar**( my_irfind_var)(struct my_irfunc* my_ic,unsigned char* my_name);
+void( my_irfunc_dead)(struct my_irfunc* my_ic);
+void( my_irfunc_flow)(struct my_irfunc* my_ic);
+void( my_irfunc_fold)(struct my_irfunc* my_ic);
void( my_irjump)(struct my_irfunc* my_ic,struct my_irblock* my_to,struct my_irblock* my_next);
void( my_irreset)(struct my_irblock* my_b);
void( my_irreturn)(struct my_irfunc* my_ic,struct my_irop* my_value);
@@ -4503,24 +4506,9 @@ unsigned long( my_hoist_locals)(struct my_compiler* my_c,struct my_decl* my_d,st
return my_offset;
}
void( my_ir_optimize)(struct my_irfunc* my_ic){
- unsigned long my_i = 0;
- (my_i)=(0UL);
- while (1) {
- if ((unsigned long)(((long)(my_i))==((long)((my_ic)->my_blocks_len)))) {
- break;
- }
- (my_irblock_dead_expr)((my_ic),(((my_ic)->my_blocks)[my_i]));
- (my_i)=((unsigned long)(((unsigned long)(my_i))+((unsigned long)(1UL))));
- }
- (my_i)=(0UL);
- while (1) {
- if ((unsigned long)(((long)(my_i))==((long)((my_ic)->my_blocks_len)))) {
- break;
- }
- (my_irblock_fold)((my_ic),(((my_ic)->my_blocks)[my_i]));
- (my_i)=((unsigned long)(((unsigned long)(my_i))+((unsigned long)(1UL))));
- }
- (my_irblock_flow)((my_ic));
+ (my_irfunc_dead)((my_ic));
+ (my_irfunc_fold)((my_ic));
+ (my_irfunc_flow)((my_ic));
}
void( my_iraddarg)(struct my_irfunc* my_ic,unsigned char* my_name,struct my_type* my_t){
struct my_irvar** my_iv = 0;
@@ -4589,11 +4577,6 @@ void( my_irblock_dead_expr)(struct my_irfunc* my_ic,struct my_irblock* my_b){
}
((my_b)->my_ops_len)=(my_j);
}
-void( my_irblock_flow)(struct my_irfunc* my_ic){
- (my_irblock_flow2)((my_ic),(((my_ic)->my_blocks)[0UL]));
- (my_irblock_flow3)((my_ic),(((my_ic)->my_blocks)[0UL]));
- (my_irreset)((((my_ic)->my_blocks)[0UL]));
-}
void( my_irblock_flow2)(struct my_irfunc* my_ic,struct my_irblock* my_ib){
unsigned long my_kind = 0;
if ((my_ib)->my_mark) {
@@ -4999,6 +4982,33 @@ struct my_irvar**( my_irfind_var)(struct my_irfunc* my_ic,unsigned char* my_name
}
}
}
+void( my_irfunc_dead)(struct my_irfunc* my_ic){
+ unsigned long my_i = 0;
+ (my_i)=(0UL);
+ while (1) {
+ if ((unsigned long)(((long)(my_i))==((long)((my_ic)->my_blocks_len)))) {
+ break;
+ }
+ (my_irblock_dead_expr)((my_ic),(((my_ic)->my_blocks)[my_i]));
+ (my_i)=((unsigned long)(((unsigned long)(my_i))+((unsigned long)(1UL))));
+ }
+}
+void( my_irfunc_flow)(struct my_irfunc* my_ic){
+ (my_irblock_flow2)((my_ic),(((my_ic)->my_blocks)[0UL]));
+ (my_irblock_flow3)((my_ic),(((my_ic)->my_blocks)[0UL]));
+ (my_irreset)((((my_ic)->my_blocks)[0UL]));
+}
+void( my_irfunc_fold)(struct my_irfunc* my_ic){
+ unsigned long my_i = 0;
+ (my_i)=(0UL);
+ while (1) {
+ if ((unsigned long)(((long)(my_i))==((long)((my_ic)->my_blocks_len)))) {
+ break;
+ }
+ (my_irblock_fold)((my_ic),(((my_ic)->my_blocks)[my_i]));
+ (my_i)=((unsigned long)(((unsigned long)(my_i))+((unsigned long)(1UL))));
+ }
+}
void( my_irjump)(struct my_irfunc* my_ic,struct my_irblock* my_to,struct my_irblock* my_next){
struct my_irblock* my_cur = 0;
struct my_irop* my_o = 0;
@@ -5616,7 +5626,7 @@ struct my_irvar*( my_mkirvar)(struct my_irfunc* my_ic,unsigned char* my_name,str
((my_ic)->my_vars)=(my_tmp);
}
(my_i)=((my_ic)->my_vars_len);
- (my_v)=((struct my_irvar*)(my_alloc)(((my_ic)->my_a),(56UL)));
+ (my_v)=((struct my_irvar*)(my_alloc)(((my_ic)->my_a),(64UL)));
((my_v)->my_n)=(my_i);
((my_v)->my_name)=(my_name);
((my_v)->my_t)=(my_t);
diff --git a/ir.om b/ir.om
@@ -90,6 +90,7 @@ struct irvar {
right: *irvar;
t: *type;
n: int;
+ reify: int;
offset: int;
mark: int;
}
@@ -1381,7 +1382,7 @@ func emit_kstart(c: *assembler) {
as_modrr(c, OP_CMPRM, R_RAX, R_RDX);
as_jmp(c, OP_JCC + CC_NE, hang);
- // Setup an early stack
+ // Setup an initial stack
as_modri(c, OP_MOVI, R_RSP, 0x00300000); // FIXME bss
// Align stack to page
@@ -2462,16 +2463,17 @@ func irblock_flow3(ic: *irfunc, ib: *irblock) {
}
}
-func irblock_flow(ic: *irfunc) {
+func irfunc_flow(ic: *irfunc) {
+ // Merge blocks that have one incoming edge
irblock_flow2(ic, ic.blocks[0]);
irblock_flow3(ic, ic.blocks[0]);
irreset(ic.blocks[0]);
}
-func ir_optimize(ic: *irfunc) {
+func irfunc_dead(ic: *irfunc) {
var i: int;
- // First scan eliminate useless expressions
+ // Eliminate dead operations
i = 0;
loop {
if i == ic.blocks_len {
@@ -2482,6 +2484,10 @@ func ir_optimize(ic: *irfunc) {
i = i + 1;
}
+}
+
+func irfunc_fold(ic: *irfunc) {
+ var i: int;
// Try to evaluate constants as much as possible
i = 0;
@@ -2495,29 +2501,17 @@ func ir_optimize(ic: *irfunc) {
i = i + 1;
}
- irblock_flow(ic);
-
- // live values
- // value forwarding
- // common subexpression
-
- // inlining
-
- // register allocation
- // instruction selection
}
-// loop detection
-// unrolling
-// loop motion / fusion
-
-// strength reduction / algebra
-
-// alias analysis
-
-// type check with ir
-// parse to ir
-
-// c generator from ir
-
-// intrinsics
+func ir_optimize(ic: *irfunc) {
+ // Do the easy simplifications first
+ irfunc_dead(ic);
+ irfunc_fold(ic);
+ irfunc_flow(ic);
+
+ // Do some data flow analysis
+ // Do value forwarding
+ // Do common expression elimination
+ // Do instruction selection
+ // Do register allocation
+}