[llvm] r259421 - [InstCombine] simplify masked scatter/gather intrinsics with zero masks
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 1 14:10:27 PST 2016
Author: spatel
Date: Mon Feb 1 16:10:26 2016
New Revision: 259421
URL: http://llvm.org/viewvc/llvm-project?rev=259421&view=rev
Log:
[InstCombine] simplify masked scatter/gather intrinsics with zero masks
A masked scatter with a zero mask means there's no store.
A masked gather with a zero mask means the passthru arg is returned.
This is a continuation of:
http://reviews.llvm.org/rL259369
http://reviews.llvm.org/rL259392
Modified:
llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/trunk/test/Transforms/InstCombine/masked_intrinsics.ll
Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp?rev=259421&r1=259420&r2=259421&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCalls.cpp Mon Feb 1 16:10:26 2016
@@ -792,6 +792,24 @@ static Instruction *simplifyMaskedStore(
return nullptr;
}
+static Instruction *simplifyMaskedGather(IntrinsicInst &II, InstCombiner &IC) {
+ // If the mask is all zeros, return the "passthru" argument of the gather.
+ auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2));
+ if (ConstMask && ConstMask->isNullValue())
+ return IC.ReplaceInstUsesWith(II, II.getArgOperand(3));
+
+ return nullptr;
+}
+
+static Instruction *simplifyMaskedScatter(IntrinsicInst &II, InstCombiner &IC) {
+ // If the mask is all zeros, a scatter does nothing.
+ auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(3));
+ if (ConstMask && ConstMask->isNullValue())
+ return IC.EraseInstFromFunction(II);
+
+ return nullptr;
+}
+
/// CallInst simplification. This mostly only handles folding of intrinsic
/// instructions. For normal calls, it allows visitCallSite to do the heavy
/// lifting.
@@ -922,10 +940,10 @@ Instruction *InstCombiner::visitCallInst
break;
case Intrinsic::masked_store:
return simplifyMaskedStore(*II, *this);
-
- // TODO: Handle the other masked ops.
- // case Intrinsic::masked_gather:
- // case Intrinsic::masked_scatter:
+ case Intrinsic::masked_gather:
+ return simplifyMaskedGather(*II, *this);
+ case Intrinsic::masked_scatter:
+ return simplifyMaskedScatter(*II, *this);
case Intrinsic::powi:
if (ConstantInt *Power = dyn_cast<ConstantInt>(II->getArgOperand(1))) {
Modified: llvm/trunk/test/Transforms/InstCombine/masked_intrinsics.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/masked_intrinsics.ll?rev=259421&r1=259420&r2=259421&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/masked_intrinsics.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/masked_intrinsics.ll Mon Feb 1 16:10:26 2016
@@ -2,7 +2,8 @@
declare <2 x double> @llvm.masked.load.v2f64(<2 x double>* %ptrs, i32, <2 x i1> %mask, <2 x double> %src0)
declare void @llvm.masked.store.v2f64(<2 x double> %val, <2 x double>* %ptrs, i32, <2 x i1> %mask)
-
+declare <2 x double> @llvm.masked.gather.v2f64(<2 x double*> %ptrs, i32, <2 x i1> %mask, <2 x double> %passthru)
+declare void @llvm.masked.scatter.v2f64(<2 x double> %val, <2 x double*> %ptrs, i32, <2 x i1> %mask)
define <2 x double> @load_zeromask(<2 x double>* %ptr, <2 x double> %passthru) {
%res = call <2 x double> @llvm.masked.load.v2f64(<2 x double>* %ptr, i32 1, <2 x i1> zeroinitializer, <2 x double> %passthru)
@@ -26,7 +27,7 @@ define void @store_zeromask(<2 x double>
ret void
; CHECK-LABEL: @store_zeromask(
-; CHECK-NEXT: ret void
+; CHECK-NEXT: ret void
}
define void @store_onemask(<2 x double>* %ptr, <2 x double> %val) {
@@ -35,6 +36,22 @@ define void @store_onemask(<2 x double>*
; CHECK-LABEL: @store_onemask(
; CHECK-NEXT: store <2 x double> %val, <2 x double>* %ptr, align 4
-; CHECK-NEXT: ret void
+; CHECK-NEXT: ret void
+}
+
+define <2 x double> @gather_zeromask(<2 x double*> %ptrs, <2 x double> %passthru) {
+ %res = call <2 x double> @llvm.masked.gather.v2f64(<2 x double*> %ptrs, i32 5, <2 x i1> zeroinitializer, <2 x double> %passthru)
+ ret <2 x double> %res
+
+; CHECK-LABEL: @gather_zeromask(
+; CHECK-NEXT: ret <2 x double> %passthru
+}
+
+define void @scatter_zeromask(<2 x double*> %ptrs, <2 x double> %val) {
+ call void @llvm.masked.scatter.v2f64(<2 x double> %val, <2 x double*> %ptrs, i32 6, <2 x i1> zeroinitializer)
+ ret void
+
+; CHECK-LABEL: @scatter_zeromask(
+; CHECK-NEXT: ret void
}
More information about the llvm-commits
mailing list