diff options
| author | David Majnemer <david.majnemer@gmail.com> | 2016-08-07 07:58:12 +0000 |
|---|---|---|
| committer | David Majnemer <david.majnemer@gmail.com> | 2016-08-07 07:58:12 +0000 |
| commit | d150137f64831a5573015313d91a0562fb1935b8 (patch) | |
| tree | 76e7e41a0f3f128aa8b29f0d30694dbd178905cd /llvm/lib/Analysis | |
| parent | dc8767a49afe43b8d4bd5d9ad7e812bb662881fc (diff) | |
| download | bcm5719-llvm-d150137f64831a5573015313d91a0562fb1935b8.tar.gz bcm5719-llvm-d150137f64831a5573015313d91a0562fb1935b8.zip | |
[InstSimplify] Fold gep (gep V, C), (sub 0, V) to C
llvm-svn: 277952
Diffstat (limited to 'llvm/lib/Analysis')
| -rw-r--r-- | llvm/lib/Analysis/InstructionSimplify.cpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 69c60eb6f3c..e6aed6d6816 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -3645,6 +3645,26 @@ static Value *SimplifyGEPInst(Type *SrcTy, ArrayRef<Value *> Ops, } } + // gep (gep V, C), (sub 0, V) -> C + if (Q.DL.getTypeAllocSize(LastType) == 1 && + all_of(Ops.slice(1).drop_back(1), + [](Value *Idx) { return match(Idx, m_Zero()); })) { + unsigned PtrWidth = + Q.DL.getPointerSizeInBits(Ops[0]->getType()->getPointerAddressSpace()); + if (Q.DL.getTypeSizeInBits(Ops.back()->getType()) == PtrWidth) { + APInt BasePtrOffset(PtrWidth, 0); + Value *StrippedBasePtr = + Ops[0]->stripAndAccumulateInBoundsConstantOffsets(Q.DL, + BasePtrOffset); + + if (match(Ops.back(), + m_Sub(m_Zero(), m_PtrToInt(m_Specific(StrippedBasePtr))))) { + auto *CI = ConstantInt::get(GEPTy->getContext(), BasePtrOffset); + return ConstantExpr::getIntToPtr(CI, GEPTy); + } + } + } + // Check to see if this is constant foldable. for (unsigned i = 0, e = Ops.size(); i != e; ++i) if (!isa<Constant>(Ops[i])) |

