[IPSCCP] Infer attributes on arguments (#107114)

During inter-procedural SCCP, also infer attributes on arguments, not
just return values. This allows other non-interprocedural passes to make
use of the information later.
This commit is contained in:
Nikita Popov 2024-09-16 10:23:41 +02:00 committed by GitHub
parent b29c5b66fd
commit b7e51b4f13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 91 additions and 52 deletions

View File

@ -445,6 +445,9 @@ public:
/// gets the attribute from the list of attributes.
Attribute getAttributeAtIndex(unsigned i, StringRef Kind) const;
/// Check if attribute of the given kind is set at the given index.
bool hasAttributeAtIndex(unsigned Idx, Attribute::AttrKind Kind) const;
/// Return the attribute for the given attribute kind.
Attribute getFnAttribute(Attribute::AttrKind Kind) const;

View File

@ -104,6 +104,8 @@ public:
/// argument-tracked functions.
bool isArgumentTrackedFunction(Function *F);
const SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions() const;
/// Solve - Solve for constants and executable blocks.
void solve();
@ -191,6 +193,7 @@ public:
BasicBlock *&NewUnreachableBB) const;
void inferReturnAttributes() const;
void inferArgAttributes() const;
bool tryToReplaceWithConstant(Value *V);

View File

@ -765,6 +765,11 @@ Attribute Function::getAttributeAtIndex(unsigned i, StringRef Kind) const {
return AttributeSets.getAttributeAtIndex(i, Kind);
}
bool Function::hasAttributeAtIndex(unsigned Idx,
Attribute::AttrKind Kind) const {
return AttributeSets.hasAttributeAtIndex(Idx, Kind);
}
Attribute Function::getFnAttribute(Attribute::AttrKind Kind) const {
return AttributeSets.getFnAttr(Kind);
}

View File

