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