Skip to content

Commit de57983

Browse files
committed
Revert "[SCEVAA] Allowing to subtract two inttoptrs with different pointer bases"
This reverts commit 60fd999.
1 parent c2a51bc commit de57983

File tree

4 files changed

+13
-158
lines changed

4 files changed

+13
-158
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,6 @@ class ScalarEvolution {
721721
const SCEV *getTruncateOrSignExtend(const SCEV *V, Type *Ty,
722722
unsigned Depth = 0);
723723

724-
/// Return a SCEV corresponding to a conversion of the input value to the
725-
/// specified type. If the type must be extended, it is any extended.
726-
const SCEV *getTruncateOrAnyExtend(const SCEV *V, Type *Ty,
727-
unsigned Depth = 0);
728-
729724
/// Return a SCEV corresponding to a conversion of the input value to the
730725
/// specified type. If the type must be extended, it is zero extended. The
731726
/// conversion must not be narrowing.
@@ -759,26 +754,6 @@ class ScalarEvolution {
759754
const SCEV *getUMinFromMismatchedTypes(SmallVectorImpl<const SCEV *> &Ops,
760755
bool Sequential = false);
761756

762-
/// Promote the operands to the wider of the types using any-extension, and
763-
/// then perform a addrec operation with them.
764-
const SCEV *
765-
getAddRecExprFromMismatchedTypes(const SmallVectorImpl<const SCEV *> &Ops,
766-
const Loop *L, SCEV::NoWrapFlags Flags);
767-
768-
/// Promote the operands to the wider of the types using any-extension, and
769-
/// then perform a add operation with them.
770-
const SCEV *
771-
getAddExprFromMismatchedTypes(const SmallVectorImpl<const SCEV *> &Ops,
772-
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
773-
unsigned Depth = 0);
774-
const SCEV *
775-
getAddExprFromMismatchedTypes(const SCEV *LHS, const SCEV *RHS,
776-
SCEV::NoWrapFlags Flags = SCEV::FlagAnyWrap,
777-
unsigned Depth = 0) {
778-
SmallVector<const SCEV *, 2> Ops = {LHS, RHS};
779-
return getAddExprFromMismatchedTypes(Ops, Flags, Depth);
780-
}
781-
782757
/// Transitively follow the chain of pointer-type operands until reaching a
783758
/// SCEV that does not have a single pointer operand. This returns a
784759
/// SCEVUnknown pointer for well-formed pointer-type expressions, but corner

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 5 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -4650,8 +4650,7 @@ const SCEV *ScalarEvolution::removePointerBase(const SCEV *P) {
46504650
Ops[0] = removePointerBase(Ops[0]);
46514651
// Don't try to transfer nowrap flags for now. We could in some cases
46524652
// (for example, if pointer operand of the AddRec is a SCEVUnknown).
4653-
return getAddRecExprFromMismatchedTypes(Ops, AddRec->getLoop(),
4654-
SCEV::FlagAnyWrap);
4653+
return getAddRecExpr(Ops, AddRec->getLoop(), SCEV::FlagAnyWrap);
46554654
}
46564655
if (auto *Add = dyn_cast<SCEVAddExpr>(P)) {
46574656
// The base of an Add is the pointer operand.
@@ -4666,47 +4665,25 @@ const SCEV *ScalarEvolution::removePointerBase(const SCEV *P) {
46664665
*PtrOp = removePointerBase(*PtrOp);
46674666
// Don't try to transfer nowrap flags for now. We could in some cases
46684667
// (for example, if the pointer operand of the Add is a SCEVUnknown).
4669-
return getAddExprFromMismatchedTypes(Ops);
4668+
return getAddExpr(Ops);
46704669
}
4671-
4672-
if (auto *Unknown = dyn_cast<SCEVUnknown>(P)) {
4673-
if (auto *O = dyn_cast<Operator>(Unknown->getValue())) {
4674-
if (O->getOpcode() == Instruction::IntToPtr) {
4675-
Value *Op0 = O->getOperand(0);
4676-
if (isa<ConstantInt>(Op0))
4677-
return getConstant(dyn_cast<ConstantInt>(Op0));
4678-
return getSCEV(Op0);
4679-
}
4680-
}
4681-
}
4682-
46834670
// Any other expression must be a pointer base.
46844671
return getZero(P->getType());
46854672
}
46864673

4687-
static bool isIntToPtr(const SCEV *V) {
4688-
if (auto *Unknown = dyn_cast<SCEVUnknown>(V))
4689-
if (auto *Op = dyn_cast<Operator>(Unknown->getValue()))
4690-
return Op->getOpcode() == Instruction::IntToPtr;
4691-
return false;
4692-
}
4693-
46944674
const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
46954675
SCEV::NoWrapFlags Flags,
46964676
unsigned Depth) {
46974677
// Fast path: X - X --> 0.
46984678
if (LHS == RHS)
46994679
return getZero(LHS->getType());
47004680

4701-
// If we subtract two pointers except inttoptrs with different pointer bases,
4702-
// bail.
4681+
// If we subtract two pointers with different pointer bases, bail.
47034682
// Eventually, we're going to add an assertion to getMulExpr that we
47044683
// can't multiply by a pointer.
47054684
if (RHS->getType()->isPointerTy()) {
4706-
const SCEV *LBase = getPointerBase(LHS);
4707-
const SCEV *RBase = getPointerBase(RHS);
47084685
if (!LHS->getType()->isPointerTy() ||
4709-
(LBase != RBase && (!isIntToPtr(LBase) || !isIntToPtr(RBase))))
4686+
getPointerBase(LHS) != getPointerBase(RHS))
47104687
return getCouldNotCompute();
47114688
LHS = removePointerBase(LHS);
47124689
RHS = removePointerBase(RHS);
@@ -4741,8 +4718,7 @@ const SCEV *ScalarEvolution::getMinusSCEV(const SCEV *LHS, const SCEV *RHS,
47414718
// larger scope than intended.
47424719
auto NegFlags = RHSIsNotMinSigned ? SCEV::FlagNSW : SCEV::FlagAnyWrap;
47434720

4744-
return getAddExprFromMismatchedTypes(LHS, getNegativeSCEV(RHS, NegFlags),
4745-
AddFlags, Depth);
4721+
return getAddExpr(LHS, getNegativeSCEV(RHS, NegFlags), AddFlags, Depth);
47464722
}
47474723

47484724
const SCEV *ScalarEvolution::getTruncateOrZeroExtend(const SCEV *V, Type *Ty,
@@ -4769,18 +4745,6 @@ const SCEV *ScalarEvolution::getTruncateOrSignExtend(const SCEV *V, Type *Ty,
47694745
return getSignExtendExpr(V, Ty, Depth);
47704746
}
47714747

4772-
const SCEV *ScalarEvolution::getTruncateOrAnyExtend(const SCEV *V, Type *Ty,
4773-
unsigned Depth) {
4774-
Type *SrcTy = V->getType();
4775-
assert(SrcTy->isIntOrPtrTy() && Ty->isIntOrPtrTy() &&
4776-
"Cannot truncate or any extend with non-integer arguments!");
4777-
if (getTypeSizeInBits(SrcTy) == getTypeSizeInBits(Ty))
4778-
return V; // No conversion
4779-
if (getTypeSizeInBits(SrcTy) > getTypeSizeInBits(Ty))
4780-
return getTruncateExpr(V, Ty, Depth);
4781-
return getAnyExtendExpr(V, Ty);
4782-
}
4783-
47844748
const SCEV *
47854749
ScalarEvolution::getNoopOrZeroExtend(const SCEV *V, Type *Ty) {
47864750
Type *SrcTy = V->getType();
@@ -4875,58 +4839,6 @@ ScalarEvolution::getUMinFromMismatchedTypes(SmallVectorImpl<const SCEV *> &Ops,
48754839
return getUMinExpr(PromotedOps, Sequential);
48764840
}
48774841

4878-
const SCEV *ScalarEvolution::getAddRecExprFromMismatchedTypes(
4879-
const SmallVectorImpl<const SCEV *> &Ops, const Loop *L,
4880-
SCEV::NoWrapFlags Flags) {
4881-
assert(!Ops.empty() && "At least one operand must be!");
4882-
// Trivial case.
4883-
if (Ops.size() == 1)
4884-
return Ops[0];
4885-
4886-
// Find the max type first.
4887-
Type *MaxType = nullptr;
4888-
for (const auto *S : Ops)
4889-
if (MaxType)
4890-
MaxType = getWiderType(MaxType, S->getType());
4891-
else
4892-
MaxType = S->getType();
4893-
assert(MaxType && "Failed to find maximum type!");
4894-
4895-
// Extend all ops to max type.
4896-
SmallVector<const SCEV *, 2> PromotedOps;
4897-
PromotedOps.reserve(Ops.size());
4898-
for (const auto *S : Ops)
4899-
PromotedOps.push_back(getNoopOrAnyExtend(S, MaxType));
4900-
4901-
return getAddRecExpr(PromotedOps, L, Flags);
4902-
}
4903-
4904-
const SCEV *ScalarEvolution::getAddExprFromMismatchedTypes(
4905-
const SmallVectorImpl<const SCEV *> &Ops, SCEV::NoWrapFlags Flags,
4906-
unsigned Depth) {
4907-
assert(!Ops.empty() && "At least one operand must be!");
4908-
// Trivial case.
4909-
if (Ops.size() == 1)
4910-
return Ops[0];
4911-
4912-
// Find the max type first.
4913-
Type *MaxType = nullptr;
4914-
for (const auto *S : Ops)
4915-
if (MaxType)
4916-
MaxType = getWiderType(MaxType, S->getType());
4917-
else
4918-
MaxType = S->getType();
4919-
assert(MaxType && "Failed to find maximum type!");
4920-
4921-
// Extend all ops to max type.
4922-
SmallVector<const SCEV *, 2> PromotedOps;
4923-
PromotedOps.reserve(Ops.size());
4924-
for (const auto *S : Ops)
4925-
PromotedOps.push_back(getNoopOrAnyExtend(S, MaxType));
4926-
4927-
return getAddExpr(PromotedOps, Flags, Depth);
4928-
}
4929-
49304842
const SCEV *ScalarEvolution::getPointerBase(const SCEV *V) {
49314843
// A pointer operand may evaluate to a nonpointer expression, such as null.
49324844
if (!V->getType()->isPointerTy())

llvm/lib/Analysis/ScalarEvolutionAliasAnalysis.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,10 @@ AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
6767
// Test whether the difference is known to be great enough that memory of
6868
// the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
6969
// are non-zero, which is special-cased above.
70-
if (!isa<SCEVCouldNotCompute>(BA)) {
71-
if (SE.isSCEVable(BA->getType()))
72-
BA = SE.getTruncateOrAnyExtend(BA, AS->getType());
73-
if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
74-
(-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))
75-
return AliasResult::NoAlias;
76-
}
70+
if (!isa<SCEVCouldNotCompute>(BA) &&
71+
ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
72+
(-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))
73+
return AliasResult::NoAlias;
7774

7875
// Folding the subtraction while preserving range information can be tricky
7976
// (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
@@ -85,13 +82,10 @@ AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
8582
// Test whether the difference is known to be great enough that memory of
8683
// the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
8784
// are non-zero, which is special-cased above.
88-
if (!isa<SCEVCouldNotCompute>(AB)) {
89-
if (SE.isSCEVable(AB->getType()))
90-
AB = SE.getTruncateOrAnyExtend(AB, AS->getType());
91-
if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
92-
(-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))
93-
return AliasResult::NoAlias;
94-
}
85+
if (!isa<SCEVCouldNotCompute>(AB) &&
86+
BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
87+
(-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))
88+
return AliasResult::NoAlias;
9589
}
9690

9791
// If ScalarEvolution can find an underlying object, form a new query.

llvm/test/Analysis/ScalarEvolution/scev-aa.ll

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -340,29 +340,3 @@ for.latch:
340340
for.end:
341341
ret void
342342
}
343-
344-
; CHECK-LABEL: Function: test_different_pointer_bases_of_inttoptr: 2 pointers, 0 call sites
345-
; CHECK: NoAlias: <16 x i8>* %tmp5, <16 x i8>* %tmp7
346-
347-
define void @test_different_pointer_bases_of_inttoptr() {
348-
entry:
349-
br label %for.body
350-
351-
for.body:
352-
%tmp = phi i32 [ %next, %for.body ], [ 1, %entry ]
353-
%tmp1 = shl nsw i32 %tmp, 1
354-
%tmp2 = add nuw nsw i32 %tmp1, %tmp1
355-
%tmp3 = mul nsw i32 %tmp2, 1408
356-
%tmp4 = add nsw i32 %tmp3, 1408
357-
%tmp5 = getelementptr inbounds i8, ptr inttoptr (i32 1024 to ptr), i32 %tmp1
358-
%tmp6 = load <16 x i8>, ptr %tmp5, align 1
359-
%tmp7 = getelementptr inbounds i8, ptr inttoptr (i32 4096 to ptr), i32 %tmp4
360-
store <16 x i8> %tmp6, ptr %tmp7, align 1
361-
362-
%next = add i32 %tmp, 2
363-
%exitcond = icmp slt i32 %next, 10
364-
br i1 %exitcond, label %for.body, label %for.end
365-
366-
for.end:
367-
ret void
368-
}

0 commit comments

Comments
 (0)