Skip to content

Commit 3213a5d

Browse files
committed
[clang] Extract negativity check lambda to function
1 parent 8c4546f commit 3213a5d

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,21 @@ void ScalarExprEmitter::EmitIntegerTruncationCheck(Value *Src, QualType SrcType,
11031103
{Src, Dst});
11041104
}
11051105

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+
11061121
// Should be called within CodeGenFunction::SanitizerScope RAII scope.
11071122
// Returns 'i1 false' when the conversion Src -> Dst changed the sign.
11081123
static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
@@ -1127,30 +1142,12 @@ EmitIntegerSignChangeCheckHelper(Value *Src, QualType SrcType, Value *Dst,
11271142
assert(((SrcBits != DstBits) || (SrcSigned != DstSigned)) &&
11281143
"either the widths should be different, or the signednesses.");
11291144

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-
11501145
// 1. Was the old Value negative?
1151-
llvm::Value *SrcIsNegative = EmitIsNegativeTest(Src, SrcType, "src");
1146+
llvm::Value *SrcIsNegative =
1147+
EmitIsNegativeTestHelper(Src, SrcType, "src", Builder);
11521148
// 2. Is the new Value negative?
1153-
llvm::Value *DstIsNegative = EmitIsNegativeTest(Dst, DstType, "dst");
1149+
llvm::Value *DstIsNegative =
1150+
EmitIsNegativeTestHelper(Dst, DstType, "dst", Builder);
11541151
// 3. Now, was the 'negativity status' preserved during the conversion?
11551152
// NOTE: conversion from negative to zero is considered to change the sign.
11561153
// (We want to get 'false' when the conversion changed the sign)

0 commit comments

Comments
 (0)