diff --git a/llvm/include/llvm/IR/KnowledgeRetention.h b/llvm/include/llvm/IR/KnowledgeRetention.h --- a/llvm/include/llvm/IR/KnowledgeRetention.h +++ b/llvm/include/llvm/IR/KnowledgeRetention.h @@ -19,6 +19,7 @@ #include "llvm/IR/Attributes.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/PassManager.h" +#include "llvm/IR/Dominators.h" #include "llvm/ADT/DenseMap.h" namespace llvm { @@ -109,6 +110,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. @@ -133,6 +136,19 @@ /// function returned true. bool isAssumeWithEmptyBundle(CallInst &Assume); +/// return true iff U is a Use in an llvm.assume bundle and this use is +/// associated with any attributes of AttrKinds. +RetainedKnowledge +getRetainedKnowledgeFromUse(const Use *U, + ArrayRef AttrKinds); + +/// Return true iff the value V has any attribute in AttrKinds in an the operand +/// bundles of an llvm.assume and matches the Filter predicate. +RetainedKnowledge getRetainedKnowledgeForValue( + const Value *V, ArrayRef AttrKinds, + llvm::function_ref Filter = + [](RetainedKnowledge, Instruction *) { return true; }); + //===----------------------------------------------------------------------===// // Utilities for testing //===----------------------------------------------------------------------===// 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 @@ -53,6 +53,7 @@ #include "llvm/IR/Intrinsics.h" #include "llvm/IR/IntrinsicsAArch64.h" #include "llvm/IR/IntrinsicsX86.h" +#include "llvm/IR/KnowledgeRetention.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Metadata.h" #include "llvm/IR/Module.h" @@ -687,6 +688,19 @@ 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 (getRetainedKnowledgeForValue( + V, AttrKinds, [&](RetainedKnowledge, Instruction *Assume) { + return isValidAssumeForContext(Assume, Q.CxtI, Q.DT); + })) + return true; + } + for (auto &AssumeVH : Q.AC->assumptionsFor(V)) { if (!AssumeVH) continue; diff --git a/llvm/lib/IR/KnowledgeRetention.cpp b/llvm/lib/IR/KnowledgeRetention.cpp --- a/llvm/lib/IR/KnowledgeRetention.cpp +++ b/llvm/lib/IR/KnowledgeRetention.cpp @@ -298,6 +298,32 @@ }); } +RetainedKnowledge +llvm::getRetainedKnowledgeFromUse(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::getRetainedKnowledgeForValue( + const Value *V, ArrayRef AttrKinds, + llvm::function_ref Filter) { + for (auto &U : V->uses()) { + if (RetainedKnowledge RK = getRetainedKnowledgeFromUse(&U, AttrKinds)) + if (Filter(RK, cast(U.getUser()))) + return RK; + } + return RetainedKnowledge::none(); +} + PreservedAnalyses AssumeBuilderPass::run(Function &F, FunctionAnalysisManager &AM) { for (Instruction &I : instructions(F)) 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 @@ -37,6 +37,7 @@ #include "llvm/IR/IRBuilder.h" #include "llvm/IR/InstIterator.h" #include "llvm/IR/IntrinsicInst.h" +#include "llvm/IR/KnowledgeRetention.h" #include "llvm/IR/NoFolder.h" #include "llvm/IR/Verifier.h" #include "llvm/InitializePasses.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 = getRetainedKnowledgeFromUse( + 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) {