-Attribute Attribute::typeIncompatible(Type *Ty) {
- AttrBuilder Incompatible;
-
- if (!Ty->isIntegerTy())
- // Attribute that only apply to integers.
- Incompatible.addAttribute(Attribute::SExt)
- .addAttribute(Attribute::ZExt);
-
- if (!Ty->isPointerTy())
- // Attribute that only apply to pointers.
- Incompatible.addAttribute(Attribute::ByVal)
- .addAttribute(Attribute::Nest)
- .addAttribute(Attribute::NoAlias)
- .addAttribute(Attribute::NoCapture)
- .addAttribute(Attribute::StructRet);
-
- return Attribute::get(Ty->getContext(), Incompatible);
-}
-
-/// encodeLLVMAttributesForBitcode - This returns an integer containing an
-/// encoding of all the LLVM attributes found in the given attribute bitset.
-/// Any change to this encoding is a breaking change to bitcode compatibility.
-uint64_t Attribute::encodeLLVMAttributesForBitcode(Attribute Attrs) {
- // FIXME: It doesn't make sense to store the alignment information as an
- // expanded out value, we should store it as a log2 value. However, we can't
- // just change that here without breaking bitcode compatibility. If this ever
- // becomes a problem in practice, we should introduce new tag numbers in the
- // bitcode file and have those tags use a more efficiently encoded alignment
- // field.
-
- // Store the alignment in the bitcode as a 16-bit raw value instead of a 5-bit
- // log2 encoded value. Shift the bits above the alignment up by 11 bits.
- uint64_t EncodedAttrs = Attrs.Raw() & 0xffff;
- if (Attrs.hasAttribute(Attribute::Alignment))
- EncodedAttrs |= Attrs.getAlignment() << 16;
- EncodedAttrs |= (Attrs.Raw() & (0xffffULL << 21)) << 11;
- return EncodedAttrs;
-}
-
-/// decodeLLVMAttributesForBitcode - This returns an attribute bitset containing
-/// the LLVM attributes that have been decoded from the given integer. This
-/// function must stay in sync with 'encodeLLVMAttributesForBitcode'.
-Attribute Attribute::decodeLLVMAttributesForBitcode(LLVMContext &C,
- uint64_t EncodedAttrs) {
- // The alignment is stored as a 16-bit raw value from bits 31--16. We shift
- // the bits above 31 down by 11 bits.
- unsigned Alignment = (EncodedAttrs & (0xffffULL << 16)) >> 16;
- assert((!Alignment || isPowerOf2_32(Alignment)) &&
- "Alignment must be a power of two.");
-
- AttrBuilder B(EncodedAttrs & 0xffff);
- if (Alignment)
- B.addAlignmentAttr(Alignment);
- B.addRawValue((EncodedAttrs & (0xffffULL << 32)) >> 11);
- return Attribute::get(C, B);
-}
-
-std::string Attribute::getAsString() const {
- std::string Result;
- if (hasAttribute(Attribute::ZExt))
- Result += "zeroext ";
- if (hasAttribute(Attribute::SExt))
- Result += "signext ";
- if (hasAttribute(Attribute::NoReturn))
- Result += "noreturn ";
- if (hasAttribute(Attribute::NoUnwind))
- Result += "nounwind ";
- if (hasAttribute(Attribute::UWTable))
- Result += "uwtable ";
- if (hasAttribute(Attribute::ReturnsTwice))
- Result += "returns_twice ";
- if (hasAttribute(Attribute::InReg))
- Result += "inreg ";
- if (hasAttribute(Attribute::NoAlias))
- Result += "noalias ";
- if (hasAttribute(Attribute::NoCapture))
- Result += "nocapture ";
- if (hasAttribute(Attribute::StructRet))
- Result += "sret ";
- if (hasAttribute(Attribute::ByVal))
- Result += "byval ";
- if (hasAttribute(Attribute::Nest))
- Result += "nest ";
- if (hasAttribute(Attribute::ReadNone))
- Result += "readnone ";
- if (hasAttribute(Attribute::ReadOnly))
- Result += "readonly ";
- if (hasAttribute(Attribute::OptimizeForSize))
- Result += "optsize ";
- if (hasAttribute(Attribute::NoInline))
- Result += "noinline ";
- if (hasAttribute(Attribute::InlineHint))
- Result += "inlinehint ";
- if (hasAttribute(Attribute::AlwaysInline))
- Result += "alwaysinline ";
- if (hasAttribute(Attribute::StackProtect))
- Result += "ssp ";
- if (hasAttribute(Attribute::StackProtectReq))
- Result += "sspreq ";
- if (hasAttribute(Attribute::StackProtectStrong))
- Result += "sspstrong ";
- if (hasAttribute(Attribute::NoRedZone))
- Result += "noredzone ";
- if (hasAttribute(Attribute::NoImplicitFloat))
- Result += "noimplicitfloat ";
- if (hasAttribute(Attribute::Naked))
- Result += "naked ";
- if (hasAttribute(Attribute::NonLazyBind))
- Result += "nonlazybind ";
- if (hasAttribute(Attribute::AddressSafety))
- Result += "address_safety ";
- if (hasAttribute(Attribute::MinSize))
- Result += "minsize ";
- if (hasAttribute(Attribute::StackAlignment)) {
- Result += "alignstack(";
- Result += utostr(getStackAlignment());
- Result += ") ";
- }
- if (hasAttribute(Attribute::Alignment)) {
- Result += "align ";
- Result += utostr(getAlignment());
- Result += " ";
- }
- if (hasAttribute(Attribute::NoDuplicate))
- Result += "noduplicate ";
- // Trim the trailing space.
- assert(!Result.empty() && "Unknown attribute!");
- Result.erase(Result.end()-1);
- return Result;