dataflow.cpp 45 KB

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