diff options
author | Nikita Popov <nikita.ppv@gmail.com> | 2021-07-23 23:56:41 +0200 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2021-07-23 23:56:41 +0200 |
commit | f623b3a29aa9932fedf5b043b0f3b7535a19fdb7 (patch) | |
tree | 463e60d87547a7e19417f63e5c72ea2f17cbe2a9 /llvm/lib/IR/ConstantFold.cpp | |
parent | 923727e8bedac0257f5603ca3a1032737b01be96 (diff) | |
download | llvm-f623b3a29aa9932fedf5b043b0f3b7535a19fdb7.zip llvm-f623b3a29aa9932fedf5b043b0f3b7535a19fdb7.tar.gz llvm-f623b3a29aa9932fedf5b043b0f3b7535a19fdb7.tar.bz2 |
[ConstantFold] Fix GEP of GEP fold with opaque pointers
This was previously combining indices even though they operate on
different types. For non-opaque pointers, the condition is
automatically satisfied based on the pointer types being equal.
Diffstat (limited to 'llvm/lib/IR/ConstantFold.cpp')
-rw-r--r-- | llvm/lib/IR/ConstantFold.cpp | 7 |
1 files changed, 5 insertions, 2 deletions
diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index ca14787..5f05aa2 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -2347,8 +2347,11 @@ static bool isIndexInRangeOfArrayType(uint64_t NumElements, // Combine Indices - If the source pointer to this getelementptr instruction // is a getelementptr instruction, combine the indices of the two // getelementptr instructions into a single instruction. -static Constant *foldGEPOfGEP(GEPOperator *GEP, bool InBounds, +static Constant *foldGEPOfGEP(GEPOperator *GEP, Type *PointeeTy, bool InBounds, ArrayRef<Value *> Idxs) { + if (PointeeTy != GEP->getResultElementType()) + return nullptr; + Constant *Idx0 = cast<Constant>(Idxs[0]); if (Idx0->isNullValue()) { // Handle the simple case of a zero index. @@ -2491,7 +2494,7 @@ Constant *llvm::ConstantFoldGetElementPtr(Type *PointeeTy, Constant *C, if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) { if (auto *GEP = dyn_cast<GEPOperator>(CE)) - if (Constant *C = foldGEPOfGEP(GEP, InBounds, Idxs)) + if (Constant *C = foldGEPOfGEP(GEP, PointeeTy, InBounds, Idxs)) return C; // Attempt to fold casts to the same type away. For example, folding: |