forked from moinak-hft/CS-202_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimiser.py
More file actions
878 lines (620 loc) · 23 KB
/
Copy pathoptimiser.py
File metadata and controls
878 lines (620 loc) · 23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
from pycparser import c_parser, c_ast
import networkx as nx
from networkx.drawing.nx_pydot import to_pydot
from graphviz import Digraph
import re
def preprocess_code(code):
code = re.sub(r'//.*', '', code)
code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
macros = {}
lines = code.splitlines()
filtered = []
for line in lines:
stripped = line.strip()
if stripped.startswith("#define"):
parts = stripped.split(maxsplit=2)
if len(parts) >= 3:
macro_name = parts[1]
macro_value = parts[2]
macros[macro_name] = macro_value
continue
filtered.append(line)
code = "\n".join(filtered)
for macro_name, macro_value in macros.items():
pattern = r'\b' + re.escape(macro_name) + r'\b'
code = re.sub(pattern, macro_value, code)
lines = code.splitlines()
filtered = []
for line in lines:
if line.strip().startswith("#include"):
continue
filtered.append(line)
return "\n".join(filtered)
with open("debug.c", "r") as f:
code = f.read()
code = preprocess_code(code)
print(code)
parser = c_parser.CParser()
ast = parser.parse(code)
class ConstantFolder:
def fold(self, node):
if node is None:
return None
for child_name, child in node.children():
new_child = self.fold(child)
if new_child is not child:
setattr(node, child_name, new_child)
if isinstance(node, c_ast.BinaryOp):
left = node.left
right = node.right
if isinstance(left, c_ast.Constant) and isinstance(right, c_ast.Constant):
try:
l = int(left.value)
r = int(right.value)
if node.op == '+':
val = l + r
elif node.op == '-':
val = l - r
elif node.op == '*':
val = l * r
elif node.op == '/':
if r == 0:
return node
val = l / r
elif node.op == '%':
if r == 0:
return node
val = l % r
elif node.op == '>':
val = int(l > r)
elif node.op == '<':
val = int(l < r)
elif node.op == '>=':
val = int(l >= r)
elif node.op == '<=':
val = int(l <= r)
elif node.op == '==':
val = int(l == r)
else:
return node
return c_ast.Constant(type='int', value=str(int(val)))
except:
return node
return node
def apply_constant_folding(ast):
folder = ConstantFolder()
folder.fold(ast)
class ConstantPropagator:
def __init__(self):
self.env = {}
def propagate_expr(self, expr):
if expr is None:
return None
if isinstance(expr, c_ast.ID):
if expr.name in self.env:
val = self.env[expr.name]
if isinstance(val, int):
return c_ast.Constant(type='int', value=str(val))
return expr
elif isinstance(expr, c_ast.BinaryOp):
expr.left = self.propagate_expr(expr.left)
expr.right = self.propagate_expr(expr.right)
return expr
elif isinstance(expr, c_ast.UnaryOp):
if expr.op == '&':
return expr
expr.expr = self.propagate_expr(expr.expr)
return expr
elif isinstance(expr, c_ast.FuncCall):
if expr.args:
for i in range(len(expr.args.exprs)):
expr.args.exprs[i] = self.propagate_expr(expr.args.exprs[i])
return expr
elif isinstance(expr, c_ast.ExprList):
for i in range(len(expr.exprs)):
expr.exprs[i] = self.propagate_expr(expr.exprs[i])
return expr
return expr
def visit(self, stmt):
if stmt is None:
return
if isinstance(stmt, c_ast.Assignment):
stmt.rvalue = self.propagate_expr(stmt.rvalue)
if isinstance(stmt.rvalue, c_ast.Constant):
self.env[stmt.lvalue.name] = int(stmt.rvalue.value)
else:
self.env[stmt.lvalue.name] = None
elif isinstance(stmt, c_ast.Decl):
if stmt.init:
stmt.init = self.propagate_expr(stmt.init)
if isinstance(stmt.init, c_ast.Constant):
self.env[stmt.name] = int(stmt.init.value)
else:
self.env[stmt.name] = None
elif isinstance(stmt, c_ast.FuncCall):
self.propagate_expr(stmt)
elif isinstance(stmt, c_ast.If):
stmt.cond = self.propagate_expr(stmt.cond)
old_env = self.env.copy()
self.visit(stmt.iftrue)
then_env = self.env.copy()
self.env = old_env.copy()
if stmt.iffalse:
self.visit(stmt.iffalse)
else_env = self.env.copy()
else:
else_env = old_env.copy()
new_env = {}
for var in set(list(then_env.keys()) + list(else_env.keys())):
if then_env.get(var) == else_env.get(var):
new_env[var] = then_env.get(var)
else:
new_env[var] = None
self.env = new_env
elif isinstance(stmt, c_ast.While):
stmt.cond = self.propagate_expr(stmt.cond)
old_env = self.env.copy()
self.visit(stmt.stmt)
loop_env = self.env.copy()
new_env = {}
for var in set(list(old_env.keys()) + list(loop_env.keys())):
if old_env.get(var) == loop_env.get(var):
new_env[var] = old_env.get(var)
else:
new_env[var] = None
self.env = new_env
elif isinstance(stmt, c_ast.Compound):
if stmt.block_items:
for s in stmt.block_items:
self.visit(s)
elif isinstance(stmt, c_ast.Return):
stmt.expr = self.propagate_expr(stmt.expr)
def apply_constant_propagation(ast):
cp = ConstantPropagator()
for ext in ast.ext:
if isinstance(ext, c_ast.FuncDef):
cp.visit(ext.body)
def is_constant_true(expr):
return isinstance(expr, c_ast.Constant) and expr.value != '0'
def is_constant_false(expr):
return isinstance(expr, c_ast.Constant) and expr.value == '0'
def is_exit_call(stmt):
if isinstance(stmt, c_ast.FuncCall):
if isinstance(stmt.name, c_ast.ID):
return stmt.name.name in ["exit", "abort"]
return False
def always_terminates(stmt):
if isinstance(stmt, c_ast.Return):
return True
if is_exit_call(stmt):
return True
if isinstance(stmt, c_ast.Compound):
if not stmt.block_items:
return False
for s in stmt.block_items:
if always_terminates(s):
return True
return False
if isinstance(stmt, c_ast.If):
if stmt.iffalse is None:
return False
return always_terminates(stmt.iftrue) and always_terminates(stmt.iffalse)
return False
def has_side_effect(expr):
if expr is None:
return False
if isinstance(expr, c_ast.FuncCall):
return True
if isinstance(expr, c_ast.Assignment):
return True
if isinstance(expr, c_ast.UnaryOp):
if expr.op in ["p++", "p--", "++", "--"]:
return True
return has_side_effect(expr.expr)
if isinstance(expr, c_ast.BinaryOp):
return has_side_effect(expr.left) or has_side_effect(expr.right)
if isinstance(expr, c_ast.ArrayRef):
return has_side_effect(expr.name) or has_side_effect(expr.subscript)
if isinstance(expr, c_ast.ExprList):
return any(has_side_effect(e) for e in expr.exprs)
return False
class SimpleDCE:
def process_block(self, block):
if not block.block_items:
return
new_stmts = []
terminated = False
for stmt in block.block_items:
if terminated:
continue
if isinstance(stmt, c_ast.If):
cond = stmt.cond
if is_constant_true(cond):
self.process(stmt.iftrue)
new_stmts.append(stmt.iftrue)
if always_terminates(stmt.iftrue):
terminated = True
elif is_constant_false(cond):
if stmt.iffalse:
self.process(stmt.iffalse)
new_stmts.append(stmt.iffalse)
if always_terminates(stmt.iffalse):
terminated = True
else:
self.process(stmt.iftrue)
if stmt.iffalse:
self.process(stmt.iffalse)
new_stmts.append(stmt)
if stmt.iffalse and always_terminates(stmt.iftrue) and always_terminates(stmt.iffalse):
terminated = True
elif isinstance(stmt, c_ast.While):
if is_constant_false(stmt.cond):
continue
self.process(stmt.stmt)
new_stmts.append(stmt)
if is_constant_true(stmt.cond) and always_terminates(stmt.stmt):
terminated = True
elif isinstance(stmt, c_ast.For):
if stmt.cond and is_constant_false(stmt.cond):
continue
self.process(stmt.stmt)
new_stmts.append(stmt)
elif isinstance(stmt, c_ast.Return):
new_stmts.append(stmt)
terminated = True
elif is_exit_call(stmt):
new_stmts.append(stmt)
terminated = True
elif isinstance(stmt, c_ast.Compound):
self.process(stmt)
new_stmts.append(stmt)
if always_terminates(stmt):
terminated = True
else:
new_stmts.append(stmt)
block.block_items = new_stmts
def process(self, stmt):
if isinstance(stmt, c_ast.Compound):
self.process_block(stmt)
elif isinstance(stmt, c_ast.If):
self.process(stmt.iftrue)
if stmt.iffalse:
self.process(stmt.iffalse)
elif isinstance(stmt, c_ast.While):
self.process(stmt.stmt)
elif isinstance(stmt, c_ast.For):
self.process(stmt.stmt)
def apply_simple_dce(ast):
dce = SimpleDCE()
for ext in ast.ext:
if isinstance(ext, c_ast.FuncDef):
dce.process(ext.body)
def get_vars(expr):
vars = set()
if expr is None:
return vars
if isinstance(expr, c_ast.ID):
vars.add(expr.name)
elif isinstance(expr, c_ast.BinaryOp):
vars |= get_vars(expr.left)
vars |= get_vars(expr.right)
elif isinstance(expr, c_ast.UnaryOp):
vars |= get_vars(expr.expr)
elif isinstance(expr, c_ast.ArrayRef):
vars |= get_vars(expr.name)
vars |= get_vars(expr.subscript)
elif isinstance(expr, c_ast.FuncCall):
if expr.args:
for a in expr.args.exprs:
vars |= get_vars(a)
elif isinstance(expr, c_ast.ExprList):
for e in expr.exprs:
vars |= get_vars(e)
return vars
class UnusedAssignmentEliminator:
def process_block(self, block):
if not block.block_items:
return
live = set()
new_stmts = []
for stmt in reversed(block.block_items):
if isinstance(stmt, c_ast.Assignment):
lhs = stmt.lvalue.name
rhs_vars = get_vars(stmt.rvalue)
if lhs not in live:
live |= rhs_vars
if not rhs_vars and not has_side_effect(stmt.rvalue):
continue
new_stmts.append(stmt)
continue
live.discard(lhs)
live |= rhs_vars
new_stmts.append(stmt)
elif isinstance(stmt, c_ast.Decl):
if stmt.init:
lhs = stmt.name
rhs_vars = get_vars(stmt.init)
if lhs not in live:
live |= rhs_vars
if not rhs_vars and not has_side_effect(stmt.init):
continue
new_stmts.append(stmt)
continue
live.discard(lhs)
live |= rhs_vars
new_stmts.append(stmt)
else:
new_stmts.append(stmt)
elif isinstance(stmt, c_ast.Return):
if stmt.expr:
live |= get_vars(stmt.expr)
new_stmts.append(stmt)
elif isinstance(stmt, c_ast.FuncCall):
live |= get_vars(stmt)
new_stmts.append(stmt)
elif isinstance(stmt, c_ast.If):
self.process(stmt.iftrue)
if stmt.iffalse:
self.process(stmt.iffalse)
live |= get_vars(stmt.cond)
new_stmts.append(stmt)
elif isinstance(stmt, c_ast.While):
self.process(stmt.stmt)
live |= get_vars(stmt.cond)
new_stmts.append(stmt)
elif isinstance(stmt, c_ast.For):
self.process(stmt.stmt)
if stmt.cond:
live |= get_vars(stmt.cond)
new_stmts.append(stmt)
elif isinstance(stmt, c_ast.Compound):
self.process(stmt)
new_stmts.append(stmt)
else:
new_stmts.append(stmt)
block.block_items = list(reversed(new_stmts))
def process(self, stmt):
if isinstance(stmt, c_ast.Compound):
self.process_block(stmt)
elif isinstance(stmt, c_ast.If):
self.process(stmt.iftrue)
if stmt.iffalse:
self.process(stmt.iffalse)
elif isinstance(stmt, c_ast.While):
self.process(stmt.stmt)
elif isinstance(stmt, c_ast.For):
self.process(stmt.stmt)
def apply_unused_assignment_elimination(ast):
uae = UnusedAssignmentEliminator()
for ext in ast.ext:
if isinstance(ext, c_ast.FuncDef):
uae.process(ext.body)
def expr_to_str(expr):
if expr is None:
return ""
if isinstance(expr, c_ast.Constant):
return expr.value
elif isinstance(expr, c_ast.ID):
return expr.name
elif isinstance(expr, c_ast.BinaryOp):
return f"{expr_to_str(expr.left)} {expr.op} {expr_to_str(expr.right)}"
elif isinstance(expr, c_ast.Assignment):
return f"{expr_to_str(expr.lvalue)} = {expr_to_str(expr.rvalue)}"
elif isinstance(expr, c_ast.UnaryOp):
if expr.op in ['p++', 'p--']:
return f"{expr_to_str(expr.expr)}{expr.op[1:]}"
return f"{expr.op}{expr_to_str(expr.expr)}"
elif isinstance(expr, c_ast.FuncCall):
args = ""
if expr.args:
args = ", ".join(expr_to_str(a) for a in expr.args.exprs)
return f"{expr_to_str(expr.name)}({args})"
elif isinstance(expr, c_ast.ExprList):
return ", ".join(expr_to_str(e) for e in expr.exprs)
return "expr"
def get_stmt_label(stmt):
if isinstance(stmt, c_ast.Assignment):
return f"{expr_to_str(stmt.lvalue)} = {expr_to_str(stmt.rvalue)};"
elif isinstance(stmt, c_ast.Decl):
if stmt.init:
return f"{stmt.name} = {expr_to_str(stmt.init)};"
return f"int {stmt.name};"
elif isinstance(stmt, c_ast.Return):
return f"return {expr_to_str(stmt.expr)};"
elif isinstance(stmt, c_ast.If):
return f"if ({expr_to_str(stmt.cond)})"
elif isinstance(stmt, c_ast.While):
return f"while ({expr_to_str(stmt.cond)})"
elif isinstance(stmt, c_ast.FuncCall):
return f"{expr_to_str(stmt)};"
return type(stmt).__name__
ast_graph = nx.DiGraph()
ast_id = 0
def build_ast_graph(node, parent=None):
global ast_id
curr = ast_id
ast_id += 1
label = type(node).__name__
ast_graph.add_node(curr, label=label)
if parent is not None:
ast_graph.add_edge(parent, curr)
for _, child in node.children():
build_ast_graph(child, curr)
build_ast_graph(ast)
ast_pydot = to_pydot(ast_graph)
for n in ast_graph.nodes(data=True):
ast_pydot.get_node(str(n[0]))[0].set_label(n[1]['label'])
ast_pydot.write_png("ast.png")
cfg = nx.DiGraph()
node_id = 0
function_nodes = {}
def new_node(label):
global node_id
nid = node_id
node_id += 1
cfg.add_node(nid, label=label)
return nid
entry = new_node("ENTRY")
def build_cfg(stmt):
if isinstance(stmt, (c_ast.Decl, c_ast.Assignment, c_ast.FuncCall)):
n = new_node(get_stmt_label(stmt))
return n, n
elif isinstance(stmt, c_ast.Return):
n = new_node(get_stmt_label(stmt))
return n, n
elif isinstance(stmt, c_ast.If):
cond = new_node(get_stmt_label(stmt))
then_start, then_end = build_cfg(stmt.iftrue)
if stmt.iffalse:
else_start, else_end = build_cfg(stmt.iffalse)
else:
else_start = else_end = None
if then_start:
cfg.add_edge(cond, then_start, label="T")
if else_start:
cfg.add_edge(cond, else_start, label="F")
merge = new_node("merge")
if then_end:
cfg.add_edge(then_end, merge)
if else_end:
cfg.add_edge(else_end, merge)
if stmt.iffalse is None:
cfg.add_edge(cond, merge, label="F")
return cond, merge
elif isinstance(stmt, c_ast.While):
cond = new_node(get_stmt_label(stmt))
body_start, body_end = build_cfg(stmt.stmt)
if body_start:
cfg.add_edge(cond, body_start, label="T")
if body_end:
cfg.add_edge(body_end, cond)
exit_node = new_node("exit while")
cfg.add_edge(cond, exit_node, label="F")
return cond, exit_node
elif isinstance(stmt, c_ast.Compound):
if not stmt.block_items:
return None, None
start = None
prev_end = None
for s in stmt.block_items:
s_start, s_end = build_cfg(s)
if start is None:
start = s_start
if prev_end is not None and s_start is not None:
cfg.add_edge(prev_end, s_start)
prev_end = s_end
return start, prev_end
elif isinstance(stmt, c_ast.For):
if stmt.init:
init_start, init_end = build_cfg(stmt.init)
else:
init_start = init_end = None
cond_label = "for"
if stmt.cond:
cond_label = f"for ({expr_to_str(stmt.cond)})"
cond = new_node(cond_label)
body_start, body_end = build_cfg(stmt.stmt)
if stmt.next:
next_start, next_end = build_cfg(stmt.next)
else:
next_start = next_end = None
if init_end is not None:
cfg.add_edge(init_end, cond)
elif init_start is not None:
cfg.add_edge(init_start, cond)
if body_start:
cfg.add_edge(cond, body_start, label="T")
if body_end and next_start:
cfg.add_edge(body_end, next_start)
if next_end:
cfg.add_edge(next_end, cond)
elif body_end:
cfg.add_edge(body_end, cond)
exit_node = new_node("exit for")
cfg.add_edge(cond, exit_node, label="F")
if init_start:
return init_start, exit_node
else:
return cond, exit_node
else:
n = new_node(type(stmt).__name__)
return n, n
def buildandsavecfggraphfinalwala(ast, filename, printingname):
global cfg, node_id, entry, function_nodes
cfg.clear()
node_id = 0
function_nodes = {}
entry = new_node("ENTRY")
for ext in ast.ext:
if isinstance(ext, c_ast.FuncDef):
function_nodes[ext.decl.name] = new_node(f"{ext.decl.name}()")
for ext in ast.ext:
if isinstance(ext, c_ast.FuncDef):
func_node = function_nodes[ext.decl.name]
start, end = build_cfg(ext.body)
if start is not None:
cfg.add_edge(func_node, start)
if ext.decl.name == "main":
cfg.add_edge(entry, func_node)
cfg_pydot = to_pydot(cfg)
for n in cfg.nodes(data=True):
cfg_pydot.get_node(str(n[0]))[0].set_label(n[1]['label'])
for u, v, d in cfg.edges(data=True):
if 'label' in d:
edge = cfg_pydot.get_edge(str(u), str(v))[0]
edge.set_label(d['label'])
cfg_pydot.write_png(filename)
print(printingname)
def build_pipeline_graph(stages):
g = Digraph(format='png')
g.attr(rankdir='LR')
for i, (name, img) in enumerate(stages):
g.node(name, label="", image=img, shape='rectangle')
if i > 0:
prev_name = stages[i - 1][0]
if "prop" in name:
label = f"{int(i / 3) + 1}_prop"
elif "fold" in name:
label = f"{int(i / 3) + 1}_fold"
elif "dce" in name:
label = f"{int(i / 3) + 1}_dce"
else:
label = "final_UAE"
g.edge(prev_name, name, label=label)
g.render("pipeline_graph")
def ast_to_string(ast):
return str(ast)
def optimize_until_fixed(ast, max_iter=50):
prev = None
i = 0
stages = []
for _ in range(max_iter):
i = i + 1
curr = ast_to_string(ast)
if curr == prev:
break
prev = curr
apply_constant_propagation(ast)
fname = f"cfg_iter_{i}_prop.png"
buildandsavecfggraphfinalwala(ast, fname, f"{i}_prop")
stages.append((f"iter{i}_prop", fname))
apply_constant_folding(ast)
fname = f"cfg_iter_{i}_fold.png"
buildandsavecfggraphfinalwala(ast, fname, f"{i}_fold")
stages.append((f"iter{i}_fold", fname))
apply_simple_dce(ast)
fname = f"cfg_iter_{i}_dce.png"
buildandsavecfggraphfinalwala(ast, fname, f"{i}_dce")
stages.append((f"iter{i}_dce", fname))
apply_constant_propagation(ast)
apply_constant_folding(ast)
apply_unused_assignment_elimination(ast)
apply_simple_dce(ast)
fname = f"cfg_iter_{i}_UAE.png"
buildandsavecfggraphfinalwala(ast, fname, f"{i}_UAE")
stages.append((f"iter{i}_UAE", fname))
build_pipeline_graph(stages)
return ast
buildandsavecfggraphfinalwala(ast, "cfg_original.png", "original")
optimize_until_fixed(ast)
buildandsavecfggraphfinalwala(ast, "cfg_optimized.png", "final")