Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / bolt / lib / Passes / DataflowAnalysis.cpp
blob2b193c830fc670e26f404a2dcb0a61cc7b5d47f8
1 //===- bolt/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 // This file implements functions and classes used for data-flow analysis.
11 //===----------------------------------------------------------------------===//
13 #include "bolt/Passes/DataflowAnalysis.h"
14 #include "llvm/MC/MCRegisterInfo.h"
16 #define DEBUG_TYPE "dataflow"
18 namespace llvm {
20 raw_ostream &operator<<(raw_ostream &OS, const BitVector &State) {
21 LLVM_DEBUG({
22 OS << "BitVector(";
23 const char *Sep = "";
24 if (State.count() > (State.size() >> 1)) {
25 OS << "all, except: ";
26 BitVector BV = State;
27 BV.flip();
28 for (int I : BV.set_bits()) {
29 OS << Sep << I;
30 Sep = " ";
32 OS << ")";
33 return OS;
35 for (int I : State.set_bits()) {
36 OS << Sep << I;
37 Sep = " ";
39 OS << ")";
40 return OS;
41 });
42 OS << "BitVector";
43 return OS;
46 namespace bolt {
48 void doForAllPreds(const BinaryBasicBlock &BB,
49 std::function<void(ProgramPoint)> Task) {
50 MCPlusBuilder *MIB = BB.getFunction()->getBinaryContext().MIB.get();
51 for (BinaryBasicBlock *Pred : BB.predecessors()) {
52 if (Pred->isValid())
53 Task(ProgramPoint::getLastPointAt(*Pred));
55 if (!BB.isLandingPad())
56 return;
57 for (BinaryBasicBlock *Thrower : BB.throwers()) {
58 for (MCInst &Inst : *Thrower) {
59 if (!MIB->isInvoke(Inst))
60 continue;
61 const std::optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Inst);
62 if (!EHInfo || EHInfo->first != BB.getLabel())
63 continue;
64 Task(ProgramPoint(&Inst));
69 /// Operates on all successors of a basic block.
70 void doForAllSuccs(const BinaryBasicBlock &BB,
71 std::function<void(ProgramPoint)> Task) {
72 for (BinaryBasicBlock *Succ : BB.successors())
73 if (Succ->isValid())
74 Task(ProgramPoint::getFirstPointAt(*Succ));
77 void RegStatePrinter::print(raw_ostream &OS, const BitVector &State) const {
78 if (State.all()) {
79 OS << "(all)";
80 return;
82 if (State.count() > (State.size() >> 1)) {
83 OS << "all, except: ";
84 BitVector BV = State;
85 BV.flip();
86 for (int I : BV.set_bits())
87 OS << BC.MRI->getName(I) << " ";
88 return;
90 for (int I : State.set_bits())
91 OS << BC.MRI->getName(I) << " ";
94 } // namespace bolt
95 } // namespace llvm