@@ -1217,10 +1217,11 @@ Constant *llvm::ConstantFoldCompareInstOperands(
1217
1217
Type *IntPtrTy = DL.getIntPtrType (CE0->getType ());
1218
1218
// Convert the integer value to the right size to ensure we get the
1219
1219
// proper extension or truncation.
1220
- Constant *C = ConstantExpr::getIntegerCast (CE0->getOperand (0 ),
1221
- IntPtrTy, false );
1222
- Constant *Null = Constant::getNullValue (C->getType ());
1223
- return ConstantFoldCompareInstOperands (Predicate, C, Null, DL, TLI);
1220
+ if (Constant *C = ConstantFoldIntegerCast (CE0->getOperand (0 ), IntPtrTy,
1221
+ /* IsSigned*/ false , DL)) {
1222
+ Constant *Null = Constant::getNullValue (C->getType ());
1223
+ return ConstantFoldCompareInstOperands (Predicate, C, Null, DL, TLI);
1224
+ }
1224
1225
}
1225
1226
1226
1227
// Only do this transformation if the int is intptrty in size, otherwise
@@ -1242,11 +1243,12 @@ Constant *llvm::ConstantFoldCompareInstOperands(
1242
1243
1243
1244
// Convert the integer value to the right size to ensure we get the
1244
1245
// proper extension or truncation.
1245
- Constant *C0 = ConstantExpr::getIntegerCast (CE0->getOperand (0 ),
1246
- IntPtrTy, false );
1247
- Constant *C1 = ConstantExpr::getIntegerCast (CE1->getOperand (0 ),
1248
- IntPtrTy, false );
1249
- return ConstantFoldCompareInstOperands (Predicate, C0, C1, DL, TLI);
1246
+ Constant *C0 = ConstantFoldIntegerCast (CE0->getOperand (0 ), IntPtrTy,
1247
+ /* IsSigned*/ false , DL);
1248
+ Constant *C1 = ConstantFoldIntegerCast (CE1->getOperand (0 ), IntPtrTy,
1249
+ /* IsSigned*/ false , DL);
1250
+ if (C0 && C1)
1251
+ return ConstantFoldCompareInstOperands (Predicate, C0, C1, DL, TLI);
1250
1252
}
1251
1253
1252
1254
// Only do this transformation if the int is intptrty in size, otherwise
@@ -1401,9 +1403,9 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
1401
1403
// the width of a pointer, so it can't be done in ConstantExpr::getCast.
1402
1404
if (CE->getOpcode () == Instruction::IntToPtr) {
1403
1405
// zext/trunc the inttoptr to pointer size.
1404
- FoldedValue = ConstantExpr::getIntegerCast (
1405
- CE-> getOperand ( 0 ), DL.getIntPtrType (CE->getType ()),
1406
- /* IsSigned=*/ false );
1406
+ FoldedValue = ConstantFoldIntegerCast (CE-> getOperand ( 0 ),
1407
+ DL.getIntPtrType (CE->getType ()),
1408
+ /* IsSigned=*/ false , DL );
1407
1409
} else if (auto *GEP = dyn_cast<GEPOperator>(CE)) {
1408
1410
// If we have GEP, we can perform the following folds:
1409
1411
// (ptrtoint (gep null, x)) -> x
@@ -1431,8 +1433,8 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
1431
1433
}
1432
1434
if (FoldedValue) {
1433
1435
// Do a zext or trunc to get to the ptrtoint dest size.
1434
- return ConstantExpr::getIntegerCast (FoldedValue, DestTy,
1435
- /* IsSigned= */ false );
1436
+ return ConstantFoldIntegerCast (FoldedValue, DestTy, /* IsSigned= */ false ,
1437
+ DL );
1436
1438
}
1437
1439
}
1438
1440
break ;
@@ -1475,6 +1477,18 @@ Constant *llvm::ConstantFoldCastOperand(unsigned Opcode, Constant *C,
1475
1477
return ConstantFoldCastInstruction (Opcode, C, DestTy);
1476
1478
}
1477
1479
1480
+ Constant *llvm::ConstantFoldIntegerCast (Constant *C, Type *DestTy,
1481
+ bool IsSigned, const DataLayout &DL) {
1482
+ Type *SrcTy = C->getType ();
1483
+ if (SrcTy == DestTy)
1484
+ return C;
1485
+ if (SrcTy->getScalarSizeInBits () > DestTy->getScalarSizeInBits ())
1486
+ return ConstantFoldCastOperand (Instruction::Trunc, C, DestTy, DL);
1487
+ if (IsSigned)
1488
+ return ConstantFoldCastOperand (Instruction::SExt, C, DestTy, DL);
1489
+ return ConstantFoldCastOperand (Instruction::ZExt, C, DestTy, DL);
1490
+ }
1491
+
1478
1492
// ===----------------------------------------------------------------------===//
1479
1493
// Constant Folding for Calls
1480
1494
//
0 commit comments