Index: lib/CodeGen/ManagedMemoryRewrite.cpp =================================================================== --- lib/CodeGen/ManagedMemoryRewrite.cpp +++ lib/CodeGen/ManagedMemoryRewrite.cpp @@ -123,10 +123,16 @@ assert(Cur && "invalid constant expression passed"); Instruction *I = Cur->getAsInstruction(); + Cur->deleteValue(); + Expands.insert(I); Parent->setOperand(index, I); assert(I && "unable to convert ConstantExpr to Instruction"); + // Invalidate `Cur` so that no one after this point uses `Cur`. Rather, + // they should mutate `I`. + Cur = nullptr; + // The things that `Parent` uses (its operands) should be created // before `Parent`. Builder.SetInsertPoint(Parent); @@ -134,13 +140,14 @@ DEBUG(dbgs() << "Expanding ConstantExpression: " << *Cur << " | in Instruction: " << *I << "\n";); - for (unsigned i = 0; i < Cur->getNumOperands(); i++) { - Value *Op = Cur->getOperand(i); + for (unsigned i = 0; i < I->getNumOperands(); i++) { + Value *Op = I->getOperand(i); assert(isa(Op) && "constant must have a constant operand"); if (ConstantExpr *CExprOp = dyn_cast(Op)) expandConstantExpr(CExprOp, Builder, I, i, Expands); } + } // Edit all uses of `OldVal` to NewVal` in `Inst`. This will rewrite