From 74b5e797d5c9364bfc733f07ed9d27a33853f57b Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Sun, 7 Aug 2022 16:24:15 -0400 Subject: [PATCH] [InstSimplify] fold scalable vectors with over-shift splat constant to poison Fixes #56968 --- llvm/lib/Analysis/InstructionSimplify.cpp | 12 +++++++----- llvm/test/Transforms/InstSimplify/shift.ll | 3 +-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index 9cebbbd..a4f26c8 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -1252,12 +1252,14 @@ static bool isPoisonShift(Value *Amount, const SimplifyQuery &Q) { if (Q.isUndefValue(C)) return true; - // Shifting by the bitwidth or more is undefined. - if (ConstantInt *CI = dyn_cast(C)) - if (CI->getValue().uge(CI->getType()->getScalarSizeInBits())) - return true; + // Shifting by the bitwidth or more is poison. This covers scalars and + // fixed/scalable vectors with splat constants. + const APInt *AmountC; + if (match(C, m_APInt(AmountC)) && AmountC->uge(AmountC->getBitWidth())) + return true; - // If all lanes of a vector shift are undefined the whole shift is. + // Try harder for fixed-length vectors: + // If all lanes of a vector shift are poison, the whole shift is poison. if (isa(C) || isa(C)) { for (unsigned I = 0, E = cast(C->getType())->getNumElements(); diff --git a/llvm/test/Transforms/InstSimplify/shift.ll b/llvm/test/Transforms/InstSimplify/shift.ll index 8f2f026..9b14581 100644 --- a/llvm/test/Transforms/InstSimplify/shift.ll +++ b/llvm/test/Transforms/InstSimplify/shift.ll @@ -345,8 +345,7 @@ define i32 @all_ones_left_right_not_same_shift(i32 %x, i32 %y) { define @lshr_scalable_overshift( %va) { ; CHECK-LABEL: @lshr_scalable_overshift( -; CHECK-NEXT: [[VC:%.*]] = lshr [[VA:%.*]], shufflevector ( insertelement ( poison, i16 16, i32 0), poison, zeroinitializer) -; CHECK-NEXT: ret [[VC]] +; CHECK-NEXT: ret poison ; %vc = lshr %va, shufflevector ( insertelement ( poison, i16 16, i32 0), poison, zeroinitializer) ret %vc -- 2.7.4