control.cpp 24 KB

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