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"
18 /// CheapToScalarize - Return true if the value is cheaper to scalarize than it
19 /// is to leave as a vector operation. isConstant indicates whether we're
20 /// extracting one known element. If false we're extracting a variable index.
21 static bool CheapToScalarize(Value *V, bool isConstant) {
22 if (Constant *C = dyn_cast<Constant>(V)) {
23 if (isConstant) return true;
25 // If all elts are the same, we can extract it and use any of the values.
26 Constant *Op0 = C->getAggregateElement(0U);
27 for (unsigned i = 1, e = V->getType()->getVectorNumElements(); i != e; ++i)
28 if (C->getAggregateElement(i) != Op0)
32 Instruction *I = dyn_cast<Instruction>(V);
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)))
40 if (I->getOpcode() == Instruction::Load && I->hasOneUse())
42 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I))
43 if (BO->hasOneUse() &&
44 (CheapToScalarize(BO->getOperand(0), isConstant) ||
45 CheapToScalarize(BO->getOperand(1), isConstant)))
47 if (CmpInst *CI = dyn_cast<CmpInst>(I))
48 if (CI->hasOneUse() &&
49 (CheapToScalarize(CI->getOperand(0), isConstant) ||
50 CheapToScalarize(CI->getOperand(1), isConstant)))
56 /// FindScalarElement - Given a vector and an element number, see if the scalar
57 /// value is already around as a register, for example if it were inserted then
58 /// extracted from the vector.
59 static Value *FindScalarElement(Value *V, unsigned EltNo) {
60 assert(V->getType()->isVectorTy() && "Not looking at a vector?");
61 VectorType *VTy = cast<VectorType>(V->getType());
62 unsigned Width = VTy->getNumElements();
63 if (EltNo >= Width) // Out of range access.
64 return UndefValue::get(VTy->getElementType());
66 if (Constant *C = dyn_cast<Constant>(V))
67 return C->getAggregateElement(EltNo);
69 if (InsertElementInst *III = dyn_cast<InsertElementInst>(V)) {
70 // If this is an insert to a variable element, we don't know what it is.
71 if (!isa<ConstantInt>(III->getOperand(2)))
73 unsigned IIElt = cast<ConstantInt>(III->getOperand(2))->getZExtValue();
75 // If this is an insert to the element we are looking for, return the
78 return III->getOperand(1);
80 // Otherwise, the insertelement doesn't modify the value, recurse on its
82 return FindScalarElement(III->getOperand(0), EltNo);
85 if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(V)) {
86 unsigned LHSWidth = SVI->getOperand(0)->getType()->getVectorNumElements();
87 int InEl = SVI->getMaskValue(EltNo);
89 return UndefValue::get(VTy->getElementType());
90 if (InEl < (int)LHSWidth)
91 return FindScalarElement(SVI->getOperand(0), InEl);
92 return FindScalarElement(SVI->getOperand(1), InEl - LHSWidth);
95 // Otherwise, we don't know.
99 Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) {
100 // If vector val is constant with all elements the same, replace EI with
101 // that element. We handle a known element # below.
102 if (Constant *C = dyn_cast<Constant>(EI.getOperand(0)))
103 if (CheapToScalarize(C, false))
104 return ReplaceInstUsesWith(EI, C->getAggregateElement(0U));
106 // If extracting a specified index from the vector, see if we can recursively
107 // find a previously computed scalar that was inserted into the vector.
108 if (ConstantInt *IdxC = dyn_cast<ConstantInt>(EI.getOperand(1))) {
109 unsigned IndexVal = IdxC->getZExtValue();
110 unsigned VectorWidth = EI.getVectorOperandType()->getNumElements();
112 // If this is extracting an invalid index, turn this into undef, to avoid
113 // crashing the code below.
114 if (IndexVal >= VectorWidth)
115 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
117 // This instruction only demands the single element from the input vector.
118 // If the input vector has a single use, simplify it based on this use
120 if (EI.getOperand(0)->hasOneUse() && VectorWidth != 1) {
121 APInt UndefElts(VectorWidth, 0);
122 APInt DemandedMask(VectorWidth, 0);
123 DemandedMask.setBit(IndexVal);
124 if (Value *V = SimplifyDemandedVectorElts(EI.getOperand(0),
125 DemandedMask, UndefElts)) {
131 if (Value *Elt = FindScalarElement(EI.getOperand(0), IndexVal))
132 return ReplaceInstUsesWith(EI, Elt);
134 // If the this extractelement is directly using a bitcast from a vector of
135 // the same number of elements, see if we can find the source element from
136 // it. In this case, we will end up needing to bitcast the scalars.
137 if (BitCastInst *BCI = dyn_cast<BitCastInst>(EI.getOperand(0))) {
138 if (VectorType *VT = dyn_cast<VectorType>(BCI->getOperand(0)->getType()))
139 if (VT->getNumElements() == VectorWidth)
140 if (Value *Elt = FindScalarElement(BCI->getOperand(0), IndexVal))
141 return new BitCastInst(Elt, EI.getType());
145 if (Instruction *I = dyn_cast<Instruction>(EI.getOperand(0))) {
146 // Push extractelement into predecessor operation if legal and
147 // profitable to do so
148 if (BinaryOperator *BO = dyn_cast<BinaryOperator>(I)) {
149 if (I->hasOneUse() &&
150 CheapToScalarize(BO, isa<ConstantInt>(EI.getOperand(1)))) {
152 Builder->CreateExtractElement(BO->getOperand(0), EI.getOperand(1),
153 EI.getName()+".lhs");
155 Builder->CreateExtractElement(BO->getOperand(1), EI.getOperand(1),
156 EI.getName()+".rhs");
157 return BinaryOperator::Create(BO->getOpcode(), newEI0, newEI1);
159 } else if (InsertElementInst *IE = dyn_cast<InsertElementInst>(I)) {
160 // Extracting the inserted element?
161 if (IE->getOperand(2) == EI.getOperand(1))
162 return ReplaceInstUsesWith(EI, IE->getOperand(1));
163 // If the inserted and extracted elements are constants, they must not
164 // be the same value, extract from the pre-inserted value instead.
165 if (isa<Constant>(IE->getOperand(2)) && isa<Constant>(EI.getOperand(1))) {
166 Worklist.AddValue(EI.getOperand(0));
167 EI.setOperand(0, IE->getOperand(0));
170 } else if (ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(I)) {
171 // If this is extracting an element from a shufflevector, figure out where
172 // it came from and extract from the appropriate input element instead.
173 if (ConstantInt *Elt = dyn_cast<ConstantInt>(EI.getOperand(1))) {
174 int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
177 SVI->getOperand(0)->getType()->getVectorNumElements();
180 return ReplaceInstUsesWith(EI, UndefValue::get(EI.getType()));
181 if (SrcIdx < (int)LHSWidth)
182 Src = SVI->getOperand(0);
185 Src = SVI->getOperand(1);
187 Type *Int32Ty = Type::getInt32Ty(EI.getContext());
188 return ExtractElementInst::Create(Src,
189 ConstantInt::get(Int32Ty,
192 } else if (CastInst *CI = dyn_cast<CastInst>(I)) {
193 // Canonicalize extractelement(cast) -> cast(extractelement)
194 // bitcasts can change the number of vector elements and they cost nothing
195 if (CI->hasOneUse() && EI.hasOneUse() &&
196 (CI->getOpcode() != Instruction::BitCast)) {
197 Value *EE = Builder->CreateExtractElement(CI->getOperand(0),
198 EI.getIndexOperand());
199 return CastInst::Create(CI->getOpcode(), EE, EI.getType());
206 /// CollectSingleShuffleElements - If V is a shuffle of values that ONLY returns
207 /// elements from either LHS or RHS, return the shuffle mask and true.
208 /// Otherwise, return false.
209 static bool CollectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
210 SmallVectorImpl<Constant*> &Mask) {
211 assert(V->getType() == LHS->getType() && V->getType() == RHS->getType() &&
212 "Invalid CollectSingleShuffleElements");
213 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
215 if (isa<UndefValue>(V)) {
216 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
221 for (unsigned i = 0; i != NumElts; ++i)
222 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
227 for (unsigned i = 0; i != NumElts; ++i)
228 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()),
233 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
234 // If this is an insert of an extract from some other vector, include it.
235 Value *VecOp = IEI->getOperand(0);
236 Value *ScalarOp = IEI->getOperand(1);
237 Value *IdxOp = IEI->getOperand(2);
239 if (!isa<ConstantInt>(IdxOp))
241 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
243 if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
244 // Okay, we can handle this if the vector we are insertinting into is
246 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
247 // If so, update the mask to reflect the inserted undef.
248 Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext()));
251 } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
252 if (isa<ConstantInt>(EI->getOperand(1)) &&
253 EI->getOperand(0)->getType() == V->getType()) {
254 unsigned ExtractedIdx =
255 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
257 // This must be extracting from either LHS or RHS.
258 if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
259 // Okay, we can handle this if the vector we are insertinting into is
261 if (CollectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
262 // If so, update the mask to reflect the inserted value.
263 if (EI->getOperand(0) == LHS) {
264 Mask[InsertedIdx % NumElts] =
265 ConstantInt::get(Type::getInt32Ty(V->getContext()),
268 assert(EI->getOperand(0) == RHS);
269 Mask[InsertedIdx % NumElts] =
270 ConstantInt::get(Type::getInt32Ty(V->getContext()),
271 ExtractedIdx+NumElts);
279 // TODO: Handle shufflevector here!
284 /// CollectShuffleElements - We are building a shuffle of V, using RHS as the
285 /// RHS of the shuffle instruction, if it is not null. Return a shuffle mask
286 /// that computes V and the LHS value of the shuffle.
287 static Value *CollectShuffleElements(Value *V, SmallVectorImpl<Constant*> &Mask,
289 assert(V->getType()->isVectorTy() &&
290 (RHS == 0 || V->getType() == RHS->getType()) &&
292 unsigned NumElts = cast<VectorType>(V->getType())->getNumElements();
294 if (isa<UndefValue>(V)) {
295 Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
299 if (isa<ConstantAggregateZero>(V)) {
300 Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
304 if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
305 // If this is an insert of an extract from some other vector, include it.
306 Value *VecOp = IEI->getOperand(0);
307 Value *ScalarOp = IEI->getOperand(1);
308 Value *IdxOp = IEI->getOperand(2);
310 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
311 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
312 EI->getOperand(0)->getType() == V->getType()) {
313 unsigned ExtractedIdx =
314 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
315 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
317 // Either the extracted from or inserted into vector must be RHSVec,
318 // otherwise we'd end up with a shuffle of three inputs.
319 if (EI->getOperand(0) == RHS || RHS == 0) {
320 RHS = EI->getOperand(0);
321 Value *V = CollectShuffleElements(VecOp, Mask, RHS);
322 Mask[InsertedIdx % NumElts] =
323 ConstantInt::get(Type::getInt32Ty(V->getContext()),
324 NumElts+ExtractedIdx);
329 Value *V = CollectShuffleElements(EI->getOperand(0), Mask, RHS);
330 // Everything but the extracted element is replaced with the RHS.
331 for (unsigned i = 0; i != NumElts; ++i) {
332 if (i != InsertedIdx)
333 Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()),
339 // If this insertelement is a chain that comes from exactly these two
340 // vectors, return the vector and the effective shuffle.
341 if (CollectSingleShuffleElements(IEI, EI->getOperand(0), RHS, Mask))
342 return EI->getOperand(0);
346 // TODO: Handle shufflevector here!
348 // Otherwise, can't do anything fancy. Return an identity vector.
349 for (unsigned i = 0; i != NumElts; ++i)
350 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i));
354 Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) {
355 Value *VecOp = IE.getOperand(0);
356 Value *ScalarOp = IE.getOperand(1);
357 Value *IdxOp = IE.getOperand(2);
359 // Inserting an undef or into an undefined place, remove this.
360 if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
361 ReplaceInstUsesWith(IE, VecOp);
363 // If the inserted element was extracted from some other vector, and if the
364 // indexes are constant, try to turn this into a shufflevector operation.
365 if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
366 if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp) &&
367 EI->getOperand(0)->getType() == IE.getType()) {
368 unsigned NumVectorElts = IE.getType()->getNumElements();
369 unsigned ExtractedIdx =
370 cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
371 unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
373 if (ExtractedIdx >= NumVectorElts) // Out of range extract.
374 return ReplaceInstUsesWith(IE, VecOp);
376 if (InsertedIdx >= NumVectorElts) // Out of range insert.
377 return ReplaceInstUsesWith(IE, UndefValue::get(IE.getType()));
379 // If we are extracting a value from a vector, then inserting it right
380 // back into the same place, just use the input vector.
381 if (EI->getOperand(0) == VecOp && ExtractedIdx == InsertedIdx)
382 return ReplaceInstUsesWith(IE, VecOp);
384 // If this insertelement isn't used by some other insertelement, turn it
385 // (and any insertelements it points to), into one big shuffle.
386 if (!IE.hasOneUse() || !isa<InsertElementInst>(IE.use_back())) {
387 SmallVector<Constant*, 16> Mask;
389 Value *LHS = CollectShuffleElements(&IE, Mask, RHS);
390 if (RHS == 0) RHS = UndefValue::get(LHS->getType());
391 // We now have a shuffle of LHS, RHS, Mask.
392 return new ShuffleVectorInst(LHS, RHS, ConstantVector::get(Mask));
397 unsigned VWidth = cast<VectorType>(VecOp->getType())->getNumElements();
398 APInt UndefElts(VWidth, 0);
399 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
400 if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
402 return ReplaceInstUsesWith(IE, V);
410 Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
411 Value *LHS = SVI.getOperand(0);
412 Value *RHS = SVI.getOperand(1);
413 SmallVector<int, 16> Mask = SVI.getShuffleMask();
415 bool MadeChange = false;
417 // Undefined shuffle mask -> undefined value.
418 if (isa<UndefValue>(SVI.getOperand(2)))
419 return ReplaceInstUsesWith(SVI, UndefValue::get(SVI.getType()));
421 unsigned VWidth = cast<VectorType>(SVI.getType())->getNumElements();
423 APInt UndefElts(VWidth, 0);
424 APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
425 if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
427 return ReplaceInstUsesWith(SVI, V);
428 LHS = SVI.getOperand(0);
429 RHS = SVI.getOperand(1);
433 unsigned LHSWidth = cast<VectorType>(LHS->getType())->getNumElements();
435 // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
436 // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
437 if (LHS == RHS || isa<UndefValue>(LHS)) {
438 if (isa<UndefValue>(LHS) && LHS == RHS) {
439 // shuffle(undef,undef,mask) -> undef.
440 Value* result = (VWidth == LHSWidth)
441 ? LHS : UndefValue::get(SVI.getType());
442 return ReplaceInstUsesWith(SVI, result);
445 // Remap any references to RHS to use LHS.
446 SmallVector<Constant*, 16> Elts;
447 for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
449 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
453 if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) ||
454 (Mask[i] < (int)e && isa<UndefValue>(LHS))) {
455 Mask[i] = -1; // Turn into undef.
456 Elts.push_back(UndefValue::get(Type::getInt32Ty(SVI.getContext())));
458 Mask[i] = Mask[i] % e; // Force to LHS.
459 Elts.push_back(ConstantInt::get(Type::getInt32Ty(SVI.getContext()),
463 SVI.setOperand(0, SVI.getOperand(1));
464 SVI.setOperand(1, UndefValue::get(RHS->getType()));
465 SVI.setOperand(2, ConstantVector::get(Elts));
466 LHS = SVI.getOperand(0);
467 RHS = SVI.getOperand(1);
471 if (VWidth == LHSWidth) {
472 // Analyze the shuffle, are the LHS or RHS and identity shuffles?
473 bool isLHSID = true, isRHSID = true;
475 for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
476 if (Mask[i] < 0) continue; // Ignore undef values.
477 // Is this an identity shuffle of the LHS value?
478 isLHSID &= (Mask[i] == (int)i);
480 // Is this an identity shuffle of the RHS value?
481 isRHSID &= (Mask[i]-e == i);
484 // Eliminate identity shuffles.
485 if (isLHSID) return ReplaceInstUsesWith(SVI, LHS);
486 if (isRHSID) return ReplaceInstUsesWith(SVI, RHS);
489 // If the LHS is a shufflevector itself, see if we can combine it with this
490 // one without producing an unusual shuffle.
491 // Cases that might be simplified:
493 // x1=shuffle(v1,v2,mask1)
494 // x=shuffle(x1,undef,mask)
496 // x=shuffle(v1,undef,newMask)
497 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1
499 // x1=shuffle(v1,undef,mask1)
500 // x=shuffle(x1,x2,mask)
501 // where v1.size() == mask1.size()
503 // x=shuffle(v1,x2,newMask)
504 // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i]
506 // x2=shuffle(v2,undef,mask2)
507 // x=shuffle(x1,x2,mask)
508 // where v2.size() == mask2.size()
510 // x=shuffle(x1,v2,newMask)
511 // newMask[i] = (mask[i] < x1.size())
512 // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size()
514 // x1=shuffle(v1,undef,mask1)
515 // x2=shuffle(v2,undef,mask2)
516 // x=shuffle(x1,x2,mask)
517 // where v1.size() == v2.size()
519 // x=shuffle(v1,v2,newMask)
520 // newMask[i] = (mask[i] < x1.size())
521 // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size()
523 // Here we are really conservative:
524 // we are absolutely afraid of producing a shuffle mask not in the input
525 // program, because the code gen may not be smart enough to turn a merged
526 // shuffle into two specific shuffles: it may produce worse code. As such,
527 // we only merge two shuffles if the result is either a splat or one of the
528 // input shuffle masks. In this case, merging the shuffles just removes
529 // one instruction, which we know is safe. This is good for things like
530 // turning: (splat(splat)) -> splat, or
531 // merge(V[0..n], V[n+1..2n]) -> V[0..2n]
532 ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
533 ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
535 if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
538 if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
540 if (!LHSShuffle && !RHSShuffle)
541 return MadeChange ? &SVI : 0;
543 Value* LHSOp0 = NULL;
544 Value* LHSOp1 = NULL;
545 Value* RHSOp0 = NULL;
546 unsigned LHSOp0Width = 0;
547 unsigned RHSOp0Width = 0;
549 LHSOp0 = LHSShuffle->getOperand(0);
550 LHSOp1 = LHSShuffle->getOperand(1);
551 LHSOp0Width = cast<VectorType>(LHSOp0->getType())->getNumElements();
554 RHSOp0 = RHSShuffle->getOperand(0);
555 RHSOp0Width = cast<VectorType>(RHSOp0->getType())->getNumElements();
561 if (isa<UndefValue>(RHS)) {
566 else if (LHSOp0Width == LHSWidth) {
571 if (RHSShuffle && RHSOp0Width == LHSWidth) {
575 if (LHSOp0 == RHSOp0) {
580 if (newLHS == LHS && newRHS == RHS)
581 return MadeChange ? &SVI : 0;
583 SmallVector<int, 16> LHSMask;
584 SmallVector<int, 16> RHSMask;
586 LHSMask = LHSShuffle->getShuffleMask();
587 if (RHSShuffle && newRHS != RHS)
588 RHSMask = RHSShuffle->getShuffleMask();
590 unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
591 SmallVector<int, 16> newMask;
594 // Create a new mask for the new ShuffleVectorInst so that the new
595 // ShuffleVectorInst is equivalent to the original one.
596 for (unsigned i = 0; i < VWidth; ++i) {
599 // This element is an undef value.
601 } else if (Mask[i] < (int)LHSWidth) {
602 // This element is from left hand side vector operand.
604 // If LHS is going to be replaced (case 1, 2, or 4), calculate the
605 // new mask value for the element.
607 eltMask = LHSMask[Mask[i]];
608 // If the value selected is an undef value, explicitly specify it
609 // with a -1 mask value.
610 if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1))
616 // This element is from right hand side vector operand
618 // If the value selected is an undef value, explicitly specify it
619 // with a -1 mask value. (case 1)
620 if (isa<UndefValue>(RHS))
622 // If RHS is going to be replaced (case 3 or 4), calculate the
623 // new mask value for the element.
624 else if (newRHS != RHS) {
625 eltMask = RHSMask[Mask[i]-LHSWidth];
626 // If the value selected is an undef value, explicitly specify it
627 // with a -1 mask value.
628 if (eltMask >= (int)RHSOp0Width) {
629 assert(isa<UndefValue>(RHSShuffle->getOperand(1))
630 && "should have been check above");
635 eltMask = Mask[i]-LHSWidth;
637 // If LHS's width is changed, shift the mask value accordingly.
638 // If newRHS == NULL, i.e. LHSOp0 == RHSOp0, we want to remap any
639 // references from RHSOp0 to LHSOp0, so we don't need to shift the mask.
640 // If newRHS == newLHS, we want to remap any references from newRHS to
641 // newLHS so that we can properly identify splats that may occur due to
642 // obfuscation accross the two vectors.
643 if (eltMask >= 0 && newRHS != NULL && newLHS != newRHS)
644 eltMask += newLHSWidth;
647 // Check if this could still be a splat.
649 if (SplatElt >= 0 && SplatElt != eltMask)
654 newMask.push_back(eltMask);
657 // If the result mask is equal to one of the original shuffle masks,
658 // or is a splat, do the replacement.
659 if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
660 SmallVector<Constant*, 16> Elts;
661 Type *Int32Ty = Type::getInt32Ty(SVI.getContext());
662 for (unsigned i = 0, e = newMask.size(); i != e; ++i) {
663 if (newMask[i] < 0) {
664 Elts.push_back(UndefValue::get(Int32Ty));
666 Elts.push_back(ConstantInt::get(Int32Ty, newMask[i]));
670 newRHS = UndefValue::get(newLHS->getType());
671 return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts));
674 return MadeChange ? &SVI : 0;