Skip to content

Commit 8bbbbfb

Browse files
committed
[SCEV] Add SCEVPtrToAddr, use when computing backedge taken counts.
Add support for PtrToAddr to SCEV and use it to compute pointer difference when computing backedge taken counts. This patch retains SCEVPtrToInt, which is mostly used to expressions comping directly from IR. PtrToAddr more closely matches the semantics of most uses in SCEV. See #156978 for some related discussion. The patch still retains SCEVPtrToInt, as it is used for cases where a SCEV expression is used in IR by a ptrtoint, e.g. https://github.com/llvm/llvm-project/blob/main/llvm/test/Transforms/IndVarSimplify/pr59633.ll. Some (or perhaps all, still need to check) should probably just be SCEVUnknown.
1 parent e25e25d commit 8bbbbfb

File tree

47 files changed

+387
-273
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+387
-273
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,13 @@ class ScalarEvolution {
563563
LLVM_ABI const SCEV *getConstant(ConstantInt *V);
564564
LLVM_ABI const SCEV *getConstant(const APInt &Val);
565565
LLVM_ABI const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false);
566-
LLVM_ABI const SCEV *getLosslessPtrToIntExpr(const SCEV *Op,
567-
unsigned Depth = 0);
566+
LLVM_ABI const SCEV *getLosslessPtrToIntOrAddrExpr(SCEVTypes Kind,
567+
const SCEV *Op,
568+
unsigned Depth = 0);
569+
LLVM_ABI const SCEV *getLosslessPtrToIntExpr(const SCEV *Op);
570+
LLVM_ABI const SCEV *getLosslessPtrToAddrExpr(const SCEV *Op);
571+
572+
LLVM_ABI const SCEV *getPtrToAddrExpr(const SCEV *Op, Type *Ty);
568573
LLVM_ABI const SCEV *getPtrToIntExpr(const SCEV *Op, Type *Ty);
569574
LLVM_ABI const SCEV *getTruncateExpr(const SCEV *Op, Type *Ty,
570575
unsigned Depth = 0);

llvm/include/llvm/Analysis/ScalarEvolutionDivision.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ struct SCEVDivision : public SCEVVisitor<SCEVDivision, void> {
4646

4747
// Except in the trivial case described above, we do not know how to divide
4848
// Expr by Denominator for the following functions with empty implementation.
49+
void visitPtrToAddrExpr(const SCEVPtrToAddrExpr *Numerator) {}
4950
void visitPtrToIntExpr(const SCEVPtrToIntExpr *Numerator) {}
5051
void visitTruncateExpr(const SCEVTruncateExpr *Numerator) {}
5152
void visitZeroExtendExpr(const SCEVZeroExtendExpr *Numerator) {}

llvm/include/llvm/Analysis/ScalarEvolutionExpressions.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ enum SCEVTypes : unsigned short {
5353
scSMinExpr,
5454
scSequentialUMinExpr,
5555
scPtrToInt,
56+
scPtrToAddr,
5657
scUnknown,
5758
scCouldNotCompute
5859
};
@@ -121,8 +122,9 @@ class SCEVCastExpr : public SCEV {
121122

122123
/// Methods for support type inquiry through isa, cast, and dyn_cast:
123124
static bool classof(const SCEV *S) {
124-
return S->getSCEVType() == scPtrToInt || S->getSCEVType() == scTruncate ||
125-
S->getSCEVType() == scZeroExtend || S->getSCEVType() == scSignExtend;
125+
return S->getSCEVType() == scPtrToAddr || S->getSCEVType() == scPtrToInt ||
126+
S->getSCEVType() == scTruncate || S->getSCEVType() == scZeroExtend ||
127+
S->getSCEVType() == scSignExtend;
126128
}
127129
};
128130

@@ -138,6 +140,18 @@ class SCEVPtrToIntExpr : public SCEVCastExpr {
138140
static bool classof(const SCEV *S) { return S->getSCEVType() == scPtrToInt; }
139141
};
140142

143+
/// This class represents a cast from a pointer to a pointer-sized integer
144+
/// value, without capturing the provenance of the pointer.
145+
class SCEVPtrToAddrExpr : public SCEVCastExpr {
146+
friend class ScalarEvolution;
147+
148+
SCEVPtrToAddrExpr(const FoldingSetNodeIDRef ID, const SCEV *Op, Type *ITy);
149+
150+
public:
151+
/// Methods for support type inquiry through isa, cast, and dyn_cast:
152+
static bool classof(const SCEV *S) { return S->getSCEVType() == scPtrToAddr; }
153+
};
154+
141155
/// This is the base class for unary integral cast operator classes.
142156
class SCEVIntegralCastExpr : public SCEVCastExpr {
143157
protected:
@@ -615,6 +629,8 @@ template <typename SC, typename RetVal = void> struct SCEVVisitor {
615629
return ((SC *)this)->visitConstant((const SCEVConstant *)S);
616630
case scVScale:
617631
return ((SC *)this)->visitVScale((const SCEVVScale *)S);
632+
case scPtrToAddr:
633+
return ((SC *)this)->visitPtrToAddrExpr((const SCEVPtrToAddrExpr *)S);
618634
case scPtrToInt:
619635
return ((SC *)this)->visitPtrToIntExpr((const SCEVPtrToIntExpr *)S);
620636
case scTruncate:
@@ -685,6 +701,7 @@ template <typename SV> class SCEVTraversal {
685701
case scVScale:
686702
case scUnknown:
687703
continue;
704+
case scPtrToAddr:
688705
case scPtrToInt:
689706
case scTruncate:
690707
case scZeroExtend:
@@ -774,6 +791,13 @@ class SCEVRewriteVisitor : public SCEVVisitor<SC, const SCEV *> {
774791

775792
const SCEV *visitVScale(const SCEVVScale *VScale) { return VScale; }
776793

794+
const SCEV *visitPtrToAddrExpr(const SCEVPtrToAddrExpr *Expr) {
795+
const SCEV *Operand = ((SC *)this)->visit(Expr->getOperand());
796+
return Operand == Expr->getOperand()
797+
? Expr
798+
: SE.getPtrToAddrExpr(Operand, Expr->getType());
799+
}
800+
777801
const SCEV *visitPtrToIntExpr(const SCEVPtrToIntExpr *Expr) {
778802
const SCEV *Operand = ((SC *)this)->visit(Expr->getOperand());
779803
return Operand == Expr->getOperand()

llvm/include/llvm/Transforms/Utils/ScalarEvolutionExpander.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,8 @@ class SCEVExpander : public SCEVVisitor<SCEVExpander, Value *> {
498498

499499
Value *visitVScale(const SCEVVScale *S);
500500

501+
Value *visitPtrToAddrExpr(const SCEVPtrToAddrExpr *S);
502+
501503
Value *visitPtrToIntExpr(const SCEVPtrToIntExpr *S);
502504

503505
Value *visitTruncateExpr(const SCEVTruncateExpr *S);

0 commit comments

Comments
 (0)