Add a Block::dropAllReferences to drop all references from held instructions and call it when clearing the block. This fixes a bug where ForInst/IfInst instructions may still have references to values while being destroyed.

PiperOrigin-RevId: 229207798
This commit is contained in:
River Riddle 2019-01-14 10:30:20 -08:00 committed by jpienaar
parent a674ae8bbd
commit d6b71b0d57
3 changed files with 17 additions and 2 deletions

View File

@ -40,6 +40,9 @@ public:
~Block();
void clear() {
// Drop all references from within this block.
dropAllReferences();
// Clear instructions in the reverse order so that uses are destroyed
// before their defs.
while (!empty())
@ -162,6 +165,11 @@ public:
const_cast<Instruction *>(&inst));
}
/// This drops all operand uses from instructions within this block, which is
/// an essential step in breaking cyclic dependences between references when
/// they are to be deleted.
void dropAllReferences();
//===--------------------------------------------------------------------===//
// Terminator management
//===--------------------------------------------------------------------===//

View File

@ -87,6 +87,14 @@ Instruction *Block::findAncestorInstInBlock(Instruction *inst) {
return currInst;
}
/// This drops all operand uses from instructions within this block, which is
/// an essential step in breaking cyclic dependences between references when
/// they are to be deleted.
void Block::dropAllReferences() {
for (Instruction &i : *this)
i.dropAllReferences();
}
//===----------------------------------------------------------------------===//
// Argument list management.
//===----------------------------------------------------------------------===//

View File

@ -37,8 +37,7 @@ Function::~Function() {
// Instructions may have cyclic references, which need to be dropped before we
// can start deleting them.
for (auto &bb : *this)
for (auto &inst : bb)
inst.dropAllReferences();
bb.dropAllReferences();
// Clean up function attributes referring to this function.
FunctionAttr::dropFunctionReference(this);