[llvm] r261813 - IR: Make the X / undef -> undef fold match the comment

Justin Bogner via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 24 17:02:19 PST 2016


Author: bogner
Date: Wed Feb 24 19:02:18 2016
New Revision: 261813

URL: http://llvm.org/viewvc/llvm-project?rev=261813&view=rev
Log:
IR: Make the X / undef -> undef fold match the comment

The constant folding for sdiv and udiv has a big discrepancy between the
comments and the code, which looks like a typo. Currently, we're folding
X / undef pretty inconsistently:

  0 / undef -> undef
  C / undef -> 0
  undef / undef -> 0

Whereas the comments state we do X / undef -> undef. The logic that
returns zero is actually commented as doing undef / X -> 0, despite that
the LHS isn't undef in many of the cases that hit it.

Modified:
    llvm/trunk/lib/IR/ConstantFold.cpp
    llvm/trunk/test/Transforms/InstSimplify/undef.ll

Modified: llvm/trunk/lib/IR/ConstantFold.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/ConstantFold.cpp?rev=261813&r1=261812&r2=261813&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Wed Feb 24 19:02:18 2016
@@ -948,7 +948,7 @@ Constant *llvm::ConstantFoldBinaryInstru
     case Instruction::SDiv:
     case Instruction::UDiv:
       // X / undef -> undef
-      if (match(C1, m_Zero()))
+      if (isa<UndefValue>(C2))
         return C2;
       // undef / 0 -> undef
       // undef / 1 -> undef

Modified: llvm/trunk/test/Transforms/InstSimplify/undef.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/undef.ll?rev=261813&r1=261812&r2=261813&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/undef.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/undef.ll Wed Feb 24 19:02:18 2016
@@ -279,3 +279,24 @@ define i32 @test36(i32 %V) {
   %b = extractelement <4 x i32> undef, i32 %V
   ret i32 %b
 }
+
+; CHECK-LABEL: @test37
+; CHECK: ret i32 undef
+define i32 @test37() {
+  %b = udiv i32 undef, undef
+  ret i32 %b
+}
+
+; CHECK-LABEL: @test38
+; CHECK: ret i32 undef
+define i32 @test38(i32 %a) {
+  %b = udiv i32 %a, undef
+  ret i32 %b
+}
+
+; CHECK-LABEL: @test39
+; CHECK: ret i32 undef
+define i32 @test39() {
+  %b = udiv i32 0, undef
+  ret i32 %b
+}




More information about the llvm-commits mailing list