[pypy-svn] pypy default: Find some middle-ground solution, where we can still pass a Const

arigo commits-noreply at bitbucket.org
Fri Jan 14 15:06:12 CET 2011


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r40669:55b51d9c462b
Date: 2011-01-14 11:21 +0100
http://bitbucket.org/pypy/pypy/changeset/55b51d9c462b/

Log:	Find some middle-ground solution, where we can still pass a Const
	here and there, where the right thing can occur.

diff --git a/pypy/jit/backend/llsupport/regalloc.py b/pypy/jit/backend/llsupport/regalloc.py
--- a/pypy/jit/backend/llsupport/regalloc.py
+++ b/pypy/jit/backend/llsupport/regalloc.py
@@ -210,12 +210,22 @@
         except KeyError:
             return self.frame_manager.loc(box)
 
-    def return_constant(self, v):
-        """ Return the location of the constant v.
+    def return_constant(self, v, selected_reg=None):
+        """ Return the location of the constant v.  If 'selected_reg' is
+        not None, it will first load its value into this register.
         """
         self._check_type(v)
         assert isinstance(v, Const)
-        return self.convert_to_imm(v)
+        immloc = self.convert_to_imm(v)
+        if selected_reg:
+            if selected_reg in self.free_regs:
+                self.assembler.regalloc_mov(immloc, selected_reg)
+                return selected_reg
+            loc = self._spill_var(v, forbidden_vars, selected_reg)
+            self.free_regs.append(loc)
+            self.assembler.regalloc_mov(immloc, loc)
+            return loc
+        return immloc
 
     def make_sure_var_in_reg(self, v, forbidden_vars=[], selected_reg=None,
                              need_lower_byte=False):
@@ -225,10 +235,7 @@
         """
         self._check_type(v)
         if isinstance(v, Const):
-            assert selected_reg is None, (
-                "make_sure_var_in_reg(): selected_reg can only be "
-                "specified for Boxes, not Consts")
-            return self.return_constant(v)
+            return self.return_constant(v, selected_reg)
         
         prev_loc = self.loc(v)
         loc = self.force_allocate_reg(v, forbidden_vars, selected_reg,
@@ -259,7 +266,14 @@
         """
         self._check_type(result_v)
         self._check_type(v)
-        assert isinstance(v, Box), "force_result_in_reg(): got a Const"
+        if isinstance(v, Const):
+            if self.free_regs:
+                loc = self.free_regs.pop()
+            else:
+                loc = self._spill_var(v, forbidden_vars)
+            self.assembler.regalloc_mov(self.convert_to_imm(v), loc)
+            self.reg_bindings[result_v] = loc
+            return loc
         if v not in self.reg_bindings:
             prev_loc = self.frame_manager.loc(v)
             loc = self.force_allocate_reg(v, forbidden_vars)

diff --git a/pypy/jit/backend/llsupport/test/test_regalloc.py b/pypy/jit/backend/llsupport/test/test_regalloc.py
--- a/pypy/jit/backend/llsupport/test/test_regalloc.py
+++ b/pypy/jit/backend/llsupport/test/test_regalloc.py
@@ -234,21 +234,17 @@
         rm = RegisterManager(longevity, assembler=asm,
                              frame_manager=fm)
         rm.next_instruction()
-        loc = rm.return_constant(ConstInt(0), imm_fine=False)
-        assert isinstance(loc, FakeReg)
         loc = rm.return_constant(ConstInt(1), selected_reg=r1)
         assert loc is r1
         loc = rm.return_constant(ConstInt(1), selected_reg=r1)
         assert loc is r1
-        loc = rm.return_constant(ConstInt(1), imm_fine=True)
+        loc = rm.return_constant(ConstInt(1))
         assert isinstance(loc, ConstInt)
         for box in boxes[:-1]:
             rm.force_allocate_reg(box)
-        assert len(asm.moves) == 3
-        loc = rm.return_constant(ConstInt(1), imm_fine=False)
-        assert isinstance(loc, FakeReg)
-        assert len(asm.moves) == 5
-        assert len(rm.reg_bindings) == 3
+        assert len(asm.moves) == 2       # Const(1) -> r1, twice
+        assert len(rm.reg_bindings) == 4
+        rm._check_invariants()
 
     def test_force_result_in_reg_const(self):
         boxes, longevity = boxes_and_longevity(2)


More information about the Pypy-commit mailing list