@@ -525,28 +525,33 @@ static bool dominatesMergePoint(
525
525
static ConstantInt *getConstantInt (Value *V, const DataLayout &DL) {
526
526
// Normal constant int.
527
527
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 ())
530
529
return CI;
531
530
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
+
532
536
// This is some kind of pointer constant. Turn it into a pointer-sized
533
537
// ConstantInt if possible.
534
- IntegerType *PtrTy = cast<IntegerType>(DL.getIntPtrType (V->getType ()));
538
+ IntegerType *IntPtrTy = cast<IntegerType>(DL.getIntPtrType (V->getType ()));
535
539
536
540
// Null pointer means 0, see SelectionDAGBuilder::getValue(const Value*).
537
541
if (isa<ConstantPointerNull>(V))
538
- return ConstantInt::get (PtrTy , 0 );
542
+ return ConstantInt::get (IntPtrTy , 0 );
539
543
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.
541
546
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V))
542
547
if (CE->getOpcode () == Instruction::IntToPtr)
543
548
if (ConstantInt *CI = dyn_cast<ConstantInt>(CE->getOperand (0 ))) {
544
549
// The constant is very likely to have the right type already.
545
- if (CI->getType () == PtrTy )
550
+ if (CI->getType () == IntPtrTy )
546
551
return CI;
547
552
else
548
553
return cast<ConstantInt>(
549
- ConstantFoldIntegerCast (CI, PtrTy , /* isSigned=*/ false , DL));
554
+ ConstantFoldIntegerCast (CI, IntPtrTy , /* isSigned=*/ false , DL));
550
555
}
551
556
return nullptr ;
552
557
}
@@ -866,10 +871,12 @@ Value *SimplifyCFGOpt::isValueEqualityComparison(Instruction *TI) {
866
871
}
867
872
}
868
873
869
- // Unwrap any lossless ptrtoint cast.
874
+ // Unwrap any lossless ptrtoint cast (except for unstable pointers) .
870
875
if (CV) {
871
876
if (PtrToIntInst *PTII = dyn_cast<PtrToIntInst>(CV)) {
872
877
Value *Ptr = PTII->getPointerOperand ();
878
+ if (DL.hasUnstableRepresentation (Ptr->getType ()))
879
+ return CV;
873
880
if (PTII->getType () == DL.getIntPtrType (Ptr->getType ()))
874
881
CV = Ptr;
875
882
}
@@ -1427,6 +1434,8 @@ bool SimplifyCFGOpt::performValueComparisonIntoPredecessorFolding(
1427
1434
Builder.SetInsertPoint (PTI);
1428
1435
// Convert pointer to int before we switch.
1429
1436
if (CV->getType ()->isPointerTy ()) {
1437
+ assert (!DL.hasUnstableRepresentation (CV->getType ()) &&
1438
+ " Should not end up here with unstable pointers" );
1430
1439
CV =
1431
1440
Builder.CreatePtrToInt (CV, DL.getIntPtrType (CV->getType ()), " magicptr" );
1432
1441
}
@@ -5246,6 +5255,8 @@ bool SimplifyCFGOpt::simplifyBranchOnICmpChain(BranchInst *BI,
5246
5255
Builder.SetInsertPoint (BI);
5247
5256
// Convert pointer to int before we switch.
5248
5257
if (CompVal->getType ()->isPointerTy ()) {
5258
+ assert (!DL.hasUnstableRepresentation (CompVal->getType ()) &&
5259
+ " Should not end up here with unstable pointers" );
5249
5260
CompVal = Builder.CreatePtrToInt (
5250
5261
CompVal, DL.getIntPtrType (CompVal->getType ()), " magicptr" );
5251
5262
}
0 commit comments