graph.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /*****************************************************************************
  2. * dcc project CFG related functions
  3. * (C) Cristina Cifuentes
  4. ****************************************************************************/
  5. #include "graph.h"
  6. #include "msvc_fixes.h"
  7. #include "dcc.h"
  8. #include "project.h"
  9. #include <boost/range/rbegin.hpp>
  10. #include <boost/range/rend.hpp>
  11. #include <string.h>
  12. using namespace std;
  13. using namespace boost;
  14. extern Project g_proj;
  15. //static BB * rmJMP(Function * pProc, int marker, BB * pBB);
  16. //static void mergeFallThrough(Function * pProc, BB * pBB);
  17. //static void dfsNumbering(BB * pBB, std::vector<BB*> &dfsLast, int *first, int *last);
  18. void Function::addOutEdgesForConditionalJump(BB * pBB,int next_ip, LLInst *ll)
  19. {
  20. pBB->addOutEdge(next_ip);
  21. /* This is checking for jumps off into nowhere */
  22. if ( not ll->testFlags(NO_LABEL) )
  23. pBB->addOutEdge(ll->src().getImm2());
  24. }
  25. /*****************************************************************************
  26. * createCFG - Create the basic control flow graph
  27. ****************************************************************************/
  28. void Function::createCFG()
  29. {
  30. /* Splits Icode associated with the procedure into Basic Blocks.
  31. * The links between BBs represent the control flow graph of the
  32. * procedure.
  33. * A Basic Block is defined to end on one of the following instructions:
  34. * 1) Conditional and unconditional jumps
  35. * 2) CALL(F)
  36. * 3) RET(F)
  37. * 4) On the instruction before a join (a flagged TARGET)
  38. * 5) Repeated string instructions
  39. * 6) End of procedure
  40. */
  41. BB * psBB;
  42. BB * pBB;
  43. iICODE pIcode = Icode.begin();
  44. stats.numBBbef = stats.numBBaft = 0;
  45. rICODE current_range=make_iterator_range(pIcode,++iICODE(pIcode));
  46. for (; pIcode!=Icode.end(); ++pIcode,current_range.advance_end(1))
  47. {
  48. iICODE nextIcode = ++iICODE(pIcode);
  49. pBB = nullptr;
  50. LLInst *ll = pIcode->ll();
  51. /* Only process icodes that have valid instructions */
  52. if(ll->testFlags(NO_CODE))
  53. continue;
  54. /* Stick a NOWHERE_NODE on the end if we terminate
  55. * with anything other than a ret, jump or terminate */
  56. if (nextIcode == Icode.end() and
  57. (not ll->testFlags(TERMINATES)) and
  58. (not ll->match(iJMP)) and (not ll->match(iJMPF)) and
  59. (not ll->match(iRET)) and (not ll->match(iRETF)))
  60. {
  61. pBB=BB::Create(current_range, NOWHERE_NODE, this);
  62. }
  63. else
  64. switch (ll->getOpcode()) {
  65. case iJB: case iJBE: case iJAE: case iJA:
  66. case iJL: case iJLE: case iJGE: case iJG:
  67. case iJE: case iJNE: case iJS: case iJNS:
  68. case iJO: case iJNO: case iJP: case iJNP:
  69. case iJCXZ:
  70. pBB = BB::Create(current_range, TWO_BRANCH, this);
  71. addOutEdgesForConditionalJump(pBB,nextIcode->loc_ip, ll);
  72. break;
  73. case iLOOP: case iLOOPE: case iLOOPNE:
  74. pBB = BB::Create(current_range, LOOP_NODE, this);
  75. addOutEdgesForConditionalJump(pBB,nextIcode->loc_ip, ll);
  76. break;
  77. case iJMPF: case iJMP:
  78. if (ll->testFlags(SWITCH))
  79. {
  80. pBB = BB::Create(current_range, MULTI_BRANCH, this);
  81. for (auto & elem : ll->caseTbl2)
  82. pBB->addOutEdge(elem);
  83. hasCase = true;
  84. }
  85. else if ((ll->getFlag() & (I | NO_LABEL)) == I) //TODO: WHY NO_LABEL TESTIT
  86. {
  87. pBB = BB::Create(current_range, ONE_BRANCH, this);
  88. pBB->addOutEdge(ll->src().getImm2());
  89. }
  90. else
  91. pBB = BB::Create(current_range, NOWHERE_NODE, this);
  92. break;
  93. case iCALLF: case iCALL:
  94. {
  95. Function * p = ll->src().proc.proc;
  96. pBB = BB::Create(current_range, CALL_NODE, this);
  97. if (p and not ((p->flg) & TERMINATES) )
  98. pBB->addOutEdge(nextIcode->loc_ip);
  99. break;
  100. }
  101. case iRET: case iRETF:
  102. pBB = BB::Create(current_range, RETURN_NODE, this);
  103. break;
  104. default:
  105. /* Check for exit to DOS */
  106. if ( ll->testFlags(TERMINATES) )
  107. {
  108. pBB = BB::Create(current_range, TERMINATE_NODE, this);
  109. }
  110. /* Check for a fall through */
  111. else if (nextIcode != Icode.end())
  112. {
  113. if (nextIcode->ll()->testFlags(TARGET | CASE))
  114. {
  115. pBB = BB::Create(current_range, FALL_NODE, this);
  116. pBB->addOutEdge(nextIcode->loc_ip);
  117. }
  118. }
  119. break;
  120. }
  121. if(pBB!=nullptr) // created a new Basic block
  122. {
  123. // restart the range
  124. // end iterator will be updated by expression in for statement
  125. current_range=make_iterator_range(nextIcode,nextIcode);
  126. }
  127. if (nextIcode == Icode.end())
  128. break;
  129. }
  130. for (auto pr : m_ip_to_bb)
  131. {
  132. BB* pBB=pr.second;
  133. for (auto & elem : pBB->edges)
  134. {
  135. int32_t ip = elem.ip;
  136. if (ip >= SYNTHESIZED_MIN)
  137. {
  138. fatalError (INVALID_SYNTHETIC_BB);
  139. return;
  140. }
  141. auto iter2=m_ip_to_bb.find(ip);
  142. if(iter2==m_ip_to_bb.end())
  143. fatalError(NO_BB, ip, qPrintable(name));
  144. psBB = iter2->second;
  145. elem.BBptr = psBB;
  146. psBB->inEdges.push_back((BB *)nullptr);
  147. }
  148. }
  149. }
  150. void Function::markImpure()
  151. {
  152. PROG &prog(Project::get()->prog);
  153. for(ICODE &icod : Icode)
  154. {
  155. if ( not icod.ll()->testFlags(SYM_USE | SYM_DEF))
  156. continue;
  157. //assert that case tbl has less entries then symbol table ????
  158. //WARNING: Case entries are held in symbol table !
  159. assert(Project::get()->validSymIdx(icod.ll()->caseEntry));
  160. const SYM &psym(Project::get()->getSymByIdx(icod.ll()->caseEntry));
  161. for (int c = (int)psym.label; c < (int)psym.label+psym.size; c++)
  162. {
  163. if (BITMAP(c, BM_CODE))
  164. {
  165. icod.ll()->setFlags(IMPURE);
  166. flg |= IMPURE;
  167. break;
  168. }
  169. }
  170. }
  171. }
  172. /*****************************************************************************
  173. * freeCFG - Deallocates a cfg
  174. ****************************************************************************/
  175. void Function::freeCFG()
  176. {
  177. for(auto p : m_ip_to_bb)
  178. {
  179. delete p.second;
  180. }
  181. m_ip_to_bb.clear();
  182. }
  183. /*****************************************************************************
  184. * compressCFG - Remove redundancies and add in-edge information
  185. ****************************************************************************/
  186. void Function::compressCFG()
  187. {
  188. BB *pNxt;
  189. int ip, first=0, last;
  190. /* First pass over BB list removes redundant jumps of the form
  191. * (Un)Conditional -> Unconditional jump */
  192. for (BB *pBB : m_actual_cfg) //m_cfg
  193. {
  194. if(pBB->inEdges.empty() or (pBB->nodeType != ONE_BRANCH and pBB->nodeType != TWO_BRANCH))
  195. continue;
  196. for (TYPEADR_TYPE &edgeRef : pBB->edges)
  197. {
  198. ip = pBB->rbegin()->loc_ip;
  199. pNxt = edgeRef.BBptr->rmJMP(ip, edgeRef.BBptr);
  200. if (not pBB->edges.empty()) /* Might have been clobbered */
  201. {
  202. edgeRef.BBptr = pNxt;
  203. assert(pBB->back().loc_ip==ip);
  204. pBB->back().ll()->SetImmediateOp((uint32_t)pNxt->begin()->loc_ip);
  205. //Icode[ip].SetImmediateOp((uint32_t)pNxt->begin());
  206. }
  207. }
  208. }
  209. /* Next is a depth-first traversal merging any FALL_NODE or
  210. * ONE_BRANCH that fall through to a node with that as their only
  211. * in-edge. */
  212. m_actual_cfg.front()->mergeFallThrough(Icode);
  213. /* Remove redundant BBs created by the above compressions
  214. * and allocate in-edge arrays as required. */
  215. stats.numBBaft = stats.numBBbef;
  216. bool entry_node=true;
  217. for(BB *pBB : m_actual_cfg)
  218. {
  219. if (pBB->inEdges.empty())
  220. {
  221. if (entry_node) /* Init it misses out on */
  222. pBB->index = UN_INIT;
  223. else
  224. {
  225. delete pBB;
  226. stats.numBBaft--;
  227. }
  228. }
  229. else
  230. {
  231. pBB->inEdgeCount = pBB->inEdges.size();
  232. }
  233. entry_node=false;
  234. }
  235. /* Allocate storage for dfsLast[] array */
  236. numBBs = stats.numBBaft;
  237. m_dfsLast.resize(numBBs,nullptr); // = (BB **)allocMem(numBBs * sizeof(BB *))
  238. /* Now do a dfs numbering traversal and fill in the inEdges[] array */
  239. last = numBBs - 1;
  240. m_actual_cfg.front()->dfsNumbering(m_dfsLast, &first, &last);
  241. }
  242. /****************************************************************************
  243. * rmJMP - If BB addressed is just a JMP it is replaced with its target
  244. ***************************************************************************/
  245. BB *BB::rmJMP(int marker, BB * pBB)
  246. {
  247. marker += (int)DFS_JMP;
  248. while (pBB->nodeType == ONE_BRANCH and pBB->size() == 1)
  249. {
  250. if (pBB->traversed != marker)
  251. {
  252. pBB->traversed = (eDFS)marker;
  253. pBB->inEdges.pop_back();
  254. if (not pBB->inEdges.empty())
  255. {
  256. pBB->edges[0].BBptr->inEdges.push_back((BB *)nullptr);
  257. }
  258. else
  259. {
  260. pBB->front().ll()->setFlags(NO_CODE);
  261. pBB->front().invalidate(); //pProc->Icode.SetLlInvalid(pBB->begin(), true);
  262. }
  263. pBB = pBB->edges[0].BBptr;
  264. }
  265. else
  266. {
  267. /* We are going around in circles */
  268. pBB->nodeType = NOWHERE_NODE;
  269. pBB->front().ll()->replaceSrc(LLOperand::CreateImm2(pBB->front().loc_ip));
  270. //pBB->front().ll()->src.immed.op = pBB->front().loc_ip;
  271. do {
  272. pBB = pBB->edges[0].BBptr;
  273. pBB->inEdges.pop_back(); // was --numInedges
  274. if (not pBB->inEdges.empty())
  275. {
  276. pBB->front().ll()->setFlags(NO_CODE);
  277. pBB->front().invalidate();
  278. // pProc->Icode.setFlags(pBB->start, NO_CODE);
  279. // pProc->Icode.SetLlInvalid(pBB->start, true);
  280. }
  281. } while (pBB->nodeType != NOWHERE_NODE);
  282. pBB->edges.clear();
  283. }
  284. }
  285. return pBB;
  286. }
  287. /*****************************************************************************
  288. * mergeFallThrough
  289. ****************************************************************************/
  290. void BB::mergeFallThrough( CIcodeRec &Icode)
  291. {
  292. BB * pChild;
  293. if (nullptr==this)
  294. {
  295. printf("mergeFallThrough on empty BB!\n");
  296. }
  297. while (nodeType == FALL_NODE or nodeType == ONE_BRANCH)
  298. {
  299. pChild = edges[0].BBptr;
  300. /* Jump to next instruction can always be removed */
  301. if (nodeType == ONE_BRANCH)
  302. {
  303. assert(Parent==pChild->Parent);
  304. if(back().loc_ip>pChild->front().loc_ip) // back edege
  305. break;
  306. auto iter=std::find_if(this->end(),pChild->begin(),[](ICODE &c)
  307. {return not c.ll()->testFlags(NO_CODE);});
  308. if (iter != pChild->begin())
  309. break;
  310. back().ll()->setFlags(NO_CODE);
  311. back().invalidate();
  312. nodeType = FALL_NODE;
  313. //instructions.advance_end(-1); //TODO: causes creation of empty BB
  314. }
  315. /* If there's no other edges into child can merge */
  316. if (pChild->inEdges.size() != 1)
  317. break;
  318. nodeType = pChild->nodeType;
  319. instructions = boost::make_iterator_range(begin(),pChild->end());
  320. pChild->front().ll()->clrFlags(TARGET);
  321. edges.swap(pChild->edges);
  322. pChild->inEdges.clear();
  323. pChild->edges.clear();
  324. }
  325. traversed = DFS_MERGE;
  326. /* Process all out edges recursively */
  327. for (auto & elem : edges)
  328. {
  329. if (elem.BBptr->traversed != DFS_MERGE)
  330. elem.BBptr->mergeFallThrough(Icode);
  331. }
  332. }
  333. /*****************************************************************************
  334. * dfsNumbering - Numbers nodes during first and last visits and determine
  335. * in-edges
  336. ****************************************************************************/
  337. void BB::dfsNumbering(std::vector<BB *> &dfsLast, int *first, int *last)
  338. {
  339. BB * pChild;
  340. traversed = DFS_NUM;
  341. dfsFirstNum = (*first)++;
  342. /* index is being used as an index to inEdges[]. */
  343. // for (i = 0; i < edges.size(); i++)
  344. for(auto edge : edges)
  345. {
  346. pChild = edge.BBptr;
  347. pChild->inEdges[pChild->index++] = this;
  348. /* Is this the last visit? */
  349. if (pChild->index == int(pChild->inEdges.size()))
  350. pChild->index = UN_INIT;
  351. if (pChild->traversed != DFS_NUM)
  352. pChild->dfsNumbering(dfsLast, first, last);
  353. }
  354. dfsLastNum = *last;
  355. dfsLast[(*last)--] = this;
  356. }