aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorDavid Majnemer <david.majnemer@gmail.com>2016-07-13 05:16:16 +0000
committerDavid Majnemer <david.majnemer@gmail.com>2016-07-13 05:16:16 +0000
commit1b3db33e3d67b708c489280b7efeaf33e46a4e05 (patch)
tree7d413a2a3642c3b50bc54c9868dd445b7e0b8e75 /llvm/lib/Analysis/ConstantFolding.cpp
parentc2f791d8a798676067116518ae2b8cfa3c90d638 (diff)
downloadllvm-1b3db33e3d67b708c489280b7efeaf33e46a4e05.zip
llvm-1b3db33e3d67b708c489280b7efeaf33e46a4e05.tar.gz
llvm-1b3db33e3d67b708c489280b7efeaf33e46a4e05.tar.bz2
[ConstantFolding] Don't treat negative GEP offsets as positive
GEP offsets are signed, don't treat them as huge positive numbers. llvm-svn: 275251
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp11
1 files changed, 7 insertions, 4 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index bf0cb3e..20370d2 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -845,15 +845,18 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
// Determine which element of the array the offset points into.
APInt ElemSize(BitWidth, DL.getTypeAllocSize(Ty));
- if (ElemSize == 0)
+ if (ElemSize == 0) {
// The element size is 0. This may be [0 x Ty]*, so just use a zero
// index for this level and proceed to the next level to see if it can
// accommodate the offset.
NewIdxs.push_back(ConstantInt::get(IntPtrTy, 0));
- else {
+ } else if (ElemSize.isAllOnesValue()) {
+ // Avoid signed overflow.
+ break;
+ } else {
// The element size is non-zero divide the offset by the element
// size (rounding down), to compute the index at this level.
- APInt NewIdx = Offset.udiv(ElemSize);
+ APInt NewIdx = Offset.sdiv(ElemSize);
Offset -= NewIdx * ElemSize;
NewIdxs.push_back(ConstantInt::get(IntPtrTy, NewIdx));
}
@@ -864,7 +867,7 @@ Constant *SymbolicallyEvaluateGEP(const GEPOperator *GEP,
// operand likely went through casts that are necessary to make the GEP
// sensible.
const StructLayout &SL = *DL.getStructLayout(STy);
- if (Offset.uge(SL.getSizeInBytes()))
+ if (Offset.isNegative() || Offset.uge(SL.getSizeInBytes()))
break;
// Determine which field of the struct the offset points into. The