[WebAssembly] Don't use range-based loop for a list that's being modified
authorDan Gohman <dan433584@gmail.com>
Wed, 6 Jan 2016 18:29:35 +0000 (18:29 +0000)
committerDan Gohman <dan433584@gmail.com>
Wed, 6 Jan 2016 18:29:35 +0000 (18:29 +0000)
The first instruction in a block is what the rend() iterator points to, so
if it moves, we need to re-evaluate rend() so that we continue to iterate
through the rest of the instructions.

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

lib/Target/WebAssembly/WebAssemblyRegStackify.cpp
test/CodeGen/WebAssembly/cfg-stackify.ll

index 89ef5cdb2bef5a98d60c7e819abdae6f38f8b979..537c147e61421a19108ac8de8534408205a1f312 100644 (file)
@@ -147,8 +147,10 @@ bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
   // block boundaries, and the blocks aren't ordered so the block visitation
   // order isn't significant, but we may want to change this in the future.
   for (MachineBasicBlock &MBB : MF) {
-    for (MachineInstr &MI : reverse(MBB)) {
-      MachineInstr *Insert = &MI;
+    // Don't use a range-based for loop, because we modify the list as we're
+    // iterating over it and the end iterator may change.
+    for (auto MII = MBB.rbegin(); MII != MBB.rend(); ++MII) {
+      MachineInstr *Insert = &*MII;
       // Don't nest anything inside a phi.
       if (Insert->getOpcode() == TargetOpcode::PHI)
         break;
@@ -221,7 +223,7 @@ bool WebAssemblyRegStackify::runOnMachineFunction(MachineFunction &MF) {
         Insert = Def;
       }
       if (AnyStackified)
-        ImposeStackOrdering(&MI);
+        ImposeStackOrdering(&*MII);
     }
   }
 
index 71f3551347bf5b1c5213bbb005146369421f0056..d0250dc29e0fa6c140a2234cc807075a76f9d02c 100644 (file)
@@ -93,6 +93,7 @@ back:
 ; Test that a simple loop is handled as expected.
 
 ; CHECK-LABEL: test2:
+; CHECK-NOT: local
 ; CHECK: block BB2_2{{$}}
 ; CHECK: br_if {{[^,]*}}, BB2_2{{$}}
 ; CHECK: BB2_1: