blob: 4adca83af19883eb76c970a9f69b3ff01941195c [file] [log] [blame]
Dan Gohman53c5e422010-04-16 23:04:22 +00001//===-- X86SelectionDAGInfo.cpp - X86 SelectionDAG Info -------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the X86SelectionDAGInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "x86-selectiondag-info"
Dan Gohmanff7a5622010-05-11 17:31:57 +000015#include "X86TargetMachine.h"
16#include "llvm/DerivedTypes.h"
17#include "llvm/CodeGen/SelectionDAG.h"
Dan Gohman53c5e422010-04-16 23:04:22 +000018using namespace llvm;
19
Dan Gohmanff7a5622010-05-11 17:31:57 +000020X86SelectionDAGInfo::X86SelectionDAGInfo(const X86TargetMachine &TM) :
21 TargetSelectionDAGInfo(TM),
22 Subtarget(&TM.getSubtarget<X86Subtarget>()),
23 TLI(*TM.getTargetLowering()) {
Dan Gohman53c5e422010-04-16 23:04:22 +000024}
25
26X86SelectionDAGInfo::~X86SelectionDAGInfo() {
27}
Dan Gohmanff7a5622010-05-11 17:31:57 +000028
29SDValue
30X86SelectionDAGInfo::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
31 SDValue Chain,
32 SDValue Dst, SDValue Src,
33 SDValue Size, unsigned Align,
34 bool isVolatile,
Chris Lattnere72f2022010-09-21 05:40:29 +000035 MachinePointerInfo DstPtrInfo) const {
Dan Gohmanff7a5622010-05-11 17:31:57 +000036 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
37
Chris Lattnere54b4822010-09-21 05:43:34 +000038 // If to a segment-relative address space, use the default lowering.
39 if (DstPtrInfo.getAddrSpace() >= 256)
40 return SDValue();
Chad Rosiera20e1e72012-08-01 18:39:17 +000041
Dan Gohmanff7a5622010-05-11 17:31:57 +000042 // If not DWORD aligned or size is more than the threshold, call the library.
43 // The libc version is likely to be faster for these cases. It can use the
44 // address value and run time information about the CPU.
45 if ((Align & 3) != 0 ||
46 !ConstantSize ||
47 ConstantSize->getZExtValue() >
48 Subtarget->getMaxInlineSizeThreshold()) {
49 SDValue InFlag(0, 0);
50
51 // Check to see if there is a specialized entry-point for memory zeroing.
52 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
53
54 if (const char *bzeroEntry = V &&
55 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
56 EVT IntPtr = TLI.getPointerTy();
Micah Villmowaa76e9e2012-10-24 15:52:52 +000057 unsigned AS = DstPtrInfo.getAddrSpace();
58 Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext(), AS);
Dan Gohmanff7a5622010-05-11 17:31:57 +000059 TargetLowering::ArgListTy Args;
60 TargetLowering::ArgListEntry Entry;
61 Entry.Node = Dst;
62 Entry.Ty = IntPtrTy;
63 Args.push_back(Entry);
64 Entry.Node = Size;
65 Args.push_back(Entry);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +000066 TargetLowering::
67 CallLoweringInfo CLI(Chain, Type::getVoidTy(*DAG.getContext()),
Dan Gohmanff7a5622010-05-11 17:31:57 +000068 false, false, false, false,
Evan Cheng4bfcd4a2012-02-28 18:51:51 +000069 0, CallingConv::C, /*isTailCall=*/false,
70 /*doesNotRet=*/false, /*isReturnValueUsed=*/false,
Dan Gohmanff7a5622010-05-11 17:31:57 +000071 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args,
72 DAG, dl);
Justin Holewinskid2ea0e12012-05-25 16:35:28 +000073 std::pair<SDValue,SDValue> CallResult =
74 TLI.LowerCallTo(CLI);
Dan Gohmanff7a5622010-05-11 17:31:57 +000075 return CallResult.second;
76 }
77
78 // Otherwise have the target-independent code call memset.
79 return SDValue();
80 }
81
82 uint64_t SizeVal = ConstantSize->getZExtValue();
83 SDValue InFlag(0, 0);
84 EVT AVT;
85 SDValue Count;
86 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
87 unsigned BytesLeft = 0;
88 bool TwoRepStos = false;
89 if (ValC) {
90 unsigned ValReg;
91 uint64_t Val = ValC->getZExtValue() & 255;
92
93 // If the value is a constant, then we can potentially use larger sets.
94 switch (Align & 3) {
95 case 2: // WORD aligned
96 AVT = MVT::i16;
97 ValReg = X86::AX;
98 Val = (Val << 8) | Val;
99 break;
100 case 0: // DWORD aligned
101 AVT = MVT::i32;
102 ValReg = X86::EAX;
103 Val = (Val << 8) | Val;
104 Val = (Val << 16) | Val;
105 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
106 AVT = MVT::i64;
107 ValReg = X86::RAX;
108 Val = (Val << 32) | Val;
109 }
110 break;
111 default: // Byte aligned
112 AVT = MVT::i8;
113 ValReg = X86::AL;
114 Count = DAG.getIntPtrConstant(SizeVal);
115 break;
116 }
117
118 if (AVT.bitsGT(MVT::i8)) {
119 unsigned UBytes = AVT.getSizeInBits() / 8;
120 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
121 BytesLeft = SizeVal % UBytes;
122 }
123
124 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
125 InFlag);
126 InFlag = Chain.getValue(1);
127 } else {
128 AVT = MVT::i8;
129 Count = DAG.getIntPtrConstant(SizeVal);
130 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
131 InFlag = Chain.getValue(1);
132 }
133
134 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
135 X86::ECX,
136 Count, InFlag);
137 InFlag = Chain.getValue(1);
138 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
139 X86::EDI,
140 Dst, InFlag);
141 InFlag = Chain.getValue(1);
142
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000143 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanff7a5622010-05-11 17:31:57 +0000144 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
145 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
146
147 if (TwoRepStos) {
148 InFlag = Chain.getValue(1);
149 Count = Size;
150 EVT CVT = Count.getValueType();
151 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
152 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
153 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
154 X86::ECX,
155 Left, InFlag);
156 InFlag = Chain.getValue(1);
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000157 Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanff7a5622010-05-11 17:31:57 +0000158 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
159 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops));
160 } else if (BytesLeft) {
161 // Handle the last 1 - 7 bytes.
162 unsigned Offset = SizeVal - BytesLeft;
163 EVT AddrVT = Dst.getValueType();
164 EVT SizeVT = Size.getValueType();
165
166 Chain = DAG.getMemset(Chain, dl,
167 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
168 DAG.getConstant(Offset, AddrVT)),
169 Src,
170 DAG.getConstant(BytesLeft, SizeVT),
Chris Lattnere72f2022010-09-21 05:40:29 +0000171 Align, isVolatile, DstPtrInfo.getWithOffset(Offset));
Dan Gohmanff7a5622010-05-11 17:31:57 +0000172 }
173
174 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
175 return Chain;
176}
177
178SDValue
179X86SelectionDAGInfo::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
180 SDValue Chain, SDValue Dst, SDValue Src,
181 SDValue Size, unsigned Align,
182 bool isVolatile, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +0000183 MachinePointerInfo DstPtrInfo,
184 MachinePointerInfo SrcPtrInfo) const {
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000185 // This requires the copy size to be a constant, preferably
Dan Gohmanff7a5622010-05-11 17:31:57 +0000186 // within a subtarget-specific limit.
187 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
188 if (!ConstantSize)
189 return SDValue();
190 uint64_t SizeVal = ConstantSize->getZExtValue();
191 if (!AlwaysInline && SizeVal > Subtarget->getMaxInlineSizeThreshold())
192 return SDValue();
193
Duncan Sands1e92ec62010-11-04 21:16:46 +0000194 /// If not DWORD aligned, it is more efficient to call the library. However
195 /// if calling the library is not allowed (AlwaysInline), then soldier on as
196 /// the code generated here is better than the long load-store sequence we
197 /// would otherwise get.
198 if (!AlwaysInline && (Align & 3) != 0)
Dan Gohmanff7a5622010-05-11 17:31:57 +0000199 return SDValue();
200
Chris Lattnere54b4822010-09-21 05:43:34 +0000201 // If to a segment-relative address space, use the default lowering.
202 if (DstPtrInfo.getAddrSpace() >= 256 ||
203 SrcPtrInfo.getAddrSpace() >= 256)
204 return SDValue();
Duncan Sands1e92ec62010-11-04 21:16:46 +0000205
206 MVT AVT;
207 if (Align & 1)
208 AVT = MVT::i8;
209 else if (Align & 2)
210 AVT = MVT::i16;
211 else if (Align & 4)
212 // DWORD aligned
213 AVT = MVT::i32;
214 else
215 // QWORD aligned
216 AVT = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanff7a5622010-05-11 17:31:57 +0000217
218 unsigned UBytes = AVT.getSizeInBits() / 8;
219 unsigned CountVal = SizeVal / UBytes;
220 SDValue Count = DAG.getIntPtrConstant(CountVal);
221 unsigned BytesLeft = SizeVal % UBytes;
222
223 SDValue InFlag(0, 0);
224 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
225 X86::ECX,
226 Count, InFlag);
227 InFlag = Chain.getValue(1);
228 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
229 X86::EDI,
230 Dst, InFlag);
231 InFlag = Chain.getValue(1);
232 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
233 X86::ESI,
234 Src, InFlag);
235 InFlag = Chain.getValue(1);
236
Chris Lattnerf1b4eaf2010-12-21 02:38:05 +0000237 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanff7a5622010-05-11 17:31:57 +0000238 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
239 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops,
240 array_lengthof(Ops));
241
242 SmallVector<SDValue, 4> Results;
243 Results.push_back(RepMovs);
244 if (BytesLeft) {
245 // Handle the last 1 - 7 bytes.
246 unsigned Offset = SizeVal - BytesLeft;
247 EVT DstVT = Dst.getValueType();
248 EVT SrcVT = Src.getValueType();
249 EVT SizeVT = Size.getValueType();
250 Results.push_back(DAG.getMemcpy(Chain, dl,
251 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
252 DAG.getConstant(Offset, DstVT)),
253 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
254 DAG.getConstant(Offset, SrcVT)),
255 DAG.getConstant(BytesLeft, SizeVT),
256 Align, isVolatile, AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +0000257 DstPtrInfo.getWithOffset(Offset),
258 SrcPtrInfo.getWithOffset(Offset)));
Dan Gohmanff7a5622010-05-11 17:31:57 +0000259 }
260
261 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
262 &Results[0], Results.size());
263}