LLVM  8.0.1
InstCombineVectorOps.cpp
Go to the documentation of this file.
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 "InstCombineInternal.h"
16 #include "llvm/ADT/APInt.h"
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/IR/BasicBlock.h"
24 #include "llvm/IR/Constant.h"
25 #include "llvm/IR/Constants.h"
26 #include "llvm/IR/DerivedTypes.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/IR/Instruction.h"
29 #include "llvm/IR/Instructions.h"
30 #include "llvm/IR/Operator.h"
31 #include "llvm/IR/PatternMatch.h"
32 #include "llvm/IR/Type.h"
33 #include "llvm/IR/User.h"
34 #include "llvm/IR/Value.h"
35 #include "llvm/Support/Casting.h"
38 #include <cassert>
39 #include <cstdint>
40 #include <iterator>
41 #include <utility>
42 
43 using namespace llvm;
44 using namespace PatternMatch;
45 
46 #define DEBUG_TYPE "instcombine"
47 
48 /// Return true if the value is cheaper to scalarize than it is to leave as a
49 /// vector operation. IsConstantExtractIndex indicates whether we are extracting
50 /// one known element from a vector constant.
51 ///
52 /// FIXME: It's possible to create more instructions than previously existed.
53 static bool cheapToScalarize(Value *V, bool IsConstantExtractIndex) {
54  // If we can pick a scalar constant value out of a vector, that is free.
55  if (auto *C = dyn_cast<Constant>(V))
56  return IsConstantExtractIndex || C->getSplatValue();
57 
58  // An insertelement to the same constant index as our extract will simplify
59  // to the scalar inserted element. An insertelement to a different constant
60  // index is irrelevant to our extract.
62  return IsConstantExtractIndex;
63 
64  if (match(V, m_OneUse(m_Load(m_Value()))))
65  return true;
66 
67  Value *V0, *V1;
68  if (match(V, m_OneUse(m_BinOp(m_Value(V0), m_Value(V1)))))
69  if (cheapToScalarize(V0, IsConstantExtractIndex) ||
70  cheapToScalarize(V1, IsConstantExtractIndex))
71  return true;
72 
73  CmpInst::Predicate UnusedPred;
74  if (match(V, m_OneUse(m_Cmp(UnusedPred, m_Value(V0), m_Value(V1)))))
75  if (cheapToScalarize(V0, IsConstantExtractIndex) ||
76  cheapToScalarize(V1, IsConstantExtractIndex))
77  return true;
78 
79  return false;
80 }
81 
82 // If we have a PHI node with a vector type that is only used to feed
83 // itself and be an operand of extractelement at a constant location,
84 // try to replace the PHI of the vector type with a PHI of a scalar type.
85 Instruction *InstCombiner::scalarizePHI(ExtractElementInst &EI, PHINode *PN) {
87  // The users we want the PHI to have are:
88  // 1) The EI ExtractElement (we already know this)
89  // 2) Possibly more ExtractElements with the same index.
90  // 3) Another operand, which will feed back into the PHI.
91  Instruction *PHIUser = nullptr;
92  for (auto U : PN->users()) {
93  if (ExtractElementInst *EU = dyn_cast<ExtractElementInst>(U)) {
94  if (EI.getIndexOperand() == EU->getIndexOperand())
95  Extracts.push_back(EU);
96  else
97  return nullptr;
98  } else if (!PHIUser) {
99  PHIUser = cast<Instruction>(U);
100  } else {
101  return nullptr;
102  }
103  }
104 
105  if (!PHIUser)
106  return nullptr;
107 
108  // Verify that this PHI user has one use, which is the PHI itself,
109  // and that it is a binary operation which is cheap to scalarize.
110  // otherwise return nullptr.
111  if (!PHIUser->hasOneUse() || !(PHIUser->user_back() == PN) ||
112  !(isa<BinaryOperator>(PHIUser)) || !cheapToScalarize(PHIUser, true))
113  return nullptr;
114 
115  // Create a scalar PHI node that will replace the vector PHI node
116  // just before the current PHI node.
117  PHINode *scalarPHI = cast<PHINode>(InsertNewInstWith(
118  PHINode::Create(EI.getType(), PN->getNumIncomingValues(), ""), *PN));
119  // Scalarize each PHI operand.
120  for (unsigned i = 0; i < PN->getNumIncomingValues(); i++) {
121  Value *PHIInVal = PN->getIncomingValue(i);
122  BasicBlock *inBB = PN->getIncomingBlock(i);
123  Value *Elt = EI.getIndexOperand();
124  // If the operand is the PHI induction variable:
125  if (PHIInVal == PHIUser) {
126  // Scalarize the binary operation. Its first operand is the
127  // scalar PHI, and the second operand is extracted from the other
128  // vector operand.
129  BinaryOperator *B0 = cast<BinaryOperator>(PHIUser);
130  unsigned opId = (B0->getOperand(0) == PN) ? 1 : 0;
131  Value *Op = InsertNewInstWith(
132  ExtractElementInst::Create(B0->getOperand(opId), Elt,
133  B0->getOperand(opId)->getName() + ".Elt"),
134  *B0);
135  Value *newPHIUser = InsertNewInstWith(
137  scalarPHI, Op, B0), *B0);
138  scalarPHI->addIncoming(newPHIUser, inBB);
139  } else {
140  // Scalarize PHI input:
141  Instruction *newEI = ExtractElementInst::Create(PHIInVal, Elt, "");
142  // Insert the new instruction into the predecessor basic block.
143  Instruction *pos = dyn_cast<Instruction>(PHIInVal);
144  BasicBlock::iterator InsertPos;
145  if (pos && !isa<PHINode>(pos)) {
146  InsertPos = ++pos->getIterator();
147  } else {
148  InsertPos = inBB->getFirstInsertionPt();
149  }
150 
151  InsertNewInstWith(newEI, *InsertPos);
152 
153  scalarPHI->addIncoming(newEI, inBB);
154  }
155  }
156 
157  for (auto E : Extracts)
158  replaceInstUsesWith(*E, scalarPHI);
159 
160  return &EI;
161 }
162 
164  InstCombiner::BuilderTy &Builder,
165  bool IsBigEndian) {
166  Value *X;
167  uint64_t ExtIndexC;
168  if (!match(Ext.getVectorOperand(), m_BitCast(m_Value(X))) ||
169  !X->getType()->isVectorTy() ||
170  !match(Ext.getIndexOperand(), m_ConstantInt(ExtIndexC)))
171  return nullptr;
172 
173  // If this extractelement is using a bitcast from a vector of the same number
174  // of elements, see if we can find the source element from the source vector:
175  // extelt (bitcast VecX), IndexC --> bitcast X[IndexC]
176  Type *SrcTy = X->getType();
177  Type *DestTy = Ext.getType();
178  unsigned NumSrcElts = SrcTy->getVectorNumElements();
179  unsigned NumElts = Ext.getVectorOperandType()->getNumElements();
180  if (NumSrcElts == NumElts)
181  if (Value *Elt = findScalarElement(X, ExtIndexC))
182  return new BitCastInst(Elt, DestTy);
183 
184  // If the source elements are wider than the destination, try to shift and
185  // truncate a subset of scalar bits of an insert op.
186  if (NumSrcElts < NumElts) {
187  Value *Scalar;
188  uint64_t InsIndexC;
189  if (!match(X, m_InsertElement(m_Value(), m_Value(Scalar),
190  m_ConstantInt(InsIndexC))))
191  return nullptr;
192 
193  // The extract must be from the subset of vector elements that we inserted
194  // into. Example: if we inserted element 1 of a <2 x i64> and we are
195  // extracting an i16 (narrowing ratio = 4), then this extract must be from 1
196  // of elements 4-7 of the bitcasted vector.
197  unsigned NarrowingRatio = NumElts / NumSrcElts;
198  if (ExtIndexC / NarrowingRatio != InsIndexC)
199  return nullptr;
200 
201  // We are extracting part of the original scalar. How that scalar is
202  // inserted into the vector depends on the endian-ness. Example:
203  // Vector Byte Elt Index: 0 1 2 3 4 5 6 7
204  // +--+--+--+--+--+--+--+--+
205  // inselt <2 x i32> V, <i32> S, 1: |V0|V1|V2|V3|S0|S1|S2|S3|
206  // extelt <4 x i16> V', 3: | |S2|S3|
207  // +--+--+--+--+--+--+--+--+
208  // If this is little-endian, S2|S3 are the MSB of the 32-bit 'S' value.
209  // If this is big-endian, S2|S3 are the LSB of the 32-bit 'S' value.
210  // In this example, we must right-shift little-endian. Big-endian is just a
211  // truncate.
212  unsigned Chunk = ExtIndexC % NarrowingRatio;
213  if (IsBigEndian)
214  Chunk = NarrowingRatio - 1 - Chunk;
215 
216  // Bail out if this is an FP vector to FP vector sequence. That would take
217  // more instructions than we started with unless there is no shift, and it
218  // may not be handled as well in the backend.
219  bool NeedSrcBitcast = SrcTy->getScalarType()->isFloatingPointTy();
220  bool NeedDestBitcast = DestTy->isFloatingPointTy();
221  if (NeedSrcBitcast && NeedDestBitcast)
222  return nullptr;
223 
224  unsigned SrcWidth = SrcTy->getScalarSizeInBits();
225  unsigned DestWidth = DestTy->getPrimitiveSizeInBits();
226  unsigned ShAmt = Chunk * DestWidth;
227 
228  // TODO: This limitation is more strict than necessary. We could sum the
229  // number of new instructions and subtract the number eliminated to know if
230  // we can proceed.
231  if (!X->hasOneUse() || !Ext.getVectorOperand()->hasOneUse())
232  if (NeedSrcBitcast || NeedDestBitcast)
233  return nullptr;
234 
235  if (NeedSrcBitcast) {
236  Type *SrcIntTy = IntegerType::getIntNTy(Scalar->getContext(), SrcWidth);
237  Scalar = Builder.CreateBitCast(Scalar, SrcIntTy);
238  }
239 
240  if (ShAmt) {
241  // Bail out if we could end with more instructions than we started with.
242  if (!Ext.getVectorOperand()->hasOneUse())
243  return nullptr;
244  Scalar = Builder.CreateLShr(Scalar, ShAmt);
245  }
246 
247  if (NeedDestBitcast) {
248  Type *DestIntTy = IntegerType::getIntNTy(Scalar->getContext(), DestWidth);
249  return new BitCastInst(Builder.CreateTrunc(Scalar, DestIntTy), DestTy);
250  }
251  return new TruncInst(Scalar, DestTy);
252  }
253 
254  return nullptr;
255 }
256 
258  Value *SrcVec = EI.getVectorOperand();
259  Value *Index = EI.getIndexOperand();
260  if (Value *V = SimplifyExtractElementInst(SrcVec, Index,
261  SQ.getWithInstruction(&EI)))
262  return replaceInstUsesWith(EI, V);
263 
264  // If extracting a specified index from the vector, see if we can recursively
265  // find a previously computed scalar that was inserted into the vector.
266  auto *IndexC = dyn_cast<ConstantInt>(Index);
267  if (IndexC) {
268  unsigned NumElts = EI.getVectorOperandType()->getNumElements();
269 
270  // InstSimplify should handle cases where the index is invalid.
271  if (!IndexC->getValue().ule(NumElts))
272  return nullptr;
273 
274  // This instruction only demands the single element from the input vector.
275  // If the input vector has a single use, simplify it based on this use
276  // property.
277  if (SrcVec->hasOneUse() && NumElts != 1) {
278  APInt UndefElts(NumElts, 0);
279  APInt DemandedElts(NumElts, 0);
280  DemandedElts.setBit(IndexC->getZExtValue());
281  if (Value *V = SimplifyDemandedVectorElts(SrcVec, DemandedElts,
282  UndefElts)) {
283  EI.setOperand(0, V);
284  return &EI;
285  }
286  }
287 
288  if (Instruction *I = foldBitcastExtElt(EI, Builder, DL.isBigEndian()))
289  return I;
290 
291  // If there's a vector PHI feeding a scalar use through this extractelement
292  // instruction, try to scalarize the PHI.
293  if (auto *Phi = dyn_cast<PHINode>(SrcVec))
294  if (Instruction *ScalarPHI = scalarizePHI(EI, Phi))
295  return ScalarPHI;
296  }
297 
298  BinaryOperator *BO;
299  if (match(SrcVec, m_BinOp(BO)) && cheapToScalarize(SrcVec, IndexC)) {
300  // extelt (binop X, Y), Index --> binop (extelt X, Index), (extelt Y, Index)
301  Value *X = BO->getOperand(0), *Y = BO->getOperand(1);
302  Value *E0 = Builder.CreateExtractElement(X, Index);
303  Value *E1 = Builder.CreateExtractElement(Y, Index);
304  return BinaryOperator::CreateWithCopiedFlags(BO->getOpcode(), E0, E1, BO);
305  }
306 
307  Value *X, *Y;
308  CmpInst::Predicate Pred;
309  if (match(SrcVec, m_Cmp(Pred, m_Value(X), m_Value(Y))) &&
310  cheapToScalarize(SrcVec, IndexC)) {
311  // extelt (cmp X, Y), Index --> cmp (extelt X, Index), (extelt Y, Index)
312  Value *E0 = Builder.CreateExtractElement(X, Index);
313  Value *E1 = Builder.CreateExtractElement(Y, Index);
314  return CmpInst::Create(cast<CmpInst>(SrcVec)->getOpcode(), Pred, E0, E1);
315  }
316 
317  if (auto *I = dyn_cast<Instruction>(SrcVec)) {
318  if (auto *IE = dyn_cast<InsertElementInst>(I)) {
319  // Extracting the inserted element?
320  if (IE->getOperand(2) == Index)
321  return replaceInstUsesWith(EI, IE->getOperand(1));
322  // If the inserted and extracted elements are constants, they must not
323  // be the same value, extract from the pre-inserted value instead.
324  if (isa<Constant>(IE->getOperand(2)) && IndexC) {
325  Worklist.AddValue(SrcVec);
326  EI.setOperand(0, IE->getOperand(0));
327  return &EI;
328  }
329  } else if (auto *SVI = dyn_cast<ShuffleVectorInst>(I)) {
330  // If this is extracting an element from a shufflevector, figure out where
331  // it came from and extract from the appropriate input element instead.
332  if (auto *Elt = dyn_cast<ConstantInt>(Index)) {
333  int SrcIdx = SVI->getMaskValue(Elt->getZExtValue());
334  Value *Src;
335  unsigned LHSWidth =
336  SVI->getOperand(0)->getType()->getVectorNumElements();
337 
338  if (SrcIdx < 0)
339  return replaceInstUsesWith(EI, UndefValue::get(EI.getType()));
340  if (SrcIdx < (int)LHSWidth)
341  Src = SVI->getOperand(0);
342  else {
343  SrcIdx -= LHSWidth;
344  Src = SVI->getOperand(1);
345  }
347  return ExtractElementInst::Create(Src,
348  ConstantInt::get(Int32Ty,
349  SrcIdx, false));
350  }
351  } else if (auto *CI = dyn_cast<CastInst>(I)) {
352  // Canonicalize extractelement(cast) -> cast(extractelement).
353  // Bitcasts can change the number of vector elements, and they cost
354  // nothing.
355  if (CI->hasOneUse() && (CI->getOpcode() != Instruction::BitCast)) {
356  Value *EE = Builder.CreateExtractElement(CI->getOperand(0), Index);
357  Worklist.AddValue(EE);
358  return CastInst::Create(CI->getOpcode(), EE, EI.getType());
359  }
360  }
361  }
362  return nullptr;
363 }
364 
365 /// If V is a shuffle of values that ONLY returns elements from either LHS or
366 /// RHS, return the shuffle mask and true. Otherwise, return false.
367 static bool collectSingleShuffleElements(Value *V, Value *LHS, Value *RHS,
369  assert(LHS->getType() == RHS->getType() &&
370  "Invalid CollectSingleShuffleElements");
371  unsigned NumElts = V->getType()->getVectorNumElements();
372 
373  if (isa<UndefValue>(V)) {
374  Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
375  return true;
376  }
377 
378  if (V == LHS) {
379  for (unsigned i = 0; i != NumElts; ++i)
381  return true;
382  }
383 
384  if (V == RHS) {
385  for (unsigned i = 0; i != NumElts; ++i)
387  i+NumElts));
388  return true;
389  }
390 
391  if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
392  // If this is an insert of an extract from some other vector, include it.
393  Value *VecOp = IEI->getOperand(0);
394  Value *ScalarOp = IEI->getOperand(1);
395  Value *IdxOp = IEI->getOperand(2);
396 
397  if (!isa<ConstantInt>(IdxOp))
398  return false;
399  unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
400 
401  if (isa<UndefValue>(ScalarOp)) { // inserting undef into vector.
402  // We can handle this if the vector we are inserting into is
403  // transitively ok.
404  if (collectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
405  // If so, update the mask to reflect the inserted undef.
406  Mask[InsertedIdx] = UndefValue::get(Type::getInt32Ty(V->getContext()));
407  return true;
408  }
409  } else if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)){
410  if (isa<ConstantInt>(EI->getOperand(1))) {
411  unsigned ExtractedIdx =
412  cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
413  unsigned NumLHSElts = LHS->getType()->getVectorNumElements();
414 
415  // This must be extracting from either LHS or RHS.
416  if (EI->getOperand(0) == LHS || EI->getOperand(0) == RHS) {
417  // We can handle this if the vector we are inserting into is
418  // transitively ok.
419  if (collectSingleShuffleElements(VecOp, LHS, RHS, Mask)) {
420  // If so, update the mask to reflect the inserted value.
421  if (EI->getOperand(0) == LHS) {
422  Mask[InsertedIdx % NumElts] =
424  ExtractedIdx);
425  } else {
426  assert(EI->getOperand(0) == RHS);
427  Mask[InsertedIdx % NumElts] =
429  ExtractedIdx + NumLHSElts);
430  }
431  return true;
432  }
433  }
434  }
435  }
436  }
437 
438  return false;
439 }
440 
441 /// If we have insertion into a vector that is wider than the vector that we
442 /// are extracting from, try to widen the source vector to allow a single
443 /// shufflevector to replace one or more insert/extract pairs.
445  ExtractElementInst *ExtElt,
446  InstCombiner &IC) {
447  VectorType *InsVecType = InsElt->getType();
448  VectorType *ExtVecType = ExtElt->getVectorOperandType();
449  unsigned NumInsElts = InsVecType->getVectorNumElements();
450  unsigned NumExtElts = ExtVecType->getVectorNumElements();
451 
452  // The inserted-to vector must be wider than the extracted-from vector.
453  if (InsVecType->getElementType() != ExtVecType->getElementType() ||
454  NumExtElts >= NumInsElts)
455  return;
456 
457  // Create a shuffle mask to widen the extended-from vector using undefined
458  // values. The mask selects all of the values of the original vector followed
459  // by as many undefined values as needed to create a vector of the same length
460  // as the inserted-to vector.
461  SmallVector<Constant *, 16> ExtendMask;
462  IntegerType *IntType = Type::getInt32Ty(InsElt->getContext());
463  for (unsigned i = 0; i < NumExtElts; ++i)
464  ExtendMask.push_back(ConstantInt::get(IntType, i));
465  for (unsigned i = NumExtElts; i < NumInsElts; ++i)
466  ExtendMask.push_back(UndefValue::get(IntType));
467 
468  Value *ExtVecOp = ExtElt->getVectorOperand();
469  auto *ExtVecOpInst = dyn_cast<Instruction>(ExtVecOp);
470  BasicBlock *InsertionBlock = (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst))
471  ? ExtVecOpInst->getParent()
472  : ExtElt->getParent();
473 
474  // TODO: This restriction matches the basic block check below when creating
475  // new extractelement instructions. If that limitation is removed, this one
476  // could also be removed. But for now, we just bail out to ensure that we
477  // will replace the extractelement instruction that is feeding our
478  // insertelement instruction. This allows the insertelement to then be
479  // replaced by a shufflevector. If the insertelement is not replaced, we can
480  // induce infinite looping because there's an optimization for extractelement
481  // that will delete our widening shuffle. This would trigger another attempt
482  // here to create that shuffle, and we spin forever.
483  if (InsertionBlock != InsElt->getParent())
484  return;
485 
486  // TODO: This restriction matches the check in visitInsertElementInst() and
487  // prevents an infinite loop caused by not turning the extract/insert pair
488  // into a shuffle. We really should not need either check, but we're lacking
489  // folds for shufflevectors because we're afraid to generate shuffle masks
490  // that the backend can't handle.
491  if (InsElt->hasOneUse() && isa<InsertElementInst>(InsElt->user_back()))
492  return;
493 
494  auto *WideVec = new ShuffleVectorInst(ExtVecOp, UndefValue::get(ExtVecType),
495  ConstantVector::get(ExtendMask));
496 
497  // Insert the new shuffle after the vector operand of the extract is defined
498  // (as long as it's not a PHI) or at the start of the basic block of the
499  // extract, so any subsequent extracts in the same basic block can use it.
500  // TODO: Insert before the earliest ExtractElementInst that is replaced.
501  if (ExtVecOpInst && !isa<PHINode>(ExtVecOpInst))
502  WideVec->insertAfter(ExtVecOpInst);
503  else
504  IC.InsertNewInstWith(WideVec, *ExtElt->getParent()->getFirstInsertionPt());
505 
506  // Replace extracts from the original narrow vector with extracts from the new
507  // wide vector.
508  for (User *U : ExtVecOp->users()) {
510  if (!OldExt || OldExt->getParent() != WideVec->getParent())
511  continue;
512  auto *NewExt = ExtractElementInst::Create(WideVec, OldExt->getOperand(1));
513  NewExt->insertAfter(OldExt);
514  IC.replaceInstUsesWith(*OldExt, NewExt);
515  }
516 }
517 
518 /// We are building a shuffle to create V, which is a sequence of insertelement,
519 /// extractelement pairs. If PermittedRHS is set, then we must either use it or
520 /// not rely on the second vector source. Return a std::pair containing the
521 /// left and right vectors of the proposed shuffle (or 0), and set the Mask
522 /// parameter as required.
523 ///
524 /// Note: we intentionally don't try to fold earlier shuffles since they have
525 /// often been chosen carefully to be efficiently implementable on the target.
526 using ShuffleOps = std::pair<Value *, Value *>;
527 
530  Value *PermittedRHS,
531  InstCombiner &IC) {
532  assert(V->getType()->isVectorTy() && "Invalid shuffle!");
533  unsigned NumElts = V->getType()->getVectorNumElements();
534 
535  if (isa<UndefValue>(V)) {
536  Mask.assign(NumElts, UndefValue::get(Type::getInt32Ty(V->getContext())));
537  return std::make_pair(
538  PermittedRHS ? UndefValue::get(PermittedRHS->getType()) : V, nullptr);
539  }
540 
541  if (isa<ConstantAggregateZero>(V)) {
542  Mask.assign(NumElts, ConstantInt::get(Type::getInt32Ty(V->getContext()),0));
543  return std::make_pair(V, nullptr);
544  }
545 
546  if (InsertElementInst *IEI = dyn_cast<InsertElementInst>(V)) {
547  // If this is an insert of an extract from some other vector, include it.
548  Value *VecOp = IEI->getOperand(0);
549  Value *ScalarOp = IEI->getOperand(1);
550  Value *IdxOp = IEI->getOperand(2);
551 
552  if (ExtractElementInst *EI = dyn_cast<ExtractElementInst>(ScalarOp)) {
553  if (isa<ConstantInt>(EI->getOperand(1)) && isa<ConstantInt>(IdxOp)) {
554  unsigned ExtractedIdx =
555  cast<ConstantInt>(EI->getOperand(1))->getZExtValue();
556  unsigned InsertedIdx = cast<ConstantInt>(IdxOp)->getZExtValue();
557 
558  // Either the extracted from or inserted into vector must be RHSVec,
559  // otherwise we'd end up with a shuffle of three inputs.
560  if (EI->getOperand(0) == PermittedRHS || PermittedRHS == nullptr) {
561  Value *RHS = EI->getOperand(0);
562  ShuffleOps LR = collectShuffleElements(VecOp, Mask, RHS, IC);
563  assert(LR.second == nullptr || LR.second == RHS);
564 
565  if (LR.first->getType() != RHS->getType()) {
566  // Although we are giving up for now, see if we can create extracts
567  // that match the inserts for another round of combining.
568  replaceExtractElements(IEI, EI, IC);
569 
570  // We tried our best, but we can't find anything compatible with RHS
571  // further up the chain. Return a trivial shuffle.
572  for (unsigned i = 0; i < NumElts; ++i)
573  Mask[i] = ConstantInt::get(Type::getInt32Ty(V->getContext()), i);
574  return std::make_pair(V, nullptr);
575  }
576 
577  unsigned NumLHSElts = RHS->getType()->getVectorNumElements();
578  Mask[InsertedIdx % NumElts] =
580  NumLHSElts+ExtractedIdx);
581  return std::make_pair(LR.first, RHS);
582  }
583 
584  if (VecOp == PermittedRHS) {
585  // We've gone as far as we can: anything on the other side of the
586  // extractelement will already have been converted into a shuffle.
587  unsigned NumLHSElts =
589  for (unsigned i = 0; i != NumElts; ++i)
592  i == InsertedIdx ? ExtractedIdx : NumLHSElts + i));
593  return std::make_pair(EI->getOperand(0), PermittedRHS);
594  }
595 
596  // If this insertelement is a chain that comes from exactly these two
597  // vectors, return the vector and the effective shuffle.
598  if (EI->getOperand(0)->getType() == PermittedRHS->getType() &&
599  collectSingleShuffleElements(IEI, EI->getOperand(0), PermittedRHS,
600  Mask))
601  return std::make_pair(EI->getOperand(0), PermittedRHS);
602  }
603  }
604  }
605 
606  // Otherwise, we can't do anything fancy. Return an identity vector.
607  for (unsigned i = 0; i != NumElts; ++i)
609  return std::make_pair(V, nullptr);
610 }
611 
612 /// Try to find redundant insertvalue instructions, like the following ones:
613 /// %0 = insertvalue { i8, i32 } undef, i8 %x, 0
614 /// %1 = insertvalue { i8, i32 } %0, i8 %y, 0
615 /// Here the second instruction inserts values at the same indices, as the
616 /// first one, making the first one redundant.
617 /// It should be transformed to:
618 /// %0 = insertvalue { i8, i32 } undef, i8 %y, 0
620  bool IsRedundant = false;
621  ArrayRef<unsigned int> FirstIndices = I.getIndices();
622 
623  // If there is a chain of insertvalue instructions (each of them except the
624  // last one has only one use and it's another insertvalue insn from this
625  // chain), check if any of the 'children' uses the same indices as the first
626  // instruction. In this case, the first one is redundant.
627  Value *V = &I;
628  unsigned Depth = 0;
629  while (V->hasOneUse() && Depth < 10) {
630  User *U = V->user_back();
631  auto UserInsInst = dyn_cast<InsertValueInst>(U);
632  if (!UserInsInst || U->getOperand(0) != V)
633  break;
634  if (UserInsInst->getIndices() == FirstIndices) {
635  IsRedundant = true;
636  break;
637  }
638  V = UserInsInst;
639  Depth++;
640  }
641 
642  if (IsRedundant)
643  return replaceInstUsesWith(I, I.getOperand(0));
644  return nullptr;
645 }
646 
648  int MaskSize = Shuf.getMask()->getType()->getVectorNumElements();
649  int VecSize = Shuf.getOperand(0)->getType()->getVectorNumElements();
650 
651  // A vector select does not change the size of the operands.
652  if (MaskSize != VecSize)
653  return false;
654 
655  // Each mask element must be undefined or choose a vector element from one of
656  // the source operands without crossing vector lanes.
657  for (int i = 0; i != MaskSize; ++i) {
658  int Elt = Shuf.getMaskValue(i);
659  if (Elt != -1 && Elt != i && Elt != i + VecSize)
660  return false;
661  }
662 
663  return true;
664 }
665 
666 // Turn a chain of inserts that splats a value into a canonical insert + shuffle
667 // splat. That is:
668 // insertelt(insertelt(insertelt(insertelt X, %k, 0), %k, 1), %k, 2) ... ->
669 // shufflevector(insertelt(X, %k, 0), undef, zero)
671  // We are interested in the last insert in a chain. So, if this insert
672  // has a single user, and that user is an insert, bail.
673  if (InsElt.hasOneUse() && isa<InsertElementInst>(InsElt.user_back()))
674  return nullptr;
675 
676  VectorType *VT = cast<VectorType>(InsElt.getType());
677  int NumElements = VT->getNumElements();
678 
679  // Do not try to do this for a one-element vector, since that's a nop,
680  // and will cause an inf-loop.
681  if (NumElements == 1)
682  return nullptr;
683 
684  Value *SplatVal = InsElt.getOperand(1);
685  InsertElementInst *CurrIE = &InsElt;
686  SmallVector<bool, 16> ElementPresent(NumElements, false);
687  InsertElementInst *FirstIE = nullptr;
688 
689  // Walk the chain backwards, keeping track of which indices we inserted into,
690  // until we hit something that isn't an insert of the splatted value.
691  while (CurrIE) {
692  auto *Idx = dyn_cast<ConstantInt>(CurrIE->getOperand(2));
693  if (!Idx || CurrIE->getOperand(1) != SplatVal)
694  return nullptr;
695 
696  auto *NextIE = dyn_cast<InsertElementInst>(CurrIE->getOperand(0));
697  // Check none of the intermediate steps have any additional uses, except
698  // for the root insertelement instruction, which can be re-used, if it
699  // inserts at position 0.
700  if (CurrIE != &InsElt &&
701  (!CurrIE->hasOneUse() && (NextIE != nullptr || !Idx->isZero())))
702  return nullptr;
703 
704  ElementPresent[Idx->getZExtValue()] = true;
705  FirstIE = CurrIE;
706  CurrIE = NextIE;
707  }
708 
709  // Make sure we've seen an insert into every element.
710  if (llvm::any_of(ElementPresent, [](bool Present) { return !Present; }))
711  return nullptr;
712 
713  // All right, create the insert + shuffle.
714  Instruction *InsertFirst;
715  if (cast<ConstantInt>(FirstIE->getOperand(2))->isZero())
716  InsertFirst = FirstIE;
717  else
718  InsertFirst = InsertElementInst::Create(
719  UndefValue::get(VT), SplatVal,
721  "", &InsElt);
722 
724  VectorType::get(Type::getInt32Ty(InsElt.getContext()), NumElements));
725 
726  return new ShuffleVectorInst(InsertFirst, UndefValue::get(VT), ZeroMask);
727 }
728 
729 /// If we have an insertelement instruction feeding into another insertelement
730 /// and the 2nd is inserting a constant into the vector, canonicalize that
731 /// constant insertion before the insertion of a variable:
732 ///
733 /// insertelement (insertelement X, Y, IdxC1), ScalarC, IdxC2 -->
734 /// insertelement (insertelement X, ScalarC, IdxC2), Y, IdxC1
735 ///
736 /// This has the potential of eliminating the 2nd insertelement instruction
737 /// via constant folding of the scalar constant into a vector constant.
739  InstCombiner::BuilderTy &Builder) {
740  auto *InsElt1 = dyn_cast<InsertElementInst>(InsElt2.getOperand(0));
741  if (!InsElt1 || !InsElt1->hasOneUse())
742  return nullptr;
743 
744  Value *X, *Y;
745  Constant *ScalarC;
746  ConstantInt *IdxC1, *IdxC2;
747  if (match(InsElt1->getOperand(0), m_Value(X)) &&
748  match(InsElt1->getOperand(1), m_Value(Y)) && !isa<Constant>(Y) &&
749  match(InsElt1->getOperand(2), m_ConstantInt(IdxC1)) &&
750  match(InsElt2.getOperand(1), m_Constant(ScalarC)) &&
751  match(InsElt2.getOperand(2), m_ConstantInt(IdxC2)) && IdxC1 != IdxC2) {
752  Value *NewInsElt1 = Builder.CreateInsertElement(X, ScalarC, IdxC2);
753  return InsertElementInst::Create(NewInsElt1, Y, IdxC1);
754  }
755 
756  return nullptr;
757 }
758 
759 /// insertelt (shufflevector X, CVec, Mask|insertelt X, C1, CIndex1), C, CIndex
760 /// --> shufflevector X, CVec', Mask'
762  auto *Inst = dyn_cast<Instruction>(InsElt.getOperand(0));
763  // Bail out if the parent has more than one use. In that case, we'd be
764  // replacing the insertelt with a shuffle, and that's not a clear win.
765  if (!Inst || !Inst->hasOneUse())
766  return nullptr;
767  if (auto *Shuf = dyn_cast<ShuffleVectorInst>(InsElt.getOperand(0))) {
768  // The shuffle must have a constant vector operand. The insertelt must have
769  // a constant scalar being inserted at a constant position in the vector.
770  Constant *ShufConstVec, *InsEltScalar;
771  uint64_t InsEltIndex;
772  if (!match(Shuf->getOperand(1), m_Constant(ShufConstVec)) ||
773  !match(InsElt.getOperand(1), m_Constant(InsEltScalar)) ||
774  !match(InsElt.getOperand(2), m_ConstantInt(InsEltIndex)))
775  return nullptr;
776 
777  // Adding an element to an arbitrary shuffle could be expensive, but a
778  // shuffle that selects elements from vectors without crossing lanes is
779  // assumed cheap.
780  // If we're just adding a constant into that shuffle, it will still be
781  // cheap.
782  if (!isShuffleEquivalentToSelect(*Shuf))
783  return nullptr;
784 
785  // From the above 'select' check, we know that the mask has the same number
786  // of elements as the vector input operands. We also know that each constant
787  // input element is used in its lane and can not be used more than once by
788  // the shuffle. Therefore, replace the constant in the shuffle's constant
789  // vector with the insertelt constant. Replace the constant in the shuffle's
790  // mask vector with the insertelt index plus the length of the vector
791  // (because the constant vector operand of a shuffle is always the 2nd
792  // operand).
793  Constant *Mask = Shuf->getMask();
794  unsigned NumElts = Mask->getType()->getVectorNumElements();
795  SmallVector<Constant *, 16> NewShufElts(NumElts);
796  SmallVector<Constant *, 16> NewMaskElts(NumElts);
797  for (unsigned I = 0; I != NumElts; ++I) {
798  if (I == InsEltIndex) {
799  NewShufElts[I] = InsEltScalar;
800  Type *Int32Ty = Type::getInt32Ty(Shuf->getContext());
801  NewMaskElts[I] = ConstantInt::get(Int32Ty, InsEltIndex + NumElts);
802  } else {
803  // Copy over the existing values.
804  NewShufElts[I] = ShufConstVec->getAggregateElement(I);
805  NewMaskElts[I] = Mask->getAggregateElement(I);
806  }
807  }
808 
809  // Create new operands for a shuffle that includes the constant of the
810  // original insertelt. The old shuffle will be dead now.
811  return new ShuffleVectorInst(Shuf->getOperand(0),
812  ConstantVector::get(NewShufElts),
813  ConstantVector::get(NewMaskElts));
814  } else if (auto *IEI = dyn_cast<InsertElementInst>(Inst)) {
815  // Transform sequences of insertelements ops with constant data/indexes into
816  // a single shuffle op.
817  unsigned NumElts = InsElt.getType()->getNumElements();
818 
819  uint64_t InsertIdx[2];
820  Constant *Val[2];
821  if (!match(InsElt.getOperand(2), m_ConstantInt(InsertIdx[0])) ||
822  !match(InsElt.getOperand(1), m_Constant(Val[0])) ||
823  !match(IEI->getOperand(2), m_ConstantInt(InsertIdx[1])) ||
824  !match(IEI->getOperand(1), m_Constant(Val[1])))
825  return nullptr;
826  SmallVector<Constant *, 16> Values(NumElts);
828  auto ValI = std::begin(Val);
829  // Generate new constant vector and mask.
830  // We have 2 values/masks from the insertelements instructions. Insert them
831  // into new value/mask vectors.
832  for (uint64_t I : InsertIdx) {
833  if (!Values[I]) {
834  assert(!Mask[I]);
835  Values[I] = *ValI;
836  Mask[I] = ConstantInt::get(Type::getInt32Ty(InsElt.getContext()),
837  NumElts + I);
838  }
839  ++ValI;
840  }
841  // Remaining values are filled with 'undef' values.
842  for (unsigned I = 0; I < NumElts; ++I) {
843  if (!Values[I]) {
844  assert(!Mask[I]);
845  Values[I] = UndefValue::get(InsElt.getType()->getElementType());
846  Mask[I] = ConstantInt::get(Type::getInt32Ty(InsElt.getContext()), I);
847  }
848  }
849  // Create new operands for a shuffle that includes the constant of the
850  // original insertelt.
851  return new ShuffleVectorInst(IEI->getOperand(0),
852  ConstantVector::get(Values),
853  ConstantVector::get(Mask));
854  }
855  return nullptr;
856 }
857 
859  Value *VecOp = IE.getOperand(0);
860  Value *ScalarOp = IE.getOperand(1);
861  Value *IdxOp = IE.getOperand(2);
862 
863  if (auto *V = SimplifyInsertElementInst(
864  VecOp, ScalarOp, IdxOp, SQ.getWithInstruction(&IE)))
865  return replaceInstUsesWith(IE, V);
866 
867  // Inserting an undef or into an undefined place, remove this.
868  if (isa<UndefValue>(ScalarOp) || isa<UndefValue>(IdxOp))
869  replaceInstUsesWith(IE, VecOp);
870 
871  // If the inserted element was extracted from some other vector and both
872  // indexes are constant, try to turn this into a shuffle.
873  uint64_t InsertedIdx, ExtractedIdx;
874  Value *ExtVecOp;
875  if (match(IdxOp, m_ConstantInt(InsertedIdx)) &&
876  match(ScalarOp, m_ExtractElement(m_Value(ExtVecOp),
877  m_ConstantInt(ExtractedIdx)))) {
878  unsigned NumInsertVectorElts = IE.getType()->getNumElements();
879  unsigned NumExtractVectorElts = ExtVecOp->getType()->getVectorNumElements();
880  if (ExtractedIdx >= NumExtractVectorElts) // Out of range extract.
881  return replaceInstUsesWith(IE, VecOp);
882 
883  if (InsertedIdx >= NumInsertVectorElts) // Out of range insert.
884  return replaceInstUsesWith(IE, UndefValue::get(IE.getType()));
885 
886  // If we are extracting a value from a vector, then inserting it right
887  // back into the same place, just use the input vector.
888  if (ExtVecOp == VecOp && ExtractedIdx == InsertedIdx)
889  return replaceInstUsesWith(IE, VecOp);
890 
891  // TODO: Looking at the user(s) to determine if this insert is a
892  // fold-to-shuffle opportunity does not match the usual instcombine
893  // constraints. We should decide if the transform is worthy based only
894  // on this instruction and its operands, but that may not work currently.
895  //
896  // Here, we are trying to avoid creating shuffles before reaching
897  // the end of a chain of extract-insert pairs. This is complicated because
898  // we do not generally form arbitrary shuffle masks in instcombine
899  // (because those may codegen poorly), but collectShuffleElements() does
900  // exactly that.
901  //
902  // The rules for determining what is an acceptable target-independent
903  // shuffle mask are fuzzy because they evolve based on the backend's
904  // capabilities and real-world impact.
905  auto isShuffleRootCandidate = [](InsertElementInst &Insert) {
906  if (!Insert.hasOneUse())
907  return true;
908  auto *InsertUser = dyn_cast<InsertElementInst>(Insert.user_back());
909  if (!InsertUser)
910  return true;
911  return false;
912  };
913 
914  // Try to form a shuffle from a chain of extract-insert ops.
915  if (isShuffleRootCandidate(IE)) {
917  ShuffleOps LR = collectShuffleElements(&IE, Mask, nullptr, *this);
918 
919  // The proposed shuffle may be trivial, in which case we shouldn't
920  // perform the combine.
921  if (LR.first != &IE && LR.second != &IE) {
922  // We now have a shuffle of LHS, RHS, Mask.
923  if (LR.second == nullptr)
924  LR.second = UndefValue::get(LR.first->getType());
925  return new ShuffleVectorInst(LR.first, LR.second,
926  ConstantVector::get(Mask));
927  }
928  }
929  }
930 
931  unsigned VWidth = VecOp->getType()->getVectorNumElements();
932  APInt UndefElts(VWidth, 0);
933  APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
934  if (Value *V = SimplifyDemandedVectorElts(&IE, AllOnesEltMask, UndefElts)) {
935  if (V != &IE)
936  return replaceInstUsesWith(IE, V);
937  return &IE;
938  }
939 
941  return Shuf;
942 
943  if (Instruction *NewInsElt = hoistInsEltConst(IE, Builder))
944  return NewInsElt;
945 
946  // Turn a sequence of inserts that broadcasts a scalar into a single
947  // insert + shufflevector.
948  if (Instruction *Broadcast = foldInsSequenceIntoBroadcast(IE))
949  return Broadcast;
950 
951  return nullptr;
952 }
953 
954 /// Return true if we can evaluate the specified expression tree if the vector
955 /// elements were shuffled in a different order.
957  unsigned Depth = 5) {
958  // We can always reorder the elements of a constant.
959  if (isa<Constant>(V))
960  return true;
961 
962  // We won't reorder vector arguments. No IPO here.
964  if (!I) return false;
965 
966  // Two users may expect different orders of the elements. Don't try it.
967  if (!I->hasOneUse())
968  return false;
969 
970  if (Depth == 0) return false;
971 
972  switch (I->getOpcode()) {
973  case Instruction::Add:
974  case Instruction::FAdd:
975  case Instruction::Sub:
976  case Instruction::FSub:
977  case Instruction::Mul:
978  case Instruction::FMul:
979  case Instruction::UDiv:
980  case Instruction::SDiv:
981  case Instruction::FDiv:
982  case Instruction::URem:
983  case Instruction::SRem:
984  case Instruction::FRem:
985  case Instruction::Shl:
986  case Instruction::LShr:
987  case Instruction::AShr:
988  case Instruction::And:
989  case Instruction::Or:
990  case Instruction::Xor:
991  case Instruction::ICmp:
992  case Instruction::FCmp:
993  case Instruction::Trunc:
994  case Instruction::ZExt:
995  case Instruction::SExt:
996  case Instruction::FPToUI:
997  case Instruction::FPToSI:
998  case Instruction::UIToFP:
999  case Instruction::SIToFP:
1000  case Instruction::FPTrunc:
1001  case Instruction::FPExt:
1002  case Instruction::GetElementPtr: {
1003  // Bail out if we would create longer vector ops. We could allow creating
1004  // longer vector ops, but that may result in more expensive codegen. We
1005  // would also need to limit the transform to avoid undefined behavior for
1006  // integer div/rem.
1007  Type *ITy = I->getType();
1008  if (ITy->isVectorTy() && Mask.size() > ITy->getVectorNumElements())
1009  return false;
1010  for (Value *Operand : I->operands()) {
1011  if (!canEvaluateShuffled(Operand, Mask, Depth - 1))
1012  return false;
1013  }
1014  return true;
1015  }
1016  case Instruction::InsertElement: {
1018  if (!CI) return false;
1019  int ElementNumber = CI->getLimitedValue();
1020 
1021  // Verify that 'CI' does not occur twice in Mask. A single 'insertelement'
1022  // can't put an element into multiple indices.
1023  bool SeenOnce = false;
1024  for (int i = 0, e = Mask.size(); i != e; ++i) {
1025  if (Mask[i] == ElementNumber) {
1026  if (SeenOnce)
1027  return false;
1028  SeenOnce = true;
1029  }
1030  }
1031  return canEvaluateShuffled(I->getOperand(0), Mask, Depth - 1);
1032  }
1033  }
1034  return false;
1035 }
1036 
1037 /// Rebuild a new instruction just like 'I' but with the new operands given.
1038 /// In the event of type mismatch, the type of the operands is correct.
1040  // We don't want to use the IRBuilder here because we want the replacement
1041  // instructions to appear next to 'I', not the builder's insertion point.
1042  switch (I->getOpcode()) {
1043  case Instruction::Add:
1044  case Instruction::FAdd:
1045  case Instruction::Sub:
1046  case Instruction::FSub:
1047  case Instruction::Mul:
1048  case Instruction::FMul:
1049  case Instruction::UDiv:
1050  case Instruction::SDiv:
1051  case Instruction::FDiv:
1052  case Instruction::URem:
1053  case Instruction::SRem:
1054  case Instruction::FRem:
1055  case Instruction::Shl:
1056  case Instruction::LShr:
1057  case Instruction::AShr:
1058  case Instruction::And:
1059  case Instruction::Or:
1060  case Instruction::Xor: {
1061  BinaryOperator *BO = cast<BinaryOperator>(I);
1062  assert(NewOps.size() == 2 && "binary operator with #ops != 2");
1063  BinaryOperator *New =
1064  BinaryOperator::Create(cast<BinaryOperator>(I)->getOpcode(),
1065  NewOps[0], NewOps[1], "", BO);
1066  if (isa<OverflowingBinaryOperator>(BO)) {
1067  New->setHasNoUnsignedWrap(BO->hasNoUnsignedWrap());
1068  New->setHasNoSignedWrap(BO->hasNoSignedWrap());
1069  }
1070  if (isa<PossiblyExactOperator>(BO)) {
1071  New->setIsExact(BO->isExact());
1072  }
1073  if (isa<FPMathOperator>(BO))
1074  New->copyFastMathFlags(I);
1075  return New;
1076  }
1077  case Instruction::ICmp:
1078  assert(NewOps.size() == 2 && "icmp with #ops != 2");
1079  return new ICmpInst(I, cast<ICmpInst>(I)->getPredicate(),
1080  NewOps[0], NewOps[1]);
1081  case Instruction::FCmp:
1082  assert(NewOps.size() == 2 && "fcmp with #ops != 2");
1083  return new FCmpInst(I, cast<FCmpInst>(I)->getPredicate(),
1084  NewOps[0], NewOps[1]);
1085  case Instruction::Trunc:
1086  case Instruction::ZExt:
1087  case Instruction::SExt:
1088  case Instruction::FPToUI:
1089  case Instruction::FPToSI:
1090  case Instruction::UIToFP:
1091  case Instruction::SIToFP:
1092  case Instruction::FPTrunc:
1093  case Instruction::FPExt: {
1094  // It's possible that the mask has a different number of elements from
1095  // the original cast. We recompute the destination type to match the mask.
1096  Type *DestTy =
1098  NewOps[0]->getType()->getVectorNumElements());
1099  assert(NewOps.size() == 1 && "cast with #ops != 1");
1100  return CastInst::Create(cast<CastInst>(I)->getOpcode(), NewOps[0], DestTy,
1101  "", I);
1102  }
1103  case Instruction::GetElementPtr: {
1104  Value *Ptr = NewOps[0];
1105  ArrayRef<Value*> Idx = NewOps.slice(1);
1107  cast<GetElementPtrInst>(I)->getSourceElementType(), Ptr, Idx, "", I);
1108  GEP->setIsInBounds(cast<GetElementPtrInst>(I)->isInBounds());
1109  return GEP;
1110  }
1111  }
1112  llvm_unreachable("failed to rebuild vector instructions");
1113 }
1114 
1116  // Mask.size() does not need to be equal to the number of vector elements.
1117 
1118  assert(V->getType()->isVectorTy() && "can't reorder non-vector elements");
1119  Type *EltTy = V->getType()->getScalarType();
1120  Type *I32Ty = IntegerType::getInt32Ty(V->getContext());
1121  if (isa<UndefValue>(V))
1122  return UndefValue::get(VectorType::get(EltTy, Mask.size()));
1123 
1124  if (isa<ConstantAggregateZero>(V))
1125  return ConstantAggregateZero::get(VectorType::get(EltTy, Mask.size()));
1126 
1127  if (Constant *C = dyn_cast<Constant>(V)) {
1128  SmallVector<Constant *, 16> MaskValues;
1129  for (int i = 0, e = Mask.size(); i != e; ++i) {
1130  if (Mask[i] == -1)
1131  MaskValues.push_back(UndefValue::get(I32Ty));
1132  else
1133  MaskValues.push_back(ConstantInt::get(I32Ty, Mask[i]));
1134  }
1136  ConstantVector::get(MaskValues));
1137  }
1138 
1139  Instruction *I = cast<Instruction>(V);
1140  switch (I->getOpcode()) {
1141  case Instruction::Add:
1142  case Instruction::FAdd:
1143  case Instruction::Sub:
1144  case Instruction::FSub:
1145  case Instruction::Mul:
1146  case Instruction::FMul:
1147  case Instruction::UDiv:
1148  case Instruction::SDiv:
1149  case Instruction::FDiv:
1150  case Instruction::URem:
1151  case Instruction::SRem:
1152  case Instruction::FRem:
1153  case Instruction::Shl:
1154  case Instruction::LShr:
1155  case Instruction::AShr:
1156  case Instruction::And:
1157  case Instruction::Or:
1158  case Instruction::Xor:
1159  case Instruction::ICmp:
1160  case Instruction::FCmp:
1161  case Instruction::Trunc:
1162  case Instruction::ZExt:
1163  case Instruction::SExt:
1164  case Instruction::FPToUI:
1165  case Instruction::FPToSI:
1166  case Instruction::UIToFP:
1167  case Instruction::SIToFP:
1168  case Instruction::FPTrunc:
1169  case Instruction::FPExt:
1170  case Instruction::Select:
1171  case Instruction::GetElementPtr: {
1172  SmallVector<Value*, 8> NewOps;
1173  bool NeedsRebuild = (Mask.size() != I->getType()->getVectorNumElements());
1174  for (int i = 0, e = I->getNumOperands(); i != e; ++i) {
1175  Value *V = evaluateInDifferentElementOrder(I->getOperand(i), Mask);
1176  NewOps.push_back(V);
1177  NeedsRebuild |= (V != I->getOperand(i));
1178  }
1179  if (NeedsRebuild) {
1180  return buildNew(I, NewOps);
1181  }
1182  return I;
1183  }
1184  case Instruction::InsertElement: {
1185  int Element = cast<ConstantInt>(I->getOperand(2))->getLimitedValue();
1186 
1187  // The insertelement was inserting at Element. Figure out which element
1188  // that becomes after shuffling. The answer is guaranteed to be unique
1189  // by CanEvaluateShuffled.
1190  bool Found = false;
1191  int Index = 0;
1192  for (int e = Mask.size(); Index != e; ++Index) {
1193  if (Mask[Index] == Element) {
1194  Found = true;
1195  break;
1196  }
1197  }
1198 
1199  // If element is not in Mask, no need to handle the operand 1 (element to
1200  // be inserted). Just evaluate values in operand 0 according to Mask.
1201  if (!Found)
1202  return evaluateInDifferentElementOrder(I->getOperand(0), Mask);
1203 
1204  Value *V = evaluateInDifferentElementOrder(I->getOperand(0), Mask);
1205  return InsertElementInst::Create(V, I->getOperand(1),
1206  ConstantInt::get(I32Ty, Index), "", I);
1207  }
1208  }
1209  llvm_unreachable("failed to reorder elements of vector instruction!");
1210 }
1211 
1213  bool &isLHSID, bool &isRHSID) {
1214  isLHSID = isRHSID = true;
1215 
1216  for (unsigned i = 0, e = Mask.size(); i != e; ++i) {
1217  if (Mask[i] < 0) continue; // Ignore undef values.
1218  // Is this an identity shuffle of the LHS value?
1219  isLHSID &= (Mask[i] == (int)i);
1220 
1221  // Is this an identity shuffle of the RHS value?
1222  isRHSID &= (Mask[i]-e == i);
1223  }
1224 }
1225 
1226 // Returns true if the shuffle is extracting a contiguous range of values from
1227 // LHS, for example:
1228 // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1229 // Input: |AA|BB|CC|DD|EE|FF|GG|HH|II|JJ|KK|LL|MM|NN|OO|PP|
1230 // Shuffles to: |EE|FF|GG|HH|
1231 // +--+--+--+--+
1234  unsigned LHSElems = SVI.getOperand(0)->getType()->getVectorNumElements();
1235  unsigned MaskElems = Mask.size();
1236  unsigned BegIdx = Mask.front();
1237  unsigned EndIdx = Mask.back();
1238  if (BegIdx > EndIdx || EndIdx >= LHSElems || EndIdx - BegIdx != MaskElems - 1)
1239  return false;
1240  for (unsigned I = 0; I != MaskElems; ++I)
1241  if (static_cast<unsigned>(Mask[I]) != BegIdx + I)
1242  return false;
1243  return true;
1244 }
1245 
1246 /// These are the ingredients in an alternate form binary operator as described
1247 /// below.
1248 struct BinopElts {
1253  Value *V0 = nullptr, Value *V1 = nullptr) :
1254  Opcode(Opc), Op0(V0), Op1(V1) {}
1255  operator bool() const { return Opcode != 0; }
1256 };
1257 
1258 /// Binops may be transformed into binops with different opcodes and operands.
1259 /// Reverse the usual canonicalization to enable folds with the non-canonical
1260 /// form of the binop. If a transform is possible, return the elements of the
1261 /// new binop. If not, return invalid elements.
1263  Value *BO0 = BO->getOperand(0), *BO1 = BO->getOperand(1);
1264  Type *Ty = BO->getType();
1265  switch (BO->getOpcode()) {
1266  case Instruction::Shl: {
1267  // shl X, C --> mul X, (1 << C)
1268  Constant *C;
1269  if (match(BO1, m_Constant(C))) {
1270  Constant *ShlOne = ConstantExpr::getShl(ConstantInt::get(Ty, 1), C);
1271  return { Instruction::Mul, BO0, ShlOne };
1272  }
1273  break;
1274  }
1275  case Instruction::Or: {
1276  // or X, C --> add X, C (when X and C have no common bits set)
1277  const APInt *C;
1278  if (match(BO1, m_APInt(C)) && MaskedValueIsZero(BO0, *C, DL))
1279  return { Instruction::Add, BO0, BO1 };
1280  break;
1281  }
1282  default:
1283  break;
1284  }
1285  return {};
1286 }
1287 
1289  assert(Shuf.isSelect() && "Must have select-equivalent shuffle");
1290 
1291  // Are we shuffling together some value and that same value after it has been
1292  // modified by a binop with a constant?
1293  Value *Op0 = Shuf.getOperand(0), *Op1 = Shuf.getOperand(1);
1294  Constant *C;
1295  bool Op0IsBinop;
1296  if (match(Op0, m_BinOp(m_Specific(Op1), m_Constant(C))))
1297  Op0IsBinop = true;
1298  else if (match(Op1, m_BinOp(m_Specific(Op0), m_Constant(C))))
1299  Op0IsBinop = false;
1300  else
1301  return nullptr;
1302 
1303  // The identity constant for a binop leaves a variable operand unchanged. For
1304  // a vector, this is a splat of something like 0, -1, or 1.
1305  // If there's no identity constant for this binop, we're done.
1306  auto *BO = cast<BinaryOperator>(Op0IsBinop ? Op0 : Op1);
1307  BinaryOperator::BinaryOps BOpcode = BO->getOpcode();
1308  Constant *IdC = ConstantExpr::getBinOpIdentity(BOpcode, Shuf.getType(), true);
1309  if (!IdC)
1310  return nullptr;
1311 
1312  // Shuffle identity constants into the lanes that return the original value.
1313  // Example: shuf (mul X, {-1,-2,-3,-4}), X, {0,5,6,3} --> mul X, {-1,1,1,-4}
1314  // Example: shuf X, (add X, {-1,-2,-3,-4}), {0,1,6,7} --> add X, {0,0,-3,-4}
1315  // The existing binop constant vector remains in the same operand position.
1316  Constant *Mask = Shuf.getMask();
1317  Constant *NewC = Op0IsBinop ? ConstantExpr::getShuffleVector(C, IdC, Mask) :
1318  ConstantExpr::getShuffleVector(IdC, C, Mask);
1319 
1320  bool MightCreatePoisonOrUB =
1321  Mask->containsUndefElement() &&
1322  (Instruction::isIntDivRem(BOpcode) || Instruction::isShift(BOpcode));
1323  if (MightCreatePoisonOrUB)
1324  NewC = getSafeVectorConstantForBinop(BOpcode, NewC, true);
1325 
1326  // shuf (bop X, C), X, M --> bop X, C'
1327  // shuf X, (bop X, C), M --> bop X, C'
1328  Value *X = Op0IsBinop ? Op1 : Op0;
1329  Instruction *NewBO = BinaryOperator::Create(BOpcode, X, NewC);
1330  NewBO->copyIRFlags(BO);
1331 
1332  // An undef shuffle mask element may propagate as an undef constant element in
1333  // the new binop. That would produce poison where the original code might not.
1334  // If we already made a safe constant, then there's no danger.
1335  if (Mask->containsUndefElement() && !MightCreatePoisonOrUB)
1336  NewBO->dropPoisonGeneratingFlags();
1337  return NewBO;
1338 }
1339 
1340 /// Try to fold shuffles that are the equivalent of a vector select.
1342  InstCombiner::BuilderTy &Builder,
1343  const DataLayout &DL) {
1344  if (!Shuf.isSelect())
1345  return nullptr;
1346 
1348  return I;
1349 
1350  BinaryOperator *B0, *B1;
1351  if (!match(Shuf.getOperand(0), m_BinOp(B0)) ||
1352  !match(Shuf.getOperand(1), m_BinOp(B1)))
1353  return nullptr;
1354 
1355  Value *X, *Y;
1356  Constant *C0, *C1;
1357  bool ConstantsAreOp1;
1358  if (match(B0, m_BinOp(m_Value(X), m_Constant(C0))) &&
1359  match(B1, m_BinOp(m_Value(Y), m_Constant(C1))))
1360  ConstantsAreOp1 = true;
1361  else if (match(B0, m_BinOp(m_Constant(C0), m_Value(X))) &&
1362  match(B1, m_BinOp(m_Constant(C1), m_Value(Y))))
1363  ConstantsAreOp1 = false;
1364  else
1365  return nullptr;
1366 
1367  // We need matching binops to fold the lanes together.
1368  BinaryOperator::BinaryOps Opc0 = B0->getOpcode();
1369  BinaryOperator::BinaryOps Opc1 = B1->getOpcode();
1370  bool DropNSW = false;
1371  if (ConstantsAreOp1 && Opc0 != Opc1) {
1372  // TODO: We drop "nsw" if shift is converted into multiply because it may
1373  // not be correct when the shift amount is BitWidth - 1. We could examine
1374  // each vector element to determine if it is safe to keep that flag.
1375  if (Opc0 == Instruction::Shl || Opc1 == Instruction::Shl)
1376  DropNSW = true;
1377  if (BinopElts AltB0 = getAlternateBinop(B0, DL)) {
1378  assert(isa<Constant>(AltB0.Op1) && "Expecting constant with alt binop");
1379  Opc0 = AltB0.Opcode;
1380  C0 = cast<Constant>(AltB0.Op1);
1381  } else if (BinopElts AltB1 = getAlternateBinop(B1, DL)) {
1382  assert(isa<Constant>(AltB1.Op1) && "Expecting constant with alt binop");
1383  Opc1 = AltB1.Opcode;
1384  C1 = cast<Constant>(AltB1.Op1);
1385  }
1386  }
1387 
1388  if (Opc0 != Opc1)
1389  return nullptr;
1390 
1391  // The opcodes must be the same. Use a new name to make that clear.
1392  BinaryOperator::BinaryOps BOpc = Opc0;
1393 
1394  // Select the constant elements needed for the single binop.
1395  Constant *Mask = Shuf.getMask();
1396  Constant *NewC = ConstantExpr::getShuffleVector(C0, C1, Mask);
1397 
1398  // We are moving a binop after a shuffle. When a shuffle has an undefined
1399  // mask element, the result is undefined, but it is not poison or undefined
1400  // behavior. That is not necessarily true for div/rem/shift.
1401  bool MightCreatePoisonOrUB =
1402  Mask->containsUndefElement() &&
1404  if (MightCreatePoisonOrUB)
1405  NewC = getSafeVectorConstantForBinop(BOpc, NewC, ConstantsAreOp1);
1406 
1407  Value *V;
1408  if (X == Y) {
1409  // Remove a binop and the shuffle by rearranging the constant:
1410  // shuffle (op V, C0), (op V, C1), M --> op V, C'
1411  // shuffle (op C0, V), (op C1, V), M --> op C', V
1412  V = X;
1413  } else {
1414  // If there are 2 different variable operands, we must create a new shuffle
1415  // (select) first, so check uses to ensure that we don't end up with more
1416  // instructions than we started with.
1417  if (!B0->hasOneUse() && !B1->hasOneUse())
1418  return nullptr;
1419 
1420  // If we use the original shuffle mask and op1 is *variable*, we would be
1421  // putting an undef into operand 1 of div/rem/shift. This is either UB or
1422  // poison. We do not have to guard against UB when *constants* are op1
1423  // because safe constants guarantee that we do not overflow sdiv/srem (and
1424  // there's no danger for other opcodes).
1425  // TODO: To allow this case, create a new shuffle mask with no undefs.
1426  if (MightCreatePoisonOrUB && !ConstantsAreOp1)
1427  return nullptr;
1428 
1429  // Note: In general, we do not create new shuffles in InstCombine because we
1430  // do not know if a target can lower an arbitrary shuffle optimally. In this
1431  // case, the shuffle uses the existing mask, so there is no additional risk.
1432 
1433  // Select the variable vectors first, then perform the binop:
1434  // shuffle (op X, C0), (op Y, C1), M --> op (shuffle X, Y, M), C'
1435  // shuffle (op C0, X), (op C1, Y), M --> op C', (shuffle X, Y, M)
1436  V = Builder.CreateShuffleVector(X, Y, Mask);
1437  }
1438 
1439  Instruction *NewBO = ConstantsAreOp1 ? BinaryOperator::Create(BOpc, V, NewC) :
1440  BinaryOperator::Create(BOpc, NewC, V);
1441 
1442  // Flags are intersected from the 2 source binops. But there are 2 exceptions:
1443  // 1. If we changed an opcode, poison conditions might have changed.
1444  // 2. If the shuffle had undef mask elements, the new binop might have undefs
1445  // where the original code did not. But if we already made a safe constant,
1446  // then there's no danger.
1447  NewBO->copyIRFlags(B0);
1448  NewBO->andIRFlags(B1);
1449  if (DropNSW)
1450  NewBO->setHasNoSignedWrap(false);
1451  if (Mask->containsUndefElement() && !MightCreatePoisonOrUB)
1452  NewBO->dropPoisonGeneratingFlags();
1453  return NewBO;
1454 }
1455 
1456 /// Match a shuffle-select-shuffle pattern where the shuffles are widening and
1457 /// narrowing (concatenating with undef and extracting back to the original
1458 /// length). This allows replacing the wide select with a narrow select.
1460  InstCombiner::BuilderTy &Builder) {
1461  // This must be a narrowing identity shuffle. It extracts the 1st N elements
1462  // of the 1st vector operand of a shuffle.
1463  if (!match(Shuf.getOperand(1), m_Undef()) || !Shuf.isIdentityWithExtract())
1464  return nullptr;
1465 
1466  // The vector being shuffled must be a vector select that we can eliminate.
1467  // TODO: The one-use requirement could be eased if X and/or Y are constants.
1468  Value *Cond, *X, *Y;
1469  if (!match(Shuf.getOperand(0),
1470  m_OneUse(m_Select(m_Value(Cond), m_Value(X), m_Value(Y)))))
1471  return nullptr;
1472 
1473  // We need a narrow condition value. It must be extended with undef elements
1474  // and have the same number of elements as this shuffle.
1475  unsigned NarrowNumElts = Shuf.getType()->getVectorNumElements();
1476  Value *NarrowCond;
1477  if (!match(Cond, m_OneUse(m_ShuffleVector(m_Value(NarrowCond), m_Undef(),
1478  m_Constant()))) ||
1479  NarrowCond->getType()->getVectorNumElements() != NarrowNumElts ||
1480  !cast<ShuffleVectorInst>(Cond)->isIdentityWithPadding())
1481  return nullptr;
1482 
1483  // shuf (sel (shuf NarrowCond, undef, WideMask), X, Y), undef, NarrowMask) -->
1484  // sel NarrowCond, (shuf X, undef, NarrowMask), (shuf Y, undef, NarrowMask)
1486  Value *NarrowX = Builder.CreateShuffleVector(X, Undef, Shuf.getMask());
1487  Value *NarrowY = Builder.CreateShuffleVector(Y, Undef, Shuf.getMask());
1488  return SelectInst::Create(NarrowCond, NarrowX, NarrowY);
1489 }
1490 
1491 /// Try to combine 2 shuffles into 1 shuffle by concatenating a shuffle mask.
1493  Value *Op0 = Shuf.getOperand(0), *Op1 = Shuf.getOperand(1);
1494  if (!Shuf.isIdentityWithExtract() || !isa<UndefValue>(Op1))
1495  return nullptr;
1496 
1497  Value *X, *Y;
1498  Constant *Mask;
1499  if (!match(Op0, m_ShuffleVector(m_Value(X), m_Value(Y), m_Constant(Mask))))
1500  return nullptr;
1501 
1502  // We are extracting a subvector from a shuffle. Remove excess elements from
1503  // the 1st shuffle mask to eliminate the extract.
1504  //
1505  // This transform is conservatively limited to identity extracts because we do
1506  // not allow arbitrary shuffle mask creation as a target-independent transform
1507  // (because we can't guarantee that will lower efficiently).
1508  //
1509  // If the extracting shuffle has an undef mask element, it transfers to the
1510  // new shuffle mask. Otherwise, copy the original mask element. Example:
1511  // shuf (shuf X, Y, <C0, C1, C2, undef, C4>), undef, <0, undef, 2, 3> -->
1512  // shuf X, Y, <C0, undef, C2, undef>
1513  unsigned NumElts = Shuf.getType()->getVectorNumElements();
1514  SmallVector<Constant *, 16> NewMask(NumElts);
1515  assert(NumElts < Mask->getType()->getVectorNumElements() &&
1516  "Identity with extract must have less elements than its inputs");
1517 
1518  for (unsigned i = 0; i != NumElts; ++i) {
1519  Constant *ExtractMaskElt = Shuf.getMask()->getAggregateElement(i);
1520  Constant *MaskElt = Mask->getAggregateElement(i);
1521  NewMask[i] = isa<UndefValue>(ExtractMaskElt) ? ExtractMaskElt : MaskElt;
1522  }
1523  return new ShuffleVectorInst(X, Y, ConstantVector::get(NewMask));
1524 }
1525 
1526 /// Try to replace a shuffle with an insertelement.
1528  Value *V0 = Shuf.getOperand(0), *V1 = Shuf.getOperand(1);
1530 
1531  // The shuffle must not change vector sizes.
1532  // TODO: This restriction could be removed if the insert has only one use
1533  // (because the transform would require a new length-changing shuffle).
1534  int NumElts = Mask.size();
1535  if (NumElts != (int)(V0->getType()->getVectorNumElements()))
1536  return nullptr;
1537 
1538  // shuffle (insert ?, Scalar, IndexC), V1, Mask --> insert V1, Scalar, IndexC'
1539  auto isShufflingScalarIntoOp1 = [&](Value *&Scalar, ConstantInt *&IndexC) {
1540  // We need an insertelement with a constant index.
1541  if (!match(V0, m_InsertElement(m_Value(), m_Value(Scalar),
1542  m_ConstantInt(IndexC))))
1543  return false;
1544 
1545  // Test the shuffle mask to see if it splices the inserted scalar into the
1546  // operand 1 vector of the shuffle.
1547  int NewInsIndex = -1;
1548  for (int i = 0; i != NumElts; ++i) {
1549  // Ignore undef mask elements.
1550  if (Mask[i] == -1)
1551  continue;
1552 
1553  // The shuffle takes elements of operand 1 without lane changes.
1554  if (Mask[i] == NumElts + i)
1555  continue;
1556 
1557  // The shuffle must choose the inserted scalar exactly once.
1558  if (NewInsIndex != -1 || Mask[i] != IndexC->getSExtValue())
1559  return false;
1560 
1561  // The shuffle is placing the inserted scalar into element i.
1562  NewInsIndex = i;
1563  }
1564 
1565  assert(NewInsIndex != -1 && "Did not fold shuffle with unused operand?");
1566 
1567  // Index is updated to the potentially translated insertion lane.
1568  IndexC = ConstantInt::get(IndexC->getType(), NewInsIndex);
1569  return true;
1570  };
1571 
1572  // If the shuffle is unnecessary, insert the scalar operand directly into
1573  // operand 1 of the shuffle. Example:
1574  // shuffle (insert ?, S, 1), V1, <1, 5, 6, 7> --> insert V1, S, 0
1575  Value *Scalar;
1576  ConstantInt *IndexC;
1577  if (isShufflingScalarIntoOp1(Scalar, IndexC))
1578  return InsertElementInst::Create(V1, Scalar, IndexC);
1579 
1580  // Try again after commuting shuffle. Example:
1581  // shuffle V0, (insert ?, S, 0), <0, 1, 2, 4> -->
1582  // shuffle (insert ?, S, 0), V0, <4, 5, 6, 0> --> insert V0, S, 3
1583  std::swap(V0, V1);
1585  if (isShufflingScalarIntoOp1(Scalar, IndexC))
1586  return InsertElementInst::Create(V1, Scalar, IndexC);
1587 
1588  return nullptr;
1589 }
1590 
1592  Value *LHS = SVI.getOperand(0);
1593  Value *RHS = SVI.getOperand(1);
1594  if (auto *V = SimplifyShuffleVectorInst(
1595  LHS, RHS, SVI.getMask(), SVI.getType(), SQ.getWithInstruction(&SVI)))
1596  return replaceInstUsesWith(SVI, V);
1597 
1598  if (Instruction *I = foldSelectShuffle(SVI, Builder, DL))
1599  return I;
1600 
1601  if (Instruction *I = narrowVectorSelect(SVI, Builder))
1602  return I;
1603 
1604  unsigned VWidth = SVI.getType()->getVectorNumElements();
1605  APInt UndefElts(VWidth, 0);
1606  APInt AllOnesEltMask(APInt::getAllOnesValue(VWidth));
1607  if (Value *V = SimplifyDemandedVectorElts(&SVI, AllOnesEltMask, UndefElts)) {
1608  if (V != &SVI)
1609  return replaceInstUsesWith(SVI, V);
1610  return &SVI;
1611  }
1612 
1614  return I;
1615 
1616  // This transform has the potential to lose undef knowledge, so it is
1617  // intentionally placed after SimplifyDemandedVectorElts().
1618  if (Instruction *I = foldShuffleWithInsert(SVI))
1619  return I;
1620 
1623  unsigned LHSWidth = LHS->getType()->getVectorNumElements();
1624  bool MadeChange = false;
1625 
1626  // Canonicalize shuffle(x ,x,mask) -> shuffle(x, undef,mask')
1627  // Canonicalize shuffle(undef,x,mask) -> shuffle(x, undef,mask').
1628  if (LHS == RHS || isa<UndefValue>(LHS)) {
1629  // Remap any references to RHS to use LHS.
1631  for (unsigned i = 0, e = LHSWidth; i != VWidth; ++i) {
1632  if (Mask[i] < 0) {
1633  Elts.push_back(UndefValue::get(Int32Ty));
1634  continue;
1635  }
1636 
1637  if ((Mask[i] >= (int)e && isa<UndefValue>(RHS)) ||
1638  (Mask[i] < (int)e && isa<UndefValue>(LHS))) {
1639  Mask[i] = -1; // Turn into undef.
1640  Elts.push_back(UndefValue::get(Int32Ty));
1641  } else {
1642  Mask[i] = Mask[i] % e; // Force to LHS.
1643  Elts.push_back(ConstantInt::get(Int32Ty, Mask[i]));
1644  }
1645  }
1646  SVI.setOperand(0, SVI.getOperand(1));
1647  SVI.setOperand(1, UndefValue::get(RHS->getType()));
1648  SVI.setOperand(2, ConstantVector::get(Elts));
1649  LHS = SVI.getOperand(0);
1650  RHS = SVI.getOperand(1);
1651  MadeChange = true;
1652  }
1653 
1654  if (VWidth == LHSWidth) {
1655  // Analyze the shuffle, are the LHS or RHS and identity shuffles?
1656  bool isLHSID, isRHSID;
1657  recognizeIdentityMask(Mask, isLHSID, isRHSID);
1658 
1659  // Eliminate identity shuffles.
1660  if (isLHSID) return replaceInstUsesWith(SVI, LHS);
1661  if (isRHSID) return replaceInstUsesWith(SVI, RHS);
1662  }
1663 
1664  if (isa<UndefValue>(RHS) && canEvaluateShuffled(LHS, Mask)) {
1665  Value *V = evaluateInDifferentElementOrder(LHS, Mask);
1666  return replaceInstUsesWith(SVI, V);
1667  }
1668 
1669  // SROA generates shuffle+bitcast when the extracted sub-vector is bitcast to
1670  // a non-vector type. We can instead bitcast the original vector followed by
1671  // an extract of the desired element:
1672  //
1673  // %sroa = shufflevector <16 x i8> %in, <16 x i8> undef,
1674  // <4 x i32> <i32 0, i32 1, i32 2, i32 3>
1675  // %1 = bitcast <4 x i8> %sroa to i32
1676  // Becomes:
1677  // %bc = bitcast <16 x i8> %in to <4 x i32>
1678  // %ext = extractelement <4 x i32> %bc, i32 0
1679  //
1680  // If the shuffle is extracting a contiguous range of values from the input
1681  // vector then each use which is a bitcast of the extracted size can be
1682  // replaced. This will work if the vector types are compatible, and the begin
1683  // index is aligned to a value in the casted vector type. If the begin index
1684  // isn't aligned then we can shuffle the original vector (keeping the same
1685  // vector type) before extracting.
1686  //
1687  // This code will bail out if the target type is fundamentally incompatible
1688  // with vectors of the source type.
1689  //
1690  // Example of <16 x i8>, target type i32:
1691  // Index range [4,8): v-----------v Will work.
1692  // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
1693  // <16 x i8>: | | | | | | | | | | | | | | | | |
1694  // <4 x i32>: | | | | |
1695  // +-----------+-----------+-----------+-----------+
1696  // Index range [6,10): ^-----------^ Needs an extra shuffle.
1697  // Target type i40: ^--------------^ Won't work, bail.
1698  if (isShuffleExtractingFromLHS(SVI, Mask)) {
1699  Value *V = LHS;
1700  unsigned MaskElems = Mask.size();
1701  VectorType *SrcTy = cast<VectorType>(V->getType());
1702  unsigned VecBitWidth = SrcTy->getBitWidth();
1703  unsigned SrcElemBitWidth = DL.getTypeSizeInBits(SrcTy->getElementType());
1704  assert(SrcElemBitWidth && "vector elements must have a bitwidth");
1705  unsigned SrcNumElems = SrcTy->getNumElements();
1708  for (User *U : SVI.users())
1709  if (BitCastInst *BC = dyn_cast<BitCastInst>(U))
1710  if (!BC->use_empty())
1711  // Only visit bitcasts that weren't previously handled.
1712  BCs.push_back(BC);
1713  for (BitCastInst *BC : BCs) {
1714  unsigned BegIdx = Mask.front();
1715  Type *TgtTy = BC->getDestTy();
1716  unsigned TgtElemBitWidth = DL.getTypeSizeInBits(TgtTy);
1717  if (!TgtElemBitWidth)
1718  continue;
1719  unsigned TgtNumElems = VecBitWidth / TgtElemBitWidth;
1720  bool VecBitWidthsEqual = VecBitWidth == TgtNumElems * TgtElemBitWidth;
1721  bool BegIsAligned = 0 == ((SrcElemBitWidth * BegIdx) % TgtElemBitWidth);
1722  if (!VecBitWidthsEqual)
1723  continue;
1724  if (!VectorType::isValidElementType(TgtTy))
1725  continue;
1726  VectorType *CastSrcTy = VectorType::get(TgtTy, TgtNumElems);
1727  if (!BegIsAligned) {
1728  // Shuffle the input so [0,NumElements) contains the output, and
1729  // [NumElems,SrcNumElems) is undef.
1730  SmallVector<Constant *, 16> ShuffleMask(SrcNumElems,
1731  UndefValue::get(Int32Ty));
1732  for (unsigned I = 0, E = MaskElems, Idx = BegIdx; I != E; ++Idx, ++I)
1733  ShuffleMask[I] = ConstantInt::get(Int32Ty, Idx);
1734  V = Builder.CreateShuffleVector(V, UndefValue::get(V->getType()),
1735  ConstantVector::get(ShuffleMask),
1736  SVI.getName() + ".extract");
1737  BegIdx = 0;
1738  }
1739  unsigned SrcElemsPerTgtElem = TgtElemBitWidth / SrcElemBitWidth;
1740  assert(SrcElemsPerTgtElem);
1741  BegIdx /= SrcElemsPerTgtElem;
1742  bool BCAlreadyExists = NewBCs.find(CastSrcTy) != NewBCs.end();
1743  auto *NewBC =
1744  BCAlreadyExists
1745  ? NewBCs[CastSrcTy]
1746  : Builder.CreateBitCast(V, CastSrcTy, SVI.getName() + ".bc");
1747  if (!BCAlreadyExists)
1748  NewBCs[CastSrcTy] = NewBC;
1749  auto *Ext = Builder.CreateExtractElement(
1750  NewBC, ConstantInt::get(Int32Ty, BegIdx), SVI.getName() + ".extract");
1751  // The shufflevector isn't being replaced: the bitcast that used it
1752  // is. InstCombine will visit the newly-created instructions.
1753  replaceInstUsesWith(*BC, Ext);
1754  MadeChange = true;
1755  }
1756  }
1757 
1758  // If the LHS is a shufflevector itself, see if we can combine it with this
1759  // one without producing an unusual shuffle.
1760  // Cases that might be simplified:
1761  // 1.
1762  // x1=shuffle(v1,v2,mask1)
1763  // x=shuffle(x1,undef,mask)
1764  // ==>
1765  // x=shuffle(v1,undef,newMask)
1766  // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : -1
1767  // 2.
1768  // x1=shuffle(v1,undef,mask1)
1769  // x=shuffle(x1,x2,mask)
1770  // where v1.size() == mask1.size()
1771  // ==>
1772  // x=shuffle(v1,x2,newMask)
1773  // newMask[i] = (mask[i] < x1.size()) ? mask1[mask[i]] : mask[i]
1774  // 3.
1775  // x2=shuffle(v2,undef,mask2)
1776  // x=shuffle(x1,x2,mask)
1777  // where v2.size() == mask2.size()
1778  // ==>
1779  // x=shuffle(x1,v2,newMask)
1780  // newMask[i] = (mask[i] < x1.size())
1781  // ? mask[i] : mask2[mask[i]-x1.size()]+x1.size()
1782  // 4.
1783  // x1=shuffle(v1,undef,mask1)
1784  // x2=shuffle(v2,undef,mask2)
1785  // x=shuffle(x1,x2,mask)
1786  // where v1.size() == v2.size()
1787  // ==>
1788  // x=shuffle(v1,v2,newMask)
1789  // newMask[i] = (mask[i] < x1.size())
1790  // ? mask1[mask[i]] : mask2[mask[i]-x1.size()]+v1.size()
1791  //
1792  // Here we are really conservative:
1793  // we are absolutely afraid of producing a shuffle mask not in the input
1794  // program, because the code gen may not be smart enough to turn a merged
1795  // shuffle into two specific shuffles: it may produce worse code. As such,
1796  // we only merge two shuffles if the result is either a splat or one of the
1797  // input shuffle masks. In this case, merging the shuffles just removes
1798  // one instruction, which we know is safe. This is good for things like
1799  // turning: (splat(splat)) -> splat, or
1800  // merge(V[0..n], V[n+1..2n]) -> V[0..2n]
1801  ShuffleVectorInst* LHSShuffle = dyn_cast<ShuffleVectorInst>(LHS);
1802  ShuffleVectorInst* RHSShuffle = dyn_cast<ShuffleVectorInst>(RHS);
1803  if (LHSShuffle)
1804  if (!isa<UndefValue>(LHSShuffle->getOperand(1)) && !isa<UndefValue>(RHS))
1805  LHSShuffle = nullptr;
1806  if (RHSShuffle)
1807  if (!isa<UndefValue>(RHSShuffle->getOperand(1)))
1808  RHSShuffle = nullptr;
1809  if (!LHSShuffle && !RHSShuffle)
1810  return MadeChange ? &SVI : nullptr;
1811 
1812  Value* LHSOp0 = nullptr;
1813  Value* LHSOp1 = nullptr;
1814  Value* RHSOp0 = nullptr;
1815  unsigned LHSOp0Width = 0;
1816  unsigned RHSOp0Width = 0;
1817  if (LHSShuffle) {
1818  LHSOp0 = LHSShuffle->getOperand(0);
1819  LHSOp1 = LHSShuffle->getOperand(1);
1820  LHSOp0Width = LHSOp0->getType()->getVectorNumElements();
1821  }
1822  if (RHSShuffle) {
1823  RHSOp0 = RHSShuffle->getOperand(0);
1824  RHSOp0Width = RHSOp0->getType()->getVectorNumElements();
1825  }
1826  Value* newLHS = LHS;
1827  Value* newRHS = RHS;
1828  if (LHSShuffle) {
1829  // case 1
1830  if (isa<UndefValue>(RHS)) {
1831  newLHS = LHSOp0;
1832  newRHS = LHSOp1;
1833  }
1834  // case 2 or 4
1835  else if (LHSOp0Width == LHSWidth) {
1836  newLHS = LHSOp0;
1837  }
1838  }
1839  // case 3 or 4
1840  if (RHSShuffle && RHSOp0Width == LHSWidth) {
1841  newRHS = RHSOp0;
1842  }
1843  // case 4
1844  if (LHSOp0 == RHSOp0) {
1845  newLHS = LHSOp0;
1846  newRHS = nullptr;
1847  }
1848 
1849  if (newLHS == LHS && newRHS == RHS)
1850  return MadeChange ? &SVI : nullptr;
1851 
1852  SmallVector<int, 16> LHSMask;
1853  SmallVector<int, 16> RHSMask;
1854  if (newLHS != LHS)
1855  LHSMask = LHSShuffle->getShuffleMask();
1856  if (RHSShuffle && newRHS != RHS)
1857  RHSMask = RHSShuffle->getShuffleMask();
1858 
1859  unsigned newLHSWidth = (newLHS != LHS) ? LHSOp0Width : LHSWidth;
1860  SmallVector<int, 16> newMask;
1861  bool isSplat = true;
1862  int SplatElt = -1;
1863  // Create a new mask for the new ShuffleVectorInst so that the new
1864  // ShuffleVectorInst is equivalent to the original one.
1865  for (unsigned i = 0; i < VWidth; ++i) {
1866  int eltMask;
1867  if (Mask[i] < 0) {
1868  // This element is an undef value.
1869  eltMask = -1;
1870  } else if (Mask[i] < (int)LHSWidth) {
1871  // This element is from left hand side vector operand.
1872  //
1873  // If LHS is going to be replaced (case 1, 2, or 4), calculate the
1874  // new mask value for the element.
1875  if (newLHS != LHS) {
1876  eltMask = LHSMask[Mask[i]];
1877  // If the value selected is an undef value, explicitly specify it
1878  // with a -1 mask value.
1879  if (eltMask >= (int)LHSOp0Width && isa<UndefValue>(LHSOp1))
1880  eltMask = -1;
1881  } else
1882  eltMask = Mask[i];
1883  } else {
1884  // This element is from right hand side vector operand
1885  //
1886  // If the value selected is an undef value, explicitly specify it
1887  // with a -1 mask value. (case 1)
1888  if (isa<UndefValue>(RHS))
1889  eltMask = -1;
1890  // If RHS is going to be replaced (case 3 or 4), calculate the
1891  // new mask value for the element.
1892  else if (newRHS != RHS) {
1893  eltMask = RHSMask[Mask[i]-LHSWidth];
1894  // If the value selected is an undef value, explicitly specify it
1895  // with a -1 mask value.
1896  if (eltMask >= (int)RHSOp0Width) {
1897  assert(isa<UndefValue>(RHSShuffle->getOperand(1))
1898  && "should have been check above");
1899  eltMask = -1;
1900  }
1901  } else
1902  eltMask = Mask[i]-LHSWidth;
1903 
1904  // If LHS's width is changed, shift the mask value accordingly.
1905  // If newRHS == nullptr, i.e. LHSOp0 == RHSOp0, we want to remap any
1906  // references from RHSOp0 to LHSOp0, so we don't need to shift the mask.
1907  // If newRHS == newLHS, we want to remap any references from newRHS to
1908  // newLHS so that we can properly identify splats that may occur due to
1909  // obfuscation across the two vectors.
1910  if (eltMask >= 0 && newRHS != nullptr && newLHS != newRHS)
1911  eltMask += newLHSWidth;
1912  }
1913 
1914  // Check if this could still be a splat.
1915  if (eltMask >= 0) {
1916  if (SplatElt >= 0 && SplatElt != eltMask)
1917  isSplat = false;
1918  SplatElt = eltMask;
1919  }
1920 
1921  newMask.push_back(eltMask);
1922  }
1923 
1924  // If the result mask is equal to one of the original shuffle masks,
1925  // or is a splat, do the replacement.
1926  if (isSplat || newMask == LHSMask || newMask == RHSMask || newMask == Mask) {
1928  for (unsigned i = 0, e = newMask.size(); i != e; ++i) {
1929  if (newMask[i] < 0) {
1930  Elts.push_back(UndefValue::get(Int32Ty));
1931  } else {
1932  Elts.push_back(ConstantInt::get(Int32Ty, newMask[i]));
1933  }
1934  }
1935  if (!newRHS)
1936  newRHS = UndefValue::get(newLHS->getType());
1937  return new ShuffleVectorInst(newLHS, newRHS, ConstantVector::get(Elts));
1938  }
1939 
1940  // If the result mask is an identity, replace uses of this instruction with
1941  // corresponding argument.
1942  bool isLHSID, isRHSID;
1943  recognizeIdentityMask(newMask, isLHSID, isRHSID);
1944  if (isLHSID && VWidth == LHSOp0Width) return replaceInstUsesWith(SVI, newLHS);
1945  if (isRHSID && VWidth == RHSOp0Width) return replaceInstUsesWith(SVI, newRHS);
1946 
1947  return MadeChange ? &SVI : nullptr;
1948 }
static bool isShuffleEquivalentToSelect(ShuffleVectorInst &Shuf)
std::pair< Value *, Value * > ShuffleOps
We are building a shuffle to create V, which is a sequence of insertelement, extractelement pairs...
Instruction * visitInsertValueInst(InsertValueInst &IV)
Try to find redundant insertvalue instructions, like the following ones: %0 = insertvalue { i8...
bool isIdentityWithExtract() const
Return true if this shuffle extracts the first N elements of exactly one source vector.
uint64_t CallInst * C
Instruction * InsertNewInstWith(Instruction *New, Instruction &Old)
Same as InsertNewInstBefore, but also sets the debug loc.
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:71
class_match< UndefValue > m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:87
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:79
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
static APInt getAllOnesValue(unsigned numBits)
Get the all-ones value.
Definition: APInt.h:562
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:250
static Value * evaluateInDifferentElementOrder(Value *V, ArrayRef< int > Mask)
static Instruction * foldSelectShuffle(ShuffleVectorInst &Shuf, InstCombiner::BuilderTy &Builder, const DataLayout &DL)
Try to fold shuffles that are the equivalent of a vector select.
Value * findScalarElement(Value *V, unsigned EltNo)
Given a vector and an element number, see if the scalar value is already around as a register...
This class represents lattice values for constants.
Definition: AllocatorList.h:24
BinaryOps getOpcode() const
Definition: InstrTypes.h:316
static Instruction * foldConstantInsEltIntoShuffle(InsertElementInst &InsElt)
insertelt (shufflevector X, CVec, Mask|insertelt X, C1, CIndex1), C, CIndex –> shufflevector X...
static bool cheapToScalarize(Value *V, bool IsConstantExtractIndex)
Return true if the value is cheaper to scalarize than it is to leave as a vector operation.
static ConstantAggregateZero * get(Type *Ty)
Definition: Constants.cpp:1332
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value *> IdxList, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Definition: Instructions.h:880
static Instruction * foldIdentityExtractShuffle(ShuffleVectorInst &Shuf)
Try to combine 2 shuffles into 1 shuffle by concatenating a shuffle mask.
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElement(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:90
This instruction constructs a fixed permutation of two input vectors.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", Instruction *InsertBefore=nullptr, Instruction *MDFrom=nullptr)
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:705
ThreeOps_match< V1_t, V2_t, Mask_t, Instruction::ShuffleVector > m_ShuffleVector(const V1_t &v1, const V2_t &v2, const Mask_t &m)
Matches ShuffleVectorInst.
static bool isShuffleExtractingFromLHS(ShuffleVectorInst &SVI, SmallVector< int, 16 > &Mask)
Hexagon Common GEP
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:230
static Instruction * hoistInsEltConst(InsertElementInst &InsElt2, InstCombiner::BuilderTy &Builder)
If we have an insertelement instruction feeding into another insertelement and the 2nd is inserting a...
void copyIRFlags(const Value *V, bool IncludeWrapFlags=true)
Convenience method to copy supported exact, fast-math, and (optionally) wrapping flags from V to this...
bool hasNoSignedWrap() const
Determine whether the no signed wrap flag is set.
static BinopElts getAlternateBinop(BinaryOperator *BO, const DataLayout &DL)
Binops may be transformed into binops with different opcodes and operands.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:48
Constant * getMask() const
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
unsigned getBitWidth() const
Return the number of bits in the Vector type.
Definition: DerivedTypes.h:452
void setBit(unsigned BitPosition)
Set a given bit to 1.
Definition: APInt.h:1403
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElement(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:162
static Optional< unsigned > getOpcode(ArrayRef< VPValue *> Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:197
void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:743
The core instruction combiner logic.
void dropPoisonGeneratingFlags()
Drops flags that may cause this instruction to evaluate to poison despite having non-poison inputs...
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if &#39;V & Mask&#39; is known to be zero.
uint64_t getNumElements() const
Definition: DerivedTypes.h:359
BinopElts(BinaryOperator::BinaryOps Opc=(BinaryOperator::BinaryOps) 0, Value *V0=nullptr, Value *V1=nullptr)
This file implements a class to represent arbitrary precision integral constant values and operations...
void assign(size_type NumElts, const T &Elt)
Definition: SmallVector.h:423
static Instruction * foldInsSequenceIntoBroadcast(InsertElementInst &InsElt)
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1732
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
static Instruction * foldShuffleWithInsert(ShuffleVectorInst &Shuf)
Try to replace a shuffle with an insertelement.
static Constant * getSafeVectorConstantForBinop(BinaryOperator::BinaryOps Opcode, Constant *In, bool IsRHSConstant)
Some binary operators require special handling to avoid poison and undefined behavior.
static bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
Definition: Type.cpp:621
This instruction compares its operands according to the predicate given to the constructor.
void andIRFlags(const Value *V)
Logical &#39;and&#39; of any supported wrapping, exact, and fast-math flags of V and this instruction...
This class represents a no-op cast from one type to another.
static CmpInst * Create(OtherOps Op, Predicate predicate, Value *S1, Value *S2, const Twine &Name="", Instruction *InsertBefore=nullptr)
Construct a compare instruction, given the opcode, the predicate and the two operands.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:82
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:126
Instruction * visitExtractElementInst(ExtractElementInst &EI)
These are the ingredients in an alternate form binary operator as described below.
static bool collectSingleShuffleElements(Value *V, Value *LHS, Value *RHS, SmallVectorImpl< Constant *> &Mask)
If V is a shuffle of values that ONLY returns elements from either LHS or RHS, return the shuffle mas...
bool isSelect() const
Return true if this shuffle chooses elements from its source vectors without lane crossings and all o...
VectorType * getType() const
Overload to return most specific vector type.
This class represents a truncation of integer types.
Value * getOperand(unsigned i) const
Definition: User.h:170
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:335
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return &#39;this&#39;.
Definition: Type.h:304
Instruction * visitInsertElementInst(InsertElementInst &IE)
Value * SimplifyInsertElementInst(Value *Vec, Value *Elt, Value *Idx, const SimplifyQuery &Q)
Given operands for an InsertElement, fold the result or return null.
an instruction for type-safe pointer arithmetic to access elements of arrays and structs ...
Definition: Instructions.h:854
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:62
This instruction inserts a single (scalar) element into a VectorType value.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
Definition: PatternMatch.h:176
const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
Definition: BasicBlock.cpp:217
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
CastClass_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
Definition: Constant.h:42
This file contains the declarations for the subclasses of Constant, which represent the different fla...
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:502
static Constant * getShuffleVector(Constant *V1, Constant *V2, Constant *Mask, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2148
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly...
Definition: STLExtras.h:1193
This instruction compares its operands according to the predicate given to the constructor.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:646
static Constant * getBinOpIdentity(unsigned Opcode, Type *Ty, bool AllowRHSConstant=false)
Return the identity constant for a binary opcode.
Definition: Constants.cpp:2326
op_range operands()
Definition: User.h:238
self_iterator getIterator()
Definition: ilist_node.h:82
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:74
Class to represent integer types.
Definition: DerivedTypes.h:40
VectorType * getVectorOperandType() const
static void replaceExtractElements(InsertElementInst *InsElt, ExtractElementInst *ExtElt, InstCombiner &IC)
If we have insertion into a vector that is wider than the vector that we are extracting from...
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
static UndefValue * get(Type *T)
Static factory methods - Return an &#39;undef&#39; object of the specified type.
Definition: Constants.cpp:1415
size_t size() const
Definition: SmallVector.h:53
static wasm::ValType getType(const TargetRegisterClass *RC)
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
bool isExact() const
Determine whether the exact flag is set.
static int getMaskValue(const Constant *Mask, unsigned Elt)
Return the shuffle mask value for the specified element of the mask.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Value * CreateTrunc(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:1655
Value * SimplifyShuffleVectorInst(Value *Op0, Value *Op1, Constant *Mask, Type *RetTy, const SimplifyQuery &Q)
Given operands for a ShuffleVectorInst, fold the result or return null.
void setHasNoSignedWrap(bool b=true)
Set or clear the nsw flag on this instruction, which must be an operator which supports this flag...
Predicate getPredicate(unsigned Condition, unsigned Hint)
Return predicate consisting of specified condition and hint bits.
Definition: PPCPredicates.h:88
Iterator for intrusive lists based on ilist_node.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
Definition: Constants.h:251
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type...
Definition: Type.cpp:130
BinaryOperator::BinaryOps Opcode
bool containsUndefElement() const
Return true if this is a vector constant that includes any undefined elements.
Definition: Constants.cpp:254
This is a &#39;vector&#39; (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:847
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Definition: Instruction.h:64
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
Value * CreateInsertElement(Value *Vec, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2054
static Instruction * foldSelectShuffleWith1Binop(ShuffleVectorInst &Shuf)
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition: Type.cpp:180
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2068
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:622
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
unsigned getNumIncomingValues() const
Return the number of incoming edges.
void setOperand(unsigned i, Value *Val)
Definition: User.h:175
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:941
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:462
Class to represent vector types.
Definition: DerivedTypes.h:393
Class for arbitrary precision integers.
Definition: APInt.h:70
static BinaryOperator * Create(BinaryOps Op, Value *S1, Value *S2, const Twine &Name=Twine(), Instruction *InsertBefore=nullptr)
Construct a binary instruction, given the opcode and the two operands.
iterator_range< user_iterator > users()
Definition: Value.h:400
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array...
Definition: ArrayRef.h:179
static CastInst * Create(Instruction::CastOps, Value *S, Type *Ty, const Twine &Name="", Instruction *InsertBefore=nullptr)
Provides a way to construct any of the CastInst subclasses using an opcode instead of the subclass&#39;s ...
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:546
static BinaryOperator * CreateWithCopiedFlags(BinaryOps Opc, Value *V1, Value *V2, BinaryOperator *CopyBO, const Twine &Name="")
Definition: InstrTypes.h:163
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:176
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:107
void insertAfter(Instruction *InsertPos)
Insert an unlinked instruction into a basic block immediately after the specified instruction...
Definition: Instruction.cpp:80
#define I(x, y, z)
Definition: MD5.cpp:58
Instruction * visitShuffleVectorInst(ShuffleVectorInst &SVI)
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
This instruction extracts a single (scalar) element from a VectorType value.
static Constant * getShl(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2309
static bool canEvaluateShuffled(Value *V, ArrayRef< int > Mask, unsigned Depth=5)
Return true if we can evaluate the specified expression tree if the vector elements were shuffled in ...
static Instruction * foldBitcastExtElt(ExtractElementInst &Ext, InstCombiner::BuilderTy &Builder, bool IsBigEndian)
bool hasNoUnsignedWrap() const
Determine whether the no unsigned wrap flag is set.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Instruction * replaceInstUsesWith(Instruction &I, Value *V)
A combiner-aware RAUW-like routine.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:115
static Instruction * narrowVectorSelect(ShuffleVectorInst &Shuf, InstCombiner::BuilderTy &Builder)
Match a shuffle-select-shuffle pattern where the shuffles are widening and narrowing (concatenating w...
ArrayRef< unsigned > getIndices() const
LLVM Value Representation.
Definition: Value.h:73
This file provides internal interfaces used to implement the InstCombine.
static VectorType * get(Type *ElementType, unsigned NumElements)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:606
Value * SimplifyExtractElementInst(Value *Vec, Value *Idx, const SimplifyQuery &Q)
Given operands for an ExtractElementInst, fold the result or return null.
static Value * buildNew(Instruction *I, ArrayRef< Value *> NewOps)
Rebuild a new instruction just like &#39;I&#39; but with the new operands given.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E&#39;s largest value.
Definition: BitmaskEnum.h:81
Value * CreateLShr(Value *LHS, Value *RHS, const Twine &Name="", bool isExact=false)
Definition: IRBuilder.h:1124
Type * getElementType() const
Definition: DerivedTypes.h:360
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:413
bool isIntDivRem() const
Definition: Instruction.h:132
static bool isSplat(ArrayRef< Value *> VL)
static void recognizeIdentityMask(const SmallVectorImpl< int > &Mask, bool &isLHSID, bool &isRHSID)
static ShuffleOps collectShuffleElements(Value *V, SmallVectorImpl< Constant *> &Mask, Value *PermittedRHS, InstCombiner &IC)
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", Instruction *InsertBefore=nullptr)
VectorType * getType() const
Overload to return most specific vector type.
static Constant * get(ArrayRef< Constant *> V)
Definition: Constants.cpp:1079
static void commuteShuffleMask(MutableArrayRef< int > Mask, unsigned InVecNumElts)
Change values in a shuffle permute mask assuming the two vector operands of length InVecNumElts have ...
IntegerType * Int32Ty
User * user_back()
Definition: Value.h:386
const BasicBlock * getParent() const
Definition: Instruction.h:67
This instruction inserts a struct field of array element value into an aggregate value.