@@ -1417,50 +1417,6 @@ Constant *llvm::ConstantFoldCompareInstruction(CmpInst::Predicate Predicate,
1417
1417
return nullptr ;
1418
1418
}
1419
1419
1420
- // / Test whether the given sequence of *normalized* indices is "inbounds".
1421
- template <typename IndexTy>
1422
- static bool isInBoundsIndices (ArrayRef<IndexTy> Idxs) {
1423
- // No indices means nothing that could be out of bounds.
1424
- if (Idxs.empty ()) return true ;
1425
-
1426
- // If the first index is zero, it's in bounds.
1427
- if (cast<Constant>(Idxs[0 ])->isNullValue ()) return true ;
1428
-
1429
- // If the first index is one and all the rest are zero, it's in bounds,
1430
- // by the one-past-the-end rule.
1431
- if (auto *CI = dyn_cast<ConstantInt>(Idxs[0 ])) {
1432
- if (!CI->isOne ())
1433
- return false ;
1434
- } else {
1435
- auto *CV = cast<ConstantDataVector>(Idxs[0 ]);
1436
- CI = dyn_cast_or_null<ConstantInt>(CV->getSplatValue ());
1437
- if (!CI || !CI->isOne ())
1438
- return false ;
1439
- }
1440
-
1441
- for (unsigned i = 1 , e = Idxs.size (); i != e; ++i)
1442
- if (!cast<Constant>(Idxs[i])->isNullValue ())
1443
- return false ;
1444
- return true ;
1445
- }
1446
-
1447
- // / Test whether a given ConstantInt is in-range for a SequentialType.
1448
- static bool isIndexInRangeOfArrayType (uint64_t NumElements,
1449
- const ConstantInt *CI) {
1450
- // We cannot bounds check the index if it doesn't fit in an int64_t.
1451
- if (CI->getValue ().getSignificantBits () > 64 )
1452
- return false ;
1453
-
1454
- // A negative index or an index past the end of our sequential type is
1455
- // considered out-of-range.
1456
- int64_t IndexVal = CI->getSExtValue ();
1457
- if (IndexVal < 0 || (IndexVal != 0 && (uint64_t )IndexVal >= NumElements))
1458
- return false ;
1459
-
1460
- // Otherwise, it is in-range.
1461
- return true ;
1462
- }
1463
-
1464
1420
// Combine Indices - If the source pointer to this getelementptr instruction
1465
1421
// is a getelementptr instruction, combine the indices of the two
1466
1422
// getelementptr instructions into a single instruction.
@@ -1572,157 +1528,5 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C,
1572
1528
if (Constant *C = foldGEPOfGEP (GEP, PointeeTy, InBounds, Idxs))
1573
1529
return C;
1574
1530
1575
- // Check to see if any array indices are not within the corresponding
1576
- // notional array or vector bounds. If so, try to determine if they can be
1577
- // factored out into preceding dimensions.
1578
- SmallVector<Constant *, 8 > NewIdxs;
1579
- Type *Ty = PointeeTy;
1580
- Type *Prev = C->getType ();
1581
- auto GEPIter = gep_type_begin (PointeeTy, Idxs);
1582
- bool Unknown =
1583
- !isa<ConstantInt>(Idxs[0 ]) && !isa<ConstantDataVector>(Idxs[0 ]);
1584
- for (unsigned i = 1 , e = Idxs.size (); i != e;
1585
- Prev = Ty, Ty = (++GEPIter).getIndexedType (), ++i) {
1586
- if (!isa<ConstantInt>(Idxs[i]) && !isa<ConstantDataVector>(Idxs[i])) {
1587
- // We don't know if it's in range or not.
1588
- Unknown = true ;
1589
- continue ;
1590
- }
1591
- if (!isa<ConstantInt>(Idxs[i - 1 ]) && !isa<ConstantDataVector>(Idxs[i - 1 ]))
1592
- // Skip if the type of the previous index is not supported.
1593
- continue ;
1594
- if (isa<StructType>(Ty)) {
1595
- // The verify makes sure that GEPs into a struct are in range.
1596
- continue ;
1597
- }
1598
- if (isa<VectorType>(Ty)) {
1599
- // There can be awkward padding in after a non-power of two vector.
1600
- Unknown = true ;
1601
- continue ;
1602
- }
1603
- auto *STy = cast<ArrayType>(Ty);
1604
- if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) {
1605
- if (isIndexInRangeOfArrayType (STy->getNumElements (), CI))
1606
- // It's in range, skip to the next index.
1607
- continue ;
1608
- if (CI->isNegative ()) {
1609
- // It's out of range and negative, don't try to factor it.
1610
- Unknown = true ;
1611
- continue ;
1612
- }
1613
- } else {
1614
- auto *CV = cast<ConstantDataVector>(Idxs[i]);
1615
- bool IsInRange = true ;
1616
- for (unsigned I = 0 , E = CV->getNumElements (); I != E; ++I) {
1617
- auto *CI = cast<ConstantInt>(CV->getElementAsConstant (I));
1618
- IsInRange &= isIndexInRangeOfArrayType (STy->getNumElements (), CI);
1619
- if (CI->isNegative ()) {
1620
- Unknown = true ;
1621
- break ;
1622
- }
1623
- }
1624
- if (IsInRange || Unknown)
1625
- // It's in range, skip to the next index.
1626
- // It's out of range and negative, don't try to factor it.
1627
- continue ;
1628
- }
1629
- if (isa<StructType>(Prev)) {
1630
- // It's out of range, but the prior dimension is a struct
1631
- // so we can't do anything about it.
1632
- Unknown = true ;
1633
- continue ;
1634
- }
1635
-
1636
- // Determine the number of elements in our sequential type.
1637
- uint64_t NumElements = STy->getArrayNumElements ();
1638
- if (!NumElements) {
1639
- Unknown = true ;
1640
- continue ;
1641
- }
1642
-
1643
- // It's out of range, but we can factor it into the prior
1644
- // dimension.
1645
- NewIdxs.resize (Idxs.size ());
1646
-
1647
- // Expand the current index or the previous index to a vector from a scalar
1648
- // if necessary.
1649
- Constant *CurrIdx = cast<Constant>(Idxs[i]);
1650
- auto *PrevIdx =
1651
- NewIdxs[i - 1 ] ? NewIdxs[i - 1 ] : cast<Constant>(Idxs[i - 1 ]);
1652
- bool IsCurrIdxVector = CurrIdx->getType ()->isVectorTy ();
1653
- bool IsPrevIdxVector = PrevIdx->getType ()->isVectorTy ();
1654
- bool UseVector = IsCurrIdxVector || IsPrevIdxVector;
1655
-
1656
- if (!IsCurrIdxVector && IsPrevIdxVector)
1657
- CurrIdx = ConstantDataVector::getSplat (
1658
- cast<FixedVectorType>(PrevIdx->getType ())->getNumElements (), CurrIdx);
1659
-
1660
- if (!IsPrevIdxVector && IsCurrIdxVector)
1661
- PrevIdx = ConstantDataVector::getSplat (
1662
- cast<FixedVectorType>(CurrIdx->getType ())->getNumElements (), PrevIdx);
1663
-
1664
- Constant *Factor =
1665
- ConstantInt::get (CurrIdx->getType ()->getScalarType (), NumElements);
1666
- if (UseVector)
1667
- Factor = ConstantDataVector::getSplat (
1668
- IsPrevIdxVector
1669
- ? cast<FixedVectorType>(PrevIdx->getType ())->getNumElements ()
1670
- : cast<FixedVectorType>(CurrIdx->getType ())->getNumElements (),
1671
- Factor);
1672
-
1673
- NewIdxs[i] =
1674
- ConstantFoldBinaryInstruction (Instruction::SRem, CurrIdx, Factor);
1675
-
1676
- Constant *Div =
1677
- ConstantFoldBinaryInstruction (Instruction::SDiv, CurrIdx, Factor);
1678
-
1679
- // We're working on either ConstantInt or vectors of ConstantInt,
1680
- // so these should always fold.
1681
- assert (NewIdxs[i] != nullptr && Div != nullptr && " Should have folded" );
1682
-
1683
- unsigned CommonExtendedWidth =
1684
- std::max (PrevIdx->getType ()->getScalarSizeInBits (),
1685
- Div->getType ()->getScalarSizeInBits ());
1686
- CommonExtendedWidth = std::max (CommonExtendedWidth, 64U );
1687
-
1688
- // Before adding, extend both operands to i64 to avoid
1689
- // overflow trouble.
1690
- Type *ExtendedTy = Type::getIntNTy (Div->getContext (), CommonExtendedWidth);
1691
- if (UseVector)
1692
- ExtendedTy = FixedVectorType::get (
1693
- ExtendedTy,
1694
- IsPrevIdxVector
1695
- ? cast<FixedVectorType>(PrevIdx->getType ())->getNumElements ()
1696
- : cast<FixedVectorType>(CurrIdx->getType ())->getNumElements ());
1697
-
1698
- if (!PrevIdx->getType ()->isIntOrIntVectorTy (CommonExtendedWidth))
1699
- PrevIdx =
1700
- ConstantFoldCastInstruction (Instruction::SExt, PrevIdx, ExtendedTy);
1701
-
1702
- if (!Div->getType ()->isIntOrIntVectorTy (CommonExtendedWidth))
1703
- Div = ConstantFoldCastInstruction (Instruction::SExt, Div, ExtendedTy);
1704
-
1705
- assert (PrevIdx && Div && " Should have folded" );
1706
- NewIdxs[i - 1 ] = ConstantExpr::getAdd (PrevIdx, Div);
1707
- }
1708
-
1709
- // If we did any factoring, start over with the adjusted indices.
1710
- if (!NewIdxs.empty ()) {
1711
- for (unsigned i = 0 , e = Idxs.size (); i != e; ++i)
1712
- if (!NewIdxs[i]) NewIdxs[i] = cast<Constant>(Idxs[i]);
1713
- return ConstantExpr::getGetElementPtr (PointeeTy, C, NewIdxs, InBounds,
1714
- InRange);
1715
- }
1716
-
1717
- // If all indices are known integers and normalized, we can do a simple
1718
- // check for the "inbounds" property.
1719
- if (!Unknown && !InBounds)
1720
- if (auto *GV = dyn_cast<GlobalVariable>(C))
1721
- if (!GV->hasExternalWeakLinkage () && GV->getValueType () == PointeeTy &&
1722
- isInBoundsIndices (Idxs))
1723
- // TODO(gep_nowrap): Can also set NUW here.
1724
- return ConstantExpr::getGetElementPtr (
1725
- PointeeTy, C, Idxs, GEPNoWrapFlags::inBounds (), InRange);
1726
-
1727
1531
return nullptr ;
1728
1532
}
0 commit comments