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