2021-12-21 10:21:41 -08:00
|
|
|
//===- bolt/Passes/DataflowAnalysis.cpp -----------------------------------===//
|
2021-03-15 18:04:18 -07:00
|
|
|
//
|
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
//
|
2021-12-21 10:21:41 -08:00
|
|
|
// This file implements functions and classes used for data-flow analysis.
|
|
|
|
|
//
|
2021-03-15 18:04:18 -07:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2021-10-08 11:47:10 -07:00
|
|
|
#include "bolt/Passes/DataflowAnalysis.h"
|
2022-02-09 08:26:30 -05:00
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2017-05-01 16:51:27 -07:00
|
|
|
|
2020-12-01 16:29:39 -08:00
|
|
|
#define DEBUG_TYPE "dataflow"
|
|
|
|
|
|
2017-05-01 16:51:27 -07:00
|
|
|
namespace llvm {
|
2017-10-19 12:36:48 -07:00
|
|
|
|
2020-12-01 16:29:39 -08:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const BitVector &State) {
|
|
|
|
|
LLVM_DEBUG({
|
|
|
|
|
OS << "BitVector(";
|
2021-04-08 00:19:26 -07:00
|
|
|
const char *Sep = "";
|
2020-12-01 16:29:39 -08:00
|
|
|
if (State.count() > (State.size() >> 1)) {
|
|
|
|
|
OS << "all, except: ";
|
2021-04-08 00:19:26 -07:00
|
|
|
BitVector BV = State;
|
2020-12-01 16:29:39 -08:00
|
|
|
BV.flip();
|
2022-05-11 16:23:27 -07:00
|
|
|
for (int I : BV.set_bits()) {
|
2020-12-01 16:29:39 -08:00
|
|
|
OS << Sep << I;
|
|
|
|
|
Sep = " ";
|
|
|
|
|
}
|
|
|
|
|
OS << ")";
|
|
|
|
|
return OS;
|
|
|
|
|
}
|
2022-05-11 16:23:27 -07:00
|
|
|
for (int I : State.set_bits()) {
|
2020-12-01 16:29:39 -08:00
|
|
|
OS << Sep << I;
|
|
|
|
|
Sep = " ";
|
|
|
|
|
}
|
|
|
|
|
OS << ")";
|
|
|
|
|
return OS;
|
|
|
|
|
});
|
2017-10-19 12:36:48 -07:00
|
|
|
OS << "BitVector";
|
|
|
|
|
return OS;
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 16:51:27 -07:00
|
|
|
namespace bolt {
|
|
|
|
|
|
2021-10-26 00:06:34 -07:00
|
|
|
void doForAllPreds(const BinaryBasicBlock &BB,
|
2017-05-01 16:51:27 -07:00
|
|
|
std::function<void(ProgramPoint)> Task) {
|
2021-10-26 00:06:34 -07:00
|
|
|
MCPlusBuilder *MIB = BB.getFunction()->getBinaryContext().MIB.get();
|
2021-04-08 00:19:26 -07:00
|
|
|
for (BinaryBasicBlock *Pred : BB.predecessors()) {
|
2017-05-01 16:51:27 -07:00
|
|
|
if (Pred->isValid())
|
|
|
|
|
Task(ProgramPoint::getLastPointAt(*Pred));
|
|
|
|
|
}
|
|
|
|
|
if (!BB.isLandingPad())
|
|
|
|
|
return;
|
2021-04-08 00:19:26 -07:00
|
|
|
for (BinaryBasicBlock *Thrower : BB.throwers()) {
|
|
|
|
|
for (MCInst &Inst : *Thrower) {
|
2021-10-26 00:06:34 -07:00
|
|
|
if (!MIB->isInvoke(Inst))
|
[BOLT][Refactoring] Isolate changes to MC layer
Summary:
Changes that we made to MCInst, MCOperand, MCExpr, etc. are now all
moved into tools/llvm-bolt. That required a change to the way we handle
annotations and any extra operands for MCInst.
Any MCPlus information is now attached via an extra operand of type
MCInst with an opcode ANNOTATION_LABEL. Since this operand is MCInst, we
attach extra info as operands to this instruction. For first-level
annotations use functions to access the information, such as
getConditionalTailCall() or getEHInfo(), etc. For the rest, optional or
second-class annotations, use a general named-annotation interface such
as getAnnotationAs<uint64_t>(Inst, "Count").
I did a test on HHVM binary, and a memory consumption went down a little
bit while the runtime remained the same.
(cherry picked from FBD7405412)
2018-03-19 18:32:12 -07:00
|
|
|
continue;
|
2021-10-26 00:06:34 -07:00
|
|
|
const Optional<MCPlus::MCLandingPad> EHInfo = MIB->getEHInfo(Inst);
|
[BOLT][Refactoring] Isolate changes to MC layer
Summary:
Changes that we made to MCInst, MCOperand, MCExpr, etc. are now all
moved into tools/llvm-bolt. That required a change to the way we handle
annotations and any extra operands for MCInst.
Any MCPlus information is now attached via an extra operand of type
MCInst with an opcode ANNOTATION_LABEL. Since this operand is MCInst, we
attach extra info as operands to this instruction. For first-level
annotations use functions to access the information, such as
getConditionalTailCall() or getEHInfo(), etc. For the rest, optional or
second-class annotations, use a general named-annotation interface such
as getAnnotationAs<uint64_t>(Inst, "Count").
I did a test on HHVM binary, and a memory consumption went down a little
bit while the runtime remained the same.
(cherry picked from FBD7405412)
2018-03-19 18:32:12 -07:00
|
|
|
if (!EHInfo || EHInfo->first != BB.getLabel())
|
2017-05-01 16:51:27 -07:00
|
|
|
continue;
|
|
|
|
|
Task(ProgramPoint(&Inst));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Operates on all successors of a basic block.
|
|
|
|
|
void doForAllSuccs(const BinaryBasicBlock &BB,
|
|
|
|
|
std::function<void(ProgramPoint)> Task) {
|
2021-12-28 16:36:17 -08:00
|
|
|
for (BinaryBasicBlock *Succ : BB.successors())
|
2017-05-01 16:51:27 -07:00
|
|
|
if (Succ->isValid())
|
|
|
|
|
Task(ProgramPoint::getFirstPointAt(*Succ));
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-19 12:36:48 -07:00
|
|
|
void RegStatePrinter::print(raw_ostream &OS, const BitVector &State) const {
|
2017-11-02 00:30:11 -07:00
|
|
|
if (State.all()) {
|
|
|
|
|
OS << "(all)";
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (State.count() > (State.size() >> 1)) {
|
|
|
|
|
OS << "all, except: ";
|
2021-04-08 00:19:26 -07:00
|
|
|
BitVector BV = State;
|
2017-11-02 00:30:11 -07:00
|
|
|
BV.flip();
|
2022-05-11 16:23:27 -07:00
|
|
|
for (int I : BV.set_bits())
|
2017-11-02 00:30:11 -07:00
|
|
|
OS << BC.MRI->getName(I) << " ";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-05-11 16:23:27 -07:00
|
|
|
for (int I : State.set_bits())
|
2017-10-19 12:36:48 -07:00
|
|
|
OS << BC.MRI->getName(I) << " ";
|
|
|
|
|
}
|
|
|
|
|
|
2017-05-01 16:51:27 -07:00
|
|
|
} // namespace bolt
|
|
|
|
|
} // namespace llvm
|