blob: 93add6ee33cf67a7815b9cb50734fa5db6ad0c83 [file] [log] [blame]
Dan Gohman53c5e422010-04-16 23:04:22 +00001//===-- ARMSelectionDAGInfo.cpp - ARM 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 ARMSelectionDAGInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "arm-selectiondag-info"
Dan Gohmanff7a5622010-05-11 17:31:57 +000015#include "ARMTargetMachine.h"
Renato Golin1ec11fb2011-05-22 21:41:23 +000016#include "llvm/CodeGen/SelectionDAG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/DerivedTypes.h"
Dan Gohman53c5e422010-04-16 23:04:22 +000018using namespace llvm;
19
Dan Gohmanff7a5622010-05-11 17:31:57 +000020ARMSelectionDAGInfo::ARMSelectionDAGInfo(const TargetMachine &TM)
21 : TargetSelectionDAGInfo(TM),
22 Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
Dan Gohman53c5e422010-04-16 23:04:22 +000023}
24
25ARMSelectionDAGInfo::~ARMSelectionDAGInfo() {
26}
Dan Gohmanff7a5622010-05-11 17:31:57 +000027
28SDValue
Andrew Trickac6d9be2013-05-25 02:42:55 +000029ARMSelectionDAGInfo::EmitTargetCodeForMemcpy(SelectionDAG &DAG, SDLoc dl,
Dan Gohmanff7a5622010-05-11 17:31:57 +000030 SDValue Chain,
31 SDValue Dst, SDValue Src,
32 SDValue Size, unsigned Align,
33 bool isVolatile, bool AlwaysInline,
Chris Lattnere72f2022010-09-21 05:40:29 +000034 MachinePointerInfo DstPtrInfo,
35 MachinePointerInfo SrcPtrInfo) const {
Dan Gohmanff7a5622010-05-11 17:31:57 +000036 // Do repeated 4-byte loads and stores. To be improved.
37 // This requires 4-byte alignment.
38 if ((Align & 3) != 0)
39 return SDValue();
Chris Lattner7a2bdde2011-04-15 05:18:47 +000040 // This requires the copy size to be a constant, preferably
Dan Gohmanff7a5622010-05-11 17:31:57 +000041 // within a subtarget-specific limit.
42 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
43 if (!ConstantSize)
44 return SDValue();
45 uint64_t SizeVal = ConstantSize->getZExtValue();
46 if (!AlwaysInline && SizeVal > Subtarget->getMaxInlineSizeThreshold())
47 return SDValue();
48
49 unsigned BytesLeft = SizeVal & 3;
50 unsigned NumMemOps = SizeVal >> 2;
51 unsigned EmittedNumMemOps = 0;
52 EVT VT = MVT::i32;
53 unsigned VTSize = 4;
54 unsigned i = 0;
55 const unsigned MAX_LOADS_IN_LDM = 6;
56 SDValue TFOps[MAX_LOADS_IN_LDM];
57 SDValue Loads[MAX_LOADS_IN_LDM];
58 uint64_t SrcOff = 0, DstOff = 0;
59
60 // Emit up to MAX_LOADS_IN_LDM loads, then a TokenFactor barrier, then the
61 // same number of stores. The loads and stores will get combined into
62 // ldm/stm later on.
63 while (EmittedNumMemOps < NumMemOps) {
64 for (i = 0;
65 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
66 Loads[i] = DAG.getLoad(VT, dl, Chain,
67 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
68 DAG.getConstant(SrcOff, MVT::i32)),
Chris Lattnere72f2022010-09-21 05:40:29 +000069 SrcPtrInfo.getWithOffset(SrcOff), isVolatile,
Pete Cooperd752e0f2011-11-08 18:42:53 +000070 false, false, 0);
Dan Gohmanff7a5622010-05-11 17:31:57 +000071 TFOps[i] = Loads[i].getValue(1);
72 SrcOff += VTSize;
73 }
74 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
75
76 for (i = 0;
77 i < MAX_LOADS_IN_LDM && EmittedNumMemOps + i < NumMemOps; ++i) {
78 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
79 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
80 DAG.getConstant(DstOff, MVT::i32)),
Chris Lattnere72f2022010-09-21 05:40:29 +000081 DstPtrInfo.getWithOffset(DstOff),
82 isVolatile, false, 0);
Dan Gohmanff7a5622010-05-11 17:31:57 +000083 DstOff += VTSize;
84 }
85 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
86
87 EmittedNumMemOps += i;
88 }
89
90 if (BytesLeft == 0)
91 return Chain;
92
93 // Issue loads / stores for the trailing (1 - 3) bytes.
94 unsigned BytesLeftSave = BytesLeft;
95 i = 0;
96 while (BytesLeft) {
97 if (BytesLeft >= 2) {
98 VT = MVT::i16;
99 VTSize = 2;
100 } else {
101 VT = MVT::i8;
102 VTSize = 1;
103 }
104
105 Loads[i] = DAG.getLoad(VT, dl, Chain,
106 DAG.getNode(ISD::ADD, dl, MVT::i32, Src,
107 DAG.getConstant(SrcOff, MVT::i32)),
Pete Cooperd752e0f2011-11-08 18:42:53 +0000108 SrcPtrInfo.getWithOffset(SrcOff),
109 false, false, false, 0);
Dan Gohmanff7a5622010-05-11 17:31:57 +0000110 TFOps[i] = Loads[i].getValue(1);
111 ++i;
112 SrcOff += VTSize;
113 BytesLeft -= VTSize;
114 }
115 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
116
117 i = 0;
118 BytesLeft = BytesLeftSave;
119 while (BytesLeft) {
120 if (BytesLeft >= 2) {
121 VT = MVT::i16;
122 VTSize = 2;
123 } else {
124 VT = MVT::i8;
125 VTSize = 1;
126 }
127
128 TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
129 DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
130 DAG.getConstant(DstOff, MVT::i32)),
Chris Lattnere72f2022010-09-21 05:40:29 +0000131 DstPtrInfo.getWithOffset(DstOff), false, false, 0);
Dan Gohmanff7a5622010-05-11 17:31:57 +0000132 ++i;
133 DstOff += VTSize;
134 BytesLeft -= VTSize;
135 }
136 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
137}
Renato Golin1ec11fb2011-05-22 21:41:23 +0000138
139// Adjust parameters for memset, EABI uses format (ptr, size, value),
140// GNU library uses (ptr, value, size)
141// See RTABI section 4.3.4
Jim Grosbachb04546f2011-09-13 20:30:37 +0000142SDValue ARMSelectionDAGInfo::
Andrew Trickac6d9be2013-05-25 02:42:55 +0000143EmitTargetCodeForMemset(SelectionDAG &DAG, SDLoc dl,
Jim Grosbachb04546f2011-09-13 20:30:37 +0000144 SDValue Chain, SDValue Dst,
145 SDValue Src, SDValue Size,
146 unsigned Align, bool isVolatile,
147 MachinePointerInfo DstPtrInfo) const {
Evan Cheng07043272012-02-21 20:46:00 +0000148 // Use default for non AAPCS (or Darwin) subtargets
149 if (!Subtarget->isAAPCS_ABI() || Subtarget->isTargetDarwin())
Renato Golin1ec11fb2011-05-22 21:41:23 +0000150 return SDValue();
151
152 const ARMTargetLowering &TLI =
153 *static_cast<const ARMTargetLowering*>(DAG.getTarget().getTargetLowering());
154 TargetLowering::ArgListTy Args;
155 TargetLowering::ArgListEntry Entry;
156
157 // First argument: data pointer
Chandler Carruthece6c6b2012-11-01 08:07:29 +0000158 Type *IntPtrTy = TLI.getDataLayout()->getIntPtrType(*DAG.getContext());
Renato Golin1ec11fb2011-05-22 21:41:23 +0000159 Entry.Node = Dst;
160 Entry.Ty = IntPtrTy;
161 Args.push_back(Entry);
162
163 // Second argument: buffer size
164 Entry.Node = Size;
165 Entry.Ty = IntPtrTy;
166 Entry.isSExt = false;
167 Args.push_back(Entry);
168
169 // Extend or truncate the argument to be an i32 value for the call.
170 if (Src.getValueType().bitsGT(MVT::i32))
171 Src = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
172 else
173 Src = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
174
175 // Third argument: value to fill
176 Entry.Node = Src;
177 Entry.Ty = Type::getInt32Ty(*DAG.getContext());
178 Entry.isSExt = true;
179 Args.push_back(Entry);
180
181 // Emit __eabi_memset call
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000182 TargetLowering::CallLoweringInfo CLI(Chain,
Renato Golin1ec11fb2011-05-22 21:41:23 +0000183 Type::getVoidTy(*DAG.getContext()), // return type
184 false, // return sign ext
185 false, // return zero ext
186 false, // is var arg
187 false, // is in regs
188 0, // number of fixed arguments
189 TLI.getLibcallCallingConv(RTLIB::MEMSET), // call conv
190 false, // is tail call
Evan Cheng4bfcd4a2012-02-28 18:51:51 +0000191 false, // does not return
Renato Golin1ec11fb2011-05-22 21:41:23 +0000192 false, // is return val used
193 DAG.getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
194 TLI.getPointerTy()), // callee
Justin Holewinskid2ea0e12012-05-25 16:35:28 +0000195 Args, DAG, dl);
196 std::pair<SDValue,SDValue> CallResult =
197 TLI.LowerCallTo(CLI);
Renato Golin1ec11fb2011-05-22 21:41:23 +0000198
199 return CallResult.second;
200}