llvm.org GIT mirror llvm / 2ba4d7a
[PM] Port LoopIdiomRecognize Pass to new PM Summary: Port LoopIdiomRecognize Pass to new PM Reviewers: davidxl Subscribers: davide, sanjoy, mzolotukhin, llvm-commits Differential Revision: http://reviews.llvm.org/D22250 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@275202 91177308-0d34-0410-b5e6-96231b3b80d8 Dehao Chen 4 years ago
7 changed file(s) with 118 addition(s) and 40 deletion(s). Raw diff Collapse all Expand all
176176 void initializeLoopDeletionPass(PassRegistry&);
177177 void initializeLoopDistributePass(PassRegistry&);
178178 void initializeLoopExtractorPass(PassRegistry&);
179 void initializeLoopIdiomRecognizePass(PassRegistry&);
179 void initializeLoopIdiomRecognizeLegacyPassPass(PassRegistry&);
180180 void initializeLoopInfoWrapperPassPass(PassRegistry&);
181181 void initializeLoopInstSimplifyPass(PassRegistry&);
182182 void initializeLoopInterchangePass(PassRegistry &);
0 //===- LoopIdiomRecognize.h - Loop Idiom Recognize Pass -------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This pass implements an idiom recognizer that transforms simple loops into a
10 // non-loop form. In cases that this kicks in, it can be a significant
11 // performance win.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_TRANSFORMS_SCALAR_LOOPIDIOMRECOGNIZE_H
16 #define LLVM_TRANSFORMS_SCALAR_LOOPIDIOMRECOGNIZE_H
17
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/IR/PassManager.h"
20
21 namespace llvm {
22
23 /// Performs Loop Idiom Recognize Pass.
24 class LoopIdiomRecognizePass : public PassInfoMixin {
25 public:
26 PreservedAnalyses run(Loop &L, AnalysisManager &AM);
27 };
28 } // end namespace llvm
29
30 #endif // LLVM_TRANSFORMS_SCALAR_LOOPIDIOMRECOGNIZE_H
8787 #include "llvm/Transforms/Scalar/GuardWidening.h"
8888 #include "llvm/Transforms/Scalar/IndVarSimplify.h"
8989 #include "llvm/Transforms/Scalar/JumpThreading.h"
90 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
9091 #include "llvm/Transforms/Scalar/LoopRotation.h"
9192 #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
9293 #include "llvm/Transforms/Scalar/LowerAtomic.h"
186186 #define LOOP_PASS(NAME, CREATE_PASS)
187187 #endif
188188 LOOP_PASS("invalidate", InvalidateAllAnalysesPass())
189 LOOP_PASS("loop-idiom", LoopIdiomRecognizePass())
189190 LOOP_PASS("rotate", LoopRotatePass())
190191 LOOP_PASS("no-op-loop", NoOpLoopPass())
191192 LOOP_PASS("print", PrintLoopPass(dbgs()))
3030 //
3131 //===----------------------------------------------------------------------===//
3232
33 #include "llvm/Transforms/Scalar.h"
33 #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
3434 #include "llvm/ADT/MapVector.h"
3535 #include "llvm/ADT/SetVector.h"
3636 #include "llvm/ADT/Statistic.h"
3737 #include "llvm/Analysis/AliasAnalysis.h"
3838 #include "llvm/Analysis/BasicAliasAnalysis.h"
3939 #include "llvm/Analysis/GlobalsModRef.h"
40 #include "llvm/Analysis/LoopAccessAnalysis.h"
4041 #include "llvm/Analysis/LoopPass.h"
41 #include "llvm/Analysis/LoopAccessAnalysis.h"
42 #include "llvm/Analysis/LoopPassManager.h"
4243 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
4344 #include "llvm/Analysis/ScalarEvolutionExpander.h"
4445 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
5253 #include "llvm/IR/Module.h"
5354 #include "llvm/Support/Debug.h"
5455 #include "llvm/Support/raw_ostream.h"
56 #include "llvm/Transforms/Scalar.h"
5557 #include "llvm/Transforms/Utils/BuildLibCalls.h"
5658 #include "llvm/Transforms/Utils/Local.h"
5759 #include "llvm/Transforms/Utils/LoopUtils.h"
6466
6567 namespace {
6668
67 class LoopIdiomRecognize : public LoopPass {
69 class LoopIdiomRecognize {
6870 Loop *CurLoop;
6971 AliasAnalysis *AA;
7072 DominatorTree *DT;
7577 const DataLayout *DL;
7678
7779 public:
78 static char ID;
79 explicit LoopIdiomRecognize() : LoopPass(ID) {
80 initializeLoopIdiomRecognizePass(*PassRegistry::getPassRegistry());
81 }
82
83 bool runOnLoop(Loop *L, LPPassManager &LPM) override;
84
85 /// This transformation requires natural loop information & requires that
86 /// loop preheaders be inserted into the CFG.
87 ///
88 void getAnalysisUsage(AnalysisUsage &AU) const override {
89 AU.addRequired();
90 AU.addRequired();
91 getLoopAnalysisUsage(AU);
92 }
80 explicit LoopIdiomRecognize(AliasAnalysis *AA, DominatorTree *DT,
81 LoopInfo *LI, ScalarEvolution *SE,
82 TargetLibraryInfo *TLI,
83 const TargetTransformInfo *TTI,
84 const DataLayout *DL)
85 : CurLoop(nullptr), AA(AA), DT(DT), LI(LI), SE(SE), TLI(TLI), TTI(TTI),
86 DL(DL) {}
87
88 bool runOnLoop(Loop *L);
9389
9490 private:
9591 typedef SmallVector StoreList;
136132 /// @}
137133 };
138134
135 class LoopIdiomRecognizeLegacyPass : public LoopPass {
136 public:
137 static char ID;
138 explicit LoopIdiomRecognizeLegacyPass() : LoopPass(ID) {
139 initializeLoopIdiomRecognizeLegacyPassPass(
140 *PassRegistry::getPassRegistry());
141 }
142
143 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
144 if (skipLoop(L))
145 return false;
146
147 AliasAnalysis *AA = &getAnalysis().getAAResults();
148 DominatorTree *DT = &getAnalysis().getDomTree();
149 LoopInfo *LI = &getAnalysis().getLoopInfo();
150 ScalarEvolution *SE = &getAnalysis().getSE();
151 TargetLibraryInfo *TLI =
152 &getAnalysis().getTLI();
153 const TargetTransformInfo *TTI =
154 &getAnalysis().getTTI(
155 *L->getHeader()->getParent());
156 const DataLayout *DL = &L->getHeader()->getModule()->getDataLayout();
157
158 LoopIdiomRecognize LIR(AA, DT, LI, SE, TLI, TTI, DL);
159 return LIR.runOnLoop(L);
160 }
161
162 /// This transformation requires natural loop information & requires that
163 /// loop preheaders be inserted into the CFG.
164 ///
165 void getAnalysisUsage(AnalysisUsage &AU) const override {
166 AU.addRequired();
167 AU.addRequired();
168 getLoopAnalysisUsage(AU);
169 }
170 };
139171 } // End anonymous namespace.
140172
141 char LoopIdiomRecognize::ID = 0;
142 INITIALIZE_PASS_BEGIN(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
143 false, false)
173 PreservedAnalyses LoopIdiomRecognizePass::run(Loop &L,
174 AnalysisManager &AM) {
175 const auto &FAM =
176 AM.getResult(L).getManager();
177 Function *F = L.getHeader()->getParent();
178
179 // Use getCachedResult because Loop pass cannot trigger a function analysis.
180 auto *AA = FAM.getCachedResult(*F);
181 auto *DT = FAM.getCachedResult(*F);
182 auto *LI = FAM.getCachedResult(*F);
183 auto *SE = FAM.getCachedResult(*F);
184 auto *TLI = FAM.getCachedResult(*F);
185 const auto *TTI = FAM.getCachedResult(*F);
186 const auto *DL = &L.getHeader()->getModule()->getDataLayout();
187 assert((AA && DT && LI && SE && TLI && TTI && DL) &&
188 "Analyses for Loop Idiom Recognition not available");
189
190 LoopIdiomRecognize LIR(AA, DT, LI, SE, TLI, TTI, DL);
191 if (!LIR.runOnLoop(&L))
192 return PreservedAnalyses::all();
193
194 return getLoopPassPreservedAnalyses();
195 }
196
197 char LoopIdiomRecognizeLegacyPass::ID = 0;
198 INITIALIZE_PASS_BEGIN(LoopIdiomRecognizeLegacyPass, "loop-idiom",
199 "Recognize loop idioms", false, false)
144200 INITIALIZE_PASS_DEPENDENCY(LoopPass)
145201 INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
146202 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
147 INITIALIZE_PASS_END(LoopIdiomRecognize, "loop-idiom", "Recognize loop idioms",
148 false, false)
149
150 Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognize(); }
203 INITIALIZE_PASS_END(LoopIdiomRecognizeLegacyPass, "loop-idiom",
204 "Recognize loop idioms", false, false)
205
206 Pass *llvm::createLoopIdiomPass() { return new LoopIdiomRecognizeLegacyPass(); }
151207
152208 static void deleteDeadInstruction(Instruction *I) {
153209 I->replaceAllUsesWith(UndefValue::get(I->getType()));
160216 //
161217 //===----------------------------------------------------------------------===//
162218
163 bool LoopIdiomRecognize::runOnLoop(Loop *L, LPPassManager &LPM) {
164 if (skipLoop(L))
165 return false;
166
219 bool LoopIdiomRecognize::runOnLoop(Loop *L) {
167220 CurLoop = L;
168221 // If the loop could not be converted to canonical form, it must have an
169222 // indirectbr in it, just give up.
174227 StringRef Name = L->getHeader()->getParent()->getName();
175228 if (Name == "memset" || Name == "memcpy")
176229 return false;
177
178 AA = &getAnalysis().getAAResults();
179 DT = &getAnalysis().getDomTree();
180 LI = &getAnalysis().getLoopInfo();
181 SE = &getAnalysis().getSE();
182 TLI = &getAnalysis().getTLI();
183 TTI = &getAnalysis().getTTI(
184 *CurLoop->getHeader()->getParent());
185 DL = &CurLoop->getHeader()->getModule()->getDataLayout();
186230
187231 HasMemset = TLI->has(LibFunc::memset);
188232 HasMemsetPattern = TLI->has(LibFunc::memset_pattern16);
5959 initializeLoopUnrollPass(Registry);
6060 initializeLoopUnswitchPass(Registry);
6161 initializeLoopVersioningLICMPass(Registry);
62 initializeLoopIdiomRecognizePass(Registry);
62 initializeLoopIdiomRecognizeLegacyPassPass(Registry);
6363 initializeLowerAtomicLegacyPassPass(Registry);
6464 initializeLowerExpectIntrinsicPass(Registry);
6565 initializeLowerGuardIntrinsicPass(Registry);
0 ; RUN: opt -loop-idiom < %s -S | FileCheck %s
1 ; RUN: opt -aa-pipeline=basic-aa -passes='require,require,require,loop(loop-idiom)' < %s -S | FileCheck %s
12 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
23 target triple = "x86_64-unknown-linux-gnu"
34