@@ -525,28 +525,33 @@ static bool dominatesMergePoint(
525525static  ConstantInt *getConstantInt (Value *V, const  DataLayout &DL) {
526526  //  Normal constant int.
527527  ConstantInt *CI = dyn_cast<ConstantInt>(V);
528-   if  (CI || !isa<Constant>(V) || !V->getType ()->isPointerTy () ||
529-       DL.isNonIntegralPointerType (V->getType ()))
528+   if  (CI || !isa<Constant>(V) || !V->getType ()->isPointerTy ())
530529    return  CI;
531530
531+   //  It is not safe to look through inttoptr or ptrtoint when using unstable
532+   //  pointer types.
533+   if  (DL.hasUnstableRepresentation (V->getType ()))
534+     return  nullptr ;
535+ 
532536  //  This is some kind of pointer constant. Turn it into a pointer-sized
533537  //  ConstantInt if possible.
534-   IntegerType *PtrTy  = cast<IntegerType>(DL.getIntPtrType (V->getType ()));
538+   IntegerType *IntPtrTy  = cast<IntegerType>(DL.getIntPtrType (V->getType ()));
535539
536540  //  Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
537541  if  (isa<ConstantPointerNull>(V))
538-     return  ConstantInt::get (PtrTy , 0 );
542+     return  ConstantInt::get (IntPtrTy , 0 );
539543
540-   //  IntToPtr const int.
544+   //  IntToPtr const int, we can look through this if the semantics of
545+   //  inttoptr for this address space are a simple (truncating) bitcast.
541546  if  (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
542547    if  (CE->getOpcode () == Instruction::IntToPtr)
543548      if  (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand (0 ))) {
544549        //  The constant is very likely to have the right type already.
545-         if  (CI->getType () == PtrTy )
550+         if  (CI->getType () == IntPtrTy )
546551          return  CI;
547552        else 
548553          return  cast<ConstantInt>(
549-               ConstantFoldIntegerCast (CI, PtrTy , /* isSigned=*/ false , DL));
554+               ConstantFoldIntegerCast (CI, IntPtrTy , /* isSigned=*/ false , DL));
550555      }
551556  return  nullptr ;
552557}
@@ -866,10 +871,12 @@ Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
866871      }
867872    }
868873
869-   //  Unwrap any lossless ptrtoint cast.
874+   //  Unwrap any lossless ptrtoint cast (except for unstable pointers) .
870875  if  (CV) {
871876    if  (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) {
872877      Value *Ptr = PTII->getPointerOperand ();
878+       if  (DL.hasUnstableRepresentation (Ptr->getType ()))
879+         return  CV;
873880      if  (PTII->getType () == DL.getIntPtrType (Ptr->getType ()))
874881        CV = Ptr;
875882    }
@@ -1427,6 +1434,8 @@ bool SimplifyCFGOpt::performValueComparisonIntoPredecessorFolding(
14271434  Builder.SetInsertPoint (PTI);
14281435  //  Convert pointer to int before we switch.
14291436  if  (CV->getType ()->isPointerTy ()) {
1437+     assert (!DL.hasUnstableRepresentation (CV->getType ()) &&
1438+            " Should not end up here with unstable pointers"  );
14301439    CV =
14311440        Builder.CreatePtrToInt (CV, DL.getIntPtrType (CV->getType ()), " magicptr"  );
14321441  }
@@ -5246,6 +5255,8 @@ bool SimplifyCFGOpt::simplifyBranchOnICmpChain(BranchInst *BI,
52465255  Builder.SetInsertPoint (BI);
52475256  //  Convert pointer to int before we switch.
52485257  if  (CompVal->getType ()->isPointerTy ()) {
5258+     assert (!DL.hasUnstableRepresentation (CompVal->getType ()) &&
5259+            " Should not end up here with unstable pointers"  );
52495260    CompVal = Builder.CreatePtrToInt (
52505261        CompVal, DL.getIntPtrType (CompVal->getType ()), " magicptr"  );
52515262  }
0 commit comments