diff options
author | David Blaikie <dblaikie@gmail.com> | 2015-05-06 23:49:14 +0000 |
---|---|---|
committer | David Blaikie <dblaikie@gmail.com> | 2015-05-06 23:49:14 +0000 |
commit | e66a45fdb4ceb0ffab3ae1d5452583f9b064aecc (patch) | |
tree | b5b39342aae4ce8a29e825922b90b7c9e058b31c /llvm/lib/IR | |
parent | 74b63ebd53c292d95388b2159b26fb510af3ae3f (diff) | |
download | bcm5719-llvm-e66a45fdb4ceb0ffab3ae1d5452583f9b064aecc.tar.gz bcm5719-llvm-e66a45fdb4ceb0ffab3ae1d5452583f9b064aecc.zip |
[opaque pointer type] Pass explicit pointer type through GEP constant folding
llvm-svn: 236670
Diffstat (limited to 'llvm/lib/IR')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 30 | ||||
-rw-r--r-- | llvm/lib/IR/ConstantFold.h | 4 | ||||
-rw-r--r-- | llvm/lib/IR/Constants.cpp | 12 |
3 files changed, 34 insertions, 12 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 2a524937391..8afb3e489de 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -2028,7 +2028,7 @@ static bool isIndexInRangeOfSequentialType(const SequentialType *STy, } template<typename IndexTy> -static Constant *ConstantFoldGetElementPtrImpl(Constant *C, +static Constant *ConstantFoldGetElementPtrImpl(Type *PointeeTy, Constant *C, bool inBounds, ArrayRef<IndexTy> Idxs) { if (Idxs.empty()) return C; @@ -2165,9 +2165,9 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, // factored out into preceding dimensions. bool Unknown = false; SmallVector<Constant *, 8> NewIdxs; - Type *Ty = C->getType(); - Type *Prev = nullptr; - for (unsigned i = 0, e = Idxs.size(); i != e; + Type *Ty = PointeeTy; + Type *Prev = C->getType(); + for (unsigned i = 1, e = Idxs.size(); i != e; Prev = Ty, Ty = cast<CompositeType>(Ty)->getTypeAtIndex(Idxs[i]), ++i) { if (ConstantInt *CI = dyn_cast<ConstantInt>(Idxs[i])) { if (isa<ArrayType>(Ty) || isa<VectorType>(Ty)) @@ -2229,7 +2229,7 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, if (!Unknown && !inBounds) if (auto *GV = dyn_cast<GlobalVariable>(C)) if (!GV->hasExternalWeakLinkage() && isInBoundsIndices(Idxs)) - return ConstantExpr::getInBoundsGetElementPtr(nullptr, C, Idxs); + return ConstantExpr::getInBoundsGetElementPtr(PointeeTy, C, Idxs); return nullptr; } @@ -2237,11 +2237,27 @@ static Constant *ConstantFoldGetElementPtrImpl(Constant *C, Constant *llvm::ConstantFoldGetElementPtr(Constant *C, bool inBounds, ArrayRef<Constant *> Idxs) { - return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs); + return ConstantFoldGetElementPtrImpl( + cast<PointerType>(C->getType()->getScalarType())->getElementType(), C, + inBounds, Idxs); } Constant *llvm::ConstantFoldGetElementPtr(Constant *C, bool inBounds, ArrayRef<Value *> Idxs) { - return ConstantFoldGetElementPtrImpl(C, inBounds, Idxs); + return ConstantFoldGetElementPtrImpl( + cast<PointerType>(C->getType()->getScalarType())->getElementType(), C, + inBounds, Idxs); +} + +Constant *llvm::ConstantFoldGetElementPtr(Type *Ty, Constant *C, + bool inBounds, + ArrayRef<Constant *> Idxs) { + return ConstantFoldGetElementPtrImpl(Ty, C, inBounds, Idxs); +} + +Constant *llvm::ConstantFoldGetElementPtr(Type *Ty, Constant *C, + bool inBounds, + ArrayRef<Value *> Idxs) { + return ConstantFoldGetElementPtrImpl(Ty, C, inBounds, Idxs); } diff --git a/llvm/lib/IR/ConstantFold.h b/llvm/lib/IR/ConstantFold.h index a516abe024e..42a9c6ba908 100644 --- a/llvm/lib/IR/ConstantFold.h +++ b/llvm/lib/IR/ConstantFold.h @@ -51,6 +51,10 @@ namespace llvm { ArrayRef<Constant *> Idxs); Constant *ConstantFoldGetElementPtr(Constant *C, bool inBounds, ArrayRef<Value *> Idxs); + Constant *ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool inBounds, + ArrayRef<Constant *> Idxs); + Constant *ConstantFoldGetElementPtr(Type *Ty, Constant *C, bool inBounds, + ArrayRef<Value *> Idxs); } // End llvm namespace #endif diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index 20a5206c230..b598c2807ce 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -2015,14 +2015,16 @@ Constant *ConstantExpr::getSelect(Constant *C, Constant *V1, Constant *V2, Constant *ConstantExpr::getGetElementPtr(Type *Ty, Constant *C, ArrayRef<Value *> Idxs, bool InBounds, Type *OnlyIfReducedTy) { - if (Constant *FC = ConstantFoldGetElementPtr(C, InBounds, Idxs)) - return FC; // Fold a few common cases. - if (!Ty) Ty = cast<PointerType>(C->getType()->getScalarType())->getElementType(); else - assert(Ty == - cast<PointerType>(C->getType()->getScalarType())->getElementType()); + assert( + Ty == + cast<PointerType>(C->getType()->getScalarType())->getContainedType(0u)); + + if (Constant *FC = ConstantFoldGetElementPtr(Ty, C, InBounds, Idxs)) + return FC; // Fold a few common cases. + // Get the result type of the getelementptr! Type *DestTy = GetElementPtrInst::getIndexedType(Ty, Idxs); assert(DestTy && "GEP indices invalid!"); |