diff --git a/llvm/include/llvm/Analysis/AssumeBundleQueries.h b/llvm/include/llvm/Analysis/AssumeBundleQueries.h --- a/llvm/include/llvm/Analysis/AssumeBundleQueries.h +++ b/llvm/include/llvm/Analysis/AssumeBundleQueries.h @@ -15,7 +15,9 @@ #define LLVM_TRANSFORMS_UTILS_ASSUMEBUNDLEQUERIES_H #include "llvm/IR/Attributes.h" -#include "llvm/IR/Instructions.h" +#include "llvm/IR/Instruction.h" +#include "llvm/IR/PassManager.h" +#include "llvm/IR/Dominators.h" #include "llvm/ADT/DenseMap.h" namespace llvm { @@ -116,6 +118,8 @@ Attribute::AttrKind AttrKind = Attribute::None; Value *WasOn = nullptr; unsigned ArgValue = 0; + operator bool() const { return AttrKind != Attribute::None; } + static RetainedKnowledge none() { return RetainedKnowledge{}; } }; /// Retreive the information help by Assume on the operand at index Idx. @@ -140,6 +144,25 @@ /// function returned true. bool isAssumeWithEmptyBundle(CallInst &Assume); +/// return a valid Knowledge associated to the Use U if its Attribute kind is +/// in AttrKinds +RetainedKnowledge getKnowledgeFromUse(const Use *U, + ArrayRef AttrKinds); + +/// return a valid Knowledge associated to the Value V if its Attribute kind is +/// in AttrKinds and it matches the Filter. +RetainedKnowledge getKnowledgeForValue( + const Value *V, ArrayRef AttrKinds, + llvm::function_ref Filter = + [](RetainedKnowledge, Instruction *) { return true; }); + +/// return a valid Knowledge associated to the Value V if its Attribute kind is +/// in AttrKinds and the knowledge is suitable to be used in the context of +/// CtxI. +RetainedKnowledge getKnowledgeValidInContext( + const Value *V, ArrayRef AttrKinds, + const Instruction *CtxI, const DominatorTree *DT = nullptr); + } // namespace llvm #endif diff --git a/llvm/lib/Analysis/AssumeBundleQueries.cpp b/llvm/lib/Analysis/AssumeBundleQueries.cpp --- a/llvm/lib/Analysis/AssumeBundleQueries.cpp +++ b/llvm/lib/Analysis/AssumeBundleQueries.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "llvm/Analysis/AssumeBundleQueries.h" +#include "llvm/Analysis/ValueTracking.h" #include "llvm/IR/Function.h" #include "llvm/IR/InstIterator.h" #include "llvm/IR/IntrinsicInst.h" @@ -118,3 +119,39 @@ return BOI.Tag->getKey() != "ignore"; }); } + +RetainedKnowledge +llvm::getKnowledgeFromUse(const Use *U, + ArrayRef AttrKinds) { + auto *Intr = dyn_cast(U->getUser()); + if (!Intr || Intr->getIntrinsicID() != Intrinsic::assume || + &*Intr->args().begin() == U) + return RetainedKnowledge::none(); + RetainedKnowledge RK = + getKnowledgeFromOperandInAssume(*Intr, U->getOperandNo()); + for (auto Attr : AttrKinds) + if (Attr == RK.AttrKind) + return RK; + return RetainedKnowledge::none(); +} + +RetainedKnowledge llvm::getKnowledgeForValue( + const Value *V, ArrayRef AttrKinds, + llvm::function_ref Filter) { + for (auto &U : V->uses()) { + if (RetainedKnowledge RK = getKnowledgeFromUse(&U, AttrKinds)) + if (Filter(RK, cast(U.getUser()))) + return RK; + } + return RetainedKnowledge::none(); +} + +RetainedKnowledge +llvm::getKnowledgeValidInContext(const Value *V, + ArrayRef AttrKinds, + const Instruction *CtxI, const DominatorTree *DT) { + return getKnowledgeForValue(V, AttrKinds, + [&](RetainedKnowledge, Instruction *I) { + return isValidAssumeForContext(I, CtxI, DT); + }); +} diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -24,6 +24,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/ADT/iterator_range.h" #include "llvm/Analysis/AliasAnalysis.h" +#include "llvm/Analysis/AssumeBundleQueries.h" #include "llvm/Analysis/AssumptionCache.h" #include "llvm/Analysis/GuardUtils.h" #include "llvm/Analysis/InstructionSimplify.h" @@ -693,6 +694,16 @@ return !TrueValues.contains(APInt::getNullValue(CI->getBitWidth())); }; + if (Q.CxtI && V->getType()->isPointerTy()) { + SmallVector AttrKinds{Attribute::NonNull}; + if (!llvm::NullPointerIsDefined(Q.CxtI->getFunction(), + V->getType()->getPointerAddressSpace())) + AttrKinds.push_back(Attribute::Dereferenceable); + + if (getKnowledgeValidInContext(V, AttrKinds, Q.CxtI, Q.DT)) + return true; + } + for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { if (!AssumeVH) continue; diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -20,6 +20,7 @@ #include "llvm/ADT/SmallPtrSet.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/Statistic.h" +#include "llvm/Analysis/AssumeBundleQueries.h" #include "llvm/Analysis/CallGraph.h" #include "llvm/Analysis/CallGraphSCCPass.h" #include "llvm/Analysis/CaptureTracking.h" @@ -2021,8 +2022,15 @@ F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true; const DataLayout &DL = A.getInfoCache().getDL(); if (ImmutableCallSite ICS = ImmutableCallSite(I)) { - if (ICS.isBundleOperand(U)) + if (ICS.isBundleOperand(U)) { + if (RetainedKnowledge RK = getKnowledgeFromUse( + U, {Attribute::NonNull, Attribute::Dereferenceable})) { + IsNonNull |= + (RK.AttrKind == Attribute::NonNull || !NullPointerIsDefined); + return RK.ArgValue; + } return 0; + } if (ICS.isCallee(U)) { IsNonNull |= !NullPointerIsDefined; diff --git a/llvm/test/Analysis/ValueTracking/assume.ll b/llvm/test/Analysis/ValueTracking/assume.ll --- a/llvm/test/Analysis/ValueTracking/assume.ll +++ b/llvm/test/Analysis/ValueTracking/assume.ll @@ -21,9 +21,14 @@ define void @assume_not() { ; CHECK-LABEL: @assume_not( +; CHECK-NEXT: entry-block: +; CHECK-NEXT: [[TMP0:%.*]] = call i1 @get_val() +; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[TMP0]], true +; CHECK-NEXT: call void @llvm.assume(i1 [[TMP1]]) +; CHECK-NEXT: ret void +; entry-block: %0 = call i1 @get_val() -; CHECK: call void @llvm.assume %1 = xor i1 %0, true call void @llvm.assume(i1 %1) ret void @@ -31,3 +36,92 @@ declare i1 @get_val() declare void @llvm.assume(i1) + +define dso_local i1 @test1(i32* readonly %0) { +; CHECK-LABEL: @test1( +; CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(i32* [[TMP0:%.*]]) ] +; CHECK-NEXT: ret i1 false +; + call void @llvm.assume(i1 true) ["nonnull"(i32* %0)] + %2 = icmp eq i32* %0, null + ret i1 %2 +} + +define dso_local i1 @test2(i32* readonly %0) { +; CHECK-LABEL: @test2( +; CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(i32* [[TMP0:%.*]]) ] +; CHECK-NEXT: ret i1 false +; + %2 = icmp eq i32* %0, null + call void @llvm.assume(i1 true) ["nonnull"(i32* %0)] + ret i1 %2 +} + +define dso_local i32 @test4(i32* readonly %0, i1 %cond) { +; CHECK-LABEL: @test4( +; CHECK-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(i32* [[TMP0:%.*]], i32 4) ] +; CHECK-NEXT: br i1 [[COND:%.*]], label [[A:%.*]], label [[B:%.*]] +; CHECK: B: +; CHECK-NEXT: br label [[A]] +; CHECK: A: +; CHECK-NEXT: br i1 false, label [[TMP4:%.*]], label [[TMP2:%.*]] +; CHECK: 2: +; CHECK-NEXT: [[TMP3:%.*]] = load i32, i32* [[TMP0]], align 4 +; CHECK-NEXT: br label [[TMP4]] +; CHECK: 4: +; CHECK-NEXT: [[TMP5:%.*]] = phi i32 [ [[TMP3]], [[TMP2]] ], [ 0, [[A]] ] +; CHECK-NEXT: ret i32 [[TMP5]] +; + call void @llvm.assume(i1 true) ["dereferenceable"(i32* %0, i32 4)] + br i1 %cond, label %A, label %B + +B: + br label %A + +A: + %2 = icmp eq i32* %0, null + br i1 %2, label %5, label %3 + +3: ; preds = %1 + %4 = load i32, i32* %0, align 4 + br label %5 + +5: ; preds = %1, %3 + %6 = phi i32 [ %4, %3 ], [ 0, %A ] + ret i32 %6 +} + +define dso_local i32 @test4b(i32* readonly %0, i1 %cond) "null-pointer-is-valid"="true" { +; CHECK-LABEL: @test4b( +; CHECK-NEXT: call void @llvm.assume(i1 true) [ "dereferenceable"(i32* [[TMP0:%.*]], i32 4) ] +; CHECK-NEXT: br i1 [[COND:%.*]], label [[A:%.*]], label [[B:%.*]] +; CHECK: B: +; CHECK-NEXT: br label [[A]] +; CHECK: A: +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32* [[TMP0]], null +; CHECK-NEXT: br i1 [[TMP2]], label [[TMP5:%.*]], label [[TMP3:%.*]] +; CHECK: 3: +; CHECK-NEXT: [[TMP4:%.*]] = load i32, i32* [[TMP0]], align 4 +; CHECK-NEXT: br label [[TMP5]] +; CHECK: 5: +; CHECK-NEXT: [[TMP6:%.*]] = phi i32 [ [[TMP4]], [[TMP3]] ], [ 0, [[A]] ] +; CHECK-NEXT: ret i32 [[TMP6]] +; + call void @llvm.assume(i1 true) ["dereferenceable"(i32* %0, i32 4)] + br i1 %cond, label %A, label %B + +B: + br label %A + +A: + %2 = icmp eq i32* %0, null + br i1 %2, label %5, label %3 + +3: ; preds = %1 + %4 = load i32, i32* %0, align 4 + br label %5 + +5: ; preds = %1, %3 + %6 = phi i32 [ %4, %3 ], [ 0, %A ] + ret i32 %6 +} diff --git a/llvm/test/Transforms/Attributor/nonnull.ll b/llvm/test/Transforms/Attributor/nonnull.ll --- a/llvm/test/Transforms/Attributor/nonnull.ll +++ b/llvm/test/Transforms/Attributor/nonnull.ll @@ -22,6 +22,18 @@ ret i8* %p } +define i8* @test2A(i8* %ret) { +; ATTRIBUTOR: define nonnull i8* @test2A(i8* nofree nonnull readnone returned %ret) + call void @llvm.assume(i1 true) [ "nonnull"(i8* %ret) ] + ret i8* %ret +} + +define i8* @test2B(i8* %ret) { +; ATTRIBUTOR: define nonnull dereferenceable(4) i8* @test2B(i8* nofree nonnull readnone returned dereferenceable(4) %ret) + call void @llvm.assume(i1 true) [ "dereferenceable"(i8* %ret, i32 4) ] + ret i8* %ret +} + ; Given an SCC where one of the functions can not be marked nonnull, ; can we still mark the other one which is trivially nonnull define i8* @scc_binder(i1 %c) {