@@ -692,37 +692,29 @@ Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
692
692
// / Compute the base pointer and cumulative constant offsets for V.
693
693
// /
694
694
// / This strips all constant offsets off of V, leaving it the base pointer, and
695
- // / accumulates the total constant offset applied in the returned constant. It
696
- // / returns 0 if V is not a pointer, and returns the constant '0' if there are
697
- // / no constant offsets applied.
695
+ // / accumulates the total constant offset applied in the returned constant.
696
+ // / It returns zero if there are no constant offsets applied.
698
697
// /
699
- // / This is very similar to GetPointerBaseWithConstantOffset except it doesn't
700
- // / follow non-inbounds geps. This allows it to remain usable for icmp ult/etc.
701
- // / folding .
702
- static Constant * stripAndComputeConstantOffsets (const DataLayout &DL, Value *&V,
703
- bool AllowNonInbounds = false ) {
698
+ // / This is very similar to stripAndAccumulateConstantOffsets(), except it
699
+ // / normalizes the offset bitwidth to the stripped pointer type, not the
700
+ // / original pointer type .
701
+ static APInt stripAndComputeConstantOffsets (const DataLayout &DL, Value *&V,
702
+ bool AllowNonInbounds = false ) {
704
703
assert (V->getType ()->isPtrOrPtrVectorTy ());
705
704
706
705
APInt Offset = APInt::getZero (DL.getIndexTypeSizeInBits (V->getType ()));
707
-
708
706
V = V->stripAndAccumulateConstantOffsets (DL, Offset, AllowNonInbounds);
709
707
// As that strip may trace through `addrspacecast`, need to sext or trunc
710
708
// the offset calculated.
711
- Type *IntIdxTy = DL.getIndexType (V->getType ())->getScalarType ();
712
- Offset = Offset.sextOrTrunc (IntIdxTy->getIntegerBitWidth ());
713
-
714
- Constant *OffsetIntPtr = ConstantInt::get (IntIdxTy, Offset);
715
- if (VectorType *VecTy = dyn_cast<VectorType>(V->getType ()))
716
- return ConstantVector::getSplat (VecTy->getElementCount (), OffsetIntPtr);
717
- return OffsetIntPtr;
709
+ return Offset.sextOrTrunc (DL.getIndexTypeSizeInBits (V->getType ()));
718
710
}
719
711
720
712
// / Compute the constant difference between two pointer values.
721
713
// / If the difference is not a constant, returns zero.
722
714
static Constant *computePointerDifference (const DataLayout &DL, Value *LHS,
723
715
Value *RHS) {
724
- Constant * LHSOffset = stripAndComputeConstantOffsets (DL, LHS);
725
- Constant * RHSOffset = stripAndComputeConstantOffsets (DL, RHS);
716
+ APInt LHSOffset = stripAndComputeConstantOffsets (DL, LHS);
717
+ APInt RHSOffset = stripAndComputeConstantOffsets (DL, RHS);
726
718
727
719
// If LHS and RHS are not related via constant offsets to the same base
728
720
// value, there is nothing we can do here.
@@ -733,7 +725,10 @@ static Constant *computePointerDifference(const DataLayout &DL, Value *LHS,
733
725
// LHS - RHS
734
726
// = (LHSOffset + Base) - (RHSOffset + Base)
735
727
// = LHSOffset - RHSOffset
736
- return ConstantExpr::getSub (LHSOffset, RHSOffset);
728
+ Constant *Res = ConstantInt::get (LHS->getContext (), LHSOffset - RHSOffset);
729
+ if (auto *VecTy = dyn_cast<VectorType>(LHS->getType ()))
730
+ Res = ConstantVector::getSplat (VecTy->getElementCount (), Res);
731
+ return Res;
737
732
}
738
733
739
734
// / Given operands for a Sub, see if we can fold the result.
@@ -2592,15 +2587,14 @@ computePointerICmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
2592
2587
// Even if an non-inbounds GEP occurs along the path we can still optimize
2593
2588
// equality comparisons concerning the result.
2594
2589
bool AllowNonInbounds = ICmpInst::isEquality (Pred);
2595
- Constant *LHSOffset =
2596
- stripAndComputeConstantOffsets (DL, LHS, AllowNonInbounds);
2597
- Constant *RHSOffset =
2598
- stripAndComputeConstantOffsets (DL, RHS, AllowNonInbounds);
2590
+ APInt LHSOffset = stripAndComputeConstantOffsets (DL, LHS, AllowNonInbounds);
2591
+ APInt RHSOffset = stripAndComputeConstantOffsets (DL, RHS, AllowNonInbounds);
2599
2592
2600
2593
// If LHS and RHS are related via constant offsets to the same base
2601
2594
// value, we can replace it with an icmp which just compares the offsets.
2602
2595
if (LHS == RHS)
2603
- return ConstantExpr::getICmp (Pred, LHSOffset, RHSOffset);
2596
+ return ConstantInt::get (
2597
+ GetCompareTy (LHS), ICmpInst::compare (LHSOffset, RHSOffset, Pred));
2604
2598
2605
2599
// Various optimizations for (in)equality comparisons.
2606
2600
if (Pred == CmpInst::ICMP_EQ || Pred == CmpInst::ICMP_NE) {
@@ -2635,32 +2629,23 @@ computePointerICmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
2635
2629
// address, due to canonicalization and constant folding.
2636
2630
if (isa<AllocaInst>(LHS) &&
2637
2631
(isa<AllocaInst>(RHS) || isa<GlobalVariable>(RHS))) {
2638
- ConstantInt *LHSOffsetCI = dyn_cast<ConstantInt>(LHSOffset);
2639
- ConstantInt *RHSOffsetCI = dyn_cast<ConstantInt>(RHSOffset);
2640
2632
uint64_t LHSSize, RHSSize;
2641
2633
ObjectSizeOpts Opts;
2642
2634
Opts.NullIsUnknownSize =
2643
2635
NullPointerIsDefined (cast<AllocaInst>(LHS)->getFunction ());
2644
- if (LHSOffsetCI && RHSOffsetCI &&
2645
- getObjectSize (LHS, LHSSize, DL, TLI, Opts) &&
2646
- getObjectSize (RHS, RHSSize, DL, TLI, Opts)) {
2647
- const APInt &LHSOffsetValue = LHSOffsetCI->getValue ();
2648
- const APInt &RHSOffsetValue = RHSOffsetCI->getValue ();
2649
- if (!LHSOffsetValue.isNegative () &&
2650
- !RHSOffsetValue.isNegative () &&
2651
- LHSOffsetValue.ult (LHSSize) &&
2652
- RHSOffsetValue.ult (RHSSize)) {
2653
- return ConstantInt::get (GetCompareTy (LHS),
2654
- !CmpInst::isTrueWhenEqual (Pred));
2655
- }
2636
+ if (getObjectSize (LHS, LHSSize, DL, TLI, Opts) &&
2637
+ getObjectSize (RHS, RHSSize, DL, TLI, Opts) &&
2638
+ !LHSOffset.isNegative () && !RHSOffset.isNegative () &&
2639
+ LHSOffset.ult (LHSSize) && RHSOffset.ult (RHSSize)) {
2640
+ return ConstantInt::get (GetCompareTy (LHS),
2641
+ !CmpInst::isTrueWhenEqual (Pred));
2656
2642
}
2657
2643
2658
2644
// Repeat the above check but this time without depending on DataLayout
2659
2645
// or being able to compute a precise size.
2660
2646
if (!cast<PointerType>(LHS->getType ())->isEmptyTy () &&
2661
2647
!cast<PointerType>(RHS->getType ())->isEmptyTy () &&
2662
- LHSOffset->isNullValue () &&
2663
- RHSOffset->isNullValue ())
2648
+ LHSOffset.isNullValue () && RHSOffset.isNullValue ())
2664
2649
return ConstantInt::get (GetCompareTy (LHS),
2665
2650
!CmpInst::isTrueWhenEqual (Pred));
2666
2651
}
0 commit comments