2024-02-26 12:12:31 +00:00
|
|
|
//===- InitUndef.cpp - Initialize undef value to pseudo ----===//
|
2023-02-22 04:02:06 -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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2024-02-26 12:12:31 +00:00
|
|
|
// This file implements a function pass that initializes undef value to
|
|
|
|
// temporary pseudo instruction to prevent register allocation resulting in a
|
|
|
|
// constraint violated result for the particular instruction. It also rewrites
|
|
|
|
// the NoReg tied operand back to an IMPLICIT_DEF.
|
2023-02-22 04:02:06 -08:00
|
|
|
//
|
2024-02-26 12:12:31 +00:00
|
|
|
// Certain instructions have register overlapping constraints, and
|
|
|
|
// will cause illegal instruction trap if violated, we use early clobber to
|
|
|
|
// model this constraint, but it can't prevent register allocator allocating
|
|
|
|
// same or overlapped if the input register is undef value, so convert
|
|
|
|
// IMPLICIT_DEF to temporary pseudo instruction and remove it later could
|
|
|
|
// prevent that happen, it's not best way to resolve this, and it might
|
2023-02-22 04:02:06 -08:00
|
|
|
// change the order of program or increase the register pressure, so ideally we
|
|
|
|
// should model the constraint right, but before we model the constraint right,
|
|
|
|
// it's the only way to prevent that happen.
|
|
|
|
//
|
2024-02-26 12:12:31 +00:00
|
|
|
// When we enable the subregister liveness option, it will also trigger the same
|
2023-02-22 04:02:06 -08:00
|
|
|
// issue due to the partial of register is undef. If we pseudoinit the whole
|
|
|
|
// register, then it will generate redundant COPY instruction. Currently, it
|
|
|
|
// will generate INSERT_SUBREG to make sure the whole register is occupied
|
|
|
|
// when program encounter operation that has early-clobber constraint.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// See also: https://github.com/llvm/llvm-project/issues/50157
|
|
|
|
//
|
2024-02-26 12:12:31 +00:00
|
|
|
// Additionally, this pass rewrites tied operands of instructions
|
2023-08-14 12:39:29 -07:00
|
|
|
// from NoReg to IMPLICIT_DEF. (Not that this is a non-overlapping set of
|
|
|
|
// operands to the above.) We use NoReg to side step a MachineCSE
|
|
|
|
// optimization quality problem but need to convert back before
|
|
|
|
// TwoAddressInstruction. See pr64282 for context.
|
|
|
|
//
|
2023-02-22 04:02:06 -08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2023-08-11 09:56:23 +01:00
|
|
|
#include "llvm/ADT/SmallSet.h"
|
2023-11-20 11:44:27 +08:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2023-02-22 04:02:06 -08:00
|
|
|
#include "llvm/CodeGen/DetectDeadLanes.h"
|
2024-02-26 12:12:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2023-02-22 04:02:06 -08:00
|
|
|
#include "llvm/CodeGen/MachineFunctionPass.h"
|
2024-02-26 12:12:31 +00:00
|
|
|
#include "llvm/CodeGen/MachineRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetInstrInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetRegisterInfo.h"
|
|
|
|
#include "llvm/CodeGen/TargetSubtargetInfo.h"
|
|
|
|
#include "llvm/InitializePasses.h"
|
|
|
|
#include "llvm/MC/MCRegister.h"
|
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
|
2023-02-22 04:02:06 -08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
#define DEBUG_TYPE "init-undef"
|
|
|
|
#define INIT_UNDEF_NAME "Init Undef Pass"
|
2023-02-22 04:02:06 -08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
class InitUndef : public MachineFunctionPass {
|
2023-02-22 04:02:06 -08:00
|
|
|
const TargetInstrInfo *TII;
|
|
|
|
MachineRegisterInfo *MRI;
|
2024-02-26 12:12:31 +00:00
|
|
|
const TargetSubtargetInfo *ST;
|
2023-02-22 04:02:06 -08:00
|
|
|
const TargetRegisterInfo *TRI;
|
|
|
|
|
2023-08-01 10:40:32 -07:00
|
|
|
// Newly added vregs, assumed to be fully rewritten
|
|
|
|
SmallSet<Register, 8> NewRegs;
|
2023-11-20 11:44:27 +08:00
|
|
|
SmallVector<MachineInstr *, 8> DeadInsts;
|
|
|
|
|
2023-02-22 04:02:06 -08:00
|
|
|
public:
|
|
|
|
static char ID;
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
InitUndef() : MachineFunctionPass(ID) {}
|
2023-02-22 04:02:06 -08:00
|
|
|
bool runOnMachineFunction(MachineFunction &MF) override;
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
|
|
|
AU.setPreservesCFG();
|
|
|
|
MachineFunctionPass::getAnalysisUsage(AU);
|
|
|
|
}
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
StringRef getPassName() const override { return INIT_UNDEF_NAME; }
|
2023-02-22 04:02:06 -08:00
|
|
|
|
|
|
|
private:
|
|
|
|
bool processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
const DeadLaneDetector &DLD);
|
|
|
|
bool handleSubReg(MachineFunction &MF, MachineInstr &MI,
|
|
|
|
const DeadLaneDetector &DLD);
|
2023-12-01 01:04:42 -06:00
|
|
|
bool fixupIllOperand(MachineInstr *MI, MachineOperand &MO);
|
|
|
|
bool handleReg(MachineInstr *MI);
|
2023-02-22 04:02:06 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
char InitUndef::ID = 0;
|
|
|
|
INITIALIZE_PASS(InitUndef, DEBUG_TYPE, INIT_UNDEF_NAME, false, false)
|
|
|
|
char &llvm::InitUndefID = InitUndef::ID;
|
2023-02-22 04:02:06 -08:00
|
|
|
|
2023-08-01 10:40:32 -07:00
|
|
|
static bool isEarlyClobberMI(MachineInstr &MI) {
|
|
|
|
return llvm::any_of(MI.defs(), [](const MachineOperand &DefMO) {
|
|
|
|
return DefMO.isReg() && DefMO.isEarlyClobber();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-12-01 01:04:42 -06:00
|
|
|
static bool findImplictDefMIFromReg(Register Reg, MachineRegisterInfo *MRI) {
|
|
|
|
for (auto &DefMI : MRI->def_instructions(Reg)) {
|
|
|
|
if (DefMI.getOpcode() == TargetOpcode::IMPLICIT_DEF)
|
|
|
|
return true;
|
2023-02-22 04:02:06 -08:00
|
|
|
}
|
2023-12-01 01:04:42 -06:00
|
|
|
return false;
|
|
|
|
}
|
2023-02-22 04:02:06 -08:00
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
bool InitUndef::handleReg(MachineInstr *MI) {
|
2023-12-01 01:04:42 -06:00
|
|
|
bool Changed = false;
|
|
|
|
for (auto &UseMO : MI->uses()) {
|
|
|
|
if (!UseMO.isReg())
|
|
|
|
continue;
|
|
|
|
if (UseMO.isTied())
|
|
|
|
continue;
|
|
|
|
if (!UseMO.getReg().isVirtual())
|
|
|
|
continue;
|
2024-02-26 12:12:31 +00:00
|
|
|
if (!TRI->doesRegClassHavePseudoInitUndef(MRI->getRegClass(UseMO.getReg())))
|
2023-12-01 01:04:42 -06:00
|
|
|
continue;
|
2023-02-22 04:02:06 -08:00
|
|
|
|
2023-12-01 01:04:42 -06:00
|
|
|
if (UseMO.isUndef() || findImplictDefMIFromReg(UseMO.getReg(), MRI))
|
|
|
|
Changed |= fixupIllOperand(MI, UseMO);
|
2023-08-01 10:40:32 -07:00
|
|
|
}
|
2023-12-01 01:04:42 -06:00
|
|
|
return Changed;
|
2023-02-22 04:02:06 -08:00
|
|
|
}
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
bool InitUndef::handleSubReg(MachineFunction &MF, MachineInstr &MI,
|
|
|
|
const DeadLaneDetector &DLD) {
|
2023-02-22 04:02:06 -08:00
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
for (MachineOperand &UseMO : MI.uses()) {
|
|
|
|
if (!UseMO.isReg())
|
|
|
|
continue;
|
|
|
|
if (!UseMO.getReg().isVirtual())
|
|
|
|
continue;
|
2023-08-01 12:16:26 -07:00
|
|
|
if (UseMO.isTied())
|
|
|
|
continue;
|
2024-02-26 12:12:31 +00:00
|
|
|
if (!TRI->doesRegClassHavePseudoInitUndef(MRI->getRegClass(UseMO.getReg())))
|
|
|
|
continue;
|
2023-02-22 04:02:06 -08:00
|
|
|
|
|
|
|
Register Reg = UseMO.getReg();
|
2023-08-01 10:40:32 -07:00
|
|
|
if (NewRegs.count(Reg))
|
|
|
|
continue;
|
2023-02-22 04:02:06 -08:00
|
|
|
DeadLaneDetector::VRegInfo Info =
|
|
|
|
DLD.getVRegInfo(Register::virtReg2Index(Reg));
|
|
|
|
|
|
|
|
if (Info.UsedLanes == Info.DefinedLanes)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
const TargetRegisterClass *TargetRegClass =
|
2024-02-26 12:12:31 +00:00
|
|
|
TRI->getLargestSuperClass(MRI->getRegClass(Reg));
|
2023-02-22 04:02:06 -08:00
|
|
|
|
|
|
|
LaneBitmask NeedDef = Info.UsedLanes & ~Info.DefinedLanes;
|
|
|
|
|
|
|
|
LLVM_DEBUG({
|
|
|
|
dbgs() << "Instruction has undef subregister.\n";
|
|
|
|
dbgs() << printReg(Reg, nullptr)
|
|
|
|
<< " Used: " << PrintLaneMask(Info.UsedLanes)
|
|
|
|
<< " Def: " << PrintLaneMask(Info.DefinedLanes)
|
|
|
|
<< " Need Def: " << PrintLaneMask(NeedDef) << "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
SmallVector<unsigned> SubRegIndexNeedInsert;
|
|
|
|
TRI->getCoveringSubRegIndexes(*MRI, TargetRegClass, NeedDef,
|
|
|
|
SubRegIndexNeedInsert);
|
|
|
|
|
|
|
|
Register LatestReg = Reg;
|
|
|
|
for (auto ind : SubRegIndexNeedInsert) {
|
|
|
|
Changed = true;
|
2024-02-26 12:12:31 +00:00
|
|
|
const TargetRegisterClass *SubRegClass = TRI->getLargestSuperClass(
|
|
|
|
TRI->getSubRegisterClass(TargetRegClass, ind));
|
2023-02-22 04:02:06 -08:00
|
|
|
Register TmpInitSubReg = MRI->createVirtualRegister(SubRegClass);
|
2024-02-26 12:12:31 +00:00
|
|
|
LLVM_DEBUG(dbgs() << "Register Class ID" << SubRegClass->getID() << "\n");
|
2023-02-22 04:02:06 -08:00
|
|
|
BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(),
|
2024-02-26 12:12:31 +00:00
|
|
|
TII->get(TII->getUndefInitOpcode(SubRegClass->getID())),
|
2023-02-22 04:02:06 -08:00
|
|
|
TmpInitSubReg);
|
|
|
|
Register NewReg = MRI->createVirtualRegister(TargetRegClass);
|
|
|
|
BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(),
|
|
|
|
TII->get(TargetOpcode::INSERT_SUBREG), NewReg)
|
|
|
|
.addReg(LatestReg)
|
|
|
|
.addReg(TmpInitSubReg)
|
|
|
|
.addImm(ind);
|
|
|
|
LatestReg = NewReg;
|
|
|
|
}
|
|
|
|
|
|
|
|
UseMO.setReg(LatestReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
bool InitUndef::fixupIllOperand(MachineInstr *MI, MachineOperand &MO) {
|
2023-12-01 01:04:42 -06:00
|
|
|
|
|
|
|
LLVM_DEBUG(
|
2024-02-26 12:12:31 +00:00
|
|
|
dbgs() << "Emitting PseudoInitUndef Instruction for implicit register "
|
2023-12-01 01:04:42 -06:00
|
|
|
<< MO.getReg() << '\n');
|
|
|
|
|
|
|
|
const TargetRegisterClass *TargetRegClass =
|
2024-02-26 12:12:31 +00:00
|
|
|
TRI->getLargestSuperClass(MRI->getRegClass(MO.getReg()));
|
|
|
|
LLVM_DEBUG(dbgs() << "Register Class ID" << TargetRegClass->getID() << "\n");
|
|
|
|
unsigned Opcode = TII->getUndefInitOpcode(TargetRegClass->getID());
|
2023-12-01 01:04:42 -06:00
|
|
|
Register NewReg = MRI->createVirtualRegister(TargetRegClass);
|
|
|
|
BuildMI(*MI->getParent(), MI, MI->getDebugLoc(), TII->get(Opcode), NewReg);
|
|
|
|
MO.setReg(NewReg);
|
|
|
|
if (MO.isUndef())
|
|
|
|
MO.setIsUndef(false);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
bool InitUndef::processBasicBlock(MachineFunction &MF, MachineBasicBlock &MBB,
|
|
|
|
const DeadLaneDetector &DLD) {
|
2023-02-22 04:02:06 -08:00
|
|
|
bool Changed = false;
|
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(); I != MBB.end(); ++I) {
|
|
|
|
MachineInstr &MI = *I;
|
2023-08-14 12:39:29 -07:00
|
|
|
|
|
|
|
// If we used NoReg to represent the passthru, switch this back to being
|
|
|
|
// an IMPLICIT_DEF before TwoAddressInstructions.
|
|
|
|
unsigned UseOpIdx;
|
|
|
|
if (MI.getNumDefs() != 0 && MI.isRegTiedToUseOperand(0, &UseOpIdx)) {
|
|
|
|
MachineOperand &UseMO = MI.getOperand(UseOpIdx);
|
2024-02-26 12:12:31 +00:00
|
|
|
if (UseMO.getReg() == MCRegister::NoRegister) {
|
2023-08-14 12:39:29 -07:00
|
|
|
const TargetRegisterClass *RC =
|
2024-02-26 12:12:31 +00:00
|
|
|
TII->getRegClass(MI.getDesc(), UseOpIdx, TRI, MF);
|
2023-08-14 12:39:29 -07:00
|
|
|
Register NewDest = MRI->createVirtualRegister(RC);
|
|
|
|
// We don't have a way to update dead lanes, so keep track of the
|
|
|
|
// new register so that we avoid querying it later.
|
|
|
|
NewRegs.insert(NewDest);
|
2024-02-26 12:12:31 +00:00
|
|
|
BuildMI(MBB, I, I->getDebugLoc(), TII->get(TargetOpcode::IMPLICIT_DEF),
|
|
|
|
NewDest);
|
2023-08-14 12:39:29 -07:00
|
|
|
UseMO.setReg(NewDest);
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-01 01:04:42 -06:00
|
|
|
if (isEarlyClobberMI(MI)) {
|
|
|
|
if (ST->enableSubRegLiveness())
|
|
|
|
Changed |= handleSubReg(MF, MI, DLD);
|
|
|
|
Changed |= handleReg(&MI);
|
2023-02-22 04:02:06 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2024-02-26 12:12:31 +00:00
|
|
|
bool InitUndef::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
ST = &MF.getSubtarget();
|
|
|
|
|
|
|
|
// supportsInitUndef is implemented to reflect if an architecture has support
|
|
|
|
// for the InitUndef pass. Support comes from having the relevant Pseudo
|
|
|
|
// instructions that can be used to initialize the register. The function
|
|
|
|
// returns false by default so requires an implementation per architecture.
|
|
|
|
// Support can be added by overriding the function in a way that best fits
|
|
|
|
// the architecture.
|
|
|
|
if (!ST->supportsInitUndef())
|
2023-02-22 04:02:06 -08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
MRI = &MF.getRegInfo();
|
|
|
|
TII = ST->getInstrInfo();
|
|
|
|
TRI = MRI->getTargetRegisterInfo();
|
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
DeadLaneDetector DLD(MRI, TRI);
|
|
|
|
DLD.computeSubRegisterLaneBitInfo();
|
|
|
|
|
|
|
|
for (MachineBasicBlock &BB : MF)
|
|
|
|
Changed |= processBasicBlock(MF, BB, DLD);
|
|
|
|
|
2023-11-20 11:44:27 +08:00
|
|
|
for (auto *DeadMI : DeadInsts)
|
|
|
|
DeadMI->eraseFromParent();
|
|
|
|
DeadInsts.clear();
|
|
|
|
|
2023-02-22 04:02:06 -08:00
|
|
|
return Changed;
|
|
|
|
}
|