diff --git a/clang/lib/CodeGen/ItaniumCXXABI.cpp b/clang/lib/CodeGen/ItaniumCXXABI.cpp --- a/clang/lib/CodeGen/ItaniumCXXABI.cpp +++ b/clang/lib/CodeGen/ItaniumCXXABI.cpp @@ -2339,10 +2339,16 @@ QualType ElementType) { assert(requiresArrayCookie(expr)); - CharUnits SizeSize = CGF.getSizeSize(); - CharUnits CookieSize = getArrayCookieSizeImpl(ElementType); unsigned AS = NewPtr.getAddressSpace(); + ASTContext &Ctx = getContext(); + CharUnits SizeSize = CGF.getSizeSize(); + + // The size of the cookie. + CharUnits CookieSize = + std::max(SizeSize, Ctx.getPreferredTypeAlignInChars(ElementType)); + assert(CookieSize == getArrayCookieSizeImpl(ElementType)); + // Compute an offset to the cookie. Address CookiePtr = NewPtr; CharUnits CookieOffset = CookieSize - SizeSize; @@ -2418,19 +2424,11 @@ QualType elementType) { assert(requiresArrayCookie(expr)); - CharUnits sizeSize = CGF.getSizeSize(); - CharUnits cookieSize = getArrayCookieSizeImpl(elementType); unsigned AS = newPtr.getAddressSpace(); // The cookie is always at the start of the buffer. Address cookie = newPtr; - // Compute an offset to the cookie. - CharUnits cookieOffset = cookieSize - sizeSize*2; - assert(cookieOffset.isZero()); - if (!cookieOffset.isZero()) - cookie = CGF.Builder.CreateConstInBoundsByteGEP(cookie, cookieOffset); - // The first element is the element size. cookie = CGF.Builder.CreateElementBitCast(cookie, CGF.SizeTy); llvm::Value *elementSize = llvm::ConstantInt::get(CGF.SizeTy, @@ -2456,6 +2454,7 @@ // Finally, compute a pointer to the actual data buffer by skipping // over the cookie completely. + CharUnits cookieSize = ARMCXXABI::getArrayCookieSizeImpl(elementType); return CGF.Builder.CreateConstInBoundsByteGEP(newPtr, cookieSize); }