[llvm] r224405 - InstSimplify: shl nsw/nuw undef, %V -> undef

David Majnemer david.majnemer at gmail.com
Tue Dec 16 17:54:33 PST 2014


Author: majnemer
Date: Tue Dec 16 19:54:33 2014
New Revision: 224405

URL: http://llvm.org/viewvc/llvm-project?rev=224405&view=rev
Log:
InstSimplify: shl nsw/nuw undef, %V -> undef

We can always choose an value for undef which might cause %V to shift
out an important bit except for one case, when %V is zero.

However, shl behaves like an identity function when the right hand side
is zero.

Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/test/Transforms/InstSimplify/undef.ll

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=224405&r1=224404&r2=224405&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Tue Dec 16 19:54:33 2014
@@ -1338,6 +1338,11 @@ static Value *SimplifyRightShift(unsigne
   if (Op0 == Op1)
     return Constant::getNullValue(Op0->getType());
 
+  // undef >> X -> 0
+  // undef >> X -> undef (if it's exact)
+  if (match(Op0, m_Undef()))
+    return isExact ? Op0 : Constant::getNullValue(Op0->getType());
+
   // The low bit cannot be shifted out of an exact shift if it is set.
   if (isExact) {
     unsigned BitWidth = Op0->getType()->getScalarSizeInBits();
@@ -1360,8 +1365,9 @@ static Value *SimplifyShlInst(Value *Op0
     return V;
 
   // undef << X -> 0
+  // undef << X -> undef if (if it's NSW/NUW)
   if (match(Op0, m_Undef()))
-    return Constant::getNullValue(Op0->getType());
+    return isNSW || isNUW ? Op0 : Constant::getNullValue(Op0->getType());
 
   // (X >> A) << A -> X
   Value *X;
@@ -1386,12 +1392,6 @@ static Value *SimplifyLShrInst(Value *Op
                                     MaxRecurse))
       return V;
 
-  // undef >>l X -> 0
-  // undef >>l X -> undef (if it's exact)
-  if (match(Op0, m_Undef()))
-    return isExact ? UndefValue::get(Op0->getType())
-                   : Constant::getNullValue(Op0->getType());
-
   // (X << A) >> A -> X
   Value *X;
   if (match(Op0, m_NUWShl(m_Value(X), m_Specific(Op1))))
@@ -1422,12 +1422,6 @@ static Value *SimplifyAShrInst(Value *Op
   if (match(Op0, m_AllOnes()))
     return Op0;
 
-  // undef >>a X -> 0
-  // undef >>a X -> undef (if it's exact)
-  if (match(Op0, m_Undef()))
-    return isExact ? UndefValue::get(Op0->getType())
-                   : Constant::getNullValue(Op0->getType());
-
   // (X << A) >> A -> X
   Value *X;
   if (match(Op0, m_NSWShl(m_Value(X), m_Specific(Op1))))

Modified: llvm/trunk/test/Transforms/InstSimplify/undef.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/undef.ll?rev=224405&r1=224404&r2=224405&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/undef.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/undef.ll Tue Dec 16 19:54:33 2014
@@ -216,3 +216,31 @@ define i32 @test27() {
   %b = shl i32 0, undef
   ret i32 %b
 }
+
+; CHECK-LABEL: @test28
+; CHECK: ret i32 undef
+define i32 @test28(i32 %a) {
+  %b = shl nsw i32 undef, %a
+  ret i32 %b
+}
+
+; CHECK-LABEL: @test29
+; CHECK: ret i32 undef
+define i32 @test29(i32 %a) {
+  %b = shl nuw i32 undef, %a
+  ret i32 %b
+}
+
+; CHECK-LABEL: @test30
+; CHECK: ret i32 undef
+define i32 @test30(i32 %a) {
+  %b = shl nsw nuw i32 undef, %a
+  ret i32 %b
+}
+
+; CHECK-LABEL: @test31
+; CHECK: ret i32 0
+define i32 @test31(i32 %a) {
+  %b = shl i32 undef, %a
+  ret i32 %b
+}





More information about the llvm-commits mailing list