@@ -1103,6 +1103,21 @@ void ScalarExprEmitter::EmitIntegerTruncationCheck(Value *Src, QualType SrcType,
1103
1103
{Src, Dst});
1104
1104
}
1105
1105
1106
+ static llvm::Value *EmitIsNegativeTestHelper (Value *V, QualType VType,
1107
+ const char *Name,
1108
+ CGBuilderTy &Builder) {
1109
+ bool VSigned = VType->isSignedIntegerOrEnumerationType ();
1110
+ llvm::Type *VTy = V->getType ();
1111
+ if (!VSigned) {
1112
+ // If the value is unsigned, then it is never negative.
1113
+ return llvm::ConstantInt::getFalse (VTy->getContext ());
1114
+ }
1115
+ llvm::Constant *Zero = llvm::ConstantInt::get (VTy, 0 );
1116
+ return Builder.CreateICmp (llvm::ICmpInst::ICMP_SLT, V, Zero,
1117
+ llvm::Twine (Name) + " ." + V->getName () +
1118
+ " .negativitycheck" );
1119
+ };
1120
+
1106
1121
// Should be called within CodeGenFunction::SanitizerScope RAII scope.
1107
1122
// Returns 'i1 false' when the conversion Src -> Dst changed the sign.
1108
1123
static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
@@ -1127,30 +1142,12 @@ EmitIntegerSignChangeCheckHelper(Value *Src, QualType SrcType, Value *Dst,
1127
1142
assert (((SrcBits != DstBits) || (SrcSigned != DstSigned)) &&
1128
1143
" either the widths should be different, or the signednesses." );
1129
1144
1130
- // NOTE: zero value is considered to be non-negative.
1131
- auto EmitIsNegativeTest = [&Builder](Value *V, QualType VType,
1132
- const char *Name) -> Value * {
1133
- // Is this value a signed type?
1134
- bool VSigned = VType->isSignedIntegerOrEnumerationType ();
1135
- llvm::Type *VTy = V->getType ();
1136
- if (!VSigned) {
1137
- // If the value is unsigned, then it is never negative.
1138
- // FIXME: can we encounter non-scalar VTy here?
1139
- return llvm::ConstantInt::getFalse (VTy->getContext ());
1140
- }
1141
- // Get the zero of the same type with which we will be comparing.
1142
- llvm::Constant *Zero = llvm::ConstantInt::get (VTy, 0 );
1143
- // %V.isnegative = icmp slt %V, 0
1144
- // I.e is %V *strictly* less than zero, does it have negative value?
1145
- return Builder.CreateICmp (llvm::ICmpInst::ICMP_SLT, V, Zero,
1146
- llvm::Twine (Name) + " ." + V->getName () +
1147
- " .negativitycheck" );
1148
- };
1149
-
1150
1145
// 1. Was the old Value negative?
1151
- llvm::Value *SrcIsNegative = EmitIsNegativeTest (Src, SrcType, " src" );
1146
+ llvm::Value *SrcIsNegative =
1147
+ EmitIsNegativeTestHelper (Src, SrcType, " src" , Builder);
1152
1148
// 2. Is the new Value negative?
1153
- llvm::Value *DstIsNegative = EmitIsNegativeTest (Dst, DstType, " dst" );
1149
+ llvm::Value *DstIsNegative =
1150
+ EmitIsNegativeTestHelper (Dst, DstType, " dst" , Builder);
1154
1151
// 3. Now, was the 'negativity status' preserved during the conversion?
1155
1152
// NOTE: conversion from negative to zero is considered to change the sign.
1156
1153
// (We want to get 'false' when the conversion changed the sign)
0 commit comments