1//===-- llvm/Transforms/Utils/SimplifyIndVar.h - Indvar Utils ---*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines in interface for induction variable simplification. It does
10// not define any actual pass or policy, but provides a single function to
11// simplify a loop's induction variables based on ScalarEvolution.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TRANSFORMS_UTILS_SIMPLIFYINDVAR_H
16#define LLVM_TRANSFORMS_UTILS_SIMPLIFYINDVAR_H
17
18#include <utility>
19
20namespace llvm {
21
22class Type;
23class WeakTrackingVH;
24template <typename T> class SmallVectorImpl;
25class CastInst;
26class DominatorTree;
27class Loop;
28class LoopInfo;
29class PHINode;
30class ScalarEvolution;
31class SCEVExpander;
32class TargetTransformInfo;
33
34/// Interface for visiting interesting IV users that are recognized but not
35/// simplified by this utility.
36class IVVisitor {
37protected:
38 const DominatorTree *DT = nullptr;
39
40 virtual void anchor();
41
42public:
43 IVVisitor() = default;
44 virtual ~IVVisitor() = default;
45
46 const DominatorTree *getDomTree() const { return DT; }
47 virtual void visitCast(CastInst *Cast) = 0;
48};
49
50/// simplifyUsersOfIV - Simplify instructions that use this induction variable
51/// by using ScalarEvolution to analyze the IV's recurrence. Returns a pair
52/// where the first entry indicates that the function makes changes and the
53/// second entry indicates that it introduced new opportunities for loop
54/// unswitching.
55std::pair<bool, bool> simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE,
56 DominatorTree *DT, LoopInfo *LI,
57 const TargetTransformInfo *TTI,
58 SmallVectorImpl<WeakTrackingVH> &Dead,
59 SCEVExpander &Rewriter,
60 IVVisitor *V = nullptr);
61
62/// SimplifyLoopIVs - Simplify users of induction variables within this
63/// loop. This does not actually change or add IVs.
64bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT,
65 LoopInfo *LI, const TargetTransformInfo *TTI,
66 SmallVectorImpl<WeakTrackingVH> &Dead);
67
68/// Collect information about induction variables that are used by sign/zero
69/// extend operations. This information is recorded by CollectExtend and provides
70/// the input to WidenIV.
71struct WideIVInfo {
72 PHINode *NarrowIV = nullptr;
73
74 // Widest integer type created [sz]ext
75 Type *WidestNativeType = nullptr;
76
77 // Was a sext user seen before a zext?
78 bool IsSigned = false;
79};
80
81/// Widen Induction Variables - Extend the width of an IV to cover its
82/// widest uses.
83PHINode *createWideIV(const WideIVInfo &WI,
84 LoopInfo *LI, ScalarEvolution *SE, SCEVExpander &Rewriter,
85 DominatorTree *DT, SmallVectorImpl<WeakTrackingVH> &DeadInsts,
86 unsigned &NumElimExt, unsigned &NumWidened,
87 bool HasGuards, bool UsePostIncrementRanges);
88
89} // end namespace llvm
90
91#endif // LLVM_TRANSFORMS_UTILS_SIMPLIFYINDVAR_H
92