LoopIdiomRecognize.cpp [plain text]
#include "llvm/Transforms/Scalar.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Analysis/ScalarEvolutionExpander.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.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/Target/TargetLibraryInfo.h"
#include "llvm/Transforms/Utils/Local.h"
using namespace llvm;
#define DEBUG_TYPE "loop-idiom"
STATISTIC(NumMemSet, "Number of memset's formed from loop stores");
STATISTIC(NumMemCpy, "Number of memcpy's formed from loop load+stores");
namespace {
class LoopIdiomRecognize;
class LIRUtil {
public:
static bool isAlmostEmpty(BasicBlock *);
static BranchInst *getBranch(BasicBlock *BB) {
return dyn_cast<BranchInst>(BB->getTerminator());
}
static BasicBlock *getPrecondBb(BasicBlock *PreHead);
};
class NclPopcountRecognize {
LoopIdiomRecognize &LIR;
Loop *CurLoop;
BasicBlock *PreCondBB;
typedef IRBuilder<> IRBuilderTy;
public:
explicit NclPopcountRecognize(LoopIdiomRecognize &TheLIR);
bool recognize();
private:
bool preliminaryScreen();
Value *matchCondition(BranchInst *Br, BasicBlock *NonZeroTarget) const;
bool detectIdiom
(Instruction *&CntInst, PHINode *&CntPhi, Value *&Var) const;
void transform(Instruction *CntInst, PHINode *CntPhi, Value *Var);
CallInst *createPopcntIntrinsic(IRBuilderTy &IRB, Value *Val, DebugLoc DL);
};
class LoopIdiomRecognize : public LoopPass {
Loop *CurLoop;
const DataLayout *DL;
DominatorTree *DT;
ScalarEvolution *SE;
TargetLibraryInfo *TLI;
const TargetTransformInfo *TTI;
public:
static char ID;
explicit LoopIdiomRecognize() : LoopPass(ID) {
initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
DL = nullptr; DT = nullptr; SE = nullptr; TLI = nullptr; TTI = nullptr;
}
bool runOnLoop(Loop *L, LPPassManager &LPM) override;
bool runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
SmallVectorImpl<BasicBlock*> &ExitBlocks);
bool processLoopStore(StoreInst *SI, const SCEV *BECount);
bool processLoopMemSet(MemSetInst *MSI, const SCEV *BECount);
bool processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
unsigned StoreAlignment,
Value *SplatValue, Instruction *TheStore,
const SCEVAddRecExpr *Ev,
const SCEV *BECount);
bool processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize,
const SCEVAddRecExpr *StoreEv,
const SCEVAddRecExpr *LoadEv,
const SCEV *BECount);
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.addRequired<LoopInfo>();
AU.addPreserved<LoopInfo>();
AU.addRequiredID(LoopSimplifyID);
AU.addPreservedID(LoopSimplifyID);
AU.addRequiredID(LCSSAID);
AU.addPreservedID(LCSSAID);
AU.addRequired<AliasAnalysis>();
AU.addPreserved<AliasAnalysis>();
AU.addRequired<ScalarEvolution>();
AU.addPreserved<ScalarEvolution>();
AU.addPreserved<DominatorTreeWrapperPass>();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addRequired<TargetLibraryInfo>();
AU.addRequired<TargetTransformInfo>();
}
const DataLayout *getDataLayout() {
if (DL)
return DL;
DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
DL = DLP ? &DLP->getDataLayout() : nullptr;
return DL;
}
DominatorTree *getDominatorTree() {
return DT ? DT
: (DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree());
}
ScalarEvolution *getScalarEvolution() {
return SE ? SE : (SE = &getAnalysis<ScalarEvolution>());
}
TargetLibraryInfo *getTargetLibraryInfo() {
return TLI ? TLI : (TLI = &getAnalysis<TargetLibraryInfo>());
}
const TargetTransformInfo *getTargetTransformInfo() {
return TTI ? TTI : (TTI = &getAnalysis<TargetTransformInfo>());
}
Loop *getLoop() const { return CurLoop; }
private:
bool runOnNoncountableLoop();
bool runOnCountableLoop();
};
}
char LoopIdiomRecognize::ID = 0;
INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
false, false)
INITIALIZE_PASS_DEPENDENCY(LoopInfo)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
INITIALIZE_PASS_DEPENDENCY(LCSSA)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
INITIALIZE_AG_DEPENDENCY(TargetTransformInfo)
INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
false, false)
Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); }
static void deleteDeadInstruction(Instruction *I, ScalarEvolution &SE,
const TargetLibraryInfo *TLI) {
SmallVector<Instruction*, 32> NowDeadInsts;
NowDeadInsts.push_back(I);
do {
Instruction *DeadInst = NowDeadInsts.pop_back_val();
SE.forgetValue(DeadInst);
for (unsigned op = 0, e = DeadInst->getNumOperands(); op != e; ++op) {
Value *Op = DeadInst->getOperand(op);
DeadInst->setOperand(op, nullptr);
if (!Op->use_empty()) continue;
if (Instruction *OpI = dyn_cast<Instruction>(Op))
if (isInstructionTriviallyDead(OpI, TLI))
NowDeadInsts.push_back(OpI);
}
DeadInst->eraseFromParent();
} while (!NowDeadInsts.empty());
}
static void deleteIfDeadInstruction(Value *V, ScalarEvolution &SE,
const TargetLibraryInfo *TLI) {
if (Instruction *I = dyn_cast<Instruction>(V))
if (isInstructionTriviallyDead(I, TLI))
deleteDeadInstruction(I, SE, TLI);
}
bool LIRUtil::isAlmostEmpty(BasicBlock *BB) {
if (BranchInst *Br = getBranch(BB)) {
return Br->isUnconditional() && BB->size() == 1;
}
return false;
}
BasicBlock *LIRUtil::getPrecondBb(BasicBlock *PreHead) {
if (BasicBlock *BB = PreHead->getSinglePredecessor()) {
BranchInst *Br = getBranch(BB);
return Br && Br->isConditional() ? BB : nullptr;
}
return nullptr;
}
NclPopcountRecognize::NclPopcountRecognize(LoopIdiomRecognize &TheLIR):
LIR(TheLIR), CurLoop(TheLIR.getLoop()), PreCondBB(nullptr) {
}
bool NclPopcountRecognize::preliminaryScreen() {
const TargetTransformInfo *TTI = LIR.getTargetTransformInfo();
if (TTI->getPopcntSupport(32) != TargetTransformInfo::PSK_FastHardware)
return false;
if (CurLoop->getNumBackEdges() != 1 || CurLoop->getNumBlocks() != 1)
return false;
BasicBlock *LoopBody = *(CurLoop->block_begin());
if (LoopBody->size() >= 20) {
return false;
}
BasicBlock *PreHead = CurLoop->getLoopPreheader();
if (!PreHead || !LIRUtil::isAlmostEmpty(PreHead))
return false;
PreCondBB = LIRUtil::getPrecondBb(PreHead);
if (!PreCondBB)
return false;
return true;
}
Value *NclPopcountRecognize::matchCondition(BranchInst *Br,
BasicBlock *LoopEntry) const {
if (!Br || !Br->isConditional())
return nullptr;
ICmpInst *Cond = dyn_cast<ICmpInst>(Br->getCondition());
if (!Cond)
return nullptr;
ConstantInt *CmpZero = dyn_cast<ConstantInt>(Cond->getOperand(1));
if (!CmpZero || !CmpZero->isZero())
return nullptr;
ICmpInst::Predicate Pred = Cond->getPredicate();
if ((Pred == ICmpInst::ICMP_NE && Br->getSuccessor(0) == LoopEntry) ||
(Pred == ICmpInst::ICMP_EQ && Br->getSuccessor(1) == LoopEntry))
return Cond->getOperand(0);
return nullptr;
}
bool NclPopcountRecognize::detectIdiom(Instruction *&CntInst,
PHINode *&CntPhi,
Value *&Var) const {
BasicBlock *LoopEntry;
Instruction *DefX2, *CountInst;
Value *VarX1, *VarX0;
PHINode *PhiX, *CountPhi;
DefX2 = CountInst = nullptr;
VarX1 = VarX0 = nullptr;
PhiX = CountPhi = nullptr;
LoopEntry = *(CurLoop->block_begin());
{
if (Value *T = matchCondition (LIRUtil::getBranch(LoopEntry), LoopEntry))
DefX2 = dyn_cast<Instruction>(T);
else
return false;
}
{
if (!DefX2 || DefX2->getOpcode() != Instruction::And)
return false;
BinaryOperator *SubOneOp;
if ((SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(0))))
VarX1 = DefX2->getOperand(1);
else {
VarX1 = DefX2->getOperand(0);
SubOneOp = dyn_cast<BinaryOperator>(DefX2->getOperand(1));
}
if (!SubOneOp)
return false;
Instruction *SubInst = cast<Instruction>(SubOneOp);
ConstantInt *Dec = dyn_cast<ConstantInt>(SubInst->getOperand(1));
if (!Dec ||
!((SubInst->getOpcode() == Instruction::Sub && Dec->isOne()) ||
(SubInst->getOpcode() == Instruction::Add && Dec->isAllOnesValue()))) {
return false;
}
}
{
PhiX = dyn_cast<PHINode>(VarX1);
if (!PhiX ||
(PhiX->getOperand(0) != DefX2 && PhiX->getOperand(1) != DefX2)) {
return false;
}
}
{
CountInst = nullptr;
for (BasicBlock::iterator Iter = LoopEntry->getFirstNonPHI(),
IterE = LoopEntry->end(); Iter != IterE; Iter++) {
Instruction *Inst = Iter;
if (Inst->getOpcode() != Instruction::Add)
continue;
ConstantInt *Inc = dyn_cast<ConstantInt>(Inst->getOperand(1));
if (!Inc || !Inc->isOne())
continue;
PHINode *Phi = dyn_cast<PHINode>(Inst->getOperand(0));
if (!Phi || Phi->getParent() != LoopEntry)
continue;
bool LiveOutLoop = false;
for (User *U : Inst->users()) {
if ((cast<Instruction>(U))->getParent() != LoopEntry) {
LiveOutLoop = true; break;
}
}
if (LiveOutLoop) {
CountInst = Inst;
CountPhi = Phi;
break;
}
}
if (!CountInst)
return false;
}
{
BranchInst *PreCondBr = LIRUtil::getBranch(PreCondBB);
Value *T = matchCondition (PreCondBr, CurLoop->getLoopPreheader());
if (T != PhiX->getOperand(0) && T != PhiX->getOperand(1))
return false;
CntInst = CountInst;
CntPhi = CountPhi;
Var = T;
}
return true;
}
void NclPopcountRecognize::transform(Instruction *CntInst,
PHINode *CntPhi, Value *Var) {
ScalarEvolution *SE = LIR.getScalarEvolution();
TargetLibraryInfo *TLI = LIR.getTargetLibraryInfo();
BasicBlock *PreHead = CurLoop->getLoopPreheader();
BranchInst *PreCondBr = LIRUtil::getBranch(PreCondBB);
const DebugLoc DL = CntInst->getDebugLoc();
IRBuilderTy Builder(PreCondBr);
Value *PopCnt, *PopCntZext, *NewCount, *TripCnt;
{
PopCnt = createPopcntIntrinsic(Builder, Var, DL);
NewCount = PopCntZext =
Builder.CreateZExtOrTrunc(PopCnt, cast<IntegerType>(CntPhi->getType()));
if (NewCount != PopCnt)
(cast<Instruction>(NewCount))->setDebugLoc(DL);
TripCnt = NewCount;
Value *CntInitVal = CntPhi->getIncomingValueForBlock(PreHead);
ConstantInt *InitConst = dyn_cast<ConstantInt>(CntInitVal);
if (!InitConst || !InitConst->isZero()) {
NewCount = Builder.CreateAdd(NewCount, CntInitVal);
(cast<Instruction>(NewCount))->setDebugLoc(DL);
}
}
{
ICmpInst *PreCond = cast<ICmpInst>(PreCondBr->getCondition());
Value *Opnd0 = PopCntZext;
Value *Opnd1 = ConstantInt::get(PopCntZext->getType(), 0);
if (PreCond->getOperand(0) != Var)
std::swap(Opnd0, Opnd1);
ICmpInst *NewPreCond =
cast<ICmpInst>(Builder.CreateICmp(PreCond->getPredicate(), Opnd0, Opnd1));
PreCond->replaceAllUsesWith(NewPreCond);
deleteDeadInstruction(PreCond, *SE, TLI);
}
BasicBlock *Body = *(CurLoop->block_begin());
{
BranchInst *LbBr = LIRUtil::getBranch(Body);
ICmpInst *LbCond = cast<ICmpInst>(LbBr->getCondition());
Type *Ty = TripCnt->getType();
PHINode *TcPhi = PHINode::Create(Ty, 2, "tcphi", Body->begin());
Builder.SetInsertPoint(LbCond);
Value *Opnd1 = cast<Value>(TcPhi);
Value *Opnd2 = cast<Value>(ConstantInt::get(Ty, 1));
Instruction *TcDec =
cast<Instruction>(Builder.CreateSub(Opnd1, Opnd2, "tcdec", false, true));
TcPhi->addIncoming(TripCnt, PreHead);
TcPhi->addIncoming(TcDec, Body);
CmpInst::Predicate Pred = (LbBr->getSuccessor(0) == Body) ?
CmpInst::ICMP_UGT : CmpInst::ICMP_SLE;
LbCond->setPredicate(Pred);
LbCond->setOperand(0, TcDec);
LbCond->setOperand(1, cast<Value>(ConstantInt::get(Ty, 0)));
}
{
SmallVector<Value *, 4> CntUses;
for (User *U : CntInst->users())
if (cast<Instruction>(U)->getParent() != Body)
CntUses.push_back(U);
for (unsigned Idx = 0; Idx < CntUses.size(); Idx++) {
(cast<Instruction>(CntUses[Idx]))->replaceUsesOfWith(CntInst, NewCount);
}
}
SE->forgetLoop(CurLoop);
}
CallInst *NclPopcountRecognize::createPopcntIntrinsic(IRBuilderTy &IRBuilder,
Value *Val, DebugLoc DL) {
Value *Ops[] = { Val };
Type *Tys[] = { Val->getType() };
Module *M = (*(CurLoop->block_begin()))->getParent()->getParent();
Value *Func = Intrinsic::getDeclaration(M, Intrinsic::ctpop, Tys);
CallInst *CI = IRBuilder.CreateCall(Func, Ops);
CI->setDebugLoc(DL);
return CI;
}
bool NclPopcountRecognize::recognize() {
if (!LIR.getTargetTransformInfo())
return false;
LIR.getScalarEvolution();
if (!preliminaryScreen())
return false;
Instruction *CntInst;
PHINode *CntPhi;
Value *Val;
if (!detectIdiom(CntInst, CntPhi, Val))
return false;
transform(CntInst, CntPhi, Val);
return true;
}
bool LoopIdiomRecognize::runOnCountableLoop() {
const SCEV *BECount = SE->getBackedgeTakenCount(CurLoop);
if (isa<SCEVCouldNotCompute>(BECount)) return false;
if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
if (BECst->getValue()->getValue() == 0)
return false;
if (!getDataLayout())
return false;
(void)getDominatorTree();
LoopInfo &LI = getAnalysis<LoopInfo>();
TLI = &getAnalysis<TargetLibraryInfo>();
(void)getTargetLibraryInfo();
SmallVector<BasicBlock*, 8> ExitBlocks;
CurLoop->getUniqueExitBlocks(ExitBlocks);
DEBUG(dbgs() << "loop-idiom Scanning: F["
<< CurLoop->getHeader()->getParent()->getName()
<< "] Loop %" << CurLoop->getHeader()->getName() << "\n");
bool MadeChange = false;
for (Loop::block_iterator BI = CurLoop->block_begin(),
E = CurLoop->block_end(); BI != E; ++BI) {
if (LI.getLoopFor(*BI) != CurLoop)
continue;
MadeChange |= runOnLoopBlock(*BI, BECount, ExitBlocks);
}
return MadeChange;
}
bool LoopIdiomRecognize::runOnNoncountableLoop() {
NclPopcountRecognize Popcount(*this);
if (Popcount.recognize())
return true;
return false;
}
bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
if (skipOptnoneFunction(L))
return false;
CurLoop = L;
if (!L->getLoopPreheader())
return false;
StringRef Name = L->getHeader()->getParent()->getName();
if (Name == "memset" || Name == "memcpy")
return false;
SE = &getAnalysis<ScalarEvolution>();
if (SE->hasLoopInvariantBackedgeTakenCount(L))
return runOnCountableLoop();
return runOnNoncountableLoop();
}
bool LoopIdiomRecognize::runOnLoopBlock(BasicBlock *BB, const SCEV *BECount,
SmallVectorImpl<BasicBlock*> &ExitBlocks) {
for (unsigned i = 0, e = ExitBlocks.size(); i != e; ++i)
if (!DT->dominates(BB, ExitBlocks[i]))
return false;
bool MadeChange = false;
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
Instruction *Inst = I++;
if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
WeakVH InstPtr(I);
if (!processLoopStore(SI, BECount)) continue;
MadeChange = true;
if (!InstPtr)
I = BB->begin();
continue;
}
if (MemSetInst *MSI = dyn_cast<MemSetInst>(Inst)) {
WeakVH InstPtr(I);
if (!processLoopMemSet(MSI, BECount)) continue;
MadeChange = true;
if (!InstPtr)
I = BB->begin();
continue;
}
}
return MadeChange;
}
bool LoopIdiomRecognize::processLoopStore(StoreInst *SI, const SCEV *BECount) {
if (!SI->isSimple()) return false;
Value *StoredVal = SI->getValueOperand();
Value *StorePtr = SI->getPointerOperand();
uint64_t SizeInBits = DL->getTypeSizeInBits(StoredVal->getType());
if ((SizeInBits & 7) || (SizeInBits >> 32) != 0)
return false;
const SCEVAddRecExpr *StoreEv =
dyn_cast<SCEVAddRecExpr>(SE->getSCEV(StorePtr));
if (!StoreEv || StoreEv->getLoop() != CurLoop || !StoreEv->isAffine())
return false;
unsigned StoreSize = (unsigned)SizeInBits >> 3;
const SCEVConstant *Stride = dyn_cast<SCEVConstant>(StoreEv->getOperand(1));
if (!Stride || StoreSize != Stride->getValue()->getValue()) {
if (0 && Stride && StoreSize == -Stride->getValue()->getValue()) {
dbgs() << "NEGATIVE STRIDE: " << *SI << "\n";
dbgs() << "BB: " << *SI->getParent();
}
return false;
}
if (processLoopStridedStore(StorePtr, StoreSize, SI->getAlignment(),
StoredVal, SI, StoreEv, BECount))
return true;
if (LoadInst *LI = dyn_cast<LoadInst>(StoredVal)) {
const SCEVAddRecExpr *LoadEv =
dyn_cast<SCEVAddRecExpr>(SE->getSCEV(LI->getOperand(0)));
if (LoadEv && LoadEv->getLoop() == CurLoop && LoadEv->isAffine() &&
StoreEv->getOperand(1) == LoadEv->getOperand(1) && LI->isSimple())
if (processLoopStoreOfLoopLoad(SI, StoreSize, StoreEv, LoadEv, BECount))
return true;
}
return false;
}
bool LoopIdiomRecognize::
processLoopMemSet(MemSetInst *MSI, const SCEV *BECount) {
if (MSI->isVolatile() || !isa<ConstantInt>(MSI->getLength())) return false;
if (!TLI->has(LibFunc::memset))
return false;
Value *Pointer = MSI->getDest();
const SCEVAddRecExpr *Ev = dyn_cast<SCEVAddRecExpr>(SE->getSCEV(Pointer));
if (!Ev || Ev->getLoop() != CurLoop || !Ev->isAffine())
return false;
uint64_t SizeInBytes = cast<ConstantInt>(MSI->getLength())->getZExtValue();
if ((SizeInBytes >> 32) != 0)
return false;
const SCEVConstant *Stride = dyn_cast<SCEVConstant>(Ev->getOperand(1));
if (!Stride || MSI->getLength() != Stride->getValue())
return false;
return processLoopStridedStore(Pointer, (unsigned)SizeInBytes,
MSI->getAlignment(), MSI->getValue(),
MSI, Ev, BECount);
}
static bool mayLoopAccessLocation(Value *Ptr,AliasAnalysis::ModRefResult Access,
Loop *L, const SCEV *BECount,
unsigned StoreSize, AliasAnalysis &AA,
Instruction *IgnoredStore) {
uint64_t AccessSize = AliasAnalysis::UnknownSize;
if (const SCEVConstant *BECst = dyn_cast<SCEVConstant>(BECount))
AccessSize = (BECst->getValue()->getZExtValue()+1)*StoreSize;
AliasAnalysis::Location StoreLoc(Ptr, AccessSize);
for (Loop::block_iterator BI = L->block_begin(), E = L->block_end(); BI != E;
++BI)
for (BasicBlock::iterator I = (*BI)->begin(), E = (*BI)->end(); I != E; ++I)
if (&*I != IgnoredStore &&
(AA.getModRefInfo(I, StoreLoc) & Access))
return true;
return false;
}
static Constant *getMemSetPatternValue(Value *V, const DataLayout &DL) {
Constant *C = dyn_cast<Constant>(V);
if (!C) return nullptr;
uint64_t Size = DL.getTypeSizeInBits(V->getType());
if (Size == 0 || (Size & 7) || (Size & (Size-1)))
return nullptr;
if (DL.isBigEndian())
return nullptr;
Size /= 8;
if (Size > 16) return nullptr;
if (Size == 16) return C;
unsigned ArraySize = 16/Size;
ArrayType *AT = ArrayType::get(V->getType(), ArraySize);
return ConstantArray::get(AT, std::vector<Constant*>(ArraySize, C));
}
bool LoopIdiomRecognize::
processLoopStridedStore(Value *DestPtr, unsigned StoreSize,
unsigned StoreAlignment, Value *StoredVal,
Instruction *TheStore, const SCEVAddRecExpr *Ev,
const SCEV *BECount) {
Value *SplatValue = isBytewiseValue(StoredVal);
Constant *PatternValue = nullptr;
unsigned DestAS = DestPtr->getType()->getPointerAddressSpace();
if (SplatValue && TLI->has(LibFunc::memset) &&
CurLoop->isLoopInvariant(SplatValue)) {
PatternValue = nullptr;
} else if (DestAS == 0 &&
TLI->has(LibFunc::memset_pattern16) &&
(PatternValue = getMemSetPatternValue(StoredVal, *DL))) {
SplatValue = nullptr;
} else {
return false;
}
BasicBlock *Preheader = CurLoop->getLoopPreheader();
IRBuilder<> Builder(Preheader->getTerminator());
SCEVExpander Expander(*SE, "loop-idiom");
Type *DestInt8PtrTy = Builder.getInt8PtrTy(DestAS);
Value *BasePtr =
Expander.expandCodeFor(Ev->getStart(), DestInt8PtrTy,
Preheader->getTerminator());
if (mayLoopAccessLocation(BasePtr, AliasAnalysis::ModRef,
CurLoop, BECount,
StoreSize, getAnalysis<AliasAnalysis>(), TheStore)) {
Expander.clear();
deleteIfDeadInstruction(BasePtr, *SE, TLI);
return false;
}
Type *IntPtr = Builder.getIntPtrTy(DL, DestAS);
BECount = SE->getTruncateOrZeroExtend(BECount, IntPtr);
const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtr, 1),
SCEV::FlagNUW);
if (StoreSize != 1) {
NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtr, StoreSize),
SCEV::FlagNUW);
}
Value *NumBytes =
Expander.expandCodeFor(NumBytesS, IntPtr, Preheader->getTerminator());
CallInst *NewCall;
if (SplatValue) {
NewCall = Builder.CreateMemSet(BasePtr,
SplatValue,
NumBytes,
StoreAlignment);
} else {
Type *Int8PtrTy = DestInt8PtrTy;
Module *M = TheStore->getParent()->getParent()->getParent();
Value *MSP = M->getOrInsertFunction("memset_pattern16",
Builder.getVoidTy(),
Int8PtrTy,
Int8PtrTy,
IntPtr,
(void*)nullptr);
GlobalVariable *GV = new GlobalVariable(*M, PatternValue->getType(), true,
GlobalValue::InternalLinkage,
PatternValue, ".memset_pattern");
GV->setUnnamedAddr(true); GV->setAlignment(16);
Value *PatternPtr = ConstantExpr::getBitCast(GV, Int8PtrTy);
NewCall = Builder.CreateCall3(MSP, BasePtr, PatternPtr, NumBytes);
}
DEBUG(dbgs() << " Formed memset: " << *NewCall << "\n"
<< " from store to: " << *Ev << " at: " << *TheStore << "\n");
NewCall->setDebugLoc(TheStore->getDebugLoc());
deleteDeadInstruction(TheStore, *SE, TLI);
++NumMemSet;
return true;
}
bool LoopIdiomRecognize::
processLoopStoreOfLoopLoad(StoreInst *SI, unsigned StoreSize,
const SCEVAddRecExpr *StoreEv,
const SCEVAddRecExpr *LoadEv,
const SCEV *BECount) {
if (!TLI->has(LibFunc::memcpy))
return false;
LoadInst *LI = cast<LoadInst>(SI->getValueOperand());
BasicBlock *Preheader = CurLoop->getLoopPreheader();
IRBuilder<> Builder(Preheader->getTerminator());
SCEVExpander Expander(*SE, "loop-idiom");
Value *StoreBasePtr =
Expander.expandCodeFor(StoreEv->getStart(),
Builder.getInt8PtrTy(SI->getPointerAddressSpace()),
Preheader->getTerminator());
if (mayLoopAccessLocation(StoreBasePtr, AliasAnalysis::ModRef,
CurLoop, BECount, StoreSize,
getAnalysis<AliasAnalysis>(), SI)) {
Expander.clear();
deleteIfDeadInstruction(StoreBasePtr, *SE, TLI);
return false;
}
Value *LoadBasePtr =
Expander.expandCodeFor(LoadEv->getStart(),
Builder.getInt8PtrTy(LI->getPointerAddressSpace()),
Preheader->getTerminator());
if (mayLoopAccessLocation(LoadBasePtr, AliasAnalysis::Mod, CurLoop, BECount,
StoreSize, getAnalysis<AliasAnalysis>(), SI)) {
Expander.clear();
deleteIfDeadInstruction(LoadBasePtr, *SE, TLI);
deleteIfDeadInstruction(StoreBasePtr, *SE, TLI);
return false;
}
Type *IntPtrTy = Builder.getIntPtrTy(DL, SI->getPointerAddressSpace());
BECount = SE->getTruncateOrZeroExtend(BECount, IntPtrTy);
const SCEV *NumBytesS = SE->getAddExpr(BECount, SE->getConstant(IntPtrTy, 1),
SCEV::FlagNUW);
if (StoreSize != 1)
NumBytesS = SE->getMulExpr(NumBytesS, SE->getConstant(IntPtrTy, StoreSize),
SCEV::FlagNUW);
Value *NumBytes =
Expander.expandCodeFor(NumBytesS, IntPtrTy, Preheader->getTerminator());
CallInst *NewCall =
Builder.CreateMemCpy(StoreBasePtr, LoadBasePtr, NumBytes,
std::min(SI->getAlignment(), LI->getAlignment()));
NewCall->setDebugLoc(SI->getDebugLoc());
DEBUG(dbgs() << " Formed memcpy: " << *NewCall << "\n"
<< " from load ptr=" << *LoadEv << " at: " << *LI << "\n"
<< " from store ptr=" << *StoreEv << " at: " << *SI << "\n");
deleteDeadInstruction(SI, *SE, TLI);
++NumMemCpy;
return true;
}