Index: llvm/lib/Transforms/IPO/Attributor.cpp =================================================================== --- llvm/lib/Transforms/IPO/Attributor.cpp +++ llvm/lib/Transforms/IPO/Attributor.cpp @@ -2816,13 +2816,56 @@ auto &NoCaptureAA = A.getAAFor(*this, VIRP, /* TrackDependence */ false); // Check whether the value is captured in the scope using AANoCapture. - // FIXME: This is conservative though, it is better to look at CFG and - // check only uses possibly executed before this callsite. - if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { - LLVM_DEBUG( - dbgs() << "[AANoAliasCSArg] " << getAssociatedValue() - << " cannot be noalias as it is potentially captured\n"); + // Look at CFG and check only uses possibly executed before this + // callsite. + + auto UsePred = [&](const Use &U, bool &Follow) -> bool { + Instruction *UserI = cast(U.getUser()); + + // If user if curr instr and only use. + if ((UserI == getCtxI()) && (UserI->getNumUses() == 1)) + return true; + + const Function *ScopeFn = VIRP.getAnchorScope(); + if (ScopeFn) { + const auto &ReachabilityAA = + A.getAAFor(*this, IRPosition::function(*ScopeFn)); + + if (!ReachabilityAA.isAssumedReachable(UserI, getCtxI())) + return true; + + if (auto *CB = dyn_cast(UserI)) { + if (CB->isArgOperand(&U)) { + + unsigned ArgNo = CB->getArgOperandNo(&U); + + const auto &NoCaptureAA = A.getAAFor( + *this, IRPosition::callsite_argument(*CB, ArgNo)); + + if (NoCaptureAA.isAssumedNoCapture()) + return true; + } + } + } + + // For cases which can potentially have more users + if (isa(U) || isa(U) || isa(U) || + isa(U)) { + Follow = true; + return true; + } + + LLVM_DEBUG(dbgs() << "[AANoAliasCSArg] Unknown user: " << *U << "\n"); return false; + }; + + if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) { + if (!A.checkForAllUses(UsePred, *this, getAssociatedValue())) { + LLVM_DEBUG( + dbgs() << "[AANoAliasCSArg] " << getAssociatedValue() + << " cannot be noalias as it is potentially captured\n"); + return false; + } } A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL); @@ -5582,7 +5625,7 @@ createReplacementValues( PrivatizableType.getValue(), ACS, ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()), - NewArgOperands); + NewArgOperands); }; // Collect the types that will replace the privatizable type in the function Index: llvm/test/Transforms/Attributor/noalias.ll =================================================================== --- llvm/test/Transforms/Attributor/noalias.ll +++ llvm/test/Transforms/Attributor/noalias.ll @@ -348,3 +348,44 @@ ret i32 %l } +%struct._IO_FILE = type { i32, i8*, i8*, i32 (%struct._IO_FILE*)*, i8*, i8*, i8*, i8*, i32 (%struct._IO_FILE*, i8*, i32)*, i32 (%struct._IO_FILE*, i8*, i32)*, i64 (%struct._IO_FILE*, i64, i32)*, i8*, i32, %struct._IO_FILE*, %struct._IO_FILE*, i32, i32, i32, i16, i8, i8, i32, i32, i8*, i64, i8*, i8*, i8*, [4 x i8], i64, i64, %struct._IO_FILE*, %struct._IO_FILE*, %struct.__locale_struct*, [4 x i8] } +%struct.__locale_struct = type { [6 x %struct.__locale_map*] } +%struct.__locale_map = type opaque + +; Function Attrs: nounwind optsize +; CHECK: define internal fastcc double @strtox(i8* noalias %s) unnamed_addr +define internal fastcc double @strtox(i8* %s, i8** %p, i32 %prec) unnamed_addr { +entry: + %f = alloca %struct._IO_FILE, align 8 + %0 = bitcast %struct._IO_FILE* %f to i8* + call void @llvm.lifetime.start.p0i8(i64 144, i8* nonnull %0) + %call = call i32 bitcast (i32 (...)* @sh_fromstring to i32 (%struct._IO_FILE*, i8*)*)(%struct._IO_FILE* nonnull %f, i8* %s) + call void @__shlim(%struct._IO_FILE* nonnull %f, i64 0) + %call1 = call double @__floatscan(%struct._IO_FILE* nonnull %f, i32 %prec, i32 1) + call void @llvm.lifetime.end.p0i8(i64 144, i8* nonnull %0) + + ret double %call1 +} + +; Function Attrs: nounwind optsize +define dso_local double @strtod(i8* noalias %s, i8** noalias %p) { +entry: +; CHECK: %call = tail call fastcc double @strtox(i8* noalias %s) + %call = tail call fastcc double @strtox(i8* %s, i8** %p, i32 1) + ret double %call +} + +; Function Attrs: argmemonly nounwind willreturn +declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) + +; Function Attrs: optsize +declare dso_local i32 @sh_fromstring(...) local_unnamed_addr + +; Function Attrs: optsize +declare dso_local void @__shlim(%struct._IO_FILE*, i64) local_unnamed_addr + +; Function Attrs: optsize +declare dso_local double @__floatscan(%struct._IO_FILE*, i32, i32) local_unnamed_addr + +; Function Attrs: argmemonly nounwind willreturn +declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) Index: llvm/test/Transforms/Attributor/nonnull.ll =================================================================== --- llvm/test/Transforms/Attributor/nonnull.ll +++ llvm/test/Transforms/Attributor/nonnull.ll @@ -223,7 +223,7 @@ define internal i32* @f2(i32* %arg) { ; ATTRIBUTOR: define internal nonnull i32* @f2(i32* nofree nonnull readonly align 4 dereferenceable(4) %arg) bb: -; ATTRIBUTOR: %tmp = tail call nonnull i32* @f1(i32* nofree nonnull readonly align 4 dereferenceable(4) %arg) +; ATTRIBUTOR: %tmp = tail call nonnull i32* @f1(i32* noalias nofree nonnull readonly align 4 dereferenceable(4) %arg) %tmp = tail call i32* @f1(i32* %arg) ret i32* %tmp }