Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 1 | //===- InstCombineShifts.cpp ----------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the visitShl, visitLShr, and visitAShr functions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | a917458 | 2015-01-22 05:25:13 +0000 | [diff] [blame] | 14 | #include "InstCombineInternal.h" |
Eli Friedman | 911e12f | 2011-07-20 21:57:23 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/ConstantFolding.h" |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 16 | #include "llvm/Analysis/InstructionSimplify.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/IntrinsicInst.h" |
Chandler Carruth | 820a908 | 2014-03-04 11:08:18 +0000 | [diff] [blame] | 18 | #include "llvm/IR/PatternMatch.h" |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | using namespace PatternMatch; |
| 21 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "instcombine" |
| 23 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 24 | Instruction *InstCombiner::commonShiftTransforms(BinaryOperator &I) { |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 25 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 26 | assert(Op0->getType() == Op1->getType()); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 27 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 28 | // See if we can fold away this shift. |
| 29 | if (SimplifyDemandedInstructionBits(I)) |
| 30 | return &I; |
| 31 | |
| 32 | // Try to fold constant and into select arguments. |
| 33 | if (isa<Constant>(Op0)) |
| 34 | if (SelectInst *SI = dyn_cast<SelectInst>(Op1)) |
| 35 | if (Instruction *R = FoldOpIntoSelect(I, SI)) |
| 36 | return R; |
| 37 | |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 38 | if (Constant *CUI = dyn_cast<Constant>(Op1)) |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 39 | if (Instruction *Res = FoldShiftByConstant(Op0, CUI, I)) |
| 40 | return Res; |
Benjamin Kramer | b5afa65 | 2010-11-23 18:52:42 +0000 | [diff] [blame] | 41 | |
Simon Pilgrim | 6dd8fab | 2016-11-01 15:40:30 +0000 | [diff] [blame] | 42 | // (C1 shift (A add C2)) -> (C1 shift C2) shift A) |
| 43 | // iff A and C2 are both positive. |
| 44 | Value *A; |
| 45 | Constant *C; |
| 46 | if (match(Op0, m_Constant()) && match(Op1, m_Add(m_Value(A), m_Constant(C)))) |
| 47 | if (isKnownNonNegative(A, DL) && isKnownNonNegative(C, DL)) |
| 48 | return BinaryOperator::Create( |
| 49 | I.getOpcode(), Builder->CreateBinOp(I.getOpcode(), Op0, C), A); |
| 50 | |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 51 | // X shift (A srem B) -> X shift (A and B-1) iff B is a power of 2. |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 52 | // Because shifts by negative values (which could occur if A were negative) |
| 53 | // are undefined. |
Simon Pilgrim | 6dd8fab | 2016-11-01 15:40:30 +0000 | [diff] [blame] | 54 | const APInt *B; |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 55 | if (Op1->hasOneUse() && match(Op1, m_SRem(m_Value(A), m_Power2(B)))) { |
| 56 | // FIXME: Should this get moved into SimplifyDemandedBits by saying we don't |
| 57 | // demand the sign bit (and many others) here?? |
| 58 | Value *Rem = Builder->CreateAnd(A, ConstantInt::get(I.getType(), *B-1), |
| 59 | Op1->getName()); |
| 60 | I.setOperand(1, Rem); |
| 61 | return &I; |
| 62 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 63 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 64 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 67 | /// Return true if we can simplify two logical (either left or right) shifts |
| 68 | /// that have constant shift amounts. |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 69 | static bool canEvaluateShiftedShift(unsigned FirstShiftAmt, |
| 70 | bool IsFirstShiftLeft, |
| 71 | Instruction *SecondShift, InstCombiner &IC, |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 72 | Instruction *CxtI) { |
Sanjay Patel | 3a48e98 | 2016-04-11 17:27:44 +0000 | [diff] [blame] | 73 | assert(SecondShift->isLogicalShift() && "Unexpected instruction type"); |
Sanjay Patel | b91bcd7 | 2016-04-11 17:35:57 +0000 | [diff] [blame] | 74 | |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 75 | // We need constant shifts. |
| 76 | auto *SecondShiftConst = dyn_cast<ConstantInt>(SecondShift->getOperand(1)); |
| 77 | if (!SecondShiftConst) |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 78 | return false; |
| 79 | |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 80 | unsigned SecondShiftAmt = SecondShiftConst->getZExtValue(); |
Sanjay Patel | 3712907 | 2016-04-11 17:11:55 +0000 | [diff] [blame] | 81 | bool IsSecondShiftLeft = SecondShift->getOpcode() == Instruction::Shl; |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 82 | |
Sanjay Patel | 3712907 | 2016-04-11 17:11:55 +0000 | [diff] [blame] | 83 | // We can always fold shl(c1) + shl(c2) -> shl(c1+c2). |
| 84 | // We can always fold lshr(c1) + lshr(c2) -> lshr(c1+c2). |
| 85 | if (IsFirstShiftLeft == IsSecondShiftLeft) |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 86 | return true; |
| 87 | |
Sanjay Patel | 3712907 | 2016-04-11 17:11:55 +0000 | [diff] [blame] | 88 | // We can always fold lshr(c) + shl(c) -> and(c2). |
| 89 | // We can always fold shl(c) + lshr(c) -> and(c2). |
| 90 | if (FirstShiftAmt == SecondShiftAmt) |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 91 | return true; |
| 92 | |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 93 | unsigned TypeWidth = SecondShift->getType()->getScalarSizeInBits(); |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 94 | |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 95 | // If the 2nd shift is bigger than the 1st, we can fold: |
Sanjay Patel | 3712907 | 2016-04-11 17:11:55 +0000 | [diff] [blame] | 96 | // lshr(c1) + shl(c2) -> shl(c3) + and(c4) or |
| 97 | // shl(c1) + lshr(c2) -> lshr(c3) + and(c4), |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 98 | // but it isn't profitable unless we know the and'd out bits are already zero. |
Sanjay Patel | 816ec88 | 2016-04-11 16:50:32 +0000 | [diff] [blame] | 99 | // Also check that the 2nd shift is valid (less than the type width) or we'll |
| 100 | // crash trying to produce the bit mask for the 'and'. |
| 101 | if (SecondShiftAmt > FirstShiftAmt && SecondShiftAmt < TypeWidth) { |
Sanjay Patel | 3712907 | 2016-04-11 17:11:55 +0000 | [diff] [blame] | 102 | unsigned MaskShift = IsSecondShiftLeft ? TypeWidth - SecondShiftAmt |
| 103 | : SecondShiftAmt - FirstShiftAmt; |
Sanjay Patel | bd8b779 | 2016-04-11 16:11:07 +0000 | [diff] [blame] | 104 | APInt Mask = APInt::getLowBitsSet(TypeWidth, FirstShiftAmt) << MaskShift; |
| 105 | if (IC.MaskedValueIsZero(SecondShift->getOperand(0), Mask, 0, CxtI)) |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 112 | /// See if we can compute the specified value, but shifted logically to the left |
| 113 | /// or right by some number of bits. This should return true if the expression |
| 114 | /// can be computed for the same cost as the current expression tree. This is |
| 115 | /// used to eliminate extraneous shifting from things like: |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 116 | /// %C = shl i128 %A, 64 |
| 117 | /// %D = shl i128 %B, 96 |
| 118 | /// %E = or i128 %C, %D |
| 119 | /// %F = lshr i128 %E, 64 |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 120 | /// where the client will ask if E can be computed shifted right by 64-bits. If |
| 121 | /// this succeeds, getShiftedValue() will be called to produce the value. |
| 122 | static bool canEvaluateShifted(Value *V, unsigned NumBits, bool IsLeftShift, |
Hal Finkel | 60db058 | 2014-09-07 18:57:58 +0000 | [diff] [blame] | 123 | InstCombiner &IC, Instruction *CxtI) { |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 124 | // We can always evaluate constants shifted. |
| 125 | if (isa<Constant>(V)) |
| 126 | return true; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 128 | Instruction *I = dyn_cast<Instruction>(V); |
| 129 | if (!I) return false; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 130 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 131 | // If this is the opposite shift, we can directly reuse the input of the shift |
| 132 | // if the needed bits are already zero in the input. This allows us to reuse |
| 133 | // the value which means that we don't care if the shift has multiple uses. |
| 134 | // TODO: Handle opposite shift by exact value. |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 135 | ConstantInt *CI = nullptr; |
Sanjay Patel | 4b9c682 | 2016-04-11 17:25:23 +0000 | [diff] [blame] | 136 | if ((IsLeftShift && match(I, m_LShr(m_Value(), m_ConstantInt(CI)))) || |
| 137 | (!IsLeftShift && match(I, m_Shl(m_Value(), m_ConstantInt(CI))))) { |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 138 | if (CI->getZExtValue() == NumBits) { |
| 139 | // TODO: Check that the input bits are already zero with MaskedValueIsZero |
| 140 | #if 0 |
| 141 | // If this is a truncate of a logical shr, we can truncate it to a smaller |
Sylvestre Ledru | 91ce36c | 2012-09-27 10:14:43 +0000 | [diff] [blame] | 142 | // lshr iff we know that the bits we would otherwise be shifting in are |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 143 | // already zeros. |
| 144 | uint32_t OrigBitWidth = OrigTy->getScalarSizeInBits(); |
| 145 | uint32_t BitWidth = Ty->getScalarSizeInBits(); |
| 146 | if (MaskedValueIsZero(I->getOperand(0), |
| 147 | APInt::getHighBitsSet(OrigBitWidth, OrigBitWidth-BitWidth)) && |
| 148 | CI->getLimitedValue(BitWidth) < BitWidth) { |
| 149 | return CanEvaluateTruncated(I->getOperand(0), Ty); |
| 150 | } |
| 151 | #endif |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 155 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 156 | // We can't mutate something that has multiple uses: doing so would |
| 157 | // require duplicating the instruction in general, which isn't profitable. |
| 158 | if (!I->hasOneUse()) return false; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 159 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 160 | switch (I->getOpcode()) { |
| 161 | default: return false; |
| 162 | case Instruction::And: |
| 163 | case Instruction::Or: |
| 164 | case Instruction::Xor: |
| 165 | // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted. |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 166 | return canEvaluateShifted(I->getOperand(0), NumBits, IsLeftShift, IC, I) && |
| 167 | canEvaluateShifted(I->getOperand(1), NumBits, IsLeftShift, IC, I); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 168 | |
Sanjay Patel | 6eaff5c | 2016-04-11 15:43:41 +0000 | [diff] [blame] | 169 | case Instruction::Shl: |
Sanjay Patel | 3712907 | 2016-04-11 17:11:55 +0000 | [diff] [blame] | 170 | case Instruction::LShr: |
Sanjay Patel | 4b9c682 | 2016-04-11 17:25:23 +0000 | [diff] [blame] | 171 | return canEvaluateShiftedShift(NumBits, IsLeftShift, I, IC, CxtI); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 172 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 173 | case Instruction::Select: { |
| 174 | SelectInst *SI = cast<SelectInst>(I); |
Sanjay Patel | 690955f | 2016-01-31 16:34:11 +0000 | [diff] [blame] | 175 | Value *TrueVal = SI->getTrueValue(); |
| 176 | Value *FalseVal = SI->getFalseValue(); |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 177 | return canEvaluateShifted(TrueVal, NumBits, IsLeftShift, IC, SI) && |
| 178 | canEvaluateShifted(FalseVal, NumBits, IsLeftShift, IC, SI); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 179 | } |
| 180 | case Instruction::PHI: { |
| 181 | // We can change a phi if we can change all operands. Note that we never |
| 182 | // get into trouble with cyclic PHIs here because we only consider |
| 183 | // instructions with a single use. |
| 184 | PHINode *PN = cast<PHINode>(I); |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 185 | for (Value *IncValue : PN->incoming_values()) |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 186 | if (!canEvaluateShifted(IncValue, NumBits, IsLeftShift, IC, PN)) |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 187 | return false; |
| 188 | return true; |
| 189 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 190 | } |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 193 | /// When canEvaluateShifted() returns true for an expression, this function |
| 194 | /// inserts the new computation that produces the shifted value. |
| 195 | static Value *getShiftedValue(Value *V, unsigned NumBits, bool isLeftShift, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 196 | InstCombiner &IC, const DataLayout &DL) { |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 197 | // We can always evaluate constants shifted. |
| 198 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 199 | if (isLeftShift) |
| 200 | V = IC.Builder->CreateShl(C, NumBits); |
| 201 | else |
| 202 | V = IC.Builder->CreateLShr(C, NumBits); |
| 203 | // If we got a constantexpr back, try to simplify it with TD info. |
David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 204 | if (auto *C = dyn_cast<Constant>(V)) |
| 205 | if (auto *FoldedC = |
Justin Bogner | 9979840 | 2016-08-05 01:06:44 +0000 | [diff] [blame] | 206 | ConstantFoldConstant(C, DL, &IC.getTargetLibraryInfo())) |
David Majnemer | d536f23 | 2016-07-29 03:27:26 +0000 | [diff] [blame] | 207 | V = FoldedC; |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 208 | return V; |
| 209 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 211 | Instruction *I = cast<Instruction>(V); |
| 212 | IC.Worklist.Add(I); |
| 213 | |
| 214 | switch (I->getOpcode()) { |
Craig Topper | a2886c2 | 2012-02-07 05:05:23 +0000 | [diff] [blame] | 215 | default: llvm_unreachable("Inconsistency with CanEvaluateShifted"); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 216 | case Instruction::And: |
| 217 | case Instruction::Or: |
| 218 | case Instruction::Xor: |
| 219 | // Bitwise operators can all arbitrarily be arbitrarily evaluated shifted. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 220 | I->setOperand( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 221 | 0, getShiftedValue(I->getOperand(0), NumBits, isLeftShift, IC, DL)); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 222 | I->setOperand( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 223 | 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL)); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 224 | return I; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 226 | case Instruction::Shl: { |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 227 | BinaryOperator *BO = cast<BinaryOperator>(I); |
| 228 | unsigned TypeWidth = BO->getType()->getScalarSizeInBits(); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 229 | |
| 230 | // We only accept shifts-by-a-constant in CanEvaluateShifted. |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 231 | ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1)); |
| 232 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 233 | // We can always fold shl(c1)+shl(c2) -> shl(c1+c2). |
| 234 | if (isLeftShift) { |
| 235 | // If this is oversized composite shift, then unsigned shifts get 0. |
| 236 | unsigned NewShAmt = NumBits+CI->getZExtValue(); |
| 237 | if (NewShAmt >= TypeWidth) |
| 238 | return Constant::getNullValue(I->getType()); |
| 239 | |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 240 | BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt)); |
| 241 | BO->setHasNoUnsignedWrap(false); |
| 242 | BO->setHasNoSignedWrap(false); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 243 | return I; |
| 244 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 246 | // We turn shl(c)+lshr(c) -> and(c2) if the input doesn't already have |
| 247 | // zeros. |
Chris Lattner | 6c1395f | 2010-08-27 22:53:44 +0000 | [diff] [blame] | 248 | if (CI->getValue() == NumBits) { |
| 249 | APInt Mask(APInt::getLowBitsSet(TypeWidth, TypeWidth - NumBits)); |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 250 | V = IC.Builder->CreateAnd(BO->getOperand(0), |
| 251 | ConstantInt::get(BO->getContext(), Mask)); |
Chris Lattner | 6c1395f | 2010-08-27 22:53:44 +0000 | [diff] [blame] | 252 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 253 | VI->moveBefore(BO); |
| 254 | VI->takeName(BO); |
Chris Lattner | 6c1395f | 2010-08-27 22:53:44 +0000 | [diff] [blame] | 255 | } |
| 256 | return V; |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 257 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 6c1395f | 2010-08-27 22:53:44 +0000 | [diff] [blame] | 259 | // We turn shl(c1)+shr(c2) -> shl(c3)+and(c4), but only when we know that |
| 260 | // the and won't be needed. |
| 261 | assert(CI->getZExtValue() > NumBits); |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 262 | BO->setOperand(1, ConstantInt::get(BO->getType(), |
| 263 | CI->getZExtValue() - NumBits)); |
| 264 | BO->setHasNoUnsignedWrap(false); |
| 265 | BO->setHasNoSignedWrap(false); |
| 266 | return BO; |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 267 | } |
Sanjay Patel | b91bcd7 | 2016-04-11 17:35:57 +0000 | [diff] [blame] | 268 | // FIXME: This is almost identical to the SHL case. Refactor both cases into |
| 269 | // a helper function. |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 270 | case Instruction::LShr: { |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 271 | BinaryOperator *BO = cast<BinaryOperator>(I); |
| 272 | unsigned TypeWidth = BO->getType()->getScalarSizeInBits(); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 273 | // We only accept shifts-by-a-constant in CanEvaluateShifted. |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 274 | ConstantInt *CI = cast<ConstantInt>(BO->getOperand(1)); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 276 | // We can always fold lshr(c1)+lshr(c2) -> lshr(c1+c2). |
| 277 | if (!isLeftShift) { |
| 278 | // If this is oversized composite shift, then unsigned shifts get 0. |
| 279 | unsigned NewShAmt = NumBits+CI->getZExtValue(); |
| 280 | if (NewShAmt >= TypeWidth) |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 281 | return Constant::getNullValue(BO->getType()); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 282 | |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 283 | BO->setOperand(1, ConstantInt::get(BO->getType(), NewShAmt)); |
| 284 | BO->setIsExact(false); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 285 | return I; |
| 286 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 287 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 288 | // We turn lshr(c)+shl(c) -> and(c2) if the input doesn't already have |
| 289 | // zeros. |
Chris Lattner | 6c1395f | 2010-08-27 22:53:44 +0000 | [diff] [blame] | 290 | if (CI->getValue() == NumBits) { |
| 291 | APInt Mask(APInt::getHighBitsSet(TypeWidth, TypeWidth - NumBits)); |
| 292 | V = IC.Builder->CreateAnd(I->getOperand(0), |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 293 | ConstantInt::get(BO->getContext(), Mask)); |
Chris Lattner | 6c1395f | 2010-08-27 22:53:44 +0000 | [diff] [blame] | 294 | if (Instruction *VI = dyn_cast<Instruction>(V)) { |
| 295 | VI->moveBefore(I); |
| 296 | VI->takeName(I); |
| 297 | } |
| 298 | return V; |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 299 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 300 | |
Chris Lattner | 6c1395f | 2010-08-27 22:53:44 +0000 | [diff] [blame] | 301 | // We turn lshr(c1)+shl(c2) -> lshr(c3)+and(c4), but only when we know that |
| 302 | // the and won't be needed. |
| 303 | assert(CI->getZExtValue() > NumBits); |
Eli Friedman | 530341d | 2011-07-29 00:18:19 +0000 | [diff] [blame] | 304 | BO->setOperand(1, ConstantInt::get(BO->getType(), |
| 305 | CI->getZExtValue() - NumBits)); |
| 306 | BO->setIsExact(false); |
| 307 | return BO; |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 308 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 309 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 310 | case Instruction::Select: |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 311 | I->setOperand( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 312 | 1, getShiftedValue(I->getOperand(1), NumBits, isLeftShift, IC, DL)); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 313 | I->setOperand( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 314 | 2, getShiftedValue(I->getOperand(2), NumBits, isLeftShift, IC, DL)); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 315 | return I; |
| 316 | case Instruction::PHI: { |
| 317 | // We can change a phi if we can change all operands. Note that we never |
| 318 | // get into trouble with cyclic PHIs here because we only consider |
| 319 | // instructions with a single use. |
| 320 | PHINode *PN = cast<PHINode>(I); |
| 321 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 322 | PN->setIncomingValue(i, getShiftedValue(PN->getIncomingValue(i), NumBits, |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 323 | isLeftShift, IC, DL)); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 324 | return PN; |
| 325 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 326 | } |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 329 | /// Try to fold (X << C1) << C2, where the shifts are some combination of |
| 330 | /// shl/ashr/lshr. |
| 331 | static Instruction * |
| 332 | foldShiftByConstOfShiftByConst(BinaryOperator &I, ConstantInt *COp1, |
| 333 | InstCombiner::BuilderTy *Builder) { |
| 334 | Value *Op0 = I.getOperand(0); |
| 335 | uint32_t TypeBits = Op0->getType()->getScalarSizeInBits(); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 336 | |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 337 | // Find out if this is a shift of a shift by a constant. |
| 338 | BinaryOperator *ShiftOp = dyn_cast<BinaryOperator>(Op0); |
| 339 | if (ShiftOp && !ShiftOp->isShift()) |
| 340 | ShiftOp = nullptr; |
| 341 | |
| 342 | if (ShiftOp && isa<ConstantInt>(ShiftOp->getOperand(1))) { |
| 343 | |
| 344 | // This is a constant shift of a constant shift. Be careful about hiding |
| 345 | // shl instructions behind bit masks. They are used to represent multiplies |
| 346 | // by a constant, and it is important that simple arithmetic expressions |
| 347 | // are still recognizable by scalar evolution. |
| 348 | // |
| 349 | // The transforms applied to shl are very similar to the transforms applied |
| 350 | // to mul by constant. We can be more aggressive about optimizing right |
| 351 | // shifts. |
| 352 | // |
| 353 | // Combinations of right and left shifts will still be optimized in |
| 354 | // DAGCombine where scalar evolution no longer applies. |
| 355 | |
| 356 | ConstantInt *ShiftAmt1C = cast<ConstantInt>(ShiftOp->getOperand(1)); |
| 357 | uint32_t ShiftAmt1 = ShiftAmt1C->getLimitedValue(TypeBits); |
| 358 | uint32_t ShiftAmt2 = COp1->getLimitedValue(TypeBits); |
| 359 | assert(ShiftAmt2 != 0 && "Should have been simplified earlier"); |
| 360 | if (ShiftAmt1 == 0) |
| 361 | return nullptr; // Will be simplified in the future. |
| 362 | Value *X = ShiftOp->getOperand(0); |
| 363 | |
| 364 | IntegerType *Ty = cast<IntegerType>(I.getType()); |
| 365 | |
| 366 | // Check for (X << c1) << c2 and (X >> c1) >> c2 |
| 367 | if (I.getOpcode() == ShiftOp->getOpcode()) { |
| 368 | uint32_t AmtSum = ShiftAmt1 + ShiftAmt2; // Fold into one big shift. |
| 369 | // If this is an oversized composite shift, then unsigned shifts become |
| 370 | // zero (handled in InstSimplify) and ashr saturates. |
| 371 | if (AmtSum >= TypeBits) { |
| 372 | if (I.getOpcode() != Instruction::AShr) |
| 373 | return nullptr; |
| 374 | AmtSum = TypeBits - 1; // Saturate to 31 for i32 ashr. |
| 375 | } |
| 376 | |
| 377 | return BinaryOperator::Create(I.getOpcode(), X, |
| 378 | ConstantInt::get(Ty, AmtSum)); |
| 379 | } |
| 380 | |
| 381 | if (ShiftAmt1 == ShiftAmt2) { |
| 382 | // If we have ((X << C) >>u C), turn this into X & (-1 >>u C). |
| 383 | if (I.getOpcode() == Instruction::LShr && |
| 384 | ShiftOp->getOpcode() == Instruction::Shl) { |
| 385 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt1)); |
| 386 | return BinaryOperator::CreateAnd( |
| 387 | X, ConstantInt::get(I.getContext(), Mask)); |
| 388 | } |
| 389 | } else if (ShiftAmt1 < ShiftAmt2) { |
| 390 | uint32_t ShiftDiff = ShiftAmt2 - ShiftAmt1; |
| 391 | |
| 392 | // (X >>?,exact C1) << C2 --> X << (C2-C1) |
| 393 | // The inexact version is deferred to DAGCombine so we don't hide shl |
| 394 | // behind a bit mask. |
| 395 | if (I.getOpcode() == Instruction::Shl && |
| 396 | ShiftOp->getOpcode() != Instruction::Shl && ShiftOp->isExact()) { |
| 397 | assert(ShiftOp->getOpcode() == Instruction::LShr || |
| 398 | ShiftOp->getOpcode() == Instruction::AShr); |
| 399 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 400 | BinaryOperator *NewShl = |
| 401 | BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst); |
| 402 | NewShl->setHasNoUnsignedWrap(I.hasNoUnsignedWrap()); |
| 403 | NewShl->setHasNoSignedWrap(I.hasNoSignedWrap()); |
| 404 | return NewShl; |
| 405 | } |
| 406 | |
| 407 | // (X << C1) >>u C2 --> X >>u (C2-C1) & (-1 >> C2) |
| 408 | if (I.getOpcode() == Instruction::LShr && |
| 409 | ShiftOp->getOpcode() == Instruction::Shl) { |
| 410 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 411 | // (X <<nuw C1) >>u C2 --> X >>u (C2-C1) |
| 412 | if (ShiftOp->hasNoUnsignedWrap()) { |
| 413 | BinaryOperator *NewLShr = |
| 414 | BinaryOperator::Create(Instruction::LShr, X, ShiftDiffCst); |
| 415 | NewLShr->setIsExact(I.isExact()); |
| 416 | return NewLShr; |
| 417 | } |
| 418 | Value *Shift = Builder->CreateLShr(X, ShiftDiffCst); |
| 419 | |
| 420 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| 421 | return BinaryOperator::CreateAnd( |
| 422 | Shift, ConstantInt::get(I.getContext(), Mask)); |
| 423 | } |
| 424 | |
| 425 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However, |
| 426 | // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits. |
| 427 | if (I.getOpcode() == Instruction::AShr && |
| 428 | ShiftOp->getOpcode() == Instruction::Shl) { |
| 429 | if (ShiftOp->hasNoSignedWrap()) { |
| 430 | // (X <<nsw C1) >>s C2 --> X >>s (C2-C1) |
| 431 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 432 | BinaryOperator *NewAShr = |
| 433 | BinaryOperator::Create(Instruction::AShr, X, ShiftDiffCst); |
| 434 | NewAShr->setIsExact(I.isExact()); |
| 435 | return NewAShr; |
| 436 | } |
| 437 | } |
| 438 | } else { |
| 439 | assert(ShiftAmt2 < ShiftAmt1); |
| 440 | uint32_t ShiftDiff = ShiftAmt1 - ShiftAmt2; |
| 441 | |
| 442 | // (X >>?exact C1) << C2 --> X >>?exact (C1-C2) |
| 443 | // The inexact version is deferred to DAGCombine so we don't hide shl |
| 444 | // behind a bit mask. |
| 445 | if (I.getOpcode() == Instruction::Shl && |
| 446 | ShiftOp->getOpcode() != Instruction::Shl && ShiftOp->isExact()) { |
| 447 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 448 | BinaryOperator *NewShr = |
| 449 | BinaryOperator::Create(ShiftOp->getOpcode(), X, ShiftDiffCst); |
| 450 | NewShr->setIsExact(true); |
| 451 | return NewShr; |
| 452 | } |
| 453 | |
| 454 | // (X << C1) >>u C2 --> X << (C1-C2) & (-1 >> C2) |
| 455 | if (I.getOpcode() == Instruction::LShr && |
| 456 | ShiftOp->getOpcode() == Instruction::Shl) { |
| 457 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 458 | if (ShiftOp->hasNoUnsignedWrap()) { |
| 459 | // (X <<nuw C1) >>u C2 --> X <<nuw (C1-C2) |
| 460 | BinaryOperator *NewShl = |
| 461 | BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst); |
| 462 | NewShl->setHasNoUnsignedWrap(true); |
| 463 | return NewShl; |
| 464 | } |
| 465 | Value *Shift = Builder->CreateShl(X, ShiftDiffCst); |
| 466 | |
| 467 | APInt Mask(APInt::getLowBitsSet(TypeBits, TypeBits - ShiftAmt2)); |
| 468 | return BinaryOperator::CreateAnd( |
| 469 | Shift, ConstantInt::get(I.getContext(), Mask)); |
| 470 | } |
| 471 | |
| 472 | // We can't handle (X << C1) >>s C2, it shifts arbitrary bits in. However, |
| 473 | // we can handle (X <<nsw C1) >>s C2 since it only shifts in sign bits. |
| 474 | if (I.getOpcode() == Instruction::AShr && |
| 475 | ShiftOp->getOpcode() == Instruction::Shl) { |
| 476 | if (ShiftOp->hasNoSignedWrap()) { |
| 477 | // (X <<nsw C1) >>s C2 --> X <<nsw (C1-C2) |
| 478 | ConstantInt *ShiftDiffCst = ConstantInt::get(Ty, ShiftDiff); |
| 479 | BinaryOperator *NewShl = |
| 480 | BinaryOperator::Create(Instruction::Shl, X, ShiftDiffCst); |
| 481 | NewShl->setHasNoSignedWrap(true); |
| 482 | return NewShl; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | return nullptr; |
| 489 | } |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 490 | |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 491 | Instruction *InstCombiner::FoldShiftByConstant(Value *Op0, Constant *Op1, |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 492 | BinaryOperator &I) { |
| 493 | bool isLeftShift = I.getOpcode() == Instruction::Shl; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 494 | |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 495 | ConstantInt *COp1 = nullptr; |
| 496 | if (ConstantDataVector *CV = dyn_cast<ConstantDataVector>(Op1)) |
| 497 | COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()); |
| 498 | else if (ConstantVector *CV = dyn_cast<ConstantVector>(Op1)) |
| 499 | COp1 = dyn_cast_or_null<ConstantInt>(CV->getSplatValue()); |
| 500 | else |
| 501 | COp1 = dyn_cast<ConstantInt>(Op1); |
| 502 | |
| 503 | if (!COp1) |
| 504 | return nullptr; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 505 | |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 506 | // See if we can propagate this shift into the input, this covers the trivial |
| 507 | // cast of lshr(shl(x,c1),c2) as well as other more complex cases. |
| 508 | if (I.getOpcode() != Instruction::AShr && |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 509 | canEvaluateShifted(Op0, COp1->getZExtValue(), isLeftShift, *this, &I)) { |
Chris Lattner | dd66010 | 2010-08-28 01:20:38 +0000 | [diff] [blame] | 510 | DEBUG(dbgs() << "ICE: GetShiftedValue propagating shift through expression" |
| 511 | " to eliminate shift:\n IN: " << *Op0 << "\n SH: " << I <<"\n"); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 512 | |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 513 | return replaceInstUsesWith( |
Sanjay Patel | 20aaf58 | 2017-01-15 17:55:35 +0000 | [diff] [blame^] | 514 | I, getShiftedValue(Op0, COp1->getZExtValue(), isLeftShift, *this, DL)); |
Chris Lattner | 18d7fc8 | 2010-08-27 22:24:38 +0000 | [diff] [blame] | 515 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 516 | |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 517 | // See if we can simplify any instructions used by the instruction whose sole |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 518 | // purpose is to compute bits we don't care about. |
| 519 | uint32_t TypeBits = Op0->getType()->getScalarSizeInBits(); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 520 | |
Matt Arsenault | 6b4bed4 | 2014-04-23 16:48:40 +0000 | [diff] [blame] | 521 | assert(!COp1->uge(TypeBits) && |
| 522 | "Shift over the type width should have been removed already"); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 523 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 524 | // ((X*C1) << C2) == (X * (C1 << C2)) |
| 525 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(Op0)) |
| 526 | if (BO->getOpcode() == Instruction::Mul && isLeftShift) |
| 527 | if (Constant *BOOp = dyn_cast<Constant>(BO->getOperand(1))) |
| 528 | return BinaryOperator::CreateMul(BO->getOperand(0), |
Sanjay Patel | 690955f | 2016-01-31 16:34:11 +0000 | [diff] [blame] | 529 | ConstantExpr::getShl(BOOp, Op1)); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 530 | |
Sanjay Patel | db0938f | 2017-01-10 23:49:07 +0000 | [diff] [blame] | 531 | if (Instruction *FoldedShift = foldOpWithConstantIntoOperand(I)) |
| 532 | return FoldedShift; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 533 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 534 | // Fold shift2(trunc(shift1(x,c1)), c2) -> trunc(shift2(shift1(x,c1),c2)) |
| 535 | if (TruncInst *TI = dyn_cast<TruncInst>(Op0)) { |
| 536 | Instruction *TrOp = dyn_cast<Instruction>(TI->getOperand(0)); |
| 537 | // If 'shift2' is an ashr, we would have to get the sign bit into a funny |
| 538 | // place. Don't try to do this transformation in this case. Also, we |
| 539 | // require that the input operand is a shift-by-constant so that we have |
| 540 | // confidence that the shifts will get folded together. We could do this |
| 541 | // xform in more cases, but it is unlikely to be profitable. |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 542 | if (TrOp && I.isLogicalShift() && TrOp->isShift() && |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 543 | isa<ConstantInt>(TrOp->getOperand(1))) { |
| 544 | // Okay, we'll do this xform. Make the shift of shift. |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 545 | Constant *ShAmt = ConstantExpr::getZExt(COp1, TrOp->getType()); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 546 | // (shift2 (shift1 & 0x00FF), c2) |
| 547 | Value *NSh = Builder->CreateBinOp(I.getOpcode(), TrOp, ShAmt,I.getName()); |
| 548 | |
| 549 | // For logical shifts, the truncation has the effect of making the high |
| 550 | // part of the register be zeros. Emulate this by inserting an AND to |
| 551 | // clear the top bits as needed. This 'and' will usually be zapped by |
| 552 | // other xforms later if dead. |
| 553 | unsigned SrcSize = TrOp->getType()->getScalarSizeInBits(); |
| 554 | unsigned DstSize = TI->getType()->getScalarSizeInBits(); |
| 555 | APInt MaskV(APInt::getLowBitsSet(SrcSize, DstSize)); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 556 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 557 | // The mask we constructed says what the trunc would do if occurring |
| 558 | // between the shifts. We want to know the effect *after* the second |
| 559 | // shift. We know that it is a logical shift by a constant, so adjust the |
| 560 | // mask as appropriate. |
| 561 | if (I.getOpcode() == Instruction::Shl) |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 562 | MaskV <<= COp1->getZExtValue(); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 563 | else { |
| 564 | assert(I.getOpcode() == Instruction::LShr && "Unknown logical shift"); |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 565 | MaskV = MaskV.lshr(COp1->getZExtValue()); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | // shift1 & 0x00FF |
| 569 | Value *And = Builder->CreateAnd(NSh, |
| 570 | ConstantInt::get(I.getContext(), MaskV), |
| 571 | TI->getName()); |
| 572 | |
| 573 | // Return the value truncated to the interesting size. |
| 574 | return new TruncInst(And, I.getType()); |
| 575 | } |
| 576 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 577 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 578 | if (Op0->hasOneUse()) { |
| 579 | if (BinaryOperator *Op0BO = dyn_cast<BinaryOperator>(Op0)) { |
| 580 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 581 | Value *V1, *V2; |
| 582 | ConstantInt *CC; |
| 583 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 584 | default: break; |
| 585 | case Instruction::Add: |
| 586 | case Instruction::And: |
| 587 | case Instruction::Or: |
| 588 | case Instruction::Xor: { |
| 589 | // These operators commute. |
| 590 | // Turn (Y + (X >> C)) << C -> (X + (Y << C)) & (~0 << C) |
| 591 | if (isLeftShift && Op0BO->getOperand(1)->hasOneUse() && |
| 592 | match(Op0BO->getOperand(1), m_Shr(m_Value(V1), |
| 593 | m_Specific(Op1)))) { |
| 594 | Value *YS = // (Y << C) |
| 595 | Builder->CreateShl(Op0BO->getOperand(0), Op1, Op0BO->getName()); |
| 596 | // (X + (Y << C)) |
| 597 | Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), YS, V1, |
| 598 | Op0BO->getOperand(1)->getName()); |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 599 | uint32_t Op1Val = COp1->getLimitedValue(TypeBits); |
| 600 | |
| 601 | APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val); |
| 602 | Constant *Mask = ConstantInt::get(I.getContext(), Bits); |
| 603 | if (VectorType *VT = dyn_cast<VectorType>(X->getType())) |
| 604 | Mask = ConstantVector::getSplat(VT->getNumElements(), Mask); |
| 605 | return BinaryOperator::CreateAnd(X, Mask); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 606 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 607 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 608 | // Turn (Y + ((X >> C) & CC)) << C -> ((X & (CC << C)) + (Y << C)) |
| 609 | Value *Op0BOOp1 = Op0BO->getOperand(1); |
| 610 | if (isLeftShift && Op0BOOp1->hasOneUse() && |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 611 | match(Op0BOOp1, |
Jakub Staszak | 8432185 | 2012-12-09 16:06:44 +0000 | [diff] [blame] | 612 | m_And(m_OneUse(m_Shr(m_Value(V1), m_Specific(Op1))), |
| 613 | m_ConstantInt(CC)))) { |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 614 | Value *YS = // (Y << C) |
| 615 | Builder->CreateShl(Op0BO->getOperand(0), Op1, |
| 616 | Op0BO->getName()); |
| 617 | // X & (CC << C) |
| 618 | Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
| 619 | V1->getName()+".mask"); |
| 620 | return BinaryOperator::Create(Op0BO->getOpcode(), YS, XM); |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 621 | } |
Justin Bogner | cd1d5aa | 2016-08-17 20:30:52 +0000 | [diff] [blame] | 622 | LLVM_FALLTHROUGH; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 623 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 624 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 625 | case Instruction::Sub: { |
| 626 | // Turn ((X >> C) + Y) << C -> (X + (Y << C)) & (~0 << C) |
| 627 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 628 | match(Op0BO->getOperand(0), m_Shr(m_Value(V1), |
| 629 | m_Specific(Op1)))) { |
| 630 | Value *YS = // (Y << C) |
| 631 | Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName()); |
| 632 | // (X + (Y << C)) |
| 633 | Value *X = Builder->CreateBinOp(Op0BO->getOpcode(), V1, YS, |
| 634 | Op0BO->getOperand(0)->getName()); |
Matt Arsenault | fed3dc8 | 2014-04-14 21:50:37 +0000 | [diff] [blame] | 635 | uint32_t Op1Val = COp1->getLimitedValue(TypeBits); |
| 636 | |
| 637 | APInt Bits = APInt::getHighBitsSet(TypeBits, TypeBits - Op1Val); |
| 638 | Constant *Mask = ConstantInt::get(I.getContext(), Bits); |
| 639 | if (VectorType *VT = dyn_cast<VectorType>(X->getType())) |
| 640 | Mask = ConstantVector::getSplat(VT->getNumElements(), Mask); |
| 641 | return BinaryOperator::CreateAnd(X, Mask); |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 642 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 643 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 644 | // Turn (((X >> C)&CC) + Y) << C -> (X + (Y << C)) & (CC << C) |
| 645 | if (isLeftShift && Op0BO->getOperand(0)->hasOneUse() && |
| 646 | match(Op0BO->getOperand(0), |
Jakub Staszak | 8432185 | 2012-12-09 16:06:44 +0000 | [diff] [blame] | 647 | m_And(m_OneUse(m_Shr(m_Value(V1), m_Value(V2))), |
| 648 | m_ConstantInt(CC))) && V2 == Op1) { |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 649 | Value *YS = // (Y << C) |
| 650 | Builder->CreateShl(Op0BO->getOperand(1), Op1, Op0BO->getName()); |
| 651 | // X & (CC << C) |
| 652 | Value *XM = Builder->CreateAnd(V1, ConstantExpr::getShl(CC, Op1), |
| 653 | V1->getName()+".mask"); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 654 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 655 | return BinaryOperator::Create(Op0BO->getOpcode(), XM, YS); |
| 656 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 657 | |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 658 | break; |
| 659 | } |
| 660 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 661 | |
| 662 | |
Sanjay Patel | fc3d8f0 | 2014-07-22 04:57:06 +0000 | [diff] [blame] | 663 | // If the operand is a bitwise operator with a constant RHS, and the |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 664 | // shift is the only use, we can pull it out of the shift. |
| 665 | if (ConstantInt *Op0C = dyn_cast<ConstantInt>(Op0BO->getOperand(1))) { |
| 666 | bool isValid = true; // Valid only for And, Or, Xor |
| 667 | bool highBitSet = false; // Transform if high bit of constant set? |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 668 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 669 | switch (Op0BO->getOpcode()) { |
Chris Lattner | 2b459fe | 2010-01-10 06:59:55 +0000 | [diff] [blame] | 670 | default: isValid = false; break; // Do not perform transform! |
| 671 | case Instruction::Add: |
| 672 | isValid = isLeftShift; |
| 673 | break; |
| 674 | case Instruction::Or: |
| 675 | case Instruction::Xor: |
| 676 | highBitSet = false; |
| 677 | break; |
| 678 | case Instruction::And: |
| 679 | highBitSet = true; |
| 680 | break; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 681 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 682 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 683 | // If this is a signed shift right, and the high bit is modified |
| 684 | // by the logical operation, do not perform the transformation. |
| 685 | // The highBitSet boolean indicates the value of the high bit of |
| 686 | // the constant which would cause it to be modified for this |
| 687 | // operation. |
| 688 | // |
| 689 | if (isValid && I.getOpcode() == Instruction::AShr) |
| 690 | isValid = Op0C->getValue()[TypeBits-1] == highBitSet; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 691 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 692 | if (isValid) { |
| 693 | Constant *NewRHS = ConstantExpr::get(I.getOpcode(), Op0C, Op1); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 694 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 695 | Value *NewShift = |
| 696 | Builder->CreateBinOp(I.getOpcode(), Op0BO->getOperand(0), Op1); |
| 697 | NewShift->takeName(Op0BO); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 698 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 699 | return BinaryOperator::Create(Op0BO->getOpcode(), NewShift, |
| 700 | NewRHS); |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 705 | |
Sanjay Patel | 4c48bbe | 2016-12-10 22:16:29 +0000 | [diff] [blame] | 706 | if (Instruction *Folded = foldShiftByConstOfShiftByConst(I, COp1, Builder)) |
| 707 | return Folded; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 708 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 709 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | Instruction *InstCombiner::visitShl(BinaryOperator &I) { |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 713 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 714 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 715 | |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 716 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 717 | if (Value *V = SimplifyShlInst(Op0, Op1, I.hasNoSignedWrap(), |
| 718 | I.hasNoUnsignedWrap(), DL, &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 719 | return replaceInstUsesWith(I, V); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 720 | |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 721 | if (Instruction *V = commonShiftTransforms(I)) |
| 722 | return V; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 723 | |
Sanjay Patel | b22f6c5 | 2017-01-13 18:39:09 +0000 | [diff] [blame] | 724 | const APInt *ShAmtAPInt; |
| 725 | if (match(Op1, m_APInt(ShAmtAPInt))) { |
| 726 | unsigned ShAmt = ShAmtAPInt->getZExtValue(); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 727 | |
Sanjay Patel | acd24c7 | 2017-01-13 18:52:10 +0000 | [diff] [blame] | 728 | // shl (zext X), ShAmt --> zext (shl X, ShAmt) |
| 729 | // This is only valid if X would have zeros shifted out. |
| 730 | Value *X; |
| 731 | if (match(Op0, m_ZExt(m_Value(X)))) { |
| 732 | unsigned SrcWidth = X->getType()->getScalarSizeInBits(); |
| 733 | if (ShAmt < SrcWidth && |
| 734 | MaskedValueIsZero(X, APInt::getHighBitsSet(SrcWidth, ShAmt), 0, &I)) |
| 735 | return new ZExtInst(Builder->CreateShl(X, ShAmt), I.getType()); |
David Majnemer | cb892e9 | 2017-01-04 02:21:34 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 738 | // If the shifted-out value is known-zero, then this is a NUW shift. |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 739 | if (!I.hasNoUnsignedWrap() && |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 740 | MaskedValueIsZero( |
Sanjay Patel | b22f6c5 | 2017-01-13 18:39:09 +0000 | [diff] [blame] | 741 | Op0, APInt::getHighBitsSet(ShAmtAPInt->getBitWidth(), ShAmt), 0, |
| 742 | &I)) { |
Sanjay Patel | 690955f | 2016-01-31 16:34:11 +0000 | [diff] [blame] | 743 | I.setHasNoUnsignedWrap(); |
| 744 | return &I; |
| 745 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 746 | |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 747 | // If the shifted-out value is all signbits, then this is a NSW shift. |
| 748 | if (!I.hasNoSignedWrap() && ComputeNumSignBits(Op0, 0, &I) > ShAmt) { |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 749 | I.setHasNoSignedWrap(); |
| 750 | return &I; |
| 751 | } |
| 752 | } |
Benjamin Kramer | 16f18ed | 2011-04-29 08:15:41 +0000 | [diff] [blame] | 753 | |
Benjamin Kramer | f0e3f04 | 2011-04-29 08:41:23 +0000 | [diff] [blame] | 754 | // (C1 << A) << C2 -> (C1 << C2) << A |
Benjamin Kramer | 16f18ed | 2011-04-29 08:15:41 +0000 | [diff] [blame] | 755 | Constant *C1, *C2; |
| 756 | Value *A; |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 757 | if (match(Op0, m_OneUse(m_Shl(m_Constant(C1), m_Value(A)))) && |
| 758 | match(Op1, m_Constant(C2))) |
Benjamin Kramer | 16f18ed | 2011-04-29 08:15:41 +0000 | [diff] [blame] | 759 | return BinaryOperator::CreateShl(ConstantExpr::getShl(C1, C2), A); |
| 760 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 761 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | Instruction *InstCombiner::visitLShr(BinaryOperator &I) { |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 765 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 766 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 767 | |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 768 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 769 | if (Value *V = SimplifyLShrInst(Op0, Op1, I.isExact(), DL, &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 770 | return replaceInstUsesWith(I, V); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 771 | |
Chris Lattner | 249da5c | 2010-01-23 18:49:30 +0000 | [diff] [blame] | 772 | if (Instruction *R = commonShiftTransforms(I)) |
| 773 | return R; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 774 | |
Sanjay Patel | 2d4b456 | 2017-01-13 23:04:10 +0000 | [diff] [blame] | 775 | const APInt *ShAmtAPInt; |
| 776 | if (match(Op1, m_APInt(ShAmtAPInt))) { |
| 777 | unsigned ShAmt = ShAmtAPInt->getZExtValue(); |
| 778 | unsigned BitWidth = Op0->getType()->getScalarSizeInBits(); |
| 779 | auto *II = dyn_cast<IntrinsicInst>(Op0); |
| 780 | if (II && isPowerOf2_32(BitWidth) && Log2_32(BitWidth) == ShAmt && |
| 781 | (II->getIntrinsicID() == Intrinsic::ctlz || |
| 782 | II->getIntrinsicID() == Intrinsic::cttz || |
| 783 | II->getIntrinsicID() == Intrinsic::ctpop)) { |
Chris Lattner | 249da5c | 2010-01-23 18:49:30 +0000 | [diff] [blame] | 784 | // ctlz.i32(x)>>5 --> zext(x == 0) |
| 785 | // cttz.i32(x)>>5 --> zext(x == 0) |
| 786 | // ctpop.i32(x)>>5 --> zext(x == -1) |
Sanjay Patel | 2d4b456 | 2017-01-13 23:04:10 +0000 | [diff] [blame] | 787 | bool IsPop = II->getIntrinsicID() == Intrinsic::ctpop; |
| 788 | Constant *RHS = ConstantInt::getSigned(Op0->getType(), IsPop ? -1 : 0); |
| 789 | Value *Cmp = Builder->CreateICmpEQ(II->getArgOperand(0), RHS); |
| 790 | return new ZExtInst(Cmp, II->getType()); |
Chris Lattner | 249da5c | 2010-01-23 18:49:30 +0000 | [diff] [blame] | 791 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 792 | |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 793 | // If the shifted-out value is known-zero, then this is an exact shift. |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 794 | if (!I.isExact() && |
Sanjay Patel | 2d4b456 | 2017-01-13 23:04:10 +0000 | [diff] [blame] | 795 | MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) { |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 796 | I.setIsExact(); |
| 797 | return &I; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 798 | } |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 799 | } |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 800 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | Instruction *InstCombiner::visitAShr(BinaryOperator &I) { |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 804 | if (Value *V = SimplifyVectorOp(I)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 805 | return replaceInstUsesWith(I, V); |
Serge Pavlov | 9ef66a8 | 2014-05-11 08:46:12 +0000 | [diff] [blame] | 806 | |
Sanjay Patel | cf08203 | 2017-01-13 18:08:25 +0000 | [diff] [blame] | 807 | Value *Op0 = I.getOperand(0), *Op1 = I.getOperand(1); |
| 808 | if (Value *V = SimplifyAShrInst(Op0, Op1, I.isExact(), DL, &TLI, &DT, &AC)) |
Sanjay Patel | 4b19880 | 2016-02-01 22:23:39 +0000 | [diff] [blame] | 809 | return replaceInstUsesWith(I, V); |
Duncan Sands | 7f60dc1 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 810 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 811 | if (Instruction *R = commonShiftTransforms(I)) |
| 812 | return R; |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 813 | |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 814 | unsigned BitWidth = I.getType()->getScalarSizeInBits(); |
Sanjay Patel | 5f8451a | 2017-01-15 16:38:19 +0000 | [diff] [blame] | 815 | const APInt *ShAmtAPInt; |
| 816 | if (match(Op1, m_APInt(ShAmtAPInt))) { |
| 817 | unsigned ShAmt = ShAmtAPInt->getZExtValue(); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 818 | |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 819 | // If the shift amount equals the difference in width of the destination |
Sanjay Patel | 5f8451a | 2017-01-15 16:38:19 +0000 | [diff] [blame] | 820 | // and source scalar types: |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 821 | // ashr (shl (zext X), C), C --> sext X |
Chris Lattner | a1e223e | 2010-01-08 19:04:21 +0000 | [diff] [blame] | 822 | Value *X; |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 823 | if (match(Op0, m_Shl(m_ZExt(m_Value(X)), m_Specific(Op1))) && |
| 824 | ShAmt == BitWidth - X->getType()->getScalarSizeInBits()) |
| 825 | return new SExtInst(X, I.getType()); |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 826 | |
| 827 | // If the shifted-out value is known-zero, then this is an exact shift. |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 828 | if (!I.isExact() && |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 829 | MaskedValueIsZero(Op0, APInt::getLowBitsSet(BitWidth, ShAmt), 0, &I)) { |
Chris Lattner | 6b657ae | 2011-02-10 05:36:31 +0000 | [diff] [blame] | 830 | I.setIsExact(); |
| 831 | return &I; |
| 832 | } |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 835 | // See if we can turn a signed shr into an unsigned shr. |
Sanjay Patel | ca3124f | 2017-01-14 23:13:50 +0000 | [diff] [blame] | 836 | if (MaskedValueIsZero(Op0, APInt::getSignBit(BitWidth), 0, &I)) |
Chris Lattner | a1e223e | 2010-01-08 19:04:21 +0000 | [diff] [blame] | 837 | return BinaryOperator::CreateLShr(Op0, Op1); |
Jakub Staszak | 538e386 | 2012-12-09 15:37:46 +0000 | [diff] [blame] | 838 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 839 | return nullptr; |
Chris Lattner | dc67e13 | 2010-01-05 07:44:46 +0000 | [diff] [blame] | 840 | } |