Skip to content

Commit c287e15

Browse files
committed
[Clang][CodeGen] Add helper isUnderlyingBasePointerConstantNull
1 parent bbe2e97 commit c287e15

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4778,23 +4778,25 @@ EmitExtVectorElementExpr(const ExtVectorElementExpr *E) {
47784778
Base.getBaseInfo(), TBAAAccessInfo());
47794779
}
47804780

4781+
bool CodeGenFunction::isUnderlyingBasePointerConstantNull(const Expr *E) {
4782+
const Expr *UnderlyingBaseExpr = E->IgnoreParens();
4783+
while (auto *BaseMemberExpr = dyn_cast<MemberExpr>(UnderlyingBaseExpr))
4784+
UnderlyingBaseExpr = BaseMemberExpr->getBase()->IgnoreParens();
4785+
return getContext().isSentinelNullExpr(UnderlyingBaseExpr);
4786+
}
4787+
47814788
LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
47824789
if (DeclRefExpr *DRE = tryToConvertMemberExprToDeclRefExpr(*this, E)) {
47834790
EmitIgnoredExpr(E->getBase());
47844791
return EmitDeclRefLValue(DRE);
47854792
}
47864793

47874794
Expr *BaseExpr = E->getBase();
4788-
bool IsInBounds = !getLangOpts().PointerOverflowDefined;
4789-
if (IsInBounds) {
4790-
// Check whether the underlying base pointer is a constant null.
4791-
// If so, we do not set inbounds flag for GEP to avoid breaking some
4792-
// old-style offsetof idioms.
4793-
Expr *UnderlyingBaseExpr = BaseExpr->IgnoreParens();
4794-
while (auto *BaseMemberExpr = dyn_cast<MemberExpr>(UnderlyingBaseExpr))
4795-
UnderlyingBaseExpr = BaseMemberExpr->getBase()->IgnoreParens();
4796-
IsInBounds = !getContext().isSentinelNullExpr(UnderlyingBaseExpr);
4797-
}
4795+
// Check whether the underlying base pointer is a constant null.
4796+
// If so, we do not set inbounds flag for GEP to avoid breaking some
4797+
// old-style offsetof idioms.
4798+
bool IsInBounds = !getLangOpts().PointerOverflowDefined &&
4799+
!isUnderlyingBasePointerConstantNull(BaseExpr);
47984800
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
47994801
LValue BaseLV;
48004802
if (E->isArrow()) {

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4557,6 +4557,8 @@ class CodeGenFunction : public CodeGenTypeCache {
45574557
const CXXRecordDecl *RD);
45584558

45594559
bool isPointerKnownNonNull(const Expr *E);
4560+
/// Check whether the underlying base pointer is a constant null.
4561+
bool isUnderlyingBasePointerConstantNull(const Expr *E);
45604562

45614563
/// Create the discriminator from the storage address and the entity hash.
45624564
llvm::Value *EmitPointerAuthBlendDiscriminator(llvm::Value *StorageAddress,

0 commit comments

Comments
 (0)