summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms
diff options
context:
space:
mode:
authorDavid Majnemer <david.majnemer@gmail.com>2014-08-27 20:08:37 +0000
committerDavid Majnemer <david.majnemer@gmail.com>2014-08-27 20:08:37 +0000
commit22ccfc44845f29e850c52d4b3850f9ac8a82922c (patch)
treed220fc6fb4f8b2a66e5fd077e1915bd675055c7d /llvm/lib/Transforms
parent11ca2971e88f37dc1b40f00542cb6228e255fee4 (diff)
downloadbcm5719-llvm-22ccfc44845f29e850c52d4b3850f9ac8a82922c.tar.gz
bcm5719-llvm-22ccfc44845f29e850c52d4b3850f9ac8a82922c.zip
InstCombine: Combine gep X, (Y-X) to Y
We try to perform this transform in InstSimplify but we aren't always able to. Sometimes, we need to insert a bitcast if X and Y don't have the same time. llvm-svn: 216598
Diffstat (limited to 'llvm/lib/Transforms')
-rw-r--r--llvm/lib/Transforms/InstCombine/InstructionCombining.cpp39
1 files changed, 25 insertions, 14 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 46789711bc8..2f80e8389d9 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1516,28 +1516,39 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Type *Ty = PtrTy->getPointerElementType();
uint64_t TyAllocSize = DL->getTypeAllocSize(Ty);
- // Canonicalize (gep i8* X, -(ptrtoint Y)) to (sub (ptrtoint X), (ptrtoint Y))
- // The GEP pattern is emitted by the SCEV expander for certain kinds of
- // pointer arithmetic.
uint64_t C;
- Value *NegPtrToInt = nullptr;
+ Value *V = nullptr;
if (TyAllocSize == 1) {
- NegPtrToInt = GEP.getOperand(1);
+ V = GEP.getOperand(1);
} else if (match(GEP.getOperand(1),
- m_AShr(m_Value(NegPtrToInt), m_ConstantInt(C)))) {
+ m_AShr(m_Value(V), m_ConstantInt(C)))) {
if (TyAllocSize != 1ULL << C)
- NegPtrToInt = nullptr;
+ V = nullptr;
} else if (match(GEP.getOperand(1),
- m_SDiv(m_Value(NegPtrToInt), m_ConstantInt(C)))) {
+ m_SDiv(m_Value(V), m_ConstantInt(C)))) {
if (TyAllocSize != C)
- NegPtrToInt = nullptr;
+ V = nullptr;
}
- if (NegPtrToInt && match(NegPtrToInt, m_Neg(m_PtrToInt(m_Value())))) {
- Operator *Index = cast<Operator>(NegPtrToInt);
- Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->getType());
- Value *NewSub = Builder->CreateSub(PtrToInt, Index->getOperand(1));
- return CastInst::Create(Instruction::IntToPtr, NewSub, GEP.getType());
+ if (V) {
+ // Canonicalize (gep i8* X, -(ptrtoint Y))
+ // to (inttoptr (sub (ptrtoint X), (ptrtoint Y)))
+ // The GEP pattern is emitted by the SCEV expander for certain kinds of
+ // pointer arithmetic.
+ if (match(V, m_Neg(m_PtrToInt(m_Value())))) {
+ Operator *Index = cast<Operator>(V);
+ Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->getType());
+ Value *NewSub = Builder->CreateSub(PtrToInt, Index->getOperand(1));
+ return CastInst::Create(Instruction::IntToPtr, NewSub, GEP.getType());
+ }
+ // Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X))
+ // to (bitcast Y)
+ Value *Y;
+ if (match(V, m_Sub(m_PtrToInt(m_Value(Y)),
+ m_PtrToInt(m_Specific(GEP.getOperand(0)))))) {
+ return CastInst::CreatePointerBitCastOrAddrSpaceCast(Y,
+ GEP.getType());
+ }
}
}
}
OpenPOWER on IntegriCloud