diff options
Diffstat (limited to 'lib/IR/Globals.cpp')
-rw-r--r-- | lib/IR/Globals.cpp | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/lib/IR/Globals.cpp b/lib/IR/Globals.cpp index 1028e33eae64..79a458c26f77 100644 --- a/lib/IR/Globals.cpp +++ b/lib/IR/Globals.cpp @@ -214,14 +214,20 @@ void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, void GlobalVariable::setInitializer(Constant *InitVal) { if (!InitVal) { if (hasInitializer()) { + // Note, the num operands is used to compute the offset of the operand, so + // the order here matters. Clearing the operand then clearing the num + // operands ensures we have the correct offset to the operand. Op<0>().set(nullptr); - NumOperands = 0; + setGlobalVariableNumOperands(0); } } else { assert(InitVal->getType() == getType()->getElementType() && "Initializer type must match GlobalVariable type"); + // Note, the num operands is used to compute the offset of the operand, so + // the order here matters. We need to set num operands to 1 first so that + // we get the correct offset to the first operand when we set it. if (!hasInitializer()) - NumOperands = 1; + setGlobalVariableNumOperands(1); Op<0>().set(InitVal); } } |