ConstantFold, InstSimplify: undef >>a x can be either -1 or 0, choose 0
authorDavid Majnemer <david.majnemer@gmail.com>
Wed, 10 Dec 2014 21:58:15 +0000 (21:58 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Wed, 10 Dec 2014 21:58:15 +0000 (21:58 +0000)
Zero is usually a nicer constant to have than -1.

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

lib/Analysis/InstructionSimplify.cpp
lib/IR/ConstantFold.cpp
test/Transforms/InstCombine/shift.ll

index 1c5a917cbec7a4909879471f360547043ba2cc30..408768e8515782e1b7516a76f766fd37209f942a 100644 (file)
@@ -1422,11 +1422,11 @@ static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
   if (match(Op0, m_AllOnes()))
     return Op0;
 
-  // undef >>a X -> all ones
+  // undef >>a X -> 0
   // undef >>a X -> undef (if it's exact)
   if (match(Op0, m_Undef()))
     return isExact ? UndefValue::get(Op0->getType())
-                   : Constant::getAllOnesValue(Op0->getType());
+                   : Constant::getNullValue(Op0->getType());
 
   // (X << A) >> A -> X
   Value *X;
index 49ef3024033f98d88c5debedf7d213f8c95860c9..a05c594ac8ed224ed8558e8f98ab6e6261a81bf6 100644 (file)
@@ -960,8 +960,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode,
       // X >>a undef -> undef
       if (isa<UndefValue>(C2))
         return C2;
-      // undef >>a X -> all ones
-      return Constant::getAllOnesValue(C1->getType());
+      // TODO: undef >>a X -> undef if the shift is exact
+      // undef >>a X -> 0
+      return Constant::getNullValue(C1->getType());
     case Instruction::Shl:
       // X << undef -> undef
       if (isa<UndefValue>(C2))
index 1acc0a5643008640811d57eb68b37279ee4637b6..0b5b5deb68c571b2922d04bfab98c639bd77a661 100644 (file)
@@ -84,14 +84,14 @@ define <4 x i32> @test5a_non_splat_vector(<4 x i32> %A) {
 
 define i32 @test5b() {
 ; CHECK-LABEL: @test5b(
-; CHECK: ret i32 -1
+; CHECK: ret i32 0
         %B = ashr i32 undef, 2  ;; top two bits must be equal, so not undef
         ret i32 %B
 }
 
 define i32 @test5b2(i32 %A) {
 ; CHECK-LABEL: @test5b2(
-; CHECK: ret i32 -1
+; CHECK: ret i32 0
         %B = ashr i32 undef, %A  ;; top %A bits must be equal, so not undef
         ret i32 %B
 }