axe_reg_alloc.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. /*
  2. * Andrea Di Biagio
  3. * Politecnico di Milano, 2007
  4. *
  5. * axe_reg_alloc.c
  6. * Formal Languages & Compilers Machine, 2007/2008
  7. *
  8. */
  9. #include "axe_reg_alloc.h"
  10. #include "reg_alloc_constants.h"
  11. #include "axe_debug.h"
  12. #include "axe_errors.h"
  13. extern int errorcode;
  14. static int compareIntervalIDs(void *varA, void *varB);
  15. static int compareStartPoints(void *varA, void *varB);
  16. static int compareEndPoints(void *varA, void *varB);
  17. static t_list * updateListOfIntervals(t_list *result
  18. , t_cflow_Node *current_node, int counter);
  19. static t_list * allocFreeRegisters(int regNum);
  20. static t_list * addFreeRegister
  21. (t_list *registers, int regID, int position);
  22. static int assignRegister(t_reg_allocator *RA);
  23. static t_list * expireOldIntervals(t_reg_allocator *RA
  24. , t_list *active_intervals, t_live_interval *interval);
  25. static t_list * getLiveIntervals(t_cflow_Graph *graph);
  26. static int insertListOfIntervals(t_reg_allocator *RA, t_list *intervals);
  27. static int insertLiveInterval(t_reg_allocator *RA, t_live_interval *interval);
  28. static void finalizeLiveInterval (t_live_interval *interval);
  29. static t_live_interval * allocLiveInterval(int varID, int startPoint, int endPoint);
  30. static t_list * spillAtInterval(t_reg_allocator *RA
  31. , t_list *active_intervals, t_live_interval *interval);
  32. /*
  33. * Perform a spill that allows the allocation of the given
  34. * interval, given the list of active live intervals
  35. */
  36. t_list * spillAtInterval(t_reg_allocator *RA
  37. , t_list *active_intervals, t_live_interval *interval)
  38. {
  39. t_list *last_element;
  40. t_live_interval *last_interval;
  41. /* get the last element of the list of active intervals */
  42. /* Precondition: if the list of active intervals is empty
  43. * we are working on a machine with 0 registers available
  44. * for the register allocation */
  45. if (active_intervals == NULL)
  46. {
  47. RA->bindings[interval->varID] = RA_SPILL_REQUIRED;
  48. return active_intervals;
  49. }
  50. last_element = getLastElement(active_intervals);
  51. last_interval = (t_live_interval *) LDATA(last_element);
  52. /* If the current interval ends before the last one, spill
  53. * the last one, otherwise spill the current interval */
  54. if (last_interval->endPoint > interval->endPoint)
  55. {
  56. RA->bindings[interval->varID]
  57. = RA->bindings[last_interval->varID];
  58. RA->bindings[last_interval->varID] = RA_SPILL_REQUIRED;
  59. active_intervals = removeElement(active_intervals, last_interval);
  60. active_intervals = addSorted(active_intervals
  61. , interval, compareEndPoints);
  62. }
  63. else
  64. RA->bindings[interval->varID] = RA_SPILL_REQUIRED;
  65. return active_intervals;
  66. }
  67. /*
  68. * Given two live intervals, compare them by
  69. * the start point (find whichever starts first)
  70. */
  71. int compareStartPoints(void *varA, void *varB)
  72. {
  73. t_live_interval *liA;
  74. t_live_interval *liB;
  75. if (varA == NULL)
  76. return 0;
  77. if (varB == NULL)
  78. return 0;
  79. liA = (t_live_interval *) varA;
  80. liB = (t_live_interval *) varB;
  81. return (liA->startPoint - liB->startPoint);
  82. }
  83. /*
  84. * Given two live intervals, compare them
  85. * by the end point (find whichever ends first)
  86. */
  87. int compareEndPoints(void *varA, void *varB)
  88. {
  89. t_live_interval *liA;
  90. t_live_interval *liB;
  91. if (varA == NULL)
  92. return 0;
  93. if (varB == NULL)
  94. return 0;
  95. liA = (t_live_interval *) varA;
  96. liB = (t_live_interval *) varB;
  97. return (liA->endPoint - liB->endPoint);
  98. }
  99. /*
  100. * Given two live intervals, check if they
  101. * refer to the same interval
  102. */
  103. int compareIntervalIDs(void *varA, void *varB)
  104. {
  105. t_live_interval *liA;
  106. t_live_interval *liB;
  107. if (varA == NULL)
  108. return 0;
  109. if (varB == NULL)
  110. return 0;
  111. liA = (t_live_interval *) varA;
  112. liB = (t_live_interval *) varB;
  113. return (liA->varID == liB->varID);
  114. }
  115. /*
  116. * Insert a live interval in the register allocator
  117. */
  118. int insertLiveInterval(t_reg_allocator *RA, t_live_interval *interval)
  119. {
  120. /* test the preconditions */
  121. if (RA == NULL)
  122. return RA_INVALID_ALLOCATOR;
  123. if (interval == NULL)
  124. return RA_INVALID_INTERVAL;
  125. /* test if an interval for the requested variable is already inserted */
  126. if (CustomfindElement(RA->live_intervals
  127. , interval, compareIntervalIDs) != NULL)
  128. {
  129. return RA_INTERVAL_ALREADY_INSERTED;
  130. }
  131. /* add the given interval to the list of intervals,
  132. * in order of starting point */
  133. RA->live_intervals = addSorted(RA->live_intervals
  134. , interval, compareStartPoints);
  135. return RA_OK;
  136. }
  137. /*
  138. * Allocate and initialize the free registers list,
  139. * assuming regNum general purpose registers
  140. */
  141. t_list * allocFreeRegisters(int regNum)
  142. {
  143. int count;
  144. t_list *result;
  145. /* initialize the local variables */
  146. count = 1;
  147. result = NULL;
  148. while(count <= regNum)
  149. {
  150. /* add a new register to the list of free registers */
  151. result = addFreeRegister(result, count, -1);
  152. /* update the `count' variable */
  153. count ++;
  154. }
  155. /* return the list of free registers */
  156. return result;
  157. }
  158. /*
  159. * Return register regID to the free list in the given position
  160. */
  161. t_list * addFreeRegister(t_list *registers, int regID, int position)
  162. {
  163. int *element;
  164. /* Allocate memory space for the reg id */
  165. element = (int *) _AXE_ALLOC_FUNCTION(sizeof(int));
  166. if (element == NULL)
  167. notifyError(AXE_OUT_OF_MEMORY);
  168. /* initialize element */
  169. (* element) = regID;
  170. /* update the list of registers */
  171. registers = addElement(registers, element, position);
  172. /* return the list of free registers */
  173. return registers;
  174. }
  175. /*
  176. * Get a new register from the free list
  177. */
  178. int assignRegister(t_reg_allocator *RA)
  179. {
  180. int regID;
  181. /* Check for list not empty*/
  182. if (RA->freeRegisters == NULL)
  183. return RA_SPILL_REQUIRED;
  184. /* Get the name of the first free register */
  185. regID = (* ((int *) LDATA(RA->freeRegisters)));
  186. /* Free the space allocated to hold the register id */
  187. _AXE_FREE_FUNCTION(LDATA(RA->freeRegisters));
  188. /* Remove the structure from the free registers list */
  189. RA->freeRegisters = removeFirst(RA->freeRegisters);
  190. return regID;
  191. }
  192. /*
  193. * Allocate and initialize the register allocator
  194. */
  195. t_reg_allocator * initializeRegAlloc(t_cflow_Graph *graph)
  196. {
  197. t_reg_allocator *result; /* the register allocator */
  198. t_list *intervals;
  199. t_list *current_elem;
  200. t_list *current_cflow_var;
  201. t_cflow_var *cflow_var;
  202. int max_var_ID;
  203. int counter;
  204. /* Check preconditions: the cfg must exist */
  205. if (graph == NULL)
  206. notifyError(AXE_INVALID_CFLOW_GRAPH);
  207. /* allocate memory for a new instance of `t_reg_allocator' */
  208. result = (t_reg_allocator *) _AXE_ALLOC_FUNCTION(sizeof(t_reg_allocator));
  209. if (result == NULL)
  210. notifyError(AXE_OUT_OF_MEMORY);
  211. /* initialize the register allocator informations */
  212. /* Reserve a few registers (RA_MIN_REG_NUM) to handle spills */
  213. result->regNum = NUM_REGISTERS - RA_MIN_REG_NUM;
  214. max_var_ID = getLength(graph->cflow_variables);
  215. /* retrieve the max identifier from each live interval */
  216. current_cflow_var = graph->cflow_variables;
  217. while (current_cflow_var != NULL)
  218. {
  219. /* fetch the data informations about a variable */
  220. cflow_var = (t_cflow_var *) LDATA(current_cflow_var);
  221. assert(cflow_var != NULL);
  222. /* update the value of max_var_ID */
  223. max_var_ID = (max_var_ID < cflow_var->ID)? cflow_var->ID : max_var_ID;
  224. /* retrieve the next variable */
  225. current_cflow_var = LNEXT(current_cflow_var);
  226. }
  227. /* update the value of `result->varNum' with the correct var. identifier */
  228. result->varNum = max_var_ID;
  229. current_elem = graph->cflow_variables;
  230. /* Assuming there are some variables to associate to regs,
  231. * allocate space for the binding array, and initialize it */
  232. /*alloc memory for the array of bindings */
  233. result->bindings = (int *) _AXE_ALLOC_FUNCTION
  234. (sizeof(int) * (result->varNum + 1) );
  235. /* test if an error occurred */
  236. if (result->bindings == NULL)
  237. notifyError(AXE_OUT_OF_MEMORY);
  238. /* initialize the array of bindings */
  239. for(counter = 0; counter <= result->varNum; counter++)
  240. result->bindings[counter] = RA_REGISTER_INVALID;
  241. /* Liveness analysis: compute the list of live intervals */
  242. result->live_intervals = NULL;
  243. intervals = getLiveIntervals(graph);
  244. /* Copy the liveness info into the register allocator */
  245. if (intervals != NULL)
  246. {
  247. if (insertListOfIntervals(result, intervals) != RA_OK)
  248. {
  249. finalizeRegAlloc(result);
  250. notifyError(AXE_REG_ALLOC_ERROR);
  251. }
  252. /* deallocate memory used to hold the results of the
  253. * liveness analysis */
  254. freeList(intervals);
  255. }
  256. /* create a list of freeRegisters */
  257. if (result->regNum > 0)
  258. result->freeRegisters = allocFreeRegisters(result->regNum);
  259. else
  260. result->freeRegisters = NULL;
  261. /* return the new register allocator */
  262. return result;
  263. }
  264. /*
  265. * Deallocate the register allocator data structures
  266. */
  267. void finalizeRegAlloc(t_reg_allocator *RA)
  268. {
  269. if (RA == NULL)
  270. return;
  271. /* If the list of live intervals is not empty,
  272. * deallocate its content */
  273. if (RA->live_intervals != NULL)
  274. {
  275. t_list *current_element;
  276. t_live_interval *current_interval;
  277. /* finalize the memory blocks associated with all
  278. * the live intervals */
  279. for (current_element = RA->live_intervals
  280. ; current_element != NULL
  281. ; current_element = LNEXT(current_element))
  282. {
  283. /* fetch the current interval */
  284. current_interval = (t_live_interval *) LDATA(current_element);
  285. if (current_interval != NULL)
  286. {
  287. /* finalize the memory block associated with
  288. * the current interval */
  289. finalizeLiveInterval(current_interval);
  290. }
  291. }
  292. /* deallocate the list of intervals */
  293. freeList(RA->live_intervals);
  294. }
  295. /* Free memory used for the variable/register bindings */
  296. if (RA->bindings != NULL)
  297. _AXE_FREE_FUNCTION(RA->bindings);
  298. if (RA->freeRegisters != NULL)
  299. {
  300. t_list *current_element;
  301. current_element = RA->freeRegisters;
  302. while (current_element != NULL)
  303. {
  304. _AXE_FREE_FUNCTION(LDATA(current_element));
  305. current_element = LNEXT(current_element);
  306. }
  307. freeList(RA->freeRegisters);
  308. }
  309. _AXE_FREE_FUNCTION(RA);
  310. }
  311. /*
  312. * Allocate and initialize a live interval data structure
  313. * with a given varID, starting and ending points
  314. */
  315. t_live_interval * allocLiveInterval
  316. (int varID, int startPoint, int endPoint)
  317. {
  318. t_live_interval *result;
  319. /* create a new instance of `t_live_interval' */
  320. result = _AXE_ALLOC_FUNCTION(sizeof(t_live_interval));
  321. if (result == NULL)
  322. notifyError(AXE_OUT_OF_MEMORY);
  323. /* initialize the new instance */
  324. result->varID = varID;
  325. result->startPoint = startPoint;
  326. result->endPoint = endPoint;
  327. /* return the new `t_live_interval' */
  328. return result;
  329. }
  330. /*
  331. * Deallocate a live interval
  332. */
  333. void finalizeLiveInterval (t_live_interval *interval)
  334. {
  335. if (interval == NULL)
  336. return;
  337. /* finalize the current interval */
  338. _AXE_FREE_FUNCTION(interval);
  339. }
  340. /*
  341. * Insert all elements of the intervals list into
  342. * the register allocator data structure
  343. */
  344. int insertListOfIntervals(t_reg_allocator *RA, t_list *intervals)
  345. {
  346. t_list *current_element;
  347. t_live_interval *interval;
  348. int ra_errorcode;
  349. /* preconditions */
  350. if (RA == NULL)
  351. return RA_INVALID_ALLOCATOR;
  352. if (intervals == NULL)
  353. return RA_OK;
  354. for (current_element = intervals
  355. ; current_element != NULL
  356. ; current_element = LNEXT(current_element) )
  357. {
  358. /* Get the current live interval */
  359. interval = (t_live_interval *) LDATA(current_element);
  360. if (interval == NULL)
  361. return RA_INVALID_INTERVAL;
  362. /* insert a new live interval */
  363. ra_errorcode = insertLiveInterval(RA, interval);
  364. /* test if an error occurred */
  365. if (ra_errorcode != RA_OK)
  366. return ra_errorcode;
  367. }
  368. return RA_OK;
  369. }
  370. /*
  371. * Perform live intervals computation
  372. */
  373. t_list * getLiveIntervals(t_cflow_Graph *graph)
  374. {
  375. t_list *current_bb_element;
  376. t_list *current_nd_element;
  377. t_basic_block *current_block;
  378. t_cflow_Node *current_node;
  379. t_list *result;
  380. int counter;
  381. /* preconditions */
  382. if (graph == NULL)
  383. return NULL;
  384. if (graph->blocks == NULL)
  385. return NULL;
  386. /* initialize the local variable `result' */
  387. result = NULL;
  388. /* intialize the instruction counter */
  389. counter = 0;
  390. /* fetch the first basic block */
  391. current_bb_element = graph->blocks;
  392. while (current_bb_element != NULL)
  393. {
  394. current_block = (t_basic_block *) LDATA(current_bb_element);
  395. /* fetch the first node of the basic block */
  396. current_nd_element = current_block->nodes;
  397. while(current_nd_element != NULL)
  398. {
  399. current_node = (t_cflow_Node *) LDATA(current_nd_element);
  400. /* update the live intervals with the liveness informations */
  401. result = updateListOfIntervals(result, current_node, counter);
  402. /* fetch the next node in the basic block */
  403. counter++;
  404. current_nd_element = LNEXT(current_nd_element);
  405. }
  406. /* fetch the next element in the list of basic blocks */
  407. current_bb_element = LNEXT(current_bb_element);
  408. }
  409. return result;
  410. }
  411. /*
  412. * Update the liveness interval for the variable 'id', used or defined
  413. * at position 'counter'.
  414. */
  415. t_list *updateVarInterval( int id, int counter, t_list *intervals )
  416. {
  417. t_list *element_found;
  418. t_live_interval *interval_found;
  419. t_live_interval pattern;
  420. if (id == RA_EXCLUDED_VARIABLE)
  421. return;
  422. pattern.varID = id;
  423. /* search for the current live interval */
  424. element_found = CustomfindElement(intervals, &pattern, compareIntervalIDs);
  425. if (element_found != NULL)
  426. {
  427. interval_found = (t_live_interval *) LDATA(element_found);
  428. /* update the interval informations */
  429. if (interval_found->startPoint > counter)
  430. interval_found->startPoint = counter;
  431. if (interval_found->endPoint < counter)
  432. interval_found->endPoint = counter;
  433. }
  434. else
  435. {
  436. /* we have to add a new live interval */
  437. interval_found = allocLiveInterval(id, counter, counter);
  438. if (interval_found == NULL)
  439. notifyError(AXE_OUT_OF_MEMORY);
  440. intervals = addElement(intervals, interval_found, -1);
  441. }
  442. return intervals;
  443. }
  444. /*
  445. * Use liveness information to update the list of
  446. * live intervals
  447. */
  448. t_list * updateListOfIntervals(t_list *result
  449. , t_cflow_Node *current_node, int counter)
  450. {
  451. t_list *current_element;
  452. t_cflow_var *current_var;
  453. if (current_node == NULL)
  454. return result;
  455. current_element = current_node->in;
  456. while (current_element != NULL)
  457. {
  458. current_var = (t_cflow_var *) LDATA(current_element);
  459. result = updateVarInterval(current_var->ID, counter, result);
  460. /* fetch the next element in the list of live variables */
  461. current_element = LNEXT(current_element);
  462. }
  463. current_element = current_node->out;
  464. while (current_element != NULL)
  465. {
  466. current_var = (t_cflow_var *) LDATA(current_element);
  467. result = updateVarInterval(current_var->ID, counter, result);
  468. /* fetch the next element in the list of live variables */
  469. current_element = LNEXT(current_element);
  470. }
  471. if (current_node->def)
  472. result = updateVarInterval(current_node->def->ID, counter, result);
  473. return result;
  474. }
  475. /*
  476. * Remove from active_intervals all the live intervals that end before the
  477. * beginning of the current live interval
  478. */
  479. t_list * expireOldIntervals(t_reg_allocator *RA, t_list *active_intervals
  480. , t_live_interval *interval)
  481. {
  482. t_list *current_element;
  483. t_list *next_element;
  484. t_live_interval *current_interval;
  485. /* Check for valid register allocator and set of active intervals */
  486. if (active_intervals == NULL)
  487. return NULL;
  488. if (RA == NULL)
  489. return NULL;
  490. if (interval == NULL)
  491. return active_intervals;
  492. /* Iterate over the set of active intervals */
  493. current_element = active_intervals;
  494. while(current_element != NULL)
  495. {
  496. /* Get the live interval */
  497. current_interval = (t_live_interval *) LDATA(current_element);
  498. /* If the considered interval ends before the beginning of
  499. * the current live interval, we don't need to keep track of
  500. * it anymore; otherwise, this is the first interval we must
  501. * still take into account when assigning registers */
  502. if (current_interval->endPoint >= interval->startPoint)
  503. return active_intervals;
  504. /* Get the next live interval */
  505. next_element = LNEXT(current_element);
  506. /* Remove the current element from the list */
  507. active_intervals = removeElement(active_intervals, current_interval);
  508. /* Free all the registers associated with the removed interval */
  509. RA->freeRegisters = addFreeRegister
  510. (RA->freeRegisters, RA->bindings[current_interval->varID], 0);
  511. /* Step to the next interval */
  512. current_element = next_element;
  513. }
  514. /* Return the updated list of active intervals */
  515. return active_intervals;
  516. }
  517. /*
  518. * Main register allocation function
  519. */
  520. int execute_linear_scan(t_reg_allocator *RA)
  521. {
  522. t_list *current_element;
  523. t_live_interval *current_interval;
  524. t_list *active_intervals;
  525. /* test the preconditions */
  526. if (RA == NULL) /* Register allocator created? */
  527. return RA_INVALID_ALLOCATOR;
  528. if (RA->live_intervals == NULL) /* Liveness analysis ready? */
  529. return RA_OK;
  530. /* initialize the list of active intervals */
  531. active_intervals = NULL;
  532. /* Iterate over the list of live intervals */
  533. for( current_element = RA->live_intervals
  534. ; current_element != NULL
  535. ; current_element = LNEXT(current_element) )
  536. {
  537. /* Get the live interval */
  538. current_interval = (t_live_interval *) LDATA(current_element);
  539. /* Check which intervals are ended and remove
  540. * them from the active set, thus freeing registers */
  541. active_intervals = expireOldIntervals
  542. (RA, active_intervals, current_interval);
  543. /* If all registers are busy, perform a spill */
  544. if (getLength(active_intervals) == RA->regNum)
  545. {
  546. /* perform a spill */
  547. active_intervals = spillAtInterval
  548. (RA, active_intervals, current_interval);
  549. }
  550. else /* Otherwise, assign a new register to the current live interval */
  551. {
  552. RA->bindings[current_interval->varID] = assignRegister(RA);
  553. /* Add the current interval to the list of active intervals, in
  554. * order of ending points (to allow easier expire management) */
  555. active_intervals = addSorted(active_intervals
  556. , current_interval, compareEndPoints);
  557. }
  558. }
  559. /* free the list of active intervals */
  560. freeList(active_intervals);
  561. return RA_OK;
  562. }