Skip to content

Commit fe733d0

Browse files
committed
[Clang][CodeGen] Address review comments.
1 parent 173ba15 commit fe733d0

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
@@ -4792,6 +4792,10 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
47924792
}
47934793

47944794
Expr *BaseExpr = E->getBase();
4795+
Expr *UnderlyingBaseExpr = BaseExpr;
4796+
while (auto *BaseMemberExpr = dyn_cast<MemberExpr>(UnderlyingBaseExpr))
4797+
UnderlyingBaseExpr = BaseMemberExpr->getBase();
4798+
bool IsBaseConstantNull = getContext().isSentinelNullExpr(UnderlyingBaseExpr);
47954799
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
47964800
LValue BaseLV;
47974801
if (E->isArrow()) {
@@ -4813,7 +4817,7 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
48134817

48144818
NamedDecl *ND = E->getMemberDecl();
48154819
if (auto *Field = dyn_cast<FieldDecl>(ND)) {
4816-
LValue LV = EmitLValueForField(BaseLV, Field);
4820+
LValue LV = EmitLValueForField(BaseLV, Field, IsBaseConstantNull);
48174821
setObjCGCLValueClass(getContext(), E, LV);
48184822
if (getLangOpts().OpenMP) {
48194823
// If the member was explicitly marked as nontemporal, mark it as
@@ -4899,12 +4903,15 @@ unsigned CodeGenFunction::getDebugInfoFIndex(const RecordDecl *Rec,
48994903
/// Get the address of a zero-sized field within a record. The resulting
49004904
/// address doesn't necessarily have the right type.
49014905
static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
4902-
const FieldDecl *Field) {
4906+
const FieldDecl *Field,
4907+
bool IsBaseConstantNull) {
49034908
CharUnits Offset = CGF.getContext().toCharUnitsFromBits(
49044909
CGF.getContext().getFieldOffset(Field));
49054910
if (Offset.isZero())
49064911
return Base;
49074912
Base = Base.withElementType(CGF.Int8Ty);
4913+
if (IsBaseConstantNull)
4914+
return CGF.Builder.CreateConstByteGEP(Base, Offset);
49084915
return CGF.Builder.CreateConstInBoundsByteGEP(Base, Offset);
49094916
}
49104917

@@ -4913,15 +4920,18 @@ static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
49134920
///
49144921
/// The resulting address doesn't necessarily have the right type.
49154922
static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
4916-
const FieldDecl *field) {
4923+
const FieldDecl *field,
4924+
bool IsBaseConstantNull) {
49174925
if (isEmptyFieldForLayout(CGF.getContext(), field))
4918-
return emitAddrOfZeroSizeField(CGF, base, field);
4926+
return emitAddrOfZeroSizeField(CGF, base, field, IsBaseConstantNull);
49194927

49204928
const RecordDecl *rec = field->getParent();
49214929

49224930
unsigned idx =
49234931
CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
49244932

4933+
if (IsBaseConstantNull)
4934+
return CGF.Builder.CreateConstGEP(base, idx, field->getName());
49254935
return CGF.Builder.CreateStructGEP(base, idx, field->getName());
49264936
}
49274937

@@ -4957,8 +4967,8 @@ static bool hasAnyVptr(const QualType Type, const ASTContext &Context) {
49574967
return false;
49584968
}
49594969

4960-
LValue CodeGenFunction::EmitLValueForField(LValue base,
4961-
const FieldDecl *field) {
4970+
LValue CodeGenFunction::EmitLValueForField(LValue base, const FieldDecl *field,
4971+
bool IsBaseConstantNull) {
49624972
LValueBaseInfo BaseInfo = base.getBaseInfo();
49634973

49644974
if (field->isBitField()) {
@@ -5090,7 +5100,7 @@ LValue CodeGenFunction::EmitLValueForField(LValue base,
50905100
if (!IsInPreservedAIRegion &&
50915101
(!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>()))
50925102
// For structs, we GEP to the field that the record layout suggests.
5093-
addr = emitAddrOfFieldStorage(*this, addr, field);
5103+
addr = emitAddrOfFieldStorage(*this, addr, field, IsBaseConstantNull);
50945104
else
50955105
// Remember the original struct field index
50965106
addr = emitPreserveStructAccess(*this, base, addr, field);
@@ -5134,7 +5144,8 @@ CodeGenFunction::EmitLValueForFieldInitialization(LValue Base,
51345144
if (!FieldType->isReferenceType())
51355145
return EmitLValueForField(Base, Field);
51365146

5137-
Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field);
5147+
Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field,
5148+
/*IsBaseConstantNull=*/false);
51385149

51395150
// Make sure that the address is pointing to the right type.
51405151
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)