xref: /llvm-project/bolt/lib/Passes/DataflowAnalysis.cpp (revision a34c753fe709a624f5b087397fb05adeac2311e4)
1 //===--- Passes/DataflowAnalysis.cpp --------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //===----------------------------------------------------------------------===//
10 
11 #include "bolt/Passes/DataflowAnalysis.h"
12 
13 #define DEBUG_TYPE "dataflow"
14 
15 namespace llvm {
16 
17 raw_ostream &operator<<(raw_ostream &OS, const BitVector &State) {
18   LLVM_DEBUG({
19     OS << "BitVector(";
20     const char *Sep = "";
21     if (State.count() > (State.size() >> 1)) {
22       OS << "all, except: ";
23       BitVector BV = State;
24       BV.flip();
25       for (int I = BV.find_first(); I != -1; I = BV.find_next(I)) {
26         OS << Sep << I;
27         Sep = " ";
28       }
29       OS << ")";
30       return OS;
31     }
32     for (int I = State.find_first(); I != -1; I = State.find_next(I)) {
33       OS << Sep << I;
34       Sep = " ";
35     }
36     OS << ")";
37     return OS;
38   });
39   OS << "BitVector";
40   return OS;
41 }
42 
43 namespace bolt {
44 
45 void doForAllPreds(const BinaryContext &BC, const BinaryBasicBlock &BB,
46                    std::function<void(ProgramPoint)> Task) {
47   for (BinaryBasicBlock *Pred : BB.predecessors()) {
48     if (Pred->isValid())
49       Task(ProgramPoint::getLastPointAt(*Pred));
50   }
51   if (!BB.isLandingPad())
52     return;
53   for (BinaryBasicBlock *Thrower : BB.throwers()) {
54     for (MCInst &Inst : *Thrower) {
55       if (!BC.MIB->isInvoke(Inst))
56         continue;
57       const Optional<MCPlus::MCLandingPad> EHInfo = BC.MIB->getEHInfo(Inst);
58       if (!EHInfo || EHInfo->first != BB.getLabel())
59         continue;
60       Task(ProgramPoint(&Inst));
61     }
62   }
63 }
64 
65 /// Operates on all successors of a basic block.
66 void doForAllSuccs(const BinaryBasicBlock &BB,
67                    std::function<void(ProgramPoint)> Task) {
68   for (BinaryBasicBlock *Succ : BB.successors()) {
69     if (Succ->isValid())
70       Task(ProgramPoint::getFirstPointAt(*Succ));
71   }
72 }
73 
74 void RegStatePrinter::print(raw_ostream &OS, const BitVector &State) const {
75   if (State.all()) {
76     OS << "(all)";
77     return;
78   }
79   if (State.count() > (State.size() >> 1)) {
80     OS << "all, except: ";
81     BitVector BV = State;
82     BV.flip();
83     for (int I = BV.find_first(); I != -1; I = BV.find_next(I)) {
84       OS << BC.MRI->getName(I) << " ";
85     }
86     return;
87   }
88   for (int I = State.find_first(); I != -1; I = State.find_next(I)) {
89     OS << BC.MRI->getName(I) << " ";
90   }
91 }
92 
93 } // namespace bolt
94 } // namespace llvm
95