Skip to content

Commit 72106d3

Browse files
committed
[ValueTracking] Address review comments.
1 parent b90c7a2 commit 72106d3

File tree

4 files changed

+28
-32
lines changed

4 files changed

+28
-32
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,43 +2652,42 @@ static bool isKnownNonNullFromDominatingCondition(const Value *V,
26522652
return false;
26532653

26542654
unsigned NumUsesExplored = 0;
2655-
for (const auto *U : V->users()) {
2655+
for (auto &U : V->uses()) {
26562656
// Avoid massive lists
26572657
if (NumUsesExplored >= DomConditionsMaxUses)
26582658
break;
26592659
NumUsesExplored++;
26602660

2661+
const Instruction *UI = cast<Instruction>(U.getUser());
26612662
// If the value is used as an argument to a call or invoke, then argument
26622663
// attributes may provide an answer about null-ness.
26632664
if (V->getType()->isPointerTy()) {
2664-
if (const auto *CB = dyn_cast<CallBase>(U))
2665-
if (auto *CalledFunc = CB->getCalledFunction())
2666-
for (const Argument &Arg : CalledFunc->args())
2667-
if (CB->getArgOperand(Arg.getArgNo()) == V &&
2668-
CB->paramHasNonNullAttr(Arg.getArgNo(),
2669-
/*AllowUndefOrPoison=*/false) &&
2670-
DT->dominates(CB, CtxI))
2671-
return true;
2665+
if (const auto *CB = dyn_cast<CallBase>(UI)) {
2666+
if (CB->isArgOperand(&U) &&
2667+
CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2668+
/*AllowUndefOrPoison=*/false) &&
2669+
DT->dominates(CB, CtxI))
2670+
return true;
2671+
}
26722672
}
26732673

26742674
// If the value is used as a load/store, then the pointer must be non null.
2675-
if (V == getLoadStorePointerOperand(U)) {
2676-
const Instruction *I = cast<Instruction>(U);
2677-
if (!NullPointerIsDefined(I->getFunction(),
2675+
if (V == getLoadStorePointerOperand(UI)) {
2676+
if (!NullPointerIsDefined(UI->getFunction(),
26782677
V->getType()->getPointerAddressSpace()) &&
2679-
DT->dominates(I, CtxI))
2678+
DT->dominates(UI, CtxI))
26802679
return true;
26812680
}
26822681

2683-
if ((match(U, m_IDiv(m_Value(), m_Specific(V))) ||
2684-
match(U, m_IRem(m_Value(), m_Specific(V)))) &&
2685-
isValidAssumeForContext(cast<Instruction>(U), CtxI, DT))
2682+
if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2683+
match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2684+
isValidAssumeForContext(UI, CtxI, DT))
26862685
return true;
26872686

26882687
// Consider only compare instructions uniquely controlling a branch
26892688
Value *RHS;
26902689
CmpPredicate Pred;
2691-
if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2690+
if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
26922691
continue;
26932692

26942693
bool NonNullIfTrue;
@@ -2701,7 +2700,7 @@ static bool isKnownNonNullFromDominatingCondition(const Value *V,
27012700

27022701
SmallVector<const User *, 4> WorkList;
27032702
SmallPtrSet<const User *, 4> Visited;
2704-
for (const auto *CmpU : U->users()) {
2703+
for (const auto *CmpU : UI->users()) {
27052704
assert(WorkList.empty() && "Should be!");
27062705
if (Visited.insert(CmpU).second)
27072706
WorkList.push_back(CmpU);

llvm/lib/IR/Instructions.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,7 @@ bool CallBase::paramHasNonNullAttr(unsigned ArgNo,
440440
(AllowUndefOrPoison || paramHasAttr(ArgNo, Attribute::NoUndef)))
441441
return true;
442442

443-
Attribute Attr = getParamAttr(ArgNo, Attribute::Dereferenceable);
444-
if (Attr.isValid() && Attr.getDereferenceableBytes() > 0 &&
443+
if (getParamDereferenceableBytes(ArgNo) > 0 &&
445444
!NullPointerIsDefined(
446445
getCaller(),
447446
getArgOperand(ArgNo)->getType()->getPointerAddressSpace()))

llvm/test/Transforms/InstCombine/strncpy-4.ll

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ define void @fold_strncpy_overlap(ptr %dst, i64 %n) {
4545
define void @call_strncpy_overlap(ptr %dst, i64 %n) {
4646
; CHECK-LABEL: @call_strncpy_overlap(
4747
; CHECK-NEXT: [[ED_2:%.*]] = call ptr @strncpy(ptr noundef nonnull dereferenceable(1) [[DST:%.*]], ptr noundef nonnull dereferenceable(1) [[DST]], i64 2)
48-
; CHECK-NEXT: call void @sink(ptr [[DST]], ptr [[ED_2]])
48+
; CHECK-NEXT: call void @sink(ptr nonnull [[DST]], ptr [[ED_2]])
4949
; CHECK-NEXT: [[ED_3:%.*]] = call ptr @strncpy(ptr noundef nonnull dereferenceable(1) [[DST]], ptr noundef nonnull dereferenceable(1) [[DST]], i64 3)
50-
; CHECK-NEXT: call void @sink(ptr [[DST]], ptr [[ED_3]])
51-
; CHECK-NEXT: [[ED_N:%.*]] = call ptr @strncpy(ptr [[DST]], ptr [[DST]], i64 [[N:%.*]])
52-
; CHECK-NEXT: call void @sink(ptr [[DST]], ptr [[ED_N]])
50+
; CHECK-NEXT: call void @sink(ptr nonnull [[DST]], ptr [[ED_3]])
51+
; CHECK-NEXT: [[ED_N:%.*]] = call ptr @strncpy(ptr nonnull [[DST]], ptr nonnull [[DST]], i64 [[N:%.*]])
52+
; CHECK-NEXT: call void @sink(ptr nonnull [[DST]], ptr [[ED_N]])
5353
; CHECK-NEXT: ret void
5454
;
5555

@@ -141,11 +141,11 @@ define void @fold_strncpy_s(ptr %dst, ptr %src, i64 %n) {
141141
define void @call_strncpy_s(ptr %dst, ptr %src, i64 %n) {
142142
; CHECK-LABEL: @call_strncpy_s(
143143
; CHECK-NEXT: [[ED_2:%.*]] = call ptr @strncpy(ptr noundef nonnull dereferenceable(1) [[DST:%.*]], ptr noundef nonnull dereferenceable(1) [[SRC:%.*]], i64 2)
144-
; CHECK-NEXT: call void @sink(ptr [[DST]], ptr [[ED_2]])
144+
; CHECK-NEXT: call void @sink(ptr nonnull [[DST]], ptr [[ED_2]])
145145
; CHECK-NEXT: [[ED_9:%.*]] = call ptr @strncpy(ptr noundef nonnull dereferenceable(1) [[DST]], ptr noundef nonnull dereferenceable(1) [[SRC]], i64 9)
146-
; CHECK-NEXT: call void @sink(ptr [[DST]], ptr [[ED_9]])
147-
; CHECK-NEXT: [[ED_N:%.*]] = call ptr @strncpy(ptr [[DST]], ptr [[SRC]], i64 [[N:%.*]])
148-
; CHECK-NEXT: call void @sink(ptr [[DST]], ptr [[ED_N]])
146+
; CHECK-NEXT: call void @sink(ptr nonnull [[DST]], ptr [[ED_9]])
147+
; CHECK-NEXT: [[ED_N:%.*]] = call ptr @strncpy(ptr nonnull [[DST]], ptr nonnull [[SRC]], i64 [[N:%.*]])
148+
; CHECK-NEXT: call void @sink(ptr nonnull [[DST]], ptr [[ED_N]])
149149
; CHECK-NEXT: ret void
150150
;
151151
; Do not transform strncpy(D, S, 2) when S is unknown. Both *D and *S must

llvm/test/Transforms/InstSimplify/known-non-zero-opaque-ptrs.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ declare void @two_args(ptr, ptr)
88
define i1 @test_zero_args_nonnull(ptr %p) {
99
; CHECK-LABEL: @test_zero_args_nonnull(
1010
; CHECK-NEXT: call void @zero_args(ptr noundef nonnull [[P:%.*]])
11-
; CHECK-NEXT: [[C:%.*]] = icmp ne ptr [[P]], null
12-
; CHECK-NEXT: ret i1 [[C]]
11+
; CHECK-NEXT: ret i1 true
1312
;
1413
call void @zero_args(ptr nonnull noundef %p)
1514
%c = icmp ne ptr %p, null
@@ -31,8 +30,7 @@ define i1 @test_zero_args_maybe_null(ptr %p) {
3130
define i1 @test_two_args_nonnull(ptr %p) {
3231
; CHECK-LABEL: @test_two_args_nonnull(
3332
; CHECK-NEXT: call void @two_args(ptr noundef nonnull [[P:%.*]])
34-
; CHECK-NEXT: [[C:%.*]] = icmp ne ptr [[P]], null
35-
; CHECK-NEXT: ret i1 [[C]]
33+
; CHECK-NEXT: ret i1 true
3634
;
3735
call void @two_args(ptr nonnull noundef %p)
3836
%c = icmp ne ptr %p, null

0 commit comments

Comments
 (0)