blob: ce79fcf9ad811a599c320b549e4aac33e0f395bd [file] [log] [blame]
Dan Gohman9becddd2010-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
Eric Christophere5add682014-06-06 23:26:43 +000014#include "X86InstrInfo.h"
15#include "X86ISelLowering.h"
16#include "X86RegisterInfo.h"
17#include "X86Subtarget.h"
18#include "X86SelectionDAGInfo.h"
Dan Gohmanbb919df2010-05-11 17:31:57 +000019#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/DerivedTypes.h"
Eric Christophere5add682014-06-06 23:26:43 +000021#include "llvm/Target/TargetLowering.h"
22
Dan Gohman9becddd2010-04-16 23:04:22 +000023using namespace llvm;
24
Chandler Carruth84e68b22014-04-22 02:41:26 +000025#define DEBUG_TYPE "x86-selectiondag-info"
26
Reid Klecknerab99e242014-08-29 20:50:31 +000027bool X86SelectionDAGInfo::isBaseRegConflictPossible(
28 SelectionDAG &DAG, ArrayRef<unsigned> ClobberSet) const {
29 // We cannot use TRI->hasBasePointer() until *after* we select all basic
30 // blocks. Legalization may introduce new stack temporaries with large
31 // alignment requirements. Fall back to generic code if there are any
32 // dynamic stack adjustments (hopefully rare) and the base pointer would
33 // conflict if we had to use it.
34 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Reid Klecknere69bdb82015-07-07 23:45:58 +000035 if (!MFI->hasVarSizedObjects() && !MFI->hasOpaqueSPAdjustment())
Reid Klecknerab99e242014-08-29 20:50:31 +000036 return false;
37
38 const X86RegisterInfo *TRI = static_cast<const X86RegisterInfo *>(
39 DAG.getSubtarget().getRegisterInfo());
40 unsigned BaseReg = TRI->getBaseRegister();
41 for (unsigned R : ClobberSet)
42 if (BaseReg == R)
43 return true;
44 return false;
45}
46
Dan Gohmanbb919df2010-05-11 17:31:57 +000047SDValue
Andrew Trickef9de2a2013-05-25 02:42:55 +000048X86SelectionDAGInfo::EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc dl,
Dan Gohmanbb919df2010-05-11 17:31:57 +000049 SDValue Chain,
50 SDValue Dst, SDValue Src,
51 SDValue Size, unsigned Align,
52 bool isVolatile,
Chris Lattner2510de22010-09-21 05:40:29 +000053 MachinePointerInfo DstPtrInfo) const {
Dan Gohmanbb919df2010-05-11 17:31:57 +000054 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Eric Christopher05b81972015-02-02 17:38:43 +000055 const X86Subtarget &Subtarget =
56 DAG.getMachineFunction().getSubtarget<X86Subtarget>();
Dan Gohmanbb919df2010-05-11 17:31:57 +000057
Reid Klecknerab99e242014-08-29 20:50:31 +000058#ifndef NDEBUG
59 // If the base register might conflict with our physical registers, bail out.
Benjamin Kramer867bfc52015-03-07 17:41:00 +000060 const unsigned ClobberSet[] = {X86::RCX, X86::RAX, X86::RDI,
61 X86::ECX, X86::EAX, X86::EDI};
Reid Klecknerab99e242014-08-29 20:50:31 +000062 assert(!isBaseRegConflictPossible(DAG, ClobberSet));
63#endif
64
Chris Lattner4470c2b2010-09-21 05:43:34 +000065 // If to a segment-relative address space, use the default lowering.
66 if (DstPtrInfo.getAddrSpace() >= 256)
67 return SDValue();
Chad Rosier24c19d22012-08-01 18:39:17 +000068
Dan Gohmanbb919df2010-05-11 17:31:57 +000069 // If not DWORD aligned or size is more than the threshold, call the library.
70 // The libc version is likely to be faster for these cases. It can use the
71 // address value and run time information about the CPU.
Eric Christophere5add682014-06-06 23:26:43 +000072 if ((Align & 3) != 0 || !ConstantSize ||
73 ConstantSize->getZExtValue() > Subtarget.getMaxInlineSizeThreshold()) {
Dan Gohmanbb919df2010-05-11 17:31:57 +000074 // Check to see if there is a specialized entry-point for memory zeroing.
75 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
76
77 if (const char *bzeroEntry = V &&
Eric Christophere5add682014-06-06 23:26:43 +000078 V->isNullValue() ? Subtarget.getBZeroEntry() : nullptr) {
Mehdi Amini44ede332015-07-09 02:09:04 +000079 EVT IntPtr =
80 DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
81 Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext());
Dan Gohmanbb919df2010-05-11 17:31:57 +000082 TargetLowering::ArgListTy Args;
83 TargetLowering::ArgListEntry Entry;
84 Entry.Node = Dst;
85 Entry.Ty = IntPtrTy;
86 Args.push_back(Entry);
87 Entry.Node = Size;
88 Args.push_back(Entry);
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000089
90 TargetLowering::CallLoweringInfo CLI(DAG);
91 CLI.setDebugLoc(dl).setChain(Chain)
92 .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
Juergen Ributzka3bd03c72014-07-01 22:01:54 +000093 DAG.getExternalSymbol(bzeroEntry, IntPtr), std::move(Args),
94 0)
Saleem Abdulrasoolf3a5a5c2014-05-17 21:50:17 +000095 .setDiscardResult();
96
Eric Christophere5add682014-06-06 23:26:43 +000097 std::pair<SDValue,SDValue> CallResult = DAG.getTargetLoweringInfo().LowerCallTo(CLI);
Dan Gohmanbb919df2010-05-11 17:31:57 +000098 return CallResult.second;
99 }
100
101 // Otherwise have the target-independent code call memset.
102 return SDValue();
103 }
104
105 uint64_t SizeVal = ConstantSize->getZExtValue();
Craig Topper062a2ba2014-04-25 05:30:21 +0000106 SDValue InFlag;
Dan Gohmanbb919df2010-05-11 17:31:57 +0000107 EVT AVT;
108 SDValue Count;
109 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
110 unsigned BytesLeft = 0;
111 bool TwoRepStos = false;
112 if (ValC) {
113 unsigned ValReg;
114 uint64_t Val = ValC->getZExtValue() & 255;
115
116 // If the value is a constant, then we can potentially use larger sets.
117 switch (Align & 3) {
118 case 2: // WORD aligned
119 AVT = MVT::i16;
120 ValReg = X86::AX;
121 Val = (Val << 8) | Val;
122 break;
123 case 0: // DWORD aligned
124 AVT = MVT::i32;
125 ValReg = X86::EAX;
126 Val = (Val << 8) | Val;
127 Val = (Val << 16) | Val;
Eric Christophere5add682014-06-06 23:26:43 +0000128 if (Subtarget.is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Dan Gohmanbb919df2010-05-11 17:31:57 +0000129 AVT = MVT::i64;
130 ValReg = X86::RAX;
131 Val = (Val << 32) | Val;
132 }
133 break;
134 default: // Byte aligned
135 AVT = MVT::i8;
136 ValReg = X86::AL;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000137 Count = DAG.getIntPtrConstant(SizeVal, dl);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000138 break;
139 }
140
141 if (AVT.bitsGT(MVT::i8)) {
142 unsigned UBytes = AVT.getSizeInBits() / 8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000143 Count = DAG.getIntPtrConstant(SizeVal / UBytes, dl);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000144 BytesLeft = SizeVal % UBytes;
145 }
146
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000147 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, dl, AVT),
Dan Gohmanbb919df2010-05-11 17:31:57 +0000148 InFlag);
149 InFlag = Chain.getValue(1);
150 } else {
151 AVT = MVT::i8;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000152 Count = DAG.getIntPtrConstant(SizeVal, dl);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000153 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
154 InFlag = Chain.getValue(1);
155 }
156
Eric Christophere5add682014-06-06 23:26:43 +0000157 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX : X86::ECX,
158 Count, InFlag);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000159 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000160 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI : X86::EDI,
161 Dst, InFlag);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000162 InFlag = Chain.getValue(1);
163
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000164 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000165 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000166 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000167
168 if (TwoRepStos) {
169 InFlag = Chain.getValue(1);
170 Count = Size;
171 EVT CVT = Count.getValueType();
172 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000173 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, dl,
174 CVT));
Dan Gohmanbb919df2010-05-11 17:31:57 +0000175 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
176 X86::ECX,
177 Left, InFlag);
178 InFlag = Chain.getValue(1);
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000179 Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000180 SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000181 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000182 } else if (BytesLeft) {
183 // Handle the last 1 - 7 bytes.
184 unsigned Offset = SizeVal - BytesLeft;
185 EVT AddrVT = Dst.getValueType();
186 EVT SizeVT = Size.getValueType();
187
188 Chain = DAG.getMemset(Chain, dl,
189 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000190 DAG.getConstant(Offset, dl, AddrVT)),
Dan Gohmanbb919df2010-05-11 17:31:57 +0000191 Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000192 DAG.getConstant(BytesLeft, dl, SizeVT),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000193 Align, isVolatile, false,
194 DstPtrInfo.getWithOffset(Offset));
Dan Gohmanbb919df2010-05-11 17:31:57 +0000195 }
196
197 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
198 return Chain;
199}
200
Eric Christopher05b81972015-02-02 17:38:43 +0000201SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
202 SelectionDAG &DAG, SDLoc dl, SDValue Chain, SDValue Dst, SDValue Src,
203 SDValue Size, unsigned Align, bool isVolatile, bool AlwaysInline,
204 MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const {
Chris Lattner0ab5e2c2011-04-15 05:18:47 +0000205 // This requires the copy size to be a constant, preferably
Dan Gohmanbb919df2010-05-11 17:31:57 +0000206 // within a subtarget-specific limit.
207 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Eric Christopher05b81972015-02-02 17:38:43 +0000208 const X86Subtarget &Subtarget =
209 DAG.getMachineFunction().getSubtarget<X86Subtarget>();
Dan Gohmanbb919df2010-05-11 17:31:57 +0000210 if (!ConstantSize)
211 return SDValue();
212 uint64_t SizeVal = ConstantSize->getZExtValue();
Eric Christophere5add682014-06-06 23:26:43 +0000213 if (!AlwaysInline && SizeVal > Subtarget.getMaxInlineSizeThreshold())
Dan Gohmanbb919df2010-05-11 17:31:57 +0000214 return SDValue();
215
Duncan Sands98512312010-11-04 21:16:46 +0000216 /// If not DWORD aligned, it is more efficient to call the library. However
217 /// if calling the library is not allowed (AlwaysInline), then soldier on as
218 /// the code generated here is better than the long load-store sequence we
219 /// would otherwise get.
220 if (!AlwaysInline && (Align & 3) != 0)
Dan Gohmanbb919df2010-05-11 17:31:57 +0000221 return SDValue();
222
Chris Lattner4470c2b2010-09-21 05:43:34 +0000223 // If to a segment-relative address space, use the default lowering.
224 if (DstPtrInfo.getAddrSpace() >= 256 ||
225 SrcPtrInfo.getAddrSpace() >= 256)
226 return SDValue();
Duncan Sands98512312010-11-04 21:16:46 +0000227
Reid Klecknerab99e242014-08-29 20:50:31 +0000228 // If the base register might conflict with our physical registers, bail out.
Benjamin Kramer867bfc52015-03-07 17:41:00 +0000229 const unsigned ClobberSet[] = {X86::RCX, X86::RSI, X86::RDI,
230 X86::ECX, X86::ESI, X86::EDI};
Reid Klecknerab99e242014-08-29 20:50:31 +0000231 if (isBaseRegConflictPossible(DAG, ClobberSet))
Hans Wennborgd683a222014-03-26 16:30:54 +0000232 return SDValue();
Benjamin Kramer8e2637e2013-02-13 13:40:35 +0000233
Duncan Sands98512312010-11-04 21:16:46 +0000234 MVT AVT;
235 if (Align & 1)
236 AVT = MVT::i8;
237 else if (Align & 2)
238 AVT = MVT::i16;
239 else if (Align & 4)
240 // DWORD aligned
241 AVT = MVT::i32;
242 else
243 // QWORD aligned
Eric Christophere5add682014-06-06 23:26:43 +0000244 AVT = Subtarget.is64Bit() ? MVT::i64 : MVT::i32;
Dan Gohmanbb919df2010-05-11 17:31:57 +0000245
246 unsigned UBytes = AVT.getSizeInBits() / 8;
247 unsigned CountVal = SizeVal / UBytes;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000248 SDValue Count = DAG.getIntPtrConstant(CountVal, dl);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000249 unsigned BytesLeft = SizeVal % UBytes;
250
Craig Topper062a2ba2014-04-25 05:30:21 +0000251 SDValue InFlag;
Eric Christophere5add682014-06-06 23:26:43 +0000252 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000253 X86::ECX,
Hans Wennborgd683a222014-03-26 16:30:54 +0000254 Count, InFlag);
255 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000256 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000257 X86::EDI,
Hans Wennborgd683a222014-03-26 16:30:54 +0000258 Dst, InFlag);
259 InFlag = Chain.getValue(1);
Eric Christophere5add682014-06-06 23:26:43 +0000260 Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RSI :
Dan Gohmanbb919df2010-05-11 17:31:57 +0000261 X86::ESI,
Hans Wennborgd683a222014-03-26 16:30:54 +0000262 Src, InFlag);
263 InFlag = Chain.getValue(1);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000264
Chris Lattner3e5fbd72010-12-21 02:38:05 +0000265 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
Hans Wennborgd683a222014-03-26 16:30:54 +0000266 SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
Craig Topper48d114b2014-04-26 18:35:24 +0000267 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000268
269 SmallVector<SDValue, 4> Results;
270 Results.push_back(RepMovs);
271 if (BytesLeft) {
272 // Handle the last 1 - 7 bytes.
273 unsigned Offset = SizeVal - BytesLeft;
274 EVT DstVT = Dst.getValueType();
275 EVT SrcVT = Src.getValueType();
276 EVT SizeVT = Size.getValueType();
277 Results.push_back(DAG.getMemcpy(Chain, dl,
278 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000279 DAG.getConstant(Offset, dl,
280 DstVT)),
Dan Gohmanbb919df2010-05-11 17:31:57 +0000281 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000282 DAG.getConstant(Offset, dl,
283 SrcVT)),
284 DAG.getConstant(BytesLeft, dl, SizeVT),
Krzysztof Parzyszeka46c36b2015-04-13 17:16:45 +0000285 Align, isVolatile, AlwaysInline, false,
Chris Lattner2510de22010-09-21 05:40:29 +0000286 DstPtrInfo.getWithOffset(Offset),
287 SrcPtrInfo.getWithOffset(Offset)));
Dan Gohmanbb919df2010-05-11 17:31:57 +0000288 }
289
Craig Topper48d114b2014-04-26 18:35:24 +0000290 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Results);
Dan Gohmanbb919df2010-05-11 17:31:57 +0000291}