blob: eb7d4930edfaa4c2134dfcb1fe0961d0b5dee86e [file] [log] [blame]
Chris Lattnere6bb6492010-12-26 19:39:38 +00001//===-- LoopIdiomRecognize.cpp - Loop idiom recognition -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This pass implements an idiom recognizer that transforms simple loops into a
11// non-loop form. In cases that this kicks in, it can be a significant
12// performance win.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "loop-idiom"
17#include "llvm/Transforms/Scalar.h"
18#include "llvm/Analysis/LoopPass.h"
19#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner22920b52010-12-26 20:45:45 +000020#include "llvm/Analysis/ValueTracking.h"
21#include "llvm/Target/TargetData.h"
Chris Lattnere6bb6492010-12-26 19:39:38 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
24using namespace llvm;
25
26// TODO: Recognize "N" size array multiplies: replace with call to blas or
27// something.
28
29namespace {
30 class LoopIdiomRecognize : public LoopPass {
Chris Lattner22920b52010-12-26 20:45:45 +000031 Loop *CurLoop;
32 const TargetData *TD;
33 ScalarEvolution *SE;
Chris Lattnere6bb6492010-12-26 19:39:38 +000034 public:
35 static char ID;
36 explicit LoopIdiomRecognize() : LoopPass(ID) {
37 initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
38 }
39
40 bool runOnLoop(Loop *L, LPPassManager &LPM);
41
Chris Lattner22920b52010-12-26 20:45:45 +000042 bool processLoopStore(StoreInst *SI, const SCEV *BECount);
Chris Lattnere6bb6492010-12-26 19:39:38 +000043
44 /// This transformation requires natural loop information & requires that
45 /// loop preheaders be inserted into the CFG.
46 ///
47 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequired<LoopInfo>();
49 AU.addPreserved<LoopInfo>();
50 AU.addRequiredID(LoopSimplifyID);
51 AU.addPreservedID(LoopSimplifyID);
52 AU.addRequiredID(LCSSAID);
53 AU.addPreservedID(LCSSAID);
54 AU.addRequired<ScalarEvolution>();
55 AU.addPreserved<ScalarEvolution>();
56 AU.addPreserved<DominatorTree>();
57 }
58 };
59}
60
61char LoopIdiomRecognize::ID = 0;
62INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
63 false, false)
64INITIALIZE_PASS_DEPENDENCY(LoopInfo)
65INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
66INITIALIZE_PASS_DEPENDENCY(LCSSA)
67INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
68INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
69 false, false)
70
71Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); }
72
73bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
Chris Lattner22920b52010-12-26 20:45:45 +000074 CurLoop = L;
75
Chris Lattnere6bb6492010-12-26 19:39:38 +000076 // We only look at trivial single basic block loops.
77 // TODO: eventually support more complex loops, scanning the header.
78 if (L->getBlocks().size() != 1)
79 return false;
80
Chris Lattner22920b52010-12-26 20:45:45 +000081 // The trip count of the loop must be analyzable.
82 SE = &getAnalysis<ScalarEvolution>();
83 if (!SE->hasLoopInvariantBackedgeTakenCount(L))
84 return false;
85 const SCEV *BECount = SE->getBackedgeTakenCount(L);
86 if (isa<SCEVCouldNotCompute>(BECount)) return false;
87
88 // We require target data for now.
89 TD = getAnalysisIfAvailable<TargetData>();
90 if (TD == 0) return false;
91
Chris Lattnere6bb6492010-12-26 19:39:38 +000092 BasicBlock *BB = L->getHeader();
Chris Lattner22920b52010-12-26 20:45:45 +000093 DEBUG(dbgs() << "loop-idiom Scanning: F[" << BB->getParent()->getName()
Chris Lattnere6bb6492010-12-26 19:39:38 +000094 << "] Loop %" << BB->getName() << "\n");
95
Chris Lattner22920b52010-12-26 20:45:45 +000096 bool MadeChange = false;
97 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) {
98 // Look for store instructions, which may be memsets.
99 if (StoreInst *SI = dyn_cast<StoreInst>(I++))
100 MadeChange |= processLoopStore(SI, BECount);
101 }
102
103 return MadeChange;
Chris Lattnere6bb6492010-12-26 19:39:38 +0000104}
105
106/// scanBlock - Look over a block to see if we can promote anything out of it.
Chris Lattner22920b52010-12-26 20:45:45 +0000107bool LoopIdiomRecognize::processLoopStore(StoreInst *SI, const SCEV *BECount) {
108 Value *StoredVal = SI->getValueOperand();
Chris Lattnere6bb6492010-12-26 19:39:38 +0000109
Chris Lattner22920b52010-12-26 20:45:45 +0000110 // Check to see if the store updates all bits in memory. We don't want to
111 // process things like a store of i3. We also require that the store be a
112 // multiple of a byte.
113 uint64_t SizeInBits = TD->getTypeSizeInBits(StoredVal->getType());
114 if ((SizeInBits & 7) || (SizeInBits >> 32) != 0 ||
115 SizeInBits != TD->getTypeStoreSizeInBits(StoredVal->getType()))
116 return false;
Chris Lattnere6bb6492010-12-26 19:39:38 +0000117
Chris Lattner22920b52010-12-26 20:45:45 +0000118 // See if the pointer expression is an AddRec like {base,+,1} on the current
119 // loop, which indicates a strided store. If we have something else, it's a
120 // random store we can't handle.
121 const SCEVAddRecExpr *Ev =
122 dyn_cast<SCEVAddRecExpr>(SE->getSCEV(SI->getPointerOperand()));
123 if (Ev == 0 || Ev->getLoop() != CurLoop || !Ev->isAffine())
124 return false;
125
126 // Check to see if the stride matches the size of the store. If so, then we
127 // know that every byte is touched in the loop.
128 unsigned StoreSize = (unsigned)SizeInBits >> 3;
129 const SCEVConstant *Stride = dyn_cast<SCEVConstant>(Ev->getOperand(1));
130 if (Stride == 0 || StoreSize != Stride->getValue()->getValue())
131 return false;
132
133 errs() << "Found strided store: " << *Ev << "\n";
134
135 // Check for memcpy here.
136
137
138 // If the stored value is a byte-wise value (like i32 -1), then it may be
139 // turned into a memset of i8 -1, assuming that all the consequtive bytes
140 // are stored. A store of i32 0x01020304 can never be turned into a memset.
141 Value *SplatValue = isBytewiseValue(StoredVal);
142 if (SplatValue == 0) return false;
143
144
Chris Lattnere6bb6492010-12-26 19:39:38 +0000145 return false;
146}
147