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