]> granicus.if.org Git - llvm/commitdiff
[InstSimplify] Optimize away udivs in the presence of range metadata
authorDavid Majnemer <david.majnemer@gmail.com>
Fri, 6 Jan 2017 22:58:02 +0000 (22:58 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Fri, 6 Jan 2017 22:58:02 +0000 (22:58 +0000)
We know that udiv %V, C can be optimized away to 0 if %V is ult C.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291296 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/div.ll [new file with mode: 0644]

index 7ae7c4ef939a953942e42e6725abdb1f6c8925bd..8da2f0981d0ca1c83f1f1311aa21501dc07efeba 100644 (file)
@@ -1106,6 +1106,16 @@ static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const Query &Q,
   if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, Q, MaxRecurse))
     return V;
 
+  // udiv %V, C -> 0 if %V < C
+  if (MaxRecurse) {
+    if (Constant *C = dyn_cast_or_null<Constant>(SimplifyICmpInst(
+            ICmpInst::ICMP_ULT, Op0, Op1, Q, MaxRecurse - 1))) {
+      if (C->isAllOnesValue()) {
+        return Constant::getNullValue(Op0->getType());
+      }
+    }
+  }
+
   return nullptr;
 }
 
diff --git a/test/Transforms/InstSimplify/div.ll b/test/Transforms/InstSimplify/div.ll
new file mode 100644 (file)
index 0000000..b8ce34a
--- /dev/null
@@ -0,0 +1,15 @@
+; RUN: opt < %s -instsimplify -S | FileCheck %s
+
+declare i32 @external()
+
+define i32 @div1() {
+; CHECK-LABEL: @div1(
+; CHECK:         [[CALL:%.*]] = call i32 @external(), !range !0
+; CHECK-NEXT:    ret i32 0
+;
+  %call = call i32 @external(), !range !0
+  %urem = udiv i32 %call, 3
+  ret i32 %urem
+}
+
+!0 = !{i32 0, i32 3}