BasicBlock.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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->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->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. std::for_each(edges.begin(), edges.end(),
  91. [](TYPEADR_TYPE &pb)
  92. {
  93. if (pb.BBptr->traversed != DFS_DISP)
  94. pb.BBptr->displayDfs();
  95. });
  96. }
  97. /* Recursive procedure that writes the code for the given procedure, pointed
  98. * to by pBB.
  99. * Parameters: pBB: pointer to the cfg.
  100. * Icode: pointer to the Icode array for the cfg graph of the
  101. * current procedure.
  102. * indLevel: indentation level - used for formatting.
  103. * numLoc: last # assigned to local variables */
  104. void BB::writeCode (Int indLevel, Function * pProc , Int *numLoc,Int latchNode, Int _ifFollow)
  105. {
  106. Int follow, /* ifFollow */
  107. _loopType, /* Type of loop, if any */
  108. _nodeType; /* Type of node */
  109. BB * succ, *latch; /* Successor and latching node */
  110. ICODE * picode; /* Pointer to HLI_JCOND instruction */
  111. char *l; /* Pointer to HLI_JCOND expression */
  112. boolT emptyThen, /* THEN clause is empty */
  113. repCond; /* Repeat condition for while() */
  114. /* Check if this basic block should be analysed */
  115. if ((_ifFollow != UN_INIT) && (this == pProc->dfsLast[_ifFollow]))
  116. return;
  117. if (traversed == DFS_ALPHA)
  118. return;
  119. traversed = DFS_ALPHA;
  120. /* Check for start of loop */
  121. repCond = FALSE;
  122. latch = NULL;
  123. _loopType = loopType;
  124. if (_loopType)
  125. {
  126. latch = pProc->dfsLast[this->latchNode];
  127. switch (_loopType)
  128. {
  129. case WHILE_TYPE:
  130. picode = &this->back();
  131. /* Check for error in while condition */
  132. if (picode->ic.hl.opcode != HLI_JCOND)
  133. reportError (WHILE_FAIL);
  134. /* Check if condition is more than 1 HL instruction */
  135. if (numHlIcodes > 1)
  136. {
  137. /* Write the code for this basic block */
  138. writeBB(&pProc->Icode.front(), indLevel, pProc, numLoc);
  139. repCond = TRUE;
  140. }
  141. /* Condition needs to be inverted if the loop body is along
  142. * the THEN path of the header node */
  143. if (edges[ELSE].BBptr->dfsLastNum == loopFollow)
  144. inverseCondOp (&picode->ic.hl.oper.exp);
  145. {
  146. string e=walkCondExpr (picode->ic.hl.oper.exp, pProc, numLoc);
  147. cCode.appendCode( "\n%swhile (%s) {\n", indent(indLevel),e.c_str());
  148. }
  149. picode->invalidate();
  150. break;
  151. case REPEAT_TYPE:
  152. cCode.appendCode( "\n%sdo {\n", indent(indLevel));
  153. picode = &latch->back();
  154. picode->invalidate();
  155. break;
  156. case ENDLESS_TYPE:
  157. cCode.appendCode( "\n%sfor (;;) {\n", indent(indLevel));
  158. }
  159. stats.numHLIcode += 1;
  160. indLevel++;
  161. }
  162. /* Write the code for this basic block */
  163. if (repCond == FALSE)
  164. writeBB (&pProc->Icode.front(), indLevel, pProc, numLoc);
  165. /* Check for end of path */
  166. _nodeType = nodeType;
  167. if (_nodeType == RETURN_NODE || _nodeType == TERMINATE_NODE ||
  168. _nodeType == NOWHERE_NODE || (dfsLastNum == latchNode))
  169. return;
  170. /* Check type of loop/node and process code */
  171. if (_loopType) /* there is a loop */
  172. {
  173. assert(latch);
  174. if (this != latch) /* loop is over several bbs */
  175. {
  176. if (_loopType == WHILE_TYPE)
  177. {
  178. succ = edges[THEN].BBptr;
  179. if (succ->dfsLastNum == loopFollow)
  180. succ = edges[ELSE].BBptr;
  181. }
  182. else
  183. succ = edges[0].BBptr;
  184. if (succ->traversed != DFS_ALPHA)
  185. succ->writeCode (indLevel, pProc, numLoc, latch->dfsLastNum,_ifFollow);
  186. else /* has been traversed so we need a goto */
  187. succ->front().emitGotoLabel (indLevel);
  188. }
  189. /* Loop epilogue: generate the loop trailer */
  190. indLevel--;
  191. if (_loopType == WHILE_TYPE)
  192. {
  193. /* Check if there is need to repeat other statements involved
  194. * in while condition, then, emit the loop trailer */
  195. if (repCond)
  196. writeBB (&pProc->Icode.front(), indLevel+1, pProc, numLoc);
  197. cCode.appendCode( "%s} /* end of while */\n",indent(indLevel));
  198. }
  199. else if (_loopType == ENDLESS_TYPE)
  200. cCode.appendCode( "%s} /* end of loop */\n",indent(indLevel));
  201. else if (_loopType == REPEAT_TYPE)
  202. {
  203. if (picode->ic.hl.opcode != HLI_JCOND)
  204. reportError (REPEAT_FAIL);
  205. {
  206. string e=walkCondExpr (picode->ic.hl.oper.exp, pProc, numLoc);
  207. cCode.appendCode( "%s} while (%s);\n", indent(indLevel),e.c_str());
  208. }
  209. }
  210. /* Recurse on the loop follow */
  211. if (loopFollow != MAX)
  212. {
  213. succ = pProc->dfsLast[loopFollow];
  214. if (succ->traversed != DFS_ALPHA)
  215. succ->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  216. else /* has been traversed so we need a goto */
  217. succ->front().emitGotoLabel (indLevel);
  218. }
  219. }
  220. else /* no loop, process nodeType of the graph */
  221. {
  222. if (_nodeType == TWO_BRANCH) /* if-then[-else] */
  223. {
  224. stats.numHLIcode++;
  225. indLevel++;
  226. emptyThen = FALSE;
  227. if (ifFollow != MAX) /* there is a follow */
  228. {
  229. /* process the THEN part */
  230. follow = ifFollow;
  231. succ = edges[THEN].BBptr;
  232. if (succ->traversed != DFS_ALPHA) /* not visited */
  233. {
  234. if (succ->dfsLastNum != follow) /* THEN part */
  235. {
  236. l = writeJcond ( back().ic.hl, pProc, numLoc);
  237. cCode.appendCode( "\n%s%s", indent(indLevel-1), l);
  238. succ->writeCode (indLevel, pProc, numLoc, latchNode,follow);
  239. }
  240. else /* empty THEN part => negate ELSE part */
  241. {
  242. l = writeJcondInv ( back().ic.hl, pProc, numLoc);
  243. cCode.appendCode( "\n%s%s", indent(indLevel-1), l);
  244. edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, follow);
  245. emptyThen = TRUE;
  246. }
  247. }
  248. else /* already visited => emit label */
  249. succ->front().emitGotoLabel(indLevel);
  250. /* process the ELSE part */
  251. succ = edges[ELSE].BBptr;
  252. if (succ->traversed != DFS_ALPHA) /* not visited */
  253. {
  254. if (succ->dfsLastNum != follow) /* ELSE part */
  255. {
  256. cCode.appendCode( "%s}\n%selse {\n",
  257. indent(indLevel-1), indent(indLevel - 1));
  258. succ->writeCode (indLevel, pProc, numLoc, latchNode, follow);
  259. }
  260. /* else (empty ELSE part) */
  261. }
  262. else if (! emptyThen) /* already visited => emit label */
  263. {
  264. cCode.appendCode( "%s}\n%selse {\n",
  265. indent(indLevel-1), indent(indLevel - 1));
  266. succ->front().emitGotoLabel (indLevel);
  267. }
  268. cCode.appendCode( "%s}\n", indent(--indLevel));
  269. /* Continue with the follow */
  270. succ = pProc->dfsLast[follow];
  271. if (succ->traversed != DFS_ALPHA)
  272. succ->writeCode (indLevel, pProc, numLoc, latchNode,_ifFollow);
  273. }
  274. else /* no follow => if..then..else */
  275. {
  276. l = writeJcond (
  277. back().ic.hl, pProc, numLoc);
  278. cCode.appendCode( "%s%s", indent(indLevel-1), l);
  279. edges[THEN].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  280. cCode.appendCode( "%s}\n%selse {\n", indent(indLevel-1), indent(indLevel - 1));
  281. edges[ELSE].BBptr->writeCode (indLevel, pProc, numLoc, latchNode, _ifFollow);
  282. cCode.appendCode( "%s}\n", indent(--indLevel));
  283. }
  284. }
  285. else /* fall, call, 1w */
  286. {
  287. succ = edges[0].BBptr; /* fall-through edge */
  288. if (succ->traversed != DFS_ALPHA)
  289. succ->writeCode (indLevel, pProc,numLoc, latchNode,_ifFollow);
  290. }
  291. }
  292. }
  293. /* Writes the code for the current basic block.
  294. * Args: pBB: pointer to the current basic block.
  295. * Icode: pointer to the array of icodes for current procedure.
  296. * lev: indentation level - used for formatting. */
  297. void BB::writeBB(ICODE * hli, Int lev, Function * pProc, Int *numLoc)
  298. {
  299. Int i, last;
  300. char *line; /* Pointer to the HIGH-LEVEL line */
  301. /* Save the index into the code table in case there is a later goto
  302. * into this instruction (first instruction of the BB) */
  303. hli[start].codeIdx = nextBundleIdx (&cCode.code);
  304. /* Generate code for each hlicode that is not a HLI_JCOND */
  305. for (i = start, last = i + length; i < last; i++)
  306. if ((hli[i].type == HIGH_LEVEL) && (hli[i].invalid == FALSE))
  307. {
  308. line = write1HlIcode (hli[i].ic.hl, pProc, numLoc);
  309. if (line[0] != '\0')
  310. {
  311. cCode.appendCode( "%s%s", indent(lev), line);
  312. stats.numHLIcode++;
  313. }
  314. if (option.verbose)
  315. hli[i].writeDU(i);
  316. }
  317. }
  318. int BB::begin()
  319. {
  320. return start;
  321. }
  322. iICODE BB::begin2()
  323. {
  324. return Parent->Icode.begin()+start;
  325. }
  326. iICODE BB::end2()
  327. {
  328. return Parent->Icode.begin()+start+length;
  329. }
  330. int BB::rbegin()
  331. {
  332. return start+length-1;
  333. }
  334. int BB::end()
  335. {
  336. return start+length;
  337. }
  338. ICODE &BB::back()
  339. {
  340. return Parent->Icode[rbegin()];
  341. }
  342. size_t BB::size()
  343. {
  344. return length;
  345. }
  346. ICODE &BB::front()
  347. {
  348. return Parent->Icode[start];
  349. }
  350. riICODE BB::rbegin2()
  351. {
  352. riICODE res(Parent->Icode.begin()+start+length);
  353. assert(res->loc_ip==back().loc_ip);
  354. return res;
  355. }
  356. riICODE BB::rend2()
  357. {
  358. riICODE res(Parent->Icode.begin()+start);
  359. return res;
  360. }