From af6b9939aac065ce19bdaaf9fc54a8368367f33f Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Thu, 17 Feb 2022 11:18:26 -0800 Subject: [PATCH] [EarlyCSE][OpaquePtr] Check access type when performing DSE This will bail out on target specific intrinsics. If those are deemed important enough for EarlyCSE to handle, we can augment MemIntrinsicInfo with an access type for TargetTransformInfo::getTgtMemIntrinsic() to handle. Reviewed By: #opaque-pointers, nikic Differential Revision: https://reviews.llvm.org/D120077 --- llvm/lib/Transforms/Scalar/EarlyCSE.cpp | 18 ++++++++++++++++++ llvm/test/Transforms/EarlyCSE/opaque-ptr.ll | 11 +++++++++++ 2 files changed, 29 insertions(+) diff --git a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp index 59b934c..091b7a5 100644 --- a/llvm/lib/Transforms/Scalar/EarlyCSE.cpp +++ b/llvm/lib/Transforms/Scalar/EarlyCSE.cpp @@ -781,6 +781,21 @@ private: return getLoadStorePointerOperand(Inst); } + Type *getValueType() const { + // TODO: handle target-specific intrinsics. + if (IntrinsicInst *II = dyn_cast(Inst)) { + switch (II->getIntrinsicID()) { + case Intrinsic::masked_load: + return II->getType(); + case Intrinsic::masked_store: + return II->getArgOperand(0)->getType(); + default: + return nullptr; + } + } + return getLoadStoreType(Inst); + } + bool mayReadFromMemory() const { if (IntrID != 0) return Info.ReadMem; @@ -1162,6 +1177,9 @@ bool EarlyCSE::overridingStores(const ParseMemoryInst &Earlier, "Violated invariant"); if (Earlier.getPointerOperand() != Later.getPointerOperand()) return false; + if (!Earlier.getValueType() || !Later.getValueType() || + Earlier.getValueType() != Later.getValueType()) + return false; if (Earlier.getMatchingId() != Later.getMatchingId()) return false; // At the moment, we don't remove ordered stores, but do remove diff --git a/llvm/test/Transforms/EarlyCSE/opaque-ptr.ll b/llvm/test/Transforms/EarlyCSE/opaque-ptr.ll index bc278fe..b05fc80 100644 --- a/llvm/test/Transforms/EarlyCSE/opaque-ptr.ll +++ b/llvm/test/Transforms/EarlyCSE/opaque-ptr.ll @@ -30,3 +30,14 @@ define i32 @different_types_store(ptr %p, i32 %a) { %sub = sub i32 %a, %v2.c ret i32 %sub } + +define void @dse(ptr %p, i32 %i1, i8 %i2) { +; CHECK-LABEL: @dse( +; CHECK-NEXT: store i32 [[I1:%.*]], ptr [[P:%.*]], align 4 +; CHECK-NEXT: store i8 [[I2:%.*]], ptr [[P]], align 1 +; CHECK-NEXT: ret void +; + store i32 %i1, ptr %p + store i8 %i2, ptr %p + ret void +} -- 2.7.4