control.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /*********************************************************************
  2. * Description : Performs control flow analysis on the CFG
  3. * (C) Cristina Cifuentes
  4. ********************************************************************/
  5. #include <algorithm>
  6. #include <list>
  7. #include <cassert>
  8. #include "dcc.h"
  9. #include <stdio.h>
  10. #include <string.h>
  11. #if __BORLAND__
  12. #include <alloc.h>
  13. #else
  14. #include <malloc.h>
  15. #endif
  16. //typedef struct list {
  17. // Int nodeIdx;
  18. // struct list *next;
  19. //} nodeList;
  20. typedef std::list<Int> nodeList; /* dfsLast index to the node */
  21. #define ancestor(a,b) ((a->dfsLastNum < b->dfsLastNum) && (a->dfsFirstNum < b->dfsFirstNum))
  22. /* there is a path on the DFST from a to b if the a was first visited in a
  23. * dfs, and a was later visited than b when doing the last visit of each
  24. * node. */
  25. /* Checks if the edge (p,s) is a back edge. If node s was visited first
  26. * during the dfs traversal (ie. s has a smaller dfsFirst number) or s == p,
  27. * then it is a backedge.
  28. * Also incrementes the number of backedges entries to the header node. */
  29. static boolT isBackEdge (BB * p,BB * s)
  30. {
  31. if (p->dfsFirstNum >= s->dfsFirstNum)
  32. {
  33. s->numBackEdges++;
  34. return true;
  35. }
  36. return false;
  37. }
  38. /* Finds the common dominator of the current immediate dominator
  39. * currImmDom and its predecessor's immediate dominator predImmDom */
  40. static Int commonDom (Int currImmDom, Int predImmDom, Function * pProc)
  41. {
  42. if (currImmDom == NO_DOM)
  43. return (predImmDom);
  44. if (predImmDom == NO_DOM) /* predecessor is the root */
  45. return (currImmDom);
  46. while ((currImmDom != NO_DOM) && (predImmDom != NO_DOM) &&
  47. (currImmDom != predImmDom))
  48. {
  49. if (currImmDom < predImmDom)
  50. predImmDom = pProc->dfsLast[predImmDom]->immedDom;
  51. else
  52. currImmDom = pProc->dfsLast[currImmDom]->immedDom;
  53. }
  54. return (currImmDom);
  55. }
  56. /* Finds the immediate dominator of each node in the graph pProc->cfg.
  57. * Adapted version of the dominators algorithm by Hecht and Ullman; finds
  58. * immediate dominators only.
  59. * Note: graph should be reducible */
  60. void Function::findImmedDom ()
  61. {
  62. BB * currNode;
  63. Int currIdx, j, predIdx;
  64. for (currIdx = 0; currIdx < numBBs; currIdx++)
  65. {
  66. currNode = dfsLast[currIdx];
  67. if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
  68. continue;
  69. for (j = 0; j < currNode->inEdges.size(); j++)
  70. {
  71. BB* inedge=currNode->inEdges[j];
  72. predIdx = inedge->dfsLastNum;
  73. if (predIdx < currIdx)
  74. currNode->immedDom = commonDom (currNode->immedDom, predIdx, this);
  75. }
  76. }
  77. }
  78. /* Inserts the node n to the list l. */
  79. static void insertList (nodeList &l, Int n)
  80. {
  81. l.push_back(n);
  82. }
  83. /* Returns whether or not the node n (dfsLast numbering of a basic block)
  84. * is on the list l. */
  85. static boolT inList (nodeList &l, Int n)
  86. {
  87. return std::find(l.begin(),l.end(),n)!=l.end();
  88. }
  89. /* Frees space allocated by the list l. */
  90. static void freeList (nodeList &l)
  91. {
  92. l.clear();
  93. }
  94. /* Returns whether the node n belongs to the queue list q. */
  95. static boolT inInt(BB * n, queue &q)
  96. {
  97. return std::find(q.begin(),q.end(),n)!=q.end();
  98. }
  99. /* Finds the follow of the endless loop headed at node head (if any).
  100. * The follow node is the closest node to the loop. */
  101. static void findEndlessFollow (Function * pProc, nodeList &loopNodes, BB * head)
  102. {
  103. Int j, succ;
  104. head->loopFollow = MAX;
  105. nodeList::iterator p = loopNodes.begin();
  106. for( ;p != loopNodes.end();++p)
  107. {
  108. for (j = 0; j < pProc->dfsLast[*p]->numOutEdges; j++)
  109. {
  110. succ = pProc->dfsLast[*p]->edges[j].BBptr->dfsLastNum;
  111. if ((! inList(loopNodes, succ)) && (succ < head->loopFollow))
  112. head->loopFollow = succ;
  113. }
  114. }
  115. }
  116. //static void findNodesInLoop(BB * latchNode,BB * head,PPROC pProc,queue *intNodes)
  117. /* Flags nodes that belong to the loop determined by (latchNode, head) and
  118. * determines the type of loop. */
  119. static void findNodesInLoop(BB * latchNode,BB * head,Function * pProc,queue &intNodes)
  120. {
  121. Int i, headDfsNum, intNodeType;
  122. nodeList loopNodes;
  123. Int immedDom, /* dfsLast index to immediate dominator */
  124. thenDfs, elseDfs; /* dsfLast index for THEN and ELSE nodes */
  125. BB * pbb;
  126. /* Flag nodes in loop headed by head (except header node) */
  127. headDfsNum = head->dfsLastNum;
  128. head->loopHead = headDfsNum;
  129. insertList (loopNodes, headDfsNum);
  130. for (i = headDfsNum + 1; i < latchNode->dfsLastNum; i++)
  131. {
  132. if (pProc->dfsLast[i]->flg & INVALID_BB) /* skip invalid BBs */
  133. continue;
  134. immedDom = pProc->dfsLast[i]->immedDom;
  135. if (inList (loopNodes, immedDom) && inInt(pProc->dfsLast[i], intNodes))
  136. {
  137. insertList (loopNodes, i);
  138. if (pProc->dfsLast[i]->loopHead == NO_NODE)/*not in other loop*/
  139. pProc->dfsLast[i]->loopHead = headDfsNum;
  140. }
  141. }
  142. latchNode->loopHead = headDfsNum;
  143. if (latchNode != head)
  144. insertList (loopNodes, latchNode->dfsLastNum);
  145. /* Determine type of loop and follow node */
  146. intNodeType = head->nodeType;
  147. if (latchNode->nodeType == TWO_BRANCH)
  148. if ((intNodeType == TWO_BRANCH) || (latchNode == head))
  149. if ((latchNode == head) ||
  150. (inList (loopNodes, head->edges[THEN].BBptr->dfsLastNum) &&
  151. inList (loopNodes, head->edges[ELSE].BBptr->dfsLastNum)))
  152. {
  153. head->loopType = REPEAT_TYPE;
  154. if (latchNode->edges[0].BBptr == head)
  155. head->loopFollow = latchNode->edges[ELSE].BBptr->dfsLastNum;
  156. else
  157. head->loopFollow = latchNode->edges[THEN].BBptr->dfsLastNum;
  158. latchNode->back().SetLlFlag(JX_LOOP);
  159. }
  160. else
  161. {
  162. head->loopType = WHILE_TYPE;
  163. if (inList (loopNodes, head->edges[THEN].BBptr->dfsLastNum))
  164. head->loopFollow = head->edges[ELSE].BBptr->dfsLastNum;
  165. else
  166. head->loopFollow = head->edges[THEN].BBptr->dfsLastNum;
  167. head->back().SetLlFlag(JX_LOOP);
  168. }
  169. else /* head = anything besides 2-way, latch = 2-way */
  170. {
  171. head->loopType = REPEAT_TYPE;
  172. if (latchNode->edges[THEN].BBptr == head)
  173. head->loopFollow = latchNode->edges[ELSE].BBptr->dfsLastNum;
  174. else
  175. head->loopFollow = latchNode->edges[THEN].BBptr->dfsLastNum;
  176. latchNode->back().SetLlFlag(JX_LOOP);
  177. }
  178. else /* latch = 1-way */
  179. if (latchNode->nodeType == LOOP_NODE)
  180. {
  181. head->loopType = REPEAT_TYPE;
  182. head->loopFollow = latchNode->edges[0].BBptr->dfsLastNum;
  183. }
  184. else if (intNodeType == TWO_BRANCH)
  185. {
  186. head->loopType = WHILE_TYPE;
  187. pbb = latchNode;
  188. thenDfs = head->edges[THEN].BBptr->dfsLastNum;
  189. elseDfs = head->edges[ELSE].BBptr->dfsLastNum;
  190. while (1)
  191. {
  192. if (pbb->dfsLastNum == thenDfs)
  193. {
  194. head->loopFollow = elseDfs;
  195. break;
  196. }
  197. else if (pbb->dfsLastNum == elseDfs)
  198. {
  199. head->loopFollow = thenDfs;
  200. break;
  201. }
  202. /* Check if couldn't find it, then it is a strangely formed
  203. * loop, so it is safer to consider it an endless loop */
  204. if (pbb->dfsLastNum <= head->dfsLastNum)
  205. {
  206. head->loopType = ENDLESS_TYPE;
  207. findEndlessFollow (pProc, loopNodes, head);
  208. break;
  209. }
  210. pbb = pProc->dfsLast[pbb->immedDom];
  211. }
  212. if (pbb->dfsLastNum > head->dfsLastNum)
  213. pProc->dfsLast[head->loopFollow]->loopHead = NO_NODE; /*****/
  214. head->back().SetLlFlag(JX_LOOP);
  215. }
  216. else
  217. {
  218. head->loopType = ENDLESS_TYPE;
  219. findEndlessFollow (pProc, loopNodes, head);
  220. }
  221. freeList(loopNodes);
  222. }
  223. //static void findNodesInInt (queue **intNodes, Int level, interval *Ii)
  224. /* Recursive procedure to find nodes that belong to the interval (ie. nodes
  225. * from G1). */
  226. static void findNodesInInt (queue &intNodes, Int level, interval *Ii)
  227. {
  228. if (level == 1)
  229. {
  230. std::for_each(Ii->nodes.begin(),Ii->nodes.end(),[&intNodes](BB *en)->void {
  231. appendQueue(intNodes,en);
  232. });
  233. }
  234. else
  235. std::for_each(Ii->nodes.begin(),Ii->nodes.end(),[&intNodes,level](BB *en)->void {
  236. findNodesInInt(intNodes,level-1,en->correspInt);
  237. });
  238. }
  239. /* Algorithm for structuring loops */
  240. void Function::structLoops(derSeq *derivedG)
  241. {
  242. interval *Ii;
  243. BB * intHead, /* interval header node */
  244. * pred, /* predecessor node */
  245. * latchNode;/* latching node (in case of loops) */
  246. Int i, /* counter */
  247. level = 0; /* derived sequence level */
  248. interval *initInt; /* initial interval */
  249. queue intNodes; /* list of interval nodes */
  250. /* Structure loops */
  251. /* for all derived sequences Gi */
  252. for(derSeq::iterator iter=derivedG->begin(); iter!=derivedG->end(); ++iter)
  253. {
  254. level++;
  255. Ii = iter->Ii;
  256. while (Ii) /* for all intervals Ii of Gi */
  257. {
  258. latchNode = NULL;
  259. intNodes.clear();
  260. /* Find interval head (original BB node in G1) and create
  261. * list of nodes of interval Ii. */
  262. initInt = Ii;
  263. for (i = 1; i < level; i++)
  264. initInt = (*initInt->nodes.begin())->correspInt;
  265. intHead = *initInt->nodes.begin();
  266. /* Find nodes that belong to the interval (nodes from G1) */
  267. findNodesInInt (intNodes, level, Ii);
  268. /* Find greatest enclosing back edge (if any) */
  269. for (i = 0; i < intHead->inEdges.size(); i++)
  270. {
  271. pred = intHead->inEdges[i];
  272. if (inInt(pred, intNodes) && isBackEdge(pred, intHead))
  273. if (! latchNode)
  274. latchNode = pred;
  275. else
  276. {
  277. if (pred->dfsLastNum > latchNode->dfsLastNum)
  278. latchNode = pred;
  279. }
  280. }
  281. /* Find nodes in the loop and the type of loop */
  282. if (latchNode)
  283. {
  284. /* Check latching node is at the same nesting level of case
  285. * statements (if any) and that the node doesn't belong to
  286. * another loop. */
  287. if ((latchNode->caseHead == intHead->caseHead) &&
  288. (latchNode->loopHead == NO_NODE))
  289. {
  290. intHead->latchNode = latchNode->dfsLastNum;
  291. findNodesInLoop(latchNode, intHead, this, intNodes);
  292. latchNode->flg |= IS_LATCH_NODE;
  293. }
  294. }
  295. /* Next interval */
  296. Ii = Ii->next;
  297. }
  298. /* Next derived sequence */
  299. }
  300. }
  301. static boolT successor (Int s, Int h, Function * pProc)
  302. /* Returns whether the BB indexed by s is a successor of the BB indexed by
  303. * h. Note that h is a case node. */
  304. { Int i;
  305. BB * header;
  306. header = pProc->dfsLast[h];
  307. for (i = 0; i < header->numOutEdges; i++)
  308. if (header->edges[i].BBptr->dfsLastNum == s)
  309. return true;
  310. return false;
  311. }
  312. static void tagNodesInCase (BB * pBB, nodeList &l, Int head, Int tail)
  313. /* Recursive procedure to tag nodes that belong to the case described by
  314. * the list l, head and tail (dfsLast index to first and exit node of the
  315. * case). */
  316. { Int current, /* index to current node */
  317. i;
  318. pBB->traversed = DFS_CASE;
  319. current = pBB->dfsLastNum;
  320. if ((current != tail) && (pBB->nodeType != MULTI_BRANCH) && (inList (l, pBB->immedDom)))
  321. {
  322. insertList (l, current);
  323. pBB->caseHead = head;
  324. for (i = 0; i < pBB->numOutEdges; i++)
  325. if (pBB->edges[i].BBptr->traversed != DFS_CASE)
  326. tagNodesInCase (pBB->edges[i].BBptr, l, head, tail);
  327. }
  328. }
  329. /* Structures case statements. This procedure is invoked only when pProc
  330. * has a case node. */
  331. void Function::structCases()
  332. {
  333. Int i, j;
  334. BB * caseHeader; /* case header node */
  335. Int exitNode = NO_NODE; /* case exit node */
  336. nodeList caseNodes; /* temporary: list of nodes in case */
  337. /* Linear scan of the nodes in reverse dfsLast order, searching for
  338. * case nodes */
  339. for (i = numBBs - 1; i >= 0; i--)
  340. if (dfsLast[i]->nodeType == MULTI_BRANCH)
  341. {
  342. caseHeader = dfsLast[i];
  343. /* Find descendant node which has as immediate predecessor
  344. * the current header node, and is not a successor. */
  345. for (j = i + 2; j < numBBs; j++)
  346. {
  347. if ((!successor(j, i, this)) &&
  348. (dfsLast[j]->immedDom == i))
  349. if (exitNode == NO_NODE)
  350. exitNode = j;
  351. else if (dfsLast[exitNode]->inEdges.size() < dfsLast[j]->inEdges.size())
  352. exitNode = j;
  353. }
  354. dfsLast[i]->caseTail = exitNode;
  355. /* Tag nodes that belong to the case by recording the
  356. * header field with caseHeader. */
  357. insertList (caseNodes, i);
  358. dfsLast[i]->caseHead = i;
  359. for (j = 0; j < caseHeader->numOutEdges; j++)
  360. tagNodesInCase (caseHeader->edges[j].BBptr, caseNodes, i, exitNode);
  361. if (exitNode != NO_NODE)
  362. dfsLast[exitNode]->caseHead = i;
  363. }
  364. }
  365. /* Flags all nodes in the list l as having follow node f, and deletes all
  366. * nodes from the list. */
  367. static void flagNodes (nodeList &l, Int f, Function * pProc)
  368. {
  369. nodeList::iterator p;
  370. p = l.begin();
  371. while (p!=l.end())
  372. {
  373. pProc->dfsLast[*p]->ifFollow = f;
  374. p = l.erase(p);
  375. }
  376. }
  377. /* Structures if statements */
  378. void Function::structIfs ()
  379. {
  380. Int curr, /* Index for linear scan of nodes */
  381. desc, /* Index for descendant */
  382. followInEdges, /* Largest # in-edges so far */
  383. follow; /* Possible follow node */
  384. nodeList domDesc, /* List of nodes dominated by curr */
  385. unresolved /* List of unresolved if nodes */
  386. ;
  387. BB * currNode, /* Pointer to current node */
  388. * pbb;
  389. /* Linear scan of nodes in reverse dfsLast order */
  390. for (curr = numBBs - 1; curr >= 0; curr--)
  391. {
  392. currNode = dfsLast[curr];
  393. if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
  394. continue;
  395. if ((currNode->nodeType == TWO_BRANCH) && (!currNode->back().isLlFlag(JX_LOOP)))
  396. {
  397. followInEdges = 0;
  398. follow = 0;
  399. /* Find all nodes that have this node as immediate dominator */
  400. for (desc = curr+1; desc < numBBs; desc++)
  401. {
  402. if (dfsLast[desc]->immedDom == curr)
  403. {
  404. insertList (domDesc, desc);
  405. pbb = dfsLast[desc];
  406. if ((pbb->inEdges.size() - pbb->numBackEdges) >= followInEdges)
  407. {
  408. follow = desc;
  409. followInEdges = pbb->inEdges.size() - pbb->numBackEdges;
  410. }
  411. }
  412. }
  413. /* Determine follow according to number of descendants
  414. * immediately dominated by this node */
  415. if ((follow != 0) && (followInEdges > 1))
  416. {
  417. currNode->ifFollow = follow;
  418. if (!unresolved.empty())
  419. flagNodes (unresolved, follow, this);
  420. }
  421. else
  422. insertList (unresolved, curr);
  423. }
  424. freeList (domDesc);
  425. }
  426. }
  427. /* Checks for compound conditions of basic blocks that have only 1 high
  428. * level instruction. Whenever these blocks are found, they are merged
  429. * into one block with the appropriate condition */
  430. void Function::compoundCond()
  431. {
  432. Int i, j, k, numOutEdges;
  433. BB * pbb, * t, * e, * obb,* pred;
  434. ICODE * picode, * ticode;
  435. COND_EXPR *exp;
  436. TYPEADR_TYPE *edges;
  437. boolT change;
  438. change = TRUE;
  439. while (change)
  440. {
  441. change = FALSE;
  442. /* Traverse nodes in postorder, this way, the header node of a
  443. * compound condition is analysed first */
  444. for (i = 0; i < this->numBBs; i++)
  445. {
  446. pbb = this->dfsLast[i];
  447. if (pbb->flg & INVALID_BB)
  448. continue;
  449. if (pbb->nodeType != TWO_BRANCH)
  450. continue;
  451. t = pbb->edges[THEN].BBptr;
  452. e = pbb->edges[ELSE].BBptr;
  453. /* Check (X || Y) case */
  454. if ((t->nodeType == TWO_BRANCH) && (t->numHlIcodes == 1) &&
  455. (t->inEdges.size() == 1) && (t->edges[ELSE].BBptr == e))
  456. {
  457. obb = t->edges[THEN].BBptr;
  458. /* Construct compound DBL_OR expression */
  459. picode = &pbb->back();
  460. ticode = &t->back();
  461. exp = COND_EXPR::boolOp (picode->ic.hl.oper.exp,
  462. ticode->ic.hl.oper.exp, DBL_OR);
  463. picode->ic.hl.oper.exp = exp;
  464. /* Replace in-edge to obb from t to pbb */
  465. {
  466. auto iter=find(obb->inEdges.begin(),obb->inEdges.end(),t);
  467. if(iter!=obb->inEdges.end())
  468. *iter = pbb;
  469. }
  470. /* New THEN out-edge of pbb */
  471. pbb->edges[THEN].BBptr = obb;
  472. /* Remove in-edge t to e */
  473. auto iter=std::find(e->inEdges.begin(),e->inEdges.end(),t);
  474. assert(iter!=e->inEdges.end());
  475. e->inEdges.erase(iter);
  476. t->flg |= INVALID_BB;
  477. if (pbb->flg & IS_LATCH_NODE)
  478. this->dfsLast[t->dfsLastNum] = pbb;
  479. else
  480. i--; /* to repeat this analysis */
  481. change = TRUE;
  482. }
  483. /* Check (!X && Y) case */
  484. else if ((t->nodeType == TWO_BRANCH) && (t->numHlIcodes == 1) &&
  485. (t->inEdges.size() == 1) && (t->edges[THEN].BBptr == e))
  486. {
  487. obb = t->edges[ELSE].BBptr;
  488. /* Construct compound DBL_AND expression */
  489. picode = &pbb->back();
  490. ticode = &t->back();
  491. inverseCondOp (&picode->ic.hl.oper.exp);
  492. exp = COND_EXPR::boolOp (picode->ic.hl.oper.exp,
  493. ticode->ic.hl.oper.exp, DBL_AND);
  494. picode->ic.hl.oper.exp = exp;
  495. /* Replace in-edge to obb from t to pbb */
  496. auto iter=std::find(obb->inEdges.begin(),obb->inEdges.end(),t);
  497. assert(iter!=obb->inEdges.end());
  498. *iter=pbb;
  499. /* New THEN and ELSE out-edges of pbb */
  500. pbb->edges[THEN].BBptr = e;
  501. pbb->edges[ELSE].BBptr = obb;
  502. /* Remove in-edge t to e */
  503. iter=std::find(e->inEdges.begin(),e->inEdges.end(),t);
  504. assert(iter!=e->inEdges.end());
  505. e->inEdges.erase(iter); /* looses 1 arc */
  506. t->flg |= INVALID_BB;
  507. if (pbb->flg & IS_LATCH_NODE)
  508. this->dfsLast[t->dfsLastNum] = pbb;
  509. else
  510. i--; /* to repeat this analysis */
  511. change = TRUE;
  512. }
  513. /* Check (X && Y) case */
  514. else if ((e->nodeType == TWO_BRANCH) && (e->numHlIcodes == 1) &&
  515. (e->inEdges.size()==1) && (e->edges[THEN].BBptr == t))
  516. {
  517. obb = e->edges[ELSE].BBptr;
  518. /* Construct compound DBL_AND expression */
  519. picode = &pbb->back();
  520. ticode = &t->back();
  521. exp = COND_EXPR::boolOp (picode->ic.hl.oper.exp,
  522. ticode->ic.hl.oper.exp, DBL_AND);
  523. picode->ic.hl.oper.exp = exp;
  524. /* Replace in-edge to obb from e to pbb */
  525. auto iter = std::find(obb->inEdges.begin(),obb->inEdges.end(),e);
  526. assert(iter!=obb->inEdges.end());
  527. *iter=pbb;
  528. /* New ELSE out-edge of pbb */
  529. pbb->edges[ELSE].BBptr = obb;
  530. /* Remove in-edge e to t */
  531. iter = std::find(t->inEdges.begin(),t->inEdges.end(),e);
  532. assert(iter!=t->inEdges.end());
  533. t->inEdges.erase(iter);
  534. e->flg |= INVALID_BB;
  535. if (pbb->flg & IS_LATCH_NODE)
  536. this->dfsLast[e->dfsLastNum] = pbb;
  537. else
  538. i--; /* to repeat this analysis */
  539. change = TRUE;
  540. }
  541. /* Check (!X || Y) case */
  542. else if ((e->nodeType == TWO_BRANCH) && (e->numHlIcodes == 1) &&
  543. (e->inEdges.size() == 1) && (e->edges[ELSE].BBptr == t))
  544. {
  545. obb = e->edges[THEN].BBptr;
  546. /* Construct compound DBL_OR expression */
  547. picode = &pbb->back();
  548. ticode = &t->back();
  549. inverseCondOp (&picode->ic.hl.oper.exp);
  550. exp = COND_EXPR::boolOp (picode->ic.hl.oper.exp,
  551. ticode->ic.hl.oper.exp, DBL_OR);
  552. picode->ic.hl.oper.exp = exp;
  553. /* Replace in-edge to obb from e to pbb */
  554. auto iter = std::find(obb->inEdges.begin(),obb->inEdges.end(),e);
  555. assert(iter!=obb->inEdges.end());
  556. *iter=pbb;
  557. /* New THEN and ELSE out-edges of pbb */
  558. pbb->edges[THEN].BBptr = obb;
  559. pbb->edges[ELSE].BBptr = t;
  560. /* Remove in-edge e to t */
  561. iter = std::find(t->inEdges.begin(),t->inEdges.end(),e);
  562. assert(iter!=t->inEdges.end());
  563. t->inEdges.erase(iter);
  564. e->flg |= INVALID_BB;
  565. if (pbb->flg & IS_LATCH_NODE)
  566. this->dfsLast[e->dfsLastNum] = pbb;
  567. else
  568. i--; /* to repeat this analysis */
  569. change = TRUE;
  570. }
  571. }
  572. }
  573. }
  574. /* Structuring algorithm to find the structures of the graph pProc->cfg */
  575. void Function::structure(derSeq *derivedG)
  576. {
  577. /* Find immediate dominators of the graph */
  578. findImmedDom();
  579. if (hasCase)
  580. structCases();
  581. structLoops(derivedG);
  582. structIfs();
  583. }