aboutsummaryrefslogtreecommitdiff
path: root/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Analysis/ConstantFolding.cpp')
-rw-r--r--lib/Analysis/ConstantFolding.cpp13
1 files changed, 11 insertions, 2 deletions
diff --git a/lib/Analysis/ConstantFolding.cpp b/lib/Analysis/ConstantFolding.cpp
index e2b1e258d7..95a68bf993 100644
--- a/lib/Analysis/ConstantFolding.cpp
+++ b/lib/Analysis/ConstantFolding.cpp
@@ -254,13 +254,22 @@ static bool IsConstantOffsetFromGlobal(Constant *C, GlobalValue *&GV,
if (!CI) return false; // Index isn't a simple constant?
if (CI->isZero()) continue; // Not adding anything.
+ // Evaluate offsets in the index type.
+ APInt APOffset(CI->getBitWidth(), Offset);
+
if (StructType *ST = dyn_cast<StructType>(*GTI)) {
// N = N + Offset
- Offset += TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue());
+ APOffset +=
+ APInt(CI->getBitWidth(),
+ TD.getStructLayout(ST)->getElementOffset(CI->getZExtValue()));
} else {
SequentialType *SQT = cast<SequentialType>(*GTI);
- Offset += TD.getTypeAllocSize(SQT->getElementType())*CI->getSExtValue();
+ APOffset +=
+ APInt(CI->getBitWidth(),
+ TD.getTypeAllocSize(SQT->getElementType())*CI->getSExtValue());
}
+
+ Offset = APOffset.getSExtValue();
}
return true;
}