1 //===-- ARMSelectionDAGInfo.cpp - ARM 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 ARMSelectionDAGInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "ARMTargetMachine.h"
15 #include "llvm/CodeGen/SelectionDAG.h"
16 #include "llvm/IR/DerivedTypes.h"
19 #define DEBUG_TYPE "arm-selectiondag-info"
21 ARMSelectionDAGInfo::ARMSelectionDAGInfo(const DataLayout &DL)
22 : TargetSelectionDAGInfo(&DL) {}
24 ARMSelectionDAGInfo::~ARMSelectionDAGInfo() {
28 ARMSelectionDAGInfo::EmitTargetCodeForMemcpy(SelectionDAG &DAG, SDLoc dl,
30 SDValue Dst, SDValue Src,
31 SDValue Size, unsigned Align,
32 bool isVolatile, bool AlwaysInline,
33 MachinePointerInfo DstPtrInfo,
34 MachinePointerInfo SrcPtrInfo) const {
35 const ARMSubtarget &Subtarget = DAG.getTarget().getSubtarget<ARMSubtarget>();
36 // Do repeated 4-byte loads and stores. To be improved.
37 // This requires 4-byte alignment.
40 // This requires the copy size to be a constant, preferably
41 // within a subtarget-specific limit.
42 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
45 uint64_t SizeVal = ConstantSize->getZExtValue();
46 if (!AlwaysInline && SizeVal > Subtarget.getMaxInlineSizeThreshold())
49 unsigned BytesLeft = SizeVal & 3;
50 unsigned NumMemOps = SizeVal >> 2;
51 unsigned EmittedNumMemOps = 0;
55 // Emit a maximum of 4 loads in Thumb1 since we have fewer registers
56 const unsigned MAX_LOADS_IN_LDM = Subtarget.isThumb1Only() ? 4 : 6;
59 uint64_t SrcOff = 0, DstOff = 0;
61 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
62 // same number of stores. The loads and stores will get combined into
64 while (EmittedNumMemOps < NumMemOps) {
66 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
67 Loads[i] = DAG.getLoad(VT, dl, Chain,
68 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
69 DAG.getConstant(SrcOff, MVT::i32)),
70 SrcPtrInfo.getWithOffset(SrcOff), isVolatile,
72 TFOps[i] = Loads[i].getValue(1);
75 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
76 makeArrayRef(TFOps, i));
79 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
80 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
81 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
82 DAG.getConstant(DstOff, MVT::i32)),
83 DstPtrInfo.getWithOffset(DstOff),
84 isVolatile, false, 0);
87 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
88 makeArrayRef(TFOps, i));
90 EmittedNumMemOps += i;
96 // Issue loads / stores for the trailing (1 - 3) bytes.
97 unsigned BytesLeftSave = BytesLeft;
100 if (BytesLeft >= 2) {
108 Loads[i] = DAG.getLoad(VT, dl, Chain,
109 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
110 DAG.getConstant(SrcOff, MVT::i32)),
111 SrcPtrInfo.getWithOffset(SrcOff),
112 false, false, false, 0);
113 TFOps[i] = Loads[i].getValue(1);
118 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
119 makeArrayRef(TFOps, i));
122 BytesLeft = BytesLeftSave;
124 if (BytesLeft >= 2) {
132 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
133 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
134 DAG.getConstant(DstOff, MVT::i32)),
135 DstPtrInfo.getWithOffset(DstOff), false, false, 0);
140 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
141 makeArrayRef(TFOps, i));
144 // Adjust parameters for memset, EABI uses format (ptr, size, value),
145 // GNU library uses (ptr, value, size)
146 // See RTABI section 4.3.4
147 SDValue ARMSelectionDAGInfo::
148 EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc dl,
149 SDValue Chain, SDValue Dst,
150 SDValue Src, SDValue Size,
151 unsigned Align, bool isVolatile,
152 MachinePointerInfo DstPtrInfo) const {
153 const ARMSubtarget &Subtarget = DAG.getTarget().getSubtarget<ARMSubtarget>();
154 // Use default for non-AAPCS (or MachO) subtargets
155 if (!Subtarget.isAAPCS_ABI() || Subtarget.isTargetMachO() ||
156 Subtarget.isTargetWindows())
159 const ARMTargetLowering &TLI =
160 *DAG.getTarget().getSubtarget<ARMSubtarget>().getTargetLowering();
161 TargetLowering::ArgListTy Args;
162 TargetLowering::ArgListEntry Entry;
164 // First argument: data pointer
165 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*DAG.getContext());
168 Args.push_back(Entry);
170 // Second argument: buffer size
173 Entry.isSExt = false;
174 Args.push_back(Entry);
176 // Extend or truncate the argument to be an i32 value for the call.
177 if (Src.getValueType().bitsGT(MVT::i32))
178 Src = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
180 Src = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
182 // Third argument: value to fill
184 Entry.Ty = Type::getInt32Ty(*DAG.getContext());
186 Args.push_back(Entry);
188 // Emit __eabi_memset call
189 TargetLowering::CallLoweringInfo CLI(DAG);
190 CLI.setDebugLoc(dl).setChain(Chain)
191 .setCallee(TLI.getLibcallCallingConv(RTLIB::MEMSET),
192 Type::getVoidTy(*DAG.getContext()),
193 DAG.getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
194 TLI.getPointerTy()), std::move(Args), 0)
197 std::pair<SDValue,SDValue> CallResult = TLI.LowerCallTo(CLI);
198 return CallResult.second;