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