dataflow.cpp 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*****************************************************************************
  2. * Project: dcc
  3. * File: dataflow.c
  4. * Purpose: Data flow analysis module.
  5. * (C) Cristina Cifuentes
  6. ****************************************************************************/
  7. #include "dcc.h"
  8. #include <string.h>
  9. #include <stdio.h>
  10. /* Returns the index of the local variable or parameter at offset off, if it
  11. * is in the stack frame provided. */
  12. Int STKFRAME::getLocVar(Int off)
  13. { Int i;
  14. for (i = 0; i < sym.size(); i++)
  15. if (sym[i].off == off)
  16. break;
  17. return (i);
  18. }
  19. /* Returns a string with the source operand of Icode */
  20. static COND_EXPR *srcIdent (ICODE * Icode, Function * pProc, Int i, ICODE * duIcode, operDu du)
  21. {
  22. COND_EXPR *n;
  23. if (Icode->ic.ll.flg & I) /* immediate operand */
  24. {
  25. if (Icode->ic.ll.flg & B)
  26. n = COND_EXPR::idKte (Icode->ic.ll.immed.op, 1);
  27. else
  28. n = COND_EXPR::idKte (Icode->ic.ll.immed.op, 2);
  29. }
  30. else
  31. n = COND_EXPR::id (Icode, SRC, pProc, i, duIcode, du);
  32. return (n);
  33. }
  34. /* Returns the destination operand */
  35. static COND_EXPR *dstIdent (ICODE * pIcode, Function * pProc, Int i, ICODE * duIcode,
  36. operDu du)
  37. { COND_EXPR *n;
  38. n = COND_EXPR::id (pIcode, DST, pProc, i, duIcode, du);
  39. /** Is it needed? (pIcode->ic.ll.flg) & NO_SRC_B **/
  40. return (n);
  41. }
  42. /* Eliminates all condition codes and generates new hlIcode instructions */
  43. void Function::elimCondCodes ()
  44. {
  45. Int i,
  46. useAt, /* Index to instruction that used flag */
  47. defAt; /* Index to instruction that defined flag */
  48. byte use; /* Used flags bit vector */
  49. byte def; /* Defined flags bit vector */
  50. boolT notSup; /* Use/def combination not supported */
  51. COND_EXPR *rhs; /* Source operand */
  52. COND_EXPR *lhs; /* Destination operand */
  53. COND_EXPR *exp; /* Boolean expression */
  54. BB * pBB; /* Pointer to BBs in dfs last ordering */
  55. ICODE *prev; /* For extended basic blocks - previous icode inst */
  56. for (i = 0; i < numBBs; i++)
  57. {
  58. pBB = dfsLast[i];
  59. if (pBB->flg & INVALID_BB) continue; /* Do not process invalid BBs */
  60. for (useAt = pBB->start + pBB->length; useAt != pBB->start; useAt--)
  61. if ((Icode.GetIcode(useAt-1)->type == LOW_LEVEL) &&
  62. (Icode.GetIcode(useAt-1)->invalid == FALSE) &&
  63. (use = Icode.GetIcode(useAt-1)->ic.ll.flagDU.u))
  64. {
  65. /* Find definition within the same basic block */
  66. for (defAt = useAt-1; defAt != pBB->start; defAt--)
  67. {
  68. def = Icode.GetIcode(defAt-1)->ic.ll.flagDU.d;
  69. if ((use & def) == use)
  70. {
  71. notSup = FALSE;
  72. if ((Icode.GetLlOpcode(useAt-1) >= iJB) &&
  73. (Icode.GetLlOpcode(useAt-1) <= iJNS))
  74. {
  75. switch (Icode.GetLlOpcode(defAt-1))
  76. {
  77. case iCMP:
  78. rhs = srcIdent (Icode.GetIcode(defAt-1),
  79. this, defAt-1,
  80. Icode.GetIcode(useAt-1), eUSE);
  81. lhs = dstIdent (Icode.GetIcode(defAt-1),
  82. this, defAt-1,
  83. Icode.GetIcode(useAt-1), eUSE);
  84. break;
  85. case iOR:
  86. lhs = Icode.GetIcode(defAt-1)->ic.hl.oper.asgn.lhs->clone();
  87. Icode[useAt-1].copyDU(Icode[defAt-1], eUSE, eDEF);
  88. if (Icode.GetLlFlag(defAt-1) & B)
  89. rhs = COND_EXPR::idKte (0, 1);
  90. else
  91. rhs = COND_EXPR::idKte (0, 2);
  92. break;
  93. case iTEST:
  94. rhs = srcIdent (Icode.GetIcode(defAt-1),
  95. this, defAt-1,
  96. Icode.GetIcode(useAt-1), eUSE);
  97. lhs = dstIdent (Icode.GetIcode(defAt-1),
  98. this, defAt-1,
  99. Icode.GetIcode(useAt-1), eUSE);
  100. lhs = COND_EXPR::boolOp (lhs, rhs, AND);
  101. if (Icode.GetLlFlag(defAt-1) & B)
  102. rhs = COND_EXPR::idKte (0, 1);
  103. else
  104. rhs = COND_EXPR::idKte (0, 2);
  105. break;
  106. default:
  107. notSup = TRUE;
  108. reportError (JX_NOT_DEF, Icode.GetLlOpcode(defAt-1));
  109. flg |= PROC_ASM; /* generate asm */
  110. }
  111. if (! notSup)
  112. {
  113. exp = COND_EXPR::boolOp (lhs, rhs,
  114. condOpJCond[Icode.GetLlOpcode(useAt-1)-iJB]);
  115. Icode.GetIcode(useAt-1)->setJCond(exp);
  116. }
  117. }
  118. else if (Icode.GetLlOpcode(useAt-1) == iJCXZ)
  119. {
  120. lhs = COND_EXPR::idReg (rCX, 0, &localId);
  121. Icode.GetIcode(useAt-1)->setRegDU (rCX, eUSE);
  122. rhs = COND_EXPR::idKte (0, 2);
  123. exp = COND_EXPR::boolOp (lhs, rhs, EQUAL);
  124. Icode.GetIcode(useAt-1)->setJCond(exp);
  125. }
  126. else
  127. {
  128. reportError (NOT_DEF_USE,
  129. Icode.GetLlOpcode(defAt-1),
  130. Icode.GetLlOpcode(useAt-1));
  131. flg |= PROC_ASM; /* generate asm */
  132. }
  133. break;
  134. }
  135. }
  136. /* Check for extended basic block */
  137. if ((pBB->length == 1) &&
  138. (Icode.GetLlOpcode(useAt-1) >= iJB) &&
  139. (Icode.GetLlOpcode(useAt-1) <= iJNS))
  140. {
  141. prev = Icode.GetIcode(pBB->inEdges[0]->start +
  142. pBB->inEdges[0]->length - 1);
  143. if (prev->ic.hl.opcode == HLI_JCOND)
  144. {
  145. exp = prev->ic.hl.oper.exp->clone();
  146. exp->changeBoolOp (condOpJCond[Icode.GetLlOpcode(useAt-1)-iJB]);
  147. Icode[useAt-1].copyDU(*prev, eUSE, eUSE);
  148. Icode[useAt-1].setJCond(exp);
  149. }
  150. }
  151. /* Error - definition not found for use of a cond code */
  152. else if (defAt == pBB->start)
  153. {
  154. reportError(DEF_NOT_FOUND,Icode.GetLlOpcode(useAt-1));
  155. //fatalError (DEF_NOT_FOUND, Icode.GetLlOpcode(useAt-1));
  156. }
  157. }
  158. }
  159. }
  160. /* Generates the LiveUse() and Def() sets for each basic block in the graph.
  161. * Note: these sets are constant and could have been constructed during
  162. * the construction of the graph, but since the code hasn't been
  163. * analyzed yet for idioms, the procedure preamble misleads the
  164. * analysis (eg: push si, would include si in LiveUse; although it
  165. * is not really meant to be a register that is used before defined). */
  166. void Function::genLiveKtes ()
  167. { Int i, j;
  168. BB * pbb;
  169. ICODE * picode;
  170. dword liveUse, def;
  171. for (i = 0; i < numBBs; i++)
  172. {
  173. liveUse = def = 0;
  174. pbb = dfsLast[i];
  175. if (pbb->flg & INVALID_BB)
  176. continue; /* skip invalid BBs */
  177. for (j = pbb->start; j < (pbb->start + pbb->length); j++)
  178. {
  179. picode = Icode.GetIcode(j);
  180. if ((picode->type == HIGH_LEVEL) && (picode->invalid == FALSE))
  181. {
  182. liveUse |= (picode->du.use & ~def);
  183. def |= picode->du.def;
  184. }
  185. }
  186. pbb->liveUse = liveUse;
  187. pbb->def = def;
  188. }
  189. }
  190. /* Generates the liveIn() and liveOut() sets for each basic block via an
  191. * iterative approach.
  192. * Propagates register usage information to the procedure call. */
  193. void Function::liveRegAnalysis (dword in_liveOut)
  194. {
  195. Int i, j;
  196. BB * pbb=0; /* pointer to current basic block */
  197. Function * pcallee; /* invoked subroutine */
  198. ICODE *ticode, /* icode that invokes a subroutine */
  199. *picode; /* icode of function return */
  200. dword prevLiveOut, /* previous live out */
  201. prevLiveIn; /* previous live in */
  202. boolT change; /* is there change in the live sets?*/
  203. /* liveOut for this procedure */
  204. liveOut = in_liveOut;
  205. change = TRUE;
  206. while (change)
  207. {
  208. /* Process nodes in reverse postorder order */
  209. change = FALSE;
  210. for (i = numBBs; i > 0; i--)
  211. {
  212. pbb = dfsLast[i-1];
  213. if (pbb->flg & INVALID_BB) /* Do not process invalid BBs */
  214. continue;
  215. /* Get current liveIn() and liveOut() sets */
  216. prevLiveIn = pbb->liveIn;
  217. prevLiveOut = pbb->liveOut;
  218. /* liveOut(b) = U LiveIn(s); where s is successor(b)
  219. * liveOut(b) = {liveOut}; when b is a HLI_RET node */
  220. if (pbb->edges.empty()) /* HLI_RET node */
  221. {
  222. pbb->liveOut = in_liveOut;
  223. /* Get return expression of function */
  224. if (flg & PROC_IS_FUNC)
  225. {
  226. picode = Icode.GetIcode(pbb->start + pbb->length - 1);
  227. if (picode->ic.hl.opcode == HLI_RET)
  228. {
  229. picode->ic.hl.oper.exp = COND_EXPR::idID (&retVal,
  230. &localId, pbb->start + pbb->length - 1);
  231. picode->du.use = in_liveOut;
  232. }
  233. }
  234. }
  235. else /* Check successors */
  236. {
  237. for (j = 0; j < pbb->edges.size(); j++)
  238. pbb->liveOut |= pbb->edges[j].BBptr->liveIn;
  239. /* propagate to invoked procedure */
  240. if (pbb->nodeType == CALL_NODE)
  241. {
  242. ticode = Icode.GetIcode(pbb->start + pbb->length - 1);
  243. pcallee = ticode->ic.hl.oper.call.proc;
  244. /* user/runtime routine */
  245. if (! (pcallee->flg & PROC_ISLIB))
  246. {
  247. if (pcallee->liveAnal == FALSE) /* hasn't been processed */
  248. pcallee->dataFlow (pbb->liveOut);
  249. pbb->liveOut = pcallee->liveIn;
  250. }
  251. else /* library routine */
  252. {
  253. if ((pcallee->flg & PROC_IS_FUNC) && /* returns a value */
  254. (pcallee->liveOut & pbb->edges[0].BBptr->liveIn))
  255. pbb->liveOut = pcallee->liveOut;
  256. else
  257. pbb->liveOut = 0;
  258. }
  259. if ((! (pcallee->flg & PROC_ISLIB)) || (pbb->liveOut != 0))
  260. {
  261. switch (pcallee->retVal.type) {
  262. case TYPE_LONG_SIGN: case TYPE_LONG_UNSIGN:
  263. ticode->du1.numRegsDef = 2;
  264. break;
  265. case TYPE_WORD_SIGN: case TYPE_WORD_UNSIGN:
  266. case TYPE_BYTE_SIGN: case TYPE_BYTE_UNSIGN:
  267. ticode->du1.numRegsDef = 1;
  268. break;
  269. } /*eos*/
  270. /* Propagate def/use results to calling icode */
  271. ticode->du.use = pcallee->liveIn;
  272. ticode->du.def = pcallee->liveOut;
  273. }
  274. }
  275. }
  276. /* liveIn(b) = liveUse(b) U (liveOut(b) - def(b) */
  277. pbb->liveIn = pbb->liveUse | (pbb->liveOut & ~pbb->def);
  278. /* Check if live sets have been modified */
  279. if ((prevLiveIn != pbb->liveIn) || (prevLiveOut != pbb->liveOut))
  280. change = TRUE;
  281. }
  282. }
  283. /* Propagate liveIn(b) to procedure header */
  284. if (pbb->liveIn != 0) /* uses registers */
  285. liveIn = pbb->liveIn;
  286. /* Remove any references to register variables */
  287. if (flg & SI_REGVAR)
  288. {
  289. liveIn &= maskDuReg[rSI];
  290. pbb->liveIn &= maskDuReg[rSI];
  291. }
  292. if (flg & DI_REGVAR)
  293. {
  294. liveIn &= maskDuReg[rDI];
  295. pbb->liveIn &= maskDuReg[rDI];
  296. }
  297. }
  298. /* Generates the du chain of each instruction in a basic block */
  299. void Function::genDU1 ()
  300. {
  301. byte regi; /* Register that was defined */
  302. Int i, j, k, p, n, lastInst, defRegIdx, useIdx;
  303. ICODE * picode, *ticode;/* Current and target bb */
  304. BB * pbb, *tbb; /* Current and target basic block */
  305. boolT res;
  306. COND_EXPR *exp, *lhs;
  307. /* Traverse tree in dfsLast order */
  308. for (i = 0; i < numBBs; i++)
  309. {
  310. pbb = dfsLast[i];
  311. if (pbb->flg & INVALID_BB) continue;
  312. /* Process each register definition of a HIGH_LEVEL icode instruction.
  313. * Note that register variables should not be considered registers.
  314. */
  315. lastInst = pbb->start + pbb->length;
  316. for (j = pbb->start; j < lastInst; j++)
  317. {
  318. picode = Icode.GetIcode(j);
  319. if (picode->type == HIGH_LEVEL)
  320. {
  321. regi = 0;
  322. defRegIdx = 0;
  323. for (k = 0; k < INDEXBASE; k++)
  324. {
  325. if ((picode->du.def & power2(k)) != 0)
  326. {
  327. regi = (byte)(k + 1); /* defined register */
  328. picode->du1.regi[defRegIdx] = regi;
  329. /* Check remaining instructions of the BB for all uses
  330. * of register regi, before any definitions of the
  331. * register */
  332. if ((regi == rDI) && (flg & DI_REGVAR))
  333. continue;
  334. if ((regi == rSI) && (flg & SI_REGVAR))
  335. continue;
  336. if ((j + 1) < lastInst) /* several instructions */
  337. {
  338. useIdx = 0;
  339. for (n = j+1; n < lastInst; n++)
  340. {
  341. /* Only check uses of HIGH_LEVEL icodes */
  342. ticode = Icode.GetIcode(n);
  343. if (ticode->type == HIGH_LEVEL)
  344. {
  345. /* if used, get icode index */
  346. if (ticode->du.use & duReg[regi])
  347. picode->du1.idx[defRegIdx][useIdx++] = n;
  348. /* if defined, stop finding uses for this reg */
  349. if (ticode->du.def & duReg[regi])
  350. break;
  351. }
  352. }
  353. /* Check if last definition of this register */
  354. if ((! (ticode->du.def & duReg[regi])) &&
  355. (pbb->liveOut & duReg[regi]))
  356. picode->du.lastDefRegi |= duReg[regi];
  357. }
  358. else /* only 1 instruction in this basic block */
  359. {
  360. /* Check if last definition of this register */
  361. if (pbb->liveOut & duReg[regi])
  362. picode->du.lastDefRegi |= duReg[regi];
  363. }
  364. /* Find target icode for HLI_CALL icodes to procedures
  365. * that are functions. The target icode is in the
  366. * next basic block (unoptimized code) or somewhere else
  367. * on optimized code. */
  368. if ((picode->ic.hl.opcode == HLI_CALL) &&
  369. (picode->ic.hl.oper.call.proc->flg & PROC_IS_FUNC))
  370. {
  371. tbb = pbb->edges[0].BBptr;
  372. useIdx = 0;
  373. for (n = tbb->start; n < tbb->start + tbb->length; n++)
  374. {
  375. ticode = Icode.GetIcode(n);
  376. if (ticode->type == HIGH_LEVEL)
  377. {
  378. /* if used, get icode index */
  379. if (ticode->du.use & duReg[regi])
  380. picode->du1.idx[defRegIdx][useIdx++] = n;
  381. /* if defined, stop finding uses for this reg */
  382. if (ticode->du.def & duReg[regi])
  383. break;
  384. }
  385. }
  386. /* if not used in this basic block, check if the
  387. * register is live out, if so, make it the last
  388. * definition of this register */
  389. if ((picode->du1.idx[defRegIdx][useIdx] == 0) &&
  390. (tbb->liveOut & duReg[regi]))
  391. picode->du.lastDefRegi |= duReg[regi];
  392. }
  393. /* If not used within this bb or in successors of this
  394. * bb (ie. not in liveOut), then register is useless,
  395. * thus remove it. Also check that this is not a return
  396. * from a library function (routines such as printf
  397. * return an integer, which is normally not taken into
  398. * account by the programmer). */
  399. if ((picode->invalid == FALSE) &&
  400. (picode->du1.idx[defRegIdx][0] == 0) &&
  401. (! (picode->du.lastDefRegi & duReg[regi])) &&
  402. // (! ((picode->ic.hl.opcode != HLI_CALL) &&
  403. (! ((picode->ic.hl.opcode == HLI_CALL) &&
  404. (picode->ic.hl.oper.call.proc->flg & PROC_ISLIB))))
  405. {
  406. if (! (pbb->liveOut & duReg[regi])) /* not liveOut */
  407. {
  408. res = picode->removeDefRegi (regi, defRegIdx+1,&localId);
  409. /* Backpatch any uses of this instruction, within
  410. * the same BB, if the instruction was invalidated */
  411. if (res == TRUE)
  412. for (p = j; p > pbb->start; p--)
  413. {
  414. ticode = Icode.GetIcode(p-1);
  415. for (n = 0; n < MAX_USES; n++)
  416. {
  417. if (ticode->du1.idx[0][n] == j)
  418. {
  419. if (n < MAX_USES - 1)
  420. {
  421. memmove (&ticode->du1.idx[0][n],
  422. &ticode->du1.idx[0][n+1],
  423. (size_t)((MAX_USES - n - 1) * sizeof(Int)));
  424. n--;
  425. }
  426. ticode->du1.idx[0][MAX_USES - 1] = 0;
  427. }
  428. }
  429. }
  430. }
  431. else /* liveOut */
  432. picode->du.lastDefRegi |= duReg[regi];
  433. }
  434. defRegIdx++;
  435. /* Check if all defined registers have been processed */
  436. if ((defRegIdx >= picode->du1.numRegsDef) ||
  437. (defRegIdx == MAX_REGS_DEF))
  438. break;
  439. }
  440. }
  441. }
  442. }
  443. }
  444. }
  445. /* Substitutes the rhs (or lhs if rhs not possible) of ticode for the rhs
  446. * of picode. */
  447. static void forwardSubs (COND_EXPR *lhs, COND_EXPR *rhs, ICODE * picode,
  448. ICODE * ticode, LOCAL_ID *locsym, Int *numHlIcodes)
  449. {
  450. boolT res;
  451. if (rhs == NULL) /* In case expression popped is NULL */
  452. return;
  453. /* Insert on rhs of ticode, if possible */
  454. res = insertSubTreeReg (rhs, &ticode->ic.hl.oper.asgn.rhs,
  455. locsym->id_arr[lhs->expr.ident.idNode.regiIdx].id.regi,
  456. locsym);
  457. if (res)
  458. {
  459. picode->invalidate();
  460. (*numHlIcodes)--;
  461. }
  462. else
  463. {
  464. /* Try to insert it on lhs of ticode*/
  465. res = insertSubTreeReg (rhs, &ticode->ic.hl.oper.asgn.lhs,
  466. locsym->id_arr[lhs->expr.ident.idNode.regiIdx].id.regi,
  467. locsym);
  468. if (res)
  469. {
  470. picode->invalidate();
  471. (*numHlIcodes)--;
  472. }
  473. }
  474. }
  475. /* Substitutes the rhs (or lhs if rhs not possible) of ticode for the
  476. * expression exp given */
  477. static void forwardSubsLong (Int longIdx, COND_EXPR *exp, ICODE * picode,
  478. ICODE * ticode, Int *numHlIcodes)
  479. { boolT res;
  480. if (exp == NULL) /* In case expression popped is NULL */
  481. return;
  482. /* Insert on rhs of ticode, if possible */
  483. res = insertSubTreeLongReg (exp, &ticode->ic.hl.oper.asgn.rhs, longIdx);
  484. if (res)
  485. {
  486. picode->invalidate();
  487. (*numHlIcodes)--;
  488. }
  489. else
  490. {
  491. /* Try to insert it on lhs of ticode*/
  492. res = insertSubTreeLongReg (exp, &ticode->ic.hl.oper.asgn.lhs, longIdx);
  493. if (res)
  494. {
  495. picode->invalidate();
  496. (*numHlIcodes)--;
  497. }
  498. }
  499. }
  500. /* Returns whether the elements of the expression rhs are all x-clear from
  501. * instruction f up to instruction t. */
  502. static boolT xClear (COND_EXPR *rhs, Int f, Int t, Int lastBBinst, Function * pproc)
  503. { Int i;
  504. boolT res;
  505. byte regi;
  506. ICODE * picode;
  507. if (rhs == NULL)
  508. return false;
  509. switch (rhs->type) {
  510. case IDENTIFIER:
  511. if (rhs->expr.ident.idType == REGISTER)
  512. {
  513. picode = &pproc->Icode.front();
  514. regi= pproc->localId.id_arr[rhs->expr.ident.idNode.regiIdx].id.regi;
  515. for (i = (f + 1); (i < lastBBinst) && (i < t); i++)
  516. if ((picode[i].type == HIGH_LEVEL) &&
  517. (picode[i].invalid == FALSE))
  518. {
  519. if (picode[i].du.def & duReg[regi])
  520. return false;
  521. }
  522. if (i < lastBBinst)
  523. return true;
  524. else
  525. return false;
  526. }
  527. else
  528. return true;
  529. /* else if (rhs->expr.ident.idType == LONG_VAR)
  530. {
  531. missing all other identifiers ****
  532. } */
  533. case BOOLEAN_OP:
  534. res = xClear (rhs->expr.boolExpr.rhs, f, t, lastBBinst, pproc);
  535. if (res == FALSE)
  536. return false;
  537. return (xClear (rhs->expr.boolExpr.lhs, f, t, lastBBinst, pproc));
  538. case NEGATION:
  539. case ADDRESSOF:
  540. case DEREFERENCE:
  541. return (xClear (rhs->expr.unaryExp, f, t, lastBBinst, pproc));
  542. } /* eos */
  543. return FALSE;
  544. }
  545. /* Checks the type of the formal argument as against to the actual argument,
  546. * whenever possible, and then places the actual argument on the procedure's
  547. * argument list. */
  548. static void processCArg (Function * pp, Function * pProc, ICODE * picode, Int numArgs, Int *k)
  549. {
  550. COND_EXPR *exp;
  551. boolT res;
  552. /* if (numArgs == 0)
  553. return; */
  554. exp = popExpStk();
  555. if (pp->flg & PROC_ISLIB) /* library function */
  556. {
  557. if (pp->args.numArgs > 0)
  558. if (pp->flg & PROC_VARARG)
  559. {
  560. if (numArgs < pp->args.sym.size())
  561. adjustActArgType (exp, pp->args.sym[numArgs].type, pProc);
  562. }
  563. else
  564. adjustActArgType (exp, pp->args.sym[numArgs].type, pProc);
  565. res = newStkArg (picode, exp, picode->ic.ll.opcode, pProc);
  566. }
  567. else /* user function */
  568. {
  569. if (pp->args.numArgs > 0)
  570. pp->args.adjustForArgType (numArgs, expType (exp, pProc));
  571. res = newStkArg (picode, exp, picode->ic.ll.opcode, pProc);
  572. }
  573. /* Do not update the size of k if the expression was a segment register
  574. * in a near call */
  575. if (res == FALSE)
  576. *k += hlTypeSize (exp, pProc);
  577. }
  578. /* Eliminates extraneous intermediate icode instructions when finding
  579. * expressions. Generates new hlIcodes in the form of expression trees.
  580. * For HLI_CALL hlIcodes, places the arguments in the argument list. */
  581. void Function::findExps()
  582. {
  583. Int i, j, k, lastInst, numHlIcodes;
  584. ICODE * picode, /* Current icode */
  585. * ticode; /* Target icode */
  586. BB * pbb; /* Current and next basic block */
  587. boolT res;
  588. COND_EXPR *exp, /* expression pointer - for HLI_POP and HLI_CALL */
  589. *lhs; /* exp ptr for return value of a HLI_CALL */
  590. STKFRAME * args; /* pointer to arguments - for HLI_CALL */
  591. byte regi, regi2; /* register(s) to be forward substituted */
  592. ID *retVal; /* function return value */
  593. /* Initialize expression stack */
  594. initExpStk();
  595. /* Traverse tree in dfsLast order */
  596. for (i = 0; i < numBBs; i++)
  597. {
  598. /* Process one BB */
  599. pbb = dfsLast[i];
  600. if (pbb->flg & INVALID_BB) continue;
  601. lastInst = pbb->start + pbb->length;
  602. numHlIcodes = 0;
  603. for (j = pbb->start; j < lastInst; j++)
  604. {
  605. picode = Icode.GetIcode(j);
  606. if ((picode->type == HIGH_LEVEL) && (picode->invalid == FALSE))
  607. {
  608. numHlIcodes++;
  609. if (picode->du1.numRegsDef == 1) /* byte/word regs */
  610. {
  611. /* Check for only one use of this register. If this is
  612. * the last definition of the register in this BB, check
  613. * that it is not liveOut from this basic block */
  614. if ((picode->du1.idx[0][0] != 0) &&
  615. (picode->du1.idx[0][1] == 0))
  616. {
  617. /* Check that this register is not liveOut, if it
  618. * is the last definition of the register */
  619. regi = picode->du1.regi[0];
  620. /* Check if we can forward substitute this register */
  621. switch (picode->ic.hl.opcode) {
  622. case HLI_ASSIGN:
  623. /* Replace rhs of current icode into target
  624. * icode expression */
  625. ticode = Icode.GetIcode(picode->du1.idx[0][0]);
  626. if ((picode->du.lastDefRegi & duReg[regi]) &&
  627. ((ticode->ic.hl.opcode != HLI_CALL) &&
  628. (ticode->ic.hl.opcode != HLI_RET)))
  629. continue;
  630. if (xClear (picode->ic.hl.oper.asgn.rhs, j, picode->du1.idx[0][0], lastInst, this))
  631. {
  632. switch (ticode->ic.hl.opcode) {
  633. case HLI_ASSIGN:
  634. forwardSubs (picode->ic.hl.oper.asgn.lhs,
  635. picode->ic.hl.oper.asgn.rhs,
  636. picode, ticode, &localId,
  637. &numHlIcodes);
  638. break;
  639. case HLI_JCOND: case HLI_PUSH: case HLI_RET:
  640. res = insertSubTreeReg (
  641. picode->ic.hl.oper.asgn.rhs,
  642. &ticode->ic.hl.oper.exp,
  643. localId.id_arr[picode->ic.hl.oper.asgn.lhs->expr.ident.idNode.regiIdx].id.regi,
  644. &localId);
  645. if (res)
  646. {
  647. picode->invalidate();
  648. numHlIcodes--;
  649. }
  650. break;
  651. case HLI_CALL: /* register arguments */
  652. newRegArg (picode, ticode);
  653. picode->invalidate();
  654. numHlIcodes--;
  655. break;
  656. } /* eos */
  657. }
  658. break;
  659. case HLI_POP:
  660. ticode = Icode.GetIcode(picode->du1.idx[0][0]);
  661. if ((picode->du.lastDefRegi & duReg[regi]) &&
  662. ((ticode->ic.hl.opcode != HLI_CALL) &&
  663. (ticode->ic.hl.opcode != HLI_RET)))
  664. continue;
  665. exp = popExpStk(); /* pop last exp pushed */
  666. switch (ticode->ic.hl.opcode) {
  667. case HLI_ASSIGN:
  668. forwardSubs (picode->ic.hl.oper.exp, exp,
  669. picode, ticode, &localId,
  670. &numHlIcodes);
  671. break;
  672. case HLI_JCOND: case HLI_PUSH: case HLI_RET:
  673. res = insertSubTreeReg (exp,
  674. &ticode->ic.hl.oper.exp,
  675. localId.id_arr[picode->ic.hl.oper.exp->expr.ident.idNode.regiIdx].id.regi,
  676. &localId);
  677. if (res)
  678. {
  679. picode->invalidate();
  680. numHlIcodes--;
  681. }
  682. break;
  683. /****case HLI_CALL: /* register arguments
  684. newRegArg (pProc, picode, ticode);
  685. picode->invalidate();
  686. numHlIcodes--;
  687. break; */
  688. } /* eos */
  689. break;
  690. case HLI_CALL:
  691. ticode = Icode.GetIcode(picode->du1.idx[0][0]);
  692. switch (ticode->ic.hl.opcode) {
  693. case HLI_ASSIGN:
  694. exp = COND_EXPR::idFunc (
  695. picode->ic.hl.oper.call.proc,
  696. picode->ic.hl.oper.call.args);
  697. res = insertSubTreeReg (exp,
  698. &ticode->ic.hl.oper.asgn.rhs,
  699. picode->ic.hl.oper.call.proc->retVal.id.regi,
  700. &localId);
  701. if (! res)
  702. insertSubTreeReg (exp,
  703. &ticode->ic.hl.oper.asgn.lhs,
  704. picode->ic.hl.oper.call.proc->retVal.id.regi,
  705. &localId);
  706. /*** HERE missing: 2 regs ****/
  707. picode->invalidate();
  708. numHlIcodes--;
  709. break;
  710. case HLI_PUSH: case HLI_RET:
  711. exp = COND_EXPR::idFunc (
  712. picode->ic.hl.oper.call.proc,
  713. picode->ic.hl.oper.call.args);
  714. ticode->ic.hl.oper.exp = exp;
  715. picode->invalidate();
  716. numHlIcodes--;
  717. break;
  718. case HLI_JCOND:
  719. exp = COND_EXPR::idFunc (
  720. picode->ic.hl.oper.call.proc,
  721. picode->ic.hl.oper.call.args);
  722. retVal = &picode->ic.hl.oper.call.proc->retVal,
  723. res = insertSubTreeReg (exp,
  724. &ticode->ic.hl.oper.exp,
  725. retVal->id.regi, &localId);
  726. if (res) /* was substituted */
  727. {
  728. picode->invalidate();
  729. numHlIcodes--;
  730. }
  731. else /* cannot substitute function */
  732. {
  733. lhs = COND_EXPR::idID(retVal,&localId,j);
  734. picode->setAsgn(lhs, exp);
  735. }
  736. break;
  737. } /* eos */
  738. break;
  739. } /* eos */
  740. }
  741. }
  742. else if (picode->du1.numRegsDef == 2) /* long regs */
  743. {
  744. /* Check for only one use of these registers */
  745. if ((picode->du1.idx[0][0] != 0) &&
  746. (picode->du1.idx[0][1] == 0) &&
  747. (picode->du1.idx[1][0] != 0) &&
  748. (picode->du1.idx[1][1] == 0))
  749. {
  750. switch (picode->ic.hl.opcode) {
  751. case HLI_ASSIGN:
  752. /* Replace rhs of current icode into target
  753. * icode expression */
  754. if (picode->du1.idx[0][0] == picode->du1.idx[1][0])
  755. {
  756. ticode = Icode.GetIcode(picode->du1.idx[0][0]);
  757. if ((picode->du.lastDefRegi & duReg[regi]) &&
  758. ((ticode->ic.hl.opcode != HLI_CALL) &&
  759. (ticode->ic.hl.opcode != HLI_RET)))
  760. continue;
  761. switch (ticode->ic.hl.opcode) {
  762. case HLI_ASSIGN:
  763. forwardSubsLong (picode->ic.hl.oper.asgn.lhs->expr.ident.idNode.longIdx,
  764. picode->ic.hl.oper.asgn.rhs, picode,
  765. ticode, &numHlIcodes);
  766. break;
  767. case HLI_JCOND: case HLI_PUSH: case HLI_RET:
  768. res = insertSubTreeLongReg (
  769. picode->ic.hl.oper.asgn.rhs,
  770. &ticode->ic.hl.oper.exp,
  771. picode->ic.hl.oper.asgn.lhs->expr.ident.idNode.longIdx);
  772. if (res)
  773. {
  774. picode->invalidate();
  775. numHlIcodes--;
  776. }
  777. break;
  778. case HLI_CALL: /* register arguments */
  779. newRegArg ( picode, ticode);
  780. picode->invalidate();
  781. numHlIcodes--;
  782. break;
  783. } /* eos */
  784. }
  785. break;
  786. case HLI_POP:
  787. if (picode->du1.idx[0][0] == picode->du1.idx[1][0])
  788. {
  789. ticode = Icode.GetIcode(picode->du1.idx[0][0]);
  790. if ((picode->du.lastDefRegi & duReg[regi]) &&
  791. ((ticode->ic.hl.opcode != HLI_CALL) &&
  792. (ticode->ic.hl.opcode != HLI_RET)))
  793. continue;
  794. exp = popExpStk(); /* pop last exp pushed */
  795. switch (ticode->ic.hl.opcode) {
  796. case HLI_ASSIGN:
  797. forwardSubsLong (picode->ic.hl.oper.exp->expr.ident.idNode.longIdx,
  798. exp, picode, ticode, &numHlIcodes);
  799. break;
  800. case HLI_JCOND: case HLI_PUSH:
  801. res = insertSubTreeLongReg (exp,
  802. &ticode->ic.hl.oper.exp,
  803. picode->ic.hl.oper.asgn.lhs->expr.ident.idNode.longIdx);
  804. if (res)
  805. {
  806. picode->invalidate();
  807. numHlIcodes--;
  808. }
  809. break;
  810. case HLI_CALL: /*** missing ***/
  811. break;
  812. } /* eos */
  813. }
  814. break;
  815. case HLI_CALL: /* check for function return */
  816. ticode = Icode.GetIcode(picode->du1.idx[0][0]);
  817. switch (ticode->ic.hl.opcode)
  818. {
  819. case HLI_ASSIGN:
  820. exp = COND_EXPR::idFunc (
  821. picode->ic.hl.oper.call.proc,
  822. picode->ic.hl.oper.call.args);
  823. ticode->ic.hl.oper.asgn.lhs =
  824. COND_EXPR::idLong(&localId, DST, ticode,
  825. HIGH_FIRST, j, eDEF, 1);
  826. ticode->ic.hl.oper.asgn.rhs = exp;
  827. picode->invalidate();
  828. numHlIcodes--;
  829. break;
  830. case HLI_PUSH: case HLI_RET:
  831. exp = COND_EXPR::idFunc (
  832. picode->ic.hl.oper.call.proc,
  833. picode->ic.hl.oper.call.args);
  834. ticode->ic.hl.oper.exp = exp;
  835. picode->invalidate();
  836. numHlIcodes--;
  837. break;
  838. case HLI_JCOND:
  839. exp = COND_EXPR::idFunc (
  840. picode->ic.hl.oper.call.proc,
  841. picode->ic.hl.oper.call.args);
  842. retVal = &picode->ic.hl.oper.call.proc->retVal;
  843. res = insertSubTreeLongReg (exp,
  844. &ticode->ic.hl.oper.exp,
  845. localId.newLongReg
  846. (
  847. retVal->type, retVal->id.longId.h,
  848. retVal->id.longId.l, j));
  849. if (res) /* was substituted */
  850. {
  851. picode->invalidate();
  852. numHlIcodes--;
  853. }
  854. else /* cannot substitute function */
  855. {
  856. lhs = COND_EXPR::idID(retVal,&localId,j);
  857. picode->setAsgn(lhs, exp);
  858. }
  859. break;
  860. } /* eos */
  861. } /* eos */
  862. }
  863. }
  864. /* HLI_PUSH doesn't define any registers, only uses registers.
  865. * Push the associated expression to the register on the local
  866. * expression stack */
  867. else if (picode->ic.hl.opcode == HLI_PUSH)
  868. {
  869. pushExpStk (picode->ic.hl.oper.exp);
  870. picode->invalidate();
  871. numHlIcodes--;
  872. }
  873. /* For HLI_CALL instructions that use arguments from the stack,
  874. * pop them from the expression stack and place them on the
  875. * procedure's argument list */
  876. if ((picode->ic.hl.opcode == HLI_CALL) &&
  877. ! (picode->ic.hl.oper.call.proc->flg & REG_ARGS))
  878. { Function * pp;
  879. Int cb, numArgs;
  880. boolT res;
  881. pp = picode->ic.hl.oper.call.proc;
  882. if (pp->flg & CALL_PASCAL)
  883. {
  884. cb = pp->cbParam; /* fixed # arguments */
  885. for (k = 0, numArgs = 0; k < cb; numArgs++)
  886. {
  887. exp = popExpStk();
  888. if (pp->flg & PROC_ISLIB) /* library function */
  889. {
  890. if (pp->args.numArgs > 0)
  891. adjustActArgType(exp, pp->args.sym[numArgs].type, this);
  892. res = newStkArg (picode, exp, picode->ic.ll.opcode, this);
  893. }
  894. else /* user function */
  895. {
  896. if (pp->args.numArgs >0)
  897. pp->args.adjustForArgType (numArgs,expType (exp, this));
  898. res = newStkArg (picode, exp,picode->ic.ll.opcode, this);
  899. }
  900. if (res == FALSE)
  901. k += hlTypeSize (exp, this);
  902. }
  903. }
  904. else /* CALL_C */
  905. {
  906. cb = picode->ic.hl.oper.call.args->cb;
  907. numArgs = 0;
  908. if (cb)
  909. for (k = 0; k < cb; numArgs++)
  910. processCArg (pp, this, picode, numArgs, &k);
  911. else if ((cb == 0) && (picode->ic.ll.flg & REST_STK))
  912. while (! emptyExpStk())
  913. {
  914. processCArg (pp, this, picode, numArgs, &k);
  915. numArgs++;
  916. }
  917. }
  918. }
  919. /* If we could not substitute the result of a function,
  920. * assign it to the corresponding registers */
  921. if ((picode->ic.hl.opcode == HLI_CALL) &&
  922. ((picode->ic.hl.oper.call.proc->flg & PROC_ISLIB) !=
  923. PROC_ISLIB) && (picode->du1.idx[0][0] == 0) &&
  924. (picode->du1.numRegsDef > 0))
  925. {
  926. exp = COND_EXPR::idFunc (picode->ic.hl.oper.call.proc,
  927. picode->ic.hl.oper.call.args);
  928. lhs = COND_EXPR::idID (&picode->ic.hl.oper.call.proc->retVal,
  929. &localId, j);
  930. picode->setAsgn(lhs, exp);
  931. }
  932. }
  933. }
  934. /* Store number of high-level icodes in current basic block */
  935. pbb->numHlIcodes = numHlIcodes;
  936. }
  937. }
  938. /* Invokes procedures related with data flow analysis. Works on a procedure
  939. * at a time basis.
  940. * Note: indirect recursion in liveRegAnalysis is possible. */
  941. void Function::dataFlow(dword liveOut)
  942. {
  943. boolT isAx, isBx, isCx, isDx;
  944. Int idx;
  945. /* Remove references to register variables */
  946. if (flg & SI_REGVAR)
  947. liveOut &= maskDuReg[rSI];
  948. if (flg & DI_REGVAR)
  949. liveOut &= maskDuReg[rDI];
  950. /* Function - return value register(s) */
  951. if (liveOut != 0)
  952. {
  953. flg |= PROC_IS_FUNC;
  954. isAx = (boolT)(liveOut & power2(rAX - rAX));
  955. isBx = (boolT)(liveOut & power2(rBX - rAX));
  956. isCx = (boolT)(liveOut & power2(rCX - rAX));
  957. isDx = (boolT)(liveOut & power2(rDX - rAX));
  958. if (isAx && isDx) /* long or pointer */
  959. {
  960. retVal.type = TYPE_LONG_SIGN;
  961. retVal.loc = REG_FRAME;
  962. retVal.id.longId.h = rDX;
  963. retVal.id.longId.l = rAX;
  964. idx = localId.newLongReg(TYPE_LONG_SIGN, rDX, rAX, 0);
  965. localId.propLongId (rAX, rDX, "\0");
  966. }
  967. else if (isAx || isBx || isCx || isDx) /* word */
  968. {
  969. retVal.type = TYPE_WORD_SIGN;
  970. retVal.loc = REG_FRAME;
  971. if (isAx)
  972. retVal.id.regi = rAX;
  973. else if (isBx)
  974. retVal.id.regi = rBX;
  975. else if (isCx)
  976. retVal.id.regi = rCX;
  977. else
  978. retVal.id.regi = rDX;
  979. idx = localId.newByteWordReg(TYPE_WORD_SIGN,retVal.id.regi);
  980. }
  981. }
  982. /* Data flow analysis */
  983. liveAnal = TRUE;
  984. elimCondCodes();
  985. genLiveKtes();
  986. liveRegAnalysis (liveOut); /* calls dataFlow() recursively */
  987. if (! (flg & PROC_ASM)) /* can generate C for pProc */
  988. {
  989. genDU1 (); /* generate def/use level 1 chain */
  990. findExps (); /* forward substitution algorithm */
  991. }
  992. }