2019-01-16 08:40:37 +08:00
|
|
|
//===- CSEInfo.cpp ------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2019-01-16 08:40:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "llvm/CodeGen/GlobalISel/CSEInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "cseinfo"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
char llvm::GISelCSEAnalysisWrapperPass::ID = 0;
|
|
|
|
INITIALIZE_PASS_BEGIN(GISelCSEAnalysisWrapperPass, DEBUG_TYPE,
|
|
|
|
"Analysis containing CSE Info", false, true)
|
|
|
|
INITIALIZE_PASS_END(GISelCSEAnalysisWrapperPass, DEBUG_TYPE,
|
|
|
|
"Analysis containing CSE Info", false, true)
|
|
|
|
|
|
|
|
/// -------- UniqueMachineInstr -------------//
|
|
|
|
|
|
|
|
void UniqueMachineInstr::Profile(FoldingSetNodeID &ID) {
|
|
|
|
GISelInstProfileBuilder(ID, MI->getMF()->getRegInfo()).addNodeID(MI);
|
|
|
|
}
|
|
|
|
/// -----------------------------------------
|
|
|
|
|
2019-04-15 12:53:46 +08:00
|
|
|
/// --------- CSEConfigFull ---------- ///
|
|
|
|
bool CSEConfigFull::shouldCSEOpc(unsigned Opc) {
|
2019-01-16 08:40:37 +08:00
|
|
|
switch (Opc) {
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case TargetOpcode::G_ADD:
|
|
|
|
case TargetOpcode::G_AND:
|
|
|
|
case TargetOpcode::G_ASHR:
|
|
|
|
case TargetOpcode::G_LSHR:
|
|
|
|
case TargetOpcode::G_MUL:
|
|
|
|
case TargetOpcode::G_OR:
|
|
|
|
case TargetOpcode::G_SHL:
|
|
|
|
case TargetOpcode::G_SUB:
|
|
|
|
case TargetOpcode::G_XOR:
|
|
|
|
case TargetOpcode::G_UDIV:
|
|
|
|
case TargetOpcode::G_SDIV:
|
|
|
|
case TargetOpcode::G_UREM:
|
|
|
|
case TargetOpcode::G_SREM:
|
|
|
|
case TargetOpcode::G_CONSTANT:
|
|
|
|
case TargetOpcode::G_FCONSTANT:
|
|
|
|
case TargetOpcode::G_ZEXT:
|
|
|
|
case TargetOpcode::G_SEXT:
|
|
|
|
case TargetOpcode::G_ANYEXT:
|
|
|
|
case TargetOpcode::G_UNMERGE_VALUES:
|
|
|
|
case TargetOpcode::G_TRUNC:
|
2019-08-16 07:45:45 +08:00
|
|
|
case TargetOpcode::G_GEP:
|
2019-01-16 08:40:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CSEConfigConstantOnly::shouldCSEOpc(unsigned Opc) {
|
|
|
|
return Opc == TargetOpcode::G_CONSTANT;
|
|
|
|
}
|
2019-04-15 12:53:46 +08:00
|
|
|
|
|
|
|
std::unique_ptr<CSEConfigBase>
|
|
|
|
llvm::getStandardCSEConfigForOpt(CodeGenOpt::Level Level) {
|
|
|
|
std::unique_ptr<CSEConfigBase> Config;
|
|
|
|
if (Level == CodeGenOpt::None)
|
2019-08-15 23:54:37 +08:00
|
|
|
Config = std::make_unique<CSEConfigConstantOnly>();
|
2019-04-15 12:53:46 +08:00
|
|
|
else
|
2019-08-15 23:54:37 +08:00
|
|
|
Config = std::make_unique<CSEConfigFull>();
|
2019-04-15 12:53:46 +08:00
|
|
|
return Config;
|
|
|
|
}
|
|
|
|
|
2019-01-16 08:40:37 +08:00
|
|
|
/// -----------------------------------------
|
|
|
|
|
|
|
|
/// -------- GISelCSEInfo -------------//
|
|
|
|
void GISelCSEInfo::setMF(MachineFunction &MF) {
|
|
|
|
this->MF = &MF;
|
|
|
|
this->MRI = &MF.getRegInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
GISelCSEInfo::~GISelCSEInfo() {}
|
|
|
|
|
|
|
|
bool GISelCSEInfo::isUniqueMachineInstValid(
|
|
|
|
const UniqueMachineInstr &UMI) const {
|
|
|
|
// Should we check here and assert that the instruction has been fully
|
|
|
|
// constructed?
|
|
|
|
// FIXME: Any other checks required to be done here? Remove this method if
|
|
|
|
// none.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::invalidateUniqueMachineInstr(UniqueMachineInstr *UMI) {
|
|
|
|
bool Removed = CSEMap.RemoveNode(UMI);
|
|
|
|
(void)Removed;
|
|
|
|
assert(Removed && "Invalidation called on invalid UMI");
|
|
|
|
// FIXME: Should UMI be deallocated/destroyed?
|
|
|
|
}
|
|
|
|
|
|
|
|
UniqueMachineInstr *GISelCSEInfo::getNodeIfExists(FoldingSetNodeID &ID,
|
|
|
|
MachineBasicBlock *MBB,
|
|
|
|
void *&InsertPos) {
|
|
|
|
auto *Node = CSEMap.FindNodeOrInsertPos(ID, InsertPos);
|
|
|
|
if (Node) {
|
|
|
|
if (!isUniqueMachineInstValid(*Node)) {
|
|
|
|
invalidateUniqueMachineInstr(Node);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Node->MI->getParent() != MBB)
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::insertNode(UniqueMachineInstr *UMI, void *InsertPos) {
|
|
|
|
handleRecordedInsts();
|
|
|
|
assert(UMI);
|
|
|
|
UniqueMachineInstr *MaybeNewNode = UMI;
|
|
|
|
if (InsertPos)
|
|
|
|
CSEMap.InsertNode(UMI, InsertPos);
|
|
|
|
else
|
|
|
|
MaybeNewNode = CSEMap.GetOrInsertNode(UMI);
|
|
|
|
if (MaybeNewNode != UMI) {
|
|
|
|
// A similar node exists in the folding set. Let's ignore this one.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
assert(InstrMapping.count(UMI->MI) == 0 &&
|
|
|
|
"This instruction should not be in the map");
|
|
|
|
InstrMapping[UMI->MI] = MaybeNewNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniqueMachineInstr *GISelCSEInfo::getUniqueInstrForMI(const MachineInstr *MI) {
|
|
|
|
assert(shouldCSE(MI->getOpcode()) && "Trying to CSE an unsupported Node");
|
|
|
|
auto *Node = new (UniqueInstrAllocator) UniqueMachineInstr(MI);
|
|
|
|
return Node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::insertInstr(MachineInstr *MI, void *InsertPos) {
|
|
|
|
assert(MI);
|
|
|
|
// If it exists in temporary insts, remove it.
|
|
|
|
TemporaryInsts.remove(MI);
|
|
|
|
auto *Node = getUniqueInstrForMI(MI);
|
|
|
|
insertNode(Node, InsertPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
MachineInstr *GISelCSEInfo::getMachineInstrIfExists(FoldingSetNodeID &ID,
|
|
|
|
MachineBasicBlock *MBB,
|
|
|
|
void *&InsertPos) {
|
|
|
|
handleRecordedInsts();
|
|
|
|
if (auto *Inst = getNodeIfExists(ID, MBB, InsertPos)) {
|
2019-02-09 07:34:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "CSEInfo::Found Instr " << *Inst->MI;);
|
2019-01-16 08:40:37 +08:00
|
|
|
return const_cast<MachineInstr *>(Inst->MI);
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::countOpcodeHit(unsigned Opc) {
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (OpcodeHitTable.count(Opc))
|
|
|
|
OpcodeHitTable[Opc] += 1;
|
|
|
|
else
|
|
|
|
OpcodeHitTable[Opc] = 1;
|
|
|
|
#endif
|
|
|
|
// Else do nothing.
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::recordNewInstruction(MachineInstr *MI) {
|
|
|
|
if (shouldCSE(MI->getOpcode())) {
|
|
|
|
TemporaryInsts.insert(MI);
|
2019-02-09 07:34:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "CSEInfo::Recording new MI " << *MI);
|
2019-01-16 08:40:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::handleRecordedInst(MachineInstr *MI) {
|
|
|
|
assert(shouldCSE(MI->getOpcode()) && "Invalid instruction for CSE");
|
|
|
|
auto *UMI = InstrMapping.lookup(MI);
|
2019-02-09 07:34:11 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "CSEInfo::Handling recorded MI " << *MI);
|
2019-01-16 08:40:37 +08:00
|
|
|
if (UMI) {
|
|
|
|
// Invalidate this MI.
|
|
|
|
invalidateUniqueMachineInstr(UMI);
|
|
|
|
InstrMapping.erase(MI);
|
|
|
|
}
|
|
|
|
/// Now insert the new instruction.
|
|
|
|
if (UMI) {
|
|
|
|
/// We'll reuse the same UniqueMachineInstr to avoid the new
|
|
|
|
/// allocation.
|
|
|
|
*UMI = UniqueMachineInstr(MI);
|
|
|
|
insertNode(UMI, nullptr);
|
|
|
|
} else {
|
|
|
|
/// This is a new instruction. Allocate a new UniqueMachineInstr and
|
|
|
|
/// Insert.
|
|
|
|
insertInstr(MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::handleRemoveInst(MachineInstr *MI) {
|
|
|
|
if (auto *UMI = InstrMapping.lookup(MI)) {
|
|
|
|
invalidateUniqueMachineInstr(UMI);
|
|
|
|
InstrMapping.erase(MI);
|
|
|
|
}
|
|
|
|
TemporaryInsts.remove(MI);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::handleRecordedInsts() {
|
|
|
|
while (!TemporaryInsts.empty()) {
|
|
|
|
auto *MI = TemporaryInsts.pop_back_val();
|
|
|
|
handleRecordedInst(MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GISelCSEInfo::shouldCSE(unsigned Opc) const {
|
|
|
|
// Only GISel opcodes are CSEable
|
|
|
|
if (!isPreISelGenericOpcode(Opc))
|
|
|
|
return false;
|
|
|
|
assert(CSEOpt.get() && "CSEConfig not set");
|
|
|
|
return CSEOpt->shouldCSEOpc(Opc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::erasingInstr(MachineInstr &MI) { handleRemoveInst(&MI); }
|
|
|
|
void GISelCSEInfo::createdInstr(MachineInstr &MI) { recordNewInstruction(&MI); }
|
|
|
|
void GISelCSEInfo::changingInstr(MachineInstr &MI) {
|
|
|
|
// For now, perform erase, followed by insert.
|
|
|
|
erasingInstr(MI);
|
|
|
|
createdInstr(MI);
|
|
|
|
}
|
|
|
|
void GISelCSEInfo::changedInstr(MachineInstr &MI) { changingInstr(MI); }
|
|
|
|
|
|
|
|
void GISelCSEInfo::analyze(MachineFunction &MF) {
|
|
|
|
setMF(MF);
|
|
|
|
for (auto &MBB : MF) {
|
|
|
|
if (MBB.empty())
|
|
|
|
continue;
|
|
|
|
for (MachineInstr &MI : MBB) {
|
|
|
|
if (!shouldCSE(MI.getOpcode()))
|
|
|
|
continue;
|
2019-02-04 22:05:33 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "CSEInfo::Add MI: " << MI);
|
2019-01-16 08:40:37 +08:00
|
|
|
insertInstr(&MI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::releaseMemory() {
|
2019-02-09 07:34:11 +08:00
|
|
|
print();
|
2019-01-16 08:40:37 +08:00
|
|
|
CSEMap.clear();
|
|
|
|
InstrMapping.clear();
|
|
|
|
UniqueInstrAllocator.Reset();
|
|
|
|
TemporaryInsts.clear();
|
|
|
|
CSEOpt.reset();
|
|
|
|
MRI = nullptr;
|
|
|
|
MF = nullptr;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
OpcodeHitTable.clear();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void GISelCSEInfo::print() {
|
2019-02-09 07:34:11 +08:00
|
|
|
LLVM_DEBUG(for (auto &It
|
|
|
|
: OpcodeHitTable) {
|
|
|
|
dbgs() << "CSEInfo::CSE Hit for Opc " << It.first << " : " << It.second
|
|
|
|
<< "\n";
|
|
|
|
};);
|
2019-01-16 08:40:37 +08:00
|
|
|
}
|
|
|
|
/// -----------------------------------------
|
|
|
|
// ---- Profiling methods for FoldingSetNode --- //
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeID(const MachineInstr *MI) const {
|
|
|
|
addNodeIDMBB(MI->getParent());
|
|
|
|
addNodeIDOpcode(MI->getOpcode());
|
|
|
|
for (auto &Op : MI->operands())
|
|
|
|
addNodeIDMachineOperand(Op);
|
|
|
|
addNodeIDFlag(MI->getFlags());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDOpcode(unsigned Opc) const {
|
|
|
|
ID.AddInteger(Opc);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDRegType(const LLT &Ty) const {
|
|
|
|
uint64_t Val = Ty.getUniqueRAWLLTData();
|
|
|
|
ID.AddInteger(Val);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDRegType(const TargetRegisterClass *RC) const {
|
|
|
|
ID.AddPointer(RC);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDRegType(const RegisterBank *RB) const {
|
|
|
|
ID.AddPointer(RB);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDImmediate(int64_t Imm) const {
|
|
|
|
ID.AddInteger(Imm);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDRegNum(unsigned Reg) const {
|
|
|
|
ID.AddInteger(Reg);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDRegType(const unsigned Reg) const {
|
|
|
|
addNodeIDMachineOperand(MachineOperand::CreateReg(Reg, false));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDMBB(const MachineBasicBlock *MBB) const {
|
|
|
|
ID.AddPointer(MBB);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &
|
|
|
|
GISelInstProfileBuilder::addNodeIDFlag(unsigned Flag) const {
|
|
|
|
if (Flag)
|
|
|
|
ID.AddInteger(Flag);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
const GISelInstProfileBuilder &GISelInstProfileBuilder::addNodeIDMachineOperand(
|
|
|
|
const MachineOperand &MO) const {
|
|
|
|
if (MO.isReg()) {
|
Apply llvm-prefer-register-over-unsigned from clang-tidy to LLVM
Summary:
This clang-tidy check is looking for unsigned integer variables whose initializer
starts with an implicit cast from llvm::Register and changes the type of the
variable to llvm::Register (dropping the llvm:: where possible).
Partial reverts in:
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
X86FixupLEAs.cpp - Some functions return unsigned and arguably should be MCRegister
X86FrameLowering.cpp - Some functions return unsigned and arguably should be MCRegister
HexagonBitSimplify.cpp - Function takes BitTracker::RegisterRef which appears to be unsigned&
MachineVerifier.cpp - Ambiguous operator==() given MCRegister and const Register
PPCFastISel.cpp - No Register::operator-=()
PeepholeOptimizer.cpp - TargetInstrInfo::optimizeLoadInstr() takes an unsigned&
MachineTraceMetrics.cpp - MachineTraceMetrics lacks a suitable constructor
Manual fixups in:
ARMFastISel.cpp - ARMEmitLoad() now takes a Register& instead of unsigned&
HexagonSplitDouble.cpp - Ternary operator was ambiguous between unsigned/Register
HexagonConstExtenders.cpp - Has a local class named Register, used llvm::Register instead of Register.
PPCFastISel.cpp - PPCEmitLoad() now takes a Register& instead of unsigned&
Depends on D65919
Reviewers: arsenm, bogner, craig.topper, RKSimon
Reviewed By: arsenm
Subscribers: RKSimon, craig.topper, lenary, aemerson, wuzish, jholewinski, MatzeB, qcolombet, dschuff, jyknight, dylanmckay, sdardis, nemanjai, jvesely, wdng, nhaehnle, sbc100, jgravelle-google, kristof.beyls, hiraditya, aheejin, kbarton, fedor.sergeev, javed.absar, asb, rbar, johnrusso, simoncook, apazos, sabuasal, niosHD, jrtc27, MaskRay, zzheng, edward-jones, atanasyan, rogfer01, MartinMosbeck, brucehoult, the_o, tpr, PkmX, jocewei, jsji, Petar.Avramovic, asbirlea, Jim, s.egerton, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D65962
llvm-svn: 369041
2019-08-16 03:22:08 +08:00
|
|
|
Register Reg = MO.getReg();
|
2019-01-16 08:40:37 +08:00
|
|
|
if (!MO.isDef())
|
|
|
|
addNodeIDRegNum(Reg);
|
|
|
|
LLT Ty = MRI.getType(Reg);
|
|
|
|
if (Ty.isValid())
|
|
|
|
addNodeIDRegType(Ty);
|
|
|
|
auto *RB = MRI.getRegBankOrNull(Reg);
|
|
|
|
if (RB)
|
|
|
|
addNodeIDRegType(RB);
|
|
|
|
auto *RC = MRI.getRegClassOrNull(Reg);
|
|
|
|
if (RC)
|
|
|
|
addNodeIDRegType(RC);
|
|
|
|
assert(!MO.isImplicit() && "Unhandled case");
|
|
|
|
} else if (MO.isImm())
|
|
|
|
ID.AddInteger(MO.getImm());
|
|
|
|
else if (MO.isCImm())
|
|
|
|
ID.AddPointer(MO.getCImm());
|
|
|
|
else if (MO.isFPImm())
|
|
|
|
ID.AddPointer(MO.getFPImm());
|
|
|
|
else if (MO.isPredicate())
|
|
|
|
ID.AddInteger(MO.getPredicate());
|
|
|
|
else
|
|
|
|
llvm_unreachable("Unhandled operand type");
|
|
|
|
// Handle other types
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-04-15 12:53:46 +08:00
|
|
|
GISelCSEInfo &
|
|
|
|
GISelCSEAnalysisWrapper::get(std::unique_ptr<CSEConfigBase> CSEOpt,
|
|
|
|
bool Recompute) {
|
2019-01-16 08:40:37 +08:00
|
|
|
if (!AlreadyComputed || Recompute) {
|
|
|
|
Info.setCSEConfig(std::move(CSEOpt));
|
|
|
|
Info.analyze(*MF);
|
|
|
|
AlreadyComputed = true;
|
|
|
|
}
|
|
|
|
return Info;
|
|
|
|
}
|
|
|
|
void GISelCSEAnalysisWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GISelCSEAnalysisWrapperPass::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
releaseMemory();
|
|
|
|
Wrapper.setMF(MF);
|
|
|
|
return false;
|
|
|
|
}
|