• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- 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"
15 #include "ARMTargetMachine.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/CodeGen/SelectionDAG.h"
18 using namespace llvm;
19 
ARMSelectionDAGInfo(const TargetMachine & TM)20 ARMSelectionDAGInfo::ARMSelectionDAGInfo(const TargetMachine &TM)
21   : TargetSelectionDAGInfo(TM),
22     Subtarget(&TM.getSubtarget<ARMSubtarget>()) {
23 }
24 
~ARMSelectionDAGInfo()25 ARMSelectionDAGInfo::~ARMSelectionDAGInfo() {
26 }
27 
28 SDValue
EmitTargetCodeForMemcpy(SelectionDAG & DAG,DebugLoc dl,SDValue Chain,SDValue Dst,SDValue Src,SDValue Size,unsigned Align,bool isVolatile,bool AlwaysInline,MachinePointerInfo DstPtrInfo,MachinePointerInfo SrcPtrInfo) const29 ARMSelectionDAGInfo::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
30                                              SDValue Chain,
31                                              SDValue Dst, SDValue Src,
32                                              SDValue Size, unsigned Align,
33                                              bool isVolatile, bool AlwaysInline,
34                                              MachinePointerInfo DstPtrInfo,
35                                           MachinePointerInfo SrcPtrInfo) const {
36   // Do repeated 4-byte loads and stores. To be improved.
37   // This requires 4-byte alignment.
38   if ((Align & 3) != 0)
39     return SDValue();
40   // This requires the copy size to be a constant, preferably
41   // 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)),
69                              SrcPtrInfo.getWithOffset(SrcOff), isVolatile,
70                              false, 0);
71       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)),
81                               DstPtrInfo.getWithOffset(DstOff),
82                               isVolatile, false, 0);
83       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)),
108                            SrcPtrInfo.getWithOffset(SrcOff), false, false, 0);
109     TFOps[i] = Loads[i].getValue(1);
110     ++i;
111     SrcOff += VTSize;
112     BytesLeft -= VTSize;
113   }
114   Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
115 
116   i = 0;
117   BytesLeft = BytesLeftSave;
118   while (BytesLeft) {
119     if (BytesLeft >= 2) {
120       VT = MVT::i16;
121       VTSize = 2;
122     } else {
123       VT = MVT::i8;
124       VTSize = 1;
125     }
126 
127     TFOps[i] = DAG.getStore(Chain, dl, Loads[i],
128                             DAG.getNode(ISD::ADD, dl, MVT::i32, Dst,
129                                         DAG.getConstant(DstOff, MVT::i32)),
130                             DstPtrInfo.getWithOffset(DstOff), false, false, 0);
131     ++i;
132     DstOff += VTSize;
133     BytesLeft -= VTSize;
134   }
135   return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &TFOps[0], i);
136 }
137 
138 // Adjust parameters for memset, EABI uses format (ptr, size, value),
139 // GNU library uses (ptr, value, size)
140 // See RTABI section 4.3.4
141 SDValue ARMSelectionDAGInfo::
EmitTargetCodeForMemset(SelectionDAG & DAG,DebugLoc dl,SDValue Chain,SDValue Dst,SDValue Src,SDValue Size,unsigned Align,bool isVolatile,MachinePointerInfo DstPtrInfo) const142 EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
143                         SDValue Chain, SDValue Dst,
144                         SDValue Src, SDValue Size,
145                         unsigned Align, bool isVolatile,
146                         MachinePointerInfo DstPtrInfo) const {
147   // Use default for non AAPCS subtargets
148   if (!Subtarget->isAAPCS_ABI())
149     return SDValue();
150 
151   const ARMTargetLowering &TLI =
152     *static_cast<const ARMTargetLowering*>(DAG.getTarget().getTargetLowering());
153   TargetLowering::ArgListTy Args;
154   TargetLowering::ArgListEntry Entry;
155 
156   // First argument: data pointer
157   Type *IntPtrTy = TLI.getTargetData()->getIntPtrType(*DAG.getContext());
158   Entry.Node = Dst;
159   Entry.Ty = IntPtrTy;
160   Args.push_back(Entry);
161 
162   // Second argument: buffer size
163   Entry.Node = Size;
164   Entry.Ty = IntPtrTy;
165   Entry.isSExt = false;
166   Args.push_back(Entry);
167 
168   // Extend or truncate the argument to be an i32 value for the call.
169   if (Src.getValueType().bitsGT(MVT::i32))
170     Src = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Src);
171   else
172     Src = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Src);
173 
174   // Third argument: value to fill
175   Entry.Node = Src;
176   Entry.Ty = Type::getInt32Ty(*DAG.getContext());
177   Entry.isSExt = true;
178   Args.push_back(Entry);
179 
180   // Emit __eabi_memset call
181   std::pair<SDValue,SDValue> CallResult =
182     TLI.LowerCallTo(Chain,
183                     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
191                     false, // is return val used
192                     DAG.getExternalSymbol(TLI.getLibcallName(RTLIB::MEMSET),
193                                           TLI.getPointerTy()), // callee
194                     Args, DAG, dl); // arg list, DAG and debug
195 
196   return CallResult.second;
197 }
198