[llvm] 7d7001b - [InstCombine] Restrict a GEP transform to avoid changing provenance
Simonas Kazlauskas via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 14 07:32:09 PDT 2021
Author: Simonas Kazlauskas
Date: 2021-03-14T16:32:04+02:00
New Revision: 7d7001b2cbd05bb1955c18e7f8668644bd1258dc
URL: https://github.com/llvm/llvm-project/commit/7d7001b2cbd05bb1955c18e7f8668644bd1258dc
DIFF: https://github.com/llvm/llvm-project/commit/7d7001b2cbd05bb1955c18e7f8668644bd1258dc.diff
LOG: [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
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/getelementptr.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 6bc12fa3b5f1..65becd7904af 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -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);
}
}
diff --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index 6fc9567da3b2..8a0035c8135f 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -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
More information about the llvm-commits
mailing list