Disambiguate between const and nonconst ForEachSuccessorLabel
authorDavid Neto <dneto@ad.corp.google.com>
Fri, 2 Feb 2018 22:17:42 +0000 (14:17 -0800)
committerDavid Neto <dneto@google.com>
Fri, 2 Feb 2018 22:54:40 +0000 (17:54 -0500)
This helps VisualStudio 2013 compile the code.

Contributes to #1262

source/opt/cfg.cpp
source/opt/cfg.h
source/opt/common_uniform_elim_pass.cpp
source/opt/dead_branch_elim_pass.cpp
source/opt/dominator_tree.cpp
source/opt/inline_pass.cpp
source/opt/local_single_store_elim_pass.cpp
source/opt/loop_descriptor.cpp
source/opt/mem_pass.cpp
source/opt/propagator.cpp

index 9834256..c2dd235 100644 (file)
@@ -40,10 +40,20 @@ CFG::CFG(ir::Module* module)
   }
 }
 
+void CFG::AddEdges(ir::BasicBlock* blk) {
+  uint32_t blk_id = blk->id();
+  // Force the creation of an entry, not all basic block have predecessors
+  // (such as the entry blocks and some unreachables).
+  label2preds_[blk_id];
+  const auto* const_blk = blk;
+  const_blk->ForEachSuccessorLabel(
+      [blk_id, this](const uint32_t succ_id) { AddEdge(blk_id, succ_id); });
+}
+
 void CFG::RemoveNonExistingEdges(uint32_t blk_id) {
   std::vector<uint32_t> updated_pred_list;
   for (uint32_t id : preds(blk_id)) {
-    ir::BasicBlock* pred_blk = block(id);
+    const ir::BasicBlock* pred_blk = block(id);
     bool has_branch = false;
     pred_blk->ForEachSuccessorLabel([&has_branch, blk_id](uint32_t succ) {
       if (succ == blk_id) has_branch = true;
@@ -94,7 +104,8 @@ void CFG::ComputeStructuredSuccessors(ir::Function* func) {
     }
 
     // Add true successors.
-    blk.ForEachSuccessorLabel([&blk, this](uint32_t sbid) {
+    const auto& const_blk = blk;
+    const_blk.ForEachSuccessorLabel([&blk, this](const uint32_t sbid) {
       block2structured_succs_[&blk].push_back(id2block_[sbid]);
     });
   }
index 0680e39..138aa0a 100644 (file)
@@ -77,14 +77,7 @@ class CFG {
   }
 
   // Registers |blk| to all of its successors.
-  void AddEdges(ir::BasicBlock* blk) {
-    uint32_t blk_id = blk->id();
-    // Force the creation of an entry, not all basic block have predecessors
-    // (such as the entry blocks and some unreachables).
-    label2preds_[blk_id];
-    blk->ForEachSuccessorLabel(
-        [blk_id, this](uint32_t succ_id) { AddEdge(blk_id, succ_id); });
-  }
+  void AddEdges(ir::BasicBlock* blk);
 
   // Registers the basic block id |pred_blk_id| as being a predecessor of the
   // basic block id |succ_blk_id|.
index 92c5696..bf80da2 100644 (file)
@@ -307,7 +307,8 @@ void CommonUniformElimPass::ComputeStructuredSuccessors(ir::Function* func) {
       }
     }
     // add true successors
-    blk.ForEachSuccessorLabel([&blk, this](uint32_t sbid) {
+    const auto& const_blk = blk;
+    const_blk.ForEachSuccessorLabel([&blk, this](const uint32_t sbid) {
       block2structured_succs_[&blk].push_back(cfg()->block(sbid));
     });
   }
index 11e34c4..be2f3be 100644 (file)
@@ -159,7 +159,8 @@ bool DeadBranchElimPass::MarkLiveBlocks(
       stack.push_back(GetParentBlock(live_lab_id));
     } else {
       // All successors are live.
-      block->ForEachSuccessorLabel([&stack, this](const uint32_t label) {
+      const auto* const_block = block;
+      const_block->ForEachSuccessorLabel([&stack, this](const uint32_t label) {
         stack.push_back(GetParentBlock(label));
       });
     }
index 552a80d..c22d743 100644 (file)
@@ -180,7 +180,8 @@ void BasicBlockSuccessorHelper<BBType>::CreateSuccessorMap(
     for (BasicBlock& bb : f) {
       if (bb.hasSuccessor()) {
         BasicBlockListTy& pred_list = predecessors_[&bb];
-        bb.ForEachSuccessorLabel(
+        const auto& const_bb = bb;
+        const_bb.ForEachSuccessorLabel(
             [this, &pred_list, &bb,
              &GetSuccessorBasicBlock](const uint32_t successor_id) {
               BasicBlock* succ = GetSuccessorBasicBlock(successor_id);
@@ -203,7 +204,8 @@ void BasicBlockSuccessorHelper<BBType>::CreateSuccessorMap(
     for (BasicBlock& bb : f) {
       BasicBlockListTy& succ_list = successors_[&bb];
 
-      bb.ForEachSuccessorLabel([&](const uint32_t successor_id) {
+      const auto& const_bb = bb;
+      const_bb.ForEachSuccessorLabel([&](const uint32_t successor_id) {
         BasicBlock* succ = GetSuccessorBasicBlock(successor_id);
         succ_list.push_back(succ);
         predecessors_[succ].push_back(&bb);
index 8e4808d..6226a96 100644 (file)
@@ -523,14 +523,16 @@ void InlinePass::UpdateSucceedingPhis(
   const auto lastBlk = new_blocks.end() - 1;
   const uint32_t firstId = (*firstBlk)->id();
   const uint32_t lastId = (*lastBlk)->id();
-  (*lastBlk)->ForEachSuccessorLabel([&firstId, &lastId, this](uint32_t succ) {
-    ir::BasicBlock* sbp = this->id2block_[succ];
-    sbp->ForEachPhiInst([&firstId, &lastId](ir::Instruction* phi) {
-      phi->ForEachInId([&firstId, &lastId](uint32_t* id) {
-        if (*id == firstId) *id = lastId;
+  const ir::BasicBlock& const_last_block = *lastBlk->get();
+  const_last_block.ForEachSuccessorLabel(
+      [&firstId, &lastId, this](const uint32_t succ) {
+        ir::BasicBlock* sbp = this->id2block_[succ];
+        sbp->ForEachPhiInst([&firstId, &lastId](ir::Instruction* phi) {
+          phi->ForEachInId([&firstId, &lastId](uint32_t* id) {
+            if (*id == firstId) *id = lastId;
+          });
+        });
       });
-    });
-  });
 }
 
 bool InlinePass::HasMultipleReturns(ir::Function* func) {
@@ -560,7 +562,8 @@ void InlinePass::ComputeStructuredSuccessors(ir::Function* func) {
     }
 
     // Add true successors.
-    blk.ForEachSuccessorLabel([&blk, this](uint32_t sbid) {
+    const auto& const_blk = blk;
+    const_blk.ForEachSuccessorLabel([&blk, this](const uint32_t sbid) {
       block2structured_succs_[&blk].push_back(id2block_[sbid]);
     });
   }
index a460492..72f0396 100644 (file)
@@ -134,7 +134,8 @@ void LocalSingleStoreElimPass::CalculateImmediateDominators(
   successors_map_.clear();
   for (auto& blk : *func) {
     ordered_blocks.push_back(&blk);
-    blk.ForEachSuccessorLabel([&blk, this](uint32_t sbid) {
+    const auto& const_blk = blk;
+    const_blk.ForEachSuccessorLabel([&blk, this](const uint32_t sbid) {
       successors_map_[&blk].push_back(label2block_[sbid]);
       predecessors_map_[label2block_[sbid]].push_back(&blk);
     });
index 8c9a63c..6869740 100644 (file)
@@ -75,7 +75,8 @@ BasicBlock* Loop::FindLoopPreheader(IRContext* ir_context,
   // preheader.
   bool is_preheader = true;
   uint32_t loop_header_id = loop_header_->id();
-  loop_pred->ForEachSuccessorLabel(
+  const auto* const_loop_pred = loop_pred;
+  const_loop_pred->ForEachSuccessorLabel(
       [&is_preheader, loop_header_id](const uint32_t id) {
         if (id != loop_header_id) is_preheader = false;
       });
@@ -205,7 +206,8 @@ void Loop::SetLatchBlock(BasicBlock* latch) {
 #ifndef NDEBUG
   assert(latch->GetParent() && "The basic block does not belong to a function");
 
-  latch->ForEachSuccessorLabel([this](uint32_t id) {
+  const auto* const_latch = latch;
+  const_latch->ForEachSuccessorLabel([this](uint32_t id) {
     assert((!IsInsideLoop(id) || id == GetHeaderBlock()->id()) &&
            "A predecessor of the continue block does not belong to the loop");
   });
index d0487ce..d6c9f36 100644 (file)
@@ -640,7 +640,8 @@ Pass::Status MemPass::InsertPhiInstructions(ir::Function* func) {
     // Look for successor backedge and patch phis in loop header
     // if found.
     uint32_t header = 0;
-    bp->ForEachSuccessorLabel([&header, this](uint32_t succ) {
+    const auto* const_bp = bp;
+    const_bp->ForEachSuccessorLabel([&header, this](uint32_t succ) {
       if (visitedBlocks_.find(succ) == visitedBlocks_.end()) return;
       assert(header == 0);
       header = succ;
@@ -806,7 +807,8 @@ bool MemPass::RemoveUnreachableBlocks(ir::Function* func) {
     worklist.pop();
 
     // All the successors of a live block are also live.
-    block->ForEachSuccessorLabel(mark_reachable);
+    static_cast<const ir::BasicBlock*>(block)->ForEachSuccessorLabel(
+        mark_reachable);
 
     // All the Merge and ContinueTarget blocks of a live block are also live.
     block->ForMergeAndContinueLabel(mark_reachable);
index 0c085f2..60479a8 100644 (file)
@@ -200,7 +200,8 @@ void SSAPropagator::Initialize(ir::Function* fn) {
       Edge(ctx_->cfg()->pseudo_entry_block(), fn->entry().get()));
 
   for (auto& block : *fn) {
-    block.ForEachSuccessorLabel([this, &block](uint32_t label_id) {
+    const auto& const_block = block;
+    const_block.ForEachSuccessorLabel([this, &block](const uint32_t label_id) {
       ir::BasicBlock* succ_bb =
           ctx_->get_instr_block(get_def_use_mgr()->GetDef(label_id));
       bb_succs_[&block].push_back(Edge(&block, succ_bb));