Example #1
0
  /// Returns the byte alignment of this type
  unsigned getTypeAlignment(const llvm::Type* type)
  {
    using namespace llvm;
    // Array types are aligned to their element type
    if (const ArrayType* psAT = dyn_cast<ArrayType>(type))
    {
      return getTypeAlignment(psAT->getElementType());
    }

    // Struct alignment is the size of its largest contained type
    if (const StructType* structT = dyn_cast<StructType>(type))
    {
      if (structT->isPacked())
        return 1;
      StructType* nonConstTy = const_cast<StructType*>(structT);
      unsigned uAlign = 0, uMaxAlign = 1;
      unsigned uCount = structT->getNumElements();
      for (unsigned i = 0; i < uCount; i++)
      {
          const Type* psElemType = nonConstTy->getTypeAtIndex(i);
          uAlign = getTypeAlignment(psElemType);

          if (uAlign > uMaxAlign)
            uMaxAlign = uAlign;
      }

      return uMaxAlign;
    }

    return getTypeSize(type);
  }