/js/src/nanojit/RegAlloc.h

http://github.com/zpao/v8monkey · C Header · 205 lines · 86 code · 23 blank · 96 comment · 7 complexity · 597112439028fe52b88dd2890359ffe4 MD5 · raw file

  1. /* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
  2. /* vi: set ts=4 sw=4 expandtab: (add to ~/.vimrc: set modeline modelines=5) */
  3. /* ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is [Open Source Virtual Machine].
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Adobe System Incorporated.
  20. * Portions created by the Initial Developer are Copyright (C) 2004-2007
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Adobe AS3 Team
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either the GNU General Public License Version 2 or later (the "GPL"), or
  28. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. #ifndef __nanojit_RegAlloc__
  40. #define __nanojit_RegAlloc__
  41. namespace nanojit
  42. {
  43. class RegAlloc
  44. {
  45. public:
  46. RegAlloc()
  47. {
  48. clear();
  49. }
  50. void clear()
  51. {
  52. VMPI_memset(this, 0, sizeof(*this));
  53. }
  54. bool isFree(Register r) const
  55. {
  56. return (free & rmask(r)) != 0;
  57. }
  58. void addFree(Register r)
  59. {
  60. NanoAssert(!isFree(r));
  61. free |= rmask(r);
  62. }
  63. void removeFree(Register r)
  64. {
  65. NanoAssert(isFree(r));
  66. free &= ~rmask(r);
  67. }
  68. void addActive(Register r, LIns* v)
  69. {
  70. // Count++;
  71. NanoAssert(v);
  72. NanoAssert(active[REGNUM(r)] == NULL);
  73. active[REGNUM(r)] = v;
  74. useActive(r);
  75. }
  76. void useActive(Register r)
  77. {
  78. NanoAssert(active[REGNUM(r)] != NULL);
  79. usepri[REGNUM(r)] = priority++;
  80. }
  81. void removeActive(Register r)
  82. {
  83. //registerReleaseCount++;
  84. NanoAssert(active[REGNUM(r)] != NULL);
  85. // remove the given register from the active list
  86. active[REGNUM(r)] = NULL;
  87. }
  88. void retire(Register r)
  89. {
  90. NanoAssert(active[REGNUM(r)] != NULL);
  91. active[REGNUM(r)] = NULL;
  92. free |= rmask(r);
  93. }
  94. int32_t getPriority(Register r) {
  95. NanoAssert(active[REGNUM(r)]);
  96. return usepri[REGNUM(r)];
  97. }
  98. LIns* getActive(Register r) const {
  99. return active[REGNUM(r)];
  100. }
  101. // Return a mask containing the active registers. For each register
  102. // in this set, getActive(register) will be a nonzero LIns pointer.
  103. RegisterMask activeMask() const {
  104. return ~free & managed;
  105. }
  106. debug_only( bool isConsistent(Register r, LIns* v) const; )
  107. // Some basics:
  108. //
  109. // - 'active' indicates which registers are active at a particular
  110. // point, and for each active register, which instruction
  111. // defines the value it holds. At the start of register
  112. // allocation no registers are active.
  113. //
  114. // - 'free' indicates which registers are free at a particular point
  115. // and thus available for use. At the start of register
  116. // allocation most registers are free; those that are not
  117. // aren't available for general use, e.g. the stack pointer and
  118. // frame pointer registers.
  119. //
  120. // - 'managed' is exactly this list of initially free registers,
  121. // ie. the registers managed by the register allocator.
  122. //
  123. // - Each LIns has a "reservation" which includes a register value,
  124. // 'reg'. Combined with 'active', this provides a two-way
  125. // mapping between registers and LIR instructions.
  126. //
  127. // - Invariant 1: each register must be in exactly one of the
  128. // following states at all times: unmanaged, free, or active.
  129. // In terms of the relevant fields:
  130. //
  131. // * A register in 'managed' must be in 'active' or 'free' but
  132. // not both.
  133. //
  134. // * A register not in 'managed' must be in neither 'active' nor
  135. // 'free'.
  136. //
  137. // - Invariant 2: the two-way mapping between active registers and
  138. // their defining instructions must always hold in both
  139. // directions and be unambiguous. More specifically:
  140. //
  141. // * An LIns can appear at most once in 'active'.
  142. //
  143. // * An LIns named by 'active[R]' must have an in-use
  144. // reservation that names R.
  145. //
  146. // * And vice versa: an LIns with an in-use reservation that
  147. // names R must be named by 'active[R]'.
  148. //
  149. // * If an LIns's reservation names 'deprecated_UnknownReg' then LIns
  150. // should not be in 'active'.
  151. //
  152. LIns* active[LastRegNum + 1]; // active[REGNUM(r)] = LIns that defines r
  153. int32_t usepri[LastRegNum + 1]; // used priority. lower = more likely to spill.
  154. RegisterMask free; // Registers currently free.
  155. RegisterMask managed; // Registers under management (invariant).
  156. int32_t priority;
  157. DECLARE_PLATFORM_REGALLOC()
  158. };
  159. // Return the lowest numbered Register in mask.
  160. inline Register lsReg(RegisterMask mask) {
  161. // This is faster than it looks; we rely on the C++ optimizer
  162. // to strip the dead branch and inline just one alternative.
  163. Register r = { (sizeof(RegisterMask) == 4) ? lsbSet32(mask) : lsbSet64(mask) };
  164. return r;
  165. }
  166. // Return the highest numbered Register in mask.
  167. inline Register msReg(RegisterMask mask) {
  168. // This is faster than it looks; we rely on the C++ optimizer
  169. // to strip the dead branch and inline just one alternative.
  170. Register r = { (sizeof(RegisterMask) == 4) ? msbSet32(mask) : msbSet64(mask) };
  171. return r;
  172. }
  173. // Clear bit r in mask, then return lsReg(mask).
  174. inline Register nextLsReg(RegisterMask& mask, Register r) {
  175. return lsReg(mask &= ~rmask(r));
  176. }
  177. // Clear bit r in mask, then return msReg(mask).
  178. inline Register nextMsReg(RegisterMask& mask, Register r) {
  179. return msReg(mask &= ~rmask(r));
  180. }
  181. }
  182. #endif // __nanojit_RegAlloc__