InstCombine: Canonicalize (gep i8* X, -(ptrtoint Y)) to (sub (ptrtoint X), (ptrtoint Y))

The GEP pattern is what SCEV expander emits for "ugly geps". The latter is what
you get for pointer subtraction in C code. The rest of instcombine already
knows how to deal with that so just canonicalize on that.

llvm-svn: 191090
This commit is contained in:
Benjamin Kramer 2013-09-20 14:38:44 +00:00
parent f56b5b52ff
commit e6461e3053
2 changed files with 28 additions and 0 deletions

View File

@ -1182,6 +1182,20 @@ Instruction *InstCombiner::visitGetElementPtrInst(GetElementPtrInst &GEP) {
GetElementPtrInst::Create(Src->getOperand(0), Indices, GEP.getName());
}
// 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.
if (TD && GEP.getNumIndices() == 1 &&
match(GEP.getOperand(1), m_Neg(m_PtrToInt(m_Value()))) &&
GEP.getType() == Builder->getInt8PtrTy() &&
GEP.getOperand(1)->getType()->getScalarSizeInBits() ==
TD->getPointerSizeInBits(GEP.getPointerAddressSpace())) {
Operator *Index = cast<Operator>(GEP.getOperand(1));
Value *PtrToInt = Builder->CreatePtrToInt(PtrOp, Index->getType());
Value *NewSub = Builder->CreateSub(PtrToInt, Index->getOperand(1));
return CastInst::Create(Instruction::IntToPtr, NewSub, GEP.getType());
}
// Handle gep(bitcast x) and gep(gep x, 0, 0, 0).
Value *StrippedPtr = PtrOp->stripPointerCasts();
PointerType *StrippedPtrTy = dyn_cast<PointerType>(StrippedPtr->getType());

View File

@ -762,4 +762,18 @@ define i8 @test_gep_bitcast_array_different_size_element_as1([100 x double] addr
ret i8 %x
}
define i64 @test40() {
%array = alloca [3 x i32], align 4
%gep = getelementptr inbounds [3 x i32]* %array, i64 0, i64 2
%gepi8 = bitcast i32* %gep to i8*
%p = ptrtoint [3 x i32]* %array to i64
%np = sub i64 0, %p
%gep2 = getelementptr i8* %gepi8, i64 %np
%ret = ptrtoint i8* %gep2 to i64
ret i64 %ret
; CHECK-LABEL: @test40
; CHECK-NEXT: ret i64 8
}
; CHECK: attributes [[NUW]] = { nounwind }