llvm.org GIT mirror llvm / 3445cc7
Port DemandedBits to the new pass manager. Differential Revision: http://reviews.llvm.org/D18679 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@266699 91177308-0d34-0410-b5e6-96231b3b80d8 Michael Kuperstein 4 years ago
11 changed file(s) with 123 addition(s) and 58 deletion(s). Raw diff Collapse all Expand all
2525 #include "llvm/ADT/APInt.h"
2626 #include "llvm/ADT/DenseMap.h"
2727 #include "llvm/ADT/SmallPtrSet.h"
28 #include "llvm/IR/PassManager.h"
2829
2930 namespace llvm {
3031
3435 class DominatorTree;
3536 class AssumptionCache;
3637
37 struct DemandedBits : public FunctionPass {
38 static char ID; // Pass identification, replacement for typeid
39 DemandedBits();
38 class DemandedBits {
39 public:
40 DemandedBits(Function &F, AssumptionCache &AC, DominatorTree &DT) :
41 F(F), AC(AC), DT(DT), Analyzed(false) {}
4042
41 bool runOnFunction(Function& F) override;
42 void getAnalysisUsage(AnalysisUsage& AU) const override;
43 void print(raw_ostream &OS, const Module *M) const override;
44
4543 /// Return the bits demanded from instruction I.
4644 APInt getDemandedBits(Instruction *I);
4745
4846 /// Return true if, during analysis, I could not be reached.
4947 bool isInstructionDead(Instruction *I);
48
49 void print(raw_ostream &OS);
5050
5151 private:
52 Function &F;
53 AssumptionCache ∾
54 DominatorTree &DT;
55
5256 void performAnalysis();
5357 void determineLiveOperandBits(const Instruction *UserI,
54 const Instruction *I, unsigned OperandNo,
55 const APInt &AOut, APInt &AB,
56 APInt &KnownZero, APInt &KnownOne,
57 APInt &KnownZero2, APInt &KnownOne2);
58 const Instruction *I, unsigned OperandNo,
59 const APInt &AOut, APInt &AB,
60 APInt &KnownZero, APInt &KnownOne,
61 APInt &KnownZero2, APInt &KnownOne2);
5862
59 AssumptionCache *AC;
60 DominatorTree *DT;
61 Function *F;
6263 bool Analyzed;
6364
6465 // The set of visited instructions (non-integer-typed only).
6667 DenseMap AliveBits;
6768 };
6869
70 class DemandedBitsWrapperPass : public FunctionPass {
71 private:
72 mutable Optional DB;
73 public:
74 static char ID; // Pass identification, replacement for typeid
75 DemandedBitsWrapperPass();
76
77 bool runOnFunction(Function &F) override;
78 void getAnalysisUsage(AnalysisUsage &AU) const override;
79
80 /// Clean up memory in between runs
81 void releaseMemory() override;
82
83 DemandedBits &getDemandedBits() { return *DB; }
84
85 void print(raw_ostream &OS, const Module *M) const override;
86 };
87
88 /// An analysis that produces \c DemandedBits for a function.
89 class DemandedBitsAnalysis : public AnalysisInfoMixin {
90 friend AnalysisInfoMixin;
91 static char PassID;
92
93 public:
94 /// \brief Provide the result typedef for this analysis pass.
95 typedef DemandedBits Result;
96
97 /// \brief Run the analysis pass over a function and produce demanded bits
98 /// information.
99 DemandedBits run(Function &F, AnalysisManager &AM);
100 };
101
102 /// \brief Printer pass for DemandedBits
103 class DemandedBitsPrinterPass : public PassInfoMixin {
104 raw_ostream &OS;
105
106 public:
107 explicit DemandedBitsPrinterPass(raw_ostream &OS) : OS(OS) {}
108 PreservedAnalyses run(Function &F, AnalysisManager &AM);
109 };
110
69111 /// Create a demanded bits analysis pass.
70 FunctionPass *createDemandedBitsPass();
112 FunctionPass *createDemandedBitsWrapperPass();
71113
72114 } // End llvm namespace
73115
2121 namespace llvm {
2222
2323 template class ArrayRef;
24 struct DemandedBits;
24 class DemandedBits;
2525 class GetElementPtrInst;
2626 class Loop;
2727 class ScalarEvolution;
322322 void initializeFloat2IntPass(PassRegistry&);
323323 void initializeLoopDistributePass(PassRegistry&);
324324 void initializeSjLjEHPreparePass(PassRegistry&);
325 void initializeDemandedBitsPass(PassRegistry&);
325 void initializeDemandedBitsWrapperPassPass(PassRegistry&);
326326 void initializeFuncletLayoutPass(PassRegistry &);
327327 void initializeLoopLoadEliminationPass(PassRegistry&);
328328 void initializeFunctionImportPassPass(PassRegistry &);
3636 initializeCFLAAWrapperPassPass(Registry);
3737 initializeDependenceAnalysisPass(Registry);
3838 initializeDelinearizationPass(Registry);
39 initializeDemandedBitsPass(Registry);
39 initializeDemandedBitsWrapperPassPass(Registry);
4040 initializeDivergenceAnalysisPass(Registry);
4141 initializeDominanceFrontierWrapperPassPass(Registry);
4242 initializeDomViewerPass(Registry);
4141
4242 #define DEBUG_TYPE "demanded-bits"
4343
44 char DemandedBits::ID = 0;
45 INITIALIZE_PASS_BEGIN(DemandedBits, "demanded-bits", "Demanded bits analysis",
46 false, false)
44 char DemandedBitsWrapperPass::ID = 0;
45 INITIALIZE_PASS_BEGIN(DemandedBitsWrapperPass, "demanded-bits",
46 "Demanded bits analysis", false, false)
4747 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
4848 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
49 INITIALIZE_PASS_END(DemandedBits, "demanded-bits", "Demanded bits analysis",
50 false, false)
51
52 DemandedBits::DemandedBits() : FunctionPass(ID), F(nullptr), Analyzed(false) {
53 initializeDemandedBitsPass(*PassRegistry::getPassRegistry());
54 }
55
56 void DemandedBits::getAnalysisUsage(AnalysisUsage &AU) const {
49 INITIALIZE_PASS_END(DemandedBitsWrapperPass, "demanded-bits",
50 "Demanded bits analysis", false, false)
51
52 DemandedBitsWrapperPass::DemandedBitsWrapperPass() : FunctionPass(ID) {
53 initializeDemandedBitsWrapperPassPass(*PassRegistry::getPassRegistry());
54 }
55
56 void DemandedBitsWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
5757 AU.setPreservesCFG();
5858 AU.addRequired();
5959 AU.addRequired();
6060 AU.setPreservesAll();
61 }
62
63 void DemandedBitsWrapperPass::print(raw_ostream &OS, const Module *M) const {
64 DB->print(OS);
6165 }
6266
6367 static bool isAlwaysLive(Instruction *I) {
8387 KnownZero = APInt(BitWidth, 0);
8488 KnownOne = APInt(BitWidth, 0);
8589 computeKnownBits(const_cast(V1), KnownZero, KnownOne, DL, 0,
86 AC, UserI, DT);
90 &AC, UserI, &DT);
8791
8892 if (V2) {
8993 KnownZero2 = APInt(BitWidth, 0);
9094 KnownOne2 = APInt(BitWidth, 0);
9195 computeKnownBits(const_cast(V2), KnownZero2, KnownOne2, DL,
92 0, AC, UserI, DT);
96 0, &AC, UserI, &DT);
9397 }
9498 };
9599
242246 }
243247 }
244248
245 bool DemandedBits::runOnFunction(Function& Fn) {
246 F = &Fn;
247 Analyzed = false;
249 bool DemandedBitsWrapperPass::runOnFunction(Function &F) {
250 auto &AC = getAnalysis().getAssumptionCache(F);
251 auto &DT = getAnalysis().getDomTree();
252 DB.emplace(F, AC, DT);
248253 return false;
254 }
255
256 void DemandedBitsWrapperPass::releaseMemory() {
257 DB.reset();
249258 }
250259
251260 void DemandedBits::performAnalysis() {
253262 // Analysis already completed for this function.
254263 return;
255264 Analyzed = true;
256 AC = &getAnalysis().getAssumptionCache(*F);
257 DT = &getAnalysis().getDomTree();
258265
259266 Visited.clear();
260267 AliveBits.clear();
262269 SmallVector Worklist;
263270
264271 // Collect the set of "root" instructions that are known live.
265 for (Instruction &I : instructions(*F)) {
272 for (Instruction &I : instructions(F)) {
266273 if (!isAlwaysLive(&I))
267274 continue;
268275
367374 !isAlwaysLive(I);
368375 }
369376
370 void DemandedBits::print(raw_ostream &OS, const Module *M) const {
371 // This is gross. But the alternative is making all the state mutable
372 // just because of this one debugging method.
373 const_cast(this)->performAnalysis();
377 void DemandedBits::print(raw_ostream &OS) {
378 performAnalysis();
374379 for (auto &KV : AliveBits) {
375380 OS << "DemandedBits: 0x" << utohexstr(KV.second.getLimitedValue()) << " for "
376381 << *KV.first << "\n";
377382 }
378383 }
379384
380 FunctionPass *llvm::createDemandedBitsPass() {
381 return new DemandedBits();
382 }
385 FunctionPass *llvm::createDemandedBitsWrapperPass() {
386 return new DemandedBitsWrapperPass();
387 }
388
389 char DemandedBitsAnalysis::PassID;
390
391 DemandedBits DemandedBitsAnalysis::run(Function &F,
392 AnalysisManager &AM) {
393 auto &AC = AM.getResult(F);
394 auto &DT = AM.getResult(F);
395 return DemandedBits(F, AC, DT);
396 }
397
398 PreservedAnalyses DemandedBitsPrinterPass::run(Function &F,
399 FunctionAnalysisManager &AM) {
400 AM.getResult(F).print(OS);
401 return PreservedAnalyses::all();
402 }
2323 #include "llvm/Analysis/CFLAliasAnalysis.h"
2424 #include "llvm/Analysis/CGSCCPassManager.h"
2525 #include "llvm/Analysis/CallGraph.h"
26 #include "llvm/Analysis/DemandedBits.h"
2627 #include "llvm/Analysis/DominanceFrontier.h"
2728 #include "llvm/Analysis/GlobalsModRef.h"
2829 #include "llvm/Analysis/LazyCallGraph.h"
6767 FUNCTION_ANALYSIS("assumptions", AssumptionAnalysis())
6868 FUNCTION_ANALYSIS("domtree", DominatorTreeAnalysis())
6969 FUNCTION_ANALYSIS("postdomtree", PostDominatorTreeAnalysis())
70 FUNCTION_ANALYSIS("demanded-bits", DemandedBitsAnalysis())
7071 FUNCTION_ANALYSIS("domfrontier", DominanceFrontierAnalysis())
7172 FUNCTION_ANALYSIS("loops", LoopAnalysis())
7273 FUNCTION_ANALYSIS("memdep", MemoryDependenceAnalysis())
104105 FUNCTION_PASS("print", AssumptionPrinterPass(dbgs()))
105106 FUNCTION_PASS("print", DominatorTreePrinterPass(dbgs()))
106107 FUNCTION_PASS("print", PostDominatorTreePrinterPass(dbgs()))
108 FUNCTION_PASS("print", DemandedBitsPrinterPass(dbgs()))
107109 FUNCTION_PASS("print", DominanceFrontierPrinterPass(dbgs()))
108110 FUNCTION_PASS("print", LoopPrinterPass(dbgs()))
109111 FUNCTION_PASS("print", RegionInfoPrinterPass(dbgs()))
4444
4545 void getAnalysisUsage(AnalysisUsage& AU) const override {
4646 AU.setPreservesCFG();
47 AU.addRequired>();
47 AU.addRequiredWrapperPass>();
4848 AU.addPreserved();
4949 }
5050 };
5353 char BDCE::ID = 0;
5454 INITIALIZE_PASS_BEGIN(BDCE, "bdce", "Bit-Tracking Dead Code Elimination",
5555 false, false)
56 INITIALIZE_PASS_DEPENDENCY(DemandedBits)
56 INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
5757 INITIALIZE_PASS_END(BDCE, "bdce", "Bit-Tracking Dead Code Elimination",
5858 false, false)
5959
6060 bool BDCE::runOnFunction(Function& F) {
6161 if (skipOptnoneFunction(F))
6262 return false;
63 DemandedBits &DB = getAnalysis();
63 auto &DB = getAnalysis().getDemandedBits();
6464
6565 SmallVector Worklist;
6666 bool Changed = false;
14971497 LoopVectorizationCostModel(Loop *L, ScalarEvolution *SE, LoopInfo *LI,
14981498 LoopVectorizationLegality *Legal,
14991499 const TargetTransformInfo &TTI,
1500 const TargetLibraryInfo *TLI, DemandedBits *DB,
1501 AssumptionCache *AC, const Function *F,
1502 const LoopVectorizeHints *Hints,
1500 const TargetLibraryInfo *TLI,
1501 DemandedBits *DB, AssumptionCache *AC,
1502 const Function *F, const LoopVectorizeHints *Hints,
15031503 SmallPtrSetImpl &ValuesToIgnore)
15041504 : TheLoop(L), SE(SE), LI(LI), Legal(Legal), TTI(TTI), TLI(TLI), DB(DB),
15051505 TheFunction(F), Hints(Hints), ValuesToIgnore(ValuesToIgnore) {}
17161716 AA = &getAnalysis().getAAResults();
17171717 AC = &getAnalysis().getAssumptionCache(F);
17181718 LAA = &getAnalysis();
1719 DB = &getAnalysis>();
1719 DB = &getAnalysisWrapperPass>().getDemandedBits();
17201720
17211721 // Compute some weights outside of the loop over the loops. Compute this
17221722 // using a BranchProbability to re-use its scaling math.
20342034 AU.addRequired();
20352035 AU.addRequired();
20362036 AU.addRequired();
2037 AU.addRequired>();
2037 AU.addRequiredWrapperPass>();
20382038 AU.addPreserved();
20392039 AU.addPreserved();
20402040 AU.addPreserved();
60636063 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
60646064 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
60656065 INITIALIZE_PASS_DEPENDENCY(LoopAccessAnalysis)
6066 INITIALIZE_PASS_DEPENDENCY(DemandedBits)
6066 INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
60676067 INITIALIZE_PASS_END(LoopVectorize, LV_NAME, lv_name, false, false)
60686068
60696069 namespace llvm {
34063406 LI = &getAnalysis().getLoopInfo();
34073407 DT = &getAnalysis().getDomTree();
34083408 AC = &getAnalysis().getAssumptionCache(F);
3409 DB = &getAnalysis>();
3409 DB = &getAnalysisWrapperPass>().getDemandedBits();
34103410
34113411 Stores.clear();
34123412 GEPs.clear();
34833483 AU.addRequired();
34843484 AU.addRequired();
34853485 AU.addRequired();
3486 AU.addRequired>();
3486 AU.addRequiredWrapperPass>();
34873487 AU.addPreserved();
34883488 AU.addPreserved();
34893489 AU.addPreserved();
45744574 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
45754575 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
45764576 INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
4577 INITIALIZE_PASS_DEPENDENCY(DemandedBits)
4577 INITIALIZE_PASS_DEPENDENCY(DemandedBitsWrapperPass)
45784578 INITIALIZE_PASS_END(SLPVectorizer, SV_NAME, lv_name, false, false)
45794579
45804580 namespace llvm {
0 ; RUN: opt -S -demanded-bits -analyze < %s | FileCheck %s
1
2 ; CHECK-LABEL: 'test_mul'
1 ; RUN: opt -S -disable-output -passes="print" < %s 2>&1 | FileCheck %s
2
33 ; CHECK-DAG: DemandedBits: 0xFF for %1 = add nsw i32 %a, 5
44 ; CHECK-DAG: DemandedBits: 0xFF for %3 = trunc i32 %2 to i8
55 ; CHECK-DAG: DemandedBits: 0xFF for %2 = mul nsw i32 %1, %b