[clang] [Clang][CodeGen] Preserve alignment information for pointer arithmetics (PR #152575)
Eli Friedman via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 7 13:20:31 PDT 2025
================
@@ -4309,16 +4292,39 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,
// future proof.
llvm::Type *elemTy;
if (elementType->isVoidType() || elementType->isFunctionType())
- elemTy = CGF.Int8Ty;
+ elemTy = Int8Ty;
else
- elemTy = CGF.ConvertTypeForMem(elementType);
+ elemTy = ConvertTypeForMem(elementType);
- if (CGF.getLangOpts().PointerOverflowDefined)
- return CGF.Builder.CreateGEP(elemTy, pointer, index, "add.ptr");
+ if (getLangOpts().PointerOverflowDefined)
+ return Builder.CreateGEP(elemTy, pointer, index, "add.ptr");
+
+ return EmitCheckedInBoundsGEP(elemTy, pointer, index, isSigned, isSubtraction,
+ BO->getExprLoc(), "add.ptr");
+}
+
+/// BO_Add/BO_Sub are handled by EmitPointerWithAlignment to preserve alignment
+/// information.
+/// This is a fallback path for BO_AddAssign/BO_SubAssign.
----------------
efriedma-quic wrote:
"fallback path" doesn't seem accurate, if it's the primary path for compound assignment; probably just say "This function is used for BO_AddAssign/BO_SubAssign."
We could do something with the result of BO_AddAssign/BO_SubAssign in C; it's just a plain pointer, like a normal add/sub, just with a side-effect of modifying the pointer. Not sure how much that matters in practice. (The result of BO_AddAssign is an lvalue in C++, though.)
https://github.com/llvm/llvm-project/pull/152575
More information about the cfe-commits
mailing list