Skip to content

Commit 8fcac29

Browse files
committed
Refactor the 'count' and 'index' retrievals.
1 parent 27b2ad9 commit 8fcac29

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,28 @@ static llvm::Value *EmitPositiveResultOrZero(CodeGenFunction &CGF,
10871087
return CGF.Builder.CreateSelect(Cmp, Res, ConstantInt::get(ResType, 0, IsSigned));
10881088
}
10891089

1090+
static std::pair<llvm::Value *, llvm::Value *>
1091+
GetCountFieldAndIndex(CodeGenFunction &CGF, const MemberExpr *ME,
1092+
const FieldDecl *ArrayFD, const FieldDecl *CountFD,
1093+
const Expr *Idx, llvm::IntegerType *ResType,
1094+
bool IsSigned) {
1095+
// count = ptr->count;
1096+
Value *Count = CGF.EmitLoadOfCountedByField(ME, ArrayFD, CountFD);
1097+
if (!Count)
1098+
return std::make_pair<Value *>(nullptr, nullptr);
1099+
Count = CGF.Builder.CreateIntCast(Count, ResType, IsSigned, "count");
1100+
1101+
// index = ptr->index;
1102+
Value *Index = nullptr;
1103+
if (Idx) {
1104+
bool IdxSigned = Idx->getType()->isSignedIntegerType();
1105+
Index = CGF.EmitScalarExpr(Idx);
1106+
Index = CGF.Builder.CreateIntCast(Index, ResType, IdxSigned, "index");
1107+
}
1108+
1109+
return std::make_pair(Count, Index);
1110+
}
1111+
10901112
llvm::Value *CodeGenFunction::emitCountedByPointerSize(
10911113
const ImplicitCastExpr *E, const Expr *Idx, llvm::Value *EmittedE,
10921114
unsigned Type, llvm::IntegerType *ResType) {
@@ -1170,18 +1192,12 @@ llvm::Value *CodeGenFunction::emitCountedByPointerSize(
11701192
llvm::ConstantInt::get(ResType, BaseSize.getQuantity(), IsSigned);
11711193

11721194
// count = ptr->count;
1173-
Value *Count = EmitLoadOfCountedByField(ME, ArrayBaseFD, CountFD);
1195+
// index = ptr->index;
1196+
Value *Count, *Index;
1197+
std::tie(Count, Index) = GetCountFieldAndIndex(
1198+
*this, ME, ArrayBaseFD, CountFD, Idx, ResType, IsSigned);
11741199
if (!Count)
11751200
return nullptr;
1176-
Count = Builder.CreateIntCast(Count, ResType, IsSigned, "count");
1177-
1178-
// index = ptr->index;
1179-
Value *Index = nullptr;
1180-
if (Idx) {
1181-
bool IdxSigned = Idx->getType()->isSignedIntegerType();
1182-
Index = EmitScalarExpr(Idx);
1183-
Index = Builder.CreateIntCast(Index, ResType, IdxSigned, "index");
1184-
}
11851201

11861202
// array_size = count * array_base_size;
11871203
Value *ArraySize = Builder.CreateMul(Count, ArrayBaseSize, "array_size",
@@ -1335,18 +1351,12 @@ CodeGenFunction::emitCountedByMemberSize(const MemberExpr *ME, const Expr *Idx,
13351351
}
13361352

13371353
// count = ptr->count;
1338-
Value *Count = EmitLoadOfCountedByField(ME, FlexibleArrayMemberFD, CountFD);
1354+
// index = ptr->index;
1355+
Value *Count, *Index;
1356+
std::tie(Count, Index) = GetCountFieldAndIndex(
1357+
*this, ME, FlexibleArrayMemberFD, CountFD, Idx, ResType, IsSigned);
13391358
if (!Count)
13401359
return nullptr;
1341-
Count = Builder.CreateIntCast(Count, ResType, IsSigned, "count");
1342-
1343-
// index = ptr->index;
1344-
Value *Index = nullptr;
1345-
if (Idx) {
1346-
bool IdxSigned = Idx->getType()->isSignedIntegerType();
1347-
Index = EmitScalarExpr(Idx);
1348-
Index = Builder.CreateIntCast(Index, ResType, IdxSigned, "index");
1349-
}
13501360

13511361
// flexible_array_member_base_size = sizeof (*ptr->array);
13521362
const ArrayType *ArrayTy = Ctx.getAsArrayType(FlexibleArrayMemberTy);

0 commit comments

Comments
 (0)