@@ -4183,9 +4183,8 @@ Value *ScalarExprEmitter::EmitOverflowCheckedBinOp(const BinOpInfo &Ops) {
4183
4183
return phi;
4184
4184
}
4185
4185
4186
- // / Emit pointer + index arithmetic.
4187
- static Value *emitPointerArithmetic (CodeGenFunction &CGF,
4188
- const BinOpInfo &op,
4186
+ // / This function is used for BO_Add/BO_Sub/BO_AddAssign/BO_SubAssign.
4187
+ static Value *emitPointerArithmetic (CodeGenFunction &CGF, const BinOpInfo &op,
4189
4188
bool isSubtraction) {
4190
4189
// Must have binary (not unary) expr here. Unary pointer
4191
4190
// increment/decrement doesn't use this path.
@@ -4202,11 +4201,19 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,
4202
4201
std::swap (pointerOperand, indexOperand);
4203
4202
}
4204
4203
4204
+ return CGF.EmitPointerArithmetic (expr, pointerOperand, pointer, indexOperand,
4205
+ index, isSubtraction);
4206
+ }
4207
+
4208
+ // / Emit pointer + index arithmetic.
4209
+ llvm::Value *CodeGenFunction::EmitPointerArithmetic (
4210
+ const BinaryOperator *BO, Expr *pointerOperand, llvm::Value *pointer,
4211
+ Expr *indexOperand, llvm::Value *index, bool isSubtraction) {
4205
4212
bool isSigned = indexOperand->getType ()->isSignedIntegerOrEnumerationType ();
4206
4213
4207
4214
unsigned width = cast<llvm::IntegerType>(index->getType ())->getBitWidth ();
4208
- auto &DL = CGF. CGM .getDataLayout ();
4209
- auto PtrTy = cast<llvm::PointerType>(pointer->getType ());
4215
+ auto &DL = CGM.getDataLayout ();
4216
+ auto * PtrTy = cast<llvm::PointerType>(pointer->getType ());
4210
4217
4211
4218
// Some versions of glibc and gcc use idioms (particularly in their malloc
4212
4219
// routines) that add a pointer-sized integer (known to be a pointer value)
@@ -4227,79 +4234,77 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,
4227
4234
//
4228
4235
// Note that we do not suppress the pointer overflow check in this case.
4229
4236
if (BinaryOperator::isNullPointerArithmeticExtension (
4230
- CGF. getContext (), op. Opcode , expr-> getLHS (), expr-> getRHS () )) {
4231
- Value *Ptr = CGF. Builder .CreateIntToPtr (index, pointer->getType ());
4232
- if (CGF. getLangOpts ().PointerOverflowDefined ||
4233
- !CGF. SanOpts .has (SanitizerKind::PointerOverflow) ||
4234
- NullPointerIsDefined (CGF. Builder .GetInsertBlock ()->getParent (),
4237
+ getContext (), BO-> getOpcode (), pointerOperand, indexOperand )) {
4238
+ llvm:: Value *Ptr = Builder.CreateIntToPtr (index, pointer->getType ());
4239
+ if (getLangOpts ().PointerOverflowDefined ||
4240
+ !SanOpts.has (SanitizerKind::PointerOverflow) ||
4241
+ NullPointerIsDefined (Builder.GetInsertBlock ()->getParent (),
4235
4242
PtrTy->getPointerAddressSpace ()))
4236
4243
return Ptr;
4237
4244
// The inbounds GEP of null is valid iff the index is zero.
4238
4245
auto CheckOrdinal = SanitizerKind::SO_PointerOverflow;
4239
4246
auto CheckHandler = SanitizerHandler::PointerOverflow;
4240
- SanitizerDebugLocation SanScope (&CGF, {CheckOrdinal}, CheckHandler);
4241
- Value *IsZeroIndex = CGF.Builder .CreateIsNull (index);
4242
- llvm::Constant *StaticArgs[] = {
4243
- CGF.EmitCheckSourceLocation (op.E ->getExprLoc ())};
4247
+ SanitizerDebugLocation SanScope (this , {CheckOrdinal}, CheckHandler);
4248
+ llvm::Value *IsZeroIndex = Builder.CreateIsNull (index);
4249
+ llvm::Constant *StaticArgs[] = {EmitCheckSourceLocation (BO->getExprLoc ())};
4244
4250
llvm::Type *IntPtrTy = DL.getIntPtrType (PtrTy);
4245
- Value *IntPtr = llvm::Constant::getNullValue (IntPtrTy);
4246
- Value *ComputedGEP = CGF. Builder .CreateZExtOrTrunc (index, IntPtrTy);
4247
- Value *DynamicArgs[] = {IntPtr, ComputedGEP};
4248
- CGF. EmitCheck ({{IsZeroIndex, CheckOrdinal}}, CheckHandler, StaticArgs,
4249
- DynamicArgs);
4251
+ llvm:: Value *IntPtr = llvm::Constant::getNullValue (IntPtrTy);
4252
+ llvm:: Value *ComputedGEP = Builder.CreateZExtOrTrunc (index, IntPtrTy);
4253
+ llvm:: Value *DynamicArgs[] = {IntPtr, ComputedGEP};
4254
+ EmitCheck ({{IsZeroIndex, CheckOrdinal}}, CheckHandler, StaticArgs,
4255
+ DynamicArgs);
4250
4256
return Ptr;
4251
4257
}
4252
4258
4253
4259
if (width != DL.getIndexTypeSizeInBits (PtrTy)) {
4254
4260
// Zero-extend or sign-extend the pointer value according to
4255
4261
// whether the index is signed or not.
4256
- index = CGF. Builder .CreateIntCast (index, DL.getIndexType (PtrTy), isSigned,
4257
- " idx.ext" );
4262
+ index = Builder.CreateIntCast (index, DL.getIndexType (PtrTy), isSigned,
4263
+ " idx.ext" );
4258
4264
}
4259
4265
4260
4266
// If this is subtraction, negate the index.
4261
4267
if (isSubtraction)
4262
- index = CGF. Builder .CreateNeg (index, " idx.neg" );
4268
+ index = Builder.CreateNeg (index, " idx.neg" );
4263
4269
4264
- if (CGF. SanOpts .has (SanitizerKind::ArrayBounds))
4265
- CGF. EmitBoundsCheck (op. E , pointerOperand, index, indexOperand->getType (),
4266
- /* Accessed*/ false );
4270
+ if (SanOpts.has (SanitizerKind::ArrayBounds))
4271
+ EmitBoundsCheck (BO , pointerOperand, index, indexOperand->getType (),
4272
+ /* Accessed*/ false );
4267
4273
4268
- const PointerType *pointerType
4269
- = pointerOperand->getType ()->getAs <PointerType>();
4274
+ const PointerType *pointerType =
4275
+ pointerOperand->getType ()->getAs <PointerType>();
4270
4276
if (!pointerType) {
4271
4277
QualType objectType = pointerOperand->getType ()
4272
- ->castAs <ObjCObjectPointerType>()
4273
- ->getPointeeType ();
4274
- llvm::Value *objectSize
4275
- = CGF. CGM .getSize (CGF. getContext ().getTypeSizeInChars (objectType));
4278
+ ->castAs <ObjCObjectPointerType>()
4279
+ ->getPointeeType ();
4280
+ llvm::Value *objectSize =
4281
+ CGM.getSize (getContext ().getTypeSizeInChars (objectType));
4276
4282
4277
- index = CGF. Builder .CreateMul (index, objectSize);
4283
+ index = Builder.CreateMul (index, objectSize);
4278
4284
4279
- Value *result =
4280
- CGF.Builder .CreateGEP (CGF.Int8Ty , pointer, index, " add.ptr" );
4281
- return CGF.Builder .CreateBitCast (result, pointer->getType ());
4285
+ llvm::Value *result = Builder.CreateGEP (Int8Ty, pointer, index, " add.ptr" );
4286
+ return Builder.CreateBitCast (result, pointer->getType ());
4282
4287
}
4283
4288
4284
4289
QualType elementType = pointerType->getPointeeType ();
4285
- if (const VariableArrayType *vla
4286
- = CGF. getContext ().getAsVariableArrayType (elementType)) {
4290
+ if (const VariableArrayType *vla =
4291
+ getContext ().getAsVariableArrayType (elementType)) {
4287
4292
// The element count here is the total number of non-VLA elements.
4288
- llvm::Value *numElements = CGF. getVLASize (vla).NumElts ;
4293
+ llvm::Value *numElements = getVLASize (vla).NumElts ;
4289
4294
4290
4295
// Effectively, the multiply by the VLA size is part of the GEP.
4291
4296
// GEP indexes are signed, and scaling an index isn't permitted to
4292
4297
// signed-overflow, so we use the same semantics for our explicit
4293
4298
// multiply. We suppress this if overflow is not undefined behavior.
4294
- llvm::Type *elemTy = CGF. ConvertTypeForMem (vla->getElementType ());
4295
- if (CGF. getLangOpts ().PointerOverflowDefined ) {
4296
- index = CGF. Builder .CreateMul (index, numElements, " vla.index" );
4297
- pointer = CGF. Builder .CreateGEP (elemTy, pointer, index, " add.ptr" );
4299
+ llvm::Type *elemTy = ConvertTypeForMem (vla->getElementType ());
4300
+ if (getLangOpts ().PointerOverflowDefined ) {
4301
+ index = Builder.CreateMul (index, numElements, " vla.index" );
4302
+ pointer = Builder.CreateGEP (elemTy, pointer, index, " add.ptr" );
4298
4303
} else {
4299
- index = CGF. Builder .CreateNSWMul (index, numElements, " vla.index" );
4300
- pointer = CGF. EmitCheckedInBoundsGEP (
4301
- elemTy, pointer, index, isSigned, isSubtraction, op. E -> getExprLoc () ,
4302
- " add.ptr" );
4304
+ index = Builder.CreateNSWMul (index, numElements, " vla.index" );
4305
+ pointer =
4306
+ EmitCheckedInBoundsGEP ( elemTy, pointer, index, isSigned,
4307
+ isSubtraction, BO-> getExprLoc (), " add.ptr" );
4303
4308
}
4304
4309
return pointer;
4305
4310
}
@@ -4309,16 +4314,15 @@ static Value *emitPointerArithmetic(CodeGenFunction &CGF,
4309
4314
// future proof.
4310
4315
llvm::Type *elemTy;
4311
4316
if (elementType->isVoidType () || elementType->isFunctionType ())
4312
- elemTy = CGF. Int8Ty ;
4317
+ elemTy = Int8Ty;
4313
4318
else
4314
- elemTy = CGF. ConvertTypeForMem (elementType);
4319
+ elemTy = ConvertTypeForMem (elementType);
4315
4320
4316
- if (CGF. getLangOpts ().PointerOverflowDefined )
4317
- return CGF. Builder .CreateGEP (elemTy, pointer, index, " add.ptr" );
4321
+ if (getLangOpts ().PointerOverflowDefined )
4322
+ return Builder.CreateGEP (elemTy, pointer, index, " add.ptr" );
4318
4323
4319
- return CGF.EmitCheckedInBoundsGEP (
4320
- elemTy, pointer, index, isSigned, isSubtraction, op.E ->getExprLoc (),
4321
- " add.ptr" );
4324
+ return EmitCheckedInBoundsGEP (elemTy, pointer, index, isSigned, isSubtraction,
4325
+ BO->getExprLoc (), " add.ptr" );
4322
4326
}
4323
4327
4324
4328
// Construct an fmuladd intrinsic to represent a fused mul-add of MulOp and
0 commit comments