Skip to content

Commit 7d7001b

Browse files
committed
[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
1 parent d57d8f3 commit 7d7001b

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,14 +2173,15 @@ Instruction *InstCombinerImpl::visitGetElementPtrInst(GetElementPtrInst &GEP) {
21732173
Matched = true;
21742174
}
21752175

2176-
if (Matched) {
2177-
// Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X))
2178-
// to (bitcast Y)
2179-
Value *Y;
2180-
if (match(V, m_Sub(m_PtrToInt(m_Value(Y)),
2181-
m_PtrToInt(m_Specific(GEP.getOperand(0))))))
2182-
return CastInst::CreatePointerBitCastOrAddrSpaceCast(Y, GEPType);
2183-
}
2176+
// Canonicalize (gep i8* X, (ptrtoint Y)-(ptrtoint X)) to (bitcast Y), but
2177+
// only if both point to the same underlying object (otherwise provenance
2178+
// is not necessarily retained).
2179+
Value *Y;
2180+
Value *X = GEP.getOperand(0);
2181+
if (Matched &&
2182+
match(V, m_Sub(m_PtrToInt(m_Value(Y)), m_PtrToInt(m_Specific(X)))) &&
2183+
getUnderlyingObject(X) == getUnderlyingObject(Y))
2184+
return CastInst::CreatePointerBitCastOrAddrSpaceCast(Y, GEPType);
21842185
}
21852186
}
21862187

llvm/test/Transforms/InstCombine/getelementptr.ll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,7 +1087,11 @@ define %struct.C* @test44i(%struct.C* %c1, %struct.C* %c2) {
10871087

10881088
define %struct.C* @test45(%struct.C* %c1, %struct.C** %c2) {
10891089
; CHECK-LABEL: @test45(
1090-
; CHECK-NEXT: [[GEP:%.*]] = bitcast %struct.C** [[C2:%.*]] to %struct.C*
1090+
; CHECK-NEXT: [[PTRTOINT1:%.*]] = ptrtoint %struct.C* [[C1:%.*]] to i64
1091+
; CHECK-NEXT: [[PTRTOINT2:%.*]] = ptrtoint %struct.C** [[C2:%.*]] to i64
1092+
; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[PTRTOINT2]], [[PTRTOINT1]]
1093+
; CHECK-NEXT: [[SHR:%.*]] = sdiv i64 [[SUB]], 7
1094+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr inbounds [[STRUCT_C:%.*]], %struct.C* [[C1]], i64 [[SHR]]
10911095
; CHECK-NEXT: ret %struct.C* [[GEP]]
10921096
;
10931097
%ptrtoint1 = ptrtoint %struct.C* %c1 to i64

0 commit comments

Comments
 (0)