From b18dbbfff519831cab68156ff3af06ee8a29137e Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 8 May 2005 17:34:56 +0000 Subject: [PATCH] Strength reduce SAR into SHR if there is no way sign bits could be shifted in. This tends to get cases like this: X = cast ubyte to int Y = shr int X, ... Tested by: shift.ll:test24 llvm-svn: 21775 --- llvm/lib/Transforms/Scalar/InstructionCombining.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp index 6cafc2577713..40d107a810af 100644 --- a/llvm/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/llvm/lib/Transforms/Scalar/InstructionCombining.cpp @@ -3136,6 +3136,16 @@ Instruction *InstCombiner::visitShiftInst(ShiftInst &I) { if (Instruction *R = FoldOpIntoSelect(I, SI, this)) return R; + // See if we can turn a signed shr into an unsigned shr. + if (!isLeftShift && I.getType()->isSigned()) { + if (MaskedValueIsZero(Op0, ConstantInt::getMinValue(I.getType()))) { + Value *V = InsertCastBefore(Op0, I.getType()->getUnsignedVersion(), I); + V = InsertNewInstBefore(new ShiftInst(Instruction::Shr, V, Op1, + I.getName()), I); + return new CastInst(V, I.getType()); + } + } + if (ConstantUInt *CUI = dyn_cast(Op1)) { // shl uint X, 32 = 0 and shr ubyte Y, 9 = 0, ... just don't eliminate shr // of a signed value.