1 //===-- SystemZSelectionDAGInfo.cpp - SystemZ SelectionDAG Info -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the SystemZSelectionDAGInfo class.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "systemz-selectiondag-info"
15 #include "SystemZTargetMachine.h"
16 #include "llvm/CodeGen/SelectionDAG.h"
20 SystemZSelectionDAGInfo::
21 SystemZSelectionDAGInfo(const SystemZTargetMachine &TM)
22 : TargetSelectionDAGInfo(TM) {
25 SystemZSelectionDAGInfo::~SystemZSelectionDAGInfo() {
28 SDValue SystemZSelectionDAGInfo::
29 EmitTargetCodeForMemcpy(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
30 SDValue Dst, SDValue Src, SDValue Size, unsigned Align,
31 bool IsVolatile, bool AlwaysInline,
32 MachinePointerInfo DstPtrInfo,
33 MachinePointerInfo SrcPtrInfo) const {
37 if (ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(Size)) {
38 uint64_t Bytes = CSize->getZExtValue();
39 if (Bytes >= 1 && Bytes <= 0x100) {
41 return DAG.getNode(SystemZISD::MVC, DL, MVT::Other,
42 Chain, Dst, Src, Size);
48 // Handle a memset of 1, 2, 4 or 8 bytes with the operands given by
49 // Chain, Dst, ByteVal and Size. These cases are expected to use
50 // MVI, MVHHI, MVHI and MVGHI respectively.
51 static SDValue memsetStore(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
52 SDValue Dst, uint64_t ByteVal, uint64_t Size,
54 MachinePointerInfo DstPtrInfo) {
55 uint64_t StoreVal = ByteVal;
56 for (unsigned I = 1; I < Size; ++I)
57 StoreVal |= ByteVal << (I * 8);
58 return DAG.getStore(Chain, DL,
59 DAG.getConstant(StoreVal, MVT::getIntegerVT(Size * 8)),
60 Dst, DstPtrInfo, false, false, Align);
63 SDValue SystemZSelectionDAGInfo::
64 EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
65 SDValue Dst, SDValue Byte, SDValue Size,
66 unsigned Align, bool IsVolatile,
67 MachinePointerInfo DstPtrInfo) const {
68 EVT DstVT = Dst.getValueType();
73 if (ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(Size)) {
74 uint64_t Bytes = CSize->getZExtValue();
77 if (ConstantSDNode *CByte = dyn_cast<ConstantSDNode>(Byte)) {
78 // Handle cases that can be done using at most two of
79 // MVI, MVHI, MVHHI and MVGHI. The latter two can only be
80 // used if ByteVal is all zeros or all ones; in other casees,
81 // we can move at most 2 halfwords.
82 uint64_t ByteVal = CByte->getZExtValue();
83 if (ByteVal == 0 || ByteVal == 255 ?
84 Bytes <= 16 && CountPopulation_64(Bytes) <= 2 :
86 unsigned Size1 = Bytes == 16 ? 8 : 1 << findLastSet(Bytes);
87 unsigned Size2 = Bytes - Size1;
88 SDValue Chain1 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size1,
92 Dst = DAG.getNode(ISD::ADD, DL, DstVT, Dst,
93 DAG.getConstant(Size1, DstVT));
94 DstPtrInfo = DstPtrInfo.getWithOffset(Size1);
95 SDValue Chain2 = memsetStore(DAG, DL, Chain, Dst, ByteVal, Size2,
96 std::min(Align, Size1), DstPtrInfo);
97 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2);
100 // Handle one and two bytes using STC.
102 SDValue Chain1 = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo,
103 false, false, Align);
106 SDValue Dst2 = DAG.getNode(ISD::ADD, DL, DstVT, Dst,
107 DAG.getConstant(1, DstVT));
108 SDValue Chain2 = DAG.getStore(Chain, DL, Byte, Dst2,
109 DstPtrInfo.getWithOffset(1),
111 return DAG.getNode(ISD::TokenFactor, DL, MVT::Other, Chain1, Chain2);
114 assert(Bytes >= 2 && "Should have dealt with 0- and 1-byte cases already");
115 if (Bytes <= 0x101) {
116 // Copy the byte to the first location and then use MVC to copy
118 Chain = DAG.getStore(Chain, DL, Byte, Dst, DstPtrInfo,
119 false, false, Align);
120 SDValue Dst2 = DAG.getNode(ISD::ADD, DL, DstVT, Dst,
121 DAG.getConstant(1, DstVT));
122 return DAG.getNode(SystemZISD::MVC, DL, MVT::Other, Chain, Dst2, Dst,
123 DAG.getConstant(Bytes - 1, MVT::i32));
129 // Convert the current CC value into an integer that is 0 if CC == 0,
130 // less than zero if CC == 1 and greater than zero if CC >= 2.
131 // The sequence starts with IPM, which puts CC into bits 29 and 28
132 // of an integer and clears bits 30 and 31.
133 static SDValue addIPMSequence(SDLoc DL, SDValue Glue, SelectionDAG &DAG) {
134 SDValue IPM = DAG.getNode(SystemZISD::IPM, DL, MVT::i32, Glue);
135 SDValue SRL = DAG.getNode(ISD::SRL, DL, MVT::i32, IPM,
136 DAG.getConstant(28, MVT::i32));
137 SDValue ROTL = DAG.getNode(ISD::ROTL, DL, MVT::i32, SRL,
138 DAG.getConstant(31, MVT::i32));
142 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::
143 EmitTargetCodeForMemcmp(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
144 SDValue Src1, SDValue Src2, SDValue Size,
145 MachinePointerInfo Op1PtrInfo,
146 MachinePointerInfo Op2PtrInfo) const {
147 if (ConstantSDNode *CSize = dyn_cast<ConstantSDNode>(Size)) {
148 uint64_t Bytes = CSize->getZExtValue();
149 if (Bytes >= 1 && Bytes <= 0x100) {
151 SDVTList VTs = DAG.getVTList(MVT::Other, MVT::Glue);
152 Chain = DAG.getNode(SystemZISD::CLC, DL, VTs, Chain,
154 SDValue Glue = Chain.getValue(1);
155 return std::make_pair(addIPMSequence(DL, Glue, DAG), Chain);
158 return std::make_pair(SDValue(), SDValue());
161 std::pair<SDValue, SDValue> SystemZSelectionDAGInfo::
162 EmitTargetCodeForStrcmp(SelectionDAG &DAG, SDLoc DL, SDValue Chain,
163 SDValue Src1, SDValue Src2,
164 MachinePointerInfo Op1PtrInfo,
165 MachinePointerInfo Op2PtrInfo) const {
166 SDVTList VTs = DAG.getVTList(Src1.getValueType(), MVT::Other, MVT::Glue);
167 SDValue Unused = DAG.getNode(SystemZISD::STRCMP, DL, VTs, Chain, Src1, Src2,
168 DAG.getConstant(0, MVT::i32));
169 Chain = Unused.getValue(1);
170 SDValue Glue = Chain.getValue(2);
171 return std::make_pair(addIPMSequence(DL, Glue, DAG), Chain);