1c7a6af3e59f1e07305f97937d1c4248b708234e
[oota-llvm.git] / lib / Target / X86 / X86SelectionDAGInfo.cpp
1 //===-- 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 #include "X86InstrInfo.h"
15 #include "X86ISelLowering.h"
16 #include "X86RegisterInfo.h"
17 #include "X86Subtarget.h"
18 #include "X86SelectionDAGInfo.h"
19 #include "llvm/CodeGen/SelectionDAG.h"
20 #include "llvm/IR/DerivedTypes.h"
21 #include "llvm/Target/TargetLowering.h"
22
23 using namespace llvm;
24
25 #define DEBUG_TYPE "x86-selectiondag-info"
26
27 X86SelectionDAGInfo::X86SelectionDAGInfo(const DataLayout &DL)
28     : TargetSelectionDAGInfo(&DL) {}
29
30 X86SelectionDAGInfo::~X86SelectionDAGInfo() {}
31
32 bool X86SelectionDAGInfo::isBaseRegConflictPossible(
33     SelectionDAG &DAG, ArrayRef<unsigned> ClobberSet) const {
34   // We cannot use TRI->hasBasePointer() until *after* we select all basic
35   // blocks.  Legalization may introduce new stack temporaries with large
36   // alignment requirements.  Fall back to generic code if there are any
37   // dynamic stack adjustments (hopefully rare) and the base pointer would
38   // conflict if we had to use it.
39   MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
40   if (!MFI->hasVarSizedObjects() && !MFI->hasOpaqueSPAdjustment())
41     return false;
42
43   const X86RegisterInfo *TRI = static_cast<const X86RegisterInfo *>(
44       DAG.getSubtarget().getRegisterInfo());
45   unsigned BaseReg = TRI->getBaseRegister();
46   for (unsigned R : ClobberSet)
47     if (BaseReg == R)
48       return true;
49   return false;
50 }
51
52 SDValue
53 X86SelectionDAGInfo::EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc dl,
54                                              SDValue Chain,
55                                              SDValue Dst, SDValue Src,
56                                              SDValue Size, unsigned Align,
57                                              bool isVolatile,
58                                          MachinePointerInfo DstPtrInfo) const {
59   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
60   const X86Subtarget &Subtarget =
61       DAG.getMachineFunction().getSubtarget<X86Subtarget>();
62
63 #ifndef NDEBUG
64   // If the base register might conflict with our physical registers, bail out.
65   const unsigned ClobberSet[] = {X86::RCX, X86::RAX, X86::RDI,
66                                  X86::ECX, X86::EAX, X86::EDI};
67   assert(!isBaseRegConflictPossible(DAG, ClobberSet));
68 #endif
69
70   // If to a segment-relative address space, use the default lowering.
71   if (DstPtrInfo.getAddrSpace() >= 256)
72     return SDValue();
73
74   // If not DWORD aligned or size is more than the threshold, call the library.
75   // The libc version is likely to be faster for these cases. It can use the
76   // address value and run time information about the CPU.
77   if ((Align & 3) != 0 || !ConstantSize ||
78       ConstantSize->getZExtValue() > Subtarget.getMaxInlineSizeThreshold()) {
79     // Check to see if there is a specialized entry-point for memory zeroing.
80     ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
81
82     if (const char *bzeroEntry =  V &&
83         V->isNullValue() ? Subtarget.getBZeroEntry() : nullptr) {
84       EVT IntPtr =
85           DAG.getTargetLoweringInfo().getPointerTy(DAG.getDataLayout());
86       Type *IntPtrTy = DAG.getDataLayout().getIntPtrType(*DAG.getContext());
87       TargetLowering::ArgListTy Args;
88       TargetLowering::ArgListEntry Entry;
89       Entry.Node = Dst;
90       Entry.Ty = IntPtrTy;
91       Args.push_back(Entry);
92       Entry.Node = Size;
93       Args.push_back(Entry);
94
95       TargetLowering::CallLoweringInfo CLI(DAG);
96       CLI.setDebugLoc(dl).setChain(Chain)
97         .setCallee(CallingConv::C, Type::getVoidTy(*DAG.getContext()),
98                    DAG.getExternalSymbol(bzeroEntry, IntPtr), std::move(Args),
99                    0)
100         .setDiscardResult();
101
102       std::pair<SDValue,SDValue> CallResult = DAG.getTargetLoweringInfo().LowerCallTo(CLI);
103       return CallResult.second;
104     }
105
106     // Otherwise have the target-independent code call memset.
107     return SDValue();
108   }
109
110   uint64_t SizeVal = ConstantSize->getZExtValue();
111   SDValue InFlag;
112   EVT AVT;
113   SDValue Count;
114   ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
115   unsigned BytesLeft = 0;
116   bool TwoRepStos = false;
117   if (ValC) {
118     unsigned ValReg;
119     uint64_t Val = ValC->getZExtValue() & 255;
120
121     // If the value is a constant, then we can potentially use larger sets.
122     switch (Align & 3) {
123     case 2:   // WORD aligned
124       AVT = MVT::i16;
125       ValReg = X86::AX;
126       Val = (Val << 8) | Val;
127       break;
128     case 0:  // DWORD aligned
129       AVT = MVT::i32;
130       ValReg = X86::EAX;
131       Val = (Val << 8)  | Val;
132       Val = (Val << 16) | Val;
133       if (Subtarget.is64Bit() && ((Align & 0x7) == 0)) {  // QWORD aligned
134         AVT = MVT::i64;
135         ValReg = X86::RAX;
136         Val = (Val << 32) | Val;
137       }
138       break;
139     default:  // Byte aligned
140       AVT = MVT::i8;
141       ValReg = X86::AL;
142       Count = DAG.getIntPtrConstant(SizeVal, dl);
143       break;
144     }
145
146     if (AVT.bitsGT(MVT::i8)) {
147       unsigned UBytes = AVT.getSizeInBits() / 8;
148       Count = DAG.getIntPtrConstant(SizeVal / UBytes, dl);
149       BytesLeft = SizeVal % UBytes;
150     }
151
152     Chain  = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, dl, AVT),
153                               InFlag);
154     InFlag = Chain.getValue(1);
155   } else {
156     AVT = MVT::i8;
157     Count  = DAG.getIntPtrConstant(SizeVal, dl);
158     Chain  = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
159     InFlag = Chain.getValue(1);
160   }
161
162   Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX : X86::ECX,
163                            Count, InFlag);
164   InFlag = Chain.getValue(1);
165   Chain = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI : X86::EDI,
166                            Dst, InFlag);
167   InFlag = Chain.getValue(1);
168
169   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
170   SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
171   Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
172
173   if (TwoRepStos) {
174     InFlag = Chain.getValue(1);
175     Count  = Size;
176     EVT CVT = Count.getValueType();
177     SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
178                                DAG.getConstant((AVT == MVT::i64) ? 7 : 3, dl,
179                                                CVT));
180     Chain  = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
181                                                              X86::ECX,
182                               Left, InFlag);
183     InFlag = Chain.getValue(1);
184     Tys = DAG.getVTList(MVT::Other, MVT::Glue);
185     SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag };
186     Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops);
187   } else if (BytesLeft) {
188     // Handle the last 1 - 7 bytes.
189     unsigned Offset = SizeVal - BytesLeft;
190     EVT AddrVT = Dst.getValueType();
191     EVT SizeVT = Size.getValueType();
192
193     Chain = DAG.getMemset(Chain, dl,
194                           DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
195                                       DAG.getConstant(Offset, dl, AddrVT)),
196                           Src,
197                           DAG.getConstant(BytesLeft, dl, SizeVT),
198                           Align, isVolatile, false,
199                           DstPtrInfo.getWithOffset(Offset));
200   }
201
202   // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
203   return Chain;
204 }
205
206 SDValue X86SelectionDAGInfo::EmitTargetCodeForMemcpy(
207     SelectionDAG &DAG, SDLoc dl, SDValue Chain, SDValue Dst, SDValue Src,
208     SDValue Size, unsigned Align, bool isVolatile, bool AlwaysInline,
209     MachinePointerInfo DstPtrInfo, MachinePointerInfo SrcPtrInfo) const {
210   // This requires the copy size to be a constant, preferably
211   // within a subtarget-specific limit.
212   ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
213   const X86Subtarget &Subtarget =
214       DAG.getMachineFunction().getSubtarget<X86Subtarget>();
215   if (!ConstantSize)
216     return SDValue();
217   uint64_t SizeVal = ConstantSize->getZExtValue();
218   if (!AlwaysInline && SizeVal > Subtarget.getMaxInlineSizeThreshold())
219     return SDValue();
220
221   /// If not DWORD aligned, it is more efficient to call the library.  However
222   /// if calling the library is not allowed (AlwaysInline), then soldier on as
223   /// the code generated here is better than the long load-store sequence we
224   /// would otherwise get.
225   if (!AlwaysInline && (Align & 3) != 0)
226     return SDValue();
227
228   // If to a segment-relative address space, use the default lowering.
229   if (DstPtrInfo.getAddrSpace() >= 256 ||
230       SrcPtrInfo.getAddrSpace() >= 256)
231     return SDValue();
232
233   // If the base register might conflict with our physical registers, bail out.
234   const unsigned ClobberSet[] = {X86::RCX, X86::RSI, X86::RDI,
235                                  X86::ECX, X86::ESI, X86::EDI};
236   if (isBaseRegConflictPossible(DAG, ClobberSet))
237     return SDValue();
238
239   MVT AVT;
240   if (Align & 1)
241     AVT = MVT::i8;
242   else if (Align & 2)
243     AVT = MVT::i16;
244   else if (Align & 4)
245     // DWORD aligned
246     AVT = MVT::i32;
247   else
248     // QWORD aligned
249     AVT = Subtarget.is64Bit() ? MVT::i64 : MVT::i32;
250
251   unsigned UBytes = AVT.getSizeInBits() / 8;
252   unsigned CountVal = SizeVal / UBytes;
253   SDValue Count = DAG.getIntPtrConstant(CountVal, dl);
254   unsigned BytesLeft = SizeVal % UBytes;
255
256   SDValue InFlag;
257   Chain  = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RCX :
258                                                               X86::ECX,
259                             Count, InFlag);
260   InFlag = Chain.getValue(1);
261   Chain  = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RDI :
262                                                               X86::EDI,
263                             Dst, InFlag);
264   InFlag = Chain.getValue(1);
265   Chain  = DAG.getCopyToReg(Chain, dl, Subtarget.is64Bit() ? X86::RSI :
266                                                               X86::ESI,
267                             Src, InFlag);
268   InFlag = Chain.getValue(1);
269
270   SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue);
271   SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag };
272   SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops);
273
274   SmallVector<SDValue, 4> Results;
275   Results.push_back(RepMovs);
276   if (BytesLeft) {
277     // Handle the last 1 - 7 bytes.
278     unsigned Offset = SizeVal - BytesLeft;
279     EVT DstVT = Dst.getValueType();
280     EVT SrcVT = Src.getValueType();
281     EVT SizeVT = Size.getValueType();
282     Results.push_back(DAG.getMemcpy(Chain, dl,
283                                     DAG.getNode(ISD::ADD, dl, DstVT, Dst,
284                                                 DAG.getConstant(Offset, dl,
285                                                                 DstVT)),
286                                     DAG.getNode(ISD::ADD, dl, SrcVT, Src,
287                                                 DAG.getConstant(Offset, dl,
288                                                                 SrcVT)),
289                                     DAG.getConstant(BytesLeft, dl, SizeVT),
290                                     Align, isVolatile, AlwaysInline, false,
291                                     DstPtrInfo.getWithOffset(Offset),
292                                     SrcPtrInfo.getWithOffset(Offset)));
293   }
294
295   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Results);
296 }