LLVM API Documentation

Defines | 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/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/Type.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetLibraryInfo.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.

Defines

#define DEBUG_TYPE   "indvars"

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)
static bool ConvertToSInt (const APFloat &APF, int64_t &IntVal)
 ConvertToSInt - Convert APF to an integer, if possible.
static void visitIVCast (CastInst *Cast, WideIVInfo &WI, ScalarEvolution *SE, const DataLayout *DL)
static bool isLoopInvariant (Value *V, const Loop *L, const DominatorTree *DT)
static void truncateIVUse (NarrowIVDefUse DU, DominatorTree *DT)
static bool isHighCostExpansion (const SCEV *S, BranchInst *BI, SmallPtrSetImpl< const SCEV * > &Processed, ScalarEvolution *SE)
static bool canExpandBackedgeTakenCount (Loop *L, ScalarEvolution *SE)
static PHINodegetLoopPhiForCounter (Value *IncV, Loop *L, DominatorTree *DT)
static ICmpInstgetLoopTest (Loop *L)
 Return the compare guarding the loop latch, or NULL for unrecognized tests.
static bool needsLFTR (Loop *L, DominatorTree *DT)
static bool hasConcreteDefImpl (Value *V, SmallPtrSetImpl< Value * > &Visited, unsigned Depth)
static bool hasConcreteDef (Value *V)
static bool AlmostDeadIV (PHINode *Phi, BasicBlock *LatchBlock, Value *Cond)
static PHINodeFindLoopCounter (Loop *L, const SCEV *BECount, ScalarEvolution *SE, DominatorTree *DT, const DataLayout *DL)
static ValuegenLoopLimit (PHINode *IndVar, const SCEV *IVCount, Loop *L, SCEVExpander &Rewriter, ScalarEvolution *SE)

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."))
 indvars
Induction Variable Simplification
Induction Variable false

Define Documentation

#define DEBUG_TYPE   "indvars"

Definition at line 52 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 1471 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 
) [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 1315 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::ScalarEvolution::getBackedgeTakenCount(), llvm::LoopBase< BlockT, LoopT >::getExitingBlock(), llvm::BasicBlock::getTerminator(), 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 225 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,
const DataLayout DL 
) [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 1497 of file IndVarSimplify.cpp.

References AlmostDeadIV(), llvm::BasicBlock::begin(), llvm::dyn_cast(), llvm::PHINode::getBasicBlockIndex(), llvm::LoopBase< BlockT, LoopT >::getExitingBlock(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::PHINode::getIncomingValue(), llvm::SCEVAddRecExpr::getLoop(), llvm::LoopBase< BlockT, LoopT >::getLoopLatch(), getLoopPhiForCounter(), getLoopTest(), llvm::ScalarEvolution::getSCEV(), llvm::SCEVAddRecExpr::getStart(), llvm::SCEVAddRecExpr::getStepRecurrence(), llvm::BasicBlock::getTerminator(), llvm::SCEV::getType(), llvm::SCEVNAryExpr::getType(), llvm::Value::getType(), llvm::ScalarEvolution::getTypeSizeInBits(), hasConcreteDef(), I, llvm::SCEVAddRecExpr::isAffine(), llvm::DataLayout::isLegalInteger(), 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 194 of file IndVarSimplify.cpp.

References llvm::tgtok::Def, llvm::DominatorTree::dominates(), llvm::dyn_cast(), llvm::DominatorTreeBase< NodeT >::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 1338 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::LoopBase< BlockT, LoopT >::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 1374 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::BranchInst::getCondition(), llvm::LoopBase< BlockT, LoopT >::getExitingBlock(), llvm::LoopBase< BlockT, LoopT >::getLoopLatch(), and llvm::BasicBlock::getTerminator().

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 1463 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 1430 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 isHighCostExpansion ( const SCEV S,
BranchInst BI,
SmallPtrSetImpl< const SCEV * > &  Processed,
ScalarEvolution SE 
) [static]

Check for expressions that ScalarEvolution generates to compute BackedgeTakenInfo. If these expressions have not been reduced, then expanding them may incur additional cost (albeit in the loop preheader).

Definition at line 1256 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::BranchInst::getCondition(), llvm::ScalarEvolution::getConstant(), llvm::ScalarEvolution::getMinusSCEV(), llvm::User::getOperand(), llvm::ScalarEvolution::getSCEV(), llvm::SCEV::getType(), I, and llvm::SmallPtrSetImpl< PtrType >::insert().

Referenced by canExpandBackedgeTakenCount().

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

References llvm::dyn_cast(), llvm::LoopBase< BlockT, LoopT >::getHeader(), llvm::Instruction::getParent(), and llvm::DominatorTreeBase< NodeT >::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 1390 of file IndVarSimplify.cpp.

References llvm::dyn_cast(), llvm::PHINode::getBasicBlockIndex(), llvm::PHINode::getIncomingValue(), llvm::LoopBase< BlockT, LoopT >::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 923 of file IndVarSimplify.cpp.

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

static void visitIVCast ( CastInst Cast,
WideIVInfo &  WI,
ScalarEvolution SE,
const DataLayout DL 
) [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 663 of file IndVarSimplify.cpp.

References llvm::ScalarEvolution::getEffectiveSCEVType(), llvm::CastInst::getOpcode(), llvm::Value::getType(), llvm::ScalarEvolution::getTypeSizeInBits(), llvm::DataLayout::isLegalInteger(), and llvm::SExt.


Variable Documentation

Induction Variable false

Definition at line 131 of file IndVarSimplify.cpp.

Definition at line 131 of file IndVarSimplify.cpp.

cl::opt<bool> ReduceLiveIVs("liv-reduce", cl::Hidden, cl::desc("Reduce live induction variables.")) [static]
Induction Variable Simplification

Definition at line 131 of file IndVarSimplify.cpp.

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