From 418ab3729cbf094011eeaeb92306c410ae0f8944 Mon Sep 17 00:00:00 2001 From: Duncan Sands Date: Sat, 26 Jan 2008 06:41:49 +0000 Subject: [PATCH] Create an explicit copy for byval parameters even when inlining a readonly function. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@46393 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/InlineFunction.cpp | 15 +++++++++------ test/CFrontend/2008-01-26-ReadOnlyByVal.c | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 test/CFrontend/2008-01-26-ReadOnlyByVal.c diff --git a/lib/Transforms/Utils/InlineFunction.cpp b/lib/Transforms/Utils/InlineFunction.cpp index 552583042a7..acd2b108cea 100644 --- a/lib/Transforms/Utils/InlineFunction.cpp +++ b/lib/Transforms/Utils/InlineFunction.cpp @@ -240,12 +240,15 @@ bool llvm::InlineFunction(CallSite CS, CallGraph *CG, const TargetData *TD) { E = CalledFunc->arg_end(); I != E; ++I, ++AI, ++ArgNo) { Value *ActualArg = *AI; - // When byval arguments actually inlined, we need to make the copy implied - // by them explicit. However, we don't do this if the callee is readonly - // or readnone, because the copy would be unneeded: the callee doesn't - // modify the struct. - if (CalledFunc->paramHasAttr(ArgNo+1, ParamAttr::ByVal) && - !CalledFunc->onlyReadsMemory()) { + // When byval arguments are inlined, we need to make the copy implied + // by them explicit. It is tempting to think that this is not needed if + // the callee is readonly, because the callee doesn't modify the struct. + // However this would be wrong: readonly means that any writes the callee + // performs are not visible to the caller. But writes by the callee to + // an argument passed byval are by definition not visible to the caller! + // Since we allow this kind of readonly function, there needs to be an + // explicit copy in order to keep the writes invisible after inlining. + if (CalledFunc->paramHasAttr(ArgNo+1, ParamAttr::ByVal)) { const Type *AggTy = cast(I->getType())->getElementType(); const Type *VoidPtrTy = PointerType::getUnqual(Type::Int8Ty); diff --git a/test/CFrontend/2008-01-26-ReadOnlyByVal.c b/test/CFrontend/2008-01-26-ReadOnlyByVal.c new file mode 100644 index 00000000000..cea6d1e1522 --- /dev/null +++ b/test/CFrontend/2008-01-26-ReadOnlyByVal.c @@ -0,0 +1,19 @@ +// RUN: %llvmgcc %s -S -O1 -o - | llvm-as | opt -std-compile-opts | llvm-dis | not grep add + +struct S { int A; int B; int C; int D; }; + +int f(struct S x) __attribute__ ((const)); + +static int __attribute__ ((const)) g(struct S x) { + x.A = x.B; + return f(x); +} + +int h(void) { + struct S x; + int r; + x.A = 0; + x.B = 9; + r = g(x); + return r + x.A; +}