@ -278,6 +278,7 @@ static bool runIPSCCP(
SmallVector<ReturnInst*, 8> ReturnsToZap;
Solver.inferReturnAttributes();
Solver.inferArgAttributes();
for (const auto &[F, ReturnValue] : Solver.getTrackedRetVals()) {
assert(!F->getReturnType()->isVoidTy() &&
"should not track void functions");

View File

@ -341,31 +341,45 @@ bool SCCPSolver::removeNonFeasibleEdges(BasicBlock *BB, DomTreeUpdater &DTU,
return true;
}
static void inferAttribute(Function *F, unsigned AttrIndex,
const ValueLatticeElement &Val) {
// If there is a known constant range for the value, add range attribute.
if (Val.isConstantRange() && !Val.getConstantRange().isSingleElement()) {
// Do not add range attribute if the value may include undef.
if (Val.isConstantRangeIncludingUndef())
return;
// Take the intersection of the existing attribute and the inferred range.
Attribute OldAttr = F->getAttributeAtIndex(AttrIndex, Attribute::Range);
ConstantRange CR = Val.getConstantRange();
if (OldAttr.isValid())
CR = CR.intersectWith(OldAttr.getRange());
F->addAttributeAtIndex(
AttrIndex, Attribute::get(F->getContext(), Attribute::Range, CR));
return;
}
// Infer nonnull attribute.
if (Val.isNotConstant() && Val.getNotConstant()->getType()->isPointerTy() &&
Val.getNotConstant()->isNullValue() &&
!F->hasAttributeAtIndex(AttrIndex, Attribute::NonNull)) {
F->addAttributeAtIndex(AttrIndex,
Attribute::get(F->getContext(), Attribute::NonNull));
}
}
void SCCPSolver::inferReturnAttributes() const {
for (const auto &[F, ReturnValue] : getTrackedRetVals()) {
for (const auto &[F, ReturnValue] : getTrackedRetVals())
inferAttribute(F, AttributeList::ReturnIndex, ReturnValue);
}
// If there is a known constant range for the return value, add range
// attribute to the return value.
if (ReturnValue.isConstantRange() &&
!ReturnValue.getConstantRange().isSingleElement()) {
// Do not add range metadata if the return value may include undef.
if (ReturnValue.isConstantRangeIncludingUndef())
continue;
// Take the intersection of the existing attribute and the inferred range.
ConstantRange CR = ReturnValue.getConstantRange();
if (F->hasRetAttribute(Attribute::Range))
CR = CR.intersectWith(F->getRetAttribute(Attribute::Range).getRange());
F->addRangeRetAttr(CR);
void SCCPSolver::inferArgAttributes() const {
for (Function *F : getArgumentTrackedFunctions()) {
if (!isBlockExecutable(&F->front()))
continue;
}
// Infer nonnull return attribute.
if (F->getReturnType()->isPointerTy() && ReturnValue.isNotConstant() &&
ReturnValue.getNotConstant()->isNullValue() &&
!F->hasRetAttribute(Attribute::NonNull)) {
F->addRetAttr(Attribute::NonNull);
continue;
}
for (Argument &A : F->args())
if (!A.getType()->isStructTy())
inferAttribute(F, AttributeList::FirstArgIndex + A.getArgNo(),
getLatticeValueFor(&A));
}
}
@ -766,6 +780,10 @@ public:
return TrackingIncomingArguments.count(F);
}
const SmallPtrSetImpl<Function *> &getArgumentTrackedFunctions() const {
return TrackingIncomingArguments;
}
void solve();
bool resolvedUndef(Instruction &I);
@ -2140,6 +2158,11 @@ bool SCCPSolver::isArgumentTrackedFunction(Function *F) {
return Visitor->isArgumentTrackedFunction(F);
}
const SmallPtrSetImpl<Function *> &
SCCPSolver::getArgumentTrackedFunctions() const {
return Visitor->getArgumentTrackedFunctions();
}
void SCCPSolver::solve() { Visitor->solve(); }
bool SCCPSolver::resolvedUndefsIn(Function &F) {

View File

@ -29,7 +29,7 @@ entry:
define internal i64 @foo(i64 %n, i1 %c1, i1 %c2, i1 %c3, i1 %c4, i1 %c5, i1 %c6, i1 %c7, i1 %c8, i1 %c9, i1 %c10) {
; NOFUNCSPEC-LABEL: define internal range(i64 2, 7) i64 @foo(
; NOFUNCSPEC-SAME: i64 [[N:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]], i1 [[C3:%.*]], i1 [[C4:%.*]], i1 [[C5:%.*]], i1 [[C6:%.*]], i1 [[C7:%.*]], i1 [[C8:%.*]], i1 [[C9:%.*]], i1 [[C10:%.*]]) {
; NOFUNCSPEC-SAME: i64 range(i64 3, 5) [[N:%.*]], i1 [[C1:%.*]], i1 [[C2:%.*]], i1 [[C3:%.*]], i1 [[C4:%.*]], i1 [[C5:%.*]], i1 [[C6:%.*]], i1 [[C7:%.*]], i1 [[C8:%.*]], i1 [[C9:%.*]], i1 [[C10:%.*]]) {
; NOFUNCSPEC-NEXT: entry:
; NOFUNCSPEC-NEXT: br i1 [[C1]], label [[L1:%.*]], label [[L9:%.*]]
; NOFUNCSPEC: l1:

View File

@ -6,7 +6,7 @@
; can be added to call sites.
define internal i32 @callee(i32 %x) {
; CHECK-LABEL: define internal range(i32 0, 21) i32 @callee(
; CHECK-SAME: i32 [[X:%.*]]) {
; CHECK-SAME: i32 range(i32 0, 21) [[X:%.*]]) {
; CHECK-NEXT: ret i32 [[X]]
;
ret i32 %x
@ -133,7 +133,7 @@ define void @caller_cb3() {
; should be added at call sites.
define internal i32 @callee5(i32 %x, i32 %y) {
; CHECK-LABEL: define internal i32 @callee5(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-SAME: i32 range(i32 10, 21) [[X:%.*]], i32 range(i32 100, 201) [[Y:%.*]]) {
; CHECK-NEXT: [[C:%.*]] = icmp slt i32 [[X]], 15
; CHECK-NEXT: br i1 [[C]], label [[BB1:%.*]], label [[BB2:%.*]]
; CHECK: bb1:

View File

@ -4,7 +4,7 @@
; Constant range for %a is [1, 48) and for %b is [301, 1000)
define internal i32 @f1(i32 %a, i32 %b) {
; CHECK-LABEL: define {{[^@]+}}@f1
; CHECK-SAME: (i32 [[A:%.*]], i32 [[B:%.*]]) {
; CHECK-SAME: (i32 range(i32 1, 48) [[A:%.*]], i32 range(i32 301, 1000) [[B:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i32 poison
;
@ -27,7 +27,7 @@ entry:
; Constant range for %x is [47, 302)
define internal i32 @f2(i32 %x) {
; CHECK-LABEL: define {{[^@]+}}@f2
; CHECK-SAME: (i32 [[X:%.*]]) {
; CHECK-SAME: (i32 range(i32 47, 302) [[X:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X]], 300
; CHECK-NEXT: [[CMP4:%.*]] = icmp ugt i32 [[X]], 300
@ -79,7 +79,7 @@ entry:
define internal i32 @f3(i32 %x) {
; CHECK-LABEL: define {{[^@]+}}@f3
; CHECK-SAME: (i32 [[X:%.*]]) {
; CHECK-SAME: (i32 range(i32 0, 2) [[X:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i32 poison
;
@ -116,7 +116,7 @@ end:
define internal i32 @f4(i32 %x) {
; CHECK-LABEL: define {{[^@]+}}@f4
; CHECK-SAME: (i32 [[X:%.*]]) {
; CHECK-SAME: (i32 range(i32 301, -2147483648) [[X:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i32 poison
;
@ -170,7 +170,7 @@ entry:
define internal i1 @test_unreachable_callee(i32 %a) {
; CHECK-LABEL: define {{[^@]+}}@test_unreachable_callee
; CHECK-SAME: (i32 [[A:%.*]]) {
; CHECK-SAME: (i32 range(i32 1, 3) [[A:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: ret i1 poison
;
@ -199,7 +199,7 @@ define double @test_struct({ double, double } %test) {
; Constant range for %x is [47, 302)
define internal i32 @f5(i32 %x) {
; CHECK-LABEL: define {{[^@]+}}@f5
; CHECK-SAME: (i32 [[X:%.*]]) {
; CHECK-SAME: (i32 range(i32 47, 302) [[X:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X]], undef
; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 undef, [[X]]
@ -282,7 +282,7 @@ entry:
define internal i32 @callee6.1(i32 %i) {
; CHECK-LABEL: define {{[^@]+}}@callee6.1
; CHECK-SAME: (i32 [[I:%.*]]) {
; CHECK-SAME: (i32 range(i32 30, 44) [[I:%.*]]) {
; CHECK-NEXT: [[RES:%.*]] = call i32 @callee6.2(i32 [[I]])
; CHECK-NEXT: ret i32 poison
;
@ -292,7 +292,7 @@ define internal i32 @callee6.1(i32 %i) {
define internal i32 @callee6.2(i32 %i) {
; CHECK-LABEL: define {{[^@]+}}@callee6.2
; CHECK-SAME: (i32 [[I:%.*]]) {
; CHECK-SAME: (i32 range(i32 30, 44) [[I:%.*]]) {
; CHECK-NEXT: br label [[IF_THEN:%.*]]
; CHECK: if.then:
; CHECK-NEXT: ret i32 poison

View File

@ -4,7 +4,7 @@
; x = [100, 301)
define internal i1 @f.trunc(i32 %x) {
; CHECK-LABEL: define internal i1 @f.trunc(
; CHECK-SAME: i32 [[X:%.*]]) {
; CHECK-SAME: i32 range(i32 100, 301) [[X:%.*]]) {
; CHECK-NEXT: [[T_1:%.*]] = trunc nuw nsw i32 [[X]] to i16
; CHECK-NEXT: [[C_2:%.*]] = icmp sgt i16 [[T_1]], 299
; CHECK-NEXT: [[C_4:%.*]] = icmp slt i16 [[T_1]], 101
@ -60,7 +60,7 @@ define i1 @caller1() {
; x = [100, 301)
define internal i1 @f.zext(i32 %x, i32 %y) {
; CHECK-LABEL: define internal i1 @f.zext(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-SAME: i32 range(i32 100, 301) [[X:%.*]], i32 range(i32 -120, 901) [[Y:%.*]]) {
; CHECK-NEXT: [[T_1:%.*]] = zext nneg i32 [[X]] to i64
; CHECK-NEXT: [[C_2:%.*]] = icmp sgt i64 [[T_1]], 299
; CHECK-NEXT: [[C_4:%.*]] = icmp slt i64 [[T_1]], 101
@ -114,7 +114,7 @@ define i1 @caller.zext() {
; x = [100, 301)
define internal i1 @f.sext(i32 %x, i32 %y) {
; CHECK-LABEL: define internal i1 @f.sext(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-SAME: i32 range(i32 100, 301) [[X:%.*]], i32 range(i32 -120, 901) [[Y:%.*]]) {
; CHECK-NEXT: [[T_1:%.*]] = zext nneg i32 [[X]] to i64
; CHECK-NEXT: [[C_2:%.*]] = icmp sgt i64 [[T_1]], 299
; CHECK-NEXT: [[C_4:%.*]] = icmp slt i64 [[T_1]], 101
@ -166,7 +166,7 @@ define i1 @caller.sext() {
; There's nothing we can do besides going to the full range or overdefined.
define internal i1 @f.fptosi(i32 %x) {
; CHECK-LABEL: define internal i1 @f.fptosi(
; CHECK-SAME: i32 [[X:%.*]]) {
; CHECK-SAME: i32 range(i32 100, 301) [[X:%.*]]) {
; CHECK-NEXT: [[TO_DOUBLE:%.*]] = uitofp nneg i32 [[X]] to double
; CHECK-NEXT: [[ADD:%.*]] = fadd double 0.000000e+00, [[TO_DOUBLE]]
; CHECK-NEXT: [[TO_I32:%.*]] = fptosi double [[ADD]] to i32
@ -208,7 +208,7 @@ define i1 @caller.fptosi() {
; There's nothing we can do besides going to the full range or overdefined.
define internal i1 @f.fpext(i16 %x) {
; CHECK-LABEL: define internal i1 @f.fpext(
; CHECK-SAME: i16 [[X:%.*]]) {
; CHECK-SAME: i16 range(i16 100, 301) [[X:%.*]]) {
; CHECK-NEXT: [[TO_FLOAT:%.*]] = uitofp nneg i16 [[X]] to float
; CHECK-NEXT: [[TO_DOUBLE:%.*]] = fpext float [[TO_FLOAT]] to double
; CHECK-NEXT: [[TO_I64:%.*]] = fptoui float [[TO_FLOAT]] to i64
@ -251,7 +251,7 @@ define i1 @caller.fpext() {
; There's nothing we can do besides going to the full range or overdefined.
define internal i1 @f.inttoptr.ptrtoint(i64 %x) {
; CHECK-LABEL: define internal i1 @f.inttoptr.ptrtoint(
; CHECK-SAME: i64 [[X:%.*]]) {
; CHECK-SAME: i64 range(i64 100, 301) [[X:%.*]]) {
; CHECK-NEXT: [[TO_PTR:%.*]] = inttoptr i64 [[X]] to ptr
; CHECK-NEXT: [[TO_I64:%.*]] = ptrtoint ptr [[TO_PTR]] to i64
; CHECK-NEXT: [[C_1:%.*]] = icmp sgt i64 [[TO_I64]], 300
@ -325,7 +325,7 @@ entry:
define internal i64 @f.sext_to_zext(i32 %t) {
; CHECK-LABEL: define internal range(i64 0, 2) i64 @f.sext_to_zext(
; CHECK-SAME: i32 [[T:%.*]]) {
; CHECK-SAME: i32 range(i32 0, 2) [[T:%.*]]) {
; CHECK-NEXT: [[A:%.*]] = zext nneg i32 [[T]] to i64
; CHECK-NEXT: ret i64 [[A]]
;

View File

@ -3,7 +3,7 @@
define internal i32 @f1(i32 %x) {
; CHECK-LABEL: define {{[^@]+}}@f1
; CHECK-SAME: (i32 [[X:%.*]]) {
; CHECK-SAME: (i32 range(i32 0, 2) [[X:%.*]]) {
; CHECK-NEXT: ret i32 poison
;
%cmp = icmp sgt i32 %x, 300
@ -40,7 +40,7 @@ end:
define internal i32 @f2(i32 %x, i32 %y, i32 %z, i1 %cmp.1, i1 %cmp.2) {
; CHECK-LABEL: define {{[^@]+}}@f2
; CHECK-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], i32 [[Z:%.*]], i1 [[CMP_1:%.*]], i1 [[CMP_2:%.*]]) {
; CHECK-SAME: (i32 range(i32 0, 2) [[X:%.*]], i32 range(i32 -10, 2) [[Y:%.*]], i32 range(i32 1, 11) [[Z:%.*]], i1 [[CMP_1:%.*]], i1 [[CMP_2:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[CMP_1]], label [[IF_TRUE_1:%.*]], label [[END:%.*]]
; CHECK: if.true.1:
@ -133,7 +133,7 @@ end:
define internal i32 @f3(i32 %x, i32 %y, i1 %cmp.1) {
; CHECK-LABEL: define {{[^@]+}}@f3
; CHECK-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[CMP_1:%.*]]) {
; CHECK-SAME: (i32 range(i32 0, 6) [[X:%.*]], i32 [[Y:%.*]], i1 [[CMP_1:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: br i1 [[CMP_1]], label [[IF_TRUE_1:%.*]], label [[END:%.*]]
; CHECK: if.true.1:

View File

@ -18,7 +18,7 @@ define void @caller.1(ptr %arg) {
define internal i32 @callee.1(i32 %arg) {
; CHECK-LABEL: define {{[^@]+}}@callee.1
; CHECK-SAME: (i32 [[ARG:%.*]]) {
; CHECK-SAME: (i32 range(i32 2, 5) [[ARG:%.*]]) {
; CHECK-NEXT: [[SEL:%.*]] = select i1 false, i32 16, i32 [[ARG]]
; CHECK-NEXT: br label [[BB10:%.*]]
; CHECK: bb10:
@ -40,7 +40,7 @@ declare void @use(i32)
define internal i1 @f1(i32 %x, i32 %y, i1 %cmp) {
; CHECK-LABEL: define {{[^@]+}}@f1
; CHECK-SAME: (i32 [[X:%.*]], i32 [[Y:%.*]], i1 [[CMP:%.*]]) {
; CHECK-SAME: (i32 range(i32 10, 21) [[X:%.*]], i32 range(i32 100, 201) [[Y:%.*]], i1 [[CMP:%.*]]) {
; CHECK-NEXT: [[SEL_1:%.*]] = select i1 [[CMP]], i32 [[X]], i32 [[Y]]
; CHECK-NEXT: [[C_2:%.*]] = icmp sgt i32 [[SEL_1]], 100
; CHECK-NEXT: [[C_3:%.*]] = icmp eq i32 [[SEL_1]], 50

View File

@ -71,7 +71,7 @@ define internal ptr @no_side_effects(i8 %v) readonly nounwind willreturn {
; return value should stay as it is, and should not be zapped.
define internal ptr @dont_zap_me(i8 %v) {
; CHECK-LABEL: define {{[^@]+}}@dont_zap_me
; CHECK-SAME: (i8 [[V:%.*]]) {
; CHECK-SAME: (i8 range(i8 2, 0) [[V:%.*]]) {
; CHECK-NEXT: [[I1:%.*]] = call i32 @external()
; CHECK-NEXT: ret ptr null
;

View File

@ -248,9 +248,13 @@ define ptr @ret_maybe_null_pointer(ptr %p) {
}
define internal void @ip_nonnull_arg_callee(ptr %p) {
; CHECK-LABEL: define internal void @ip_nonnull_arg_callee(
; CHECK-SAME: ptr [[P:%.*]]) {
; CHECK-NEXT: ret void
; SCCP-LABEL: define internal void @ip_nonnull_arg_callee(
; SCCP-SAME: ptr [[P:%.*]]) {
; SCCP-NEXT: ret void
;
; IPSCCP-LABEL: define internal void @ip_nonnull_arg_callee(
; IPSCCP-SAME: ptr nonnull [[P:%.*]]) {
; IPSCCP-NEXT: ret void
;
ret void
}

View File

@ -56,7 +56,7 @@ entry:
define internal i1 @test1_g(ptr %h, i32 %i) #0 {
; CHECK-LABEL: define {{[^@]+}}@test1_g
; CHECK-SAME: (ptr [[H:%.*]], i32 [[I:%.*]]) {
; CHECK-SAME: (ptr [[H:%.*]], i32 range(i32 0, 2) [[I:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[I]], 0
; CHECK-NEXT: br i1 [[TOBOOL]], label [[LAND_RHS:%.*]], label [[LAND_END:%.*]]
@ -221,7 +221,7 @@ exit:
define internal i1 @test3_g(ptr %h, i32 %i) {
; CHECK-LABEL: define {{[^@]+}}@test3_g
; CHECK-SAME: (ptr [[H:%.*]], i32 [[I:%.*]]) {
; CHECK-SAME: (ptr [[H:%.*]], i32 range(i32 0, 2) [[I:%.*]]) {
; CHECK-NEXT: entry:
; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32 [[I]], 0
; CHECK-NEXT: br i1 [[TOBOOL]], label [[LAND_RHS:%.*]], label [[LAND_END:%.*]]

View File

@ -207,7 +207,7 @@ switch.2:
; range information.
define internal i32 @test_ip_range(i32 %x) {
; CHECK-LABEL: define internal range(i32 1, 4) i32 @test_ip_range(
; CHECK-SAME: i32 [[X:%.*]]) {
; CHECK-SAME: i32 range(i32 1, 4) [[X:%.*]]) {
; CHECK-NEXT: switch i32 [[X]], label [[DEFAULT_UNREACHABLE:%.*]] [
; CHECK-NEXT: i32 3, label [[SWITCH_3:%.*]]
; CHECK-NEXT: i32 1, label [[SWITCH_1:%.*]]