BasicBlock.cpp 14 KB

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