Index: lib/CodeGen/ManagedMemoryRewrite.cpp =================================================================== --- lib/CodeGen/ManagedMemoryRewrite.cpp +++ lib/CodeGen/ManagedMemoryRewrite.cpp @@ -123,10 +123,15 @@ assert(Cur && "invalid constant expression passed"); Instruction *I = Cur->getAsInstruction(); + 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,8 +139,8 @@ 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))