[PATCH] D57468: Strengthen handling of GEPs and generic calls for all undef lanes

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 30 12:57:47 PST 2019


reames created this revision.
reames added reviewers: RKSimon, craig.topper, spatel.
Herald added subscribers: bollu, mcrosier.

If all lanes in a vector can be proven undef, then the result is undef.  This patch implements and tests the GEP case.  Once the masked.load patch (D57372 <https://reviews.llvm.org/D57372>) has landed, I'll update to include call tests as well.


https://reviews.llvm.org/D57468

Files:
  lib/Transforms/InstCombine/InstCombineCalls.cpp
  lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
  lib/Transforms/InstCombine/InstructionCombining.cpp
  test/Transforms/InstCombine/vec_demanded_elts.ll


Index: test/Transforms/InstCombine/vec_demanded_elts.ll
===================================================================
--- test/Transforms/InstCombine/vec_demanded_elts.ll
+++ test/Transforms/InstCombine/vec_demanded_elts.ll
@@ -620,10 +620,7 @@
 
 define <2 x i32*> @gep_all_lanes_undef(i32* %base, i64 %idx) {;
 ; CHECK-LABEL: @gep_all_lanes_undef(
-; CHECK-NEXT:    [[BASEVEC:%.*]] = insertelement <2 x i32*> undef, i32* [[BASE:%.*]], i32 0
-; CHECK-NEXT:    [[IDXVEC:%.*]] = insertelement <2 x i64> undef, i64 [[IDX:%.*]], i32 1
-; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i32, <2 x i32*> [[BASEVEC]], <2 x i64> [[IDXVEC]]
-; CHECK-NEXT:    ret <2 x i32*> [[GEP]]
+; CHECK-NEXT:    ret <2 x i32*> undef
 ;
   %basevec = insertelement <2 x i32*> undef, i32* %base, i32 0
   %idxvec = insertelement <2 x i64> undef, i64 %idx, i32 1
@@ -641,4 +638,3 @@
   %ee = extractelement <2 x i32*> %gep, i32 1
   ret i32* %ee
 }
-
Index: lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- lib/Transforms/InstCombine/InstructionCombining.cpp
+++ lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -1556,6 +1556,23 @@
   if (Value *V = SimplifyGEPInst(GEPEltType, Ops, SQ.getWithInstruction(&GEP)))
     return replaceInstUsesWith(GEP, V);
 
+  // For vector geps, use the generic demanded vector support to simplify any
+  // operands or prove that all lanes must produce undef.
+  if (GEP.getType()->isVectorTy()) {
+    auto VWidth = GEP.getType()->getVectorNumElements();
+    APInt UndefElts(VWidth, 0);
+    APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
+    if (Value *V = SimplifyDemandedVectorElts(&GEP, AllOnesEltMask,
+                                              UndefElts)) {
+      if (V != &GEP)
+        return replaceInstUsesWith(GEP, V);
+      return &GEP;
+    }
+
+    if (UndefElts.isAllOnesValue())
+      return replaceInstUsesWith(GEP, UndefValue::get(GEP.getType()));
+  }
+
   Value *PtrOp = GEP.getOperand(0);
 
   // Eliminate unneeded casts for indices, and replace indices which displace
Index: lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
+++ lib/Transforms/InstCombine/InstCombineSimplifyDemanded.cpp
@@ -1189,9 +1189,18 @@
     // wouldn't have a vector result to get here. Note that we intentionally
     // merge the undef bits here since gepping with either an undef base or
     // index results in undef. 
-    for (unsigned i = 0; i < I->getNumOperands(); i++)
-      if (I->getOperand(i)->getType()->isVectorTy())
-        simplifyAndSetOp(I, i, DemandedElts, UndefElts);
+    for (unsigned i = 0; i < I->getNumOperands(); i++) {
+      if (isa<UndefValue>(I->getOperand(i))) {
+        // If the entire vector is undefined, just return this info.
+        UndefElts = EltMask;
+        return nullptr;
+      }
+      if (I->getOperand(i)->getType()->isVectorTy()) {
+        APInt UndefEltsOp(VWidth, 0);
+        simplifyAndSetOp(I, i, DemandedElts, UndefEltsOp);
+        UndefElts |= UndefEltsOp;
+      }
+    }
 
     break;
   }
Index: lib/Transforms/InstCombine/InstCombineCalls.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -1869,7 +1869,8 @@
   }
 
   // For vector result intrinsics, use the generic demanded vector support to
-  // simplify any operands before moving on to the per-intrinsic rules.    
+  // simplify any operands or prove that all lanes must produce undef, before
+  // moving on to the per-intrinsic rules.     
   if (II->getType()->isVectorTy()) {
     auto VWidth = II->getType()->getVectorNumElements();
     APInt UndefElts(VWidth, 0);
@@ -1879,6 +1880,9 @@
         return replaceInstUsesWith(*II, V);
       return II;
     }
+
+    if (UndefElts.isAllOnesValue())
+      return replaceInstUsesWith(*II, UndefValue::get(II->getType()));
   }
 
   if (Instruction *I = SimplifyNVVMIntrinsic(II, *this))


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D57468.184355.patch
Type: text/x-patch
Size: 4142 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190130/d7853365/attachment.bin>


More information about the llvm-commits mailing list