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