1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[InstCombine] Restrict a GEP transform to avoid changing provenance

This is an alternative to D98120. Herein, instead of deleting the transformation entirely, we check
that the underlying objects are both the same and therefore this transformation wouldn't incur a
provenance change, if applied.

https://alive2.llvm.org/ce/z/SYF_yv

Reviewed By: lebedev.ri

Differential Revision: https://reviews.llvm.org/D98588
This commit is contained in:
Simonas Kazlauskas 2021-03-13 21:55:26 +02:00
parent 5bf1dc2e73
commit 1101d7f026
2 changed files with 14 additions and 9 deletions

View File

@ -2173,14 +2173,15 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
Matched = true;
}
if (Matched) {
// 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, GEPType);
}
// Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X)) to (bitcast Y), but
// only if both point to the same underlying object (otherwise provenance
// is not necessarily retained).
Value *Y;
Value *X = GEP.getOperand(0);
if (Matched &&
match(V, m_Sub(m_PtrToInt(m_Value(Y)), m_PtrToInt(m_Specific(X)))) &&
getUnderlyingObject(X) == getUnderlyingObject(Y))
return CastInst::CreatePointerBitCastOrAddrSpaceCast(Y, GEPType);
}
}

View File

@ -1087,7 +1087,11 @@ define %struct.C* @test44i(%struct.C* %c1, %struct.C* %c2) {
define %struct.C* @test45(%struct.C* %c1, %struct.C** %c2) {
; CHECK-LABEL: @test45(
; CHECK-NEXT: [[GEP:%.*]] = bitcast %struct.C** [[C2:%.*]] to %struct.C*
; 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: [[SHR:%.*]] = sdiv i64 [[SUB]], 7
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_C:%.*]], %struct.C* [[C1]], i64 [[SHR]]
; CHECK-NEXT: ret %struct.C* [[GEP]]
;
%ptrtoint1 = ptrtoint %struct.C* %c1 to i64