1 //===- InstCombineVectorOps.cpp -------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements instcombine for ExtractElement, InsertElement and
13 //===----------------------------------------------------------------------===//
15 #include "InstCombine.h"
16 #include "llvm/Support/PatternMatch.h"
18 using namespace PatternMatch;
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;
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)
34 Instruction *I = dyn_cast<Instruction>(V);
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)))
42 if (I->getOpcode() == Instruction::Load && I->hasOneUse())
44 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
45 if (BO->hasOneUse() &&
46 (CheapToScalarize(BO->getOperand(0), isConstant) ||
47 CheapToScalarize(BO->getOperand(1), isConstant)))
49 if (CmpInst *CI = dyn_cast<CmpInst>(I))
50 if (CI->hasOneUse() &&
51 (CheapToScalarize(CI->getOperand(0), isConstant) ||
52 CheapToScalarize(CI->getOperand(1), isConstant)))
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());
68 if (Constant *C = dyn_cast<Constant>(V))
69 return C->getAggregateElement(EltNo);
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)))
75 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
77 // If this is an insert to the element we are looking for, return the
80 return III->getOperand(1);
82 // Otherwise, the insertelement doesn't modify the value, recurse on its
84 return FindScalarElement(III->getOperand(0), EltNo);
87 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
88 unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
89 int InEl = SVI->getMaskValue(EltNo);
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);
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);
104 // Otherwise, we don't know.
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));
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();
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()));
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
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)) {
140 if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
141 return ReplaceInstUsesWith(EI, Elt);
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());
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)))) {
161 Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
162 EI.getName()+".lhs");
164 Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
165 EI.getName()+".rhs");
166 return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
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));
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());
186 SVI->getOperand(0)->getType()->getVectorNumElements();
189 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
190 if (SrcIdx < (int)LHSWidth)
191 Src = SVI->getOperand(0);
194 Src = SVI->getOperand(1);
196 Type *Int32Ty = Type::getInt32Ty(EI.getContext());
197 return ExtractElementInst::Create(Src,
198 ConstantInt::get(Int32Ty,
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());
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();
224 if (isa<UndefValue>(V)) {
225 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
230 for (unsigned i = 0; i != NumElts; ++i)
231 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
236 for (unsigned i = 0; i != NumElts; ++i)
237 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()),
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);
248 if (!isa<ConstantInt>(IdxOp))
250 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
252 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
253 // Okay, we can handle this if the vector we are insertinting into is
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()));
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();
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
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()),
277 assert(EI->getOperand(0) == RHS);
278 Mask[InsertedIdx % NumElts] =
279 ConstantInt::get(Type::getInt32Ty(V->getContext()),
280 ExtractedIdx+NumElts);
288 // TODO: Handle shufflevector here!
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,
298 assert(V->getType()->isVectorTy() &&
299 (RHS == 0 || V->getType() == RHS->getType()) &&
301 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
303 if (isa<UndefValue>(V)) {
304 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
308 if (isa<ConstantAggregateZero>(V)) {
309 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
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);
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();
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);
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()),
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);
355 // TODO: Handle shufflevector here!
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));
363 Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
364 Value *VecOp = IE.getOperand(0);
365 Value *ScalarOp = IE.getOperand(1);
366 Value *IdxOp = IE.getOperand(2);
368 // Inserting an undef or into an undefined place, remove this.
369 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
370 ReplaceInstUsesWith(IE, VecOp);
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();
382 if (ExtractedIdx >= NumVectorElts) // Out of range extract.
383 return ReplaceInstUsesWith(IE, VecOp);
385 if (InsertedIdx >= NumVectorElts) // Out of range insert.
386 return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
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);
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;
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));
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)) {
411 return ReplaceInstUsesWith(IE, V);
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();
424 bool MadeChange = false;
426 // Undefined shuffle mask -> undefined value.
427 if (isa<UndefValue>(SVI.getOperand(2)))
428 return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
430 unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
432 APInt UndefElts(VWidth, 0);
433 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
434 if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
436 return ReplaceInstUsesWith(SVI, V);
437 LHS = SVI.getOperand(0);
438 RHS = SVI.getOperand(1);
442 unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements();
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);
454 // Remap any references to RHS to use LHS.
455 SmallVector<Constant*, 16> Elts;
456 for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
458 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
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())));
467 Mask[i] = Mask[i] % e; // Force to LHS.
468 Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
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);
480 if (VWidth == LHSWidth) {
481 // Analyze the shuffle, are the LHS or RHS and identity shuffles?
482 bool isLHSID = true, isRHSID = true;
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);
489 // Is this an identity shuffle of the RHS value?
490 isRHSID &= (Mask[i]-e == i);
493 // Eliminate identity shuffles.
494 if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
495 if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
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:
502 // x1=shuffle(v1,v2,mask1)
503 // x=shuffle(x1,undef,mask)
505 // x=shuffle(v1,undef,newMask)
506 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1
508 // x1=shuffle(v1,undef,mask1)
509 // x=shuffle(x1,x2,mask)
510 // where v1.size() == mask1.size()
512 // x=shuffle(v1,x2,newMask)
513 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i]
515 // x2=shuffle(v2,undef,mask2)
516 // x=shuffle(x1,x2,mask)
517 // where v2.size() == mask2.size()
519 // x=shuffle(x1,v2,newMask)
520 // newMask[i] = (mask[i] < x1.size())
521 // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size()
523 // x1=shuffle(v1,undef,mask1)
524 // x2=shuffle(v2,undef,mask2)
525 // x=shuffle(x1,x2,mask)
526 // where v1.size() == v2.size()
528 // x=shuffle(v1,v2,newMask)
529 // newMask[i] = (mask[i] < x1.size())
530 // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size()
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);
544 if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
547 if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
549 if (!LHSShuffle && !RHSShuffle)
550 return MadeChange ? &SVI : 0;
552 Value* LHSOp0 = NULL;
553 Value* LHSOp1 = NULL;
554 Value* RHSOp0 = NULL;
555 unsigned LHSOp0Width = 0;
556 unsigned RHSOp0Width = 0;
558 LHSOp0 = LHSShuffle->getOperand(0);
559 LHSOp1 = LHSShuffle->getOperand(1);
560 LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements();
563 RHSOp0 = RHSShuffle->getOperand(0);
564 RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements();
570 if (isa<UndefValue>(RHS)) {
575 else if (LHSOp0Width == LHSWidth) {
580 if (RHSShuffle && RHSOp0Width == LHSWidth) {
584 if (LHSOp0 == RHSOp0) {
589 if (newLHS == LHS && newRHS == RHS)
590 return MadeChange ? &SVI : 0;
592 SmallVector<int, 16> LHSMask;
593 SmallVector<int, 16> RHSMask;
595 LHSMask = LHSShuffle->getShuffleMask();
596 if (RHSShuffle && newRHS != RHS)
597 RHSMask = RHSShuffle->getShuffleMask();
599 unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
600 SmallVector<int, 16> newMask;
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) {
608 // This element is an undef value.
610 } else if (Mask[i] < (int)LHSWidth) {
611 // This element is from left hand side vector operand.
613 // If LHS is going to be replaced (case 1, 2, or 4), calculate the
614 // new mask value for the element.
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))
624 // This element is from right hand side vector operand
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))
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");
642 eltMask = Mask[i]-LHSWidth;
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;
654 // Check if this could still be a splat.
656 if (SplatElt >= 0 && SplatElt != eltMask)
661 newMask.push_back(eltMask);
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));
673 Elts.push_back(ConstantInt::get(Int32Ty, newMask[i]));
677 newRHS = UndefValue::get(newLHS->getType());
678 return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts));
681 return MadeChange ? &SVI : 0;