@@ -1443,7 +1443,7 @@ bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1443
1443
int64_t MOffset = 0 ;
1444
1444
const DataLayout &DL = MemCpy->getModule ()->getDataLayout ();
1445
1445
// We can only transforms memcpy's where the dest of one is the source of the
1446
- // other, or the memory transfer has a known offset from the memset .
1446
+ // other, or they have a known offset.
1447
1447
if (MemCpy->getSource () != MemSet->getDest ()) {
1448
1448
std::optional<int64_t > Offset =
1449
1449
MemCpy->getSource ()->getPointerOffsetFrom (MemSet->getDest (), DL);
@@ -1454,28 +1454,28 @@ bool MemCpyOptPass::performMemCpyToMemSetOptzn(MemCpyInst *MemCpy,
1454
1454
1455
1455
if (MOffset != 0 || MemSetSize != CopySize) {
1456
1456
// Make sure the memcpy doesn't read any more than what the memset wrote,
1457
- // other than undef. Don't worry about sizes larger than i64. A known memset
1458
- // size is required.
1457
+ // other than undef. Don't worry about sizes larger than i64.
1459
1458
auto *CMemSetSize = dyn_cast<ConstantInt>(MemSetSize);
1460
- if (!CMemSetSize)
1461
- return false ;
1462
-
1463
- // A known memcpy size is also required.
1464
1459
auto *CCopySize = dyn_cast<ConstantInt>(CopySize);
1465
- if (!CCopySize)
1466
- return false ;
1467
- if (CCopySize->getZExtValue () + MOffset > CMemSetSize->getZExtValue ()) {
1460
+ if (!CMemSetSize || !CCopySize ||
1461
+ CCopySize->getZExtValue () + MOffset > CMemSetSize->getZExtValue ()) {
1468
1462
if (!overreadUndefContents (MSSA, MemCpy, MemSet, BAA))
1469
1463
return false ;
1470
- // Clip the memcpy to the bounds of the memset
1471
- if (MOffset == 0 )
1472
- CopySize = MemSetSize;
1473
- else
1474
- CopySize =
1475
- ConstantInt::get (CopySize->getType (),
1476
- CMemSetSize->getZExtValue () <= (uint64_t )MOffset
1477
- ? 0
1478
- : CMemSetSize->getZExtValue () - MOffset);
1464
+
1465
+ if (CMemSetSize && CCopySize) {
1466
+ // If both have constant sizes and offsets, clip the memcpy to the
1467
+ // bounds of the memset if applicable.
1468
+ assert (CCopySize->getZExtValue () + MOffset >
1469
+ CMemSetSize->getZExtValue ());
1470
+ if (MOffset == 0 )
1471
+ CopySize = MemSetSize;
1472
+ else
1473
+ CopySize =
1474
+ ConstantInt::get (CopySize->getType (),
1475
+ CMemSetSize->getZExtValue () <= (uint64_t )MOffset
1476
+ ? 0
1477
+ : CMemSetSize->getZExtValue () - MOffset);
1478
+ }
1479
1479
}
1480
1480
}
1481
1481
0 commit comments