From 5a7717e498bc61246375a9df0061afa27d21e322 Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Wed, 10 Dec 2014 21:58:15 +0000 Subject: [PATCH] ConstantFold, InstSimplify: undef >>a x can be either -1 or 0, choose 0 Zero is usually a nicer constant to have than -1. llvm-svn: 223969 --- llvm/lib/Analysis/InstructionSimplify.cpp | 4 ++-- llvm/lib/IR/ConstantFold.cpp | 5 +++-- llvm/test/Transforms/InstCombine/shift.ll | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 1c5a917..408768e 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -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; diff --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp index 49ef302..a05c594 100644 --- a/llvm/lib/IR/ConstantFold.cpp +++ b/llvm/lib/IR/ConstantFold.cpp @@ -960,8 +960,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, // X >>a undef -> undef if (isa(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(C2)) diff --git a/llvm/test/Transforms/InstCombine/shift.ll b/llvm/test/Transforms/InstCombine/shift.ll index 1acc0a5..0b5b5de 100644 --- a/llvm/test/Transforms/InstCombine/shift.ll +++ b/llvm/test/Transforms/InstCombine/shift.ll @@ -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 } -- 2.7.4