LLVM 24.0.0git
X86PartialReduction.cpp
Go to the documentation of this file.
1//===-- X86PartialReduction.cpp -------------------------------------------===//
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 looks for add instructions used by a horizontal reduction to see
10// if we might be able to use pmaddwd or psadbw. Some cases of this require
11// cross basic block knowledge and can't be done in SelectionDAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86TargetMachine.h"
19#include "llvm/IR/Analysis.h"
20#include "llvm/IR/Constants.h"
22#include "llvm/IR/IRBuilder.h"
24#include "llvm/IR/IntrinsicsX86.h"
25#include "llvm/IR/PassManager.h"
27#include "llvm/Pass.h"
29
30using namespace llvm;
31
32#define DEBUG_TYPE "x86-partial-reduction"
33
34namespace {
35
36class X86PartialReduction {
37 const X86TargetMachine *TM;
38 const DataLayout *DL = nullptr;
39 const X86Subtarget *ST = nullptr;
40
41public:
42 X86PartialReduction(const X86TargetMachine *TM) : TM(TM) {}
43 bool run(Function &F);
44
45private:
46 bool tryMAddReplacement(Instruction *Op, bool ReduceInOneBB);
47 bool trySADReplacement(Instruction *Op);
48 bool tryByteSumReplacement(Instruction *Op);
49};
50
51class X86PartialReductionLegacy : public FunctionPass {
52public:
53 static char ID; // Pass identification, replacement for typeid.
54
55 X86PartialReductionLegacy() : FunctionPass(ID) {}
56
57 bool runOnFunction(Function &F) override;
58
59 void getAnalysisUsage(AnalysisUsage &AU) const override {
60 AU.setPreservesCFG();
61 }
62
63 StringRef getPassName() const override { return "X86 Partial Reduction"; }
64};
65}
66
68 return new X86PartialReductionLegacy();
69}
70
71char X86PartialReductionLegacy::ID = 0;
72
73INITIALIZE_PASS(X86PartialReductionLegacy, DEBUG_TYPE, "X86 Partial Reduction",
74 false, false)
75
76// This function should be aligned with detectExtMul() in X86ISelLowering.cpp.
77static bool matchVPDPBUSDPattern(const X86Subtarget *ST, BinaryOperator *Mul,
79 if (!ST->hasVNNI() && !ST->hasAVXVNNI())
80 return false;
81
82 Value *LHS = Mul->getOperand(0);
83 Value *RHS = Mul->getOperand(1);
84
87
88 auto IsFreeTruncation = [&](Value *Op) {
89 if (auto *Cast = dyn_cast<CastInst>(Op)) {
90 if (Cast->getParent() == Mul->getParent() &&
91 (Cast->getOpcode() == Instruction::SExt ||
92 Cast->getOpcode() == Instruction::ZExt) &&
93 Cast->getOperand(0)->getType()->getScalarSizeInBits() <= 8)
94 return true;
95 }
96
97 return isa<Constant>(Op);
98 };
99
100 // (dpbusd (zext a), (sext, b)). Since the first operand should be unsigned
101 // value, we need to check LHS is zero extended value. RHS should be signed
102 // value, so we just check the signed bits.
104 computeKnownBits(LHS, *DL).countMaxActiveBits() <= 8) &&
106 return true;
107
108 return false;
109}
110
111bool X86PartialReduction::tryMAddReplacement(Instruction *Op,
112 bool ReduceInOneBB) {
113 if (!ST->hasSSE2())
114 return false;
115
116 // Need at least 8 elements.
117 if (cast<FixedVectorType>(Op->getType())->getNumElements() < 8)
118 return false;
119
120 // Element type should be i32.
121 if (!cast<VectorType>(Op->getType())->getElementType()->isIntegerTy(32))
122 return false;
123
125 if (!Mul || Mul->getOpcode() != Instruction::Mul)
126 return false;
127
128 Value *LHS = Mul->getOperand(0);
129 Value *RHS = Mul->getOperand(1);
130
131 // If the target support VNNI, leave it to ISel to combine reduce operation
132 // to VNNI instruction.
133 // TODO: we can support transforming reduce to VNNI intrinsic for across block
134 // in this pass.
135 if (ReduceInOneBB && matchVPDPBUSDPattern(ST, Mul, DL))
136 return false;
137
138 // LHS and RHS should be only used once or if they are the same then only
139 // used twice. Only check this when SSE4.1 is enabled and we have zext/sext
140 // instructions, otherwise we use punpck to emulate zero extend in stages. The
141 // trunc/ we need to do likely won't introduce new instructions in that case.
142 if (ST->hasSSE41()) {
143 if (LHS == RHS) {
144 if (!isa<Constant>(LHS) && !LHS->hasNUses(2))
145 return false;
146 } else {
147 if (!isa<Constant>(LHS) && !LHS->hasOneUse())
148 return false;
149 if (!isa<Constant>(RHS) && !RHS->hasOneUse())
150 return false;
151 }
152 }
153
154 auto CanShrinkOp = [&](Value *Op) {
155 auto IsFreeTruncation = [&](Value *Op) {
156 if (auto *Cast = dyn_cast<CastInst>(Op)) {
157 if (Cast->getParent() == Mul->getParent() &&
158 (Cast->getOpcode() == Instruction::SExt ||
159 Cast->getOpcode() == Instruction::ZExt) &&
160 Cast->getOperand(0)->getType()->getScalarSizeInBits() <= 16)
161 return true;
162 }
163
164 return isa<Constant>(Op);
165 };
166
167 // If the operation can be freely truncated and has enough sign bits we
168 // can shrink.
169 if (IsFreeTruncation(Op) && ComputeNumSignBits(Op, *DL, nullptr, Mul) > 16)
170 return true;
171
172 // SelectionDAG has limited support for truncating through an add or sub if
173 // the inputs are freely truncatable.
174 if (auto *BO = dyn_cast<BinaryOperator>(Op)) {
175 if (BO->getParent() == Mul->getParent() &&
176 IsFreeTruncation(BO->getOperand(0)) &&
177 IsFreeTruncation(BO->getOperand(1)) &&
178 ComputeNumSignBits(Op, *DL, nullptr, Mul) > 16)
179 return true;
180 }
181
182 return false;
183 };
184
185 // Both Ops need to be shrinkable.
186 if (!CanShrinkOp(LHS) && !CanShrinkOp(RHS))
187 return false;
188
189 IRBuilder<> Builder(Mul);
190
191 auto *MulTy = cast<FixedVectorType>(Op->getType());
192 unsigned NumElts = MulTy->getNumElements();
193
194 // Extract even elements and odd elements and add them together. This will
195 // be pattern matched by SelectionDAG to pmaddwd. This instruction will be
196 // half the original width.
197 SmallVector<int, 16> EvenMask(NumElts / 2);
198 SmallVector<int, 16> OddMask(NumElts / 2);
199 for (int i = 0, e = NumElts / 2; i != e; ++i) {
200 EvenMask[i] = i * 2;
201 OddMask[i] = i * 2 + 1;
202 }
203 // Creating a new mul so the replaceAllUsesWith below doesn't replace the
204 // uses in the shuffles we're creating.
205 Value *NewMul = Builder.CreateMul(Mul->getOperand(0), Mul->getOperand(1));
206 Value *EvenElts = Builder.CreateShuffleVector(NewMul, NewMul, EvenMask);
207 Value *OddElts = Builder.CreateShuffleVector(NewMul, NewMul, OddMask);
208 Value *MAdd = Builder.CreateAdd(EvenElts, OddElts);
209
210 // Concatenate zeroes to extend back to the original type.
211 SmallVector<int, 32> ConcatMask(NumElts);
212 std::iota(ConcatMask.begin(), ConcatMask.end(), 0);
214 Value *Concat = Builder.CreateShuffleVector(MAdd, Zero, ConcatMask);
215
218
219 return true;
220}
221
222bool X86PartialReduction::trySADReplacement(Instruction *Op) {
223 if (!ST->hasSSE2())
224 return false;
225
226 // TODO: There's nothing special about i32, any integer type above i16 should
227 // work just as well.
228 if (!cast<VectorType>(Op->getType())->getElementType()->isIntegerTy(32))
229 return false;
230
231 Value *LHS;
233 LHS = Op->getOperand(0);
234 } else {
235 // Operand should be a select.
236 auto *SI = dyn_cast<SelectInst>(Op);
237 if (!SI)
238 return false;
239
240 Value *RHS;
241 // Select needs to implement absolute value.
242 auto SPR = matchSelectPattern(SI, LHS, RHS);
243 if (SPR.Flavor != SPF_ABS)
244 return false;
245 }
246
247 // Need a subtract of two values.
249 if (!Sub || Sub->getOpcode() != Instruction::Sub)
250 return false;
251
252 // Look for zero extend from i8.
253 auto getZeroExtendedVal = [](Value *Op) -> Value * {
254 if (auto *ZExt = dyn_cast<ZExtInst>(Op))
255 if (cast<VectorType>(ZExt->getOperand(0)->getType())
256 ->getElementType()
257 ->isIntegerTy(8))
258 return ZExt->getOperand(0);
259
260 return nullptr;
261 };
262
263 // Both operands of the subtract should be extends from vXi8.
264 Value *Op0 = getZeroExtendedVal(Sub->getOperand(0));
265 Value *Op1 = getZeroExtendedVal(Sub->getOperand(1));
266 if (!Op0 || !Op1)
267 return false;
268
269 IRBuilder<> Builder(Op);
270
271 auto *OpTy = cast<FixedVectorType>(Op->getType());
272 unsigned NumElts = OpTy->getNumElements();
273
274 unsigned IntrinsicNumElts;
275 Intrinsic::ID IID;
276 if (ST->useBWIRegs() && NumElts >= 64) {
277 IID = Intrinsic::x86_avx512_psad_bw_512;
278 IntrinsicNumElts = 64;
279 } else if (ST->hasAVX2() && NumElts >= 32) {
280 IID = Intrinsic::x86_avx2_psad_bw;
281 IntrinsicNumElts = 32;
282 } else {
283 IID = Intrinsic::x86_sse2_psad_bw;
284 IntrinsicNumElts = 16;
285 }
286
287 Function *PSADBWFn = Intrinsic::getOrInsertDeclaration(Op->getModule(), IID);
288
289 if (NumElts < 16) {
290 // Pad input with zeroes.
291 SmallVector<int, 32> ConcatMask(16);
292 for (unsigned i = 0; i != NumElts; ++i)
293 ConcatMask[i] = i;
294 for (unsigned i = NumElts; i != 16; ++i)
295 ConcatMask[i] = (i % NumElts) + NumElts;
296
298 Op0 = Builder.CreateShuffleVector(Op0, Zero, ConcatMask);
299 Op1 = Builder.CreateShuffleVector(Op1, Zero, ConcatMask);
300 NumElts = 16;
301 }
302
303 // Intrinsics produce vXi64 and need to be casted to vXi32.
304 auto *I32Ty =
305 FixedVectorType::get(Builder.getInt32Ty(), IntrinsicNumElts / 4);
306
307 assert(NumElts % IntrinsicNumElts == 0 && "Unexpected number of elements!");
308 unsigned NumSplits = NumElts / IntrinsicNumElts;
309
310 // First collect the pieces we need.
311 SmallVector<Value *, 4> Ops(NumSplits);
312 for (unsigned i = 0; i != NumSplits; ++i) {
313 SmallVector<int, 64> ExtractMask(IntrinsicNumElts);
314 std::iota(ExtractMask.begin(), ExtractMask.end(), i * IntrinsicNumElts);
315 Value *ExtractOp0 = Builder.CreateShuffleVector(Op0, Op0, ExtractMask);
316 Value *ExtractOp1 = Builder.CreateShuffleVector(Op1, Op0, ExtractMask);
317 Ops[i] = Builder.CreateCall(PSADBWFn, {ExtractOp0, ExtractOp1});
318 Ops[i] = Builder.CreateBitCast(Ops[i], I32Ty);
319 }
320
321 assert(isPowerOf2_32(NumSplits) && "Expected power of 2 splits");
322 unsigned Stages = Log2_32(NumSplits);
323 for (unsigned s = Stages; s > 0; --s) {
324 unsigned NumConcatElts =
325 cast<FixedVectorType>(Ops[0]->getType())->getNumElements() * 2;
326 for (unsigned i = 0; i != 1U << (s - 1); ++i) {
327 SmallVector<int, 64> ConcatMask(NumConcatElts);
328 std::iota(ConcatMask.begin(), ConcatMask.end(), 0);
329 Ops[i] = Builder.CreateShuffleVector(Ops[i*2], Ops[i*2+1], ConcatMask);
330 }
331 }
332
333 // At this point the final value should be in Ops[0]. Now we need to adjust
334 // it to the final original type.
335 NumElts = cast<FixedVectorType>(OpTy)->getNumElements();
336 if (NumElts == 2) {
337 // Extract down to 2 elements.
338 Ops[0] = Builder.CreateShuffleVector(Ops[0], Ops[0], ArrayRef<int>{0, 1});
339 } else if (NumElts >= 8) {
340 SmallVector<int, 32> ConcatMask(NumElts);
341 unsigned SubElts =
342 cast<FixedVectorType>(Ops[0]->getType())->getNumElements();
343 for (unsigned i = 0; i != SubElts; ++i)
344 ConcatMask[i] = i;
345 for (unsigned i = SubElts; i != NumElts; ++i)
346 ConcatMask[i] = (i % SubElts) + SubElts;
347
349 Ops[0] = Builder.CreateShuffleVector(Ops[0], Zero, ConcatMask);
350 }
351
352 Op->replaceAllUsesWith(Ops[0]);
353 Op->eraseFromParent();
354
355 return true;
356}
357
358bool X86PartialReduction::tryByteSumReplacement(Instruction *Op) {
359 if (!ST->hasSSE2())
360 return false;
361
362 auto *OpTy = dyn_cast<FixedVectorType>(Op->getType());
363 if (!OpTy)
364 return false;
365 unsigned ElemBits = OpTy->getElementType()->getScalarSizeInBits();
366 if (ElemBits != 32 && ElemBits != 64)
367 return false;
368
369 auto *ZExt = dyn_cast<ZExtInst>(Op);
370 if (!ZExt)
371 return false;
372
373 auto *SrcTy = dyn_cast<FixedVectorType>(ZExt->getOperand(0)->getType());
374 if (!SrcTy || !SrcTy->getElementType()->isIntegerTy(8))
375 return false;
376
377 unsigned NumElts = OpTy->getNumElements();
378
379 // Below 16 elements, SelectionDAG's SAD matcher handles it.
380 if (NumElts < 16)
381 return false;
382
383 // Select the widest psadbw intrinsic the subtarget supports.
384 unsigned IntrinsicNumElts;
385 Intrinsic::ID IID;
386 if (ST->useBWIRegs() && NumElts >= 64) {
387 IID = Intrinsic::x86_avx512_psad_bw_512;
388 IntrinsicNumElts = 64;
389 } else if (ST->hasAVX2() && NumElts >= 32) {
390 IID = Intrinsic::x86_avx2_psad_bw;
391 IntrinsicNumElts = 32;
392 } else {
393 IID = Intrinsic::x86_sse2_psad_bw;
394 IntrinsicNumElts = 16;
395 }
396
397 if (NumElts % IntrinsicNumElts != 0 ||
398 !isPowerOf2_32(NumElts / IntrinsicNumElts))
399 return false;
400 unsigned NumSplits = NumElts / IntrinsicNumElts;
401
402 IRBuilder<> Builder(Op);
403 Builder.SetCurrentDebugLocation(Op->getDebugLoc());
404
405 Function *PSADBWFn = Intrinsic::getOrInsertDeclaration(Op->getModule(), IID);
406
407 // psadbw(x, 0) horizontally sums 8 bytes per lane into i64.
408 auto *I8VecTy = FixedVectorType::get(Builder.getInt8Ty(), IntrinsicNumElts);
409 Value *Zeroes = Constant::getNullValue(I8VecTy);
410
411 // For i32 accumulators, bitcast each i64 lane to two i32 lanes.
412 // Per-lane sums are at most 8*255 = 2040, so the upper i32 is always zero.
413 FixedVectorType *I32PerSplitTy =
414 ElemBits == 32
415 ? FixedVectorType::get(Builder.getInt32Ty(), IntrinsicNumElts / 4)
416 : nullptr;
417
418 // Split input into IntrinsicNumElts-byte lanes and compute psadbw per lane.
419 Value *Src = ZExt->getOperand(0);
420 SmallVector<Value *, 4> Ops(NumSplits);
421 for (unsigned i = 0; i != NumSplits; ++i) {
422 SmallVector<int, 64> ExtractMask(IntrinsicNumElts);
423 std::iota(ExtractMask.begin(), ExtractMask.end(), i * IntrinsicNumElts);
424 Value *ExtractSrc = Builder.CreateShuffleVector(Src, Src, ExtractMask);
425 Ops[i] = Builder.CreateCall(PSADBWFn, {ExtractSrc, Zeroes});
426 if (I32PerSplitTy)
427 Ops[i] = Builder.CreateBitCast(Ops[i], I32PerSplitTy);
428 }
429
430 // Concat per-split results with a pairwise shuffle tree.
431 unsigned Stages = Log2_32(NumSplits);
432 for (unsigned S = Stages; S > 0; --S) {
433 unsigned NumConcatElts =
434 cast<FixedVectorType>(Ops[0]->getType())->getNumElements() * 2;
435 for (unsigned i = 0; i != 1U << (S - 1); ++i) {
436 SmallVector<int, 64> ConcatMask(NumConcatElts);
437 std::iota(ConcatMask.begin(), ConcatMask.end(), 0);
438 Ops[i] =
439 Builder.CreateShuffleVector(Ops[i * 2], Ops[i * 2 + 1], ConcatMask);
440 }
441 }
442
443 // Pad with zeros to match the original vector width.
444 SmallVector<int, 32> ConcatMask(NumElts);
445 unsigned SubElts = cast<FixedVectorType>(Ops[0]->getType())->getNumElements();
446 for (unsigned i = 0; i != SubElts; ++i)
447 ConcatMask[i] = i;
448 for (unsigned i = SubElts; i != NumElts; ++i)
449 ConcatMask[i] = (i % SubElts) + SubElts;
451 Ops[0] = Builder.CreateShuffleVector(Ops[0], Zero, ConcatMask);
452
453 Op->replaceAllUsesWith(Ops[0]);
454 Op->eraseFromParent();
455 return true;
456}
457
458// Walk backwards from the ExtractElementInst and determine if it is the end of
459// a horizontal reduction. Return the input to the reduction if we find one.
461 bool &ReduceInOneBB) {
462 ReduceInOneBB = true;
463 // Make sure we're extracting index 0.
464 auto *Index = dyn_cast<ConstantInt>(EE.getIndexOperand());
465 if (!Index || !Index->isNullValue())
466 return nullptr;
467
468 const auto *BO = dyn_cast<BinaryOperator>(EE.getVectorOperand());
469 if (!BO || BO->getOpcode() != Instruction::Add || !BO->hasOneUse())
470 return nullptr;
471 if (EE.getParent() != BO->getParent())
472 ReduceInOneBB = false;
473
474 unsigned NumElems = cast<FixedVectorType>(BO->getType())->getNumElements();
475 // Ensure the reduction size is a power of 2.
476 if (!isPowerOf2_32(NumElems))
477 return nullptr;
478
479 const Value *Op = BO;
480 unsigned Stages = Log2_32(NumElems);
481 for (unsigned i = 0; i != Stages; ++i) {
482 const auto *BO = dyn_cast<BinaryOperator>(Op);
483 if (!BO || BO->getOpcode() != Instruction::Add)
484 return nullptr;
485 if (EE.getParent() != BO->getParent())
486 ReduceInOneBB = false;
487
488 // If this isn't the first add, then it should only have 2 users, the
489 // shuffle and another add which we checked in the previous iteration.
490 if (i != 0 && !BO->hasNUses(2))
491 return nullptr;
492
493 Value *LHS = BO->getOperand(0);
494 Value *RHS = BO->getOperand(1);
495
496 auto *Shuffle = dyn_cast<ShuffleVectorInst>(LHS);
497 if (Shuffle) {
498 Op = RHS;
499 } else {
501 Op = LHS;
502 }
503
504 // The first operand of the shuffle should be the same as the other operand
505 // of the bin op.
506 if (!Shuffle || Shuffle->getOperand(0) != Op)
507 return nullptr;
508
509 // Verify the shuffle has the expected (at this stage of the pyramid) mask.
510 unsigned MaskEnd = 1 << i;
511 for (unsigned Index = 0; Index < MaskEnd; ++Index)
512 if (Shuffle->getMaskValue(Index) != (int)(MaskEnd + Index))
513 return nullptr;
514 }
515
516 return const_cast<Value *>(Op);
517}
518
519// See if this BO is reachable from this Phi by walking forward through single
520// use BinaryOperators with the same opcode. If we get back then we know we've
521// found a loop and it is safe to step through this Add to find more leaves.
523 // The PHI itself should only have one use.
524 if (!Phi->hasOneUse())
525 return false;
526
527 Instruction *U = cast<Instruction>(*Phi->user_begin());
528 if (U == BO)
529 return true;
530
531 while (U->hasOneUse() && U->getOpcode() == BO->getOpcode())
532 U = cast<Instruction>(*U->user_begin());
533
534 return U == BO;
535}
536
537// Collect all the leaves of the tree of adds that feeds into the horizontal
538// reduction. Root is the Value that is used by the horizontal reduction.
539// We look through single use phis, single use adds, or adds that are used by
540// a phi that forms a loop with the add.
544 Worklist.push_back(Root);
545
546 while (!Worklist.empty()) {
547 Value *V = Worklist.pop_back_val();
548 if (!Visited.insert(V).second)
549 continue;
550
551 if (auto *PN = dyn_cast<PHINode>(V)) {
552 // PHI node should have single use unless it is the root node, then it
553 // has 2 uses.
554 if (!PN->hasNUses(PN == Root ? 2 : 1))
555 break;
556
557 // Push incoming values to the worklist.
558 append_range(Worklist, PN->incoming_values());
559
560 continue;
561 }
562
563 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
564 if (BO->getOpcode() == Instruction::Add) {
565 // Simple case. Single use, just push its operands to the worklist.
566 if (BO->hasNUses(BO == Root ? 2 : 1)) {
567 append_range(Worklist, BO->operands());
568 continue;
569 }
570
571 // If there is additional use, make sure it is an unvisited phi that
572 // gets us back to this node.
573 if (BO->hasNUses(BO == Root ? 3 : 2)) {
574 PHINode *PN = nullptr;
575 for (auto *U : BO->users())
576 if (auto *P = dyn_cast<PHINode>(U))
577 if (!Visited.count(P))
578 PN = P;
579
580 // If we didn't find a 2-input PHI then this isn't a case we can
581 // handle.
582 if (!PN || PN->getNumIncomingValues() != 2)
583 continue;
584
585 // Walk forward from this phi to see if it reaches back to this add.
586 if (!isReachableFromPHI(PN, BO))
587 continue;
588
589 // The phi forms a loop with this Add, push its operands.
590 append_range(Worklist, BO->operands());
591 }
592 }
593 }
594
595 // Not an add or phi, make it a leaf.
596 if (auto *I = dyn_cast<Instruction>(V)) {
597 if (!V->hasNUses(I == Root ? 2 : 1))
598 continue;
599
600 // Add this as a leaf.
601 Leaves.push_back(I);
602 }
603 }
604}
605
606bool X86PartialReduction::run(Function &F) {
607 ST = TM->getSubtargetImpl(F);
608 DL = &F.getDataLayout();
609
610 bool MadeChange = false;
611 for (auto &BB : F) {
612 for (auto &I : BB) {
613 auto *EE = dyn_cast<ExtractElementInst>(&I);
614 if (!EE)
615 continue;
616
617 bool ReduceInOneBB;
618 // First find a reduction tree.
619 // FIXME: Do we need to handle other opcodes than Add?
620 Value *Root = matchAddReduction(*EE, ReduceInOneBB);
621 if (!Root)
622 continue;
623
624 SmallVector<Instruction *, 8> Leaves;
625 collectLeaves(Root, Leaves);
626
627 for (Instruction *I : Leaves) {
628 if (tryMAddReplacement(I, ReduceInOneBB)) {
629 MadeChange = true;
630 continue;
631 }
632
633 // Don't do SAD matching on the root node. SelectionDAG already
634 // has support for that and currently generates better code.
635 if (I != Root && trySADReplacement(I)) {
636 MadeChange = true;
637 continue;
638 }
639
640 // Byte sum via psadbw(x, 0). Same rationale as trySADReplacement:
641 // don't match on the root node because SelectionDAG already handles
642 // small single-vector patterns and generally emits better code for
643 // them. We only help on wider intermediate shapes that reach us
644 // from loop vectorization.
645 if (I != Root && tryByteSumReplacement(I)) {
646 MadeChange = true;
647 continue;
648 }
649 }
650 }
651 }
652
653 return MadeChange;
654}
655
656bool X86PartialReductionLegacy::runOnFunction(Function &F) {
657 if (skipFunction(F))
658 return false;
659
660 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
661 if (!TPC)
662 return false;
663
664 return X86PartialReduction(&TPC->getTM<X86TargetMachine>()).run(F);
665}
666
669 bool Changed = X86PartialReduction(TM).run(F);
670 if (!Changed)
671 return PreservedAnalyses::all();
672
675 return PA;
676}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool runOnFunction(Function &F, bool PostInlining)
#define DEBUG_TYPE
This header defines various interfaces for pass management in LLVM.
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define P(N)
FunctionAnalysisManager FAM
if(PassOpts->AAPipeline)
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
Target-Independent Code Generator Pass Configuration Options pass.
static constexpr int Concat[]
static bool isReachableFromPHI(PHINode *Phi, BinaryOperator *BO)
Value * RHS
Value * LHS
BinaryOperator * Mul
if(isa< SExtInst >(LHS)) std auto IsFreeTruncation
static Value * matchAddReduction(const ExtractElementInst &EE, bool &ReduceInOneBB)
static void collectLeaves(Value *Root, SmallVectorImpl< Instruction * > &Leaves)
Represent the analysis usage information of a pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:275
BinaryOps getOpcode() const
Definition InstrTypes.h:409
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
This instruction extracts a single (scalar) element from a VectorType value.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:867
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:439
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVM_ABI bool hasNUses(unsigned N) const
Return true if this Value has exactly N uses.
Definition Value.cpp:147
PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM)
const X86Subtarget * getSubtargetImpl(const Function &F) const override
Virtual method implemented by subclasses that returns a reference to that target's TargetSubtargetInf...
const ParentTy * getParent() const
Definition ilist_node.h:34
Changed
Pass manager infrastructure for declaring and invalidating analyses.
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
bool match(Val *V, const Pattern &P)
auto m_Intrinsic(const Ts &...Ops)
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
constexpr double e
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
FunctionPass * createX86PartialReductionLegacyPass()
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:332
@ SPF_ABS
Floating point maxnum.
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
@ Sub
Subtraction of integers.
DWARFExpression::Operation Op
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:880