36 #define DEBUG_TYPE "gen-pred" 50 struct RegisterSubReg {
53 RegisterSubReg(
unsigned r = 0,
unsigned s = 0) : R(r), S(s) {}
55 RegisterSubReg(
const Register &
Reg) : R(Reg), S(0) {}
57 bool operator== (
const RegisterSubReg &Reg)
const {
58 return R == Reg.R && S == Reg.S;
61 bool operator< (
const RegisterSubReg &Reg)
const {
62 return R < Reg.R || (R == Reg.R && S < Reg.S);
66 struct PrintRegister {
79 return OS <<
printReg(PR.Reg.R, &PR.TRI, PR.Reg.S);
91 return "Hexagon generate predicate operations";
104 using SetOfReg = std::set<RegisterSubReg>;
105 using RegToRegMap = std::map<RegisterSubReg, RegisterSubReg>;
116 void processPredicateGPR(
const RegisterSubReg &
Reg);
117 unsigned getPredForm(
unsigned Opc);
119 bool isScalarCmp(
unsigned Opc);
120 bool isScalarPred(RegisterSubReg PredReg);
121 RegisterSubReg getPredRegFor(
const RegisterSubReg &Reg);
131 "Hexagon generate predicate operations",
false,
false)
136 bool HexagonGenPredicate::
isPredReg(
unsigned R) {
140 return RC == &Hexagon::PredRegsRegClass;
143 unsigned HexagonGenPredicate::getPredForm(
unsigned Opc) {
144 using namespace Hexagon;
183 static_assert(PHI == 0,
"Use different value for <none>");
187 bool HexagonGenPredicate::isConvertibleToPredForm(
const MachineInstr *
MI) {
189 if (getPredForm(Opc) != 0)
197 case Hexagon::C2_cmpeqi:
198 case Hexagon::C4_cmpneqi:
213 case Hexagon::C2_tfrpr:
214 case TargetOpcode::COPY:
226 void HexagonGenPredicate::processPredicateGPR(
const RegisterSubReg &
Reg) {
230 use_iterator
I =
MRI->use_begin(Reg.R),
E =
MRI->use_end();
238 for (; I !=
E; ++
I) {
240 if (isConvertibleToPredForm(UseI))
245 RegisterSubReg HexagonGenPredicate::getPredRegFor(
const RegisterSubReg &Reg) {
250 RegToRegMap::iterator
F = G2P.find(Reg);
258 if (Opc == Hexagon::C2_tfrpr || Opc == TargetOpcode::COPY) {
261 G2P.insert(std::make_pair(Reg, PR));
269 Register NewPR =
MRI->createVirtualRegister(PredRC);
273 if (isConvertibleToPredForm(DefI)) {
275 BuildMI(B, std::next(DefIt), DL,
TII->get(TargetOpcode::COPY), NewPR)
276 .addReg(Reg.R, 0, Reg.S);
277 G2P.insert(std::make_pair(Reg, RegisterSubReg(NewPR)));
280 return RegisterSubReg(NewPR);
286 bool HexagonGenPredicate::isScalarCmp(
unsigned Opc) {
288 case Hexagon::C2_cmpeq:
289 case Hexagon::C2_cmpgt:
290 case Hexagon::C2_cmpgtu:
291 case Hexagon::C2_cmpeqp:
292 case Hexagon::C2_cmpgtp:
293 case Hexagon::C2_cmpgtup:
294 case Hexagon::C2_cmpeqi:
295 case Hexagon::C2_cmpgti:
296 case Hexagon::C2_cmpgtui:
297 case Hexagon::C2_cmpgei:
298 case Hexagon::C2_cmpgeui:
299 case Hexagon::C4_cmpneqi:
300 case Hexagon::C4_cmpltei:
301 case Hexagon::C4_cmplteui:
302 case Hexagon::C4_cmpneq:
303 case Hexagon::C4_cmplte:
304 case Hexagon::C4_cmplteu:
305 case Hexagon::A4_cmpbeq:
306 case Hexagon::A4_cmpbeqi:
307 case Hexagon::A4_cmpbgtu:
308 case Hexagon::A4_cmpbgtui:
309 case Hexagon::A4_cmpbgt:
310 case Hexagon::A4_cmpbgti:
311 case Hexagon::A4_cmpheq:
312 case Hexagon::A4_cmphgt:
313 case Hexagon::A4_cmphgtu:
314 case Hexagon::A4_cmpheqi:
315 case Hexagon::A4_cmphgti:
316 case Hexagon::A4_cmphgtui:
322 bool HexagonGenPredicate::isScalarPred(RegisterSubReg PredReg) {
323 std::queue<RegisterSubReg> WorkQ;
326 while (!WorkQ.empty()) {
327 RegisterSubReg PR = WorkQ.front();
334 case TargetOpcode::COPY: {
336 if (
MRI->getRegClass(PR.R) != PredRC)
341 case Hexagon::C2_and:
342 case Hexagon::C2_andn:
343 case Hexagon::C4_and_and:
344 case Hexagon::C4_and_andn:
345 case Hexagon::C4_and_or:
347 case Hexagon::C2_orn:
348 case Hexagon::C4_or_and:
349 case Hexagon::C4_or_andn:
350 case Hexagon::C4_or_or:
351 case Hexagon::C4_or_orn:
352 case Hexagon::C2_xor:
355 if (MO.isReg() && MO.isUse())
356 WorkQ.push(RegisterSubReg(MO.getReg()));
361 return isScalarCmp(DefOpc);
368 bool HexagonGenPredicate::convertToPredForm(
MachineInstr *MI) {
372 assert(isConvertibleToPredForm(MI));
374 for (
unsigned i = 0; i < NumOps; ++i) {
378 RegisterSubReg
Reg(MO);
379 if (Reg.S && Reg.S != Hexagon::isub_lo)
381 if (!PredGPRs.count(Reg))
388 unsigned NewOpc = getPredForm(Opc);
392 case Hexagon::C2_cmpeqi:
393 NewOpc = Hexagon::C2_not;
395 case Hexagon::C4_cmpneqi:
396 NewOpc = TargetOpcode::COPY;
405 RegisterSubReg PR = getPredRegFor(MI->
getOperand(1));
406 if (!isScalarPred(PR))
416 RegisterSubReg OutR(Op0);
422 RegisterSubReg NewPR =
MRI->createVirtualRegister(PredRC);
426 for (
unsigned i = 1; i < NumOps; ++i) {
428 RegisterSubReg Pred = getPredRegFor(GPR);
429 MIB.
addReg(Pred.R, 0, Pred.S);
437 BuildMI(B, MI, DL,
TII->get(TargetOpcode::COPY), NewOutR)
438 .addReg(NewPR.R, 0, NewPR.S);
439 MRI->replaceRegWith(OutR.R, NewOutR);
446 RegisterSubReg R(NewOutR);
448 processPredicateGPR(R);
456 bool Changed =
false;
471 if (MI.
getOpcode() != TargetOpcode::COPY)
479 if (
MRI->getRegClass(DR.R) != PredRC)
481 if (
MRI->getRegClass(SR.R) != PredRC)
483 assert(!DR.S && !SR.S &&
"Unexpected subregister");
484 MRI->replaceRegWith(DR.R, SR.R);
490 for (VectOfInst::iterator
I = Erase.begin(),
E = Erase.end();
I !=
E; ++
I)
491 (*I)->eraseFromParent();
507 bool Changed =
false;
508 collectPredicateGPR(MF);
509 for (SetOfReg::iterator
I = PredGPRs.begin(),
E = PredGPRs.end();
I !=
E; ++
I)
510 processPredicateGPR(*
I);
515 VectOfInst Processed, Copy;
517 using iterator = VectOfInst::iterator;
520 for (iterator
I = Copy.begin(),
E = Copy.end();
I !=
E; ++
I) {
522 bool Done = convertToPredForm(MI);
524 Processed.insert(MI);
531 return Processed.count(MI);
533 PUsers.remove_if(Done);
536 Changed |= eliminatePredCopies(MF);
541 return new HexagonGenPredicate();
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
This class represents lattice values for constants.
class llvm::RegisterBankInfo GPR
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
A global registry used in conjunction with static constructors to make pluggable components (like tar...
unsigned getSubReg() const
unsigned const TargetRegisterInfo * TRI
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
iterator_range< mop_iterator > operands()
bool isImm() const
isImm - Tests if this is a MO_Immediate operand.
hexagon gen Hexagon generate predicate operations
FunctionPass * createHexagonGenPredicate()
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
unsigned getNumOperands() const
Retuns the total number of operands.
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
INITIALIZE_PASS_BEGIN(HexagonGenPredicate, "hexagon-gen-pred", "Hexagon generate predicate operations", false, false) INITIALIZE_PASS_END(HexagonGenPredicate
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
MachineInstrBuilder BuildMI(MachineFunction &MF, const DebugLoc &DL, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Represent the analysis usage information of a pass.
#define LLVM_ATTRIBUTE_UNUSED
defusechain_iterator< true, false, false, true, false, false > use_iterator
use_iterator/use_begin/use_end - Walk all uses of the specified register.
FunctionPass class - This class is used to implement most global optimizations.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Iterator for intrusive lists based on ilist_node.
MachineOperand class - Representation of each machine instruction operand.
const Function & getFunction() const
Return the LLVM function that this machine code represents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
void initializeHexagonGenPredicatePass(PassRegistry &Registry)
Representation of each machine instruction.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
raw_ostream & operator<<(raw_ostream &OS, const APInt &I)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator<(int64_t V1, const APSInt &V2)
bool isPredReg(unsigned Reg)
A vector that has set insertion semantics.
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
bool operator==(uint64_t V1, const APInt &V2)
Register getReg() const
getReg - Returns the register number.
const MachineOperand & getOperand(unsigned i) const
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Wrapper class representing virtual and physical registers.