[llvm] r216598 - InstCombine: Combine gep X, (Y-X) to Y
David Majnemer
david.majnemer at gmail.com
Wed Aug 27 13:08:37 PDT 2014
Author: majnemer
Date: Wed Aug 27 15:08:37 2014
New Revision: 216598
URL: http://llvm.org/viewvc/llvm-project?rev=216598&view=rev
Log:
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.
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/trunk/test/Transforms/InstCombine/getelementptr.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp?rev=216598&r1=216597&r2=216598&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstructionCombining.cpp Wed Aug 27 15:08:37 2014
@@ -1516,28 +1516,39 @@ Instruction *InstCombiner::visitGetEleme
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());
+ }
}
}
}
Modified: llvm/trunk/test/Transforms/InstCombine/getelementptr.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/getelementptr.ll?rev=216598&r1=216597&r2=216598&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/getelementptr.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/getelementptr.ll Wed Aug 27 15:08:37 2014
@@ -858,6 +858,19 @@ define %struct.C* @test44(%struct.C* %c1
; CHECK-NEXT: ret %struct.C* [[INTTOPTR]]
}
+define %struct.C* @test45(%struct.C* %c1, %struct.C** %c2) {
+ %ptrtoint1 = ptrtoint %struct.C* %c1 to i64
+ %ptrtoint2 = ptrtoint %struct.C** %c2 to i64
+ %sub = sub i64 %ptrtoint2, %ptrtoint1 ; C2 - C1
+ %shr = sdiv i64 %sub, 7
+ %gep = getelementptr inbounds %struct.C* %c1, i64 %shr ; C1 + (C2 - C1)
+ ret %struct.C* %gep
+
+; CHECK-LABEL: @test45(
+; CHECK-NEXT: [[BITCAST:%.*]] = bitcast %struct.C** %c2 to %struct.C*
+; CHECK-NEXT: ret %struct.C* [[BITCAST]]
+}
+
define i32 addrspace(1)* @ascast_0_gep(i32* %p) nounwind {
; CHECK-LABEL: @ascast_0_gep(
; CHECK-NOT: getelementptr
More information about the llvm-commits
mailing list