@@ -72,12 +72,21 @@ bool Constant::isNegativeZeroValue() const {
7272}
7373
7474// Return true iff this constant is positive zero (floating point), negative
75- // zero (floating point), or a null value.
75+ // zero (floating point), zero-value pointer, or a null value.
7676bool Constant::isZeroValue () const {
7777 // Floating point values have an explicit -0.0 value.
7878 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(this ))
7979 return CFP->isZero ();
8080
81+ // Zero value pointer is a constant expression of inttoptr(0).
82+ if (const auto *CE = dyn_cast<ConstantExpr>(this )) {
83+ if (CE->getOpcode () == Instruction::IntToPtr) {
84+ Constant *SrcCI = cast<Constant>(CE->getOperand (0 ));
85+ if (SrcCI->isZeroValue ())
86+ return true ;
87+ }
88+ }
89+
8190 // Check for constant splat vectors of 1 values.
8291 if (getType ()->isVectorTy ())
8392 if (const auto *SplatCFP = dyn_cast_or_null<ConstantFP>(getSplatValue ()))
@@ -369,7 +378,10 @@ bool Constant::containsConstantExpression() const {
369378 return false ;
370379}
371380
372- // / Constructor to create a '0' constant of arbitrary type.
381+ // / Constructor that creates a null constant of any type. For most types, this
382+ // / means a constant with value '0', but for pointer types, it represents a
383+ // / nullptr constant. A nullptr isn't always a zero-value pointer in certain
384+ // / address spaces on some targets.
373385Constant *Constant::getNullValue (Type *Ty) {
374386 switch (Ty->getTypeID ()) {
375387 case Type::IntegerTyID:
@@ -400,6 +412,19 @@ Constant *Constant::getNullValue(Type *Ty) {
400412 }
401413}
402414
415+ // / Constructor that creates a zero constant of any type. For most types, this
416+ // / is equivalent to getNullValue. For pointer types, it creates an inttoptr
417+ // / constant expression.
418+ Constant *Constant::getZeroValue (Type *Ty) {
419+ switch (Ty->getTypeID ()) {
420+ case Type::PointerTyID:
421+ return ConstantExpr::getIntToPtr (
422+ ConstantInt::get (Type::getInt8Ty (Ty->getContext ()), 0 ), Ty);
423+ default :
424+ return Constant::getNullValue (Ty);
425+ }
426+ }
427+
403428Constant *Constant::getIntegerValue (Type *Ty, const APInt &V) {
404429 Type *ScalarTy = Ty->getScalarType ();
405430
@@ -735,7 +760,7 @@ static bool constantIsDead(const Constant *C, bool RemoveDeadUsers) {
735760 ReplaceableMetadataImpl::SalvageDebugInfo (*C);
736761 const_cast <Constant *>(C)->destroyConstant ();
737762 }
738-
763+
739764 return true ;
740765}
741766
0 commit comments