summaryrefslogtreecommitdiff
path: root/lib/Analysis/ValueTracking.cpp
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2011-10-20 00:34:35 +0000
committerNick Lewycky <nicholas@mxc.ca>2011-10-20 00:34:35 +0000
commit0cd0fee91eadcee37d01398e05176e7c63bda2a7 (patch)
tree31a0f95a4e71949ff31bb21cc0d5d29ce73540a5 /lib/Analysis/ValueTracking.cpp
parent6690bca623d1f6405b95db5b1760f7ba8436e3fb (diff)
downloadllvm-0cd0fee91eadcee37d01398e05176e7c63bda2a7.tar.gz
llvm-0cd0fee91eadcee37d01398e05176e7c63bda2a7.tar.bz2
llvm-0cd0fee91eadcee37d01398e05176e7c63bda2a7.tar.xz
"@string = constant i8 0" is a value i8* string of length zero. Analyze that
correctly in GetStringLength, fixing PR11181! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142558 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Analysis/ValueTracking.cpp')
-rw-r--r--lib/Analysis/ValueTracking.cpp19
1 files changed, 13 insertions, 6 deletions
diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp
index 4d94f619fd..e80ee65abd 100644
--- a/lib/Analysis/ValueTracking.cpp
+++ b/lib/Analysis/ValueTracking.cpp
@@ -1525,8 +1525,7 @@ Value *llvm::GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset,
/// null-terminated C string pointed to by V. If successful, it returns true
/// and returns the string in Str. If unsuccessful, it returns false.
bool llvm::GetConstantStringInfo(const Value *V, std::string &Str,
- uint64_t Offset,
- bool StopAtNul) {
+ uint64_t Offset, bool StopAtNul) {
// If V is NULL then return false;
if (V == NULL) return false;
@@ -1536,7 +1535,7 @@ bool llvm::GetConstantStringInfo(const Value *V, std::string &Str,
// If the value is not a GEP instruction nor a constant expression with a
// GEP instruction, then return false because ConstantArray can't occur
- // any other way
+ // any other way.
const User *GEP = 0;
if (const GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(V)) {
GEP = GEPI;
@@ -1576,7 +1575,7 @@ bool llvm::GetConstantStringInfo(const Value *V, std::string &Str,
return GetConstantStringInfo(GEP->getOperand(0), Str, StartIdx+Offset,
StopAtNul);
}
-
+
// The GEP instruction, constant or instruction, must reference a global
// variable that is a constant and is initialized. The referenced constant
// initializer is the array that we'll use for optimization.
@@ -1585,8 +1584,8 @@ bool llvm::GetConstantStringInfo(const Value *V, std::string &Str,
return false;
const Constant *GlobalInit = GV->getInitializer();
- // Handle the ConstantAggregateZero case
- if (isa<ConstantAggregateZero>(GlobalInit)) {
+ // Handle the all-zeros case
+ if (GlobalInit->isNullValue()) {
// This is a degenerate case. The initializer is constant zero so the
// length of the string must be zero.
Str.clear();
@@ -1667,6 +1666,14 @@ static uint64_t GetStringLengthH(Value *V, SmallPtrSet<PHINode*, 32> &PHIs) {
return Len1;
}
+ // As a special-case, "@string = constant i8 0" is also a string with zero
+ // length, not wrapped in a bitcast or GEP.
+ if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
+ if (GV->isConstant() && GV->hasDefinitiveInitializer())
+ if (GV->getInitializer()->isNullValue()) return 1;
+ return 0;
+ }
+
// If the value is not a GEP instruction nor a constant expression with a
// GEP instruction, then return unknown.
User *GEP = 0;