From 0dc20e321cb58946cf14e59aa028ab6287bfe687 Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Sun, 30 Jan 2022 10:46:54 +0000 Subject: [PATCH] [InstSimplify] fold 'xor X, poison' and 'div/rem X, poison' to poison --- llvm/lib/Analysis/InstructionSimplify.cpp | 6 +++++- llvm/test/Transforms/NewGVN/basic.ll | 6 ++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp index b71b39334ace..4775340b3438 100644 --- a/llvm/lib/Analysis/InstructionSimplify.cpp +++ b/llvm/lib/Analysis/InstructionSimplify.cpp @@ -951,7 +951,7 @@ static Value *simplifyDivRem(Instruction::BinaryOps Opcode, Value *Op0, // X / undef -> poison // X % undef -> poison - if (Q.isUndefValue(Op1)) + if (Q.isUndefValue(Op1) || isa(Op1)) return PoisonValue::get(Ty); // X / 0 -> poison @@ -2418,6 +2418,10 @@ static Value *SimplifyXorInst(Value *Op0, Value *Op1, const SimplifyQuery &Q, if (Constant *C = foldOrCommuteConstant(Instruction::Xor, Op0, Op1, Q)) return C; + // X ^ poison -> poison + if (isa(Op1)) + return Op1; + // A ^ undef -> undef if (Q.isUndefValue(Op1)) return Op1; diff --git a/llvm/test/Transforms/NewGVN/basic.ll b/llvm/test/Transforms/NewGVN/basic.ll index fb167ebd0fac..970ec81d0054 100644 --- a/llvm/test/Transforms/NewGVN/basic.ll +++ b/llvm/test/Transforms/NewGVN/basic.ll @@ -44,8 +44,7 @@ define i8 @simplify_add_poison(i8 %x) { define i8 @simplify_xor_poison(i8 %x) { ; CHECK-LABEL: @simplify_xor_poison( -; CHECK-NEXT: [[R:%.*]] = xor i8 poison, [[X:%.*]] -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 poison ; %r = xor i8 poison, %x ret i8 %r @@ -61,8 +60,7 @@ define i8 @simplify_sdiv_poison(i8 %x) { define i8 @simplify_urem_poison(i8 %x) { ; CHECK-LABEL: @simplify_urem_poison( -; CHECK-NEXT: [[R:%.*]] = urem i8 [[X:%.*]], poison -; CHECK-NEXT: ret i8 [[R]] +; CHECK-NEXT: ret i8 poison ; %r = urem i8 %x, poison ret i8 %r