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