+/*-----------------------------------------------------------------*/
+/* addPostLoopBlock - add a ebblock before the successors of the */
+/* loop exits */
+/*-----------------------------------------------------------------*/
+static void
+addPostLoopBlock (region *loopReg, ebbIndex *ebbi, iCode *ic)
+{
+ bitVect *loopSuccs;
+ int i;
+
+ /* if the number of exits is greater than one then
+ we use another trick: we will create an intersection
+ of succesors of the exits, then take those that are not
+ part of the loop and have dfNumber greater loop entry (eblock),
+ insert a new predecessor postLoopBlk before them and add
+ a copy of ic in the new block. The postLoopBlk in between
+ is necessary, because the old successors of the loop exits can
+ be successors of other blocks too: see bug-136564. */
+
+ /* loopSuccs now contains intersection
+ of all the loops successors */
+ loopSuccs = intersectLoopSucc (loopReg->exits, ebbi->dfOrder);
+ if (!loopSuccs)
+ return;
+
+ for (i = 0; i < loopSuccs->size; i++)
+ {
+ eBBlock *eblock = NULL;
+ eBBlock *postLoopBlk;
+ iCode *newic;
+ int j;
+
+ if (!bitVectBitValue (loopSuccs, i))
+ continue;
+
+ /* Need to search for bbnum == i since ebbi->dfOrder is
+ sorted by dfnum; a direct index won't do. */
+ for (j = 0; j < ebbi->count; j++)
+ if (ebbi->dfOrder[j]->bbnum == i)
+ {
+ eblock = ebbi->dfOrder[j];
+ break;
+ }
+ assert(eblock);
+
+ /* if the successor does not belong to the loop
+ and will be executed after the loop : then
+ add a definition to the block */
+ if (isinSet (loopReg->regBlocks, eblock))
+ continue;
+ if (eblock->dfnum <= loopReg->entry->dfnum)
+ continue;
+
+ /* look for an existing loopExitBlock */
+ if (strncmp (LOOPEXITLBL,
+ eblock->entryLabel->name,
+ sizeof(LOOPEXITLBL) - 1) == 0)
+ {
+ /* reuse the existing one */
+ postLoopBlk = eblock;
+ }
+ else
+ {
+ /* create and insert a new eBBlock.
+ Damn, that's messy ... */
+ int i;
+ set *oldPredList;
+ eBBlock *ebpi;
+
+ postLoopBlk = neweBBlock();
+
+ /* create a loopExit-label */
+ postLoopBlk->entryLabel = newiTempLoopHeaderLabel (0);
+
+ /* increase bbnum for all blocks after
+ (and including) eblock */
+ for (i = 0; i < ebbi->count; i++)
+ {
+ if (ebbi->bbOrder[i]->bbnum >= eblock->bbnum)
+ ++ebbi->bbOrder[i]->bbnum;
+ }
+ /* insert postLoopBlk before bbnum */
+ postLoopBlk->bbnum = eblock->bbnum - 1;
+
+ ++ebbi->count;
+
+ /* these arrays need one more block, which ... */
+ ebbi->bbOrder = Safe_realloc (ebbi->bbOrder,
+ (ebbi->count + 1) * sizeof(eBBlock *));
+ /* ... must be initialized with 0 */
+ ebbi->bbOrder[ebbi->count] = NULL;
+ /* move the blocks up ... */
+ memmove (&ebbi->bbOrder[postLoopBlk->bbnum + 1],
+ &ebbi->bbOrder[postLoopBlk->bbnum],
+ (ebbi->count - postLoopBlk->bbnum - 1) * sizeof(eBBlock *));
+ /* ... and insert postLoopBlk */
+ ebbi->bbOrder[postLoopBlk->bbnum] = postLoopBlk;
+
+ /* just add postLoopBlk at the end of dfOrder,
+ computeControlFlow() will compute the new dfnum */
+ ebbi->dfOrder = Safe_realloc (ebbi->dfOrder,
+ (ebbi->count + 1) * sizeof(eBBlock *));
+ ebbi->dfOrder[ebbi->count] = NULL;
+ ebbi->dfOrder[ebbi->count - 1] = postLoopBlk;
+
+ /* copy loop information from eblock to postLoopBlk */
+ if (eblock->partOfLoop)
+ {
+ postLoopBlk->partOfLoop = eblock->partOfLoop;
+ /* add postLoopBlk to loop region */
+ addSetHead (&postLoopBlk->partOfLoop->regBlocks, postLoopBlk);
+ }
+
+ /* go through loop exits and replace the old exit block eblock
+ with the new postLoopBlk */
+ for (ebpi = setFirstItem (loopReg->exits);
+ ebpi;
+ ebpi = setNextItem (loopReg->exits))
+ {
+ /* replace old label with new label */
+ replaceLabel (ebpi, eblock->entryLabel, postLoopBlk->entryLabel);
+ }
+
+ /* now eblock is replaced by postLoopBlk.
+ It's possible, that eblock has an immediate predecessor
+ (with ebpi->bbnum + 1 == eblock->bbnum), which isn't part of the
+ loop. This predecessor must stay predecessor of eblock, not of
+ postLoopBlk. But now postLoopBlk is in between, therefore a GOTO
+ from this predecessor to eblock must be appended.
+ Example: bug-136564.c */
+
+ /* take all predecessors and subtract the loop exits */
+ oldPredList = subtractFromSet (eblock->predList,
+ loopReg->exits,
+ THROW_NONE);
+ for (ebpi = setFirstItem (oldPredList);
+ ebpi;
+ ebpi = setNextItem (oldPredList))
+ {
+ /* Is it an immediate predecessor (without GOTO)?
+ All other predecessors end with a
+ GOTO, IF, IFX or JUMPTABLE: nothing to to do */
+ if (ebpi->bbnum + 1 == postLoopBlk->bbnum)
+ {
+ /* insert goto to old predecessor of eblock */
+ newic = newiCodeLabelGoto (GOTO, eblock->entryLabel);
+ addiCodeToeBBlock (ebpi, newic, NULL);
+ break; /* got it, only one is possible */
+ }
+ }
+
+ /* set the label */
+ postLoopBlk->sch =
+ postLoopBlk->ech = newiCodeLabelGoto (LABEL, postLoopBlk->entryLabel);
+ }
+
+ /* create the definition in postLoopBlk */
+ newic = newiCode ('=', NULL, operandFromOperand (IC_RIGHT (ic)));
+ IC_RESULT (newic) = operandFromOperand (IC_RESULT (ic));
+ /* maintain data flow */
+ OP_DEFS(IC_RESULT (newic)) = bitVectSetBit (OP_DEFS (IC_RESULT (newic)),
+ newic->key);
+ OP_USES(IC_RIGHT (newic)) = bitVectSetBit (OP_USES (IC_RIGHT (newic)),
+ newic->key);
+
+ /* and add it */
+ addiCodeToeBBlock (postLoopBlk, newic, NULL);
+ postLoopBlk->sch->filename =
+ postLoopBlk->ech->filename = eblock->sch->filename;
+ postLoopBlk->sch->lineno =
+ postLoopBlk->ech->lineno = eblock->sch->lineno;
+
+ /* outDefs is needed by computeControlFlow(), anything
+ else will be set up by computeControlFlow() */
+ postLoopBlk->outDefs = bitVectSetBit (postLoopBlk->outDefs, newic->key);
+ } /* for (i = 0; i < loopSuccs->size; i++) */
+
+ /* the postLoopBlk and the induction significantly
+ changed the control flow, recompute it */
+ computeControlFlow (ebbi);
+}
+