|
LLVM
4.0.0
|
#include "llvm/Transforms/Scalar/IndVarSimplify.h"#include "llvm/ADT/SmallVector.h"#include "llvm/ADT/Statistic.h"#include "llvm/Analysis/GlobalsModRef.h"#include "llvm/Analysis/LoopInfo.h"#include "llvm/Analysis/LoopPass.h"#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.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/Scalar.h"#include "llvm/Transforms/Scalar/LoopPassManager.h"#include "llvm/Transforms/Utils/BasicBlockUtils.h"#include "llvm/Transforms/Utils/Local.h"#include "llvm/Transforms/Utils/LoopUtils.h"#include "llvm/Transforms/Utils/SimplifyIndVar.h"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") | |
| static Instruction * | getInsertPointForUses (Instruction *User, Value *Def, DominatorTree *DT, LoopInfo *LI) |
| Determine the insertion point for this user. More... | |
| static bool | ConvertToSInt (const APFloat &APF, int64_t &IntVal) |
| Convert APF to an integer, if possible. More... | |
| static void | visitIVCast (CastInst *Cast, WideIVInfo &WI, ScalarEvolution *SE, const TargetTransformInfo *TTI) |
| 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) |
| 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, LoopInfo *LI) |
| This IV user cannot be widen. More... | |
| static bool | canExpandBackedgeTakenCount (Loop *L, ScalarEvolution *SE, SCEVExpander &Rewriter) |
| 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 PHINode * | getLoopPhiForCounter (Value *IncV, Loop *L, DominatorTree *DT) |
| Return the loop header phi IFF IncV adds a loop invariant value to the phi. More... | |
| static ICmpInst * | getLoopTest (Loop *L) |
| Return the compare guarding the loop latch, or NULL for unrecognized tests. More... | |
| static bool | needsLFTR (Loop *L, DominatorTree *DT) |
| 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) |
| Return true if this IV has any uses other than the (soon to be rewritten) loop exit test. More... | |
| static PHINode * | FindLoopCounter (Loop *L, const SCEV *BECount, ScalarEvolution *SE, DominatorTree *DT) |
| Find an affine IV in canonical form. More... | |
| static Value * | genLoopLimit (PHINode *IndVar, const SCEV *IVCount, Loop *L, SCEVExpander &Rewriter, ScalarEvolution *SE) |
| Help linearFunctionTestReplace by generating a value that holds the RHS of the new loop test. More... | |
| INITIALIZE_PASS_BEGIN (IndVarSimplifyLegacyPass,"indvars","Induction Variable Simplification", false, false) INITIALIZE_PASS_END(IndVarSimplifyLegacyPass | |
Variables | |
| static cl::opt< bool > | VerifyIndvars ("verify-indvars", cl::Hidden, cl::desc("Verify the ScalarEvolution result after running indvars")) |
| 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"))) |
| static cl::opt< bool > | UsePostIncrementRanges ("indvars-post-increment-ranges", cl::Hidden, cl::desc("Use post increment control-dependent ranges in IndVarSimplify"), cl::init(true)) |
| indvars | |
| Induction Variable | Simplification |
| Induction Variable | false |
| #define DEBUG_TYPE "indvars" |
Definition at line 58 of file IndVarSimplify.cpp.
| enum ReplaceExitVal |
| Enumerator | |
|---|---|
| NeverRepl | |
| OnlyCheapRepl | |
| AlwaysRepl | |
Definition at line 73 of file IndVarSimplify.cpp.
|
static |
Return true if this IV has any uses other than the (soon to be rewritten) loop exit test.
Definition at line 1952 of file IndVarSimplify.cpp.
References llvm::PHINode::getBasicBlockIndex(), llvm::PHINode::getIncomingValue(), and llvm::Value::users().
Referenced by FindLoopCounter().
|
static |
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 1798 of file IndVarSimplify.cpp.
References llvm::ScalarEvolution::getBackedgeTakenCount(), llvm::LoopBase< N, M >::getExitingBlock(), llvm::SCEVExpander::isHighCostExpansion(), and llvm::SCEV::isZero().
Convert APF to an integer, if possible.
Definition at line 230 of file IndVarSimplify.cpp.
References llvm::APFloat::convertToInteger(), llvm::APFloatBase::opOK, and llvm::APFloatBase::rmTowardZero.
|
static |
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 1977 of file IndVarSimplify.cpp.
References AlmostDeadIV(), assert(), 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::DataLayout::isLegalInteger(), llvm::SCEV::isOne(), llvm::Type::isPointerTy(), llvm::ScalarEvolution::isSCEVable(), llvm::SCEV::isZero(), and L.
|
static |
Help linearFunctionTestReplace by generating a value that holds the RHS of the new loop test.
Definition at line 2060 of file IndVarSimplify.cpp.
References assert(), llvm::IRBuilder< T, Inserter >::CreateGEP(), llvm::dyn_cast(), llvm::SCEVExpander::expandCodeFor(), llvm::ScalarEvolution::getAddExpr(), llvm::Value::getContext(), llvm::ScalarEvolution::getEffectiveSCEVType(), llvm::LoopBase< N, M >::getExitingBlock(), llvm::PHINode::getIncomingValueForBlock(), llvm::SCEVAddRecExpr::getLoop(), llvm::LoopBase< N, M >::getLoopPreheader(), llvm::ScalarEvolution::getSCEV(), llvm::ScalarEvolution::getSizeOfExpr(), llvm::SCEVAddRecExpr::getStart(), llvm::SCEVAddRecExpr::getStepRecurrence(), llvm::ScalarEvolution::getTruncateExpr(), llvm::ScalarEvolution::getTruncateOrZeroExtend(), llvm::SCEV::getType(), llvm::Value::getType(), llvm::ScalarEvolution::getTypeSizeInBits(), llvm::SCEVAddRecExpr::isAffine(), llvm::ScalarEvolution::isLoopInvariant(), llvm::SCEV::isOne(), llvm::Type::isPointerTy(), and llvm::SCEV::isZero().
|
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 188 of file IndVarSimplify.cpp.
References assert(), llvm::tgtok::Def, llvm::DominatorTree::dominates(), llvm::dyn_cast(), llvm::DominatorTreeBase< N >::findNearestCommonDominator(), llvm::PHINode::getIncomingBlock(), llvm::PHINode::getIncomingValue(), llvm::LoopInfoBase< N, M >::getLoopFor(), llvm::PHINode::getNumIncomingValues(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), i, L, and llvm_unreachable.
Referenced by truncateIVUse().
|
static |
Return the loop header phi IFF IncV adds a loop invariant value to the phi.
Definition at line 1819 of file IndVarSimplify.cpp.
References llvm::MCID::Add, llvm::dyn_cast(), llvm::LoopBase< N, M >::getHeader(), llvm::User::getNumOperands(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), isLoopInvariant(), and L.
Referenced by FindLoopCounter(), and needsLFTR().
Return the compare guarding the loop latch, or NULL for unrecognized tests.
Definition at line 1855 of file IndVarSimplify.cpp.
References assert(), llvm::dyn_cast(), llvm::BranchInst::getCondition(), llvm::LoopBase< N, M >::getExitingBlock(), and llvm::LoopBase< N, M >::getLoopLatch().
Referenced by FindLoopCounter(), and needsLFTR().
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 1944 of file IndVarSimplify.cpp.
References hasConcreteDefImpl(), and llvm::SmallPtrSetImpl< PtrType >::insert().
Referenced by FindLoopCounter().
|
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 1911 of file IndVarSimplify.cpp.
References llvm::dyn_cast(), I, llvm::SmallPtrSetImpl< PtrType >::insert(), llvm::Instruction::mayReadFromMemory(), and llvm::User::operands().
Referenced by hasConcreteDef().
| INITIALIZE_PASS_BEGIN | ( | IndVarSimplifyLegacyPass | , |
| "indvars" | , | ||
| "Induction Variable Simplification" | , | ||
| false | , | ||
| false | |||
| ) |
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 994 of file IndVarSimplify.cpp.
References llvm::dyn_cast(), llvm::LoopBase< N, M >::getHeader(), llvm::Instruction::getParent(), and llvm::DominatorTreeBase< N >::properlyDominates().
Referenced by getLoopPhiForCounter(), and needsLFTR().
|
static |
linearFunctionTestReplace policy.
Return true unless we can show that the current exit test is already sufficiently canonical.
Definition at line 1871 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(), 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 |
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 1277 of file IndVarSimplify.cpp.
References llvm::IRBuilder< T, Inserter >::CreateTrunc(), llvm::dbgs(), DEBUG, and getInsertPointForUses().
|
static |
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 812 of file IndVarSimplify.cpp.
References llvm::MCID::Add, 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().
| Induction Variable false |
Definition at line 2534 of file IndVarSimplify.cpp.
| indvars |
Definition at line 2534 of file IndVarSimplify.cpp.
|
static |
| Induction Variable Simplification |
Definition at line 2534 of file IndVarSimplify.cpp.
|
static |
1.8.6