diff --git a/lib/Analysis/BasicAliasAnalysis.cpp b/lib/Analysis/BasicAliasAnalysis.cpp index aa0f9ed873..956c334374 100644 --- a/lib/Analysis/BasicAliasAnalysis.cpp +++ b/lib/Analysis/BasicAliasAnalysis.cpp @@ -1117,7 +1117,11 @@ AliasResult BasicAliasAnalysis::aliasGEP( // stripped a gep with negative index ('gep , -1, ...). if (V1Size != MemoryLocation::UnknownSize && V2Size != MemoryLocation::UnknownSize) { - if (-(uint64_t)GEP1BaseOffset < V1Size) + // GEP1BaseOffset is negative in this else block and because we're + // assigning to an unsigned variable, we can make use of + // -I == (~I + 1) to compute the absolute value of GEP1BaseOffset. + const uint64_t GEP1BaseOffsetAbs = (~GEP1BaseOffset + 1ULL); + if (GEP1BaseOffsetAbs < V1Size) return PartialAlias; return NoAlias; } diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp index 69c9b10b60..0167bdf0a1 100644 --- a/lib/Analysis/ConstantFolding.cpp +++ b/lib/Analysis/ConstantFolding.cpp @@ -187,7 +187,7 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { // Shift it to the right place, depending on endianness. Src = ConstantExpr::getShl(Src, ConstantInt::get(Src->getType(), ShiftAmt)); - ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; + ShiftAmt += isLittleEndian ? SrcBitSize : (~SrcBitSize + 1U); // Mix it in. Elt = ConstantExpr::getOr(Elt, Src); @@ -213,7 +213,7 @@ static Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { // endianness. Constant *Elt = ConstantExpr::getLShr(Src, ConstantInt::get(Src->getType(), ShiftAmt)); - ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; + ShiftAmt += isLittleEndian ? DstBitSize : (~DstBitSize + 1U); // Truncate the element to an integer with the same pointer size and // convert the element back to a pointer using a inttoptr. diff --git a/lib/Analysis/InstructionSimplify.cpp b/lib/Analysis/InstructionSimplify.cpp index 89c7cc7a3e..96c0b3302d 100644 --- a/lib/Analysis/InstructionSimplify.cpp +++ b/lib/Analysis/InstructionSimplify.cpp @@ -4109,7 +4109,7 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { // Shift it to the right place, depending on endianness. Src = ConstantExpr::getShl(Src, ConstantInt::get(Src->getType(), ShiftAmt)); - ShiftAmt += isLittleEndian ? SrcBitSize : -SrcBitSize; + ShiftAmt += isLittleEndian ? SrcBitSize : (~SrcBitSize + 1U); // Mix it in. Elt = ConstantExpr::getOr(Elt, Src); @@ -4144,9 +4144,9 @@ Constant *FoldBitCast(Constant *C, Type *DestTy, const DataLayout &DL) { for (unsigned j = 0; j != Ratio; ++j) { // Shift the piece of the value into the right place, depending on // endianness. - Constant *Elt = ConstantExpr::getLShr(Src, - ConstantInt::get(Src->getType(), ShiftAmt)); - ShiftAmt += isLittleEndian ? DstBitSize : -DstBitSize; + Constant *Elt = ConstantExpr::getLShr( + Src, ConstantInt::get(Src->getType(), ShiftAmt)); + ShiftAmt += isLittleEndian ? DstBitSize : (~DstBitSize + 1U); // Truncate the element to an integer with the same pointer size and // convert the element back to a pointer using a inttoptr.