Index: include/llvm/IR/Argument.h =================================================================== --- include/llvm/IR/Argument.h +++ include/llvm/IR/Argument.h @@ -59,6 +59,11 @@ /// containing function. bool hasByValAttr() const; + /// \brief Return true if this argument has the byval attribute or inalloca + /// attribute on it in its containing function. These attributes are very + /// similar from the perspective of the callee. + bool hasByValOrInAllocaAttr() const; + /// \brief If this is a byval or inalloca argument, return its alignment. unsigned getParamAlignment() const; Index: include/llvm/IR/Instructions.h =================================================================== --- include/llvm/IR/Instructions.h +++ include/llvm/IR/Instructions.h @@ -32,6 +32,7 @@ class ConstantRange; class DataLayout; class LLVMContext; +class ImmutableCallSite; enum AtomicOrdering { NotAtomic = 0, @@ -110,6 +111,14 @@ /// into the prolog/epilog code, so it is basically free. bool isStaticAlloca() const; + /// isUsedWithInAlloca - Return true if this alloca is used as an inalloca + /// argument to a call. Such allocas are never considered static. + bool isUsedWithInAlloca() const; + + /// getInAllocaCallSite - Get the call site which uses this alloca with the + /// InAlloca attribute, or nothing. There can be zero or one such call sites. + ImmutableCallSite getInAllocaCallSite() const; + // Methods for support type inquiry through isa, cast, and dyn_cast: static inline bool classof(const Instruction *I) { return (I->getOpcode() == Instruction::Alloca); Index: include/llvm/Support/CallSite.h =================================================================== --- include/llvm/Support/CallSite.h +++ include/llvm/Support/CallSite.h @@ -334,6 +334,7 @@ class ImmutableCallSite : public CallSiteBase<> { typedef CallSiteBase<> Base; public: + ImmutableCallSite() {} ImmutableCallSite(const Value* V) : Base(V) {} ImmutableCallSite(const CallInst *CI) : Base(CI) {} ImmutableCallSite(const InvokeInst *II) : Base(II) {} Index: lib/Analysis/MemoryBuiltins.cpp =================================================================== --- lib/Analysis/MemoryBuiltins.cpp +++ lib/Analysis/MemoryBuiltins.cpp @@ -458,7 +458,7 @@ SizeOffsetType ObjectSizeOffsetVisitor::visitArgument(Argument &A) { // no interprocedural analysis is done at the moment - if (!A.hasByValAttr()) { + if (!A.hasByValOrInAllocaAttr()) { ++ObjectVisitorArgument; return unknown(); } Index: lib/Analysis/ValueTracking.cpp =================================================================== --- lib/Analysis/ValueTracking.cpp +++ lib/Analysis/ValueTracking.cpp @@ -311,8 +311,9 @@ if (Argument *A = dyn_cast(V)) { unsigned Align = 0; - if (A->hasByValAttr()) { - // Get alignment information off byval arguments if specified in the IR. + if (A->hasByValOrInAllocaAttr()) { + // Get alignment information off byval/inalloca arguments if specified in + // the IR. Align = A->getParamAlignment(); } else if (TD && A->hasStructRetAttr()) { // An sret parameter has at least the ABI alignment of the return type. @@ -2070,9 +2071,9 @@ // Alloca never returns null, malloc might. if (isa(V)) return true; - // A byval argument is never null. + // A byval or inalloca argument is never null. if (const Argument *A = dyn_cast(V)) - return A->hasByValAttr(); + return A->hasByValOrInAllocaAttr(); // Global values are not null unless extern weak. if (const GlobalValue *GV = dyn_cast(V)) Index: lib/IR/Function.cpp =================================================================== --- lib/IR/Function.cpp +++ lib/IR/Function.cpp @@ -92,6 +92,13 @@ hasAttribute(getArgNo()+1, Attribute::InAlloca); } +bool Argument::hasByValOrInAllocaAttr() const { + if (!getType()->isPointerTy()) return false; + AttributeSet Attrs = getParent()->getAttributes(); + return Attrs.hasAttribute(getArgNo() + 1, Attribute::ByVal) || + Attrs.hasAttribute(getArgNo() + 1, Attribute::InAlloca); +} + unsigned Argument::getParamAlignment() const { assert(getType()->isPointerTy() && "Only pointers have alignments"); return getParent()->getParamAlignment(getArgNo()+1); Index: lib/IR/Instructions.cpp =================================================================== --- lib/IR/Instructions.cpp +++ lib/IR/Instructions.cpp @@ -916,7 +916,26 @@ // Must be in the entry block. const BasicBlock *Parent = getParent(); - return Parent == &Parent->getParent()->front(); + return Parent == &Parent->getParent()->front() && !isUsedWithInAlloca(); +} + +bool AllocaInst::isUsedWithInAlloca() const { + // This is linear in the number of uses. If it becomes hot, we can cache the + // result in Instruction's free subclass bits and require passes forming + // inalloca arguments to invalidate the cache. + return bool(getInAllocaCallSite()); +} + +ImmutableCallSite AllocaInst::getInAllocaCallSite() const { + for (const_use_iterator UI = use_begin(), UE = use_end(); UI != UE; ++UI) { + ImmutableCallSite CS(*UI); + if (!CS) + continue; + unsigned ArgNo = CS.getArgumentNo(UI); + if (CS.paramHasAttr(1 + ArgNo, Attribute::InAlloca)) + return CS; + } + return ImmutableCallSite(); } //===----------------------------------------------------------------------===// Index: lib/Transforms/IPO/ArgumentPromotion.cpp =================================================================== --- lib/Transforms/IPO/ArgumentPromotion.cpp +++ lib/Transforms/IPO/ArgumentPromotion.cpp @@ -155,7 +155,8 @@ Type *AgTy = cast(PtrArg->getType())->getElementType(); // If this is a byval argument, and if the aggregate type is small, just - // pass the elements, which is always safe. + // pass the elements, which is always safe. This does not apply to + // inalloca. if (PtrArg->hasByValAttr()) { if (StructType *STy = dyn_cast(AgTy)) { if (maxElements > 0 && STy->getNumElements() > maxElements) { @@ -201,7 +202,7 @@ } // Otherwise, see if we can promote the pointer to its value. - if (isSafeToPromoteArgument(PtrArg, PtrArg->hasByValAttr())) + if (isSafeToPromoteArgument(PtrArg, PtrArg->hasByValOrInAllocaAttr())) ArgsToPromote.insert(PtrArg); } @@ -301,7 +302,8 @@ /// This method limits promotion of aggregates to only promote up to three /// elements of the aggregate in order to avoid exploding the number of /// arguments passed in. -bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg, bool isByVal) const { +bool ArgPromotion::isSafeToPromoteArgument(Argument *Arg, + bool isByValOrInAlloca) const { typedef std::set GEPIndicesSet; // Quick exit for unused arguments @@ -323,6 +325,9 @@ // // This set will contain all sets of indices that are loaded in the entry // block, and thus are safe to unconditionally load in the caller. + // + // This optimization is also safe for InAlloca parameters, because it verifies + // that the address isn't captured. GEPIndicesSet SafeToUnconditionallyLoad; // This set contains all the sets of indices that we are planning to promote. @@ -330,7 +335,7 @@ GEPIndicesSet ToPromote; // If the pointer is always valid, any load with first index 0 is valid. - if (isByVal || AllCallersPassInValidPointerForArgument(Arg)) + if (isByValOrInAlloca || AllCallersPassInValidPointerForArgument(Arg)) SafeToUnconditionallyLoad.insert(IndicesVector(1, 0)); // First, iterate the entry block and mark loads of (geps of) arguments as @@ -389,7 +394,7 @@ // TODO: This runs the above loop over and over again for dead GEPs // Couldn't we just do increment the UI iterator earlier and erase the // use? - return isSafeToPromoteArgument(Arg, isByVal); + return isSafeToPromoteArgument(Arg, isByValOrInAlloca); } // Ensure that all of the indices are constants. Index: lib/Transforms/IPO/DeadArgumentElimination.cpp =================================================================== --- lib/Transforms/IPO/DeadArgumentElimination.cpp +++ lib/Transforms/IPO/DeadArgumentElimination.cpp @@ -378,7 +378,7 @@ I != E; ++I) { Argument *Arg = I; - if (Arg->use_empty() && !Arg->hasByValAttr()) + if (Arg->use_empty() && !Arg->hasByValOrInAllocaAttr()) UnusedArgs.push_back(Arg->getArgNo()); } Index: lib/Transforms/IPO/FunctionAttrs.cpp =================================================================== --- lib/Transforms/IPO/FunctionAttrs.cpp +++ lib/Transforms/IPO/FunctionAttrs.cpp @@ -414,6 +414,11 @@ SmallSet Visited; int Count = 0; + // Don't mark inalloca parameters as readonly or readnone. They are always + // considered written by the call. + if (A->hasInAllocaAttr()) + return Attribute::None; + bool IsRead = false; // We don't need to track IsWritten. If A is written to, return immediately. Index: lib/Transforms/IPO/IPConstantPropagation.cpp =================================================================== --- lib/Transforms/IPO/IPConstantPropagation.cpp +++ lib/Transforms/IPO/IPConstantPropagation.cpp @@ -135,7 +135,7 @@ for (unsigned i = 0, e = ArgumentConstants.size(); i != e; ++i, ++AI) { // Do we have a constant argument? if (ArgumentConstants[i].second || AI->use_empty() || - (AI->hasByValAttr() && !F.onlyReadsMemory())) + AI->hasInAllocaAttr() || (AI->hasByValAttr() && !F.onlyReadsMemory())) continue; Value *V = ArgumentConstants[i].first; Index: lib/Transforms/InstCombine/InstCombineCalls.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineCalls.cpp +++ lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -767,10 +767,10 @@ if (!CI->isLosslessCast()) return false; - // The size of ByVal arguments is derived from the type, so we + // The size of ByVal or InAlloca arguments is derived from the type, so we // can't change to a type with a different size. If the size were // passed explicitly we could avoid this check. - if (!CS.isByValArgument(ix)) + if (!CS.isByValOrInAllocaArgument(ix)) return true; Type* SrcTy = @@ -1048,6 +1048,9 @@ typeIncompatible(ParamTy, i + 1), i + 1)) return false; // Attribute not compatible with transformed value. + if (CS.isInAllocaArgument(i)) + return false; // Inalloca arguments cannot be bitcast. + // If the parameter is passed as a byval argument, then we have to have a // sized type and the sized type has to have the same size as the old type. if (ParamTy != ActTy && Index: lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp =================================================================== --- lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp +++ lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp @@ -81,10 +81,14 @@ if (CS.isCallee(UI)) continue; + // Inalloca arguments are considered to be clobbered by the call. + unsigned ArgNo = CS.getArgumentNo(UI); + if (CS.isInAllocaArgument(ArgNo)) + return false; + // If this is a readonly/readnone call site, then we know it is just a // load (but one that potentially returns the value itself), so we can // ignore it if we know that the value isn't captured. - unsigned ArgNo = CS.getArgumentNo(UI); if (CS.onlyReadsMemory() && (CS.getInstruction()->use_empty() || CS.doesNotCapture(ArgNo))) continue; Index: lib/Transforms/ObjCARC/ObjCARC.h =================================================================== --- lib/Transforms/ObjCARC/ObjCARC.h +++ lib/Transforms/ObjCARC/ObjCARC.h @@ -308,6 +308,7 @@ // Special arguments can not be a valid retainable object pointer. if (const Argument *Arg = dyn_cast(Op)) if (Arg->hasByValAttr() || + Arg->hasInAllocaAttr() || Arg->hasNestAttr() || Arg->hasStructRetAttr()) return false; Index: lib/Transforms/Scalar/DeadStoreElimination.cpp =================================================================== --- lib/Transforms/Scalar/DeadStoreElimination.cpp +++ lib/Transforms/Scalar/DeadStoreElimination.cpp @@ -374,8 +374,8 @@ return OverwriteUnknown; // Check to see if the later store is to the entire object (either a global, - // an alloca, or a byval argument). If so, then it clearly overwrites any - // other store to the same object. + // an alloca, or a byval/inalloca argument). If so, then it clearly + // overwrites any other store to the same object. const DataLayout *TD = AA.getDataLayout(); const Value *UO1 = GetUnderlyingObject(P1, TD), @@ -742,11 +742,11 @@ DeadStackObjects.insert(I); } - // Treat byval arguments the same, stores to them are dead at the end of the - // function. + // Treat byval or inalloca arguments the same, stores to them are dead at the + // end of the function. for (Function::arg_iterator AI = BB.getParent()->arg_begin(), AE = BB.getParent()->arg_end(); AI != AE; ++AI) - if (AI->hasByValAttr()) + if (AI->hasByValOrInAllocaAttr()) DeadStackObjects.insert(AI); // Scan the basic block backwards Index: test/Transforms/ArgumentPromotion/inalloca-2.ll =================================================================== --- /dev/null +++ test/Transforms/ArgumentPromotion/inalloca-2.ll @@ -0,0 +1,18 @@ +; RUN: opt %s -argpromotion -S | FileCheck %s +; Argpromote can't promote %a because of the icmp use. + +%struct.ss = type { i32, i64 } + +define internal i1 @f(%struct.ss* inalloca %a, %struct.ss* %b) nounwind { +; CHECK: define internal i1 @f(%struct.ss* inalloca %a, %struct.ss* %b) + entry: + %c = icmp eq %struct.ss* %a, %b + ret i1 %c +} + +define i32 @test() { + %S = alloca %struct.ss + %c = call i1 @f(%struct.ss* inalloca %S, %struct.ss* %S) +; CHECK: call i1 @f(%struct.ss* inalloca %S, %struct.ss* %S) + ret i32 0 +} Index: test/Transforms/ArgumentPromotion/inalloca.ll =================================================================== --- /dev/null +++ test/Transforms/ArgumentPromotion/inalloca.ll @@ -0,0 +1,28 @@ +; RUN: opt %s -argpromotion -scalarrepl -S | FileCheck %s +; CHECK-NOT: load + +target datalayout = "E-p:64:64:64-a0:0:8-f32:32:32-f64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-v64:64:64-v128:128:128" +; Argpromote + scalarrepl should change this to passing the two integers by value. + +%struct.ss = type { i32, i32 } + +define internal i32 @f(%struct.ss* inalloca %s) { +entry: + %f0 = getelementptr %struct.ss* %s, i32 0, i32 0 + %f1 = getelementptr %struct.ss* %s, i32 0, i32 1 + %a = load i32* %f0, align 4 + %b = load i32* %f1, align 4 + %r = add i32 %a, %b + ret i32 %r +} + +define i32 @main() { +entry: + %S = alloca %struct.ss + %f0 = getelementptr %struct.ss* %S, i32 0, i32 0 + %f1 = getelementptr %struct.ss* %S, i32 0, i32 1 + store i32 1, i32* %f0, align 4 + store i32 2, i32* %f1, align 4 + %r = call i32 @f(%struct.ss* inalloca %S) + ret i32 %r +} Index: test/Transforms/DeadStoreElimination/simple.ll =================================================================== --- test/Transforms/DeadStoreElimination/simple.ll +++ test/Transforms/DeadStoreElimination/simple.ll @@ -105,6 +105,15 @@ ; CHECK-NEXT: ret void } +; Test for inalloca handling. +define void @test9_2(%struct.x* inalloca %a) nounwind { + %tmp2 = getelementptr %struct.x* %a, i32 0, i32 0 + store i32 1, i32* %tmp2, align 4 + ret void +; CHECK-LABEL: @test9_2( +; CHECK-NEXT: ret void +} + ; va_arg has fuzzy dependence, the store shouldn't be zapped. define double @test10(i8* %X) { %X_addr = alloca i8* Index: test/Transforms/FunctionAttrs/readattrs.ll =================================================================== --- test/Transforms/FunctionAttrs/readattrs.ll +++ test/Transforms/FunctionAttrs/readattrs.ll @@ -45,3 +45,9 @@ call void @test6_1() ret void } + +; CHECK: define void @test7_1(i32* inalloca nocapture %a) +; inalloca parameters are always considered written +define void @test7_1(i32* inalloca %a) { + ret void +}