[llvm] r289175 - [SCCP] Teach the pass about `mul %x 0` even if %x is overdefined.

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 8 19:08:43 PST 2016


Author: davide
Date: Thu Dec  8 21:08:42 2016
New Revision: 289175

URL: http://llvm.org/viewvc/llvm-project?rev=289175&view=rev
Log:
[SCCP] Teach the pass about `mul %x 0` even if %x is overdefined.

The motivating example is:

extern int patatino;
int goo() {
    int x = 0;
    for (int i = 0; i < 1000000; ++i) {
        x *= patatino;
    }
    return x;
}

Currently SCCP will not realize that this function returns always zero,
therefore will try to unroll and vectorize the loop at -O3 producing an
awful lot of (useless) code. With this change, it will just produce:

0000000000000000 <g>:
   xor    %eax,%eax
   retq

Modified:
    llvm/trunk/lib/Transforms/Scalar/SCCP.cpp
    llvm/trunk/test/Transforms/SCCP/logical-nuke.ll

Modified: llvm/trunk/lib/Transforms/Scalar/SCCP.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/SCCP.cpp?rev=289175&r1=289174&r2=289175&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/SCCP.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/SCCP.cpp Thu Dec  8 21:08:42 2016
@@ -916,7 +916,8 @@ void SCCPSolver::visitBinaryOperator(Ins
 
   // If this is an AND or OR with 0 or -1, it doesn't matter that the other
   // operand is overdefined.
-  if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Or) {
+  if (I.getOpcode() == Instruction::And || I.getOpcode() == Instruction::Mul ||
+      I.getOpcode() == Instruction::Or) {
     LatticeVal *NonOverdefVal = nullptr;
     if (!V1State.isOverdefined())
       NonOverdefVal = &V1State;
@@ -927,8 +928,10 @@ void SCCPSolver::visitBinaryOperator(Ins
       if (NonOverdefVal->isUnknown())
         return;
 
-      if (I.getOpcode() == Instruction::And) {
+      if (I.getOpcode() == Instruction::And ||
+          I.getOpcode() == Instruction::Mul) {
         // X and 0 = 0
+        // X * 0 = 0
         if (NonOverdefVal->getConstant()->isNullValue())
           return markConstant(IV, &I, NonOverdefVal->getConstant());
       } else {

Modified: llvm/trunk/test/Transforms/SCCP/logical-nuke.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/SCCP/logical-nuke.ll?rev=289175&r1=289174&r2=289175&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/SCCP/logical-nuke.ll (original)
+++ llvm/trunk/test/Transforms/SCCP/logical-nuke.ll Thu Dec  8 21:08:42 2016
@@ -1,6 +1,6 @@
 ; RUN: opt < %s -sccp -S | FileCheck %s
 
-; Test that SCCP has basic knowledge of when and/or nuke overdefined values.
+; Test that SCCP has basic knowledge of when and/or/mul nuke overdefined values.
 
 ; CHECK-LABEL: test
 ; CHECK: ret i32 0
@@ -29,3 +29,10 @@ define i32 @test4(i32 %X) {
   %Y = or i32 %X, undef
   ret i32 %Y
 }
+
+; CHECK-LABEL: test5
+; CHECK: ret i32 0
+define i32 @test5(i32 %foo) {
+  %patatino = mul i32 %foo, undef
+  ret i32 %patatino
+}




More information about the llvm-commits mailing list