Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1 | //===- InstCombineVectorOps.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 instcombine for ExtractElement, InsertElement and |
| 11 | // ShuffleVector. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "InstCombine.h" |
| 16 | using namespace llvm; |
| 17 | |
| 18 | /// CheapToScalarize - Return true if the value is cheaper to scalarize than it |
| 19 | /// is to leave as a vector operation. |
| 20 | static bool CheapToScalarize(Value *V, bool isConstant) { |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 21 | if (isa<ConstantAggregateZero>(V)) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 22 | return true; |
| 23 | if (ConstantVector *C = dyn_cast<ConstantVector>(V)) { |
| 24 | if (isConstant) return true; |
| 25 | // If all elts are the same, we can extract. |
| 26 | Constant *Op0 = C->getOperand(0); |
| 27 | for (unsigned i = 1; i < C->getNumOperands(); ++i) |
| 28 | if (C->getOperand(i) != Op0) |
| 29 | return false; |
| 30 | return true; |
| 31 | } |
| 32 | Instruction *I = dyn_cast<Instruction>(V); |
| 33 | if (!I) return false; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 34 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 35 | // Insert element gets simplified to the inserted element or is deleted if |
| 36 | // this is constant idx extract element and its a constant idx insertelt. |
| 37 | if (I->getOpcode() == Instruction::InsertElement && isConstant && |
| 38 | isa<ConstantInt>(I->getOperand(2))) |
| 39 | return true; |
| 40 | if (I->getOpcode() == Instruction::Load && I->hasOneUse()) |
| 41 | return true; |
| 42 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) |
| 43 | if (BO->hasOneUse() && |
| 44 | (CheapToScalarize(BO->getOperand(0), isConstant) || |
| 45 | CheapToScalarize(BO->getOperand(1), isConstant))) |
| 46 | return true; |
| 47 | if (CmpInst *CI = dyn_cast<CmpInst>(I)) |
| 48 | if (CI->hasOneUse() && |
| 49 | (CheapToScalarize(CI->getOperand(0), isConstant) || |
| 50 | CheapToScalarize(CI->getOperand(1), isConstant))) |
| 51 | return true; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 52 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 56 | /// getShuffleMask - Read and decode a shufflevector mask. |
| 57 | /// Turn undef elements into negative values. |
| 58 | static std::vector<int> getShuffleMask(const ShuffleVectorInst *SVI) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 59 | unsigned NElts = SVI->getType()->getNumElements(); |
| 60 | if (isa<ConstantAggregateZero>(SVI->getOperand(2))) |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 61 | return std::vector<int>(NElts, 0); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 62 | if (isa<UndefValue>(SVI->getOperand(2))) |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 63 | return std::vector<int>(NElts, -1); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 64 | |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 65 | std::vector<int> Result; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 66 | const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2)); |
| 67 | for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i) |
| 68 | if (isa<UndefValue>(*i)) |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 69 | Result.push_back(-1); // undef |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 70 | else |
| 71 | Result.push_back(cast<ConstantInt>(*i)->getZExtValue()); |
| 72 | return Result; |
| 73 | } |
| 74 | |
| 75 | /// FindScalarElement - Given a vector and an element number, see if the scalar |
| 76 | /// value is already around as a register, for example if it were inserted then |
| 77 | /// extracted from the vector. |
| 78 | static Value *FindScalarElement(Value *V, unsigned EltNo) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 79 | assert(V->getType()->isVectorTy() && "Not looking at a vector?"); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 80 | VectorType *PTy = cast<VectorType>(V->getType()); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 81 | unsigned Width = PTy->getNumElements(); |
| 82 | if (EltNo >= Width) // Out of range access. |
| 83 | return UndefValue::get(PTy->getElementType()); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 84 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 85 | if (isa<UndefValue>(V)) |
| 86 | return UndefValue::get(PTy->getElementType()); |
Chris Lattner | 841af4f | 2010-01-05 05:42:08 +0000 | [diff] [blame] | 87 | if (isa<ConstantAggregateZero>(V)) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 88 | return Constant::getNullValue(PTy->getElementType()); |
Chris Lattner | 841af4f | 2010-01-05 05:42:08 +0000 | [diff] [blame] | 89 | if (ConstantVector *CP = dyn_cast<ConstantVector>(V)) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 90 | return CP->getOperand(EltNo); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 841af4f | 2010-01-05 05:42:08 +0000 | [diff] [blame] | 92 | if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 93 | // If this is an insert to a variable element, we don't know what it is. |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 94 | if (!isa<ConstantInt>(III->getOperand(2))) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 95 | return 0; |
| 96 | unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 97 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 98 | // If this is an insert to the element we are looking for, return the |
| 99 | // inserted value. |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 100 | if (EltNo == IIElt) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 101 | return III->getOperand(1); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 102 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 103 | // Otherwise, the insertelement doesn't modify the value, recurse on its |
| 104 | // vector input. |
| 105 | return FindScalarElement(III->getOperand(0), EltNo); |
Chris Lattner | 841af4f | 2010-01-05 05:42:08 +0000 | [diff] [blame] | 106 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 841af4f | 2010-01-05 05:42:08 +0000 | [diff] [blame] | 108 | if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 109 | unsigned LHSWidth = |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 110 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
| 111 | int InEl = getShuffleMask(SVI)[EltNo]; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 112 | if (InEl < 0) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 113 | return UndefValue::get(PTy->getElementType()); |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 114 | if (InEl < (int)LHSWidth) |
| 115 | return FindScalarElement(SVI->getOperand(0), InEl); |
| 116 | return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 117 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 118 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 119 | // Otherwise, we don't know. |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
| 124 | // If vector val is undef, replace extract with scalar undef. |
| 125 | if (isa<UndefValue>(EI.getOperand(0))) |
| 126 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 127 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 128 | // If vector val is constant 0, replace extract with scalar 0. |
| 129 | if (isa<ConstantAggregateZero>(EI.getOperand(0))) |
| 130 | return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 131 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 132 | if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) { |
| 133 | // If vector val is constant with all elements the same, replace EI with |
| 134 | // that element. When the elements are not identical, we cannot replace yet |
| 135 | // (we do that below, but only when the index is constant). |
| 136 | Constant *op0 = C->getOperand(0); |
| 137 | for (unsigned i = 1; i != C->getNumOperands(); ++i) |
| 138 | if (C->getOperand(i) != op0) { |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 139 | op0 = 0; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | if (op0) |
| 143 | return ReplaceInstUsesWith(EI, op0); |
| 144 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 145 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 146 | // If extracting a specified index from the vector, see if we can recursively |
| 147 | // find a previously computed scalar that was inserted into the vector. |
| 148 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 149 | unsigned IndexVal = IdxC->getZExtValue(); |
| 150 | unsigned VectorWidth = EI.getVectorOperandType()->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 151 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 152 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 153 | // crashing the code below. |
| 154 | if (IndexVal >= VectorWidth) |
| 155 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 156 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 157 | // This instruction only demands the single element from the input vector. |
| 158 | // If the input vector has a single use, simplify it based on this use |
| 159 | // property. |
| 160 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
| 161 | APInt UndefElts(VectorWidth, 0); |
Chris Lattner | b22423c | 2010-02-08 23:56:03 +0000 | [diff] [blame] | 162 | APInt DemandedMask(VectorWidth, 0); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 163 | DemandedMask.setBit(IndexVal); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 164 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
| 165 | DemandedMask, UndefElts)) { |
| 166 | EI.setOperand(0, V); |
| 167 | return &EI; |
| 168 | } |
| 169 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 170 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 171 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
| 172 | return ReplaceInstUsesWith(EI, Elt); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 173 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 174 | // If the this extractelement is directly using a bitcast from a vector of |
| 175 | // the same number of elements, see if we can find the source element from |
| 176 | // it. In this case, we will end up needing to bitcast the scalars. |
| 177 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 178 | if (VectorType *VT = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 179 | dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
| 180 | if (VT->getNumElements() == VectorWidth) |
| 181 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 182 | return new BitCastInst(Elt, EI.getType()); |
| 183 | } |
| 184 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 185 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 186 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
| 187 | // Push extractelement into predecessor operation if legal and |
| 188 | // profitable to do so |
| 189 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 190 | if (I->hasOneUse() && |
| 191 | CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) { |
| 192 | Value *newEI0 = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 193 | Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1), |
| 194 | EI.getName()+".lhs"); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 195 | Value *newEI1 = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 196 | Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1), |
| 197 | EI.getName()+".rhs"); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 198 | return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); |
| 199 | } |
| 200 | } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 201 | // Extracting the inserted element? |
| 202 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 203 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 204 | // If the inserted and extracted elements are constants, they must not |
| 205 | // be the same value, extract from the pre-inserted value instead. |
| 206 | if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) { |
| 207 | Worklist.AddValue(EI.getOperand(0)); |
| 208 | EI.setOperand(0, IE->getOperand(0)); |
| 209 | return &EI; |
| 210 | } |
| 211 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 212 | // If this is extracting an element from a shufflevector, figure out where |
| 213 | // it came from and extract from the appropriate input element instead. |
| 214 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 215 | int SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()]; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 216 | Value *Src; |
| 217 | unsigned LHSWidth = |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 218 | cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 219 | |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 220 | if (SrcIdx < 0) |
| 221 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 222 | if (SrcIdx < (int)LHSWidth) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 223 | Src = SVI->getOperand(0); |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 224 | else { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 225 | SrcIdx -= LHSWidth; |
| 226 | Src = SVI->getOperand(1); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 227 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 228 | Type *Int32Ty = Type::getInt32Ty(EI.getContext()); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 229 | return ExtractElementInst::Create(Src, |
Bob Wilson | 9d07f39 | 2010-10-29 22:03:07 +0000 | [diff] [blame] | 230 | ConstantInt::get(Int32Ty, |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 231 | SrcIdx, false)); |
| 232 | } |
Nadav Rotem | d74b72b | 2011-03-31 22:57:29 +0000 | [diff] [blame] | 233 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 234 | // Canonicalize extractelement(cast) -> cast(extractelement) |
| 235 | // bitcasts can change the number of vector elements and they cost nothing |
| 236 | if (CI->hasOneUse() && EI.hasOneUse() && |
| 237 | (CI->getOpcode() != Instruction::BitCast)) { |
| 238 | Value *EE = Builder->CreateExtractElement(CI->getOperand(0), |
| 239 | EI.getIndexOperand()); |
| 240 | return CastInst::Create(CI->getOpcode(), EE, EI.getType()); |
| 241 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 242 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 243 | } |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 248 | /// elements from either LHS or RHS, return the shuffle mask and true. |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 249 | /// Otherwise, return false. |
| 250 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
| 251 | std::vector<Constant*> &Mask) { |
| 252 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 253 | "Invalid CollectSingleShuffleElements"); |
| 254 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 255 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 256 | if (isa<UndefValue>(V)) { |
| 257 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
| 258 | return true; |
| 259 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 260 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 261 | if (V == LHS) { |
| 262 | for (unsigned i = 0; i != NumElts; ++i) |
| 263 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
| 264 | return true; |
| 265 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 266 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 267 | if (V == RHS) { |
| 268 | for (unsigned i = 0; i != NumElts; ++i) |
| 269 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 270 | i+NumElts)); |
| 271 | return true; |
| 272 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 273 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 274 | if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 275 | // If this is an insert of an extract from some other vector, include it. |
| 276 | Value *VecOp = IEI->getOperand(0); |
| 277 | Value *ScalarOp = IEI->getOperand(1); |
| 278 | Value *IdxOp = IEI->getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 279 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 280 | if (!isa<ConstantInt>(IdxOp)) |
| 281 | return false; |
| 282 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 283 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 284 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 285 | // Okay, we can handle this if the vector we are insertinting into is |
| 286 | // transitively ok. |
| 287 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 288 | // If so, update the mask to reflect the inserted undef. |
| 289 | Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext())); |
| 290 | return true; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 291 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 292 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 293 | if (isa<ConstantInt>(EI->getOperand(1)) && |
| 294 | EI->getOperand(0)->getType() == V->getType()) { |
| 295 | unsigned ExtractedIdx = |
| 296 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 297 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 298 | // This must be extracting from either LHS or RHS. |
| 299 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 300 | // Okay, we can handle this if the vector we are insertinting into is |
| 301 | // transitively ok. |
| 302 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 303 | // If so, update the mask to reflect the inserted value. |
| 304 | if (EI->getOperand(0) == LHS) { |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 305 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 306 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 307 | ExtractedIdx); |
| 308 | } else { |
| 309 | assert(EI->getOperand(0) == RHS); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 310 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 311 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 312 | ExtractedIdx+NumElts); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 313 | } |
| 314 | return true; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | // TODO: Handle shufflevector here! |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 321 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 322 | return false; |
| 323 | } |
| 324 | |
| 325 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 326 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 327 | /// that computes V and the LHS value of the shuffle. |
| 328 | static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask, |
| 329 | Value *&RHS) { |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 330 | assert(V->getType()->isVectorTy() && |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 331 | (RHS == 0 || V->getType() == RHS->getType()) && |
| 332 | "Invalid shuffle!"); |
| 333 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 334 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 335 | if (isa<UndefValue>(V)) { |
| 336 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
| 337 | return V; |
| 338 | } else if (isa<ConstantAggregateZero>(V)) { |
| 339 | Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0)); |
| 340 | return V; |
| 341 | } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 342 | // If this is an insert of an extract from some other vector, include it. |
| 343 | Value *VecOp = IEI->getOperand(0); |
| 344 | Value *ScalarOp = IEI->getOperand(1); |
| 345 | Value *IdxOp = IEI->getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 346 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 347 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 348 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 349 | EI->getOperand(0)->getType() == V->getType()) { |
| 350 | unsigned ExtractedIdx = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 351 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 352 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 353 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 354 | // Either the extracted from or inserted into vector must be RHSVec, |
| 355 | // otherwise we'd end up with a shuffle of three inputs. |
| 356 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 357 | RHS = EI->getOperand(0); |
| 358 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 359 | Mask[InsertedIdx % NumElts] = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 360 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 361 | NumElts+ExtractedIdx); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 362 | return V; |
| 363 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 364 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 365 | if (VecOp == RHS) { |
| 366 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
| 367 | // Everything but the extracted element is replaced with the RHS. |
| 368 | for (unsigned i = 0; i != NumElts; ++i) { |
| 369 | if (i != InsertedIdx) |
| 370 | Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 371 | NumElts+i); |
| 372 | } |
| 373 | return V; |
| 374 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 375 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 376 | // If this insertelement is a chain that comes from exactly these two |
| 377 | // vectors, return the vector and the effective shuffle. |
| 378 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 379 | return EI->getOperand(0); |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | // TODO: Handle shufflevector here! |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 384 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 385 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 386 | for (unsigned i = 0; i != NumElts; ++i) |
| 387 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
| 388 | return V; |
| 389 | } |
| 390 | |
| 391 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 392 | Value *VecOp = IE.getOperand(0); |
| 393 | Value *ScalarOp = IE.getOperand(1); |
| 394 | Value *IdxOp = IE.getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 395 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 396 | // Inserting an undef or into an undefined place, remove this. |
| 397 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 398 | ReplaceInstUsesWith(IE, VecOp); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 399 | |
| 400 | // If the inserted element was extracted from some other vector, and if the |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 401 | // indexes are constant, try to turn this into a shufflevector operation. |
| 402 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 403 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 404 | EI->getOperand(0)->getType() == IE.getType()) { |
| 405 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
| 406 | unsigned ExtractedIdx = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 407 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 408 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 409 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 410 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 411 | return ReplaceInstUsesWith(IE, VecOp); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 412 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 413 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 414 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 415 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 416 | // If we are extracting a value from a vector, then inserting it right |
| 417 | // back into the same place, just use the input vector. |
| 418 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 419 | return ReplaceInstUsesWith(IE, VecOp); |
| 420 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 421 | // If this insertelement isn't used by some other insertelement, turn it |
| 422 | // (and any insertelements it points to), into one big shuffle. |
| 423 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
| 424 | std::vector<Constant*> Mask; |
| 425 | Value *RHS = 0; |
| 426 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 427 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 428 | // We now have a shuffle of LHS, RHS, Mask. |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 429 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 433 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 434 | unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements(); |
| 435 | APInt UndefElts(VWidth, 0); |
| 436 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 437 | if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) { |
| 438 | if (V != &IE) |
| 439 | return ReplaceInstUsesWith(IE, V); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 440 | return &IE; |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 441 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 442 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 448 | Value *LHS = SVI.getOperand(0); |
| 449 | Value *RHS = SVI.getOperand(1); |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 450 | std::vector<int> Mask = getShuffleMask(&SVI); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 451 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 452 | bool MadeChange = false; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 453 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 454 | // Undefined shuffle mask -> undefined value. |
| 455 | if (isa<UndefValue>(SVI.getOperand(2))) |
| 456 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 457 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 458 | unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 459 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 460 | if (VWidth != cast<VectorType>(LHS->getType())->getNumElements()) |
| 461 | return 0; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 462 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 463 | APInt UndefElts(VWidth, 0); |
| 464 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 465 | if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) { |
| 466 | if (V != &SVI) |
| 467 | return ReplaceInstUsesWith(SVI, V); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 468 | LHS = SVI.getOperand(0); |
| 469 | RHS = SVI.getOperand(1); |
| 470 | MadeChange = true; |
| 471 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 472 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 473 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 474 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 475 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 476 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
| 477 | // shuffle(undef,undef,mask) -> undef. |
| 478 | return ReplaceInstUsesWith(SVI, LHS); |
| 479 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 480 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 481 | // Remap any references to RHS to use LHS. |
| 482 | std::vector<Constant*> Elts; |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 483 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 484 | if (Mask[i] < 0) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 485 | Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext()))); |
| 486 | else { |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 487 | if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) || |
| 488 | (Mask[i] < (int)e && isa<UndefValue>(LHS))) { |
| 489 | Mask[i] = -1; // Turn into undef. |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 490 | Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext()))); |
| 491 | } else { |
| 492 | Mask[i] = Mask[i] % e; // Force to LHS. |
| 493 | Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()), |
| 494 | Mask[i])); |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | SVI.setOperand(0, SVI.getOperand(1)); |
| 499 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
| 500 | SVI.setOperand(2, ConstantVector::get(Elts)); |
| 501 | LHS = SVI.getOperand(0); |
| 502 | RHS = SVI.getOperand(1); |
| 503 | MadeChange = true; |
| 504 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 505 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 506 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 507 | bool isLHSID = true, isRHSID = true; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 508 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 509 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 510 | if (Mask[i] < 0) continue; // Ignore undef values. |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 511 | // Is this an identity shuffle of the LHS value? |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 512 | isLHSID &= (Mask[i] == (int)i); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 513 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 514 | // Is this an identity shuffle of the RHS value? |
| 515 | isRHSID &= (Mask[i]-e == i); |
| 516 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 517 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 518 | // Eliminate identity shuffles. |
| 519 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 520 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 521 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 522 | // If the LHS is a shufflevector itself, see if we can combine it with this |
| 523 | // one without producing an unusual shuffle. Here we are really conservative: |
| 524 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 525 | // program, because the code gen may not be smart enough to turn a merged |
| 526 | // shuffle into two specific shuffles: it may produce worse code. As such, |
Bob Wilson | cb11b48 | 2010-10-29 22:02:50 +0000 | [diff] [blame] | 527 | // we only merge two shuffles if the result is either a splat or one of the |
| 528 | // two input shuffle masks. In this case, merging the shuffles just removes |
| 529 | // one instruction, which we know is safe. This is good for things like |
| 530 | // turning: (splat(splat)) -> splat. |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 531 | if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) { |
| 532 | if (isa<UndefValue>(RHS)) { |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 533 | std::vector<int> LHSMask = getShuffleMask(LHSSVI); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 534 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 535 | if (LHSMask.size() == Mask.size()) { |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 536 | std::vector<int> NewMask; |
Bob Wilson | cb11b48 | 2010-10-29 22:02:50 +0000 | [diff] [blame] | 537 | bool isSplat = true; |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 538 | int SplatElt = -1; // undef |
Bob Wilson | cb11b48 | 2010-10-29 22:02:50 +0000 | [diff] [blame] | 539 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 540 | int MaskElt; |
| 541 | if (Mask[i] < 0 || Mask[i] >= (int)e) |
| 542 | MaskElt = -1; // undef |
| 543 | else |
Bob Wilson | cb11b48 | 2010-10-29 22:02:50 +0000 | [diff] [blame] | 544 | MaskElt = LHSMask[Mask[i]]; |
| 545 | // Check if this could still be a splat. |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 546 | if (MaskElt >= 0) { |
| 547 | if (SplatElt >=0 && SplatElt != MaskElt) |
Bob Wilson | cb11b48 | 2010-10-29 22:02:50 +0000 | [diff] [blame] | 548 | isSplat = false; |
| 549 | SplatElt = MaskElt; |
| 550 | } |
| 551 | NewMask.push_back(MaskElt); |
| 552 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 553 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 554 | // If the result mask is equal to the src shuffle or this |
| 555 | // shuffle mask, do the replacement. |
Bob Wilson | cb11b48 | 2010-10-29 22:02:50 +0000 | [diff] [blame] | 556 | if (isSplat || NewMask == LHSMask || NewMask == Mask) { |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 557 | std::vector<Constant*> Elts; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame^] | 558 | Type *Int32Ty = Type::getInt32Ty(SVI.getContext()); |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 559 | for (unsigned i = 0, e = NewMask.size(); i != e; ++i) { |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 560 | if (NewMask[i] < 0) { |
Bob Wilson | cb11b48 | 2010-10-29 22:02:50 +0000 | [diff] [blame] | 561 | Elts.push_back(UndefValue::get(Int32Ty)); |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 562 | } else { |
Bob Wilson | cb11b48 | 2010-10-29 22:02:50 +0000 | [diff] [blame] | 563 | Elts.push_back(ConstantInt::get(Int32Ty, NewMask[i])); |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | return new ShuffleVectorInst(LHSSVI->getOperand(0), |
| 567 | LHSSVI->getOperand(1), |
| 568 | ConstantVector::get(Elts)); |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 571 | } |
| 572 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 573 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 574 | return MadeChange ? &SVI : 0; |
| 575 | } |