From: Vasileios Porpodas Date: Wed, 7 Dec 2022 21:10:23 +0000 (-0800) Subject: [IR][NFC] Cleanup: Remove non-const block iterators to force all updates go through... X-Git-Tag: upstream/17.0.6~23523 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=470bc76b1350119ede9768fd3e83a56d361d43f3;p=platform%2Fupstream%2Fllvm.git [IR][NFC] Cleanup: Remove non-const block iterators to force all updates go through an interface function Differential Revision: https://reviews.llvm.org/D140154 --- diff --git a/llvm/include/llvm/IR/Instructions.h b/llvm/include/llvm/IR/Instructions.h index ed4bfa8..1ba544d 100644 --- a/llvm/include/llvm/IR/Instructions.h +++ b/llvm/include/llvm/IR/Instructions.h @@ -2751,30 +2751,20 @@ public: // 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 @@ public: } 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 index 24afd66..46afae1 100644 --- a/llvm/lib/IR/Instructions.cpp +++ b/llvm/lib/IR/Instructions.cpp @@ -106,7 +106,7 @@ PHINode::PHINode(const PHINode &PN) 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 @@ Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { // 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);