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