LLVM  5.0.0
LoopUtils.cpp
Go to the documentation of this file.
1 //===-- LoopUtils.cpp - Loop Utility functions -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines common loop utility functions.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "llvm/ADT/ScopeExit.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/LoopPass.h"
26 #include "llvm/IR/Dominators.h"
27 #include "llvm/IR/Instructions.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/PatternMatch.h"
30 #include "llvm/IR/ValueHandle.h"
31 #include "llvm/Pass.h"
32 #include "llvm/Support/Debug.h"
34 
35 using namespace llvm;
36 using namespace llvm::PatternMatch;
37 
38 #define DEBUG_TYPE "loop-utils"
39 
42  for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use)
43  if (!Set.count(dyn_cast<Instruction>(*Use)))
44  return false;
45  return true;
46 }
47 
49  switch (Kind) {
50  default:
51  break;
52  case RK_IntegerAdd:
53  case RK_IntegerMult:
54  case RK_IntegerOr:
55  case RK_IntegerAnd:
56  case RK_IntegerXor:
57  case RK_IntegerMinMax:
58  return true;
59  }
60  return false;
61 }
62 
64  return (Kind != RK_NoRecurrence) && !isIntegerRecurrenceKind(Kind);
65 }
66 
68  switch (Kind) {
69  default:
70  break;
71  case RK_IntegerAdd:
72  case RK_IntegerMult:
73  case RK_FloatAdd:
74  case RK_FloatMult:
75  return true;
76  }
77  return false;
78 }
79 
84  if (!Phi->hasOneUse())
85  return Phi;
86 
87  const APInt *M = nullptr;
88  Instruction *I, *J = cast<Instruction>(Phi->use_begin()->getUser());
89 
90  // Matches either I & 2^x-1 or 2^x-1 & I. If we find a match, we update RT
91  // with a new integer type of the corresponding bit width.
92  if (match(J, m_c_And(m_Instruction(I), m_APInt(M)))) {
93  int32_t Bits = (*M + 1).exactLogBase2();
94  if (Bits > 0) {
95  RT = IntegerType::get(Phi->getContext(), Bits);
96  Visited.insert(Phi);
97  CI.insert(J);
98  return J;
99  }
100  }
101  return Phi;
102 }
103 
105  Instruction *Start, Instruction *Exit, Type *RT, bool &IsSigned,
108 
110  bool FoundOneOperand = false;
111  unsigned DstSize = RT->getPrimitiveSizeInBits();
112  Worklist.push_back(Exit);
113 
114  // Traverse the instructions in the reduction expression, beginning with the
115  // exit value.
116  while (!Worklist.empty()) {
117  Instruction *I = Worklist.pop_back_val();
118  for (Use &U : I->operands()) {
119 
120  // Terminate the traversal if the operand is not an instruction, or we
121  // reach the starting value.
122  Instruction *J = dyn_cast<Instruction>(U.get());
123  if (!J || J == Start)
124  continue;
125 
126  // Otherwise, investigate the operation if it is also in the expression.
127  if (Visited.count(J)) {
128  Worklist.push_back(J);
129  continue;
130  }
131 
132  // If the operand is not in Visited, it is not a reduction operation, but
133  // it does feed into one. Make sure it is either a single-use sign- or
134  // zero-extend instruction.
135  CastInst *Cast = dyn_cast<CastInst>(J);
136  bool IsSExtInst = isa<SExtInst>(J);
137  if (!Cast || !Cast->hasOneUse() || !(isa<ZExtInst>(J) || IsSExtInst))
138  return false;
139 
140  // Ensure the source type of the extend is no larger than the reduction
141  // type. It is not necessary for the types to be identical.
142  unsigned SrcSize = Cast->getSrcTy()->getPrimitiveSizeInBits();
143  if (SrcSize > DstSize)
144  return false;
145 
146  // Furthermore, ensure that all such extends are of the same kind.
147  if (FoundOneOperand) {
148  if (IsSigned != IsSExtInst)
149  return false;
150  } else {
151  FoundOneOperand = true;
152  IsSigned = IsSExtInst;
153  }
154 
155  // Lastly, if the source type of the extend matches the reduction type,
156  // add the extend to CI so that we can avoid accounting for it in the
157  // cost model.
158  if (SrcSize == DstSize)
159  CI.insert(Cast);
160  }
161  }
162  return true;
163 }
164 
166  Loop *TheLoop, bool HasFunNoNaNAttr,
167  RecurrenceDescriptor &RedDes) {
168  if (Phi->getNumIncomingValues() != 2)
169  return false;
170 
171  // Reduction variables are only found in the loop header block.
172  if (Phi->getParent() != TheLoop->getHeader())
173  return false;
174 
175  // Obtain the reduction start value from the value that comes from the loop
176  // preheader.
177  Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
178 
179  // ExitInstruction is the single value which is used outside the loop.
180  // We only allow for a single reduction value to be used outside the loop.
181  // This includes users of the reduction, variables (which form a cycle
182  // which ends in the phi node).
183  Instruction *ExitInstruction = nullptr;
184  // Indicates that we found a reduction operation in our scan.
185  bool FoundReduxOp = false;
186 
187  // We start with the PHI node and scan for all of the users of this
188  // instruction. All users must be instructions that can be used as reduction
189  // variables (such as ADD). We must have a single out-of-block user. The cycle
190  // must include the original PHI.
191  bool FoundStartPHI = false;
192 
193  // To recognize min/max patterns formed by a icmp select sequence, we store
194  // the number of instruction we saw from the recognized min/max pattern,
195  // to make sure we only see exactly the two instructions.
196  unsigned NumCmpSelectPatternInst = 0;
197  InstDesc ReduxDesc(false, nullptr);
198 
199  // Data used for determining if the recurrence has been type-promoted.
200  Type *RecurrenceType = Phi->getType();
202  Instruction *Start = Phi;
203  bool IsSigned = false;
204 
205  SmallPtrSet<Instruction *, 8> VisitedInsts;
207 
208  // Return early if the recurrence kind does not match the type of Phi. If the
209  // recurrence kind is arithmetic, we attempt to look through AND operations
210  // resulting from the type promotion performed by InstCombine. Vector
211  // operations are not limited to the legal integer widths, so we may be able
212  // to evaluate the reduction in the narrower width.
213  if (RecurrenceType->isFloatingPointTy()) {
214  if (!isFloatingPointRecurrenceKind(Kind))
215  return false;
216  } else {
217  if (!isIntegerRecurrenceKind(Kind))
218  return false;
219  if (isArithmeticRecurrenceKind(Kind))
220  Start = lookThroughAnd(Phi, RecurrenceType, VisitedInsts, CastInsts);
221  }
222 
223  Worklist.push_back(Start);
224  VisitedInsts.insert(Start);
225 
226  // A value in the reduction can be used:
227  // - By the reduction:
228  // - Reduction operation:
229  // - One use of reduction value (safe).
230  // - Multiple use of reduction value (not safe).
231  // - PHI:
232  // - All uses of the PHI must be the reduction (safe).
233  // - Otherwise, not safe.
234  // - By instructions outside of the loop (safe).
235  // * One value may have several outside users, but all outside
236  // uses must be of the same value.
237  // - By an instruction that is not part of the reduction (not safe).
238  // This is either:
239  // * An instruction type other than PHI or the reduction operation.
240  // * A PHI in the header other than the initial PHI.
241  while (!Worklist.empty()) {
242  Instruction *Cur = Worklist.back();
243  Worklist.pop_back();
244 
245  // No Users.
246  // If the instruction has no users then this is a broken chain and can't be
247  // a reduction variable.
248  if (Cur->use_empty())
249  return false;
250 
251  bool IsAPhi = isa<PHINode>(Cur);
252 
253  // A header PHI use other than the original PHI.
254  if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent())
255  return false;
256 
257  // Reductions of instructions such as Div, and Sub is only possible if the
258  // LHS is the reduction variable.
259  if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) &&
260  !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
261  !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0))))
262  return false;
263 
264  // Any reduction instruction must be of one of the allowed kinds. We ignore
265  // the starting value (the Phi or an AND instruction if the Phi has been
266  // type-promoted).
267  if (Cur != Start) {
268  ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr);
269  if (!ReduxDesc.isRecurrence())
270  return false;
271  }
272 
273  // A reduction operation must only have one use of the reduction value.
274  if (!IsAPhi && Kind != RK_IntegerMinMax && Kind != RK_FloatMinMax &&
275  hasMultipleUsesOf(Cur, VisitedInsts))
276  return false;
277 
278  // All inputs to a PHI node must be a reduction value.
279  if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts))
280  return false;
281 
282  if (Kind == RK_IntegerMinMax &&
283  (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur)))
284  ++NumCmpSelectPatternInst;
285  if (Kind == RK_FloatMinMax && (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur)))
286  ++NumCmpSelectPatternInst;
287 
288  // Check whether we found a reduction operator.
289  FoundReduxOp |= !IsAPhi && Cur != Start;
290 
291  // Process users of current instruction. Push non-PHI nodes after PHI nodes
292  // onto the stack. This way we are going to have seen all inputs to PHI
293  // nodes once we get to them.
296  for (User *U : Cur->users()) {
297  Instruction *UI = cast<Instruction>(U);
298 
299  // Check if we found the exit user.
300  BasicBlock *Parent = UI->getParent();
301  if (!TheLoop->contains(Parent)) {
302  // If we already know this instruction is used externally, move on to
303  // the next user.
304  if (ExitInstruction == Cur)
305  continue;
306 
307  // Exit if you find multiple values used outside or if the header phi
308  // node is being used. In this case the user uses the value of the
309  // previous iteration, in which case we would loose "VF-1" iterations of
310  // the reduction operation if we vectorize.
311  if (ExitInstruction != nullptr || Cur == Phi)
312  return false;
313 
314  // The instruction used by an outside user must be the last instruction
315  // before we feed back to the reduction phi. Otherwise, we loose VF-1
316  // operations on the value.
317  if (!is_contained(Phi->operands(), Cur))
318  return false;
319 
320  ExitInstruction = Cur;
321  continue;
322  }
323 
324  // Process instructions only once (termination). Each reduction cycle
325  // value must only be used once, except by phi nodes and min/max
326  // reductions which are represented as a cmp followed by a select.
327  InstDesc IgnoredVal(false, nullptr);
328  if (VisitedInsts.insert(UI).second) {
329  if (isa<PHINode>(UI))
330  PHIs.push_back(UI);
331  else
332  NonPHIs.push_back(UI);
333  } else if (!isa<PHINode>(UI) &&
334  ((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) &&
335  !isa<SelectInst>(UI)) ||
336  !isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence()))
337  return false;
338 
339  // Remember that we completed the cycle.
340  if (UI == Phi)
341  FoundStartPHI = true;
342  }
343  Worklist.append(PHIs.begin(), PHIs.end());
344  Worklist.append(NonPHIs.begin(), NonPHIs.end());
345  }
346 
347  // This means we have seen one but not the other instruction of the
348  // pattern or more than just a select and cmp.
349  if ((Kind == RK_IntegerMinMax || Kind == RK_FloatMinMax) &&
350  NumCmpSelectPatternInst != 2)
351  return false;
352 
353  if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction)
354  return false;
355 
356  // If we think Phi may have been type-promoted, we also need to ensure that
357  // all source operands of the reduction are either SExtInsts or ZEstInsts. If
358  // so, we will be able to evaluate the reduction in the narrower bit width.
359  if (Start != Phi)
360  if (!getSourceExtensionKind(Start, ExitInstruction, RecurrenceType,
361  IsSigned, VisitedInsts, CastInsts))
362  return false;
363 
364  // We found a reduction var if we have reached the original phi node and we
365  // only have a single instruction with out-of-loop users.
366 
367  // The ExitInstruction(Instruction which is allowed to have out-of-loop users)
368  // is saved as part of the RecurrenceDescriptor.
369 
370  // Save the description of this reduction variable.
372  RdxStart, ExitInstruction, Kind, ReduxDesc.getMinMaxKind(),
373  ReduxDesc.getUnsafeAlgebraInst(), RecurrenceType, IsSigned, CastInsts);
374  RedDes = RD;
375 
376  return true;
377 }
378 
379 /// Returns true if the instruction is a Select(ICmp(X, Y), X, Y) instruction
380 /// pattern corresponding to a min(X, Y) or max(X, Y).
383 
384  assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) &&
385  "Expect a select instruction");
386  Instruction *Cmp = nullptr;
387  SelectInst *Select = nullptr;
388 
389  // We must handle the select(cmp()) as a single instruction. Advance to the
390  // select.
391  if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) {
392  if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin())))
393  return InstDesc(false, I);
394  return InstDesc(Select, Prev.getMinMaxKind());
395  }
396 
397  // Only handle single use cases for now.
398  if (!(Select = dyn_cast<SelectInst>(I)))
399  return InstDesc(false, I);
400  if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) &&
401  !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0))))
402  return InstDesc(false, I);
403  if (!Cmp->hasOneUse())
404  return InstDesc(false, I);
405 
406  Value *CmpLeft;
407  Value *CmpRight;
408 
409  // Look for a min/max pattern.
410  if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
411  return InstDesc(Select, MRK_UIntMin);
412  else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
413  return InstDesc(Select, MRK_UIntMax);
414  else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
415  return InstDesc(Select, MRK_SIntMax);
416  else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
417  return InstDesc(Select, MRK_SIntMin);
418  else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
419  return InstDesc(Select, MRK_FloatMin);
420  else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
421  return InstDesc(Select, MRK_FloatMax);
422  else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
423  return InstDesc(Select, MRK_FloatMin);
424  else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
425  return InstDesc(Select, MRK_FloatMax);
426 
427  return InstDesc(false, I);
428 }
429 
432  InstDesc &Prev, bool HasFunNoNaNAttr) {
433  bool FP = I->getType()->isFloatingPointTy();
434  Instruction *UAI = Prev.getUnsafeAlgebraInst();
435  if (!UAI && FP && !I->hasUnsafeAlgebra())
436  UAI = I; // Found an unsafe (unvectorizable) algebra instruction.
437 
438  switch (I->getOpcode()) {
439  default:
440  return InstDesc(false, I);
441  case Instruction::PHI:
442  return InstDesc(I, Prev.getMinMaxKind(), Prev.getUnsafeAlgebraInst());
443  case Instruction::Sub:
444  case Instruction::Add:
445  return InstDesc(Kind == RK_IntegerAdd, I);
446  case Instruction::Mul:
447  return InstDesc(Kind == RK_IntegerMult, I);
448  case Instruction::And:
449  return InstDesc(Kind == RK_IntegerAnd, I);
450  case Instruction::Or:
451  return InstDesc(Kind == RK_IntegerOr, I);
452  case Instruction::Xor:
453  return InstDesc(Kind == RK_IntegerXor, I);
454  case Instruction::FMul:
455  return InstDesc(Kind == RK_FloatMult, I, UAI);
456  case Instruction::FSub:
457  case Instruction::FAdd:
458  return InstDesc(Kind == RK_FloatAdd, I, UAI);
459  case Instruction::FCmp:
460  case Instruction::ICmp:
461  case Instruction::Select:
462  if (Kind != RK_IntegerMinMax &&
463  (!HasFunNoNaNAttr || Kind != RK_FloatMinMax))
464  return InstDesc(false, I);
465  return isMinMaxSelectCmpPattern(I, Prev);
466  }
467 }
468 
471  unsigned NumUses = 0;
472  for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E;
473  ++Use) {
474  if (Insts.count(dyn_cast<Instruction>(*Use)))
475  ++NumUses;
476  if (NumUses > 1)
477  return true;
478  }
479 
480  return false;
481 }
483  RecurrenceDescriptor &RedDes) {
484 
485  BasicBlock *Header = TheLoop->getHeader();
486  Function &F = *Header->getParent();
487  bool HasFunNoNaNAttr =
488  F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
489 
490  if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
491  DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
492  return true;
493  }
494  if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
495  DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
496  return true;
497  }
498  if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes)) {
499  DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
500  return true;
501  }
502  if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes)) {
503  DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
504  return true;
505  }
506  if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes)) {
507  DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
508  return true;
509  }
510  if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr,
511  RedDes)) {
512  DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n");
513  return true;
514  }
515  if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
516  DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
517  return true;
518  }
519  if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
520  DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
521  return true;
522  }
523  if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes)) {
524  DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi << "\n");
525  return true;
526  }
527  // Not a reduction of known type.
528  return false;
529 }
530 
532  PHINode *Phi, Loop *TheLoop,
534 
535  // Ensure the phi node is in the loop header and has two incoming values.
536  if (Phi->getParent() != TheLoop->getHeader() ||
537  Phi->getNumIncomingValues() != 2)
538  return false;
539 
540  // Ensure the loop has a preheader and a single latch block. The loop
541  // vectorizer will need the latch to set up the next iteration of the loop.
542  auto *Preheader = TheLoop->getLoopPreheader();
543  auto *Latch = TheLoop->getLoopLatch();
544  if (!Preheader || !Latch)
545  return false;
546 
547  // Ensure the phi node's incoming blocks are the loop preheader and latch.
548  if (Phi->getBasicBlockIndex(Preheader) < 0 ||
549  Phi->getBasicBlockIndex(Latch) < 0)
550  return false;
551 
552  // Get the previous value. The previous value comes from the latch edge while
553  // the initial value comes form the preheader edge.
554  auto *Previous = dyn_cast<Instruction>(Phi->getIncomingValueForBlock(Latch));
555  if (!Previous || !TheLoop->contains(Previous) || isa<PHINode>(Previous) ||
556  SinkAfter.count(Previous)) // Cannot rely on dominance due to motion.
557  return false;
558 
559  // Ensure every user of the phi node is dominated by the previous value.
560  // The dominance requirement ensures the loop vectorizer will not need to
561  // vectorize the initial value prior to the first iteration of the loop.
562  // TODO: Consider extending this sinking to handle other kinds of instructions
563  // and expressions, beyond sinking a single cast past Previous.
564  if (Phi->hasOneUse()) {
565  auto *I = Phi->user_back();
566  if (I->isCast() && (I->getParent() == Phi->getParent()) && I->hasOneUse() &&
567  DT->dominates(Previous, I->user_back())) {
568  SinkAfter[I] = Previous;
569  return true;
570  }
571  }
572 
573  for (User *U : Phi->users())
574  if (auto *I = dyn_cast<Instruction>(U)) {
575  if (!DT->dominates(Previous, I))
576  return false;
577  }
578 
579  return true;
580 }
581 
582 /// This function returns the identity element (or neutral element) for
583 /// the operation K.
585  Type *Tp) {
586  switch (K) {
587  case RK_IntegerXor:
588  case RK_IntegerAdd:
589  case RK_IntegerOr:
590  // Adding, Xoring, Oring zero to a number does not change it.
591  return ConstantInt::get(Tp, 0);
592  case RK_IntegerMult:
593  // Multiplying a number by 1 does not change it.
594  return ConstantInt::get(Tp, 1);
595  case RK_IntegerAnd:
596  // AND-ing a number with an all-1 value does not change it.
597  return ConstantInt::get(Tp, -1, true);
598  case RK_FloatMult:
599  // Multiplying a number by 1 does not change it.
600  return ConstantFP::get(Tp, 1.0L);
601  case RK_FloatAdd:
602  // Adding zero to a number does not change it.
603  return ConstantFP::get(Tp, 0.0L);
604  default:
605  llvm_unreachable("Unknown recurrence kind");
606  }
607 }
608 
609 /// This function translates the recurrence kind to an LLVM binary operator.
611  switch (Kind) {
612  case RK_IntegerAdd:
613  return Instruction::Add;
614  case RK_IntegerMult:
615  return Instruction::Mul;
616  case RK_IntegerOr:
617  return Instruction::Or;
618  case RK_IntegerAnd:
619  return Instruction::And;
620  case RK_IntegerXor:
621  return Instruction::Xor;
622  case RK_FloatMult:
623  return Instruction::FMul;
624  case RK_FloatAdd:
625  return Instruction::FAdd;
626  case RK_IntegerMinMax:
627  return Instruction::ICmp;
628  case RK_FloatMinMax:
629  return Instruction::FCmp;
630  default:
631  llvm_unreachable("Unknown recurrence operation");
632  }
633 }
634 
637  Value *Left, Value *Right) {
639  switch (RK) {
640  default:
641  llvm_unreachable("Unknown min/max recurrence kind");
642  case MRK_UIntMin:
643  P = CmpInst::ICMP_ULT;
644  break;
645  case MRK_UIntMax:
646  P = CmpInst::ICMP_UGT;
647  break;
648  case MRK_SIntMin:
649  P = CmpInst::ICMP_SLT;
650  break;
651  case MRK_SIntMax:
652  P = CmpInst::ICMP_SGT;
653  break;
654  case MRK_FloatMin:
655  P = CmpInst::FCMP_OLT;
656  break;
657  case MRK_FloatMax:
658  P = CmpInst::FCMP_OGT;
659  break;
660  }
661 
662  // We only match FP sequences with unsafe algebra, so we can unconditionally
663  // set it on any generated instructions.
664  IRBuilder<>::FastMathFlagGuard FMFG(Builder);
665  FastMathFlags FMF;
666  FMF.setUnsafeAlgebra();
667  Builder.setFastMathFlags(FMF);
668 
669  Value *Cmp;
670  if (RK == MRK_FloatMin || RK == MRK_FloatMax)
671  Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
672  else
673  Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
674 
675  Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
676  return Select;
677 }
678 
679 InductionDescriptor::InductionDescriptor(Value *Start, InductionKind K,
680  const SCEV *Step, BinaryOperator *BOp)
681  : StartValue(Start), IK(K), Step(Step), InductionBinOp(BOp) {
682  assert(IK != IK_NoInduction && "Not an induction");
683 
684  // Start value type should match the induction kind and the value
685  // itself should not be null.
686  assert(StartValue && "StartValue is null");
687  assert((IK != IK_PtrInduction || StartValue->getType()->isPointerTy()) &&
688  "StartValue is not a pointer for pointer induction");
689  assert((IK != IK_IntInduction || StartValue->getType()->isIntegerTy()) &&
690  "StartValue is not an integer for integer induction");
691 
692  // Check the Step Value. It should be non-zero integer value.
693  assert((!getConstIntStepValue() || !getConstIntStepValue()->isZero()) &&
694  "Step value is zero");
695 
696  assert((IK != IK_PtrInduction || getConstIntStepValue()) &&
697  "Step value should be constant for pointer induction");
698  assert((IK == IK_FpInduction || Step->getType()->isIntegerTy()) &&
699  "StepValue is not an integer");
700 
701  assert((IK != IK_FpInduction || Step->getType()->isFloatingPointTy()) &&
702  "StepValue is not FP for FpInduction");
703  assert((IK != IK_FpInduction || (InductionBinOp &&
704  (InductionBinOp->getOpcode() == Instruction::FAdd ||
705  InductionBinOp->getOpcode() == Instruction::FSub))) &&
706  "Binary opcode should be specified for FP induction");
707 }
708 
710  ConstantInt *ConstStep = getConstIntStepValue();
711  if (ConstStep && (ConstStep->isOne() || ConstStep->isMinusOne()))
712  return ConstStep->getSExtValue();
713  return 0;
714 }
715 
717  if (isa<SCEVConstant>(Step))
718  return dyn_cast<ConstantInt>(cast<SCEVConstant>(Step)->getValue());
719  return nullptr;
720 }
721 
723  ScalarEvolution *SE,
724  const DataLayout& DL) const {
725 
726  SCEVExpander Exp(*SE, DL, "induction");
727  assert(Index->getType() == Step->getType() &&
728  "Index type does not match StepValue type");
729  switch (IK) {
730  case IK_IntInduction: {
731  assert(Index->getType() == StartValue->getType() &&
732  "Index type does not match StartValue type");
733 
734  // FIXME: Theoretically, we can call getAddExpr() of ScalarEvolution
735  // and calculate (Start + Index * Step) for all cases, without
736  // special handling for "isOne" and "isMinusOne".
737  // But in the real life the result code getting worse. We mix SCEV
738  // expressions and ADD/SUB operations and receive redundant
739  // intermediate values being calculated in different ways and
740  // Instcombine is unable to reduce them all.
741 
742  if (getConstIntStepValue() &&
744  return B.CreateSub(StartValue, Index);
745  if (getConstIntStepValue() &&
746  getConstIntStepValue()->isOne())
747  return B.CreateAdd(StartValue, Index);
748  const SCEV *S = SE->getAddExpr(SE->getSCEV(StartValue),
749  SE->getMulExpr(Step, SE->getSCEV(Index)));
750  return Exp.expandCodeFor(S, StartValue->getType(), &*B.GetInsertPoint());
751  }
752  case IK_PtrInduction: {
753  assert(isa<SCEVConstant>(Step) &&
754  "Expected constant step for pointer induction");
755  const SCEV *S = SE->getMulExpr(SE->getSCEV(Index), Step);
756  Index = Exp.expandCodeFor(S, Index->getType(), &*B.GetInsertPoint());
757  return B.CreateGEP(nullptr, StartValue, Index);
758  }
759  case IK_FpInduction: {
760  assert(Step->getType()->isFloatingPointTy() && "Expected FP Step value");
761  assert(InductionBinOp &&
762  (InductionBinOp->getOpcode() == Instruction::FAdd ||
763  InductionBinOp->getOpcode() == Instruction::FSub) &&
764  "Original bin op should be defined for FP induction");
765 
766  Value *StepValue = cast<SCEVUnknown>(Step)->getValue();
767 
768  // Floating point operations had to be 'fast' to enable the induction.
770  Flags.setUnsafeAlgebra();
771 
772  Value *MulExp = B.CreateFMul(StepValue, Index);
773  if (isa<Instruction>(MulExp))
774  // We have to check, the MulExp may be a constant.
775  cast<Instruction>(MulExp)->setFastMathFlags(Flags);
776 
777  Value *BOp = B.CreateBinOp(InductionBinOp->getOpcode() , StartValue,
778  MulExp, "induction");
779  if (isa<Instruction>(BOp))
780  cast<Instruction>(BOp)->setFastMathFlags(Flags);
781 
782  return BOp;
783  }
784  case IK_NoInduction:
785  return nullptr;
786  }
787  llvm_unreachable("invalid enum");
788 }
789 
791  ScalarEvolution *SE,
793 
794  // Here we only handle FP induction variables.
795  assert(Phi->getType()->isFloatingPointTy() && "Unexpected Phi type");
796 
797  if (TheLoop->getHeader() != Phi->getParent())
798  return false;
799 
800  // The loop may have multiple entrances or multiple exits; we can analyze
801  // this phi if it has a unique entry value and a unique backedge value.
802  if (Phi->getNumIncomingValues() != 2)
803  return false;
804  Value *BEValue = nullptr, *StartValue = nullptr;
805  if (TheLoop->contains(Phi->getIncomingBlock(0))) {
806  BEValue = Phi->getIncomingValue(0);
807  StartValue = Phi->getIncomingValue(1);
808  } else {
809  assert(TheLoop->contains(Phi->getIncomingBlock(1)) &&
810  "Unexpected Phi node in the loop");
811  BEValue = Phi->getIncomingValue(1);
812  StartValue = Phi->getIncomingValue(0);
813  }
814 
815  BinaryOperator *BOp = dyn_cast<BinaryOperator>(BEValue);
816  if (!BOp)
817  return false;
818 
819  Value *Addend = nullptr;
820  if (BOp->getOpcode() == Instruction::FAdd) {
821  if (BOp->getOperand(0) == Phi)
822  Addend = BOp->getOperand(1);
823  else if (BOp->getOperand(1) == Phi)
824  Addend = BOp->getOperand(0);
825  } else if (BOp->getOpcode() == Instruction::FSub)
826  if (BOp->getOperand(0) == Phi)
827  Addend = BOp->getOperand(1);
828 
829  if (!Addend)
830  return false;
831 
832  // The addend should be loop invariant
833  if (auto *I = dyn_cast<Instruction>(Addend))
834  if (TheLoop->contains(I))
835  return false;
836 
837  // FP Step has unknown SCEV
838  const SCEV *Step = SE->getUnknown(Addend);
839  D = InductionDescriptor(StartValue, IK_FpInduction, Step, BOp);
840  return true;
841 }
842 
846  bool Assume) {
847  Type *PhiTy = Phi->getType();
848 
849  // Handle integer and pointer inductions variables.
850  // Now we handle also FP induction but not trying to make a
851  // recurrent expression from the PHI node in-place.
852 
853  if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy() &&
854  !PhiTy->isFloatTy() && !PhiTy->isDoubleTy() && !PhiTy->isHalfTy())
855  return false;
856 
857  if (PhiTy->isFloatingPointTy())
858  return isFPInductionPHI(Phi, TheLoop, PSE.getSE(), D);
859 
860  const SCEV *PhiScev = PSE.getSCEV(Phi);
861  const auto *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
862 
863  // We need this expression to be an AddRecExpr.
864  if (Assume && !AR)
865  AR = PSE.getAsAddRec(Phi);
866 
867  if (!AR) {
868  DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
869  return false;
870  }
871 
872  return isInductionPHI(Phi, TheLoop, PSE.getSE(), D, AR);
873 }
874 
876  ScalarEvolution *SE,
878  const SCEV *Expr) {
879  Type *PhiTy = Phi->getType();
880  // We only handle integer and pointer inductions variables.
881  if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy())
882  return false;
883 
884  // Check that the PHI is consecutive.
885  const SCEV *PhiScev = Expr ? Expr : SE->getSCEV(Phi);
886  const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
887 
888  if (!AR) {
889  DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
890  return false;
891  }
892 
893  if (AR->getLoop() != TheLoop) {
894  // FIXME: We should treat this as a uniform. Unfortunately, we
895  // don't currently know how to handled uniform PHIs.
896  DEBUG(dbgs() << "LV: PHI is a recurrence with respect to an outer loop.\n");
897  return false;
898  }
899 
900  Value *StartValue =
902  const SCEV *Step = AR->getStepRecurrence(*SE);
903  // Calculate the pointer stride and check if it is consecutive.
904  // The stride may be a constant or a loop invariant integer value.
905  const SCEVConstant *ConstStep = dyn_cast<SCEVConstant>(Step);
906  if (!ConstStep && !SE->isLoopInvariant(Step, TheLoop))
907  return false;
908 
909  if (PhiTy->isIntegerTy()) {
910  D = InductionDescriptor(StartValue, IK_IntInduction, Step);
911  return true;
912  }
913 
914  assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
915  // Pointer induction should be a constant.
916  if (!ConstStep)
917  return false;
918 
919  ConstantInt *CV = ConstStep->getValue();
920  Type *PointerElementType = PhiTy->getPointerElementType();
921  // The pointer stride cannot be determined if the pointer element type is not
922  // sized.
923  if (!PointerElementType->isSized())
924  return false;
925 
926  const DataLayout &DL = Phi->getModule()->getDataLayout();
927  int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType));
928  if (!Size)
929  return false;
930 
931  int64_t CVSize = CV->getSExtValue();
932  if (CVSize % Size)
933  return false;
934  auto *StepValue = SE->getConstant(CV->getType(), CVSize / Size,
935  true /* signed */);
936  D = InductionDescriptor(StartValue, IK_PtrInduction, StepValue);
937  return true;
938 }
939 
941  bool PreserveLCSSA) {
942  bool Changed = false;
943 
944  // We re-use a vector for the in-loop predecesosrs.
945  SmallVector<BasicBlock *, 4> InLoopPredecessors;
946 
947  auto RewriteExit = [&](BasicBlock *BB) {
948  assert(InLoopPredecessors.empty() &&
949  "Must start with an empty predecessors list!");
950  auto Cleanup = make_scope_exit([&] { InLoopPredecessors.clear(); });
951 
952  // See if there are any non-loop predecessors of this exit block and
953  // keep track of the in-loop predecessors.
954  bool IsDedicatedExit = true;
955  for (auto *PredBB : predecessors(BB))
956  if (L->contains(PredBB)) {
957  if (isa<IndirectBrInst>(PredBB->getTerminator()))
958  // We cannot rewrite exiting edges from an indirectbr.
959  return false;
960 
961  InLoopPredecessors.push_back(PredBB);
962  } else {
963  IsDedicatedExit = false;
964  }
965 
966  assert(!InLoopPredecessors.empty() && "Must have *some* loop predecessor!");
967 
968  // Nothing to do if this is already a dedicated exit.
969  if (IsDedicatedExit)
970  return false;
971 
972  auto *NewExitBB = SplitBlockPredecessors(
973  BB, InLoopPredecessors, ".loopexit", DT, LI, PreserveLCSSA);
974 
975  if (!NewExitBB)
976  DEBUG(dbgs() << "WARNING: Can't create a dedicated exit block for loop: "
977  << *L << "\n");
978  else
979  DEBUG(dbgs() << "LoopSimplify: Creating dedicated exit block "
980  << NewExitBB->getName() << "\n");
981  return true;
982  };
983 
984  // Walk the exit blocks directly rather than building up a data structure for
985  // them, but only visit each one once.
987  for (auto *BB : L->blocks())
988  for (auto *SuccBB : successors(BB)) {
989  // We're looking for exit blocks so skip in-loop successors.
990  if (L->contains(SuccBB))
991  continue;
992 
993  // Visit each exit block exactly once.
994  if (!Visited.insert(SuccBB).second)
995  continue;
996 
997  Changed |= RewriteExit(SuccBB);
998  }
999 
1000  return Changed;
1001 }
1002 
1003 /// \brief Returns the instructions that use values defined in the loop.
1005  SmallVector<Instruction *, 8> UsedOutside;
1006 
1007  for (auto *Block : L->getBlocks())
1008  // FIXME: I believe that this could use copy_if if the Inst reference could
1009  // be adapted into a pointer.
1010  for (auto &Inst : *Block) {
1011  auto Users = Inst.users();
1012  if (any_of(Users, [&](User *U) {
1013  auto *Use = cast<Instruction>(U);
1014  return !L->contains(Use->getParent());
1015  }))
1016  UsedOutside.push_back(&Inst);
1017  }
1018 
1019  return UsedOutside;
1020 }
1021 
1023  // By definition, all loop passes need the LoopInfo analysis and the
1024  // Dominator tree it depends on. Because they all participate in the loop
1025  // pass manager, they must also preserve these.
1030 
1031  // We must also preserve LoopSimplify and LCSSA. We locally access their IDs
1032  // here because users shouldn't directly get them from this header.
1033  extern char &LoopSimplifyID;
1034  extern char &LCSSAID;
1035  AU.addRequiredID(LoopSimplifyID);
1036  AU.addPreservedID(LoopSimplifyID);
1037  AU.addRequiredID(LCSSAID);
1038  AU.addPreservedID(LCSSAID);
1039  // This is used in the LPPassManager to perform LCSSA verification on passes
1040  // which preserve lcssa form
1043 
1044  // Loop passes are designed to run inside of a loop pass manager which means
1045  // that any function analyses they require must be required by the first loop
1046  // pass in the manager (so that it is computed before the loop pass manager
1047  // runs) and preserved by all loop pasess in the manager. To make this
1048  // reasonably robust, the set needed for most loop passes is maintained here.
1049  // If your loop pass requires an analysis not listed here, you will need to
1050  // carefully audit the loop pass manager nesting structure that results.
1058 }
1059 
1060 /// Manually defined generic "LoopPass" dependency initialization. This is used
1061 /// to initialize the exact set of passes from above in \c
1062 /// getLoopAnalysisUsage. It can be used within a loop pass's initialization
1063 /// with:
1064 ///
1065 /// INITIALIZE_PASS_DEPENDENCY(LoopPass)
1066 ///
1067 /// As-if "LoopPass" were a pass.
1071  INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
1072  INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
1078 }
1079 
1080 /// \brief Find string metadata for loop
1081 ///
1082 /// If it has a value (e.g. {"llvm.distribute", 1} return the value as an
1083 /// operand or null otherwise. If the string metadata is not found return
1084 /// Optional's not-a-value.
1086  StringRef Name) {
1087  MDNode *LoopID = TheLoop->getLoopID();
1088  // Return none if LoopID is false.
1089  if (!LoopID)
1090  return None;
1091 
1092  // First operand should refer to the loop id itself.
1093  assert(LoopID->getNumOperands() > 0 && "requires at least one operand");
1094  assert(LoopID->getOperand(0) == LoopID && "invalid loop id");
1095 
1096  // Iterate over LoopID operands and look for MDString Metadata
1097  for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) {
1098  MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i));
1099  if (!MD)
1100  continue;
1101  MDString *S = dyn_cast<MDString>(MD->getOperand(0));
1102  if (!S)
1103  continue;
1104  // Return true if MDString holds expected MetaData.
1105  if (Name.equals(S->getString()))
1106  switch (MD->getNumOperands()) {
1107  case 1:
1108  return nullptr;
1109  case 2:
1110  return &MD->getOperand(1);
1111  default:
1112  llvm_unreachable("loop metadata has 0 or 1 operand");
1113  }
1114  }
1115  return None;
1116 }
1117 
1118 /// Returns true if the instruction in a loop is guaranteed to execute at least
1119 /// once.
1121  const DominatorTree *DT, const Loop *CurLoop,
1122  const LoopSafetyInfo *SafetyInfo) {
1123  // We have to check to make sure that the instruction dominates all
1124  // of the exit blocks. If it doesn't, then there is a path out of the loop
1125  // which does not execute this instruction, so we can't hoist it.
1126 
1127  // If the instruction is in the header block for the loop (which is very
1128  // common), it is always guaranteed to dominate the exit blocks. Since this
1129  // is a common case, and can save some work, check it now.
1130  if (Inst.getParent() == CurLoop->getHeader())
1131  // If there's a throw in the header block, we can't guarantee we'll reach
1132  // Inst.
1133  return !SafetyInfo->HeaderMayThrow;
1134 
1135  // Somewhere in this loop there is an instruction which may throw and make us
1136  // exit the loop.
1137  if (SafetyInfo->MayThrow)
1138  return false;
1139 
1140  // Get the exit blocks for the current loop.
1141  SmallVector<BasicBlock *, 8> ExitBlocks;
1142  CurLoop->getExitBlocks(ExitBlocks);
1143 
1144  // Verify that the block dominates each of the exit blocks of the loop.
1145  for (BasicBlock *ExitBlock : ExitBlocks)
1146  if (!DT->dominates(Inst.getParent(), ExitBlock))
1147  return false;
1148 
1149  // As a degenerate case, if the loop is statically infinite then we haven't
1150  // proven anything since there are no exit blocks.
1151  if (ExitBlocks.empty())
1152  return false;
1153 
1154  // FIXME: In general, we have to prove that the loop isn't an infinite loop.
1155  // See http::llvm.org/PR24078 . (The "ExitBlocks.empty()" check above is
1156  // just a special case of this.)
1157  return true;
1158 }
1159 
1161  // Only support loops with a unique exiting block, and a latch.
1162  if (!L->getExitingBlock())
1163  return None;
1164 
1165  // Get the branch weights for the the loop's backedge.
1166  BranchInst *LatchBR =
1167  dyn_cast<BranchInst>(L->getLoopLatch()->getTerminator());
1168  if (!LatchBR || LatchBR->getNumSuccessors() != 2)
1169  return None;
1170 
1171  assert((LatchBR->getSuccessor(0) == L->getHeader() ||
1172  LatchBR->getSuccessor(1) == L->getHeader()) &&
1173  "At least one edge out of the latch must go to the header");
1174 
1175  // To estimate the number of times the loop body was executed, we want to
1176  // know the number of times the backedge was taken, vs. the number of times
1177  // we exited the loop.
1178  uint64_t TrueVal, FalseVal;
1179  if (!LatchBR->extractProfMetadata(TrueVal, FalseVal))
1180  return None;
1181 
1182  if (!TrueVal || !FalseVal)
1183  return 0;
1184 
1185  // Divide the count of the backedge by the count of the edge exiting the loop,
1186  // rounding to nearest.
1187  if (LatchBR->getSuccessor(0) == L->getHeader())
1188  return (TrueVal + (FalseVal / 2)) / FalseVal;
1189  else
1190  return (FalseVal + (TrueVal / 2)) / TrueVal;
1191 }
1192 
1193 /// \brief Adds a 'fast' flag to floating point operations.
1195  if (isa<FPMathOperator>(V)) {
1197  Flags.setUnsafeAlgebra();
1198  cast<Instruction>(V)->setFastMathFlags(Flags);
1199  }
1200  return V;
1201 }
1202 
1203 // Helper to generate a log2 shuffle reduction.
1204 Value *
1207  ArrayRef<Value *> RedOps) {
1208  unsigned VF = Src->getType()->getVectorNumElements();
1209  // VF is a power of 2 so we can emit the reduction using log2(VF) shuffles
1210  // and vector ops, reducing the set of values being computed by half each
1211  // round.
1212  assert(isPowerOf2_32(VF) &&
1213  "Reduction emission only supported for pow2 vectors!");
1214  Value *TmpVec = Src;
1215  SmallVector<Constant *, 32> ShuffleMask(VF, nullptr);
1216  for (unsigned i = VF; i != 1; i >>= 1) {
1217  // Move the upper half of the vector to the lower half.
1218  for (unsigned j = 0; j != i / 2; ++j)
1219  ShuffleMask[j] = Builder.getInt32(i / 2 + j);
1220 
1221  // Fill the rest of the mask with undef.
1222  std::fill(&ShuffleMask[i / 2], ShuffleMask.end(),
1223  UndefValue::get(Builder.getInt32Ty()));
1224 
1225  Value *Shuf = Builder.CreateShuffleVector(
1226  TmpVec, UndefValue::get(TmpVec->getType()),
1227  ConstantVector::get(ShuffleMask), "rdx.shuf");
1228 
1229  if (Op != Instruction::ICmp && Op != Instruction::FCmp) {
1230  // Floating point operations had to be 'fast' to enable the reduction.
1231  TmpVec = addFastMathFlag(Builder.CreateBinOp((Instruction::BinaryOps)Op,
1232  TmpVec, Shuf, "bin.rdx"));
1233  } else {
1234  assert(MinMaxKind != RecurrenceDescriptor::MRK_Invalid &&
1235  "Invalid min/max");
1236  TmpVec = RecurrenceDescriptor::createMinMaxOp(Builder, MinMaxKind, TmpVec,
1237  Shuf);
1238  }
1239  if (!RedOps.empty())
1240  propagateIRFlags(TmpVec, RedOps);
1241  }
1242  // The result is in the first element of the vector.
1243  return Builder.CreateExtractElement(TmpVec, Builder.getInt32(0));
1244 }
1245 
1246 /// Create a simple vector reduction specified by an opcode and some
1247 /// flags (if generating min/max reductions).
1249  IRBuilder<> &Builder, const TargetTransformInfo *TTI, unsigned Opcode,
1251  ArrayRef<Value *> RedOps) {
1252  assert(isa<VectorType>(Src->getType()) && "Type must be a vector");
1253 
1254  Value *ScalarUdf = UndefValue::get(Src->getType()->getVectorElementType());
1255  std::function<Value*()> BuildFunc;
1256  using RD = RecurrenceDescriptor;
1257  RD::MinMaxRecurrenceKind MinMaxKind = RD::MRK_Invalid;
1258  // TODO: Support creating ordered reductions.
1259  FastMathFlags FMFUnsafe;
1260  FMFUnsafe.setUnsafeAlgebra();
1261 
1262  switch (Opcode) {
1263  case Instruction::Add:
1264  BuildFunc = [&]() { return Builder.CreateAddReduce(Src); };
1265  break;
1266  case Instruction::Mul:
1267  BuildFunc = [&]() { return Builder.CreateMulReduce(Src); };
1268  break;
1269  case Instruction::And:
1270  BuildFunc = [&]() { return Builder.CreateAndReduce(Src); };
1271  break;
1272  case Instruction::Or:
1273  BuildFunc = [&]() { return Builder.CreateOrReduce(Src); };
1274  break;
1275  case Instruction::Xor:
1276  BuildFunc = [&]() { return Builder.CreateXorReduce(Src); };
1277  break;
1278  case Instruction::FAdd:
1279  BuildFunc = [&]() {
1280  auto Rdx = Builder.CreateFAddReduce(ScalarUdf, Src);
1281  cast<CallInst>(Rdx)->setFastMathFlags(FMFUnsafe);
1282  return Rdx;
1283  };
1284  break;
1285  case Instruction::FMul:
1286  BuildFunc = [&]() {
1287  auto Rdx = Builder.CreateFMulReduce(ScalarUdf, Src);
1288  cast<CallInst>(Rdx)->setFastMathFlags(FMFUnsafe);
1289  return Rdx;
1290  };
1291  break;
1292  case Instruction::ICmp:
1293  if (Flags.IsMaxOp) {
1294  MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMax : RD::MRK_UIntMax;
1295  BuildFunc = [&]() {
1296  return Builder.CreateIntMaxReduce(Src, Flags.IsSigned);
1297  };
1298  } else {
1299  MinMaxKind = Flags.IsSigned ? RD::MRK_SIntMin : RD::MRK_UIntMin;
1300  BuildFunc = [&]() {
1301  return Builder.CreateIntMinReduce(Src, Flags.IsSigned);
1302  };
1303  }
1304  break;
1305  case Instruction::FCmp:
1306  if (Flags.IsMaxOp) {
1307  MinMaxKind = RD::MRK_FloatMax;
1308  BuildFunc = [&]() { return Builder.CreateFPMaxReduce(Src, Flags.NoNaN); };
1309  } else {
1310  MinMaxKind = RD::MRK_FloatMin;
1311  BuildFunc = [&]() { return Builder.CreateFPMinReduce(Src, Flags.NoNaN); };
1312  }
1313  break;
1314  default:
1315  llvm_unreachable("Unhandled opcode");
1316  break;
1317  }
1318  if (TTI->useReductionIntrinsic(Opcode, Src->getType(), Flags))
1319  return BuildFunc();
1320  return getShuffleReduction(Builder, Src, Opcode, MinMaxKind, RedOps);
1321 }
1322 
1323 /// Create a vector reduction using a given recurrence descriptor.
1325  const TargetTransformInfo *TTI,
1327  bool NoNaN) {
1328  // TODO: Support in-order reductions based on the recurrence descriptor.
1331  Flags.NoNaN = NoNaN;
1332  auto getSimpleRdx = [&](unsigned Opc) {
1333  return createSimpleTargetReduction(Builder, TTI, Opc, Src, Flags);
1334  };
1335  switch (RecKind) {
1337  return getSimpleRdx(Instruction::FAdd);
1339  return getSimpleRdx(Instruction::FMul);
1341  return getSimpleRdx(Instruction::Add);
1343  return getSimpleRdx(Instruction::Mul);
1345  return getSimpleRdx(Instruction::And);
1347  return getSimpleRdx(Instruction::Or);
1349  return getSimpleRdx(Instruction::Xor);
1351  switch (Desc.getMinMaxRecurrenceKind()) {
1353  Flags.IsSigned = true;
1354  Flags.IsMaxOp = true;
1355  break;
1357  Flags.IsMaxOp = true;
1358  break;
1360  Flags.IsSigned = true;
1361  break;
1363  break;
1364  default:
1365  llvm_unreachable("Unhandled MRK");
1366  }
1367  return getSimpleRdx(Instruction::ICmp);
1368  }
1370  Flags.IsMaxOp =
1372  return getSimpleRdx(Instruction::FCmp);
1373  }
1374  default:
1375  llvm_unreachable("Unhandled RecKind");
1376  }
1377 }
1378 
1380  auto *VecOp = dyn_cast<Instruction>(I);
1381  if (!VecOp)
1382  return;
1383  auto *Intersection = (OpValue == nullptr) ? dyn_cast<Instruction>(VL[0])
1384  : dyn_cast<Instruction>(OpValue);
1385  if (!Intersection)
1386  return;
1387  const unsigned Opcode = Intersection->getOpcode();
1388  VecOp->copyIRFlags(Intersection);
1389  for (auto *V : VL) {
1390  auto *Instr = dyn_cast<Instruction>(V);
1391  if (!Instr)
1392  continue;
1393  if (OpValue == nullptr || Opcode == Instr->getOpcode())
1394  VecOp->andIRFlags(V);
1395  }
1396 }
Legacy wrapper pass to provide the GlobalsAAResult object.
MachineLoop * L
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
Bitwise or logical XOR of numbers.
Definition: LoopUtils.h:81
const NoneType None
Definition: None.h:24
static bool isArithmeticRecurrenceKind(RecurrenceKind Kind)
Returns true if the recurrence kind is an arithmetic kind.
Definition: LoopUtils.cpp:67
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:172
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:72
Value * CreateICmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="")
Definition: IRBuilder.h:1634
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
BasicBlock::iterator GetInsertPoint() const
Definition: IRBuilder.h:123
Value * CreateBinOp(Instruction::BinaryOps Opc, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1108
static bool isFPInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D)
Returns true if Phi is a floating point induction in the loop L.
Definition: LoopUtils.cpp:790
InductionDescriptor()=default
Default constructor - creates an invalid induction.
const SCEV * getConstant(ConstantInt *V)
This is the interface for a simple mod/ref and alias analysis over globals.
Min/max implemented in terms of select(cmp()).
Definition: LoopUtils.h:82
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1073
The main scalar evolution driver.
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
DataLayout DL
int getConsecutiveDirection() const
Get the consecutive direction.
Definition: LoopUtils.cpp:709
A global registry used in conjunction with static constructors to make pluggable components (like tar...
Definition: Registry.h:45
unsigned less than
Definition: InstrTypes.h:885
static bool isReductionPHI(PHINode *Phi, Loop *TheLoop, RecurrenceDescriptor &RedDes)
Returns true if Phi is a reduction in TheLoop.
Definition: LoopUtils.cpp:482
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:866
const SCEV * getStepRecurrence(ScalarEvolution &SE) const
Constructs and returns the recurrence indicating how much this expression steps by.
bool isDoubleTy() const
Return true if this is 'double', a 64-bit IEEE fp type.
Definition: Type.h:150
LLVM_NODISCARD detail::scope_exit< typename std::decay< Callable >::type > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:47
bool isLoopInvariant(const SCEV *S, const Loop *L)
Return true if the value of the given SCEV is unchanging in the specified loop.
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
MinMaxRecurrenceKind getMinMaxRecurrenceKind()
Definition: LoopUtils.h:198
Metadata node.
Definition: Metadata.h:862
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
Definition: Function.h:262
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
iv Induction Variable Users
Definition: IVUsers.cpp:51
static InstDesc isRecurrenceInstr(Instruction *I, RecurrenceKind Kind, InstDesc &Prev, bool HasFunNoNaNAttr)
Returns a struct describing if the instruction 'I' can be a recurrence variable of type 'Kind'...
Definition: LoopUtils.cpp:431
const std::vector< BlockT * > & getBlocks() const
Get a list of the basic blocks which make up this loop.
Definition: LoopInfo.h:140
bool isMinusOne() const
This function will return true iff every bit in this constant is set to true.
Definition: Constants.h:209
op_iterator op_begin()
Definition: User.h:214
BlockT * getHeader() const
Definition: LoopInfo.h:103
static Instruction * lookThroughAnd(PHINode *Phi, Type *&RT, SmallPtrSetImpl< Instruction * > &Visited, SmallPtrSetImpl< Instruction * > &CI)
Determines if Phi may have been type-promoted.
Definition: LoopUtils.cpp:81
Type * getPointerElementType() const
Definition: Type.h:373
static bool isFirstOrderRecurrence(PHINode *Phi, Loop *TheLoop, DenseMap< Instruction *, Instruction * > &SinkAfter, DominatorTree *DT)
Returns true if Phi is a first-order recurrence.
Definition: LoopUtils.cpp:531
BlockT * getLoopLatch() const
If there is a single latch block for this loop, return it.
Definition: LoopInfoImpl.h:149
IntegerType * getInt32Ty()
Fetch the type representing a 32-bit integer.
Definition: IRBuilder.h:348
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:51
This is the interface for a SCEV-based alias analysis.
void initializeLoopPassPass(PassRegistry &)
Manually defined generic "LoopPass" dependency initialization.
Definition: LoopUtils.cpp:1068
This class represents the LLVM 'select' instruction.
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:560
CallInst * CreateFAddReduce(Value *Acc, Value *Src)
Create a vector fadd reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:204
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
Value * createSimpleTargetReduction(IRBuilder<> &B, const TargetTransformInfo *TTI, unsigned Opcode, Value *Src, TargetTransformInfo::ReductionFlags Flags=TargetTransformInfo::ReductionFlags(), ArrayRef< Value * > RedOps=ArrayRef< Value * >())
Create a target reduction of the given vector.
Definition: LoopUtils.cpp:1248
static Constant * get(ArrayRef< Constant * > V)
Definition: Constants.cpp:984
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition: IRBuilder.h:664
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:169
static InstDesc isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev)
Returns a struct describing if the instruction if the instruction is a Select(ICmp(X, Y), X, Y) instruction pattern corresponding to a min(X, Y) or max(X, Y).
Definition: LoopUtils.cpp:382
Value * CreateAdd(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:889
MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an 'unordered' floating point minimum function.
ConstantInt * getConstIntStepValue() const
Definition: LoopUtils.cpp:716
SmallVector< Instruction *, 8 > findDefsUsedOutsideOfLoop(Loop *L)
Returns the instructions that use values defined in the loop.
Definition: LoopUtils.cpp:1004
Type * getVectorElementType() const
Definition: Type.h:368
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
void getExitBlocks(SmallVectorImpl< BlockT * > &ExitBlocks) const
Return all of the successor blocks of this loop.
Definition: LoopInfoImpl.h:62
CallInst * CreateFPMinReduce(Value *Src, bool NoNaN=false)
Create a vector float min reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:272
#define F(x, y, z)
Definition: MD5.cpp:55
Value * transform(IRBuilder<> &B, Value *Index, ScalarEvolution *SE, const DataLayout &DL) const
Compute the transformed value of Index at offset StartValue using step StepValue. ...
Definition: LoopUtils.cpp:722
This node represents a polynomial recurrence on the trip count of the specified loop.
bool isHalfTy() const
Return true if this is 'half', a 16-bit IEEE fp type.
Definition: Type.h:144
This instruction compares its operands according to the predicate given to the constructor.
void andIRFlags(const Value *V)
Logical 'and' of any supported wrapping, exact, and fast-math flags of V and this instruction...
BasicBlock * getSuccessor(unsigned i) const
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
bool hasUnsafeAlgebra() const
Determine whether the unsafe-algebra flag is set.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
static bool areAllUsesIn(Instruction *I, SmallPtrSetImpl< Instruction * > &Set)
Returns true if all uses of the instruction I is within the Set.
Definition: LoopUtils.cpp:40
const SCEV * getMulExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical multiply expression, or something simpler if possible.
const SCEVAddRecExpr * getAsAddRec(Value *V)
Attempts to produce an AddRecExpr for V by adding additional SCEV predicates.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
void propagateIRFlags(Value *I, ArrayRef< Value * > VL, Value *OpValue=nullptr)
Get the intersection (logical and) of all of the potential IR flags of each scalar operation (VL) tha...
Definition: LoopUtils.cpp:1379
bool isFloatingPointTy() const
Return true if this is one of the six floating-point types.
Definition: Type.h:162
AnalysisUsage & addPreservedID(const void *ID)
Value * CreateSub(Value *LHS, Value *RHS, const Twine &Name="", bool HasNUW=false, bool HasNSW=false)
Definition: IRBuilder.h:911
Not an induction variable.
Definition: LoopUtils.h:274
This POD struct holds information about a potential recurrence operation.
Definition: LoopUtils.h:110
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree...
Definition: Dominators.h:134
Bitwise or logical AND of numbers.
Definition: LoopUtils.h:80
const char * Name
static bool hasMultipleUsesOf(Instruction *I, SmallPtrSetImpl< Instruction * > &Insts)
Returns true if instruction I has multiple uses in Insts.
Definition: LoopUtils.cpp:469
unsigned getNumIncomingValues() const
Return the number of incoming edges.
iterator_range< block_iterator > blocks() const
Definition: LoopInfo.h:144
Pointer induction var. Step = C / sizeof(elem).
Definition: LoopUtils.h:276
const SCEV * getSCEV(Value *V)
Returns the SCEV expression of V, in the context of the current SCEV predicate.
CallInst * CreateXorReduce(Value *Src)
Create a vector int XOR reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:244
static bool AddReductionVar(PHINode *Phi, RecurrenceKind Kind, Loop *TheLoop, bool HasFunNoNaNAttr, RecurrenceDescriptor &RedDes)
Returns true if Phi is a reduction of type Kind and adds it to the RecurrenceDescriptor.
Definition: LoopUtils.cpp:165
Value * CreateFCmp(CmpInst::Predicate P, Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:1641
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:262
#define P(N)
Integer induction variable. Step = C.
Definition: LoopUtils.h:275
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
* if(!EatIfPresent(lltok::kw_thread_local)) return false
ParseOptionalThreadLocal := /*empty.
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt...
Definition: PatternMatch.h:240
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
Definition: LoopInfoImpl.h:101
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition: MathExtras.h:421
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
Flags describing the kind of vector reduction.
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
Type * getType() const
Return the LLVM type of this SCEV expression.
Conditional or Unconditional Branch instruction.
Min/max implemented in terms of select(cmp()).
Definition: LoopUtils.h:85
This is an important base class in LLVM.
Definition: Constant.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
Definition: SmallVector.h:115
Value * CreateSelect(Value *C, Value *True, Value *False, const Twine &Name="", Instruction *MDFrom=nullptr)
Definition: IRBuilder.h:1689
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition: Type.h:147
char & LCSSAID
Definition: LCSSA.cpp:412
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:372
bool useReductionIntrinsic(unsigned Opcode, Type *Ty, ReductionFlags Flags) const
bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const
Retrieve the raw weight values of a conditional branch or select.
Definition: Metadata.cpp:1303
MDNode * getLoopID() const
Return the llvm.loop loop id metadata node for this loop if it is present.
Definition: LoopInfo.cpp:212
void setUnsafeAlgebra()
Definition: Operator.h:204
Represent the analysis usage information of a pass.
op_iterator op_end()
Definition: User.h:216
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
bool contains(const LoopT *L) const
Return true if the specified loop is contained within in this loop.
Definition: LoopInfo.h:110
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:825
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:860
Value * expandCodeFor(const SCEV *SH, Type *Ty, Instruction *I)
Insert code to directly compute the specified SCEV expression into the program.
BlockT * getExitingBlock() const
If getExitingBlocks would return exactly one block, return that block.
Definition: LoopInfoImpl.h:49
Value * getOperand(unsigned i) const
Definition: User.h:154
op_range operands()
Definition: User.h:222
bool isCommutative() const
Return true if the instruction is commutative:
Definition: Instruction.h:416
Optional< unsigned > getLoopEstimatedTripCount(Loop *L)
Get a loop's estimated trip count based on branch weight metadata.
Definition: LoopUtils.cpp:1160
Type * getSrcTy() const
Return the source type, as a convenience.
Definition: InstrTypes.h:825
Value * CreateExtractElement(Value *Vec, Value *Idx, const Twine &Name="")
Definition: IRBuilder.h:1709
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:144
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:221
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1320
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:697
bool IsMaxOp
If the op a min/max kind, true if it's a max operation.
RecurrenceKind getRecurrenceKind()
Definition: LoopUtils.h:196
Optional< const MDOperand * > findStringMetadataForLoop(Loop *TheLoop, StringRef Name)
Find string metadata for loop.
Definition: LoopUtils.cpp:1085
static unsigned getRecurrenceBinOp(RecurrenceKind Kind)
Returns the opcode of binary operation corresponding to the RecurrenceKind.
Definition: LoopUtils.cpp:610
signed greater than
Definition: InstrTypes.h:887
char & LoopSimplifyID
bool dominates(const Instruction *Def, const Use &U) const
Return true if Def dominates a use in User.
Definition: Dominators.cpp:234
The RecurrenceDescriptor is used to identify recurrences variables in a loop.
Definition: LoopUtils.h:72
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:864
BinaryOps getOpcode() const
Definition: InstrTypes.h:523
StringRef getString() const
Definition: Metadata.cpp:456
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:240
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1067
MinMaxRecurrenceKind getMinMaxKind()
Definition: LoopUtils.h:126
CallInst * CreateAddReduce(Value *Src)
Create a vector int add reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:224
#define E
Definition: LargeTest.cpp:27
Legacy wrapper pass to provide the SCEVAAResult object.
This is the shared class of boolean and integer constants.
Definition: Constants.h:84
A struct for saving information about induction variables.
Definition: LoopUtils.h:270
#define B
Definition: LargeTest.cpp:24
Bitwise or logical OR of numbers.
Definition: LoopUtils.h:79
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
CallInst * CreateIntMaxReduce(Value *Src, bool IsSigned=false)
Create a vector integer max reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:249
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
uint64_t getTypeAllocSize(Type *Ty) const
Returns the offset in bytes between successive objects of the specified type, including alignment pad...
Definition: DataLayout.h:405
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:291
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:57
static bool getSourceExtensionKind(Instruction *Start, Instruction *Exit, Type *RT, bool &IsSigned, SmallPtrSetImpl< Instruction * > &Visited, SmallPtrSetImpl< Instruction * > &CI)
Returns true if all the source operands of a recurrence are either SExtInsts or ZExtInsts.
Definition: LoopUtils.cpp:104
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
Definition: Instruction.h:63
static bool isFloatingPointRecurrenceKind(RecurrenceKind Kind)
Returns true if the recurrence kind is a floating point kind.
Definition: LoopUtils.cpp:63
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:383
An interface layer with SCEV used to manage how we see SCEV expressions for values in the context of ...
signed less than
Definition: InstrTypes.h:889
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
ConstantInt * getInt32(uint32_t C)
Get a constant 32-bit value.
Definition: IRBuilder.h:308
Value * CreateShuffleVector(Value *V1, Value *V2, Value *Mask, const Twine &Name="")
Definition: IRBuilder.h:1736
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:560
pred_range predecessors(BasicBlock *BB)
Definition: CFG.h:110
ConstantInt * getValue() const
static Constant * get(Type *Ty, double V)
This returns a ConstantFP, or a vector containing a splat of a ConstantFP, for the specified value in...
Definition: Constants.cpp:623
CallInst * CreateFMulReduce(Value *Acc, Value *Src)
Create a vector fmul reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:214
Value * createTargetReduction(IRBuilder<> &B, const TargetTransformInfo *TTI, RecurrenceDescriptor &Desc, Value *Src, bool NoNaN=false)
Create a generic target reduction using a recurrence descriptor Desc The target is queried to determi...
Definition: LoopUtils.cpp:1324
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
static Constant * getRecurrenceIdentity(RecurrenceKind K, Type *Tp)
Returns identity corresponding to the RecurrenceKind.
Definition: LoopUtils.cpp:584
Value * CreateGEP(Value *Ptr, ArrayRef< Value * > IdxList, const Twine &Name="")
Definition: IRBuilder.h:1223
Class for arbitrary precision integers.
Definition: APInt.h:69
Value * getIncomingValueForBlock(const BasicBlock *BB) const
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:197
iterator_range< user_iterator > users()
Definition: Value.h:395
unsigned getVectorNumElements() const
Definition: DerivedTypes.h:462
static Value * createMinMaxOp(IRBuilder<> &Builder, MinMaxRecurrenceKind RK, Value *Left, Value *Right)
Returns a Min/Max operation corresponding to MinMaxRecurrenceKind.
Definition: LoopUtils.cpp:635
This class uses information about analyze scalars to rewrite expressions in canonical form...
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
Definition: DenseMap.h:126
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:395
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
llvm_unreachable("Unknown RegBank!")
use_iterator use_begin()
Definition: Value.h:334
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
CallInst * CreateAndReduce(Value *Src)
Create a vector int AND reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:234
static bool isZero(Value *V, const DataLayout &DL, DominatorTree *DT, AssumptionCache *AC)
Definition: Lint.cpp:529
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.cpp:361
CallInst * CreateOrReduce(Value *Src)
Create a vector int OR reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:239
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Definition: SmallVector.h:119
CallInst * CreateIntMinReduce(Value *Src, bool IsSigned=false)
Create a vector integer min reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:255
Captures loop safety information.
Definition: LoopUtils.h:50
This class represents an analyzed expression in the program.
unsigned getNumSuccessors() const
static bool isIntegerRecurrenceKind(RecurrenceKind Kind)
Returns true if the recurrence kind is an integer kind.
Definition: LoopUtils.cpp:48
Floating point induction variable.
Definition: LoopUtils.h:277
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:360
ScalarEvolution * getSE() const
Returns the ScalarEvolution analysis used.
#define I(x, y, z)
Definition: MD5.cpp:58
bool hasOneUse() const
Return true if there is exactly one user of this value.
Definition: Value.h:408
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
void getLoopAnalysisUsage(AnalysisUsage &AU)
Helper to consistently add the set of standard passes to a loop pass's AnalysisUsage.
Definition: LoopUtils.cpp:1022
CallInst * CreateMulReduce(Value *Src)
Create a vector int mul reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:229
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
const Loop * getLoop() const
BasicBlock * SplitBlockPredecessors(BasicBlock *BB, ArrayRef< BasicBlock * > Preds, const char *Suffix, DominatorTree *DT=nullptr, LoopInfo *LI=nullptr, bool PreserveLCSSA=false)
This method introduces at least one new basic block into the function and moves some of the predecess...
const unsigned Kind
StringRef getValueAsString() const
Return the attribute's value as a string.
Definition: Attributes.cpp:195
bool use_empty() const
Definition: Value.h:322
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
Definition: Value.h:371
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
unsigned getPrimitiveSizeInBits() const LLVM_READONLY
Return the basic size of this type if it is a primitive type.
Definition: Type.cpp:115
LLVM Value Representation.
Definition: Value.h:73
succ_range successors(BasicBlock *BB)
Definition: CFG.h:143
const SCEV * getSCEV(Value *V)
Return a SCEV expression for the full generality of the specified expression.
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:121
void setFastMathFlags(FastMathFlags NewFMF)
Set the fast-math flags to be used with generated fp-math operators.
Definition: IRBuilder.h:221
bool formDedicatedExitBlocks(Loop *L, DominatorTree *DT, LoopInfo *LI, bool PreserveLCSSA)
Ensure that all exit blocks of the loop are dedicated exits.
Definition: LoopUtils.cpp:940
#define DEBUG(X)
Definition: Debug.h:118
const SCEV * getUnknown(Value *V)
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:845
Convenience struct for specifying and reasoning about fast-math flags.
Definition: Operator.h:160
unsigned greater than
Definition: InstrTypes.h:883
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
This is the interface for LLVM's primary stateless and local alias analysis.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:40
A single uniqued string.
Definition: Metadata.h:602
Value * getShuffleReduction(IRBuilder<> &Builder, Value *Src, unsigned Op, RecurrenceDescriptor::MinMaxRecurrenceKind MinMaxKind=RecurrenceDescriptor::MRK_Invalid, ArrayRef< Value * > RedOps=ArrayRef< Value * >())
Generates a vector reduction using shufflevectors to reduce the value.
Definition: LoopUtils.cpp:1205
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:261
This pass exposes codegen information to IR-level passes.
const SCEV * getAddExpr(SmallVectorImpl< const SCEV * > &Ops, SCEV::NoWrapFlags Flags=SCEV::FlagAnyWrap, unsigned Depth=0)
Get a canonical add expression, or something simpler if possible.
A wrapper pass to provide the legacy pass manager access to a suitably prepared AAResults object...
CallInst * CreateFPMaxReduce(Value *Src, bool NoNaN=false)
Create a vector float max reduction intrinsic of the source vector.
Definition: IRBuilder.cpp:261
Value * CreateFMul(Value *LHS, Value *RHS, const Twine &Name="", MDNode *FPMathTag=nullptr)
Definition: IRBuilder.h:947
int64_t getSExtValue() const
Return the constant as a 64-bit integer value after it has been sign extended as appropriate for the ...
Definition: Constants.h:157
bool isGuaranteedToExecute(const Instruction &Inst, const DominatorTree *DT, const Loop *CurLoop, const LoopSafetyInfo *SafetyInfo)
Returns true if the instruction in a loop is guaranteed to execute at least once. ...
Definition: LoopUtils.cpp:1120
static Value * addFastMathFlag(Value *V)
Adds a 'fast' flag to floating point operations.
Definition: LoopUtils.cpp:1194
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
bool NoNaN
If op is an fp min/max, whether NaNs may be present.
const BasicBlock * getParent() const
Definition: Instruction.h:66
#define D
Definition: LargeTest.cpp:26
static bool isInductionPHI(PHINode *Phi, const Loop *L, ScalarEvolution *SE, InductionDescriptor &D, const SCEV *Expr=nullptr)
Returns true if Phi is an induction in the loop L.
Definition: LoopUtils.cpp:875
bool isOne() const
This is just a convenience method to make client code smaller for a common case.
Definition: Constants.h:201
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
Definition: PatternMatch.h:335
RecurrenceKind
This enum represents the kinds of recurrences that we support.
Definition: LoopUtils.h:75
This class represents a constant integer value.
Legacy wrapper pass to provide the BasicAAResult object.
bool IsSigned
Whether the operation is a signed int reduction.
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:872