@@ -20,12 +20,12 @@ void computeDominators(IRFunction* func, int** dominators) {
2020 }
2121
2222 bool changed = true;
23+ int * new_dom = (int * )malloc (sizeof (int ) * n );
24+ if (!new_dom ) { fprintf (stderr , "OOM\n" ); exit (1 ); }
2325 while (changed ) {
2426 changed = false;
2527 for (int i = 1 ; i < n ; i ++ ) { // Skip entry block
2628 IRBasicBlock * block = func -> blocks [i ];
27- int * new_dom = (int * )malloc (sizeof (int ) * n );
28- if (!new_dom ) { fprintf (stderr , "OOM\\n" ); exit (1 ); }
2929 for (int j = 0 ; j < n ; j ++ ) new_dom [j ] = 1 ;
3030
3131 for (int p = 0 ; p < block -> predCount ; p ++ ) {
@@ -42,12 +42,12 @@ void computeDominators(IRFunction* func, int** dominators) {
4242 changed = true;
4343 }
4444 }
45- free (new_dom );
4645 }
4746 }
47+ free (new_dom );
4848}
4949
50- void computeDominanceFrontiers (IRFunction * func , int * * dominators , int * * df ) {
50+ int * computeImmediateDominators (IRFunction * func , int * * dominators ) {
5151 int n = func -> blockCount ;
5252 int * idom = (int * )malloc (sizeof (int ) * n );
5353 for (int i = 0 ; i < n ; i ++ ) idom [i ] = -1 ;
@@ -61,9 +61,6 @@ void computeDominanceFrontiers(IRFunction* func, int** dominators, int** df) {
6161 for (int other_d = 0 ; other_d < n ; other_d ++ ) {
6262 if (dominators [i ][other_d ] && i != other_d && d != other_d ) {
6363 if (dominators [other_d ][d ]) {
64- // d dominates other_d, and other_d dominates i
65- // This means other_d is strictly between d and i.
66- // Therefore d cannot be the immediate dominator.
6764 is_idom = false;
6865 break ;
6966 }
@@ -73,6 +70,11 @@ void computeDominanceFrontiers(IRFunction* func, int** dominators, int** df) {
7370 }
7471 }
7572 }
73+ return idom ;
74+ }
75+
76+ void computeDominanceFrontiers (IRFunction * func , int * * dominators , int * idom , int * * df ) {
77+ int n = func -> blockCount ;
7678
7779 // Compute DF
7880 for (int i = 0 ; i < n ; i ++ ) {
@@ -88,7 +90,6 @@ void computeDominanceFrontiers(IRFunction* func, int** dominators, int** df) {
8890 }
8991 }
9092 }
91- free (idom );
9293}
9394
9495typedef struct {
@@ -291,7 +292,8 @@ void promoteMemoryToRegisters(IRFunction* func) {
291292 for (int i = 0 ; i < n ; i ++ ) df [i ] = (int * )calloc (n , sizeof (int ));
292293
293294 computeDominators (func , dominators );
294- computeDominanceFrontiers (func , dominators , df );
295+ int * idom = computeImmediateDominators (func , dominators );
296+ computeDominanceFrontiers (func , dominators , idom , df );
295297
296298 // Identify Allocas and their definitions
297299 int allocaCap = 1024 ;
@@ -375,21 +377,6 @@ void promoteMemoryToRegisters(IRFunction* func) {
375377 }
376378
377379 // 4. Renaming
378- int * idom = (int * )malloc (sizeof (int ) * n );
379- for (int i = 0 ; i < n ; i ++ ) idom [i ] = -1 ;
380- for (int i = 1 ; i < n ; i ++ ) {
381- for (int d = 0 ; d < n ; d ++ ) {
382- if (dominators [i ][d ] && i != d ) {
383- bool is_idom = true;
384- for (int other_d = 0 ; other_d < n ; other_d ++ ) {
385- if (dominators [i ][other_d ] && i != other_d && d != other_d && dominators [other_d ][d ]) {
386- is_idom = false; break ;
387- }
388- }
389- if (is_idom ) idom [i ] = d ;
390- }
391- }
392- }
393380
394381 int * * domChildren = (int * * )malloc (sizeof (int * ) * n );
395382 int * childCount = (int * )calloc (n , sizeof (int ));
0 commit comments