blob: d4344c3c4d678ade771d87715cc751dfe415c9b0 [file] [log] [blame]
Chris Lattnerec97a902010-01-05 05:36:20 +00001//===- 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 Rotem7df85092013-01-15 23:43:14 +000016#include "llvm/Support/PatternMatch.h"
Chris Lattnerec97a902010-01-05 05:36:20 +000017using namespace llvm;
Nadav Rotem7df85092013-01-15 23:43:14 +000018using namespace PatternMatch;
Chris Lattnerec97a902010-01-05 05:36:20 +000019
20/// CheapToScalarize - Return true if the value is cheaper to scalarize than it
Chris Lattnera0d01ff2012-01-24 14:31:22 +000021/// 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 Lattnerec97a902010-01-05 05:36:20 +000023static bool CheapToScalarize(Value *V, bool isConstant) {
Chris Lattner8326bd82012-01-26 00:42:34 +000024 if (Constant *C = dyn_cast<Constant>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +000025 if (isConstant) return true;
Chris Lattner8326bd82012-01-26 00:42:34 +000026
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 Lattnerec97a902010-01-05 05:36:20 +000031 return false;
32 return true;
33 }
34 Instruction *I = dyn_cast<Instruction>(V);
35 if (!I) return false;
Bob Wilson8ecf98b2010-10-29 22:20:43 +000036
Chris Lattnerec97a902010-01-05 05:36:20 +000037 // 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 Wilson8ecf98b2010-10-29 22:20:43 +000054
Chris Lattnerec97a902010-01-05 05:36:20 +000055 return false;
56}
57
Chris Lattnerec97a902010-01-05 05:36:20 +000058/// 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.
61static Value *FindScalarElement(Value *V, unsigned EltNo) {
Duncan Sands19d0b472010-02-16 11:11:14 +000062 assert(V->getType()->isVectorTy() && "Not looking at a vector?");
Chris Lattner8326bd82012-01-26 00:42:34 +000063 VectorType *VTy = cast<VectorType>(V->getType());
64 unsigned Width = VTy->getNumElements();
Chris Lattnerec97a902010-01-05 05:36:20 +000065 if (EltNo >= Width) // Out of range access.
Chris Lattner8326bd82012-01-26 00:42:34 +000066 return UndefValue::get(VTy->getElementType());
Bob Wilson8ecf98b2010-10-29 22:20:43 +000067
Chris Lattner8326bd82012-01-26 00:42:34 +000068 if (Constant *C = dyn_cast<Constant>(V))
69 return C->getAggregateElement(EltNo);
Bob Wilson8ecf98b2010-10-29 22:20:43 +000070
Chris Lattner841af4f2010-01-05 05:42:08 +000071 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +000072 // If this is an insert to a variable element, we don't know what it is.
Bob Wilson8ecf98b2010-10-29 22:20:43 +000073 if (!isa<ConstantInt>(III->getOperand(2)))
Chris Lattnerec97a902010-01-05 05:36:20 +000074 return 0;
75 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +000076
Chris Lattnerec97a902010-01-05 05:36:20 +000077 // If this is an insert to the element we are looking for, return the
78 // inserted value.
Bob Wilson8ecf98b2010-10-29 22:20:43 +000079 if (EltNo == IIElt)
Chris Lattnerec97a902010-01-05 05:36:20 +000080 return III->getOperand(1);
Bob Wilson8ecf98b2010-10-29 22:20:43 +000081
Chris Lattnerec97a902010-01-05 05:36:20 +000082 // Otherwise, the insertelement doesn't modify the value, recurse on its
83 // vector input.
84 return FindScalarElement(III->getOperand(0), EltNo);
Chris Lattner841af4f2010-01-05 05:42:08 +000085 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +000086
Chris Lattner841af4f2010-01-05 05:42:08 +000087 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
Chris Lattner8326bd82012-01-26 00:42:34 +000088 unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
Eli Friedman303c81c2011-10-21 19:11:34 +000089 int InEl = SVI->getMaskValue(EltNo);
Bob Wilson8ecf98b2010-10-29 22:20:43 +000090 if (InEl < 0)
Chris Lattner8326bd82012-01-26 00:42:34 +000091 return UndefValue::get(VTy->getElementType());
Bob Wilson11ee4562010-10-29 22:03:05 +000092 if (InEl < (int)LHSWidth)
93 return FindScalarElement(SVI->getOperand(0), InEl);
94 return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth);
Chris Lattnerec97a902010-01-05 05:36:20 +000095 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +000096
Nadav Rotem7df85092013-01-15 23:43:14 +000097 // 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 Lattnerec97a902010-01-05 05:36:20 +0000104 // Otherwise, we don't know.
105 return 0;
106}
107
Anat Shemer0c95efa2013-04-18 19:35:39 +0000108// 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
111Instruction *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 Goulyb34294d2013-05-24 12:33:28 +0000128 !(isa<BinaryOperator>(PHIUser)) || !CheapToScalarize(PHIUser, true))
Anat Shemer0c95efa2013-04-18 19:35:39 +0000129 return NULL;
130
131 // Create a scalar PHI node that will replace the vector PHI node
132 // just before the current PHI node.
Joey Goulyb34294d2013-05-24 12:33:28 +0000133 PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith(
134 PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), *PN));
Anat Shemer0c95efa2013-04-18 19:35:39 +0000135 // Scalarize each PHI operand.
Joey Goulyb34294d2013-05-24 12:33:28 +0000136 for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
Anat Shemer0c95efa2013-04-18 19:35:39 +0000137 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 Goulyb34294d2013-05-24 12:33:28 +0000146 unsigned opId = (B0->getOperand(0) == PN) ? 1 : 0;
Joey Gouly83699282013-05-24 12:29:54 +0000147 Value *Op = InsertNewInstWith(
148 ExtractElementInst::Create(B0->getOperand(opId), Elt,
149 B0->getOperand(opId)->getName() + ".Elt"),
150 *B0);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000151 Value *newPHIUser = InsertNewInstWith(
Joey Goulyb34294d2013-05-24 12:33:28 +0000152 BinaryOperator::Create(B0->getOpcode(), scalarPHI, Op), *B0);
Anat Shemer0c95efa2013-04-18 19:35:39 +0000153 scalarPHI->addIncoming(newPHIUser, inBB);
154 } else {
155 // Scalarize PHI input:
Joey Goulyb34294d2013-05-24 12:33:28 +0000156 Instruction *newEI = ExtractElementInst::Create(PHIInVal, Elt, "");
Anat Shemer0c95efa2013-04-18 19:35:39 +0000157 // 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 Lattnerec97a902010-01-05 05:36:20 +0000175Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
Chris Lattner8326bd82012-01-26 00:42:34 +0000176 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000181
Chris Lattnerec97a902010-01-05 05:36:20 +0000182 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000187
Chris Lattnerec97a902010-01-05 05:36:20 +0000188 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000192
Chris Lattnerec97a902010-01-05 05:36:20 +0000193 // 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 Lattnerb22423c2010-02-08 23:56:03 +0000198 APInt DemandedMask(VectorWidth, 0);
Jay Foad25a5e4c2010-12-01 08:53:58 +0000199 DemandedMask.setBit(IndexVal);
Chris Lattnerec97a902010-01-05 05:36:20 +0000200 if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
201 DemandedMask, UndefElts)) {
202 EI.setOperand(0, V);
203 return &EI;
204 }
205 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000206
Chris Lattnerec97a902010-01-05 05:36:20 +0000207 if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
208 return ReplaceInstUsesWith(EI, Elt);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000209
Chris Lattnerec97a902010-01-05 05:36:20 +0000210 // 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 Lattner8326bd82012-01-26 00:42:34 +0000214 if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
Chris Lattnerec97a902010-01-05 05:36:20 +0000215 if (VT->getNumElements() == VectorWidth)
216 if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
217 return new BitCastInst(Elt, EI.getType());
218 }
Anat Shemer0c95efa2013-04-18 19:35:39 +0000219
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 Lewycky881e9d62013-05-04 01:08:15 +0000223 Instruction *scalarPHI = scalarizePHI(EI, PN);
224 if (scalarPHI)
Joey Goulyb34294d2013-05-24 12:33:28 +0000225 return scalarPHI;
Anat Shemer0c95efa2013-04-18 19:35:39 +0000226 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000227 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000228
Chris Lattnerec97a902010-01-05 05:36:20 +0000229 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 Wilson67a6f322010-10-29 22:20:45 +0000236 Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
237 EI.getName()+".lhs");
Chris Lattnerec97a902010-01-05 05:36:20 +0000238 Value *newEI1 =
Bob Wilson67a6f322010-10-29 22:20:45 +0000239 Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
240 EI.getName()+".rhs");
Chris Lattnerec97a902010-01-05 05:36:20 +0000241 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 Friedman303c81c2011-10-21 19:11:34 +0000258 int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
Chris Lattnerec97a902010-01-05 05:36:20 +0000259 Value *Src;
260 unsigned LHSWidth =
Chris Lattner8326bd82012-01-26 00:42:34 +0000261 SVI->getOperand(0)->getType()->getVectorNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000262
Bob Wilson11ee4562010-10-29 22:03:05 +0000263 if (SrcIdx < 0)
264 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
265 if (SrcIdx < (int)LHSWidth)
Chris Lattnerec97a902010-01-05 05:36:20 +0000266 Src = SVI->getOperand(0);
Bob Wilson11ee4562010-10-29 22:03:05 +0000267 else {
Chris Lattnerec97a902010-01-05 05:36:20 +0000268 SrcIdx -= LHSWidth;
269 Src = SVI->getOperand(1);
Chris Lattnerec97a902010-01-05 05:36:20 +0000270 }
Chris Lattner229907c2011-07-18 04:54:35 +0000271 Type *Int32Ty = Type::getInt32Ty(EI.getContext());
Chris Lattnerec97a902010-01-05 05:36:20 +0000272 return ExtractElementInst::Create(Src,
Bob Wilson9d07f392010-10-29 22:03:07 +0000273 ConstantInt::get(Int32Ty,
Chris Lattnerec97a902010-01-05 05:36:20 +0000274 SrcIdx, false));
275 }
Nadav Rotemd74b72b2011-03-31 22:57:29 +0000276 } 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 Shemer55703182013-04-18 19:56:44 +0000279 if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) {
Anat Shemer10260a72013-04-22 20:51:10 +0000280 Value *EE = Builder->CreateExtractElement(CI->getOperand(0),
281 EI.getIndexOperand());
282 Worklist.AddValue(EE);
Nadav Rotemd74b72b2011-03-31 22:57:29 +0000283 return CastInst::Create(CI->getOpcode(), EE, EI.getType());
284 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000285 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000286 }
287 return 0;
288}
289
290/// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000291/// elements from either LHS or RHS, return the shuffle mask and true.
Chris Lattnerec97a902010-01-05 05:36:20 +0000292/// Otherwise, return false.
293static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
Chris Lattner0256be92012-01-27 03:08:05 +0000294 SmallVectorImpl<Constant*> &Mask) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000295 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
296 "Invalid CollectSingleShuffleElements");
297 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000298
Chris Lattnerec97a902010-01-05 05:36:20 +0000299 if (isa<UndefValue>(V)) {
300 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
301 return true;
302 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000303
Chris Lattnerec97a902010-01-05 05:36:20 +0000304 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 Wilson8ecf98b2010-10-29 22:20:43 +0000309
Chris Lattnerec97a902010-01-05 05:36:20 +0000310 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 Wilson8ecf98b2010-10-29 22:20:43 +0000316
Chris Lattnerec97a902010-01-05 05:36:20 +0000317 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 Wilson8ecf98b2010-10-29 22:20:43 +0000322
Chris Lattnerec97a902010-01-05 05:36:20 +0000323 if (!isa<ConstantInt>(IdxOp))
324 return false;
325 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000326
Chris Lattnerec97a902010-01-05 05:36:20 +0000327 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 Wilson8ecf98b2010-10-29 22:20:43 +0000334 }
Chris Lattnerec97a902010-01-05 05:36:20 +0000335 } 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 Wilson8ecf98b2010-10-29 22:20:43 +0000340
Chris Lattnerec97a902010-01-05 05:36:20 +0000341 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000348 Mask[InsertedIdx % NumElts] =
Chris Lattnerec97a902010-01-05 05:36:20 +0000349 ConstantInt::get(Type::getInt32Ty(V->getContext()),
350 ExtractedIdx);
351 } else {
352 assert(EI->getOperand(0) == RHS);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000353 Mask[InsertedIdx % NumElts] =
Chris Lattnerec97a902010-01-05 05:36:20 +0000354 ConstantInt::get(Type::getInt32Ty(V->getContext()),
355 ExtractedIdx+NumElts);
Chris Lattnerec97a902010-01-05 05:36:20 +0000356 }
357 return true;
358 }
359 }
360 }
361 }
362 }
363 // TODO: Handle shufflevector here!
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000364
Chris Lattnerec97a902010-01-05 05:36:20 +0000365 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 Lattner0256be92012-01-27 03:08:05 +0000371static Value *CollectShuffleElements(Value *V, SmallVectorImpl<Constant*> &Mask,
Chris Lattnerec97a902010-01-05 05:36:20 +0000372 Value *&RHS) {
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000373 assert(V->getType()->isVectorTy() &&
Chris Lattnerec97a902010-01-05 05:36:20 +0000374 (RHS == 0 || V->getType() == RHS->getType()) &&
375 "Invalid shuffle!");
376 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000377
Chris Lattnerec97a902010-01-05 05:36:20 +0000378 if (isa<UndefValue>(V)) {
379 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
380 return V;
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000381 }
Craig Topper2ea22b02013-01-18 05:09:16 +0000382
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000383 if (isa<ConstantAggregateZero>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000384 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
385 return V;
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000386 }
Craig Topper2ea22b02013-01-18 05:09:16 +0000387
Chris Lattnera0d01ff2012-01-24 14:31:22 +0000388 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000389 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000393
Chris Lattnerec97a902010-01-05 05:36:20 +0000394 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 Wilson67a6f322010-10-29 22:20:45 +0000398 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattnerec97a902010-01-05 05:36:20 +0000399 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000400
Chris Lattnerec97a902010-01-05 05:36:20 +0000401 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000406 Mask[InsertedIdx % NumElts] =
Bob Wilson67a6f322010-10-29 22:20:45 +0000407 ConstantInt::get(Type::getInt32Ty(V->getContext()),
408 NumElts+ExtractedIdx);
Chris Lattnerec97a902010-01-05 05:36:20 +0000409 return V;
410 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000411
Chris Lattnerec97a902010-01-05 05:36:20 +0000412 if (VecOp == RHS) {
413 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
Benjamin Kramera95f8742013-04-11 15:10:09 +0000414 // 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 Lattnerec97a902010-01-05 05:36:20 +0000418 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000426
Chris Lattnerec97a902010-01-05 05:36:20 +0000427 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000435
Chris Lattnerec97a902010-01-05 05:36:20 +0000436 // 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
442Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
443 Value *VecOp = IE.getOperand(0);
444 Value *ScalarOp = IE.getOperand(1);
445 Value *IdxOp = IE.getOperand(2);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000446
Chris Lattnerec97a902010-01-05 05:36:20 +0000447 // Inserting an undef or into an undefined place, remove this.
448 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
449 ReplaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000450
451 // If the inserted element was extracted from some other vector, and if the
Chris Lattnerec97a902010-01-05 05:36:20 +0000452 // 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 Wilson67a6f322010-10-29 22:20:45 +0000458 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
Chris Lattnerec97a902010-01-05 05:36:20 +0000459 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000460
Chris Lattnerec97a902010-01-05 05:36:20 +0000461 if (ExtractedIdx >= NumVectorElts) // Out of range extract.
462 return ReplaceInstUsesWith(IE, VecOp);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000463
Chris Lattnerec97a902010-01-05 05:36:20 +0000464 if (InsertedIdx >= NumVectorElts) // Out of range insert.
465 return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000466
Chris Lattnerec97a902010-01-05 05:36:20 +0000467 // 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 Wilson8ecf98b2010-10-29 22:20:43 +0000470 return ReplaceInstUsesWith(IE, VecOp);
471
Chris Lattnerec97a902010-01-05 05:36:20 +0000472 // 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 Lattner0256be92012-01-27 03:08:05 +0000475 SmallVector<Constant*, 16> Mask;
Chris Lattnerec97a902010-01-05 05:36:20 +0000476 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 Wilson67a6f322010-10-29 22:20:45 +0000480 return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask));
Chris Lattnerec97a902010-01-05 05:36:20 +0000481 }
482 }
483 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000484
Chris Lattnerec97a902010-01-05 05:36:20 +0000485 unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
486 APInt UndefElts(VWidth, 0);
487 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
Eli Friedmanef200db2011-02-19 22:42:40 +0000488 if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
489 if (V != &IE)
490 return ReplaceInstUsesWith(IE, V);
Chris Lattnerec97a902010-01-05 05:36:20 +0000491 return &IE;
Eli Friedmanef200db2011-02-19 22:42:40 +0000492 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000493
Chris Lattnerec97a902010-01-05 05:36:20 +0000494 return 0;
495}
496
497
498Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
499 Value *LHS = SVI.getOperand(0);
500 Value *RHS = SVI.getOperand(1);
Chris Lattner8326bd82012-01-26 00:42:34 +0000501 SmallVector<int, 16> Mask = SVI.getShuffleMask();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000502
Chris Lattnerec97a902010-01-05 05:36:20 +0000503 bool MadeChange = false;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000504
Chris Lattnerec97a902010-01-05 05:36:20 +0000505 // Undefined shuffle mask -> undefined value.
506 if (isa<UndefValue>(SVI.getOperand(2)))
507 return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000508
Eric Christopher51edc7b2010-08-17 22:55:27 +0000509 unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000510
Chris Lattnerec97a902010-01-05 05:36:20 +0000511 APInt UndefElts(VWidth, 0);
512 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
Eli Friedmanef200db2011-02-19 22:42:40 +0000513 if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
514 if (V != &SVI)
515 return ReplaceInstUsesWith(SVI, V);
Chris Lattnerec97a902010-01-05 05:36:20 +0000516 LHS = SVI.getOperand(0);
517 RHS = SVI.getOperand(1);
518 MadeChange = true;
519 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000520
Eli Friedmance818272011-10-21 19:06:29 +0000521 unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements();
522
Chris Lattnerec97a902010-01-05 05:36:20 +0000523 // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
524 // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
525 if (LHS == RHS || isa<UndefValue>(LHS)) {
Eric Christopher51edc7b2010-08-17 22:55:27 +0000526 if (isa<UndefValue>(LHS) && LHS == RHS) {
527 // shuffle(undef,undef,mask) -> undef.
Eli Friedmance818272011-10-21 19:06:29 +0000528 Value* result = (VWidth == LHSWidth)
529 ? LHS : UndefValue::get(SVI.getType());
530 return ReplaceInstUsesWith(SVI, result);
Eric Christopher51edc7b2010-08-17 22:55:27 +0000531 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000532
Chris Lattnerec97a902010-01-05 05:36:20 +0000533 // Remap any references to RHS to use LHS.
Chris Lattner0256be92012-01-27 03:08:05 +0000534 SmallVector<Constant*, 16> Elts;
Eli Friedmance818272011-10-21 19:06:29 +0000535 for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
Chris Lattner0256be92012-01-27 03:08:05 +0000536 if (Mask[i] < 0) {
Chris Lattnerec97a902010-01-05 05:36:20 +0000537 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
Chris Lattner0256be92012-01-27 03:08:05 +0000538 continue;
539 }
540
541 if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) ||
542 (Mask[i] < (int)e && isa<UndefValue>(LHS))) {
543 Mask[i] = -1; // Turn into undef.
544 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
545 } else {
546 Mask[i] = Mask[i] % e; // Force to LHS.
547 Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
548 Mask[i]));
Chris Lattnerec97a902010-01-05 05:36:20 +0000549 }
550 }
551 SVI.setOperand(0, SVI.getOperand(1));
552 SVI.setOperand(1, UndefValue::get(RHS->getType()));
553 SVI.setOperand(2, ConstantVector::get(Elts));
554 LHS = SVI.getOperand(0);
555 RHS = SVI.getOperand(1);
556 MadeChange = true;
557 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000558
Eli Friedmance818272011-10-21 19:06:29 +0000559 if (VWidth == LHSWidth) {
560 // Analyze the shuffle, are the LHS or RHS and identity shuffles?
561 bool isLHSID = true, isRHSID = true;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000562
Eli Friedmance818272011-10-21 19:06:29 +0000563 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
564 if (Mask[i] < 0) continue; // Ignore undef values.
565 // Is this an identity shuffle of the LHS value?
566 isLHSID &= (Mask[i] == (int)i);
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000567
Eli Friedmance818272011-10-21 19:06:29 +0000568 // Is this an identity shuffle of the RHS value?
569 isRHSID &= (Mask[i]-e == i);
570 }
571
572 // Eliminate identity shuffles.
573 if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
574 if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
Eric Christopher51edc7b2010-08-17 22:55:27 +0000575 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000576
Eric Christopher51edc7b2010-08-17 22:55:27 +0000577 // If the LHS is a shufflevector itself, see if we can combine it with this
Eli Friedmance818272011-10-21 19:06:29 +0000578 // one without producing an unusual shuffle.
579 // Cases that might be simplified:
580 // 1.
581 // x1=shuffle(v1,v2,mask1)
582 // x=shuffle(x1,undef,mask)
583 // ==>
584 // x=shuffle(v1,undef,newMask)
585 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1
586 // 2.
587 // x1=shuffle(v1,undef,mask1)
588 // x=shuffle(x1,x2,mask)
589 // where v1.size() == mask1.size()
590 // ==>
591 // x=shuffle(v1,x2,newMask)
592 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i]
593 // 3.
594 // x2=shuffle(v2,undef,mask2)
595 // x=shuffle(x1,x2,mask)
596 // where v2.size() == mask2.size()
597 // ==>
598 // x=shuffle(x1,v2,newMask)
599 // newMask[i] = (mask[i] < x1.size())
600 // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size()
601 // 4.
602 // x1=shuffle(v1,undef,mask1)
603 // x2=shuffle(v2,undef,mask2)
604 // x=shuffle(x1,x2,mask)
605 // where v1.size() == v2.size()
606 // ==>
607 // x=shuffle(v1,v2,newMask)
608 // newMask[i] = (mask[i] < x1.size())
609 // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size()
610 //
611 // Here we are really conservative:
Eric Christopher51edc7b2010-08-17 22:55:27 +0000612 // we are absolutely afraid of producing a shuffle mask not in the input
613 // program, because the code gen may not be smart enough to turn a merged
614 // shuffle into two specific shuffles: it may produce worse code. As such,
Jim Grosbachd11584a2013-05-01 00:25:27 +0000615 // we only merge two shuffles if the result is either a splat or one of the
616 // input shuffle masks. In this case, merging the shuffles just removes
617 // one instruction, which we know is safe. This is good for things like
Eli Friedmance818272011-10-21 19:06:29 +0000618 // turning: (splat(splat)) -> splat, or
619 // merge(V[0..n], V[n+1..2n]) -> V[0..2n]
620 ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
621 ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
622 if (LHSShuffle)
623 if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
624 LHSShuffle = NULL;
625 if (RHSShuffle)
626 if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
627 RHSShuffle = NULL;
628 if (!LHSShuffle && !RHSShuffle)
629 return MadeChange ? &SVI : 0;
630
631 Value* LHSOp0 = NULL;
632 Value* LHSOp1 = NULL;
633 Value* RHSOp0 = NULL;
634 unsigned LHSOp0Width = 0;
635 unsigned RHSOp0Width = 0;
636 if (LHSShuffle) {
637 LHSOp0 = LHSShuffle->getOperand(0);
638 LHSOp1 = LHSShuffle->getOperand(1);
639 LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements();
640 }
641 if (RHSShuffle) {
642 RHSOp0 = RHSShuffle->getOperand(0);
643 RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements();
644 }
645 Value* newLHS = LHS;
646 Value* newRHS = RHS;
647 if (LHSShuffle) {
648 // case 1
Eric Christopher51edc7b2010-08-17 22:55:27 +0000649 if (isa<UndefValue>(RHS)) {
Eli Friedmance818272011-10-21 19:06:29 +0000650 newLHS = LHSOp0;
651 newRHS = LHSOp1;
652 }
653 // case 2 or 4
654 else if (LHSOp0Width == LHSWidth) {
655 newLHS = LHSOp0;
656 }
657 }
658 // case 3 or 4
659 if (RHSShuffle && RHSOp0Width == LHSWidth) {
660 newRHS = RHSOp0;
661 }
662 // case 4
663 if (LHSOp0 == RHSOp0) {
664 newLHS = LHSOp0;
665 newRHS = NULL;
666 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000667
Eli Friedmance818272011-10-21 19:06:29 +0000668 if (newLHS == LHS && newRHS == RHS)
669 return MadeChange ? &SVI : 0;
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000670
Eli Friedmance818272011-10-21 19:06:29 +0000671 SmallVector<int, 16> LHSMask;
672 SmallVector<int, 16> RHSMask;
Chris Lattner8326bd82012-01-26 00:42:34 +0000673 if (newLHS != LHS)
674 LHSMask = LHSShuffle->getShuffleMask();
675 if (RHSShuffle && newRHS != RHS)
676 RHSMask = RHSShuffle->getShuffleMask();
677
Eli Friedmance818272011-10-21 19:06:29 +0000678 unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
679 SmallVector<int, 16> newMask;
680 bool isSplat = true;
681 int SplatElt = -1;
682 // Create a new mask for the new ShuffleVectorInst so that the new
683 // ShuffleVectorInst is equivalent to the original one.
684 for (unsigned i = 0; i < VWidth; ++i) {
685 int eltMask;
Craig Topper45d9f4b2013-01-18 05:30:07 +0000686 if (Mask[i] < 0) {
Eli Friedmance818272011-10-21 19:06:29 +0000687 // This element is an undef value.
688 eltMask = -1;
689 } else if (Mask[i] < (int)LHSWidth) {
690 // This element is from left hand side vector operand.
Craig Topper2ea22b02013-01-18 05:09:16 +0000691 //
Eli Friedmance818272011-10-21 19:06:29 +0000692 // If LHS is going to be replaced (case 1, 2, or 4), calculate the
693 // new mask value for the element.
694 if (newLHS != LHS) {
695 eltMask = LHSMask[Mask[i]];
696 // If the value selected is an undef value, explicitly specify it
697 // with a -1 mask value.
698 if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1))
699 eltMask = -1;
Craig Topper2ea22b02013-01-18 05:09:16 +0000700 } else
Eli Friedmance818272011-10-21 19:06:29 +0000701 eltMask = Mask[i];
702 } else {
703 // This element is from right hand side vector operand
704 //
705 // If the value selected is an undef value, explicitly specify it
706 // with a -1 mask value. (case 1)
707 if (isa<UndefValue>(RHS))
708 eltMask = -1;
709 // If RHS is going to be replaced (case 3 or 4), calculate the
710 // new mask value for the element.
711 else if (newRHS != RHS) {
712 eltMask = RHSMask[Mask[i]-LHSWidth];
713 // If the value selected is an undef value, explicitly specify it
714 // with a -1 mask value.
715 if (eltMask >= (int)RHSOp0Width) {
716 assert(isa<UndefValue>(RHSShuffle->getOperand(1))
717 && "should have been check above");
718 eltMask = -1;
Nate Begeman2a0ca3e92010-08-13 00:17:53 +0000719 }
Craig Topper2ea22b02013-01-18 05:09:16 +0000720 } else
Eli Friedmance818272011-10-21 19:06:29 +0000721 eltMask = Mask[i]-LHSWidth;
722
723 // If LHS's width is changed, shift the mask value accordingly.
724 // If newRHS == NULL, i.e. LHSOp0 == RHSOp0, we want to remap any
Michael Gottesman02a11412012-10-16 21:29:38 +0000725 // references from RHSOp0 to LHSOp0, so we don't need to shift the mask.
726 // If newRHS == newLHS, we want to remap any references from newRHS to
727 // newLHS so that we can properly identify splats that may occur due to
728 // obfuscation accross the two vectors.
729 if (eltMask >= 0 && newRHS != NULL && newLHS != newRHS)
Eli Friedmance818272011-10-21 19:06:29 +0000730 eltMask += newLHSWidth;
Nate Begeman2a0ca3e92010-08-13 00:17:53 +0000731 }
Eli Friedmance818272011-10-21 19:06:29 +0000732
733 // Check if this could still be a splat.
734 if (eltMask >= 0) {
735 if (SplatElt >= 0 && SplatElt != eltMask)
736 isSplat = false;
737 SplatElt = eltMask;
738 }
739
740 newMask.push_back(eltMask);
741 }
742
743 // If the result mask is equal to one of the original shuffle masks,
Jim Grosbachd11584a2013-05-01 00:25:27 +0000744 // or is a splat, do the replacement.
745 if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
Eli Friedmance818272011-10-21 19:06:29 +0000746 SmallVector<Constant*, 16> Elts;
747 Type *Int32Ty = Type::getInt32Ty(SVI.getContext());
748 for (unsigned i = 0, e = newMask.size(); i != e; ++i) {
749 if (newMask[i] < 0) {
750 Elts.push_back(UndefValue::get(Int32Ty));
751 } else {
752 Elts.push_back(ConstantInt::get(Int32Ty, newMask[i]));
753 }
754 }
755 if (newRHS == NULL)
756 newRHS = UndefValue::get(newLHS->getType());
757 return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts));
Nate Begeman2a0ca3e92010-08-13 00:17:53 +0000758 }
Bob Wilson8ecf98b2010-10-29 22:20:43 +0000759
Chris Lattnerec97a902010-01-05 05:36:20 +0000760 return MadeChange ? &SVI : 0;
761}