From ea9bcfc70719c1ecc18828781ef4cd3bce8c8e2f Mon Sep 17 00:00:00 2001 From: David Majnemer Date: Wed, 10 Dec 2014 21:38:05 +0000 Subject: [PATCH] ConstantFold: an undef shift amount results in undef X shifted by undef results in undef because the undef value can represent values greater than the width of the operands. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@223968 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/IR/ConstantFold.cpp | 27 ++++++++++++++------------- test/Transforms/InstSimplify/undef.ll | 21 +++++++++++++++++++++ 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/lib/IR/ConstantFold.cpp b/lib/IR/ConstantFold.cpp index cd409083afc..49ef3024033 100644 --- a/lib/IR/ConstantFold.cpp +++ b/lib/IR/ConstantFold.cpp @@ -951,21 +951,22 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, return C1; return Constant::getAllOnesValue(C1->getType()); // undef | X -> ~0 case Instruction::LShr: - if (isa(C2) && isa(C1)) - return C1; // undef lshr undef -> undef - return Constant::getNullValue(C1->getType()); // X lshr undef -> 0 - // undef lshr X -> 0 + // X >>l undef -> undef + if (isa(C2)) + return C2; + // undef >>l X -> 0 + return Constant::getNullValue(C1->getType()); case Instruction::AShr: - if (!isa(C2)) // undef ashr X --> all ones - return Constant::getAllOnesValue(C1->getType()); - else if (isa(C1)) - return C1; // undef ashr undef -> undef - else - return C1; // X ashr undef --> X + // X >>a undef -> undef + if (isa(C2)) + return C2; + // undef >>a X -> all ones + return Constant::getAllOnesValue(C1->getType()); case Instruction::Shl: - if (isa(C2) && isa(C1)) - return C1; // undef shl undef -> undef - // undef << X -> 0 or X << undef -> 0 + // X << undef -> undef + if (isa(C2)) + return C2; + // undef << X -> 0 return Constant::getNullValue(C1->getType()); } } diff --git a/test/Transforms/InstSimplify/undef.ll b/test/Transforms/InstSimplify/undef.ll index 5787badfd14..d72ff8568e6 100644 --- a/test/Transforms/InstSimplify/undef.ll +++ b/test/Transforms/InstSimplify/undef.ll @@ -195,3 +195,24 @@ define i32 @test24(i32 %a) { %b = udiv i32 undef, 0 ret i32 %b } + +; CHECK-LABEL: @test25 +; CHECK: ret i32 undef +define i32 @test25(i32 %a) { + %b = lshr i32 0, undef + ret i32 %b +} + +; CHECK-LABEL: @test26 +; CHECK: ret i32 undef +define i32 @test26(i32 %a) { + %b = ashr i32 0, undef + ret i32 %b +} + +; CHECK-LABEL: @test27 +; CHECK: ret i32 undef +define i32 @test27(i32 %a) { + %b = shl i32 0, undef + ret i32 %b +} -- 2.34.1