blob: 9977b830f370e0538f8f65554792f10968efacb6 [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"
16using namespace llvm;
17
18/// CheapToScalarize - Return true if the value is cheaper to scalarize than it
19/// is to leave as a vector operation.
20static bool CheapToScalarize(Value *V, bool isConstant) {
21 if (isa<ConstantAggregateZero>(V))
22 return true;
23 if (ConstantVector *C = dyn_cast<ConstantVector>(V)) {
24 if (isConstant) return true;
25 // If all elts are the same, we can extract.
26 Constant *Op0 = C->getOperand(0);
27 for (unsigned i = 1; i < C->getNumOperands(); ++i)
28 if (C->getOperand(i) != Op0)
29 return false;
30 return true;
31 }
32 Instruction *I = dyn_cast<Instruction>(V);
33 if (!I) return false;
34
35 // Insert element gets simplified to the inserted element or is deleted if
36 // this is constant idx extract element and its a constant idx insertelt.
37 if (I->getOpcode() == Instruction::InsertElement && isConstant &&
38 isa<ConstantInt>(I->getOperand(2)))
39 return true;
40 if (I->getOpcode() == Instruction::Load && I->hasOneUse())
41 return true;
42 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
43 if (BO->hasOneUse() &&
44 (CheapToScalarize(BO->getOperand(0), isConstant) ||
45 CheapToScalarize(BO->getOperand(1), isConstant)))
46 return true;
47 if (CmpInst *CI = dyn_cast<CmpInst>(I))
48 if (CI->hasOneUse() &&
49 (CheapToScalarize(CI->getOperand(0), isConstant) ||
50 CheapToScalarize(CI->getOperand(1), isConstant)))
51 return true;
52
53 return false;
54}
55
56/// Read and decode a shufflevector mask.
57///
58/// It turns undef elements into values that are larger than the number of
59/// elements in the input.
60static std::vector<unsigned> getShuffleMask(const ShuffleVectorInst *SVI) {
61 unsigned NElts = SVI->getType()->getNumElements();
62 if (isa<ConstantAggregateZero>(SVI->getOperand(2)))
63 return std::vector<unsigned>(NElts, 0);
64 if (isa<UndefValue>(SVI->getOperand(2)))
65 return std::vector<unsigned>(NElts, 2*NElts);
66
67 std::vector<unsigned> Result;
68 const ConstantVector *CP = cast<ConstantVector>(SVI->getOperand(2));
69 for (User::const_op_iterator i = CP->op_begin(), e = CP->op_end(); i!=e; ++i)
70 if (isa<UndefValue>(*i))
71 Result.push_back(NElts*2); // undef -> 8
72 else
73 Result.push_back(cast<ConstantInt>(*i)->getZExtValue());
74 return Result;
75}
76
77/// FindScalarElement - Given a vector and an element number, see if the scalar
78/// value is already around as a register, for example if it were inserted then
79/// extracted from the vector.
80static Value *FindScalarElement(Value *V, unsigned EltNo) {
81 assert(isa<VectorType>(V->getType()) && "Not looking at a vector?");
82 const VectorType *PTy = cast<VectorType>(V->getType());
83 unsigned Width = PTy->getNumElements();
84 if (EltNo >= Width) // Out of range access.
85 return UndefValue::get(PTy->getElementType());
86
87 if (isa<UndefValue>(V))
88 return UndefValue::get(PTy->getElementType());
89 else if (isa<ConstantAggregateZero>(V))
90 return Constant::getNullValue(PTy->getElementType());
91 else if (ConstantVector *CP = dyn_cast<ConstantVector>(V))
92 return CP->getOperand(EltNo);
93 else if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
94 // If this is an insert to a variable element, we don't know what it is.
95 if (!isa<ConstantInt>(III->getOperand(2)))
96 return 0;
97 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
98
99 // If this is an insert to the element we are looking for, return the
100 // inserted value.
101 if (EltNo == IIElt)
102 return III->getOperand(1);
103
104 // Otherwise, the insertelement doesn't modify the value, recurse on its
105 // vector input.
106 return FindScalarElement(III->getOperand(0), EltNo);
107 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
108 unsigned LHSWidth =
109 cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
110 unsigned InEl = getShuffleMask(SVI)[EltNo];
111 if (InEl < LHSWidth)
112 return FindScalarElement(SVI->getOperand(0), InEl);
113 else if (InEl < LHSWidth*2)
114 return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth);
115 else
116 return UndefValue::get(PTy->getElementType());
117 }
118
119 // Otherwise, we don't know.
120 return 0;
121}
122
123Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
124 // If vector val is undef, replace extract with scalar undef.
125 if (isa<UndefValue>(EI.getOperand(0)))
126 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
127
128 // If vector val is constant 0, replace extract with scalar 0.
129 if (isa<ConstantAggregateZero>(EI.getOperand(0)))
130 return ReplaceInstUsesWith(EI, Constant::getNullValue(EI.getType()));
131
132 if (ConstantVector *C = dyn_cast<ConstantVector>(EI.getOperand(0))) {
133 // If vector val is constant with all elements the same, replace EI with
134 // that element. When the elements are not identical, we cannot replace yet
135 // (we do that below, but only when the index is constant).
136 Constant *op0 = C->getOperand(0);
137 for (unsigned i = 1; i != C->getNumOperands(); ++i)
138 if (C->getOperand(i) != op0) {
139 op0 = 0;
140 break;
141 }
142 if (op0)
143 return ReplaceInstUsesWith(EI, op0);
144 }
145
146 // If extracting a specified index from the vector, see if we can recursively
147 // find a previously computed scalar that was inserted into the vector.
148 if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
149 unsigned IndexVal = IdxC->getZExtValue();
150 unsigned VectorWidth = EI.getVectorOperandType()->getNumElements();
151
152 // If this is extracting an invalid index, turn this into undef, to avoid
153 // crashing the code below.
154 if (IndexVal >= VectorWidth)
155 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
156
157 // This instruction only demands the single element from the input vector.
158 // If the input vector has a single use, simplify it based on this use
159 // property.
160 if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
161 APInt UndefElts(VectorWidth, 0);
162 APInt DemandedMask(VectorWidth, 1 << IndexVal);
163 if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
164 DemandedMask, UndefElts)) {
165 EI.setOperand(0, V);
166 return &EI;
167 }
168 }
169
170 if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
171 return ReplaceInstUsesWith(EI, Elt);
172
173 // If the this extractelement is directly using a bitcast from a vector of
174 // the same number of elements, see if we can find the source element from
175 // it. In this case, we will end up needing to bitcast the scalars.
176 if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
177 if (const VectorType *VT =
178 dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
179 if (VT->getNumElements() == VectorWidth)
180 if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
181 return new BitCastInst(Elt, EI.getType());
182 }
183 }
184
185 if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
186 // Push extractelement into predecessor operation if legal and
187 // profitable to do so
188 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
189 if (I->hasOneUse() &&
190 CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) {
191 Value *newEI0 =
192 Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
193 EI.getName()+".lhs");
194 Value *newEI1 =
195 Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
196 EI.getName()+".rhs");
197 return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
198 }
199 } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
200 // Extracting the inserted element?
201 if (IE->getOperand(2) == EI.getOperand(1))
202 return ReplaceInstUsesWith(EI, IE->getOperand(1));
203 // If the inserted and extracted elements are constants, they must not
204 // be the same value, extract from the pre-inserted value instead.
205 if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) {
206 Worklist.AddValue(EI.getOperand(0));
207 EI.setOperand(0, IE->getOperand(0));
208 return &EI;
209 }
210 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
211 // If this is extracting an element from a shufflevector, figure out where
212 // it came from and extract from the appropriate input element instead.
213 if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
214 unsigned SrcIdx = getShuffleMask(SVI)[Elt->getZExtValue()];
215 Value *Src;
216 unsigned LHSWidth =
217 cast<VectorType>(SVI->getOperand(0)->getType())->getNumElements();
218
219 if (SrcIdx < LHSWidth)
220 Src = SVI->getOperand(0);
221 else if (SrcIdx < LHSWidth*2) {
222 SrcIdx -= LHSWidth;
223 Src = SVI->getOperand(1);
224 } else {
225 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
226 }
227 return ExtractElementInst::Create(Src,
228 ConstantInt::get(Type::getInt32Ty(EI.getContext()),
229 SrcIdx, false));
230 }
231 }
232 // FIXME: Canonicalize extractelement(bitcast) -> bitcast(extractelement)
233 }
234 return 0;
235}
236
237/// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
238/// elements from either LHS or RHS, return the shuffle mask and true.
239/// Otherwise, return false.
240static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
241 std::vector<Constant*> &Mask) {
242 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
243 "Invalid CollectSingleShuffleElements");
244 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
245
246 if (isa<UndefValue>(V)) {
247 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
248 return true;
249 }
250
251 if (V == LHS) {
252 for (unsigned i = 0; i != NumElts; ++i)
253 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
254 return true;
255 }
256
257 if (V == RHS) {
258 for (unsigned i = 0; i != NumElts; ++i)
259 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()),
260 i+NumElts));
261 return true;
262 }
263
264 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
265 // If this is an insert of an extract from some other vector, include it.
266 Value *VecOp = IEI->getOperand(0);
267 Value *ScalarOp = IEI->getOperand(1);
268 Value *IdxOp = IEI->getOperand(2);
269
270 if (!isa<ConstantInt>(IdxOp))
271 return false;
272 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
273
274 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
275 // Okay, we can handle this if the vector we are insertinting into is
276 // transitively ok.
277 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
278 // If so, update the mask to reflect the inserted undef.
279 Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext()));
280 return true;
281 }
282 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
283 if (isa<ConstantInt>(EI->getOperand(1)) &&
284 EI->getOperand(0)->getType() == V->getType()) {
285 unsigned ExtractedIdx =
286 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
287
288 // This must be extracting from either LHS or RHS.
289 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
290 // Okay, we can handle this if the vector we are insertinting into is
291 // transitively ok.
292 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
293 // If so, update the mask to reflect the inserted value.
294 if (EI->getOperand(0) == LHS) {
295 Mask[InsertedIdx % NumElts] =
296 ConstantInt::get(Type::getInt32Ty(V->getContext()),
297 ExtractedIdx);
298 } else {
299 assert(EI->getOperand(0) == RHS);
300 Mask[InsertedIdx % NumElts] =
301 ConstantInt::get(Type::getInt32Ty(V->getContext()),
302 ExtractedIdx+NumElts);
303
304 }
305 return true;
306 }
307 }
308 }
309 }
310 }
311 // TODO: Handle shufflevector here!
312
313 return false;
314}
315
316/// CollectShuffleElements - We are building a shuffle of V, using RHS as the
317/// RHS of the shuffle instruction, if it is not null. Return a shuffle mask
318/// that computes V and the LHS value of the shuffle.
319static Value *CollectShuffleElements(Value *V, std::vector<Constant*> &Mask,
320 Value *&RHS) {
321 assert(isa<VectorType>(V->getType()) &&
322 (RHS == 0 || V->getType() == RHS->getType()) &&
323 "Invalid shuffle!");
324 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
325
326 if (isa<UndefValue>(V)) {
327 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
328 return V;
329 } else if (isa<ConstantAggregateZero>(V)) {
330 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
331 return V;
332 } else if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
333 // If this is an insert of an extract from some other vector, include it.
334 Value *VecOp = IEI->getOperand(0);
335 Value *ScalarOp = IEI->getOperand(1);
336 Value *IdxOp = IEI->getOperand(2);
337
338 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
339 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
340 EI->getOperand(0)->getType() == V->getType()) {
341 unsigned ExtractedIdx =
342 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
343 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
344
345 // Either the extracted from or inserted into vector must be RHSVec,
346 // otherwise we'd end up with a shuffle of three inputs.
347 if (EI->getOperand(0) == RHS || RHS == 0) {
348 RHS = EI->getOperand(0);
349 Value *V = CollectShuffleElements(VecOp, Mask, RHS);
350 Mask[InsertedIdx % NumElts] =
351 ConstantInt::get(Type::getInt32Ty(V->getContext()),
352 NumElts+ExtractedIdx);
353 return V;
354 }
355
356 if (VecOp == RHS) {
357 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
358 // Everything but the extracted element is replaced with the RHS.
359 for (unsigned i = 0; i != NumElts; ++i) {
360 if (i != InsertedIdx)
361 Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()),
362 NumElts+i);
363 }
364 return V;
365 }
366
367 // If this insertelement is a chain that comes from exactly these two
368 // vectors, return the vector and the effective shuffle.
369 if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
370 return EI->getOperand(0);
371 }
372 }
373 }
374 // TODO: Handle shufflevector here!
375
376 // Otherwise, can't do anything fancy. Return an identity vector.
377 for (unsigned i = 0; i != NumElts; ++i)
378 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
379 return V;
380}
381
382Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
383 Value *VecOp = IE.getOperand(0);
384 Value *ScalarOp = IE.getOperand(1);
385 Value *IdxOp = IE.getOperand(2);
386
387 // Inserting an undef or into an undefined place, remove this.
388 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
389 ReplaceInstUsesWith(IE, VecOp);
390
391 // If the inserted element was extracted from some other vector, and if the
392 // indexes are constant, try to turn this into a shufflevector operation.
393 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
394 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
395 EI->getOperand(0)->getType() == IE.getType()) {
396 unsigned NumVectorElts = IE.getType()->getNumElements();
397 unsigned ExtractedIdx =
398 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
399 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
400
401 if (ExtractedIdx >= NumVectorElts) // Out of range extract.
402 return ReplaceInstUsesWith(IE, VecOp);
403
404 if (InsertedIdx >= NumVectorElts) // Out of range insert.
405 return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
406
407 // If we are extracting a value from a vector, then inserting it right
408 // back into the same place, just use the input vector.
409 if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
410 return ReplaceInstUsesWith(IE, VecOp);
411
412 // If this insertelement isn't used by some other insertelement, turn it
413 // (and any insertelements it points to), into one big shuffle.
414 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
415 std::vector<Constant*> Mask;
416 Value *RHS = 0;
417 Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
418 if (RHS == 0) RHS = UndefValue::get(LHS->getType());
419 // We now have a shuffle of LHS, RHS, Mask.
420 return new ShuffleVectorInst(LHS, RHS,
421 ConstantVector::get(Mask));
422 }
423 }
424 }
425
426 unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
427 APInt UndefElts(VWidth, 0);
428 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
429 if (SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts))
430 return &IE;
431
432 return 0;
433}
434
435
436Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
437 Value *LHS = SVI.getOperand(0);
438 Value *RHS = SVI.getOperand(1);
439 std::vector<unsigned> Mask = getShuffleMask(&SVI);
440
441 bool MadeChange = false;
442
443 // Undefined shuffle mask -> undefined value.
444 if (isa<UndefValue>(SVI.getOperand(2)))
445 return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
446
447 unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
448
449 if (VWidth != cast<VectorType>(LHS->getType())->getNumElements())
450 return 0;
451
452 APInt UndefElts(VWidth, 0);
453 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
454 if (SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
455 LHS = SVI.getOperand(0);
456 RHS = SVI.getOperand(1);
457 MadeChange = true;
458 }
459
460 // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
461 // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
462 if (LHS == RHS || isa<UndefValue>(LHS)) {
463 if (isa<UndefValue>(LHS) && LHS == RHS) {
464 // shuffle(undef,undef,mask) -> undef.
465 return ReplaceInstUsesWith(SVI, LHS);
466 }
467
468 // Remap any references to RHS to use LHS.
469 std::vector<Constant*> Elts;
470 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
471 if (Mask[i] >= 2*e)
472 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
473 else {
474 if ((Mask[i] >= e && isa<UndefValue>(RHS)) ||
475 (Mask[i] < e && isa<UndefValue>(LHS))) {
476 Mask[i] = 2*e; // Turn into undef.
477 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
478 } else {
479 Mask[i] = Mask[i] % e; // Force to LHS.
480 Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
481 Mask[i]));
482 }
483 }
484 }
485 SVI.setOperand(0, SVI.getOperand(1));
486 SVI.setOperand(1, UndefValue::get(RHS->getType()));
487 SVI.setOperand(2, ConstantVector::get(Elts));
488 LHS = SVI.getOperand(0);
489 RHS = SVI.getOperand(1);
490 MadeChange = true;
491 }
492
493 // Analyze the shuffle, are the LHS or RHS and identity shuffles?
494 bool isLHSID = true, isRHSID = true;
495
496 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
497 if (Mask[i] >= e*2) continue; // Ignore undef values.
498 // Is this an identity shuffle of the LHS value?
499 isLHSID &= (Mask[i] == i);
500
501 // Is this an identity shuffle of the RHS value?
502 isRHSID &= (Mask[i]-e == i);
503 }
504
505 // Eliminate identity shuffles.
506 if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
507 if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
508
509 // If the LHS is a shufflevector itself, see if we can combine it with this
510 // one without producing an unusual shuffle. Here we are really conservative:
511 // we are absolutely afraid of producing a shuffle mask not in the input
512 // program, because the code gen may not be smart enough to turn a merged
513 // shuffle into two specific shuffles: it may produce worse code. As such,
514 // we only merge two shuffles if the result is one of the two input shuffle
515 // masks. In this case, merging the shuffles just removes one instruction,
516 // which we know is safe. This is good for things like turning:
517 // (splat(splat)) -> splat.
518 if (ShuffleVectorInst *LHSSVI = dyn_cast<ShuffleVectorInst>(LHS)) {
519 if (isa<UndefValue>(RHS)) {
520 std::vector<unsigned> LHSMask = getShuffleMask(LHSSVI);
521
522 if (LHSMask.size() == Mask.size()) {
523 std::vector<unsigned> NewMask;
524 for (unsigned i = 0, e = Mask.size(); i != e; ++i)
525 if (Mask[i] >= e)
526 NewMask.push_back(2*e);
527 else
528 NewMask.push_back(LHSMask[Mask[i]]);
529
530 // If the result mask is equal to the src shuffle or this
531 // shuffle mask, do the replacement.
532 if (NewMask == LHSMask || NewMask == Mask) {
533 unsigned LHSInNElts =
534 cast<VectorType>(LHSSVI->getOperand(0)->getType())->
535 getNumElements();
536 std::vector<Constant*> Elts;
537 for (unsigned i = 0, e = NewMask.size(); i != e; ++i) {
538 if (NewMask[i] >= LHSInNElts*2) {
539 Elts.push_back(UndefValue::get(
540 Type::getInt32Ty(SVI.getContext())));
541 } else {
542 Elts.push_back(ConstantInt::get(
543 Type::getInt32Ty(SVI.getContext()),
544 NewMask[i]));
545 }
546 }
547 return new ShuffleVectorInst(LHSSVI->getOperand(0),
548 LHSSVI->getOperand(1),
549 ConstantVector::get(Elts));
550 }
551 }
552 }
553 }
554
555 return MadeChange ? &SVI : 0;
556}
557