diff --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp --- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp +++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp @@ -719,16 +719,8 @@ return Attribute::None; } - // Given we've explictily handled the callee operand above, what's left - // must be a data operand (e.g. argument or operand bundle) - const bool IsOperandBundleUse = UseIndex >= CB.arg_size(); - if (UseIndex >= F->arg_size() && !IsOperandBundleUse) { - assert(F->isVarArg() && "More params than args in non-varargs call"); - return Attribute::None; - } - - - if (CB.isArgOperand(U) && SCCNodes.count(F->getArg(UseIndex))) { + if (CB.isArgOperand(U) && UseIndex < F->arg_size() && + SCCNodes.count(F->getArg(UseIndex))) { // This is an argument which is part of the speculative SCC. Note that // only operands corresponding to formal arguments of the callee can // participate in the speculation. diff --git a/llvm/test/Transforms/FunctionAttrs/out-of-bounds-iterator-bug.ll b/llvm/test/Transforms/FunctionAttrs/out-of-bounds-iterator-bug.ll --- a/llvm/test/Transforms/FunctionAttrs/out-of-bounds-iterator-bug.ll +++ b/llvm/test/Transforms/FunctionAttrs/out-of-bounds-iterator-bug.ll @@ -1,14 +1,8 @@ ; RUN: opt -function-attrs -S < %s | FileCheck %s ; RUN: opt -passes=function-attrs -S < %s | FileCheck %s -; This checks for an iterator wraparound bug in FunctionAttrs. The previous -; "incorrect" behavior was inferring readonly for the %x argument in @caller. -; Inferring readonly for %x *is* actually correct, since @va_func is marked -; readonly, but FunctionAttrs was inferring readonly for the wrong reasons (and -; we _need_ the readonly on @va_func to trigger the problematic code path). It -; is possible that in the future FunctionAttrs becomes smart enough to infer -; readonly for %x for the right reasons, and at that point this test will have -; to be marked invalid. +; This checks for a previously existing iterator wraparound bug in +; FunctionAttrs, and in the process covers corner cases with varargs. declare void @llvm.va_start(i8*) declare void @llvm.va_end(i8*) @@ -24,8 +18,26 @@ } define i32 @caller(i32* %x) { -; CHECK-LABEL: define i32 @caller(i32* nocapture %x) +; CHECK-LABEL: define i32 @caller(i32* nocapture readonly %x) entry: call void(i32*,...) @va_func(i32* null, i32 0, i32 0, i32 0, i32* %x) ret i32 42 } + +define void @va_func2(i32* readonly %b, ...) { +; CHECK-LABEL: define void @va_func2(i32* nocapture readonly %b, ...) + entry: + %valist = alloca i8 + call void @llvm.va_start(i8* %valist) + call void @llvm.va_end(i8* %valist) + %x = call i32 @caller(i32* %b) + ret void +} + +define i32 @caller2(i32* %x, i32* %y) { +; CHECK-LABEL: define i32 @caller2(i32* nocapture readonly %x, i32* %y) + entry: + call void(i32*,...) @va_func2(i32* %x, i32 0, i32 0, i32 0, i32* %y) + ret i32 42 +} +