control.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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]->edges.size(); 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->edges.size(); 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->edges.size(); 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. std::for_each(caseHeader->edges.begin(),caseHeader->edges.end(),
  360. [&caseNodes, i, exitNode](TYPEADR_TYPE &pb)
  361. {tagNodesInCase(pb.BBptr, caseNodes, i, exitNode);});
  362. // for (j = 0; j < caseHeader->edges[j]; j++)
  363. // tagNodesInCase (caseHeader->edges[j].BBptr, caseNodes, i, exitNode);
  364. if (exitNode != NO_NODE)
  365. dfsLast[exitNode]->caseHead = i;
  366. }
  367. }
  368. /* Flags all nodes in the list l as having follow node f, and deletes all
  369. * nodes from the list. */
  370. static void flagNodes (nodeList &l, Int f, Function * pProc)
  371. {
  372. nodeList::iterator p;
  373. p = l.begin();
  374. while (p!=l.end())
  375. {
  376. pProc->dfsLast[*p]->ifFollow = f;
  377. p = l.erase(p);
  378. }
  379. }
  380. /* Structures if statements */
  381. void Function::structIfs ()
  382. {
  383. Int curr, /* Index for linear scan of nodes */
  384. desc, /* Index for descendant */
  385. followInEdges, /* Largest # in-edges so far */
  386. follow; /* Possible follow node */
  387. nodeList domDesc, /* List of nodes dominated by curr */
  388. unresolved /* List of unresolved if nodes */
  389. ;
  390. BB * currNode, /* Pointer to current node */
  391. * pbb;
  392. /* Linear scan of nodes in reverse dfsLast order */
  393. for (curr = numBBs - 1; curr >= 0; curr--)
  394. {
  395. currNode = dfsLast[curr];
  396. if (currNode->flg & INVALID_BB) /* Do not process invalid BBs */
  397. continue;
  398. if ((currNode->nodeType == TWO_BRANCH) && (!currNode->back().isLlFlag(JX_LOOP)))
  399. {
  400. followInEdges = 0;
  401. follow = 0;
  402. /* Find all nodes that have this node as immediate dominator */
  403. for (desc = curr+1; desc < numBBs; desc++)
  404. {
  405. if (dfsLast[desc]->immedDom == curr)
  406. {
  407. insertList (domDesc, desc);
  408. pbb = dfsLast[desc];
  409. if ((pbb->inEdges.size() - pbb->numBackEdges) >= followInEdges)
  410. {
  411. follow = desc;
  412. followInEdges = pbb->inEdges.size() - pbb->numBackEdges;
  413. }
  414. }
  415. }
  416. /* Determine follow according to number of descendants
  417. * immediately dominated by this node */
  418. if ((follow != 0) && (followInEdges > 1))
  419. {
  420. currNode->ifFollow = follow;
  421. if (!unresolved.empty())
  422. flagNodes (unresolved, follow, this);
  423. }
  424. else
  425. insertList (unresolved, curr);
  426. }
  427. freeList (domDesc);
  428. }
  429. }
  430. /* Checks for compound conditions of basic blocks that have only 1 high
  431. * level instruction. Whenever these blocks are found, they are merged
  432. * into one block with the appropriate condition */
  433. void Function::compoundCond()
  434. {
  435. Int i, j, k, numOutEdges;
  436. BB * pbb, * t, * e, * obb,* pred;
  437. ICODE * picode, * ticode;
  438. COND_EXPR *exp;
  439. TYPEADR_TYPE *edges;
  440. boolT change;
  441. change = TRUE;
  442. while (change)
  443. {
  444. change = FALSE;
  445. /* Traverse nodes in postorder, this way, the header node of a
  446. * compound condition is analysed first */
  447. for (i = 0; i < this->numBBs; i++)
  448. {
  449. pbb = this->dfsLast[i];
  450. if (pbb->flg & INVALID_BB)
  451. continue;
  452. if (pbb->nodeType != TWO_BRANCH)
  453. continue;
  454. t = pbb->edges[THEN].BBptr;
  455. e = pbb->edges[ELSE].BBptr;
  456. /* Check (X || Y) case */
  457. if ((t->nodeType == TWO_BRANCH) && (t->numHlIcodes == 1) &&
  458. (t->inEdges.size() == 1) && (t->edges[ELSE].BBptr == e))
  459. {
  460. obb = t->edges[THEN].BBptr;
  461. /* Construct compound DBL_OR expression */
  462. picode = &pbb->back();
  463. ticode = &t->back();
  464. exp = COND_EXPR::boolOp (picode->ic.hl.oper.exp,
  465. ticode->ic.hl.oper.exp, DBL_OR);
  466. picode->ic.hl.oper.exp = exp;
  467. /* Replace in-edge to obb from t to pbb */
  468. {
  469. auto iter=find(obb->inEdges.begin(),obb->inEdges.end(),t);
  470. if(iter!=obb->inEdges.end())
  471. *iter = pbb;
  472. }
  473. /* New THEN out-edge of pbb */
  474. pbb->edges[THEN].BBptr = obb;
  475. /* Remove in-edge t to e */
  476. auto iter=std::find(e->inEdges.begin(),e->inEdges.end(),t);
  477. assert(iter!=e->inEdges.end());
  478. e->inEdges.erase(iter);
  479. t->flg |= INVALID_BB;
  480. if (pbb->flg & IS_LATCH_NODE)
  481. this->dfsLast[t->dfsLastNum] = pbb;
  482. else
  483. i--; /* to repeat this analysis */
  484. change = TRUE;
  485. }
  486. /* Check (!X && Y) case */
  487. else if ((t->nodeType == TWO_BRANCH) && (t->numHlIcodes == 1) &&
  488. (t->inEdges.size() == 1) && (t->edges[THEN].BBptr == e))
  489. {
  490. obb = t->edges[ELSE].BBptr;
  491. /* Construct compound DBL_AND expression */
  492. picode = &pbb->back();
  493. ticode = &t->back();
  494. inverseCondOp (&picode->ic.hl.oper.exp);
  495. exp = COND_EXPR::boolOp (picode->ic.hl.oper.exp,
  496. ticode->ic.hl.oper.exp, DBL_AND);
  497. picode->ic.hl.oper.exp = exp;
  498. /* Replace in-edge to obb from t to pbb */
  499. auto iter=std::find(obb->inEdges.begin(),obb->inEdges.end(),t);
  500. assert(iter!=obb->inEdges.end());
  501. *iter=pbb;
  502. /* New THEN and ELSE out-edges of pbb */
  503. pbb->edges[THEN].BBptr = e;
  504. pbb->edges[ELSE].BBptr = obb;
  505. /* Remove in-edge t to e */
  506. iter=std::find(e->inEdges.begin(),e->inEdges.end(),t);
  507. assert(iter!=e->inEdges.end());
  508. e->inEdges.erase(iter); /* looses 1 arc */
  509. t->flg |= INVALID_BB;
  510. if (pbb->flg & IS_LATCH_NODE)
  511. this->dfsLast[t->dfsLastNum] = pbb;
  512. else
  513. i--; /* to repeat this analysis */
  514. change = TRUE;
  515. }
  516. /* Check (X && Y) case */
  517. else if ((e->nodeType == TWO_BRANCH) && (e->numHlIcodes == 1) &&
  518. (e->inEdges.size()==1) && (e->edges[THEN].BBptr == t))
  519. {
  520. obb = e->edges[ELSE].BBptr;
  521. /* Construct compound DBL_AND expression */
  522. picode = &pbb->back();
  523. ticode = &t->back();
  524. exp = COND_EXPR::boolOp (picode->ic.hl.oper.exp,
  525. ticode->ic.hl.oper.exp, DBL_AND);
  526. picode->ic.hl.oper.exp = exp;
  527. /* Replace in-edge to obb from e to pbb */
  528. auto iter = std::find(obb->inEdges.begin(),obb->inEdges.end(),e);
  529. assert(iter!=obb->inEdges.end());
  530. *iter=pbb;
  531. /* New ELSE out-edge of pbb */
  532. pbb->edges[ELSE].BBptr = obb;
  533. /* Remove in-edge e to t */
  534. iter = std::find(t->inEdges.begin(),t->inEdges.end(),e);
  535. assert(iter!=t->inEdges.end());
  536. t->inEdges.erase(iter);
  537. e->flg |= INVALID_BB;
  538. if (pbb->flg & IS_LATCH_NODE)
  539. this->dfsLast[e->dfsLastNum] = pbb;
  540. else
  541. i--; /* to repeat this analysis */
  542. change = TRUE;
  543. }
  544. /* Check (!X || Y) case */
  545. else if ((e->nodeType == TWO_BRANCH) && (e->numHlIcodes == 1) &&
  546. (e->inEdges.size() == 1) && (e->edges[ELSE].BBptr == t))
  547. {
  548. obb = e->edges[THEN].BBptr;
  549. /* Construct compound DBL_OR expression */
  550. picode = &pbb->back();
  551. ticode = &t->back();
  552. inverseCondOp (&picode->ic.hl.oper.exp);
  553. exp = COND_EXPR::boolOp (picode->ic.hl.oper.exp,
  554. ticode->ic.hl.oper.exp, DBL_OR);
  555. picode->ic.hl.oper.exp = exp;
  556. /* Replace in-edge to obb from e to pbb */
  557. auto iter = std::find(obb->inEdges.begin(),obb->inEdges.end(),e);
  558. assert(iter!=obb->inEdges.end());
  559. *iter=pbb;
  560. /* New THEN and ELSE out-edges of pbb */
  561. pbb->edges[THEN].BBptr = obb;
  562. pbb->edges[ELSE].BBptr = t;
  563. /* Remove in-edge e to t */
  564. iter = std::find(t->inEdges.begin(),t->inEdges.end(),e);
  565. assert(iter!=t->inEdges.end());
  566. t->inEdges.erase(iter);
  567. e->flg |= INVALID_BB;
  568. if (pbb->flg & IS_LATCH_NODE)
  569. this->dfsLast[e->dfsLastNum] = pbb;
  570. else
  571. i--; /* to repeat this analysis */
  572. change = TRUE;
  573. }
  574. }
  575. }
  576. }
  577. /* Structuring algorithm to find the structures of the graph pProc->cfg */
  578. void Function::structure(derSeq *derivedG)
  579. {
  580. /* Find immediate dominators of the graph */
  581. findImmedDom();
  582. if (hasCase)
  583. structCases();
  584. structLoops(derivedG);
  585. structIfs();
  586. }