[InstCombine] Masked loads with undef masks can fold to normal loads
authorDavid Majnemer <david.majnemer@gmail.com>
Thu, 14 Jul 2016 06:58:42 +0000 (06:58 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Thu, 14 Jul 2016 06:58:42 +0000 (06:58 +0000)
We were able to fold masked loads with an all-ones mask to a normal
load.  However, we couldn't turn a masked load with a mask with mixed
ones and undefs into a normal load.

llvm-svn: 275380

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/test/Transforms/InstCombine/masked_intrinsics.ll

index 36c9762..8acff91 100644 (file)
@@ -1038,14 +1038,27 @@ static Value *simplifyMinnumMaxnum(const IntrinsicInst &II) {
   return nullptr;
 }
 
-static Value *simplifyMaskedLoad(const IntrinsicInst &II,
-                                 InstCombiner::BuilderTy &Builder) {
-  auto *ConstMask = dyn_cast<Constant>(II.getArgOperand(2));
+static bool maskIsAllOneOrUndef(Value *Mask) {
+  auto *ConstMask = dyn_cast<Constant>(Mask);
   if (!ConstMask)
-    return nullptr;
+    return false;
+  if (ConstMask->isAllOnesValue() || isa<UndefValue>(ConstMask))
+    return true;
+  for (unsigned I = 0, E = ConstMask->getType()->getVectorNumElements(); I != E;
+       ++I) {
+    if (auto *MaskElt = ConstMask->getAggregateElement(I))
+      if (MaskElt->isAllOnesValue() || isa<UndefValue>(MaskElt))
+        continue;
+    return false;
+  }
+  return true;
+}
 
-  // If the mask is all ones, this is a plain vector load of the 1st argument.
-  if (ConstMask->isAllOnesValue()) {
+static Value *simplifyMaskedLoad(const IntrinsicInst &II,
+                                 InstCombiner::BuilderTy &Builder) {
+  // If the mask is all ones or undefs, this is a plain vector load of the 1st
+  // argument.
+  if (maskIsAllOneOrUndef(II.getArgOperand(2))) {
     Value *LoadPtr = II.getArgOperand(0);
     unsigned Alignment = cast<ConstantInt>(II.getArgOperand(1))->getZExtValue();
     return Builder.CreateAlignedLoad(LoadPtr, Alignment, "unmaskedload");
index 0b15f7a..ce79ce5 100644 (file)
@@ -22,6 +22,15 @@ define <2 x double> @load_onemask(<2 x double>* %ptr, <2 x double> %passthru)  {
 ; CHECK-NEXT:  ret <2 x double> %unmaskedload
 }
 
+define <2 x double> @load_undefmask(<2 x double>* %ptr, <2 x double> %passthru)  {
+  %res = call <2 x double> @llvm.masked.load.v2f64.p0v2f64(<2 x double>* %ptr, i32 2, <2 x i1> <i1 1, i1 undef>, <2 x double> %passthru)
+  ret <2 x double> %res
+
+; CHECK-LABEL: @load_undefmask(
+; CHECK-NEXT:  %unmaskedload = load <2 x double>, <2 x double>* %ptr, align 2
+; CHECK-NEXT:  ret <2 x double> %unmaskedload
+}
+
 define void @store_zeromask(<2 x double>* %ptr, <2 x double> %val)  {
   call void @llvm.masked.store.v2f64.p0v2f64(<2 x double> %val, <2 x double>* %ptr, i32 3, <2 x i1> zeroinitializer)
   ret void