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 |
Matt Arsenault | 3887473 | 2013-08-28 22:17:26 +0000 | [diff] [blame] | 109 | // itself and be an operand of extractelement at a constant location, |
| 110 | // try to replace the PHI of the vector type with a PHI of a scalar type. |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 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) || |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 128 | !(isa<BinaryOperator>(PHIUser)) || !CheapToScalarize(PHIUser, true)) |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 129 | return NULL; |
| 130 | |
| 131 | // Create a scalar PHI node that will replace the vector PHI node |
| 132 | // just before the current PHI node. |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 133 | PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith( |
| 134 | PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), *PN)); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 135 | // Scalarize each PHI operand. |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 136 | for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) { |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 137 | Value *PHIInVal = PN->getIncomingValue(i); |
| 138 | BasicBlock *inBB = PN->getIncomingBlock(i); |
| 139 | Value *Elt = EI.getIndexOperand(); |
| 140 | // If the operand is the PHI induction variable: |
| 141 | if (PHIInVal == PHIUser) { |
| 142 | // Scalarize the binary operation. Its first operand is the |
| 143 | // scalar PHI and the second operand is extracted from the other |
| 144 | // vector operand. |
| 145 | BinaryOperator *B0 = cast<BinaryOperator>(PHIUser); |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 146 | unsigned opId = (B0->getOperand(0) == PN) ? 1 : 0; |
Joey Gouly | 8369928 | 2013-05-24 12:29:54 +0000 | [diff] [blame] | 147 | Value *Op = InsertNewInstWith( |
| 148 | ExtractElementInst::Create(B0->getOperand(opId), Elt, |
| 149 | B0->getOperand(opId)->getName() + ".Elt"), |
| 150 | *B0); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 151 | Value *newPHIUser = InsertNewInstWith( |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 152 | BinaryOperator::Create(B0->getOpcode(), scalarPHI, Op), *B0); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 153 | scalarPHI->addIncoming(newPHIUser, inBB); |
| 154 | } else { |
| 155 | // Scalarize PHI input: |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 156 | Instruction *newEI = ExtractElementInst::Create(PHIInVal, Elt, ""); |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 157 | // Insert the new instruction into the predecessor basic block. |
| 158 | Instruction *pos = dyn_cast<Instruction>(PHIInVal); |
| 159 | BasicBlock::iterator InsertPos; |
| 160 | if (pos && !isa<PHINode>(pos)) { |
| 161 | InsertPos = pos; |
| 162 | ++InsertPos; |
| 163 | } else { |
| 164 | InsertPos = inBB->getFirstInsertionPt(); |
| 165 | } |
| 166 | |
| 167 | InsertNewInstWith(newEI, *InsertPos); |
| 168 | |
| 169 | scalarPHI->addIncoming(newEI, inBB); |
| 170 | } |
| 171 | } |
| 172 | return ReplaceInstUsesWith(EI, scalarPHI); |
| 173 | } |
| 174 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 175 | Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 176 | // If vector val is constant with all elements the same, replace EI with |
| 177 | // that element. We handle a known element # below. |
| 178 | if (Constant *C = dyn_cast<Constant>(EI.getOperand(0))) |
| 179 | if (CheapToScalarize(C, false)) |
| 180 | return ReplaceInstUsesWith(EI, C->getAggregateElement(0U)); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 181 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 182 | // If extracting a specified index from the vector, see if we can recursively |
| 183 | // find a previously computed scalar that was inserted into the vector. |
| 184 | if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
| 185 | unsigned IndexVal = IdxC->getZExtValue(); |
| 186 | unsigned VectorWidth = EI.getVectorOperandType()->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 187 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 188 | // If this is extracting an invalid index, turn this into undef, to avoid |
| 189 | // crashing the code below. |
| 190 | if (IndexVal >= VectorWidth) |
| 191 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 192 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 193 | // This instruction only demands the single element from the input vector. |
| 194 | // If the input vector has a single use, simplify it based on this use |
| 195 | // property. |
| 196 | if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) { |
| 197 | APInt UndefElts(VectorWidth, 0); |
Chris Lattner | b22423c | 2010-02-08 23:56:03 +0000 | [diff] [blame] | 198 | APInt DemandedMask(VectorWidth, 0); |
Jay Foad | 25a5e4c | 2010-12-01 08:53:58 +0000 | [diff] [blame] | 199 | DemandedMask.setBit(IndexVal); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 200 | if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0), |
| 201 | DemandedMask, UndefElts)) { |
| 202 | EI.setOperand(0, V); |
| 203 | return &EI; |
| 204 | } |
| 205 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 206 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 207 | if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal)) |
| 208 | return ReplaceInstUsesWith(EI, Elt); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 209 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 210 | // If the this extractelement is directly using a bitcast from a vector of |
| 211 | // the same number of elements, see if we can find the source element from |
| 212 | // it. In this case, we will end up needing to bitcast the scalars. |
| 213 | if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) { |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 214 | if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType())) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 215 | if (VT->getNumElements() == VectorWidth) |
| 216 | if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal)) |
| 217 | return new BitCastInst(Elt, EI.getType()); |
| 218 | } |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 219 | |
| 220 | // If there's a vector PHI feeding a scalar use through this extractelement |
| 221 | // instruction, try to scalarize the PHI. |
| 222 | if (PHINode *PN = dyn_cast<PHINode>(EI.getOperand(0))) { |
Nick Lewycky | 881e9d6 | 2013-05-04 01:08:15 +0000 | [diff] [blame] | 223 | Instruction *scalarPHI = scalarizePHI(EI, PN); |
| 224 | if (scalarPHI) |
Joey Gouly | b34294d | 2013-05-24 12:33:28 +0000 | [diff] [blame] | 225 | return scalarPHI; |
Anat Shemer | 0c95efa | 2013-04-18 19:35:39 +0000 | [diff] [blame] | 226 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 227 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 228 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 229 | if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) { |
| 230 | // Push extractelement into predecessor operation if legal and |
| 231 | // profitable to do so |
| 232 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) { |
| 233 | if (I->hasOneUse() && |
| 234 | CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) { |
| 235 | Value *newEI0 = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 236 | Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1), |
| 237 | EI.getName()+".lhs"); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 238 | Value *newEI1 = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 239 | Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1), |
| 240 | EI.getName()+".rhs"); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 241 | return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1); |
| 242 | } |
| 243 | } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) { |
| 244 | // Extracting the inserted element? |
| 245 | if (IE->getOperand(2) == EI.getOperand(1)) |
| 246 | return ReplaceInstUsesWith(EI, IE->getOperand(1)); |
| 247 | // If the inserted and extracted elements are constants, they must not |
| 248 | // be the same value, extract from the pre-inserted value instead. |
| 249 | if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) { |
| 250 | Worklist.AddValue(EI.getOperand(0)); |
| 251 | EI.setOperand(0, IE->getOperand(0)); |
| 252 | return &EI; |
| 253 | } |
| 254 | } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) { |
| 255 | // If this is extracting an element from a shufflevector, figure out where |
| 256 | // it came from and extract from the appropriate input element instead. |
| 257 | if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) { |
Eli Friedman | 303c81c | 2011-10-21 19:11:34 +0000 | [diff] [blame] | 258 | int SrcIdx = SVI->getMaskValue(Elt->getZExtValue()); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 259 | Value *Src; |
| 260 | unsigned LHSWidth = |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 261 | SVI->getOperand(0)->getType()->getVectorNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 262 | |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 263 | if (SrcIdx < 0) |
| 264 | return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType())); |
| 265 | if (SrcIdx < (int)LHSWidth) |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 266 | Src = SVI->getOperand(0); |
Bob Wilson | 11ee456 | 2010-10-29 22:03:05 +0000 | [diff] [blame] | 267 | else { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 268 | SrcIdx -= LHSWidth; |
| 269 | Src = SVI->getOperand(1); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 270 | } |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 271 | Type *Int32Ty = Type::getInt32Ty(EI.getContext()); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 272 | return ExtractElementInst::Create(Src, |
Bob Wilson | 9d07f39 | 2010-10-29 22:03:07 +0000 | [diff] [blame] | 273 | ConstantInt::get(Int32Ty, |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 274 | SrcIdx, false)); |
| 275 | } |
Nadav Rotem | d74b72b | 2011-03-31 22:57:29 +0000 | [diff] [blame] | 276 | } else if (CastInst *CI = dyn_cast<CastInst>(I)) { |
| 277 | // Canonicalize extractelement(cast) -> cast(extractelement) |
| 278 | // bitcasts can change the number of vector elements and they cost nothing |
Anat Shemer | 5570318 | 2013-04-18 19:56:44 +0000 | [diff] [blame] | 279 | if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) { |
Anat Shemer | 10260a7 | 2013-04-22 20:51:10 +0000 | [diff] [blame] | 280 | Value *EE = Builder->CreateExtractElement(CI->getOperand(0), |
| 281 | EI.getIndexOperand()); |
| 282 | Worklist.AddValue(EE); |
Nadav Rotem | d74b72b | 2011-03-31 22:57:29 +0000 | [diff] [blame] | 283 | return CastInst::Create(CI->getOpcode(), EE, EI.getType()); |
| 284 | } |
Matt Arsenault | 243140f | 2013-11-04 20:36:06 +0000 | [diff] [blame] | 285 | } else if (SelectInst *SI = dyn_cast<SelectInst>(I)) { |
| 286 | if (SI->hasOneUse()) { |
| 287 | // TODO: For a select on vectors, it might be useful to do this if it |
| 288 | // has multiple extractelement uses. For vector select, that seems to |
| 289 | // fight the vectorizer. |
| 290 | |
| 291 | // If we are extracting an element from a vector select or a select on |
| 292 | // vectors, a select on the scalars extracted from the vector arguments. |
| 293 | Value *TrueVal = SI->getTrueValue(); |
| 294 | Value *FalseVal = SI->getFalseValue(); |
| 295 | |
| 296 | Value *Cond = SI->getCondition(); |
| 297 | if (Cond->getType()->isVectorTy()) { |
| 298 | Cond = Builder->CreateExtractElement(Cond, |
| 299 | EI.getIndexOperand(), |
| 300 | Cond->getName() + ".elt"); |
| 301 | } |
| 302 | |
| 303 | Value *V1Elem |
| 304 | = Builder->CreateExtractElement(TrueVal, |
| 305 | EI.getIndexOperand(), |
| 306 | TrueVal->getName() + ".elt"); |
| 307 | |
| 308 | Value *V2Elem |
| 309 | = Builder->CreateExtractElement(FalseVal, |
| 310 | EI.getIndexOperand(), |
| 311 | FalseVal->getName() + ".elt"); |
| 312 | return SelectInst::Create(Cond, |
| 313 | V1Elem, |
| 314 | V2Elem, |
| 315 | SI->getName() + ".elt"); |
| 316 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 317 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 323 | /// elements from either LHS or RHS, return the shuffle mask and true. |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 324 | /// Otherwise, return false. |
| 325 | static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 326 | SmallVectorImpl<Constant*> &Mask) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 327 | assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() && |
| 328 | "Invalid CollectSingleShuffleElements"); |
Matt Arsenault | 8227b9f | 2013-09-06 00:37:24 +0000 | [diff] [blame] | 329 | unsigned NumElts = V->getType()->getVectorNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 330 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 331 | if (isa<UndefValue>(V)) { |
| 332 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
| 333 | return true; |
| 334 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 335 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 336 | if (V == LHS) { |
| 337 | for (unsigned i = 0; i != NumElts; ++i) |
| 338 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
| 339 | return true; |
| 340 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 341 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 342 | if (V == RHS) { |
| 343 | for (unsigned i = 0; i != NumElts; ++i) |
| 344 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 345 | i+NumElts)); |
| 346 | return true; |
| 347 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 348 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 349 | if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
| 350 | // If this is an insert of an extract from some other vector, include it. |
| 351 | Value *VecOp = IEI->getOperand(0); |
| 352 | Value *ScalarOp = IEI->getOperand(1); |
| 353 | Value *IdxOp = IEI->getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 354 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 355 | if (!isa<ConstantInt>(IdxOp)) |
| 356 | return false; |
| 357 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
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 | if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector. |
| 360 | // Okay, we can handle this if the vector we are insertinting into is |
| 361 | // transitively ok. |
| 362 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 363 | // If so, update the mask to reflect the inserted undef. |
| 364 | Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext())); |
| 365 | return true; |
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 | } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){ |
| 368 | if (isa<ConstantInt>(EI->getOperand(1)) && |
| 369 | EI->getOperand(0)->getType() == V->getType()) { |
| 370 | unsigned ExtractedIdx = |
| 371 | cast<ConstantInt>(EI->getOperand(1))->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 | // This must be extracting from either LHS or RHS. |
| 374 | if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) { |
| 375 | // Okay, we can handle this if the vector we are insertinting into is |
| 376 | // transitively ok. |
| 377 | if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) { |
| 378 | // If so, update the mask to reflect the inserted value. |
| 379 | if (EI->getOperand(0) == LHS) { |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 380 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 381 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 382 | ExtractedIdx); |
| 383 | } else { |
| 384 | assert(EI->getOperand(0) == RHS); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 385 | Mask[InsertedIdx % NumElts] = |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 386 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 387 | ExtractedIdx+NumElts); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 388 | } |
| 389 | return true; |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | // TODO: Handle shufflevector here! |
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 | return false; |
| 398 | } |
| 399 | |
| 400 | /// CollectShuffleElements - We are building a shuffle of V, using RHS as the |
| 401 | /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask |
| 402 | /// that computes V and the LHS value of the shuffle. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 403 | static Value *CollectShuffleElements(Value *V, SmallVectorImpl<Constant*> &Mask, |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 404 | Value *&RHS) { |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 405 | assert(V->getType()->isVectorTy() && |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 406 | (RHS == 0 || V->getType() == RHS->getType()) && |
| 407 | "Invalid shuffle!"); |
| 408 | unsigned NumElts = cast<VectorType>(V->getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 409 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 410 | if (isa<UndefValue>(V)) { |
| 411 | Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext()))); |
| 412 | return V; |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 413 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 414 | |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 415 | if (isa<ConstantAggregateZero>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 416 | Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0)); |
| 417 | return V; |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 418 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 419 | |
Chris Lattner | a0d01ff | 2012-01-24 14:31:22 +0000 | [diff] [blame] | 420 | if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 421 | // If this is an insert of an extract from some other vector, include it. |
| 422 | Value *VecOp = IEI->getOperand(0); |
| 423 | Value *ScalarOp = IEI->getOperand(1); |
| 424 | Value *IdxOp = IEI->getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 425 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 426 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 427 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 428 | EI->getOperand(0)->getType() == V->getType()) { |
| 429 | unsigned ExtractedIdx = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 430 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 431 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 432 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 433 | // Either the extracted from or inserted into vector must be RHSVec, |
| 434 | // otherwise we'd end up with a shuffle of three inputs. |
| 435 | if (EI->getOperand(0) == RHS || RHS == 0) { |
| 436 | RHS = EI->getOperand(0); |
| 437 | Value *V = CollectShuffleElements(VecOp, Mask, RHS); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 438 | Mask[InsertedIdx % NumElts] = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 439 | ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 440 | NumElts+ExtractedIdx); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 441 | return V; |
| 442 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 443 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 444 | if (VecOp == RHS) { |
| 445 | Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS); |
Benjamin Kramer | a95f874 | 2013-04-11 15:10:09 +0000 | [diff] [blame] | 446 | // Update Mask to reflect that `ScalarOp' has been inserted at |
| 447 | // position `InsertedIdx' within the vector returned by IEI. |
| 448 | Mask[InsertedIdx % NumElts] = Mask[ExtractedIdx]; |
| 449 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 450 | // Everything but the extracted element is replaced with the RHS. |
| 451 | for (unsigned i = 0; i != NumElts; ++i) { |
| 452 | if (i != InsertedIdx) |
| 453 | Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), |
| 454 | NumElts+i); |
| 455 | } |
| 456 | return V; |
| 457 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 458 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 459 | // If this insertelement is a chain that comes from exactly these two |
| 460 | // vectors, return the vector and the effective shuffle. |
| 461 | if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask)) |
| 462 | return EI->getOperand(0); |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | // TODO: Handle shufflevector here! |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 467 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 468 | // Otherwise, can't do anything fancy. Return an identity vector. |
| 469 | for (unsigned i = 0; i != NumElts; ++i) |
| 470 | Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); |
| 471 | return V; |
| 472 | } |
| 473 | |
| 474 | Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { |
| 475 | Value *VecOp = IE.getOperand(0); |
| 476 | Value *ScalarOp = IE.getOperand(1); |
| 477 | Value *IdxOp = IE.getOperand(2); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 478 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 479 | // Inserting an undef or into an undefined place, remove this. |
| 480 | if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp)) |
| 481 | ReplaceInstUsesWith(IE, VecOp); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 482 | |
| 483 | // 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] | 484 | // indexes are constant, try to turn this into a shufflevector operation. |
| 485 | if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) { |
| 486 | if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) && |
| 487 | EI->getOperand(0)->getType() == IE.getType()) { |
| 488 | unsigned NumVectorElts = IE.getType()->getNumElements(); |
| 489 | unsigned ExtractedIdx = |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 490 | cast<ConstantInt>(EI->getOperand(1))->getZExtValue(); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 491 | unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 492 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 493 | if (ExtractedIdx >= NumVectorElts) // Out of range extract. |
| 494 | return ReplaceInstUsesWith(IE, VecOp); |
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 | if (InsertedIdx >= NumVectorElts) // Out of range insert. |
| 497 | return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 498 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 499 | // If we are extracting a value from a vector, then inserting it right |
| 500 | // back into the same place, just use the input vector. |
| 501 | if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx) |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 502 | return ReplaceInstUsesWith(IE, VecOp); |
| 503 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 504 | // If this insertelement isn't used by some other insertelement, turn it |
| 505 | // (and any insertelements it points to), into one big shuffle. |
| 506 | if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 507 | SmallVector<Constant*, 16> Mask; |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 508 | Value *RHS = 0; |
| 509 | Value *LHS = CollectShuffleElements(&IE, Mask, RHS); |
| 510 | if (RHS == 0) RHS = UndefValue::get(LHS->getType()); |
| 511 | // We now have a shuffle of LHS, RHS, Mask. |
Bob Wilson | 67a6f32 | 2010-10-29 22:20:45 +0000 | [diff] [blame] | 512 | return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask)); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 513 | } |
| 514 | } |
| 515 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 516 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 517 | unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements(); |
| 518 | APInt UndefElts(VWidth, 0); |
| 519 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 520 | if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) { |
| 521 | if (V != &IE) |
| 522 | return ReplaceInstUsesWith(IE, V); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 523 | return &IE; |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 524 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 525 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 526 | return 0; |
| 527 | } |
| 528 | |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 529 | /// Return true if we can evaluate the specified expression tree if the vector |
| 530 | /// elements were shuffled in a different order. |
| 531 | static bool CanEvaluateShuffled(Value *V, ArrayRef<int> Mask, |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 532 | unsigned Depth = 5) { |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 533 | // We can always reorder the elements of a constant. |
| 534 | if (isa<Constant>(V)) |
| 535 | return true; |
| 536 | |
| 537 | // We won't reorder vector arguments. No IPO here. |
| 538 | Instruction *I = dyn_cast<Instruction>(V); |
| 539 | if (!I) return false; |
| 540 | |
| 541 | // Two users may expect different orders of the elements. Don't try it. |
| 542 | if (!I->hasOneUse()) |
| 543 | return false; |
| 544 | |
| 545 | if (Depth == 0) return false; |
| 546 | |
| 547 | switch (I->getOpcode()) { |
| 548 | case Instruction::Add: |
| 549 | case Instruction::FAdd: |
| 550 | case Instruction::Sub: |
| 551 | case Instruction::FSub: |
| 552 | case Instruction::Mul: |
| 553 | case Instruction::FMul: |
| 554 | case Instruction::UDiv: |
| 555 | case Instruction::SDiv: |
| 556 | case Instruction::FDiv: |
| 557 | case Instruction::URem: |
| 558 | case Instruction::SRem: |
| 559 | case Instruction::FRem: |
| 560 | case Instruction::Shl: |
| 561 | case Instruction::LShr: |
| 562 | case Instruction::AShr: |
| 563 | case Instruction::And: |
| 564 | case Instruction::Or: |
| 565 | case Instruction::Xor: |
| 566 | case Instruction::ICmp: |
| 567 | case Instruction::FCmp: |
| 568 | case Instruction::Trunc: |
| 569 | case Instruction::ZExt: |
| 570 | case Instruction::SExt: |
| 571 | case Instruction::FPToUI: |
| 572 | case Instruction::FPToSI: |
| 573 | case Instruction::UIToFP: |
| 574 | case Instruction::SIToFP: |
| 575 | case Instruction::FPTrunc: |
| 576 | case Instruction::FPExt: |
| 577 | case Instruction::GetElementPtr: { |
| 578 | for (int i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 579 | if (!CanEvaluateShuffled(I->getOperand(i), Mask, Depth-1)) |
| 580 | return false; |
| 581 | } |
| 582 | return true; |
| 583 | } |
| 584 | case Instruction::InsertElement: { |
| 585 | ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(2)); |
| 586 | if (!CI) return false; |
| 587 | int ElementNumber = CI->getLimitedValue(); |
| 588 | |
| 589 | // Verify that 'CI' does not occur twice in Mask. A single 'insertelement' |
| 590 | // can't put an element into multiple indices. |
| 591 | bool SeenOnce = false; |
| 592 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 593 | if (Mask[i] == ElementNumber) { |
| 594 | if (SeenOnce) |
| 595 | return false; |
| 596 | SeenOnce = true; |
| 597 | } |
| 598 | } |
| 599 | return CanEvaluateShuffled(I->getOperand(0), Mask, Depth-1); |
| 600 | } |
| 601 | } |
| 602 | return false; |
| 603 | } |
| 604 | |
| 605 | /// Rebuild a new instruction just like 'I' but with the new operands given. |
| 606 | /// In the event of type mismatch, the type of the operands is correct. |
| 607 | static Value *BuildNew(Instruction *I, ArrayRef<Value*> NewOps) { |
| 608 | // We don't want to use the IRBuilder here because we want the replacement |
| 609 | // instructions to appear next to 'I', not the builder's insertion point. |
| 610 | switch (I->getOpcode()) { |
| 611 | case Instruction::Add: |
| 612 | case Instruction::FAdd: |
| 613 | case Instruction::Sub: |
| 614 | case Instruction::FSub: |
| 615 | case Instruction::Mul: |
| 616 | case Instruction::FMul: |
| 617 | case Instruction::UDiv: |
| 618 | case Instruction::SDiv: |
| 619 | case Instruction::FDiv: |
| 620 | case Instruction::URem: |
| 621 | case Instruction::SRem: |
| 622 | case Instruction::FRem: |
| 623 | case Instruction::Shl: |
| 624 | case Instruction::LShr: |
| 625 | case Instruction::AShr: |
| 626 | case Instruction::And: |
| 627 | case Instruction::Or: |
| 628 | case Instruction::Xor: { |
| 629 | BinaryOperator *BO = cast<BinaryOperator>(I); |
| 630 | assert(NewOps.size() == 2 && "binary operator with #ops != 2"); |
| 631 | BinaryOperator *New = |
| 632 | BinaryOperator::Create(cast<BinaryOperator>(I)->getOpcode(), |
| 633 | NewOps[0], NewOps[1], "", BO); |
| 634 | if (isa<OverflowingBinaryOperator>(BO)) { |
| 635 | New->setHasNoUnsignedWrap(BO->hasNoUnsignedWrap()); |
| 636 | New->setHasNoSignedWrap(BO->hasNoSignedWrap()); |
| 637 | } |
| 638 | if (isa<PossiblyExactOperator>(BO)) { |
| 639 | New->setIsExact(BO->isExact()); |
| 640 | } |
Owen Anderson | 48b842e | 2014-01-18 00:48:14 +0000 | [diff] [blame^] | 641 | if (isa<FPMathOperator>(BO)) |
| 642 | New->copyFastMathFlags(I); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 643 | return New; |
| 644 | } |
| 645 | case Instruction::ICmp: |
| 646 | assert(NewOps.size() == 2 && "icmp with #ops != 2"); |
| 647 | return new ICmpInst(I, cast<ICmpInst>(I)->getPredicate(), |
| 648 | NewOps[0], NewOps[1]); |
| 649 | case Instruction::FCmp: |
| 650 | assert(NewOps.size() == 2 && "fcmp with #ops != 2"); |
| 651 | return new FCmpInst(I, cast<FCmpInst>(I)->getPredicate(), |
| 652 | NewOps[0], NewOps[1]); |
| 653 | case Instruction::Trunc: |
| 654 | case Instruction::ZExt: |
| 655 | case Instruction::SExt: |
| 656 | case Instruction::FPToUI: |
| 657 | case Instruction::FPToSI: |
| 658 | case Instruction::UIToFP: |
| 659 | case Instruction::SIToFP: |
| 660 | case Instruction::FPTrunc: |
| 661 | case Instruction::FPExt: { |
| 662 | // It's possible that the mask has a different number of elements from |
| 663 | // the original cast. We recompute the destination type to match the mask. |
| 664 | Type *DestTy = |
| 665 | VectorType::get(I->getType()->getScalarType(), |
| 666 | NewOps[0]->getType()->getVectorNumElements()); |
| 667 | assert(NewOps.size() == 1 && "cast with #ops != 1"); |
| 668 | return CastInst::Create(cast<CastInst>(I)->getOpcode(), NewOps[0], DestTy, |
| 669 | "", I); |
| 670 | } |
| 671 | case Instruction::GetElementPtr: { |
| 672 | Value *Ptr = NewOps[0]; |
| 673 | ArrayRef<Value*> Idx = NewOps.slice(1); |
| 674 | GetElementPtrInst *GEP = GetElementPtrInst::Create(Ptr, Idx, "", I); |
| 675 | GEP->setIsInBounds(cast<GetElementPtrInst>(I)->isInBounds()); |
| 676 | return GEP; |
| 677 | } |
| 678 | } |
| 679 | llvm_unreachable("failed to rebuild vector instructions"); |
| 680 | } |
| 681 | |
| 682 | Value * |
| 683 | InstCombiner::EvaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask) { |
| 684 | // Mask.size() does not need to be equal to the number of vector elements. |
| 685 | |
| 686 | assert(V->getType()->isVectorTy() && "can't reorder non-vector elements"); |
| 687 | if (isa<UndefValue>(V)) { |
| 688 | return UndefValue::get(VectorType::get(V->getType()->getScalarType(), |
| 689 | Mask.size())); |
| 690 | } |
| 691 | if (isa<ConstantAggregateZero>(V)) { |
| 692 | return ConstantAggregateZero::get( |
| 693 | VectorType::get(V->getType()->getScalarType(), |
| 694 | Mask.size())); |
| 695 | } |
| 696 | if (Constant *C = dyn_cast<Constant>(V)) { |
| 697 | SmallVector<Constant *, 16> MaskValues; |
| 698 | for (int i = 0, e = Mask.size(); i != e; ++i) { |
| 699 | if (Mask[i] == -1) |
| 700 | MaskValues.push_back(UndefValue::get(Builder->getInt32Ty())); |
| 701 | else |
| 702 | MaskValues.push_back(Builder->getInt32(Mask[i])); |
| 703 | } |
| 704 | return ConstantExpr::getShuffleVector(C, UndefValue::get(C->getType()), |
| 705 | ConstantVector::get(MaskValues)); |
| 706 | } |
| 707 | |
| 708 | Instruction *I = cast<Instruction>(V); |
| 709 | switch (I->getOpcode()) { |
| 710 | case Instruction::Add: |
| 711 | case Instruction::FAdd: |
| 712 | case Instruction::Sub: |
| 713 | case Instruction::FSub: |
| 714 | case Instruction::Mul: |
| 715 | case Instruction::FMul: |
| 716 | case Instruction::UDiv: |
| 717 | case Instruction::SDiv: |
| 718 | case Instruction::FDiv: |
| 719 | case Instruction::URem: |
| 720 | case Instruction::SRem: |
| 721 | case Instruction::FRem: |
| 722 | case Instruction::Shl: |
| 723 | case Instruction::LShr: |
| 724 | case Instruction::AShr: |
| 725 | case Instruction::And: |
| 726 | case Instruction::Or: |
| 727 | case Instruction::Xor: |
| 728 | case Instruction::ICmp: |
| 729 | case Instruction::FCmp: |
| 730 | case Instruction::Trunc: |
| 731 | case Instruction::ZExt: |
| 732 | case Instruction::SExt: |
| 733 | case Instruction::FPToUI: |
| 734 | case Instruction::FPToSI: |
| 735 | case Instruction::UIToFP: |
| 736 | case Instruction::SIToFP: |
| 737 | case Instruction::FPTrunc: |
| 738 | case Instruction::FPExt: |
| 739 | case Instruction::Select: |
| 740 | case Instruction::GetElementPtr: { |
| 741 | SmallVector<Value*, 8> NewOps; |
| 742 | bool NeedsRebuild = (Mask.size() != I->getType()->getVectorNumElements()); |
| 743 | for (int i = 0, e = I->getNumOperands(); i != e; ++i) { |
| 744 | Value *V = EvaluateInDifferentElementOrder(I->getOperand(i), Mask); |
| 745 | NewOps.push_back(V); |
| 746 | NeedsRebuild |= (V != I->getOperand(i)); |
| 747 | } |
| 748 | if (NeedsRebuild) { |
| 749 | return BuildNew(I, NewOps); |
| 750 | } |
| 751 | return I; |
| 752 | } |
| 753 | case Instruction::InsertElement: { |
| 754 | int Element = cast<ConstantInt>(I->getOperand(2))->getLimitedValue(); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 755 | |
| 756 | // The insertelement was inserting at Element. Figure out which element |
| 757 | // that becomes after shuffling. The answer is guaranteed to be unique |
| 758 | // by CanEvaluateShuffled. |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 759 | bool Found = false; |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 760 | int Index = 0; |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 761 | for (int e = Mask.size(); Index != e; ++Index) { |
| 762 | if (Mask[Index] == Element) { |
| 763 | Found = true; |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 764 | break; |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 765 | } |
| 766 | } |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 767 | |
Hao Liu | 26abebb | 2014-01-08 03:06:15 +0000 | [diff] [blame] | 768 | // If element is not in Mask, no need to handle the operand 1 (element to |
| 769 | // be inserted). Just evaluate values in operand 0 according to Mask. |
Nick Lewycky | 3f715e2 | 2013-06-01 20:51:31 +0000 | [diff] [blame] | 770 | if (!Found) |
Hao Liu | 26abebb | 2014-01-08 03:06:15 +0000 | [diff] [blame] | 771 | return EvaluateInDifferentElementOrder(I->getOperand(0), Mask); |
Joey Gouly | a3250f2 | 2013-07-12 23:08:06 +0000 | [diff] [blame] | 772 | |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 773 | Value *V = EvaluateInDifferentElementOrder(I->getOperand(0), Mask); |
| 774 | return InsertElementInst::Create(V, I->getOperand(1), |
| 775 | Builder->getInt32(Index), "", I); |
| 776 | } |
| 777 | } |
| 778 | llvm_unreachable("failed to reorder elements of vector instruction!"); |
| 779 | } |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 780 | |
| 781 | Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { |
| 782 | Value *LHS = SVI.getOperand(0); |
| 783 | Value *RHS = SVI.getOperand(1); |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 784 | SmallVector<int, 16> Mask = SVI.getShuffleMask(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 785 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 786 | bool MadeChange = false; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 787 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 788 | // Undefined shuffle mask -> undefined value. |
| 789 | if (isa<UndefValue>(SVI.getOperand(2))) |
| 790 | return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType())); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 791 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 792 | unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements(); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 793 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 794 | APInt UndefElts(VWidth, 0); |
| 795 | APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth)); |
Eli Friedman | ef200db | 2011-02-19 22:42:40 +0000 | [diff] [blame] | 796 | if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) { |
| 797 | if (V != &SVI) |
| 798 | return ReplaceInstUsesWith(SVI, V); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 799 | LHS = SVI.getOperand(0); |
| 800 | RHS = SVI.getOperand(1); |
| 801 | MadeChange = true; |
| 802 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 803 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 804 | unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements(); |
| 805 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 806 | // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask') |
| 807 | // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask'). |
| 808 | if (LHS == RHS || isa<UndefValue>(LHS)) { |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 809 | if (isa<UndefValue>(LHS) && LHS == RHS) { |
| 810 | // shuffle(undef,undef,mask) -> undef. |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 811 | Value *Result = (VWidth == LHSWidth) |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 812 | ? LHS : UndefValue::get(SVI.getType()); |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 813 | return ReplaceInstUsesWith(SVI, Result); |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 814 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 815 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 816 | // Remap any references to RHS to use LHS. |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 817 | SmallVector<Constant*, 16> Elts; |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 818 | for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) { |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 819 | if (Mask[i] < 0) { |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 820 | Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext()))); |
Chris Lattner | 0256be9 | 2012-01-27 03:08:05 +0000 | [diff] [blame] | 821 | continue; |
| 822 | } |
| 823 | |
| 824 | if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) || |
| 825 | (Mask[i] < (int)e && isa<UndefValue>(LHS))) { |
| 826 | Mask[i] = -1; // Turn into undef. |
| 827 | Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext()))); |
| 828 | } else { |
| 829 | Mask[i] = Mask[i] % e; // Force to LHS. |
| 830 | Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()), |
| 831 | Mask[i])); |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 832 | } |
| 833 | } |
| 834 | SVI.setOperand(0, SVI.getOperand(1)); |
| 835 | SVI.setOperand(1, UndefValue::get(RHS->getType())); |
| 836 | SVI.setOperand(2, ConstantVector::get(Elts)); |
| 837 | LHS = SVI.getOperand(0); |
| 838 | RHS = SVI.getOperand(1); |
| 839 | MadeChange = true; |
| 840 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 841 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 842 | if (VWidth == LHSWidth) { |
| 843 | // Analyze the shuffle, are the LHS or RHS and identity shuffles? |
| 844 | bool isLHSID = true, isRHSID = true; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 845 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 846 | for (unsigned i = 0, e = Mask.size(); i != e; ++i) { |
| 847 | if (Mask[i] < 0) continue; // Ignore undef values. |
| 848 | // Is this an identity shuffle of the LHS value? |
| 849 | isLHSID &= (Mask[i] == (int)i); |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 850 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 851 | // Is this an identity shuffle of the RHS value? |
| 852 | isRHSID &= (Mask[i]-e == i); |
| 853 | } |
| 854 | |
| 855 | // Eliminate identity shuffles. |
| 856 | if (isLHSID) return ReplaceInstUsesWith(SVI, LHS); |
| 857 | if (isRHSID) return ReplaceInstUsesWith(SVI, RHS); |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 858 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 859 | |
Nick Lewycky | 688d668 | 2013-06-03 23:15:20 +0000 | [diff] [blame] | 860 | if (isa<UndefValue>(RHS) && CanEvaluateShuffled(LHS, Mask)) { |
Nick Lewycky | a2b7720 | 2013-05-31 00:59:42 +0000 | [diff] [blame] | 861 | Value *V = EvaluateInDifferentElementOrder(LHS, Mask); |
| 862 | return ReplaceInstUsesWith(SVI, V); |
| 863 | } |
| 864 | |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 865 | // 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] | 866 | // one without producing an unusual shuffle. |
| 867 | // Cases that might be simplified: |
| 868 | // 1. |
| 869 | // x1=shuffle(v1,v2,mask1) |
| 870 | // x=shuffle(x1,undef,mask) |
| 871 | // ==> |
| 872 | // x=shuffle(v1,undef,newMask) |
| 873 | // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1 |
| 874 | // 2. |
| 875 | // x1=shuffle(v1,undef,mask1) |
| 876 | // x=shuffle(x1,x2,mask) |
| 877 | // where v1.size() == mask1.size() |
| 878 | // ==> |
| 879 | // x=shuffle(v1,x2,newMask) |
| 880 | // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i] |
| 881 | // 3. |
| 882 | // x2=shuffle(v2,undef,mask2) |
| 883 | // x=shuffle(x1,x2,mask) |
| 884 | // where v2.size() == mask2.size() |
| 885 | // ==> |
| 886 | // x=shuffle(x1,v2,newMask) |
| 887 | // newMask[i] = (mask[i] < x1.size()) |
| 888 | // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size() |
| 889 | // 4. |
| 890 | // x1=shuffle(v1,undef,mask1) |
| 891 | // x2=shuffle(v2,undef,mask2) |
| 892 | // x=shuffle(x1,x2,mask) |
| 893 | // where v1.size() == v2.size() |
| 894 | // ==> |
| 895 | // x=shuffle(v1,v2,newMask) |
| 896 | // newMask[i] = (mask[i] < x1.size()) |
| 897 | // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size() |
| 898 | // |
| 899 | // Here we are really conservative: |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 900 | // we are absolutely afraid of producing a shuffle mask not in the input |
| 901 | // program, because the code gen may not be smart enough to turn a merged |
| 902 | // shuffle into two specific shuffles: it may produce worse code. As such, |
Jim Grosbach | d11584a | 2013-05-01 00:25:27 +0000 | [diff] [blame] | 903 | // we only merge two shuffles if the result is either a splat or one of the |
| 904 | // input shuffle masks. In this case, merging the shuffles just removes |
| 905 | // 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] | 906 | // turning: (splat(splat)) -> splat, or |
| 907 | // merge(V[0..n], V[n+1..2n]) -> V[0..2n] |
| 908 | ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS); |
| 909 | ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS); |
| 910 | if (LHSShuffle) |
| 911 | if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS)) |
| 912 | LHSShuffle = NULL; |
| 913 | if (RHSShuffle) |
| 914 | if (!isa<UndefValue>(RHSShuffle->getOperand(1))) |
| 915 | RHSShuffle = NULL; |
| 916 | if (!LHSShuffle && !RHSShuffle) |
| 917 | return MadeChange ? &SVI : 0; |
| 918 | |
| 919 | Value* LHSOp0 = NULL; |
| 920 | Value* LHSOp1 = NULL; |
| 921 | Value* RHSOp0 = NULL; |
| 922 | unsigned LHSOp0Width = 0; |
| 923 | unsigned RHSOp0Width = 0; |
| 924 | if (LHSShuffle) { |
| 925 | LHSOp0 = LHSShuffle->getOperand(0); |
| 926 | LHSOp1 = LHSShuffle->getOperand(1); |
| 927 | LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements(); |
| 928 | } |
| 929 | if (RHSShuffle) { |
| 930 | RHSOp0 = RHSShuffle->getOperand(0); |
| 931 | RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements(); |
| 932 | } |
| 933 | Value* newLHS = LHS; |
| 934 | Value* newRHS = RHS; |
| 935 | if (LHSShuffle) { |
| 936 | // case 1 |
Eric Christopher | 51edc7b | 2010-08-17 22:55:27 +0000 | [diff] [blame] | 937 | if (isa<UndefValue>(RHS)) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 938 | newLHS = LHSOp0; |
| 939 | newRHS = LHSOp1; |
| 940 | } |
| 941 | // case 2 or 4 |
| 942 | else if (LHSOp0Width == LHSWidth) { |
| 943 | newLHS = LHSOp0; |
| 944 | } |
| 945 | } |
| 946 | // case 3 or 4 |
| 947 | if (RHSShuffle && RHSOp0Width == LHSWidth) { |
| 948 | newRHS = RHSOp0; |
| 949 | } |
| 950 | // case 4 |
| 951 | if (LHSOp0 == RHSOp0) { |
| 952 | newLHS = LHSOp0; |
| 953 | newRHS = NULL; |
| 954 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 955 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 956 | if (newLHS == LHS && newRHS == RHS) |
| 957 | return MadeChange ? &SVI : 0; |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 958 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 959 | SmallVector<int, 16> LHSMask; |
| 960 | SmallVector<int, 16> RHSMask; |
Chris Lattner | 8326bd8 | 2012-01-26 00:42:34 +0000 | [diff] [blame] | 961 | if (newLHS != LHS) |
| 962 | LHSMask = LHSShuffle->getShuffleMask(); |
| 963 | if (RHSShuffle && newRHS != RHS) |
| 964 | RHSMask = RHSShuffle->getShuffleMask(); |
| 965 | |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 966 | unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth; |
| 967 | SmallVector<int, 16> newMask; |
| 968 | bool isSplat = true; |
| 969 | int SplatElt = -1; |
| 970 | // Create a new mask for the new ShuffleVectorInst so that the new |
| 971 | // ShuffleVectorInst is equivalent to the original one. |
| 972 | for (unsigned i = 0; i < VWidth; ++i) { |
| 973 | int eltMask; |
Craig Topper | 45d9f4b | 2013-01-18 05:30:07 +0000 | [diff] [blame] | 974 | if (Mask[i] < 0) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 975 | // This element is an undef value. |
| 976 | eltMask = -1; |
| 977 | } else if (Mask[i] < (int)LHSWidth) { |
| 978 | // This element is from left hand side vector operand. |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 979 | // |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 980 | // If LHS is going to be replaced (case 1, 2, or 4), calculate the |
| 981 | // new mask value for the element. |
| 982 | if (newLHS != LHS) { |
| 983 | eltMask = LHSMask[Mask[i]]; |
| 984 | // If the value selected is an undef value, explicitly specify it |
| 985 | // with a -1 mask value. |
| 986 | if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1)) |
| 987 | eltMask = -1; |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 988 | } else |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 989 | eltMask = Mask[i]; |
| 990 | } else { |
| 991 | // This element is from right hand side vector operand |
| 992 | // |
| 993 | // If the value selected is an undef value, explicitly specify it |
| 994 | // with a -1 mask value. (case 1) |
| 995 | if (isa<UndefValue>(RHS)) |
| 996 | eltMask = -1; |
| 997 | // If RHS is going to be replaced (case 3 or 4), calculate the |
| 998 | // new mask value for the element. |
| 999 | else if (newRHS != RHS) { |
| 1000 | eltMask = RHSMask[Mask[i]-LHSWidth]; |
| 1001 | // If the value selected is an undef value, explicitly specify it |
| 1002 | // with a -1 mask value. |
| 1003 | if (eltMask >= (int)RHSOp0Width) { |
| 1004 | assert(isa<UndefValue>(RHSShuffle->getOperand(1)) |
| 1005 | && "should have been check above"); |
| 1006 | eltMask = -1; |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 1007 | } |
Craig Topper | 2ea22b0 | 2013-01-18 05:09:16 +0000 | [diff] [blame] | 1008 | } else |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1009 | eltMask = Mask[i]-LHSWidth; |
| 1010 | |
| 1011 | // If LHS's width is changed, shift the mask value accordingly. |
| 1012 | // If newRHS == NULL, i.e. LHSOp0 == RHSOp0, we want to remap any |
Michael Gottesman | 02a1141 | 2012-10-16 21:29:38 +0000 | [diff] [blame] | 1013 | // references from RHSOp0 to LHSOp0, so we don't need to shift the mask. |
| 1014 | // If newRHS == newLHS, we want to remap any references from newRHS to |
| 1015 | // newLHS so that we can properly identify splats that may occur due to |
| 1016 | // obfuscation accross the two vectors. |
| 1017 | if (eltMask >= 0 && newRHS != NULL && newLHS != newRHS) |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1018 | eltMask += newLHSWidth; |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 1019 | } |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1020 | |
| 1021 | // Check if this could still be a splat. |
| 1022 | if (eltMask >= 0) { |
| 1023 | if (SplatElt >= 0 && SplatElt != eltMask) |
| 1024 | isSplat = false; |
| 1025 | SplatElt = eltMask; |
| 1026 | } |
| 1027 | |
| 1028 | newMask.push_back(eltMask); |
| 1029 | } |
| 1030 | |
| 1031 | // 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] | 1032 | // or is a splat, do the replacement. |
| 1033 | if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) { |
Eli Friedman | ce81827 | 2011-10-21 19:06:29 +0000 | [diff] [blame] | 1034 | SmallVector<Constant*, 16> Elts; |
| 1035 | Type *Int32Ty = Type::getInt32Ty(SVI.getContext()); |
| 1036 | for (unsigned i = 0, e = newMask.size(); i != e; ++i) { |
| 1037 | if (newMask[i] < 0) { |
| 1038 | Elts.push_back(UndefValue::get(Int32Ty)); |
| 1039 | } else { |
| 1040 | Elts.push_back(ConstantInt::get(Int32Ty, newMask[i])); |
| 1041 | } |
| 1042 | } |
| 1043 | if (newRHS == NULL) |
| 1044 | newRHS = UndefValue::get(newLHS->getType()); |
| 1045 | return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts)); |
Nate Begeman | 2a0ca3e9 | 2010-08-13 00:17:53 +0000 | [diff] [blame] | 1046 | } |
Bob Wilson | 8ecf98b | 2010-10-29 22:20:43 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | ec97a90 | 2010-01-05 05:36:20 +0000 | [diff] [blame] | 1048 | return MadeChange ? &SVI : 0; |
| 1049 | } |