From 73059bd1f10f28f574304c98038b91103cfdb68c Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Thu, 18 Dec 2014 23:54:43 +0000 Subject: [PATCH] ConstantFold: Shifting undef by zero results in undef git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@224553 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/IR/ConstantFold.cpp | 9 +++++++++ test/Transforms/InstSimplify/undef.ll | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/IR/ConstantFold.cpp b/lib/IR/ConstantFold.cpp index 31e962af0b..9176bf2aee 100644 --- a/lib/IR/ConstantFold.cpp +++ b/lib/IR/ConstantFold.cpp @@ -956,12 +956,18 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, // X >>l undef -> undef if (isa(C2)) return C2; + // undef >>l 0 -> undef + if (match(C2, m_Zero())) + return C1; // undef >>l X -> 0 return Constant::getNullValue(C1->getType()); case Instruction::AShr: // X >>a undef -> undef if (isa(C2)) return C2; + // undef >>a 0 -> undef + if (match(C2, m_Zero())) + return C1; // TODO: undef >>a X -> undef if the shift is exact // undef >>a X -> 0 return Constant::getNullValue(C1->getType()); @@ -969,6 +975,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, // X << undef -> undef if (isa(C2)) return C2; + // undef << 0 -> undef + if (match(C2, m_Zero())) + return C1; // undef << X -> 0 return Constant::getNullValue(C1->getType()); } diff --git a/test/Transforms/InstSimplify/undef.ll b/test/Transforms/InstSimplify/undef.ll index bf643171cc..e8b49b6397 100644 --- a/test/Transforms/InstSimplify/undef.ll +++ b/test/Transforms/InstSimplify/undef.ll @@ -244,3 +244,24 @@ define i32 @test31(i32 %a) { %b = shl i32 undef, %a ret i32 %b } + +; CHECK-LABEL: @test32 +; CHECK: ret i32 undef +define i32 @test32(i32 %a) { + %b = shl i32 undef, 0 + ret i32 %b +} + +; CHECK-LABEL: @test33 +; CHECK: ret i32 undef +define i32 @test33(i32 %a) { + %b = ashr i32 undef, 0 + ret i32 %b +} + +; CHECK-LABEL: @test34 +; CHECK: ret i32 undef +define i32 @test34(i32 %a) { + %b = lshr i32 undef, 0 + ret i32 %b +} -- 2.39.2