proplong.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /**************************************************************************
  2. * File : propLong.c
  3. * Purpose: propagate the value of long variables (local variables and
  4. * registers) along the graph. Structure the graph in this way.
  5. * (C) Cristina Cifuentes
  6. **************************************************************************/
  7. #include <string.h>
  8. #include <memory.h>
  9. #include <cassert>
  10. #include <algorithm>
  11. #include "dcc.h"
  12. static boolT isJCond (llIcode opcode)
  13. /* Returns whether the given icode opcode is within the range of valid
  14. * high-level conditional jump icodes (iJB..iJG) */
  15. {
  16. if ((opcode >= iJB) && (opcode <= iJG))
  17. return true;
  18. return false;
  19. }
  20. /* Returns whether the conditions for a 2-3 long variable are satisfied */
  21. static boolT isLong23 (Int i, BB * pbb, Int *off, Int *arc)
  22. {
  23. BB * t, * e, * obb2;
  24. if (pbb->nodeType != TWO_BRANCH)
  25. return false;
  26. t = pbb->edges[THEN].BBptr;
  27. e = pbb->edges[ELSE].BBptr;
  28. /* Check along the THEN path */
  29. if ((t->length == 1) && (t->nodeType == TWO_BRANCH) && (t->inEdges.size() == 1))
  30. {
  31. obb2 = t->edges[THEN].BBptr;
  32. if ((obb2->length == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ic.ll.opcode == iCMP))
  33. {
  34. *off = obb2->front().loc_ip - i;
  35. *arc = THEN;
  36. return true;
  37. }
  38. }
  39. /* Check along the ELSE path */
  40. else if ((e->length == 1) && (e->nodeType == TWO_BRANCH) && (e->inEdges.size() == 1))
  41. {
  42. obb2 = e->edges[THEN].BBptr;
  43. if ((obb2->length == 2) && (obb2->nodeType == TWO_BRANCH) && (obb2->front().ic.ll.opcode == iCMP))
  44. {
  45. *off = obb2->front().loc_ip - i;
  46. *arc = ELSE;
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. /* Returns whether the conditions for a 2-2 long variable are satisfied */
  53. static boolT isLong22 (ICODE * pIcode, ICODE * pEnd, Int *off)
  54. {
  55. if (((pIcode+2) < pEnd) && ((pIcode+2)->ic.ll.opcode == iCMP) &&
  56. (isJCond ((pIcode+1)->ic.ll.opcode)) &&
  57. (isJCond ((pIcode+3)->ic.ll.opcode)))
  58. {
  59. *off = 2;
  60. return true;
  61. }
  62. return false;
  63. }
  64. /* Creates a long conditional <=, >=, <, or > at (pIcode+1).
  65. * Removes excess nodes from the graph by flagging them, and updates
  66. * the new edges for the remaining nodes. */
  67. static void longJCond23 (COND_EXPR *rhs, COND_EXPR *lhs, ICODE * pIcode,
  68. Int *idx, Function * pProc, Int arc, Int off)
  69. { Int j;
  70. BB * pbb, * obb1, * obb2, * tbb;
  71. if (arc == THEN)
  72. {
  73. /* Find intermediate basic blocks and target block */
  74. pbb = pIcode->inBB;
  75. obb1 = pbb->edges[THEN].BBptr;
  76. obb2 = obb1->edges[THEN].BBptr;
  77. tbb = obb2->edges[THEN].BBptr;
  78. /* Modify out edge of header basic block */
  79. pbb->edges[THEN].BBptr = tbb;
  80. /* Modify in edges of target basic block */
  81. auto newlast=std::remove_if(tbb->inEdges.begin(),tbb->inEdges.end(),
  82. [obb1,obb2](BB *b) -> bool
  83. {
  84. return (b==obb1) || (b==obb2);
  85. }
  86. );
  87. tbb->inEdges.erase(newlast,tbb->inEdges.end());
  88. tbb->inEdges.push_back(pbb); /* looses 2 arcs, gains 1 arc */
  89. /* Modify in edges of the ELSE basic block */
  90. tbb = pbb->edges[ELSE].BBptr;
  91. auto iter=std::find(tbb->inEdges.begin(),tbb->inEdges.end(),obb2);
  92. assert(iter!=tbb->inEdges.end());
  93. tbb->inEdges.erase(iter); /* looses 1 arc */
  94. /* Update icode index */
  95. (*idx) += 5;
  96. }
  97. else /* ELSE arc */
  98. {
  99. /* Find intermediate basic blocks and target block */
  100. pbb = pIcode->inBB;
  101. obb1 = pbb->edges[ELSE].BBptr;
  102. obb2 = obb1->edges[THEN].BBptr;
  103. tbb = obb2->edges[THEN].BBptr;
  104. /* Modify in edges of target basic block */
  105. auto iter=std::find(tbb->inEdges.begin(),tbb->inEdges.end(),obb2);
  106. assert(iter!=tbb->inEdges.end());
  107. tbb->inEdges.erase(iter); /* looses 1 arc */
  108. /* Modify in edges of the ELSE basic block */
  109. tbb = obb2->edges[ELSE].BBptr;
  110. auto newlast=std::remove_if(tbb->inEdges.begin(),tbb->inEdges.end(),
  111. [obb1,obb2](BB *b) -> bool
  112. {
  113. return (b==obb1) || (b==obb2);
  114. }
  115. );
  116. tbb->inEdges.erase(newlast,tbb->inEdges.end());
  117. tbb->inEdges.push_back(pbb); /* looses 2 arcs, gains 1 arc */
  118. /* Modify out edge of header basic block */
  119. pbb->edges[ELSE].BBptr = tbb;
  120. /* Update icode index */
  121. (*idx) += 2;
  122. }
  123. /* Create new HLI_JCOND and condition */
  124. lhs = COND_EXPR::boolOp (lhs, rhs, condOpJCond[(pIcode+off+1)->ic.ll.opcode-iJB]);
  125. (pIcode+1)->setJCond(lhs);
  126. (pIcode+1)->copyDU(*pIcode, eUSE, eUSE);
  127. (pIcode+1)->du.use |= (pIcode+off)->du.use;
  128. /* Update statistics */
  129. obb1->flg |= INVALID_BB;
  130. obb2->flg |= INVALID_BB;
  131. stats.numBBaft -= 2;
  132. pIcode->invalidate();
  133. obb1->front().invalidate();
  134. obb2->front().invalidate();
  135. (obb2->begin2()+1)->invalidate();
  136. }
  137. /* Creates a long conditional equality or inequality at (pIcode+1).
  138. * Removes excess nodes from the graph by flagging them, and updates
  139. * the new edges for the remaining nodes. */
  140. static void longJCond22 (COND_EXPR *rhs, COND_EXPR *lhs, ICODE * pIcode, Int *idx)
  141. {
  142. Int j;
  143. BB * pbb, * obb1, * tbb;
  144. /* Form conditional expression */
  145. lhs = COND_EXPR::boolOp (lhs, rhs, condOpJCond[(pIcode+3)->ic.ll.opcode - iJB]);
  146. (pIcode+1)->setJCond(lhs);
  147. (pIcode+1)->copyDU (*pIcode, eUSE, eUSE);
  148. (pIcode+1)->du.use |= (pIcode+2)->du.use;
  149. /* Adjust outEdges[0] to the new target basic block */
  150. pbb = pIcode->inBB;
  151. if (pbb->back().loc_ip == (*idx + 1))
  152. {
  153. /* Find intermediate and target basic blocks */
  154. obb1 = pbb->edges[THEN].BBptr;
  155. tbb = obb1->edges[THEN].BBptr;
  156. /* Modify THEN out edge of header basic block */
  157. pbb->edges[THEN].BBptr = tbb;
  158. /* Modify in edges of target basic block */
  159. auto iter=std::find(tbb->inEdges.begin(),tbb->inEdges.end(),obb1);
  160. assert(iter!=tbb->inEdges.end());
  161. tbb->inEdges.erase(iter);
  162. if ((pIcode+3)->ic.ll.opcode != iJE)
  163. tbb->inEdges.push_back(pbb); /* iJNE => replace arc */
  164. /* Modify ELSE out edge of header basic block */
  165. tbb = obb1->edges[ELSE].BBptr;
  166. pbb->edges[ELSE].BBptr = tbb;
  167. iter=std::find(tbb->inEdges.begin(),tbb->inEdges.end(),obb1);
  168. assert(iter!=tbb->inEdges.end());
  169. tbb->inEdges.erase(iter);
  170. if ((pIcode+3)->ic.ll.opcode == iJE) /* replace */
  171. tbb->inEdges.push_back(pbb);
  172. // else
  173. // tbb->numInEdges--; /* iJNE => looses 1 arc */
  174. /* Update statistics */
  175. obb1->flg |= INVALID_BB;
  176. stats.numBBaft--;
  177. }
  178. pIcode->invalidate();
  179. (pIcode+2)->invalidate();
  180. (pIcode+3)->invalidate();
  181. (*idx) += 4;
  182. }
  183. /* Propagates TYPE_LONG_(UN)SIGN icode information to the current pIcode
  184. * Pointer.
  185. * Arguments: i : index into the local identifier table
  186. * pLocId: ptr to the long local identifier
  187. * pProc : ptr to current procedure's record. */
  188. void Function::propLongStk (Int i, ID *pLocId)
  189. {
  190. Int idx, off, arc;
  191. COND_EXPR *lhs, *rhs; /* Pointers to left and right hand expression */
  192. ICODE * pIcode, * pEnd;
  193. /* Check all icodes for offHi:offLo */
  194. pEnd = this->Icode.GetIcode(this->Icode.GetNumIcodes() -1);
  195. for (idx = 0; idx < (this->Icode.GetNumIcodes() - 1); idx++)
  196. {
  197. pIcode = this->Icode.GetIcode(idx);
  198. if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE))
  199. continue;
  200. if (pIcode->ic.ll.opcode == (pIcode+1)->ic.ll.opcode)
  201. {
  202. switch (pIcode->ic.ll.opcode) {
  203. case iMOV:
  204. if (checkLongEq (pLocId->id.longStkId, pIcode, i, idx, this,
  205. &rhs, &lhs, 1) == TRUE)
  206. {
  207. pIcode->setAsgn(lhs, rhs);
  208. (pIcode+1)->invalidate();
  209. idx++;
  210. }
  211. break;
  212. case iAND: case iOR: case iXOR:
  213. if (checkLongEq (pLocId->id.longStkId, pIcode, i, idx, this,
  214. &rhs, &lhs, 1) == TRUE)
  215. {
  216. switch (pIcode->ic.ll.opcode) {
  217. case iAND: rhs = COND_EXPR::boolOp (lhs, rhs, AND);
  218. break;
  219. case iOR: rhs = COND_EXPR::boolOp (lhs, rhs, OR);
  220. break;
  221. case iXOR: rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
  222. break;
  223. }
  224. pIcode->setAsgn(lhs, rhs);
  225. (pIcode+1)->invalidate();
  226. idx++;
  227. }
  228. break;
  229. case iPUSH:
  230. if (checkLongEq (pLocId->id.longStkId, pIcode, i, idx, this,
  231. &rhs, &lhs, 1) == TRUE)
  232. {
  233. pIcode->setUnary( HLI_PUSH, lhs);
  234. (pIcode+1)->invalidate();
  235. idx++;
  236. }
  237. break;
  238. } /*eos*/
  239. }
  240. /* Check long conditional (i.e. 2 CMPs and 3 branches */
  241. else if ((pIcode->ic.ll.opcode == iCMP) && (isLong23 (idx, pIcode->inBB, &off, &arc)))
  242. {
  243. if (checkLongEq (pLocId->id.longStkId, pIcode, i, idx, this, &rhs, &lhs, off) == TRUE)
  244. longJCond23 (rhs, lhs, pIcode, &idx, this, arc, off);
  245. }
  246. /* Check for long conditional equality or inequality. This requires
  247. * 2 CMPs and 2 branches */
  248. else if ((pIcode->ic.ll.opcode == iCMP) &&
  249. isLong22 (pIcode, pEnd, &off))
  250. {
  251. if (checkLongEq (pLocId->id.longStkId, pIcode, i, idx, this,
  252. &rhs, &lhs, off) == TRUE)
  253. longJCond22 (rhs, lhs, pIcode, &idx);
  254. }
  255. }
  256. }
  257. /* Finds the definition of the long register pointed to by pLocId, and
  258. * transforms that instruction into a HIGH_LEVEL icode instruction.
  259. * Arguments: i : index into the local identifier table
  260. * pLocId: ptr to the long local identifier
  261. * pProc : ptr to current procedure's record. */
  262. void Function::propLongReg (Int i, ID *pLocId)
  263. {
  264. COND_EXPR *lhs, *rhs;
  265. Int idx, j, off, arc;
  266. ICODE * pIcode, * pEnd;
  267. ICODEMEM * pmH,* pmL; /* Pointers to dst LOW_LEVEL icodes */
  268. /* Process all definitions/uses of long registers at an icode position */
  269. pEnd = this->Icode.GetIcode(this->Icode.GetNumIcodes() -1);
  270. for (j = 0; j < pLocId->idx.size(); j++)
  271. {
  272. /* Check backwards for a definition of this long register */
  273. for (idx = pLocId->idx[j] - 1; idx > 0 ; idx--)
  274. {
  275. pIcode = this->Icode.GetIcode(idx-1);
  276. if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE))
  277. continue;
  278. if (pIcode->ic.ll.opcode == (pIcode+1)->ic.ll.opcode)
  279. switch (pIcode->ic.ll.opcode)
  280. {
  281. case iMOV:
  282. pmH = &pIcode->ic.ll.dst;
  283. pmL = &(pIcode+1)->ic.ll.dst;
  284. if ((pLocId->id.longId.h == pmH->regi) && (pLocId->id.longId.l == pmL->regi))
  285. {
  286. lhs = COND_EXPR::idLongIdx (i);
  287. this->localId.id_arr[i].idx.push_back(idx-1);
  288. pIcode->setRegDU( pmL->regi, eDEF);
  289. rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode, HIGH_FIRST, idx, eUSE, 1);
  290. pIcode->setAsgn(lhs, rhs);
  291. (pIcode+1)->invalidate();
  292. idx = 0; /* to exit the loop */
  293. }
  294. break;
  295. case iPOP:
  296. pmH = &(pIcode+1)->ic.ll.dst;
  297. pmL = &pIcode->ic.ll.dst;
  298. if ((pLocId->id.longId.h == pmH->regi) && (pLocId->id.longId.l == pmL->regi))
  299. {
  300. lhs = COND_EXPR::idLongIdx (i);
  301. pIcode->setRegDU( pmH->regi, eDEF);
  302. pIcode->setUnary(HLI_POP, lhs);
  303. (pIcode+1)->invalidate();
  304. idx = 0; /* to exit the loop */
  305. }
  306. break;
  307. /**** others missing ***/
  308. case iAND: case iOR: case iXOR:
  309. pmL = &pIcode->ic.ll.dst;
  310. pmH = &(pIcode+1)->ic.ll.dst;
  311. if ((pLocId->id.longId.h == pmH->regi) && (pLocId->id.longId.l == pmL->regi))
  312. {
  313. lhs = COND_EXPR::idLongIdx (i);
  314. pIcode->setRegDU( pmH->regi, USE_DEF);
  315. rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode, LOW_FIRST, idx, eUSE, 1);
  316. switch (pIcode->ic.ll.opcode) {
  317. case iAND: rhs = COND_EXPR::boolOp (lhs, rhs, AND);
  318. break;
  319. case iOR:
  320. rhs = COND_EXPR::boolOp (lhs, rhs, OR);
  321. break;
  322. case iXOR: rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
  323. break;
  324. } /* eos */
  325. pIcode->setAsgn(lhs, rhs);
  326. (pIcode+1)->invalidate();
  327. idx = 0;
  328. }
  329. break;
  330. } /* eos */
  331. }
  332. /* If no definition backwards, check forward for a use of this long reg */
  333. if (idx <= 0)
  334. for (idx = pLocId->idx[j] + 1; idx < this->Icode.GetNumIcodes() - 1; idx++)
  335. {
  336. pIcode = this->Icode.GetIcode(idx);
  337. if ((pIcode->type == HIGH_LEVEL) || (pIcode->invalid == TRUE))
  338. continue;
  339. if (pIcode->ic.ll.opcode == (pIcode+1)->ic.ll.opcode)
  340. switch (pIcode->ic.ll.opcode) {
  341. case iMOV:
  342. if ((pLocId->id.longId.h == pIcode->ic.ll.src.regi) &&
  343. (pLocId->id.longId.l == (pIcode+1)->ic.ll.src.regi))
  344. {
  345. rhs = COND_EXPR::idLongIdx (i);
  346. pIcode->setRegDU( (pIcode+1)->ic.ll.src.regi, eUSE);
  347. lhs = COND_EXPR::idLong (&this->localId, DST, pIcode,
  348. HIGH_FIRST, idx, eDEF, 1);
  349. pIcode->setAsgn(lhs, rhs);
  350. (pIcode+1)->invalidate();
  351. idx = this->Icode.GetNumIcodes(); /* to exit the loop */
  352. }
  353. break;
  354. case iPUSH:
  355. if ((pLocId->id.longId.h == pIcode->ic.ll.src.regi) &&
  356. (pLocId->id.longId.l == (pIcode+1)->ic.ll.src.regi))
  357. {
  358. rhs = COND_EXPR::idLongIdx (i);
  359. pIcode->setRegDU( (pIcode+1)->ic.ll.src.regi, eUSE);
  360. pIcode->setUnary(HLI_PUSH, lhs);
  361. (pIcode+1)->invalidate();
  362. }
  363. idx = this->Icode.GetNumIcodes(); /* to exit the loop */
  364. break;
  365. /*** others missing ****/
  366. case iAND: case iOR: case iXOR:
  367. pmL = &pIcode->ic.ll.dst;
  368. pmH = &(pIcode+1)->ic.ll.dst;
  369. if ((pLocId->id.longId.h == pmH->regi) &&
  370. (pLocId->id.longId.l == pmL->regi))
  371. {
  372. lhs = COND_EXPR::idLongIdx (i);
  373. pIcode->setRegDU( pmH->regi, USE_DEF);
  374. rhs = COND_EXPR::idLong (&this->localId, SRC, pIcode,
  375. LOW_FIRST, idx, eUSE, 1);
  376. switch (pIcode->ic.ll.opcode) {
  377. case iAND: rhs = COND_EXPR::boolOp (lhs, rhs, AND);
  378. break;
  379. case iOR: rhs = COND_EXPR::boolOp (lhs, rhs, OR);
  380. break;
  381. case iXOR: rhs = COND_EXPR::boolOp (lhs, rhs, XOR);
  382. break;
  383. }
  384. pIcode->setAsgn(lhs, rhs);
  385. (pIcode+1)->invalidate();
  386. idx = 0;
  387. }
  388. break;
  389. } /* eos */
  390. /* Check long conditional (i.e. 2 CMPs and 3 branches */
  391. else if ((pIcode->ic.ll.opcode == iCMP) &&
  392. (isLong23 (idx, pIcode->inBB, &off, &arc)))
  393. {
  394. if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this,
  395. &rhs, &lhs, off) == TRUE)
  396. longJCond23 (rhs, lhs, pIcode, &idx, this, arc, off);
  397. }
  398. /* Check for long conditional equality or inequality. This requires
  399. * 2 CMPs and 2 branches */
  400. else if ((pIcode->ic.ll.opcode == iCMP) &&
  401. (isLong22 (pIcode, pEnd, &off)))
  402. {
  403. if (checkLongRegEq (pLocId->id.longId, pIcode, i, idx, this,
  404. &rhs, &lhs, off) == TRUE)
  405. longJCond22 (rhs, lhs, pIcode, &idx);
  406. }
  407. /* Check for OR regH, regL
  408. * JX lab
  409. * => HLI_JCOND (regH:regL X 0) lab
  410. * This is better code than HLI_JCOND (HI(regH:regL) | LO(regH:regL)) */
  411. else if ((pIcode->ic.ll.opcode == iOR) && ((pIcode+1) < pEnd) &&
  412. (isJCond ((pIcode+1)->ic.ll.opcode)))
  413. {
  414. if ((pIcode->ic.ll.dst.regi == pLocId->id.longId.h) &&
  415. (pIcode->ic.ll.src.regi == pLocId->id.longId.l))
  416. {
  417. lhs = COND_EXPR::idLongIdx (i);
  418. rhs = COND_EXPR::idKte (0, 4); /* long 0 */
  419. lhs = COND_EXPR::boolOp (lhs, rhs,
  420. condOpJCond[(pIcode+1)->ic.ll.opcode - iJB]);
  421. (pIcode+1)->setJCond(lhs);
  422. (pIcode+1)->copyDU(*pIcode, eUSE, eUSE);
  423. pIcode->invalidate();
  424. }
  425. }
  426. } /* end for */
  427. } /* end for */
  428. }
  429. /* Propagates the long global address across all LOW_LEVEL icodes.
  430. * Transforms some LOW_LEVEL icodes into HIGH_LEVEL */
  431. void Function::propLongGlb (Int i, ID *pLocId)
  432. {
  433. printf("WARN: Function::propLongGlb not implemented");
  434. }
  435. /* Propagated identifier information, thus converting some LOW_LEVEL icodes
  436. * into HIGH_LEVEL icodes. */
  437. void Function::propLong()
  438. {
  439. Int i;
  440. ID *pLocId; /* Pointer to current local identifier */
  441. for (i = 0; i < localId.csym(); i++)
  442. {
  443. pLocId = &localId.id_arr[i];
  444. if ((pLocId->type==TYPE_LONG_SIGN) || (pLocId->type==TYPE_LONG_UNSIGN))
  445. {
  446. switch (pLocId->loc)
  447. {
  448. case STK_FRAME:
  449. propLongStk (i, pLocId);
  450. break;
  451. case REG_FRAME:
  452. propLongReg (i, pLocId);
  453. break;
  454. case GLB_FRAME:
  455. propLongGlb (i, pLocId);
  456. break;
  457. }
  458. }
  459. }
  460. }