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