[clang] [clang] Implement C2y stdc_store8_* functions and their builtin spellings (PR #241419)
NagaChaitanya Vellanki via cfe-commits
cfe-commits at lists.llvm.org
Wed Nov 11 09:56:20 PST 2026
https://github.com/chaitanyav created https://github.com/llvm/llvm-project/pull/241419
Implement all 32 stdc_store8_* functions from C2y:
stdc_store8_le{u,s}{8,16,32,64}
stdc_store8_be{u,s}{8,16,32,64}
stdc_store8_aligned_le{u,s}{8,16,32,64}
stdc_store8_aligned_be{u,s}{8,16,32,64}
These write an N-bit integer to N/8 bytes of an unsigned char* in little- or big-endian byte order.
Constexpr evaluation is supported in both the AST and bytecode interpreters. Codegen emits an integer store, preceded by llvm.bswap when the requested byte order differs from the target's.
The load8 and store8 evaluators now share their pointer checks and byte iteration. No change to stdc_load8_* behavior.
>From 5ee89de9743f832a5f8b37d18fd08ca06833228e Mon Sep 17 00:00:00 2001
From: NagaChaitanya Vellanki <pnagato at protonmail.com>
Date: Tue, 20 Oct 2026 13:01:31 -0700
Subject: [PATCH] [clang] Implement C2y stdc_store8_* functions and their
builtin spellings
Implement all 32 stdc_store8_* functions from C2y:
stdc_store8_le{u,s}{8,16,32,64}
stdc_store8_be{u,s}{8,16,32,64}
stdc_store8_aligned_le{u,s}{8,16,32,64}
stdc_store8_aligned_be{u,s}{8,16,32,64}
These write an N-bit integer to N/8 bytes of an unsigned char* in
little- or big-endian byte order.
Constexpr evaluation is supported in both the AST and bytecode
interpreters. Codegen emits an integer store, preceded by
llvm.bswap when the requested byte order differs from the target's.
The load8 and store8 evaluators now share their pointer checks and
byte iteration. No change to stdc_load8_* behavior.
---
clang/docs/ReleaseNotes.md | 7 +
clang/include/clang/Basic/Builtins.td | 58 ++-
clang/lib/AST/ByteCode/InterpBuiltin.cpp | 177 +++++--
clang/lib/AST/ExprConstant.cpp | 159 +++++-
clang/lib/CodeGen/CGBuiltin.cpp | 95 ++++
clang/lib/Sema/SemaChecking.cpp | 64 +++
clang/test/CodeGen/builtin-stdc-store8.c | 481 ++++++++++++++++++
clang/test/Sema/Inputs/stdbit.h | 40 ++
clang/test/Sema/builtin-stdc-store8.c | 128 +++++
.../SemaCXX/constexpr-builtin-stdc-store8.cpp | 282 ++++++++++
10 files changed, 1425 insertions(+), 66 deletions(-)
create mode 100644 clang/test/CodeGen/builtin-stdc-store8.c
create mode 100644 clang/test/Sema/builtin-stdc-store8.c
create mode 100644 clang/test/SemaCXX/constexpr-builtin-stdc-store8.cpp
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 0f9e7e01636d71..6063e306021ab2 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -327,6 +327,13 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
and assemble them into an integer using little-endian or big-endian
byte order.
+- Implemented the C2y `<stdbit.h>` endian-aware 8-bit store functions
+ `stdc_store8_{le,be}{u,s}{8,16,32,64}` and their aligned counterparts
+ `stdc_store8_aligned_{le,be}{u,s}{8,16,32,64}`. These functions write an
+ integer to N/8 bytes of an `unsigned char *` using little-endian or
+ big-endian byte order. They can be evaluated in constant expressions
+ through their `__builtin_` spellings.
+
- Clang now diagnoses the use of the same identifier with both internal and
external linkage within a translation unit, as made ill-formed by
[N3410](https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3410.pdf).
diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td
index 15693d2fe128e5..cb5ca9225408bb 100644
--- a/clang/include/clang/Basic/Builtins.td
+++ b/clang/include/clang/Basic/Builtins.td
@@ -38,11 +38,11 @@ class MemReverse8Template : Template<
["uint8_t", "uint16_t", "uint32_t", "uint64_t"],
["u8", "u16", "u32", "u64"]>;
-class Load8UnsignedTemplate : Template<
+class Load8Store8UnsignedTemplate : Template<
["uint_least8_t", "uint_least16_t", "uint_least32_t", "uint_least64_t"],
["8", "16", "32", "64"]>;
-class Load8SignedTemplate : Template<
+class Load8Store8SignedTemplate : Template<
["int_least8_t", "int_least16_t", "int_least32_t", "int_least64_t"],
["8", "16", "32", "64"]>;
@@ -1016,38 +1016,76 @@ class Load8Common : LibBuiltin<"stdbit.h", "C2Y_LANG"> {
let Prototype = "T(const unsigned char*)";
}
-def StdcLoad8LeU : Load8Common, Load8UnsignedTemplate {
+def StdcLoad8LeU : Load8Common, Load8Store8UnsignedTemplate {
let Spellings = ["stdc_load8_leu"];
}
-def StdcLoad8BeU : Load8Common, Load8UnsignedTemplate {
+def StdcLoad8BeU : Load8Common, Load8Store8UnsignedTemplate {
let Spellings = ["stdc_load8_beu"];
}
-def StdcLoad8AlignedLeU : Load8Common, Load8UnsignedTemplate {
+def StdcLoad8AlignedLeU : Load8Common, Load8Store8UnsignedTemplate {
let Spellings = ["stdc_load8_aligned_leu"];
}
-def StdcLoad8AlignedBeU : Load8Common, Load8UnsignedTemplate {
+def StdcLoad8AlignedBeU : Load8Common, Load8Store8UnsignedTemplate {
let Spellings = ["stdc_load8_aligned_beu"];
}
-def StdcLoad8LeS : Load8Common, Load8SignedTemplate {
+def StdcLoad8LeS : Load8Common, Load8Store8SignedTemplate {
let Spellings = ["stdc_load8_les"];
}
-def StdcLoad8BeS : Load8Common, Load8SignedTemplate {
+def StdcLoad8BeS : Load8Common, Load8Store8SignedTemplate {
let Spellings = ["stdc_load8_bes"];
}
-def StdcLoad8AlignedLeS : Load8Common, Load8SignedTemplate {
+def StdcLoad8AlignedLeS : Load8Common, Load8Store8SignedTemplate {
let Spellings = ["stdc_load8_aligned_les"];
}
-def StdcLoad8AlignedBeS : Load8Common, Load8SignedTemplate {
+def StdcLoad8AlignedBeS : Load8Common, Load8Store8SignedTemplate {
let Spellings = ["stdc_load8_aligned_bes"];
}
+class Store8Common : LibBuiltin<"stdbit.h", "C2Y_LANG"> {
+ let Attributes = [NoThrow, Constexpr, NonNull<NonOptimizing, [1]>];
+ let Prototype = "void(T, unsigned char*)";
+ let AddBuiltinPrefixedAlias = 1;
+}
+
+def StdcStore8LeU : Store8Common, Load8Store8UnsignedTemplate {
+ let Spellings = ["stdc_store8_leu"];
+}
+
+def StdcStore8BeU : Store8Common, Load8Store8UnsignedTemplate {
+ let Spellings = ["stdc_store8_beu"];
+}
+
+def StdcStore8AlignedLeU : Store8Common, Load8Store8UnsignedTemplate {
+ let Spellings = ["stdc_store8_aligned_leu"];
+}
+
+def StdcStore8AlignedBeU : Store8Common, Load8Store8UnsignedTemplate {
+ let Spellings = ["stdc_store8_aligned_beu"];
+}
+
+def StdcStore8LeS : Store8Common, Load8Store8SignedTemplate {
+ let Spellings = ["stdc_store8_les"];
+}
+
+def StdcStore8BeS : Store8Common, Load8Store8SignedTemplate {
+ let Spellings = ["stdc_store8_bes"];
+}
+
+def StdcStore8AlignedLeS : Store8Common, Load8Store8SignedTemplate {
+ let Spellings = ["stdc_store8_aligned_les"];
+}
+
+def StdcStore8AlignedBeS : Store8Common, Load8Store8SignedTemplate {
+ let Spellings = ["stdc_store8_aligned_bes"];
+}
+
// Random GCC builtins
// FIXME: The builtins marked FunctionWithBuiltinPrefix below should be
diff --git a/clang/lib/AST/ByteCode/InterpBuiltin.cpp b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
index e35e198da0dcbe..80c64be959f34c 100644
--- a/clang/lib/AST/ByteCode/InterpBuiltin.cpp
+++ b/clang/lib/AST/ByteCode/InterpBuiltin.cpp
@@ -2240,15 +2240,20 @@ static bool interp__builtin_stdc_memreverse8(InterpState &S, CodePtr OpPC,
return true;
}
-static bool interp__builtin_load8(InterpState &S, CodePtr OpPC,
- const InterpFrame *Frame,
- const CallExpr *Call, bool IsBigEndian,
- bool IsAligned) {
- Pointer Ptr = S.Stk.pop<Pointer>();
-
+/// Checks that the bytes of a ValueTy at Ptr can be accessed with AK, then
+/// calls Fn(BytePtr, ElemT, Shift) for each byte in memory order. The aligned_*
+/// variants also require Ptr to be aligned to ValueTy. With N the bit width of
+/// ValueTy, the byte at Ptr[I] is placed at bit Shift of the value, where
+/// Shift is 8 * I for little-endian and 8 * (N/8 - I - 1) for big-endian. This
+/// matches the b_index mapping in §7.18.21 (loads) and the index sequence in
+/// §7.18.22 (stores).
+static bool forEachLoad8Store8Byte(
+ InterpState &S, CodePtr OpPC, const CallExpr *Call, Pointer Ptr,
+ QualType ValueTy, AccessKinds AK, bool IsBigEndian, bool IsAligned,
+ llvm::function_ref<bool(const Pointer &, PrimType, unsigned)> Fn) {
if (Ptr.isZero()) {
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_access_null)
- << AK_Read;
+ << AK;
return false;
}
@@ -2259,8 +2264,7 @@ static bool interp__builtin_load8(InterpState &S, CodePtr OpPC,
return false;
if (IsAligned) {
- CharUnits RequiredAlign =
- S.getASTContext().getTypeAlignInChars(Call->getType());
+ CharUnits RequiredAlign = S.getASTContext().getTypeAlignInChars(ValueTy);
CharUnits BaseAlignment = getBaseAlignment(S.getASTContext(), Ptr);
std::optional<size_t> LayoutOffset =
Ptr.computeLayoutOffset(S.getASTContext());
@@ -2289,7 +2293,7 @@ static bool interp__builtin_load8(InterpState &S, CodePtr OpPC,
uint64_t ArraySize = Ptr.getNumElems();
uint64_t RemainingElems = ArraySize - BaseIdx;
- unsigned ByteWidth = S.getASTContext().getTypeSize(Call->getType()) / 8;
+ unsigned ByteWidth = S.getASTContext().getTypeSize(ValueTy) / 8;
if (ByteWidth > RemainingElems) {
uint64_t LastIndex = llvm::SaturatingAdd(BaseIdx, uint64_t(ByteWidth - 1));
S.FFDiag(S.Current->getSource(OpPC), diag::note_constexpr_array_index)
@@ -2300,34 +2304,68 @@ static bool interp__builtin_load8(InterpState &S, CodePtr OpPC,
OptPrimType ElemT = S.getContext().classify(ElemTy);
if (!ElemT || !isIntegerOrBoolType(*ElemT))
return false;
- uint64_t Result = 0;
+ assert((IsArray || ByteWidth == 1) &&
+ "non-array pointer, expected a 1-byte access");
- if (IsArray) {
- // C2y §7.18.21: result = sum(b_index * 2^(8*index)) for index in [0, N/8)
- // where b_index = ptr[index] (LE) or ptr[N/8 - index - 1] (BE).
- for (unsigned I = 0; I != ByteWidth; ++I) {
- size_t SrcIdx = IsBigEndian ? (ByteWidth - I - 1) : I;
- Pointer BytePtr = Ptr.atIndex(BaseIdx + SrcIdx);
- if (!CheckLoad(S, OpPC, BytePtr, AK_Read))
- return false;
- INT_TYPE_SWITCH(*ElemT, {
- Result |= BytePtr.load<T>().toAPSInt().getZExtValue() << (8 * I);
- });
- }
- } else {
- assert(ByteWidth == 1 && "non-array pointer, expected a 1-byte load");
- if (!CheckLoad(S, OpPC, Ptr, AK_Read))
+ for (unsigned I = 0; I != ByteWidth; ++I) {
+ Pointer BytePtr = IsArray ? Ptr.atIndex(BaseIdx + I) : Ptr;
+ unsigned Shift = 8 * (IsBigEndian ? ByteWidth - I - 1 : I);
+ if (!Fn(BytePtr, *ElemT, Shift))
return false;
- INT_TYPE_SWITCH(*ElemT,
- { Result = Ptr.load<T>().toAPSInt().getZExtValue(); });
}
+ return true;
+}
- bool IsSigned = Call->getType()->isSignedIntegerType();
- pushInteger(S, APSInt(APInt(ByteWidth * 8, Result), !IsSigned),
- Call->getType());
+static bool interp__builtin_load8(InterpState &S, CodePtr OpPC,
+ const InterpFrame *Frame,
+ const CallExpr *Call, bool IsBigEndian,
+ bool IsAligned) {
+ Pointer Ptr = S.Stk.pop<Pointer>();
+ QualType ResultTy = Call->getType();
+
+ uint64_t Result = 0;
+ if (!forEachLoad8Store8Byte(
+ S, OpPC, Call, Ptr, ResultTy, AK_Read, IsBigEndian, IsAligned,
+ [&](const Pointer &BytePtr, PrimType ElemT, unsigned Shift) {
+ if (!CheckLoad(S, OpPC, BytePtr, AK_Read))
+ return false;
+ INT_TYPE_SWITCH(ElemT, {
+ Result |= BytePtr.load<T>().toAPSInt().getZExtValue() << Shift;
+ });
+ return true;
+ }))
+ return false;
+
+ unsigned BitWidth = S.getASTContext().getTypeSize(ResultTy);
+ pushInteger(S,
+ APSInt(APInt(BitWidth, Result), !ResultTy->isSignedIntegerType()),
+ ResultTy);
return true;
}
+static bool interp__builtin_store8(InterpState &S, CodePtr OpPC,
+ const InterpFrame *Frame,
+ const CallExpr *Call, bool IsBigEndian,
+ bool IsAligned) {
+ Pointer Ptr = S.Stk.pop<Pointer>();
+ QualType ValueTy = Call->getArg(0)->getType();
+ uint64_t ValueBits;
+ if (!popToUInt64(S, Call->getArg(0), ValueBits))
+ return false;
+
+ return forEachLoad8Store8Byte(
+ S, OpPC, Call, Ptr, ValueTy, AK_Assign, IsBigEndian, IsAligned,
+ [&](const Pointer &BytePtr, PrimType ElemT, unsigned Shift) {
+ if (!CheckStore(S, OpPC, BytePtr))
+ return false;
+ uint8_t Byte = ValueBits >> Shift;
+ FIXED_SIZE_INT_TYPE_SWITCH(ElemT,
+ { BytePtr.deref<T>() = T::from(Byte); });
+ BytePtr.initialize();
+ return true;
+ });
+}
+
static bool interp__builtin_memcmp(InterpState &S, CodePtr OpPC,
const InterpFrame *Frame,
const CallExpr *Call, unsigned ID) {
@@ -4808,6 +4846,7 @@ static bool interp__builtin_meta_is_type(InterpState &S, CodePtr OpPC,
bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
uint32_t BuiltinID) {
const InterpFrame *Frame = S.Current;
+
switch (BuiltinID) {
case Builtin::BI__builtin_is_constant_evaluated:
return interp__builtin_is_constant_evaluated(S, OpPC, Frame, Call);
@@ -5384,6 +5423,82 @@ bool InterpretBuiltin(InterpState &S, CodePtr OpPC, const CallExpr *Call,
return interp__builtin_load8(S, OpPC, Frame, Call, /*IsBigEndian=*/true,
/*IsAligned=*/true);
+ case Builtin::BIstdc_store8_leu8:
+ case Builtin::BI__builtin_stdc_store8_leu8:
+ case Builtin::BIstdc_store8_leu16:
+ case Builtin::BI__builtin_stdc_store8_leu16:
+ case Builtin::BIstdc_store8_leu32:
+ case Builtin::BI__builtin_stdc_store8_leu32:
+ case Builtin::BIstdc_store8_leu64:
+ case Builtin::BI__builtin_stdc_store8_leu64:
+ case Builtin::BIstdc_store8_les8:
+ case Builtin::BI__builtin_stdc_store8_les8:
+ case Builtin::BIstdc_store8_les16:
+ case Builtin::BI__builtin_stdc_store8_les16:
+ case Builtin::BIstdc_store8_les32:
+ case Builtin::BI__builtin_stdc_store8_les32:
+ case Builtin::BIstdc_store8_les64:
+ case Builtin::BI__builtin_stdc_store8_les64:
+ return interp__builtin_store8(S, OpPC, Frame, Call, /*IsBigEndian=*/false,
+ /*IsAligned=*/false);
+
+ case Builtin::BIstdc_store8_aligned_leu8:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu8:
+ case Builtin::BIstdc_store8_aligned_leu16:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu16:
+ case Builtin::BIstdc_store8_aligned_leu32:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu32:
+ case Builtin::BIstdc_store8_aligned_leu64:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu64:
+ case Builtin::BIstdc_store8_aligned_les8:
+ case Builtin::BI__builtin_stdc_store8_aligned_les8:
+ case Builtin::BIstdc_store8_aligned_les16:
+ case Builtin::BI__builtin_stdc_store8_aligned_les16:
+ case Builtin::BIstdc_store8_aligned_les32:
+ case Builtin::BI__builtin_stdc_store8_aligned_les32:
+ case Builtin::BIstdc_store8_aligned_les64:
+ case Builtin::BI__builtin_stdc_store8_aligned_les64:
+ return interp__builtin_store8(S, OpPC, Frame, Call, /*IsBigEndian=*/false,
+ /*IsAligned=*/true);
+
+ case Builtin::BIstdc_store8_beu8:
+ case Builtin::BI__builtin_stdc_store8_beu8:
+ case Builtin::BIstdc_store8_beu16:
+ case Builtin::BI__builtin_stdc_store8_beu16:
+ case Builtin::BIstdc_store8_beu32:
+ case Builtin::BI__builtin_stdc_store8_beu32:
+ case Builtin::BIstdc_store8_beu64:
+ case Builtin::BI__builtin_stdc_store8_beu64:
+ case Builtin::BIstdc_store8_bes8:
+ case Builtin::BI__builtin_stdc_store8_bes8:
+ case Builtin::BIstdc_store8_bes16:
+ case Builtin::BI__builtin_stdc_store8_bes16:
+ case Builtin::BIstdc_store8_bes32:
+ case Builtin::BI__builtin_stdc_store8_bes32:
+ case Builtin::BIstdc_store8_bes64:
+ case Builtin::BI__builtin_stdc_store8_bes64:
+ return interp__builtin_store8(S, OpPC, Frame, Call, /*IsBigEndian=*/true,
+ /*IsAligned=*/false);
+
+ case Builtin::BIstdc_store8_aligned_beu8:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu8:
+ case Builtin::BIstdc_store8_aligned_beu16:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu16:
+ case Builtin::BIstdc_store8_aligned_beu32:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu32:
+ case Builtin::BIstdc_store8_aligned_beu64:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu64:
+ case Builtin::BIstdc_store8_aligned_bes8:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes8:
+ case Builtin::BIstdc_store8_aligned_bes16:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes16:
+ case Builtin::BIstdc_store8_aligned_bes32:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes32:
+ case Builtin::BIstdc_store8_aligned_bes64:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes64:
+ return interp__builtin_store8(S, OpPC, Frame, Call, /*IsBigEndian=*/true,
+ /*IsAligned=*/true);
+
case Builtin::BI__atomic_always_lock_free:
case Builtin::BI__atomic_is_lock_free:
return interp__builtin_atomic_lock_free(S, OpPC, Frame, Call, BuiltinID);
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 57d5bb475d0f9b..690d0349b2cc63 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -17161,25 +17161,29 @@ static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info,
return true;
}
-// Implements the C2y §7.18.21 stdc_load8_* family: reads N bytes from
-// E->getArg(0) and assembles them per IsBE into a value of E's type. The
-// aligned_* variants additionally require the pointer to be aligned to the
-// natural alignment of E's type.
-static bool EvaluateStdcLoad8(EvalInfo &Info, const CallExpr *E, bool IsBE,
- bool IsAligned, APSInt &Result) {
+/// Evaluates PtrArg and checks that the bytes of a ValueTy it points to can be
+/// accessed with AK, then calls Fn(BytePtr, CharTy, Shift) for each byte in
+/// memory order. The aligned_* variants also require the pointer to be aligned
+/// to ValueTy. With N the bit width of ValueTy, the byte at Ptr[I] is placed at
+/// bit Shift of the value, where Shift is 8 * I for little-endian and
+/// 8 * (N/8 - I - 1) for big-endian. This matches the b_index mapping in
+/// §7.18.21 (loads) and the index sequence in §7.18.22 (stores).
+static bool ForEachStdcLoadStore8Byte(
+ EvalInfo &Info, const CallExpr *E, const Expr *PtrArg, QualType ValueTy,
+ AccessKinds AK, bool IsBE, bool IsAligned,
+ llvm::function_ref<bool(const LValue &, QualType, unsigned)> Fn) {
LValue Ptr;
- if (!EvaluatePointer(E->getArg(0), Ptr, Info))
+ if (!EvaluatePointer(PtrArg, Ptr, Info))
return false;
- if (!Ptr.checkNullPointerForFoldAccess(Info, E, AK_Read) ||
- Ptr.Designator.Invalid)
+ if (!Ptr.checkNullPointerForFoldAccess(Info, E, AK) || Ptr.Designator.Invalid)
return false;
if (Ptr.Designator.isMostDerivedAnUnsizedArray())
return false;
if (IsAligned) {
- CharUnits RequiredAlign = Info.Ctx.getTypeAlignInChars(E->getType());
+ CharUnits RequiredAlign = Info.Ctx.getTypeAlignInChars(ValueTy);
CharUnits BaseAlignment = getBaseAlignment(Info, Ptr);
CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(Ptr.Offset);
if (PtrAlign < RequiredAlign) {
@@ -17191,7 +17195,7 @@ static bool EvaluateStdcLoad8(EvalInfo &Info, const CallExpr *E, bool IsBE,
}
QualType CharTy = Ptr.Designator.getType(Info.Ctx);
- unsigned ByteWidth = Info.Ctx.getTypeSize(E->getType()) / 8;
+ unsigned ByteWidth = Info.Ctx.getTypeSize(ValueTy) / 8;
uint64_t RemainingElems = Ptr.Designator.validIndexAdjustments().second;
if (ByteWidth > RemainingElems) {
uint64_t ArrayIndex = Ptr.Designator.MostDerivedIsArrayElement
@@ -17202,27 +17206,60 @@ static bool EvaluateStdcLoad8(EvalInfo &Info, const CallExpr *E, bool IsBE,
return false;
}
- // Load the bytes and assemble them per C2y §7.18.21:
- // result = sum(b_index * 2^(8*index)) where b_index = ptr[index] (LE)
- // or ptr[N/8 - index - 1] (BE).
- uint64_t ResultBits = 0;
- LValue BytePtr = Ptr;
for (unsigned I = 0; I < ByteWidth; ++I) {
- APValue ByteVal;
- if (!handleLValueToRValueConversion(Info, E, CharTy, BytePtr, ByteVal))
- return false;
unsigned Shift = 8 * (IsBE ? ByteWidth - I - 1 : I);
- ResultBits |= ByteVal.getInt().getZExtValue() << Shift;
- if (I + 1 < ByteWidth)
- if (!HandleLValueArrayAdjustment(Info, E, BytePtr, CharTy, 1))
- return false;
+ if (!Fn(Ptr, CharTy, Shift))
+ return false;
+ if (I + 1 < ByteWidth &&
+ !HandleLValueArrayAdjustment(Info, E, Ptr, CharTy, 1))
+ return false;
}
+ return true;
+}
+
+// Implements the C2y §7.18.21 stdc_load8_* family: reads N bytes from
+// E->getArg(0) and assembles them per IsBE into a value of E's type.
+static bool EvaluateStdcLoad8(EvalInfo &Info, const CallExpr *E, bool IsBE,
+ bool IsAligned, APSInt &Result) {
+ QualType ResultTy = E->getType();
+ uint64_t ResultBits = 0;
+ if (!ForEachStdcLoadStore8Byte(
+ Info, E, E->getArg(0), ResultTy, AK_Read, IsBE, IsAligned,
+ [&](const LValue &BytePtr, QualType CharTy, unsigned Shift) {
+ APValue ByteVal;
+ if (!handleLValueToRValueConversion(Info, E, CharTy, BytePtr,
+ ByteVal))
+ return false;
+ ResultBits |= ByteVal.getInt().getZExtValue() << Shift;
+ return true;
+ }))
+ return false;
- bool IsSigned = E->getType()->isSignedIntegerType();
- Result = APSInt(APInt(ByteWidth * 8, ResultBits), !IsSigned);
+ unsigned BitWidth = Info.Ctx.getTypeSize(ResultTy);
+ Result =
+ APSInt(APInt(BitWidth, ResultBits), !ResultTy->isSignedIntegerType());
return true;
}
+// Implements the C2y §7.18.22 stdc_store8_* family: writes the N-bit value
+// E->getArg(0) to the bytes at E->getArg(1) per IsBE.
+static bool EvaluateStdcStore8(EvalInfo &Info, const CallExpr *E, bool IsBE,
+ bool IsAligned) {
+ const Expr *ValueArg = E->getArg(0);
+ APSInt Value;
+ if (!EvaluateInteger(ValueArg, Value, Info))
+ return false;
+
+ uint64_t ValueBits = Value.getZExtValue();
+ return ForEachStdcLoadStore8Byte(
+ Info, E, E->getArg(1), ValueArg->getType(), AK_Assign, IsBE, IsAligned,
+ [&](const LValue &BytePtr, QualType CharTy, unsigned Shift) {
+ APValue ByteVal(APSInt(APInt(8, (ValueBits >> Shift) & 0xFF),
+ !CharTy->isSignedIntegerType()));
+ return handleAssignment(Info, E, BytePtr, CharTy, ByteVal);
+ });
+}
+
bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
unsigned BuiltinOp) {
if (Info.Ctx.BuiltinInfo.isReflectionMetafunctionBuiltin(BuiltinOp)) {
@@ -21985,6 +22022,78 @@ class VoidExprEvaluator
case Builtin::BI__builtin_operator_delete:
return HandleOperatorDeleteCall(Info, E);
+ case Builtin::BIstdc_store8_leu8:
+ case Builtin::BI__builtin_stdc_store8_leu8:
+ case Builtin::BIstdc_store8_leu16:
+ case Builtin::BI__builtin_stdc_store8_leu16:
+ case Builtin::BIstdc_store8_leu32:
+ case Builtin::BI__builtin_stdc_store8_leu32:
+ case Builtin::BIstdc_store8_leu64:
+ case Builtin::BI__builtin_stdc_store8_leu64:
+ case Builtin::BIstdc_store8_les8:
+ case Builtin::BI__builtin_stdc_store8_les8:
+ case Builtin::BIstdc_store8_les16:
+ case Builtin::BI__builtin_stdc_store8_les16:
+ case Builtin::BIstdc_store8_les32:
+ case Builtin::BI__builtin_stdc_store8_les32:
+ case Builtin::BIstdc_store8_les64:
+ case Builtin::BI__builtin_stdc_store8_les64:
+ return EvaluateStdcStore8(Info, E, /*IsBE=*/false, /*IsAligned=*/false);
+
+ case Builtin::BIstdc_store8_aligned_leu8:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu8:
+ case Builtin::BIstdc_store8_aligned_leu16:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu16:
+ case Builtin::BIstdc_store8_aligned_leu32:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu32:
+ case Builtin::BIstdc_store8_aligned_leu64:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu64:
+ case Builtin::BIstdc_store8_aligned_les8:
+ case Builtin::BI__builtin_stdc_store8_aligned_les8:
+ case Builtin::BIstdc_store8_aligned_les16:
+ case Builtin::BI__builtin_stdc_store8_aligned_les16:
+ case Builtin::BIstdc_store8_aligned_les32:
+ case Builtin::BI__builtin_stdc_store8_aligned_les32:
+ case Builtin::BIstdc_store8_aligned_les64:
+ case Builtin::BI__builtin_stdc_store8_aligned_les64:
+ return EvaluateStdcStore8(Info, E, /*IsBE=*/false, /*IsAligned=*/true);
+
+ case Builtin::BIstdc_store8_beu8:
+ case Builtin::BI__builtin_stdc_store8_beu8:
+ case Builtin::BIstdc_store8_beu16:
+ case Builtin::BI__builtin_stdc_store8_beu16:
+ case Builtin::BIstdc_store8_beu32:
+ case Builtin::BI__builtin_stdc_store8_beu32:
+ case Builtin::BIstdc_store8_beu64:
+ case Builtin::BI__builtin_stdc_store8_beu64:
+ case Builtin::BIstdc_store8_bes8:
+ case Builtin::BI__builtin_stdc_store8_bes8:
+ case Builtin::BIstdc_store8_bes16:
+ case Builtin::BI__builtin_stdc_store8_bes16:
+ case Builtin::BIstdc_store8_bes32:
+ case Builtin::BI__builtin_stdc_store8_bes32:
+ case Builtin::BIstdc_store8_bes64:
+ case Builtin::BI__builtin_stdc_store8_bes64:
+ return EvaluateStdcStore8(Info, E, /*IsBE=*/true, /*IsAligned=*/false);
+
+ case Builtin::BIstdc_store8_aligned_beu8:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu8:
+ case Builtin::BIstdc_store8_aligned_beu16:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu16:
+ case Builtin::BIstdc_store8_aligned_beu32:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu32:
+ case Builtin::BIstdc_store8_aligned_beu64:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu64:
+ case Builtin::BIstdc_store8_aligned_bes8:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes8:
+ case Builtin::BIstdc_store8_aligned_bes16:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes16:
+ case Builtin::BIstdc_store8_aligned_bes32:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes32:
+ case Builtin::BIstdc_store8_aligned_bes64:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes64:
+ return EvaluateStdcStore8(Info, E, /*IsBE=*/true, /*IsAligned=*/true);
+
case Builtin::BIstdc_memreverse8:
case Builtin::BI__builtin_stdc_memreverse8: {
APSInt N;
diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp
index c520016ab1a513..babaf6c6b7ccc0 100644
--- a/clang/lib/CodeGen/CGBuiltin.cpp
+++ b/clang/lib/CodeGen/CGBuiltin.cpp
@@ -4087,6 +4087,101 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
return RValue::get(Val);
}
+ // stdc_store8_*: store an N-bit integer into an unsigned char* using
+ // little-endian or big-endian byte order.
+ case Builtin::BIstdc_store8_leu8:
+ case Builtin::BI__builtin_stdc_store8_leu8:
+ case Builtin::BIstdc_store8_les8:
+ case Builtin::BI__builtin_stdc_store8_les8:
+ case Builtin::BIstdc_store8_beu8:
+ case Builtin::BI__builtin_stdc_store8_beu8:
+ case Builtin::BIstdc_store8_bes8:
+ case Builtin::BI__builtin_stdc_store8_bes8:
+ case Builtin::BIstdc_store8_aligned_leu8:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu8:
+ case Builtin::BIstdc_store8_aligned_les8:
+ case Builtin::BI__builtin_stdc_store8_aligned_les8:
+ case Builtin::BIstdc_store8_aligned_beu8:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu8:
+ case Builtin::BIstdc_store8_aligned_bes8:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes8:
+ case Builtin::BIstdc_store8_leu16:
+ case Builtin::BI__builtin_stdc_store8_leu16:
+ case Builtin::BIstdc_store8_les16:
+ case Builtin::BI__builtin_stdc_store8_les16:
+ case Builtin::BIstdc_store8_beu16:
+ case Builtin::BI__builtin_stdc_store8_beu16:
+ case Builtin::BIstdc_store8_bes16:
+ case Builtin::BI__builtin_stdc_store8_bes16:
+ case Builtin::BIstdc_store8_aligned_leu16:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu16:
+ case Builtin::BIstdc_store8_aligned_les16:
+ case Builtin::BI__builtin_stdc_store8_aligned_les16:
+ case Builtin::BIstdc_store8_aligned_beu16:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu16:
+ case Builtin::BIstdc_store8_aligned_bes16:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes16:
+ case Builtin::BIstdc_store8_leu32:
+ case Builtin::BI__builtin_stdc_store8_leu32:
+ case Builtin::BIstdc_store8_les32:
+ case Builtin::BI__builtin_stdc_store8_les32:
+ case Builtin::BIstdc_store8_beu32:
+ case Builtin::BI__builtin_stdc_store8_beu32:
+ case Builtin::BIstdc_store8_bes32:
+ case Builtin::BI__builtin_stdc_store8_bes32:
+ case Builtin::BIstdc_store8_aligned_leu32:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu32:
+ case Builtin::BIstdc_store8_aligned_les32:
+ case Builtin::BI__builtin_stdc_store8_aligned_les32:
+ case Builtin::BIstdc_store8_aligned_beu32:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu32:
+ case Builtin::BIstdc_store8_aligned_bes32:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes32:
+ case Builtin::BIstdc_store8_leu64:
+ case Builtin::BI__builtin_stdc_store8_leu64:
+ case Builtin::BIstdc_store8_les64:
+ case Builtin::BI__builtin_stdc_store8_les64:
+ case Builtin::BIstdc_store8_beu64:
+ case Builtin::BI__builtin_stdc_store8_beu64:
+ case Builtin::BIstdc_store8_bes64:
+ case Builtin::BI__builtin_stdc_store8_bes64:
+ case Builtin::BIstdc_store8_aligned_leu64:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu64:
+ case Builtin::BIstdc_store8_aligned_les64:
+ case Builtin::BI__builtin_stdc_store8_aligned_les64:
+ case Builtin::BIstdc_store8_aligned_beu64:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu64:
+ case Builtin::BIstdc_store8_aligned_bes64:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes64: {
+ // The name is [__builtin_]stdc_store8_[aligned_]{le,be}{u,s}N.
+ std::string BuiltinName = getContext().BuiltinInfo.getName(BuiltinID);
+ StringRef Name = BuiltinName;
+ Name.consume_front("__builtin_");
+ Name.consume_front("stdc_store8_");
+ bool IsAligned = Name.consume_front("aligned_");
+ bool IsBE = Name.starts_with("be");
+
+ Value *Val = EmitScalarExpr(E->getArg(0));
+ llvm::Type *IntTy = Val->getType();
+ unsigned BitWidth = IntTy->getIntegerBitWidth();
+ assert(
+ (BitWidth == 8 || BitWidth == 16 || BitWidth == 32 || BitWidth == 64) &&
+ "unexpected bit width for stdc_store8_*");
+ Address Addr = EmitPointerWithAlignment(E->getArg(1));
+ if (IsAligned) {
+ CharUnits TypeAlign =
+ getContext().getTypeAlignInChars(E->getArg(0)->getType());
+ Addr = Addr.withAlignment(std::max(Addr.getAlignment(), TypeAlign));
+ }
+ Addr = Addr.withElementType(IntTy);
+ if ((BitWidth == 16 || BitWidth == 32 || BitWidth == 64) &&
+ IsBE != getTarget().isBigEndian())
+ Val = Builder.CreateCall(CGM.getIntrinsic(Intrinsic::bswap, IntTy), Val);
+ Builder.CreateStore(Val, Addr);
+
+ return RValue::get(nullptr);
+ }
+
case Builtin::BI__builtin_constant_p: {
llvm::Type *ResultType = ConvertType(E->getType());
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index a273eef3526d3c..7eb6826976068d 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -4246,6 +4246,70 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
case Builtin::BIstdc_load8_aligned_bes16:
case Builtin::BIstdc_load8_aligned_bes32:
case Builtin::BIstdc_load8_aligned_bes64:
+ case Builtin::BIstdc_store8_leu8:
+ case Builtin::BI__builtin_stdc_store8_leu8:
+ case Builtin::BIstdc_store8_leu16:
+ case Builtin::BI__builtin_stdc_store8_leu16:
+ case Builtin::BIstdc_store8_leu32:
+ case Builtin::BI__builtin_stdc_store8_leu32:
+ case Builtin::BIstdc_store8_leu64:
+ case Builtin::BI__builtin_stdc_store8_leu64:
+ case Builtin::BIstdc_store8_les8:
+ case Builtin::BI__builtin_stdc_store8_les8:
+ case Builtin::BIstdc_store8_les16:
+ case Builtin::BI__builtin_stdc_store8_les16:
+ case Builtin::BIstdc_store8_les32:
+ case Builtin::BI__builtin_stdc_store8_les32:
+ case Builtin::BIstdc_store8_les64:
+ case Builtin::BI__builtin_stdc_store8_les64:
+ case Builtin::BIstdc_store8_beu8:
+ case Builtin::BI__builtin_stdc_store8_beu8:
+ case Builtin::BIstdc_store8_beu16:
+ case Builtin::BI__builtin_stdc_store8_beu16:
+ case Builtin::BIstdc_store8_beu32:
+ case Builtin::BI__builtin_stdc_store8_beu32:
+ case Builtin::BIstdc_store8_beu64:
+ case Builtin::BI__builtin_stdc_store8_beu64:
+ case Builtin::BIstdc_store8_bes8:
+ case Builtin::BI__builtin_stdc_store8_bes8:
+ case Builtin::BIstdc_store8_bes16:
+ case Builtin::BI__builtin_stdc_store8_bes16:
+ case Builtin::BIstdc_store8_bes32:
+ case Builtin::BI__builtin_stdc_store8_bes32:
+ case Builtin::BIstdc_store8_bes64:
+ case Builtin::BI__builtin_stdc_store8_bes64:
+ case Builtin::BIstdc_store8_aligned_leu8:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu8:
+ case Builtin::BIstdc_store8_aligned_leu16:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu16:
+ case Builtin::BIstdc_store8_aligned_leu32:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu32:
+ case Builtin::BIstdc_store8_aligned_leu64:
+ case Builtin::BI__builtin_stdc_store8_aligned_leu64:
+ case Builtin::BIstdc_store8_aligned_les8:
+ case Builtin::BI__builtin_stdc_store8_aligned_les8:
+ case Builtin::BIstdc_store8_aligned_les16:
+ case Builtin::BI__builtin_stdc_store8_aligned_les16:
+ case Builtin::BIstdc_store8_aligned_les32:
+ case Builtin::BI__builtin_stdc_store8_aligned_les32:
+ case Builtin::BIstdc_store8_aligned_les64:
+ case Builtin::BI__builtin_stdc_store8_aligned_les64:
+ case Builtin::BIstdc_store8_aligned_beu8:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu8:
+ case Builtin::BIstdc_store8_aligned_beu16:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu16:
+ case Builtin::BIstdc_store8_aligned_beu32:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu32:
+ case Builtin::BIstdc_store8_aligned_beu64:
+ case Builtin::BI__builtin_stdc_store8_aligned_beu64:
+ case Builtin::BIstdc_store8_aligned_bes8:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes8:
+ case Builtin::BIstdc_store8_aligned_bes16:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes16:
+ case Builtin::BIstdc_store8_aligned_bes32:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes32:
+ case Builtin::BIstdc_store8_aligned_bes64:
+ case Builtin::BI__builtin_stdc_store8_aligned_bes64:
if (Context.getTargetInfo().getCharWidth() != 8) {
Diag(TheCall->getBeginLoc(), diag::err_builtin_requires_char_bit_8)
<< TheCall->getDirectCallee()->getName();
diff --git a/clang/test/CodeGen/builtin-stdc-store8.c b/clang/test/CodeGen/builtin-stdc-store8.c
new file mode 100644
index 00000000000000..22a4c58da172be
--- /dev/null
+++ b/clang/test/CodeGen/builtin-stdc-store8.c
@@ -0,0 +1,481 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=c2y -isystem %S/../Sema/Inputs -O1 -emit-llvm -o - %s | FileCheck %s --check-prefix=LE
+// RUN: %clang_cc1 -triple s390x-unknown-unknown -std=c2y -isystem %S/../Sema/Inputs -O1 -emit-llvm -o - %s | FileCheck %s --check-prefix=BE
+
+#include <stdbit.h>
+
+// Every stdc_store8_* variant is lowered to an optional llvm.bswap (when the
+// requested byte order differs from the target's) followed by one integer
+// store. The unaligned variants store with align 1; the aligned_* variants use
+// the natural alignment of the value's type. 8-bit stores never need a swap.
+
+// LE-LABEL: @test_leu8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_leu8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_leu8(__UINT_LEAST8_TYPE__ v, unsigned char *p) { stdc_store8_leu8(v, p); }
+
+// LE-LABEL: @test_leu16(
+// LE: store i16 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_leu16(
+// BE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// BE-NEXT: store i16 %0, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_leu16(__UINT_LEAST16_TYPE__ v, unsigned char *p) { stdc_store8_leu16(v, p); }
+
+// LE-LABEL: @test_leu32(
+// LE: store i32 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_leu32(
+// BE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// BE-NEXT: store i32 %0, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_leu32(__UINT_LEAST32_TYPE__ v, unsigned char *p) { stdc_store8_leu32(v, p); }
+
+// LE-LABEL: @test_leu64(
+// LE: store i64 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_leu64(
+// BE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// BE-NEXT: store i64 %0, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_leu64(__UINT_LEAST64_TYPE__ v, unsigned char *p) { stdc_store8_leu64(v, p); }
+
+// LE-LABEL: @test_beu8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_beu8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_beu8(__UINT_LEAST8_TYPE__ v, unsigned char *p) { stdc_store8_beu8(v, p); }
+
+// LE-LABEL: @test_beu16(
+// LE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// LE-NEXT: store i16 %0, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_beu16(
+// BE: store i16 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_beu16(__UINT_LEAST16_TYPE__ v, unsigned char *p) { stdc_store8_beu16(v, p); }
+
+// LE-LABEL: @test_beu32(
+// LE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// LE-NEXT: store i32 %0, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_beu32(
+// BE: store i32 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_beu32(__UINT_LEAST32_TYPE__ v, unsigned char *p) { stdc_store8_beu32(v, p); }
+
+// LE-LABEL: @test_beu64(
+// LE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// LE-NEXT: store i64 %0, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_beu64(
+// BE: store i64 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_beu64(__UINT_LEAST64_TYPE__ v, unsigned char *p) { stdc_store8_beu64(v, p); }
+
+// LE-LABEL: @test_aligned_leu8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_leu8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_aligned_leu8(__UINT_LEAST8_TYPE__ v, unsigned char *p) { stdc_store8_aligned_leu8(v, p); }
+
+// LE-LABEL: @test_aligned_leu16(
+// LE: store i16 %v, ptr %p, align 2
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_leu16(
+// BE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// BE-NEXT: store i16 %0, ptr %p, align 2
+// BE-NEXT: ret void
+void test_aligned_leu16(__UINT_LEAST16_TYPE__ v, unsigned char *p) { stdc_store8_aligned_leu16(v, p); }
+
+// LE-LABEL: @test_aligned_leu32(
+// LE: store i32 %v, ptr %p, align 4
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_leu32(
+// BE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// BE-NEXT: store i32 %0, ptr %p, align 4
+// BE-NEXT: ret void
+void test_aligned_leu32(__UINT_LEAST32_TYPE__ v, unsigned char *p) { stdc_store8_aligned_leu32(v, p); }
+
+// LE-LABEL: @test_aligned_leu64(
+// LE: store i64 %v, ptr %p, align 8
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_leu64(
+// BE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// BE-NEXT: store i64 %0, ptr %p, align 8
+// BE-NEXT: ret void
+void test_aligned_leu64(__UINT_LEAST64_TYPE__ v, unsigned char *p) { stdc_store8_aligned_leu64(v, p); }
+
+// LE-LABEL: @test_aligned_beu8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_beu8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_aligned_beu8(__UINT_LEAST8_TYPE__ v, unsigned char *p) { stdc_store8_aligned_beu8(v, p); }
+
+// LE-LABEL: @test_aligned_beu16(
+// LE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// LE-NEXT: store i16 %0, ptr %p, align 2
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_beu16(
+// BE: store i16 %v, ptr %p, align 2
+// BE-NEXT: ret void
+void test_aligned_beu16(__UINT_LEAST16_TYPE__ v, unsigned char *p) { stdc_store8_aligned_beu16(v, p); }
+
+// LE-LABEL: @test_aligned_beu32(
+// LE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// LE-NEXT: store i32 %0, ptr %p, align 4
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_beu32(
+// BE: store i32 %v, ptr %p, align 4
+// BE-NEXT: ret void
+void test_aligned_beu32(__UINT_LEAST32_TYPE__ v, unsigned char *p) { stdc_store8_aligned_beu32(v, p); }
+
+// LE-LABEL: @test_aligned_beu64(
+// LE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// LE-NEXT: store i64 %0, ptr %p, align 8
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_beu64(
+// BE: store i64 %v, ptr %p, align 8
+// BE-NEXT: ret void
+void test_aligned_beu64(__UINT_LEAST64_TYPE__ v, unsigned char *p) { stdc_store8_aligned_beu64(v, p); }
+
+// LE-LABEL: @test_les8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_les8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_les8(__INT_LEAST8_TYPE__ v, unsigned char *p) { stdc_store8_les8(v, p); }
+
+// LE-LABEL: @test_les16(
+// LE: store i16 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_les16(
+// BE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// BE-NEXT: store i16 %0, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_les16(__INT_LEAST16_TYPE__ v, unsigned char *p) { stdc_store8_les16(v, p); }
+
+// LE-LABEL: @test_les32(
+// LE: store i32 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_les32(
+// BE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// BE-NEXT: store i32 %0, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_les32(__INT_LEAST32_TYPE__ v, unsigned char *p) { stdc_store8_les32(v, p); }
+
+// LE-LABEL: @test_les64(
+// LE: store i64 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_les64(
+// BE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// BE-NEXT: store i64 %0, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_les64(__INT_LEAST64_TYPE__ v, unsigned char *p) { stdc_store8_les64(v, p); }
+
+// LE-LABEL: @test_bes8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_bes8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_bes8(__INT_LEAST8_TYPE__ v, unsigned char *p) { stdc_store8_bes8(v, p); }
+
+// LE-LABEL: @test_bes16(
+// LE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// LE-NEXT: store i16 %0, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_bes16(
+// BE: store i16 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_bes16(__INT_LEAST16_TYPE__ v, unsigned char *p) { stdc_store8_bes16(v, p); }
+
+// LE-LABEL: @test_bes32(
+// LE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// LE-NEXT: store i32 %0, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_bes32(
+// BE: store i32 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_bes32(__INT_LEAST32_TYPE__ v, unsigned char *p) { stdc_store8_bes32(v, p); }
+
+// LE-LABEL: @test_bes64(
+// LE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// LE-NEXT: store i64 %0, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_bes64(
+// BE: store i64 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_bes64(__INT_LEAST64_TYPE__ v, unsigned char *p) { stdc_store8_bes64(v, p); }
+
+// LE-LABEL: @test_aligned_les8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_les8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_aligned_les8(__INT_LEAST8_TYPE__ v, unsigned char *p) { stdc_store8_aligned_les8(v, p); }
+
+// LE-LABEL: @test_aligned_les16(
+// LE: store i16 %v, ptr %p, align 2
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_les16(
+// BE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// BE-NEXT: store i16 %0, ptr %p, align 2
+// BE-NEXT: ret void
+void test_aligned_les16(__INT_LEAST16_TYPE__ v, unsigned char *p) { stdc_store8_aligned_les16(v, p); }
+
+// LE-LABEL: @test_aligned_les32(
+// LE: store i32 %v, ptr %p, align 4
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_les32(
+// BE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// BE-NEXT: store i32 %0, ptr %p, align 4
+// BE-NEXT: ret void
+void test_aligned_les32(__INT_LEAST32_TYPE__ v, unsigned char *p) { stdc_store8_aligned_les32(v, p); }
+
+// LE-LABEL: @test_aligned_les64(
+// LE: store i64 %v, ptr %p, align 8
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_les64(
+// BE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// BE-NEXT: store i64 %0, ptr %p, align 8
+// BE-NEXT: ret void
+void test_aligned_les64(__INT_LEAST64_TYPE__ v, unsigned char *p) { stdc_store8_aligned_les64(v, p); }
+
+// LE-LABEL: @test_aligned_bes8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_bes8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_aligned_bes8(__INT_LEAST8_TYPE__ v, unsigned char *p) { stdc_store8_aligned_bes8(v, p); }
+
+// LE-LABEL: @test_aligned_bes16(
+// LE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// LE-NEXT: store i16 %0, ptr %p, align 2
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_bes16(
+// BE: store i16 %v, ptr %p, align 2
+// BE-NEXT: ret void
+void test_aligned_bes16(__INT_LEAST16_TYPE__ v, unsigned char *p) { stdc_store8_aligned_bes16(v, p); }
+
+// LE-LABEL: @test_aligned_bes32(
+// LE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// LE-NEXT: store i32 %0, ptr %p, align 4
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_bes32(
+// BE: store i32 %v, ptr %p, align 4
+// BE-NEXT: ret void
+void test_aligned_bes32(__INT_LEAST32_TYPE__ v, unsigned char *p) { stdc_store8_aligned_bes32(v, p); }
+
+// LE-LABEL: @test_aligned_bes64(
+// LE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// LE-NEXT: store i64 %0, ptr %p, align 8
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_bes64(
+// BE: store i64 %v, ptr %p, align 8
+// BE-NEXT: ret void
+void test_aligned_bes64(__INT_LEAST64_TYPE__ v, unsigned char *p) { stdc_store8_aligned_bes64(v, p); }
+
+// A pointer with known alignment keeps it for the unaligned variants...
+alignas(4) unsigned char buf4[4];
+// LE-LABEL: @test_leu32_known_alignment(
+// LE: store i32 %v, ptr @buf4, align 4
+// LE-NEXT: ret void
+// BE-LABEL: @test_leu32_known_alignment(
+// BE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// BE-NEXT: store i32 %0, ptr @buf4, align 4
+// BE-NEXT: ret void
+void test_leu32_known_alignment(__UINT_LEAST32_TYPE__ v) { stdc_store8_leu32(v, buf4); }
+
+// ...and the aligned variants never lower it below the natural alignment.
+alignas(16) unsigned char buf16[16];
+// LE-LABEL: @test_aligned_leu64_known_alignment(
+// LE: store i64 %v, ptr @buf16, align 16
+// LE-NEXT: ret void
+// BE-LABEL: @test_aligned_leu64_known_alignment(
+// BE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// BE-NEXT: store i64 %0, ptr @buf16, align 16
+// BE-NEXT: ret void
+void test_aligned_leu64_known_alignment(__UINT_LEAST64_TYPE__ v) { stdc_store8_aligned_leu64(v, buf16); }
+
+// The result is void and the arguments are each evaluated exactly once.
+__UINT_LEAST32_TYPE__ next_value(void);
+unsigned char *next_ptr(void);
+// LE-LABEL: @test_single_evaluation(
+// LE: %call = tail call i32 @next_value()
+// LE-NEXT: %call1 = tail call ptr @next_ptr()
+// LE-NEXT: %0 = tail call i32 @llvm.bswap.i32(i32 %call)
+// LE-NEXT: store i32 %0, ptr %call1, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_single_evaluation(
+// BE: %call = tail call zeroext i32 @next_value()
+// BE-NEXT: %call1 = tail call ptr @next_ptr()
+// BE-NEXT: store i32 %call, ptr %call1, align 1{{$}}
+// BE-NEXT: ret void
+void test_single_evaluation(void) { stdc_store8_beu32(next_value(), next_ptr()); }
+
+// The destination may be a variable-length array.
+void use_buf(unsigned char *);
+// LE-LABEL: @test_vla(
+// LE: %0 = zext i32 %n to i64
+// LE-NEXT: %vla = alloca i8, i64 %0, align 16
+// LE-NEXT: %1 = tail call i32 @llvm.bswap.i32(i32 %v)
+// LE-NEXT: store i32 %1, ptr %vla, align 16
+// LE-NEXT: call void @use_buf(ptr noundef nonnull %vla)
+// LE-NEXT: ret void
+// BE-LABEL: @test_vla(
+// BE: %0 = zext i32 %n to i64
+// BE-NEXT: %vla = alloca i8, i64 %0, align 4
+// BE-NEXT: store i32 %v, ptr %vla, align 4
+// BE-NEXT: call void @use_buf(ptr noundef nonnull %vla)
+// BE-NEXT: ret void
+void test_vla(int n, __UINT_LEAST32_TYPE__ v) {
+ unsigned char buf[n];
+ stdc_store8_beu32(v, buf);
+ use_buf(buf);
+}
+
+// The __builtin_ spellings lower exactly like the stdc_ ones.
+// LE-LABEL: @test_builtin_leu16(
+// LE: store i16 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_builtin_leu16(
+// BE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// BE-NEXT: store i16 %0, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_builtin_leu16(__UINT_LEAST16_TYPE__ v, unsigned char *p) { __builtin_stdc_store8_leu16(v, p); }
+
+// LE-LABEL: @test_builtin_beu32(
+// LE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// LE-NEXT: store i32 %0, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_builtin_beu32(
+// BE: store i32 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_builtin_beu32(__UINT_LEAST32_TYPE__ v, unsigned char *p) { __builtin_stdc_store8_beu32(v, p); }
+
+// LE-LABEL: @test_builtin_aligned_bes64(
+// LE: %0 = tail call i64 @llvm.bswap.i64(i64 %v)
+// LE-NEXT: store i64 %0, ptr %p, align 8
+// LE-NEXT: ret void
+// BE-LABEL: @test_builtin_aligned_bes64(
+// BE: store i64 %v, ptr %p, align 8
+// BE-NEXT: ret void
+void test_builtin_aligned_bes64(__INT_LEAST64_TYPE__ v, unsigned char *p) { __builtin_stdc_store8_aligned_bes64(v, p); }
+
+// LE-LABEL: @test_builtin_les8(
+// LE: store i8 %v, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_builtin_les8(
+// BE: store i8 %v, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_builtin_les8(__INT_LEAST8_TYPE__ v, unsigned char *p) { __builtin_stdc_store8_les8(v, p); }
+
+// The alignment of the destination is derived from the pointer expression.
+alignas(8) unsigned char buf8[16];
+struct S { char c; alignas(4) unsigned char b[8]; };
+struct S gs;
+
+// LE-LABEL: @test_array_offset_2(
+// LE: store i16 %v, ptr getelementptr inbounds nuw (i8, ptr @buf8, i64 2), align 2
+// LE-NEXT: ret void
+// BE-LABEL: @test_array_offset_2(
+// BE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// BE-NEXT: store i16 %0, ptr getelementptr inbounds nuw (i8, ptr @buf8, i64 2), align 2
+// BE-NEXT: ret void
+void test_array_offset_2(__UINT_LEAST16_TYPE__ v) { stdc_store8_leu16(v, buf8 + 2); }
+
+// LE-LABEL: @test_array_offset_3(
+// LE: store i16 %v, ptr getelementptr inbounds nuw (i8, ptr @buf8, i64 3), align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_array_offset_3(
+// BE: %0 = tail call i16 @llvm.bswap.i16(i16 %v)
+// BE-NEXT: store i16 %0, ptr getelementptr inbounds nuw (i8, ptr @buf8, i64 3), align 1{{$}}
+// BE-NEXT: ret void
+void test_array_offset_3(__UINT_LEAST16_TYPE__ v) { stdc_store8_leu16(v, buf8 + 3); }
+
+// LE-LABEL: @test_member(
+// LE: store i32 %v, ptr getelementptr inbounds nuw (i8, ptr @gs, i64 4), align 4
+// LE-NEXT: ret void
+// BE-LABEL: @test_member(
+// BE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// BE-NEXT: store i32 %0, ptr getelementptr inbounds nuw (i8, ptr @gs, i64 4), align 4
+// BE-NEXT: ret void
+void test_member(__UINT_LEAST32_TYPE__ v) { stdc_store8_leu32(v, gs.b); }
+
+// LE-LABEL: @test_member_offset_4(
+// LE: store i32 %v, ptr getelementptr inbounds nuw (i8, ptr @gs, i64 8), align 4
+// LE-NEXT: ret void
+// BE-LABEL: @test_member_offset_4(
+// BE: %0 = tail call i32 @llvm.bswap.i32(i32 %v)
+// BE-NEXT: store i32 %0, ptr getelementptr inbounds nuw (i8, ptr @gs, i64 8), align 4
+// BE-NEXT: ret void
+void test_member_offset_4(__UINT_LEAST32_TYPE__ v) { stdc_store8_leu32(v, gs.b + 4); }
+
+// With constant arguments the byte swap folds into the stored constant.
+// LE-LABEL: @test_const_leu32(
+// LE: store i32 305419896, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_const_leu32(
+// BE: store i32 2018915346, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_const_leu32(unsigned char *p) { stdc_store8_leu32(0x12345678u, p); }
+
+// LE-LABEL: @test_const_beu32(
+// LE: store i32 2018915346, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_const_beu32(
+// BE: store i32 305419896, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_const_beu32(unsigned char *p) { stdc_store8_beu32(0x12345678u, p); }
+
+// LE-LABEL: @test_const_bes16(
+// LE: store i16 -257, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_const_bes16(
+// BE: store i16 -2, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_const_bes16(unsigned char *p) { stdc_store8_bes16(-2, p); }
+
+// LE-LABEL: @test_const_les16(
+// LE: store i16 -2, ptr %p, align 1{{$}}
+// LE-NEXT: ret void
+// BE-LABEL: @test_const_les16(
+// BE: store i16 -257, ptr %p, align 1{{$}}
+// BE-NEXT: ret void
+void test_const_les16(unsigned char *p) { stdc_store8_les16(-2, p); }
+
+// A big-endian store followed by a big-endian load folds to the identity.
+// LE-LABEL: @test_roundtrip(
+// LE: ret i32 %v
+// BE-LABEL: @test_roundtrip(
+// BE: ret i32 %v
+__UINT_LEAST32_TYPE__ test_roundtrip(__UINT_LEAST32_TYPE__ v) {
+ unsigned char b[4];
+ stdc_store8_beu32(v, b);
+ return stdc_load8_beu32(b);
+}
+
+// The store is visible to a load of the same bytes through another type.
+// LE-LABEL: @test_visible_through_int(
+// LE: store i32 16909060, ptr %ip, align 1{{$}}
+// LE-NEXT: ret i32 16909060
+// BE-LABEL: @test_visible_through_int(
+// BE: store i32 67305985, ptr %ip, align 1{{$}}
+// BE-NEXT: ret i32 67305985
+unsigned test_visible_through_int(unsigned *ip) {
+ stdc_store8_leu32(0x01020304u, (unsigned char *)ip);
+ return *ip;
+}
diff --git a/clang/test/Sema/Inputs/stdbit.h b/clang/test/Sema/Inputs/stdbit.h
index 9fa5ed7a655c25..51c0715bdcd411 100644
--- a/clang/test/Sema/Inputs/stdbit.h
+++ b/clang/test/Sema/Inputs/stdbit.h
@@ -158,4 +158,44 @@ __INT_LEAST16_TYPE__ stdc_load8_aligned_bes16(const unsigned char *);
__INT_LEAST32_TYPE__ stdc_load8_aligned_bes32(const unsigned char *);
__INT_LEAST64_TYPE__ stdc_load8_aligned_bes64(const unsigned char *);
+void stdc_store8_leu8(__UINT_LEAST8_TYPE__, unsigned char *);
+void stdc_store8_leu16(__UINT_LEAST16_TYPE__, unsigned char *);
+void stdc_store8_leu32(__UINT_LEAST32_TYPE__, unsigned char *);
+void stdc_store8_leu64(__UINT_LEAST64_TYPE__, unsigned char *);
+
+void stdc_store8_beu8(__UINT_LEAST8_TYPE__, unsigned char *);
+void stdc_store8_beu16(__UINT_LEAST16_TYPE__, unsigned char *);
+void stdc_store8_beu32(__UINT_LEAST32_TYPE__, unsigned char *);
+void stdc_store8_beu64(__UINT_LEAST64_TYPE__, unsigned char *);
+
+void stdc_store8_aligned_leu8(__UINT_LEAST8_TYPE__, unsigned char *);
+void stdc_store8_aligned_leu16(__UINT_LEAST16_TYPE__, unsigned char *);
+void stdc_store8_aligned_leu32(__UINT_LEAST32_TYPE__, unsigned char *);
+void stdc_store8_aligned_leu64(__UINT_LEAST64_TYPE__, unsigned char *);
+
+void stdc_store8_aligned_beu8(__UINT_LEAST8_TYPE__, unsigned char *);
+void stdc_store8_aligned_beu16(__UINT_LEAST16_TYPE__, unsigned char *);
+void stdc_store8_aligned_beu32(__UINT_LEAST32_TYPE__, unsigned char *);
+void stdc_store8_aligned_beu64(__UINT_LEAST64_TYPE__, unsigned char *);
+
+void stdc_store8_les8(__INT_LEAST8_TYPE__, unsigned char *);
+void stdc_store8_les16(__INT_LEAST16_TYPE__, unsigned char *);
+void stdc_store8_les32(__INT_LEAST32_TYPE__, unsigned char *);
+void stdc_store8_les64(__INT_LEAST64_TYPE__, unsigned char *);
+
+void stdc_store8_bes8(__INT_LEAST8_TYPE__, unsigned char *);
+void stdc_store8_bes16(__INT_LEAST16_TYPE__, unsigned char *);
+void stdc_store8_bes32(__INT_LEAST32_TYPE__, unsigned char *);
+void stdc_store8_bes64(__INT_LEAST64_TYPE__, unsigned char *);
+
+void stdc_store8_aligned_les8(__INT_LEAST8_TYPE__, unsigned char *);
+void stdc_store8_aligned_les16(__INT_LEAST16_TYPE__, unsigned char *);
+void stdc_store8_aligned_les32(__INT_LEAST32_TYPE__, unsigned char *);
+void stdc_store8_aligned_les64(__INT_LEAST64_TYPE__, unsigned char *);
+
+void stdc_store8_aligned_bes8(__INT_LEAST8_TYPE__, unsigned char *);
+void stdc_store8_aligned_bes16(__INT_LEAST16_TYPE__, unsigned char *);
+void stdc_store8_aligned_bes32(__INT_LEAST32_TYPE__, unsigned char *);
+void stdc_store8_aligned_bes64(__INT_LEAST64_TYPE__, unsigned char *);
+
#endif
diff --git a/clang/test/Sema/builtin-stdc-store8.c b/clang/test/Sema/builtin-stdc-store8.c
new file mode 100644
index 00000000000000..bb20a1602bf689
--- /dev/null
+++ b/clang/test/Sema/builtin-stdc-store8.c
@@ -0,0 +1,128 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=c2y -isystem %S/Inputs -fsyntax-only -verify %s
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -std=c2y -isystem %S/Inputs -fsyntax-only -verify %s -fexperimental-new-constant-interpreter
+#include <stdbit.h>
+
+// Null pointer is diagnosed (NonNull attribute).
+void test_null(void) {
+ stdc_store8_leu8(1, 0); // expected-warning{{null passed to a callee that requires a non-null argument}}
+ stdc_store8_beu32(1, nullptr); // expected-warning{{null passed to a callee that requires a non-null argument}}
+ stdc_store8_aligned_les64(1, 0); // expected-warning{{null passed to a callee that requires a non-null argument}}
+}
+
+// Only the destination pointer is non-null; a zero value is fine.
+void test_zero_value(unsigned char *p) {
+ stdc_store8_leu8(0, p);
+ stdc_store8_beu32(0, p);
+ stdc_store8_aligned_les64(0, p);
+}
+
+// Wrong pointer types are rejected by the type system at the call site.
+void test_wrong_types(void) {
+ int int_arr[1] = {0};
+ unsigned int uint_arr[1] = {0};
+ char char_arr[4] = "abc";
+
+ stdc_store8_leu32(1, int_arr); // expected-error{{incompatible pointer types}} expected-note at Inputs/stdbit.h:*{{passing argument to parameter here}}
+ stdc_store8_leu32(1, uint_arr); // expected-error{{incompatible pointer types}} expected-note at Inputs/stdbit.h:*{{passing argument to parameter here}}
+ stdc_store8_leu32(1, char_arr); // expected-warning{{converts between pointers to integer types}} expected-note at Inputs/stdbit.h:*{{passing argument to parameter here}}
+}
+
+// The destination is written to, so it must not be const.
+void test_const_destination(const unsigned char *cp) {
+ const unsigned char arr[4] = {0};
+ stdc_store8_leu32(1, cp); // expected-warning{{discards qualifiers}} expected-note at Inputs/stdbit.h:*{{passing argument to parameter here}}
+ stdc_store8_leu32(1, arr); // expected-warning{{discards qualifiers}} expected-note at Inputs/stdbit.h:*{{passing argument to parameter here}}
+}
+
+// Argument count is checked.
+void test_arg_count(unsigned char *p) {
+ stdc_store8_leu32(1); // expected-error{{too few arguments to function call}}
+ stdc_store8_leu32(1, p, p); // expected-error{{too many arguments to function call}}
+}
+
+// The functions return void.
+void test_return_type(unsigned char *p) {
+ __typeof__(stdc_store8_leu8(1, p)) *vp = 0;
+ void *check = vp;
+ (void)check;
+ int x = stdc_store8_leu8(1, p); // expected-error{{initializing 'int' with an expression of incompatible type 'void'}}
+}
+
+// Every width, signedness, byte order and alignment variant is accepted.
+void test_all_variants(unsigned char *p) {
+ alignas(8) unsigned char buf[8];
+ stdc_store8_leu8(1, p);
+ stdc_store8_les8(1, p);
+ stdc_store8_beu8(1, p);
+ stdc_store8_bes8(1, p);
+ stdc_store8_leu16(1, p);
+ stdc_store8_les16(1, p);
+ stdc_store8_beu16(1, p);
+ stdc_store8_bes16(1, p);
+ stdc_store8_leu32(1, p);
+ stdc_store8_les32(1, p);
+ stdc_store8_beu32(1, p);
+ stdc_store8_bes32(1, p);
+ stdc_store8_leu64(1, p);
+ stdc_store8_les64(1, p);
+ stdc_store8_beu64(1, p);
+ stdc_store8_bes64(1, p);
+ stdc_store8_aligned_leu8(1, buf);
+ stdc_store8_aligned_les8(1, buf);
+ stdc_store8_aligned_beu8(1, buf);
+ stdc_store8_aligned_bes8(1, buf);
+ stdc_store8_aligned_leu16(1, buf);
+ stdc_store8_aligned_les16(1, buf);
+ stdc_store8_aligned_beu16(1, buf);
+ stdc_store8_aligned_bes16(1, buf);
+ stdc_store8_aligned_leu32(1, buf);
+ stdc_store8_aligned_les32(1, buf);
+ stdc_store8_aligned_beu32(1, buf);
+ stdc_store8_aligned_bes32(1, buf);
+ stdc_store8_aligned_leu64(1, buf);
+ stdc_store8_aligned_les64(1, buf);
+ stdc_store8_aligned_beu64(1, buf);
+ stdc_store8_aligned_bes64(1, buf);
+}
+
+void test_all_builtin_variants(unsigned char *p) {
+ alignas(8) unsigned char buf[8];
+ __builtin_stdc_store8_leu8(1, p);
+ __builtin_stdc_store8_les8(1, p);
+ __builtin_stdc_store8_beu8(1, p);
+ __builtin_stdc_store8_bes8(1, p);
+ __builtin_stdc_store8_leu16(1, p);
+ __builtin_stdc_store8_les16(1, p);
+ __builtin_stdc_store8_beu16(1, p);
+ __builtin_stdc_store8_bes16(1, p);
+ __builtin_stdc_store8_leu32(1, p);
+ __builtin_stdc_store8_les32(1, p);
+ __builtin_stdc_store8_beu32(1, p);
+ __builtin_stdc_store8_bes32(1, p);
+ __builtin_stdc_store8_leu64(1, p);
+ __builtin_stdc_store8_les64(1, p);
+ __builtin_stdc_store8_beu64(1, p);
+ __builtin_stdc_store8_bes64(1, p);
+ __builtin_stdc_store8_aligned_leu8(1, buf);
+ __builtin_stdc_store8_aligned_les8(1, buf);
+ __builtin_stdc_store8_aligned_beu8(1, buf);
+ __builtin_stdc_store8_aligned_bes8(1, buf);
+ __builtin_stdc_store8_aligned_leu16(1, buf);
+ __builtin_stdc_store8_aligned_les16(1, buf);
+ __builtin_stdc_store8_aligned_beu16(1, buf);
+ __builtin_stdc_store8_aligned_bes16(1, buf);
+ __builtin_stdc_store8_aligned_leu32(1, buf);
+ __builtin_stdc_store8_aligned_les32(1, buf);
+ __builtin_stdc_store8_aligned_beu32(1, buf);
+ __builtin_stdc_store8_aligned_bes32(1, buf);
+ __builtin_stdc_store8_aligned_leu64(1, buf);
+ __builtin_stdc_store8_aligned_les64(1, buf);
+ __builtin_stdc_store8_aligned_beu64(1, buf);
+ __builtin_stdc_store8_aligned_bes64(1, buf);
+}
+
+// A call cannot be a constant expression in C: it modifies an object.
+void test_not_constant(void) {
+ unsigned char buf[4];
+ static_assert((stdc_store8_leu32(1, buf), 1), ""); // expected-error{{static assertion expression is not an integral constant expression}}
+}
diff --git a/clang/test/SemaCXX/constexpr-builtin-stdc-store8.cpp b/clang/test/SemaCXX/constexpr-builtin-stdc-store8.cpp
new file mode 100644
index 00000000000000..76465f4e9937bd
--- /dev/null
+++ b/clang/test/SemaCXX/constexpr-builtin-stdc-store8.cpp
@@ -0,0 +1,282 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify=ref,both -std=c++20 %s
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify=expected,both -std=c++20 %s -fexperimental-new-constant-interpreter
+
+// Constant evaluation of the C2y stdc_store8_* functions through their
+// __builtin_ spellings. Mutation in a constant expression needs a constexpr
+// function, so this is exercised as C++.
+
+// Store VAL with FN into a zeroed, 8-byte aligned buffer at offset 0 and check
+// that exactly the listed bytes were written (in order) and nothing else.
+#define STORE_TEST(NAME, FN, VAL, ...) \
+ constexpr bool NAME() { \
+ alignas(8) unsigned char buf[8] = {}; \
+ FN(VAL, buf); \
+ const unsigned char expected[] = {__VA_ARGS__}; \
+ unsigned i = 0; \
+ for (; i < sizeof(expected); ++i) \
+ if (buf[i] != expected[i]) \
+ return false; \
+ for (; i < sizeof(buf); ++i) \
+ if (buf[i] != 0) \
+ return false; \
+ return true; \
+ } \
+ static_assert(NAME(), #NAME);
+
+
+// Unaligned variants, all widths, both byte orders, both signednesses.
+STORE_TEST(t_leu8, __builtin_stdc_store8_leu8, 0xA5, 0xa5)
+STORE_TEST(t_beu8, __builtin_stdc_store8_beu8, 0xA5, 0xa5)
+STORE_TEST(t_leu16, __builtin_stdc_store8_leu16, 0xABCD, 0xcd, 0xab)
+STORE_TEST(t_beu16, __builtin_stdc_store8_beu16, 0xABCD, 0xab, 0xcd)
+STORE_TEST(t_leu32, __builtin_stdc_store8_leu32, 0x12345678u, 0x78, 0x56, 0x34, 0x12)
+STORE_TEST(t_beu32, __builtin_stdc_store8_beu32, 0x12345678u, 0x12, 0x34, 0x56, 0x78)
+STORE_TEST(t_leu64, __builtin_stdc_store8_leu64, 0x1122334455667788ull, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11)
+STORE_TEST(t_beu64, __builtin_stdc_store8_beu64, 0x1122334455667788ull, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88)
+STORE_TEST(t_les8, __builtin_stdc_store8_les8, -128, 0x80)
+STORE_TEST(t_bes8, __builtin_stdc_store8_bes8, -128, 0x80)
+STORE_TEST(t_les16, __builtin_stdc_store8_les16, -2, 0xfe, 0xff)
+STORE_TEST(t_bes16, __builtin_stdc_store8_bes16, -2, 0xff, 0xfe)
+STORE_TEST(t_les32, __builtin_stdc_store8_les32, -2, 0xfe, 0xff, 0xff, 0xff)
+STORE_TEST(t_bes32, __builtin_stdc_store8_bes32, -2, 0xff, 0xff, 0xff, 0xfe)
+STORE_TEST(t_les64, __builtin_stdc_store8_les64, (-9223372036854775807ll - 1), 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80)
+STORE_TEST(t_bes64, __builtin_stdc_store8_bes64, (-9223372036854775807ll - 1), 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
+
+// Aligned variants, all widths, both byte orders, both signednesses.
+STORE_TEST(t_aligned_leu8, __builtin_stdc_store8_aligned_leu8, 0xA5, 0xa5)
+STORE_TEST(t_aligned_beu8, __builtin_stdc_store8_aligned_beu8, 0xA5, 0xa5)
+STORE_TEST(t_aligned_leu16, __builtin_stdc_store8_aligned_leu16, 0xABCD, 0xcd, 0xab)
+STORE_TEST(t_aligned_beu16, __builtin_stdc_store8_aligned_beu16, 0xABCD, 0xab, 0xcd)
+STORE_TEST(t_aligned_leu32, __builtin_stdc_store8_aligned_leu32, 0x12345678u, 0x78, 0x56, 0x34, 0x12)
+STORE_TEST(t_aligned_beu32, __builtin_stdc_store8_aligned_beu32, 0x12345678u, 0x12, 0x34, 0x56, 0x78)
+STORE_TEST(t_aligned_leu64, __builtin_stdc_store8_aligned_leu64, 0x1122334455667788ull, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11)
+STORE_TEST(t_aligned_beu64, __builtin_stdc_store8_aligned_beu64, 0x1122334455667788ull, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88)
+STORE_TEST(t_aligned_les8, __builtin_stdc_store8_aligned_les8, -128, 0x80)
+STORE_TEST(t_aligned_bes8, __builtin_stdc_store8_aligned_bes8, -128, 0x80)
+STORE_TEST(t_aligned_les16, __builtin_stdc_store8_aligned_les16, -2, 0xfe, 0xff)
+STORE_TEST(t_aligned_bes16, __builtin_stdc_store8_aligned_bes16, -2, 0xff, 0xfe)
+STORE_TEST(t_aligned_les32, __builtin_stdc_store8_aligned_les32, -2, 0xfe, 0xff, 0xff, 0xff)
+STORE_TEST(t_aligned_bes32, __builtin_stdc_store8_aligned_bes32, -2, 0xff, 0xff, 0xff, 0xfe)
+STORE_TEST(t_aligned_les64, __builtin_stdc_store8_aligned_les64, (-9223372036854775807ll - 1), 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80)
+STORE_TEST(t_aligned_bes64, __builtin_stdc_store8_aligned_bes64, (-9223372036854775807ll - 1), 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
+
+// Writes land at the given offset and leave neighbouring bytes alone.
+constexpr bool test_offset() {
+ unsigned char buf[6] = {1, 1, 1, 1, 1, 1};
+ __builtin_stdc_store8_beu16(0xABCD, buf + 2);
+ return buf[0] == 1 && buf[1] == 1 && buf[2] == 0xAB && buf[3] == 0xCD &&
+ buf[4] == 1 && buf[5] == 1;
+}
+static_assert(test_offset(), "");
+
+// Later stores overwrite earlier ones.
+constexpr bool test_overwrite() {
+ unsigned char buf[4] = {};
+ __builtin_stdc_store8_leu32(0xFFFFFFFFu, buf);
+ __builtin_stdc_store8_beu16(0x0102, buf + 1);
+ return buf[0] == 0xFF && buf[1] == 0x01 && buf[2] == 0x02 && buf[3] == 0xFF;
+}
+static_assert(test_overwrite(), "");
+
+// Storing initializes an otherwise uninitialized buffer.
+constexpr bool test_initializes() {
+ unsigned char buf[4];
+ __builtin_stdc_store8_leu32(0x04030201u, buf);
+ return buf[0] == 1 && buf[1] == 2 && buf[2] == 3 && buf[3] == 4;
+}
+static_assert(test_initializes(), "");
+
+// An aligned store at an offset that is only as aligned as the width needs.
+constexpr bool test_aligned_offset() {
+ alignas(8) unsigned char buf[8] = {};
+ __builtin_stdc_store8_aligned_leu16(0x0201, buf + 2);
+ __builtin_stdc_store8_aligned_beu32(0x03040506u, buf + 4);
+ return buf[2] == 1 && buf[3] == 2 && buf[4] == 3 && buf[7] == 6;
+}
+static_assert(test_aligned_offset(), "");
+
+// Storing through a pointer to a larger object's byte range.
+struct S { alignas(4) unsigned char bytes[4]; unsigned char tail; };
+constexpr bool test_member() {
+ S s = {};
+ __builtin_stdc_store8_aligned_leu32(0xAABBCCDDu, s.bytes);
+ return s.bytes[0] == 0xDD && s.bytes[3] == 0xAA && s.tail == 0;
+}
+static_assert(test_member(), "");
+
+// Errors.
+constexpr bool test_oob() { // both-error{{constexpr function never produces a constant expression}}
+ unsigned char buf[2];
+ __builtin_stdc_store8_leu32(1, buf); // both-note 2{{cannot refer to element 3 of array of 2 elements in a constant expression}}
+ return true;
+}
+static_assert(test_oob(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_oob()'}}
+
+constexpr bool test_oob_offset() { // both-error{{constexpr function never produces a constant expression}}
+ unsigned char buf[4];
+ __builtin_stdc_store8_leu32(1, buf + 1); // both-note 2{{cannot refer to element 4 of array of 4 elements in a constant expression}}
+ return true;
+}
+static_assert(test_oob_offset(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_oob_offset()'}}
+
+constexpr bool test_one_past_end() { // both-error{{constexpr function never produces a constant expression}}
+ unsigned char buf[1];
+ __builtin_stdc_store8_leu8(1, buf + 1); // both-note 2{{cannot refer to element 1 of array of 1 element in a constant expression}}
+ return true;
+}
+static_assert(test_one_past_end(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_one_past_end()'}}
+
+constexpr bool test_null() { // both-error{{constexpr function never produces a constant expression}}
+ __builtin_stdc_store8_leu32(1, (unsigned char *)0); // both-warning{{null passed to a callee that requires a non-null argument}} both-note 2{{assignment to dereferenced null pointer is not allowed in a constant expression}}
+ return true;
+}
+static_assert(test_null(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_null()'}}
+
+constexpr unsigned char read_only[4] = {};
+constexpr bool test_const_object() { // both-error{{constexpr function never produces a constant expression}}
+ __builtin_stdc_store8_leu32(1, const_cast<unsigned char *>(read_only)); // both-note 2{{a constant expression cannot modify an object that is visible outside that expression}}
+ return true;
+}
+static_assert(test_const_object(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_const_object()'}}
+
+// aligned_* requires the pointer to be aligned to the width of the value.
+constexpr bool test_misaligned16() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(8) unsigned char buf[9] = {};
+ __builtin_stdc_store8_aligned_leu16(1, buf + 1); // both-note 2{{'__builtin_stdc_store8_aligned_leu16' requires a pointer aligned to 2 bytes, but the given pointer is only aligned to 1 byte}}
+ return true;
+}
+static_assert(test_misaligned16(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_misaligned16()'}}
+
+constexpr bool test_misaligned32() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(8) unsigned char buf[8] = {};
+ __builtin_stdc_store8_aligned_beu32(1, buf + 2); // both-note 2{{'__builtin_stdc_store8_aligned_beu32' requires a pointer aligned to 4 bytes, but the given pointer is only aligned to 2 bytes}}
+ return true;
+}
+static_assert(test_misaligned32(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_misaligned32()'}}
+
+constexpr bool test_unaligned_buffer() { // both-error{{constexpr function never produces a constant expression}}
+ unsigned char buf[8] = {};
+ __builtin_stdc_store8_aligned_les64(1, buf); // both-note 2{{'__builtin_stdc_store8_aligned_les64' requires a pointer aligned to 8 bytes, but the given pointer is only aligned to 1 byte}}
+ return true;
+}
+static_assert(test_unaligned_buffer(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_unaligned_buffer()'}}
+
+// The functions return void.
+static_assert(__is_same(decltype(__builtin_stdc_store8_leu8(0, (unsigned char *)0)), void), "");
+static_assert(__is_same(decltype(__builtin_stdc_store8_aligned_bes64(0, (unsigned char *)0)), void), "");
+
+// An unsigned variant converts a negative argument to the unsigned type.
+constexpr bool test_negative_to_unsigned() {
+ unsigned char buf[2] = {};
+ __builtin_stdc_store8_leu16(-2, buf);
+ return buf[0] == 0xFE && buf[1] == 0xFF;
+}
+static_assert(test_negative_to_unsigned(), "");
+
+// The 8-bit aligned variants never need alignment.
+constexpr bool test_aligned8_unaligned_buffer() {
+ unsigned char buf[3] = {};
+ __builtin_stdc_store8_aligned_leu8(0x11, buf + 1);
+ __builtin_stdc_store8_aligned_bes8(-1, buf + 2);
+ return buf[0] == 0 && buf[1] == 0x11 && buf[2] == 0xFF;
+}
+static_assert(test_aligned8_unaligned_buffer(), "");
+
+// Scalar (non-array) destinations: an 8-bit store is fine, wider ones run past
+// the object.
+constexpr bool test_scalar_ok() {
+ unsigned char c = 0;
+ __builtin_stdc_store8_leu8(0x42, &c);
+ __builtin_stdc_store8_aligned_beu8(0x43, &c);
+ return c == 0x43;
+}
+static_assert(test_scalar_ok(), "");
+
+constexpr bool test_scalar_oob() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(4) unsigned char c = 0;
+ __builtin_stdc_store8_aligned_leu32(1, &c); // both-note 2{{cannot refer to element 3 of non-array object in a constant expression}}
+ return true;
+}
+static_assert(test_scalar_oob(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_scalar_oob()'}}
+
+// An aligned store one past the end is out of bounds, whether or not the
+// pointer is aligned.
+constexpr bool test_aligned_one_past_end() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(8) unsigned char buf[4] = {};
+ __builtin_stdc_store8_aligned_leu32(1, buf + 4); // both-note 2{{cannot refer to element 7 of array of 4 elements in a constant expression}}
+ return true;
+}
+static_assert(test_aligned_one_past_end(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_aligned_one_past_end()'}}
+
+constexpr bool test_aligned_one_past_end_misaligned() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(8) unsigned char buf[5] = {};
+ __builtin_stdc_store8_aligned_leu32(1, buf + 5); // both-note 2{{'__builtin_stdc_store8_aligned_leu32' requires a pointer aligned to 4 bytes, but the given pointer is only aligned to 1 byte}}
+ return true;
+}
+static_assert(test_aligned_one_past_end_misaligned(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_aligned_one_past_end_misaligned()'}}
+
+// The signed and big-endian aligned variants check alignment too.
+constexpr bool test_misaligned_les32() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(8) unsigned char buf[9] = {};
+ __builtin_stdc_store8_aligned_les32(1, buf + 1); // both-note 2{{'__builtin_stdc_store8_aligned_les32' requires a pointer aligned to 4 bytes, but the given pointer is only aligned to 1 byte}}
+ return true;
+}
+static_assert(test_misaligned_les32(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_misaligned_les32()'}}
+
+constexpr bool test_misaligned_bes32() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(8) unsigned char buf[9] = {};
+ __builtin_stdc_store8_aligned_bes32(1, buf + 1); // both-note 2{{'__builtin_stdc_store8_aligned_bes32' requires a pointer aligned to 4 bytes, but the given pointer is only aligned to 1 byte}}
+ return true;
+}
+static_assert(test_misaligned_bes32(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_misaligned_bes32()'}}
+
+// Alignment of a pointer to a member depends on the member's offset.
+struct FieldOffset { unsigned char pad; unsigned char data[8]; };
+constexpr bool test_member_misaligned() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(8) FieldOffset s = {};
+ __builtin_stdc_store8_aligned_leu32(1, s.data); // both-note 2{{'__builtin_stdc_store8_aligned_leu32' requires a pointer aligned to 4 bytes, but the given pointer is only aligned to 1 byte}}
+ return true;
+}
+static_assert(test_member_misaligned(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_member_misaligned()'}}
+
+constexpr bool test_array_member_misaligned() { // both-error{{constexpr function never produces a constant expression}}
+ alignas(8) FieldOffset s[2] = {};
+ __builtin_stdc_store8_aligned_leu32(1, s[1].data); // both-note 2{{'__builtin_stdc_store8_aligned_leu32' requires a pointer aligned to 4 bytes, but the given pointer is only aligned to 2 bytes}}
+ return true;
+}
+static_assert(test_array_member_misaligned(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_array_member_misaligned()'}}
+
+union FieldUnion { unsigned char c; unsigned char data[8]; };
+constexpr bool test_union_member() {
+ alignas(8) FieldUnion u = {.data = {}};
+ __builtin_stdc_store8_aligned_leu32(0x12345678u, u.data);
+ return u.data[0] == 0x78 && u.data[3] == 0x12;
+}
+static_assert(test_union_member(), "");
+
+// Alignment from a typedef'd attribute applies to the offset arithmetic.
+typedef unsigned char AlignedBuf8[8] __attribute__((aligned(8)));
+constexpr bool test_typedef_aligned() {
+ AlignedBuf8 buf = {};
+ __builtin_stdc_store8_aligned_leu32(0x01020304u, buf + 4);
+ return buf[4] == 4 && buf[7] == 1;
+}
+static_assert(test_typedef_aligned(), "");
+
+constexpr bool test_typedef_offset_fail() { // both-error{{constexpr function never produces a constant expression}}
+ AlignedBuf8 buf = {};
+ __builtin_stdc_store8_aligned_leu32(1, buf + 2); // both-note 2{{'__builtin_stdc_store8_aligned_leu32' requires a pointer aligned to 4 bytes, but the given pointer is only aligned to 2 bytes}}
+ return true;
+}
+static_assert(test_typedef_offset_fail(), ""); // both-error{{not an integral constant expression}} both-note{{in call to 'test_typedef_offset_fail()'}}
+
+// Dynamically allocated storage is at least as aligned as the allocation
+// function guarantees.
+constexpr bool test_dynamic_alloc() {
+ unsigned char *p = new unsigned char[8];
+ __builtin_stdc_store8_aligned_beu64(0x0102030405060708ull, p);
+ bool ok = p[0] == 1 && p[7] == 8;
+ delete[] p;
+ return ok;
+}
+static_assert(test_dynamic_alloc(), "");
More information about the cfe-commits
mailing list