LLVM  4.0.0
Macros | Functions | Variables
LoopIdiomRecognize.cpp File Reference
#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/BasicAliasAnalysis.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/LoopAccessAnalysis.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
#include "llvm/Analysis/ScalarEvolutionExpander.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h"
#include "llvm/Analysis/TargetLibraryInfo.h"
#include "llvm/Analysis/TargetTransformInfo.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.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/BuildLibCalls.h"
#include "llvm/Transforms/Utils/Local.h"
#include "llvm/Transforms/Utils/LoopUtils.h"
Include dependency graph for LoopIdiomRecognize.cpp:

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "loop-idiom"
 

Functions

 STATISTIC (NumMemSet,"Number of memset's formed from loop stores")
 
 STATISTIC (NumMemCpy,"Number of memcpy's formed from loop load+stores")
 
 INITIALIZE_PASS_BEGIN (LoopIdiomRecognizeLegacyPass,"loop-idiom","Recognize loop idioms", false, false) INITIALIZE_PASS_END(LoopIdiomRecognizeLegacyPass
 
static void deleteDeadInstruction (Instruction *I)
 
static unsigned getStoreSizeInBytes (StoreInst *SI, const DataLayout *DL)
 
static APInt getStoreStride (const SCEVAddRecExpr *StoreEv)
 
static ConstantgetMemSetPatternValue (Value *V, const DataLayout *DL)
 getMemSetPatternValue - If a strided store of the specified value is safe to turn into a memset_pattern16, return a ConstantArray of 16 bytes that should be passed in. More...
 
static bool mayLoopAccessLocation (Value *Ptr, ModRefInfo Access, Loop *L, const SCEV *BECount, unsigned StoreSize, AliasAnalysis &AA, SmallPtrSetImpl< Instruction * > &IgnoredStores)
 mayLoopAccessLocation - Return true if the specified loop might access the specified pointer location, which is a loop-strided access. More...
 
static const SCEVgetStartForNegStride (const SCEV *Start, const SCEV *BECount, Type *IntPtr, unsigned StoreSize, ScalarEvolution *SE)
 
static ValuematchCondition (BranchInst *BI, BasicBlock *LoopEntry)
 Check if the given conditional branch is based on the comparison between a variable and zero, and if the variable is non-zero, the control yields to the loop entry. More...
 
static bool detectPopcountIdiom (Loop *CurLoop, BasicBlock *PreCondBB, Instruction *&CntInst, PHINode *&CntPhi, Value *&Var)
 Return true iff the idiom is detected in the loop. More...
 
static CallInstcreatePopcntIntrinsic (IRBuilder<> &IRBuilder, Value *Val, const DebugLoc &DL)
 

Variables

static cl::opt< boolUseLIRCodeSizeHeurs ("use-lir-code-size-heurs", cl::desc("Use loop idiom recognition code size heuristics when compiling""with -Os/-Oz"), cl::init(true), cl::Hidden)
 
loop idiom
 
loop Recognize loop idioms
 
loop Recognize loop false
 

Macro Definition Documentation

#define DEBUG_TYPE   "loop-idiom"

Definition at line 69 of file LoopIdiomRecognize.cpp.

Function Documentation

static CallInst* createPopcntIntrinsic ( IRBuilder<> &  IRBuilder,
Value Val,
const DebugLoc DL 
)
static
static void deleteDeadInstruction ( Instruction I)
static
static bool detectPopcountIdiom ( Loop CurLoop,
BasicBlock PreCondBB,
Instruction *&  CntInst,
PHINode *&  CntPhi,
Value *&  Var 
)
static

Return true iff the idiom is detected in the loop.

Additionally: 1) CntInst is set to the instruction counting the population bit. 2) CntPhi is set to the corresponding phi node. 3) Var is set to the value whose population bits are being counted.

The core idiom we are trying to detect is:

if (x0 != 0)
goto loop-exit // the precondition of the loop
cnt0 = init-val;
do {
x1 = phi (x0, x2);
cnt1 = phi(cnt0, cnt2);
cnt2 = cnt1 + 1;
...
x2 = x1 & (x1 - 1);
...
} while(x != 0);
loop-exit:

Definition at line 1034 of file LoopIdiomRecognize.cpp.

