Skip to content

Commit 8b76571

Browse files
committed
[clang] Extract negativity check lambda to function
1 parent 47615dd commit 8b76571

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
@@ -1094,6 +1094,27 @@ void ScalarExprEmitter::EmitIntegerTruncationCheck(Value *Src, QualType SrcType,
10941094
{Src, Dst});
10951095
}
10961096

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

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

0 commit comments

Comments
 (0)