Skip to content

Commit 3b0af99

Browse files
committed
[clang] Extract negativity check lambda to function
1 parent d109f94 commit 3b0af99

File tree

1 file changed

+25
-22
lines changed

1 file changed

+25
-22
lines changed

clang/lib/CodeGen/CGExprScalar.cpp

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,6 +1097,27 @@ void ScalarExprEmitter::EmitIntegerTruncationCheck(Value *Src, QualType SrcType,
10971097
{Src, Dst});
10981098
}
10991099

1100+
static llvm::Value *EmitIsNegativeTestHelper(Value *V, QualType VType,
1101+
const char *Name,
1102+
CGBuilderTy &Builder) {
1103+
// NOTE: zero value is considered to be non-negative.
1104+
// Is this value a signed type?
1105+
bool VSigned = VType->isSignedIntegerOrEnumerationType();
1106+
llvm::Type *VTy = V->getType();
1107+
if (!VSigned) {
1108+
// If the value is unsigned, then it is never negative.
1109+
// FIXME: can we encounter non-scalar VTy here?
1110+
return llvm::ConstantInt::getFalse(VTy->getContext());
1111+
}
1112+
// Get the zero of the same type with which we will be comparing.
1113+
llvm::Constant *Zero = llvm::ConstantInt::get(VTy, 0);
1114+
// %V.isnegative = icmp slt %V, 0
1115+
// I.e is %V *strictly* less than zero, does it have negative value?
1116+
return Builder.CreateICmp(llvm::ICmpInst::ICMP_SLT, V, Zero,
1117+
llvm::Twine(Name) + "." + V->getName() +
1118+
".negativitycheck");
1119+
};
1120+
11001121
// Should be called within CodeGenFunction::SanitizerScope RAII scope.
11011122
// Returns 'i1 false' when the conversion Src -> Dst changed the sign.
11021123
static std::pair<ScalarExprEmitter::ImplicitConversionCheckKind,
@@ -1121,30 +1142,12 @@ EmitIntegerSignChangeCheckHelper(Value *Src, QualType SrcType, Value *Dst,
11211142
assert(((SrcBits != DstBits) || (SrcSigned != DstSigned)) &&
11221143
"either the widths should be different, or the signednesses.");
11231144

1124-
// NOTE: zero value is considered to be non-negative.
1125-
auto EmitIsNegativeTest = [&Builder](Value *V, QualType VType,
1126-
const char *Name) -> Value * {
1127-
// Is this value a signed type?
1128-
bool VSigned = VType->isSignedIntegerOrEnumerationType();
1129-
llvm::Type *VTy = V->getType();
1130-
if (!VSigned) {
1131-
// If the value is unsigned, then it is never negative.
1132-
// FIXME: can we encounter non-scalar VTy here?
1133-
return llvm::ConstantInt::getFalse(VTy->getContext());
1134-
}
1135-
// Get the zero of the same type with which we will be comparing.
1136-
llvm::Constant *Zero = llvm::ConstantInt::get(VTy, 0);
1137-
// %V.isnegative = icmp slt %V, 0
1138-
// I.e is %V *strictly* less than zero, does it have negative value?
1139-
return Builder.CreateICmp(llvm::ICmpInst::ICMP_SLT, V, Zero,
1140-
llvm::Twine(Name) + "." + V->getName() +
1141-
".negativitycheck");
1142-
};
1143-
11441145
// 1. Was the old Value negative?
1145-
llvm::Value *SrcIsNegative = EmitIsNegativeTest(Src, SrcType, "src");
1146+
llvm::Value *SrcIsNegative =
1147+
EmitIsNegativeTestHelper(Src, SrcType, "src", Builder);
11461148
// 2. Is the new Value negative?
1147-
llvm::Value *DstIsNegative = EmitIsNegativeTest(Dst, DstType, "dst");
1149+
llvm::Value *DstIsNegative =
1150+
EmitIsNegativeTestHelper(Dst, DstType, "dst", Builder);
11481151
// 3. Now, was the 'negativity status' preserved during the conversion?
11491152
// NOTE: conversion from negative to zero is considered to change the sign.
11501153
// (We want to get 'false' when the conversion changed the sign)

0 commit comments

Comments
 (0)