[llvm] [Xtensa] Implement lowering SELECT_CC, SETCC. (PR #97017)
Sergei Barannikov via llvm-commits
llvm-commits at lists.llvm.org
Sat Jul 6 18:15:45 PDT 2024
================
@@ -697,6 +718,126 @@ const char *XtensaTargetLowering::getTargetNodeName(unsigned Opcode) const {
return "XtensaISD::PCREL_WRAPPER";
case XtensaISD::RET:
return "XtensaISD::RET";
+ case XtensaISD::SELECT_CC:
+ return "XtensaISD::SELECT_CC";
}
return nullptr;
}
+
+//===----------------------------------------------------------------------===//
+// Custom insertion
+//===----------------------------------------------------------------------===//
+
+static int GetBranchKind(int Cond, bool &BrInv) {
+ switch (Cond) {
+ case ISD::SETEQ:
+ return Xtensa::BEQ;
+ case ISD::SETNE:
+ return Xtensa::BNE;
+ case ISD::SETLT:
+ return Xtensa::BLT;
+ case ISD::SETLE:
+ BrInv = true;
+ return Xtensa::BGE;
+ case ISD::SETGT:
+ BrInv = true;
+ return Xtensa::BLT;
+ case ISD::SETGE:
+ return Xtensa::BGE;
+ case ISD::SETULT:
+ return Xtensa::BLTU;
+ case ISD::SETULE:
+ BrInv = true;
+ return Xtensa::BGEU;
+ case ISD::SETUGT:
+ BrInv = true;
+ return Xtensa::BLTU;
+ case ISD::SETUGE:
+ return Xtensa::BGEU;
+ default:
+ return -1;
+ }
+}
+
+MachineBasicBlock *
+XtensaTargetLowering::emitSelectCC(MachineInstr &MI,
+ MachineBasicBlock *MBB) const {
+ const TargetInstrInfo &TII = *Subtarget.getInstrInfo();
+ DebugLoc DL = MI.getDebugLoc();
+
+ MachineOperand &LHS = MI.getOperand(1);
+ MachineOperand &RHS = MI.getOperand(2);
+ MachineOperand &TrueValue = MI.getOperand(3);
+ MachineOperand &FalseValue = MI.getOperand(4);
+ MachineOperand &Cond = MI.getOperand(5);
+
+ // To "insert" a SELECT_CC instruction, we actually have to insert
+ // CopyMBB and SinkMBB blocks and add branch to MBB. We build phi
+ // operation in SinkMBB like phi (TrueVakue,FalseValue), where TrueValue
+ // is passed from MMB and FalseValue is passed from CopyMBB.
+ // MBB
+ // | \
+ // | CopyMBB
+ // | /
+ // SinkMBB
+ // The incoming instruction knows the
+ // destination vreg to set, the condition code register to branch on, the
+ // true/false values to select between, and a branch opcode to use.
+ const BasicBlock *LLVM_BB = MBB->getBasicBlock();
+ MachineFunction::iterator It = ++MBB->getIterator();
+
+ MachineFunction *F = MBB->getParent();
+ MachineBasicBlock *CopyMBB = F->CreateMachineBasicBlock(LLVM_BB);
+ MachineBasicBlock *SinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
+
+ F->insert(It, CopyMBB);
+ F->insert(It, SinkMBB);
+
+ // Transfer the remainder of MBB and its successor edges to SinkMBB.
+ SinkMBB->splice(SinkMBB->begin(), MBB,
+ std::next(MachineBasicBlock::iterator(MI)), MBB->end());
+ SinkMBB->transferSuccessorsAndUpdatePHIs(MBB);
+
+ MBB->addSuccessor(CopyMBB);
+ MBB->addSuccessor(SinkMBB);
+
+ bool BrInv = false;
+ int BrKind = GetBranchKind(Cond.getImm(), BrInv);
+ if (BrInv) {
+ BuildMI(MBB, DL, TII.get(BrKind))
+ .addReg(RHS.getReg())
+ .addReg(LHS.getReg())
+ .addMBB(SinkMBB);
+ } else {
+ BuildMI(MBB, DL, TII.get(BrKind))
+ .addReg(LHS.getReg())
+ .addReg(RHS.getReg())
+ .addMBB(SinkMBB);
+ }
+
+ CopyMBB->addSuccessor(SinkMBB);
+
+ // SinkMBB:
+ // %Result = phi [ %FalseValue, CopyMBB ], [ %TrueValue, MBB ]
+ // ...
+
+ BuildMI(*SinkMBB, SinkMBB->begin(), DL, TII.get(Xtensa::PHI),
+ MI.getOperand(0).getReg())
+ .addReg(FalseValue.getReg())
+ .addMBB(CopyMBB)
+ .addReg(TrueValue.getReg())
+ .addMBB(MBB);
+
+ MI.eraseFromParent(); // The pseudo instruction is gone now.
+ return SinkMBB;
+}
+
+MachineBasicBlock *XtensaTargetLowering::EmitInstrWithCustomInserter(
+ MachineInstr &MI, MachineBasicBlock *MBB) const {
+ switch (MI.getOpcode()) {
+ case Xtensa::SELECT:
+ return emitSelectCC(MI, MBB);
+ default:
+ report_fatal_error("Unexpected instr type to insert");
----------------
s-barannikov wrote:
Should be llvm_unreachable
https://github.com/llvm/llvm-project/pull/97017
More information about the llvm-commits
mailing list