From 6935aefdf0fdfd2641ebb27dd3b1aa6e5e2fe4ab Mon Sep 17 00:00:00 2001 From: George Burgess IV Date: Mon, 1 May 2017 23:12:08 +0000 Subject: [PATCH] [InstSimplify] Handle selects of GEPs with 0 offset In particular (since it wouldn't fit nicely in the summary): (select (icmp eq V 0) P (getelementptr P V)) -> (getelementptr P V) Differential Revision: https://reviews.llvm.org/D31435 llvm-svn: 301880 --- llvm/lib/Analysis/InstructionSimplify.cpp | 13 +++++++++++++ llvm/test/Transforms/InstSimplify/select.ll | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 2f25a11..3137154 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -62,6 +62,8 @@ static Value *SimplifyOrInst(Value *, Value *, const SimplifyQuery &, unsigned); static Value *SimplifyXorInst(Value *, Value *, const SimplifyQuery &, unsigned); static Value *SimplifyCastInst(unsigned, Value *, Type *, const SimplifyQuery &, unsigned); +static Value *SimplifyGEPInst(Type *, ArrayRef, const SimplifyQuery &, + unsigned); /// For a boolean type or a vector of boolean type, return false or a vector /// with every element false. @@ -3491,6 +3493,17 @@ static const Value *SimplifyWithOpReplaced(Value *V, Value *Op, Value *RepOp, } } + // Same for GEPs. + if (auto *GEP = dyn_cast(I)) { + if (MaxRecurse) { + SmallVector 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 diff --git a/llvm/test/Transforms/InstSimplify/select.ll b/llvm/test/Transforms/InstSimplify/select.ll index cb2502cf..3e1af8c 100644 --- a/llvm/test/Transforms/InstSimplify/select.ll +++ b/llvm/test/Transforms/InstSimplify/select.ll @@ -431,3 +431,22 @@ define i8 @do_not_assume_sel_cond(i1 %cond, i8 %x, i8 %y) { 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 +} -- 2.7.4