[llvm] r223968 - ConstantFold: an undef shift amount results in undef

David Majnemer david.majnemer at gmail.com
Wed Dec 10 13:38:05 PST 2014


Author: majnemer
Date: Wed Dec 10 15:38:05 2014
New Revision: 223968

URL: http://llvm.org/viewvc/llvm-project?rev=223968&view=rev
Log:
ConstantFold: an undef shift amount results in undef

X shifted by undef results in undef because the undef value can
represent values greater than the width of the operands.

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=223968&r1=223967&r2=223968&view=diff
==============================================================================
--- llvm/trunk/lib/IR/ConstantFold.cpp (original)
+++ llvm/trunk/lib/IR/ConstantFold.cpp Wed Dec 10 15:38:05 2014
@@ -951,21 +951,22 @@ Constant *llvm::ConstantFoldBinaryInstru
         return C1;
       return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0
     case Instruction::LShr:
-      if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
-        return C1;                                  // undef lshr undef -> undef
-      return Constant::getNullValue(C1->getType()); // X lshr undef -> 0
-                                                    // undef lshr X -> 0
+      // X >>l undef -> undef
+      if (isa<UndefValue>(C2))
+        return C2;
+      // undef >>l X -> 0
+      return Constant::getNullValue(C1->getType());
     case Instruction::AShr:
-      if (!isa<UndefValue>(C2))                     // undef ashr X --> all ones
-        return Constant::getAllOnesValue(C1->getType());
-      else if (isa<UndefValue>(C1)) 
-        return C1;                                  // undef ashr undef -> undef
-      else
-        return C1;                                  // X ashr undef --> X
+      // X >>a undef -> undef
+      if (isa<UndefValue>(C2))
+        return C2;
+      // undef >>a X -> all ones
+      return Constant::getAllOnesValue(C1->getType());
     case Instruction::Shl:
-      if (isa<UndefValue>(C2) && isa<UndefValue>(C1))
-        return C1;                                  // undef shl undef -> undef
-      // undef << X -> 0   or   X << undef -> 0
+      // X << undef -> undef
+      if (isa<UndefValue>(C2))
+        return C2;
+      // undef << X -> 0
       return Constant::getNullValue(C1->getType());
     }
   }

Modified: llvm/trunk/test/Transforms/InstSimplify/undef.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/undef.ll?rev=223968&r1=223967&r2=223968&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/undef.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/undef.ll Wed Dec 10 15:38:05 2014
@@ -195,3 +195,24 @@ define i32 @test24(i32 %a) {
   %b = udiv i32 undef, 0
   ret i32 %b
 }
+
+; CHECK-LABEL: @test25
+; CHECK: ret i32 undef
+define i32 @test25(i32 %a) {
+  %b = lshr i32 0, undef
+  ret i32 %b
+}
+
+; CHECK-LABEL: @test26
+; CHECK: ret i32 undef
+define i32 @test26(i32 %a) {
+  %b = ashr i32 0, undef
+  ret i32 %b
+}
+
+; CHECK-LABEL: @test27
+; CHECK: ret i32 undef
+define i32 @test27(i32 %a) {
+  %b = shl i32 0, undef
+  ret i32 %b
+}





More information about the llvm-commits mailing list