[clang] [clang] Bind the __builtin_bit_cast operand as std::bit_cast binds its parameter (PR #231830)

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Sun Oct 18 13:12:56 PDT 2026


================
@@ -2658,6 +2658,8 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
     Address Addr =
         SourceLVal.getAddress().withElementType(CGF.ConvertTypeForMem(DestTy));
     LValue DestLV = CGF.MakeAddrLValue(Addr, DestTy);
+    if (SourceLVal.isVolatileQualified())
+      DestLV.getQuals().addVolatile();
----------------
efriedma-quic wrote:

Interesting.

The standard specification for std::bit_cast doesn't actually say what happens if the operand is volatile.

Consider the following example:

```
volatile double z1;
volatile long long z2;
void f() {
    __builtin_bit_cast(long long, z1);
    __builtin_bit_cast(long long, z2);
}
```

MSVC optimizes the casts away.  gcc preserves the casts.  It also preserves the types of the loads: it generates movsd when the operand is a double, and an integer mov when the operand is a "long long".

https://github.com/llvm/llvm-project/pull/231830


More information about the cfe-commits mailing list