Check for less than 0 in shuffle mask instead of -1. It's more consistent with other...
[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 #include "llvm/Support/PatternMatch.h"
17 using namespace llvm;
18 using namespace PatternMatch;
19
20 /// CheapToScalarize - Return true if the value is cheaper to scalarize than it
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.
23 static bool CheapToScalarize(Value *V, bool isConstant) {
24   if (Constant *C = dyn_cast<Constant>(V)) {
25     if (isConstant) return true;
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)
31         return false;
32     return true;
33   }
34   Instruction *I = dyn_cast<Instruction>(V);
35   if (!I) return false;
36
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;
54
55   return false;
56 }
57
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) {
62   assert(V->getType()->isVectorTy() && "Not looking at a vector?");
63   VectorType *VTy = cast<VectorType>(V->getType());
64   unsigned Width = VTy->getNumElements();
65   if (EltNo >= Width)  // Out of range access.
66     return UndefValue::get(VTy->getElementType());
67
68   if (Constant *C = dyn_cast<Constant>(V))
69     return C->getAggregateElement(EltNo);
70
71   if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
72     // If this is an insert to a variable element, we don't know what it is.
73     if (!isa<ConstantInt>(III->getOperand(2)))
74       return 0;
75     unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
76
77     // If this is an insert to the element we are looking for, return the
78     // inserted value.
79     if (EltNo == IIElt)
80       return III->getOperand(1);
81
82     // Otherwise, the insertelement doesn't modify the value, recurse on its
83     // vector input.
84     return FindScalarElement(III->getOperand(0), EltNo);
85   }
86
87   if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
88     unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
89     int InEl = SVI->getMaskValue(EltNo);
90     if (InEl < 0)
91       return UndefValue::get(VTy->getElementType());
92     if (InEl < (int)LHSWidth)
93       return FindScalarElement(SVI->getOperand(0), InEl);
94     return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth);
95   }
96
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
104   // Otherwise, we don't know.
105   return 0;
106 }
107
108 Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
109   // If vector val is constant with all elements the same, replace EI with
110   // that element.  We handle a known element # below.
111   if (Constant *C = dyn_cast<Constant>(EI.getOperand(0)))
112     if (CheapToScalarize(C, false))
113       return ReplaceInstUsesWith(EI, C->getAggregateElement(0U));
114
115   // If extracting a specified index from the vector, see if we can recursively
116   // find a previously computed scalar that was inserted into the vector.
117   if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
118     unsigned IndexVal = IdxC->getZExtValue();
119     unsigned VectorWidth = EI.getVectorOperandType()->getNumElements();
120
121     // If this is extracting an invalid index, turn this into undef, to avoid
122     // crashing the code below.
123     if (IndexVal >= VectorWidth)
124       return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
125
126     // This instruction only demands the single element from the input vector.
127     // If the input vector has a single use, simplify it based on this use
128     // property.
129     if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
130       APInt UndefElts(VectorWidth, 0);
131       APInt DemandedMask(VectorWidth, 0);
132       DemandedMask.setBit(IndexVal);
133       if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
134                                                 DemandedMask, UndefElts)) {
135         EI.setOperand(0, V);
136         return &EI;
137       }
138     }
139
140     if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
141       return ReplaceInstUsesWith(EI, Elt);
142
143     // If the this extractelement is directly using a bitcast from a vector of
144     // the same number of elements, see if we can find the source element from
145     // it.  In this case, we will end up needing to bitcast the scalars.
146     if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
147       if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
148         if (VT->getNumElements() == VectorWidth)
149           if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
150             return new BitCastInst(Elt, EI.getType());
151     }
152   }
153
154   if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
155     // Push extractelement into predecessor operation if legal and
156     // profitable to do so
157     if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
158       if (I->hasOneUse() &&
159           CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) {
160         Value *newEI0 =
161           Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
162                                         EI.getName()+".lhs");
163         Value *newEI1 =
164           Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
165                                         EI.getName()+".rhs");
166         return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
167       }
168     } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
169       // Extracting the inserted element?
170       if (IE->getOperand(2) == EI.getOperand(1))
171         return ReplaceInstUsesWith(EI, IE->getOperand(1));
172       // If the inserted and extracted elements are constants, they must not
173       // be the same value, extract from the pre-inserted value instead.
174       if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) {
175         Worklist.AddValue(EI.getOperand(0));
176         EI.setOperand(0, IE->getOperand(0));
177         return &EI;
178       }
179     } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
180       // If this is extracting an element from a shufflevector, figure out where
181       // it came from and extract from the appropriate input element instead.
182       if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
183         int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
184         Value *Src;
185         unsigned LHSWidth =
186           SVI->getOperand(0)->getType()->getVectorNumElements();
187
188         if (SrcIdx < 0)
189           return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
190         if (SrcIdx < (int)LHSWidth)
191           Src = SVI->getOperand(0);
192         else {
193           SrcIdx -= LHSWidth;
194           Src = SVI->getOperand(1);
195         }
196         Type *Int32Ty = Type::getInt32Ty(EI.getContext());
197         return ExtractElementInst::Create(Src,
198                                           ConstantInt::get(Int32Ty,
199                                                            SrcIdx, false));
200       }
201     } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
202       // Canonicalize extractelement(cast) -> cast(extractelement)
203       // bitcasts can change the number of vector elements and they cost nothing
204       if (CI->hasOneUse() && EI.hasOneUse() &&
205           (CI->getOpcode() != Instruction::BitCast)) {
206         Value *EE = Builder->CreateExtractElement(CI->getOperand(0),
207                                                   EI.getIndexOperand());
208         return CastInst::Create(CI->getOpcode(), EE, EI.getType());
209       }
210     }
211   }
212   return 0;
213 }
214
215 /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
216 /// elements from either LHS or RHS, return the shuffle mask and true.
217 /// Otherwise, return false.
218 static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
219                                          SmallVectorImpl<Constant*> &Mask) {
220   assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
221          "Invalid CollectSingleShuffleElements");
222   unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
223
224   if (isa<UndefValue>(V)) {
225     Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
226     return true;
227   }
228
229   if (V == LHS) {
230     for (unsigned i = 0; i != NumElts; ++i)
231       Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
232     return true;
233   }
234
235   if (V == RHS) {
236     for (unsigned i = 0; i != NumElts; ++i)
237       Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()),
238                                       i+NumElts));
239     return true;
240   }
241
242   if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
243     // If this is an insert of an extract from some other vector, include it.
244     Value *VecOp    = IEI->getOperand(0);
245     Value *ScalarOp = IEI->getOperand(1);
246     Value *IdxOp    = IEI->getOperand(2);
247
248     if (!isa<ConstantInt>(IdxOp))
249       return false;
250     unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
251
252     if (isa<UndefValue>(ScalarOp)) {  // inserting undef into vector.
253       // Okay, we can handle this if the vector we are insertinting into is
254       // transitively ok.
255       if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
256         // If so, update the mask to reflect the inserted undef.
257         Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext()));
258         return true;
259       }
260     } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
261       if (isa<ConstantInt>(EI->getOperand(1)) &&
262           EI->getOperand(0)->getType() == V->getType()) {
263         unsigned ExtractedIdx =
264         cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
265
266         // This must be extracting from either LHS or RHS.
267         if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
268           // Okay, we can handle this if the vector we are insertinting into is
269           // transitively ok.
270           if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
271             // If so, update the mask to reflect the inserted value.
272             if (EI->getOperand(0) == LHS) {
273               Mask[InsertedIdx % NumElts] =
274               ConstantInt::get(Type::getInt32Ty(V->getContext()),
275                                ExtractedIdx);
276             } else {
277               assert(EI->getOperand(0) == RHS);
278               Mask[InsertedIdx % NumElts] =
279               ConstantInt::get(Type::getInt32Ty(V->getContext()),
280                                ExtractedIdx+NumElts);
281             }
282             return true;
283           }
284         }
285       }
286     }
287   }
288   // TODO: Handle shufflevector here!
289
290   return false;
291 }
292
293 /// CollectShuffleElements - We are building a shuffle of V, using RHS as the
294 /// RHS of the shuffle instruction, if it is not null.  Return a shuffle mask
295 /// that computes V and the LHS value of the shuffle.
296 static Value *CollectShuffleElements(Value *V, SmallVectorImpl<Constant*> &Mask,
297                                      Value *&RHS) {
298   assert(V->getType()->isVectorTy() &&
299          (RHS == 0 || V->getType() == RHS->getType()) &&
300          "Invalid shuffle!");
301   unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
302
303   if (isa<UndefValue>(V)) {
304     Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
305     return V;
306   }
307
308   if (isa<ConstantAggregateZero>(V)) {
309     Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
310     return V;
311   }
312
313   if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
314     // If this is an insert of an extract from some other vector, include it.
315     Value *VecOp    = IEI->getOperand(0);
316     Value *ScalarOp = IEI->getOperand(1);
317     Value *IdxOp    = IEI->getOperand(2);
318
319     if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
320       if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
321           EI->getOperand(0)->getType() == V->getType()) {
322         unsigned ExtractedIdx =
323           cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
324         unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
325
326         // Either the extracted from or inserted into vector must be RHSVec,
327         // otherwise we'd end up with a shuffle of three inputs.
328         if (EI->getOperand(0) == RHS || RHS == 0) {
329           RHS = EI->getOperand(0);
330           Value *V = CollectShuffleElements(VecOp, Mask, RHS);
331           Mask[InsertedIdx % NumElts] =
332             ConstantInt::get(Type::getInt32Ty(V->getContext()),
333                              NumElts+ExtractedIdx);
334           return V;
335         }
336
337         if (VecOp == RHS) {
338           Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
339           // Everything but the extracted element is replaced with the RHS.
340           for (unsigned i = 0; i != NumElts; ++i) {
341             if (i != InsertedIdx)
342               Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()),
343                                          NumElts+i);
344           }
345           return V;
346         }
347
348         // If this insertelement is a chain that comes from exactly these two
349         // vectors, return the vector and the effective shuffle.
350         if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
351           return EI->getOperand(0);
352       }
353     }
354   }
355   // TODO: Handle shufflevector here!
356
357   // Otherwise, can't do anything fancy.  Return an identity vector.
358   for (unsigned i = 0; i != NumElts; ++i)
359     Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
360   return V;
361 }
362
363 Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
364   Value *VecOp    = IE.getOperand(0);
365   Value *ScalarOp = IE.getOperand(1);
366   Value *IdxOp    = IE.getOperand(2);
367
368   // Inserting an undef or into an undefined place, remove this.
369   if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
370     ReplaceInstUsesWith(IE, VecOp);
371
372   // If the inserted element was extracted from some other vector, and if the
373   // indexes are constant, try to turn this into a shufflevector operation.
374   if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
375     if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
376         EI->getOperand(0)->getType() == IE.getType()) {
377       unsigned NumVectorElts = IE.getType()->getNumElements();
378       unsigned ExtractedIdx =
379         cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
380       unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
381
382       if (ExtractedIdx >= NumVectorElts) // Out of range extract.
383         return ReplaceInstUsesWith(IE, VecOp);
384
385       if (InsertedIdx >= NumVectorElts)  // Out of range insert.
386         return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
387
388       // If we are extracting a value from a vector, then inserting it right
389       // back into the same place, just use the input vector.
390       if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
391         return ReplaceInstUsesWith(IE, VecOp);
392
393       // If this insertelement isn't used by some other insertelement, turn it
394       // (and any insertelements it points to), into one big shuffle.
395       if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
396         SmallVector<Constant*, 16> Mask;
397         Value *RHS = 0;
398         Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
399         if (RHS == 0) RHS = UndefValue::get(LHS->getType());
400         // We now have a shuffle of LHS, RHS, Mask.
401         return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask));
402       }
403     }
404   }
405
406   unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
407   APInt UndefElts(VWidth, 0);
408   APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
409   if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
410     if (V != &IE)
411       return ReplaceInstUsesWith(IE, V);
412     return &IE;
413   }
414
415   return 0;
416 }
417
418
419 Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
420   Value *LHS = SVI.getOperand(0);
421   Value *RHS = SVI.getOperand(1);
422   SmallVector<int, 16> Mask = SVI.getShuffleMask();
423
424   bool MadeChange = false;
425
426   // Undefined shuffle mask -> undefined value.
427   if (isa<UndefValue>(SVI.getOperand(2)))
428     return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
429
430   unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
431
432   APInt UndefElts(VWidth, 0);
433   APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
434   if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
435     if (V != &SVI)
436       return ReplaceInstUsesWith(SVI, V);
437     LHS = SVI.getOperand(0);
438     RHS = SVI.getOperand(1);
439     MadeChange = true;
440   }
441
442   unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements();
443
444   // Canonicalize shuffle(x    ,x,mask) -> shuffle(x, undef,mask')
445   // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
446   if (LHS == RHS || isa<UndefValue>(LHS)) {
447     if (isa<UndefValue>(LHS) && LHS == RHS) {
448       // shuffle(undef,undef,mask) -> undef.
449       Value* result = (VWidth == LHSWidth)
450                       ? LHS : UndefValue::get(SVI.getType());
451       return ReplaceInstUsesWith(SVI, result);
452     }
453
454     // Remap any references to RHS to use LHS.
455     SmallVector<Constant*, 16> Elts;
456     for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
457       if (Mask[i] < 0) {
458         Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
459         continue;
460       }
461
462       if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) ||
463           (Mask[i] <  (int)e && isa<UndefValue>(LHS))) {
464         Mask[i] = -1;     // Turn into undef.
465         Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
466       } else {
467         Mask[i] = Mask[i] % e;  // Force to LHS.
468         Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
469                                         Mask[i]));
470       }
471     }
472     SVI.setOperand(0, SVI.getOperand(1));
473     SVI.setOperand(1, UndefValue::get(RHS->getType()));
474     SVI.setOperand(2, ConstantVector::get(Elts));
475     LHS = SVI.getOperand(0);
476     RHS = SVI.getOperand(1);
477     MadeChange = true;
478   }
479
480   if (VWidth == LHSWidth) {
481     // Analyze the shuffle, are the LHS or RHS and identity shuffles?
482     bool isLHSID = true, isRHSID = true;
483
484     for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
485       if (Mask[i] < 0) continue;  // Ignore undef values.
486       // Is this an identity shuffle of the LHS value?
487       isLHSID &= (Mask[i] == (int)i);
488
489       // Is this an identity shuffle of the RHS value?
490       isRHSID &= (Mask[i]-e == i);
491     }
492
493     // Eliminate identity shuffles.
494     if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
495     if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
496   }
497
498   // If the LHS is a shufflevector itself, see if we can combine it with this
499   // one without producing an unusual shuffle.
500   // Cases that might be simplified:
501   // 1.
502   // x1=shuffle(v1,v2,mask1)
503   //  x=shuffle(x1,undef,mask)
504   //        ==>
505   //  x=shuffle(v1,undef,newMask)
506   // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1
507   // 2.
508   // x1=shuffle(v1,undef,mask1)
509   //  x=shuffle(x1,x2,mask)
510   // where v1.size() == mask1.size()
511   //        ==>
512   //  x=shuffle(v1,x2,newMask)
513   // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i]
514   // 3.
515   // x2=shuffle(v2,undef,mask2)
516   //  x=shuffle(x1,x2,mask)
517   // where v2.size() == mask2.size()
518   //        ==>
519   //  x=shuffle(x1,v2,newMask)
520   // newMask[i] = (mask[i] < x1.size())
521   //              ? mask[i] : mask2[mask[i]-x1.size()]+x1.size()
522   // 4.
523   // x1=shuffle(v1,undef,mask1)
524   // x2=shuffle(v2,undef,mask2)
525   //  x=shuffle(x1,x2,mask)
526   // where v1.size() == v2.size()
527   //        ==>
528   //  x=shuffle(v1,v2,newMask)
529   // newMask[i] = (mask[i] < x1.size())
530   //              ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size()
531   //
532   // Here we are really conservative:
533   // we are absolutely afraid of producing a shuffle mask not in the input
534   // program, because the code gen may not be smart enough to turn a merged
535   // shuffle into two specific shuffles: it may produce worse code.  As such,
536   // we only merge two shuffles if the result is either a splat or one of the
537   // input shuffle masks.  In this case, merging the shuffles just removes
538   // one instruction, which we know is safe.  This is good for things like
539   // turning: (splat(splat)) -> splat, or
540   // merge(V[0..n], V[n+1..2n]) -> V[0..2n]
541   ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
542   ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
543   if (LHSShuffle)
544     if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
545       LHSShuffle = NULL;
546   if (RHSShuffle)
547     if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
548       RHSShuffle = NULL;
549   if (!LHSShuffle && !RHSShuffle)
550     return MadeChange ? &SVI : 0;
551
552   Value* LHSOp0 = NULL;
553   Value* LHSOp1 = NULL;
554   Value* RHSOp0 = NULL;
555   unsigned LHSOp0Width = 0;
556   unsigned RHSOp0Width = 0;
557   if (LHSShuffle) {
558     LHSOp0 = LHSShuffle->getOperand(0);
559     LHSOp1 = LHSShuffle->getOperand(1);
560     LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements();
561   }
562   if (RHSShuffle) {
563     RHSOp0 = RHSShuffle->getOperand(0);
564     RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements();
565   }
566   Value* newLHS = LHS;
567   Value* newRHS = RHS;
568   if (LHSShuffle) {
569     // case 1
570     if (isa<UndefValue>(RHS)) {
571       newLHS = LHSOp0;
572       newRHS = LHSOp1;
573     }
574     // case 2 or 4
575     else if (LHSOp0Width == LHSWidth) {
576       newLHS = LHSOp0;
577     }
578   }
579   // case 3 or 4
580   if (RHSShuffle && RHSOp0Width == LHSWidth) {
581     newRHS = RHSOp0;
582   }
583   // case 4
584   if (LHSOp0 == RHSOp0) {
585     newLHS = LHSOp0;
586     newRHS = NULL;
587   }
588
589   if (newLHS == LHS && newRHS == RHS)
590     return MadeChange ? &SVI : 0;
591
592   SmallVector<int, 16> LHSMask;
593   SmallVector<int, 16> RHSMask;
594   if (newLHS != LHS)
595     LHSMask = LHSShuffle->getShuffleMask();
596   if (RHSShuffle && newRHS != RHS)
597     RHSMask = RHSShuffle->getShuffleMask();
598
599   unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
600   SmallVector<int, 16> newMask;
601   bool isSplat = true;
602   int SplatElt = -1;
603   // Create a new mask for the new ShuffleVectorInst so that the new
604   // ShuffleVectorInst is equivalent to the original one.
605   for (unsigned i = 0; i < VWidth; ++i) {
606     int eltMask;
607     if (Mask[i] < 0) {
608       // This element is an undef value.
609       eltMask = -1;
610     } else if (Mask[i] < (int)LHSWidth) {
611       // This element is from left hand side vector operand.
612       //
613       // If LHS is going to be replaced (case 1, 2, or 4), calculate the
614       // new mask value for the element.
615       if (newLHS != LHS) {
616         eltMask = LHSMask[Mask[i]];
617         // If the value selected is an undef value, explicitly specify it
618         // with a -1 mask value.
619         if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1))
620           eltMask = -1;
621       } else
622         eltMask = Mask[i];
623     } else {
624       // This element is from right hand side vector operand
625       //
626       // If the value selected is an undef value, explicitly specify it
627       // with a -1 mask value. (case 1)
628       if (isa<UndefValue>(RHS))
629         eltMask = -1;
630       // If RHS is going to be replaced (case 3 or 4), calculate the
631       // new mask value for the element.
632       else if (newRHS != RHS) {
633         eltMask = RHSMask[Mask[i]-LHSWidth];
634         // If the value selected is an undef value, explicitly specify it
635         // with a -1 mask value.
636         if (eltMask >= (int)RHSOp0Width) {
637           assert(isa<UndefValue>(RHSShuffle->getOperand(1))
638                  && "should have been check above");
639           eltMask = -1;
640         }
641       } else
642         eltMask = Mask[i]-LHSWidth;
643
644       // If LHS's width is changed, shift the mask value accordingly.
645       // If newRHS == NULL, i.e. LHSOp0 == RHSOp0, we want to remap any
646       // references from RHSOp0 to LHSOp0, so we don't need to shift the mask.
647       // If newRHS == newLHS, we want to remap any references from newRHS to
648       // newLHS so that we can properly identify splats that may occur due to
649       // obfuscation accross the two vectors.
650       if (eltMask >= 0 && newRHS != NULL && newLHS != newRHS)
651         eltMask += newLHSWidth;
652     }
653
654     // Check if this could still be a splat.
655     if (eltMask >= 0) {
656       if (SplatElt >= 0 && SplatElt != eltMask)
657         isSplat = false;
658       SplatElt = eltMask;
659     }
660
661     newMask.push_back(eltMask);
662   }
663
664   // If the result mask is equal to one of the original shuffle masks,
665   // or is a splat, do the replacement.
666   if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
667     SmallVector<Constant*, 16> Elts;
668     Type *Int32Ty = Type::getInt32Ty(SVI.getContext());
669     for (unsigned i = 0, e = newMask.size(); i != e; ++i) {
670       if (newMask[i] < 0) {
671         Elts.push_back(UndefValue::get(Int32Ty));
672       } else {
673         Elts.push_back(ConstantInt::get(Int32Ty, newMask[i]));
674       }
675     }
676     if (newRHS == NULL)
677       newRHS = UndefValue::get(newLHS->getType());
678     return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts));
679   }
680
681   return MadeChange ? &SVI : 0;
682 }