@@ -27,7 +27,6 @@ typedef struct Context_ {
27
27
Rewriter rewriter ;
28
28
CFG * cfg ;
29
29
const UsesMap * uses ;
30
- struct Dict * live_vars ;
31
30
32
31
struct Dict * lifted ;
33
32
bool disable_lowering ;
@@ -83,27 +82,32 @@ static void add_to_recover_context(struct List* recover_context, struct Dict* se
83
82
}
84
83
}
85
84
86
- static LiftedCont * lambda_lift (Context * ctx , const Node * cont , String given_name ) {
87
- assert (is_basic_block (cont ) || is_case (cont ));
88
- LiftedCont * * found = find_value_dict (const Node * , LiftedCont * , ctx -> lifted , cont );
85
+ static LiftedCont * lambda_lift (Context * ctx , const Node * liftee , String given_name ) {
86
+ assert (is_basic_block (liftee ) || is_case (liftee ));
87
+ LiftedCont * * found = find_value_dict (const Node * , LiftedCont * , ctx -> lifted , liftee );
89
88
if (found )
90
89
return * found ;
91
90
92
91
IrArena * a = ctx -> rewriter .dst_arena ;
93
- Nodes oparams = get_abstraction_params (cont );
94
- const Node * obody = get_abstraction_body (cont );
92
+ Nodes oparams = get_abstraction_params (liftee );
93
+ const Node * obody = get_abstraction_body (liftee );
95
94
96
- String name = is_basic_block (cont ) ? format_string_arena (a -> arena , "%s_%s" , get_abstraction_name (cont -> payload .basic_block .fn ), get_abstraction_name (cont )) : unique_name (a , given_name );
95
+ String name = is_basic_block (liftee ) ? format_string_arena (a -> arena , "%s_%s" , get_abstraction_name (liftee -> payload .basic_block .fn ), get_abstraction_name (liftee )) : unique_name (a , given_name );
97
96
98
97
// Compute the live stuff we'll need
99
- CFNode * cf_node = cfg_lookup (ctx -> cfg , cont );
100
- CFNodeVariables * node_vars = * find_value_dict (CFNode * , CFNodeVariables * , ctx -> live_vars , cf_node );
98
+ CFG * cfg_rooted_in_liftee = build_cfg (ctx -> cfg -> entry -> node , liftee , NULL , false);
99
+ CFNode * cf_node = cfg_lookup (cfg_rooted_in_liftee , liftee );
100
+ struct Dict * live_vars = compute_cfg_variables_map (cfg_rooted_in_liftee );
101
+ CFNodeVariables * node_vars = * find_value_dict (CFNode * , CFNodeVariables * , live_vars , cf_node );
101
102
struct List * recover_context = new_list (const Node * );
102
103
103
- add_to_recover_context (recover_context , node_vars -> bound_set , cont );
104
+ add_to_recover_context (recover_context , node_vars -> free_set , liftee );
104
105
size_t recover_context_size = entries_count_list (recover_context );
105
106
106
- debugv_print ("lambda_lift: free (to-be-spilled) variables at '%s' (count=%d): " , name , recover_context_size );
107
+ destroy_cfg_variables_map (live_vars );
108
+ destroy_cfg (cfg_rooted_in_liftee );
109
+
110
+ debugv_print ("lambda_lift: free (to-be-spilled) variables at '%s' (count=%d): " , get_abstraction_name_safe (liftee ), recover_context_size );
107
111
for (size_t i = 0 ; i < recover_context_size ; i ++ ) {
108
112
const Node * item = read_list (const Node * , recover_context )[i ];
109
113
debugv_print ("%s %%%d" , get_value_name (item ) ? get_value_name (item ) : "" , item -> id );
@@ -116,9 +120,9 @@ static LiftedCont* lambda_lift(Context* ctx, const Node* cont, String given_name
116
120
Nodes new_params = recreate_variables (& ctx -> rewriter , oparams );
117
121
118
122
LiftedCont * lifted_cont = calloc (sizeof (LiftedCont ), 1 );
119
- lifted_cont -> old_cont = cont ;
123
+ lifted_cont -> old_cont = liftee ;
120
124
lifted_cont -> save_values = recover_context ;
121
- insert_dict (const Node * , LiftedCont * , ctx -> lifted , cont , lifted_cont );
125
+ insert_dict (const Node * , LiftedCont * , ctx -> lifted , liftee , lifted_cont );
122
126
123
127
Context lifting_ctx = * ctx ;
124
128
lifting_ctx .rewriter = create_children_rewriter (& ctx -> rewriter );
@@ -175,15 +179,13 @@ static const Node* process_node(Context* ctx, const Node* node) {
175
179
Context fn_ctx = * ctx ;
176
180
fn_ctx .cfg = build_fn_cfg (node );
177
181
fn_ctx .uses = create_uses_map (node , (NcDeclaration | NcType ));
178
- fn_ctx .live_vars = compute_cfg_variables_map (fn_ctx .cfg );
179
182
fn_ctx .disable_lowering = lookup_annotation (node , "Internal" );
180
183
ctx = & fn_ctx ;
181
184
182
185
Node * new = recreate_decl_header_identity (& ctx -> rewriter , node );
183
186
recreate_decl_body_identity (& ctx -> rewriter , node , new );
184
187
185
188
destroy_uses_map (ctx -> uses );
186
- destroy_cfg_variables_map (ctx -> live_vars );
187
189
destroy_cfg (ctx -> cfg );
188
190
return new ;
189
191
}
@@ -205,7 +207,7 @@ static const Node* process_node(Context* ctx, const Node* node) {
205
207
206
208
const Node * otail = get_let_tail (node );
207
209
BodyBuilder * bb = begin_body (a );
208
- LiftedCont * lifted_tail = lambda_lift (ctx , otail , unique_name (a , format_string_arena (a -> arena , "post_control_ %s" , get_abstraction_name ( ctx -> cfg -> entry -> node ))));
210
+ LiftedCont * lifted_tail = lambda_lift (ctx , otail , unique_name (a , format_string_arena (a -> arena , "lifted %s" , get_abstraction_name_safe ( otail ))));
209
211
const Node * sp = add_spill_instrs (ctx , bb , lifted_tail -> save_values );
210
212
const Node * tail_ptr = fn_addr_helper (a , lifted_tail -> lifted_fn );
211
213
@@ -253,7 +255,7 @@ Module* lift_indirect_targets(const CompilerConfig* config, Module* src) {
253
255
}
254
256
destroy_dict (ctx .lifted );
255
257
destroy_rewriter (& ctx .rewriter );
256
- log_module (DEBUGVV , config , dst );
258
+ // log_module(DEBUGVV, config, dst);
257
259
verify_module (config , dst );
258
260
src = dst ;
259
261
if (oa )
0 commit comments