diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -138,6 +138,17 @@ /// how to cast various constants. Value *getWithType(Value &V, Type &Ty); +/// Return the combination of \p A and \p B such that the result is a possible +/// value of both. \p B is potentially casted to match the type \p Ty or the +/// type of \p A if \p Ty is null. +/// +/// Examples: +/// X + none => X +/// not_none + undef => not_none +/// V1 + V2 => nullptr +Optional +combineOptionalValuesInAAValueLatice(const Optional &A, + const Optional &B, Type *Ty); } // namespace AA /// The value passed to the line option that defines the maximal initialization 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 @@ -177,6 +177,30 @@ return nullptr; } +Optional +AA::combineOptionalValuesInAAValueLatice(const Optional &A, + const Optional &B, Type *Ty) { + if (A == B) + return A; + if (!B.hasValue()) + return A; + if (*B == nullptr) + return nullptr; + if (!A.hasValue()) + return Ty ? getWithType(**B, *Ty) : nullptr; + if (*A == nullptr) + return nullptr; + if (!Ty) + Ty = (*A)->getType(); + if (isa_and_nonnull(*A)) + return getWithType(**B, *Ty); + if (isa(*B)) + return A; + if (*A && *B && *A == getWithType(**B, *Ty)) + return A; + return nullptr; +} + /// Return true if \p New is equal or worse than \p Old. static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) { if (!Old.isIntAttribute()) diff --git a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp --- a/llvm/lib/Transforms/IPO/AttributorAttributes.cpp +++ b/llvm/lib/Transforms/IPO/AttributorAttributes.cpp @@ -1001,22 +1001,11 @@ // multiple, a nullptr is returned indicating there cannot be a unique // returned value. Optional UniqueRV; + Type *Ty = getAssociatedFunction()->getReturnType(); auto Pred = [&](Value &RV) -> bool { - // If we found a second returned value and neither the current nor the saved - // one is an undef, there is no unique returned value. Undefs are special - // since we can pretend they have any value. - if (UniqueRV.hasValue() && UniqueRV != &RV && - !(isa(RV) || isa(UniqueRV.getValue()))) { - UniqueRV = nullptr; - return false; - } - - // Do not overwrite a value with an undef. - if (!UniqueRV.hasValue() || !isa(RV)) - UniqueRV = &RV; - - return true; + UniqueRV = AA::combineOptionalValuesInAAValueLatice(UniqueRV, &RV, Ty); + return UniqueRV != Optional(nullptr); }; if (!A.checkForAllReturnedValues(Pred, *this)) @@ -4559,31 +4548,21 @@ IRPosition::value(QueryingValue, QueryingAA.getCallBaseContext()), DepClassTy::REQUIRED); - Optional QueryingValueSimplified = - ValueSimplifyAA.getAssumedSimplifiedValue(A); - - if (!QueryingValueSimplified.hasValue()) - return true; - - if (!QueryingValueSimplified.getValue()) + AccumulatedSimplifiedValue = AA::combineOptionalValuesInAAValueLatice( + AccumulatedSimplifiedValue, + ValueSimplifyAA.getAssumedSimplifiedValue(A), + QueryingAA.getAssociatedType()); + if (AccumulatedSimplifiedValue == Optional(nullptr)) return false; - Value &QueryingValueSimplifiedUnwrapped = - *QueryingValueSimplified.getValue(); - - if (AccumulatedSimplifiedValue.hasValue() && - !isa(AccumulatedSimplifiedValue.getValue()) && - !isa(QueryingValueSimplifiedUnwrapped)) - return AccumulatedSimplifiedValue == QueryingValueSimplified; - if (AccumulatedSimplifiedValue.hasValue() && - isa(QueryingValueSimplifiedUnwrapped)) - return true; - - LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue - << " is assumed to be " - << QueryingValueSimplifiedUnwrapped << "\n"); - - AccumulatedSimplifiedValue = QueryingValueSimplified; + LLVM_DEBUG({ + if (AccumulatedSimplifiedValue.hasValue()) + dbgs() << "[ValueSimplify] " << QueryingValue << " is assumed to be " + << **AccumulatedSimplifiedValue << "\n"; + else + dbgs() << "[ValueSimplify] " << QueryingValue + << " is assumed to be \n"; + }); return true; } diff --git a/llvm/test/Transforms/Attributor/nocapture-2.ll b/llvm/test/Transforms/Attributor/nocapture-2.ll --- a/llvm/test/Transforms/Attributor/nocapture-2.ll +++ b/llvm/test/Transforms/Attributor/nocapture-2.ll @@ -211,7 +211,7 @@ define float* @scc_A(i32* dereferenceable_or_null(4) %a) { ; CHECK: Function Attrs: nofree nosync nounwind readnone ; CHECK-LABEL: define {{[^@]+}}@scc_A -; CHECK-SAME: (i32* nofree readnone returned dereferenceable_or_null(4) "no-capture-maybe-returned" [[A:%.*]]) #[[ATTR2:[0-9]+]] { +; CHECK-SAME: (i32* nofree readnone dereferenceable_or_null(4) "no-capture-maybe-returned" [[A:%.*]]) #[[ATTR2:[0-9]+]] { ; CHECK-NEXT: entry: ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne i32* [[A]], null ; CHECK-NEXT: br i1 [[TOBOOL]], label [[COND_TRUE:%.*]], label [[COND_FALSE:%.*]] @@ -257,7 +257,7 @@ define i64* @scc_B(double* dereferenceable_or_null(8) %a) { ; CHECK: Function Attrs: nofree nosync nounwind readnone ; CHECK-LABEL: define {{[^@]+}}@scc_B -; CHECK-SAME: (double* nofree readnone returned dereferenceable_or_null(8) "no-capture-maybe-returned" [[A:%.*]]) #[[ATTR2]] { +; CHECK-SAME: (double* nofree readnone dereferenceable_or_null(8) "no-capture-maybe-returned" [[A:%.*]]) #[[ATTR2]] { ; CHECK-NEXT: entry: ; CHECK-NEXT: [[TOBOOL:%.*]] = icmp ne double* [[A]], null ; CHECK-NEXT: br i1 [[TOBOOL]], label [[COND_TRUE:%.*]], label [[COND_FALSE:%.*]] @@ -303,7 +303,7 @@ define i8* @scc_C(i16* dereferenceable_or_null(2) %a) { ; CHECK: Function Attrs: nofree nosync nounwind readnone ; CHECK-LABEL: define {{[^@]+}}@scc_C -; CHECK-SAME: (i16* nofree readnone returned dereferenceable_or_null(4) "no-capture-maybe-returned" [[A:%.*]]) #[[ATTR2]] { +; CHECK-SAME: (i16* nofree readnone dereferenceable_or_null(4) "no-capture-maybe-returned" [[A:%.*]]) #[[ATTR2]] { ; CHECK-NEXT: entry: ; CHECK-NEXT: [[BC:%.*]] = bitcast i16* [[A]] to i32* ; CHECK-NEXT: [[CALL:%.*]] = call dereferenceable_or_null(4) float* @scc_A(i32* noalias nofree readnone dereferenceable_or_null(4) "no-capture-maybe-returned" [[BC]]) #[[ATTR2]] 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 @@ -1671,14 +1671,12 @@ ; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@nonnull_function_ptr_1 ; IS__TUNIT____-SAME: () #[[ATTR1]] { -; IS__TUNIT____-NEXT: [[BC:%.*]] = bitcast i8* ()* @nonnull_function_ptr_1 to i8* -; IS__TUNIT____-NEXT: ret i8* [[BC]] +; IS__TUNIT____-NEXT: ret i8* bitcast (i8* ()* @nonnull_function_ptr_1 to i8*) ; ; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@nonnull_function_ptr_1 ; IS__CGSCC____-SAME: () #[[ATTR1]] { -; IS__CGSCC____-NEXT: [[BC:%.*]] = bitcast i8* ()* @nonnull_function_ptr_1 to i8* -; IS__CGSCC____-NEXT: ret i8* [[BC]] +; IS__CGSCC____-NEXT: ret i8* bitcast (i8* ()* @nonnull_function_ptr_1 to i8*) ; %bc = bitcast i8*()* @nonnull_function_ptr_1 to i8* ret i8* %bc @@ -1689,14 +1687,12 @@ ; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@nonnull_function_ptr_2 ; IS__TUNIT____-SAME: () #[[ATTR1]] { -; IS__TUNIT____-NEXT: [[BC:%.*]] = bitcast i8* ()* @function_decl to i8* -; IS__TUNIT____-NEXT: ret i8* [[BC]] +; IS__TUNIT____-NEXT: ret i8* bitcast (i8* ()* @function_decl to i8*) ; ; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@nonnull_function_ptr_2 ; IS__CGSCC____-SAME: () #[[ATTR1]] { -; IS__CGSCC____-NEXT: [[BC:%.*]] = bitcast i8* ()* @function_decl to i8* -; IS__CGSCC____-NEXT: ret i8* [[BC]] +; IS__CGSCC____-NEXT: ret i8* bitcast (i8* ()* @function_decl to i8*) ; %bc = bitcast i8*()* @function_decl to i8* ret i8* %bc diff --git a/llvm/test/Transforms/Attributor/returned.ll b/llvm/test/Transforms/Attributor/returned.ll --- a/llvm/test/Transforms/Attributor/returned.ll +++ b/llvm/test/Transforms/Attributor/returned.ll @@ -917,14 +917,14 @@ define double* @bitcast(i32* %b) #0 { ; IS__TUNIT____: Function Attrs: nofree noinline nosync nounwind readnone uwtable willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@bitcast -; IS__TUNIT____-SAME: (i32* nofree readnone returned "no-capture-maybe-returned" [[B:%.*]]) #[[ATTR0]] { +; IS__TUNIT____-SAME: (i32* nofree readnone "no-capture-maybe-returned" [[B:%.*]]) #[[ATTR0]] { ; IS__TUNIT____-NEXT: entry: ; IS__TUNIT____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__TUNIT____-NEXT: ret double* [[BC0]] ; ; IS__CGSCC____: Function Attrs: nofree noinline norecurse nosync nounwind readnone uwtable willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@bitcast -; IS__CGSCC____-SAME: (i32* nofree readnone returned "no-capture-maybe-returned" [[B:%.*]]) #[[ATTR0]] { +; IS__CGSCC____-SAME: (i32* nofree readnone "no-capture-maybe-returned" [[B:%.*]]) #[[ATTR0]] { ; IS__CGSCC____-NEXT: entry: ; IS__CGSCC____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__CGSCC____-NEXT: ret double* [[BC0]] @@ -947,7 +947,7 @@ define double* @bitcasts_select_and_phi(i32* %b) #0 { ; IS__TUNIT____: Function Attrs: nofree noinline nosync nounwind readnone uwtable willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@bitcasts_select_and_phi -; IS__TUNIT____-SAME: (i32* nofree readnone returned [[B:%.*]]) #[[ATTR0]] { +; IS__TUNIT____-SAME: (i32* nofree readnone [[B:%.*]]) #[[ATTR0]] { ; IS__TUNIT____-NEXT: entry: ; IS__TUNIT____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__TUNIT____-NEXT: [[CMP:%.*]] = icmp eq double* [[BC0]], null @@ -966,7 +966,7 @@ ; ; IS__CGSCC____: Function Attrs: nofree noinline norecurse nosync nounwind readnone uwtable willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@bitcasts_select_and_phi -; IS__CGSCC____-SAME: (i32* nofree readnone returned [[B:%.*]]) #[[ATTR0]] { +; IS__CGSCC____-SAME: (i32* nofree readnone [[B:%.*]]) #[[ATTR0]] { ; IS__CGSCC____-NEXT: entry: ; IS__CGSCC____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__CGSCC____-NEXT: [[CMP:%.*]] = icmp eq double* [[BC0]], null @@ -1016,7 +1016,7 @@ define double* @ret_arg_arg_undef(i32* %b) #0 { ; IS__TUNIT____: Function Attrs: nofree noinline nosync nounwind readnone uwtable willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@ret_arg_arg_undef -; IS__TUNIT____-SAME: (i32* nofree readnone returned [[B:%.*]]) #[[ATTR0]] { +; IS__TUNIT____-SAME: (i32* nofree readnone [[B:%.*]]) #[[ATTR0]] { ; IS__TUNIT____-NEXT: entry: ; IS__TUNIT____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__TUNIT____-NEXT: [[CMP:%.*]] = icmp eq double* [[BC0]], null @@ -1033,7 +1033,7 @@ ; ; IS__CGSCC____: Function Attrs: nofree noinline norecurse nosync nounwind readnone uwtable willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@ret_arg_arg_undef -; IS__CGSCC____-SAME: (i32* nofree readnone returned [[B:%.*]]) #[[ATTR0]] { +; IS__CGSCC____-SAME: (i32* nofree readnone [[B:%.*]]) #[[ATTR0]] { ; IS__CGSCC____-NEXT: entry: ; IS__CGSCC____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__CGSCC____-NEXT: [[CMP:%.*]] = icmp eq double* [[BC0]], null @@ -1081,7 +1081,7 @@ define double* @ret_undef_arg_arg(i32* %b) #0 { ; IS__TUNIT____: Function Attrs: nofree noinline nosync nounwind readnone uwtable willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@ret_undef_arg_arg -; IS__TUNIT____-SAME: (i32* nofree readnone returned [[B:%.*]]) #[[ATTR0]] { +; IS__TUNIT____-SAME: (i32* nofree readnone [[B:%.*]]) #[[ATTR0]] { ; IS__TUNIT____-NEXT: entry: ; IS__TUNIT____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__TUNIT____-NEXT: [[CMP:%.*]] = icmp eq double* [[BC0]], null @@ -1098,7 +1098,7 @@ ; ; IS__CGSCC____: Function Attrs: nofree noinline norecurse nosync nounwind readnone uwtable willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@ret_undef_arg_arg -; IS__CGSCC____-SAME: (i32* nofree readnone returned [[B:%.*]]) #[[ATTR0]] { +; IS__CGSCC____-SAME: (i32* nofree readnone [[B:%.*]]) #[[ATTR0]] { ; IS__CGSCC____-NEXT: entry: ; IS__CGSCC____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__CGSCC____-NEXT: [[CMP:%.*]] = icmp eq double* [[BC0]], null @@ -1146,7 +1146,7 @@ define double* @ret_undef_arg_undef(i32* %b) #0 { ; IS__TUNIT____: Function Attrs: nofree noinline nosync nounwind readnone uwtable willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@ret_undef_arg_undef -; IS__TUNIT____-SAME: (i32* nofree readnone returned [[B:%.*]]) #[[ATTR0]] { +; IS__TUNIT____-SAME: (i32* nofree readnone [[B:%.*]]) #[[ATTR0]] { ; IS__TUNIT____-NEXT: entry: ; IS__TUNIT____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__TUNIT____-NEXT: [[CMP:%.*]] = icmp eq double* [[BC0]], null @@ -1162,7 +1162,7 @@ ; ; IS__CGSCC____: Function Attrs: nofree noinline norecurse nosync nounwind readnone uwtable willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@ret_undef_arg_undef -; IS__CGSCC____-SAME: (i32* nofree readnone returned [[B:%.*]]) #[[ATTR0]] { +; IS__CGSCC____-SAME: (i32* nofree readnone [[B:%.*]]) #[[ATTR0]] { ; IS__CGSCC____-NEXT: entry: ; IS__CGSCC____-NEXT: [[BC0:%.*]] = bitcast i32* [[B]] to double* ; IS__CGSCC____-NEXT: [[CMP:%.*]] = icmp eq double* [[BC0]], null @@ -1513,14 +1513,12 @@ ; IS__TUNIT____: Function Attrs: nofree noinline nosync nounwind readnone uwtable willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@ret_const ; IS__TUNIT____-SAME: () #[[ATTR0]] { -; IS__TUNIT____-NEXT: [[BC:%.*]] = bitcast i8* @G to i32* -; IS__TUNIT____-NEXT: ret i32* [[BC]] +; IS__TUNIT____-NEXT: ret i32* bitcast (i8* @G to i32*) ; ; IS__CGSCC____: Function Attrs: nofree noinline norecurse nosync nounwind readnone uwtable willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@ret_const ; IS__CGSCC____-SAME: () #[[ATTR0]] { -; IS__CGSCC____-NEXT: [[BC:%.*]] = bitcast i8* @G to i32* -; IS__CGSCC____-NEXT: ret i32* [[BC]] +; IS__CGSCC____-NEXT: ret i32* bitcast (i8* @G to i32*) ; %bc = bitcast i8* @G to i32* ret i32* %bc @@ -1543,14 +1541,12 @@ ; IS__TUNIT____: Function Attrs: nofree noinline nosync nounwind readnone uwtable willreturn ; IS__TUNIT____-LABEL: define {{[^@]+}}@dont_use_const ; IS__TUNIT____-SAME: () #[[ATTR0]] { -; IS__TUNIT____-NEXT: [[C:%.*]] = musttail call i32* @ret_const() #[[ATTR5]] -; IS__TUNIT____-NEXT: ret i32* [[C]] +; IS__TUNIT____-NEXT: ret i32* bitcast (i8* @G to i32*) ; ; IS__CGSCC____: Function Attrs: nofree noinline norecurse nosync nounwind readnone uwtable willreturn ; IS__CGSCC____-LABEL: define {{[^@]+}}@dont_use_const ; IS__CGSCC____-SAME: () #[[ATTR0]] { -; IS__CGSCC____-NEXT: [[C:%.*]] = musttail call i32* @ret_const() #[[ATTR6]] -; IS__CGSCC____-NEXT: ret i32* [[C]] +; IS__CGSCC____-NEXT: ret i32* bitcast (i8* @G to i32*) ; %c = musttail call i32* @ret_const() ret i32* %c diff --git a/llvm/test/Transforms/Attributor/value-simplify.ll b/llvm/test/Transforms/Attributor/value-simplify.ll --- a/llvm/test/Transforms/Attributor/value-simplify.ll +++ b/llvm/test/Transforms/Attributor/value-simplify.ll @@ -1086,6 +1086,32 @@ %c = icmp eq i8* null, null ret i1 %c } + +define i1 @test_cmp_null_after_cast() { +; IS__TUNIT____: Function Attrs: nofree nosync nounwind readnone willreturn +; IS__TUNIT____-LABEL: define {{[^@]+}}@test_cmp_null_after_cast +; IS__TUNIT____-SAME: () #[[ATTR1]] { +; IS__TUNIT____-NEXT: ret i1 true +; +; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn +; IS__CGSCC____-LABEL: define {{[^@]+}}@test_cmp_null_after_cast +; IS__CGSCC____-SAME: () #[[ATTR1]] { +; IS__CGSCC____-NEXT: ret i1 true +; + %c = call i1 @cmp_null_after_cast(i32 0, i8 0) + ret i1 %c +} +define internal i1 @cmp_null_after_cast(i32 %a, i8 %b) { +; IS__CGSCC____: Function Attrs: nofree norecurse nosync nounwind readnone willreturn +; IS__CGSCC____-LABEL: define {{[^@]+}}@cmp_null_after_cast +; IS__CGSCC____-SAME: () #[[ATTR1]] { +; IS__CGSCC____-NEXT: ret i1 undef +; + %t = trunc i32 %a to i8 + %c = icmp eq i8 %t, %b + ret i1 %c +} + ;. ; IS__TUNIT_OPM: attributes #[[ATTR0]] = { nofree nosync nounwind willreturn } ; IS__TUNIT_OPM: attributes #[[ATTR1]] = { nofree nosync nounwind readnone willreturn }