blob: dae19d23db6175024f7a9c72783fa937409ccbb3 [file] [log] [blame]
Karthik Bhat76aa6622015-04-20 04:38:33 +00001//===-- 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
14#include "llvm/Analysis/LoopInfo.h"
15#include "llvm/IR/Instructions.h"
16#include "llvm/IR/PatternMatch.h"
17#include "llvm/IR/ValueHandle.h"
18#include "llvm/Support/Debug.h"
Karthik Bhat24e6cc22015-04-23 08:29:20 +000019#include "llvm/Analysis/ScalarEvolution.h"
20#include "llvm/Analysis/ScalarEvolutionExpressions.h"
21#include "llvm/IR/Module.h"
Karthik Bhat76aa6622015-04-20 04:38:33 +000022#include "llvm/Transforms/Utils/LoopUtils.h"
23
24using namespace llvm;
25using namespace llvm::PatternMatch;
26
27#define DEBUG_TYPE "loop-utils"
28
Tyler Nowicki0a913102015-06-16 18:07:34 +000029bool RecurrenceDescriptor::areAllUsesIn(Instruction *I,
30 SmallPtrSetImpl<Instruction *> &Set) {
Karthik Bhat76aa6622015-04-20 04:38:33 +000031 for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E; ++Use)
32 if (!Set.count(dyn_cast<Instruction>(*Use)))
33 return false;
34 return true;
35}
36
Tyler Nowicki0a913102015-06-16 18:07:34 +000037bool RecurrenceDescriptor::AddReductionVar(PHINode *Phi, RecurrenceKind Kind,
38 Loop *TheLoop, bool HasFunNoNaNAttr,
39 RecurrenceDescriptor &RedDes) {
Karthik Bhat76aa6622015-04-20 04:38:33 +000040 if (Phi->getNumIncomingValues() != 2)
41 return false;
42
43 // Reduction variables are only found in the loop header block.
44 if (Phi->getParent() != TheLoop->getHeader())
45 return false;
46
47 // Obtain the reduction start value from the value that comes from the loop
48 // preheader.
49 Value *RdxStart = Phi->getIncomingValueForBlock(TheLoop->getLoopPreheader());
50
51 // ExitInstruction is the single value which is used outside the loop.
52 // We only allow for a single reduction value to be used outside the loop.
53 // This includes users of the reduction, variables (which form a cycle
54 // which ends in the phi node).
55 Instruction *ExitInstruction = nullptr;
56 // Indicates that we found a reduction operation in our scan.
57 bool FoundReduxOp = false;
58
59 // We start with the PHI node and scan for all of the users of this
60 // instruction. All users must be instructions that can be used as reduction
61 // variables (such as ADD). We must have a single out-of-block user. The cycle
62 // must include the original PHI.
63 bool FoundStartPHI = false;
64
65 // To recognize min/max patterns formed by a icmp select sequence, we store
66 // the number of instruction we saw from the recognized min/max pattern,
67 // to make sure we only see exactly the two instructions.
68 unsigned NumCmpSelectPatternInst = 0;
Tyler Nowicki27b2c392015-06-16 22:59:45 +000069 InstDesc ReduxDesc(false, nullptr);
Karthik Bhat76aa6622015-04-20 04:38:33 +000070
71 SmallPtrSet<Instruction *, 8> VisitedInsts;
72 SmallVector<Instruction *, 8> Worklist;
73 Worklist.push_back(Phi);
74 VisitedInsts.insert(Phi);
75
76 // A value in the reduction can be used:
77 // - By the reduction:
78 // - Reduction operation:
79 // - One use of reduction value (safe).
80 // - Multiple use of reduction value (not safe).
81 // - PHI:
82 // - All uses of the PHI must be the reduction (safe).
83 // - Otherwise, not safe.
84 // - By one instruction outside of the loop (safe).
85 // - By further instructions outside of the loop (not safe).
86 // - By an instruction that is not part of the reduction (not safe).
87 // This is either:
88 // * An instruction type other than PHI or the reduction operation.
89 // * A PHI in the header other than the initial PHI.
90 while (!Worklist.empty()) {
91 Instruction *Cur = Worklist.back();
92 Worklist.pop_back();
93
94 // No Users.
95 // If the instruction has no users then this is a broken chain and can't be
96 // a reduction variable.
97 if (Cur->use_empty())
98 return false;
99
100 bool IsAPhi = isa<PHINode>(Cur);
101
102 // A header PHI use other than the original PHI.
103 if (Cur != Phi && IsAPhi && Cur->getParent() == Phi->getParent())
104 return false;
105
106 // Reductions of instructions such as Div, and Sub is only possible if the
107 // LHS is the reduction variable.
108 if (!Cur->isCommutative() && !IsAPhi && !isa<SelectInst>(Cur) &&
109 !isa<ICmpInst>(Cur) && !isa<FCmpInst>(Cur) &&
110 !VisitedInsts.count(dyn_cast<Instruction>(Cur->getOperand(0))))
111 return false;
112
113 // Any reduction instruction must be of one of the allowed kinds.
Tyler Nowicki0a913102015-06-16 18:07:34 +0000114 ReduxDesc = isRecurrenceInstr(Cur, Kind, ReduxDesc, HasFunNoNaNAttr);
115 if (!ReduxDesc.isRecurrence())
Karthik Bhat76aa6622015-04-20 04:38:33 +0000116 return false;
117
118 // A reduction operation must only have one use of the reduction value.
119 if (!IsAPhi && Kind != RK_IntegerMinMax && Kind != RK_FloatMinMax &&
120 hasMultipleUsesOf(Cur, VisitedInsts))
121 return false;
122
123 // All inputs to a PHI node must be a reduction value.
124 if (IsAPhi && Cur != Phi && !areAllUsesIn(Cur, VisitedInsts))
125 return false;
126
127 if (Kind == RK_IntegerMinMax &&
128 (isa<ICmpInst>(Cur) || isa<SelectInst>(Cur)))
129 ++NumCmpSelectPatternInst;
130 if (Kind == RK_FloatMinMax && (isa<FCmpInst>(Cur) || isa<SelectInst>(Cur)))
131 ++NumCmpSelectPatternInst;
132
133 // Check whether we found a reduction operator.
134 FoundReduxOp |= !IsAPhi;
135
136 // Process users of current instruction. Push non-PHI nodes after PHI nodes
137 // onto the stack. This way we are going to have seen all inputs to PHI
138 // nodes once we get to them.
139 SmallVector<Instruction *, 8> NonPHIs;
140 SmallVector<Instruction *, 8> PHIs;
141 for (User *U : Cur->users()) {
142 Instruction *UI = cast<Instruction>(U);
143
144 // Check if we found the exit user.
145 BasicBlock *Parent = UI->getParent();
146 if (!TheLoop->contains(Parent)) {
147 // Exit if you find multiple outside users or if the header phi node is
148 // being used. In this case the user uses the value of the previous
149 // iteration, in which case we would loose "VF-1" iterations of the
150 // reduction operation if we vectorize.
151 if (ExitInstruction != nullptr || Cur == Phi)
152 return false;
153
154 // The instruction used by an outside user must be the last instruction
155 // before we feed back to the reduction phi. Otherwise, we loose VF-1
156 // operations on the value.
157 if (std::find(Phi->op_begin(), Phi->op_end(), Cur) == Phi->op_end())
158 return false;
159
160 ExitInstruction = Cur;
161 continue;
162 }
163
164 // Process instructions only once (termination). Each reduction cycle
165 // value must only be used once, except by phi nodes and min/max
166 // reductions which are represented as a cmp followed by a select.
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000167 InstDesc IgnoredVal(false, nullptr);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000168 if (VisitedInsts.insert(UI).second) {
169 if (isa<PHINode>(UI))
170 PHIs.push_back(UI);
171 else
172 NonPHIs.push_back(UI);
173 } else if (!isa<PHINode>(UI) &&
174 ((!isa<FCmpInst>(UI) && !isa<ICmpInst>(UI) &&
175 !isa<SelectInst>(UI)) ||
Tyler Nowicki0a913102015-06-16 18:07:34 +0000176 !isMinMaxSelectCmpPattern(UI, IgnoredVal).isRecurrence()))
Karthik Bhat76aa6622015-04-20 04:38:33 +0000177 return false;
178
179 // Remember that we completed the cycle.
180 if (UI == Phi)
181 FoundStartPHI = true;
182 }
183 Worklist.append(PHIs.begin(), PHIs.end());
184 Worklist.append(NonPHIs.begin(), NonPHIs.end());
185 }
186
187 // This means we have seen one but not the other instruction of the
188 // pattern or more than just a select and cmp.
189 if ((Kind == RK_IntegerMinMax || Kind == RK_FloatMinMax) &&
190 NumCmpSelectPatternInst != 2)
191 return false;
192
193 if (!FoundStartPHI || !FoundReduxOp || !ExitInstruction)
194 return false;
195
196 // We found a reduction var if we have reached the original phi node and we
197 // only have a single instruction with out-of-loop users.
198
199 // The ExitInstruction(Instruction which is allowed to have out-of-loop users)
Tyler Nowicki0a913102015-06-16 18:07:34 +0000200 // is saved as part of the RecurrenceDescriptor.
Karthik Bhat76aa6622015-04-20 04:38:33 +0000201
202 // Save the description of this reduction variable.
Tyler Nowicki0a913102015-06-16 18:07:34 +0000203 RecurrenceDescriptor RD(RdxStart, ExitInstruction, Kind,
Tyler Nowickic1a86f52015-08-10 19:51:46 +0000204 ReduxDesc.getMinMaxKind(),
205 ReduxDesc.getUnsafeAlgebraInst());
Karthik Bhat76aa6622015-04-20 04:38:33 +0000206
207 RedDes = RD;
208
209 return true;
210}
211
212/// Returns true if the instruction is a Select(ICmp(X, Y), X, Y) instruction
213/// pattern corresponding to a min(X, Y) or max(X, Y).
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000214RecurrenceDescriptor::InstDesc
215RecurrenceDescriptor::isMinMaxSelectCmpPattern(Instruction *I, InstDesc &Prev) {
Karthik Bhat76aa6622015-04-20 04:38:33 +0000216
217 assert((isa<ICmpInst>(I) || isa<FCmpInst>(I) || isa<SelectInst>(I)) &&
218 "Expect a select instruction");
219 Instruction *Cmp = nullptr;
220 SelectInst *Select = nullptr;
221
222 // We must handle the select(cmp()) as a single instruction. Advance to the
223 // select.
224 if ((Cmp = dyn_cast<ICmpInst>(I)) || (Cmp = dyn_cast<FCmpInst>(I))) {
225 if (!Cmp->hasOneUse() || !(Select = dyn_cast<SelectInst>(*I->user_begin())))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000226 return InstDesc(false, I);
227 return InstDesc(Select, Prev.getMinMaxKind());
Karthik Bhat76aa6622015-04-20 04:38:33 +0000228 }
229
230 // Only handle single use cases for now.
231 if (!(Select = dyn_cast<SelectInst>(I)))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000232 return InstDesc(false, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000233 if (!(Cmp = dyn_cast<ICmpInst>(I->getOperand(0))) &&
234 !(Cmp = dyn_cast<FCmpInst>(I->getOperand(0))))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000235 return InstDesc(false, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000236 if (!Cmp->hasOneUse())
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000237 return InstDesc(false, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000238
239 Value *CmpLeft;
240 Value *CmpRight;
241
242 // Look for a min/max pattern.
243 if (m_UMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000244 return InstDesc(Select, MRK_UIntMin);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000245 else if (m_UMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000246 return InstDesc(Select, MRK_UIntMax);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000247 else if (m_SMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000248 return InstDesc(Select, MRK_SIntMax);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000249 else if (m_SMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000250 return InstDesc(Select, MRK_SIntMin);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000251 else if (m_OrdFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000252 return InstDesc(Select, MRK_FloatMin);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000253 else if (m_OrdFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000254 return InstDesc(Select, MRK_FloatMax);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000255 else if (m_UnordFMin(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000256 return InstDesc(Select, MRK_FloatMin);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000257 else if (m_UnordFMax(m_Value(CmpLeft), m_Value(CmpRight)).match(Select))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000258 return InstDesc(Select, MRK_FloatMax);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000259
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000260 return InstDesc(false, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000261}
262
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000263RecurrenceDescriptor::InstDesc
Tyler Nowicki0a913102015-06-16 18:07:34 +0000264RecurrenceDescriptor::isRecurrenceInstr(Instruction *I, RecurrenceKind Kind,
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000265 InstDesc &Prev, bool HasFunNoNaNAttr) {
Karthik Bhat76aa6622015-04-20 04:38:33 +0000266 bool FP = I->getType()->isFloatingPointTy();
Tyler Nowickic1a86f52015-08-10 19:51:46 +0000267 Instruction *UAI = Prev.getUnsafeAlgebraInst();
268 if (!UAI && FP && !I->hasUnsafeAlgebra())
269 UAI = I; // Found an unsafe (unvectorizable) algebra instruction.
270
Karthik Bhat76aa6622015-04-20 04:38:33 +0000271 switch (I->getOpcode()) {
272 default:
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000273 return InstDesc(false, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000274 case Instruction::PHI:
275 if (FP &&
276 (Kind != RK_FloatMult && Kind != RK_FloatAdd && Kind != RK_FloatMinMax))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000277 return InstDesc(false, I);
278 return InstDesc(I, Prev.getMinMaxKind());
Karthik Bhat76aa6622015-04-20 04:38:33 +0000279 case Instruction::Sub:
280 case Instruction::Add:
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000281 return InstDesc(Kind == RK_IntegerAdd, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000282 case Instruction::Mul:
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000283 return InstDesc(Kind == RK_IntegerMult, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000284 case Instruction::And:
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000285 return InstDesc(Kind == RK_IntegerAnd, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000286 case Instruction::Or:
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000287 return InstDesc(Kind == RK_IntegerOr, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000288 case Instruction::Xor:
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000289 return InstDesc(Kind == RK_IntegerXor, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000290 case Instruction::FMul:
Tyler Nowickic1a86f52015-08-10 19:51:46 +0000291 return InstDesc(Kind == RK_FloatMult, I, UAI);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000292 case Instruction::FSub:
293 case Instruction::FAdd:
Tyler Nowickic1a86f52015-08-10 19:51:46 +0000294 return InstDesc(Kind == RK_FloatAdd, I, UAI);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000295 case Instruction::FCmp:
296 case Instruction::ICmp:
297 case Instruction::Select:
298 if (Kind != RK_IntegerMinMax &&
299 (!HasFunNoNaNAttr || Kind != RK_FloatMinMax))
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000300 return InstDesc(false, I);
Karthik Bhat76aa6622015-04-20 04:38:33 +0000301 return isMinMaxSelectCmpPattern(I, Prev);
302 }
303}
304
Tyler Nowicki0a913102015-06-16 18:07:34 +0000305bool RecurrenceDescriptor::hasMultipleUsesOf(
Karthik Bhat76aa6622015-04-20 04:38:33 +0000306 Instruction *I, SmallPtrSetImpl<Instruction *> &Insts) {
307 unsigned NumUses = 0;
308 for (User::op_iterator Use = I->op_begin(), E = I->op_end(); Use != E;
309 ++Use) {
310 if (Insts.count(dyn_cast<Instruction>(*Use)))
311 ++NumUses;
312 if (NumUses > 1)
313 return true;
314 }
315
316 return false;
317}
Tyler Nowicki0a913102015-06-16 18:07:34 +0000318bool RecurrenceDescriptor::isReductionPHI(PHINode *Phi, Loop *TheLoop,
319 RecurrenceDescriptor &RedDes) {
Karthik Bhat76aa6622015-04-20 04:38:33 +0000320
321 bool HasFunNoNaNAttr = false;
322 BasicBlock *Header = TheLoop->getHeader();
323 Function &F = *Header->getParent();
324 if (F.hasFnAttribute("no-nans-fp-math"))
325 HasFunNoNaNAttr =
326 F.getFnAttribute("no-nans-fp-math").getValueAsString() == "true";
327
328 if (AddReductionVar(Phi, RK_IntegerAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
329 DEBUG(dbgs() << "Found an ADD reduction PHI." << *Phi << "\n");
330 return true;
331 }
332 if (AddReductionVar(Phi, RK_IntegerMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
333 DEBUG(dbgs() << "Found a MUL reduction PHI." << *Phi << "\n");
334 return true;
335 }
336 if (AddReductionVar(Phi, RK_IntegerOr, TheLoop, HasFunNoNaNAttr, RedDes)) {
337 DEBUG(dbgs() << "Found an OR reduction PHI." << *Phi << "\n");
338 return true;
339 }
340 if (AddReductionVar(Phi, RK_IntegerAnd, TheLoop, HasFunNoNaNAttr, RedDes)) {
341 DEBUG(dbgs() << "Found an AND reduction PHI." << *Phi << "\n");
342 return true;
343 }
344 if (AddReductionVar(Phi, RK_IntegerXor, TheLoop, HasFunNoNaNAttr, RedDes)) {
345 DEBUG(dbgs() << "Found a XOR reduction PHI." << *Phi << "\n");
346 return true;
347 }
348 if (AddReductionVar(Phi, RK_IntegerMinMax, TheLoop, HasFunNoNaNAttr,
349 RedDes)) {
350 DEBUG(dbgs() << "Found a MINMAX reduction PHI." << *Phi << "\n");
351 return true;
352 }
353 if (AddReductionVar(Phi, RK_FloatMult, TheLoop, HasFunNoNaNAttr, RedDes)) {
354 DEBUG(dbgs() << "Found an FMult reduction PHI." << *Phi << "\n");
355 return true;
356 }
357 if (AddReductionVar(Phi, RK_FloatAdd, TheLoop, HasFunNoNaNAttr, RedDes)) {
358 DEBUG(dbgs() << "Found an FAdd reduction PHI." << *Phi << "\n");
359 return true;
360 }
361 if (AddReductionVar(Phi, RK_FloatMinMax, TheLoop, HasFunNoNaNAttr, RedDes)) {
362 DEBUG(dbgs() << "Found an float MINMAX reduction PHI." << *Phi << "\n");
363 return true;
364 }
365 // Not a reduction of known type.
366 return false;
367}
368
369/// This function returns the identity element (or neutral element) for
370/// the operation K.
Tyler Nowicki0a913102015-06-16 18:07:34 +0000371Constant *RecurrenceDescriptor::getRecurrenceIdentity(RecurrenceKind K,
372 Type *Tp) {
Karthik Bhat76aa6622015-04-20 04:38:33 +0000373 switch (K) {
374 case RK_IntegerXor:
375 case RK_IntegerAdd:
376 case RK_IntegerOr:
377 // Adding, Xoring, Oring zero to a number does not change it.
378 return ConstantInt::get(Tp, 0);
379 case RK_IntegerMult:
380 // Multiplying a number by 1 does not change it.
381 return ConstantInt::get(Tp, 1);
382 case RK_IntegerAnd:
383 // AND-ing a number with an all-1 value does not change it.
384 return ConstantInt::get(Tp, -1, true);
385 case RK_FloatMult:
386 // Multiplying a number by 1 does not change it.
387 return ConstantFP::get(Tp, 1.0L);
388 case RK_FloatAdd:
389 // Adding zero to a number does not change it.
390 return ConstantFP::get(Tp, 0.0L);
391 default:
Tyler Nowicki0a913102015-06-16 18:07:34 +0000392 llvm_unreachable("Unknown recurrence kind");
Karthik Bhat76aa6622015-04-20 04:38:33 +0000393 }
394}
395
Tyler Nowicki0a913102015-06-16 18:07:34 +0000396/// This function translates the recurrence kind to an LLVM binary operator.
397unsigned RecurrenceDescriptor::getRecurrenceBinOp(RecurrenceKind Kind) {
Karthik Bhat76aa6622015-04-20 04:38:33 +0000398 switch (Kind) {
399 case RK_IntegerAdd:
400 return Instruction::Add;
401 case RK_IntegerMult:
402 return Instruction::Mul;
403 case RK_IntegerOr:
404 return Instruction::Or;
405 case RK_IntegerAnd:
406 return Instruction::And;
407 case RK_IntegerXor:
408 return Instruction::Xor;
409 case RK_FloatMult:
410 return Instruction::FMul;
411 case RK_FloatAdd:
412 return Instruction::FAdd;
413 case RK_IntegerMinMax:
414 return Instruction::ICmp;
415 case RK_FloatMinMax:
416 return Instruction::FCmp;
417 default:
Tyler Nowicki0a913102015-06-16 18:07:34 +0000418 llvm_unreachable("Unknown recurrence operation");
Karthik Bhat76aa6622015-04-20 04:38:33 +0000419 }
420}
421
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000422Value *RecurrenceDescriptor::createMinMaxOp(IRBuilder<> &Builder,
423 MinMaxRecurrenceKind RK,
424 Value *Left, Value *Right) {
Karthik Bhat76aa6622015-04-20 04:38:33 +0000425 CmpInst::Predicate P = CmpInst::ICMP_NE;
426 switch (RK) {
427 default:
Tyler Nowicki0a913102015-06-16 18:07:34 +0000428 llvm_unreachable("Unknown min/max recurrence kind");
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000429 case MRK_UIntMin:
Karthik Bhat76aa6622015-04-20 04:38:33 +0000430 P = CmpInst::ICMP_ULT;
431 break;
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000432 case MRK_UIntMax:
Karthik Bhat76aa6622015-04-20 04:38:33 +0000433 P = CmpInst::ICMP_UGT;
434 break;
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000435 case MRK_SIntMin:
Karthik Bhat76aa6622015-04-20 04:38:33 +0000436 P = CmpInst::ICMP_SLT;
437 break;
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000438 case MRK_SIntMax:
Karthik Bhat76aa6622015-04-20 04:38:33 +0000439 P = CmpInst::ICMP_SGT;
440 break;
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000441 case MRK_FloatMin:
Karthik Bhat76aa6622015-04-20 04:38:33 +0000442 P = CmpInst::FCMP_OLT;
443 break;
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000444 case MRK_FloatMax:
Karthik Bhat76aa6622015-04-20 04:38:33 +0000445 P = CmpInst::FCMP_OGT;
446 break;
447 }
448
449 Value *Cmp;
Tyler Nowicki27b2c392015-06-16 22:59:45 +0000450 if (RK == MRK_FloatMin || RK == MRK_FloatMax)
Karthik Bhat76aa6622015-04-20 04:38:33 +0000451 Cmp = Builder.CreateFCmp(P, Left, Right, "rdx.minmax.cmp");
452 else
453 Cmp = Builder.CreateICmp(P, Left, Right, "rdx.minmax.cmp");
454
455 Value *Select = Builder.CreateSelect(Cmp, Left, Right, "rdx.minmax.select");
456 return Select;
457}
Karthik Bhat24e6cc22015-04-23 08:29:20 +0000458
459bool llvm::isInductionPHI(PHINode *Phi, ScalarEvolution *SE,
460 ConstantInt *&StepValue) {
461 Type *PhiTy = Phi->getType();
462 // We only handle integer and pointer inductions variables.
463 if (!PhiTy->isIntegerTy() && !PhiTy->isPointerTy())
464 return false;
465
466 // Check that the PHI is consecutive.
467 const SCEV *PhiScev = SE->getSCEV(Phi);
468 const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(PhiScev);
469 if (!AR) {
470 DEBUG(dbgs() << "LV: PHI is not a poly recurrence.\n");
471 return false;
472 }
473
474 const SCEV *Step = AR->getStepRecurrence(*SE);
475 // Calculate the pointer stride and check if it is consecutive.
476 const SCEVConstant *C = dyn_cast<SCEVConstant>(Step);
477 if (!C)
478 return false;
479
480 ConstantInt *CV = C->getValue();
481 if (PhiTy->isIntegerTy()) {
482 StepValue = CV;
483 return true;
484 }
485
486 assert(PhiTy->isPointerTy() && "The PHI must be a pointer");
487 Type *PointerElementType = PhiTy->getPointerElementType();
488 // The pointer stride cannot be determined if the pointer element type is not
489 // sized.
490 if (!PointerElementType->isSized())
491 return false;
492
493 const DataLayout &DL = Phi->getModule()->getDataLayout();
494 int64_t Size = static_cast<int64_t>(DL.getTypeAllocSize(PointerElementType));
David Majnemerb58f32f2015-06-05 10:52:40 +0000495 if (!Size)
496 return false;
497
Karthik Bhat24e6cc22015-04-23 08:29:20 +0000498 int64_t CVSize = CV->getSExtValue();
499 if (CVSize % Size)
500 return false;
501 StepValue = ConstantInt::getSigned(CV->getType(), CVSize / Size);
502 return true;
503}