From cb3de0bc800d7920087b19bb12a545d4cc84114e Mon Sep 17 00:00:00 2001 From: Bill Wendling Date: Mon, 15 Oct 2012 04:46:55 +0000 Subject: Attributes Rewrite Convert the internal representation of the Attributes class into a pointer to an opaque object that's uniqued by and stored in the LLVMContext object. The Attributes class then becomes a thin wrapper around this opaque object. Eventually, the internal representation will be expanded to include attributes that represent code generation options, etc. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@165917 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/VMCore/Attributes.cpp | 35 ++++++++++++++++------------------- lib/VMCore/AutoUpgrade.cpp | 3 ++- lib/VMCore/Core.cpp | 33 +++++++++++++++++++++++---------- lib/VMCore/Function.cpp | 2 +- lib/VMCore/Verifier.cpp | 2 +- 5 files changed, 43 insertions(+), 32 deletions(-) (limited to 'lib/VMCore') diff --git a/lib/VMCore/Attributes.cpp b/lib/VMCore/Attributes.cpp index 15890f92b3..010a356dea 100644 --- a/lib/VMCore/Attributes.cpp +++ b/lib/VMCore/Attributes.cpp @@ -27,21 +27,18 @@ using namespace llvm; // Attributes Implementation //===----------------------------------------------------------------------===// -Attributes::Attributes(uint64_t Val) : Attrs(Val) {} - -Attributes::Attributes(LLVMContext &C, AttrVal Val) - : Attrs(Attributes::get(Attributes::Builder().addAttribute(Val)).Attrs) {} +Attributes::Attributes(LLVMContext &C, ArrayRef Vals) { + Attributes::Builder B; + for (ArrayRef::iterator I = Vals.begin(), E = Vals.end(); + I != E; ++I) + B.addAttribute(*I); + Attrs = Attributes::get(C, B).Attrs; +} -Attributes::Attributes(AttributesImpl *A) : Attrs(A->Bits) {} +Attributes::Attributes(AttributesImpl *A) : Attrs(A) {} Attributes::Attributes(const Attributes &A) : Attrs(A.Attrs) {} -// FIXME: This is temporary until we have implemented the uniquified version of -// AttributesImpl. -Attributes Attributes::get(Attributes::Builder &B) { - return Attributes(B.Bits); -} - Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) { // If there are no attributes, return an empty Attributes class. if (B.Bits == 0) @@ -67,18 +64,18 @@ Attributes Attributes::get(LLVMContext &Context, Attributes::Builder &B) { } bool Attributes::hasAttribute(AttrVal Val) const { - return Attrs.hasAttribute(Val); + return Attrs && Attrs->hasAttribute(Val); } bool Attributes::hasAttributes(const Attributes &A) const { - return Attrs.hasAttributes(A); + return Attrs && Attrs->hasAttributes(A); } /// This returns the alignment field of an attribute as a byte alignment value. unsigned Attributes::getAlignment() const { if (!hasAttribute(Attributes::Alignment)) return 0; - return 1U << ((Attrs.getAlignment() >> 16) - 1); + return 1U << ((Attrs->getAlignment() >> 16) - 1); } /// This returns the stack alignment field of an attribute as a byte alignment @@ -86,11 +83,11 @@ unsigned Attributes::getAlignment() const { unsigned Attributes::getStackAlignment() const { if (!hasAttribute(Attributes::StackAlignment)) return 0; - return 1U << ((Attrs.getStackAlignment() >> 26) - 1); + return 1U << ((Attrs->getStackAlignment() >> 26) - 1); } uint64_t Attributes::Raw() const { - return Attrs.Bits; + return Attrs ? Attrs->Bits : 0; // FIXME: Don't access this directly! } Attributes Attributes::typeIncompatible(Type *Ty) { @@ -109,7 +106,7 @@ Attributes Attributes::typeIncompatible(Type *Ty) { .addAttribute(Attributes::NoCapture) .addAttribute(Attributes::StructRet); - return Attributes(Incompatible.Bits); // FIXME: Use Attributes::get(). + return Attributes::get(Ty->getContext(), Incompatible); } std::string Attributes::getAsString() const { @@ -514,7 +511,7 @@ AttrListPtr AttrListPtr::addAttr(LLVMContext &C, unsigned Idx, // If there are attributes already at this index, merge them in. if (i != e && OldAttrList[i].Index == Idx) { Attrs = - Attributes::get(Attributes::Builder(Attrs). + Attributes::get(C, Attributes::Builder(Attrs). addAttributes(OldAttrList[i].Attrs)); ++i; } @@ -555,7 +552,7 @@ AttrListPtr AttrListPtr::removeAttr(LLVMContext &C, unsigned Idx, // If there are attributes already at this index, merge them in. assert(OldAttrList[i].Index == Idx && "Attribute isn't set?"); - Attrs = Attributes::get(Attributes::Builder(OldAttrList[i].Attrs). + Attrs = Attributes::get(C, Attributes::Builder(OldAttrList[i].Attrs). removeAttributes(Attrs)); ++i; if (Attrs.hasAttributes()) // If any attributes left for this param, add them. diff --git a/lib/VMCore/AutoUpgrade.cpp b/lib/VMCore/AutoUpgrade.cpp index 094ca75513..5fff460e8b 100644 --- a/lib/VMCore/AutoUpgrade.cpp +++ b/lib/VMCore/AutoUpgrade.cpp @@ -148,7 +148,8 @@ bool llvm::UpgradeIntrinsicFunction(Function *F, Function *&NewFn) { if (NewFn) F = NewFn; if (unsigned id = F->getIntrinsicID()) - F->setAttributes(Intrinsic::getAttributes((Intrinsic::ID)id)); + F->setAttributes(Intrinsic::getAttributes(F->getContext(), + (Intrinsic::ID)id)); return Upgraded; } diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index c6da24be93..6643ad6e9a 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -1381,16 +1381,20 @@ void LLVMSetGC(LLVMValueRef Fn, const char *GC) { void LLVMAddFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { Function *Func = unwrap(Fn); const AttrListPtr PAL = Func->getAttributes(); - const AttrListPtr PALnew = PAL.addAttr(Func->getContext(), ~0U, - Attributes(PA)); + Attributes::Builder B(PA); + const AttrListPtr PALnew = + PAL.addAttr(Func->getContext(), ~0U, + Attributes::get(Func->getContext(), B)); Func->setAttributes(PALnew); } void LLVMRemoveFunctionAttr(LLVMValueRef Fn, LLVMAttribute PA) { Function *Func = unwrap(Fn); const AttrListPtr PAL = Func->getAttributes(); - const AttrListPtr PALnew = PAL.removeAttr(Func->getContext(), ~0U, - Attributes(PA)); + Attributes::Builder B(PA); + const AttrListPtr PALnew = + PAL.removeAttr(Func->getContext(), ~0U, + Attributes::get(Func->getContext(), B)); Func->setAttributes(PALnew); } @@ -1460,11 +1464,15 @@ LLVMValueRef LLVMGetPreviousParam(LLVMValueRef Arg) { } void LLVMAddAttribute(LLVMValueRef Arg, LLVMAttribute PA) { - unwrap(Arg)->addAttr(Attributes(PA)); + Argument *A = unwrap(Arg); + Attributes::Builder B(PA); + A->addAttr(Attributes::get(A->getContext(), B)); } void LLVMRemoveAttribute(LLVMValueRef Arg, LLVMAttribute PA) { - unwrap(Arg)->removeAttr(Attributes(PA)); + Argument *A = unwrap(Arg); + Attributes::Builder B(PA); + A->removeAttr(Attributes::get(A->getContext(), B)); } LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) { @@ -1478,7 +1486,8 @@ LLVMAttribute LLVMGetAttribute(LLVMValueRef Arg) { void LLVMSetParamAlignment(LLVMValueRef Arg, unsigned align) { Attributes::Builder B; B.addAlignmentAttr(align); - unwrap(Arg)->addAttr(Attributes::get(B)); + unwrap(Arg)->addAttr(Attributes:: + get(unwrap(Arg)->getContext(), B)); } /*--.. Operations on basic blocks ..........................................--*/ @@ -1667,15 +1676,19 @@ void LLVMSetInstructionCallConv(LLVMValueRef Instr, unsigned CC) { void LLVMAddInstrAttribute(LLVMValueRef Instr, unsigned index, LLVMAttribute PA) { CallSite Call = CallSite(unwrap(Instr)); + Attributes::Builder B(PA); Call.setAttributes( - Call.getAttributes().addAttr(Call->getContext(), index, Attributes(PA))); + Call.getAttributes().addAttr(Call->getContext(), index, + Attributes::get(Call->getContext(), B))); } void LLVMRemoveInstrAttribute(LLVMValueRef Instr, unsigned index, LLVMAttribute PA) { CallSite Call = CallSite(unwrap(Instr)); + Attributes::Builder B(PA); Call.setAttributes( - Call.getAttributes().removeAttr(Call->getContext(), index, Attributes(PA))); + Call.getAttributes().removeAttr(Call->getContext(), index, + Attributes::get(Call->getContext(), B))); } void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, @@ -1684,7 +1697,7 @@ void LLVMSetInstrParamAlignment(LLVMValueRef Instr, unsigned index, Attributes::Builder B; B.addAlignmentAttr(align); Call.setAttributes(Call.getAttributes().addAttr(Call->getContext(), index, - Attributes::get(B))); + Attributes::get(Call->getContext(), B))); } /*--.. Operations on call instructions (only) ..............................--*/ diff --git a/lib/VMCore/Function.cpp b/lib/VMCore/Function.cpp index cbf1037f17..9c4f2d9399 100644 --- a/lib/VMCore/Function.cpp +++ b/lib/VMCore/Function.cpp @@ -185,7 +185,7 @@ Function::Function(FunctionType *Ty, LinkageTypes Linkage, // Ensure intrinsics have the right parameter attributes. if (unsigned IID = getIntrinsicID()) - setAttributes(Intrinsic::getAttributes(Intrinsic::ID(IID))); + setAttributes(Intrinsic::getAttributes(getContext(), Intrinsic::ID(IID))); } diff --git a/lib/VMCore/Verifier.cpp b/lib/VMCore/Verifier.cpp index 5d431df281..da1a452d3e 100644 --- a/lib/VMCore/Verifier.cpp +++ b/lib/VMCore/Verifier.cpp @@ -618,7 +618,7 @@ void Verifier::VerifyFunctionAttrs(FunctionType *FT, Attributes::Builder NotFn(FAttrs); NotFn.removeFunctionOnlyAttrs(); Assert1(!NotFn.hasAttributes(), "Attributes '" + - Attributes::get(NotFn).getAsString() + + Attributes::get(V->getContext(), NotFn).getAsString() + "' do not apply to the function!", V); // Check for mutually incompatible attributes. -- cgit v1.2.3