IR: Make the X / undef -> undef fold match the comment
authorJustin Bogner <mail@justinbogner.com>
Thu, 25 Feb 2016 01:02:18 +0000 (01:02 +0000)
committerJustin Bogner <mail@justinbogner.com>
Thu, 25 Feb 2016 01:02:18 +0000 (01:02 +0000)
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.

llvm-svn: 261813

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

index 00ff9788a9c2a188e32759b35364d2ce9ea87579..aea8308e9c64944bfbb822d4eb4dda37bace8186 100644 (file)
@@ -948,7 +948,7 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
     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
index d75dc364243cd632cc9ede895f81cc379ce5b9fb..6141180e08d620f9b546126c8168d43f18c0bc88 100644 (file)
@@ -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
+}