[WebAssembly] Fix WebAssembly register numbering for registers added late.
authorDan Gohman <dan433584@gmail.com>
Wed, 25 Nov 2015 21:13:02 +0000 (21:13 +0000)
committerDan Gohman <dan433584@gmail.com>
Wed, 25 Nov 2015 21:13:02 +0000 (21:13 +0000)
If virtual registers are created late, mappings to WebAssembly
registers need to be added explicitly. This patch adds a function
to do so and teaches WebAssemblyPeephole to use it. This fixes
an out-of-bounds access on the WARegs vector.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@254094 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Target/WebAssembly/WebAssemblyMachineFunctionInfo.h
lib/Target/WebAssembly/WebAssemblyPeephole.cpp

index 9c23412..4760f0d 100644 (file)
@@ -63,11 +63,19 @@ public:
   void initWARegs();
   void setWAReg(unsigned VReg, unsigned WAReg) {
     assert(WAReg != UnusedReg);
+    assert(TargetRegisterInfo::virtReg2Index(VReg) < WARegs.size());
     WARegs[TargetRegisterInfo::virtReg2Index(VReg)] = WAReg;
   }
   unsigned getWAReg(unsigned VReg) const {
+    assert(TargetRegisterInfo::virtReg2Index(VReg) < WARegs.size());
     return WARegs[TargetRegisterInfo::virtReg2Index(VReg)];
   }
+  // If new virtual registers are created after initWARegs has been called,
+  // this function can be used to add WebAssembly register mappings for them.
+  void addWAReg(unsigned VReg, unsigned WAReg) {
+    assert(VReg = WARegs.size());
+    WARegs.push_back(WAReg);
+  }
 };
 
 } // end namespace llvm
index 1399562..e149d9c 100644 (file)
@@ -69,6 +69,7 @@ bool WebAssemblyPeephole::runOnMachineFunction(MachineFunction &MF) {
           MO.setReg(NewReg);
           MO.setIsDead();
           MFI.stackifyVReg(NewReg);
+          MFI.addWAReg(NewReg, WebAssemblyFunctionInfo::UnusedReg);
         }
       }
       }