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 { @@ -125,6 +126,14 @@ /// function returned true. bool isAssumeWithEmptyBundle(CallInst &Assume); +/// Return true iff the value V has any attribute in AttrKinds in an the operand +/// bundles of an llvm.assume and this assume dominate the instruction Ctx. +/// Knowledge can be filtered using an optional predicate. +bool hasDominatingAttributeInAssumeBundle( + const Value *V, const Instruction *Ctx, const DominatorTree *DT, + ArrayRef AttrKinds, + llvm::function_ref Filter = [](auto) { 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" @@ -660,6 +661,16 @@ return !TrueValues.contains(APInt::getNullValue(CI->getBitWidth())); }; + if (hasDominatingAttributeInAssumeBundle( + V, Q.CxtI, Q.DT, {Attribute::NonNull, Attribute::Dereferenceable}, + [&](RetainedKnowledge RK) { + return RK.AttrKind == Attribute::NonNull || + !llvm::NullPointerIsDefined( + Q.CxtI->getFunction(), + V->getType()->getPointerAddressSpace()); + })) + 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 @@ -303,3 +303,21 @@ Assume->insertBefore(&I); return PreservedAnalyses::all(); } + +bool llvm::hasDominatingAttributeInAssumeBundle( + const Value *V, const Instruction *Ctx, const DominatorTree *DT, + ArrayRef AttrKinds, + llvm::function_ref Filter) { + if (DT && Ctx) + for (auto &U : V->uses()) { + if (auto *Intr = dyn_cast(U.getUser())) + if (Intr->getIntrinsicID() == Intrinsic::assume) { + RetainedKnowledge RK = + getKnowledgeFromOperandInAssume(*Intr, U.getOperandNo()); + for (auto Attr : AttrKinds) + if (Attr == RK.AttrKind && DT->dominates(Intr, Ctx) && Filter(RK)) + return true; + } + } + return false; +} 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,60 @@ 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 +} + +; FIXME: We could fold the icmp in the following example. +; because if the icmp is reached the assume will necessarly be reached. +define dso_local i1 @test2(i32* readonly %0) { +; CHECK-LABEL: @test2( +; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32* [[TMP0:%.*]], null +; CHECK-NEXT: call void @llvm.assume(i1 true) [ "nonnull"(i32* [[TMP0]]) ] +; CHECK-NEXT: ret i1 [[TMP2]] +; + %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 +}