diff options
author | Nate Begeman <natebegeman@mac.com> | 2008-03-31 00:22:16 +0000 |
---|---|---|
committer | Nate Begeman <natebegeman@mac.com> | 2008-03-31 00:22:16 +0000 |
commit | 83ad90a779f8ce46d6923bc6f63aa15b83968efb (patch) | |
tree | f4a3fa7b8c2fc4d0a222c6c9278f930dc0eedd1d /lib/VMCore/ConstantFold.cpp | |
parent | 4f678c36922d2537c1a81bd1946691f3acabd515 (diff) | |
download | external_llvm-83ad90a779f8ce46d6923bc6f63aa15b83968efb.zip external_llvm-83ad90a779f8ce46d6923bc6f63aa15b83968efb.tar.gz external_llvm-83ad90a779f8ce46d6923bc6f63aa15b83968efb.tar.bz2 |
Don't eliminate bitcast instructions that change the type of a pointer
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@48971 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/VMCore/ConstantFold.cpp')
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 40 |
1 files changed, 21 insertions, 19 deletions
diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 7b21817..1f75fe5 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -96,27 +96,29 @@ static Constant *FoldBitCast(Constant *V, const Type *DestTy) { // Check to see if we are casting a pointer to an aggregate to a pointer to // the first element. If so, return the appropriate GEP instruction. if (const PointerType *PTy = dyn_cast<PointerType>(V->getType())) - if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) { - SmallVector<Value*, 8> IdxList; - IdxList.push_back(Constant::getNullValue(Type::Int32Ty)); - const Type *ElTy = PTy->getElementType(); - while (ElTy != DPTy->getElementType()) { - if (const StructType *STy = dyn_cast<StructType>(ElTy)) { - if (STy->getNumElements() == 0) break; - ElTy = STy->getElementType(0); - IdxList.push_back(Constant::getNullValue(Type::Int32Ty)); - } else if (const SequentialType *STy = dyn_cast<SequentialType>(ElTy)) { - if (isa<PointerType>(ElTy)) break; // Can't index into pointers! - ElTy = STy->getElementType(); - IdxList.push_back(IdxList[0]); - } else { - break; + if (const PointerType *DPTy = dyn_cast<PointerType>(DestTy)) + if (PTy->getAddressSpace() == DPTy->getAddressSpace()) { + SmallVector<Value*, 8> IdxList; + IdxList.push_back(Constant::getNullValue(Type::Int32Ty)); + const Type *ElTy = PTy->getElementType(); + while (ElTy != DPTy->getElementType()) { + if (const StructType *STy = dyn_cast<StructType>(ElTy)) { + if (STy->getNumElements() == 0) break; + ElTy = STy->getElementType(0); + IdxList.push_back(Constant::getNullValue(Type::Int32Ty)); + } else if (const SequentialType *STy = + dyn_cast<SequentialType>(ElTy)) { + if (isa<PointerType>(ElTy)) break; // Can't index into pointers! + ElTy = STy->getElementType(); + IdxList.push_back(IdxList[0]); + } else { + break; + } } + + if (ElTy == DPTy->getElementType()) + return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size()); } - - if (ElTy == DPTy->getElementType()) - return ConstantExpr::getGetElementPtr(V, &IdxList[0], IdxList.size()); - } // Handle casts from one vector constant to another. We know that the src // and dest type have the same size (otherwise its an illegal cast). |