[PATCH] D31435: [InstSimplify] Fold (select (icmp eq V 0) P (getelementptr P V)) -> (getelementptr P V)

George Burgess IV via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 28 13:31:24 PDT 2017


george.burgess.iv created this revision.

I'm unsure if we want to do all of the GEP simplifications here; I'm happy to factor some of the `Ops.size() == 2` checks out and just do those if that's preferable.


https://reviews.llvm.org/D31435

Files:
  lib/Analysis/InstructionSimplify.cpp
  test/Transforms/InstSimplify/select.ll


Index: test/Transforms/InstSimplify/select.ll
===================================================================
--- test/Transforms/InstSimplify/select.ll
+++ test/Transforms/InstSimplify/select.ll
@@ -431,3 +431,22 @@
   ret i8 %sel
 }
 
+define i32* @select_icmp_eq_0_gep_operand(i32* %base, i64 %n) {
+; CHECK-LABEL: @select_icmp_eq_0_gep_operand(
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr
+; CHECK-NEXT: ret i32* [[GEP]]
+  %cond = icmp eq i64 %n, 0
+  %gep = getelementptr i32, i32* %base, i64 %n
+  %r = select i1 %cond, i32* %base, i32* %gep
+  ret i32* %r
+}
+
+define i32* @select_icmp_ne_0_gep_operand(i32* %base, i64 %n) {
+; CHECK-LABEL: @select_icmp_ne_0_gep_operand(
+; CHECK-NEXT: [[GEP:%.*]] = getelementptr
+; CHECK-NEXT: ret i32* [[GEP]]
+  %cond = icmp ne i64 %n, 0
+  %gep = getelementptr i32, i32* %base, i64 %n
+  %r = select i1 %cond, i32* %gep, i32* %base
+  ret i32* %r
+}
Index: lib/Analysis/InstructionSimplify.cpp
===================================================================
--- lib/Analysis/InstructionSimplify.cpp
+++ lib/Analysis/InstructionSimplify.cpp
@@ -74,6 +74,8 @@
 static Value *SimplifyXorInst(Value *, Value *, const Query &, unsigned);
 static Value *SimplifyCastInst(unsigned, Value *, Type *,
                                const Query &, unsigned);
+static Value *SimplifyGEPInst(Type *, ArrayRef<Value *>, const Query &,
+                              unsigned);
 
 /// For a boolean type, or a vector of boolean type, return false, or
 /// a vector with every element false, as appropriate for the type.
@@ -3586,6 +3588,17 @@
     }
   }
 
+  // Same for GEPs.
+  if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
+    if (MaxRecurse) {
+      SmallVector<Value *, 8> NewOps(GEP->getNumOperands());
+      transform(GEP->operands(), NewOps.begin(),
+                [&](Value *V) { return V == Op ? RepOp : V; });
+      return SimplifyGEPInst(GEP->getSourceElementType(), NewOps, Q,
+                             MaxRecurse - 1);
+    }
+  }
+
   // TODO: We could hand off more cases to instsimplify here.
 
   // If all operands are constant after substituting Op for RepOp then we can


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31435.93286.patch
Type: text/x-patch
Size: 2148 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170328/44f30179/attachment.bin>


More information about the llvm-commits mailing list