LLVM  10.0.0svn
Public Types | Public Member Functions | Static Public Member Functions | List of all members
llvm::Loop::LoopBounds Struct Reference

Below are some utilities to get loop bounds and induction variable, and check if a given phinode is an auxiliary induction variable, as well as checking if the loop is canonical. More...

#include "llvm/Analysis/LoopInfo.h"

Public Types

enum  Direction { Direction::Increasing, Direction::Decreasing, Direction::Unknown }
 An enum for the direction of the loop. More...
 

Public Member Functions

ValuegetInitialIVValue () const
 Get the initial value of the loop induction variable. More...
 
InstructiongetStepInst () const
 Get the instruction that updates the loop induction variable. More...
 
ValuegetStepValue () const
 Get the step that the loop induction variable gets updated by in each loop iteration. More...
 
ValuegetFinalIVValue () const
 Get the final value of the loop induction variable. More...
 
ICmpInst::Predicate getCanonicalPredicate () const
 Return the canonical predicate for the latch compare instruction, if able to be calcuated. More...
 
Direction getDirection () const
 Get the direction of the loop. More...
 

Static Public Member Functions

static Optional< Loop::LoopBoundsgetBounds (const Loop &L, PHINode &IndVar, ScalarEvolution &SE)
 Return the LoopBounds object if. More...
 

Detailed Description

Below are some utilities to get loop bounds and induction variable, and check if a given phinode is an auxiliary induction variable, as well as checking if the loop is canonical.

Here is an example:

for (int i = lb; i < ub; i+=step)
<loop body>
--- pseudo LLVMIR ---
beforeloop:
guardcmp = (lb < ub)
if (guardcmp) goto preheader; else goto afterloop
loop:
i_1 = phi[{lb, preheader}, {i_2, latch}]
<loop body>
i_2 = i_1 + step
latch:
cmp = (i_2 < ub)
if (cmp) goto loop
exit:
afterloop:

Definition at line 608 of file LoopInfo.h.

Member Enumeration Documentation

◆ Direction

An enum for the direction of the loop.

  • for (int i = 0; i < ub; ++i) –> Increasing
  • for (int i = ub; i > 0; –i) –> Descresing
  • for (int i = x; i != y; i+=z) –> Unknown
Enumerator
Increasing 
Decreasing 
Unknown 

Definition at line 671 of file LoopInfo.h.

Member Function Documentation

◆ getBounds()

Optional< Loop::LoopBounds > Loop::LoopBounds::getBounds ( const Loop L,
PHINode IndVar,
ScalarEvolution SE 
)
static

Return the LoopBounds object if.

  • the given IndVar is an induction variable
  • the initial value of the induction variable can be found
  • the step instruction of the induction variable can be found
  • the final value of the induction variable can be found

Else None.

Definition at line 197 of file LoopInfo.cpp.

References findFinalIVValue(), llvm::InductionDescriptor::getInductionBinOp(), llvm::User::getOperand(), llvm::ScalarEvolution::getSCEV(), llvm::InductionDescriptor::getStartValue(), llvm::InductionDescriptor::getStep(), llvm::InductionDescriptor::isInductionPHI(), and llvm::None.

Referenced by llvm::Loop::getBounds().

◆ getCanonicalPredicate()

ICmpInst::Predicate Loop::LoopBounds::getCanonicalPredicate ( ) const

Return the canonical predicate for the latch compare instruction, if able to be calcuated.

Else BAD_ICMP_PREDICATE.

A predicate is considered as canonical if requirements below are all satisfied:

  1. The first successor of the latch branch is the loop header If not, inverse the predicate.
  2. One of the operands of the latch comparison is StepInst If not, and
    • if the current calcuated predicate is not ne or eq, flip the predicate.
    • else if the loop is increasing, return slt (notice that it is safe to change from ne or eq to sign compare)
    • else if the loop is decreasing, return sgt (notice that it is safe to change from ne or eq to sign compare)

Here is an example when both (1) and (2) are not satisfied:

loop.header:
%iv = phi [%initialiv, %loop.preheader], [%inc, %loop.header]
%inc = add %iv, %step
%cmp = slt %iv, %finaliv
br %cmp, %loop.exit, %loop.header
loop.exit:
  • The second successor of the latch branch is the loop header instead of the first successor (slt -> sge)
  • The first operand of the latch comparison (cmp) is the IndVar (iv) instead of the StepInst (inc) (sge -> sgt)

The predicate would be sgt if both (1) and (2) are satisfied. getCanonicalPredicate() returns sgt for this example. Note: The IR is not changed.

Definition at line 228 of file LoopInfo.cpp.

References assert(), llvm::CmpInst::BAD_ICMP_PREDICATE, D, llvm::dyn_cast(), llvm::BranchInst::getCondition(), llvm::CmpInst::getFlippedStrictnessPredicate(), llvm::BranchInst::getSuccessor(), llvm::CmpInst::getSwappedPredicate(), llvm::BasicBlock::getTerminator(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::CmpInst::ICMP_SGT, llvm::CmpInst::ICMP_SLT, and llvm::BranchInst::isConditional().

◆ getDirection()

Direction Loop::LoopBounds::getDirection ( ) const

Get the direction of the loop.

Definition at line 270 of file LoopInfo.cpp.

References llvm::Unknown.

◆ getFinalIVValue()

Value& llvm::Loop::LoopBounds::getFinalIVValue ( ) const
inline

Get the final value of the loop induction variable.

Definition at line 630 of file LoopInfo.h.

◆ getInitialIVValue()

Value& llvm::Loop::LoopBounds::getInitialIVValue ( ) const
inline

Get the initial value of the loop induction variable.

Definition at line 620 of file LoopInfo.h.

◆ getStepInst()

Instruction& llvm::Loop::LoopBounds::getStepInst ( ) const
inline

Get the instruction that updates the loop induction variable.

Definition at line 623 of file LoopInfo.h.

◆ getStepValue()

Value* llvm::Loop::LoopBounds::getStepValue ( ) const
inline

Get the step that the loop induction variable gets updated by in each loop iteration.

Return nullptr if not found.

Definition at line 627 of file LoopInfo.h.


The documentation for this struct was generated from the following files: