@@ -517,6 +517,38 @@ XtensaTargetLowering::LowerReturn(SDValue Chain, CallingConv::ID CallConv,
517517 return DAG.getNode (XtensaISD::RET, DL, MVT::Other, RetOps);
518518}
519519
520+ static unsigned getBranchOpcode (ISD::CondCode Cond, bool &BrInv) {
521+ BrInv = false ;
522+ switch (Cond) {
523+ case ISD::SETEQ:
524+ return Xtensa::BEQ;
525+ case ISD::SETNE:
526+ return Xtensa::BNE;
527+ case ISD::SETLT:
528+ return Xtensa::BLT;
529+ case ISD::SETLE:
530+ BrInv = true ;
531+ return Xtensa::BGE;
532+ case ISD::SETGT:
533+ BrInv = true ;
534+ return Xtensa::BLT;
535+ case ISD::SETGE:
536+ return Xtensa::BGE;
537+ case ISD::SETULT:
538+ return Xtensa::BLTU;
539+ case ISD::SETULE:
540+ BrInv = true ;
541+ return Xtensa::BGEU;
542+ case ISD::SETUGT:
543+ BrInv = true ;
544+ return Xtensa::BLTU;
545+ case ISD::SETUGE:
546+ return Xtensa::BGEU;
547+ default :
548+ llvm_unreachable (" Unknown branch kind" );
549+ }
550+ }
551+
520552SDValue XtensaTargetLowering::LowerSELECT_CC (SDValue Op,
521553 SelectionDAG &DAG) const {
522554 SDLoc DL (Op);
@@ -526,11 +558,19 @@ SDValue XtensaTargetLowering::LowerSELECT_CC(SDValue Op,
526558 SDValue TrueValue = Op.getOperand (2 );
527559 SDValue FalseValue = Op.getOperand (3 );
528560 ISD::CondCode CC = cast<CondCodeSDNode>(Op->getOperand (4 ))->get ();
529- SDValue TargetCC = DAG.getConstant (CC, DL, MVT::i32 );
561+
562+ bool BrInv;
563+ unsigned BrKind = getBranchOpcode (CC, BrInv);
564+ SDValue TargetCC = DAG.getConstant (BrKind, DL, MVT::i32 );
530565
531566 // Wrap select nodes
532- return DAG.getNode (XtensaISD::SELECT_CC, DL, Ty, LHS, RHS, TrueValue,
533- FalseValue, TargetCC);
567+ if (BrInv) {
568+ return DAG.getNode (XtensaISD::SELECT_CC, DL, Ty, RHS, LHS, TrueValue,
569+ FalseValue, TargetCC);
570+ } else {
571+ return DAG.getNode (XtensaISD::SELECT_CC, DL, Ty, LHS, RHS, TrueValue,
572+ FalseValue, TargetCC);
573+ }
534574}
535575
536576SDValue XtensaTargetLowering::LowerImmediate (SDValue Op,
@@ -728,37 +768,6 @@ const char *XtensaTargetLowering::getTargetNodeName(unsigned Opcode) const {
728768// Custom insertion
729769// ===----------------------------------------------------------------------===//
730770
731- static int GetBranchKind (int Cond, bool &BrInv) {
732- switch (Cond) {
733- case ISD::SETEQ:
734- return Xtensa::BEQ;
735- case ISD::SETNE:
736- return Xtensa::BNE;
737- case ISD::SETLT:
738- return Xtensa::BLT;
739- case ISD::SETLE:
740- BrInv = true ;
741- return Xtensa::BGE;
742- case ISD::SETGT:
743- BrInv = true ;
744- return Xtensa::BLT;
745- case ISD::SETGE:
746- return Xtensa::BGE;
747- case ISD::SETULT:
748- return Xtensa::BLTU;
749- case ISD::SETULE:
750- BrInv = true ;
751- return Xtensa::BGEU;
752- case ISD::SETUGT:
753- BrInv = true ;
754- return Xtensa::BLTU;
755- case ISD::SETUGE:
756- return Xtensa::BGEU;
757- default :
758- return -1 ;
759- }
760- }
761-
762771MachineBasicBlock *
763772XtensaTargetLowering::emitSelectCC (MachineInstr &MI,
764773 MachineBasicBlock *MBB) const {
@@ -769,7 +778,7 @@ XtensaTargetLowering::emitSelectCC(MachineInstr &MI,
769778 MachineOperand &RHS = MI.getOperand (2 );
770779 MachineOperand &TrueValue = MI.getOperand (3 );
771780 MachineOperand &FalseValue = MI.getOperand (4 );
772- MachineOperand &Cond = MI.getOperand (5 );
781+ unsigned BrKind = MI.getOperand (5 ). getImm ( );
773782
774783 // To "insert" a SELECT_CC instruction, we actually have to insert
775784 // CopyMBB and SinkMBB blocks and add branch to MBB. We build phi
@@ -801,19 +810,10 @@ XtensaTargetLowering::emitSelectCC(MachineInstr &MI,
801810 MBB->addSuccessor (CopyMBB);
802811 MBB->addSuccessor (SinkMBB);
803812
804- bool BrInv = false ;
805- int BrKind = GetBranchKind (Cond.getImm (), BrInv);
806- if (BrInv) {
807- BuildMI (MBB, DL, TII.get (BrKind))
808- .addReg (RHS.getReg ())
809- .addReg (LHS.getReg ())
810- .addMBB (SinkMBB);
811- } else {
812- BuildMI (MBB, DL, TII.get (BrKind))
813- .addReg (LHS.getReg ())
814- .addReg (RHS.getReg ())
815- .addMBB (SinkMBB);
816- }
813+ BuildMI (MBB, DL, TII.get (BrKind))
814+ .addReg (LHS.getReg ())
815+ .addReg (RHS.getReg ())
816+ .addMBB (SinkMBB);
817817
818818 CopyMBB->addSuccessor (SinkMBB);
819819
@@ -838,6 +838,6 @@ MachineBasicBlock *XtensaTargetLowering::EmitInstrWithCustomInserter(
838838 case Xtensa::SELECT:
839839 return emitSelectCC (MI, MBB);
840840 default :
841- report_fatal_error (" Unexpected instr type to insert" );
841+ llvm_unreachable (" Unexpected instr type to insert" );
842842 }
843843}
0 commit comments