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" 21 #include "llvm/Analysis/InstructionSimplify.h" 22 #include "llvm/Analysis/VectorUtils.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" 36 #include "llvm/Support/ErrorHandling.h" 37 #include "llvm/Transforms/InstCombine/InstCombineWorklist.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. 61 if (match(V, m_InsertElement(m_Value(), m_Value(), m_ConstantInt()))) 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) { 86 SmallVector<Instruction *, 2> Extracts; 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( 136 BinaryOperator::CreateWithCopiedFlags(B0->getOpcode(), 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 163 static Instruction *foldBitcastExtElt(ExtractElementInst &Ext, 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 257 Instruction *InstCombiner::visitExtractElementInst(ExtractElementInst &EI) { 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 } 346 Type *Int32Ty = Type::getInt32Ty(EI.getContext()); 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, 368 SmallVectorImpl<Constant*> &Mask) { 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) 380 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), i)); 381 return true; 382 } 383 384 if (V == RHS) { 385 for (unsigned i = 0; i != NumElts; ++i) 386 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), 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] = 423 ConstantInt::get(Type::getInt32Ty(V->getContext()), 424 ExtractedIdx); 425 } else { 426 assert(EI->getOperand(0) == RHS); 427 Mask[InsertedIdx % NumElts] = 428 ConstantInt::get(Type::getInt32Ty(V->getContext()), 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. 444 static void replaceExtractElements(InsertElementInst *InsElt, 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()) { 509 ExtractElementInst *OldExt = dyn_cast<ExtractElementInst>(U); 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 528 static ShuffleOps collectShuffleElements(Value *V, 529 SmallVectorImpl<Constant *> &Mask, 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] = 579 ConstantInt::get(Type::getInt32Ty(V->getContext()), 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 = 588 EI->getOperand(0)->getType()->getVectorNumElements(); 589 for (unsigned i = 0; i != NumElts; ++i) 590 Mask.push_back(ConstantInt::get( 591 Type::getInt32Ty(V->getContext()), 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) 608 Mask.push_back(ConstantInt::get(Type::getInt32Ty(V->getContext()), 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 619 Instruction *InstCombiner::visitInsertValueInst(InsertValueInst &I) { 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 647 static bool isShuffleEquivalentToSelect(ShuffleVectorInst &Shuf) { 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) 670 static Instruction *foldInsSequenceIntoBroadcast(InsertElementInst &InsElt) { 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, 720 ConstantInt::get(Type::getInt32Ty(InsElt.getContext()), 0), 721 "", &InsElt); 722 723 Constant *ZeroMask = ConstantAggregateZero::get( 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. 738 static Instruction *hoistInsEltConst(InsertElementInst &InsElt2, 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' 761 static Instruction *foldConstantInsEltIntoShuffle(InsertElementInst &InsElt) { 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); 827 SmallVector<Constant *, 16> Mask(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 858 Instruction *InstCombiner::visitInsertElementInst(InsertElementInst &IE) { 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)) { 916 SmallVector<Constant*, 16> Mask; 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 940 if (Instruction *Shuf = foldConstantInsEltIntoShuffle(IE)) 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. 956 static bool canEvaluateShuffled(Value *V, ArrayRef<int> Mask, 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. 963 Instruction *I = dyn_cast<Instruction>(V); 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: { 1017 ConstantInt *CI = dyn_cast<ConstantInt>(I->getOperand(2)); 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. 1039 static Value *buildNew(Instruction *I, ArrayRef<Value*> NewOps) { 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 = 1097 VectorType::get(I->getType()->getScalarType(), 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); 1106 GetElementPtrInst *GEP = GetElementPtrInst::Create( 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 1115 static Value *evaluateInDifferentElementOrder(Value *V, ArrayRef<int> Mask) { 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 } 1135 return ConstantExpr::getShuffleVector(C, UndefValue::get(C->getType()), 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 1212 static void recognizeIdentityMask(const SmallVectorImpl<int> &Mask, 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 // +--+--+--+--+ 1232 static bool isShuffleExtractingFromLHS(ShuffleVectorInst &SVI, 1233 SmallVector<int, 16> &Mask) { 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 { 1249 BinaryOperator::BinaryOps Opcode; 1250 Value *Op0; 1251 Value *Op1; 1252 BinopElts(BinaryOperator::BinaryOps Opc = (BinaryOperator::BinaryOps)0, 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. 1262 static BinopElts getAlternateBinop(BinaryOperator *BO, const DataLayout &DL) { 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 1288 static Instruction *foldSelectShuffleWith1Binop(ShuffleVectorInst &Shuf) { 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. 1341 static Instruction *foldSelectShuffle(ShuffleVectorInst &Shuf, 1342 InstCombiner::BuilderTy &Builder, 1343 const DataLayout &DL) { 1344 if (!Shuf.isSelect()) 1345 return nullptr; 1346 1347 if (Instruction *I = foldSelectShuffleWith1Binop(Shuf)) 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() && 1403 (Instruction::isIntDivRem(BOpc) || Instruction::isShift(BOpc)); 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. 1459 static Instruction *narrowVectorSelect(ShuffleVectorInst &Shuf, 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) 1485 Value *Undef = UndefValue::get(X->getType()); 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. 1492 static Instruction *foldIdentityExtractShuffle(ShuffleVectorInst &Shuf) { 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. 1527 static Instruction *foldShuffleWithInsert(ShuffleVectorInst &Shuf) { 1528 Value *V0 = Shuf.getOperand(0), *V1 = Shuf.getOperand(1); 1529 SmallVector<int, 16> Mask = Shuf.getShuffleMask(); 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); 1584 ShuffleVectorInst::commuteShuffleMask(Mask, NumElts); 1585 if (isShufflingScalarIntoOp1(Scalar, IndexC)) 1586 return InsertElementInst::Create(V1, Scalar, IndexC); 1587 1588 return nullptr; 1589 } 1590 1591 Instruction *InstCombiner::visitShuffleVectorInst(ShuffleVectorInst &SVI) { 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 1613 if (Instruction *I = foldIdentityExtractShuffle(SVI)) 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 1621 SmallVector<int, 16> Mask = SVI.getShuffleMask(); 1622 Type *Int32Ty = Type::getInt32Ty(SVI.getContext()); 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. 1630 SmallVector<Constant*, 16> Elts; 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(); 1706 SmallVector<BitCastInst *, 8> BCs; 1707 DenseMap<Type *, Value *> NewBCs; 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) { 1927 SmallVector<Constant*, 16> Elts; 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 } 1949