[llvm] r292531 - [SCCP] Teach the pass how to handle `div` with overdefined operands.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 19 15:07:52 PST 2017


Author: davide
Date: Thu Jan 19 17:07:51 2017
New Revision: 292531

URL: http://llvm.org/viewvc/llvm-project?rev=292531&view=rev
Log:
[SCCP] Teach the pass how to handle `div` with overdefined operands.

This can prove that:

extern int f;
int g() {
    int x = 0;
    for (int i = 0; i < 365; ++i) {
        x /= f;
    }
    return x;
}

always returns zero. Thanks to Sanjoy for confirming this
transformation actually made sense (bugs are mine).

Added:
    llvm/trunk/test/Transforms/SCCP/overdefined-div.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/SCCP.cpp

Modified: llvm/trunk/lib/Transforms/Scalar/SCCP.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SCCP.cpp?rev=292531&r1=292530&r2=292531&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SCCP.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SCCP.cpp Thu Jan 19 17:07:51 2017
@@ -910,6 +910,12 @@ void SCCPSolver::visitBinaryOperator(Ins
 
   // Otherwise, one of our operands is overdefined.  Try to produce something
   // better than overdefined with some tricks.
+  // If this is 0 / Y, it doesn't matter that the second operand is
+  // overdefined, and we can replace it with zero.
+  if (I.getOpcode() == Instruction::UDiv || I.getOpcode() == Instruction::SDiv)
+    if (V1State.isConstant() && V1State.getConstant()->isNullValue())
+      return markConstant(IV, &I, V1State.getConstant());
+
   // If this is:
   // -> AND/MUL with 0
   // -> OR with -1

Added: llvm/trunk/test/Transforms/SCCP/overdefined-div.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SCCP/overdefined-div.ll?rev=292531&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/SCCP/overdefined-div.ll (added)
+++ llvm/trunk/test/Transforms/SCCP/overdefined-div.ll Thu Jan 19 17:07:51 2017
@@ -0,0 +1,32 @@
+; RUN: opt < %s -sccp -S | FileCheck %s
+
+; Test that SCCP has basic knowledge of when div can nuke overdefined values.
+
+; 0 / X = 0 even if X is overdefined.
+; CHECK-LABEL: test1
+; CHECK-NEXT: ret i32 0
+define i32 @test1(i32 %foo) {
+  %tinkywinky = udiv i32 0, %foo
+  ret i32 %tinkywinky
+}
+
+; CHECK-LABEL: test2
+; CHECK-NEXT: ret i32 0
+define i32 @test2(i32 %foo) {
+  %tinkywinky = sdiv i32 0, %foo
+  ret i32 %tinkywinky
+}
+
+; CHECK-LABEL: test3
+; CHECK: ret i32 %tinkywinky
+define i32 @test3(i32 %foo) {
+  %tinkywinky = udiv i32 %foo, 0
+  ret i32 %tinkywinky
+}
+
+; CHECK-LABEL: test4
+; CHECK: ret i32 %tinkywinky
+define i32 @test4(i32 %foo) {
+  %tinkywinky = sdiv i32 %foo, 0
+  ret i32 %tinkywinky
+}




More information about the llvm-commits mailing list