Skip to content

Commit 3e01e8b

Browse files
authored
[NFC] Address compiler warnings: C4146 - Another round of use two's complement instead of negation (microsoft#7567)
Addresses microsoft#7565 A few more instances where we can take advantage of -N being equivalent to (~N + 1)
1 parent 8f55958 commit 3e01e8b

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

lib/Analysis/BasicAliasAnalysis.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1117,7 +1117,11 @@ AliasResult BasicAliasAnalysis::aliasGEP(
11171117
// stripped a gep with negative index ('gep <ptr>, -1, ...).
11181118
if (V1Size != MemoryLocation::UnknownSize &&
11191119
V2Size != MemoryLocation::UnknownSize) {
1120-
if (-(uint64_t)GEP1BaseOffset < V1Size)
1120+
// GEP1BaseOffset is negative in this else block and because we're
1121+
// assigning to an unsigned variable, we can make use of
1122+
// -I == (~I + 1) to compute the absolute value of GEP1BaseOffset.
1123+
const uint64_t GEP1BaseOffsetAbs = (~GEP1BaseOffset + 1ULL);
1124+
if (GEP1BaseOffsetAbs < V1Size)
11211125
return PartialAlias;
11221126
return NoAlias;
11231127
}

lib/Analysis/ConstantFolding.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
187187
// Shift it to the right place, depending on endianness.
188188
Src = ConstantExpr::getShl(Src,
189189
ConstantInt::get(Src->getType(), ShiftAmt));
190-
ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
190+
ShiftAmt += isLittleEndian ? SrcBitSize : (~SrcBitSize + 1U);
191191

192192
// Mix it in.
193193
Elt = ConstantExpr::getOr(Elt, Src);
@@ -213,7 +213,7 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
213213
// endianness.
214214
Constant *Elt = ConstantExpr::getLShr(Src,
215215
ConstantInt::get(Src->getType(), ShiftAmt));
216-
ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
216+
ShiftAmt += isLittleEndian ? DstBitSize : (~DstBitSize + 1U);
217217

218218
// Truncate the element to an integer with the same pointer size and
219219
// convert the element back to a pointer using a inttoptr.

lib/Analysis/InstructionSimplify.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4109,7 +4109,7 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
41094109
// Shift it to the right place, depending on endianness.
41104110
Src = ConstantExpr::getShl(Src,
41114111
ConstantInt::get(Src->getType(), ShiftAmt));
4112-
ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize;
4112+
ShiftAmt += isLittleEndian ? SrcBitSize : (~SrcBitSize + 1U);
41134113

41144114
// Mix it in.
41154115
Elt = ConstantExpr::getOr(Elt, Src);
@@ -4144,9 +4144,9 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) {
41444144
for (unsigned j = 0; j != Ratio; ++j) {
41454145
// Shift the piece of the value into the right place, depending on
41464146
// endianness.
4147-
Constant *Elt = ConstantExpr::getLShr(Src,
4148-
ConstantInt::get(Src->getType(), ShiftAmt));
4149-
ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize;
4147+
Constant *Elt = ConstantExpr::getLShr(
4148+
Src, ConstantInt::get(Src->getType(), ShiftAmt));
4149+
ShiftAmt += isLittleEndian ? DstBitSize : (~DstBitSize + 1U);
41504150

41514151
// Truncate the element to an integer with the same pointer size and
41524152
// convert the element back to a pointer using a inttoptr.

0 commit comments

Comments
 (0)