mirror of
https://github.com/RPCS3/llvm-mirror.git
synced 2025-01-31 20:51:52 +01:00
InstCombine: Optimize GEP's involving ptrtoint better
We supported transforming: (gep i8* X, -(ptrtoint Y)) to: (inttoptr (sub (ptrtoint X), (ptrtoint Y))) However, this only fired if 'X' had type i8*. Generalize this to support various types of different sizes. This results in much better CodeGen, especially for pointers to packed structs. llvm-svn: 216523
This commit is contained in:
parent
4442146a4e
commit
826f5eb297
@ -1508,19 +1508,37 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
|
|||||||
GetElementPtrInst::Create(Src->getOperand(0), Indices, GEP.getName());
|
GetElementPtrInst::Create(Src->getOperand(0), Indices, GEP.getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Canonicalize (gep i8* X, -(ptrtoint Y)) to (sub (ptrtoint X), (ptrtoint Y))
|
if (DL && GEP.getNumIndices() == 1) {
|
||||||
// The GEP pattern is emitted by the SCEV expander for certain kinds of
|
|
||||||
// pointer arithmetic.
|
|
||||||
if (DL && GEP.getNumIndices() == 1 &&
|
|
||||||
match(GEP.getOperand(1), m_Neg(m_PtrToInt(m_Value())))) {
|
|
||||||
unsigned AS = GEP.getPointerAddressSpace();
|
unsigned AS = GEP.getPointerAddressSpace();
|
||||||
if (GEP.getType() == Builder->getInt8PtrTy(AS) &&
|
if (GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
|
||||||
GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
|
|
||||||
DL->getPointerSizeInBits(AS)) {
|
DL->getPointerSizeInBits(AS)) {
|
||||||
Operator *Index = cast<Operator>(GEP.getOperand(1));
|
Type *PtrTy = GEP.getPointerOperandType();
|
||||||
Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->getType());
|
Type *Ty = PtrTy->getPointerElementType();
|
||||||
Value *NewSub = Builder->CreateSub(PtrToInt, Index->getOperand(1));
|
uint64_t TyAllocSize = DL->getTypeAllocSize(Ty);
|
||||||
return CastInst::Create(Instruction::IntToPtr, NewSub, GEP.getType());
|
|
||||||
|
// 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;
|
||||||
|
if (TyAllocSize == 1) {
|
||||||
|
NegPtrToInt = GEP.getOperand(1);
|
||||||
|
} else if (match(GEP.getOperand(1),
|
||||||
|
m_AShr(m_Value(NegPtrToInt), m_ConstantInt(C)))) {
|
||||||
|
if (TyAllocSize != 1ULL << C)
|
||||||
|
NegPtrToInt = nullptr;
|
||||||
|
} else if (match(GEP.getOperand(1),
|
||||||
|
m_SDiv(m_Value(NegPtrToInt), m_ConstantInt(C)))) {
|
||||||
|
if (TyAllocSize != C)
|
||||||
|
NegPtrToInt = 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,7 @@ target datalayout = "e-p:64:64-p1:16:16-p2:32:32:32-p3:64:64:64"
|
|||||||
%pair = type { i32, i32 }
|
%pair = type { i32, i32 }
|
||||||
%struct.B = type { double }
|
%struct.B = type { double }
|
||||||
%struct.A = type { %struct.B, i32, i32 }
|
%struct.A = type { %struct.B, i32, i32 }
|
||||||
|
%struct.C = type { [7 x i8] }
|
||||||
|
|
||||||
|
|
||||||
@Global = constant [10 x i8] c"helloworld"
|
@Global = constant [10 x i8] c"helloworld"
|
||||||
@ -813,6 +814,50 @@ define i16 @test41([3 x i32] addrspace(1)* %array) {
|
|||||||
; CHECK-NEXT: ret i16 8
|
; CHECK-NEXT: ret i16 8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
define i8* @test42(i8* %c1, i8* %c2) {
|
||||||
|
%ptrtoint = ptrtoint i8* %c1 to i64
|
||||||
|
%sub = sub i64 0, %ptrtoint
|
||||||
|
%gep = getelementptr inbounds i8* %c2, i64 %sub
|
||||||
|
ret i8* %gep
|
||||||
|
|
||||||
|
; CHECK-LABEL: @test42(
|
||||||
|
; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint i8* %c1 to i64
|
||||||
|
; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint i8* %c2 to i64
|
||||||
|
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
|
||||||
|
; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i8*
|
||||||
|
; CHECK-NEXT: ret i8* [[INTTOPTR]]
|
||||||
|
}
|
||||||
|
|
||||||
|
define i16* @test43(i16* %c1, i16* %c2) {
|
||||||
|
%ptrtoint = ptrtoint i16* %c1 to i64
|
||||||
|
%sub = sub i64 0, %ptrtoint
|
||||||
|
%shr = ashr i64 %sub, 1
|
||||||
|
%gep = getelementptr inbounds i16* %c2, i64 %shr
|
||||||
|
ret i16* %gep
|
||||||
|
|
||||||
|
; CHECK-LABEL: @test43(
|
||||||
|
; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint i16* %c1 to i64
|
||||||
|
; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint i16* %c2 to i64
|
||||||
|
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
|
||||||
|
; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to i16*
|
||||||
|
; CHECK-NEXT: ret i16* [[INTTOPTR]]
|
||||||
|
}
|
||||||
|
|
||||||
|
define %struct.C* @test44(%struct.C* %c1, %struct.C* %c2) {
|
||||||
|
%ptrtoint = ptrtoint %struct.C* %c1 to i64
|
||||||
|
%sub = sub i64 0, %ptrtoint
|
||||||
|
%shr = sdiv i64 %sub, 7
|
||||||
|
%gep = getelementptr inbounds %struct.C* %c2, i64 %shr
|
||||||
|
ret %struct.C* %gep
|
||||||
|
|
||||||
|
; CHECK-LABEL: @test44(
|
||||||
|
; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint %struct.C* %c1 to i64
|
||||||
|
; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint %struct.C* %c2 to i64
|
||||||
|
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
|
||||||
|
; CHECK-NEXT: [[INTTOPTR:%.*]] = inttoptr i64 [[SUB]] to %struct.C*
|
||||||
|
; CHECK-NEXT: ret %struct.C* [[INTTOPTR]]
|
||||||
|
}
|
||||||
|
|
||||||
define i32 addrspace(1)* @ascast_0_gep(i32* %p) nounwind {
|
define i32 addrspace(1)* @ascast_0_gep(i32* %p) nounwind {
|
||||||
; CHECK-LABEL: @ascast_0_gep(
|
; CHECK-LABEL: @ascast_0_gep(
|
||||||
; CHECK-NOT: getelementptr
|
; CHECK-NOT: getelementptr
|
||||||
|
Loading…
x
Reference in New Issue
Block a user