References llvm::MCID::Add, llvm::APIntOps::And(), llvm::LoopBase< N, M >::block_begin(), llvm::dyn_cast(), llvm::BasicBlock::end(), llvm::BasicBlock::getFirstNonPHI(), llvm::ilist_node_impl< OptionsT >::getIterator(), llvm::LoopBase< N, M >::getLoopPreheader(), llvm::Instruction::getOpcode(), llvm::User::getOperand(), llvm::Instruction::getParent(), llvm::BasicBlock::getTerminator(), llvm::ConstantInt::isOne(), matchCondition(), T, and llvm::Value::users().

static Constant* getMemSetPatternValue ( Value V,
const DataLayout DL 
)
static

getMemSetPatternValue - If a strided store of the specified value is safe to turn into a memset_pattern16, return a ConstantArray of 16 bytes that should be passed in.

Otherwise, return null.

Note that we don't ever attempt to use memset_pattern8 or 4, because these just replicate their input array and then pass on to memset_pattern16.

Definition at line 307 of file LoopIdiomRecognize.cpp.

References C, llvm::dyn_cast(), llvm::ArrayType::get(), llvm::ConstantArray::get(), llvm::Value::getType(), llvm::DataLayout::getTypeSizeInBits(), and llvm::DataLayout::isBigEndian().

static const SCEV* getStartForNegStride ( const SCEV Start,
const SCEV BECount,
Type IntPtr,
unsigned  StoreSize,
ScalarEvolution SE 
)
static
static unsigned getStoreSizeInBytes ( StoreInst SI,
const DataLayout DL 
)
static
static APInt getStoreStride ( const SCEVAddRecExpr StoreEv)
static
INITIALIZE_PASS_BEGIN ( LoopIdiomRecognizeLegacyPass  ,
"loop-idiom ,
"Recognize loop idioms ,
false  ,
false   
)
static Value* matchCondition ( BranchInst BI,
BasicBlock LoopEntry 
)
static

Check if the given conditional branch is based on the comparison between a variable and zero, and if the variable is non-zero, the control yields to the loop entry.

If the branch matches the behavior, the variable involved in the comparison is returned. This function will be called to see if the precondition and postcondition of the loop are in desirable form.

Definition at line 990 of file LoopIdiomRecognize.cpp.

References llvm::dyn_cast(), llvm::BranchInst::getCondition(), llvm::User::getOperand(), llvm::CmpInst::getPredicate(), llvm::BranchInst::getSuccessor(), llvm::CmpInst::ICMP_EQ, llvm::CmpInst::ICMP_NE, llvm::BranchInst::isConditional(), and llvm::ConstantInt::isZero().

Referenced by detectPopcountIdiom().

static bool mayLoopAccessLocation ( Value Ptr,
ModRefInfo  Access,
Loop L,
const SCEV BECount,
unsigned  StoreSize,
AliasAnalysis AA,
SmallPtrSetImpl< Instruction * > &  IgnoredStores 
)
static

mayLoopAccessLocation - Return true if the specified loop might access the specified pointer location, which is a loop-strided access.

The 'Access' argument specifies what the verboten forms of access are (read or write).

Definition at line 706 of file LoopIdiomRecognize.cpp.

References llvm::LoopBase< N, M >::block_begin(), llvm::LoopBase< N, M >::block_end(), llvm::SmallPtrSetImpl< PtrType >::count(), E, llvm::AAResults::getModRefInfo(), and llvm::MemoryLocation::UnknownSize.

STATISTIC ( NumMemSet  ,
"Number of memset's formed from loop stores  
)
STATISTIC ( NumMemCpy  ,
"Number of memcpy's formed from loop load+stores  
)

Variable Documentation

loop Recognize loop false

Definition at line 207 of file LoopIdiomRecognize.cpp.

loop idiom

Definition at line 207 of file LoopIdiomRecognize.cpp.

loop Recognize loop idioms

Definition at line 207 of file LoopIdiomRecognize.cpp.

cl::opt<bool> UseLIRCodeSizeHeurs("use-lir-code-size-heurs", cl::desc("Use loop idiom recognition code size heuristics when compiling""with -Os/-Oz"), cl::init(true), cl::Hidden)
static