LLVM 20.0.0git
VectorCombine.cpp
Go to the documentation of this file.
1//===------- VectorCombine.cpp - Optimize partial vector operations -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass optimizes scalar/vector interactions using target cost models. The
10// transforms implemented here may not fit in traditional loop-based or SLP
11// vectorization passes.
12//
13//===----------------------------------------------------------------------===//
14
16#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/ScopeExit.h"
19#include "llvm/ADT/Statistic.h"
23#include "llvm/Analysis/Loads.h"
27#include "llvm/IR/Dominators.h"
28#include "llvm/IR/Function.h"
29#include "llvm/IR/IRBuilder.h"
34#include <numeric>
35#include <queue>
36
37#define DEBUG_TYPE "vector-combine"
39
40using namespace llvm;
41using namespace llvm::PatternMatch;
42
43STATISTIC(NumVecLoad, "Number of vector loads formed");
44STATISTIC(NumVecCmp, "Number of vector compares formed");
45STATISTIC(NumVecBO, "Number of vector binops formed");
46STATISTIC(NumVecCmpBO, "Number of vector compare + binop formed");
47STATISTIC(NumShufOfBitcast, "Number of shuffles moved after bitcast");
48STATISTIC(NumScalarBO, "Number of scalar binops formed");
49STATISTIC(NumScalarCmp, "Number of scalar compares formed");
50
52 "disable-vector-combine", cl::init(false), cl::Hidden,
53 cl::desc("Disable all vector combine transforms"));
54
56 "disable-binop-extract-shuffle", cl::init(false), cl::Hidden,
57 cl::desc("Disable binop extract to shuffle transforms"));
58
60 "vector-combine-max-scan-instrs", cl::init(30), cl::Hidden,
61 cl::desc("Max number of instructions to scan for vector combining."));
62
63static const unsigned InvalidIndex = std::numeric_limits<unsigned>::max();
64
65namespace {
66class VectorCombine {
67public:
68 VectorCombine(Function &F, const TargetTransformInfo &TTI,
69 const DominatorTree &DT, AAResults &AA, AssumptionCache &AC,
70 const DataLayout *DL, bool TryEarlyFoldsOnly)
71 : F(F), Builder(F.getContext()), TTI(TTI), DT(DT), AA(AA), AC(AC), DL(DL),
72 TryEarlyFoldsOnly(TryEarlyFoldsOnly) {}
73
74 bool run();
75
76private:
77 Function &F;
78 IRBuilder<> Builder;
80 const DominatorTree &DT;
81 AAResults &AA;
83 const DataLayout *DL;
84
85 /// If true, only perform beneficial early IR transforms. Do not introduce new
86 /// vector operations.
87 bool TryEarlyFoldsOnly;
88
89 InstructionWorklist Worklist;
90
91 // TODO: Direct calls from the top-level "run" loop use a plain "Instruction"
92 // parameter. That should be updated to specific sub-classes because the
93 // run loop was changed to dispatch on opcode.
94 bool vectorizeLoadInsert(Instruction &I);
95 bool widenSubvectorLoad(Instruction &I);
96 ExtractElementInst *getShuffleExtract(ExtractElementInst *Ext0,
98 unsigned PreferredExtractIndex) const;
99 bool isExtractExtractCheap(ExtractElementInst *Ext0, ExtractElementInst *Ext1,
100 const Instruction &I,
101 ExtractElementInst *&ConvertToShuffle,
102 unsigned PreferredExtractIndex);
103 void foldExtExtCmp(ExtractElementInst *Ext0, ExtractElementInst *Ext1,
104 Instruction &I);
105 void foldExtExtBinop(ExtractElementInst *Ext0, ExtractElementInst *Ext1,
106 Instruction &I);
107 bool foldExtractExtract(Instruction &I);
108 bool foldInsExtFNeg(Instruction &I);
109 bool foldBitcastShuffle(Instruction &I);
110 bool scalarizeBinopOrCmp(Instruction &I);
111 bool scalarizeVPIntrinsic(Instruction &I);
112 bool foldExtractedCmps(Instruction &I);
113 bool foldSingleElementStore(Instruction &I);
114 bool scalarizeLoadExtract(Instruction &I);
115 bool foldShuffleOfBinops(Instruction &I);
116 bool foldShuffleOfCastops(Instruction &I);
117 bool foldShuffleOfShuffles(Instruction &I);
118 bool foldShuffleToIdentity(Instruction &I);
119 bool foldShuffleFromReductions(Instruction &I);
120 bool foldCastFromReductions(Instruction &I);
121 bool foldSelectShuffle(Instruction &I, bool FromReduction = false);
122
123 void replaceValue(Value &Old, Value &New) {
124 Old.replaceAllUsesWith(&New);
125 if (auto *NewI = dyn_cast<Instruction>(&New)) {
126 New.takeName(&Old);
127 Worklist.pushUsersToWorkList(*NewI);
128 Worklist.pushValue(NewI);
129 }
130 Worklist.pushValue(&Old);
131 }
132
134 for (Value *Op : I.operands())
135 Worklist.pushValue(Op);
136 Worklist.remove(&I);
137 I.eraseFromParent();
138 }
139};
140} // namespace
141
142/// Return the source operand of a potentially bitcasted value. If there is no
143/// bitcast, return the input value itself.
145 while (auto *BitCast = dyn_cast<BitCastInst>(V))
146 V = BitCast->getOperand(0);
147 return V;
148}
149
150static bool canWidenLoad(LoadInst *Load, const TargetTransformInfo &TTI) {
151 // Do not widen load if atomic/volatile or under asan/hwasan/memtag/tsan.
152 // The widened load may load data from dirty regions or create data races
153 // non-existent in the source.
154 if (!Load || !Load->isSimple() || !Load->hasOneUse() ||
155 Load->getFunction()->hasFnAttribute(Attribute::SanitizeMemTag) ||
157 return false;
158
159 // We are potentially transforming byte-sized (8-bit) memory accesses, so make
160 // sure we have all of our type-based constraints in place for this target.
161 Type *ScalarTy = Load->getType()->getScalarType();
162 uint64_t ScalarSize = ScalarTy->getPrimitiveSizeInBits();
163 unsigned MinVectorSize = TTI.getMinVectorRegisterBitWidth();
164 if (!ScalarSize || !MinVectorSize || MinVectorSize % ScalarSize != 0 ||
165 ScalarSize % 8 != 0)
166 return false;
167
168 return true;
169}
170
171bool VectorCombine::vectorizeLoadInsert(Instruction &I) {
172 // Match insert into fixed vector of scalar value.
173 // TODO: Handle non-zero insert index.
174 Value *Scalar;
175 if (!match(&I, m_InsertElt(m_Undef(), m_Value(Scalar), m_ZeroInt())) ||
176 !Scalar->hasOneUse())
177 return false;
178
179 // Optionally match an extract from another vector.
180 Value *X;
181 bool HasExtract = match(Scalar, m_ExtractElt(m_Value(X), m_ZeroInt()));
182 if (!HasExtract)
183 X = Scalar;
184
185 auto *Load = dyn_cast<LoadInst>(X);
186 if (!canWidenLoad(Load, TTI))
187 return false;
188
189 Type *ScalarTy = Scalar->getType();
190 uint64_t ScalarSize = ScalarTy->getPrimitiveSizeInBits();
191 unsigned MinVectorSize = TTI.getMinVectorRegisterBitWidth();
192
193 // Check safety of replacing the scalar load with a larger vector load.
194 // We use minimal alignment (maximum flexibility) because we only care about
195 // the dereferenceable region. When calculating cost and creating a new op,
196 // we may use a larger value based on alignment attributes.
197 Value *SrcPtr = Load->getPointerOperand()->stripPointerCasts();
198 assert(isa<PointerType>(SrcPtr->getType()) && "Expected a pointer type");
199
200 unsigned MinVecNumElts = MinVectorSize / ScalarSize;
201 auto *MinVecTy = VectorType::get(ScalarTy, MinVecNumElts, false);
202 unsigned OffsetEltIndex = 0;
203 Align Alignment = Load->getAlign();
204 if (!isSafeToLoadUnconditionally(SrcPtr, MinVecTy, Align(1), *DL, Load, &AC,
205 &DT)) {
206 // It is not safe to load directly from the pointer, but we can still peek
207 // through gep offsets and check if it safe to load from a base address with
208 // updated alignment. If it is, we can shuffle the element(s) into place
209 // after loading.
210 unsigned OffsetBitWidth = DL->getIndexTypeSizeInBits(SrcPtr->getType());
211 APInt Offset(OffsetBitWidth, 0);
213
214 // We want to shuffle the result down from a high element of a vector, so
215 // the offset must be positive.
216 if (Offset.isNegative())
217 return false;
218
219 // The offset must be a multiple of the scalar element to shuffle cleanly
220 // in the element's size.
221 uint64_t ScalarSizeInBytes = ScalarSize / 8;
222 if (Offset.urem(ScalarSizeInBytes) != 0)
223 return false;
224
225 // If we load MinVecNumElts, will our target element still be loaded?
226 OffsetEltIndex = Offset.udiv(ScalarSizeInBytes).getZExtValue();
227 if (OffsetEltIndex >= MinVecNumElts)
228 return false;
229
230 if (!isSafeToLoadUnconditionally(SrcPtr, MinVecTy, Align(1), *DL, Load, &AC,
231 &DT))
232 return false;
233
234 // Update alignment with offset value. Note that the offset could be negated
235 // to more accurately represent "(new) SrcPtr - Offset = (old) SrcPtr", but
236 // negation does not change the result of the alignment calculation.
237 Alignment = commonAlignment(Alignment, Offset.getZExtValue());
238 }
239
240 // Original pattern: insertelt undef, load [free casts of] PtrOp, 0
241 // Use the greater of the alignment on the load or its source pointer.
242 Alignment = std::max(SrcPtr->getPointerAlignment(*DL), Alignment);
243 Type *LoadTy = Load->getType();
244 unsigned AS = Load->getPointerAddressSpace();
245 InstructionCost OldCost =
246 TTI.getMemoryOpCost(Instruction::Load, LoadTy, Alignment, AS);
247 APInt DemandedElts = APInt::getOneBitSet(MinVecNumElts, 0);
249 OldCost +=
250 TTI.getScalarizationOverhead(MinVecTy, DemandedElts,
251 /* Insert */ true, HasExtract, CostKind);
252
253 // New pattern: load VecPtr
254 InstructionCost NewCost =
255 TTI.getMemoryOpCost(Instruction::Load, MinVecTy, Alignment, AS);
256 // Optionally, we are shuffling the loaded vector element(s) into place.
257 // For the mask set everything but element 0 to undef to prevent poison from
258 // propagating from the extra loaded memory. This will also optionally
259 // shrink/grow the vector from the loaded size to the output size.
260 // We assume this operation has no cost in codegen if there was no offset.
261 // Note that we could use freeze to avoid poison problems, but then we might
262 // still need a shuffle to change the vector size.
263 auto *Ty = cast<FixedVectorType>(I.getType());
264 unsigned OutputNumElts = Ty->getNumElements();
266 assert(OffsetEltIndex < MinVecNumElts && "Address offset too big");
267 Mask[0] = OffsetEltIndex;
268 if (OffsetEltIndex)
269 NewCost += TTI.getShuffleCost(TTI::SK_PermuteSingleSrc, MinVecTy, Mask);
270
271 // We can aggressively convert to the vector form because the backend can
272 // invert this transform if it does not result in a performance win.
273 if (OldCost < NewCost || !NewCost.isValid())
274 return false;
275
276 // It is safe and potentially profitable to load a vector directly:
277 // inselt undef, load Scalar, 0 --> load VecPtr
278 IRBuilder<> Builder(Load);
279 Value *CastedPtr =
280 Builder.CreatePointerBitCastOrAddrSpaceCast(SrcPtr, Builder.getPtrTy(AS));
281 Value *VecLd = Builder.CreateAlignedLoad(MinVecTy, CastedPtr, Alignment);
282 VecLd = Builder.CreateShuffleVector(VecLd, Mask);
283
284 replaceValue(I, *VecLd);
285 ++NumVecLoad;
286 return true;
287}
288
289/// If we are loading a vector and then inserting it into a larger vector with
290/// undefined elements, try to load the larger vector and eliminate the insert.
291/// This removes a shuffle in IR and may allow combining of other loaded values.
292bool VectorCombine::widenSubvectorLoad(Instruction &I) {
293 // Match subvector insert of fixed vector.
294 auto *Shuf = cast<ShuffleVectorInst>(&I);
295 if (!Shuf->isIdentityWithPadding())
296 return false;
297
298 // Allow a non-canonical shuffle mask that is choosing elements from op1.
299 unsigned NumOpElts =
300 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
301 unsigned OpIndex = any_of(Shuf->getShuffleMask(), [&NumOpElts](int M) {
302 return M >= (int)(NumOpElts);
303 });
304
305 auto *Load = dyn_cast<LoadInst>(Shuf->getOperand(OpIndex));
306 if (!canWidenLoad(Load, TTI))
307 return false;
308
309 // We use minimal alignment (maximum flexibility) because we only care about
310 // the dereferenceable region. When calculating cost and creating a new op,
311 // we may use a larger value based on alignment attributes.
312 auto *Ty = cast<FixedVectorType>(I.getType());
313 Value *SrcPtr = Load->getPointerOperand()->stripPointerCasts();
314 assert(isa<PointerType>(SrcPtr->getType()) && "Expected a pointer type");
315 Align Alignment = Load->getAlign();
316 if (!isSafeToLoadUnconditionally(SrcPtr, Ty, Align(1), *DL, Load, &AC, &DT))
317 return false;
318
319 Alignment = std::max(SrcPtr->getPointerAlignment(*DL), Alignment);
320 Type *LoadTy = Load->getType();
321 unsigned AS = Load->getPointerAddressSpace();
322
323 // Original pattern: insert_subvector (load PtrOp)
324 // This conservatively assumes that the cost of a subvector insert into an
325 // undef value is 0. We could add that cost if the cost model accurately
326 // reflects the real cost of that operation.
327 InstructionCost OldCost =
328 TTI.getMemoryOpCost(Instruction::Load, LoadTy, Alignment, AS);
329
330 // New pattern: load PtrOp
331 InstructionCost NewCost =
332 TTI.getMemoryOpCost(Instruction::Load, Ty, Alignment, AS);
333
334 // We can aggressively convert to the vector form because the backend can
335 // invert this transform if it does not result in a performance win.
336 if (OldCost < NewCost || !NewCost.isValid())
337 return false;
338
339 IRBuilder<> Builder(Load);
340 Value *CastedPtr =
341 Builder.CreatePointerBitCastOrAddrSpaceCast(SrcPtr, Builder.getPtrTy(AS));
342 Value *VecLd = Builder.CreateAlignedLoad(Ty, CastedPtr, Alignment);
343 replaceValue(I, *VecLd);
344 ++NumVecLoad;
345 return true;
346}
347
348/// Determine which, if any, of the inputs should be replaced by a shuffle
349/// followed by extract from a different index.
350ExtractElementInst *VectorCombine::getShuffleExtract(
352 unsigned PreferredExtractIndex = InvalidIndex) const {
353 auto *Index0C = dyn_cast<ConstantInt>(Ext0->getIndexOperand());
354 auto *Index1C = dyn_cast<ConstantInt>(Ext1->getIndexOperand());
355 assert(Index0C && Index1C && "Expected constant extract indexes");
356
357 unsigned Index0 = Index0C->getZExtValue();
358 unsigned Index1 = Index1C->getZExtValue();
359
360 // If the extract indexes are identical, no shuffle is needed.
361 if (Index0 == Index1)
362 return nullptr;
363
364 Type *VecTy = Ext0->getVectorOperand()->getType();
366 assert(VecTy == Ext1->getVectorOperand()->getType() && "Need matching types");
367 InstructionCost Cost0 =
368 TTI.getVectorInstrCost(*Ext0, VecTy, CostKind, Index0);
369 InstructionCost Cost1 =
370 TTI.getVectorInstrCost(*Ext1, VecTy, CostKind, Index1);
371
372 // If both costs are invalid no shuffle is needed
373 if (!Cost0.isValid() && !Cost1.isValid())
374 return nullptr;
375
376 // We are extracting from 2 different indexes, so one operand must be shuffled
377 // before performing a vector operation and/or extract. The more expensive
378 // extract will be replaced by a shuffle.
379 if (Cost0 > Cost1)
380 return Ext0;
381 if (Cost1 > Cost0)
382 return Ext1;
383
384 // If the costs are equal and there is a preferred extract index, shuffle the
385 // opposite operand.
386 if (PreferredExtractIndex == Index0)
387 return Ext1;
388 if (PreferredExtractIndex == Index1)
389 return Ext0;
390
391 // Otherwise, replace the extract with the higher index.
392 return Index0 > Index1 ? Ext0 : Ext1;
393}
394
395/// Compare the relative costs of 2 extracts followed by scalar operation vs.
396/// vector operation(s) followed by extract. Return true if the existing
397/// instructions are cheaper than a vector alternative. Otherwise, return false
398/// and if one of the extracts should be transformed to a shufflevector, set
399/// \p ConvertToShuffle to that extract instruction.
400bool VectorCombine::isExtractExtractCheap(ExtractElementInst *Ext0,
401 ExtractElementInst *Ext1,
402 const Instruction &I,
403 ExtractElementInst *&ConvertToShuffle,
404 unsigned PreferredExtractIndex) {
405 auto *Ext0IndexC = dyn_cast<ConstantInt>(Ext0->getOperand(1));
406 auto *Ext1IndexC = dyn_cast<ConstantInt>(Ext1->getOperand(1));
407 assert(Ext0IndexC && Ext1IndexC && "Expected constant extract indexes");
408
409 unsigned Opcode = I.getOpcode();
410 Type *ScalarTy = Ext0->getType();
411 auto *VecTy = cast<VectorType>(Ext0->getOperand(0)->getType());
412 InstructionCost ScalarOpCost, VectorOpCost;
413
414 // Get cost estimates for scalar and vector versions of the operation.
415 bool IsBinOp = Instruction::isBinaryOp(Opcode);
416 if (IsBinOp) {
417 ScalarOpCost = TTI.getArithmeticInstrCost(Opcode, ScalarTy);
418 VectorOpCost = TTI.getArithmeticInstrCost(Opcode, VecTy);
419 } else {
420 assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
421 "Expected a compare");
422 CmpInst::Predicate Pred = cast<CmpInst>(I).getPredicate();
423 ScalarOpCost = TTI.getCmpSelInstrCost(
424 Opcode, ScalarTy, CmpInst::makeCmpResultType(ScalarTy), Pred);
425 VectorOpCost = TTI.getCmpSelInstrCost(
426 Opcode, VecTy, CmpInst::makeCmpResultType(VecTy), Pred);
427 }
428
429 // Get cost estimates for the extract elements. These costs will factor into
430 // both sequences.
431 unsigned Ext0Index = Ext0IndexC->getZExtValue();
432 unsigned Ext1Index = Ext1IndexC->getZExtValue();
434
435 InstructionCost Extract0Cost =
436 TTI.getVectorInstrCost(*Ext0, VecTy, CostKind, Ext0Index);
437 InstructionCost Extract1Cost =
438 TTI.getVectorInstrCost(*Ext1, VecTy, CostKind, Ext1Index);
439
440 // A more expensive extract will always be replaced by a splat shuffle.
441 // For example, if Ext0 is more expensive:
442 // opcode (extelt V0, Ext0), (ext V1, Ext1) -->
443 // extelt (opcode (splat V0, Ext0), V1), Ext1
444 // TODO: Evaluate whether that always results in lowest cost. Alternatively,
445 // check the cost of creating a broadcast shuffle and shuffling both
446 // operands to element 0.
447 InstructionCost CheapExtractCost = std::min(Extract0Cost, Extract1Cost);
448
449 // Extra uses of the extracts mean that we include those costs in the
450 // vector total because those instructions will not be eliminated.
451 InstructionCost OldCost, NewCost;
452 if (Ext0->getOperand(0) == Ext1->getOperand(0) && Ext0Index == Ext1Index) {
453 // Handle a special case. If the 2 extracts are identical, adjust the
454 // formulas to account for that. The extra use charge allows for either the
455 // CSE'd pattern or an unoptimized form with identical values:
456 // opcode (extelt V, C), (extelt V, C) --> extelt (opcode V, V), C
457 bool HasUseTax = Ext0 == Ext1 ? !Ext0->hasNUses(2)
458 : !Ext0->hasOneUse() || !Ext1->hasOneUse();
459 OldCost = CheapExtractCost + ScalarOpCost;
460 NewCost = VectorOpCost + CheapExtractCost + HasUseTax * CheapExtractCost;
461 } else {
462 // Handle the general case. Each extract is actually a different value:
463 // opcode (extelt V0, C0), (extelt V1, C1) --> extelt (opcode V0, V1), C
464 OldCost = Extract0Cost + Extract1Cost + ScalarOpCost;
465 NewCost = VectorOpCost + CheapExtractCost +
466 !Ext0->hasOneUse() * Extract0Cost +
467 !Ext1->hasOneUse() * Extract1Cost;
468 }
469
470 ConvertToShuffle = getShuffleExtract(Ext0, Ext1, PreferredExtractIndex);
471 if (ConvertToShuffle) {
472 if (IsBinOp && DisableBinopExtractShuffle)
473 return true;
474
475 // If we are extracting from 2 different indexes, then one operand must be
476 // shuffled before performing the vector operation. The shuffle mask is
477 // poison except for 1 lane that is being translated to the remaining
478 // extraction lane. Therefore, it is a splat shuffle. Ex:
479 // ShufMask = { poison, poison, 0, poison }
480 // TODO: The cost model has an option for a "broadcast" shuffle
481 // (splat-from-element-0), but no option for a more general splat.
482 NewCost +=
484 }
485
486 // Aggressively form a vector op if the cost is equal because the transform
487 // may enable further optimization.
488 // Codegen can reverse this transform (scalarize) if it was not profitable.
489 return OldCost < NewCost;
490}
491
492/// Create a shuffle that translates (shifts) 1 element from the input vector
493/// to a new element location.
494static Value *createShiftShuffle(Value *Vec, unsigned OldIndex,
495 unsigned NewIndex, IRBuilder<> &Builder) {
496 // The shuffle mask is poison except for 1 lane that is being translated
497 // to the new element index. Example for OldIndex == 2 and NewIndex == 0:
498 // ShufMask = { 2, poison, poison, poison }
499 auto *VecTy = cast<FixedVectorType>(Vec->getType());
500 SmallVector<int, 32> ShufMask(VecTy->getNumElements(), PoisonMaskElem);
501 ShufMask[NewIndex] = OldIndex;
502 return Builder.CreateShuffleVector(Vec, ShufMask, "shift");
503}
504
505/// Given an extract element instruction with constant index operand, shuffle
506/// the source vector (shift the scalar element) to a NewIndex for extraction.
507/// Return null if the input can be constant folded, so that we are not creating
508/// unnecessary instructions.
510 unsigned NewIndex,
511 IRBuilder<> &Builder) {
512 // Shufflevectors can only be created for fixed-width vectors.
513 if (!isa<FixedVectorType>(ExtElt->getOperand(0)->getType()))
514 return nullptr;
515
516 // If the extract can be constant-folded, this code is unsimplified. Defer
517 // to other passes to handle that.
518 Value *X = ExtElt->getVectorOperand();
519 Value *C = ExtElt->getIndexOperand();
520 assert(isa<ConstantInt>(C) && "Expected a constant index operand");
521 if (isa<Constant>(X))
522 return nullptr;
523
524 Value *Shuf = createShiftShuffle(X, cast<ConstantInt>(C)->getZExtValue(),
525 NewIndex, Builder);
526 return cast<ExtractElementInst>(Builder.CreateExtractElement(Shuf, NewIndex));
527}
528
529/// Try to reduce extract element costs by converting scalar compares to vector
530/// compares followed by extract.
531/// cmp (ext0 V0, C), (ext1 V1, C)
532void VectorCombine::foldExtExtCmp(ExtractElementInst *Ext0,
534 assert(isa<CmpInst>(&I) && "Expected a compare");
535 assert(cast<ConstantInt>(Ext0->getIndexOperand())->getZExtValue() ==
536 cast<ConstantInt>(Ext1->getIndexOperand())->getZExtValue() &&
537 "Expected matching constant extract indexes");
538
539 // cmp Pred (extelt V0, C), (extelt V1, C) --> extelt (cmp Pred V0, V1), C
540 ++NumVecCmp;
541 CmpInst::Predicate Pred = cast<CmpInst>(&I)->getPredicate();
542 Value *V0 = Ext0->getVectorOperand(), *V1 = Ext1->getVectorOperand();
543 Value *VecCmp = Builder.CreateCmp(Pred, V0, V1);
544 Value *NewExt = Builder.CreateExtractElement(VecCmp, Ext0->getIndexOperand());
545 replaceValue(I, *NewExt);
546}
547
548/// Try to reduce extract element costs by converting scalar binops to vector
549/// binops followed by extract.
550/// bo (ext0 V0, C), (ext1 V1, C)
551void VectorCombine::foldExtExtBinop(ExtractElementInst *Ext0,
553 assert(isa<BinaryOperator>(&I) && "Expected a binary operator");
554 assert(cast<ConstantInt>(Ext0->getIndexOperand())->getZExtValue() ==
555 cast<ConstantInt>(Ext1->getIndexOperand())->getZExtValue() &&
556 "Expected matching constant extract indexes");
557
558 // bo (extelt V0, C), (extelt V1, C) --> extelt (bo V0, V1), C
559 ++NumVecBO;
560 Value *V0 = Ext0->getVectorOperand(), *V1 = Ext1->getVectorOperand();
561 Value *VecBO =
562 Builder.CreateBinOp(cast<BinaryOperator>(&I)->getOpcode(), V0, V1);
563
564 // All IR flags are safe to back-propagate because any potential poison
565 // created in unused vector elements is discarded by the extract.
566 if (auto *VecBOInst = dyn_cast<Instruction>(VecBO))
567 VecBOInst->copyIRFlags(&I);
568
569 Value *NewExt = Builder.CreateExtractElement(VecBO, Ext0->getIndexOperand());
570 replaceValue(I, *NewExt);
571}
572
573/// Match an instruction with extracted vector operands.
574bool VectorCombine::foldExtractExtract(Instruction &I) {
575 // It is not safe to transform things like div, urem, etc. because we may
576 // create undefined behavior when executing those on unknown vector elements.
578 return false;
579
580 Instruction *I0, *I1;
582 if (!match(&I, m_Cmp(Pred, m_Instruction(I0), m_Instruction(I1))) &&
584 return false;
585
586 Value *V0, *V1;
587 uint64_t C0, C1;
588 if (!match(I0, m_ExtractElt(m_Value(V0), m_ConstantInt(C0))) ||
589 !match(I1, m_ExtractElt(m_Value(V1), m_ConstantInt(C1))) ||
590 V0->getType() != V1->getType())
591 return false;
592
593 // If the scalar value 'I' is going to be re-inserted into a vector, then try
594 // to create an extract to that same element. The extract/insert can be
595 // reduced to a "select shuffle".
596 // TODO: If we add a larger pattern match that starts from an insert, this
597 // probably becomes unnecessary.
598 auto *Ext0 = cast<ExtractElementInst>(I0);
599 auto *Ext1 = cast<ExtractElementInst>(I1);
600 uint64_t InsertIndex = InvalidIndex;
601 if (I.hasOneUse())
602 match(I.user_back(),
603 m_InsertElt(m_Value(), m_Value(), m_ConstantInt(InsertIndex)));
604
605 ExtractElementInst *ExtractToChange;
606 if (isExtractExtractCheap(Ext0, Ext1, I, ExtractToChange, InsertIndex))
607 return false;
608
609 if (ExtractToChange) {
610 unsigned CheapExtractIdx = ExtractToChange == Ext0 ? C1 : C0;
611 ExtractElementInst *NewExtract =
612 translateExtract(ExtractToChange, CheapExtractIdx, Builder);
613 if (!NewExtract)
614 return false;
615 if (ExtractToChange == Ext0)
616 Ext0 = NewExtract;
617 else
618 Ext1 = NewExtract;
619 }
620
621 if (Pred != CmpInst::BAD_ICMP_PREDICATE)
622 foldExtExtCmp(Ext0, Ext1, I);
623 else
624 foldExtExtBinop(Ext0, Ext1, I);
625
626 Worklist.push(Ext0);
627 Worklist.push(Ext1);
628 return true;
629}
630
631/// Try to replace an extract + scalar fneg + insert with a vector fneg +
632/// shuffle.
633bool VectorCombine::foldInsExtFNeg(Instruction &I) {
634 // Match an insert (op (extract)) pattern.
635 Value *DestVec;
637 Instruction *FNeg;
638 if (!match(&I, m_InsertElt(m_Value(DestVec), m_OneUse(m_Instruction(FNeg)),
640 return false;
641
642 // Note: This handles the canonical fneg instruction and "fsub -0.0, X".
643 Value *SrcVec;
644 Instruction *Extract;
645 if (!match(FNeg, m_FNeg(m_CombineAnd(
646 m_Instruction(Extract),
648 return false;
649
650 // TODO: We could handle this with a length-changing shuffle.
651 auto *VecTy = cast<FixedVectorType>(I.getType());
652 if (SrcVec->getType() != VecTy)
653 return false;
654
655 // Ignore bogus insert/extract index.
656 unsigned NumElts = VecTy->getNumElements();
657 if (Index >= NumElts)
658 return false;
659
660 // We are inserting the negated element into the same lane that we extracted
661 // from. This is equivalent to a select-shuffle that chooses all but the
662 // negated element from the destination vector.
663 SmallVector<int> Mask(NumElts);
664 std::iota(Mask.begin(), Mask.end(), 0);
665 Mask[Index] = Index + NumElts;
666
667 Type *ScalarTy = VecTy->getScalarType();
669 InstructionCost OldCost =
670 TTI.getArithmeticInstrCost(Instruction::FNeg, ScalarTy) +
672
673 // If the extract has one use, it will be eliminated, so count it in the
674 // original cost. If it has more than one use, ignore the cost because it will
675 // be the same before/after.
676 if (Extract->hasOneUse())
677 OldCost += TTI.getVectorInstrCost(*Extract, VecTy, CostKind, Index);
678
679 InstructionCost NewCost =
680 TTI.getArithmeticInstrCost(Instruction::FNeg, VecTy) +
682
683 if (NewCost > OldCost)
684 return false;
685
686 // insertelt DestVec, (fneg (extractelt SrcVec, Index)), Index -->
687 // shuffle DestVec, (fneg SrcVec), Mask
688 Value *VecFNeg = Builder.CreateFNegFMF(SrcVec, FNeg);
689 Value *Shuf = Builder.CreateShuffleVector(DestVec, VecFNeg, Mask);
690 replaceValue(I, *Shuf);
691 return true;
692}
693
694/// If this is a bitcast of a shuffle, try to bitcast the source vector to the
695/// destination type followed by shuffle. This can enable further transforms by
696/// moving bitcasts or shuffles together.
697bool VectorCombine::foldBitcastShuffle(Instruction &I) {
698 Value *V0, *V1;
700 if (!match(&I, m_BitCast(m_OneUse(
701 m_Shuffle(m_Value(V0), m_Value(V1), m_Mask(Mask))))))
702 return false;
703
704 // 1) Do not fold bitcast shuffle for scalable type. First, shuffle cost for
705 // scalable type is unknown; Second, we cannot reason if the narrowed shuffle
706 // mask for scalable type is a splat or not.
707 // 2) Disallow non-vector casts.
708 // TODO: We could allow any shuffle.
709 auto *DestTy = dyn_cast<FixedVectorType>(I.getType());
710 auto *SrcTy = dyn_cast<FixedVectorType>(V0->getType());
711 if (!DestTy || !SrcTy)
712 return false;
713
714 unsigned DestEltSize = DestTy->getScalarSizeInBits();
715 unsigned SrcEltSize = SrcTy->getScalarSizeInBits();
716 if (SrcTy->getPrimitiveSizeInBits() % DestEltSize != 0)
717 return false;
718
719 bool IsUnary = isa<UndefValue>(V1);
720
721 // For binary shuffles, only fold bitcast(shuffle(X,Y))
722 // if it won't increase the number of bitcasts.
723 if (!IsUnary) {
724 auto *BCTy0 = dyn_cast<FixedVectorType>(peekThroughBitcasts(V0)->getType());
725 auto *BCTy1 = dyn_cast<FixedVectorType>(peekThroughBitcasts(V1)->getType());
726 if (!(BCTy0 && BCTy0->getElementType() == DestTy->getElementType()) &&
727 !(BCTy1 && BCTy1->getElementType() == DestTy->getElementType()))
728 return false;
729 }
730
731 SmallVector<int, 16> NewMask;
732 if (DestEltSize <= SrcEltSize) {
733 // The bitcast is from wide to narrow/equal elements. The shuffle mask can
734 // always be expanded to the equivalent form choosing narrower elements.
735 assert(SrcEltSize % DestEltSize == 0 && "Unexpected shuffle mask");
736 unsigned ScaleFactor = SrcEltSize / DestEltSize;
737 narrowShuffleMaskElts(ScaleFactor, Mask, NewMask);
738 } else {
739 // The bitcast is from narrow elements to wide elements. The shuffle mask
740 // must choose consecutive elements to allow casting first.
741 assert(DestEltSize % SrcEltSize == 0 && "Unexpected shuffle mask");
742 unsigned ScaleFactor = DestEltSize / SrcEltSize;
743 if (!widenShuffleMaskElts(ScaleFactor, Mask, NewMask))
744 return false;
745 }
746
747 // Bitcast the shuffle src - keep its original width but using the destination
748 // scalar type.
749 unsigned NumSrcElts = SrcTy->getPrimitiveSizeInBits() / DestEltSize;
750 auto *NewShuffleTy =
751 FixedVectorType::get(DestTy->getScalarType(), NumSrcElts);
752 auto *OldShuffleTy =
753 FixedVectorType::get(SrcTy->getScalarType(), Mask.size());
754 unsigned NumOps = IsUnary ? 1 : 2;
755
756 // The new shuffle must not cost more than the old shuffle.
762
763 InstructionCost DestCost =
764 TTI.getShuffleCost(SK, NewShuffleTy, NewMask, CK) +
765 (NumOps * TTI.getCastInstrCost(Instruction::BitCast, NewShuffleTy, SrcTy,
766 TargetTransformInfo::CastContextHint::None,
767 CK));
768 InstructionCost SrcCost =
769 TTI.getShuffleCost(SK, SrcTy, Mask, CK) +
770 TTI.getCastInstrCost(Instruction::BitCast, DestTy, OldShuffleTy,
771 TargetTransformInfo::CastContextHint::None, CK);
772 if (DestCost > SrcCost || !DestCost.isValid())
773 return false;
774
775 // bitcast (shuf V0, V1, MaskC) --> shuf (bitcast V0), (bitcast V1), MaskC'
776 ++NumShufOfBitcast;
777 Value *CastV0 = Builder.CreateBitCast(peekThroughBitcasts(V0), NewShuffleTy);
778 Value *CastV1 = Builder.CreateBitCast(peekThroughBitcasts(V1), NewShuffleTy);
779 Value *Shuf = Builder.CreateShuffleVector(CastV0, CastV1, NewMask);
780 replaceValue(I, *Shuf);
781 return true;
782}
783
784/// VP Intrinsics whose vector operands are both splat values may be simplified
785/// into the scalar version of the operation and the result splatted. This
786/// can lead to scalarization down the line.
787bool VectorCombine::scalarizeVPIntrinsic(Instruction &I) {
788 if (!isa<VPIntrinsic>(I))
789 return false;
790 VPIntrinsic &VPI = cast<VPIntrinsic>(I);
791 Value *Op0 = VPI.getArgOperand(0);
792 Value *Op1 = VPI.getArgOperand(1);
793
794 if (!isSplatValue(Op0) || !isSplatValue(Op1))
795 return false;
796
797 // Check getSplatValue early in this function, to avoid doing unnecessary
798 // work.
799 Value *ScalarOp0 = getSplatValue(Op0);
800 Value *ScalarOp1 = getSplatValue(Op1);
801 if (!ScalarOp0 || !ScalarOp1)
802 return false;
803
804 // For the binary VP intrinsics supported here, the result on disabled lanes
805 // is a poison value. For now, only do this simplification if all lanes
806 // are active.
807 // TODO: Relax the condition that all lanes are active by using insertelement
808 // on inactive lanes.
809 auto IsAllTrueMask = [](Value *MaskVal) {
810 if (Value *SplattedVal = getSplatValue(MaskVal))
811 if (auto *ConstValue = dyn_cast<Constant>(SplattedVal))
812 return ConstValue->isAllOnesValue();
813 return false;
814 };
815 if (!IsAllTrueMask(VPI.getArgOperand(2)))
816 return false;
817
818 // Check to make sure we support scalarization of the intrinsic
819 Intrinsic::ID IntrID = VPI.getIntrinsicID();
820 if (!VPBinOpIntrinsic::isVPBinOp(IntrID))
821 return false;
822
823 // Calculate cost of splatting both operands into vectors and the vector
824 // intrinsic
825 VectorType *VecTy = cast<VectorType>(VPI.getType());
828 if (auto *FVTy = dyn_cast<FixedVectorType>(VecTy))
829 Mask.resize(FVTy->getNumElements(), 0);
830 InstructionCost SplatCost =
831 TTI.getVectorInstrCost(Instruction::InsertElement, VecTy, CostKind, 0) +
833
834 // Calculate the cost of the VP Intrinsic
836 for (Value *V : VPI.args())
837 Args.push_back(V->getType());
838 IntrinsicCostAttributes Attrs(IntrID, VecTy, Args);
839 InstructionCost VectorOpCost = TTI.getIntrinsicInstrCost(Attrs, CostKind);
840 InstructionCost OldCost = 2 * SplatCost + VectorOpCost;
841
842 // Determine scalar opcode
843 std::optional<unsigned> FunctionalOpcode =
845 std::optional<Intrinsic::ID> ScalarIntrID = std::nullopt;
846 if (!FunctionalOpcode) {
847 ScalarIntrID = VPI.getFunctionalIntrinsicID();
848 if (!ScalarIntrID)
849 return false;
850 }
851
852 // Calculate cost of scalarizing
853 InstructionCost ScalarOpCost = 0;
854 if (ScalarIntrID) {
855 IntrinsicCostAttributes Attrs(*ScalarIntrID, VecTy->getScalarType(), Args);
856 ScalarOpCost = TTI.getIntrinsicInstrCost(Attrs, CostKind);
857 } else {
858 ScalarOpCost =
859 TTI.getArithmeticInstrCost(*FunctionalOpcode, VecTy->getScalarType());
860 }
861
862 // The existing splats may be kept around if other instructions use them.
863 InstructionCost CostToKeepSplats =
864 (SplatCost * !Op0->hasOneUse()) + (SplatCost * !Op1->hasOneUse());
865 InstructionCost NewCost = ScalarOpCost + SplatCost + CostToKeepSplats;
866
867 LLVM_DEBUG(dbgs() << "Found a VP Intrinsic to scalarize: " << VPI
868 << "\n");
869 LLVM_DEBUG(dbgs() << "Cost of Intrinsic: " << OldCost
870 << ", Cost of scalarizing:" << NewCost << "\n");
871
872 // We want to scalarize unless the vector variant actually has lower cost.
873 if (OldCost < NewCost || !NewCost.isValid())
874 return false;
875
876 // Scalarize the intrinsic
877 ElementCount EC = cast<VectorType>(Op0->getType())->getElementCount();
878 Value *EVL = VPI.getArgOperand(3);
879
880 // If the VP op might introduce UB or poison, we can scalarize it provided
881 // that we know the EVL > 0: If the EVL is zero, then the original VP op
882 // becomes a no-op and thus won't be UB, so make sure we don't introduce UB by
883 // scalarizing it.
884 bool SafeToSpeculate;
885 if (ScalarIntrID)
886 SafeToSpeculate = Intrinsic::getAttributes(I.getContext(), *ScalarIntrID)
887 .hasFnAttr(Attribute::AttrKind::Speculatable);
888 else
890 *FunctionalOpcode, &VPI, nullptr, &AC, &DT);
891 if (!SafeToSpeculate &&
892 !isKnownNonZero(EVL, SimplifyQuery(*DL, &DT, &AC, &VPI)))
893 return false;
894
895 Value *ScalarVal =
896 ScalarIntrID
897 ? Builder.CreateIntrinsic(VecTy->getScalarType(), *ScalarIntrID,
898 {ScalarOp0, ScalarOp1})
899 : Builder.CreateBinOp((Instruction::BinaryOps)(*FunctionalOpcode),
900 ScalarOp0, ScalarOp1);
901
902 replaceValue(VPI, *Builder.CreateVectorSplat(EC, ScalarVal));
903 return true;
904}
905
906/// Match a vector binop or compare instruction with at least one inserted
907/// scalar operand and convert to scalar binop/cmp followed by insertelement.
908bool VectorCombine::scalarizeBinopOrCmp(Instruction &I) {
910 Value *Ins0, *Ins1;
911 if (!match(&I, m_BinOp(m_Value(Ins0), m_Value(Ins1))) &&
912 !match(&I, m_Cmp(Pred, m_Value(Ins0), m_Value(Ins1))))
913 return false;
914
915 // Do not convert the vector condition of a vector select into a scalar
916 // condition. That may cause problems for codegen because of differences in
917 // boolean formats and register-file transfers.
918 // TODO: Can we account for that in the cost model?
919 bool IsCmp = Pred != CmpInst::Predicate::BAD_ICMP_PREDICATE;
920 if (IsCmp)
921 for (User *U : I.users())
922 if (match(U, m_Select(m_Specific(&I), m_Value(), m_Value())))
923 return false;
924
925 // Match against one or both scalar values being inserted into constant
926 // vectors:
927 // vec_op VecC0, (inselt VecC1, V1, Index)
928 // vec_op (inselt VecC0, V0, Index), VecC1
929 // vec_op (inselt VecC0, V0, Index), (inselt VecC1, V1, Index)
930 // TODO: Deal with mismatched index constants and variable indexes?
931 Constant *VecC0 = nullptr, *VecC1 = nullptr;
932 Value *V0 = nullptr, *V1 = nullptr;
933 uint64_t Index0 = 0, Index1 = 0;
934 if (!match(Ins0, m_InsertElt(m_Constant(VecC0), m_Value(V0),
935 m_ConstantInt(Index0))) &&
936 !match(Ins0, m_Constant(VecC0)))
937 return false;
938 if (!match(Ins1, m_InsertElt(m_Constant(VecC1), m_Value(V1),
939 m_ConstantInt(Index1))) &&
940 !match(Ins1, m_Constant(VecC1)))
941 return false;
942
943 bool IsConst0 = !V0;
944 bool IsConst1 = !V1;
945 if (IsConst0 && IsConst1)
946 return false;
947 if (!IsConst0 && !IsConst1 && Index0 != Index1)
948 return false;
949
950 // Bail for single insertion if it is a load.
951 // TODO: Handle this once getVectorInstrCost can cost for load/stores.
952 auto *I0 = dyn_cast_or_null<Instruction>(V0);
953 auto *I1 = dyn_cast_or_null<Instruction>(V1);
954 if ((IsConst0 && I1 && I1->mayReadFromMemory()) ||
955 (IsConst1 && I0 && I0->mayReadFromMemory()))
956 return false;
957
958 uint64_t Index = IsConst0 ? Index1 : Index0;
959 Type *ScalarTy = IsConst0 ? V1->getType() : V0->getType();
960 Type *VecTy = I.getType();
961 assert(VecTy->isVectorTy() &&
962 (IsConst0 || IsConst1 || V0->getType() == V1->getType()) &&
963 (ScalarTy->isIntegerTy() || ScalarTy->isFloatingPointTy() ||
964 ScalarTy->isPointerTy()) &&
965 "Unexpected types for insert element into binop or cmp");
966
967 unsigned Opcode = I.getOpcode();
968 InstructionCost ScalarOpCost, VectorOpCost;
969 if (IsCmp) {
970 CmpInst::Predicate Pred = cast<CmpInst>(I).getPredicate();
971 ScalarOpCost = TTI.getCmpSelInstrCost(
972 Opcode, ScalarTy, CmpInst::makeCmpResultType(ScalarTy), Pred);
973 VectorOpCost = TTI.getCmpSelInstrCost(
974 Opcode, VecTy, CmpInst::makeCmpResultType(VecTy), Pred);
975 } else {
976 ScalarOpCost = TTI.getArithmeticInstrCost(Opcode, ScalarTy);
977 VectorOpCost = TTI.getArithmeticInstrCost(Opcode, VecTy);
978 }
979
980 // Get cost estimate for the insert element. This cost will factor into
981 // both sequences.
984 Instruction::InsertElement, VecTy, CostKind, Index);
985 InstructionCost OldCost =
986 (IsConst0 ? 0 : InsertCost) + (IsConst1 ? 0 : InsertCost) + VectorOpCost;
987 InstructionCost NewCost = ScalarOpCost + InsertCost +
988 (IsConst0 ? 0 : !Ins0->hasOneUse() * InsertCost) +
989 (IsConst1 ? 0 : !Ins1->hasOneUse() * InsertCost);
990
991 // We want to scalarize unless the vector variant actually has lower cost.
992 if (OldCost < NewCost || !NewCost.isValid())
993 return false;
994
995 // vec_op (inselt VecC0, V0, Index), (inselt VecC1, V1, Index) -->
996 // inselt NewVecC, (scalar_op V0, V1), Index
997 if (IsCmp)
998 ++NumScalarCmp;
999 else
1000 ++NumScalarBO;
1001
1002 // For constant cases, extract the scalar element, this should constant fold.
1003 if (IsConst0)
1004 V0 = ConstantExpr::getExtractElement(VecC0, Builder.getInt64(Index));
1005 if (IsConst1)
1006 V1 = ConstantExpr::getExtractElement(VecC1, Builder.getInt64(Index));
1007
1008 Value *Scalar =
1009 IsCmp ? Builder.CreateCmp(Pred, V0, V1)
1010 : Builder.CreateBinOp((Instruction::BinaryOps)Opcode, V0, V1);
1011
1012 Scalar->setName(I.getName() + ".scalar");
1013
1014 // All IR flags are safe to back-propagate. There is no potential for extra
1015 // poison to be created by the scalar instruction.
1016 if (auto *ScalarInst = dyn_cast<Instruction>(Scalar))
1017 ScalarInst->copyIRFlags(&I);
1018
1019 // Fold the vector constants in the original vectors into a new base vector.
1020 Value *NewVecC =
1021 IsCmp ? Builder.CreateCmp(Pred, VecC0, VecC1)
1022 : Builder.CreateBinOp((Instruction::BinaryOps)Opcode, VecC0, VecC1);
1023 Value *Insert = Builder.CreateInsertElement(NewVecC, Scalar, Index);
1024 replaceValue(I, *Insert);
1025 return true;
1026}
1027
1028/// Try to combine a scalar binop + 2 scalar compares of extracted elements of
1029/// a vector into vector operations followed by extract. Note: The SLP pass
1030/// may miss this pattern because of implementation problems.
1031bool VectorCombine::foldExtractedCmps(Instruction &I) {
1032 // We are looking for a scalar binop of booleans.
1033 // binop i1 (cmp Pred I0, C0), (cmp Pred I1, C1)
1034 if (!I.isBinaryOp() || !I.getType()->isIntegerTy(1))
1035 return false;
1036
1037 // The compare predicates should match, and each compare should have a
1038 // constant operand.
1039 // TODO: Relax the one-use constraints.
1040 Value *B0 = I.getOperand(0), *B1 = I.getOperand(1);
1041 Instruction *I0, *I1;
1042 Constant *C0, *C1;
1043 CmpInst::Predicate P0, P1;
1044 if (!match(B0, m_OneUse(m_Cmp(P0, m_Instruction(I0), m_Constant(C0)))) ||
1045 !match(B1, m_OneUse(m_Cmp(P1, m_Instruction(I1), m_Constant(C1)))) ||
1046 P0 != P1)
1047 return false;
1048
1049 // The compare operands must be extracts of the same vector with constant
1050 // extract indexes.
1051 // TODO: Relax the one-use constraints.
1052 Value *X;
1053 uint64_t Index0, Index1;
1054 if (!match(I0, m_OneUse(m_ExtractElt(m_Value(X), m_ConstantInt(Index0)))) ||
1056 return false;
1057
1058 auto *Ext0 = cast<ExtractElementInst>(I0);
1059 auto *Ext1 = cast<ExtractElementInst>(I1);
1060 ExtractElementInst *ConvertToShuf = getShuffleExtract(Ext0, Ext1);
1061 if (!ConvertToShuf)
1062 return false;
1063
1064 // The original scalar pattern is:
1065 // binop i1 (cmp Pred (ext X, Index0), C0), (cmp Pred (ext X, Index1), C1)
1066 CmpInst::Predicate Pred = P0;
1067 unsigned CmpOpcode = CmpInst::isFPPredicate(Pred) ? Instruction::FCmp
1068 : Instruction::ICmp;
1069 auto *VecTy = dyn_cast<FixedVectorType>(X->getType());
1070 if (!VecTy)
1071 return false;
1072
1074 InstructionCost OldCost =
1075 TTI.getVectorInstrCost(*Ext0, VecTy, CostKind, Index0);
1076 OldCost += TTI.getVectorInstrCost(*Ext1, VecTy, CostKind, Index1);
1077 OldCost +=
1078 TTI.getCmpSelInstrCost(CmpOpcode, I0->getType(),
1079 CmpInst::makeCmpResultType(I0->getType()), Pred) *
1080 2;
1081 OldCost += TTI.getArithmeticInstrCost(I.getOpcode(), I.getType());
1082
1083 // The proposed vector pattern is:
1084 // vcmp = cmp Pred X, VecC
1085 // ext (binop vNi1 vcmp, (shuffle vcmp, Index1)), Index0
1086 int CheapIndex = ConvertToShuf == Ext0 ? Index1 : Index0;
1087 int ExpensiveIndex = ConvertToShuf == Ext0 ? Index0 : Index1;
1088 auto *CmpTy = cast<FixedVectorType>(CmpInst::makeCmpResultType(X->getType()));
1090 CmpOpcode, X->getType(), CmpInst::makeCmpResultType(X->getType()), Pred);
1091 SmallVector<int, 32> ShufMask(VecTy->getNumElements(), PoisonMaskElem);
1092 ShufMask[CheapIndex] = ExpensiveIndex;
1094 ShufMask);
1095 NewCost += TTI.getArithmeticInstrCost(I.getOpcode(), CmpTy);
1096 NewCost += TTI.getVectorInstrCost(*Ext0, CmpTy, CostKind, CheapIndex);
1097
1098 // Aggressively form vector ops if the cost is equal because the transform
1099 // may enable further optimization.
1100 // Codegen can reverse this transform (scalarize) if it was not profitable.
1101 if (OldCost < NewCost || !NewCost.isValid())
1102 return false;
1103
1104 // Create a vector constant from the 2 scalar constants.
1105 SmallVector<Constant *, 32> CmpC(VecTy->getNumElements(),
1106 PoisonValue::get(VecTy->getElementType()));
1107 CmpC[Index0] = C0;
1108 CmpC[Index1] = C1;
1109 Value *VCmp = Builder.CreateCmp(Pred, X, ConstantVector::get(CmpC));
1110
1111 Value *Shuf = createShiftShuffle(VCmp, ExpensiveIndex, CheapIndex, Builder);
1112 Value *VecLogic = Builder.CreateBinOp(cast<BinaryOperator>(I).getOpcode(),
1113 VCmp, Shuf);
1114 Value *NewExt = Builder.CreateExtractElement(VecLogic, CheapIndex);
1115 replaceValue(I, *NewExt);
1116 ++NumVecCmpBO;
1117 return true;
1118}
1119
1120// Check if memory loc modified between two instrs in the same BB
1123 const MemoryLocation &Loc, AAResults &AA) {
1124 unsigned NumScanned = 0;
1125 return std::any_of(Begin, End, [&](const Instruction &Instr) {
1126 return isModSet(AA.getModRefInfo(&Instr, Loc)) ||
1127 ++NumScanned > MaxInstrsToScan;
1128 });
1129}
1130
1131namespace {
1132/// Helper class to indicate whether a vector index can be safely scalarized and
1133/// if a freeze needs to be inserted.
1134class ScalarizationResult {
1135 enum class StatusTy { Unsafe, Safe, SafeWithFreeze };
1136
1137 StatusTy Status;
1138 Value *ToFreeze;
1139
1140 ScalarizationResult(StatusTy Status, Value *ToFreeze = nullptr)
1141 : Status(Status), ToFreeze(ToFreeze) {}
1142
1143public:
1144 ScalarizationResult(const ScalarizationResult &Other) = default;
1145 ~ScalarizationResult() {
1146 assert(!ToFreeze && "freeze() not called with ToFreeze being set");
1147 }
1148
1149 static ScalarizationResult unsafe() { return {StatusTy::Unsafe}; }
1150 static ScalarizationResult safe() { return {StatusTy::Safe}; }
1151 static ScalarizationResult safeWithFreeze(Value *ToFreeze) {
1152 return {StatusTy::SafeWithFreeze, ToFreeze};
1153 }
1154
1155 /// Returns true if the index can be scalarize without requiring a freeze.
1156 bool isSafe() const { return Status == StatusTy::Safe; }
1157 /// Returns true if the index cannot be scalarized.
1158 bool isUnsafe() const { return Status == StatusTy::Unsafe; }
1159 /// Returns true if the index can be scalarize, but requires inserting a
1160 /// freeze.
1161 bool isSafeWithFreeze() const { return Status == StatusTy::SafeWithFreeze; }
1162
1163 /// Reset the state of Unsafe and clear ToFreze if set.
1164 void discard() {
1165 ToFreeze = nullptr;
1166 Status = StatusTy::Unsafe;
1167 }
1168
1169 /// Freeze the ToFreeze and update the use in \p User to use it.
1170 void freeze(IRBuilder<> &Builder, Instruction &UserI) {
1171 assert(isSafeWithFreeze() &&
1172 "should only be used when freezing is required");
1173 assert(is_contained(ToFreeze->users(), &UserI) &&
1174 "UserI must be a user of ToFreeze");
1175 IRBuilder<>::InsertPointGuard Guard(Builder);
1176 Builder.SetInsertPoint(cast<Instruction>(&UserI));
1177 Value *Frozen =
1178 Builder.CreateFreeze(ToFreeze, ToFreeze->getName() + ".frozen");
1179 for (Use &U : make_early_inc_range((UserI.operands())))
1180 if (U.get() == ToFreeze)
1181 U.set(Frozen);
1182
1183 ToFreeze = nullptr;
1184 }
1185};
1186} // namespace
1187
1188/// Check if it is legal to scalarize a memory access to \p VecTy at index \p
1189/// Idx. \p Idx must access a valid vector element.
1190static ScalarizationResult canScalarizeAccess(VectorType *VecTy, Value *Idx,
1191 Instruction *CtxI,
1192 AssumptionCache &AC,
1193 const DominatorTree &DT) {
1194 // We do checks for both fixed vector types and scalable vector types.
1195 // This is the number of elements of fixed vector types,
1196 // or the minimum number of elements of scalable vector types.
1197 uint64_t NumElements = VecTy->getElementCount().getKnownMinValue();
1198
1199 if (auto *C = dyn_cast<ConstantInt>(Idx)) {
1200 if (C->getValue().ult(NumElements))
1201 return ScalarizationResult::safe();
1202 return ScalarizationResult::unsafe();
1203 }
1204
1205 unsigned IntWidth = Idx->getType()->getScalarSizeInBits();
1206 APInt Zero(IntWidth, 0);
1207 APInt MaxElts(IntWidth, NumElements);
1208 ConstantRange ValidIndices(Zero, MaxElts);
1209 ConstantRange IdxRange(IntWidth, true);
1210
1211 if (isGuaranteedNotToBePoison(Idx, &AC)) {
1212 if (ValidIndices.contains(computeConstantRange(Idx, /* ForSigned */ false,
1213 true, &AC, CtxI, &DT)))
1214 return ScalarizationResult::safe();
1215 return ScalarizationResult::unsafe();
1216 }
1217
1218 // If the index may be poison, check if we can insert a freeze before the
1219 // range of the index is restricted.
1220 Value *IdxBase;
1221 ConstantInt *CI;
1222 if (match(Idx, m_And(m_Value(IdxBase), m_ConstantInt(CI)))) {
1223 IdxRange = IdxRange.binaryAnd(CI->getValue());
1224 } else if (match(Idx, m_URem(m_Value(IdxBase), m_ConstantInt(CI)))) {
1225 IdxRange = IdxRange.urem(CI->getValue());
1226 }
1227
1228 if (ValidIndices.contains(IdxRange))
1229 return ScalarizationResult::safeWithFreeze(IdxBase);
1230 return ScalarizationResult::unsafe();
1231}
1232
1233/// The memory operation on a vector of \p ScalarType had alignment of
1234/// \p VectorAlignment. Compute the maximal, but conservatively correct,
1235/// alignment that will be valid for the memory operation on a single scalar
1236/// element of the same type with index \p Idx.
1238 Type *ScalarType, Value *Idx,
1239 const DataLayout &DL) {
1240 if (auto *C = dyn_cast<ConstantInt>(Idx))
1241 return commonAlignment(VectorAlignment,
1242 C->getZExtValue() * DL.getTypeStoreSize(ScalarType));
1243 return commonAlignment(VectorAlignment, DL.getTypeStoreSize(ScalarType));
1244}
1245
1246// Combine patterns like:
1247// %0 = load <4 x i32>, <4 x i32>* %a
1248// %1 = insertelement <4 x i32> %0, i32 %b, i32 1
1249// store <4 x i32> %1, <4 x i32>* %a
1250// to:
1251// %0 = bitcast <4 x i32>* %a to i32*
1252// %1 = getelementptr inbounds i32, i32* %0, i64 0, i64 1
1253// store i32 %b, i32* %1
1254bool VectorCombine::foldSingleElementStore(Instruction &I) {
1255 auto *SI = cast<StoreInst>(&I);
1256 if (!SI->isSimple() || !isa<VectorType>(SI->getValueOperand()->getType()))
1257 return false;
1258
1259 // TODO: Combine more complicated patterns (multiple insert) by referencing
1260 // TargetTransformInfo.
1262 Value *NewElement;
1263 Value *Idx;
1264 if (!match(SI->getValueOperand(),
1265 m_InsertElt(m_Instruction(Source), m_Value(NewElement),
1266 m_Value(Idx))))
1267 return false;
1268
1269 if (auto *Load = dyn_cast<LoadInst>(Source)) {
1270 auto VecTy = cast<VectorType>(SI->getValueOperand()->getType());
1271 Value *SrcAddr = Load->getPointerOperand()->stripPointerCasts();
1272 // Don't optimize for atomic/volatile load or store. Ensure memory is not
1273 // modified between, vector type matches store size, and index is inbounds.
1274 if (!Load->isSimple() || Load->getParent() != SI->getParent() ||
1275 !DL->typeSizeEqualsStoreSize(Load->getType()->getScalarType()) ||
1276 SrcAddr != SI->getPointerOperand()->stripPointerCasts())
1277 return false;
1278
1279 auto ScalarizableIdx = canScalarizeAccess(VecTy, Idx, Load, AC, DT);
1280 if (ScalarizableIdx.isUnsafe() ||
1281 isMemModifiedBetween(Load->getIterator(), SI->getIterator(),
1282 MemoryLocation::get(SI), AA))
1283 return false;
1284
1285 if (ScalarizableIdx.isSafeWithFreeze())
1286 ScalarizableIdx.freeze(Builder, *cast<Instruction>(Idx));
1287 Value *GEP = Builder.CreateInBoundsGEP(
1288 SI->getValueOperand()->getType(), SI->getPointerOperand(),
1289 {ConstantInt::get(Idx->getType(), 0), Idx});
1290 StoreInst *NSI = Builder.CreateStore(NewElement, GEP);
1291 NSI->copyMetadata(*SI);
1292 Align ScalarOpAlignment = computeAlignmentAfterScalarization(
1293 std::max(SI->getAlign(), Load->getAlign()), NewElement->getType(), Idx,
1294 *DL);
1295 NSI->setAlignment(ScalarOpAlignment);
1296 replaceValue(I, *NSI);
1298 return true;
1299 }
1300
1301 return false;
1302}
1303
1304/// Try to scalarize vector loads feeding extractelement instructions.
1305bool VectorCombine::scalarizeLoadExtract(Instruction &I) {
1306 Value *Ptr;
1307 if (!match(&I, m_Load(m_Value(Ptr))))
1308 return false;
1309
1310 auto *VecTy = cast<VectorType>(I.getType());
1311 auto *LI = cast<LoadInst>(&I);
1312 if (LI->isVolatile() || !DL->typeSizeEqualsStoreSize(VecTy->getScalarType()))
1313 return false;
1314
1315 InstructionCost OriginalCost =
1316 TTI.getMemoryOpCost(Instruction::Load, VecTy, LI->getAlign(),
1317 LI->getPointerAddressSpace());
1318 InstructionCost ScalarizedCost = 0;
1319
1320 Instruction *LastCheckedInst = LI;
1321 unsigned NumInstChecked = 0;
1323 auto FailureGuard = make_scope_exit([&]() {
1324 // If the transform is aborted, discard the ScalarizationResults.
1325 for (auto &Pair : NeedFreeze)
1326 Pair.second.discard();
1327 });
1328
1329 // Check if all users of the load are extracts with no memory modifications
1330 // between the load and the extract. Compute the cost of both the original
1331 // code and the scalarized version.
1332 for (User *U : LI->users()) {
1333 auto *UI = dyn_cast<ExtractElementInst>(U);
1334 if (!UI || UI->getParent() != LI->getParent())
1335 return false;
1336
1337 // Check if any instruction between the load and the extract may modify
1338 // memory.
1339 if (LastCheckedInst->comesBefore(UI)) {
1340 for (Instruction &I :
1341 make_range(std::next(LI->getIterator()), UI->getIterator())) {
1342 // Bail out if we reached the check limit or the instruction may write
1343 // to memory.
1344 if (NumInstChecked == MaxInstrsToScan || I.mayWriteToMemory())
1345 return false;
1346 NumInstChecked++;
1347 }
1348 LastCheckedInst = UI;
1349 }
1350
1351 auto ScalarIdx = canScalarizeAccess(VecTy, UI->getOperand(1), &I, AC, DT);
1352 if (ScalarIdx.isUnsafe())
1353 return false;
1354 if (ScalarIdx.isSafeWithFreeze()) {
1355 NeedFreeze.try_emplace(UI, ScalarIdx);
1356 ScalarIdx.discard();
1357 }
1358
1359 auto *Index = dyn_cast<ConstantInt>(UI->getOperand(1));
1361 OriginalCost +=
1362 TTI.getVectorInstrCost(Instruction::ExtractElement, VecTy, CostKind,
1363 Index ? Index->getZExtValue() : -1);
1364 ScalarizedCost +=
1365 TTI.getMemoryOpCost(Instruction::Load, VecTy->getElementType(),
1366 Align(1), LI->getPointerAddressSpace());
1367 ScalarizedCost += TTI.getAddressComputationCost(VecTy->getElementType());
1368 }
1369
1370 if (ScalarizedCost >= OriginalCost)
1371 return false;
1372
1373 // Replace extracts with narrow scalar loads.
1374 for (User *U : LI->users()) {
1375 auto *EI = cast<ExtractElementInst>(U);
1376 Value *Idx = EI->getOperand(1);
1377
1378 // Insert 'freeze' for poison indexes.
1379 auto It = NeedFreeze.find(EI);
1380 if (It != NeedFreeze.end())
1381 It->second.freeze(Builder, *cast<Instruction>(Idx));
1382
1383 Builder.SetInsertPoint(EI);
1384 Value *GEP =
1385 Builder.CreateInBoundsGEP(VecTy, Ptr, {Builder.getInt32(0), Idx});
1386 auto *NewLoad = cast<LoadInst>(Builder.CreateLoad(
1387 VecTy->getElementType(), GEP, EI->getName() + ".scalar"));
1388
1389 Align ScalarOpAlignment = computeAlignmentAfterScalarization(
1390 LI->getAlign(), VecTy->getElementType(), Idx, *DL);
1391 NewLoad->setAlignment(ScalarOpAlignment);
1392
1393 replaceValue(*EI, *NewLoad);
1394 }
1395
1396 FailureGuard.release();
1397 return true;
1398}
1399
1400/// Try to convert "shuffle (binop), (binop)" into "binop (shuffle), (shuffle)".
1401bool VectorCombine::foldShuffleOfBinops(Instruction &I) {
1402 BinaryOperator *B0, *B1;
1403 ArrayRef<int> OldMask;
1404 if (!match(&I, m_Shuffle(m_OneUse(m_BinOp(B0)), m_OneUse(m_BinOp(B1)),
1405 m_Mask(OldMask))))
1406 return false;
1407
1408 // Don't introduce poison into div/rem.
1409 if (llvm::is_contained(OldMask, PoisonMaskElem) && B0->isIntDivRem())
1410 return false;
1411
1412 // TODO: Add support for addlike etc.
1413 Instruction::BinaryOps Opcode = B0->getOpcode();
1414 if (Opcode != B1->getOpcode())
1415 return false;
1416
1417 auto *ShuffleDstTy = dyn_cast<FixedVectorType>(I.getType());
1418 auto *BinOpTy = dyn_cast<FixedVectorType>(B0->getType());
1419 if (!ShuffleDstTy || !BinOpTy)
1420 return false;
1421
1422 unsigned NumSrcElts = BinOpTy->getNumElements();
1423
1424 // If we have something like "add X, Y" and "add Z, X", swap ops to match.
1425 Value *X = B0->getOperand(0), *Y = B0->getOperand(1);
1426 Value *Z = B1->getOperand(0), *W = B1->getOperand(1);
1427 if (BinaryOperator::isCommutative(Opcode) && X != Z && Y != W &&
1428 (X == W || Y == Z))
1429 std::swap(X, Y);
1430
1431 auto ConvertToUnary = [NumSrcElts](int &M) {
1432 if (M >= (int)NumSrcElts)
1433 M -= NumSrcElts;
1434 };
1435
1436 SmallVector<int> NewMask0(OldMask);
1438 if (X == Z) {
1439 llvm::for_each(NewMask0, ConvertToUnary);
1441 Z = PoisonValue::get(BinOpTy);
1442 }
1443
1444 SmallVector<int> NewMask1(OldMask);
1446 if (Y == W) {
1447 llvm::for_each(NewMask1, ConvertToUnary);
1449 W = PoisonValue::get(BinOpTy);
1450 }
1451
1452 // Try to replace a binop with a shuffle if the shuffle is not costly.
1454
1455 InstructionCost OldCost =
1456 TTI.getArithmeticInstrCost(B0->getOpcode(), BinOpTy, CostKind) +
1457 TTI.getArithmeticInstrCost(B1->getOpcode(), BinOpTy, CostKind) +
1459 OldMask, CostKind, 0, nullptr, {B0, B1}, &I);
1460
1461 InstructionCost NewCost =
1462 TTI.getShuffleCost(SK0, BinOpTy, NewMask0, CostKind, 0, nullptr, {X, Z}) +
1463 TTI.getShuffleCost(SK1, BinOpTy, NewMask1, CostKind, 0, nullptr, {Y, W}) +
1464 TTI.getArithmeticInstrCost(Opcode, ShuffleDstTy, CostKind);
1465
1466 LLVM_DEBUG(dbgs() << "Found a shuffle feeding two binops: " << I
1467 << "\n OldCost: " << OldCost << " vs NewCost: " << NewCost
1468 << "\n");
1469 if (NewCost >= OldCost)
1470 return false;
1471
1472 Value *Shuf0 = Builder.CreateShuffleVector(X, Z, NewMask0);
1473 Value *Shuf1 = Builder.CreateShuffleVector(Y, W, NewMask1);
1474 Value *NewBO = Builder.CreateBinOp(Opcode, Shuf0, Shuf1);
1475
1476 // Intersect flags from the old binops.
1477 if (auto *NewInst = dyn_cast<Instruction>(NewBO)) {
1478 NewInst->copyIRFlags(B0);
1479 NewInst->andIRFlags(B1);
1480 }
1481
1482 Worklist.pushValue(Shuf0);
1483 Worklist.pushValue(Shuf1);
1484 replaceValue(I, *NewBO);
1485 return true;
1486}
1487
1488/// Try to convert "shuffle (castop), (castop)" with a shared castop operand
1489/// into "castop (shuffle)".
1490bool VectorCombine::foldShuffleOfCastops(Instruction &I) {
1491 Value *V0, *V1;
1492 ArrayRef<int> OldMask;
1493 if (!match(&I, m_Shuffle(m_Value(V0), m_Value(V1), m_Mask(OldMask))))
1494 return false;
1495
1496 auto *C0 = dyn_cast<CastInst>(V0);
1497 auto *C1 = dyn_cast<CastInst>(V1);
1498 if (!C0 || !C1)
1499 return false;
1500
1501 Instruction::CastOps Opcode = C0->getOpcode();
1502 if (C0->getSrcTy() != C1->getSrcTy())
1503 return false;
1504
1505 // Handle shuffle(zext_nneg(x), sext(y)) -> sext(shuffle(x,y)) folds.
1506 if (Opcode != C1->getOpcode()) {
1507 if (match(C0, m_SExtLike(m_Value())) && match(C1, m_SExtLike(m_Value())))
1508 Opcode = Instruction::SExt;
1509 else
1510 return false;
1511 }
1512
1513 auto *ShuffleDstTy = dyn_cast<FixedVectorType>(I.getType());
1514 auto *CastDstTy = dyn_cast<FixedVectorType>(C0->getDestTy());
1515 auto *CastSrcTy = dyn_cast<FixedVectorType>(C0->getSrcTy());
1516 if (!ShuffleDstTy || !CastDstTy || !CastSrcTy)
1517 return false;
1518
1519 unsigned NumSrcElts = CastSrcTy->getNumElements();
1520 unsigned NumDstElts = CastDstTy->getNumElements();
1521 assert((NumDstElts == NumSrcElts || Opcode == Instruction::BitCast) &&
1522 "Only bitcasts expected to alter src/dst element counts");
1523
1524 // Check for bitcasting of unscalable vector types.
1525 // e.g. <32 x i40> -> <40 x i32>
1526 if (NumDstElts != NumSrcElts && (NumSrcElts % NumDstElts) != 0 &&
1527 (NumDstElts % NumSrcElts) != 0)
1528 return false;
1529
1530 SmallVector<int, 16> NewMask;
1531 if (NumSrcElts >= NumDstElts) {
1532 // The bitcast is from wide to narrow/equal elements. The shuffle mask can
1533 // always be expanded to the equivalent form choosing narrower elements.
1534 assert(NumSrcElts % NumDstElts == 0 && "Unexpected shuffle mask");
1535 unsigned ScaleFactor = NumSrcElts / NumDstElts;
1536 narrowShuffleMaskElts(ScaleFactor, OldMask, NewMask);
1537 } else {
1538 // The bitcast is from narrow elements to wide elements. The shuffle mask
1539 // must choose consecutive elements to allow casting first.
1540 assert(NumDstElts % NumSrcElts == 0 && "Unexpected shuffle mask");
1541 unsigned ScaleFactor = NumDstElts / NumSrcElts;
1542 if (!widenShuffleMaskElts(ScaleFactor, OldMask, NewMask))
1543 return false;
1544 }
1545
1546 auto *NewShuffleDstTy =
1547 FixedVectorType::get(CastSrcTy->getScalarType(), NewMask.size());
1548
1549 // Try to replace a castop with a shuffle if the shuffle is not costly.
1551
1552 InstructionCost CostC0 =
1553 TTI.getCastInstrCost(C0->getOpcode(), CastDstTy, CastSrcTy,
1555 InstructionCost CostC1 =
1556 TTI.getCastInstrCost(C1->getOpcode(), CastDstTy, CastSrcTy,
1558 InstructionCost OldCost = CostC0 + CostC1;
1559 OldCost +=
1561 OldMask, CostKind, 0, nullptr, std::nullopt, &I);
1562
1564 TargetTransformInfo::SK_PermuteTwoSrc, CastSrcTy, NewMask, CostKind);
1565 NewCost += TTI.getCastInstrCost(Opcode, ShuffleDstTy, NewShuffleDstTy,
1567 if (!C0->hasOneUse())
1568 NewCost += CostC0;
1569 if (!C1->hasOneUse())
1570 NewCost += CostC1;
1571
1572 LLVM_DEBUG(dbgs() << "Found a shuffle feeding two casts: " << I
1573 << "\n OldCost: " << OldCost << " vs NewCost: " << NewCost
1574 << "\n");
1575 if (NewCost > OldCost)
1576 return false;
1577
1578 Value *Shuf = Builder.CreateShuffleVector(C0->getOperand(0),
1579 C1->getOperand(0), NewMask);
1580 Value *Cast = Builder.CreateCast(Opcode, Shuf, ShuffleDstTy);
1581
1582 // Intersect flags from the old casts.
1583 if (auto *NewInst = dyn_cast<Instruction>(Cast)) {
1584 NewInst->copyIRFlags(C0);
1585 NewInst->andIRFlags(C1);
1586 }
1587
1588 Worklist.pushValue(Shuf);
1589 replaceValue(I, *Cast);
1590 return true;
1591}
1592
1593/// Try to convert "shuffle (shuffle x, undef), (shuffle y, undef)"
1594/// into "shuffle x, y".
1595bool VectorCombine::foldShuffleOfShuffles(Instruction &I) {
1596 Value *V0, *V1;
1597 UndefValue *U0, *U1;
1598 ArrayRef<int> OuterMask, InnerMask0, InnerMask1;
1600 m_Mask(InnerMask0))),
1602 m_Mask(InnerMask1))),
1603 m_Mask(OuterMask))))
1604 return false;
1605
1606 auto *ShufI0 = dyn_cast<Instruction>(I.getOperand(0));
1607 auto *ShufI1 = dyn_cast<Instruction>(I.getOperand(1));
1608 auto *ShuffleDstTy = dyn_cast<FixedVectorType>(I.getType());
1609 auto *ShuffleSrcTy = dyn_cast<FixedVectorType>(V0->getType());
1610 auto *ShuffleImmTy = dyn_cast<FixedVectorType>(I.getOperand(0)->getType());
1611 if (!ShuffleDstTy || !ShuffleSrcTy || !ShuffleImmTy ||
1612 V0->getType() != V1->getType())
1613 return false;
1614
1615 unsigned NumSrcElts = ShuffleSrcTy->getNumElements();
1616 unsigned NumImmElts = ShuffleImmTy->getNumElements();
1617
1618 // Bail if either inner masks reference a RHS undef arg.
1619 if ((!isa<PoisonValue>(U0) &&
1620 any_of(InnerMask0, [&](int M) { return M >= (int)NumSrcElts; })) ||
1621 (!isa<PoisonValue>(U1) &&
1622 any_of(InnerMask1, [&](int M) { return M >= (int)NumSrcElts; })))
1623 return false;
1624
1625 // Merge shuffles - replace index to the RHS poison arg with PoisonMaskElem,
1626 SmallVector<int, 16> NewMask(OuterMask);
1627 for (int &M : NewMask) {
1628 if (0 <= M && M < (int)NumImmElts) {
1629 M = (InnerMask0[M] >= (int)NumSrcElts) ? PoisonMaskElem : InnerMask0[M];
1630 } else if (M >= (int)NumImmElts) {
1631 if (InnerMask1[M - NumImmElts] >= (int)NumSrcElts)
1632 M = PoisonMaskElem;
1633 else
1634 M = InnerMask1[M - NumImmElts] + (V0 == V1 ? 0 : NumSrcElts);
1635 }
1636 }
1637
1638 // Have we folded to an Identity shuffle?
1639 if (ShuffleVectorInst::isIdentityMask(NewMask, NumSrcElts)) {
1640 replaceValue(I, *V0);
1641 return true;
1642 }
1643
1644 // Try to merge the shuffles if the new shuffle is not costly.
1646
1647 InstructionCost OldCost =
1649 InnerMask0, CostKind, 0, nullptr, {V0, U0}, ShufI0) +
1651 InnerMask1, CostKind, 0, nullptr, {V1, U1}, ShufI1) +
1653 OuterMask, CostKind, 0, nullptr, {ShufI0, ShufI1}, &I);
1654
1655 InstructionCost NewCost =
1657 NewMask, CostKind, 0, nullptr, {V0, V1});
1658
1659 LLVM_DEBUG(dbgs() << "Found a shuffle feeding two shuffles: " << I
1660 << "\n OldCost: " << OldCost << " vs NewCost: " << NewCost
1661 << "\n");
1662 if (NewCost > OldCost)
1663 return false;
1664
1665 // Clear unused sources to poison.
1666 if (none_of(NewMask, [&](int M) { return 0 <= M && M < (int)NumSrcElts; }))
1667 V0 = PoisonValue::get(ShuffleSrcTy);
1668 if (none_of(NewMask, [&](int M) { return (int)NumSrcElts <= M; }))
1669 V1 = PoisonValue::get(ShuffleSrcTy);
1670
1671 Value *Shuf = Builder.CreateShuffleVector(V0, V1, NewMask);
1672 replaceValue(I, *Shuf);
1673 return true;
1674}
1675
1676using InstLane = std::pair<Use *, int>;
1677
1678static InstLane lookThroughShuffles(Use *U, int Lane) {
1679 while (auto *SV = dyn_cast<ShuffleVectorInst>(U->get())) {
1680 unsigned NumElts =
1681 cast<FixedVectorType>(SV->getOperand(0)->getType())->getNumElements();
1682 int M = SV->getMaskValue(Lane);
1683 if (M < 0)
1684 return {nullptr, PoisonMaskElem};
1685 if (static_cast<unsigned>(M) < NumElts) {
1686 U = &SV->getOperandUse(0);
1687 Lane = M;
1688 } else {
1689 U = &SV->getOperandUse(1);
1690 Lane = M - NumElts;
1691 }
1692 }
1693 return InstLane{U, Lane};
1694}
1695
1699 for (InstLane IL : Item) {
1700 auto [U, Lane] = IL;
1701 InstLane OpLane =
1702 U ? lookThroughShuffles(&cast<Instruction>(U->get())->getOperandUse(Op),
1703 Lane)
1704 : InstLane{nullptr, PoisonMaskElem};
1705 NItem.emplace_back(OpLane);
1706 }
1707 return NItem;
1708}
1709
1710/// Detect concat of multiple values into a vector
1712 const TargetTransformInfo &TTI) {
1713 auto *Ty = cast<FixedVectorType>(Item.front().first->get()->getType());
1714 unsigned NumElts = Ty->getNumElements();
1715 if (Item.size() == NumElts || NumElts == 1 || Item.size() % NumElts != 0)
1716 return false;
1717
1718 // Check that the concat is free, usually meaning that the type will be split
1719 // during legalization.
1720 SmallVector<int, 16> ConcatMask(NumElts * 2);
1721 std::iota(ConcatMask.begin(), ConcatMask.end(), 0);
1722 if (TTI.getShuffleCost(TTI::SK_PermuteTwoSrc, Ty, ConcatMask,
1724 return false;
1725
1726 unsigned NumSlices = Item.size() / NumElts;
1727 // Currently we generate a tree of shuffles for the concats, which limits us
1728 // to a power2.
1729 if (!isPowerOf2_32(NumSlices))
1730 return false;
1731 for (unsigned Slice = 0; Slice < NumSlices; ++Slice) {
1732 Use *SliceV = Item[Slice * NumElts].first;
1733 if (!SliceV || SliceV->get()->getType() != Ty)
1734 return false;
1735 for (unsigned Elt = 0; Elt < NumElts; ++Elt) {
1736 auto [V, Lane] = Item[Slice * NumElts + Elt];
1737 if (Lane != static_cast<int>(Elt) || SliceV->get() != V->get())
1738 return false;
1739 }
1740 }
1741 return true;
1742}
1743
1745 const SmallPtrSet<Use *, 4> &IdentityLeafs,
1746 const SmallPtrSet<Use *, 4> &SplatLeafs,
1747 const SmallPtrSet<Use *, 4> &ConcatLeafs,
1748 IRBuilder<> &Builder) {
1749 auto [FrontU, FrontLane] = Item.front();
1750
1751 if (IdentityLeafs.contains(FrontU)) {
1752 return FrontU->get();
1753 }
1754 if (SplatLeafs.contains(FrontU)) {
1755 SmallVector<int, 16> Mask(Ty->getNumElements(), FrontLane);
1756 return Builder.CreateShuffleVector(FrontU->get(), Mask);
1757 }
1758 if (ConcatLeafs.contains(FrontU)) {
1759 unsigned NumElts =
1760 cast<FixedVectorType>(FrontU->get()->getType())->getNumElements();
1761 SmallVector<Value *> Values(Item.size() / NumElts, nullptr);
1762 for (unsigned S = 0; S < Values.size(); ++S)
1763 Values[S] = Item[S * NumElts].first->get();
1764
1765 while (Values.size() > 1) {
1766 NumElts *= 2;
1767 SmallVector<int, 16> Mask(NumElts, 0);
1768 std::iota(Mask.begin(), Mask.end(), 0);
1769 SmallVector<Value *> NewValues(Values.size() / 2, nullptr);
1770 for (unsigned S = 0; S < NewValues.size(); ++S)
1771 NewValues[S] =
1772 Builder.CreateShuffleVector(Values[S * 2], Values[S * 2 + 1], Mask);
1773 Values = NewValues;
1774 }
1775 return Values[0];
1776 }
1777
1778 auto *I = cast<Instruction>(FrontU->get());
1779 auto *II = dyn_cast<IntrinsicInst>(I);
1780 unsigned NumOps = I->getNumOperands() - (II ? 1 : 0);
1781 SmallVector<Value *> Ops(NumOps);
1782 for (unsigned Idx = 0; Idx < NumOps; Idx++) {
1783 if (II && isVectorIntrinsicWithScalarOpAtArg(II->getIntrinsicID(), Idx)) {
1784 Ops[Idx] = II->getOperand(Idx);
1785 continue;
1786 }
1787 Ops[Idx] =
1789 IdentityLeafs, SplatLeafs, ConcatLeafs, Builder);
1790 }
1791
1792 SmallVector<Value *, 8> ValueList;
1793 for (const auto &Lane : Item)
1794 if (Lane.first)
1795 ValueList.push_back(Lane.first->get());
1796
1797 Type *DstTy =
1798 FixedVectorType::get(I->getType()->getScalarType(), Ty->getNumElements());
1799 if (auto *BI = dyn_cast<BinaryOperator>(I)) {
1800 auto *Value = Builder.CreateBinOp((Instruction::BinaryOps)BI->getOpcode(),
1801 Ops[0], Ops[1]);
1802 propagateIRFlags(Value, ValueList);
1803 return Value;
1804 }
1805 if (auto *CI = dyn_cast<CmpInst>(I)) {
1806 auto *Value = Builder.CreateCmp(CI->getPredicate(), Ops[0], Ops[1]);
1807 propagateIRFlags(Value, ValueList);
1808 return Value;
1809 }
1810 if (auto *SI = dyn_cast<SelectInst>(I)) {
1811 auto *Value = Builder.CreateSelect(Ops[0], Ops[1], Ops[2], "", SI);
1812 propagateIRFlags(Value, ValueList);
1813 return Value;
1814 }
1815 if (auto *CI = dyn_cast<CastInst>(I)) {
1816 auto *Value = Builder.CreateCast((Instruction::CastOps)CI->getOpcode(),
1817 Ops[0], DstTy);
1818 propagateIRFlags(Value, ValueList);
1819 return Value;
1820 }
1821 if (II) {
1822 auto *Value = Builder.CreateIntrinsic(DstTy, II->getIntrinsicID(), Ops);
1823 propagateIRFlags(Value, ValueList);
1824 return Value;
1825 }
1826 assert(isa<UnaryInstruction>(I) && "Unexpected instruction type in Generate");
1827 auto *Value =
1828 Builder.CreateUnOp((Instruction::UnaryOps)I->getOpcode(), Ops[0]);
1829 propagateIRFlags(Value, ValueList);
1830 return Value;
1831}
1832
1833// Starting from a shuffle, look up through operands tracking the shuffled index
1834// of each lane. If we can simplify away the shuffles to identities then
1835// do so.
1836bool VectorCombine::foldShuffleToIdentity(Instruction &I) {
1837 auto *Ty = dyn_cast<FixedVectorType>(I.getType());
1838 if (!Ty || I.use_empty())
1839 return false;
1840
1841 SmallVector<InstLane> Start(Ty->getNumElements());
1842 for (unsigned M = 0, E = Ty->getNumElements(); M < E; ++M)
1843 Start[M] = lookThroughShuffles(&*I.use_begin(), M);
1844
1846 Worklist.push_back(Start);
1847 SmallPtrSet<Use *, 4> IdentityLeafs, SplatLeafs, ConcatLeafs;
1848 unsigned NumVisited = 0;
1849
1850 while (!Worklist.empty()) {
1851 if (++NumVisited > MaxInstrsToScan)
1852 return false;
1853
1854 SmallVector<InstLane> Item = Worklist.pop_back_val();
1855 auto [FrontU, FrontLane] = Item.front();
1856
1857 // If we found an undef first lane then bail out to keep things simple.
1858 if (!FrontU)
1859 return false;
1860
1861 // Helper to peek through bitcasts to the same value.
1862 auto IsEquiv = [&](Value *X, Value *Y) {
1863 return X->getType() == Y->getType() &&
1865 };
1866
1867 // Look for an identity value.
1868 if (FrontLane == 0 &&
1869 cast<FixedVectorType>(FrontU->get()->getType())->getNumElements() ==
1870 Ty->getNumElements() &&
1871 all_of(drop_begin(enumerate(Item)), [IsEquiv, Item](const auto &E) {
1872 Value *FrontV = Item.front().first->get();
1873 return !E.value().first || (IsEquiv(E.value().first->get(), FrontV) &&
1874 E.value().second == (int)E.index());
1875 })) {
1876 IdentityLeafs.insert(FrontU);
1877 continue;
1878 }
1879 // Look for constants, for the moment only supporting constant splats.
1880 if (auto *C = dyn_cast<Constant>(FrontU);
1881 C && C->getSplatValue() &&
1882 all_of(drop_begin(Item), [Item](InstLane &IL) {
1883 Value *FrontV = Item.front().first->get();
1884 Use *U = IL.first;
1885 return !U || U->get() == FrontV;
1886 })) {
1887 SplatLeafs.insert(FrontU);
1888 continue;
1889 }
1890 // Look for a splat value.
1891 if (all_of(drop_begin(Item), [Item](InstLane &IL) {
1892 auto [FrontU, FrontLane] = Item.front();
1893 auto [U, Lane] = IL;
1894 return !U || (U->get() == FrontU->get() && Lane == FrontLane);
1895 })) {
1896 SplatLeafs.insert(FrontU);
1897 continue;
1898 }
1899
1900 // We need each element to be the same type of value, and check that each
1901 // element has a single use.
1902 if (all_of(drop_begin(Item), [Item](InstLane IL) {
1903 Value *FrontV = Item.front().first->get();
1904 if (!IL.first)
1905 return true;
1906 Value *V = IL.first->get();
1907 if (auto *I = dyn_cast<Instruction>(V); I && !I->hasOneUse())
1908 return false;
1909 if (V->getValueID() != FrontV->getValueID())
1910 return false;
1911 if (auto *CI = dyn_cast<CmpInst>(V))
1912 if (CI->getPredicate() != cast<CmpInst>(FrontV)->getPredicate())
1913 return false;
1914 if (auto *CI = dyn_cast<CastInst>(V))
1915 if (CI->getSrcTy() != cast<CastInst>(FrontV)->getSrcTy())
1916 return false;
1917 if (auto *SI = dyn_cast<SelectInst>(V))
1918 if (!isa<VectorType>(SI->getOperand(0)->getType()) ||
1919 SI->getOperand(0)->getType() !=
1920 cast<SelectInst>(FrontV)->getOperand(0)->getType())
1921 return false;
1922 if (isa<CallInst>(V) && !isa<IntrinsicInst>(V))
1923 return false;
1924 auto *II = dyn_cast<IntrinsicInst>(V);
1925 return !II || (isa<IntrinsicInst>(FrontV) &&
1926 II->getIntrinsicID() ==
1927 cast<IntrinsicInst>(FrontV)->getIntrinsicID());
1928 })) {
1929 // Check the operator is one that we support.
1930 if (isa<BinaryOperator, CmpInst>(FrontU)) {
1931 // We exclude div/rem in case they hit UB from poison lanes.
1932 if (auto *BO = dyn_cast<BinaryOperator>(FrontU);
1933 BO && BO->isIntDivRem())
1934 return false;
1937 continue;
1938 } else if (isa<UnaryOperator, TruncInst, ZExtInst, SExtInst>(FrontU)) {
1940 continue;
1941 } else if (auto *BitCast = dyn_cast<BitCastInst>(FrontU)) {
1942 // TODO: Handle vector widening/narrowing bitcasts.
1943 auto *DstTy = dyn_cast<FixedVectorType>(BitCast->getDestTy());
1944 auto *SrcTy = dyn_cast<FixedVectorType>(BitCast->getSrcTy());
1945 if (DstTy && SrcTy &&
1946 SrcTy->getNumElements() == DstTy->getNumElements()) {
1948 continue;
1949 }
1950 } else if (isa<SelectInst>(FrontU)) {
1954 continue;
1955 } else if (auto *II = dyn_cast<IntrinsicInst>(FrontU);
1956 II && isTriviallyVectorizable(II->getIntrinsicID())) {
1957 for (unsigned Op = 0, E = II->getNumOperands() - 1; Op < E; Op++) {
1958 if (isVectorIntrinsicWithScalarOpAtArg(II->getIntrinsicID(), Op)) {
1959 if (!all_of(drop_begin(Item), [Item, Op](InstLane &IL) {
1960 Value *FrontV = Item.front().first->get();
1961 Use *U = IL.first;
1962 return !U || (cast<Instruction>(U->get())->getOperand(Op) ==
1963 cast<Instruction>(FrontV)->getOperand(Op));
1964 }))
1965 return false;
1966 continue;
1967 }
1969 }
1970 continue;
1971 }
1972 }
1973
1974 if (isFreeConcat(Item, TTI)) {
1975 ConcatLeafs.insert(FrontU);
1976 continue;
1977 }
1978
1979 return false;
1980 }
1981
1982 if (NumVisited <= 1)
1983 return false;
1984
1985 // If we got this far, we know the shuffles are superfluous and can be
1986 // removed. Scan through again and generate the new tree of instructions.
1987 Builder.SetInsertPoint(&I);
1988 Value *V = generateNewInstTree(Start, Ty, IdentityLeafs, SplatLeafs,
1989 ConcatLeafs, Builder);
1990 replaceValue(I, *V);
1991 return true;
1992}
1993
1994/// Given a commutative reduction, the order of the input lanes does not alter
1995/// the results. We can use this to remove certain shuffles feeding the
1996/// reduction, removing the need to shuffle at all.
1997bool VectorCombine::foldShuffleFromReductions(Instruction &I) {
1998 auto *II = dyn_cast<IntrinsicInst>(&I);
1999 if (!II)
2000 return false;
2001 switch (II->getIntrinsicID()) {
2002 case Intrinsic::vector_reduce_add:
2003 case Intrinsic::vector_reduce_mul:
2004 case Intrinsic::vector_reduce_and:
2005 case Intrinsic::vector_reduce_or:
2006 case Intrinsic::vector_reduce_xor:
2007 case Intrinsic::vector_reduce_smin:
2008 case Intrinsic::vector_reduce_smax:
2009 case Intrinsic::vector_reduce_umin:
2010 case Intrinsic::vector_reduce_umax:
2011 break;
2012 default:
2013 return false;
2014 }
2015
2016 // Find all the inputs when looking through operations that do not alter the
2017 // lane order (binops, for example). Currently we look for a single shuffle,
2018 // and can ignore splat values.
2019 std::queue<Value *> Worklist;
2021 ShuffleVectorInst *Shuffle = nullptr;
2022 if (auto *Op = dyn_cast<Instruction>(I.getOperand(0)))
2023 Worklist.push(Op);
2024
2025 while (!Worklist.empty()) {
2026 Value *CV = Worklist.front();
2027 Worklist.pop();
2028 if (Visited.contains(CV))
2029 continue;
2030
2031 // Splats don't change the order, so can be safely ignored.
2032 if (isSplatValue(CV))
2033 continue;
2034
2035 Visited.insert(CV);
2036
2037 if (auto *CI = dyn_cast<Instruction>(CV)) {
2038 if (CI->isBinaryOp()) {
2039 for (auto *Op : CI->operand_values())
2040 Worklist.push(Op);
2041 continue;
2042 } else if (auto *SV = dyn_cast<ShuffleVectorInst>(CI)) {
2043 if (Shuffle && Shuffle != SV)
2044 return false;
2045 Shuffle = SV;
2046 continue;
2047 }
2048 }
2049
2050 // Anything else is currently an unknown node.
2051 return false;
2052 }
2053
2054 if (!Shuffle)
2055 return false;
2056
2057 // Check all uses of the binary ops and shuffles are also included in the
2058 // lane-invariant operations (Visited should be the list of lanewise
2059 // instructions, including the shuffle that we found).
2060 for (auto *V : Visited)
2061 for (auto *U : V->users())
2062 if (!Visited.contains(U) && U != &I)
2063 return false;
2064
2066 dyn_cast<FixedVectorType>(II->getOperand(0)->getType());
2067 if (!VecType)
2068 return false;
2069 FixedVectorType *ShuffleInputType =
2070 dyn_cast<FixedVectorType>(Shuffle->getOperand(0)->getType());
2071 if (!ShuffleInputType)
2072 return false;
2073 unsigned NumInputElts = ShuffleInputType->getNumElements();
2074
2075 // Find the mask from sorting the lanes into order. This is most likely to
2076 // become a identity or concat mask. Undef elements are pushed to the end.
2077 SmallVector<int> ConcatMask;
2078 Shuffle->getShuffleMask(ConcatMask);
2079 sort(ConcatMask, [](int X, int Y) { return (unsigned)X < (unsigned)Y; });
2080 // In the case of a truncating shuffle it's possible for the mask
2081 // to have an index greater than the size of the resulting vector.
2082 // This requires special handling.
2083 bool IsTruncatingShuffle = VecType->getNumElements() < NumInputElts;
2084 bool UsesSecondVec =
2085 any_of(ConcatMask, [&](int M) { return M >= (int)NumInputElts; });
2086
2087 FixedVectorType *VecTyForCost =
2088 (UsesSecondVec && !IsTruncatingShuffle) ? VecType : ShuffleInputType;
2091 VecTyForCost, Shuffle->getShuffleMask());
2094 VecTyForCost, ConcatMask);
2095
2096 LLVM_DEBUG(dbgs() << "Found a reduction feeding from a shuffle: " << *Shuffle
2097 << "\n");
2098 LLVM_DEBUG(dbgs() << " OldCost: " << OldCost << " vs NewCost: " << NewCost
2099 << "\n");
2100 if (NewCost < OldCost) {
2101 Builder.SetInsertPoint(Shuffle);
2102 Value *NewShuffle = Builder.CreateShuffleVector(
2103 Shuffle->getOperand(0), Shuffle->getOperand(1), ConcatMask);
2104 LLVM_DEBUG(dbgs() << "Created new shuffle: " << *NewShuffle << "\n");
2105 replaceValue(*Shuffle, *NewShuffle);
2106 }
2107
2108 // See if we can re-use foldSelectShuffle, getting it to reduce the size of
2109 // the shuffle into a nicer order, as it can ignore the order of the shuffles.
2110 return foldSelectShuffle(*Shuffle, true);
2111}
2112
2113/// Determine if its more efficient to fold:
2114/// reduce(trunc(x)) -> trunc(reduce(x)).
2115/// reduce(sext(x)) -> sext(reduce(x)).
2116/// reduce(zext(x)) -> zext(reduce(x)).
2117bool VectorCombine::foldCastFromReductions(Instruction &I) {
2118 auto *II = dyn_cast<IntrinsicInst>(&I);
2119 if (!II)
2120 return false;
2121
2122 bool TruncOnly = false;
2123 Intrinsic::ID IID = II->getIntrinsicID();
2124 switch (IID) {
2125 case Intrinsic::vector_reduce_add:
2126 case Intrinsic::vector_reduce_mul:
2127 TruncOnly = true;
2128 break;
2129 case Intrinsic::vector_reduce_and:
2130 case Intrinsic::vector_reduce_or:
2131 case Intrinsic::vector_reduce_xor:
2132 break;
2133 default:
2134 return false;
2135 }
2136
2137 unsigned ReductionOpc = getArithmeticReductionInstruction(IID);
2138 Value *ReductionSrc = I.getOperand(0);
2139
2140 Value *Src;
2141 if (!match(ReductionSrc, m_OneUse(m_Trunc(m_Value(Src)))) &&
2142 (TruncOnly || !match(ReductionSrc, m_OneUse(m_ZExtOrSExt(m_Value(Src))))))
2143 return false;
2144
2145 auto CastOpc =
2146 (Instruction::CastOps)cast<Instruction>(ReductionSrc)->getOpcode();
2147
2148 auto *SrcTy = cast<VectorType>(Src->getType());
2149 auto *ReductionSrcTy = cast<VectorType>(ReductionSrc->getType());
2150 Type *ResultTy = I.getType();
2151
2154 ReductionOpc, ReductionSrcTy, std::nullopt, CostKind);
2155 OldCost += TTI.getCastInstrCost(CastOpc, ReductionSrcTy, SrcTy,
2157 cast<CastInst>(ReductionSrc));
2158 InstructionCost NewCost =
2159 TTI.getArithmeticReductionCost(ReductionOpc, SrcTy, std::nullopt,
2160 CostKind) +
2161 TTI.getCastInstrCost(CastOpc, ResultTy, ReductionSrcTy->getScalarType(),
2163
2164 if (OldCost <= NewCost || !NewCost.isValid())
2165 return false;
2166
2167 Value *NewReduction = Builder.CreateIntrinsic(SrcTy->getScalarType(),
2168 II->getIntrinsicID(), {Src});
2169 Value *NewCast = Builder.CreateCast(CastOpc, NewReduction, ResultTy);
2170 replaceValue(I, *NewCast);
2171 return true;
2172}
2173
2174/// This method looks for groups of shuffles acting on binops, of the form:
2175/// %x = shuffle ...
2176/// %y = shuffle ...
2177/// %a = binop %x, %y
2178/// %b = binop %x, %y
2179/// shuffle %a, %b, selectmask
2180/// We may, especially if the shuffle is wider than legal, be able to convert
2181/// the shuffle to a form where only parts of a and b need to be computed. On
2182/// architectures with no obvious "select" shuffle, this can reduce the total
2183/// number of operations if the target reports them as cheaper.
2184bool VectorCombine::foldSelectShuffle(Instruction &I, bool FromReduction) {
2185 auto *SVI = cast<ShuffleVectorInst>(&I);
2186 auto *VT = cast<FixedVectorType>(I.getType());
2187 auto *Op0 = dyn_cast<Instruction>(SVI->getOperand(0));
2188 auto *Op1 = dyn_cast<Instruction>(SVI->getOperand(1));
2189 if (!Op0 || !Op1 || Op0 == Op1 || !Op0->isBinaryOp() || !Op1->isBinaryOp() ||
2190 VT != Op0->getType())
2191 return false;
2192
2193 auto *SVI0A = dyn_cast<Instruction>(Op0->getOperand(0));
2194 auto *SVI0B = dyn_cast<Instruction>(Op0->getOperand(1));
2195 auto *SVI1A = dyn_cast<Instruction>(Op1->getOperand(0));
2196 auto *SVI1B = dyn_cast<Instruction>(Op1->getOperand(1));
2197 SmallPtrSet<Instruction *, 4> InputShuffles({SVI0A, SVI0B, SVI1A, SVI1B});
2198 auto checkSVNonOpUses = [&](Instruction *I) {
2199 if (!I || I->getOperand(0)->getType() != VT)
2200 return true;
2201 return any_of(I->users(), [&](User *U) {
2202 return U != Op0 && U != Op1 &&
2203 !(isa<ShuffleVectorInst>(U) &&
2204 (InputShuffles.contains(cast<Instruction>(U)) ||
2205 isInstructionTriviallyDead(cast<Instruction>(U))));
2206 });
2207 };
2208 if (checkSVNonOpUses(SVI0A) || checkSVNonOpUses(SVI0B) ||
2209 checkSVNonOpUses(SVI1A) || checkSVNonOpUses(SVI1B))
2210 return false;
2211
2212 // Collect all the uses that are shuffles that we can transform together. We
2213 // may not have a single shuffle, but a group that can all be transformed
2214 // together profitably.
2216 auto collectShuffles = [&](Instruction *I) {
2217 for (auto *U : I->users()) {
2218 auto *SV = dyn_cast<ShuffleVectorInst>(U);
2219 if (!SV || SV->getType() != VT)
2220 return false;
2221 if ((SV->getOperand(0) != Op0 && SV->getOperand(0) != Op1) ||
2222 (SV->getOperand(1) != Op0 && SV->getOperand(1) != Op1))
2223 return false;
2224 if (!llvm::is_contained(Shuffles, SV))
2225 Shuffles.push_back(SV);
2226 }
2227 return true;
2228 };
2229 if (!collectShuffles(Op0) || !collectShuffles(Op1))
2230 return false;
2231 // From a reduction, we need to be processing a single shuffle, otherwise the
2232 // other uses will not be lane-invariant.
2233 if (FromReduction && Shuffles.size() > 1)
2234 return false;
2235
2236 // Add any shuffle uses for the shuffles we have found, to include them in our
2237 // cost calculations.
2238 if (!FromReduction) {
2239 for (ShuffleVectorInst *SV : Shuffles) {
2240 for (auto *U : SV->users()) {
2241 ShuffleVectorInst *SSV = dyn_cast<ShuffleVectorInst>(U);
2242 if (SSV && isa<UndefValue>(SSV->getOperand(1)) && SSV->getType() == VT)
2243 Shuffles.push_back(SSV);
2244 }
2245 }
2246 }
2247
2248 // For each of the output shuffles, we try to sort all the first vector
2249 // elements to the beginning, followed by the second array elements at the
2250 // end. If the binops are legalized to smaller vectors, this may reduce total
2251 // number of binops. We compute the ReconstructMask mask needed to convert
2252 // back to the original lane order.
2254 SmallVector<SmallVector<int>> OrigReconstructMasks;
2255 int MaxV1Elt = 0, MaxV2Elt = 0;
2256 unsigned NumElts = VT->getNumElements();
2257 for (ShuffleVectorInst *SVN : Shuffles) {
2259 SVN->getShuffleMask(Mask);
2260
2261 // Check the operands are the same as the original, or reversed (in which
2262 // case we need to commute the mask).
2263 Value *SVOp0 = SVN->getOperand(0);
2264 Value *SVOp1 = SVN->getOperand(1);
2265 if (isa<UndefValue>(SVOp1)) {
2266 auto *SSV = cast<ShuffleVectorInst>(SVOp0);
2267 SVOp0 = SSV->getOperand(0);
2268 SVOp1 = SSV->getOperand(1);
2269 for (unsigned I = 0, E = Mask.size(); I != E; I++) {
2270 if (Mask[I] >= static_cast<int>(SSV->getShuffleMask().size()))
2271 return false;
2272 Mask[I] = Mask[I] < 0 ? Mask[I] : SSV->getMaskValue(Mask[I]);
2273 }
2274 }
2275 if (SVOp0 == Op1 && SVOp1 == Op0) {
2276 std::swap(SVOp0, SVOp1);
2278 }
2279 if (SVOp0 != Op0 || SVOp1 != Op1)
2280 return false;
2281
2282 // Calculate the reconstruction mask for this shuffle, as the mask needed to
2283 // take the packed values from Op0/Op1 and reconstructing to the original
2284 // order.
2285 SmallVector<int> ReconstructMask;
2286 for (unsigned I = 0; I < Mask.size(); I++) {
2287 if (Mask[I] < 0) {
2288 ReconstructMask.push_back(-1);
2289 } else if (Mask[I] < static_cast<int>(NumElts)) {
2290 MaxV1Elt = std::max(MaxV1Elt, Mask[I]);
2291 auto It = find_if(V1, [&](const std::pair<int, int> &A) {
2292 return Mask[I] == A.first;
2293 });
2294 if (It != V1.end())
2295 ReconstructMask.push_back(It - V1.begin());
2296 else {
2297 ReconstructMask.push_back(V1.size());
2298 V1.emplace_back(Mask[I], V1.size());
2299 }
2300 } else {
2301 MaxV2Elt = std::max<int>(MaxV2Elt, Mask[I] - NumElts);
2302 auto It = find_if(V2, [&](const std::pair<int, int> &A) {
2303 return Mask[I] - static_cast<int>(NumElts) == A.first;
2304 });
2305 if (It != V2.end())
2306 ReconstructMask.push_back(NumElts + It - V2.begin());
2307 else {
2308 ReconstructMask.push_back(NumElts + V2.size());
2309 V2.emplace_back(Mask[I] - NumElts, NumElts + V2.size());
2310 }
2311 }
2312 }
2313
2314 // For reductions, we know that the lane ordering out doesn't alter the
2315 // result. In-order can help simplify the shuffle away.
2316 if (FromReduction)
2317 sort(ReconstructMask);
2318 OrigReconstructMasks.push_back(std::move(ReconstructMask));
2319 }
2320
2321 // If the Maximum element used from V1 and V2 are not larger than the new
2322 // vectors, the vectors are already packes and performing the optimization
2323 // again will likely not help any further. This also prevents us from getting
2324 // stuck in a cycle in case the costs do not also rule it out.
2325 if (V1.empty() || V2.empty() ||
2326 (MaxV1Elt == static_cast<int>(V1.size()) - 1 &&
2327 MaxV2Elt == static_cast<int>(V2.size()) - 1))
2328 return false;
2329
2330 // GetBaseMaskValue takes one of the inputs, which may either be a shuffle, a
2331 // shuffle of another shuffle, or not a shuffle (that is treated like a
2332 // identity shuffle).
2333 auto GetBaseMaskValue = [&](Instruction *I, int M) {
2334 auto *SV = dyn_cast<ShuffleVectorInst>(I);
2335 if (!SV)
2336 return M;
2337 if (isa<UndefValue>(SV->getOperand(1)))
2338 if (auto *SSV = dyn_cast<ShuffleVectorInst>(SV->getOperand(0)))
2339 if (InputShuffles.contains(SSV))
2340 return SSV->getMaskValue(SV->getMaskValue(M));
2341 return SV->getMaskValue(M);
2342 };
2343
2344 // Attempt to sort the inputs my ascending mask values to make simpler input
2345 // shuffles and push complex shuffles down to the uses. We sort on the first
2346 // of the two input shuffle orders, to try and get at least one input into a
2347 // nice order.
2348 auto SortBase = [&](Instruction *A, std::pair<int, int> X,
2349 std::pair<int, int> Y) {
2350 int MXA = GetBaseMaskValue(A, X.first);
2351 int MYA = GetBaseMaskValue(A, Y.first);
2352 return MXA < MYA;
2353 };
2354 stable_sort(V1, [&](std::pair<int, int> A, std::pair<int, int> B) {
2355 return SortBase(SVI0A, A, B);
2356 });
2357 stable_sort(V2, [&](std::pair<int, int> A, std::pair<int, int> B) {
2358 return SortBase(SVI1A, A, B);
2359 });
2360 // Calculate our ReconstructMasks from the OrigReconstructMasks and the
2361 // modified order of the input shuffles.
2362 SmallVector<SmallVector<int>> ReconstructMasks;
2363 for (const auto &Mask : OrigReconstructMasks) {
2364 SmallVector<int> ReconstructMask;
2365 for (int M : Mask) {
2366 auto FindIndex = [](const SmallVector<std::pair<int, int>> &V, int M) {
2367 auto It = find_if(V, [M](auto A) { return A.second == M; });
2368 assert(It != V.end() && "Expected all entries in Mask");
2369 return std::distance(V.begin(), It);
2370 };
2371 if (M < 0)
2372 ReconstructMask.push_back(-1);
2373 else if (M < static_cast<int>(NumElts)) {
2374 ReconstructMask.push_back(FindIndex(V1, M));
2375 } else {
2376 ReconstructMask.push_back(NumElts + FindIndex(V2, M));
2377 }
2378 }
2379 ReconstructMasks.push_back(std::move(ReconstructMask));
2380 }
2381
2382 // Calculate the masks needed for the new input shuffles, which get padded
2383 // with undef
2384 SmallVector<int> V1A, V1B, V2A, V2B;
2385 for (unsigned I = 0; I < V1.size(); I++) {
2386 V1A.push_back(GetBaseMaskValue(SVI0A, V1[I].first));
2387 V1B.push_back(GetBaseMaskValue(SVI0B, V1[I].first));
2388 }
2389 for (unsigned I = 0; I < V2.size(); I++) {
2390 V2A.push_back(GetBaseMaskValue(SVI1A, V2[I].first));
2391 V2B.push_back(GetBaseMaskValue(SVI1B, V2[I].first));
2392 }
2393 while (V1A.size() < NumElts) {
2396 }
2397 while (V2A.size() < NumElts) {
2400 }
2401
2402 auto AddShuffleCost = [&](InstructionCost C, Instruction *I) {
2403 auto *SV = dyn_cast<ShuffleVectorInst>(I);
2404 if (!SV)
2405 return C;
2406 return C + TTI.getShuffleCost(isa<UndefValue>(SV->getOperand(1))
2409 VT, SV->getShuffleMask());
2410 };
2411 auto AddShuffleMaskCost = [&](InstructionCost C, ArrayRef<int> Mask) {
2412 return C + TTI.getShuffleCost(TTI::SK_PermuteTwoSrc, VT, Mask);
2413 };
2414
2415 // Get the costs of the shuffles + binops before and after with the new
2416 // shuffle masks.
2417 InstructionCost CostBefore =
2418 TTI.getArithmeticInstrCost(Op0->getOpcode(), VT) +
2419 TTI.getArithmeticInstrCost(Op1->getOpcode(), VT);
2420 CostBefore += std::accumulate(Shuffles.begin(), Shuffles.end(),
2421 InstructionCost(0), AddShuffleCost);
2422 CostBefore += std::accumulate(InputShuffles.begin(), InputShuffles.end(),
2423 InstructionCost(0), AddShuffleCost);
2424
2425 // The new binops will be unused for lanes past the used shuffle lengths.
2426 // These types attempt to get the correct cost for that from the target.
2427 FixedVectorType *Op0SmallVT =
2428 FixedVectorType::get(VT->getScalarType(), V1.size());
2429 FixedVectorType *Op1SmallVT =
2430 FixedVectorType::get(VT->getScalarType(), V2.size());
2431 InstructionCost CostAfter =
2432 TTI.getArithmeticInstrCost(Op0->getOpcode(), Op0SmallVT) +
2433 TTI.getArithmeticInstrCost(Op1->getOpcode(), Op1SmallVT);
2434 CostAfter += std::accumulate(ReconstructMasks.begin(), ReconstructMasks.end(),
2435 InstructionCost(0), AddShuffleMaskCost);
2436 std::set<SmallVector<int>> OutputShuffleMasks({V1A, V1B, V2A, V2B});
2437 CostAfter +=
2438 std::accumulate(OutputShuffleMasks.begin(), OutputShuffleMasks.end(),
2439 InstructionCost(0), AddShuffleMaskCost);
2440
2441 LLVM_DEBUG(dbgs() << "Found a binop select shuffle pattern: " << I << "\n");
2442 LLVM_DEBUG(dbgs() << " CostBefore: " << CostBefore
2443 << " vs CostAfter: " << CostAfter << "\n");
2444 if (CostBefore <= CostAfter)
2445 return false;
2446
2447 // The cost model has passed, create the new instructions.
2448 auto GetShuffleOperand = [&](Instruction *I, unsigned Op) -> Value * {
2449 auto *SV = dyn_cast<ShuffleVectorInst>(I);
2450 if (!SV)
2451 return I;
2452 if (isa<UndefValue>(SV->getOperand(1)))
2453 if (auto *SSV = dyn_cast<ShuffleVectorInst>(SV->getOperand(0)))
2454 if (InputShuffles.contains(SSV))
2455 return SSV->getOperand(Op);
2456 return SV->getOperand(Op);
2457 };
2458 Builder.SetInsertPoint(*SVI0A->getInsertionPointAfterDef());
2459 Value *NSV0A = Builder.CreateShuffleVector(GetShuffleOperand(SVI0A, 0),
2460 GetShuffleOperand(SVI0A, 1), V1A);
2461 Builder.SetInsertPoint(*SVI0B->getInsertionPointAfterDef());
2462 Value *NSV0B = Builder.CreateShuffleVector(GetShuffleOperand(SVI0B, 0),
2463 GetShuffleOperand(SVI0B, 1), V1B);
2464 Builder.SetInsertPoint(*SVI1A->getInsertionPointAfterDef());
2465 Value *NSV1A = Builder.CreateShuffleVector(GetShuffleOperand(SVI1A, 0),
2466 GetShuffleOperand(SVI1A, 1), V2A);
2467 Builder.SetInsertPoint(*SVI1B->getInsertionPointAfterDef());
2468 Value *NSV1B = Builder.CreateShuffleVector(GetShuffleOperand(SVI1B, 0),
2469 GetShuffleOperand(SVI1B, 1), V2B);
2470 Builder.SetInsertPoint(Op0);
2471 Value *NOp0 = Builder.CreateBinOp((Instruction::BinaryOps)Op0->getOpcode(),
2472 NSV0A, NSV0B);
2473 if (auto *I = dyn_cast<Instruction>(NOp0))
2474 I->copyIRFlags(Op0, true);
2475 Builder.SetInsertPoint(Op1);
2476 Value *NOp1 = Builder.CreateBinOp((Instruction::BinaryOps)Op1->getOpcode(),
2477 NSV1A, NSV1B);
2478 if (auto *I = dyn_cast<Instruction>(NOp1))
2479 I->copyIRFlags(Op1, true);
2480
2481 for (int S = 0, E = ReconstructMasks.size(); S != E; S++) {
2482 Builder.SetInsertPoint(Shuffles[S]);
2483 Value *NSV = Builder.CreateShuffleVector(NOp0, NOp1, ReconstructMasks[S]);
2484 replaceValue(*Shuffles[S], *NSV);
2485 }
2486
2487 Worklist.pushValue(NSV0A);
2488 Worklist.pushValue(NSV0B);
2489 Worklist.pushValue(NSV1A);
2490 Worklist.pushValue(NSV1B);
2491 for (auto *S : Shuffles)
2492 Worklist.add(S);
2493 return true;
2494}
2495
2496/// This is the entry point for all transforms. Pass manager differences are
2497/// handled in the callers of this function.
2498bool VectorCombine::run() {
2500 return false;
2501
2502 // Don't attempt vectorization if the target does not support vectors.
2503 if (!TTI.getNumberOfRegisters(TTI.getRegisterClassForType(/*Vector*/ true)))
2504 return false;
2505
2506 bool MadeChange = false;
2507 auto FoldInst = [this, &MadeChange](Instruction &I) {
2508 Builder.SetInsertPoint(&I);
2509 bool IsFixedVectorType = isa<FixedVectorType>(I.getType());
2510 auto Opcode = I.getOpcode();
2511
2512 // These folds should be beneficial regardless of when this pass is run
2513 // in the optimization pipeline.
2514 // The type checking is for run-time efficiency. We can avoid wasting time
2515 // dispatching to folding functions if there's no chance of matching.
2516 if (IsFixedVectorType) {
2517 switch (Opcode) {
2518 case Instruction::InsertElement:
2519 MadeChange |= vectorizeLoadInsert(I);
2520 break;
2521 case Instruction::ShuffleVector:
2522 MadeChange |= widenSubvectorLoad(I);
2523 break;
2524 default:
2525 break;
2526 }
2527 }
2528
2529 // This transform works with scalable and fixed vectors
2530 // TODO: Identify and allow other scalable transforms
2531 if (isa<VectorType>(I.getType())) {
2532 MadeChange |= scalarizeBinopOrCmp(I);
2533 MadeChange |= scalarizeLoadExtract(I);
2534 MadeChange |= scalarizeVPIntrinsic(I);
2535 }
2536
2537 if (Opcode == Instruction::Store)
2538 MadeChange |= foldSingleElementStore(I);
2539
2540 // If this is an early pipeline invocation of this pass, we are done.
2541 if (TryEarlyFoldsOnly)
2542 return;
2543
2544 // Otherwise, try folds that improve codegen but may interfere with
2545 // early IR canonicalizations.
2546 // The type checking is for run-time efficiency. We can avoid wasting time
2547 // dispatching to folding functions if there's no chance of matching.
2548 if (IsFixedVectorType) {
2549 switch (Opcode) {
2550 case Instruction::InsertElement:
2551 MadeChange |= foldInsExtFNeg(I);
2552 break;
2553 case Instruction::ShuffleVector:
2554 MadeChange |= foldShuffleOfBinops(I);
2555 MadeChange |= foldShuffleOfCastops(I);
2556 MadeChange |= foldShuffleOfShuffles(I);
2557 MadeChange |= foldSelectShuffle(I);
2558 MadeChange |= foldShuffleToIdentity(I);
2559 break;
2560 case Instruction::BitCast:
2561 MadeChange |= foldBitcastShuffle(I);
2562 break;
2563 }
2564 } else {
2565 switch (Opcode) {
2566 case Instruction::Call:
2567 MadeChange |= foldShuffleFromReductions(I);
2568 MadeChange |= foldCastFromReductions(I);
2569 break;
2570 case Instruction::ICmp:
2571 case Instruction::FCmp:
2572 MadeChange |= foldExtractExtract(I);
2573 break;
2574 default:
2575 if (Instruction::isBinaryOp(Opcode)) {
2576 MadeChange |= foldExtractExtract(I);
2577 MadeChange |= foldExtractedCmps(I);
2578 }
2579 break;
2580 }
2581 }
2582 };
2583
2584 for (BasicBlock &BB : F) {
2585 // Ignore unreachable basic blocks.
2586 if (!DT.isReachableFromEntry(&BB))
2587 continue;
2588 // Use early increment range so that we can erase instructions in loop.
2589 for (Instruction &I : make_early_inc_range(BB)) {
2590 if (I.isDebugOrPseudoInst())
2591 continue;
2592 FoldInst(I);
2593 }
2594 }
2595
2596 while (!Worklist.isEmpty()) {
2597 Instruction *I = Worklist.removeOne();
2598 if (!I)
2599 continue;
2600
2603 continue;
2604 }
2605
2606 FoldInst(*I);
2607 }
2608
2609 return MadeChange;
2610}
2611
2614 auto &AC = FAM.getResult<AssumptionAnalysis>(F);
2618 const DataLayout *DL = &F.getDataLayout();
2619 VectorCombine Combiner(F, TTI, DT, AA, AC, DL, TryEarlyFoldsOnly);
2620 if (!Combiner.run())
2621 return PreservedAnalyses::all();
2624 return PA;
2625}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This is the interface for LLVM's primary stateless and local alias analysis.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static cl::opt< TargetTransformInfo::TargetCostKind > CostKind("cost-kind", cl::desc("Target cost kind"), cl::init(TargetTransformInfo::TCK_RecipThroughput), cl::values(clEnumValN(TargetTransformInfo::TCK_RecipThroughput, "throughput", "Reciprocal throughput"), clEnumValN(TargetTransformInfo::TCK_Latency, "latency", "Instruction latency"), clEnumValN(TargetTransformInfo::TCK_CodeSize, "code-size", "Code size"), clEnumValN(TargetTransformInfo::TCK_SizeAndLatency, "size-latency", "Code size and latency")))
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
This file defines the DenseMap class.
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1309
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
This is the interface for a simple mod/ref and alias analysis over globals.
Hexagon Common GEP
static void eraseInstruction(Instruction &I, ICFLoopSafetyInfo &SafetyInfo, MemorySSAUpdater &MSSAU)
Definition: LICM.cpp:1502
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
FunctionAnalysisManager FAM
if(PassOpts->AAPipeline)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned OpIndex
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition: Statistic.h:166
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
This pass exposes codegen information to IR-level passes.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp:191
static SmallVector< InstLane > generateInstLaneVectorFromOperand(ArrayRef< InstLane > Item, int Op)
static Value * createShiftShuffle(Value *Vec, unsigned OldIndex, unsigned NewIndex, IRBuilder<> &Builder)
Create a shuffle that translates (shifts) 1 element from the input vector to a new element location.
static Value * peekThroughBitcasts(Value *V)
Return the source operand of a potentially bitcasted value.
static Align computeAlignmentAfterScalarization(Align VectorAlignment, Type *ScalarType, Value *Idx, const DataLayout &DL)
The memory operation on a vector of ScalarType had alignment of VectorAlignment.
static ScalarizationResult canScalarizeAccess(VectorType *VecTy, Value *Idx, Instruction *CtxI, AssumptionCache &AC, const DominatorTree &DT)
Check if it is legal to scalarize a memory access to VecTy at index Idx.
static cl::opt< bool > DisableVectorCombine("disable-vector-combine", cl::init(false), cl::Hidden, cl::desc("Disable all vector combine transforms"))
static InstLane lookThroughShuffles(Use *U, int Lane)
static bool canWidenLoad(LoadInst *Load, const TargetTransformInfo &TTI)
static bool isFreeConcat(ArrayRef< InstLane > Item, const TargetTransformInfo &TTI)
Detect concat of multiple values into a vector.
static Value * generateNewInstTree(ArrayRef< InstLane > Item, FixedVectorType *Ty, const SmallPtrSet< Use *, 4 > &IdentityLeafs, const SmallPtrSet< Use *, 4 > &SplatLeafs, const SmallPtrSet< Use *, 4 > &ConcatLeafs, IRBuilder<> &Builder)
static const unsigned InvalidIndex
std::pair< Use *, int > InstLane
static cl::opt< unsigned > MaxInstrsToScan("vector-combine-max-scan-instrs", cl::init(30), cl::Hidden, cl::desc("Max number of instructions to scan for vector combining."))
static cl::opt< bool > DisableBinopExtractShuffle("disable-binop-extract-shuffle", cl::init(false), cl::Hidden, cl::desc("Disable binop extract to shuffle transforms"))
static bool isMemModifiedBetween(BasicBlock::iterator Begin, BasicBlock::iterator End, const MemoryLocation &Loc, AAResults &AA)
static ExtractElementInst * translateExtract(ExtractElementInst *ExtElt, unsigned NewIndex, IRBuilder<> &Builder)
Given an extract element instruction with constant index operand, shuffle the source vector (shift th...
A manager for alias analyses.
ModRefInfo getModRefInfo(const Instruction *I, const std::optional< MemoryLocation > &OptLoc)
Check whether or not an instruction may read or write the optionally specified memory location.
Class for arbitrary precision integers.
Definition: APInt.h:78
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:253
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Definition: PassManager.h:405
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:168
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
A function analysis which provides an AssumptionCache.
A cache of @llvm.assume calls within a function.
bool hasFnAttr(Attribute::AttrKind Kind) const
Return true if the attribute exists for the function.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
BinaryOps getOpcode() const
Definition: InstrTypes.h:442
Represents analyses that only rely on functions' control flow.
Definition: Analysis.h:72
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1410
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1401
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1104
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:757
bool isFPPredicate() const
Definition: InstrTypes.h:864
Combiner implementation.
Definition: Combiner.h:34
static Constant * getExtractElement(Constant *Vec, Constant *Idx, Type *OnlyIfReducedTy=nullptr)
Definition: Constants.cpp:2528
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:146
This class represents a range of values.
Definition: ConstantRange.h:47
ConstantRange urem(const ConstantRange &Other) const
Return a new range representing the possible values resulting from an unsigned remainder operation of...
ConstantRange binaryAnd(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a binary-and of a value in this ra...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:1399
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
iterator find(const_arg_type_t< KeyT > Val)
Definition: DenseMap.h:155
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&... Args)
Definition: DenseMap.h:226
iterator end()
Definition: DenseMap.h:84
Analysis pass which computes a DominatorTree.
Definition: Dominators.h:279
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool isReachableFromEntry(const Use &U) const
Provide an overload for a Use.
Definition: Dominators.cpp:321
This instruction extracts a single (scalar) element from a VectorType value.
Class to represent fixed width SIMD vectors.
Definition: DerivedTypes.h:539
unsigned getNumElements() const
Definition: DerivedTypes.h:582
static FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition: Type.cpp:680
Value * CreateInsertElement(Type *VecTy, Value *NewElt, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2492
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:2480
LoadInst * CreateAlignedLoad(Type *Ty, Value *Ptr, MaybeAlign Align, const char *Name)
Definition: IRBuilder.h:1824
Value * CreateVectorSplat(unsigned NumElts, Value *V, const Twine &Name="")
Return a vector value that contains.
Definition: IRBuilder.cpp:1193
CallInst * CreateIntrinsic(Intrinsic::ID ID, ArrayRef< Type * > Types, ArrayRef< Value * > Args, Instruction *FMFSource=nullptr, const Twine &Name="")
Create a call to intrinsic ID with Args, mangled using Types.
Definition: IRBuilder.cpp:933
Value * CreateFNegFMF(Value *V, Instruction *FMFSource, const Twine &Name="")
Copy fast-math-flags from an instruction rather than using the builder's default FMF.
Definition: IRBuilder.h:1757
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.cpp:1091
Value * CreateFreeze(Value *V, const Twine &Name="")
Definition: IRBuilder.h:2555
Value * CreateInBoundsGEP(Type *Ty, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:1891
Value * CreatePointerBitCastOrAddrSpaceCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2202
ConstantInt * getInt64(uint64_t C)
Get a constant 64-bit value.
Definition: IRBuilder.h:488
Value * CreateUnOp(Instruction::UnaryOps Opc, Value *V, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1770
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:483
Value * CreateCmp(CmpInst::Predicate Pred, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:2386
Value * CreateBitCast(Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2147
LoadInst * CreateLoad(Type *Ty, Value *Ptr, const char *Name)
Provided to resolve 'CreateLoad(Ty, Ptr, "...")' correctly, instead of converting the string to 'bool...
Definition: IRBuilder.h:1807
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:2514
StoreInst * CreateStore(Value *Val, Value *Ptr, bool isVolatile=false)
Definition: IRBuilder.h:1820
PointerType * getPtrTy(unsigned AddrSpace=0)
Fetch the type representing a pointer.
Definition: IRBuilder.h:566
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1683
Value * CreateCast(Instruction::CastOps Op, Value *V, Type *DestTy, const Twine &Name="")
Definition: IRBuilder.h:2181
void SetInsertPoint(BasicBlock *TheBB)
This specifies that created instructions should be appended to the end of the specified block.
Definition: IRBuilder.h:177
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:2686
InstructionWorklist - This is the worklist management logic for InstCombine and other simplification ...
void pushUsersToWorkList(Instruction &I)
When an instruction is simplified, add all users of the instruction to the work lists because they mi...
void push(Instruction *I)
Push the instruction onto the worklist stack.
void remove(Instruction *I)
Remove I from the worklist if it exists.
bool isBinaryOp() const
Definition: Instruction.h:279
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
bool mayReadFromMemory() const LLVM_READONLY
Return true if this instruction may read memory.
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
bool isIntDivRem() const
Definition: Instruction.h:280
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:55
An instruction for reading from memory.
Definition: Instructions.h:174
Representation for a specific memory location.
static MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1852
A set of analyses that are preserved following a run of a transformation pass.
Definition: Analysis.h:111
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: Analysis.h:117
void preserveSet()
Mark an analysis set as preserved.
Definition: Analysis.h:146
This instruction constructs a fixed permutation of two input vectors.
int getMaskValue(unsigned Elt) const
Return the shuffle mask value of this instruction for the given element index.
VectorType * getType() const
Overload to return most specific vector type.
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
static bool isIdentityMask(ArrayRef< int > Mask, int NumSrcElts)
Return true if this shuffle mask chooses elements from exactly one source vector without lane crossin...
static void commuteShuffleMask(MutableArrayRef< int > Mask, unsigned InVecNumElts)
Change values in a shuffle permute mask assuming the two vector operands of length InVecNumElts have ...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:367
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:441
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:502
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
reference emplace_back(ArgTypes &&... Args)
Definition: SmallVector.h:950
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:290
void setAlignment(Align Align)
Definition: Instructions.h:333
Analysis pass providing the TargetTransformInfo.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
InstructionCost getAddressComputationCost(Type *Ty, ScalarEvolution *SE=nullptr, const SCEV *Ptr=nullptr) const
InstructionCost getMemoryOpCost(unsigned Opcode, Type *Src, Align Alignment, unsigned AddressSpace, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, OperandValueInfo OpdInfo={OK_AnyValue, OP_None}, const Instruction *I=nullptr) const
InstructionCost getIntrinsicInstrCost(const IntrinsicCostAttributes &ICA, TTI::TargetCostKind CostKind) const
InstructionCost getArithmeticReductionCost(unsigned Opcode, VectorType *Ty, std::optional< FastMathFlags > FMF, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput) const
Calculate the cost of vector reduction intrinsics.
InstructionCost getCastInstrCost(unsigned Opcode, Type *Dst, Type *Src, TTI::CastContextHint CCH, TTI::TargetCostKind CostKind=TTI::TCK_SizeAndLatency, const Instruction *I=nullptr) const
InstructionCost getArithmeticInstrCost(unsigned Opcode, Type *Ty, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, TTI::OperandValueInfo Opd1Info={TTI::OK_AnyValue, TTI::OP_None}, TTI::OperandValueInfo Opd2Info={TTI::OK_AnyValue, TTI::OP_None}, ArrayRef< const Value * > Args=std::nullopt, const Instruction *CxtI=nullptr, const TargetLibraryInfo *TLibInfo=nullptr) const
This is an approximation of reciprocal throughput of a math/logic op.
unsigned getRegisterClassForType(bool Vector, Type *Ty=nullptr) const
TargetCostKind
The kind of cost model.
@ TCK_RecipThroughput
Reciprocal throughput.
unsigned getMinVectorRegisterBitWidth() const
unsigned getNumberOfRegisters(unsigned ClassID) const
InstructionCost getShuffleCost(ShuffleKind Kind, VectorType *Tp, ArrayRef< int > Mask=std::nullopt, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, int Index=0, VectorType *SubTp=nullptr, ArrayRef< const Value * > Args=std::nullopt, const Instruction *CxtI=nullptr) const
InstructionCost getScalarizationOverhead(VectorType *Ty, const APInt &DemandedElts, bool Insert, bool Extract, TTI::TargetCostKind CostKind) const
Estimate the overhead of scalarizing an instruction.
InstructionCost getVectorInstrCost(unsigned Opcode, Type *Val, TTI::TargetCostKind CostKind, unsigned Index=-1, Value *Op0=nullptr, Value *Op1=nullptr) const
ShuffleKind
The various kinds of shuffle patterns for vector queries.
@ SK_Select
Selects elements from the corresponding lane of either source operand.
@ SK_PermuteSingleSrc
Shuffle elements of single source vector with any shuffle mask.
@ SK_Broadcast
Broadcast element 0 to all other elements.
@ SK_PermuteTwoSrc
Merge elements from two source vectors into one with any shuffle mask.
@ None
The cast is not used with a load/store of any kind.
InstructionCost getCmpSelInstrCost(unsigned Opcode, Type *ValTy, Type *CondTy, CmpInst::Predicate VecPred, TTI::TargetCostKind CostKind=TTI::TCK_RecipThroughput, const Instruction *I=nullptr) const
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:261
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:251
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
Definition: Type.h:184
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:224
TypeSize getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:343
'undef' values are things that do not have specified contents.
Definition: Constants.h:1398
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
op_range operands()
Definition: User.h:242
Value * getOperand(unsigned i) const
Definition: User.h:169
static bool isVPBinOp(Intrinsic::ID ID)
This is the common base class for vector predication intrinsics.
std::optional< unsigned > getFunctionalIntrinsicID() const
std::optional< unsigned > getFunctionalOpcode() const
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition: Value.h:736
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition: Value.h:434
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
iterator_range< user_iterator > users()
Definition: Value.h:421
Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition: Value.cpp:927
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:532
bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
Definition: Value.cpp:149
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:309
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
Definition: BitmaskEnum.h:121
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
AttributeList getAttributes(LLVMContext &C, ID id)
Return the attributes for an intrinsic.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
class_match< BinaryOperator > m_BinOp()
Match an arbitrary binary operation and ignore it.
Definition: PatternMatch.h:100
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
specific_intval< false > m_SpecificInt(const APInt &V)
Match a specific integer value or vector with all elements equal to the value.
Definition: PatternMatch.h:972
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:816
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:875
TwoOps_match< Val_t, Idx_t, Instruction::ExtractElement > m_ExtractElt(const Val_t &Val, const Idx_t &Idx)
Matches ExtractElementInst.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_and< LTy, RTy > m_CombineAnd(const LTy &L, const RTy &R)
Combine two pattern matchers matching L && R.
Definition: PatternMatch.h:245
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
TwoOps_match< V1_t, V2_t, Instruction::ShuffleVector > m_Shuffle(const V1_t &v1, const V2_t &v2)
Matches ShuffleVectorInst independently of mask value.
OneOps_match< OpTy, Instruction::Load > m_Load(const OpTy &Op)
Matches LoadInst.
class_match< UndefValue > m_UndefValue()
Match an arbitrary UndefValue constant.
Definition: PatternMatch.h:155
class_match< CmpInst > m_Cmp()
Matches any compare instruction and ignore it.
Definition: PatternMatch.h:105
CastOperator_match< OpTy, Instruction::BitCast > m_BitCast(const OpTy &Op)
Matches BitCast.
match_combine_or< CastInst_match< OpTy, SExtInst >, NNegZExt_match< OpTy > > m_SExtLike(const OpTy &Op)
Match either "sext" or "zext nneg".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
auto m_Undef()
Match an arbitrary undef constant.
Definition: PatternMatch.h:152
ThreeOps_match< Val_t, Elt_t, Idx_t, Instruction::InsertElement > m_InsertElt(const Val_t &Val, const Elt_t &Elt, const Idx_t &Idx)
Matches InsertElementInst.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition: STLExtras.h:329
@ Offset
Definition: DWP.cpp:480
void stable_sort(R &&Range)
Definition: STLExtras.h:2020
UnaryFunction for_each(R &&Range, UnaryFunction F)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1715
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1722
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition: STLExtras.h:2431
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
unsigned getArithmeticReductionInstruction(Intrinsic::ID RdxID)
Returns the arithmetic instruction opcode used when expanding a reduction.
Definition: LoopUtils.cpp:959
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition: STLExtras.h:656
bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition: Loads.cpp:357
bool widenShuffleMaskElts(int Scale, ArrayRef< int > Mask, SmallVectorImpl< int > &ScaledMask)
Try to transform a shuffle mask by replacing elements with the scaled index for an equivalent mask of...
Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
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:1729
bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition: Local.cpp:400
bool isSplatValue(const Value *V, int Index=-1, unsigned Depth=0)
Return true if each element of the vector value V is poisoned or equal to every other non-poisoned el...
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:291
bool isModSet(const ModRefInfo MRI)
Definition: ModRef.h:48
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1647
bool isSafeToLoadUnconditionally(Value *V, Align Alignment, const APInt &Size, const DataLayout &DL, Instruction *ScanFrom, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if we know that executing a load from this value cannot trap.
Definition: Loads.cpp:372
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1736
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
void propagateIRFlags(Value *I, ArrayRef< Value * > VL, Value *OpValue=nullptr, bool IncludeWrapFlags=true)
Get the intersection (logical and) of all of the potential IR flags of each scalar operation (VL) tha...
Definition: LoopUtils.cpp:1308
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
void narrowShuffleMaskElts(int Scale, ArrayRef< int > Mask, SmallVectorImpl< int > &ScaledMask)
Replace each shuffle mask index with the scaled sequential indices for an equivalent mask of narrowed...
DWARFExpression::Operation Op
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1749
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1886
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
Definition: Alignment.h:212
bool isVectorIntrinsicWithScalarOpAtArg(Intrinsic::ID ID, unsigned ScalarOpdIdx)
Identifies if the vector form of the intrinsic has a scalar operand.
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition: VectorUtils.cpp:46
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39