Fix PR2596: out of bound reference.

llvm-svn: 54375
diff --git a/llvm/lib/CodeGen/VirtRegMap.cpp b/llvm/lib/CodeGen/VirtRegMap.cpp
index b9e96b1..c06ff71 100644
--- a/llvm/lib/CodeGen/VirtRegMap.cpp
+++ b/llvm/lib/CodeGen/VirtRegMap.cpp
@@ -560,8 +560,11 @@
     if (!MO.isRegister() || !MO.isUse() || !MO.isKill())
       continue;
     unsigned Reg = MO.getReg();
+    if (TargetRegisterInfo::isVirtualRegister(Reg))
+      continue;
     if (KillRegs)
       KillRegs->push_back(Reg);
+    assert(Reg < KillOps.size());
     if (KillOps[Reg] == &MO) {
       RegKills.reset(Reg);
       KillOps[Reg] = NULL;
@@ -943,9 +946,11 @@
         return false;
       continue;
     }
-    PhysReg = VRM.getPhys(VirtReg);
-    if (!TRI->regsOverlap(PhysReg, UnfoldPR))
-      continue;
+    if (VRM.hasPhys(VirtReg)) {
+      PhysReg = VRM.getPhys(VirtReg);
+      if (!TRI->regsOverlap(PhysReg, UnfoldPR))
+        continue;
+    }
 
     // Ok, we'll need to reload the value into a register which makes
     // it impossible to perform the store unfolding optimization later.