Skip to content

Commit 4b6cc2b

Browse files
committed
[Clang][CodeGen] Address review comments.
1 parent b987682 commit 4b6cc2b

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

clang/lib/CodeGen/CGBuilder.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -223,16 +223,11 @@ class CGBuilderTy : public CGBuilderBaseTy {
223223
const llvm::StructLayout *Layout = DL.getStructLayout(ElTy);
224224
auto Offset = CharUnits::fromQuantity(Layout->getElementOffset(Index));
225225

226-
// Specially, we don't add inbounds flags if the base pointer is null.
227-
// This is a workaround for old-style offsetof macros.
228-
llvm::GEPNoWrapFlags NWFlags = llvm::GEPNoWrapFlags::noUnsignedWrap();
229-
if (!isa<llvm::ConstantPointerNull>(Addr.getBasePointer()))
230-
NWFlags |= llvm::GEPNoWrapFlags::inBounds();
231-
return Address(
232-
CreateConstGEP2_32(Addr.getElementType(), Addr.getBasePointer(), 0,
233-
Index, Name, NWFlags),
234-
ElTy->getElementType(Index),
235-
Addr.getAlignment().alignmentAtOffset(Offset), Addr.isKnownNonNull());
226+
return Address(CreateStructGEP(Addr.getElementType(), Addr.getBasePointer(),
227+
Index, Name),
228+
ElTy->getElementType(Index),
229+
Addr.getAlignment().alignmentAtOffset(Offset),
230+
Addr.isKnownNonNull());
236231
}
237232

238233
/// Given

clang/lib/CodeGen/CGExpr.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4778,6 +4778,10 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
47784778
}
47794779

47804780
Expr *BaseExpr = E->getBase();
4781+
Expr *UnderlyingBaseExpr = BaseExpr;
4782+
while (auto *BaseMemberExpr = dyn_cast<MemberExpr>(UnderlyingBaseExpr))
4783+
UnderlyingBaseExpr = BaseMemberExpr->getBase();
4784+
bool IsBaseConstantNull = getContext().isSentinelNullExpr(UnderlyingBaseExpr);
47814785
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
47824786
LValue BaseLV;
47834787
if (E->isArrow()) {
@@ -4799,7 +4803,7 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
47994803

48004804
NamedDecl *ND = E->getMemberDecl();
48014805
if (auto *Field = dyn_cast<FieldDecl>(ND)) {
4802-
LValue LV = EmitLValueForField(BaseLV, Field);
4806+
LValue LV = EmitLValueForField(BaseLV, Field, IsBaseConstantNull);
48034807
setObjCGCLValueClass(getContext(), E, LV);
48044808
if (getLangOpts().OpenMP) {
48054809
// If the member was explicitly marked as nontemporal, mark it as
@@ -4885,12 +4889,15 @@ unsigned CodeGenFunction::getDebugInfoFIndex(const RecordDecl *Rec,
48854889
/// Get the address of a zero-sized field within a record. The resulting
48864890
/// address doesn't necessarily have the right type.
48874891
static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
4888-
const FieldDecl *Field) {
4892+
const FieldDecl *Field,
4893+
bool IsBaseConstantNull) {
48894894
CharUnits Offset = CGF.getContext().toCharUnitsFromBits(
48904895
CGF.getContext().getFieldOffset(Field));
48914896
if (Offset.isZero())
48924897
return Base;
48934898
Base = Base.withElementType(CGF.Int8Ty);
4899+
if (IsBaseConstantNull)
4900+
return CGF.Builder.CreateConstByteGEP(Base, Offset);
48944901
return CGF.Builder.CreateConstInBoundsByteGEP(Base, Offset);
48954902
}
48964903

@@ -4899,15 +4906,18 @@ static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
48994906
///
49004907
/// The resulting address doesn't necessarily have the right type.
49014908
static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
4902-
const FieldDecl *field) {
4909+
const FieldDecl *field,
4910+
bool IsBaseConstantNull) {
49034911
if (isEmptyFieldForLayout(CGF.getContext(), field))
4904-
return emitAddrOfZeroSizeField(CGF, base, field);
4912+
return emitAddrOfZeroSizeField(CGF, base, field, IsBaseConstantNull);
49054913

49064914
const RecordDecl *rec = field->getParent();
49074915

49084916
unsigned idx =
49094917
CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
49104918

4919+
if (IsBaseConstantNull)
4920+
return CGF.Builder.CreateConstGEP(base, idx, field->getName());
49114921
return CGF.Builder.CreateStructGEP(base, idx, field->getName());
49124922
}
49134923

@@ -4943,8 +4953,8 @@ static bool hasAnyVptr(const QualType Type, const ASTContext &Context) {
49434953
return false;
49444954
}
49454955

4946-
LValue CodeGenFunction::EmitLValueForField(LValue base,
4947-
const FieldDecl *field) {
4956+
LValue CodeGenFunction::EmitLValueForField(LValue base, const FieldDecl *field,
4957+
bool IsBaseConstantNull) {
49484958
LValueBaseInfo BaseInfo = base.getBaseInfo();
49494959

49504960
if (field->isBitField()) {
@@ -5076,7 +5086,7 @@ LValue CodeGenFunction::EmitLValueForField(LValue base,
50765086
if (!IsInPreservedAIRegion &&
50775087
(!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>()))
50785088
// For structs, we GEP to the field that the record layout suggests.
5079-
addr = emitAddrOfFieldStorage(*this, addr, field);
5089+
addr = emitAddrOfFieldStorage(*this, addr, field, IsBaseConstantNull);
50805090
else
50815091
// Remember the original struct field index
50825092
addr = emitPreserveStructAccess(*this, base, addr, field);
@@ -5120,7 +5130,8 @@ CodeGenFunction::EmitLValueForFieldInitialization(LValue Base,
51205130
if (!FieldType->isReferenceType())
51215131
return EmitLValueForField(Base, Field);
51225132

5123-
Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field);
5133+
Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field,
5134+
/*IsBaseConstantNull=*/false);
51245135

51255136
// Make sure that the address is pointing to the right type.
51265137
llvm::Type *llvmType = ConvertTypeForMem(FieldType);

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4472,7 +4472,8 @@ class CodeGenFunction : public CodeGenTypeCache {
44724472
const ObjCIvarDecl *Ivar);
44734473
llvm::Value *EmitIvarOffsetAsPointerDiff(const ObjCInterfaceDecl *Interface,
44744474
const ObjCIvarDecl *Ivar);
4475-
LValue EmitLValueForField(LValue Base, const FieldDecl *Field);
4475+
LValue EmitLValueForField(LValue Base, const FieldDecl *Field,
4476+
bool IsBaseConstantNull = false);
44764477
LValue EmitLValueForLambdaField(const FieldDecl *Field);
44774478
LValue EmitLValueForLambdaField(const FieldDecl *Field,
44784479
llvm::Value *ThisValue);

0 commit comments

Comments
 (0)