@@ -909,7 +909,7 @@ ANode *ANode::getChildANode(ANode *parent)
909909//
910910// changed to
911911// goto L0
912- // L0: (new empty BB)
912+ // L0:
913913// L1:
914914// goto L1
915915// 2) avoid a fall-thru BB of a backward goto BB is the target BB of another
@@ -921,99 +921,62 @@ ANode *ANode::getChildANode(ANode *parent)
921921// changed to
922922// L0:
923923// (p0) goto L0
924- // L : (new empty BB)
924+ // L :
925925// L1 :
926926// (p1) goto L1
927- // 3) make sure the last BB's pred is its physical predecessor.
928- // In another word, if there is a case:
929- // B0 : goto L1
930- // ...
931- // L0:
932- // ...
933- // B1: goto L0
934- // L1:
935- // ret
936- // changed to
937- // B0 : goto L
938- // ...
939- // L0:
940- // ...
941- // B1: goto L0
942- // L: (new empty BB)
943- // L1:
944- // ret
945- // This case is to guarantee that the last HG has its valid, non-null exit BB
946- // except Basic block ANode. (Without this, the last HG isn't handled completely
947- // with the current algo.)
948927void CFGStructurizer::preProcess ()
949928{
950929 bool CFGChanged = false ;
951930 for (BB_LIST_ITER BI = CFG->begin (), BE = CFG->end ();
952931 BI != BE; ++BI)
953932 {
954- bool insertEmptyBBBefore = false ;
955- bool isLastBB = (std::next (BI) == BE);
956933 G4_BB *B = *BI;
957- BB_LIST_ITER IT, IE;
958- if (isLastBB)
934+ if (B->Preds .size () < 2 )
959935 {
960- // case 3
961- if (B->Preds .size () >= 2 )
962- {
963- // Maybe too conservative, but shouldn't cause any perf change!
964- insertEmptyBBBefore = true ;
965- }
966- else if (B->Preds .size () == 1 )
967- {
968- G4_BB* phyPred = B->getPhysicalPred ();
969- G4_BB* pred = B->Preds .back ();
970- insertEmptyBBBefore = (phyPred != pred);
971- }
936+ continue ;
972937 }
973938
974- if (!insertEmptyBBBefore && B->Preds .size () >= 2 )
939+ // Check if this B is a successor of both backward and forward branches.
940+ bool isForwardTarget = false ;
941+ bool isBackwardTarget = false ;
942+ bool isFallThruOfBackwardGotoBB = false ;
943+ BB_LIST_ITER IT, IE;
944+ for (IT = B->Preds .begin (), IE = B->Preds .end (); IT != IE; IT++)
975945 {
976- // case 1 & 2
977- // Check if this B is a successor of both backward and forward branches.
978- bool isForwardTarget = false ;
979- bool isBackwardTarget = false ;
980- bool isFallThruOfBackwardGotoBB = false ;
981- for (IT = B->Preds .begin (), IE = B->Preds .end (); IT != IE; IT++)
946+ G4_BB* P = *IT;
947+ if (P->getId () >= B->getId ())
982948 {
983- G4_BB* P = *IT;
984- if (P->getId () >= B->getId ())
985- {
986- isBackwardTarget = true ;
987- continue ;
988- }
989- // P is a BB before B
990- G4_INST* gotoInstP = getGotoInst (P);
991- if (gotoInstP && P->getPhysicalSucc () != B)
992- {
993- isForwardTarget = true ;
994- }
995- else if (gotoInstP
996- && P->getPhysicalSucc () == B
997- && gotoInstP->asCFInst ()->isBackward ())
998- {
999- isFallThruOfBackwardGotoBB = true ;
1000- }
949+ isBackwardTarget = true ;
950+ continue ;
1001951 }
1002-
1003- if ( (isBackwardTarget && isForwardTarget) // case 1
1004- || (isBackwardTarget && isFallThruOfBackwardGotoBB)) // case 2
952+ // P is a BB before B
953+ G4_INST *gotoInst = getGotoInst (B);
954+ G4_INST* gotoInstP = getGotoInst (P);
955+ if (gotoInst && P->getPhysicalSucc () != B)
1005956 {
1006- insertEmptyBBBefore = true ;
957+ isForwardTarget = true ;
958+ }
959+ else if (gotoInstP
960+ && P->getPhysicalSucc () == B
961+ && gotoInstP->asCFInst ()->isBackward ())
962+ {
963+ isFallThruOfBackwardGotoBB = true ;
1007964 }
1008965 }
1009966
1010- if (!insertEmptyBBBefore)
967+ if ( !(isBackwardTarget && isForwardTarget) // case 1
968+ && !(isBackwardTarget && isFallThruOfBackwardGotoBB)) // case 2
1011969 {
970+ // not candidate
1012971 continue ;
1013972 }
1014973
1015- // Now, create an empty BB right before "B", and adjust all forward
1016- // branching to this new BB and leave all backward branching unchanged.
974+ // "B" is the target of both forward and backward branching, or is the
975+ // target of a backward branching and also the fall-thru of another
976+ // backward goto BB.
977+ //
978+ // Create an empty BB right before "B", and adjust all forward
979+ // branching to this new BB.
1017980 G4_BB *newBB = createBBWithLabel ();
1018981 G4_Label *newLabel = newBB->getLabel ();
1019982
@@ -2571,9 +2534,7 @@ void CFGStructurizer::constructPST(BB_LIST_ITER IB, BB_LIST_ITER IE)
25712534 G4_BB *bb = *II;
25722535 ANodeBB *ndbb = getANodeBB (bb);
25732536
2574- // Do the following cases in order. (Note that bb should be part of
2575- // the current pending ANode (top of ANStack), although it could
2576- // be the beginning node of the new inner ANode.)
2537+ // Do the following cases in order:
25772538 //
25782539 // 1: If bb is the target of some gotos, that is, the end of
25792540 // some CGs, merge the current bb into ANStack nodes of
@@ -2587,16 +2548,15 @@ void CFGStructurizer::constructPST(BB_LIST_ITER IB, BB_LIST_ITER IE)
25872548 // cg that is associated with a backward goto, a forward
25882549 // conditional goto, and the root (first) forward unconditional
25892550 // goto. No new HG is created for a non-root unconditional
2590- // goto (see details in code). This is where an inner HG is formed.)
2551+ // goto (see details in code).
25912552 //
25922553 // 3: Add bb into this node as it is part of node. If bb can
25932554 // be merged with its pred, merge it. The new node's type
25942555 // must be type AN_SEQUENCE.
25952556 //
25962557 // 4: Check if bb is the end of the node. If it is, finalize the
2597- // node. If it is not, go on to process the next BB in the next
2598- // iteration (This implies that if the next BB should be part
2599- // of the current pending HG in ANStack).
2558+ // node. If it is not, go on to process the next BB and continue
2559+ // the next iteration.
26002560 //
26012561
26022562 // case 1.
0 commit comments