InstSimplify: [al]shr exact undef, %X -> undef
authorDavid Majnemer <david.majnemer@gmail.com>
Wed, 10 Dec 2014 09:14:52 +0000 (09:14 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Wed, 10 Dec 2014 09:14:52 +0000 (09:14 +0000)
Exact shifts always keep the non-zero bits of their input.  This means
it keeps it's undef bits.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223923 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/InstructionSimplify.cpp
test/Transforms/InstSimplify/undef.ll

index 767b42b4bd703a30cf13ea8e752ca86b280a39b0..1c5a917cbec7a4909879471f360547043ba2cc30 100644 (file)
@@ -1387,8 +1387,10 @@ static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
       return V;
 
   // undef >>l X -> 0
+  // undef >>l X -> undef (if it's exact)
   if (match(Op0, m_Undef()))
-    return Constant::getNullValue(Op0->getType());
+    return isExact ? UndefValue::get(Op0->getType())
+                   : Constant::getNullValue(Op0->getType());
 
   // (X << A) >> A -> X
   Value *X;
@@ -1421,8 +1423,10 @@ static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
     return Op0;
 
   // undef >>a X -> all ones
+  // undef >>a X -> undef (if it's exact)
   if (match(Op0, m_Undef()))
-    return Constant::getAllOnesValue(Op0->getType());
+    return isExact ? UndefValue::get(Op0->getType())
+                   : Constant::getAllOnesValue(Op0->getType());
 
   // (X << A) >> A -> X
   Value *X;
index a6ae7a9fbf78044115b6924cb7853c77f0b70f1f..3742d7450af620f6367ad36d23c6e10254762340 100644 (file)
@@ -174,3 +174,17 @@ define i32 @test21(i32 %a) {
   %b = sdiv i32 %a, 0
   ret i32 %b
 }
+
+; CHECK-LABEL: @test22
+; CHECK: ret i32 undef
+define i32 @test22(i32 %a) {
+  %b = ashr exact i32 undef, %a
+  ret i32 %b
+}
+
+; CHECK-LABEL: @test23
+; CHECK: ret i32 undef
+define i32 @test23(i32 %a) {
+  %b = lshr exact i32 undef, %a
+  ret i32 %b
+}