diff --git a/llvm/lib/Transforms/Utils/InlineFunction.cpp b/llvm/lib/Transforms/Utils/InlineFunction.cpp --- a/llvm/lib/Transforms/Utils/InlineFunction.cpp +++ b/llvm/lib/Transforms/Utils/InlineFunction.cpp @@ -79,11 +79,21 @@ cl::Hidden, cl::desc("Convert noalias attributes to metadata during inlining.")); +static cl::opt UpdateReturnAttributes( + "update-return-attrs", cl::init(true), cl::Hidden, + cl::desc("Update return attributes on calls within inlined body")); + static cl::opt PreserveAlignmentAssumptions("preserve-alignment-assumptions-during-inlining", cl::init(true), cl::Hidden, cl::desc("Convert align attributes to assumptions during inlining.")); +static cl::opt MaxInstCheckedForThrow( + "max-inst-checked-for-throw-during-inlining", cl::Hidden, + cl::desc("the maximum number of instructions analyzed for may throw during " + "attribute inference in inlined body"), + cl::init(4)); + llvm::InlineResult llvm::InlineFunction(CallBase *CB, InlineFunctionInfo &IFI, AAResults *CalleeAAR, bool InsertLifetime) { @@ -1135,6 +1145,77 @@ } } +static void AddReturnAttributes(CallSite CS, ValueToValueMapTy &VMap) { + if (!UpdateReturnAttributes) + return; + AttrBuilder AB(CS.getAttributes(), AttributeList::ReturnIndex); + if (AB.empty()) + return; + + auto *CalledFunction = CS.getCalledFunction(); + auto &Context = CalledFunction->getContext(); + + auto GetClonedValue = [&](Instruction *I) -> Value * { + ValueToValueMapTy::iterator VMI = VMap.find(I); + if (VMI == VMap.end()) + return nullptr; + return VMI->second; + }; + + auto MayContainThrowingCall = [&](Instruction *RVal, Instruction *RInst) { + unsigned NumInstChecked = 0; + for (auto &I : + make_range(RVal->getIterator(), std::prev(RInst->getIterator()))) + if (NumInstChecked++ > MaxInstCheckedForThrow || I.mayThrow()) + return true; + return false; + }; + + for (auto &BB : *CalledFunction) { + auto *RI = dyn_cast(BB.getTerminator()); + if (!RI || !isa(RI->getOperand(0))) + continue; + // Sanity check that the cloned return instruction exists and is a return + // instruction itself. + auto *NewRI = dyn_cast_or_null(GetClonedValue(RI)); + if (!NewRI) + continue; + auto *RetVal = cast(RI->getOperand(0)); + // Sanity check that the cloned RetVal exists and is a call. + // Simplification during inlining could have transformed the cloned + // instruction. + auto *NewRetVal = dyn_cast_or_null(GetClonedValue(RetVal)); + if (!NewRetVal) + continue; + // Backward propagation of attributes to the returned value may be incorrect + // if it is control flow dependent. + // Consider: + // @callee { + // %rv = call @foo() + // %rv2 = call @bar() + // if (%rv2 != null) + // return %rv2 + // if (%rv == null) + // exit() + // return %rv + // } + // caller() { + // %val = call nonnull @callee() + // } + // Here we cannot add the nonnull attribute on either foo or bar. So, we + // limit the check to both NewRetVal and NewRI are in the same basic block + // and there are no throwing instructions between these instructioSns. + if (NewRI->getParent() != NewRetVal->getParent() || + MayContainThrowingCall(NewRetVal, NewRI)) + continue; + // Add to the existing attributes. + AttributeList AL = NewRetVal->getAttributes(); + AttributeList NewAL = + AL.addAttributes(Context, AttributeList::ReturnIndex, AB); + NewRetVal->setAttributes(NewAL); + } +} + /// If the inlined function has non-byval align arguments, then /// add @llvm.assume-based alignment assumptions to preserve this information. static void AddAlignmentAssumptions(CallSite CS, InlineFunctionInfo &IFI) { @@ -1795,6 +1876,10 @@ // Add noalias metadata if necessary. AddAliasScopeMetadata(CS, VMap, DL, CalleeAAR); + // Clone return attributes on the callsite into the calls within the inlined + // function which feed into its return value. + AddReturnAttributes(CS, VMap); + // Propagate llvm.mem.parallel_loop_access if necessary. PropagateParallelLoopAccessMetadata(CS, VMap); diff --git a/llvm/test/Transforms/Inline/ret_attr_update.ll b/llvm/test/Transforms/Inline/ret_attr_update.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Transforms/Inline/ret_attr_update.ll @@ -0,0 +1,75 @@ +; RUN: opt < %s -inline-threshold=0 -always-inline -S | FileCheck %s +; RUN: opt < %s -passes=always-inline -S | FileCheck %s + +declare i8* @foo(i8*) +declare i8* @bar(i8*) + +define i8* @callee(i8 *%p) alwaysinline { +; CHECK: @callee( +; CHECK: call i8* @foo(i8* noalias %p) + %r = call i8* @foo(i8* noalias %p) + ret i8* %r +} + +define i8* @caller(i8* %ptr, i64 %x) { +; CHECK-LABEL: @caller +; CHECK: call nonnull i8* @foo(i8* noalias + %gep = getelementptr inbounds i8, i8* %ptr, i64 %x + %p = call nonnull i8* @callee(i8* %gep) + ret i8* %p +} + +declare void @llvm.experimental.guard(i1,...) +; Cannot add nonnull attribute to foo. +define internal i8* @callee_with_throwable(i8* %p) alwaysinline { +; CHECK-NOT: callee_with_throwable + %r = call i8* @foo(i8* %p) + %cond = icmp ne i8* %r, null + call void (i1, ...) @llvm.experimental.guard(i1 %cond) [ "deopt"() ] + ret i8* %r +} + +; Here also we cannot add nonnull attribute to the call bar. +define internal i8* @callee_with_explicit_control_flow(i8* %p) alwaysinline { +; CHECK-NOT: callee_with_explicit_control_flow + %r = call i8* @bar(i8* %p) + %cond = icmp ne i8* %r, null + br i1 %cond, label %ret, label %orig + +ret: + ret i8* %r + +orig: + ret i8* %p +} + +define i8* @caller2(i8* %ptr, i64 %x, i1 %cond) { +; CHECK-LABEL: @caller2 +; CHECK: call i8* @foo +; CHECK: call i8* @bar + %gep = getelementptr inbounds i8, i8* %ptr, i64 %x + %p = call nonnull i8* @callee_with_throwable(i8* %gep) + %q = call nonnull i8* @callee_with_explicit_control_flow(i8* %gep) + br i1 %cond, label %pret, label %qret + +pret: + ret i8* %p + +qret: + ret i8* %q +} + +define internal i8* @callee3(i8 *%p) alwaysinline { +; CHECK-NOT: callee3 + %r = call noalias i8* @foo(i8* %p) + ret i8* %r +} + +; add the deref attribute to the existing attributes on foo. +define i8* @caller3(i8* %ptr, i64 %x) { +; CHECK-LABEL: caller3 +; CHECK: call noalias dereferenceable_or_null(12) i8* @foo + %gep = getelementptr inbounds i8, i8* %ptr, i64 %x + %p = call dereferenceable_or_null(12) i8* @callee3(i8* %gep) + ret i8* %p +}