blob: f191591783641034644f5fc80af0b897e04590cb [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000022#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000024#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000026#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000027#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000028#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000029#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000034#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000035#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000036using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000051class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
Chris Lattner6831a812006-02-13 09:18:02 +000055 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +000061
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000068 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000070 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 };
Chris Lattner6831a812006-02-13 09:18:02 +000072
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000076 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner03c85462005-01-15 05:21:40 +000083 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000087
Chris Lattnerc7029802006-03-18 01:44:44 +000088 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000089 /// which operands are the expanded version of the input. This allows
Chris Lattnerc7029802006-03-18 01:44:44 +000090 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000092
Chris Lattnerc7029802006-03-18 01:44:44 +000093 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000094 /// which operands are the split version of the input. This allows us
Chris Lattnerc7029802006-03-18 01:44:44 +000095 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +000097
Dan Gohman7f321562007-06-25 16:23:39 +000098 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000100 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000102
Mon P Wang0c397192008-10-30 08:01:45 +0000103 /// WidenNodes - For nodes that need to be widen from one vector type to
104 /// another, this contains the mapping of ones we have already widen. This
105 /// allows us to avoid widening more than once.
106 std::map<SDValue, SDValue> WidenNodes;
107
Dan Gohman475871a2008-07-27 21:46:04 +0000108 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113 }
Dan Gohman475871a2008-07-27 21:46:04 +0000114 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Chris Lattner69a889e2005-12-20 00:53:54 +0000117 // If someone requests legalization of the new node, return itself.
118 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000119 }
Mon P Wang0c397192008-10-30 08:01:45 +0000120 void AddWidenOperand(SDValue From, SDValue To) {
121 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
122 assert(isNew && "Got into the map somehow?");
123 // If someone requests legalization of the new node, return itself.
124 LegalizedNodes.insert(std::make_pair(To, To));
125 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000126
Chris Lattner3e928bb2005-01-07 07:47:09 +0000127public:
Dan Gohman1002c022008-07-07 18:00:37 +0000128 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000129
Chris Lattner3e928bb2005-01-07 07:47:09 +0000130 /// getTypeAction - Return how we should legalize values of this type, either
131 /// it is already legal or we need to expand it into multiple registers of
132 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000133 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000134 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000135 }
136
137 /// isTypeLegal - Return true if this type is legal on this target.
138 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000139 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000140 return getTypeAction(VT) == Legal;
141 }
142
Chris Lattner3e928bb2005-01-07 07:47:09 +0000143 void LegalizeDAG();
144
Chris Lattner456a93a2006-01-28 07:39:30 +0000145private:
Dan Gohman7f321562007-06-25 16:23:39 +0000146 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000147 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000148 void HandleOp(SDValue Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000149
150 /// LegalizeOp - We know that the specified value has a legal type.
151 /// Recursively ensure that the operands have legal types, then return the
152 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000153 SDValue LegalizeOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000154
Dan Gohman82669522007-10-11 23:57:53 +0000155 /// UnrollVectorOp - We know that the given vector has a legal type, however
156 /// the operation it performs is not legal and is an operation that we have
157 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
158 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000159 SDValue UnrollVectorOp(SDValue O);
Nate Begeman68679912008-04-25 18:07:40 +0000160
161 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
162 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
163 /// is necessary to spill the vector being inserted into to memory, perform
164 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000165 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
166 SDValue Idx);
Dan Gohman82669522007-10-11 23:57:53 +0000167
Chris Lattnerc7029802006-03-18 01:44:44 +0000168 /// PromoteOp - Given an operation that produces a value in an invalid type,
169 /// promote it to compute the value into a larger type. The produced value
170 /// will have the correct bits for the low portion of the register, but no
171 /// guarantee is made about the top bits: it may be zero, sign-extended, or
172 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000173 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000174
Dan Gohman475871a2008-07-27 21:46:04 +0000175 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000176 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman929d3eb2008-10-01 15:07:49 +0000177 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattnerc7029802006-03-18 01:44:44 +0000178 /// the ExpandedNodes map is filled in for any results that are expanded, and
179 /// the Lo/Hi values are returned. This applies to integer types and Vector
180 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000181 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000182
Mon P Wang0c397192008-10-30 08:01:45 +0000183 /// WidenVectorOp - Widen a vector operation in order to do the computation
184 /// in a wider type given to a wider type given by WidenVT (e.g., v3i32 to
185 /// v4i32). The produced value will have the correct value for the existing
186 /// elements but no guarantee is made about the new elements at the end of
187 /// the vector: it may be zero, sign-extended, or garbage. This is useful
188 /// when we have instruction operating on an illegal vector type and we want
189 /// to widen it to do the computation on a legal wider vector type.
190 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
191
Dan Gohman7f321562007-06-25 16:23:39 +0000192 /// SplitVectorOp - Given an operand of vector type, break it down into
193 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000194 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000195
Dan Gohman89b20c02007-06-27 14:06:22 +0000196 /// ScalarizeVectorOp - Given an operand of single-element vector type
197 /// (e.g. v1f32), convert it into the equivalent operation that returns a
198 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000199 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000200
Mon P Wang0c397192008-10-30 08:01:45 +0000201 /// Useful 16 element vector used to pass operands for widening
202 typedef SmallVector<SDValue, 16> SDValueVector;
203
204 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
205 /// the LdChain contains a single load and false if it contains a token
206 /// factor for multiple loads. It takes
207 /// Result: location to return the result
208 /// LdChain: location to return the load chain
209 /// Op: load operation to widen
210 /// NVT: widen vector result type we want for the load
211 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
212 SDValue Op, MVT NVT);
213
214 /// Helper genWidenVectorLoads - Helper function to generate a set of
215 /// loads to load a vector with a resulting wider type. It takes
216 /// LdChain: list of chains for the load we have generated
217 /// Chain: incoming chain for the ld vector
218 /// BasePtr: base pointer to load from
219 /// SV: memory disambiguation source value
220 /// SVOffset: memory disambiugation offset
221 /// Alignment: alignment of the memory
222 /// isVolatile: volatile load
223 /// LdWidth: width of memory that we want to load
224 /// ResType: the wider result result type for the resulting loaded vector
225 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
226 SDValue BasePtr, const Value *SV,
227 int SVOffset, unsigned Alignment,
228 bool isVolatile, unsigned LdWidth,
229 MVT ResType);
230
231 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
232 /// location. It takes
233 /// ST: store node that we want to replace
234 /// Chain: incoming store chain
235 /// BasePtr: base address of where we want to store into
236 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
237 SDValue BasePtr);
238
239 /// Helper genWidenVectorStores - Helper function to generate a set of
240 /// stores to store a widen vector into non widen memory
241 // It takes
242 // StChain: list of chains for the stores we have generated
243 // Chain: incoming chain for the ld vector
244 // BasePtr: base pointer to load from
245 // SV: memory disambiguation source value
246 // SVOffset: memory disambiugation offset
247 // Alignment: alignment of the memory
248 // isVolatile: volatile lod
249 // ValOp: value to store
250 // StWidth: width of memory that we want to store
251 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
252 SDValue BasePtr, const Value *SV,
253 int SVOffset, unsigned Alignment,
254 bool isVolatile, SDValue ValOp,
255 unsigned StWidth);
256
Duncan Sandsd038e042008-07-21 10:20:31 +0000257 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000258 /// specified mask and type. Targets can specify exactly which masks they
259 /// support and the code generator is tasked with not creating illegal masks.
260 ///
261 /// Note that this will also return true for shuffles that are promoted to a
262 /// different type.
263 ///
264 /// If this is a legal shuffle, this method returns the (possibly promoted)
265 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000266 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000267
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000268 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000269 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000270
Dan Gohman475871a2008-07-27 21:46:04 +0000271 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng7f042682008-10-15 02:05:31 +0000272 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
273 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
274 LegalizeSetCCOperands(LHS, RHS, CC);
275 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
276 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000277
Dan Gohman475871a2008-07-27 21:46:04 +0000278 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
279 SDValue &Hi);
280 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000281
Dan Gohman475871a2008-07-27 21:46:04 +0000282 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
283 SDValue ExpandBUILD_VECTOR(SDNode *Node);
284 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman7f8613e2008-08-14 20:04:46 +0000285 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000286 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
287 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
288 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000289
Dan Gohman475871a2008-07-27 21:46:04 +0000290 SDValue ExpandBSWAP(SDValue Op);
291 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
292 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
293 SDValue &Lo, SDValue &Hi);
294 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
295 SDValue &Lo, SDValue &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000296
Dan Gohman475871a2008-07-27 21:46:04 +0000297 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
298 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000299};
300}
301
Chris Lattner4352cc92006-04-04 17:23:26 +0000302/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
303/// specified mask and type. Targets can specify exactly which masks they
304/// support and the code generator is tasked with not creating illegal masks.
305///
306/// Note that this will also return true for shuffles that are promoted to a
307/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000308SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000309 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
310 default: return 0;
311 case TargetLowering::Legal:
312 case TargetLowering::Custom:
313 break;
314 case TargetLowering::Promote: {
315 // If this is promoted to a different type, convert the shuffle mask and
316 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000317 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000318 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000319
320 // If we changed # elements, change the shuffle mask.
321 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000322 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000323 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
324 if (NumEltsGrowth > 1) {
325 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000326 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000327 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000328 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000329 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
330 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000331 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000332 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000333 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000334 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000335 }
336 }
337 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000338 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000339 }
340 VT = NVT;
341 break;
342 }
343 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000344 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000345}
346
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000347SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
348 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
349 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000350 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000351 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352}
353
Chris Lattner3e928bb2005-01-07 07:47:09 +0000354void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000355 LastCALLSEQ_END = DAG.getEntryNode();
356 IsLegalizingCall = false;
357
Chris Lattnerab510a72005-10-02 17:49:46 +0000358 // The legalize process is inherently a bottom-up recursive process (users
359 // legalize their uses before themselves). Given infinite stack space, we
360 // could just start legalizing on the root and traverse the whole graph. In
361 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000362 // blocks. To avoid this problem, compute an ordering of the nodes where each
363 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000364 DAG.AssignTopologicalOrder();
365 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
366 E = prior(DAG.allnodes_end()); I != next(E); ++I)
367 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000368
369 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000370 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000371 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
372 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000373
374 ExpandedNodes.clear();
375 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000376 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000377 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000378 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000379 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000380
381 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000382 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000383}
384
Chris Lattner6831a812006-02-13 09:18:02 +0000385
386/// FindCallEndFromCallStart - Given a chained node that is part of a call
387/// sequence, find the CALLSEQ_END node that terminates the call sequence.
388static SDNode *FindCallEndFromCallStart(SDNode *Node) {
389 if (Node->getOpcode() == ISD::CALLSEQ_END)
390 return Node;
391 if (Node->use_empty())
392 return 0; // No CallSeqEnd
393
394 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000395 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000396 if (TheChain.getValueType() != MVT::Other) {
397 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000398 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000399 if (TheChain.getValueType() != MVT::Other) {
400 // Otherwise, hunt for it.
401 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
402 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000403 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000404 break;
405 }
406
407 // Otherwise, we walked into a node without a chain.
408 if (TheChain.getValueType() != MVT::Other)
409 return 0;
410 }
411 }
412
413 for (SDNode::use_iterator UI = Node->use_begin(),
414 E = Node->use_end(); UI != E; ++UI) {
415
416 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000417 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000418 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
419 if (User->getOperand(i) == TheChain)
420 if (SDNode *Result = FindCallEndFromCallStart(User))
421 return Result;
422 }
423 return 0;
424}
425
426/// FindCallStartFromCallEnd - Given a chained node that is part of a call
427/// sequence, find the CALLSEQ_START node that initiates the call sequence.
428static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
429 assert(Node && "Didn't find callseq_start for a call??");
430 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
431
432 assert(Node->getOperand(0).getValueType() == MVT::Other &&
433 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000434 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000435}
436
437/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
438/// see if any uses can reach Dest. If no dest operands can get to dest,
439/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000440///
441/// Keep track of the nodes we fine that actually do lead to Dest in
442/// NodesLeadingTo. This avoids retraversing them exponential number of times.
443///
444bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000445 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000446 if (N == Dest) return true; // N certainly leads to Dest :)
447
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000448 // If we've already processed this node and it does lead to Dest, there is no
449 // need to reprocess it.
450 if (NodesLeadingTo.count(N)) return true;
451
Chris Lattner6831a812006-02-13 09:18:02 +0000452 // If the first result of this node has been already legalized, then it cannot
453 // reach N.
454 switch (getTypeAction(N->getValueType(0))) {
455 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000456 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000457 break;
458 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000459 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000460 break;
461 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000462 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000463 break;
464 }
465
466 // Okay, this node has not already been legalized. Check and legalize all
467 // operands. If none lead to Dest, then we can legalize this node.
468 bool OperandsLeadToDest = false;
469 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
470 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000471 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000472
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000473 if (OperandsLeadToDest) {
474 NodesLeadingTo.insert(N);
475 return true;
476 }
Chris Lattner6831a812006-02-13 09:18:02 +0000477
478 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000479 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000480 return false;
481}
482
Mon P Wang0c397192008-10-30 08:01:45 +0000483/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000484/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000485void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000486 MVT VT = Op.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000487 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000488 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000489 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000490 case Promote:
491 if (!VT.isVector()) {
492 (void)PromoteOp(Op);
493 break;
494 }
495 else {
496 // See if we can widen otherwise use Expand to either scalarize or split
497 MVT WidenVT = TLI.getWidenVectorType(VT);
498 if (WidenVT != MVT::Other) {
499 (void) WidenVectorOp(Op, WidenVT);
500 break;
501 }
502 // else fall thru to expand since we can't widen the vector
503 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000504 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000505 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000506 // If this is an illegal scalar, expand it into its two component
507 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000508 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000509 if (Op.getOpcode() == ISD::TargetConstant)
510 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000511 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000512 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000513 // If this is an illegal single element vector, convert it to a
514 // scalar operation.
515 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000516 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000517 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000518 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000519 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000520 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000521 }
522 break;
523 }
524}
Chris Lattner6831a812006-02-13 09:18:02 +0000525
Evan Cheng9f877882006-12-13 20:57:08 +0000526/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
527/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000528static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000529 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000530 bool Extend = false;
531
532 // If a FP immediate is precise when represented as a float and if the
533 // target can do an extending load from float to double, we put it into
534 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000535 // double. This shrinks FP constants and canonicalizes them for targets where
536 // an FP extending load is the same cost as a normal load (such as on the x87
537 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000538 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000539 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000540 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000541 if (VT!=MVT::f64 && VT!=MVT::f32)
542 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000543 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000544 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000545 }
546
Duncan Sands83ec4b62008-06-06 12:08:01 +0000547 MVT OrigVT = VT;
548 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000549 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000550 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000551 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
552 // Only do this if the target has a native EXTLOAD instruction from
553 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000554 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000555 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000556 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000557 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
558 VT = SVT;
559 Extend = true;
560 }
Evan Cheng00495212006-12-12 21:32:44 +0000561 }
562
Dan Gohman475871a2008-07-27 21:46:04 +0000563 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000564 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000565 if (Extend)
566 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000567 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000568 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000569 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000570 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000571}
572
Chris Lattner6831a812006-02-13 09:18:02 +0000573
Evan Cheng912095b2007-01-04 21:56:39 +0000574/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
575/// operations.
576static
Dan Gohman475871a2008-07-27 21:46:04 +0000577SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
578 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000579 MVT VT = Node->getValueType(0);
580 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000581 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
582 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000583 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000584
Evan Cheng912095b2007-01-04 21:56:39 +0000585 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000586 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000587 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
588 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000589 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000590 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000591 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000592 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000593 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000594 if (SizeDiff > 0) {
595 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
596 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
597 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000598 } else if (SizeDiff < 0) {
599 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
600 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
601 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
602 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000603
604 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000605 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000606 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
607 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
608 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000609 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000610 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
611
612 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000613 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
614 return Result;
615}
616
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000617/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
618static
Dan Gohman475871a2008-07-27 21:46:04 +0000619SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
620 TargetLowering &TLI) {
621 SDValue Chain = ST->getChain();
622 SDValue Ptr = ST->getBasePtr();
623 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000624 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000625 int Alignment = ST->getAlignment();
626 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000627 if (ST->getMemoryVT().isFloatingPoint() ||
628 ST->getMemoryVT().isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000629 // Expand to a bitconvert of the value to the integer type of the
630 // same size, then a (misaligned) int store.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000631 MVT intVT;
632 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000633 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000634 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000635 intVT = MVT::i64;
636 else if (VT==MVT::f32)
637 intVT = MVT::i32;
638 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000639 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000640
Dan Gohman475871a2008-07-27 21:46:04 +0000641 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000642 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
643 SVOffset, ST->isVolatile(), Alignment);
644 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000645 assert(ST->getMemoryVT().isInteger() &&
646 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000647 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000648 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000649 MVT NewStoredVT =
650 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
651 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000652 int IncrementSize = NumBits / 8;
653
654 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000655 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
656 SDValue Lo = Val;
657 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000658
659 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000660 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000661 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
662 ST->getSrcValue(), SVOffset, NewStoredVT,
663 ST->isVolatile(), Alignment);
664 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
665 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000666 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000667 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
668 ST->getSrcValue(), SVOffset + IncrementSize,
669 NewStoredVT, ST->isVolatile(), Alignment);
670
671 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
672}
673
674/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
675static
Dan Gohman475871a2008-07-27 21:46:04 +0000676SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
677 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000678 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000679 SDValue Chain = LD->getChain();
680 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000681 MVT VT = LD->getValueType(0);
682 MVT LoadedVT = LD->getMemoryVT();
683 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000684 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000685 // then bitconvert to floating point or vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000686 MVT intVT;
687 if (LoadedVT.is128BitVector() ||
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000688 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000689 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000690 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000691 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000692 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000693 intVT = MVT::i32;
694 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000695 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000696
Dan Gohman475871a2008-07-27 21:46:04 +0000697 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000698 SVOffset, LD->isVolatile(),
699 LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +0000700 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000701 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000702 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
703
Dan Gohman475871a2008-07-27 21:46:04 +0000704 SDValue Ops[] = { Result, Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000705 return DAG.getMergeValues(Ops, 2);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000706 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000707 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000708 "Unaligned load of unsupported type.");
709
Dale Johannesen8155d642008-02-27 22:36:00 +0000710 // Compute the new VT that is half the size of the old one. This is an
711 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000712 unsigned NumBits = LoadedVT.getSizeInBits();
713 MVT NewLoadedVT;
714 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000715 NumBits >>= 1;
716
717 unsigned Alignment = LD->getAlignment();
718 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000719 ISD::LoadExtType HiExtType = LD->getExtensionType();
720
721 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
722 if (HiExtType == ISD::NON_EXTLOAD)
723 HiExtType = ISD::ZEXTLOAD;
724
725 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000726 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000727 if (TLI.isLittleEndian()) {
728 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
729 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
730 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
731 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
732 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
733 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000734 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000735 } else {
736 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
737 NewLoadedVT,LD->isVolatile(), Alignment);
738 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
739 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
740 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
741 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000742 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000743 }
744
745 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000746 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
747 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000748 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
749
Dan Gohman475871a2008-07-27 21:46:04 +0000750 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000751 Hi.getValue(1));
752
Dan Gohman475871a2008-07-27 21:46:04 +0000753 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000754 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000755}
Evan Cheng912095b2007-01-04 21:56:39 +0000756
Dan Gohman82669522007-10-11 23:57:53 +0000757/// UnrollVectorOp - We know that the given vector has a legal type, however
758/// the operation it performs is not legal and is an operation that we have
759/// no way of lowering. "Unroll" the vector, splitting out the scalars and
760/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000761SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000762 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000763 assert(isTypeLegal(VT) &&
764 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000765 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000766 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000767 unsigned NE = VT.getVectorNumElements();
768 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000769
Dan Gohman475871a2008-07-27 21:46:04 +0000770 SmallVector<SDValue, 8> Scalars;
771 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000772 for (unsigned i = 0; i != NE; ++i) {
773 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000774 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000775 MVT OperandVT = Operand.getValueType();
776 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000777 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000778 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000779 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
780 OperandEltVT,
781 Operand,
782 DAG.getConstant(i, MVT::i32));
783 } else {
784 // A scalar operand; just use it as is.
785 Operands[j] = Operand;
786 }
787 }
788 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
789 &Operands[0], Operands.size()));
790 }
791
792 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
793}
794
Duncan Sands007f9842008-01-10 10:28:30 +0000795/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000796static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000797 RTLIB::Libcall Call_F32,
798 RTLIB::Libcall Call_F64,
799 RTLIB::Libcall Call_F80,
800 RTLIB::Libcall Call_PPCF128) {
801 return
802 VT == MVT::f32 ? Call_F32 :
803 VT == MVT::f64 ? Call_F64 :
804 VT == MVT::f80 ? Call_F80 :
805 VT == MVT::ppcf128 ? Call_PPCF128 :
806 RTLIB::UNKNOWN_LIBCALL;
807}
808
Nate Begeman68679912008-04-25 18:07:40 +0000809/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
810/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
811/// is necessary to spill the vector being inserted into to memory, perform
812/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000813SDValue SelectionDAGLegalize::
814PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
815 SDValue Tmp1 = Vec;
816 SDValue Tmp2 = Val;
817 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000818
819 // If the target doesn't support this, we have to spill the input vector
820 // to a temporary stack slot, update the element, then reload it. This is
821 // badness. We could also load the value into a vector register (either
822 // with a "move to register" or "extload into register" instruction, then
823 // permute it into place, if the idx is a constant and if the idx is
824 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000825 MVT VT = Tmp1.getValueType();
826 MVT EltVT = VT.getVectorElementType();
827 MVT IdxVT = Tmp3.getValueType();
828 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000829 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000830
Gabor Greifba36cb52008-08-28 21:40:38 +0000831 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000832
833 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000834 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000835 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000836
837 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000838 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000839 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
840 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000841 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000842 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000843 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000844 // Store the scalar value.
845 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000846 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000847 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000848 return DAG.getLoad(VT, Ch, StackPtr,
849 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000850}
851
Dan Gohmana3466152007-07-13 20:14:11 +0000852/// LegalizeOp - We know that the specified value has a legal type, and
853/// that its operands are legal. Now ensure that the operation itself
854/// is legal, recursively ensuring that the operands' operations remain
855/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000856SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000857 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
858 return Op;
859
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000860 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000861 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000862 SDNode *Node = Op.getNode();
Chris Lattnere3304a32005-01-08 20:35:13 +0000863
Chris Lattner3e928bb2005-01-07 07:47:09 +0000864 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000865 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000866 if (Node->getNumValues() > 1) {
867 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000868 if (getTypeAction(Node->getValueType(i)) != Legal) {
869 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000870 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000871 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000872 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000873 }
874 }
875
Chris Lattner45982da2005-05-12 16:53:42 +0000876 // Note that LegalizeOp may be reentered even from single-use nodes, which
877 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000878 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000879 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000880
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
882 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000883 bool isCustom = false;
884
Chris Lattner3e928bb2005-01-07 07:47:09 +0000885 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000886 case ISD::FrameIndex:
887 case ISD::EntryToken:
888 case ISD::Register:
889 case ISD::BasicBlock:
890 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000891 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000892 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000893 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000894 case ISD::TargetConstantPool:
895 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000896 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000897 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +0000898 case ISD::VALUETYPE:
899 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000900 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000901 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +0000902 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +0000903 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000904 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000905 "This must be legal!");
906 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000907 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000908 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
909 // If this is a target node, legalize it by legalizing the operands then
910 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +0000911 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000912 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000913 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000914
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000915 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000916
917 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
918 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000919 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000920 }
921 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000922#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000923 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000924#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000925 assert(0 && "Do not know how to legalize this operator!");
926 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000927 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000928 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000929 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000930 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000931 case ISD::ConstantPool:
932 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000933 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
934 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000935 case TargetLowering::Custom:
936 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000937 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +0000938 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000939 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000940 break;
941 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000942 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000943 case ISD::FRAMEADDR:
944 case ISD::RETURNADDR:
945 // The only option for these nodes is to custom lower them. If the target
946 // does not custom lower them, then return zero.
947 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000948 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +0000949 Result = Tmp1;
950 else
951 Result = DAG.getConstant(0, TLI.getPointerTy());
952 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000953 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000954 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000955 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
956 default: assert(0 && "This action is not supported yet!");
957 case TargetLowering::Custom:
958 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000959 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000960 // Fall Thru
961 case TargetLowering::Legal:
962 Result = DAG.getConstant(0, VT);
963 break;
964 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000965 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000966 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000967 case ISD::EXCEPTIONADDR: {
968 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000969 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000970 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
971 default: assert(0 && "This action is not supported yet!");
972 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000973 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000974 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000975 }
976 break;
977 case TargetLowering::Custom:
978 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000979 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000980 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000981 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000982 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000983 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000984 break;
985 }
986 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000987 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000988 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000989
Gabor Greifba36cb52008-08-28 21:40:38 +0000990 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000991 "Cannot return more than two values!");
992
993 // Since we produced two values, make sure to remember that we
994 // legalized both of them.
995 Tmp1 = LegalizeOp(Result);
996 Tmp2 = LegalizeOp(Result.getValue(1));
997 AddLegalizedOperand(Op.getValue(0), Tmp1);
998 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000999 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001000 case ISD::EHSELECTION: {
1001 Tmp1 = LegalizeOp(Node->getOperand(0));
1002 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001003 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001004 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1005 default: assert(0 && "This action is not supported yet!");
1006 case TargetLowering::Expand: {
1007 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001008 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001009 }
1010 break;
1011 case TargetLowering::Custom:
1012 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001013 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001014 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001015 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001016 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001017 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001018 break;
1019 }
1020 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001021 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001022 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001023
Gabor Greifba36cb52008-08-28 21:40:38 +00001024 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001025 "Cannot return more than two values!");
1026
1027 // Since we produced two values, make sure to remember that we
1028 // legalized both of them.
1029 Tmp1 = LegalizeOp(Result);
1030 Tmp2 = LegalizeOp(Result.getValue(1));
1031 AddLegalizedOperand(Op.getValue(0), Tmp1);
1032 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001033 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001034 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001035 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001036 // The only "good" option for this node is to custom lower it.
1037 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1038 default: assert(0 && "This action is not supported at all!");
1039 case TargetLowering::Custom:
1040 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001041 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001042 // Fall Thru
1043 case TargetLowering::Legal:
1044 // Target does not know, how to lower this, lower to noop
1045 Result = LegalizeOp(Node->getOperand(0));
1046 break;
1047 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001048 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001049 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001050 case ISD::AssertSext:
1051 case ISD::AssertZext:
1052 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001053 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001054 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001055 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001056 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001057 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001058 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001059 case ISD::CopyFromReg:
1060 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001061 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001062 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001063 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001064 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001065 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001066 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001067 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001068 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1069 } else {
1070 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1071 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001072 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1073 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001074 // Since CopyFromReg produces two values, make sure to remember that we
1075 // legalized both of them.
1076 AddLegalizedOperand(Op.getValue(0), Result);
1077 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001078 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001079 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001080 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001081 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001082 default: assert(0 && "This action is not supported yet!");
1083 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001084 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001085 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001086 else if (VT.isFloatingPoint())
1087 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001088 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001089 else
1090 assert(0 && "Unknown value type!");
1091 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001092 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001093 break;
1094 }
1095 break;
1096 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001097
Chris Lattner48b61a72006-03-28 00:40:33 +00001098 case ISD::INTRINSIC_W_CHAIN:
1099 case ISD::INTRINSIC_WO_CHAIN:
1100 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001101 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001102 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1103 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001104 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001105
1106 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001107 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001108 TargetLowering::Custom) {
1109 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001110 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001111 }
1112
Gabor Greifba36cb52008-08-28 21:40:38 +00001113 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001114
1115 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001116 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001117 "Cannot return more than two values!");
1118
1119 // Since loads produce two values, make sure to remember that we
1120 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001121 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1122 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001123 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001124 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001125
Dan Gohman7f460202008-06-30 20:59:49 +00001126 case ISD::DBG_STOPPOINT:
1127 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001128 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1129
Dan Gohman7f460202008-06-30 20:59:49 +00001130 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001131 case TargetLowering::Promote:
1132 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001133 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001134 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001135 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohman44066042008-07-01 00:05:16 +00001136 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001137
Dan Gohman7f460202008-06-30 20:59:49 +00001138 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001139 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman7f460202008-06-30 20:59:49 +00001140 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1141 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001142
Dan Gohman7f460202008-06-30 20:59:49 +00001143 unsigned Line = DSP->getLine();
1144 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001145
1146 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001147 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001148 DAG.getConstant(Col, MVT::i32),
1149 DAG.getConstant(SrcFile, MVT::i32) };
1150 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001151 } else {
Evan Chenga647c922008-02-01 02:05:57 +00001152 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001153 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001154 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001155 } else {
1156 Result = Tmp1; // chain
1157 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001158 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001159 }
Evan Cheng71e86852008-07-08 20:06:39 +00001160 case TargetLowering::Legal: {
1161 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1162 if (Action == Legal && Tmp1 == Node->getOperand(0))
1163 break;
1164
Dan Gohman475871a2008-07-27 21:46:04 +00001165 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001166 Ops.push_back(Tmp1);
1167 if (Action == Legal) {
1168 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1169 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1170 } else {
1171 // Otherwise promote them.
1172 Ops.push_back(PromoteOp(Node->getOperand(1)));
1173 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001174 }
Evan Cheng71e86852008-07-08 20:06:39 +00001175 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1176 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1177 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001178 break;
1179 }
Evan Cheng71e86852008-07-08 20:06:39 +00001180 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001181 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001182
1183 case ISD::DECLARE:
1184 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1185 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1186 default: assert(0 && "This action is not supported yet!");
1187 case TargetLowering::Legal:
1188 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1189 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1190 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1191 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1192 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001193 case TargetLowering::Expand:
1194 Result = LegalizeOp(Node->getOperand(0));
1195 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001196 }
1197 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001198
1199 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001200 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001201 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001202 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001203 case TargetLowering::Legal: {
1204 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001205 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001206 if (Action == Legal && Tmp1 == Node->getOperand(0))
1207 break;
1208 if (Action == Legal) {
1209 Tmp2 = Node->getOperand(1);
1210 Tmp3 = Node->getOperand(2);
1211 Tmp4 = Node->getOperand(3);
1212 } else {
1213 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1214 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1215 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1216 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001218 break;
1219 }
Evan Cheng71e86852008-07-08 20:06:39 +00001220 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001221 break;
1222
Dan Gohman44066042008-07-01 00:05:16 +00001223 case ISD::DBG_LABEL:
1224 case ISD::EH_LABEL:
1225 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1226 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001227 default: assert(0 && "This action is not supported yet!");
1228 case TargetLowering::Legal:
1229 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001230 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001231 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001232 case TargetLowering::Expand:
1233 Result = LegalizeOp(Node->getOperand(0));
1234 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001235 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001236 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001237
Evan Cheng27b7db52008-03-08 00:58:38 +00001238 case ISD::PREFETCH:
1239 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1240 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1241 default: assert(0 && "This action is not supported yet!");
1242 case TargetLowering::Legal:
1243 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1244 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1245 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1246 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1247 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1248 break;
1249 case TargetLowering::Expand:
1250 // It's a noop.
1251 Result = LegalizeOp(Node->getOperand(0));
1252 break;
1253 }
1254 break;
1255
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001256 case ISD::MEMBARRIER: {
1257 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001258 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1259 default: assert(0 && "This action is not supported yet!");
1260 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001261 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001262 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001263 for (int x = 1; x < 6; ++x) {
1264 Ops[x] = Node->getOperand(x);
1265 if (!isTypeLegal(Ops[x].getValueType()))
1266 Ops[x] = PromoteOp(Ops[x]);
1267 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001268 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1269 break;
1270 }
1271 case TargetLowering::Expand:
1272 //There is no libgcc call for this op
1273 Result = Node->getOperand(0); // Noop
1274 break;
1275 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001276 break;
1277 }
1278
Dale Johannesene00a8a22008-08-28 02:44:49 +00001279 case ISD::ATOMIC_CMP_SWAP_8:
1280 case ISD::ATOMIC_CMP_SWAP_16:
1281 case ISD::ATOMIC_CMP_SWAP_32:
1282 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001283 unsigned int num_operands = 4;
1284 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001285 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001286 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001287 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001288 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1289
1290 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1291 default: assert(0 && "This action is not supported yet!");
1292 case TargetLowering::Custom:
1293 Result = TLI.LowerOperation(Result, DAG);
1294 break;
1295 case TargetLowering::Legal:
1296 break;
1297 }
Dan Gohman475871a2008-07-27 21:46:04 +00001298 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1299 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001300 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001301 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00001302 case ISD::ATOMIC_LOAD_ADD_8:
1303 case ISD::ATOMIC_LOAD_SUB_8:
1304 case ISD::ATOMIC_LOAD_AND_8:
1305 case ISD::ATOMIC_LOAD_OR_8:
1306 case ISD::ATOMIC_LOAD_XOR_8:
1307 case ISD::ATOMIC_LOAD_NAND_8:
1308 case ISD::ATOMIC_LOAD_MIN_8:
1309 case ISD::ATOMIC_LOAD_MAX_8:
1310 case ISD::ATOMIC_LOAD_UMIN_8:
1311 case ISD::ATOMIC_LOAD_UMAX_8:
1312 case ISD::ATOMIC_SWAP_8:
1313 case ISD::ATOMIC_LOAD_ADD_16:
1314 case ISD::ATOMIC_LOAD_SUB_16:
1315 case ISD::ATOMIC_LOAD_AND_16:
1316 case ISD::ATOMIC_LOAD_OR_16:
1317 case ISD::ATOMIC_LOAD_XOR_16:
1318 case ISD::ATOMIC_LOAD_NAND_16:
1319 case ISD::ATOMIC_LOAD_MIN_16:
1320 case ISD::ATOMIC_LOAD_MAX_16:
1321 case ISD::ATOMIC_LOAD_UMIN_16:
1322 case ISD::ATOMIC_LOAD_UMAX_16:
1323 case ISD::ATOMIC_SWAP_16:
1324 case ISD::ATOMIC_LOAD_ADD_32:
1325 case ISD::ATOMIC_LOAD_SUB_32:
1326 case ISD::ATOMIC_LOAD_AND_32:
1327 case ISD::ATOMIC_LOAD_OR_32:
1328 case ISD::ATOMIC_LOAD_XOR_32:
1329 case ISD::ATOMIC_LOAD_NAND_32:
1330 case ISD::ATOMIC_LOAD_MIN_32:
1331 case ISD::ATOMIC_LOAD_MAX_32:
1332 case ISD::ATOMIC_LOAD_UMIN_32:
1333 case ISD::ATOMIC_LOAD_UMAX_32:
1334 case ISD::ATOMIC_SWAP_32:
1335 case ISD::ATOMIC_LOAD_ADD_64:
1336 case ISD::ATOMIC_LOAD_SUB_64:
1337 case ISD::ATOMIC_LOAD_AND_64:
1338 case ISD::ATOMIC_LOAD_OR_64:
1339 case ISD::ATOMIC_LOAD_XOR_64:
1340 case ISD::ATOMIC_LOAD_NAND_64:
1341 case ISD::ATOMIC_LOAD_MIN_64:
1342 case ISD::ATOMIC_LOAD_MAX_64:
1343 case ISD::ATOMIC_LOAD_UMIN_64:
1344 case ISD::ATOMIC_LOAD_UMAX_64:
1345 case ISD::ATOMIC_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001346 unsigned int num_operands = 3;
1347 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001348 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001349 for (unsigned int x = 0; x < num_operands; ++x)
1350 Ops[x] = LegalizeOp(Node->getOperand(x));
1351 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001352
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001353 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001354 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001355 case TargetLowering::Custom:
1356 Result = TLI.LowerOperation(Result, DAG);
1357 break;
1358 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001359 break;
1360 }
Dan Gohman475871a2008-07-27 21:46:04 +00001361 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1362 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001363 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001364 }
Scott Michelc1513d22007-08-08 23:23:31 +00001365 case ISD::Constant: {
1366 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1367 unsigned opAction =
1368 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1369
Chris Lattner3e928bb2005-01-07 07:47:09 +00001370 // We know we don't need to expand constants here, constants only have one
1371 // value and we check that it is fine above.
1372
Scott Michelc1513d22007-08-08 23:23:31 +00001373 if (opAction == TargetLowering::Custom) {
1374 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001375 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001376 Result = Tmp1;
1377 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001378 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001379 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001380 case ISD::ConstantFP: {
1381 // Spill FP immediates to the constant pool if the target cannot directly
1382 // codegen them. Targets often have some immediate values that can be
1383 // efficiently generated into an FP register without a load. We explicitly
1384 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001385 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1386
Chris Lattner3181a772006-01-29 06:26:56 +00001387 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1388 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001389 case TargetLowering::Legal:
1390 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001391 case TargetLowering::Custom:
1392 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001393 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001394 Result = Tmp3;
1395 break;
1396 }
1397 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001398 case TargetLowering::Expand: {
1399 // Check to see if this FP immediate is already legal.
1400 bool isLegal = false;
1401 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1402 E = TLI.legal_fpimm_end(); I != E; ++I) {
1403 if (CFP->isExactlyValue(*I)) {
1404 isLegal = true;
1405 break;
1406 }
1407 }
1408 // If this is a legal constant, turn it into a TargetConstantFP node.
1409 if (isLegal)
1410 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001411 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001412 }
Nate Begemane1795842008-02-14 08:57:00 +00001413 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001414 break;
1415 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001416 case ISD::TokenFactor:
1417 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001418 Tmp1 = LegalizeOp(Node->getOperand(0));
1419 Tmp2 = LegalizeOp(Node->getOperand(1));
1420 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1421 } else if (Node->getNumOperands() == 3) {
1422 Tmp1 = LegalizeOp(Node->getOperand(0));
1423 Tmp2 = LegalizeOp(Node->getOperand(1));
1424 Tmp3 = LegalizeOp(Node->getOperand(2));
1425 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001426 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001427 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001428 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001429 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1430 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001431 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001432 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001433 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001434
1435 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001436 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001437 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001438 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001439 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001440 // A call within a calling sequence must be legalized to something
1441 // other than the normal CALLSEQ_END. Violating this gets Legalize
1442 // into an infinite loop.
1443 assert ((!IsLegalizingCall ||
1444 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001445 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001446 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001447
1448 // The number of incoming and outgoing values should match; unless the final
1449 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001450 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1451 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1452 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001453 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001454 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001455
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001456 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001457 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001458 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1459 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001460 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001461 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001462 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001463 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001464 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001465 }
1466 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001467 case ISD::EXTRACT_SUBREG: {
1468 Tmp1 = LegalizeOp(Node->getOperand(0));
1469 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1470 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001471 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001472 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1473 }
1474 break;
1475 case ISD::INSERT_SUBREG: {
1476 Tmp1 = LegalizeOp(Node->getOperand(0));
1477 Tmp2 = LegalizeOp(Node->getOperand(1));
1478 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1479 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001480 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001481 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1482 }
1483 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001484 case ISD::BUILD_VECTOR:
1485 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001486 default: assert(0 && "This action is not supported yet!");
1487 case TargetLowering::Custom:
1488 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001489 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001490 Result = Tmp3;
1491 break;
1492 }
1493 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001494 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001495 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001496 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001497 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001498 break;
1499 case ISD::INSERT_VECTOR_ELT:
1500 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001501 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001502
1503 // The type of the value to insert may not be legal, even though the vector
1504 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1505 // here.
1506 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1507 default: assert(0 && "Cannot expand insert element operand");
1508 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1509 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001510 case Expand:
1511 // FIXME: An alternative would be to check to see if the target is not
1512 // going to custom lower this operation, we could bitcast to half elt
1513 // width and perform two inserts at that width, if that is legal.
1514 Tmp2 = Node->getOperand(1);
1515 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001516 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001517 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1518
1519 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1520 Node->getValueType(0))) {
1521 default: assert(0 && "This action is not supported yet!");
1522 case TargetLowering::Legal:
1523 break;
1524 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001525 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001526 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001527 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001528 break;
1529 }
1530 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001531 case TargetLowering::Promote:
1532 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001533 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001534 // If the insert index is a constant, codegen this as a scalar_to_vector,
1535 // then a shuffle that inserts it into the right position in the vector.
1536 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001537 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1538 // match the element type of the vector being created.
1539 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001540 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001541 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001542 Tmp1.getValueType(), Tmp2);
1543
Duncan Sands83ec4b62008-06-06 12:08:01 +00001544 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1545 MVT ShufMaskVT =
1546 MVT::getIntVectorWithNumElements(NumElts);
1547 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001548
1549 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1550 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1551 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001552 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001553 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001554 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001555 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1556 else
1557 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1558 }
Dan Gohman475871a2008-07-27 21:46:04 +00001559 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001560 &ShufOps[0], ShufOps.size());
1561
1562 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1563 Tmp1, ScVec, ShufMask);
1564 Result = LegalizeOp(Result);
1565 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001566 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001567 }
Nate Begeman68679912008-04-25 18:07:40 +00001568 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001569 break;
1570 }
1571 }
1572 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001573 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001574 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1575 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1576 break;
1577 }
1578
Chris Lattnerce872152006-03-19 06:31:19 +00001579 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1580 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1581 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1582 Node->getValueType(0))) {
1583 default: assert(0 && "This action is not supported yet!");
1584 case TargetLowering::Legal:
1585 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001586 case TargetLowering::Custom:
1587 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001588 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001589 Result = Tmp3;
1590 break;
1591 }
1592 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001593 case TargetLowering::Expand:
1594 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001595 break;
1596 }
Chris Lattnerce872152006-03-19 06:31:19 +00001597 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001598 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001599 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1600 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1601 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1602
1603 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001604 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1605 default: assert(0 && "Unknown operation action!");
1606 case TargetLowering::Legal:
1607 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1608 "vector shuffle should not be created if not legal!");
1609 break;
1610 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001611 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001612 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001613 Result = Tmp3;
1614 break;
1615 }
1616 // FALLTHROUGH
1617 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001618 MVT VT = Node->getValueType(0);
1619 MVT EltVT = VT.getVectorElementType();
1620 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001621 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001622 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001623 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001624 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001625 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001626 if (Arg.getOpcode() == ISD::UNDEF) {
1627 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1628 } else {
1629 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001630 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001631 if (Idx < NumElems)
1632 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1633 DAG.getConstant(Idx, PtrVT)));
1634 else
1635 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1636 DAG.getConstant(Idx - NumElems, PtrVT)));
1637 }
1638 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001639 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001640 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001641 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001642 case TargetLowering::Promote: {
1643 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001644 MVT OVT = Node->getValueType(0);
1645 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001646
1647 // Cast the two input vectors.
1648 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1649 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1650
1651 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001652 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001653 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001654 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1655 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1656 break;
1657 }
Chris Lattner87100e02006-03-20 01:52:29 +00001658 }
1659 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001660
1661 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001662 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001663 Tmp2 = LegalizeOp(Node->getOperand(1));
1664 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001665 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001666 break;
1667
Dan Gohman7f321562007-06-25 16:23:39 +00001668 case ISD::EXTRACT_SUBVECTOR:
1669 Tmp1 = Node->getOperand(0);
1670 Tmp2 = LegalizeOp(Node->getOperand(1));
1671 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1672 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001673 break;
1674
Mon P Wang0c397192008-10-30 08:01:45 +00001675 case ISD::CONCAT_VECTORS: {
1676 // Use extract/insert/build vector for now. We might try to be
1677 // more clever later.
1678 MVT PtrVT = TLI.getPointerTy();
1679 SmallVector<SDValue, 8> Ops;
1680 unsigned NumOperands = Node->getNumOperands();
1681 for (unsigned i=0; i < NumOperands; ++i) {
1682 SDValue SubOp = Node->getOperand(i);
1683 MVT VVT = SubOp.getNode()->getValueType(0);
1684 MVT EltVT = VVT.getVectorElementType();
1685 unsigned NumSubElem = VVT.getVectorNumElements();
1686 for (unsigned j=0; j < NumSubElem; ++j) {
1687 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1688 DAG.getConstant(j, PtrVT)));
1689 }
1690 }
1691 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1692 &Ops[0], Ops.size()));
1693 }
1694
Chris Lattner6831a812006-02-13 09:18:02 +00001695 case ISD::CALLSEQ_START: {
1696 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1697
1698 // Recursively Legalize all of the inputs of the call end that do not lead
1699 // to this call start. This ensures that any libcalls that need be inserted
1700 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001701 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001702 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001703 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001704 NodesLeadingTo);
1705 }
Chris Lattner6831a812006-02-13 09:18:02 +00001706
1707 // Now that we legalized all of the inputs (which may have inserted
1708 // libcalls) create the new CALLSEQ_START node.
1709 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1710
1711 // Merge in the last call, to ensure that this call start after the last
1712 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001713 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001714 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1715 Tmp1 = LegalizeOp(Tmp1);
1716 }
Chris Lattner6831a812006-02-13 09:18:02 +00001717
1718 // Do not try to legalize the target-specific arguments (#1+).
1719 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001720 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001721 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001722 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001723 }
1724
1725 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001726 AddLegalizedOperand(Op.getValue(0), Result);
1727 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1728 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1729
Chris Lattner6831a812006-02-13 09:18:02 +00001730 // Now that the callseq_start and all of the non-call nodes above this call
1731 // sequence have been legalized, legalize the call itself. During this
1732 // process, no libcalls can/will be inserted, guaranteeing that no calls
1733 // can overlap.
1734 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001735 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001736 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001737 IsLegalizingCall = true;
1738
1739 // Legalize the call, starting from the CALLSEQ_END.
1740 LegalizeOp(LastCALLSEQ_END);
1741 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1742 return Result;
1743 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001744 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001745 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1746 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001747 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001748 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1749 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001750 assert(I != LegalizedNodes.end() &&
1751 "Legalizing the call start should have legalized this node!");
1752 return I->second;
1753 }
1754
1755 // Otherwise, the call start has been legalized and everything is going
1756 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001757 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001758 // Do not try to legalize the target-specific arguments (#1+), except for
1759 // an optional flag input.
1760 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1761 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001762 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001763 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001764 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001765 }
1766 } else {
1767 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1768 if (Tmp1 != Node->getOperand(0) ||
1769 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001770 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001771 Ops[0] = Tmp1;
1772 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001773 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001774 }
Chris Lattner6a542892006-01-24 05:48:21 +00001775 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001776 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001777 // This finishes up call legalization.
1778 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001779
1780 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001781 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001782 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001783 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001784 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001785 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001786 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001787 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1788 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1789 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001790 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001791
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001792 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001793 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001794 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001795 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001796 case TargetLowering::Expand: {
1797 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1798 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1799 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001800 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001801
1802 // Chain the dynamic stack allocation so that it doesn't modify the stack
1803 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001804 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001805
Dan Gohman475871a2008-07-27 21:46:04 +00001806 SDValue Size = Tmp2.getOperand(1);
1807 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001808 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001809 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001810 unsigned StackAlign =
1811 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1812 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001813 SP = DAG.getNode(ISD::AND, VT, SP,
1814 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001815 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001816 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1817
Chris Lattnere563bbc2008-10-11 22:08:30 +00001818 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1819 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001820
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001821 Tmp1 = LegalizeOp(Tmp1);
1822 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001823 break;
1824 }
1825 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001826 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001827 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001828 Tmp1 = LegalizeOp(Tmp3);
1829 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001830 }
Chris Lattner903d2782006-01-15 08:54:32 +00001831 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001832 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001833 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001834 }
Chris Lattner903d2782006-01-15 08:54:32 +00001835 // Since this op produce two values, make sure to remember that we
1836 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001837 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1838 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001839 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001840 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001841 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001842 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001843 bool Changed = false;
1844 // Legalize all of the operands of the inline asm, in case they are nodes
1845 // that need to be expanded or something. Note we skip the asm string and
1846 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001847 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001848 Changed = Op != Ops[0];
1849 Ops[0] = Op;
1850
1851 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1852 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001853 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001854 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001855 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001856 if (Op != Ops[i]) {
1857 Changed = true;
1858 Ops[i] = Op;
1859 }
1860 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001861 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001862
1863 if (HasInFlag) {
1864 Op = LegalizeOp(Ops.back());
1865 Changed |= Op != Ops.back();
1866 Ops.back() = Op;
1867 }
1868
1869 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001870 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001871
1872 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001873 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1874 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001875 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001876 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001877 case ISD::BR:
1878 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001879 // Ensure that libcalls are emitted before a branch.
1880 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1881 Tmp1 = LegalizeOp(Tmp1);
1882 LastCALLSEQ_END = DAG.getEntryNode();
1883
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001885 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001886 case ISD::BRIND:
1887 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1888 // Ensure that libcalls are emitted before a branch.
1889 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1890 Tmp1 = LegalizeOp(Tmp1);
1891 LastCALLSEQ_END = DAG.getEntryNode();
1892
1893 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1894 default: assert(0 && "Indirect target must be legal type (pointer)!");
1895 case Legal:
1896 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1897 break;
1898 }
1899 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1900 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001901 case ISD::BR_JT:
1902 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1903 // Ensure that libcalls are emitted before a branch.
1904 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1905 Tmp1 = LegalizeOp(Tmp1);
1906 LastCALLSEQ_END = DAG.getEntryNode();
1907
1908 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1909 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1910
1911 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1912 default: assert(0 && "This action is not supported yet!");
1913 case TargetLowering::Legal: break;
1914 case TargetLowering::Custom:
1915 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001916 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001917 break;
1918 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001919 SDValue Chain = Result.getOperand(0);
1920 SDValue Table = Result.getOperand(1);
1921 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001922
Duncan Sands83ec4b62008-06-06 12:08:01 +00001923 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001924 MachineFunction &MF = DAG.getMachineFunction();
1925 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001926 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00001927 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001928
Dan Gohman475871a2008-07-27 21:46:04 +00001929 SDValue LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001930 switch (EntrySize) {
1931 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001932 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001933 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001934 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001935 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001936 }
1937
Evan Chengcc415862007-11-09 01:32:10 +00001938 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001939 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001940 // For PIC, the sequence is:
1941 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001942 // RelocBase can be JumpTable, GOT or some sort of global base.
1943 if (PTy != MVT::i32)
1944 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1945 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1946 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001947 }
Evan Chengcc415862007-11-09 01:32:10 +00001948 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001949 }
1950 }
1951 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001952 case ISD::BRCOND:
1953 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001954 // Ensure that libcalls are emitted before a return.
1955 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1956 Tmp1 = LegalizeOp(Tmp1);
1957 LastCALLSEQ_END = DAG.getEntryNode();
1958
Chris Lattner47e92232005-01-18 19:27:06 +00001959 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1960 case Expand: assert(0 && "It's impossible to expand bools");
1961 case Legal:
1962 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1963 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001964 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001965 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001966
1967 // The top bits of the promoted condition are not necessarily zero, ensure
1968 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001969 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001970 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001971 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001972 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001973 break;
1974 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001975 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001976
1977 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001978 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001979
1980 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1981 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001982 case TargetLowering::Legal: break;
1983 case TargetLowering::Custom:
1984 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001985 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001986 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001987 case TargetLowering::Expand:
1988 // Expand brcond's setcc into its constituent parts and create a BR_CC
1989 // Node.
1990 if (Tmp2.getOpcode() == ISD::SETCC) {
1991 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1992 Tmp2.getOperand(0), Tmp2.getOperand(1),
1993 Node->getOperand(2));
1994 } else {
1995 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1996 DAG.getCondCode(ISD::SETNE), Tmp2,
1997 DAG.getConstant(0, Tmp2.getValueType()),
1998 Node->getOperand(2));
1999 }
2000 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002001 }
2002 break;
2003 case ISD::BR_CC:
2004 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002005 // Ensure that libcalls are emitted before a branch.
2006 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2007 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002008 Tmp2 = Node->getOperand(2); // LHS
2009 Tmp3 = Node->getOperand(3); // RHS
2010 Tmp4 = Node->getOperand(1); // CC
2011
Evan Cheng7f042682008-10-15 02:05:31 +00002012 LegalizeSetCC(Node->getValueType(0), Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002013 LastCALLSEQ_END = DAG.getEntryNode();
2014
Evan Cheng7f042682008-10-15 02:05:31 +00002015 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002016 // the LHS is a legal SETCC itself. In this case, we need to compare
2017 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002018 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002019 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2020 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002021 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002022
2023 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2024 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002025
Chris Lattner181b7a32005-12-17 23:46:46 +00002026 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2027 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002028 case TargetLowering::Legal: break;
2029 case TargetLowering::Custom:
2030 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002031 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002032 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002033 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002034 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002035 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002036 LoadSDNode *LD = cast<LoadSDNode>(Node);
2037 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2038 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002039
Evan Cheng466685d2006-10-09 20:57:25 +00002040 ISD::LoadExtType ExtType = LD->getExtensionType();
2041 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002042 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002043 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2044 Tmp3 = Result.getValue(0);
2045 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002046
Evan Cheng466685d2006-10-09 20:57:25 +00002047 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2048 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002049 case TargetLowering::Legal:
2050 // If this is an unaligned load and the target doesn't support it,
2051 // expand it.
2052 if (!TLI.allowsUnalignedMemoryAccesses()) {
2053 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002054 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002055 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002056 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002057 TLI);
2058 Tmp3 = Result.getOperand(0);
2059 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002060 Tmp3 = LegalizeOp(Tmp3);
2061 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002062 }
2063 }
2064 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002065 case TargetLowering::Custom:
2066 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002067 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002068 Tmp3 = LegalizeOp(Tmp1);
2069 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002070 }
Evan Cheng466685d2006-10-09 20:57:25 +00002071 break;
2072 case TargetLowering::Promote: {
2073 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002074 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002075 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002076 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002077
2078 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002079 LD->getSrcValueOffset(),
2080 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002081 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2082 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002083 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002084 }
Evan Cheng466685d2006-10-09 20:57:25 +00002085 }
2086 // Since loads produce two values, make sure to remember that we
2087 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002088 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2089 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002090 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002091 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002092 MVT SrcVT = LD->getMemoryVT();
2093 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002094 int SVOffset = LD->getSrcValueOffset();
2095 unsigned Alignment = LD->getAlignment();
2096 bool isVolatile = LD->isVolatile();
2097
Duncan Sands83ec4b62008-06-06 12:08:01 +00002098 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002099 // Some targets pretend to have an i1 loading operation, and actually
2100 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2101 // bits are guaranteed to be zero; it helps the optimizers understand
2102 // that these bits are zero. It is also useful for EXTLOAD, since it
2103 // tells the optimizers that those bits are undefined. It would be
2104 // nice to have an effective generic way of getting these benefits...
2105 // Until such a way is found, don't insist on promoting i1 here.
2106 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002107 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002108 // Promote to a byte-sized load if not loading an integral number of
2109 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002110 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2111 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002112 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002113
2114 // The extra bits are guaranteed to be zero, since we stored them that
2115 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2116
2117 ISD::LoadExtType NewExtType =
2118 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2119
2120 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2121 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2122 NVT, isVolatile, Alignment);
2123
2124 Ch = Result.getValue(1); // The chain.
2125
2126 if (ExtType == ISD::SEXTLOAD)
2127 // Having the top bits zero doesn't help when sign extending.
2128 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2129 Result, DAG.getValueType(SrcVT));
2130 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2131 // All the top bits are guaranteed to be zero - inform the optimizers.
2132 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2133 DAG.getValueType(SrcVT));
2134
2135 Tmp1 = LegalizeOp(Result);
2136 Tmp2 = LegalizeOp(Ch);
2137 } else if (SrcWidth & (SrcWidth - 1)) {
2138 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002139 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002140 "Unsupported extload!");
2141 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2142 assert(RoundWidth < SrcWidth);
2143 unsigned ExtraWidth = SrcWidth - RoundWidth;
2144 assert(ExtraWidth < RoundWidth);
2145 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2146 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002147 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2148 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002149 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002150 unsigned IncrementSize;
2151
2152 if (TLI.isLittleEndian()) {
2153 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2154 // Load the bottom RoundWidth bits.
2155 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2156 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2157 Alignment);
2158
2159 // Load the remaining ExtraWidth bits.
2160 IncrementSize = RoundWidth / 8;
2161 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2162 DAG.getIntPtrConstant(IncrementSize));
2163 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2164 LD->getSrcValue(), SVOffset + IncrementSize,
2165 ExtraVT, isVolatile,
2166 MinAlign(Alignment, IncrementSize));
2167
2168 // Build a factor node to remember that this load is independent of the
2169 // other one.
2170 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2171 Hi.getValue(1));
2172
2173 // Move the top bits to the right place.
2174 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2175 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2176
2177 // Join the hi and lo parts.
2178 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002179 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002180 // Big endian - avoid unaligned loads.
2181 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2182 // Load the top RoundWidth bits.
2183 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2184 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2185 Alignment);
2186
2187 // Load the remaining ExtraWidth bits.
2188 IncrementSize = RoundWidth / 8;
2189 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2190 DAG.getIntPtrConstant(IncrementSize));
2191 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2192 LD->getSrcValue(), SVOffset + IncrementSize,
2193 ExtraVT, isVolatile,
2194 MinAlign(Alignment, IncrementSize));
2195
2196 // Build a factor node to remember that this load is independent of the
2197 // other one.
2198 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2199 Hi.getValue(1));
2200
2201 // Move the top bits to the right place.
2202 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2203 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2204
2205 // Join the hi and lo parts.
2206 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2207 }
2208
2209 Tmp1 = LegalizeOp(Result);
2210 Tmp2 = LegalizeOp(Ch);
2211 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002212 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002213 default: assert(0 && "This action is not supported yet!");
2214 case TargetLowering::Custom:
2215 isCustom = true;
2216 // FALLTHROUGH
2217 case TargetLowering::Legal:
2218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2219 Tmp1 = Result.getValue(0);
2220 Tmp2 = Result.getValue(1);
2221
2222 if (isCustom) {
2223 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002224 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002225 Tmp1 = LegalizeOp(Tmp3);
2226 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2227 }
2228 } else {
2229 // If this is an unaligned load and the target doesn't support it,
2230 // expand it.
2231 if (!TLI.allowsUnalignedMemoryAccesses()) {
2232 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002233 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002234 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002235 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002236 TLI);
2237 Tmp1 = Result.getOperand(0);
2238 Tmp2 = Result.getOperand(1);
2239 Tmp1 = LegalizeOp(Tmp1);
2240 Tmp2 = LegalizeOp(Tmp2);
2241 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002242 }
2243 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002244 break;
2245 case TargetLowering::Expand:
2246 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2247 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002248 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002249 LD->getSrcValueOffset(),
2250 LD->isVolatile(), LD->getAlignment());
2251 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2252 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2253 Tmp2 = LegalizeOp(Load.getValue(1));
2254 break;
2255 }
2256 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2257 // Turn the unsupported load into an EXTLOAD followed by an explicit
2258 // zero/sign extend inreg.
2259 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2260 Tmp1, Tmp2, LD->getSrcValue(),
2261 LD->getSrcValueOffset(), SrcVT,
2262 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002263 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002264 if (ExtType == ISD::SEXTLOAD)
2265 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2266 Result, DAG.getValueType(SrcVT));
2267 else
2268 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2269 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2270 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002271 break;
2272 }
Evan Cheng466685d2006-10-09 20:57:25 +00002273 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002274
Evan Cheng466685d2006-10-09 20:57:25 +00002275 // Since loads produce two values, make sure to remember that we legalized
2276 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002277 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2278 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002279 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002280 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002281 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002282 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002283 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002284 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002285 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002286 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002287 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002288 // 1 -> Hi
2289 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002290 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002291 TLI.getShiftAmountTy()));
2292 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2293 } else {
2294 // 0 -> Lo
2295 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2296 Node->getOperand(0));
2297 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002298 break;
2299 case Expand:
2300 // Get both the low and high parts.
2301 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002302 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002303 Result = Tmp2; // 1 -> Hi
2304 else
2305 Result = Tmp1; // 0 -> Lo
2306 break;
2307 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002308 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002309 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002310
2311 case ISD::CopyToReg:
2312 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002313
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002314 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002315 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002316 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002317 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002318 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002319 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002320 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002321 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002322 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002323 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002324 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2325 Tmp3);
2326 } else {
2327 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002328 }
2329
2330 // Since this produces two values, make sure to remember that we legalized
2331 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002332 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2333 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002334 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002335 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002336 break;
2337
2338 case ISD::RET:
2339 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002340
2341 // Ensure that libcalls are emitted before a return.
2342 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2343 Tmp1 = LegalizeOp(Tmp1);
2344 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002345
Chris Lattner3e928bb2005-01-07 07:47:09 +00002346 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002347 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002348 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002349 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002350 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002351 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002352 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002353 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002354 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002355 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002356 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002357 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002358
2359 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002360 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002361 std::swap(Lo, Hi);
2362
Gabor Greifba36cb52008-08-28 21:40:38 +00002363 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002364 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2365 else
2366 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002367 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002368 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002369 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002370 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002371 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2372 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002373
Dan Gohman7f321562007-06-25 16:23:39 +00002374 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002375 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002376 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002377 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002378 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002379 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002380 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002381 } else if (NumElems == 1) {
2382 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002383 Tmp2 = ScalarizeVectorOp(Tmp2);
2384 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002385 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002386
2387 // FIXME: Returns of gcc generic vectors smaller than a legal type
2388 // should be returned in integer registers!
2389
Chris Lattnerf87324e2006-04-11 01:31:51 +00002390 // The scalarized value type may not be legal, e.g. it might require
2391 // promotion or expansion. Relegalize the return.
2392 Result = LegalizeOp(Result);
2393 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002394 // FIXME: Returns of gcc generic vectors larger than a legal vector
2395 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002397 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002398 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002399 Result = LegalizeOp(Result);
2400 }
2401 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002402 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002403 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002404 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002405 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002406 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002407 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002408 }
2409 break;
2410 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002411 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002412 break;
2413 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002414 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002415 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002416 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002417 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2418 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002419 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002420 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002421 break;
2422 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002423 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002424 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002425 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002426 ExpandOp(Node->getOperand(i), Lo, Hi);
2427 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002428 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002429 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002430 NewValues.push_back(Hi);
2431 NewValues.push_back(Node->getOperand(i+1));
2432 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002433 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002434 }
2435 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002436 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002437 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002438
2439 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002440 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002441 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002442 Result = DAG.getNode(ISD::RET, MVT::Other,
2443 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002444 break;
2445 }
2446 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002447
Chris Lattner6862dbc2006-01-29 21:02:23 +00002448 if (Result.getOpcode() == ISD::RET) {
2449 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2450 default: assert(0 && "This action is not supported yet!");
2451 case TargetLowering::Legal: break;
2452 case TargetLowering::Custom:
2453 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002454 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002455 break;
2456 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002457 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002458 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002459 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002460 StoreSDNode *ST = cast<StoreSDNode>(Node);
2461 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2462 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002463 int SVOffset = ST->getSrcValueOffset();
2464 unsigned Alignment = ST->getAlignment();
2465 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002466
Evan Cheng8b2794a2006-10-13 21:14:26 +00002467 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002468 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2469 // FIXME: We shouldn't do this for TargetConstantFP's.
2470 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2471 // to phase ordering between legalized code and the dag combiner. This
2472 // probably means that we need to integrate dag combiner and legalizer
2473 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002474 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002475 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002476 if (CFP->getValueType(0) == MVT::f32 &&
2477 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002478 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002479 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002480 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002481 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2482 SVOffset, isVolatile, Alignment);
2483 break;
2484 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002485 // If this target supports 64-bit registers, do a single 64-bit store.
2486 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002487 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002488 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002489 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2490 SVOffset, isVolatile, Alignment);
2491 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002492 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002493 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2494 // stores. If the target supports neither 32- nor 64-bits, this
2495 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002496 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002497 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2498 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002499 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002500
2501 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2502 SVOffset, isVolatile, Alignment);
2503 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002504 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002505 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002506 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002507
2508 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2509 break;
2510 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002511 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002512 }
2513
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002514 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002515 case Legal: {
2516 Tmp3 = LegalizeOp(ST->getValue());
2517 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2518 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002519
Duncan Sands83ec4b62008-06-06 12:08:01 +00002520 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002521 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2522 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002523 case TargetLowering::Legal:
2524 // If this is an unaligned store and the target doesn't support it,
2525 // expand it.
2526 if (!TLI.allowsUnalignedMemoryAccesses()) {
2527 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002528 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002529 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002530 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002531 TLI);
2532 }
2533 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002534 case TargetLowering::Custom:
2535 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002536 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002537 break;
2538 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002539 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002540 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2541 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2542 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002543 ST->getSrcValue(), SVOffset, isVolatile,
2544 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002545 break;
2546 }
2547 break;
2548 }
2549 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002550 if (!ST->getMemoryVT().isVector()) {
2551 // Truncate the value and store the result.
2552 Tmp3 = PromoteOp(ST->getValue());
2553 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2554 SVOffset, ST->getMemoryVT(),
2555 isVolatile, Alignment);
2556 break;
2557 }
2558 // Fall thru to expand for vector
2559 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002560 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002561 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002562
2563 // If this is a vector type, then we have to calculate the increment as
2564 // the product of the element size in bytes, and the number of elements
2565 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002566 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002567 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002568 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002569 MVT InVT = InVal->getValueType(InIx);
2570 unsigned NumElems = InVT.getVectorNumElements();
2571 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002572
Dan Gohman7f321562007-06-25 16:23:39 +00002573 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002574 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002575 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002576 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002577 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002578 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002579 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002580 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002581 Result = LegalizeOp(Result);
2582 break;
2583 } else if (NumElems == 1) {
2584 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002585 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002586 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002587 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002588 // The scalarized value type may not be legal, e.g. it might require
2589 // promotion or expansion. Relegalize the scalar store.
2590 Result = LegalizeOp(Result);
2591 break;
2592 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002593 // Check if we have widen this node with another value
2594 std::map<SDValue, SDValue>::iterator I =
2595 WidenNodes.find(ST->getValue());
2596 if (I != WidenNodes.end()) {
2597 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2598 break;
2599 }
2600 else {
2601 SplitVectorOp(ST->getValue(), Lo, Hi);
2602 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2603 EVT.getSizeInBits()/8;
2604 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002605 }
2606 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002607 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002608 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002609
Richard Pennington4b052dc2008-09-25 16:15:10 +00002610 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002611 std::swap(Lo, Hi);
2612 }
2613
2614 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002615 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002616
Gabor Greifba36cb52008-08-28 21:40:38 +00002617 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002618 // Must be int <-> float one-to-one expansion.
2619 Result = Lo;
2620 break;
2621 }
2622
Evan Cheng8b2794a2006-10-13 21:14:26 +00002623 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002624 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002625 assert(isTypeLegal(Tmp2.getValueType()) &&
2626 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002627 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002628 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002629 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002630 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002631 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2632 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002633 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002634 }
2635 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002636 switch (getTypeAction(ST->getValue().getValueType())) {
2637 case Legal:
2638 Tmp3 = LegalizeOp(ST->getValue());
2639 break;
2640 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002641 if (!ST->getValue().getValueType().isVector()) {
2642 // We can promote the value, the truncstore will still take care of it.
2643 Tmp3 = PromoteOp(ST->getValue());
2644 break;
2645 }
2646 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002647 case Expand:
2648 // Just store the low part. This may become a non-trunc store, so make
2649 // sure to use getTruncStore, not UpdateNodeOperands below.
2650 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2651 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2652 SVOffset, MVT::i8, isVolatile, Alignment);
2653 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002654
Duncan Sands83ec4b62008-06-06 12:08:01 +00002655 MVT StVT = ST->getMemoryVT();
2656 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002657
Duncan Sands83ec4b62008-06-06 12:08:01 +00002658 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002659 // Promote to a byte-sized store with upper bits zero if not
2660 // storing an integral number of bytes. For example, promote
2661 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002662 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002663 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2664 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2665 SVOffset, NVT, isVolatile, Alignment);
2666 } else if (StWidth & (StWidth - 1)) {
2667 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002668 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002669 "Unsupported truncstore!");
2670 unsigned RoundWidth = 1 << Log2_32(StWidth);
2671 assert(RoundWidth < StWidth);
2672 unsigned ExtraWidth = StWidth - RoundWidth;
2673 assert(ExtraWidth < RoundWidth);
2674 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2675 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002676 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2677 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002678 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002679 unsigned IncrementSize;
2680
2681 if (TLI.isLittleEndian()) {
2682 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2683 // Store the bottom RoundWidth bits.
2684 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2685 SVOffset, RoundVT,
2686 isVolatile, Alignment);
2687
2688 // Store the remaining ExtraWidth bits.
2689 IncrementSize = RoundWidth / 8;
2690 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2691 DAG.getIntPtrConstant(IncrementSize));
2692 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2693 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2694 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2695 SVOffset + IncrementSize, ExtraVT, isVolatile,
2696 MinAlign(Alignment, IncrementSize));
2697 } else {
2698 // Big endian - avoid unaligned stores.
2699 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2700 // Store the top RoundWidth bits.
2701 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2702 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2703 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2704 RoundVT, isVolatile, Alignment);
2705
2706 // Store the remaining ExtraWidth bits.
2707 IncrementSize = RoundWidth / 8;
2708 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2709 DAG.getIntPtrConstant(IncrementSize));
2710 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2711 SVOffset + IncrementSize, ExtraVT, isVolatile,
2712 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002713 }
Duncan Sands7e857202008-01-22 07:17:34 +00002714
2715 // The order of the stores doesn't matter.
2716 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2717 } else {
2718 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2719 Tmp2 != ST->getBasePtr())
2720 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2721 ST->getOffset());
2722
2723 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2724 default: assert(0 && "This action is not supported yet!");
2725 case TargetLowering::Legal:
2726 // If this is an unaligned store and the target doesn't support it,
2727 // expand it.
2728 if (!TLI.allowsUnalignedMemoryAccesses()) {
2729 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002730 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002731 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002732 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002733 TLI);
2734 }
2735 break;
2736 case TargetLowering::Custom:
2737 Result = TLI.LowerOperation(Result, DAG);
2738 break;
2739 case Expand:
2740 // TRUNCSTORE:i16 i32 -> STORE i16
2741 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2742 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2743 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2744 isVolatile, Alignment);
2745 break;
2746 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002747 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002748 }
2749 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002750 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002751 case ISD::PCMARKER:
2752 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002753 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002754 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002755 case ISD::STACKSAVE:
2756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002757 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2758 Tmp1 = Result.getValue(0);
2759 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002760
Chris Lattner140d53c2006-01-13 02:50:02 +00002761 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2762 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002763 case TargetLowering::Legal: break;
2764 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002765 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002766 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002767 Tmp1 = LegalizeOp(Tmp3);
2768 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002769 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002770 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002771 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002772 // Expand to CopyFromReg if the target set
2773 // StackPointerRegisterToSaveRestore.
2774 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002775 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002776 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002777 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002778 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002779 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2780 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002781 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002782 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002783 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002784
2785 // Since stacksave produce two values, make sure to remember that we
2786 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002787 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2788 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002789 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002790
Chris Lattner140d53c2006-01-13 02:50:02 +00002791 case ISD::STACKRESTORE:
2792 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2793 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002794 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002795
2796 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2797 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002798 case TargetLowering::Legal: break;
2799 case TargetLowering::Custom:
2800 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002801 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002802 break;
2803 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002804 // Expand to CopyToReg if the target set
2805 // StackPointerRegisterToSaveRestore.
2806 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2807 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2808 } else {
2809 Result = Tmp1;
2810 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002811 break;
2812 }
2813 break;
2814
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002815 case ISD::READCYCLECOUNTER:
2816 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002817 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002818 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2819 Node->getValueType(0))) {
2820 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002821 case TargetLowering::Legal:
2822 Tmp1 = Result.getValue(0);
2823 Tmp2 = Result.getValue(1);
2824 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002825 case TargetLowering::Custom:
2826 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002827 Tmp1 = LegalizeOp(Result.getValue(0));
2828 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002829 break;
2830 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002831
2832 // Since rdcc produce two values, make sure to remember that we legalized
2833 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002834 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2835 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002836 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002837
Chris Lattner2ee743f2005-01-14 22:08:15 +00002838 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002839 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2840 case Expand: assert(0 && "It's impossible to expand bools");
2841 case Legal:
2842 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2843 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002844 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002845 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002846 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002847 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002848 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002849 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002850 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002851 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002852 break;
2853 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002854 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002855 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002856 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002857
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002858 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002859
Nate Begemanb942a3d2005-08-23 04:29:48 +00002860 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002861 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002862 case TargetLowering::Legal: break;
2863 case TargetLowering::Custom: {
2864 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002865 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002866 break;
2867 }
Nate Begeman9373a812005-08-10 20:51:12 +00002868 case TargetLowering::Expand:
2869 if (Tmp1.getOpcode() == ISD::SETCC) {
2870 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2871 Tmp2, Tmp3,
2872 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2873 } else {
2874 Result = DAG.getSelectCC(Tmp1,
2875 DAG.getConstant(0, Tmp1.getValueType()),
2876 Tmp2, Tmp3, ISD::SETNE);
2877 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002878 break;
2879 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002880 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002881 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2882 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002883 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002884 ExtOp = ISD::BIT_CONVERT;
2885 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002886 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002887 ExtOp = ISD::ANY_EXTEND;
2888 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002889 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002890 ExtOp = ISD::FP_EXTEND;
2891 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002892 }
2893 // Promote each of the values to the new type.
2894 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2895 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2896 // Perform the larger operation, then round down.
2897 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002898 if (TruncOp != ISD::FP_ROUND)
2899 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2900 else
2901 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2902 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002903 break;
2904 }
2905 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002906 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002907 case ISD::SELECT_CC: {
2908 Tmp1 = Node->getOperand(0); // LHS
2909 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002910 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2911 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002912 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002913
Evan Cheng7f042682008-10-15 02:05:31 +00002914 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002915
Evan Cheng7f042682008-10-15 02:05:31 +00002916 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002917 // the LHS is a legal SETCC itself. In this case, we need to compare
2918 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002919 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002920 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2921 CC = DAG.getCondCode(ISD::SETNE);
2922 }
2923 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2924
2925 // Everything is legal, see if we should expand this op or something.
2926 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2927 default: assert(0 && "This action is not supported yet!");
2928 case TargetLowering::Legal: break;
2929 case TargetLowering::Custom:
2930 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002931 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002932 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002933 }
2934 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002935 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002936 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002937 Tmp1 = Node->getOperand(0);
2938 Tmp2 = Node->getOperand(1);
2939 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00002940 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002941
2942 // If we had to Expand the SetCC operands into a SELECT node, then it may
2943 // not always be possible to return a true LHS & RHS. In this case, just
2944 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002945 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002946 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002947 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002948 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002949
Chris Lattner73e142f2006-01-30 22:43:50 +00002950 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002951 default: assert(0 && "Cannot handle this action for SETCC yet!");
2952 case TargetLowering::Custom:
2953 isCustom = true;
2954 // FALLTHROUGH.
2955 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002956 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002957 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002958 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002959 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002960 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002961 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002962 case TargetLowering::Promote: {
2963 // First step, figure out the appropriate operation to use.
2964 // Allow SETCC to not be supported for all legal data types
2965 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002966 MVT NewInTy = Node->getOperand(0).getValueType();
2967 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002968
2969 // Scan for the appropriate larger type to use.
2970 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002971 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002972
Duncan Sands83ec4b62008-06-06 12:08:01 +00002973 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002974 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002975 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002976 "Fell off of the edge of the floating point world");
2977
2978 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002979 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002980 break;
2981 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002982 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002983 assert(0 && "Cannot promote Legal Integer SETCC yet");
2984 else {
2985 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2986 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2987 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002988 Tmp1 = LegalizeOp(Tmp1);
2989 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002990 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002991 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002992 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002993 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002994 case TargetLowering::Expand:
2995 // Expand a setcc node into a select_cc of the same condition, lhs, and
2996 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002997 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002998 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2999 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003000 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003001 break;
3002 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003003 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003004 case ISD::VSETCC: {
3005 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3006 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003007 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003008
3009 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3010
3011 // Everything is legal, see if we should expand this op or something.
3012 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3013 default: assert(0 && "This action is not supported yet!");
3014 case TargetLowering::Legal: break;
3015 case TargetLowering::Custom:
3016 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003017 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003018 break;
3019 }
3020 break;
3021 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003022
Chris Lattner5b359c62005-04-02 04:00:59 +00003023 case ISD::SHL_PARTS:
3024 case ISD::SRA_PARTS:
3025 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003026 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003027 bool Changed = false;
3028 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3029 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3030 Changed |= Ops.back() != Node->getOperand(i);
3031 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003032 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003033 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003034
Evan Cheng05a2d562006-01-09 18:31:59 +00003035 switch (TLI.getOperationAction(Node->getOpcode(),
3036 Node->getValueType(0))) {
3037 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003038 case TargetLowering::Legal: break;
3039 case TargetLowering::Custom:
3040 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003041 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003042 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003043 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003044 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003045 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003046 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003047 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003048 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003049 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003050 return RetVal;
3051 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003052 break;
3053 }
3054
Chris Lattner2c8086f2005-04-02 05:00:07 +00003055 // Since these produce multiple values, make sure to remember that we
3056 // legalized all of them.
3057 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003058 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003059 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003060 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003061
3062 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003063 case ISD::ADD:
3064 case ISD::SUB:
3065 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003066 case ISD::MULHS:
3067 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003068 case ISD::UDIV:
3069 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003070 case ISD::AND:
3071 case ISD::OR:
3072 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003073 case ISD::SHL:
3074 case ISD::SRL:
3075 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003076 case ISD::FADD:
3077 case ISD::FSUB:
3078 case ISD::FMUL:
3079 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003080 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003081 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003082 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3083 case Expand: assert(0 && "Not possible");
3084 case Legal:
3085 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3086 break;
3087 case Promote:
3088 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3089 break;
3090 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003091
3092 if ((Node->getOpcode() == ISD::SHL ||
3093 Node->getOpcode() == ISD::SRL ||
3094 Node->getOpcode() == ISD::SRA) &&
3095 !Node->getValueType(0).isVector()) {
3096 if (TLI.getShiftAmountTy().bitsLT(Tmp2.getValueType()))
3097 Tmp2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Tmp2);
3098 else if (TLI.getShiftAmountTy().bitsGT(Tmp2.getValueType()))
3099 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Tmp2);
3100 }
3101
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003102 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00003103
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003104 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003105 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003106 case TargetLowering::Legal: break;
3107 case TargetLowering::Custom:
3108 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003109 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003110 Result = Tmp1;
3111 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003112 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003113 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003114 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003115 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003116
Dan Gohman525178c2007-10-08 18:33:35 +00003117 // See if multiply or divide can be lowered using two-result operations.
3118 SDVTList VTs = DAG.getVTList(VT, VT);
3119 if (Node->getOpcode() == ISD::MUL) {
3120 // We just need the low half of the multiply; try both the signed
3121 // and unsigned forms. If the target supports both SMUL_LOHI and
3122 // UMUL_LOHI, form a preference by checking which forms of plain
3123 // MULH it supports.
3124 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3125 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3126 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3127 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3128 unsigned OpToUse = 0;
3129 if (HasSMUL_LOHI && !HasMULHS) {
3130 OpToUse = ISD::SMUL_LOHI;
3131 } else if (HasUMUL_LOHI && !HasMULHU) {
3132 OpToUse = ISD::UMUL_LOHI;
3133 } else if (HasSMUL_LOHI) {
3134 OpToUse = ISD::SMUL_LOHI;
3135 } else if (HasUMUL_LOHI) {
3136 OpToUse = ISD::UMUL_LOHI;
3137 }
3138 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003139 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003140 break;
3141 }
3142 }
3143 if (Node->getOpcode() == ISD::MULHS &&
3144 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003145 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3146 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003147 break;
3148 }
3149 if (Node->getOpcode() == ISD::MULHU &&
3150 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003151 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3152 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003153 break;
3154 }
3155 if (Node->getOpcode() == ISD::SDIV &&
3156 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003157 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3158 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003159 break;
3160 }
3161 if (Node->getOpcode() == ISD::UDIV &&
3162 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003163 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3164 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003165 break;
3166 }
Mon P Wang0c397192008-10-30 08:01:45 +00003167
Dan Gohman82669522007-10-11 23:57:53 +00003168 // Check to see if we have a libcall for this operator.
3169 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3170 bool isSigned = false;
3171 switch (Node->getOpcode()) {
3172 case ISD::UDIV:
3173 case ISD::SDIV:
3174 if (VT == MVT::i32) {
3175 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003176 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003177 isSigned = Node->getOpcode() == ISD::SDIV;
3178 }
3179 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003180 case ISD::MUL:
3181 if (VT == MVT::i32)
3182 LC = RTLIB::MUL_I32;
3183 break;
Dan Gohman82669522007-10-11 23:57:53 +00003184 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003185 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3186 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003187 break;
3188 default: break;
3189 }
3190 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003191 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003192 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003193 break;
3194 }
Mon P Wang0c397192008-10-30 08:01:45 +00003195
Duncan Sands83ec4b62008-06-06 12:08:01 +00003196 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003197 "Cannot expand this binary operator!");
3198 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003199 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003200 break;
3201 }
Evan Chengcc987612006-04-12 21:20:24 +00003202 case TargetLowering::Promote: {
3203 switch (Node->getOpcode()) {
3204 default: assert(0 && "Do not know how to promote this BinOp!");
3205 case ISD::AND:
3206 case ISD::OR:
3207 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003208 MVT OVT = Node->getValueType(0);
3209 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3210 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003211 // Bit convert each of the values to the new type.
3212 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3213 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3214 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3215 // Bit convert the result back the original type.
3216 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3217 break;
3218 }
3219 }
3220 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003221 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003222 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003223
Dan Gohmane14ea862007-10-05 14:17:22 +00003224 case ISD::SMUL_LOHI:
3225 case ISD::UMUL_LOHI:
3226 case ISD::SDIVREM:
3227 case ISD::UDIVREM:
3228 // These nodes will only be produced by target-specific lowering, so
3229 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003230 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003231 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003232
3233 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3234 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3235 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003236 break;
3237
Chris Lattnera09f8482006-03-05 05:09:38 +00003238 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3239 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3240 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3241 case Expand: assert(0 && "Not possible");
3242 case Legal:
3243 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3244 break;
3245 case Promote:
3246 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3247 break;
3248 }
3249
3250 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3251
3252 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3253 default: assert(0 && "Operation not supported");
3254 case TargetLowering::Custom:
3255 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003256 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003257 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003258 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003259 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003260 // If this target supports fabs/fneg natively and select is cheap,
3261 // do this efficiently.
3262 if (!TLI.isSelectExpensive() &&
3263 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3264 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003265 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003266 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003267 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003268 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003269 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003270 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003271 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003272 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3273 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003274 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003275 // Select between the nabs and abs value based on the sign bit of
3276 // the input.
3277 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3278 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3279 AbsVal),
3280 AbsVal);
3281 Result = LegalizeOp(Result);
3282 break;
3283 }
3284
3285 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003286 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003287 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3288 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3289 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3290 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003291 break;
3292 }
Evan Cheng912095b2007-01-04 21:56:39 +00003293 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003294 break;
3295
Nate Begeman551bf3f2006-02-17 05:43:56 +00003296 case ISD::ADDC:
3297 case ISD::SUBC:
3298 Tmp1 = LegalizeOp(Node->getOperand(0));
3299 Tmp2 = LegalizeOp(Node->getOperand(1));
3300 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3301 // Since this produces two values, make sure to remember that we legalized
3302 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003303 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3304 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Nate Begeman551bf3f2006-02-17 05:43:56 +00003305 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003306
Nate Begeman551bf3f2006-02-17 05:43:56 +00003307 case ISD::ADDE:
3308 case ISD::SUBE:
3309 Tmp1 = LegalizeOp(Node->getOperand(0));
3310 Tmp2 = LegalizeOp(Node->getOperand(1));
3311 Tmp3 = LegalizeOp(Node->getOperand(2));
3312 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3313 // Since this produces two values, make sure to remember that we legalized
3314 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003315 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3316 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Nate Begeman551bf3f2006-02-17 05:43:56 +00003317 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003318
Nate Begeman419f8b62005-10-18 00:27:41 +00003319 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003320 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003321 // TODO: handle the case where the Lo and Hi operands are not of legal type
3322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3323 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3324 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003325 case TargetLowering::Promote:
3326 case TargetLowering::Custom:
3327 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003328 case TargetLowering::Legal:
3329 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3330 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3331 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003332 case TargetLowering::Expand:
3333 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3334 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3335 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003336 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003337 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003338 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003339 break;
3340 }
3341 break;
3342 }
3343
Nate Begemanc105e192005-04-06 00:23:54 +00003344 case ISD::UREM:
3345 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003346 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003347 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3348 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003349
Nate Begemanc105e192005-04-06 00:23:54 +00003350 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003351 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3352 case TargetLowering::Custom:
3353 isCustom = true;
3354 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003355 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003356 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003357 if (isCustom) {
3358 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003359 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003360 }
Nate Begemanc105e192005-04-06 00:23:54 +00003361 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003362 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003363 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003364 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003365 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003366
3367 // See if remainder can be lowered using two-result operations.
3368 SDVTList VTs = DAG.getVTList(VT, VT);
3369 if (Node->getOpcode() == ISD::SREM &&
3370 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003371 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003372 break;
3373 }
3374 if (Node->getOpcode() == ISD::UREM &&
3375 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003376 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003377 break;
3378 }
3379
Duncan Sands83ec4b62008-06-06 12:08:01 +00003380 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003381 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003382 TargetLowering::Legal) {
3383 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003384 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003385 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3386 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003387 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003388 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003389 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003390 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003391 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003392 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3393 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003394 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003395 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003396 }
Dan Gohman0d974262007-11-06 22:11:54 +00003397 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003398 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003399 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003400 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003401 Result = LegalizeOp(UnrollVectorOp(Op));
3402 } else {
3403 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003404 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3405 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003406 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003407 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003408 }
Nate Begemanc105e192005-04-06 00:23:54 +00003409 }
3410 break;
3411 }
Dan Gohman525178c2007-10-08 18:33:35 +00003412 }
Nate Begemanc105e192005-04-06 00:23:54 +00003413 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003414 case ISD::VAARG: {
3415 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3416 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3417
Duncan Sands83ec4b62008-06-06 12:08:01 +00003418 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003419 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3420 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003421 case TargetLowering::Custom:
3422 isCustom = true;
3423 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003424 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003425 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3426 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003427 Tmp1 = Result.getValue(1);
3428
3429 if (isCustom) {
3430 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003431 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003432 Result = LegalizeOp(Tmp2);
3433 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3434 }
3435 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003436 break;
3437 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003438 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003439 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003440 // Increment the pointer, VAList, to the next vaarg
3441 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003442 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begemanacc398c2006-01-25 18:21:52 +00003443 TLI.getPointerTy()));
3444 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003445 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003446 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003447 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003448 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003449 Result = LegalizeOp(Result);
3450 break;
3451 }
3452 }
3453 // Since VAARG produces two values, make sure to remember that we
3454 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003455 AddLegalizedOperand(SDValue(Node, 0), Result);
3456 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003457 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003458 }
3459
3460 case ISD::VACOPY:
3461 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3462 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3463 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3464
3465 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3466 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003467 case TargetLowering::Custom:
3468 isCustom = true;
3469 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003470 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003471 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3472 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003473 if (isCustom) {
3474 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003475 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003476 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003477 break;
3478 case TargetLowering::Expand:
3479 // This defaults to loading a pointer from the input and storing it to the
3480 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003481 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3482 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003483 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3484 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003485 break;
3486 }
3487 break;
3488
3489 case ISD::VAEND:
3490 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3491 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3492
3493 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3494 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003495 case TargetLowering::Custom:
3496 isCustom = true;
3497 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003498 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003499 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003500 if (isCustom) {
3501 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003502 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003503 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003504 break;
3505 case TargetLowering::Expand:
3506 Result = Tmp1; // Default to a no-op, return the chain
3507 break;
3508 }
3509 break;
3510
3511 case ISD::VASTART:
3512 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3513 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3514
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003515 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3516
Nate Begemanacc398c2006-01-25 18:21:52 +00003517 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3518 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003519 case TargetLowering::Legal: break;
3520 case TargetLowering::Custom:
3521 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003522 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003523 break;
3524 }
3525 break;
3526
Nate Begeman35ef9132006-01-11 21:21:00 +00003527 case ISD::ROTL:
3528 case ISD::ROTR:
3529 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3530 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003531 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003532 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3533 default:
3534 assert(0 && "ROTL/ROTR legalize operation not supported");
3535 break;
3536 case TargetLowering::Legal:
3537 break;
3538 case TargetLowering::Custom:
3539 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003540 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003541 break;
3542 case TargetLowering::Promote:
3543 assert(0 && "Do not know how to promote ROTL/ROTR");
3544 break;
3545 case TargetLowering::Expand:
3546 assert(0 && "Do not know how to expand ROTL/ROTR");
3547 break;
3548 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003549 break;
3550
Nate Begemand88fc032006-01-14 03:14:10 +00003551 case ISD::BSWAP:
3552 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3553 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003554 case TargetLowering::Custom:
3555 assert(0 && "Cannot custom legalize this yet!");
3556 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003557 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003558 break;
3559 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003560 MVT OVT = Tmp1.getValueType();
3561 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3562 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003563
Chris Lattner456a93a2006-01-28 07:39:30 +00003564 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3565 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3566 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3567 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3568 break;
3569 }
3570 case TargetLowering::Expand:
3571 Result = ExpandBSWAP(Tmp1);
3572 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003573 }
3574 break;
3575
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003576 case ISD::CTPOP:
3577 case ISD::CTTZ:
3578 case ISD::CTLZ:
3579 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3580 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003581 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003582 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003583 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003584 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003585 TargetLowering::Custom) {
3586 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003587 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003588 Result = Tmp1;
3589 }
Scott Michel910b66d2007-07-30 21:00:31 +00003590 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003591 break;
3592 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003593 MVT OVT = Tmp1.getValueType();
3594 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003595
3596 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003597 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3598 // Perform the larger operation, then subtract if needed.
3599 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003600 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003601 case ISD::CTPOP:
3602 Result = Tmp1;
3603 break;
3604 case ISD::CTTZ:
3605 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003606 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003607 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003608 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003609 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003610 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003611 break;
3612 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003613 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003614 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003615 DAG.getConstant(NVT.getSizeInBits() -
3616 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003617 break;
3618 }
3619 break;
3620 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003621 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003622 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003623 break;
3624 }
3625 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003626
Chris Lattner2c8086f2005-04-02 05:00:07 +00003627 // Unary operators
3628 case ISD::FABS:
3629 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003630 case ISD::FSQRT:
3631 case ISD::FSIN:
3632 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003633 case ISD::FLOG:
3634 case ISD::FLOG2:
3635 case ISD::FLOG10:
3636 case ISD::FEXP:
3637 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003638 case ISD::FTRUNC:
3639 case ISD::FFLOOR:
3640 case ISD::FCEIL:
3641 case ISD::FRINT:
3642 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003643 Tmp1 = LegalizeOp(Node->getOperand(0));
3644 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003645 case TargetLowering::Promote:
3646 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003647 isCustom = true;
3648 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003649 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003650 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003651 if (isCustom) {
3652 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003653 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003654 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003655 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003656 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003657 switch (Node->getOpcode()) {
3658 default: assert(0 && "Unreachable!");
3659 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003660 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3661 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003662 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003663 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003664 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003665 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003666 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003667 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003668 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003669 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003670 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3671 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003672 break;
3673 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003674 case ISD::FSQRT:
3675 case ISD::FSIN:
3676 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003677 case ISD::FLOG:
3678 case ISD::FLOG2:
3679 case ISD::FLOG10:
3680 case ISD::FEXP:
3681 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003682 case ISD::FTRUNC:
3683 case ISD::FFLOOR:
3684 case ISD::FCEIL:
3685 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003686 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003687 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003688
3689 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003690 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003691 Result = LegalizeOp(UnrollVectorOp(Op));
3692 break;
3693 }
3694
Evan Cheng56966222007-01-12 02:11:51 +00003695 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003696 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003697 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003698 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3699 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003700 break;
3701 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003702 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3703 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003704 break;
3705 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003706 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3707 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003708 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003709 case ISD::FLOG:
3710 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3711 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3712 break;
3713 case ISD::FLOG2:
3714 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3715 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3716 break;
3717 case ISD::FLOG10:
3718 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3719 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3720 break;
3721 case ISD::FEXP:
3722 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3723 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3724 break;
3725 case ISD::FEXP2:
3726 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3727 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3728 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003729 case ISD::FTRUNC:
3730 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3731 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3732 break;
3733 case ISD::FFLOOR:
3734 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3735 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3736 break;
3737 case ISD::FCEIL:
3738 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3739 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3740 break;
3741 case ISD::FRINT:
3742 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3743 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3744 break;
3745 case ISD::FNEARBYINT:
3746 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3747 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3748 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003749 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003750 default: assert(0 && "Unreachable!");
3751 }
Dan Gohman475871a2008-07-27 21:46:04 +00003752 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003753 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003754 break;
3755 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003756 }
3757 break;
3758 }
3759 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003760 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003761 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003762
3763 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003764 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003765 Result = LegalizeOp(UnrollVectorOp(Op));
3766 break;
3767 }
3768
3769 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003770 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3771 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003772 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003773 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003774 break;
3775 }
Chris Lattner35481892005-12-23 00:16:34 +00003776 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003777 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003778 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3779 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003780 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003781 // The input has to be a vector type, we have to either scalarize it, pack
3782 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003783 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003784 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003785 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3786 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003787
3788 // Figure out if there is a simple type corresponding to this Vector
3789 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003790 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003791 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003792 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003793 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3794 LegalizeOp(Node->getOperand(0)));
3795 break;
3796 } else if (NumElems == 1) {
3797 // Turn this into a bit convert of the scalar input.
3798 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3799 ScalarizeVectorOp(Node->getOperand(0)));
3800 break;
3801 } else {
3802 // FIXME: UNIMP! Store then reload
3803 assert(0 && "Cast from unsupported vector type not implemented yet!");
3804 }
Chris Lattner67993f72006-01-23 07:30:46 +00003805 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003806 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3807 Node->getOperand(0).getValueType())) {
3808 default: assert(0 && "Unknown operation action!");
3809 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003810 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3811 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003812 break;
3813 case TargetLowering::Legal:
3814 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003815 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003816 break;
3817 }
3818 }
3819 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003820
Chris Lattner2c8086f2005-04-02 05:00:07 +00003821 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003822 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003823 case ISD::UINT_TO_FP: {
3824 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003825 Result = LegalizeINT_TO_FP(Result, isSigned,
3826 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003827 break;
3828 }
3829 case ISD::TRUNCATE:
3830 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3831 case Legal:
3832 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003833 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003834 break;
3835 case Expand:
3836 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3837
3838 // Since the result is legal, we should just be able to truncate the low
3839 // part of the source.
3840 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3841 break;
3842 case Promote:
3843 Result = PromoteOp(Node->getOperand(0));
3844 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3845 break;
3846 }
3847 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003848
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003849 case ISD::FP_TO_SINT:
3850 case ISD::FP_TO_UINT:
3851 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3852 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003853 Tmp1 = LegalizeOp(Node->getOperand(0));
3854
Chris Lattner1618beb2005-07-29 00:11:56 +00003855 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3856 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003857 case TargetLowering::Custom:
3858 isCustom = true;
3859 // FALLTHROUGH
3860 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003861 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003862 if (isCustom) {
3863 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003864 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003865 }
3866 break;
3867 case TargetLowering::Promote:
3868 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3869 Node->getOpcode() == ISD::FP_TO_SINT);
3870 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003871 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003872 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00003873 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003874 MVT VT = Node->getOperand(0).getValueType();
3875 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003876 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00003877 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3878 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003879 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003880 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003881 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00003882 Node->getOperand(0), Tmp2, ISD::SETLT);
3883 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3884 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003885 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003886 Tmp2));
3887 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003888 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003889 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3890 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003891 } else {
3892 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3893 }
3894 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003895 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003896 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003897 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003898 MVT VT = Op.getValueType();
3899 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003900 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003901 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003902 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3903 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3904 Node->getOperand(0), DAG.getValueType(MVT::f64));
3905 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3906 DAG.getIntPtrConstant(1));
3907 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3908 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003909 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3910 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3911 Tmp2 = DAG.getConstantFP(apf, OVT);
3912 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3913 // FIXME: generated code sucks.
3914 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3915 DAG.getNode(ISD::ADD, MVT::i32,
3916 DAG.getNode(ISD::FP_TO_SINT, VT,
3917 DAG.getNode(ISD::FSUB, OVT,
3918 Node->getOperand(0), Tmp2)),
3919 DAG.getConstant(0x80000000, MVT::i32)),
3920 DAG.getNode(ISD::FP_TO_SINT, VT,
3921 Node->getOperand(0)),
3922 DAG.getCondCode(ISD::SETGE));
3923 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003924 break;
3925 }
Dan Gohmana2e94852008-03-10 23:03:31 +00003926 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00003927 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3928 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3929 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00003930 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003931 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003932 break;
3933 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003934 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003935 Tmp1 = PromoteOp(Node->getOperand(0));
3936 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3937 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003938 break;
3939 }
3940 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003941
Chris Lattnerf2670a82008-01-16 06:57:07 +00003942 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003943 MVT DstVT = Op.getValueType();
3944 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003945 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3946 // The only other way we can lower this is to turn it into a STORE,
3947 // LOAD pair, targetting a temporary location (a stack slot).
3948 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3949 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003950 }
3951 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3952 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3953 case Legal:
3954 Tmp1 = LegalizeOp(Node->getOperand(0));
3955 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3956 break;
3957 case Promote:
3958 Tmp1 = PromoteOp(Node->getOperand(0));
3959 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3960 break;
3961 }
3962 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003963 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003964 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003965 MVT DstVT = Op.getValueType();
3966 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003967 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3968 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00003969 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003970 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003971 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003972 if (DstVT!=MVT::f64)
3973 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003974 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003975 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003976 // The only other way we can lower this is to turn it into a STORE,
3977 // LOAD pair, targetting a temporary location (a stack slot).
3978 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3979 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003980 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003981 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3982 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3983 case Legal:
3984 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003985 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003986 break;
3987 case Promote:
3988 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003989 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3990 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003991 break;
3992 }
3993 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003994 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003995 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003996 case ISD::ZERO_EXTEND:
3997 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003998 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003999 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004000 case Legal:
4001 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004002 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004003 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4004 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004005 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004006 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004007 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004008 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004009 case Promote:
4010 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004011 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004012 Tmp1 = PromoteOp(Node->getOperand(0));
4013 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004014 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004015 case ISD::ZERO_EXTEND:
4016 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004017 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004018 Result = DAG.getZeroExtendInReg(Result,
4019 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004020 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004021 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004022 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004023 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004024 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004025 Result,
4026 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004027 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004028 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004029 }
4030 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004031 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004032 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004033 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004034 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004035
4036 // If this operation is not supported, convert it to a shl/shr or load/store
4037 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004038 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4039 default: assert(0 && "This action not supported for this op yet!");
4040 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004041 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004042 break;
4043 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004044 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004045 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004046 // NOTE: we could fall back on load/store here too for targets without
4047 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004048 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4049 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004050 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004051 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4052 Node->getOperand(0), ShiftCst);
4053 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4054 Result, ShiftCst);
4055 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004056 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004057 // EXTLOAD pair, targetting a temporary location (a stack slot).
4058
4059 // NOTE: there is a choice here between constantly creating new stack
4060 // slots and always reusing the same one. We currently always create
4061 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004062 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4063 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004064 } else {
4065 assert(0 && "Unknown op");
4066 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004067 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004068 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004069 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004070 }
Duncan Sands36397f52007-07-27 12:58:54 +00004071 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004072 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004073 for (unsigned i = 0; i != 6; ++i)
4074 Ops[i] = LegalizeOp(Node->getOperand(i));
4075 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4076 // The only option for this node is to custom lower it.
4077 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004078 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004079
4080 // Since trampoline produces two values, make sure to remember that we
4081 // legalized both of them.
4082 Tmp1 = LegalizeOp(Result.getValue(1));
4083 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004084 AddLegalizedOperand(SDValue(Node, 0), Result);
4085 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004086 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004087 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004088 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004089 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004090 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4091 default: assert(0 && "This action not supported for this op yet!");
4092 case TargetLowering::Custom:
4093 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004094 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004095 // Fall Thru
4096 case TargetLowering::Legal:
4097 // If this operation is not supported, lower it to constant 1
4098 Result = DAG.getConstant(1, VT);
4099 break;
4100 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004101 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004102 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004103 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004104 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004105 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4106 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004107 case TargetLowering::Legal:
4108 Tmp1 = LegalizeOp(Node->getOperand(0));
4109 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4110 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004111 case TargetLowering::Custom:
4112 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004113 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004114 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004115 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004116 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004117 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004118 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004119 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004120 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004121 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004122 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner034f12e2008-01-15 22:09:33 +00004123 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004124 Result = CallResult.second;
4125 break;
4126 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004127 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004128 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00004129 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004130
Chris Lattner4ddd2832006-04-08 04:13:17 +00004131 assert(Result.getValueType() == Op.getValueType() &&
4132 "Bad legalization!");
4133
Chris Lattner456a93a2006-01-28 07:39:30 +00004134 // Make sure that the generated code is itself legal.
4135 if (Result != Op)
4136 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004137
Chris Lattner45982da2005-05-12 16:53:42 +00004138 // Note that LegalizeOp may be reentered even from single-use nodes, which
4139 // means that we always must cache transformed nodes.
4140 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004141 return Result;
4142}
4143
Chris Lattner8b6fa222005-01-15 22:16:26 +00004144/// PromoteOp - Given an operation that produces a value in an invalid type,
4145/// promote it to compute the value into a larger type. The produced value will
4146/// have the correct bits for the low portion of the register, but no guarantee
4147/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004148SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004149 MVT VT = Op.getValueType();
4150 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004151 assert(getTypeAction(VT) == Promote &&
4152 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004153 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004154 "Cannot promote to smaller type!");
4155
Dan Gohman475871a2008-07-27 21:46:04 +00004156 SDValue Tmp1, Tmp2, Tmp3;
4157 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004158 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004159
Dan Gohman475871a2008-07-27 21:46:04 +00004160 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004161 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004162
Chris Lattner03c85462005-01-15 05:21:40 +00004163 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004164 case ISD::CopyFromReg:
4165 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004166 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004167#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004168 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004169#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004170 assert(0 && "Do not know how to promote this operator!");
4171 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004172 case ISD::UNDEF:
4173 Result = DAG.getNode(ISD::UNDEF, NVT);
4174 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004175 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004176 if (VT != MVT::i1)
4177 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4178 else
4179 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004180 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4181 break;
4182 case ISD::ConstantFP:
4183 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4184 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4185 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004186
Chris Lattner82fbfb62005-01-18 02:59:52 +00004187 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004188 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004189 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004190 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004191 TLI.getSetCCResultType(Node->getOperand(0)),
4192 Node->getOperand(0), Node->getOperand(1),
4193 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004194 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004195
Chris Lattner03c85462005-01-15 05:21:40 +00004196 case ISD::TRUNCATE:
4197 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4198 case Legal:
4199 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004200 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004201 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004202 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004203 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4204 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004205 case Promote:
4206 // The truncation is not required, because we don't guarantee anything
4207 // about high bits anyway.
4208 Result = PromoteOp(Node->getOperand(0));
4209 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004210 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004211 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4212 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004213 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004214 }
4215 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004216 case ISD::SIGN_EXTEND:
4217 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004218 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004219 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4220 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4221 case Legal:
4222 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004223 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004224 break;
4225 case Promote:
4226 // Promote the reg if it's smaller.
4227 Result = PromoteOp(Node->getOperand(0));
4228 // The high bits are not guaranteed to be anything. Insert an extend.
4229 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004230 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004231 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004232 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004233 Result = DAG.getZeroExtendInReg(Result,
4234 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004235 break;
4236 }
4237 break;
Chris Lattner35481892005-12-23 00:16:34 +00004238 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004239 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4240 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004241 Result = PromoteOp(Result);
4242 break;
4243
Chris Lattner8b6fa222005-01-15 22:16:26 +00004244 case ISD::FP_EXTEND:
4245 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4246 case ISD::FP_ROUND:
4247 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4248 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4249 case Promote: assert(0 && "Unreachable with 2 FP types!");
4250 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004251 if (Node->getConstantOperandVal(1) == 0) {
4252 // Input is legal? Do an FP_ROUND_INREG.
4253 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4254 DAG.getValueType(VT));
4255 } else {
4256 // Just remove the truncate, it isn't affecting the value.
4257 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4258 Node->getOperand(1));
4259 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004260 break;
4261 }
4262 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004263 case ISD::SINT_TO_FP:
4264 case ISD::UINT_TO_FP:
4265 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4266 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004267 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004268 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004269 break;
4270
4271 case Promote:
4272 Result = PromoteOp(Node->getOperand(0));
4273 if (Node->getOpcode() == ISD::SINT_TO_FP)
4274 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004275 Result,
4276 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004277 else
Chris Lattner23993562005-04-13 02:38:47 +00004278 Result = DAG.getZeroExtendInReg(Result,
4279 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004280 // No extra round required here.
4281 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004282 break;
4283 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004284 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4285 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004286 // Round if we cannot tolerate excess precision.
4287 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004288 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4289 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004290 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004291 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004292 break;
4293
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004294 case ISD::SIGN_EXTEND_INREG:
4295 Result = PromoteOp(Node->getOperand(0));
4296 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4297 Node->getOperand(1));
4298 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004299 case ISD::FP_TO_SINT:
4300 case ISD::FP_TO_UINT:
4301 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4302 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004303 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004304 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004305 break;
4306 case Promote:
4307 // The input result is prerounded, so we don't have to do anything
4308 // special.
4309 Tmp1 = PromoteOp(Node->getOperand(0));
4310 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004311 }
Nate Begemand2558e32005-08-14 01:20:53 +00004312 // If we're promoting a UINT to a larger size, check to see if the new node
4313 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4314 // we can use that instead. This allows us to generate better code for
4315 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4316 // legal, such as PowerPC.
4317 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004318 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004319 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4320 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004321 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4322 } else {
4323 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4324 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004325 break;
4326
Chris Lattner2c8086f2005-04-02 05:00:07 +00004327 case ISD::FABS:
4328 case ISD::FNEG:
4329 Tmp1 = PromoteOp(Node->getOperand(0));
4330 assert(Tmp1.getValueType() == NVT);
4331 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4332 // NOTE: we do not have to do any extra rounding here for
4333 // NoExcessFPPrecision, because we know the input will have the appropriate
4334 // precision, and these operations don't modify precision at all.
4335 break;
4336
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004337 case ISD::FLOG:
4338 case ISD::FLOG2:
4339 case ISD::FLOG10:
4340 case ISD::FEXP:
4341 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004342 case ISD::FSQRT:
4343 case ISD::FSIN:
4344 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004345 case ISD::FTRUNC:
4346 case ISD::FFLOOR:
4347 case ISD::FCEIL:
4348 case ISD::FRINT:
4349 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004350 Tmp1 = PromoteOp(Node->getOperand(0));
4351 assert(Tmp1.getValueType() == NVT);
4352 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004353 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004354 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4355 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004356 break;
4357
Evan Cheng9d24ac52008-09-09 23:35:53 +00004358 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004359 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004360 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004361 // directly as well, which may be better.
4362 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004363 Tmp2 = Node->getOperand(1);
4364 if (Node->getOpcode() == ISD::FPOW)
4365 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004366 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004367 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004368 if (NoExcessFPPrecision)
4369 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4370 DAG.getValueType(VT));
4371 break;
4372 }
4373
Dale Johannesene00a8a22008-08-28 02:44:49 +00004374 case ISD::ATOMIC_CMP_SWAP_8:
4375 case ISD::ATOMIC_CMP_SWAP_16:
4376 case ISD::ATOMIC_CMP_SWAP_32:
4377 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004378 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004379 Tmp2 = PromoteOp(Node->getOperand(2));
4380 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang28873102008-06-25 08:15:39 +00004381 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4382 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004383 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004384 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004385 // Remember that we legalized the chain.
4386 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4387 break;
4388 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00004389 case ISD::ATOMIC_LOAD_ADD_8:
4390 case ISD::ATOMIC_LOAD_SUB_8:
4391 case ISD::ATOMIC_LOAD_AND_8:
4392 case ISD::ATOMIC_LOAD_OR_8:
4393 case ISD::ATOMIC_LOAD_XOR_8:
4394 case ISD::ATOMIC_LOAD_NAND_8:
4395 case ISD::ATOMIC_LOAD_MIN_8:
4396 case ISD::ATOMIC_LOAD_MAX_8:
4397 case ISD::ATOMIC_LOAD_UMIN_8:
4398 case ISD::ATOMIC_LOAD_UMAX_8:
4399 case ISD::ATOMIC_SWAP_8:
4400 case ISD::ATOMIC_LOAD_ADD_16:
4401 case ISD::ATOMIC_LOAD_SUB_16:
4402 case ISD::ATOMIC_LOAD_AND_16:
4403 case ISD::ATOMIC_LOAD_OR_16:
4404 case ISD::ATOMIC_LOAD_XOR_16:
4405 case ISD::ATOMIC_LOAD_NAND_16:
4406 case ISD::ATOMIC_LOAD_MIN_16:
4407 case ISD::ATOMIC_LOAD_MAX_16:
4408 case ISD::ATOMIC_LOAD_UMIN_16:
4409 case ISD::ATOMIC_LOAD_UMAX_16:
4410 case ISD::ATOMIC_SWAP_16:
4411 case ISD::ATOMIC_LOAD_ADD_32:
4412 case ISD::ATOMIC_LOAD_SUB_32:
4413 case ISD::ATOMIC_LOAD_AND_32:
4414 case ISD::ATOMIC_LOAD_OR_32:
4415 case ISD::ATOMIC_LOAD_XOR_32:
4416 case ISD::ATOMIC_LOAD_NAND_32:
4417 case ISD::ATOMIC_LOAD_MIN_32:
4418 case ISD::ATOMIC_LOAD_MAX_32:
4419 case ISD::ATOMIC_LOAD_UMIN_32:
4420 case ISD::ATOMIC_LOAD_UMAX_32:
4421 case ISD::ATOMIC_SWAP_32:
4422 case ISD::ATOMIC_LOAD_ADD_64:
4423 case ISD::ATOMIC_LOAD_SUB_64:
4424 case ISD::ATOMIC_LOAD_AND_64:
4425 case ISD::ATOMIC_LOAD_OR_64:
4426 case ISD::ATOMIC_LOAD_XOR_64:
4427 case ISD::ATOMIC_LOAD_NAND_64:
4428 case ISD::ATOMIC_LOAD_MIN_64:
4429 case ISD::ATOMIC_LOAD_MAX_64:
4430 case ISD::ATOMIC_LOAD_UMIN_64:
4431 case ISD::ATOMIC_LOAD_UMAX_64:
4432 case ISD::ATOMIC_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004433 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004434 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00004435 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4436 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004437 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004438 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004439 // Remember that we legalized the chain.
4440 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4441 break;
4442 }
4443
Chris Lattner03c85462005-01-15 05:21:40 +00004444 case ISD::AND:
4445 case ISD::OR:
4446 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004447 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004448 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004449 case ISD::MUL:
4450 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004451 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004452 // that too is okay if they are integer operations.
4453 Tmp1 = PromoteOp(Node->getOperand(0));
4454 Tmp2 = PromoteOp(Node->getOperand(1));
4455 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4456 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004457 break;
4458 case ISD::FADD:
4459 case ISD::FSUB:
4460 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004461 Tmp1 = PromoteOp(Node->getOperand(0));
4462 Tmp2 = PromoteOp(Node->getOperand(1));
4463 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4464 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4465
4466 // Floating point operations will give excess precision that we may not be
4467 // able to tolerate. If we DO allow excess precision, just leave it,
4468 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004469 // FIXME: Why would we need to round FP ops more than integer ones?
4470 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004471 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004472 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4473 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004474 break;
4475
Chris Lattner8b6fa222005-01-15 22:16:26 +00004476 case ISD::SDIV:
4477 case ISD::SREM:
4478 // These operators require that their input be sign extended.
4479 Tmp1 = PromoteOp(Node->getOperand(0));
4480 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004481 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004482 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4483 DAG.getValueType(VT));
4484 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4485 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004486 }
4487 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4488
4489 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004490 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004491 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4492 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004493 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004494 case ISD::FDIV:
4495 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004496 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004497 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004499 case Expand: assert(0 && "not implemented");
4500 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4501 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004502 }
4503 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004504 case Expand: assert(0 && "not implemented");
4505 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4506 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004507 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004508 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4509
4510 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004511 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004512 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4513 DAG.getValueType(VT));
4514 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004515
4516 case ISD::UDIV:
4517 case ISD::UREM:
4518 // These operators require that their input be zero extended.
4519 Tmp1 = PromoteOp(Node->getOperand(0));
4520 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004521 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004522 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4523 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004524 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4525 break;
4526
4527 case ISD::SHL:
4528 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004529 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004530 break;
4531 case ISD::SRA:
4532 // The input value must be properly sign extended.
4533 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004534 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4535 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004536 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004537 break;
4538 case ISD::SRL:
4539 // The input value must be properly zero extended.
4540 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004541 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004542 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004543 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004544
4545 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004546 Tmp1 = Node->getOperand(0); // Get the chain.
4547 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004548 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4549 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004550 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004551 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004552 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004553 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004554 // Increment the pointer, VAList, to the next vaarg
4555 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004556 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004557 TLI.getPointerTy()));
4558 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004559 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004560 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004561 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004562 }
4563 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004564 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004565 break;
4566
Evan Cheng466685d2006-10-09 20:57:25 +00004567 case ISD::LOAD: {
4568 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004569 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4570 ? ISD::EXTLOAD : LD->getExtensionType();
4571 Result = DAG.getExtLoad(ExtType, NVT,
4572 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004573 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004574 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004575 LD->isVolatile(),
4576 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004577 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004578 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004579 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004580 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004581 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004582 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4583 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004584
Duncan Sands83ec4b62008-06-06 12:08:01 +00004585 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004586 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004587 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4588 // Ensure that the resulting node is at least the same size as the operands'
4589 // value types, because we cannot assume that TLI.getSetCCValueType() is
4590 // constant.
4591 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004592 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004593 }
Nate Begeman9373a812005-08-10 20:51:12 +00004594 case ISD::SELECT_CC:
4595 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4596 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4597 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004598 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004599 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004600 case ISD::BSWAP:
4601 Tmp1 = Node->getOperand(0);
4602 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4603 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4604 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004605 DAG.getConstant(NVT.getSizeInBits() -
4606 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004607 TLI.getShiftAmountTy()));
4608 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004609 case ISD::CTPOP:
4610 case ISD::CTTZ:
4611 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004612 // Zero extend the argument
4613 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004614 // Perform the larger operation, then subtract if needed.
4615 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004616 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004617 case ISD::CTPOP:
4618 Result = Tmp1;
4619 break;
4620 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004621 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004622 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004623 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004624 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004625 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004626 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004627 break;
4628 case ISD::CTLZ:
4629 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004630 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004631 DAG.getConstant(NVT.getSizeInBits() -
4632 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004633 break;
4634 }
4635 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004636 case ISD::EXTRACT_SUBVECTOR:
4637 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004638 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004639 case ISD::EXTRACT_VECTOR_ELT:
4640 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4641 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004642 }
4643
Gabor Greifba36cb52008-08-28 21:40:38 +00004644 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004645
4646 // Make sure the result is itself legal.
4647 Result = LegalizeOp(Result);
4648
4649 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004650 AddPromotedOperand(Op, Result);
4651 return Result;
4652}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004653
Dan Gohman7f321562007-06-25 16:23:39 +00004654/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4655/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4656/// based on the vector type. The return type of this matches the element type
4657/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004658SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004659 // We know that operand #0 is the Vec vector. If the index is a constant
4660 // or if the invec is a supported hardware type, we can use it. Otherwise,
4661 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004662 SDValue Vec = Op.getOperand(0);
4663 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004664
Duncan Sands83ec4b62008-06-06 12:08:01 +00004665 MVT TVT = Vec.getValueType();
4666 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004667
Dan Gohman7f321562007-06-25 16:23:39 +00004668 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4669 default: assert(0 && "This action is not supported yet!");
4670 case TargetLowering::Custom: {
4671 Vec = LegalizeOp(Vec);
4672 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004673 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004674 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004675 return Tmp3;
4676 break;
4677 }
4678 case TargetLowering::Legal:
4679 if (isTypeLegal(TVT)) {
4680 Vec = LegalizeOp(Vec);
4681 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004682 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004683 }
4684 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004685 case TargetLowering::Promote:
4686 assert(TVT.isVector() && "not vector type");
4687 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004688 case TargetLowering::Expand:
4689 break;
4690 }
4691
4692 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004693 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004694 Op = ScalarizeVectorOp(Vec);
4695 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004696 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004697 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004698 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00004699 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004700 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004701 Vec = Lo;
4702 } else {
4703 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004704 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004705 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004706 }
Dan Gohman7f321562007-06-25 16:23:39 +00004707
Chris Lattner15972212006-03-31 17:55:51 +00004708 // It's now an extract from the appropriate high or low part. Recurse.
4709 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004710 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004711 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004712 // Store the value to a temporary stack slot, then LOAD the scalar
4713 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00004714 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4715 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00004716
4717 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004718 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00004719 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4720 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004721
Duncan Sands8e4eb092008-06-08 20:54:56 +00004722 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004723 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004724 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004725 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004726
Dan Gohman7f321562007-06-25 16:23:39 +00004727 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4728
4729 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004730 }
Dan Gohman7f321562007-06-25 16:23:39 +00004731 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004732}
4733
Dan Gohman7f321562007-06-25 16:23:39 +00004734/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004735/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004736SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004737 // We know that operand #0 is the Vec vector. For now we assume the index
4738 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00004739 SDValue Vec = Op.getOperand(0);
4740 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00004741
Duncan Sands83ec4b62008-06-06 12:08:01 +00004742 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00004743
Duncan Sands83ec4b62008-06-06 12:08:01 +00004744 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00004745 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004746 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004747 }
4748
4749 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004750 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00004751 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004752 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00004753 Vec = Lo;
4754 } else {
4755 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004756 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4757 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004758 }
4759
4760 // It's now an extract from the appropriate high or low part. Recurse.
4761 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004762 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004763}
4764
Nate Begeman750ac1b2006-02-01 07:19:44 +00004765/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4766/// with condition CC on the current target. This usually involves legalizing
4767/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4768/// there may be no choice but to create a new SetCC node to represent the
4769/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00004770/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4771void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4772 SDValue &RHS,
4773 SDValue &CC) {
4774 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004775
4776 switch (getTypeAction(LHS.getValueType())) {
4777 case Legal:
4778 Tmp1 = LegalizeOp(LHS); // LHS
4779 Tmp2 = LegalizeOp(RHS); // RHS
4780 break;
4781 case Promote:
4782 Tmp1 = PromoteOp(LHS); // LHS
4783 Tmp2 = PromoteOp(RHS); // RHS
4784
4785 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004786 if (LHS.getValueType().isInteger()) {
4787 MVT VT = LHS.getValueType();
4788 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004789
4790 // Otherwise, we have to insert explicit sign or zero extends. Note
4791 // that we could insert sign extends for ALL conditions, but zero extend
4792 // is cheaper on many machines (an AND instead of two shifts), so prefer
4793 // it.
4794 switch (cast<CondCodeSDNode>(CC)->get()) {
4795 default: assert(0 && "Unknown integer comparison!");
4796 case ISD::SETEQ:
4797 case ISD::SETNE:
4798 case ISD::SETUGE:
4799 case ISD::SETUGT:
4800 case ISD::SETULE:
4801 case ISD::SETULT:
4802 // ALL of these operations will work if we either sign or zero extend
4803 // the operands (including the unsigned comparisons!). Zero extend is
4804 // usually a simpler/cheaper operation, so prefer it.
4805 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4806 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4807 break;
4808 case ISD::SETGE:
4809 case ISD::SETGT:
4810 case ISD::SETLT:
4811 case ISD::SETLE:
4812 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4813 DAG.getValueType(VT));
4814 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4815 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00004816 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
4817 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00004818 break;
4819 }
4820 }
4821 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004822 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004823 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00004824 if (VT == MVT::f32 || VT == MVT::f64) {
4825 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00004826 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004827 switch (cast<CondCodeSDNode>(CC)->get()) {
4828 case ISD::SETEQ:
4829 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004830 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004831 break;
4832 case ISD::SETNE:
4833 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004834 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004835 break;
4836 case ISD::SETGE:
4837 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004838 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004839 break;
4840 case ISD::SETLT:
4841 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004842 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004843 break;
4844 case ISD::SETLE:
4845 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004846 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004847 break;
4848 case ISD::SETGT:
4849 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004850 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004851 break;
4852 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004853 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4854 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004855 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004856 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004857 break;
4858 default:
Evan Cheng56966222007-01-12 02:11:51 +00004859 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004860 switch (cast<CondCodeSDNode>(CC)->get()) {
4861 case ISD::SETONE:
4862 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004863 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004864 // Fallthrough
4865 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004866 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004867 break;
4868 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004869 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004870 break;
4871 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004872 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004873 break;
4874 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004875 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004876 break;
Evan Cheng56966222007-01-12 02:11:51 +00004877 case ISD::SETUEQ:
4878 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004879 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004880 default: assert(0 && "Unsupported FP setcc!");
4881 }
4882 }
Duncan Sandsf9516202008-06-30 10:19:09 +00004883
Dan Gohman475871a2008-07-27 21:46:04 +00004884 SDValue Dummy;
4885 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00004886 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00004887 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004888 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004889 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004890 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00004891 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00004892 CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00004893 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00004894 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004895 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004896 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004897 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00004898 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00004899 }
Evan Cheng21cacc42008-07-07 07:18:09 +00004900 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00004901 RHS = Tmp2;
4902 return;
4903 }
4904
Dan Gohman475871a2008-07-27 21:46:04 +00004905 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004906 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004907 ExpandOp(RHS, RHSLo, RHSHi);
4908 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4909
4910 if (VT==MVT::ppcf128) {
4911 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00004912 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00004913 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00004914 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00004915 // The following can be improved, but not that much.
Dale Johannesene2f20832008-09-12 00:30:56 +00004916 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
4917 ISD::SETOEQ);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004918 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004919 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesene2f20832008-09-12 00:30:56 +00004920 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
4921 ISD::SETUNE);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004922 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004923 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4924 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00004925 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00004926 break;
4927 }
4928
4929 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004930 case ISD::SETEQ:
4931 case ISD::SETNE:
4932 if (RHSLo == RHSHi)
4933 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4934 if (RHSCST->isAllOnesValue()) {
4935 // Comparison to -1.
4936 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4937 Tmp2 = RHSLo;
4938 break;
4939 }
4940
4941 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4942 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4943 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4944 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4945 break;
4946 default:
4947 // If this is a comparison of the sign bit, just look at the top part.
4948 // X > -1, x < 0
4949 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4950 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004951 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00004952 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4953 CST->isAllOnesValue())) { // X > -1
4954 Tmp1 = LHSHi;
4955 Tmp2 = RHSHi;
4956 break;
4957 }
4958
4959 // FIXME: This generated code sucks.
4960 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004961 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004962 default: assert(0 && "Unknown integer setcc!");
4963 case ISD::SETLT:
4964 case ISD::SETULT: LowCC = ISD::SETULT; break;
4965 case ISD::SETGT:
4966 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4967 case ISD::SETLE:
4968 case ISD::SETULE: LowCC = ISD::SETULE; break;
4969 case ISD::SETGE:
4970 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4971 }
4972
4973 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4974 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4975 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4976
4977 // NOTE: on targets without efficient SELECT of bools, we can always use
4978 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004979 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004980 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00004981 LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004982 if (!Tmp1.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00004983 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4984 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00004985 CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004986 if (!Tmp2.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00004987 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00004988 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004989
Gabor Greifba36cb52008-08-28 21:40:38 +00004990 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
4991 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00004992 if ((Tmp1C && Tmp1C->isNullValue()) ||
4993 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00004994 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4995 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00004996 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00004997 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4998 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4999 // low part is known false, returns high part.
5000 // For LE / GE, if high part is known false, ignore the low part.
5001 // For LT / GT, if high part is known true, ignore the low part.
5002 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005003 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005004 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005005 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005006 ISD::SETEQ, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005007 if (!Result.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005008 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005009 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005010 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5011 Result, Tmp1, Tmp2));
5012 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005013 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005014 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005015 }
5016 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005017 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005018 LHS = Tmp1;
5019 RHS = Tmp2;
5020}
5021
Evan Cheng7f042682008-10-15 02:05:31 +00005022/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5023/// condition code CC on the current target. This routine assumes LHS and rHS
5024/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5025/// illegal condition code into AND / OR of multiple SETCC values.
5026void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5027 SDValue &LHS, SDValue &RHS,
5028 SDValue &CC) {
5029 MVT OpVT = LHS.getValueType();
5030 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5031 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5032 default: assert(0 && "Unknown condition code action!");
5033 case TargetLowering::Legal:
5034 // Nothing to do.
5035 break;
5036 case TargetLowering::Expand: {
5037 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5038 unsigned Opc = 0;
5039 switch (CCCode) {
5040 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005041 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5042 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5043 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5044 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5045 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5046 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5047 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5048 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5049 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5050 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5051 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5052 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005053 // FIXME: Implement more expansions.
5054 }
5055
5056 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5057 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5058 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5059 RHS = SDValue();
5060 CC = SDValue();
5061 break;
5062 }
5063 }
5064}
5065
Chris Lattner1401d152008-01-16 07:45:30 +00005066/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5067/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5068/// a load from the stack slot to DestVT, extending it if needed.
5069/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005070SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5071 MVT SlotVT,
5072 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005073 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005074 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5075 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005076 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005077
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005078 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005079 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00005080
Duncan Sands83ec4b62008-06-06 12:08:01 +00005081 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5082 unsigned SlotSize = SlotVT.getSizeInBits();
5083 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005084 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5085 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005086
Chris Lattner1401d152008-01-16 07:45:30 +00005087 // Emit a store to the stack slot. Use a truncstore if the input value is
5088 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005089 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005090
Chris Lattner1401d152008-01-16 07:45:30 +00005091 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005092 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005093 PseudoSourceValue::getFixedStack(SPFI), 0,
5094 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005095 else {
5096 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005097 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005098 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00005099 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005100 }
5101
Chris Lattner35481892005-12-23 00:16:34 +00005102 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005103 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00005104 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005105
5106 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00005107 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5108 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005109}
5110
Dan Gohman475871a2008-07-27 21:46:04 +00005111SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005112 // Create a vector sized/aligned stack slot, store the value to element #0,
5113 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005114 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005115
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005116 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005117 int SPFI = StackPtrFI->getIndex();
5118
Dan Gohman475871a2008-07-27 21:46:04 +00005119 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005120 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005121 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005122 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005123}
5124
5125
Chris Lattnerce872152006-03-19 06:31:19 +00005126/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005127/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005128SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005129
5130 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005131 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005132 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005133 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005134 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005135
Dan Gohman475871a2008-07-27 21:46:04 +00005136 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005137 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005138 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005139 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005140 bool isConstant = true;
5141 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5142 SplatValue.getOpcode() != ISD::UNDEF)
5143 isConstant = false;
5144
Evan Cheng033e6812006-03-24 01:17:21 +00005145 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005146 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005147 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005148 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005149 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005150 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005151 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005152
5153 // If this isn't a constant element or an undef, we can't use a constant
5154 // pool load.
5155 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5156 V.getOpcode() != ISD::UNDEF)
5157 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005158 }
5159
5160 if (isOnlyLowElement) {
5161 // If the low element is an undef too, then this whole things is an undef.
5162 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5163 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5164 // Otherwise, turn this into a scalar_to_vector node.
5165 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5166 Node->getOperand(0));
5167 }
5168
Chris Lattner2eb86532006-03-24 07:29:17 +00005169 // If all elements are constants, create a load from the constant pool.
5170 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005171 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005172 std::vector<Constant*> CV;
5173 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5174 if (ConstantFPSDNode *V =
5175 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005176 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005177 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005178 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005179 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005180 } else {
5181 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005182 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005183 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005184 CV.push_back(UndefValue::get(OpNTy));
5185 }
5186 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005187 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005188 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005189 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005190 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005191 PseudoSourceValue::getConstantPool(), 0,
5192 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005193 }
5194
Gabor Greifba36cb52008-08-28 21:40:38 +00005195 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005196 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005197 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005198 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5199 std::vector<SDValue> ZeroVec(NumElems, Zero);
5200 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005201 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005202
5203 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005204 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005205 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005206 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005207 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5208
5209 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5210 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5211 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5212 SplatMask);
5213 }
5214 }
5215
Evan Cheng033e6812006-03-24 01:17:21 +00005216 // If there are only two unique elements, we may be able to turn this into a
5217 // vector shuffle.
5218 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005219 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005220 SDValue Val1 = Node->getOperand(1);
5221 SDValue Val2;
5222 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005223 if (MI->first != Val1)
5224 Val2 = MI->first;
5225 else
5226 Val2 = (++MI)->first;
5227
5228 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5229 // vector shuffle has the undef vector on the RHS.
5230 if (Val1.getOpcode() == ISD::UNDEF)
5231 std::swap(Val1, Val2);
5232
Evan Cheng033e6812006-03-24 01:17:21 +00005233 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005234 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5235 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005236 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005237
5238 // Set elements of the shuffle mask for Val1.
5239 std::vector<unsigned> &Val1Elts = Values[Val1];
5240 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5241 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5242
5243 // Set elements of the shuffle mask for Val2.
5244 std::vector<unsigned> &Val2Elts = Values[Val2];
5245 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5246 if (Val2.getOpcode() != ISD::UNDEF)
5247 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5248 else
5249 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5250
Dan Gohman475871a2008-07-27 21:46:04 +00005251 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005252 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005253
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005254 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005255 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5256 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005257 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5258 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005259 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005260
5261 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005262 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005263 }
5264 }
Chris Lattnerce872152006-03-19 06:31:19 +00005265
5266 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5267 // aligned object on the stack, store each element into it, then load
5268 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005269 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005270 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005271 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005272
5273 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005274 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005275 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005276 // Store (in the right endianness) the elements to memory.
5277 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5278 // Ignore undef elements.
5279 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5280
Chris Lattner841c8822006-03-22 01:46:54 +00005281 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005282
Dan Gohman475871a2008-07-27 21:46:04 +00005283 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005284 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5285
Evan Cheng786225a2006-10-05 23:01:46 +00005286 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005287 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005288 }
5289
Dan Gohman475871a2008-07-27 21:46:04 +00005290 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005291 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005292 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5293 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005294 else
5295 StoreChain = DAG.getEntryNode();
5296
5297 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005298 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005299}
5300
Chris Lattner5b359c62005-04-02 04:00:59 +00005301void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005302 SDValue Op, SDValue Amt,
5303 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005304 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005305 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005306 ExpandOp(Op, LHSL, LHSH);
5307
Dan Gohman475871a2008-07-27 21:46:04 +00005308 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005309 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005310 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005311 Hi = Lo.getValue(1);
5312}
5313
5314
Chris Lattnere34b3962005-01-19 04:19:40 +00005315/// ExpandShift - Try to find a clever way to expand this shift operation out to
5316/// smaller elements. If we can't find a way that is more efficient than a
5317/// libcall on this target, return false. Otherwise, return true with the
5318/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005319bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5320 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005321 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5322 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005323
Duncan Sands83ec4b62008-06-06 12:08:01 +00005324 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005325 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005326 MVT ShTy = ShAmt.getValueType();
5327 unsigned ShBits = ShTy.getSizeInBits();
5328 unsigned VTBits = Op.getValueType().getSizeInBits();
5329 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005330
Chris Lattner7a3c8552007-10-14 20:35:12 +00005331 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005332 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005333 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005334 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005335 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005336 ExpandOp(Op, InL, InH);
5337 switch(Opc) {
5338 case ISD::SHL:
5339 if (Cst > VTBits) {
5340 Lo = DAG.getConstant(0, NVT);
5341 Hi = DAG.getConstant(0, NVT);
5342 } else if (Cst > NVTBits) {
5343 Lo = DAG.getConstant(0, NVT);
5344 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005345 } else if (Cst == NVTBits) {
5346 Lo = DAG.getConstant(0, NVT);
5347 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005348 } else {
5349 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5350 Hi = DAG.getNode(ISD::OR, NVT,
5351 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5352 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5353 }
5354 return true;
5355 case ISD::SRL:
5356 if (Cst > VTBits) {
5357 Lo = DAG.getConstant(0, NVT);
5358 Hi = DAG.getConstant(0, NVT);
5359 } else if (Cst > NVTBits) {
5360 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5361 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005362 } else if (Cst == NVTBits) {
5363 Lo = InH;
5364 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005365 } else {
5366 Lo = DAG.getNode(ISD::OR, NVT,
5367 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5368 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5369 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5370 }
5371 return true;
5372 case ISD::SRA:
5373 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005374 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005375 DAG.getConstant(NVTBits-1, ShTy));
5376 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005377 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005378 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005379 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005380 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005381 } else if (Cst == NVTBits) {
5382 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005383 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005384 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005385 } else {
5386 Lo = DAG.getNode(ISD::OR, NVT,
5387 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5388 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5389 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5390 }
5391 return true;
5392 }
5393 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005394
5395 // Okay, the shift amount isn't constant. However, if we can tell that it is
5396 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005397 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5398 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005399 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005400
Dan Gohman9e255b72008-02-22 01:12:31 +00005401 // If we know that if any of the high bits of the shift amount are one, then
5402 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005403 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005404 // Mask out the high bit, which we know is set.
5405 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005406 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005407
5408 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005409 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005410 ExpandOp(Op, InL, InH);
5411 switch(Opc) {
5412 case ISD::SHL:
5413 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5414 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5415 return true;
5416 case ISD::SRL:
5417 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5418 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5419 return true;
5420 case ISD::SRA:
5421 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5422 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5423 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5424 return true;
5425 }
5426 }
5427
Dan Gohman9e255b72008-02-22 01:12:31 +00005428 // If we know that the high bits of the shift amount are all zero, then we can
5429 // do this as a couple of simple shifts.
5430 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005431 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005432 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005433 DAG.getConstant(NVTBits, Amt.getValueType()),
5434 Amt);
5435
5436 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005437 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005438 ExpandOp(Op, InL, InH);
5439 switch(Opc) {
5440 case ISD::SHL:
5441 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5442 Hi = DAG.getNode(ISD::OR, NVT,
5443 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5444 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5445 return true;
5446 case ISD::SRL:
5447 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5448 Lo = DAG.getNode(ISD::OR, NVT,
5449 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5450 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5451 return true;
5452 case ISD::SRA:
5453 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5454 Lo = DAG.getNode(ISD::OR, NVT,
5455 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5456 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5457 return true;
5458 }
5459 }
5460
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005461 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005462}
Chris Lattner77e77a62005-01-21 06:05:23 +00005463
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005464
Chris Lattner77e77a62005-01-21 06:05:23 +00005465// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5466// does not fit into a register, return the lo part and set the hi part to the
5467// by-reg argument. If it does fit into a single register, return the result
5468// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005469SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5470 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005471 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5472 // The input chain to this libcall is the entry node of the function.
5473 // Legalizing the call will automatically add the previous call to the
5474 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005475 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005476
Chris Lattner77e77a62005-01-21 06:05:23 +00005477 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005478 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005479 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005480 MVT ArgVT = Node->getOperand(i).getValueType();
5481 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005482 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005483 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005484 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005485 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005486 }
Bill Wendling056292f2008-09-16 21:48:12 +00005487 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005488 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005489
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005490 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005491 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005492 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005493 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5494 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005495
Chris Lattner6831a812006-02-13 09:18:02 +00005496 // Legalize the call sequence, starting with the chain. This will advance
5497 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5498 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5499 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005500 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005501 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005502 default: assert(0 && "Unknown thing");
5503 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005504 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005505 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005506 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005507 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005508 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005509 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005510 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005511}
5512
Dan Gohman7f8613e2008-08-14 20:04:46 +00005513/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5514///
5515SDValue SelectionDAGLegalize::
5516LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5517 bool isCustom = false;
5518 SDValue Tmp1;
5519 switch (getTypeAction(Op.getValueType())) {
5520 case Legal:
5521 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5522 Op.getValueType())) {
5523 default: assert(0 && "Unknown operation action!");
5524 case TargetLowering::Custom:
5525 isCustom = true;
5526 // FALLTHROUGH
5527 case TargetLowering::Legal:
5528 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005529 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005530 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5531 else
5532 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5533 DestTy, Tmp1);
5534 if (isCustom) {
5535 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005536 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005537 }
5538 break;
5539 case TargetLowering::Expand:
5540 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5541 break;
5542 case TargetLowering::Promote:
5543 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5544 break;
5545 }
5546 break;
5547 case Expand:
5548 Result = ExpandIntToFP(isSigned, DestTy, Op);
5549 break;
5550 case Promote:
5551 Tmp1 = PromoteOp(Op);
5552 if (isSigned) {
5553 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5554 Tmp1, DAG.getValueType(Op.getValueType()));
5555 } else {
5556 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5557 Op.getValueType());
5558 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005559 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005560 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5561 else
5562 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5563 DestTy, Tmp1);
5564 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5565 break;
5566 }
5567 return Result;
5568}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005569
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005570/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5571///
Dan Gohman475871a2008-07-27 21:46:04 +00005572SDValue SelectionDAGLegalize::
5573ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005574 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005575 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005576
Dan Gohman7f8613e2008-08-14 20:04:46 +00005577 // Expand unsupported int-to-fp vector casts by unrolling them.
5578 if (DestTy.isVector()) {
5579 if (!ExpandSource)
5580 return LegalizeOp(UnrollVectorOp(Source));
5581 MVT DestEltTy = DestTy.getVectorElementType();
5582 if (DestTy.getVectorNumElements() == 1) {
5583 SDValue Scalar = ScalarizeVectorOp(Source);
5584 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5585 DestEltTy, Scalar);
5586 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5587 }
5588 SDValue Lo, Hi;
5589 SplitVectorOp(Source, Lo, Hi);
5590 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5591 DestTy.getVectorNumElements() / 2);
5592 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5593 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengefa53392008-10-13 18:46:18 +00005594 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5595 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005596 }
5597
Evan Cheng9845eb52008-04-01 02:18:22 +00005598 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5599 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005600 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005601 // incoming integer is set. To handle this, we dynamically test to see if
5602 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005603 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005604 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005605 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005606 ExpandOp(Source, Lo, Hi);
5607 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5608 } else {
5609 // The comparison for the sign bit will use the entire operand.
5610 Hi = Source;
5611 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005612
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005613 // Check to see if the target has a custom way to lower this. If so, use it.
5614 // (Note we've already expanded the operand in this case.)
5615 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5616 default: assert(0 && "This action not implemented for this operation!");
5617 case TargetLowering::Legal:
5618 case TargetLowering::Expand:
5619 break; // This case is handled below.
5620 case TargetLowering::Custom: {
5621 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5622 Source), DAG);
5623 if (NV.getNode())
5624 return LegalizeOp(NV);
5625 break; // The target decided this was legal after all
5626 }
5627 }
5628
Chris Lattner66de05b2005-05-13 04:45:13 +00005629 // If this is unsigned, and not supported, first perform the conversion to
5630 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005631 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005632
Dan Gohman475871a2008-07-27 21:46:04 +00005633 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005634 DAG.getConstant(0, Hi.getValueType()),
5635 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005636 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5637 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005638 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005639 uint64_t FF = 0x5f800000ULL;
5640 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005641 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005642
Dan Gohman475871a2008-07-27 21:46:04 +00005643 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005644 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere9c35e72005-04-13 05:09:42 +00005645 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005646 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005647 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005648 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005649 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005650 PseudoSourceValue::getConstantPool(), 0,
5651 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005652 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005653 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005654 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005655 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005656 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005657 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005658 else
5659 assert(0 && "Unexpected conversion");
5660
Duncan Sands83ec4b62008-06-06 12:08:01 +00005661 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005662 if (SCVT != DestTy) {
5663 // Destination type needs to be expanded as well. The FADD now we are
5664 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005665 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5666 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005667 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005668 SignedConv, SignedConv.getValue(1));
5669 }
5670 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5671 }
Chris Lattner473a9902005-09-29 06:44:39 +00005672 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005673 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005674
Chris Lattnera88a2602005-05-14 05:33:54 +00005675 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005676 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005677 default: assert(0 && "This action not implemented for this operation!");
5678 case TargetLowering::Legal:
5679 case TargetLowering::Expand:
5680 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005681 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005682 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005683 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005684 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005685 return LegalizeOp(NV);
5686 break; // The target decided this was legal after all
5687 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005688 }
5689
Chris Lattner13689e22005-05-12 07:00:44 +00005690 // Expand the source, then glue it back together for the call. We must expand
5691 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005692 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005693 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005694 ExpandOp(Source, SrcLo, SrcHi);
5695 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5696 }
Chris Lattner13689e22005-05-12 07:00:44 +00005697
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005698 RTLIB::Libcall LC = isSigned ?
5699 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5700 RTLIB::getUINTTOFP(SourceVT, DestTy);
5701 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5702
Chris Lattner6831a812006-02-13 09:18:02 +00005703 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00005704 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00005705 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5706 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmana2e94852008-03-10 23:03:31 +00005707 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5708 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005709}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005710
Chris Lattner22cde6a2006-01-28 08:25:58 +00005711/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5712/// INT_TO_FP operation of the specified operand when the target requests that
5713/// we expand it. At this point, we know that the result and operand types are
5714/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00005715SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5716 SDValue Op0,
5717 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005718 if (Op0.getValueType() == MVT::i32) {
5719 // simple 32-bit [signed|unsigned] integer to float/double expansion
5720
Chris Lattner23594d42008-01-16 07:03:22 +00005721 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00005722 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00005723
Chris Lattner22cde6a2006-01-28 08:25:58 +00005724 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00005725 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00005726 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00005727 SDValue Hi = StackSlot;
5728 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00005729 if (TLI.isLittleEndian())
5730 std::swap(Hi, Lo);
5731
Chris Lattner22cde6a2006-01-28 08:25:58 +00005732 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00005733 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005734 if (isSigned) {
5735 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00005736 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005737 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5738 } else {
5739 Op0Mapped = Op0;
5740 }
5741 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00005742 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005743 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005744 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005745 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005746 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00005747 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005748 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005749 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005750 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00005751 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00005752 BitsToDouble(0x4330000080000000ULL)
5753 : BitsToDouble(0x4330000000000000ULL),
5754 MVT::f64);
5755 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00005756 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005757 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00005758 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005759 // handle final rounding
5760 if (DestVT == MVT::f64) {
5761 // do nothing
5762 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00005763 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005764 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5765 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005766 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005767 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005768 }
5769 return Result;
5770 }
5771 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00005772 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005773
Dan Gohman475871a2008-07-27 21:46:04 +00005774 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005775 DAG.getConstant(0, Op0.getValueType()),
5776 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005777 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5778 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005779 SignSet, Four, Zero);
5780
5781 // If the sign bit of the integer is set, the large number will be treated
5782 // as a negative number. To counteract this, the dynamic code adds an
5783 // offset depending on the data type.
5784 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005785 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005786 default: assert(0 && "Unsupported integer type!");
5787 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5788 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5789 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5790 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5791 }
5792 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005793 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005794
Dan Gohman475871a2008-07-27 21:46:04 +00005795 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005796 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005797 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005798 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005799 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005800 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005801 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005802 PseudoSourceValue::getConstantPool(), 0,
5803 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005804 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005805 FudgeInReg =
5806 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5807 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005808 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005809 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005810 }
5811
5812 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5813}
5814
5815/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5816/// *INT_TO_FP operation of the specified operand when the target requests that
5817/// we promote it. At this point, we know that the result and operand types are
5818/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5819/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00005820SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5821 MVT DestVT,
5822 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005823 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005824 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005825
5826 unsigned OpToUse = 0;
5827
5828 // Scan for the appropriate larger type to use.
5829 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005830 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5831 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005832
5833 // If the target supports SINT_TO_FP of this type, use it.
5834 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5835 default: break;
5836 case TargetLowering::Legal:
5837 if (!TLI.isTypeLegal(NewInTy))
5838 break; // Can't use this datatype.
5839 // FALL THROUGH.
5840 case TargetLowering::Custom:
5841 OpToUse = ISD::SINT_TO_FP;
5842 break;
5843 }
5844 if (OpToUse) break;
5845 if (isSigned) continue;
5846
5847 // If the target supports UINT_TO_FP of this type, use it.
5848 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5849 default: break;
5850 case TargetLowering::Legal:
5851 if (!TLI.isTypeLegal(NewInTy))
5852 break; // Can't use this datatype.
5853 // FALL THROUGH.
5854 case TargetLowering::Custom:
5855 OpToUse = ISD::UINT_TO_FP;
5856 break;
5857 }
5858 if (OpToUse) break;
5859
5860 // Otherwise, try a larger type.
5861 }
5862
5863 // Okay, we found the operation and type to use. Zero extend our input to the
5864 // desired type then run the operation on it.
5865 return DAG.getNode(OpToUse, DestVT,
5866 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5867 NewInTy, LegalOp));
5868}
5869
5870/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5871/// FP_TO_*INT operation of the specified operand when the target requests that
5872/// we promote it. At this point, we know that the result and operand types are
5873/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5874/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00005875SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
5876 MVT DestVT,
5877 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005878 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005879 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005880
5881 unsigned OpToUse = 0;
5882
5883 // Scan for the appropriate larger type to use.
5884 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005885 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5886 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005887
5888 // If the target supports FP_TO_SINT returning this type, use it.
5889 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5890 default: break;
5891 case TargetLowering::Legal:
5892 if (!TLI.isTypeLegal(NewOutTy))
5893 break; // Can't use this datatype.
5894 // FALL THROUGH.
5895 case TargetLowering::Custom:
5896 OpToUse = ISD::FP_TO_SINT;
5897 break;
5898 }
5899 if (OpToUse) break;
5900
5901 // If the target supports FP_TO_UINT of this type, use it.
5902 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5903 default: break;
5904 case TargetLowering::Legal:
5905 if (!TLI.isTypeLegal(NewOutTy))
5906 break; // Can't use this datatype.
5907 // FALL THROUGH.
5908 case TargetLowering::Custom:
5909 OpToUse = ISD::FP_TO_UINT;
5910 break;
5911 }
5912 if (OpToUse) break;
5913
5914 // Otherwise, try a larger type.
5915 }
5916
Chris Lattner27a6c732007-11-24 07:07:01 +00005917
5918 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00005919 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00005920
Chris Lattner27a6c732007-11-24 07:07:01 +00005921 // If the operation produces an invalid type, it must be custom lowered. Use
5922 // the target lowering hooks to expand it. Just keep the low part of the
5923 // expanded operation, we know that we're truncating anyway.
5924 if (getTypeAction(NewOutTy) == Expand) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005925 Operation = SDValue(TLI.ReplaceNodeResults(Operation.getNode(), DAG), 0);
5926 assert(Operation.getNode() && "Didn't return anything");
Chris Lattner27a6c732007-11-24 07:07:01 +00005927 }
Duncan Sands126d9072008-07-04 11:47:58 +00005928
Chris Lattner27a6c732007-11-24 07:07:01 +00005929 // Truncate the result of the extended FP_TO_*INT operation to the desired
5930 // size.
5931 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005932}
5933
5934/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5935///
Dan Gohman475871a2008-07-27 21:46:04 +00005936SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005937 MVT VT = Op.getValueType();
5938 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005939 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005940 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005941 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5942 case MVT::i16:
5943 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5944 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5945 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5946 case MVT::i32:
5947 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5948 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5949 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5950 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5951 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5952 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5953 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5954 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5955 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5956 case MVT::i64:
5957 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5958 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5959 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5960 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5961 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5962 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5963 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5964 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5965 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5966 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5967 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5968 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5969 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5970 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5971 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5972 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5973 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5974 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5975 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5976 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5977 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5978 }
5979}
5980
5981/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5982///
Dan Gohman475871a2008-07-27 21:46:04 +00005983SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005984 switch (Opc) {
5985 default: assert(0 && "Cannot expand this yet!");
5986 case ISD::CTPOP: {
5987 static const uint64_t mask[6] = {
5988 0x5555555555555555ULL, 0x3333333333333333ULL,
5989 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5990 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5991 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005992 MVT VT = Op.getValueType();
5993 MVT ShVT = TLI.getShiftAmountTy();
5994 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005995 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5996 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman475871a2008-07-27 21:46:04 +00005997 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
5998 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005999 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6000 DAG.getNode(ISD::AND, VT,
6001 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6002 }
6003 return Op;
6004 }
6005 case ISD::CTLZ: {
6006 // for now, we do this:
6007 // x = x | (x >> 1);
6008 // x = x | (x >> 2);
6009 // ...
6010 // x = x | (x >>16);
6011 // x = x | (x >>32); // for 64-bit input
6012 // return popcount(~x);
6013 //
6014 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006015 MVT VT = Op.getValueType();
6016 MVT ShVT = TLI.getShiftAmountTy();
6017 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006018 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006019 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006020 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6021 }
6022 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6023 return DAG.getNode(ISD::CTPOP, VT, Op);
6024 }
6025 case ISD::CTTZ: {
6026 // for now, we use: { return popcount(~x & (x - 1)); }
6027 // unless the target has ctlz but not ctpop, in which case we use:
6028 // { return 32 - nlz(~x & (x-1)); }
6029 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006030 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00006031 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6032 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006033 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6034 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6035 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6036 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6037 TLI.isOperationLegal(ISD::CTLZ, VT))
6038 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006039 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006040 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6041 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6042 }
6043 }
6044}
Chris Lattnere34b3962005-01-19 04:19:40 +00006045
Dan Gohman475871a2008-07-27 21:46:04 +00006046/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006047/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006048/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006049/// ExpandedNodes map is filled in for any results that are expanded, and the
6050/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006051void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006052 MVT VT = Op.getValueType();
6053 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006054 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006055 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006056 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006057 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006058
Chris Lattner6fdcb252005-09-02 20:32:45 +00006059 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006060 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006061 = ExpandedNodes.find(Op);
6062 if (I != ExpandedNodes.end()) {
6063 Lo = I->second.first;
6064 Hi = I->second.second;
6065 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006066 }
6067
Chris Lattner3e928bb2005-01-07 07:47:09 +00006068 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006069 case ISD::CopyFromReg:
6070 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006071 case ISD::FP_ROUND_INREG:
6072 if (VT == MVT::ppcf128 &&
6073 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6074 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006075 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006076 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6077 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006078 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006079 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006080 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6081 Lo = Result.getNode()->getOperand(0);
6082 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006083 break;
6084 }
6085 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006086 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006087#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006088 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006089#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006090 assert(0 && "Do not know how to expand this operator!");
6091 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006092 case ISD::EXTRACT_ELEMENT:
6093 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006094 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006095 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006096 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006097 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006098 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6099 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6100 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006101 case ISD::UNDEF:
6102 Lo = DAG.getNode(ISD::UNDEF, NVT);
6103 Hi = DAG.getNode(ISD::UNDEF, NVT);
6104 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006105 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006106 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006107 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6108 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6109 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006110 break;
6111 }
Evan Cheng00495212006-12-12 21:32:44 +00006112 case ISD::ConstantFP: {
6113 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006114 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006115 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006116 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6117 MVT::f64);
6118 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6119 MVT::f64);
6120 break;
6121 }
Evan Cheng279101e2006-12-12 22:19:28 +00006122 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006123 if (getTypeAction(Lo.getValueType()) == Expand)
6124 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006125 break;
6126 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006127 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006128 // Return the operands.
6129 Lo = Node->getOperand(0);
6130 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006131 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006132
6133 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006134 if (Node->getNumValues() == 1) {
6135 ExpandOp(Op.getOperand(0), Lo, Hi);
6136 break;
6137 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006138 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006139 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006140 Op.getValue(1).getValueType() == MVT::Other &&
6141 "unhandled MERGE_VALUES");
6142 ExpandOp(Op.getOperand(0), Lo, Hi);
6143 // Remember that we legalized the chain.
6144 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6145 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006146
6147 case ISD::SIGN_EXTEND_INREG:
6148 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006149 // sext_inreg the low part if needed.
6150 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6151
6152 // The high part gets the sign extension from the lo-part. This handles
6153 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006154 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006155 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006156 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006157 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006158
Nate Begemand88fc032006-01-14 03:14:10 +00006159 case ISD::BSWAP: {
6160 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006161 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006162 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6163 Lo = TempLo;
6164 break;
6165 }
6166
Chris Lattneredb1add2005-05-11 04:51:16 +00006167 case ISD::CTPOP:
6168 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006169 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6170 DAG.getNode(ISD::CTPOP, NVT, Lo),
6171 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006172 Hi = DAG.getConstant(0, NVT);
6173 break;
6174
Chris Lattner39a8f332005-05-12 19:05:01 +00006175 case ISD::CTLZ: {
6176 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006177 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006178 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6179 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6180 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006181 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006182 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006183 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6184
6185 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6186 Hi = DAG.getConstant(0, NVT);
6187 break;
6188 }
6189
6190 case ISD::CTTZ: {
6191 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006192 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006193 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6194 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6195 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006196 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006197 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006198 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6199
6200 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6201 Hi = DAG.getConstant(0, NVT);
6202 break;
6203 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006204
Nate Begemanacc398c2006-01-25 18:21:52 +00006205 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006206 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6207 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006208 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6209 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6210
6211 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006212 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006213 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006214 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006215 std::swap(Lo, Hi);
6216 break;
6217 }
6218
Chris Lattner3e928bb2005-01-07 07:47:09 +00006219 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006220 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006221 SDValue Ch = LD->getChain(); // Legalize the chain.
6222 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006223 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006224 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006225 int SVOffset = LD->getSrcValueOffset();
6226 unsigned Alignment = LD->getAlignment();
6227 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006228
Evan Cheng466685d2006-10-09 20:57:25 +00006229 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006230 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006231 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006232 if (VT == MVT::f32 || VT == MVT::f64) {
6233 // f32->i32 or f64->i64 one to one expansion.
6234 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006235 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006236 // Recursively expand the new load.
6237 if (getTypeAction(NVT) == Expand)
6238 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006239 break;
6240 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006241
Evan Cheng466685d2006-10-09 20:57:25 +00006242 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006243 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006244 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006245 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006246 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006247 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006248 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006249 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006250
Evan Cheng466685d2006-10-09 20:57:25 +00006251 // Build a factor node to remember that this load is independent of the
6252 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006253 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006254 Hi.getValue(1));
6255
6256 // Remember that we legalized the chain.
6257 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006258 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006259 std::swap(Lo, Hi);
6260 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006261 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006262
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006263 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6264 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006265 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006266 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006267 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006268 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006269 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006270 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6271 break;
6272 }
Evan Cheng466685d2006-10-09 20:57:25 +00006273
6274 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006275 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006276 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006277 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006278 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006279 SVOffset, EVT, isVolatile,
6280 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006281
6282 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006283 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006284
6285 if (ExtType == ISD::SEXTLOAD) {
6286 // The high part is obtained by SRA'ing all but one of the bits of the
6287 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006288 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006289 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6290 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6291 } else if (ExtType == ISD::ZEXTLOAD) {
6292 // The high part is just a zero.
6293 Hi = DAG.getConstant(0, NVT);
6294 } else /* if (ExtType == ISD::EXTLOAD) */ {
6295 // The high part is undefined.
6296 Hi = DAG.getNode(ISD::UNDEF, NVT);
6297 }
6298 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006299 break;
6300 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006301 case ISD::AND:
6302 case ISD::OR:
6303 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006304 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006305 ExpandOp(Node->getOperand(0), LL, LH);
6306 ExpandOp(Node->getOperand(1), RL, RH);
6307 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6308 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6309 break;
6310 }
6311 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006312 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006313 ExpandOp(Node->getOperand(1), LL, LH);
6314 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006315 if (getTypeAction(NVT) == Expand)
6316 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006317 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006318 if (VT != MVT::f32)
6319 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006320 break;
6321 }
Nate Begeman9373a812005-08-10 20:51:12 +00006322 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006323 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006324 ExpandOp(Node->getOperand(2), TL, TH);
6325 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006326 if (getTypeAction(NVT) == Expand)
6327 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006328 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6329 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006330 if (VT != MVT::f32)
6331 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6332 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006333 break;
6334 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006335 case ISD::ANY_EXTEND:
6336 // The low part is any extension of the input (which degenerates to a copy).
6337 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6338 // The high part is undefined.
6339 Hi = DAG.getNode(ISD::UNDEF, NVT);
6340 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006341 case ISD::SIGN_EXTEND: {
6342 // The low part is just a sign extension of the input (which degenerates to
6343 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006344 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006345
Chris Lattner3e928bb2005-01-07 07:47:09 +00006346 // The high part is obtained by SRA'ing all but one of the bits of the lo
6347 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006348 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006349 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6350 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006351 break;
6352 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006353 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006354 // The low part is just a zero extension of the input (which degenerates to
6355 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006356 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006357
Chris Lattner3e928bb2005-01-07 07:47:09 +00006358 // The high part is just a zero.
6359 Hi = DAG.getConstant(0, NVT);
6360 break;
Chris Lattner35481892005-12-23 00:16:34 +00006361
Chris Lattner4c948eb2007-02-13 23:55:16 +00006362 case ISD::TRUNCATE: {
6363 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006364 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006365 ExpandOp(Node->getOperand(0), NewLo, Hi);
6366
6367 // The low part is now either the right size, or it is closer. If not the
6368 // right size, make an illegal truncate so we recursively expand it.
6369 if (NewLo.getValueType() != Node->getValueType(0))
6370 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6371 ExpandOp(NewLo, Lo, Hi);
6372 break;
6373 }
6374
Chris Lattner35481892005-12-23 00:16:34 +00006375 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006376 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006377 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6378 // If the target wants to, allow it to lower this itself.
6379 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6380 case Expand: assert(0 && "cannot expand FP!");
6381 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6382 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6383 }
6384 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6385 }
6386
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006387 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006388 if (VT == MVT::f32 || VT == MVT::f64) {
6389 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006390 if (getTypeAction(NVT) == Expand)
6391 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006392 break;
6393 }
6394
6395 // If source operand will be expanded to the same type as VT, i.e.
6396 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006397 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006398 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6399 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006400 break;
6401 }
6402
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006403 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006404 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006405 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006406
Chris Lattner35481892005-12-23 00:16:34 +00006407 ExpandOp(Tmp, Lo, Hi);
6408 break;
6409 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006410
Chris Lattner27a6c732007-11-24 07:07:01 +00006411 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006412 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6413 TargetLowering::Custom &&
6414 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006415 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006416 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006417 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006418 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006419 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006420 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006421 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006422
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006423 case ISD::ATOMIC_CMP_SWAP_64: {
6424 // This operation does not need a loop.
6425 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6426 assert(Tmp.getNode() && "Node must be custom expanded!");
6427 ExpandOp(Tmp.getValue(0), Lo, Hi);
6428 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6429 LegalizeOp(Tmp.getValue(1)));
6430 break;
6431 }
6432
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006433 case ISD::ATOMIC_LOAD_ADD_64:
6434 case ISD::ATOMIC_LOAD_SUB_64:
6435 case ISD::ATOMIC_LOAD_AND_64:
6436 case ISD::ATOMIC_LOAD_OR_64:
6437 case ISD::ATOMIC_LOAD_XOR_64:
6438 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006439 case ISD::ATOMIC_SWAP_64: {
6440 // These operations require a loop to be generated. We can't do that yet,
6441 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006442 SDValue In2Lo, In2Hi, In2;
6443 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6444 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006445 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6446 SDValue Replace =
6447 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6448 Anode->getSrcValue(), Anode->getAlignment());
6449 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006450 ExpandOp(Result.getValue(0), Lo, Hi);
6451 // Remember that we legalized the chain.
6452 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006453 break;
6454 }
6455
Chris Lattner4e6c7462005-01-08 19:27:05 +00006456 // These operators cannot be expanded directly, emit them as calls to
6457 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006458 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006459 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006460 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006461 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6462 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006463 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6464 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006465 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006466
Chris Lattnerf20d1832005-07-30 01:40:57 +00006467 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6468
Chris Lattner80a3e942005-07-29 00:33:32 +00006469 // Now that the custom expander is done, expand the result, which is still
6470 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006471 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006472 ExpandOp(Op, Lo, Hi);
6473 break;
6474 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006475 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006476
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006477 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6478 VT);
6479 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6480 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006481 break;
Evan Cheng56966222007-01-12 02:11:51 +00006482 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006483
Evan Cheng56966222007-01-12 02:11:51 +00006484 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006485 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006486 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006487 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6488 case Expand: assert(0 && "cannot expand FP!");
6489 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6490 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6491 }
6492
6493 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6494
6495 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006496 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006497 ExpandOp(Op, Lo, Hi);
6498 break;
6499 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006500 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006501
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006502 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6503 VT);
6504 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6505 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006506 break;
Evan Cheng56966222007-01-12 02:11:51 +00006507 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006508
Evan Cheng05a2d562006-01-09 18:31:59 +00006509 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006510 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006511 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006512 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006513 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006514 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006515 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006516 // Now that the custom expander is done, expand the result, which is
6517 // still VT.
6518 ExpandOp(Op, Lo, Hi);
6519 break;
6520 }
6521 }
6522
Chris Lattner79980b02006-09-13 03:50:39 +00006523 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6524 // this X << 1 as X+X.
6525 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006526 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006527 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006528 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006529 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6530 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6531 LoOps[1] = LoOps[0];
6532 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6533
6534 HiOps[1] = HiOps[0];
6535 HiOps[2] = Lo.getValue(1);
6536 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6537 break;
6538 }
6539 }
6540
Chris Lattnere34b3962005-01-19 04:19:40 +00006541 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006542 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006543 break;
Chris Lattner47599822005-04-02 03:38:53 +00006544
6545 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006546 TargetLowering::LegalizeAction Action =
6547 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6548 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6549 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006550 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006551 break;
6552 }
6553
Chris Lattnere34b3962005-01-19 04:19:40 +00006554 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006555 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006556 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006557 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006558
Evan Cheng05a2d562006-01-09 18:31:59 +00006559 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006560 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006561 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006562 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006563 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006564 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006565 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006566 // Now that the custom expander is done, expand the result, which is
6567 // still VT.
6568 ExpandOp(Op, Lo, Hi);
6569 break;
6570 }
6571 }
6572
Chris Lattnere34b3962005-01-19 04:19:40 +00006573 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006574 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006575 break;
Chris Lattner47599822005-04-02 03:38:53 +00006576
6577 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006578 TargetLowering::LegalizeAction Action =
6579 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6580 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6581 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006582 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006583 break;
6584 }
6585
Chris Lattnere34b3962005-01-19 04:19:40 +00006586 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006587 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006588 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006589 }
6590
6591 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006592 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006593 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006594 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006595 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006596 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006597 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006598 // Now that the custom expander is done, expand the result, which is
6599 // still VT.
6600 ExpandOp(Op, Lo, Hi);
6601 break;
6602 }
6603 }
6604
Chris Lattnere34b3962005-01-19 04:19:40 +00006605 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006606 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006607 break;
Chris Lattner47599822005-04-02 03:38:53 +00006608
6609 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006610 TargetLowering::LegalizeAction Action =
6611 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6612 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6613 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006614 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006615 break;
6616 }
6617
Chris Lattnere34b3962005-01-19 04:19:40 +00006618 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006619 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006620 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006621 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006622
Misha Brukmanedf128a2005-04-21 22:36:52 +00006623 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006624 case ISD::SUB: {
6625 // If the target wants to custom expand this, let them.
6626 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6627 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006628 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006629 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006630 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006631 break;
6632 }
6633 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006634 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006635 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006636 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6637 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006638 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006639 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006640 LoOps[0] = LHSL;
6641 LoOps[1] = RHSL;
6642 HiOps[0] = LHSH;
6643 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006644
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006645 //cascaded check to see if any smaller size has a a carry flag.
6646 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6647 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006648 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6649 MVT AVT = MVT::getIntegerVT(BitSize);
6650 if (TLI.isOperationLegal(OpV, AVT)) {
6651 hasCarry = true;
6652 break;
6653 }
6654 }
6655
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006656 if(hasCarry) {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006657 if (Node->getOpcode() == ISD::ADD) {
6658 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6659 HiOps[2] = Lo.getValue(1);
6660 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6661 } else {
6662 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6663 HiOps[2] = Lo.getValue(1);
6664 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6665 }
6666 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006667 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006668 if (Node->getOpcode() == ISD::ADD) {
6669 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
6670 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006671 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6672 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006673 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6674 DAG.getConstant(1, NVT),
6675 DAG.getConstant(0, NVT));
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006676 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6677 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006678 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6679 DAG.getConstant(1, NVT),
6680 Carry1);
6681 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6682 } else {
6683 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
6684 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
6685 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6686 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6687 DAG.getConstant(1, NVT),
6688 DAG.getConstant(0, NVT));
6689 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6690 }
6691 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006692 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006693 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006694
6695 case ISD::ADDC:
6696 case ISD::SUBC: {
6697 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006698 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006699 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6700 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6701 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006702 SDValue LoOps[2] = { LHSL, RHSL };
6703 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006704
6705 if (Node->getOpcode() == ISD::ADDC) {
6706 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6707 HiOps[2] = Lo.getValue(1);
6708 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6709 } else {
6710 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6711 HiOps[2] = Lo.getValue(1);
6712 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6713 }
6714 // Remember that we legalized the flag.
6715 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6716 break;
6717 }
6718 case ISD::ADDE:
6719 case ISD::SUBE: {
6720 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006721 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006722 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6723 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6724 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006725 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6726 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006727
6728 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6729 HiOps[2] = Lo.getValue(1);
6730 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6731
6732 // Remember that we legalized the flag.
6733 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6734 break;
6735 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006736 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006737 // If the target wants to custom expand this, let them.
6738 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006739 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006740 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006741 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006742 break;
6743 }
6744 }
6745
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006746 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6747 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006748 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6749 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6750 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00006751 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00006752 ExpandOp(Node->getOperand(0), LL, LH);
6753 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006754 unsigned OuterBitSize = Op.getValueSizeInBits();
6755 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006756 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6757 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006758 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6759 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6760 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006761 // The inputs are both zero-extended.
6762 if (HasUMUL_LOHI) {
6763 // We can emit a umul_lohi.
6764 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006765 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006766 break;
6767 }
6768 if (HasMULHU) {
6769 // We can emit a mulhu+mul.
6770 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6771 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6772 break;
6773 }
Dan Gohman525178c2007-10-08 18:33:35 +00006774 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006775 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006776 // The input values are both sign-extended.
6777 if (HasSMUL_LOHI) {
6778 // We can emit a smul_lohi.
6779 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006780 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006781 break;
6782 }
6783 if (HasMULHS) {
6784 // We can emit a mulhs+mul.
6785 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6786 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6787 break;
6788 }
6789 }
6790 if (HasUMUL_LOHI) {
6791 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00006792 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00006793 DAG.getVTList(NVT, NVT), LL, RL);
6794 Lo = UMulLOHI;
6795 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006796 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6797 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6798 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6799 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006800 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006801 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006802 if (HasMULHU) {
6803 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6804 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6805 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6806 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6807 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6808 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6809 break;
6810 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006811 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006812
Dan Gohman525178c2007-10-08 18:33:35 +00006813 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006814 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006815 break;
6816 }
Evan Cheng56966222007-01-12 02:11:51 +00006817 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006818 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006819 break;
6820 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006821 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006822 break;
6823 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006824 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006825 break;
6826 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006827 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006828 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006829
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006830 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00006831 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6832 RTLIB::ADD_F64,
6833 RTLIB::ADD_F80,
6834 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006835 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006836 break;
6837 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00006838 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6839 RTLIB::SUB_F64,
6840 RTLIB::SUB_F80,
6841 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006842 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006843 break;
6844 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00006845 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6846 RTLIB::MUL_F64,
6847 RTLIB::MUL_F80,
6848 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006849 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006850 break;
6851 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006852 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6853 RTLIB::DIV_F64,
6854 RTLIB::DIV_F80,
6855 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006856 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006857 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006858 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006859 if (VT == MVT::ppcf128) {
6860 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6861 Node->getOperand(0).getValueType()==MVT::f64);
6862 const uint64_t zero = 0;
6863 if (Node->getOperand(0).getValueType()==MVT::f32)
6864 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6865 else
6866 Hi = Node->getOperand(0);
6867 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6868 break;
6869 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006870 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6871 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6872 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006873 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006874 }
6875 case ISD::FP_ROUND: {
6876 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6877 VT);
6878 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6879 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006880 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006881 }
Evan Cheng4b887022008-09-09 23:02:14 +00006882 case ISD::FSQRT:
6883 case ISD::FSIN:
6884 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00006885 case ISD::FLOG:
6886 case ISD::FLOG2:
6887 case ISD::FLOG10:
6888 case ISD::FEXP:
6889 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00006890 case ISD::FTRUNC:
6891 case ISD::FFLOOR:
6892 case ISD::FCEIL:
6893 case ISD::FRINT:
6894 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00006895 case ISD::FPOW:
6896 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00006897 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006898 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006899 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006900 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6901 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006902 break;
6903 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006904 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6905 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006906 break;
6907 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006908 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6909 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006910 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00006911 case ISD::FLOG:
6912 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
6913 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
6914 break;
6915 case ISD::FLOG2:
6916 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
6917 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
6918 break;
6919 case ISD::FLOG10:
6920 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
6921 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
6922 break;
6923 case ISD::FEXP:
6924 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
6925 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
6926 break;
6927 case ISD::FEXP2:
6928 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
6929 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
6930 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00006931 case ISD::FTRUNC:
6932 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
6933 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
6934 break;
6935 case ISD::FFLOOR:
6936 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
6937 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
6938 break;
6939 case ISD::FCEIL:
6940 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
6941 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
6942 break;
6943 case ISD::FRINT:
6944 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
6945 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
6946 break;
6947 case ISD::FNEARBYINT:
6948 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
6949 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
6950 break;
Evan Cheng4b887022008-09-09 23:02:14 +00006951 case ISD::FPOW:
6952 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
6953 RTLIB::POW_PPCF128);
6954 break;
6955 case ISD::FPOWI:
6956 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
6957 RTLIB::POWI_PPCF128);
6958 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006959 default: assert(0 && "Unreachable!");
6960 }
Duncan Sands460a14e2008-04-12 17:14:18 +00006961 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006962 break;
6963 }
Evan Cheng966bf242006-12-16 00:52:40 +00006964 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006965 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006966 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00006967 ExpandOp(Node->getOperand(0), Lo, Tmp);
6968 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6969 // lo = hi==fabs(hi) ? lo : -lo;
6970 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6971 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6972 DAG.getCondCode(ISD::SETEQ));
6973 break;
6974 }
Dan Gohman475871a2008-07-27 21:46:04 +00006975 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006976 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6977 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6978 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6979 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6980 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6981 if (getTypeAction(NVT) == Expand)
6982 ExpandOp(Lo, Lo, Hi);
6983 break;
6984 }
6985 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006986 if (VT == MVT::ppcf128) {
6987 ExpandOp(Node->getOperand(0), Lo, Hi);
6988 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6989 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6990 break;
6991 }
Dan Gohman475871a2008-07-27 21:46:04 +00006992 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006993 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6994 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6995 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6996 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6997 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6998 if (getTypeAction(NVT) == Expand)
6999 ExpandOp(Lo, Lo, Hi);
7000 break;
7001 }
Evan Cheng912095b2007-01-04 21:56:39 +00007002 case ISD::FCOPYSIGN: {
7003 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7004 if (getTypeAction(NVT) == Expand)
7005 ExpandOp(Lo, Lo, Hi);
7006 break;
7007 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007008 case ISD::SINT_TO_FP:
7009 case ISD::UINT_TO_FP: {
7010 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007011 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007012
7013 // Promote the operand if needed. Do this before checking for
7014 // ppcf128 so conversions of i16 and i8 work.
7015 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007016 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007017 Tmp = isSigned
7018 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7019 DAG.getValueType(SrcVT))
7020 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007021 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007022 SrcVT = Node->getOperand(0).getValueType();
7023 }
7024
Dan Gohmana2e94852008-03-10 23:03:31 +00007025 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007026 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007027 if (isSigned) {
7028 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7029 Node->getOperand(0)));
7030 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7031 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007032 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007033 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7034 Node->getOperand(0)));
7035 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7036 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007037 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007038 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7039 DAG.getConstant(0, MVT::i32),
7040 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7041 DAG.getConstantFP(
7042 APFloat(APInt(128, 2, TwoE32)),
7043 MVT::ppcf128)),
7044 Hi,
7045 DAG.getCondCode(ISD::SETLT)),
7046 Lo, Hi);
7047 }
7048 break;
7049 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007050 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7051 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007052 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007053 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7054 Lo, Hi);
7055 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7056 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7057 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7058 DAG.getConstant(0, MVT::i64),
7059 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7060 DAG.getConstantFP(
7061 APFloat(APInt(128, 2, TwoE64)),
7062 MVT::ppcf128)),
7063 Hi,
7064 DAG.getCondCode(ISD::SETLT)),
7065 Lo, Hi);
7066 break;
7067 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007068
Dan Gohmana2e94852008-03-10 23:03:31 +00007069 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7070 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00007071 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007072 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007073 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007074 break;
7075 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007076 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007077
Chris Lattner83397362005-12-21 18:02:52 +00007078 // Make sure the resultant values have been legalized themselves, unless this
7079 // is a type that requires multi-step expansion.
7080 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7081 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007082 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007083 // Don't legalize the high part if it is expanded to a single node.
7084 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007085 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007086
7087 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007088 bool isNew =
7089 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007090 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00007091}
7092
Dan Gohman7f321562007-06-25 16:23:39 +00007093/// SplitVectorOp - Given an operand of vector type, break it down into
7094/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007095void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7096 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007097 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007098 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007099 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007100 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007101
Duncan Sands83ec4b62008-06-06 12:08:01 +00007102 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007103
7104 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7105 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7106
Duncan Sands83ec4b62008-06-06 12:08:01 +00007107 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7108 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007109
Chris Lattnerc7029802006-03-18 01:44:44 +00007110 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007111 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007112 = SplitNodes.find(Op);
7113 if (I != SplitNodes.end()) {
7114 Lo = I->second.first;
7115 Hi = I->second.second;
7116 return;
7117 }
7118
7119 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007120 default:
7121#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007122 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007123#endif
7124 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007125 case ISD::UNDEF:
7126 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7127 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7128 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007129 case ISD::BUILD_PAIR:
7130 Lo = Node->getOperand(0);
7131 Hi = Node->getOperand(1);
7132 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007133 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007134 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7135 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007136 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007137 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007138 if (Index < NewNumElts_Lo)
7139 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7140 DAG.getIntPtrConstant(Index));
7141 else
7142 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7143 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7144 break;
7145 }
Dan Gohman475871a2008-07-27 21:46:04 +00007146 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007147 Node->getOperand(1),
7148 Node->getOperand(2));
7149 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007150 break;
7151 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007152 case ISD::VECTOR_SHUFFLE: {
7153 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007154 SDValue Mask = Node->getOperand(2);
7155 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007156 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007157
7158 // Insert all of the elements from the input that are needed. We use
7159 // buildvector of extractelement here because the input vectors will have
7160 // to be legalized, so this makes the code simpler.
7161 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007162 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007163 if (IdxNode.getOpcode() == ISD::UNDEF) {
7164 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7165 continue;
7166 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007167 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007168 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007169 if (Idx >= NumElements) {
7170 InVec = Node->getOperand(1);
7171 Idx -= NumElements;
7172 }
7173 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7174 DAG.getConstant(Idx, PtrVT)));
7175 }
7176 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7177 Ops.clear();
7178
7179 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007180 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007181 if (IdxNode.getOpcode() == ISD::UNDEF) {
7182 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7183 continue;
7184 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007185 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007186 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007187 if (Idx >= NumElements) {
7188 InVec = Node->getOperand(1);
7189 Idx -= NumElements;
7190 }
7191 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7192 DAG.getConstant(Idx, PtrVT)));
7193 }
Mon P Wang92879f32008-07-25 01:30:26 +00007194 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007195 break;
7196 }
Dan Gohman7f321562007-06-25 16:23:39 +00007197 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007198 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007199 Node->op_begin()+NewNumElts_Lo);
7200 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007201
Dan Gohman475871a2008-07-27 21:46:04 +00007202 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007203 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007204 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007205 break;
7206 }
Dan Gohman7f321562007-06-25 16:23:39 +00007207 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007208 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007209 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7210 if (NewNumSubvectors == 1) {
7211 Lo = Node->getOperand(0);
7212 Hi = Node->getOperand(1);
7213 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00007214 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Dan Gohman7f321562007-06-25 16:23:39 +00007215 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007216 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007217
Dan Gohman475871a2008-07-27 21:46:04 +00007218 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007219 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007220 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007221 }
Dan Gohman65956352007-06-13 15:12:02 +00007222 break;
7223 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007224 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007225 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007226
Dan Gohman475871a2008-07-27 21:46:04 +00007227 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007228 SplitVectorOp(Node->getOperand(1), LL, LH);
7229 SplitVectorOp(Node->getOperand(2), RL, RH);
7230
Duncan Sands83ec4b62008-06-06 12:08:01 +00007231 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007232 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007233 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007234 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007235 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7236 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007237 } else {
7238 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007239 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7240 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007241 }
7242 break;
7243 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007244 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007245 SDValue CondLHS = Node->getOperand(0);
7246 SDValue CondRHS = Node->getOperand(1);
7247 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007248
Dan Gohman475871a2008-07-27 21:46:04 +00007249 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007250 SplitVectorOp(Node->getOperand(2), LL, LH);
7251 SplitVectorOp(Node->getOperand(3), RL, RH);
7252
7253 // Handle a simple select with vector operands.
7254 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7255 LL, RL, CondCode);
7256 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7257 LH, RH, CondCode);
7258 break;
7259 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007260 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007261 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007262 SplitVectorOp(Node->getOperand(0), LL, LH);
7263 SplitVectorOp(Node->getOperand(1), RL, RH);
7264 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7265 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7266 break;
7267 }
Dan Gohman7f321562007-06-25 16:23:39 +00007268 case ISD::ADD:
7269 case ISD::SUB:
7270 case ISD::MUL:
7271 case ISD::FADD:
7272 case ISD::FSUB:
7273 case ISD::FMUL:
7274 case ISD::SDIV:
7275 case ISD::UDIV:
7276 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007277 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007278 case ISD::AND:
7279 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007280 case ISD::XOR:
7281 case ISD::UREM:
7282 case ISD::SREM:
7283 case ISD::FREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00007284 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007285 SplitVectorOp(Node->getOperand(0), LL, LH);
7286 SplitVectorOp(Node->getOperand(1), RL, RH);
7287
Nate Begeman5db1afb2007-11-15 21:15:26 +00007288 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7289 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007290 break;
7291 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007292 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007293 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007294 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007295 SplitVectorOp(Node->getOperand(0), L, H);
7296
Nate Begeman5db1afb2007-11-15 21:15:26 +00007297 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7298 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007299 break;
7300 }
7301 case ISD::CTTZ:
7302 case ISD::CTLZ:
7303 case ISD::CTPOP:
7304 case ISD::FNEG:
7305 case ISD::FABS:
7306 case ISD::FSQRT:
7307 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007308 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007309 case ISD::FLOG:
7310 case ISD::FLOG2:
7311 case ISD::FLOG10:
7312 case ISD::FEXP:
7313 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007314 case ISD::FP_TO_SINT:
7315 case ISD::FP_TO_UINT:
7316 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007317 case ISD::UINT_TO_FP:
7318 case ISD::TRUNCATE:
7319 case ISD::ANY_EXTEND:
7320 case ISD::SIGN_EXTEND:
7321 case ISD::ZERO_EXTEND:
7322 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007323 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007324 SplitVectorOp(Node->getOperand(0), L, H);
7325
Nate Begeman5db1afb2007-11-15 21:15:26 +00007326 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7327 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007328 break;
7329 }
Dan Gohman7f321562007-06-25 16:23:39 +00007330 case ISD::LOAD: {
7331 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007332 SDValue Ch = LD->getChain();
7333 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007334 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007335 const Value *SV = LD->getSrcValue();
7336 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007337 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007338 unsigned Alignment = LD->getAlignment();
7339 bool isVolatile = LD->isVolatile();
7340
Dan Gohman7f8613e2008-08-14 20:04:46 +00007341 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7342 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7343
7344 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7345 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7346 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7347
7348 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7349 NewVT_Lo, Ch, Ptr, Offset,
7350 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7351 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007352 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007353 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007354 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007355 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007356 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7357 NewVT_Hi, Ch, Ptr, Offset,
7358 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007359
7360 // Build a factor node to remember that this load is independent of the
7361 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007362 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007363 Hi.getValue(1));
7364
7365 // Remember that we legalized the chain.
7366 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007367 break;
7368 }
Dan Gohman7f321562007-06-25 16:23:39 +00007369 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007370 // We know the result is a vector. The input may be either a vector or a
7371 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007372 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007373 if (!InOp.getValueType().isVector() ||
7374 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007375 // The input is a scalar or single-element vector.
7376 // Lower to a store/load so that it can be split.
7377 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007378 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7379 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007380 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007381 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007382
Dan Gohman475871a2008-07-27 21:46:04 +00007383 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007384 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007385 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007386 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007387 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007388 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007389 // Split the vector and convert each of the pieces now.
7390 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007391 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7392 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007393 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007394 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007395 }
7396
7397 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007398 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007399 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007400 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007401}
7402
7403
Dan Gohman89b20c02007-06-27 14:06:22 +00007404/// ScalarizeVectorOp - Given an operand of single-element vector type
7405/// (e.g. v1f32), convert it into the equivalent operation that returns a
7406/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007407SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007408 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007409 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007410 MVT NewVT = Op.getValueType().getVectorElementType();
7411 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007412
Dan Gohman7f321562007-06-25 16:23:39 +00007413 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007414 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007415 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007416
Dan Gohman475871a2008-07-27 21:46:04 +00007417 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007418 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007419 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007420#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007421 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007422#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007423 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7424 case ISD::ADD:
7425 case ISD::FADD:
7426 case ISD::SUB:
7427 case ISD::FSUB:
7428 case ISD::MUL:
7429 case ISD::FMUL:
7430 case ISD::SDIV:
7431 case ISD::UDIV:
7432 case ISD::FDIV:
7433 case ISD::SREM:
7434 case ISD::UREM:
7435 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007436 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007437 case ISD::AND:
7438 case ISD::OR:
7439 case ISD::XOR:
7440 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007441 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007442 ScalarizeVectorOp(Node->getOperand(0)),
7443 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007444 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007445 case ISD::FNEG:
7446 case ISD::FABS:
7447 case ISD::FSQRT:
7448 case ISD::FSIN:
7449 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007450 case ISD::FLOG:
7451 case ISD::FLOG2:
7452 case ISD::FLOG10:
7453 case ISD::FEXP:
7454 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007455 case ISD::FP_TO_SINT:
7456 case ISD::FP_TO_UINT:
7457 case ISD::SINT_TO_FP:
7458 case ISD::UINT_TO_FP:
7459 case ISD::SIGN_EXTEND:
7460 case ISD::ZERO_EXTEND:
7461 case ISD::ANY_EXTEND:
7462 case ISD::TRUNCATE:
7463 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007464 Result = DAG.getNode(Node->getOpcode(),
7465 NewVT,
7466 ScalarizeVectorOp(Node->getOperand(0)));
7467 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00007468 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007469 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007470 Result = DAG.getNode(Node->getOpcode(),
7471 NewVT,
7472 ScalarizeVectorOp(Node->getOperand(0)),
7473 Node->getOperand(1));
7474 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007475 case ISD::LOAD: {
7476 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007477 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7478 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007479 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007480 const Value *SV = LD->getSrcValue();
7481 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007482 MVT MemoryVT = LD->getMemoryVT();
7483 unsigned Alignment = LD->getAlignment();
7484 bool isVolatile = LD->isVolatile();
7485
7486 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7487 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7488
7489 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7490 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7491 MemoryVT.getVectorElementType(),
7492 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007493
Chris Lattnerc7029802006-03-18 01:44:44 +00007494 // Remember that we legalized the chain.
7495 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7496 break;
7497 }
Dan Gohman7f321562007-06-25 16:23:39 +00007498 case ISD::BUILD_VECTOR:
7499 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007500 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007501 case ISD::INSERT_VECTOR_ELT:
7502 // Returning the inserted scalar element.
7503 Result = Node->getOperand(1);
7504 break;
7505 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007506 assert(Node->getOperand(0).getValueType() == NewVT &&
7507 "Concat of non-legal vectors not yet supported!");
7508 Result = Node->getOperand(0);
7509 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007510 case ISD::VECTOR_SHUFFLE: {
7511 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007512 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007513 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007514 Result = ScalarizeVectorOp(Node->getOperand(1));
7515 else
7516 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007517 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007518 }
7519 case ISD::EXTRACT_SUBVECTOR:
7520 Result = Node->getOperand(0);
Dan Gohman65956352007-06-13 15:12:02 +00007521 assert(Result.getValueType() == NewVT);
7522 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007523 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007524 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007525 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007526 Op0 = ScalarizeVectorOp(Op0);
7527 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007528 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007529 }
Dan Gohman7f321562007-06-25 16:23:39 +00007530 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007531 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007532 ScalarizeVectorOp(Op.getOperand(1)),
7533 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007534 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007535 case ISD::SELECT_CC:
7536 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7537 Node->getOperand(1),
7538 ScalarizeVectorOp(Op.getOperand(2)),
7539 ScalarizeVectorOp(Op.getOperand(3)),
7540 Node->getOperand(4));
7541 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007542 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007543 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7544 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007545 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7546 Op.getOperand(2));
7547 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7548 DAG.getConstant(-1ULL, NewVT),
7549 DAG.getConstant(0ULL, NewVT));
7550 break;
7551 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007552 }
7553
7554 if (TLI.isTypeLegal(NewVT))
7555 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007556 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7557 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007558 return Result;
7559}
7560
Chris Lattner3e928bb2005-01-07 07:47:09 +00007561
Mon P Wang0c397192008-10-30 08:01:45 +00007562SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7563 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7564 if (I != WidenNodes.end()) return I->second;
7565
7566 MVT VT = Op.getValueType();
7567 assert(VT.isVector() && "Cannot widen non-vector type!");
7568
7569 SDValue Result;
7570 SDNode *Node = Op.getNode();
7571 MVT EVT = VT.getVectorElementType();
7572
7573 unsigned NumElts = VT.getVectorNumElements();
7574 unsigned NewNumElts = WidenVT.getVectorNumElements();
7575 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7576 assert(NewNumElts < 17);
7577
7578 // When widen is called, it is assumed that it is more efficient to use a
7579 // wide type. The default action is to widen to operation to a wider legal
7580 // vector type and then do the operation if it is legal by calling LegalizeOp
7581 // again. If there is no vector equivalent, we will unroll the operation, do
7582 // it, and rebuild the vector. If most of the operations are vectorizible to
7583 // the legal type, the resulting code will be more efficient. If this is not
7584 // the case, the resulting code will preform badly as we end up generating
7585 // code to pack/unpack the results. It is the function that calls widen
7586 // that is responsible for seeing this doesn't happen. For some cases, we
7587 // have decided that it is not worth widening so we just split the operation.
7588 switch (Node->getOpcode()) {
7589 default:
7590#ifndef NDEBUG
7591 Node->dump(&DAG);
7592#endif
7593 assert(0 && "Unexpected operation in WidenVectorOp!");
7594 break;
7595 case ISD::CopyFromReg:
7596 assert(0 && "CopyFromReg must be legal!");
7597 case ISD::UNDEF:
7598 case ISD::Constant:
7599 case ISD::ConstantFP:
7600 // To build a vector of these elements, clients should call BuildVector
7601 // and with each element instead of creating a node with a vector type
7602 assert(0 && "Unexpected operation in WidenVectorOp!");
7603 case ISD::VAARG:
7604 // Variable Arguments with vector types doesn't make any sense to me
7605 assert(0 && "Unexpected operation in WidenVectorOp!");
7606 break;
7607 case ISD::BUILD_VECTOR: {
7608 // Build a vector with undefined for the new nodes
7609 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7610 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7611 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7612 }
7613 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7614 break;
7615 }
7616 case ISD::INSERT_VECTOR_ELT: {
7617 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7618 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7619 Node->getOperand(1), Node->getOperand(2));
7620 break;
7621 }
7622 case ISD::VECTOR_SHUFFLE: {
7623 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7624 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7625 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7626 // used as permutation array. We build the vector here instead of widening
7627 // because we don't want to legalize and have it turned to something else.
7628 SDValue PermOp = Node->getOperand(2);
7629 SDValueVector NewOps;
7630 MVT PVT = PermOp.getValueType().getVectorElementType();
7631 for (unsigned i = 0; i < NumElts; ++i) {
7632 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7633 NewOps.push_back(PermOp.getOperand(i));
7634 } else {
7635 unsigned Idx =
7636 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7637 if (Idx < NumElts) {
7638 NewOps.push_back(PermOp.getOperand(i));
7639 }
7640 else {
7641 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7642 PermOp.getOperand(i).getValueType()));
7643 }
7644 }
7645 }
7646 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7647 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7648 }
7649
7650 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7651 MVT::getVectorVT(PVT, NewOps.size()),
7652 &NewOps[0], NewOps.size());
7653
7654 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7655 break;
7656 }
7657 case ISD::LOAD: {
7658 // If the load widen returns true, we can use a single load for the
7659 // vector. Otherwise, it is returning a token factor for multiple
7660 // loads.
7661 SDValue TFOp;
7662 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7663 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7664 else
7665 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7666 break;
7667 }
7668
7669 case ISD::BIT_CONVERT: {
7670 SDValue Tmp1 = Node->getOperand(0);
7671 // Converts between two different types so we need to determine
7672 // the correct widen type for the input operand.
7673 MVT TVT = Tmp1.getValueType();
7674 assert(TVT.isVector() && "can not widen non vector type");
7675 MVT TEVT = TVT.getVectorElementType();
7676 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7677 "can not widen bit bit convert that are not multiple of element type");
7678 MVT TWidenVT = MVT::getVectorVT(TEVT,
7679 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7680 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7681 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7682 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7683
7684 TargetLowering::LegalizeAction action =
7685 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7686 switch (action) {
7687 default: assert(0 && "action not supported");
7688 case TargetLowering::Legal:
7689 break;
7690 case TargetLowering::Promote:
7691 // We defer the promotion to when we legalize the op
7692 break;
7693 case TargetLowering::Expand:
7694 // Expand the operation into a bunch of nasty scalar code.
7695 Result = LegalizeOp(UnrollVectorOp(Result));
7696 break;
7697 }
7698 break;
7699 }
7700
7701 case ISD::SINT_TO_FP:
7702 case ISD::UINT_TO_FP:
7703 case ISD::FP_TO_SINT:
7704 case ISD::FP_TO_UINT: {
7705 SDValue Tmp1 = Node->getOperand(0);
7706 // Converts between two different types so we need to determine
7707 // the correct widen type for the input operand.
7708 MVT TVT = Tmp1.getValueType();
7709 assert(TVT.isVector() && "can not widen non vector type");
7710 MVT TEVT = TVT.getVectorElementType();
7711 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7712 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7713 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7714 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7715
7716 TargetLowering::LegalizeAction action =
7717 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7718 switch (action) {
7719 default: assert(0 && "action not supported");
7720 case TargetLowering::Legal:
7721 break;
7722 case TargetLowering::Promote:
7723 // We defer the promotion to when we legalize the op
7724 break;
7725 case TargetLowering::Expand:
7726 // Expand the operation into a bunch of nasty scalar code.
7727 Result = LegalizeOp(UnrollVectorOp(Result));
7728 break;
7729 }
7730 break;
7731 }
7732
7733 case ISD::FP_EXTEND:
7734 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7735 case ISD::TRUNCATE:
7736 case ISD::SIGN_EXTEND:
7737 case ISD::ZERO_EXTEND:
7738 case ISD::ANY_EXTEND:
7739 case ISD::FP_ROUND:
7740 case ISD::SIGN_EXTEND_INREG:
7741 case ISD::FABS:
7742 case ISD::FNEG:
7743 case ISD::FSQRT:
7744 case ISD::FSIN:
7745 case ISD::FCOS: {
7746 // Unary op widening
7747 SDValue Tmp1;
7748 TargetLowering::LegalizeAction action =
7749 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7750
7751 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7752 assert(Tmp1.getValueType() == WidenVT);
7753 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7754 switch (action) {
7755 default: assert(0 && "action not supported");
7756 case TargetLowering::Legal:
7757 break;
7758 case TargetLowering::Promote:
7759 // We defer the promotion to when we legalize the op
7760 break;
7761 case TargetLowering::Expand:
7762 // Expand the operation into a bunch of nasty scalar code.
7763 Result = LegalizeOp(UnrollVectorOp(Result));
7764 break;
7765 }
7766 break;
7767 }
7768 case ISD::FPOW:
7769 case ISD::FPOWI:
7770 case ISD::ADD:
7771 case ISD::SUB:
7772 case ISD::MUL:
7773 case ISD::MULHS:
7774 case ISD::MULHU:
7775 case ISD::AND:
7776 case ISD::OR:
7777 case ISD::XOR:
7778 case ISD::FADD:
7779 case ISD::FSUB:
7780 case ISD::FMUL:
7781 case ISD::SDIV:
7782 case ISD::SREM:
7783 case ISD::FDIV:
7784 case ISD::FREM:
7785 case ISD::FCOPYSIGN:
7786 case ISD::UDIV:
7787 case ISD::UREM:
7788 case ISD::BSWAP: {
7789 // Binary op widening
7790 TargetLowering::LegalizeAction action =
7791 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7792
7793 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7794 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7795 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
7796 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
7797 switch (action) {
7798 default: assert(0 && "action not supported");
7799 case TargetLowering::Legal:
7800 break;
7801 case TargetLowering::Promote:
7802 // We defer the promotion to when we legalize the op
7803 break;
7804 case TargetLowering::Expand:
7805 // Expand the operation into a bunch of nasty scalar code by first
7806 // Widening to the right type and then unroll the beast.
7807 Result = LegalizeOp(UnrollVectorOp(Result));
7808 break;
7809 }
7810 break;
7811 }
7812
7813 case ISD::SHL:
7814 case ISD::SRA:
7815 case ISD::SRL: {
7816 // Binary op with one non vector operand
7817 TargetLowering::LegalizeAction action =
7818 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7819
7820 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7821 assert(Tmp1.getValueType() == WidenVT);
7822 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
7823 switch (action) {
7824 default: assert(0 && "action not supported");
7825 case TargetLowering::Legal:
7826 break;
7827 case TargetLowering::Promote:
7828 // We defer the promotion to when we legalize the op
7829 break;
7830 case TargetLowering::Expand:
7831 // Expand the operation into a bunch of nasty scalar code.
7832 Result = LegalizeOp(UnrollVectorOp(Result));
7833 break;
7834 }
7835 break;
7836 }
7837 case ISD::EXTRACT_VECTOR_ELT: {
7838 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7839 assert(Tmp1.getValueType() == WidenVT);
7840 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
7841 break;
7842 }
7843 case ISD::CONCAT_VECTORS: {
7844 // We concurrently support only widen on a multiple of the incoming vector.
7845 // We could widen on a multiple of the incoming operand if necessary.
7846 unsigned NumConcat = NewNumElts / NumElts;
7847 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
7848 std::vector<SDValue> UnOps(NumElts, DAG.getNode(ISD::UNDEF,
7849 VT.getVectorElementType()));
7850 SDValue UndefVal = DAG.getNode(ISD::BUILD_VECTOR, VT,
7851 &UnOps[0], UnOps.size());
7852 SmallVector<SDValue, 8> MOps;
7853 MOps.push_back(Op);
7854 for (unsigned i = 1; i != NumConcat; ++i) {
7855 MOps.push_back(UndefVal);
7856 }
7857 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
7858 &MOps[0], MOps.size()));
7859 break;
7860 }
7861 case ISD::EXTRACT_SUBVECTOR: {
7862 SDValue Tmp1;
7863
7864 // The incoming vector might already be the proper type
7865 if (Node->getOperand(0).getValueType() != WidenVT)
7866 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7867 else
7868 Tmp1 = Node->getOperand(0);
7869 assert(Tmp1.getValueType() == WidenVT);
7870 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
7871 break;
7872 }
7873
7874 case ISD::SELECT: {
7875 TargetLowering::LegalizeAction action =
7876 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7877
7878 // Determine new condition widen type and widen
7879 SDValue Cond1 = Node->getOperand(0);
7880 MVT CondVT = Cond1.getValueType();
7881 assert(CondVT.isVector() && "can not widen non vector type");
7882 MVT CondEVT = CondVT.getVectorElementType();
7883 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
7884 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
7885 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
7886
7887 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
7888 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
7889 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
7890 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
7891 switch (action) {
7892 default: assert(0 && "action not supported");
7893 case TargetLowering::Legal:
7894 break;
7895 case TargetLowering::Promote:
7896 // We defer the promotion to when we legalize the op
7897 break;
7898 case TargetLowering::Expand:
7899 // Expand the operation into a bunch of nasty scalar code by first
7900 // Widening to the right type and then unroll the beast.
7901 Result = LegalizeOp(UnrollVectorOp(Result));
7902 break;
7903 }
7904 break;
7905 }
7906
7907 case ISD::SELECT_CC: {
7908 TargetLowering::LegalizeAction action =
7909 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7910
7911 // Determine new condition widen type and widen
7912 SDValue Cond1 = Node->getOperand(0);
7913 SDValue Cond2 = Node->getOperand(1);
7914 MVT CondVT = Cond1.getValueType();
7915 assert(CondVT.isVector() && "can not widen non vector type");
7916 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
7917 MVT CondEVT = CondVT.getVectorElementType();
7918 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
7919 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
7920 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
7921 assert(Cond1.getValueType() == CondWidenVT &&
7922 Cond2.getValueType() == CondWidenVT && "condition not widen");
7923
7924 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
7925 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
7926 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
7927 "operands not widen");
7928 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
7929 Tmp2, Node->getOperand(4));
7930 switch (action) {
7931 default: assert(0 && "action not supported");
7932 case TargetLowering::Legal:
7933 break;
7934 case TargetLowering::Promote:
7935 // We defer the promotion to when we legalize the op
7936 break;
7937 case TargetLowering::Expand:
7938 // Expand the operation into a bunch of nasty scalar code by first
7939 // Widening to the right type and then unroll the beast.
7940 Result = LegalizeOp(UnrollVectorOp(Result));
7941 break;
7942 }
7943 break;
7944 break;
7945 }
7946
7947 case ISD::ATOMIC_CMP_SWAP_8:
7948 case ISD::ATOMIC_CMP_SWAP_16:
7949 case ISD::ATOMIC_CMP_SWAP_32:
7950 case ISD::ATOMIC_CMP_SWAP_64:
7951 case ISD::ATOMIC_LOAD_ADD_8:
7952 case ISD::ATOMIC_LOAD_SUB_8:
7953 case ISD::ATOMIC_LOAD_AND_8:
7954 case ISD::ATOMIC_LOAD_OR_8:
7955 case ISD::ATOMIC_LOAD_XOR_8:
7956 case ISD::ATOMIC_LOAD_NAND_8:
7957 case ISD::ATOMIC_LOAD_MIN_8:
7958 case ISD::ATOMIC_LOAD_MAX_8:
7959 case ISD::ATOMIC_LOAD_UMIN_8:
7960 case ISD::ATOMIC_LOAD_UMAX_8:
7961 case ISD::ATOMIC_SWAP_8:
7962 case ISD::ATOMIC_LOAD_ADD_16:
7963 case ISD::ATOMIC_LOAD_SUB_16:
7964 case ISD::ATOMIC_LOAD_AND_16:
7965 case ISD::ATOMIC_LOAD_OR_16:
7966 case ISD::ATOMIC_LOAD_XOR_16:
7967 case ISD::ATOMIC_LOAD_NAND_16:
7968 case ISD::ATOMIC_LOAD_MIN_16:
7969 case ISD::ATOMIC_LOAD_MAX_16:
7970 case ISD::ATOMIC_LOAD_UMIN_16:
7971 case ISD::ATOMIC_LOAD_UMAX_16:
7972 case ISD::ATOMIC_SWAP_16:
7973 case ISD::ATOMIC_LOAD_ADD_32:
7974 case ISD::ATOMIC_LOAD_SUB_32:
7975 case ISD::ATOMIC_LOAD_AND_32:
7976 case ISD::ATOMIC_LOAD_OR_32:
7977 case ISD::ATOMIC_LOAD_XOR_32:
7978 case ISD::ATOMIC_LOAD_NAND_32:
7979 case ISD::ATOMIC_LOAD_MIN_32:
7980 case ISD::ATOMIC_LOAD_MAX_32:
7981 case ISD::ATOMIC_LOAD_UMIN_32:
7982 case ISD::ATOMIC_LOAD_UMAX_32:
7983 case ISD::ATOMIC_SWAP_32:
7984 case ISD::ATOMIC_LOAD_ADD_64:
7985 case ISD::ATOMIC_LOAD_SUB_64:
7986 case ISD::ATOMIC_LOAD_AND_64:
7987 case ISD::ATOMIC_LOAD_OR_64:
7988 case ISD::ATOMIC_LOAD_XOR_64:
7989 case ISD::ATOMIC_LOAD_NAND_64:
7990 case ISD::ATOMIC_LOAD_MIN_64:
7991 case ISD::ATOMIC_LOAD_MAX_64:
7992 case ISD::ATOMIC_LOAD_UMIN_64:
7993 case ISD::ATOMIC_LOAD_UMAX_64:
7994 case ISD::ATOMIC_SWAP_64: {
7995 // For now, we assume that using vectors for these operations don't make
7996 // much sense so we just split it. We return an empty result
7997 SDValue X, Y;
7998 SplitVectorOp(Op, X, Y);
7999 return Result;
8000 break;
8001 }
8002
8003 } // end switch (Node->getOpcode())
8004
8005 assert(Result.getNode() && "Didn't set a result!");
8006 if (Result != Op)
8007 Result = LegalizeOp(Result);
8008
8009 AddWidenOperand(Op, Result);
8010 return Result;
8011}
8012
8013// Utility function to find a legal vector type and its associated element
8014// type from a preferred width and whose vector type must be the same size
8015// as the VVT.
8016// TLI: Target lowering used to determine legal types
8017// Width: Preferred width of element type
8018// VVT: Vector value type whose size we must match.
8019// Returns VecEVT and EVT - the vector type and its associated element type
8020static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8021 MVT& EVT, MVT& VecEVT) {
8022 // We start with the preferred width, make it a power of 2 and see if
8023 // we can find a vector type of that width. If not, we reduce it by
8024 // another power of 2. If we have widen the type, a vector of bytes should
8025 // always be legal.
8026 assert(TLI.isTypeLegal(VVT));
8027 unsigned EWidth = Width + 1;
8028 do {
8029 assert(EWidth > 0);
8030 EWidth = (1 << Log2_32(EWidth-1));
8031 EVT = MVT::getIntegerVT(EWidth);
8032 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8033 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8034 } while (!TLI.isTypeLegal(VecEVT) ||
8035 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8036}
8037
8038SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8039 SDValue Chain,
8040 SDValue BasePtr,
8041 const Value *SV,
8042 int SVOffset,
8043 unsigned Alignment,
8044 bool isVolatile,
8045 unsigned LdWidth,
8046 MVT ResType) {
8047 // We assume that we have good rules to handle loading power of two loads so
8048 // we break down the operations to power of 2 loads. The strategy is to
8049 // load the largest power of 2 that we can easily transform to a legal vector
8050 // and then insert into that vector, and the cast the result into the legal
8051 // vector that we want. This avoids unnecessary stack converts.
8052 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8053 // the load is nonvolatile, we an use a wider load for the value.
8054 // Find a vector length we can load a large chunk
8055 MVT EVT, VecEVT;
8056 unsigned EVTWidth;
8057 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8058 EVTWidth = EVT.getSizeInBits();
8059
8060 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8061 isVolatile, Alignment);
8062 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8063 LdChain.push_back(LdOp.getValue(1));
8064
8065 // Check if we can load the element with one instruction
8066 if (LdWidth == EVTWidth) {
8067 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8068 }
8069
8070 // The vector element order is endianness dependent.
8071 unsigned Idx = 1;
8072 LdWidth -= EVTWidth;
8073 unsigned Offset = 0;
8074
8075 while (LdWidth > 0) {
8076 unsigned Increment = EVTWidth / 8;
8077 Offset += Increment;
8078 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8079 DAG.getIntPtrConstant(Increment));
8080
8081 if (LdWidth < EVTWidth) {
8082 // Our current type we are using is too large, use a smaller size by
8083 // using a smaller power of 2
8084 unsigned oEVTWidth = EVTWidth;
8085 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8086 EVTWidth = EVT.getSizeInBits();
8087 // Readjust position and vector position based on new load type
8088 Idx = Idx * (oEVTWidth/EVTWidth)+1;
8089 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8090 }
8091
8092 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8093 SVOffset+Offset, isVolatile,
8094 MinAlign(Alignment, Offset));
8095 LdChain.push_back(LdOp.getValue(1));
8096 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8097 DAG.getIntPtrConstant(Idx++));
8098
8099 LdWidth -= EVTWidth;
8100 }
8101
8102 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8103}
8104
8105bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8106 SDValue& TFOp,
8107 SDValue Op,
8108 MVT NVT) {
8109 // TODO: Add support for ConcatVec and the ability to load many vector
8110 // types (e.g., v4i8). This will not work when a vector register
8111 // to memory mapping is strange (e.g., vector elements are not
8112 // stored in some sequential order).
8113
8114 // It must be true that the widen vector type is bigger than where
8115 // we need to load from.
8116 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8117 MVT LdVT = LD->getMemoryVT();
8118 assert(LdVT.isVector() && NVT.isVector());
8119 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8120
8121 // Load information
8122 SDValue Chain = LD->getChain();
8123 SDValue BasePtr = LD->getBasePtr();
8124 int SVOffset = LD->getSrcValueOffset();
8125 unsigned Alignment = LD->getAlignment();
8126 bool isVolatile = LD->isVolatile();
8127 const Value *SV = LD->getSrcValue();
8128 unsigned int LdWidth = LdVT.getSizeInBits();
8129
8130 // Load value as a large register
8131 SDValueVector LdChain;
8132 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8133 Alignment, isVolatile, LdWidth, NVT);
8134
8135 if (LdChain.size() == 1) {
8136 TFOp = LdChain[0];
8137 return true;
8138 }
8139 else {
8140 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8141 return false;
8142 }
8143}
8144
8145
8146void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8147 SDValue Chain,
8148 SDValue BasePtr,
8149 const Value *SV,
8150 int SVOffset,
8151 unsigned Alignment,
8152 bool isVolatile,
8153 SDValue ValOp,
8154 unsigned StWidth) {
8155 // Breaks the stores into a series of power of 2 width stores. For any
8156 // width, we convert the vector to the vector of element size that we
8157 // want to store. This avoids requiring a stack convert.
8158
8159 // Find a width of the element type we can store with
8160 MVT VVT = ValOp.getValueType();
8161 MVT EVT, VecEVT;
8162 unsigned EVTWidth;
8163 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8164 EVTWidth = EVT.getSizeInBits();
8165
8166 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8167 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
8168 DAG.getIntPtrConstant(0));
8169 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8170 isVolatile, Alignment);
8171 StChain.push_back(StOp);
8172
8173 // Check if we are done
8174 if (StWidth == EVTWidth) {
8175 return;
8176 }
8177
8178 unsigned Idx = 1;
8179 StWidth -= EVTWidth;
8180 unsigned Offset = 0;
8181
8182 while (StWidth > 0) {
8183 unsigned Increment = EVTWidth / 8;
8184 Offset += Increment;
8185 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8186 DAG.getIntPtrConstant(Increment));
8187
8188 if (StWidth < EVTWidth) {
8189 // Our current type we are using is too large, use a smaller size by
8190 // using a smaller power of 2
8191 unsigned oEVTWidth = EVTWidth;
8192 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8193 EVTWidth = EVT.getSizeInBits();
8194 // Readjust position and vector position based on new load type
8195 Idx = Idx * (oEVTWidth/EVTWidth)+1;
8196 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8197 }
8198
8199 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
8200 DAG.getIntPtrConstant(Idx));
8201 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8202 SVOffset + Offset, isVolatile,
8203 MinAlign(Alignment, Offset)));
8204 StWidth -= EVTWidth;
8205 }
8206}
8207
8208
8209SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8210 SDValue Chain,
8211 SDValue BasePtr) {
8212 // TODO: It might be cleaner if we can use SplitVector and have more legal
8213 // vector types that can be stored into memory (e.g., v4xi8 can
8214 // be stored as a word). This will not work when a vector register
8215 // to memory mapping is strange (e.g., vector elements are not
8216 // stored in some sequential order).
8217
8218 MVT StVT = ST->getMemoryVT();
8219 SDValue ValOp = ST->getValue();
8220
8221 // Check if we have widen this node with another value
8222 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8223 if (I != WidenNodes.end())
8224 ValOp = I->second;
8225
8226 MVT VVT = ValOp.getValueType();
8227
8228 // It must be true that we the widen vector type is bigger than where
8229 // we need to store.
8230 assert(StVT.isVector() && VVT.isVector());
8231 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8232 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8233
8234 // Store value
8235 SDValueVector StChain;
8236 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8237 ST->getSrcValueOffset(), ST->getAlignment(),
8238 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8239 if (StChain.size() == 1)
8240 return StChain[0];
8241 else
8242 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8243}
8244
8245
Chris Lattner3e928bb2005-01-07 07:47:09 +00008246// SelectionDAG::Legalize - This is the entry point for the file.
8247//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00008248void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008249 /// run - This is the main entry point to this class.
8250 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00008251 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008252}
8253