Dan Gohman | 9becddd | 2010-04-16 23:04:22 +0000 | [diff] [blame] | 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 | |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 14 | #include "X86TargetMachine.h" |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/SelectionDAG.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/DerivedTypes.h" |
Dan Gohman | 9becddd | 2010-04-16 23:04:22 +0000 | [diff] [blame] | 17 | using namespace llvm; |
| 18 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 19 | #define DEBUG_TYPE "x86-selectiondag-info" |
| 20 | |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 21 | X86SelectionDAGInfo::X86SelectionDAGInfo(const X86TargetMachine &TM) : |
| 22 | TargetSelectionDAGInfo(TM), |
| 23 | Subtarget(&TM.getSubtarget<X86Subtarget>()), |
| 24 | TLI(*TM.getTargetLowering()) { |
Dan Gohman | 9becddd | 2010-04-16 23:04:22 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | X86SelectionDAGInfo::~X86SelectionDAGInfo() { |
| 28 | } |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 29 | |
| 30 | SDValue |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 31 | X86SelectionDAGInfo::EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc dl, |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 32 | SDValue Chain, |
| 33 | SDValue Dst, SDValue Src, |
| 34 | SDValue Size, unsigned Align, |
| 35 | bool isVolatile, |
Chris Lattner | 2510de2 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 36 | MachinePointerInfo DstPtrInfo) const { |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 37 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
| 38 | |
Chris Lattner | 4470c2b | 2010-09-21 05:43:34 +0000 | [diff] [blame] | 39 | // If to a segment-relative address space, use the default lowering. |
| 40 | if (DstPtrInfo.getAddrSpace() >= 256) |
| 41 | return SDValue(); |
Chad Rosier | 24c19d2 | 2012-08-01 18:39:17 +0000 | [diff] [blame] | 42 | |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 43 | // If not DWORD aligned or size is more than the threshold, call the library. |
| 44 | // The libc version is likely to be faster for these cases. It can use the |
| 45 | // address value and run time information about the CPU. |
| 46 | if ((Align & 3) != 0 || |
| 47 | !ConstantSize || |
| 48 | ConstantSize->getZExtValue() > |
| 49 | Subtarget->getMaxInlineSizeThreshold()) { |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 50 | // Check to see if there is a specialized entry-point for memory zeroing. |
| 51 | ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src); |
| 52 | |
| 53 | if (const char *bzeroEntry = V && |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 54 | V->isNullValue() ? Subtarget->getBZeroEntry() : nullptr) { |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 55 | EVT IntPtr = TLI.getPointerTy(); |
Chandler Carruth | 7ec5085 | 2012-11-01 08:07:29 +0000 | [diff] [blame] | 56 | Type *IntPtrTy = getDataLayout()->getIntPtrType(*DAG.getContext()); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 57 | TargetLowering::ArgListTy Args; |
| 58 | TargetLowering::ArgListEntry Entry; |
| 59 | Entry.Node = Dst; |
| 60 | Entry.Ty = IntPtrTy; |
| 61 | Args.push_back(Entry); |
| 62 | Entry.Node = Size; |
| 63 | Args.push_back(Entry); |
Justin Holewinski | aa58397 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 64 | TargetLowering:: |
| 65 | CallLoweringInfo CLI(Chain, Type::getVoidTy(*DAG.getContext()), |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 66 | false, false, false, false, |
Evan Cheng | 65f9d19 | 2012-02-28 18:51:51 +0000 | [diff] [blame] | 67 | 0, CallingConv::C, /*isTailCall=*/false, |
| 68 | /*doesNotRet=*/false, /*isReturnValueUsed=*/false, |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 69 | DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, |
| 70 | DAG, dl); |
Justin Holewinski | aa58397 | 2012-05-25 16:35:28 +0000 | [diff] [blame] | 71 | std::pair<SDValue,SDValue> CallResult = |
| 72 | TLI.LowerCallTo(CLI); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 73 | return CallResult.second; |
| 74 | } |
| 75 | |
| 76 | // Otherwise have the target-independent code call memset. |
| 77 | return SDValue(); |
| 78 | } |
| 79 | |
| 80 | uint64_t SizeVal = ConstantSize->getZExtValue(); |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 81 | SDValue InFlag; |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 82 | EVT AVT; |
| 83 | SDValue Count; |
| 84 | ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src); |
| 85 | unsigned BytesLeft = 0; |
| 86 | bool TwoRepStos = false; |
| 87 | if (ValC) { |
| 88 | unsigned ValReg; |
| 89 | uint64_t Val = ValC->getZExtValue() & 255; |
| 90 | |
| 91 | // If the value is a constant, then we can potentially use larger sets. |
| 92 | switch (Align & 3) { |
| 93 | case 2: // WORD aligned |
| 94 | AVT = MVT::i16; |
| 95 | ValReg = X86::AX; |
| 96 | Val = (Val << 8) | Val; |
| 97 | break; |
| 98 | case 0: // DWORD aligned |
| 99 | AVT = MVT::i32; |
| 100 | ValReg = X86::EAX; |
| 101 | Val = (Val << 8) | Val; |
| 102 | Val = (Val << 16) | Val; |
| 103 | if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned |
| 104 | AVT = MVT::i64; |
| 105 | ValReg = X86::RAX; |
| 106 | Val = (Val << 32) | Val; |
| 107 | } |
| 108 | break; |
| 109 | default: // Byte aligned |
| 110 | AVT = MVT::i8; |
| 111 | ValReg = X86::AL; |
| 112 | Count = DAG.getIntPtrConstant(SizeVal); |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | if (AVT.bitsGT(MVT::i8)) { |
| 117 | unsigned UBytes = AVT.getSizeInBits() / 8; |
| 118 | Count = DAG.getIntPtrConstant(SizeVal / UBytes); |
| 119 | BytesLeft = SizeVal % UBytes; |
| 120 | } |
| 121 | |
| 122 | Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT), |
| 123 | InFlag); |
| 124 | InFlag = Chain.getValue(1); |
| 125 | } else { |
| 126 | AVT = MVT::i8; |
| 127 | Count = DAG.getIntPtrConstant(SizeVal); |
| 128 | Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag); |
| 129 | InFlag = Chain.getValue(1); |
| 130 | } |
| 131 | |
| 132 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX : |
| 133 | X86::ECX, |
| 134 | Count, InFlag); |
| 135 | InFlag = Chain.getValue(1); |
| 136 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI : |
| 137 | X86::EDI, |
| 138 | Dst, InFlag); |
| 139 | InFlag = Chain.getValue(1); |
| 140 | |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 141 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 142 | SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag }; |
| 143 | Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops)); |
| 144 | |
| 145 | if (TwoRepStos) { |
| 146 | InFlag = Chain.getValue(1); |
| 147 | Count = Size; |
| 148 | EVT CVT = Count.getValueType(); |
| 149 | SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count, |
| 150 | DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT)); |
| 151 | Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX : |
| 152 | X86::ECX, |
| 153 | Left, InFlag); |
| 154 | InFlag = Chain.getValue(1); |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 155 | Tys = DAG.getVTList(MVT::Other, MVT::Glue); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 156 | SDValue Ops[] = { Chain, DAG.getValueType(MVT::i8), InFlag }; |
| 157 | Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, Ops, array_lengthof(Ops)); |
| 158 | } else if (BytesLeft) { |
| 159 | // Handle the last 1 - 7 bytes. |
| 160 | unsigned Offset = SizeVal - BytesLeft; |
| 161 | EVT AddrVT = Dst.getValueType(); |
| 162 | EVT SizeVT = Size.getValueType(); |
| 163 | |
| 164 | Chain = DAG.getMemset(Chain, dl, |
| 165 | DAG.getNode(ISD::ADD, dl, AddrVT, Dst, |
| 166 | DAG.getConstant(Offset, AddrVT)), |
| 167 | Src, |
| 168 | DAG.getConstant(BytesLeft, SizeVT), |
Chris Lattner | 2510de2 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 169 | Align, isVolatile, DstPtrInfo.getWithOffset(Offset)); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain. |
| 173 | return Chain; |
| 174 | } |
| 175 | |
| 176 | SDValue |
Andrew Trick | ef9de2a | 2013-05-25 02:42:55 +0000 | [diff] [blame] | 177 | X86SelectionDAGInfo::EmitTargetCodeForMemcpy(SelectionDAG &DAG, SDLoc dl, |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 178 | SDValue Chain, SDValue Dst, SDValue Src, |
| 179 | SDValue Size, unsigned Align, |
| 180 | bool isVolatile, bool AlwaysInline, |
Chris Lattner | 2510de2 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 181 | MachinePointerInfo DstPtrInfo, |
| 182 | MachinePointerInfo SrcPtrInfo) const { |
Chris Lattner | 0ab5e2c | 2011-04-15 05:18:47 +0000 | [diff] [blame] | 183 | // This requires the copy size to be a constant, preferably |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 184 | // within a subtarget-specific limit. |
| 185 | ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size); |
| 186 | if (!ConstantSize) |
| 187 | return SDValue(); |
| 188 | uint64_t SizeVal = ConstantSize->getZExtValue(); |
| 189 | if (!AlwaysInline && SizeVal > Subtarget->getMaxInlineSizeThreshold()) |
| 190 | return SDValue(); |
| 191 | |
Duncan Sands | 9851231 | 2010-11-04 21:16:46 +0000 | [diff] [blame] | 192 | /// If not DWORD aligned, it is more efficient to call the library. However |
| 193 | /// if calling the library is not allowed (AlwaysInline), then soldier on as |
| 194 | /// the code generated here is better than the long load-store sequence we |
| 195 | /// would otherwise get. |
| 196 | if (!AlwaysInline && (Align & 3) != 0) |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 197 | return SDValue(); |
| 198 | |
Chris Lattner | 4470c2b | 2010-09-21 05:43:34 +0000 | [diff] [blame] | 199 | // If to a segment-relative address space, use the default lowering. |
| 200 | if (DstPtrInfo.getAddrSpace() >= 256 || |
| 201 | SrcPtrInfo.getAddrSpace() >= 256) |
| 202 | return SDValue(); |
Duncan Sands | 9851231 | 2010-11-04 21:16:46 +0000 | [diff] [blame] | 203 | |
Hans Wennborg | d683a22 | 2014-03-26 16:30:54 +0000 | [diff] [blame] | 204 | // ESI might be used as a base pointer, in that case we can't simply overwrite |
| 205 | // the register. Fall back to generic code. |
Benjamin Kramer | 8e2637e | 2013-02-13 13:40:35 +0000 | [diff] [blame] | 206 | const X86RegisterInfo *TRI = |
| 207 | static_cast<const X86RegisterInfo *>(DAG.getTarget().getRegisterInfo()); |
Hans Wennborg | d683a22 | 2014-03-26 16:30:54 +0000 | [diff] [blame] | 208 | if (TRI->hasBasePointer(DAG.getMachineFunction()) && |
| 209 | TRI->getBaseRegister() == X86::ESI) |
| 210 | return SDValue(); |
Benjamin Kramer | 8e2637e | 2013-02-13 13:40:35 +0000 | [diff] [blame] | 211 | |
Duncan Sands | 9851231 | 2010-11-04 21:16:46 +0000 | [diff] [blame] | 212 | MVT AVT; |
| 213 | if (Align & 1) |
| 214 | AVT = MVT::i8; |
| 215 | else if (Align & 2) |
| 216 | AVT = MVT::i16; |
| 217 | else if (Align & 4) |
| 218 | // DWORD aligned |
| 219 | AVT = MVT::i32; |
| 220 | else |
| 221 | // QWORD aligned |
| 222 | AVT = Subtarget->is64Bit() ? MVT::i64 : MVT::i32; |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 223 | |
| 224 | unsigned UBytes = AVT.getSizeInBits() / 8; |
| 225 | unsigned CountVal = SizeVal / UBytes; |
| 226 | SDValue Count = DAG.getIntPtrConstant(CountVal); |
| 227 | unsigned BytesLeft = SizeVal % UBytes; |
| 228 | |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame^] | 229 | SDValue InFlag; |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 230 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX : |
| 231 | X86::ECX, |
Hans Wennborg | d683a22 | 2014-03-26 16:30:54 +0000 | [diff] [blame] | 232 | Count, InFlag); |
| 233 | InFlag = Chain.getValue(1); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 234 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI : |
| 235 | X86::EDI, |
Hans Wennborg | d683a22 | 2014-03-26 16:30:54 +0000 | [diff] [blame] | 236 | Dst, InFlag); |
| 237 | InFlag = Chain.getValue(1); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 238 | Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI : |
| 239 | X86::ESI, |
Hans Wennborg | d683a22 | 2014-03-26 16:30:54 +0000 | [diff] [blame] | 240 | Src, InFlag); |
| 241 | InFlag = Chain.getValue(1); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 242 | |
Chris Lattner | 3e5fbd7 | 2010-12-21 02:38:05 +0000 | [diff] [blame] | 243 | SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Glue); |
Hans Wennborg | d683a22 | 2014-03-26 16:30:54 +0000 | [diff] [blame] | 244 | SDValue Ops[] = { Chain, DAG.getValueType(AVT), InFlag }; |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 245 | SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, Ops, |
| 246 | array_lengthof(Ops)); |
| 247 | |
| 248 | SmallVector<SDValue, 4> Results; |
| 249 | Results.push_back(RepMovs); |
| 250 | if (BytesLeft) { |
| 251 | // Handle the last 1 - 7 bytes. |
| 252 | unsigned Offset = SizeVal - BytesLeft; |
| 253 | EVT DstVT = Dst.getValueType(); |
| 254 | EVT SrcVT = Src.getValueType(); |
| 255 | EVT SizeVT = Size.getValueType(); |
| 256 | Results.push_back(DAG.getMemcpy(Chain, dl, |
| 257 | DAG.getNode(ISD::ADD, dl, DstVT, Dst, |
| 258 | DAG.getConstant(Offset, DstVT)), |
| 259 | DAG.getNode(ISD::ADD, dl, SrcVT, Src, |
| 260 | DAG.getConstant(Offset, SrcVT)), |
| 261 | DAG.getConstant(BytesLeft, SizeVT), |
| 262 | Align, isVolatile, AlwaysInline, |
Chris Lattner | 2510de2 | 2010-09-21 05:40:29 +0000 | [diff] [blame] | 263 | DstPtrInfo.getWithOffset(Offset), |
| 264 | SrcPtrInfo.getWithOffset(Offset))); |
Dan Gohman | bb919df | 2010-05-11 17:31:57 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, |
| 268 | &Results[0], Results.size()); |
| 269 | } |