Skip to content

Commit 222a681

Browse files
committed
[Clang][CodeGen] Address review comments.
1 parent 02065e8 commit 222a681

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

47404740
Expr *BaseExpr = E->getBase();
4741+
Expr *UnderlyingBaseExpr = BaseExpr;
4742+
while (auto *BaseMemberExpr = dyn_cast<MemberExpr>(UnderlyingBaseExpr))
4743+
UnderlyingBaseExpr = BaseMemberExpr->getBase();
4744+
bool IsBaseConstantNull = getContext().isSentinelNullExpr(UnderlyingBaseExpr);
47414745
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
47424746
LValue BaseLV;
47434747
if (E->isArrow()) {
@@ -4759,7 +4763,7 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
47594763

47604764
NamedDecl *ND = E->getMemberDecl();
47614765
if (auto *Field = dyn_cast<FieldDecl>(ND)) {
4762-
LValue LV = EmitLValueForField(BaseLV, Field);
4766+
LValue LV = EmitLValueForField(BaseLV, Field, IsBaseConstantNull);
47634767
setObjCGCLValueClass(getContext(), E, LV);
47644768
if (getLangOpts().OpenMP) {
47654769
// If the member was explicitly marked as nontemporal, mark it as
@@ -4845,12 +4849,15 @@ unsigned CodeGenFunction::getDebugInfoFIndex(const RecordDecl *Rec,
48454849
/// Get the address of a zero-sized field within a record. The resulting
48464850
/// address doesn't necessarily have the right type.
48474851
static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
4848-
const FieldDecl *Field) {
4852+
const FieldDecl *Field,
4853+
bool IsBaseConstantNull) {
48494854
CharUnits Offset = CGF.getContext().toCharUnitsFromBits(
48504855
CGF.getContext().getFieldOffset(Field));
48514856
if (Offset.isZero())
48524857
return Base;
48534858
Base = Base.withElementType(CGF.Int8Ty);
4859+
if (IsBaseConstantNull)
4860+
return CGF.Builder.CreateConstByteGEP(Base, Offset);
48544861
return CGF.Builder.CreateConstInBoundsByteGEP(Base, Offset);
48554862
}
48564863

@@ -4859,15 +4866,18 @@ static Address emitAddrOfZeroSizeField(CodeGenFunction &CGF, Address Base,
48594866
///
48604867
/// The resulting address doesn't necessarily have the right type.
48614868
static Address emitAddrOfFieldStorage(CodeGenFunction &CGF, Address base,
4862-
const FieldDecl *field) {
4869+
const FieldDecl *field,
4870+
bool IsBaseConstantNull) {
48634871
if (isEmptyFieldForLayout(CGF.getContext(), field))
4864-
return emitAddrOfZeroSizeField(CGF, base, field);
4872+
return emitAddrOfZeroSizeField(CGF, base, field, IsBaseConstantNull);
48654873

48664874
const RecordDecl *rec = field->getParent();
48674875

48684876
unsigned idx =
48694877
CGF.CGM.getTypes().getCGRecordLayout(rec).getLLVMFieldNo(field);
48704878

4879+
if (IsBaseConstantNull)
4880+
return CGF.Builder.CreateConstGEP(base, idx, field->getName());
48714881
return CGF.Builder.CreateStructGEP(base, idx, field->getName());
48724882
}
48734883

@@ -4903,8 +4913,8 @@ static bool hasAnyVptr(const QualType Type, const ASTContext &Context) {
49034913
return false;
49044914
}
49054915

4906-
LValue CodeGenFunction::EmitLValueForField(LValue base,
4907-
const FieldDecl *field) {
4916+
LValue CodeGenFunction::EmitLValueForField(LValue base, const FieldDecl *field,
4917+
bool IsBaseConstantNull) {
49084918
LValueBaseInfo BaseInfo = base.getBaseInfo();
49094919

49104920
if (field->isBitField()) {
@@ -5036,7 +5046,7 @@ LValue CodeGenFunction::EmitLValueForField(LValue base,
50365046
if (!IsInPreservedAIRegion &&
50375047
(!getDebugInfo() || !rec->hasAttr<BPFPreserveAccessIndexAttr>()))
50385048
// For structs, we GEP to the field that the record layout suggests.
5039-
addr = emitAddrOfFieldStorage(*this, addr, field);
5049+
addr = emitAddrOfFieldStorage(*this, addr, field, IsBaseConstantNull);
50405050
else
50415051
// Remember the original struct field index
50425052
addr = emitPreserveStructAccess(*this, base, addr, field);
@@ -5080,7 +5090,8 @@ CodeGenFunction::EmitLValueForFieldInitialization(LValue Base,
50805090
if (!FieldType->isReferenceType())
50815091
return EmitLValueForField(Base, Field);
50825092

5083-
Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field);
5093+
Address V = emitAddrOfFieldStorage(*this, Base.getAddress(), Field,
5094+
/*IsBaseConstantNull=*/false);
50845095

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

clang/lib/CodeGen/CodeGenFunction.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4470,7 +4470,8 @@ class CodeGenFunction : public CodeGenTypeCache {
44704470
const ObjCIvarDecl *Ivar);
44714471
llvm::Value *EmitIvarOffsetAsPointerDiff(const ObjCInterfaceDecl *Interface,
44724472
const ObjCIvarDecl *Ivar);
4473-
LValue EmitLValueForField(LValue Base, const FieldDecl *Field);
4473+
LValue EmitLValueForField(LValue Base, const FieldDecl *Field,
4474+
bool IsBaseConstantNull = false);
44744475
LValue EmitLValueForLambdaField(const FieldDecl *Field);
44754476
LValue EmitLValueForLambdaField(const FieldDecl *Field,
44764477
llvm::Value *ThisValue);

0 commit comments

Comments
 (0)