mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-13 20:32:21 +00:00
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@191090 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
8607525110
commit
4c332fa5a5
@ -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());
|
||||
|
@ -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 }
|
||||
|
Loading…
Reference in New Issue
Block a user