BasicBlock.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. #include <cassert>
  2. #include <string>
  3. #include "BasicBlock.h"
  4. #include "Procedure.h"
  5. #include "dcc.h"
  6. using namespace std;
  7. extern char *indent (Int indLevel);
  8. BB *BB::Create(void *ctx, const string &s, Function *parent, BB *insertBefore)
  9. {
  10. BB *pnewBB = new BB;
  11. pnewBB->Parent = parent;
  12. return pnewBB;
  13. }
  14. BB *BB::Create(Int start, Int ip, byte nodeType, Int numOutEdges, Function *parent)
  15. {
  16. parent->m_cfg;
  17. BB* pnewBB;
  18. pnewBB = new BB;
  19. pnewBB->nodeType = nodeType; /* Initialise */
  20. pnewBB->start = start;
  21. pnewBB->length = ip - start + 1;
  22. pnewBB->immedDom = NO_DOM;
  23. pnewBB->loopHead = pnewBB->caseHead = pnewBB->caseTail =
  24. pnewBB->latchNode= pnewBB->loopFollow = NO_NODE;
  25. if (numOutEdges)
  26. pnewBB->edges.resize(numOutEdges);
  27. /* Mark the basic block to which the icodes belong to, but only for
  28. * real code basic blocks (ie. not interval bbs) */
  29. if(parent)
  30. {
  31. if (start >= 0)
  32. parent->Icode.SetInBB(start, ip, pnewBB);
  33. parent->heldBBs.push_back(pnewBB);
  34. parent->m_cfg.push_back(pnewBB);
  35. pnewBB->Parent = parent;
  36. }
  37. if (start != -1) /* Only for code BB's */
  38. stats.numBBbef++;
  39. return pnewBB;
  40. }
  41. static const char *const s_nodeType[] = {"branch", "if", "case", "fall", "return", "call",
  42. "loop", "repeat", "interval", "cycleHead",
  43. "caseHead", "terminate",
  44. "nowhere" };
  45. static const char *const s_loopType[] = {"noLoop", "while", "repeat", "loop", "for"};
  46. void BB::display()
  47. {
  48. printf("\nnode type = %s, ", s_nodeType[nodeType]);
  49. printf("start = %ld, length = %ld, #out edges = %ld\n", start, length, edges.size());
  50. for (int i = 0; i < edges.size(); i++)
  51. printf(" outEdge[%2d] = %ld\n",i, edges[i].BBptr->start);
  52. }
  53. /*****************************************************************************
  54. * displayDfs - Displays the CFG using a depth first traversal
  55. ****************************************************************************/
  56. void BB::displayDfs()
  57. {
  58. Int i;
  59. assert(this);
  60. traversed = DFS_DISP;
  61. printf("node type = %s, ", s_nodeType[nodeType]);
  62. printf("start = %ld, length = %ld, #in-edges = %ld, #out-edges = %ld\n",
  63. start, length, inEdges.size(), edges.size());
  64. printf("dfsFirst = %ld, dfsLast = %ld, immed dom = %ld\n",
  65. dfsFirstNum, dfsLastNum,
  66. immedDom == MAX ? -1 : immedDom);
  67. printf("loopType = %s, loopHead = %ld, latchNode = %ld, follow = %ld\n",
  68. s_loopType[loopType],
  69. loopHead == MAX ? -1 : loopHead,
  70. latchNode == MAX ? -1 : latchNode,
  71. loopFollow == MAX ? -1 : loopFollow);
  72. printf ("ifFollow = %ld, caseHead = %ld, caseTail = %ld\n",
  73. ifFollow == MAX ? -1 : ifFollow,
  74. caseHead == MAX ? -1 : caseHead,
  75. caseTail == MAX ? -1 : caseTail);
  76. if (nodeType == INTERVAL_NODE)
  77. printf("corresponding interval = %ld\n", correspInt->numInt);
  78. else
  79. for (i = 0; i < inEdges.size(); i++)
  80. printf (" inEdge[%ld] = %ld\n", i, inEdges[i]->begin());
  81. /* Display out edges information */
  82. for (i = 0; i < edges.size(); i++)
  83. if (nodeType == INTERVAL_NODE)
  84. printf(" outEdge[%ld] = %ld\n", i,
  85. edges[i].BBptr->correspInt->numInt);
  86. else
  87. printf(" outEdge[%d] = %ld\n", i, edges[i].BBptr->begin());
  88. printf("----\n");
  89. /* Recursive call on successors of current node */
  90. for(TYPEADR_TYPE &pb : edges)
  91. {
  92. if (pb.BBptr->traversed != DFS_DISP)
  93. pb.BBptr->displayDfs();
  94. }
  95. }
  96. /* Recursive procedure that writes the code for the given procedure, pointed
  97. * to by pBB.
  98. * Parameters: pBB: pointer to the cfg.
  99. * Icode: pointer to the Icode array for the cfg graph of the
  100. * current procedure.
  101. * indLevel: indentation level - used for formatting.
  102. * numLoc: last # assigned to local variables */
  103. void BB::writeCode (Int indLevel, Function * pProc , Int *numLoc,Int latchNode, Int _ifFollow)
  104. {
  105. Int follow, /* ifFollow */
  106. _loopType, /* Type of loop, if any */
  107. _nodeType; /* Type of node */
  108. BB * succ, *latch; /* Successor and latching node */
  109. ICODE * picode; /* Pointer to HLI_JCOND instruction */
  110. char *l; /* Pointer to HLI_JCOND expression */
  111. boolT emptyThen, /* THEN clause is empty */
  112. repCond; /* Repeat condition for while() */
  113. /* Check if this basic block should be analysed */
  114. if ((_ifFollow != UN_INIT) && (this == pProc->m_dfsLast[_ifFollow]))
  115. return;
  116. if (traversed == DFS_ALPHA)
  117. return;
  118. traversed = DFS_ALPHA;
  119. /* Check for start of loop */
  120. repCond = FALSE;
  121. latch = NULL;
  122. _loopType = loopType;
  123. if (_loopType)
  124. {
  125. latch = pProc->m_dfsLast[this->latchNode];
  126. switch (_loopType)
  127. {
  128. case WHILE_TYPE:
  129. picode = &this->back();
  130. /* Check for error in while condition */
  131. if (picode->ic.hl.opcode != HLI_JCOND)
  132. reportError (WHILE_FAIL);
  133. /* Check if condition is more than 1 HL instruction */
  134. if (numHlIcodes > 1)
  135. {
  136. /* Write the code for this basic block */
  137. writeBB(&pProc->Icode.front(), indLevel, pProc, numLoc);
  138. repCond = true;
  139. }
  140. /* Condition needs to be inverted if the loop body is along
  141. * the THEN path of the header node */
  142. if (edges[ELSE].BBptr->dfsLastNum == loopFollow)
  143. inverseCondOp (&picode->ic.hl.oper.exp);
  144. {
  145. string e=walkCondExpr (picode->ic.hl.oper.exp, pProc, numLoc);
  146. cCode.appendCode( "\n%swhile (%s) {\n", indent(indLevel),e.c_str());
  147. }
  148. picode->invalidate();
  149. break;
  150. case REPEAT_TYPE:
  151. cCode.appendCode( "\n%sdo {\n", indent(indLevel));
  152. picode = &latch->back();
  153. picode->invalidate();
  154. break;
  155. case ENDLESS_TYPE:
  156. cCode.appendCode( "\n%sfor (;;) {\n", indent(indLevel));
  157. }
  158. stats.numHLIcode += 1;
  159. indLevel++;
  160. }
  161. /* Write the code for this basic block */
  162. if (repCond == FALSE)
  163. writeBB (&pProc->Icode.front(), indLevel, pProc, numLoc);
  164. /* Check for end of path */
  165. _nodeType = nodeType;
  166. if (_nodeType == RETURN_NODE || _nodeType == TERMINATE_NODE ||
  167. _nodeType == NOWHERE_NODE || (dfsLastNum == latchNode))
  168. return;
  169. /* Check type of loop/node and process code */
  170. if (_loopType) /* there is a loop */
  171. {
  172. assert(latch);
  173. if (this != latch) /* loop is over several bbs */
  174. {
  175. if (_loopType == WHILE_TYPE)
  176. {
  177. succ = edges[THEN].BBptr;
  178. if (succ->dfsLastNum == loopFollow)
  179. succ = edges[ELSE].BBptr;
  180. }
  181. else
  182. succ = edges[0].BBptr;
  183. if (succ->traversed != DFS_ALPHA)
  184. succ->writeCode (indLevel, pProc, numLoc, latch->dfsLastNum,_ifFollow);
  185. else /* has been traversed so we need a goto */
  186. succ->front().emitGotoLabel (indLevel);
  187. }
  188. /* Loop epilogue: generate the loop trailer */
  189. indLevel--;
  190. if (_loopType == WHILE_TYPE)
  191. {
  192. /* Check if there is need to repeat other statements involved
  193. * in while condition, then, emit the loop trailer */
  194. if (repCond)
  195. writeBB (&pProc->Icode.front(), indLevel+1, pProc, numLoc);
  196. cCode.appendCode( "%s} /* end of while */\n",indent(indLevel));
  197. }
  198. else if (_loopType == ENDLESS_TYPE)
  199. cCode.appendCode( "%s} /* end of loop */\n",indent(indLevel));
  200. else if (_loopType == REPEAT_TYPE)
  201. {
  202. if (picode->ic.hl.opcode != HLI_JCOND)
  203. reportError (REPEAT_FAIL);
  204. {
  205. string e=walkCondExpr (picode->ic.hl.oper.exp, pProc, numLoc);
  206. cCode.appendCode( "%s} while (%s);\n", indent(indLevel),e.c_str());
  207. }
  208. }
  209. /* Recurse on the loop follow */
  210. if (loopFollow != MAX)
  211. {
  212. succ = pProc->m_dfsLast[loopFollow];
  213. if (succ->traversed != DFS_ALPHA)
  214. succ->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  215. else /* has been traversed so we need a goto */
  216. succ->front().emitGotoLabel (indLevel);
  217. }
  218. }
  219. else /* no loop, process nodeType of the graph */
  220. {
  221. if (_nodeType == TWO_BRANCH) /* if-then[-else] */
  222. {
  223. stats.numHLIcode++;
  224. indLevel++;
  225. emptyThen = FALSE;
  226. if (ifFollow != MAX) /* there is a follow */
  227. {
  228. /* process the THEN part */
  229. follow = ifFollow;
  230. succ = edges[THEN].BBptr;
  231. if (succ->traversed != DFS_ALPHA) /* not visited */
  232. {
  233. if (succ->dfsLastNum != follow) /* THEN part */
  234. {
  235. l = writeJcond ( back().ic.hl, pProc, numLoc);
  236. cCode.appendCode( "\n%s%s", indent(indLevel-1), l);
  237. succ->writeCode (indLevel, pProc, numLoc, latchNode,follow);
  238. }
  239. else /* empty THEN part => negate ELSE part */
  240. {
  241. l = writeJcondInv ( back().ic.hl, pProc, numLoc);
  242. cCode.appendCode( "\n%s%s", indent(indLevel-1), l);
  243. edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, follow);
  244. emptyThen = true;
  245. }
  246. }
  247. else /* already visited => emit label */
  248. succ->front().emitGotoLabel(indLevel);
  249. /* process the ELSE part */
  250. succ = edges[ELSE].BBptr;
  251. if (succ->traversed != DFS_ALPHA) /* not visited */
  252. {
  253. if (succ->dfsLastNum != follow) /* ELSE part */
  254. {
  255. cCode.appendCode( "%s}\n%selse {\n",
  256. indent(indLevel-1), indent(indLevel - 1));
  257. succ->writeCode (indLevel, pProc, numLoc, latchNode, follow);
  258. }
  259. /* else (empty ELSE part) */
  260. }
  261. else if (! emptyThen) /* already visited => emit label */
  262. {
  263. cCode.appendCode( "%s}\n%selse {\n",
  264. indent(indLevel-1), indent(indLevel - 1));
  265. succ->front().emitGotoLabel (indLevel);
  266. }
  267. cCode.appendCode( "%s}\n", indent(--indLevel));
  268. /* Continue with the follow */
  269. succ = pProc->m_dfsLast[follow];
  270. if (succ->traversed != DFS_ALPHA)
  271. succ->writeCode (indLevel, pProc, numLoc, latchNode,_ifFollow);
  272. }
  273. else /* no follow => if..then..else */
  274. {
  275. l = writeJcond (
  276. back().ic.hl, pProc, numLoc);
  277. cCode.appendCode( "%s%s", indent(indLevel-1), l);
  278. edges[THEN].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  279. cCode.appendCode( "%s}\n%selse {\n", indent(indLevel-1), indent(indLevel - 1));
  280. edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  281. cCode.appendCode( "%s}\n", indent(--indLevel));
  282. }
  283. }
  284. else /* fall, call, 1w */
  285. {
  286. succ = edges[0].BBptr; /* fall-through edge */
  287. if (succ->traversed != DFS_ALPHA)
  288. succ->writeCode (indLevel, pProc,numLoc, latchNode,_ifFollow);
  289. }
  290. }
  291. }
  292. /* Writes the code for the current basic block.
  293. * Args: pBB: pointer to the current basic block.
  294. * Icode: pointer to the array of icodes for current procedure.
  295. * lev: indentation level - used for formatting. */
  296. void BB::writeBB(ICODE * hli, Int lev, Function * pProc, Int *numLoc)
  297. {
  298. Int i, last;
  299. char *line; /* Pointer to the HIGH-LEVEL line */
  300. /* Save the index into the code table in case there is a later goto
  301. * into this instruction (first instruction of the BB) */
  302. hli[start].codeIdx = nextBundleIdx (&cCode.code);
  303. /* Generate code for each hlicode that is not a HLI_JCOND */
  304. for (i = start, last = i + length; i < last; i++)
  305. if ((hli[i].type == HIGH_LEVEL) && (hli[i].invalid == FALSE))
  306. {
  307. line = write1HlIcode (hli[i].ic.hl, pProc, numLoc);
  308. if (line[0] != '\0')
  309. {
  310. cCode.appendCode( "%s%s", indent(lev), line);
  311. stats.numHLIcode++;
  312. }
  313. if (option.verbose)
  314. hli[i].writeDU(i);
  315. }
  316. }
  317. int BB::begin()
  318. {
  319. return start;
  320. }
  321. iICODE BB::begin2()
  322. {
  323. return Parent->Icode.begin()+start;
  324. }
  325. iICODE BB::end2()
  326. {
  327. return Parent->Icode.begin()+start+length;
  328. }
  329. int BB::rbegin()
  330. {
  331. return start+length-1;
  332. }
  333. int BB::end()
  334. {
  335. return start+length;
  336. }
  337. ICODE &BB::back()
  338. {
  339. return Parent->Icode[rbegin()];
  340. }
  341. size_t BB::size()
  342. {
  343. return length;
  344. }
  345. ICODE &BB::front()
  346. {
  347. return Parent->Icode[start];
  348. }
  349. riICODE BB::rbegin2()
  350. {
  351. riICODE res(Parent->Icode.begin()+start+length);
  352. assert(res->loc_ip==back().loc_ip);
  353. return res;
  354. }
  355. riICODE BB::rend2()
  356. {
  357. riICODE res(Parent->Icode.begin()+start);
  358. return res;
  359. }