LLVM 19.0.0git
Namespaces | Macros | Functions
SimplifyIndVar.cpp File Reference
#include "llvm/Transforms/Utils/SimplifyIndVar.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"

Go to the source code of this file.

Namespaces

namespace  llvm
 This is an optimization pass for GlobalISel generic memory operations.
 

Macros

#define DEBUG_TYPE   "indvars"
 

Functions

 STATISTIC (NumElimIdentity, "Number of IV identities eliminated")
 
 STATISTIC (NumElimOperand, "Number of IV operands folded into a use")
 
 STATISTIC (NumFoldedUser, "Number of IV users folded into a constant")
 
 STATISTIC (NumElimRem, "Number of IV remainder operations eliminated")
 
 STATISTIC (NumSimplifiedSDiv, "Number of IV signed division operations converted to unsigned division")
 
 STATISTIC (NumSimplifiedSRem, "Number of IV signed remainder operations converted to unsigned remainder")
 
 STATISTIC (NumElimCmp, "Number of IV comparisons eliminated")
 
static InstructionfindCommonDominator (ArrayRef< Instruction * > Instructions, DominatorTree &DT)
 Find a point in code which dominates all given instructions.
 
static InstructionGetLoopInvariantInsertPosition (Loop *L, Instruction *Hint)
 
static void pushIVUsers (Instruction *Def, Loop *L, SmallPtrSet< Instruction *, 16 > &Simplified, SmallVectorImpl< std::pair< Instruction *, Instruction * > > &SimpleIVUsers)
 Add all uses of Def to the current IV's worklist.
 
static bool isSimpleIVUser (Instruction *I, const Loop *L, ScalarEvolution *SE)
 Return true if this instruction generates a simple SCEV expression in terms of that IV.
 
bool llvm::simplifyUsersOfIV (PHINode *CurrIV, ScalarEvolution *SE, DominatorTree *DT, LoopInfo *LI, const TargetTransformInfo *TTI, SmallVectorImpl< WeakTrackingVH > &Dead, SCEVExpander &Rewriter, IVVisitor *V=nullptr)
 simplifyUsersOfIV - Simplify instructions that use this induction variable by using ScalarEvolution to analyze the IV's recurrence.
 
bool llvm::simplifyLoopIVs (Loop *L, ScalarEvolution *SE, DominatorTree *DT, LoopInfo *LI, const TargetTransformInfo *TTI, SmallVectorImpl< WeakTrackingVH > &Dead)
 SimplifyLoopIVs - Simplify users of induction variables within this loop.
 
static InstructiongetInsertPointForUses (Instruction *User, Value *Def, DominatorTree *DT, LoopInfo *LI)
 Determine the insertion point for this user.
 
static std::optional< BinaryOp > matchBinaryOp (Instruction *Op)
 
static void truncateIVUse (WidenIV::NarrowIVDefUse DU, DominatorTree *DT, LoopInfo *LI)
 This IV user cannot be widened.
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "indvars"

Definition at line 34 of file SimplifyIndVar.cpp.

Function Documentation

◆ findCommonDominator()

static Instruction * findCommonDominator ( ArrayRef< Instruction * >  Instructions,
DominatorTree DT 
)
static

Find a point in code which dominates all given instructions.

We can safely assume that, whatever fact we can prove at the found point, this fact is also true for each of the given instructions.

Definition at line 109 of file SimplifyIndVar.cpp.

References assert(), llvm::DominatorTree::findNearestCommonDominator(), and Insn.

◆ getInsertPointForUses()

static Instruction * getInsertPointForUses ( Instruction User,
Value Def,
DominatorTree DT,
LoopInfo LI 
)
static

Determine the insertion point for this user.

By default, insert immediately before the user. SCEVExpander or LICM will hoist loop invariants out of the loop. For PHI nodes, there may be multiple uses, so compute the nearest common dominator for the incoming blocks. A nullptr can be returned if no viable location is found: it may happen if User is a PHI and Def only comes to this PHI from unreachable blocks.

Definition at line 1165 of file SimplifyIndVar.cpp.

References assert(), llvm::DominatorTree::dominates(), llvm::DominatorTree::findNearestCommonDominator(), llvm::LoopInfoBase< BlockT, LoopT >::getLoopFor(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::DominatorTree::isReachableFromEntry(), llvm_unreachable, and PHI.

Referenced by truncateIVUse().

◆ GetLoopInvariantInsertPosition()

static Instruction * GetLoopInvariantInsertPosition ( Loop L,
Instruction Hint 
)
static

Definition at line 616 of file SimplifyIndVar.cpp.

◆ isSimpleIVUser()

static bool isSimpleIVUser ( Instruction I,
const Loop L,
ScalarEvolution SE 
)
static

Return true if this instruction generates a simple SCEV expression in terms of that IV.

This is similar to IVUsers' isInteresting() but processes each instruction non-recursively when the operand is already known to be a simpleIVUser.

Definition at line 873 of file SimplifyIndVar.cpp.

References llvm::SCEVAddRecExpr::getLoop(), llvm::ScalarEvolution::getSCEV(), I, and llvm::ScalarEvolution::isSCEVable().

◆ matchBinaryOp()

static std::optional< BinaryOp > matchBinaryOp ( Instruction Op)
static

Definition at line 1411 of file SimplifyIndVar.cpp.

References llvm::BitWidth, llvm::APInt::getOneBitSet(), and X.

◆ pushIVUsers()

static void pushIVUsers ( Instruction Def,
Loop L,
SmallPtrSet< Instruction *, 16 > &  Simplified,
SmallVectorImpl< std::pair< Instruction *, Instruction * > > &  SimpleIVUsers 
)
static

Add all uses of Def to the current IV's worklist.

Definition at line 839 of file SimplifyIndVar.cpp.

◆ STATISTIC() [1/7]

STATISTIC ( NumElimCmp  ,
"Number of IV comparisons eliminated"   
)

References Rewriter.

◆ STATISTIC() [2/7]

STATISTIC ( NumElimIdentity  ,
"Number of IV identities eliminated"   
)

◆ STATISTIC() [3/7]

STATISTIC ( NumElimOperand  ,
"Number of IV operands folded into a use"   
)

◆ STATISTIC() [4/7]

STATISTIC ( NumElimRem  ,
"Number of IV remainder operations eliminated"   
)

◆ STATISTIC() [5/7]

STATISTIC ( NumFoldedUser  ,
"Number of IV users folded into a constant"   
)

◆ STATISTIC() [6/7]

STATISTIC ( NumSimplifiedSDiv  ,
"Number of IV signed division operations converted to unsigned division"   
)

◆ STATISTIC() [7/7]

STATISTIC ( NumSimplifiedSRem  ,
"Number of IV signed remainder operations converted to unsigned remainder"   
)

◆ truncateIVUse()

static void truncateIVUse ( WidenIV::NarrowIVDefUse  DU,
DominatorTree DT,
LoopInfo LI 
)
static

This IV user cannot be widened.

Replace this use of the original narrow IV with a truncation of the new wide IV to isolate and eliminate the narrow IV.

Definition at line 1564 of file SimplifyIndVar.cpp.

References llvm::IRBuilderBase::CreateTrunc(), llvm::dbgs(), getInsertPointForUses(), and LLVM_DEBUG.