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 "llvm/Analysis/ScalarEvolutionExpressions.h" 19 #include "llvm/IR/ConstantRange.h" 20 #include "llvm/IR/ValueHandle.h" 21 22 namespace llvm { 23 24 class CastInst; 25 class DominatorTree; 26 class Loop; 27 class LoopInfo; 28 class PHINode; 29 class ScalarEvolution; 30 class SCEVExpander; 31 class TargetTransformInfo; 32 33 /// Interface for visiting interesting IV users that are recognized but not 34 /// simplified by this utility. 35 class IVVisitor { 36 protected: 37 const DominatorTree *DT = nullptr; 38 39 virtual void anchor(); 40 41 public: 42 IVVisitor() = default; 43 virtual ~IVVisitor() = default; 44 getDomTree()45 const DominatorTree *getDomTree() const { return DT; } 46 virtual void visitCast(CastInst *Cast) = 0; 47 }; 48 49 /// simplifyUsersOfIV - Simplify instructions that use this induction variable 50 /// by using ScalarEvolution to analyze the IV's recurrence. 51 bool simplifyUsersOfIV(PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, 52 LoopInfo *LI, const TargetTransformInfo *TTI, 53 SmallVectorImpl<WeakTrackingVH> &Dead, 54 SCEVExpander &Rewriter, IVVisitor *V = nullptr); 55 56 /// SimplifyLoopIVs - Simplify users of induction variables within this 57 /// loop. This does not actually change or add IVs. 58 bool simplifyLoopIVs(Loop *L, ScalarEvolution *SE, DominatorTree *DT, 59 LoopInfo *LI, const TargetTransformInfo *TTI, 60 SmallVectorImpl<WeakTrackingVH> &Dead); 61 62 /// Collect information about induction variables that are used by sign/zero 63 /// extend operations. This information is recorded by CollectExtend and provides 64 /// the input to WidenIV. 65 struct WideIVInfo { 66 PHINode *NarrowIV = nullptr; 67 68 // Widest integer type created [sz]ext 69 Type *WidestNativeType = nullptr; 70 71 // Was a sext user seen before a zext? 72 bool IsSigned = false; 73 }; 74 75 /// Widen Induction Variables - Extend the width of an IV to cover its 76 /// widest uses. 77 PHINode *createWideIV(const WideIVInfo &WI, 78 LoopInfo *LI, ScalarEvolution *SE, SCEVExpander &Rewriter, 79 DominatorTree *DT, SmallVectorImpl<WeakTrackingVH> &DeadInsts, 80 unsigned &NumElimExt, unsigned &NumWidened, 81 bool HasGuards, bool UsePostIncrementRanges); 82 83 } // end namespace llvm 84 85 #endif // LLVM_TRANSFORMS_UTILS_SIMPLIFYINDVAR_H 86