21
21
#include " llvm/CodeGen/MachineJumpTableInfo.h"
22
22
#include " llvm/CodeGen/MachineRegisterInfo.h"
23
23
#include " llvm/CodeGen/TargetLoweringObjectFileImpl.h"
24
+ #include " llvm/IR/GlobalVariable.h"
24
25
#include " llvm/Support/Debug.h"
25
26
#include " llvm/Support/ErrorHandling.h"
26
27
#include " llvm/Support/MathExtras.h"
@@ -98,6 +99,32 @@ XtensaTargetLowering::XtensaTargetLowering(const TargetMachine &TM,
98
99
setCondCodeAction (ISD::SETUGT, MVT::i32, Expand);
99
100
setCondCodeAction (ISD::SETULE, MVT::i32, Expand);
100
101
102
+ setOperationAction (ISD::MUL, MVT::i32, Expand);
103
+ setOperationAction (ISD::MULHU, MVT::i32, Expand);
104
+ setOperationAction (ISD::MULHS, MVT::i32, Expand);
105
+ setOperationAction (ISD::SMUL_LOHI, MVT::i32, Expand);
106
+ setOperationAction (ISD::UMUL_LOHI, MVT::i32, Expand);
107
+
108
+ setOperationAction (ISD::SDIV, MVT::i32, Expand);
109
+ setOperationAction (ISD::UDIV, MVT::i32, Expand);
110
+ setOperationAction (ISD::SREM, MVT::i32, Expand);
111
+ setOperationAction (ISD::UREM, MVT::i32, Expand);
112
+ setOperationAction (ISD::SDIVREM, MVT::i32, Expand);
113
+ setOperationAction (ISD::UDIVREM, MVT::i32, Expand);
114
+
115
+ setOperationAction (ISD::SHL_PARTS, MVT::i32, Custom);
116
+ setOperationAction (ISD::SRA_PARTS, MVT::i32, Custom);
117
+ setOperationAction (ISD::SRL_PARTS, MVT::i32, Custom);
118
+
119
+ setOperationAction (ISD::BSWAP, MVT::i32, Expand);
120
+ setOperationAction (ISD::ROTL, MVT::i32, Expand);
121
+ setOperationAction (ISD::ROTR, MVT::i32, Expand);
122
+ setOperationAction (ISD::CTPOP, MVT::i32, Custom);
123
+ setOperationAction (ISD::CTTZ, MVT::i32, Expand);
124
+ setOperationAction (ISD::CTLZ, MVT::i32, Expand);
125
+ setOperationAction (ISD::CTTZ_ZERO_UNDEF, MVT::i32, Expand);
126
+ setOperationAction (ISD::CTLZ_ZERO_UNDEF, MVT::i32, Expand);
127
+
101
128
// Implement custom stack allocations
102
129
setOperationAction (ISD::DYNAMIC_STACKALLOC, PtrVT, Custom);
103
130
// Implement custom stack save and restore
@@ -629,8 +656,12 @@ SDValue XtensaTargetLowering::LowerBR_JT(SDValue Op, SelectionDAG &DAG) const {
629
656
EVT PtrVT = Table.getValueType ();
630
657
unsigned EntrySize = MJTI->getEntrySize (TD);
631
658
632
- Index = DAG.getNode (ISD::MUL, DL, Index.getValueType (), Index,
633
- DAG.getConstant (EntrySize, DL, Index.getValueType ()));
659
+ assert ((MJTI->getEntrySize (TD) == 4 ) && " Unsupported jump-table entry size" );
660
+
661
+ Index = DAG.getNode (
662
+ ISD::SHL, DL, Index.getValueType (), Index,
663
+ DAG.getConstant (Log2_32 (EntrySize), DL, Index.getValueType ()));
664
+
634
665
SDValue Addr = DAG.getNode (ISD::ADD, DL, Index.getValueType (), Index, Table);
635
666
SDValue LD =
636
667
DAG.getLoad (PtrVT, DL, Chain, Addr,
@@ -662,10 +693,12 @@ SDValue XtensaTargetLowering::getAddrPCRel(SDValue Op,
662
693
return DAG.getNode (XtensaISD::PCREL_WRAPPER, DL, Ty, Op);
663
694
}
664
695
665
- SDValue XtensaTargetLowering::LowerConstantPool (ConstantPoolSDNode *CP ,
696
+ SDValue XtensaTargetLowering::LowerConstantPool (SDValue Op ,
666
697
SelectionDAG &DAG) const {
667
- EVT PtrVT = getPointerTy (DAG.getDataLayout ());
698
+ EVT PtrVT = Op.getValueType ();
699
+ ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
668
700
SDValue Result;
701
+
669
702
if (!CP->isMachineConstantPoolEntry ()) {
670
703
Result = DAG.getTargetConstantPool (CP->getConstVal (), PtrVT, CP->getAlign (),
671
704
CP->getOffset ());
@@ -713,6 +746,119 @@ SDValue XtensaTargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
713
746
return DAG.getMergeValues (Ops, DL);
714
747
}
715
748
749
+ SDValue XtensaTargetLowering::LowerShiftLeftParts (SDValue Op,
750
+ SelectionDAG &DAG) const {
751
+ SDLoc DL (Op);
752
+ MVT VT = MVT::i32;
753
+ SDValue Lo = Op.getOperand (0 ), Hi = Op.getOperand (1 );
754
+ SDValue Shamt = Op.getOperand (2 );
755
+
756
+ // if Shamt - register size < 0: // Shamt < register size
757
+ // Lo = Lo << Shamt
758
+ // Hi = (Hi << Shamt) | (Lo >>u (register size - Shamt))
759
+ // else:
760
+ // Lo = 0
761
+ // Hi = Lo << (Shamt - register size)
762
+
763
+ SDValue MinusRegisterSize = DAG.getConstant (-32 , DL, VT);
764
+ SDValue ShamtMinusRegisterSize =
765
+ DAG.getNode (ISD::ADD, DL, VT, Shamt, MinusRegisterSize);
766
+
767
+ SDValue LoTrue = DAG.getNode (ISD::SHL, DL, VT, Lo, Shamt);
768
+ SDValue HiTrue = DAG.getNode (XtensaISD::SRCL, DL, VT, Hi, Lo, Shamt);
769
+ SDValue Zero = DAG.getConstant (0 , DL, VT);
770
+ SDValue HiFalse = DAG.getNode (ISD::SHL, DL, VT, Lo, ShamtMinusRegisterSize);
771
+
772
+ SDValue Cond = DAG.getSetCC (DL, VT, ShamtMinusRegisterSize, Zero, ISD::SETLT);
773
+ Lo = DAG.getNode (ISD::SELECT, DL, VT, Cond, LoTrue, Zero);
774
+ Hi = DAG.getNode (ISD::SELECT, DL, VT, Cond, HiTrue, HiFalse);
775
+
776
+ return DAG.getMergeValues ({Lo, Hi}, DL);
777
+ }
778
+
779
+ SDValue XtensaTargetLowering::LowerShiftRightParts (SDValue Op,
780
+ SelectionDAG &DAG,
781
+ bool IsSRA) const {
782
+ SDLoc DL (Op);
783
+ SDValue Lo = Op.getOperand (0 ), Hi = Op.getOperand (1 );
784
+ SDValue Shamt = Op.getOperand (2 );
785
+ MVT VT = MVT::i32;
786
+
787
+ // SRA expansion:
788
+ // if Shamt - register size < 0: // Shamt < register size
789
+ // Lo = (Lo >>u Shamt) | (Hi << u (register size - Shamt))
790
+ // Hi = Hi >>s Shamt
791
+ // else:
792
+ // Lo = Hi >>s (Shamt - register size);
793
+ // Hi = Hi >>s (register size - 1)
794
+ //
795
+ // SRL expansion:
796
+ // if Shamt - register size < 0: // Shamt < register size
797
+ // Lo = (Lo >>u Shamt) | (Hi << u (register size - Shamt))
798
+ // Hi = Hi >>u Shamt
799
+ // else:
800
+ // Lo = Hi >>u (Shamt - register size);
801
+ // Hi = 0;
802
+
803
+ unsigned ShiftRightOp = IsSRA ? ISD::SRA : ISD::SRL;
804
+ SDValue MinusRegisterSize = DAG.getConstant (-32 , DL, VT);
805
+ SDValue RegisterSizeMinus1 = DAG.getConstant (32 - 1 , DL, VT);
806
+ SDValue ShamtMinusRegisterSize =
807
+ DAG.getNode (ISD::ADD, DL, VT, Shamt, MinusRegisterSize);
808
+
809
+ SDValue LoTrue = DAG.getNode (XtensaISD::SRCR, DL, VT, Hi, Lo, Shamt);
810
+ SDValue HiTrue = DAG.getNode (ShiftRightOp, DL, VT, Hi, Shamt);
811
+ SDValue Zero = DAG.getConstant (0 , DL, VT);
812
+ SDValue LoFalse =
813
+ DAG.getNode (ShiftRightOp, DL, VT, Hi, ShamtMinusRegisterSize);
814
+ SDValue HiFalse;
815
+
816
+ if (IsSRA) {
817
+ HiFalse = DAG.getNode (ShiftRightOp, DL, VT, Hi, RegisterSizeMinus1);
818
+ } else {
819
+ HiFalse = Zero;
820
+ }
821
+
822
+ SDValue Cond = DAG.getSetCC (DL, VT, ShamtMinusRegisterSize, Zero, ISD::SETLT);
823
+ Lo = DAG.getNode (ISD::SELECT, DL, VT, Cond, LoTrue, LoFalse);
824
+ Hi = DAG.getNode (ISD::SELECT, DL, VT, Cond, HiTrue, HiFalse);
825
+
826
+ return DAG.getMergeValues ({Lo, Hi}, DL);
827
+ }
828
+
829
+ SDValue XtensaTargetLowering::LowerCTPOP (SDValue Op, SelectionDAG &DAG) const {
830
+ auto &TLI = DAG.getTargetLoweringInfo ();
831
+ return TLI.expandCTPOP (Op.getNode (), DAG);
832
+ }
833
+
834
+ bool XtensaTargetLowering::decomposeMulByConstant (LLVMContext &Context, EVT VT,
835
+ SDValue C) const {
836
+ APInt Imm;
837
+ unsigned EltSizeInBits;
838
+
839
+ if (ISD::isConstantSplatVector (C.getNode (), Imm)) {
840
+ EltSizeInBits = VT.getScalarSizeInBits ();
841
+ } else if (VT.isScalarInteger ()) {
842
+ EltSizeInBits = VT.getSizeInBits ();
843
+ if (auto *ConstNode = dyn_cast<ConstantSDNode>(C.getNode ()))
844
+ Imm = ConstNode->getAPIntValue ();
845
+ else
846
+ return false ;
847
+ } else {
848
+ return false ;
849
+ }
850
+
851
+ // Omit if data size exceeds.
852
+ if (EltSizeInBits > 32 )
853
+ return false ;
854
+
855
+ // Convert MULT to LSL.
856
+ if (Imm.isPowerOf2 () && Imm.isIntN (5 ))
857
+ return true ;
858
+
859
+ return false ;
860
+ }
861
+
716
862
SDValue XtensaTargetLowering::LowerOperation (SDValue Op,
717
863
SelectionDAG &DAG) const {
718
864
switch (Op.getOpcode ()) {
@@ -726,8 +872,10 @@ SDValue XtensaTargetLowering::LowerOperation(SDValue Op,
726
872
return LowerBlockAddress (Op, DAG);
727
873
case ISD::JumpTable:
728
874
return LowerJumpTable (Op, DAG);
875
+ case ISD::CTPOP:
876
+ return LowerCTPOP (Op, DAG);
729
877
case ISD::ConstantPool:
730
- return LowerConstantPool (cast<ConstantPoolSDNode>(Op) , DAG);
878
+ return LowerConstantPool (Op , DAG);
731
879
case ISD::SELECT_CC:
732
880
return LowerSELECT_CC (Op, DAG);
733
881
case ISD::STACKSAVE:
@@ -736,6 +884,12 @@ SDValue XtensaTargetLowering::LowerOperation(SDValue Op,
736
884
return LowerSTACKRESTORE (Op, DAG);
737
885
case ISD::DYNAMIC_STACKALLOC:
738
886
return LowerDYNAMIC_STACKALLOC (Op, DAG);
887
+ case ISD::SHL_PARTS:
888
+ return LowerShiftLeftParts (Op, DAG);
889
+ case ISD::SRA_PARTS:
890
+ return LowerShiftRightParts (Op, DAG, true );
891
+ case ISD::SRL_PARTS:
892
+ return LowerShiftRightParts (Op, DAG, false );
739
893
default :
740
894
report_fatal_error (" Unexpected node to lower" );
741
895
}
@@ -747,12 +901,18 @@ const char *XtensaTargetLowering::getTargetNodeName(unsigned Opcode) const {
747
901
return " XtensaISD::BR_JT" ;
748
902
case XtensaISD::CALL:
749
903
return " XtensaISD::CALL" ;
904
+ case XtensaISD::EXTUI:
905
+ return " XtensaISD::EXTUI" ;
750
906
case XtensaISD::PCREL_WRAPPER:
751
907
return " XtensaISD::PCREL_WRAPPER" ;
752
908
case XtensaISD::RET:
753
909
return " XtensaISD::RET" ;
754
910
case XtensaISD::SELECT_CC:
755
911
return " XtensaISD::SELECT_CC" ;
912
+ case XtensaISD::SRCL:
913
+ return " XtensaISD::SRCL" ;
914
+ case XtensaISD::SRCR:
915
+ return " XtensaISD::SRCR" ;
756
916
}
757
917
return nullptr ;
758
918
}
@@ -827,6 +987,8 @@ XtensaTargetLowering::emitSelectCC(MachineInstr &MI,
827
987
828
988
MachineBasicBlock *XtensaTargetLowering::EmitInstrWithCustomInserter (
829
989
MachineInstr &MI, MachineBasicBlock *MBB) const {
990
+ DebugLoc DL = MI.getDebugLoc ();
991
+
830
992
switch (MI.getOpcode ()) {
831
993
case Xtensa::SELECT:
832
994
return emitSelectCC (MI, MBB);
0 commit comments