diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -2751,30 +2751,20 @@ // Block iterator interface. This provides access to the list of incoming // basic blocks, which parallels the list of incoming values. + // Please note that we are not providing non-const iterators for blocks to + // force all updates go through an interface function. using block_iterator = BasicBlock **; using const_block_iterator = BasicBlock * const *; - block_iterator block_begin() { - return reinterpret_cast(op_begin() + ReservedSpace); - } - const_block_iterator block_begin() const { return reinterpret_cast(op_begin() + ReservedSpace); } - block_iterator block_end() { - return block_begin() + getNumOperands(); - } - const_block_iterator block_end() const { return block_begin() + getNumOperands(); } - iterator_range blocks() { - return make_range(block_begin(), block_end()); - } - iterator_range blocks() const { return make_range(block_begin(), block_end()); } @@ -2829,8 +2819,14 @@ } void setIncomingBlock(unsigned i, BasicBlock *BB) { - assert(BB && "PHI node got a null basic block!"); - block_begin()[i] = BB; + const_cast(block_begin())[i] = BB; + } + + /// Copies the basic blocks from \p BBRange to the incoming basic block list + /// of this PHINode, starting at \p ToIdx. + void copyIncomingBlocks(iterator_range BBRange, + uint32_t ToIdx = 0) { + copy(BBRange, const_cast(block_begin()) + ToIdx); } /// Replace every incoming basic block \p Old to basic block \p New. diff --git a/llvm/lib/IR/Instructions.cpp b/llvm/lib/IR/Instructions.cpp --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -106,7 +106,7 @@ ReservedSpace(PN.getNumOperands()) { allocHungoffUses(PN.getNumOperands()); std::copy(PN.op_begin(), PN.op_end(), op_begin()); - std::copy(PN.block_begin(), PN.block_end(), block_begin()); + copyIncomingBlocks(make_range(PN.block_begin(), PN.block_end())); SubclassOptionalData = PN.SubclassOptionalData; } @@ -121,7 +121,7 @@ // clients might not expect this to happen. The code as it is thrashes the // use/def lists, which is kinda lame. std::copy(op_begin() + Idx + 1, op_end(), op_begin() + Idx); - std::copy(block_begin() + Idx + 1, block_end(), block_begin() + Idx); + copyIncomingBlocks(make_range(block_begin() + Idx + 1, block_end()), Idx); // Nuke the last value. Op<-1>().set(nullptr);