LLVM  3.7.0
Macros | Enumerations | Functions | Variables
IndVarSimplify.cpp File Reference
#include "llvm/Transforms/Scalar.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/ScalarEvolutionExpander.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/SimplifyIndVar.h"
Include dependency graph for IndVarSimplify.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "indvars"
 

Enumerations

enum  ReplaceExitVal { NeverRepl, OnlyCheapRepl, AlwaysRepl }
 

Functions

 STATISTIC (NumWidened,"Number of indvars widened")
 
 STATISTIC (NumReplaced,"Number of exit values replaced")
 
 STATISTIC (NumLFTR,"Number of loop exit tests replaced")
 
 STATISTIC (NumElimExt,"Number of IV sign/zero extends eliminated")
 
 STATISTIC (NumElimIV,"Number of congruent IVs eliminated")
 
 INITIALIZE_PASS_BEGIN (IndVarSimplify,"indvars","Induction Variable Simplification", false, false) INITIALIZE_PASS_END(IndVarSimplify
 
static InstructiongetInsertPointForUses (Instruction *User, Value *Def, DominatorTree *DT)
 Determine the insertion point for this user. More...
 
static bool ConvertToSInt (const APFloat &APF, int64_t &IntVal)
 ConvertToSInt - Convert APF to an integer, if possible. More...
 
static void visitIVCast (CastInst *Cast, WideIVInfo &WI, ScalarEvolution *SE, const TargetTransformInfo *TTI)
 visitCast - Update information about the induction variable that is extended by this sign or zero extend operation. More...
 
static bool isLoopInvariant (Value *V, const Loop *L, const DominatorTree *DT)
 isLoopInvariant - Perform a quick domtree based check for loop invariance assuming that V is used within the loop. More...
 
static void truncateIVUse (NarrowIVDefUse DU, DominatorTree *DT)
 This IV user cannot be widen. More...
 
static bool canExpandBackedgeTakenCount (Loop *L, ScalarEvolution *SE, SCEVExpander &Rewriter)
 canExpandBackedgeTakenCount - Return true if this loop's backedge taken count expression can be safely and cheaply expanded into an instruction sequence that can be used by LinearFunctionTestReplace. More...
 
static PHINodegetLoopPhiForCounter (Value *IncV, Loop *L, DominatorTree *DT)
 getLoopPhiForCounter - Return the loop header phi IFF IncV adds a loop invariant value to the phi. More...
 
static ICmpInstgetLoopTest (Loop *L)
 Return the compare guarding the loop latch, or NULL for unrecognized tests. More...
 
static bool needsLFTR (Loop *L, DominatorTree *DT)
 needsLFTR - LinearFunctionTestReplace policy. More...
 
static bool hasConcreteDefImpl (Value *V, SmallPtrSetImpl< Value * > &Visited, unsigned Depth)
 Recursive helper for hasConcreteDef(). More...
 
static bool hasConcreteDef (Value *V)
 Return true if the given value is concrete. More...
 
static bool AlmostDeadIV (PHINode *Phi, BasicBlock *LatchBlock, Value *Cond)
 AlmostDeadIV - Return true if this IV has any uses other than the (soon to be rewritten) loop exit test. More...
 
static PHINodeFindLoopCounter (Loop *L, const SCEV *BECount, ScalarEvolution *SE, DominatorTree *DT)
 FindLoopCounter - Find an affine IV in canonical form. More...
 
static ValuegenLoopLimit (PHINode *IndVar, const SCEV *IVCount, Loop *L, SCEVExpander &Rewriter, ScalarEvolution *SE)
 genLoopLimit - Help LinearFunctionTestReplace by generating a value that holds the RHS of the new loop test. More...
 

Variables

static cl::opt< boolVerifyIndvars ("verify-indvars", cl::Hidden, cl::desc("Verify the ScalarEvolution result after running indvars"))
 
static cl::opt< boolReduceLiveIVs ("liv-reduce", cl::Hidden, cl::desc("Reduce live induction variables."))
 
static cl::opt< ReplaceExitValReplaceExitValue ("replexitval", cl::Hidden, cl::init(OnlyCheapRepl), cl::desc("Choose the strategy to replace exit value in IndVarSimplify"), cl::values(clEnumValN(NeverRepl,"never","never replace exit value"), clEnumValN(OnlyCheapRepl,"cheap","only replace exit value when the cost is cheap"), clEnumValN(AlwaysRepl,"always","always replace exit value whenever possible"), clEnumValEnd))
 
 indvars
 
Induction Variable Simplification
 
Induction Variable false
 

Macro Definition Documentation

#define DEBUG_TYPE   "indvars"

Definition at line 54 of file IndVarSimplify.cpp.

Enumeration Type Documentation

Enumerator
NeverRepl 
OnlyCheapRepl 
AlwaysRepl 

Definition at line 72 of file IndVarSimplify.cpp.

Function Documentation

static bool AlmostDeadIV ( PHINode Phi,
BasicBlock LatchBlock,
Value Cond 
)
static

AlmostDeadIV - Return true if this IV has any uses other than the (soon to be rewritten) loop exit test.

Definition at line 1599 of file IndVarSimplify.cpp.

References llvm::PHINode::getBasicBlockIndex(), llvm::PHINode::getIncomingValue(), and llvm::Value::users().

Referenced by FindLoopCounter().

static bool canExpandBackedgeTakenCount ( Loop L,
ScalarEvolution SE,
SCEVExpander Rewriter 
)
static

canExpandBackedgeTakenCount - Return true if this loop's backedge taken count expression can be safely and cheaply expanded into an instruction sequence that can be used by LinearFunctionTestReplace.

TODO: This fails for pointer-type loop counters with greater than one byte strides, consequently preventing LFTR from running. For the purpose of LFTR we could skip this check in the case that the LFTR loop counter (chosen by FindLoopCounter) is also pointer type. Instead, we could directly convert the loop test to an inequality test by checking the target data's alignment of element types (given that the initial pointer value originates from or is used by ABI constrained operation, as opposed to inttoptr/ptrtoint). However, we don't yet have a strong motivation for converting loop tests into inequality tests.

Definition at line 1444 of file IndVarSimplify.cpp.

References llvm::ScalarEvolution::getBackedgeTakenCount(), llvm::LoopBase< N, M >::getExitingBlock(), llvm::SCEVExpander::isHighCostExpansion(), and llvm::SCEV::isZero().

static bool ConvertToSInt ( const APFloat APF,
int64_t &  IntVal 
)
static

ConvertToSInt - Convert APF to an integer, if possible.

Definition at line 248 of file IndVarSimplify.cpp.

References llvm::APFloat::convertToInteger(), llvm::APFloat::opOK, and llvm::APFloat::rmTowardZero.

static PHINode* FindLoopCounter ( Loop L,
const SCEV BECount,
ScalarEvolution SE,
DominatorTree DT 
)
static

FindLoopCounter - Find an affine IV in canonical form.

BECount may be an i8* pointer type. The pointer difference is already valid count without scaling the address stride, so it remains a pointer expression as far as SCEV is concerned.

Currently only valid for LFTR. See the comments on hasConcreteDef below.

FIXME: Accept -1 stride and set IVLimit = IVInit - BECount

FIXME: Accept non-unit stride as long as SCEV can reduce BECount * Stride. This is difficult in general for SCEV because of potential overflow. But we could at least handle constant BECounts.

Definition at line 1624 of file IndVarSimplify.cpp.

References AlmostDeadIV(), llvm::dyn_cast(), llvm::PHINode::getBasicBlockIndex(), llvm::LoopBase< N, M >::getExitingBlock(), llvm::LoopBase< N, M >::getHeader(), llvm::PHINode::getIncomingValue(), llvm::SCEVAddRecExpr::getLoop(), llvm::LoopBase< N, M >::getLoopLatch(), getLoopPhiForCounter(), getLoopTest(), llvm::ScalarEvolution::getSCEV(), llvm::SCEVAddRecExpr::getStart(), llvm::SCEVAddRecExpr::getStepRecurrence(), llvm::SCEV::getType(), llvm::SCEVNAryExpr::getType(), llvm::Value::getType(), llvm::ScalarEvolution::getTypeSizeInBits(), hasConcreteDef(), I, llvm::SCEVAddRecExpr::isAffine(), llvm::SCEV::isOne(), llvm::Type::isPointerTy(), llvm::ScalarEvolution::isSCEVable(), and llvm::SCEV::isZero().

static Value* genLoopLimit ( PHINode IndVar,
const SCEV IVCount,
Loop L,
SCEVExpander Rewriter,
ScalarEvolution SE 
)
static
static Instruction* getInsertPointForUses ( Instruction User,
Value Def,
DominatorTree DT 
)
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.

Definition at line 217 of file IndVarSimplify.cpp.

References llvm::tgtok::Def, llvm::DominatorTree::dominates(), llvm::dyn_cast(), llvm::DominatorTreeBase< N >::findNearestCommonDominator(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), and llvm::TargetOpcode::PHI.

Referenced by truncateIVUse().

static PHINode* getLoopPhiForCounter ( Value IncV,
Loop L,
DominatorTree DT 
)
static

getLoopPhiForCounter - Return the loop header phi IFF IncV adds a loop invariant value to the phi.

Definition at line 1466 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::LoopBase< N, M >::getHeader(), llvm::User::getNumOperands(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), and isLoopInvariant().

Referenced by FindLoopCounter(), and needsLFTR().

static ICmpInst* getLoopTest ( Loop L)
static

Return the compare guarding the loop latch, or NULL for unrecognized tests.

Definition at line 1502 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::BranchInst::getCondition(), llvm::LoopBase< N, M >::getExitingBlock(), and llvm::LoopBase< N, M >::getLoopLatch().

Referenced by FindLoopCounter(), and needsLFTR().

static bool hasConcreteDef ( Value V)
static

Return true if the given value is concrete.

We must prove that undef can never reach it.

TODO: If we decide that this is a good approach to checking for undef, we may factor it into a common location.

Definition at line 1591 of file IndVarSimplify.cpp.

References hasConcreteDefImpl(), and llvm::SmallPtrSetImpl< PtrType >::insert().

Referenced by FindLoopCounter().

static bool hasConcreteDefImpl ( Value V,
SmallPtrSetImpl< Value * > &  Visited,
unsigned  Depth 
)
static

Recursive helper for hasConcreteDef().

Unfortunately, this currently boils down to checking that all operands are constant and listing instructions that may hide undef.

Definition at line 1558 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Instruction::mayReadFromMemory(), llvm::User::op_begin(), and llvm::User::op_end().

Referenced by hasConcreteDef().

INITIALIZE_PASS_BEGIN ( IndVarSimplify  ,
"indvars"  ,
"Induction Variable Simplification ,
false  ,
false   
)
static bool isLoopInvariant ( Value V,
const Loop L,
const DominatorTree DT 
)
static

isLoopInvariant - Perform a quick domtree based check for loop invariance assuming that V is used within the loop.

LoopInfo::isLoopInvariant() seems gratuitous for this purpose.

Definition at line 942 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::LoopBase< N, M >::getHeader(), llvm::Instruction::getParent(), and llvm::DominatorTreeBase< N >::properlyDominates().

Referenced by llvm::DependenceAnalysis::depends(), getLoopPhiForCounter(), llvm::DependenceAnalysis::getSplitIteration(), and needsLFTR().

static bool needsLFTR ( Loop L,
DominatorTree DT 
)
static

needsLFTR - LinearFunctionTestReplace policy.

Return true unless we can show that the current exit test is already sufficiently canonical.

Definition at line 1518 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::PHINode::getBasicBlockIndex(), llvm::PHINode::getIncomingValue(), llvm::LoopBase< N, M >::getLoopLatch(), getLoopPhiForCounter(), getLoopTest(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, isLoopInvariant(), and std::swap().

STATISTIC ( NumWidened  ,
"Number of indvars widened"   
)
STATISTIC ( NumReplaced  ,
"Number of exit values replaced"   
)
STATISTIC ( NumLFTR  ,
"Number of loop exit tests replaced"   
)
STATISTIC ( NumElimExt  ,
"Number of IV sign/zero extends eliminated"   
)
STATISTIC ( NumElimIV  ,
"Number of congruent IVs eliminated"   
)
static void truncateIVUse ( NarrowIVDefUse  DU,
DominatorTree DT 
)
static

This IV user cannot be widen.

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 1100 of file IndVarSimplify.cpp.

References llvm::IRBuilder< preserveNames, T, Inserter >::CreateTrunc(), llvm::dbgs(), DEBUG, and getInsertPointForUses().

static void visitIVCast ( CastInst Cast,
WideIVInfo &  WI,
ScalarEvolution SE,
const TargetTransformInfo TTI 
)
static

visitCast - Update information about the induction variable that is extended by this sign or zero extend operation.

This is used to determine the final width of the IV before actually widening it.

Definition at line 818 of file IndVarSimplify.cpp.

References llvm::TargetTransformInfo::getArithmeticInstrCost(), llvm::Module::getDataLayout(), llvm::ScalarEvolution::getEffectiveSCEVType(), llvm::Instruction::getModule(), llvm::CastInst::getOpcode(), llvm::User::getOperand(), llvm::Value::getType(), llvm::ScalarEvolution::getTypeSizeInBits(), and llvm::DataLayout::isLegalInteger().

Variable Documentation

Induction Variable false

Definition at line 154 of file IndVarSimplify.cpp.

indvars

Definition at line 154 of file IndVarSimplify.cpp.

cl::opt<bool> ReduceLiveIVs("liv-reduce", cl::Hidden, cl::desc("Reduce live induction variables."))
static
cl::opt<ReplaceExitVal> ReplaceExitValue("replexitval", cl::Hidden, cl::init(OnlyCheapRepl), cl::desc("Choose the strategy to replace exit value in IndVarSimplify"), cl::values(clEnumValN(NeverRepl,"never","never replace exit value"),clEnumValN(OnlyCheapRepl,"cheap","only replace exit value when the cost is cheap"),clEnumValN(AlwaysRepl,"always","always replace exit value whenever possible"),clEnumValEnd))
static
Induction Variable Simplification

Definition at line 154 of file IndVarSimplify.cpp.

cl::opt<bool> VerifyIndvars("verify-indvars", cl::Hidden, cl::desc("Verify the ScalarEvolution result after running indvars"))
static