blob: 84c6fb82f721d46d6e47da4e97091aa2360be851 [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 Wangf007a8b2008-11-06 05:31:54 +0000103 /// WidenNodes - For nodes that need to be widened from one vector type to
104 /// another, this contains the mapping of those that we have already widen.
105 /// This allows us to avoid widening more than once.
Mon P Wang0c397192008-10-30 08:01:45 +0000106 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 Wangf007a8b2008-11-06 05:31:54 +0000120 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang0c397192008-10-30 08:01:45 +0000121 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 Wangf007a8b2008-11-06 05:31:54 +0000183 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
184 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
185 /// for the existing elements but no guarantee is made about the new elements
186 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
187 /// when we have an instruction operating on an illegal vector type and we
188 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang0c397192008-10-30 08:01:45 +0000189 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
190
Dan Gohman7f321562007-06-25 16:23:39 +0000191 /// SplitVectorOp - Given an operand of vector type, break it down into
192 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000193 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000194
Dan Gohman89b20c02007-06-27 14:06:22 +0000195 /// ScalarizeVectorOp - Given an operand of single-element vector type
196 /// (e.g. v1f32), convert it into the equivalent operation that returns a
197 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000198 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000199
Mon P Wangf007a8b2008-11-06 05:31:54 +0000200 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang0c397192008-10-30 08:01:45 +0000201 typedef SmallVector<SDValue, 16> SDValueVector;
202
203 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
204 /// the LdChain contains a single load and false if it contains a token
205 /// factor for multiple loads. It takes
206 /// Result: location to return the result
207 /// LdChain: location to return the load chain
208 /// Op: load operation to widen
209 /// NVT: widen vector result type we want for the load
210 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
211 SDValue Op, MVT NVT);
212
213 /// Helper genWidenVectorLoads - Helper function to generate a set of
214 /// loads to load a vector with a resulting wider type. It takes
215 /// LdChain: list of chains for the load we have generated
216 /// Chain: incoming chain for the ld vector
217 /// BasePtr: base pointer to load from
218 /// SV: memory disambiguation source value
219 /// SVOffset: memory disambiugation offset
220 /// Alignment: alignment of the memory
221 /// isVolatile: volatile load
222 /// LdWidth: width of memory that we want to load
223 /// ResType: the wider result result type for the resulting loaded vector
224 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
225 SDValue BasePtr, const Value *SV,
226 int SVOffset, unsigned Alignment,
227 bool isVolatile, unsigned LdWidth,
228 MVT ResType);
229
230 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
231 /// location. It takes
232 /// ST: store node that we want to replace
233 /// Chain: incoming store chain
234 /// BasePtr: base address of where we want to store into
235 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
236 SDValue BasePtr);
237
238 /// Helper genWidenVectorStores - Helper function to generate a set of
239 /// stores to store a widen vector into non widen memory
240 // It takes
241 // StChain: list of chains for the stores we have generated
242 // Chain: incoming chain for the ld vector
243 // BasePtr: base pointer to load from
244 // SV: memory disambiguation source value
245 // SVOffset: memory disambiugation offset
246 // Alignment: alignment of the memory
247 // isVolatile: volatile lod
248 // ValOp: value to store
249 // StWidth: width of memory that we want to store
250 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
251 SDValue BasePtr, const Value *SV,
252 int SVOffset, unsigned Alignment,
253 bool isVolatile, SDValue ValOp,
254 unsigned StWidth);
255
Duncan Sandsd038e042008-07-21 10:20:31 +0000256 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000257 /// specified mask and type. Targets can specify exactly which masks they
258 /// support and the code generator is tasked with not creating illegal masks.
259 ///
260 /// Note that this will also return true for shuffles that are promoted to a
261 /// different type.
262 ///
263 /// If this is a legal shuffle, this method returns the (possibly promoted)
264 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000265 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000266
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000267 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000268 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000269
Dan Gohman475871a2008-07-27 21:46:04 +0000270 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng7f042682008-10-15 02:05:31 +0000271 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
272 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
273 LegalizeSetCCOperands(LHS, RHS, CC);
274 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
275 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000276
Dan Gohman475871a2008-07-27 21:46:04 +0000277 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
278 SDValue &Hi);
279 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000280
Dan Gohman475871a2008-07-27 21:46:04 +0000281 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
282 SDValue ExpandBUILD_VECTOR(SDNode *Node);
283 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman7f8613e2008-08-14 20:04:46 +0000284 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000285 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
286 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
287 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000288
Dan Gohman475871a2008-07-27 21:46:04 +0000289 SDValue ExpandBSWAP(SDValue Op);
290 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
291 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
292 SDValue &Lo, SDValue &Hi);
293 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
294 SDValue &Lo, SDValue &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000295
Dan Gohman475871a2008-07-27 21:46:04 +0000296 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
297 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000298};
299}
300
Chris Lattner4352cc92006-04-04 17:23:26 +0000301/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
302/// specified mask and type. Targets can specify exactly which masks they
303/// support and the code generator is tasked with not creating illegal masks.
304///
305/// Note that this will also return true for shuffles that are promoted to a
306/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000307SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000308 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
309 default: return 0;
310 case TargetLowering::Legal:
311 case TargetLowering::Custom:
312 break;
313 case TargetLowering::Promote: {
314 // If this is promoted to a different type, convert the shuffle mask and
315 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000316 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000317 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000318
319 // If we changed # elements, change the shuffle mask.
320 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000321 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000322 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
323 if (NumEltsGrowth > 1) {
324 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000325 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000326 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000327 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000328 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
329 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000330 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000331 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000332 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000333 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000334 }
335 }
336 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000337 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000338 }
339 VT = NVT;
340 break;
341 }
342 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000343 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000344}
345
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000346SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
347 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
348 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000349 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000350 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000351}
352
Chris Lattner3e928bb2005-01-07 07:47:09 +0000353void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000354 LastCALLSEQ_END = DAG.getEntryNode();
355 IsLegalizingCall = false;
356
Chris Lattnerab510a72005-10-02 17:49:46 +0000357 // The legalize process is inherently a bottom-up recursive process (users
358 // legalize their uses before themselves). Given infinite stack space, we
359 // could just start legalizing on the root and traverse the whole graph. In
360 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000361 // blocks. To avoid this problem, compute an ordering of the nodes where each
362 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000363 DAG.AssignTopologicalOrder();
364 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
365 E = prior(DAG.allnodes_end()); I != next(E); ++I)
366 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000367
368 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000369 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000370 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
371 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000372
373 ExpandedNodes.clear();
374 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000375 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000376 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000377 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000378 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000379
380 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000381 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000382}
383
Chris Lattner6831a812006-02-13 09:18:02 +0000384
385/// FindCallEndFromCallStart - Given a chained node that is part of a call
386/// sequence, find the CALLSEQ_END node that terminates the call sequence.
387static SDNode *FindCallEndFromCallStart(SDNode *Node) {
388 if (Node->getOpcode() == ISD::CALLSEQ_END)
389 return Node;
390 if (Node->use_empty())
391 return 0; // No CallSeqEnd
392
393 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000394 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000395 if (TheChain.getValueType() != MVT::Other) {
396 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000397 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000398 if (TheChain.getValueType() != MVT::Other) {
399 // Otherwise, hunt for it.
400 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
401 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000402 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000403 break;
404 }
405
406 // Otherwise, we walked into a node without a chain.
407 if (TheChain.getValueType() != MVT::Other)
408 return 0;
409 }
410 }
411
412 for (SDNode::use_iterator UI = Node->use_begin(),
413 E = Node->use_end(); UI != E; ++UI) {
414
415 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000416 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000417 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
418 if (User->getOperand(i) == TheChain)
419 if (SDNode *Result = FindCallEndFromCallStart(User))
420 return Result;
421 }
422 return 0;
423}
424
425/// FindCallStartFromCallEnd - Given a chained node that is part of a call
426/// sequence, find the CALLSEQ_START node that initiates the call sequence.
427static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
428 assert(Node && "Didn't find callseq_start for a call??");
429 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
430
431 assert(Node->getOperand(0).getValueType() == MVT::Other &&
432 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000433 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000434}
435
436/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
437/// see if any uses can reach Dest. If no dest operands can get to dest,
438/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000439///
440/// Keep track of the nodes we fine that actually do lead to Dest in
441/// NodesLeadingTo. This avoids retraversing them exponential number of times.
442///
443bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000444 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000445 if (N == Dest) return true; // N certainly leads to Dest :)
446
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000447 // If we've already processed this node and it does lead to Dest, there is no
448 // need to reprocess it.
449 if (NodesLeadingTo.count(N)) return true;
450
Chris Lattner6831a812006-02-13 09:18:02 +0000451 // If the first result of this node has been already legalized, then it cannot
452 // reach N.
453 switch (getTypeAction(N->getValueType(0))) {
454 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000455 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000456 break;
457 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000458 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000459 break;
460 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000461 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000462 break;
463 }
464
465 // Okay, this node has not already been legalized. Check and legalize all
466 // operands. If none lead to Dest, then we can legalize this node.
467 bool OperandsLeadToDest = false;
468 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
469 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000470 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000471
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000472 if (OperandsLeadToDest) {
473 NodesLeadingTo.insert(N);
474 return true;
475 }
Chris Lattner6831a812006-02-13 09:18:02 +0000476
477 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000478 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000479 return false;
480}
481
Mon P Wang0c397192008-10-30 08:01:45 +0000482/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000483/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000484void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000485 MVT VT = Op.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000486 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000487 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000488 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000489 case Promote:
490 if (!VT.isVector()) {
491 (void)PromoteOp(Op);
492 break;
493 }
494 else {
495 // See if we can widen otherwise use Expand to either scalarize or split
496 MVT WidenVT = TLI.getWidenVectorType(VT);
497 if (WidenVT != MVT::Other) {
498 (void) WidenVectorOp(Op, WidenVT);
499 break;
500 }
501 // else fall thru to expand since we can't widen the vector
502 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000503 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000504 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000505 // If this is an illegal scalar, expand it into its two component
506 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000507 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000508 if (Op.getOpcode() == ISD::TargetConstant)
509 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000510 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000511 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000512 // If this is an illegal single element vector, convert it to a
513 // scalar operation.
514 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000515 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000516 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000517 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000518 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000519 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000520 }
521 break;
522 }
523}
Chris Lattner6831a812006-02-13 09:18:02 +0000524
Evan Cheng9f877882006-12-13 20:57:08 +0000525/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
526/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000527static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000528 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000529 bool Extend = false;
530
531 // If a FP immediate is precise when represented as a float and if the
532 // target can do an extending load from float to double, we put it into
533 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000534 // double. This shrinks FP constants and canonicalizes them for targets where
535 // an FP extending load is the same cost as a normal load (such as on the x87
536 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000537 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000538 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000539 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000540 if (VT!=MVT::f64 && VT!=MVT::f32)
541 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000542 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000543 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000544 }
545
Duncan Sands83ec4b62008-06-06 12:08:01 +0000546 MVT OrigVT = VT;
547 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000548 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000549 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000550 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
551 // Only do this if the target has a native EXTLOAD instruction from
552 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000553 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000554 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000555 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000556 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
557 VT = SVT;
558 Extend = true;
559 }
Evan Cheng00495212006-12-12 21:32:44 +0000560 }
561
Dan Gohman475871a2008-07-27 21:46:04 +0000562 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000563 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000564 if (Extend)
565 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000566 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000567 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000568 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000569 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000570}
571
Chris Lattner6831a812006-02-13 09:18:02 +0000572
Evan Cheng912095b2007-01-04 21:56:39 +0000573/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
574/// operations.
575static
Dan Gohman475871a2008-07-27 21:46:04 +0000576SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
577 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000578 MVT VT = Node->getValueType(0);
579 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000580 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
581 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000582 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000583
Evan Cheng912095b2007-01-04 21:56:39 +0000584 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000585 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000586 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
587 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000588 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000589 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000590 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000591 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000592 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000593 if (SizeDiff > 0) {
594 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
595 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
596 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000597 } else if (SizeDiff < 0) {
598 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
599 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
600 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
601 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000602
603 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000604 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000605 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
606 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
607 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000608 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000609 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
610
611 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000612 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
613 return Result;
614}
615
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000616/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
617static
Dan Gohman475871a2008-07-27 21:46:04 +0000618SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
619 TargetLowering &TLI) {
620 SDValue Chain = ST->getChain();
621 SDValue Ptr = ST->getBasePtr();
622 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000623 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000624 int Alignment = ST->getAlignment();
625 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000626 if (ST->getMemoryVT().isFloatingPoint() ||
627 ST->getMemoryVT().isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000628 // Expand to a bitconvert of the value to the integer type of the
629 // same size, then a (misaligned) int store.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000630 MVT intVT;
631 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000632 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000633 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000634 intVT = MVT::i64;
635 else if (VT==MVT::f32)
636 intVT = MVT::i32;
637 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000638 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000639
Dan Gohman475871a2008-07-27 21:46:04 +0000640 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000641 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
642 SVOffset, ST->isVolatile(), Alignment);
643 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000644 assert(ST->getMemoryVT().isInteger() &&
645 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000646 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000647 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000648 MVT NewStoredVT =
649 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
650 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000651 int IncrementSize = NumBits / 8;
652
653 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000654 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
655 SDValue Lo = Val;
656 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000657
658 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000659 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000660 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
661 ST->getSrcValue(), SVOffset, NewStoredVT,
662 ST->isVolatile(), Alignment);
663 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
664 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000665 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000666 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
667 ST->getSrcValue(), SVOffset + IncrementSize,
668 NewStoredVT, ST->isVolatile(), Alignment);
669
670 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
671}
672
673/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
674static
Dan Gohman475871a2008-07-27 21:46:04 +0000675SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
676 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000677 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000678 SDValue Chain = LD->getChain();
679 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000680 MVT VT = LD->getValueType(0);
681 MVT LoadedVT = LD->getMemoryVT();
682 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000683 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000684 // then bitconvert to floating point or vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000685 MVT intVT;
686 if (LoadedVT.is128BitVector() ||
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000687 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000688 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000689 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000690 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000691 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000692 intVT = MVT::i32;
693 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000694 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000695
Dan Gohman475871a2008-07-27 21:46:04 +0000696 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000697 SVOffset, LD->isVolatile(),
698 LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +0000699 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000700 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000701 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
702
Dan Gohman475871a2008-07-27 21:46:04 +0000703 SDValue Ops[] = { Result, Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000704 return DAG.getMergeValues(Ops, 2);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000705 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000706 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000707 "Unaligned load of unsupported type.");
708
Dale Johannesen8155d642008-02-27 22:36:00 +0000709 // Compute the new VT that is half the size of the old one. This is an
710 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000711 unsigned NumBits = LoadedVT.getSizeInBits();
712 MVT NewLoadedVT;
713 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000714 NumBits >>= 1;
715
716 unsigned Alignment = LD->getAlignment();
717 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000718 ISD::LoadExtType HiExtType = LD->getExtensionType();
719
720 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
721 if (HiExtType == ISD::NON_EXTLOAD)
722 HiExtType = ISD::ZEXTLOAD;
723
724 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000725 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000726 if (TLI.isLittleEndian()) {
727 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
728 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
729 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
730 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
731 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
732 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000733 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000734 } else {
735 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
736 NewLoadedVT,LD->isVolatile(), Alignment);
737 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
738 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
739 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
740 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000741 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000742 }
743
744 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000745 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
746 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000747 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
748
Dan Gohman475871a2008-07-27 21:46:04 +0000749 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000750 Hi.getValue(1));
751
Dan Gohman475871a2008-07-27 21:46:04 +0000752 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000753 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000754}
Evan Cheng912095b2007-01-04 21:56:39 +0000755
Dan Gohman82669522007-10-11 23:57:53 +0000756/// UnrollVectorOp - We know that the given vector has a legal type, however
757/// the operation it performs is not legal and is an operation that we have
758/// no way of lowering. "Unroll" the vector, splitting out the scalars and
759/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000760SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000761 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000762 assert(isTypeLegal(VT) &&
763 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000764 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000765 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000766 unsigned NE = VT.getVectorNumElements();
767 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000768
Dan Gohman475871a2008-07-27 21:46:04 +0000769 SmallVector<SDValue, 8> Scalars;
770 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000771 for (unsigned i = 0; i != NE; ++i) {
772 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000773 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000774 MVT OperandVT = Operand.getValueType();
775 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000776 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000777 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000778 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
779 OperandEltVT,
780 Operand,
781 DAG.getConstant(i, MVT::i32));
782 } else {
783 // A scalar operand; just use it as is.
784 Operands[j] = Operand;
785 }
786 }
787 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
788 &Operands[0], Operands.size()));
789 }
790
791 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
792}
793
Duncan Sands007f9842008-01-10 10:28:30 +0000794/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000795static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000796 RTLIB::Libcall Call_F32,
797 RTLIB::Libcall Call_F64,
798 RTLIB::Libcall Call_F80,
799 RTLIB::Libcall Call_PPCF128) {
800 return
801 VT == MVT::f32 ? Call_F32 :
802 VT == MVT::f64 ? Call_F64 :
803 VT == MVT::f80 ? Call_F80 :
804 VT == MVT::ppcf128 ? Call_PPCF128 :
805 RTLIB::UNKNOWN_LIBCALL;
806}
807
Nate Begeman68679912008-04-25 18:07:40 +0000808/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
809/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
810/// is necessary to spill the vector being inserted into to memory, perform
811/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000812SDValue SelectionDAGLegalize::
813PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
814 SDValue Tmp1 = Vec;
815 SDValue Tmp2 = Val;
816 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000817
818 // If the target doesn't support this, we have to spill the input vector
819 // to a temporary stack slot, update the element, then reload it. This is
820 // badness. We could also load the value into a vector register (either
821 // with a "move to register" or "extload into register" instruction, then
822 // permute it into place, if the idx is a constant and if the idx is
823 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000824 MVT VT = Tmp1.getValueType();
825 MVT EltVT = VT.getVectorElementType();
826 MVT IdxVT = Tmp3.getValueType();
827 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000828 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000829
Gabor Greifba36cb52008-08-28 21:40:38 +0000830 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000831
832 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000833 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000834 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000835
836 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000837 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000838 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
839 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000840 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000841 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000842 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000843 // Store the scalar value.
844 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000845 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000846 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000847 return DAG.getLoad(VT, Ch, StackPtr,
848 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000849}
850
Dan Gohmana3466152007-07-13 20:14:11 +0000851/// LegalizeOp - We know that the specified value has a legal type, and
852/// that its operands are legal. Now ensure that the operation itself
853/// is legal, recursively ensuring that the operands' operations remain
854/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000855SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000856 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
857 return Op;
858
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000859 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000860 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000861 SDNode *Node = Op.getNode();
Chris Lattnere3304a32005-01-08 20:35:13 +0000862
Chris Lattner3e928bb2005-01-07 07:47:09 +0000863 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000864 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000865 if (Node->getNumValues() > 1) {
866 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000867 if (getTypeAction(Node->getValueType(i)) != Legal) {
868 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000869 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000870 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000871 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000872 }
873 }
874
Chris Lattner45982da2005-05-12 16:53:42 +0000875 // Note that LegalizeOp may be reentered even from single-use nodes, which
876 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000877 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000878 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000879
Dan Gohman475871a2008-07-27 21:46:04 +0000880 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
881 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000882 bool isCustom = false;
883
Chris Lattner3e928bb2005-01-07 07:47:09 +0000884 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000885 case ISD::FrameIndex:
886 case ISD::EntryToken:
887 case ISD::Register:
888 case ISD::BasicBlock:
889 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000890 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000891 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000892 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000893 case ISD::TargetConstantPool:
894 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000895 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000896 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +0000897 case ISD::VALUETYPE:
898 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000899 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000900 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +0000901 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +0000902 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000903 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000904 "This must be legal!");
905 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000906 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000907 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
908 // If this is a target node, legalize it by legalizing the operands then
909 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +0000910 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000911 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000912 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000913
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000914 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000915
916 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
917 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000918 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000919 }
920 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000921#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000922 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000923#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000924 assert(0 && "Do not know how to legalize this operator!");
925 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000926 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000927 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000928 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000929 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000930 case ISD::ConstantPool:
931 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000932 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
933 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000934 case TargetLowering::Custom:
935 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000936 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +0000937 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000938 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000939 break;
940 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000941 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000942 case ISD::FRAMEADDR:
943 case ISD::RETURNADDR:
944 // The only option for these nodes is to custom lower them. If the target
945 // does not custom lower them, then return zero.
946 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000947 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +0000948 Result = Tmp1;
949 else
950 Result = DAG.getConstant(0, TLI.getPointerTy());
951 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000952 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000953 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000954 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
955 default: assert(0 && "This action is not supported yet!");
956 case TargetLowering::Custom:
957 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000958 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000959 // Fall Thru
960 case TargetLowering::Legal:
961 Result = DAG.getConstant(0, VT);
962 break;
963 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000964 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000965 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000966 case ISD::EXCEPTIONADDR: {
967 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000968 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000969 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
970 default: assert(0 && "This action is not supported yet!");
971 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000972 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000973 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000974 }
975 break;
976 case TargetLowering::Custom:
977 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000978 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000979 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000980 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000981 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000982 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000983 break;
984 }
985 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000986 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000987 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000988
Gabor Greifba36cb52008-08-28 21:40:38 +0000989 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000990 "Cannot return more than two values!");
991
992 // Since we produced two values, make sure to remember that we
993 // legalized both of them.
994 Tmp1 = LegalizeOp(Result);
995 Tmp2 = LegalizeOp(Result.getValue(1));
996 AddLegalizedOperand(Op.getValue(0), Tmp1);
997 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +0000998 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +0000999 case ISD::EHSELECTION: {
1000 Tmp1 = LegalizeOp(Node->getOperand(0));
1001 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001002 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001003 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1004 default: assert(0 && "This action is not supported yet!");
1005 case TargetLowering::Expand: {
1006 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001007 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001008 }
1009 break;
1010 case TargetLowering::Custom:
1011 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001012 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001013 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001014 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001015 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001016 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001017 break;
1018 }
1019 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001020 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001021 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001022
Gabor Greifba36cb52008-08-28 21:40:38 +00001023 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001024 "Cannot return more than two values!");
1025
1026 // Since we produced two values, make sure to remember that we
1027 // legalized both of them.
1028 Tmp1 = LegalizeOp(Result);
1029 Tmp2 = LegalizeOp(Result.getValue(1));
1030 AddLegalizedOperand(Op.getValue(0), Tmp1);
1031 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001032 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001033 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001034 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001035 // The only "good" option for this node is to custom lower it.
1036 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1037 default: assert(0 && "This action is not supported at all!");
1038 case TargetLowering::Custom:
1039 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001040 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001041 // Fall Thru
1042 case TargetLowering::Legal:
1043 // Target does not know, how to lower this, lower to noop
1044 Result = LegalizeOp(Node->getOperand(0));
1045 break;
1046 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001047 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001048 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001049 case ISD::AssertSext:
1050 case ISD::AssertZext:
1051 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001052 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001053 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001054 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001055 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001056 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001057 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001058 case ISD::CopyFromReg:
1059 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001060 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001061 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001062 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001063 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001064 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001065 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001066 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001067 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1068 } else {
1069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1070 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001071 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1072 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001073 // Since CopyFromReg produces two values, make sure to remember that we
1074 // legalized both of them.
1075 AddLegalizedOperand(Op.getValue(0), Result);
1076 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001077 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001078 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001079 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001080 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001081 default: assert(0 && "This action is not supported yet!");
1082 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001083 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001084 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001085 else if (VT.isFloatingPoint())
1086 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001087 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001088 else
1089 assert(0 && "Unknown value type!");
1090 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001091 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001092 break;
1093 }
1094 break;
1095 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001096
Chris Lattner48b61a72006-03-28 00:40:33 +00001097 case ISD::INTRINSIC_W_CHAIN:
1098 case ISD::INTRINSIC_WO_CHAIN:
1099 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001100 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001101 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1102 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001103 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001104
1105 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001106 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001107 TargetLowering::Custom) {
1108 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001109 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001110 }
1111
Gabor Greifba36cb52008-08-28 21:40:38 +00001112 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001113
1114 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001115 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001116 "Cannot return more than two values!");
1117
1118 // Since loads produce two values, make sure to remember that we
1119 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001120 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1121 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001122 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001123 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001124
Dan Gohman7f460202008-06-30 20:59:49 +00001125 case ISD::DBG_STOPPOINT:
1126 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001127 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1128
Dan Gohman7f460202008-06-30 20:59:49 +00001129 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001130 case TargetLowering::Promote:
1131 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001132 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001133 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001134 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohman44066042008-07-01 00:05:16 +00001135 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001136
Dan Gohman7f460202008-06-30 20:59:49 +00001137 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001138 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman7f460202008-06-30 20:59:49 +00001139 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1140 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001141
Dan Gohman7f460202008-06-30 20:59:49 +00001142 unsigned Line = DSP->getLine();
1143 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001144
1145 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001146 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001147 DAG.getConstant(Col, MVT::i32),
1148 DAG.getConstant(SrcFile, MVT::i32) };
1149 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001150 } else {
Evan Chenga647c922008-02-01 02:05:57 +00001151 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001152 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001153 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001154 } else {
1155 Result = Tmp1; // chain
1156 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001157 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001158 }
Evan Cheng71e86852008-07-08 20:06:39 +00001159 case TargetLowering::Legal: {
1160 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1161 if (Action == Legal && Tmp1 == Node->getOperand(0))
1162 break;
1163
Dan Gohman475871a2008-07-27 21:46:04 +00001164 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001165 Ops.push_back(Tmp1);
1166 if (Action == Legal) {
1167 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1168 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1169 } else {
1170 // Otherwise promote them.
1171 Ops.push_back(PromoteOp(Node->getOperand(1)));
1172 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001173 }
Evan Cheng71e86852008-07-08 20:06:39 +00001174 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1175 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1176 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001177 break;
1178 }
Evan Cheng71e86852008-07-08 20:06:39 +00001179 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001180 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001181
1182 case ISD::DECLARE:
1183 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1184 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1185 default: assert(0 && "This action is not supported yet!");
1186 case TargetLowering::Legal:
1187 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1188 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1189 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1191 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001192 case TargetLowering::Expand:
1193 Result = LegalizeOp(Node->getOperand(0));
1194 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001195 }
1196 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001197
1198 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001199 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001200 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001201 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001202 case TargetLowering::Legal: {
1203 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001204 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001205 if (Action == Legal && Tmp1 == Node->getOperand(0))
1206 break;
1207 if (Action == Legal) {
1208 Tmp2 = Node->getOperand(1);
1209 Tmp3 = Node->getOperand(2);
1210 Tmp4 = Node->getOperand(3);
1211 } else {
1212 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1213 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1214 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1215 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001216 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001217 break;
1218 }
Evan Cheng71e86852008-07-08 20:06:39 +00001219 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001220 break;
1221
Dan Gohman44066042008-07-01 00:05:16 +00001222 case ISD::DBG_LABEL:
1223 case ISD::EH_LABEL:
1224 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1225 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001226 default: assert(0 && "This action is not supported yet!");
1227 case TargetLowering::Legal:
1228 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001229 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001230 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001231 case TargetLowering::Expand:
1232 Result = LegalizeOp(Node->getOperand(0));
1233 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001234 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001235 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001236
Evan Cheng27b7db52008-03-08 00:58:38 +00001237 case ISD::PREFETCH:
1238 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1239 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1240 default: assert(0 && "This action is not supported yet!");
1241 case TargetLowering::Legal:
1242 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1243 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1244 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1245 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1246 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1247 break;
1248 case TargetLowering::Expand:
1249 // It's a noop.
1250 Result = LegalizeOp(Node->getOperand(0));
1251 break;
1252 }
1253 break;
1254
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001255 case ISD::MEMBARRIER: {
1256 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001257 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1258 default: assert(0 && "This action is not supported yet!");
1259 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001260 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001261 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001262 for (int x = 1; x < 6; ++x) {
1263 Ops[x] = Node->getOperand(x);
1264 if (!isTypeLegal(Ops[x].getValueType()))
1265 Ops[x] = PromoteOp(Ops[x]);
1266 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001267 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1268 break;
1269 }
1270 case TargetLowering::Expand:
1271 //There is no libgcc call for this op
1272 Result = Node->getOperand(0); // Noop
1273 break;
1274 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001275 break;
1276 }
1277
Dale Johannesene00a8a22008-08-28 02:44:49 +00001278 case ISD::ATOMIC_CMP_SWAP_8:
1279 case ISD::ATOMIC_CMP_SWAP_16:
1280 case ISD::ATOMIC_CMP_SWAP_32:
1281 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001282 unsigned int num_operands = 4;
1283 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001284 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001285 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001286 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001287 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1288
1289 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1290 default: assert(0 && "This action is not supported yet!");
1291 case TargetLowering::Custom:
1292 Result = TLI.LowerOperation(Result, DAG);
1293 break;
1294 case TargetLowering::Legal:
1295 break;
1296 }
Dan Gohman475871a2008-07-27 21:46:04 +00001297 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1298 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001299 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001300 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00001301 case ISD::ATOMIC_LOAD_ADD_8:
1302 case ISD::ATOMIC_LOAD_SUB_8:
1303 case ISD::ATOMIC_LOAD_AND_8:
1304 case ISD::ATOMIC_LOAD_OR_8:
1305 case ISD::ATOMIC_LOAD_XOR_8:
1306 case ISD::ATOMIC_LOAD_NAND_8:
1307 case ISD::ATOMIC_LOAD_MIN_8:
1308 case ISD::ATOMIC_LOAD_MAX_8:
1309 case ISD::ATOMIC_LOAD_UMIN_8:
1310 case ISD::ATOMIC_LOAD_UMAX_8:
1311 case ISD::ATOMIC_SWAP_8:
1312 case ISD::ATOMIC_LOAD_ADD_16:
1313 case ISD::ATOMIC_LOAD_SUB_16:
1314 case ISD::ATOMIC_LOAD_AND_16:
1315 case ISD::ATOMIC_LOAD_OR_16:
1316 case ISD::ATOMIC_LOAD_XOR_16:
1317 case ISD::ATOMIC_LOAD_NAND_16:
1318 case ISD::ATOMIC_LOAD_MIN_16:
1319 case ISD::ATOMIC_LOAD_MAX_16:
1320 case ISD::ATOMIC_LOAD_UMIN_16:
1321 case ISD::ATOMIC_LOAD_UMAX_16:
1322 case ISD::ATOMIC_SWAP_16:
1323 case ISD::ATOMIC_LOAD_ADD_32:
1324 case ISD::ATOMIC_LOAD_SUB_32:
1325 case ISD::ATOMIC_LOAD_AND_32:
1326 case ISD::ATOMIC_LOAD_OR_32:
1327 case ISD::ATOMIC_LOAD_XOR_32:
1328 case ISD::ATOMIC_LOAD_NAND_32:
1329 case ISD::ATOMIC_LOAD_MIN_32:
1330 case ISD::ATOMIC_LOAD_MAX_32:
1331 case ISD::ATOMIC_LOAD_UMIN_32:
1332 case ISD::ATOMIC_LOAD_UMAX_32:
1333 case ISD::ATOMIC_SWAP_32:
1334 case ISD::ATOMIC_LOAD_ADD_64:
1335 case ISD::ATOMIC_LOAD_SUB_64:
1336 case ISD::ATOMIC_LOAD_AND_64:
1337 case ISD::ATOMIC_LOAD_OR_64:
1338 case ISD::ATOMIC_LOAD_XOR_64:
1339 case ISD::ATOMIC_LOAD_NAND_64:
1340 case ISD::ATOMIC_LOAD_MIN_64:
1341 case ISD::ATOMIC_LOAD_MAX_64:
1342 case ISD::ATOMIC_LOAD_UMIN_64:
1343 case ISD::ATOMIC_LOAD_UMAX_64:
1344 case ISD::ATOMIC_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001345 unsigned int num_operands = 3;
1346 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001347 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001348 for (unsigned int x = 0; x < num_operands; ++x)
1349 Ops[x] = LegalizeOp(Node->getOperand(x));
1350 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001351
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001352 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001353 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001354 case TargetLowering::Custom:
1355 Result = TLI.LowerOperation(Result, DAG);
1356 break;
1357 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001358 break;
1359 }
Dan Gohman475871a2008-07-27 21:46:04 +00001360 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1361 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001362 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001363 }
Scott Michelc1513d22007-08-08 23:23:31 +00001364 case ISD::Constant: {
1365 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1366 unsigned opAction =
1367 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1368
Chris Lattner3e928bb2005-01-07 07:47:09 +00001369 // We know we don't need to expand constants here, constants only have one
1370 // value and we check that it is fine above.
1371
Scott Michelc1513d22007-08-08 23:23:31 +00001372 if (opAction == TargetLowering::Custom) {
1373 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001374 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001375 Result = Tmp1;
1376 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001377 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001378 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001379 case ISD::ConstantFP: {
1380 // Spill FP immediates to the constant pool if the target cannot directly
1381 // codegen them. Targets often have some immediate values that can be
1382 // efficiently generated into an FP register without a load. We explicitly
1383 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001384 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1385
Chris Lattner3181a772006-01-29 06:26:56 +00001386 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1387 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001388 case TargetLowering::Legal:
1389 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001390 case TargetLowering::Custom:
1391 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001392 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001393 Result = Tmp3;
1394 break;
1395 }
1396 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001397 case TargetLowering::Expand: {
1398 // Check to see if this FP immediate is already legal.
1399 bool isLegal = false;
1400 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1401 E = TLI.legal_fpimm_end(); I != E; ++I) {
1402 if (CFP->isExactlyValue(*I)) {
1403 isLegal = true;
1404 break;
1405 }
1406 }
1407 // If this is a legal constant, turn it into a TargetConstantFP node.
1408 if (isLegal)
1409 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001410 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001411 }
Nate Begemane1795842008-02-14 08:57:00 +00001412 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001413 break;
1414 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001415 case ISD::TokenFactor:
1416 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001417 Tmp1 = LegalizeOp(Node->getOperand(0));
1418 Tmp2 = LegalizeOp(Node->getOperand(1));
1419 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1420 } else if (Node->getNumOperands() == 3) {
1421 Tmp1 = LegalizeOp(Node->getOperand(0));
1422 Tmp2 = LegalizeOp(Node->getOperand(1));
1423 Tmp3 = LegalizeOp(Node->getOperand(2));
1424 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001425 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001426 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001427 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001428 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1429 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001430 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001431 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001432 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001433
1434 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001435 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001436 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001437 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001438 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001439 // A call within a calling sequence must be legalized to something
1440 // other than the normal CALLSEQ_END. Violating this gets Legalize
1441 // into an infinite loop.
1442 assert ((!IsLegalizingCall ||
1443 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001444 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001445 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001446
1447 // The number of incoming and outgoing values should match; unless the final
1448 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001449 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1450 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1451 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001452 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001453 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001454
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001455 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001456 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001457 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1458 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001459 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001460 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001461 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001462 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001463 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001464 }
1465 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001466 case ISD::EXTRACT_SUBREG: {
1467 Tmp1 = LegalizeOp(Node->getOperand(0));
1468 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1469 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001470 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001471 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1472 }
1473 break;
1474 case ISD::INSERT_SUBREG: {
1475 Tmp1 = LegalizeOp(Node->getOperand(0));
1476 Tmp2 = LegalizeOp(Node->getOperand(1));
1477 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1478 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001479 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001480 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1481 }
1482 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001483 case ISD::BUILD_VECTOR:
1484 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001485 default: assert(0 && "This action is not supported yet!");
1486 case TargetLowering::Custom:
1487 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001488 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001489 Result = Tmp3;
1490 break;
1491 }
1492 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001493 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001494 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001495 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001496 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001497 break;
1498 case ISD::INSERT_VECTOR_ELT:
1499 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001500 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001501
1502 // The type of the value to insert may not be legal, even though the vector
1503 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1504 // here.
1505 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1506 default: assert(0 && "Cannot expand insert element operand");
1507 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1508 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001509 case Expand:
1510 // FIXME: An alternative would be to check to see if the target is not
1511 // going to custom lower this operation, we could bitcast to half elt
1512 // width and perform two inserts at that width, if that is legal.
1513 Tmp2 = Node->getOperand(1);
1514 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001515 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1517
1518 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1519 Node->getValueType(0))) {
1520 default: assert(0 && "This action is not supported yet!");
1521 case TargetLowering::Legal:
1522 break;
1523 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001524 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001525 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001526 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001527 break;
1528 }
1529 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001530 case TargetLowering::Promote:
1531 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001532 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001533 // If the insert index is a constant, codegen this as a scalar_to_vector,
1534 // then a shuffle that inserts it into the right position in the vector.
1535 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001536 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1537 // match the element type of the vector being created.
1538 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001539 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001540 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001541 Tmp1.getValueType(), Tmp2);
1542
Duncan Sands83ec4b62008-06-06 12:08:01 +00001543 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1544 MVT ShufMaskVT =
1545 MVT::getIntVectorWithNumElements(NumElts);
1546 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001547
1548 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1549 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1550 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001551 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001552 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001553 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001554 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1555 else
1556 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1557 }
Dan Gohman475871a2008-07-27 21:46:04 +00001558 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001559 &ShufOps[0], ShufOps.size());
1560
1561 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1562 Tmp1, ScVec, ShufMask);
1563 Result = LegalizeOp(Result);
1564 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001565 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001566 }
Nate Begeman68679912008-04-25 18:07:40 +00001567 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001568 break;
1569 }
1570 }
1571 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001572 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001573 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1574 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1575 break;
1576 }
1577
Chris Lattnerce872152006-03-19 06:31:19 +00001578 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1579 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1580 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1581 Node->getValueType(0))) {
1582 default: assert(0 && "This action is not supported yet!");
1583 case TargetLowering::Legal:
1584 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001585 case TargetLowering::Custom:
1586 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001587 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001588 Result = Tmp3;
1589 break;
1590 }
1591 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001592 case TargetLowering::Expand:
1593 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001594 break;
1595 }
Chris Lattnerce872152006-03-19 06:31:19 +00001596 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001597 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1599 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1601
1602 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001603 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1604 default: assert(0 && "Unknown operation action!");
1605 case TargetLowering::Legal:
1606 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1607 "vector shuffle should not be created if not legal!");
1608 break;
1609 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001610 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001611 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001612 Result = Tmp3;
1613 break;
1614 }
1615 // FALLTHROUGH
1616 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001617 MVT VT = Node->getValueType(0);
1618 MVT EltVT = VT.getVectorElementType();
1619 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001620 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001621 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001622 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001623 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001624 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001625 if (Arg.getOpcode() == ISD::UNDEF) {
1626 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1627 } else {
1628 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001629 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001630 if (Idx < NumElems)
1631 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1632 DAG.getConstant(Idx, PtrVT)));
1633 else
1634 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1635 DAG.getConstant(Idx - NumElems, PtrVT)));
1636 }
1637 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001638 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001639 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001640 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001641 case TargetLowering::Promote: {
1642 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001643 MVT OVT = Node->getValueType(0);
1644 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001645
1646 // Cast the two input vectors.
1647 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1648 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1649
1650 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001651 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001652 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001653 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1654 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1655 break;
1656 }
Chris Lattner87100e02006-03-20 01:52:29 +00001657 }
1658 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001659
1660 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001661 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001662 Tmp2 = LegalizeOp(Node->getOperand(1));
1663 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001664 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001665 break;
1666
Dan Gohman7f321562007-06-25 16:23:39 +00001667 case ISD::EXTRACT_SUBVECTOR:
1668 Tmp1 = Node->getOperand(0);
1669 Tmp2 = LegalizeOp(Node->getOperand(1));
1670 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1671 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001672 break;
1673
Mon P Wang0c397192008-10-30 08:01:45 +00001674 case ISD::CONCAT_VECTORS: {
1675 // Use extract/insert/build vector for now. We might try to be
1676 // more clever later.
1677 MVT PtrVT = TLI.getPointerTy();
1678 SmallVector<SDValue, 8> Ops;
1679 unsigned NumOperands = Node->getNumOperands();
1680 for (unsigned i=0; i < NumOperands; ++i) {
1681 SDValue SubOp = Node->getOperand(i);
1682 MVT VVT = SubOp.getNode()->getValueType(0);
1683 MVT EltVT = VVT.getVectorElementType();
1684 unsigned NumSubElem = VVT.getVectorNumElements();
1685 for (unsigned j=0; j < NumSubElem; ++j) {
1686 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1687 DAG.getConstant(j, PtrVT)));
1688 }
1689 }
1690 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1691 &Ops[0], Ops.size()));
1692 }
1693
Chris Lattner6831a812006-02-13 09:18:02 +00001694 case ISD::CALLSEQ_START: {
1695 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1696
1697 // Recursively Legalize all of the inputs of the call end that do not lead
1698 // to this call start. This ensures that any libcalls that need be inserted
1699 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001700 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001701 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001702 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001703 NodesLeadingTo);
1704 }
Chris Lattner6831a812006-02-13 09:18:02 +00001705
1706 // Now that we legalized all of the inputs (which may have inserted
1707 // libcalls) create the new CALLSEQ_START node.
1708 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1709
1710 // Merge in the last call, to ensure that this call start after the last
1711 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001712 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001713 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1714 Tmp1 = LegalizeOp(Tmp1);
1715 }
Chris Lattner6831a812006-02-13 09:18:02 +00001716
1717 // Do not try to legalize the target-specific arguments (#1+).
1718 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001719 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001720 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001721 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001722 }
1723
1724 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001725 AddLegalizedOperand(Op.getValue(0), Result);
1726 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1727 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1728
Chris Lattner6831a812006-02-13 09:18:02 +00001729 // Now that the callseq_start and all of the non-call nodes above this call
1730 // sequence have been legalized, legalize the call itself. During this
1731 // process, no libcalls can/will be inserted, guaranteeing that no calls
1732 // can overlap.
1733 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001734 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001735 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001736 IsLegalizingCall = true;
1737
1738 // Legalize the call, starting from the CALLSEQ_END.
1739 LegalizeOp(LastCALLSEQ_END);
1740 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1741 return Result;
1742 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001743 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001744 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1745 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001746 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001747 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1748 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001749 assert(I != LegalizedNodes.end() &&
1750 "Legalizing the call start should have legalized this node!");
1751 return I->second;
1752 }
1753
1754 // Otherwise, the call start has been legalized and everything is going
1755 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001756 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001757 // Do not try to legalize the target-specific arguments (#1+), except for
1758 // an optional flag input.
1759 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1760 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001761 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001762 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001763 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001764 }
1765 } else {
1766 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1767 if (Tmp1 != Node->getOperand(0) ||
1768 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001769 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001770 Ops[0] = Tmp1;
1771 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001772 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001773 }
Chris Lattner6a542892006-01-24 05:48:21 +00001774 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001775 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001776 // This finishes up call legalization.
1777 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001778
1779 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001780 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001781 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001782 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001783 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001784 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001785 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001786 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1787 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1788 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001789 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001790
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001791 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001792 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001793 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001794 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001795 case TargetLowering::Expand: {
1796 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1797 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1798 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001799 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001800
1801 // Chain the dynamic stack allocation so that it doesn't modify the stack
1802 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001803 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001804
Dan Gohman475871a2008-07-27 21:46:04 +00001805 SDValue Size = Tmp2.getOperand(1);
1806 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001807 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001808 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001809 unsigned StackAlign =
1810 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1811 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001812 SP = DAG.getNode(ISD::AND, VT, SP,
1813 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001814 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001815 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1816
Chris Lattnere563bbc2008-10-11 22:08:30 +00001817 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1818 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001819
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001820 Tmp1 = LegalizeOp(Tmp1);
1821 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001822 break;
1823 }
1824 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001825 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001826 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001827 Tmp1 = LegalizeOp(Tmp3);
1828 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001829 }
Chris Lattner903d2782006-01-15 08:54:32 +00001830 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001831 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001832 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001833 }
Chris Lattner903d2782006-01-15 08:54:32 +00001834 // Since this op produce two values, make sure to remember that we
1835 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001836 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1837 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001838 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001839 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001840 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001841 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001842 bool Changed = false;
1843 // Legalize all of the operands of the inline asm, in case they are nodes
1844 // that need to be expanded or something. Note we skip the asm string and
1845 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001846 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001847 Changed = Op != Ops[0];
1848 Ops[0] = Op;
1849
1850 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1851 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001852 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001853 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001854 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001855 if (Op != Ops[i]) {
1856 Changed = true;
1857 Ops[i] = Op;
1858 }
1859 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001860 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001861
1862 if (HasInFlag) {
1863 Op = LegalizeOp(Ops.back());
1864 Changed |= Op != Ops.back();
1865 Ops.back() = Op;
1866 }
1867
1868 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001869 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001870
1871 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001872 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1873 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001874 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001875 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001876 case ISD::BR:
1877 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001878 // Ensure that libcalls are emitted before a branch.
1879 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1880 Tmp1 = LegalizeOp(Tmp1);
1881 LastCALLSEQ_END = DAG.getEntryNode();
1882
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001883 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001884 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001885 case ISD::BRIND:
1886 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1887 // Ensure that libcalls are emitted before a branch.
1888 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1889 Tmp1 = LegalizeOp(Tmp1);
1890 LastCALLSEQ_END = DAG.getEntryNode();
1891
1892 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1893 default: assert(0 && "Indirect target must be legal type (pointer)!");
1894 case Legal:
1895 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1896 break;
1897 }
1898 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1899 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001900 case ISD::BR_JT:
1901 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1902 // Ensure that libcalls are emitted before a branch.
1903 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1904 Tmp1 = LegalizeOp(Tmp1);
1905 LastCALLSEQ_END = DAG.getEntryNode();
1906
1907 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1908 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1909
1910 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1911 default: assert(0 && "This action is not supported yet!");
1912 case TargetLowering::Legal: break;
1913 case TargetLowering::Custom:
1914 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001915 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001916 break;
1917 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001918 SDValue Chain = Result.getOperand(0);
1919 SDValue Table = Result.getOperand(1);
1920 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001921
Duncan Sands83ec4b62008-06-06 12:08:01 +00001922 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001923 MachineFunction &MF = DAG.getMachineFunction();
1924 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001925 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00001926 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001927
Dan Gohman475871a2008-07-27 21:46:04 +00001928 SDValue LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001929 switch (EntrySize) {
1930 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001931 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001932 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001933 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001934 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001935 }
1936
Evan Chengcc415862007-11-09 01:32:10 +00001937 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001938 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001939 // For PIC, the sequence is:
1940 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001941 // RelocBase can be JumpTable, GOT or some sort of global base.
1942 if (PTy != MVT::i32)
1943 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1944 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1945 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001946 }
Evan Chengcc415862007-11-09 01:32:10 +00001947 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001948 }
1949 }
1950 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001951 case ISD::BRCOND:
1952 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001953 // Ensure that libcalls are emitted before a return.
1954 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1955 Tmp1 = LegalizeOp(Tmp1);
1956 LastCALLSEQ_END = DAG.getEntryNode();
1957
Chris Lattner47e92232005-01-18 19:27:06 +00001958 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1959 case Expand: assert(0 && "It's impossible to expand bools");
1960 case Legal:
1961 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1962 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001963 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001964 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001965
1966 // The top bits of the promoted condition are not necessarily zero, ensure
1967 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001968 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001969 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001970 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001971 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001972 break;
1973 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001974 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001975
1976 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001977 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001978
1979 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1980 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001981 case TargetLowering::Legal: break;
1982 case TargetLowering::Custom:
1983 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001984 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001985 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001986 case TargetLowering::Expand:
1987 // Expand brcond's setcc into its constituent parts and create a BR_CC
1988 // Node.
1989 if (Tmp2.getOpcode() == ISD::SETCC) {
1990 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1991 Tmp2.getOperand(0), Tmp2.getOperand(1),
1992 Node->getOperand(2));
1993 } else {
1994 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1995 DAG.getCondCode(ISD::SETNE), Tmp2,
1996 DAG.getConstant(0, Tmp2.getValueType()),
1997 Node->getOperand(2));
1998 }
1999 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002000 }
2001 break;
2002 case ISD::BR_CC:
2003 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002004 // Ensure that libcalls are emitted before a branch.
2005 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2006 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002007 Tmp2 = Node->getOperand(2); // LHS
2008 Tmp3 = Node->getOperand(3); // RHS
2009 Tmp4 = Node->getOperand(1); // CC
2010
Dale Johannesen53e4e442008-11-07 22:54:33 +00002011 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002012 LastCALLSEQ_END = DAG.getEntryNode();
2013
Evan Cheng7f042682008-10-15 02:05:31 +00002014 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002015 // the LHS is a legal SETCC itself. In this case, we need to compare
2016 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002017 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002018 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2019 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002020 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002021
2022 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2023 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002024
Chris Lattner181b7a32005-12-17 23:46:46 +00002025 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2026 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002027 case TargetLowering::Legal: break;
2028 case TargetLowering::Custom:
2029 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002030 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002031 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002032 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002033 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002034 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002035 LoadSDNode *LD = cast<LoadSDNode>(Node);
2036 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2037 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002038
Evan Cheng466685d2006-10-09 20:57:25 +00002039 ISD::LoadExtType ExtType = LD->getExtensionType();
2040 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002041 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002042 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2043 Tmp3 = Result.getValue(0);
2044 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002045
Evan Cheng466685d2006-10-09 20:57:25 +00002046 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2047 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002048 case TargetLowering::Legal:
2049 // If this is an unaligned load and the target doesn't support it,
2050 // expand it.
2051 if (!TLI.allowsUnalignedMemoryAccesses()) {
2052 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002053 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002054 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002055 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002056 TLI);
2057 Tmp3 = Result.getOperand(0);
2058 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002059 Tmp3 = LegalizeOp(Tmp3);
2060 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002061 }
2062 }
2063 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002064 case TargetLowering::Custom:
2065 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002066 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002067 Tmp3 = LegalizeOp(Tmp1);
2068 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002069 }
Evan Cheng466685d2006-10-09 20:57:25 +00002070 break;
2071 case TargetLowering::Promote: {
2072 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002073 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002074 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002075 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002076
2077 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002078 LD->getSrcValueOffset(),
2079 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002080 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2081 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002082 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002083 }
Evan Cheng466685d2006-10-09 20:57:25 +00002084 }
2085 // Since loads produce two values, make sure to remember that we
2086 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002087 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2088 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002089 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002090 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002091 MVT SrcVT = LD->getMemoryVT();
2092 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002093 int SVOffset = LD->getSrcValueOffset();
2094 unsigned Alignment = LD->getAlignment();
2095 bool isVolatile = LD->isVolatile();
2096
Duncan Sands83ec4b62008-06-06 12:08:01 +00002097 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002098 // Some targets pretend to have an i1 loading operation, and actually
2099 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2100 // bits are guaranteed to be zero; it helps the optimizers understand
2101 // that these bits are zero. It is also useful for EXTLOAD, since it
2102 // tells the optimizers that those bits are undefined. It would be
2103 // nice to have an effective generic way of getting these benefits...
2104 // Until such a way is found, don't insist on promoting i1 here.
2105 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002106 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002107 // Promote to a byte-sized load if not loading an integral number of
2108 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002109 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2110 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002111 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002112
2113 // The extra bits are guaranteed to be zero, since we stored them that
2114 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2115
2116 ISD::LoadExtType NewExtType =
2117 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2118
2119 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2120 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2121 NVT, isVolatile, Alignment);
2122
2123 Ch = Result.getValue(1); // The chain.
2124
2125 if (ExtType == ISD::SEXTLOAD)
2126 // Having the top bits zero doesn't help when sign extending.
2127 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2128 Result, DAG.getValueType(SrcVT));
2129 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2130 // All the top bits are guaranteed to be zero - inform the optimizers.
2131 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2132 DAG.getValueType(SrcVT));
2133
2134 Tmp1 = LegalizeOp(Result);
2135 Tmp2 = LegalizeOp(Ch);
2136 } else if (SrcWidth & (SrcWidth - 1)) {
2137 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002138 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002139 "Unsupported extload!");
2140 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2141 assert(RoundWidth < SrcWidth);
2142 unsigned ExtraWidth = SrcWidth - RoundWidth;
2143 assert(ExtraWidth < RoundWidth);
2144 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2145 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002146 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2147 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002148 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002149 unsigned IncrementSize;
2150
2151 if (TLI.isLittleEndian()) {
2152 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2153 // Load the bottom RoundWidth bits.
2154 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2155 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2156 Alignment);
2157
2158 // Load the remaining ExtraWidth bits.
2159 IncrementSize = RoundWidth / 8;
2160 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2161 DAG.getIntPtrConstant(IncrementSize));
2162 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2163 LD->getSrcValue(), SVOffset + IncrementSize,
2164 ExtraVT, isVolatile,
2165 MinAlign(Alignment, IncrementSize));
2166
2167 // Build a factor node to remember that this load is independent of the
2168 // other one.
2169 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2170 Hi.getValue(1));
2171
2172 // Move the top bits to the right place.
2173 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2174 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2175
2176 // Join the hi and lo parts.
2177 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002178 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002179 // Big endian - avoid unaligned loads.
2180 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2181 // Load the top RoundWidth bits.
2182 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2183 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2184 Alignment);
2185
2186 // Load the remaining ExtraWidth bits.
2187 IncrementSize = RoundWidth / 8;
2188 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2189 DAG.getIntPtrConstant(IncrementSize));
2190 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2191 LD->getSrcValue(), SVOffset + IncrementSize,
2192 ExtraVT, isVolatile,
2193 MinAlign(Alignment, IncrementSize));
2194
2195 // Build a factor node to remember that this load is independent of the
2196 // other one.
2197 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2198 Hi.getValue(1));
2199
2200 // Move the top bits to the right place.
2201 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2202 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2203
2204 // Join the hi and lo parts.
2205 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2206 }
2207
2208 Tmp1 = LegalizeOp(Result);
2209 Tmp2 = LegalizeOp(Ch);
2210 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002211 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002212 default: assert(0 && "This action is not supported yet!");
2213 case TargetLowering::Custom:
2214 isCustom = true;
2215 // FALLTHROUGH
2216 case TargetLowering::Legal:
2217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2218 Tmp1 = Result.getValue(0);
2219 Tmp2 = Result.getValue(1);
2220
2221 if (isCustom) {
2222 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002223 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002224 Tmp1 = LegalizeOp(Tmp3);
2225 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2226 }
2227 } else {
2228 // If this is an unaligned load and the target doesn't support it,
2229 // expand it.
2230 if (!TLI.allowsUnalignedMemoryAccesses()) {
2231 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002232 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002233 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002234 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002235 TLI);
2236 Tmp1 = Result.getOperand(0);
2237 Tmp2 = Result.getOperand(1);
2238 Tmp1 = LegalizeOp(Tmp1);
2239 Tmp2 = LegalizeOp(Tmp2);
2240 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002241 }
2242 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002243 break;
2244 case TargetLowering::Expand:
2245 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2246 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002247 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002248 LD->getSrcValueOffset(),
2249 LD->isVolatile(), LD->getAlignment());
2250 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2251 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2252 Tmp2 = LegalizeOp(Load.getValue(1));
2253 break;
2254 }
2255 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2256 // Turn the unsupported load into an EXTLOAD followed by an explicit
2257 // zero/sign extend inreg.
2258 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2259 Tmp1, Tmp2, LD->getSrcValue(),
2260 LD->getSrcValueOffset(), SrcVT,
2261 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002262 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002263 if (ExtType == ISD::SEXTLOAD)
2264 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2265 Result, DAG.getValueType(SrcVT));
2266 else
2267 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2268 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2269 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002270 break;
2271 }
Evan Cheng466685d2006-10-09 20:57:25 +00002272 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002273
Evan Cheng466685d2006-10-09 20:57:25 +00002274 // Since loads produce two values, make sure to remember that we legalized
2275 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002276 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2277 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002278 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002279 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002280 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002281 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002282 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002283 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002284 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002285 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002286 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002287 // 1 -> Hi
2288 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002289 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002290 TLI.getShiftAmountTy()));
2291 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2292 } else {
2293 // 0 -> Lo
2294 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2295 Node->getOperand(0));
2296 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002297 break;
2298 case Expand:
2299 // Get both the low and high parts.
2300 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002301 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002302 Result = Tmp2; // 1 -> Hi
2303 else
2304 Result = Tmp1; // 0 -> Lo
2305 break;
2306 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002307 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002308 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002309
2310 case ISD::CopyToReg:
2311 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002312
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002313 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002314 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002315 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002316 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002317 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002318 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002319 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002320 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002321 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002322 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2324 Tmp3);
2325 } else {
2326 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002327 }
2328
2329 // Since this produces two values, make sure to remember that we legalized
2330 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002331 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2332 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002333 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002334 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002335 break;
2336
2337 case ISD::RET:
2338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002339
2340 // Ensure that libcalls are emitted before a return.
2341 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2342 Tmp1 = LegalizeOp(Tmp1);
2343 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002344
Chris Lattner3e928bb2005-01-07 07:47:09 +00002345 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002346 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002347 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002348 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002349 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002350 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002351 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002352 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002353 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002354 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002355 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002356 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002357
2358 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002359 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002360 std::swap(Lo, Hi);
2361
Gabor Greifba36cb52008-08-28 21:40:38 +00002362 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002363 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2364 else
2365 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002366 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002367 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002368 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002369 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002370 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2371 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002372
Dan Gohman7f321562007-06-25 16:23:39 +00002373 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002374 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002375 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002376 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002377 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002378 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002379 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002380 } else if (NumElems == 1) {
2381 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002382 Tmp2 = ScalarizeVectorOp(Tmp2);
2383 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002384 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002385
2386 // FIXME: Returns of gcc generic vectors smaller than a legal type
2387 // should be returned in integer registers!
2388
Chris Lattnerf87324e2006-04-11 01:31:51 +00002389 // The scalarized value type may not be legal, e.g. it might require
2390 // promotion or expansion. Relegalize the return.
2391 Result = LegalizeOp(Result);
2392 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002393 // FIXME: Returns of gcc generic vectors larger than a legal vector
2394 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002395 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002396 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002397 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002398 Result = LegalizeOp(Result);
2399 }
2400 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002401 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002402 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002403 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002404 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002405 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002406 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002407 }
2408 break;
2409 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002410 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002411 break;
2412 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002413 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002414 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002415 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002416 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2417 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002418 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002419 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002420 break;
2421 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002422 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002423 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002424 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002425 ExpandOp(Node->getOperand(i), Lo, Hi);
2426 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002427 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002428 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002429 NewValues.push_back(Hi);
2430 NewValues.push_back(Node->getOperand(i+1));
2431 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002432 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002433 }
2434 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002435 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002436 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002437
2438 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002439 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002440 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002441 Result = DAG.getNode(ISD::RET, MVT::Other,
2442 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002443 break;
2444 }
2445 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002446
Chris Lattner6862dbc2006-01-29 21:02:23 +00002447 if (Result.getOpcode() == ISD::RET) {
2448 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2449 default: assert(0 && "This action is not supported yet!");
2450 case TargetLowering::Legal: break;
2451 case TargetLowering::Custom:
2452 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002453 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002454 break;
2455 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002456 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002457 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002458 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002459 StoreSDNode *ST = cast<StoreSDNode>(Node);
2460 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2461 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002462 int SVOffset = ST->getSrcValueOffset();
2463 unsigned Alignment = ST->getAlignment();
2464 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002465
Evan Cheng8b2794a2006-10-13 21:14:26 +00002466 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002467 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2468 // FIXME: We shouldn't do this for TargetConstantFP's.
2469 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2470 // to phase ordering between legalized code and the dag combiner. This
2471 // probably means that we need to integrate dag combiner and legalizer
2472 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002473 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002474 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002475 if (CFP->getValueType(0) == MVT::f32 &&
2476 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002477 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002478 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002479 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002480 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2481 SVOffset, isVolatile, Alignment);
2482 break;
2483 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002484 // If this target supports 64-bit registers, do a single 64-bit store.
2485 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002486 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002487 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002488 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2489 SVOffset, isVolatile, Alignment);
2490 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002491 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002492 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2493 // stores. If the target supports neither 32- nor 64-bits, this
2494 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002495 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002496 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2497 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002498 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002499
2500 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2501 SVOffset, isVolatile, Alignment);
2502 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002503 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002504 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002505 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002506
2507 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2508 break;
2509 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002510 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002511 }
2512
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002513 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002514 case Legal: {
2515 Tmp3 = LegalizeOp(ST->getValue());
2516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2517 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002518
Duncan Sands83ec4b62008-06-06 12:08:01 +00002519 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002520 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2521 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002522 case TargetLowering::Legal:
2523 // If this is an unaligned store and the target doesn't support it,
2524 // expand it.
2525 if (!TLI.allowsUnalignedMemoryAccesses()) {
2526 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002527 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002528 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002529 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002530 TLI);
2531 }
2532 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002533 case TargetLowering::Custom:
2534 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002535 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002536 break;
2537 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002538 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002539 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2540 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2541 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002542 ST->getSrcValue(), SVOffset, isVolatile,
2543 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002544 break;
2545 }
2546 break;
2547 }
2548 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002549 if (!ST->getMemoryVT().isVector()) {
2550 // Truncate the value and store the result.
2551 Tmp3 = PromoteOp(ST->getValue());
2552 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2553 SVOffset, ST->getMemoryVT(),
2554 isVolatile, Alignment);
2555 break;
2556 }
2557 // Fall thru to expand for vector
2558 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002559 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002560 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002561
2562 // If this is a vector type, then we have to calculate the increment as
2563 // the product of the element size in bytes, and the number of elements
2564 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002565 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002566 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002567 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002568 MVT InVT = InVal->getValueType(InIx);
2569 unsigned NumElems = InVT.getVectorNumElements();
2570 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002571
Dan Gohman7f321562007-06-25 16:23:39 +00002572 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002573 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002574 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002575 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002576 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002577 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002578 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002579 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002580 Result = LegalizeOp(Result);
2581 break;
2582 } else if (NumElems == 1) {
2583 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002584 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002585 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002586 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002587 // The scalarized value type may not be legal, e.g. it might require
2588 // promotion or expansion. Relegalize the scalar store.
2589 Result = LegalizeOp(Result);
2590 break;
2591 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002592 // Check if we have widen this node with another value
2593 std::map<SDValue, SDValue>::iterator I =
2594 WidenNodes.find(ST->getValue());
2595 if (I != WidenNodes.end()) {
2596 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2597 break;
2598 }
2599 else {
2600 SplitVectorOp(ST->getValue(), Lo, Hi);
2601 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2602 EVT.getSizeInBits()/8;
2603 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002604 }
2605 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002606 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002607 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002608
Richard Pennington4b052dc2008-09-25 16:15:10 +00002609 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002610 std::swap(Lo, Hi);
2611 }
2612
2613 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002614 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002615
Gabor Greifba36cb52008-08-28 21:40:38 +00002616 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002617 // Must be int <-> float one-to-one expansion.
2618 Result = Lo;
2619 break;
2620 }
2621
Evan Cheng8b2794a2006-10-13 21:14:26 +00002622 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002623 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002624 assert(isTypeLegal(Tmp2.getValueType()) &&
2625 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002626 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002627 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002628 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002629 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002630 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2631 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002632 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002633 }
2634 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002635 switch (getTypeAction(ST->getValue().getValueType())) {
2636 case Legal:
2637 Tmp3 = LegalizeOp(ST->getValue());
2638 break;
2639 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002640 if (!ST->getValue().getValueType().isVector()) {
2641 // We can promote the value, the truncstore will still take care of it.
2642 Tmp3 = PromoteOp(ST->getValue());
2643 break;
2644 }
2645 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002646 case Expand:
2647 // Just store the low part. This may become a non-trunc store, so make
2648 // sure to use getTruncStore, not UpdateNodeOperands below.
2649 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2650 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2651 SVOffset, MVT::i8, isVolatile, Alignment);
2652 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002653
Duncan Sands83ec4b62008-06-06 12:08:01 +00002654 MVT StVT = ST->getMemoryVT();
2655 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002656
Duncan Sands83ec4b62008-06-06 12:08:01 +00002657 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002658 // Promote to a byte-sized store with upper bits zero if not
2659 // storing an integral number of bytes. For example, promote
2660 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002661 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002662 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2663 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2664 SVOffset, NVT, isVolatile, Alignment);
2665 } else if (StWidth & (StWidth - 1)) {
2666 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002667 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002668 "Unsupported truncstore!");
2669 unsigned RoundWidth = 1 << Log2_32(StWidth);
2670 assert(RoundWidth < StWidth);
2671 unsigned ExtraWidth = StWidth - RoundWidth;
2672 assert(ExtraWidth < RoundWidth);
2673 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2674 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002675 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2676 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002677 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002678 unsigned IncrementSize;
2679
2680 if (TLI.isLittleEndian()) {
2681 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2682 // Store the bottom RoundWidth bits.
2683 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2684 SVOffset, RoundVT,
2685 isVolatile, Alignment);
2686
2687 // Store the remaining ExtraWidth bits.
2688 IncrementSize = RoundWidth / 8;
2689 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2690 DAG.getIntPtrConstant(IncrementSize));
2691 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2692 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2693 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2694 SVOffset + IncrementSize, ExtraVT, isVolatile,
2695 MinAlign(Alignment, IncrementSize));
2696 } else {
2697 // Big endian - avoid unaligned stores.
2698 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2699 // Store the top RoundWidth bits.
2700 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2701 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2702 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2703 RoundVT, isVolatile, Alignment);
2704
2705 // Store the remaining ExtraWidth bits.
2706 IncrementSize = RoundWidth / 8;
2707 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2708 DAG.getIntPtrConstant(IncrementSize));
2709 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2710 SVOffset + IncrementSize, ExtraVT, isVolatile,
2711 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002712 }
Duncan Sands7e857202008-01-22 07:17:34 +00002713
2714 // The order of the stores doesn't matter.
2715 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2716 } else {
2717 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2718 Tmp2 != ST->getBasePtr())
2719 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2720 ST->getOffset());
2721
2722 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2723 default: assert(0 && "This action is not supported yet!");
2724 case TargetLowering::Legal:
2725 // If this is an unaligned store and the target doesn't support it,
2726 // expand it.
2727 if (!TLI.allowsUnalignedMemoryAccesses()) {
2728 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002729 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002730 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002731 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002732 TLI);
2733 }
2734 break;
2735 case TargetLowering::Custom:
2736 Result = TLI.LowerOperation(Result, DAG);
2737 break;
2738 case Expand:
2739 // TRUNCSTORE:i16 i32 -> STORE i16
2740 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2741 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2742 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2743 isVolatile, Alignment);
2744 break;
2745 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002746 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002747 }
2748 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002749 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002750 case ISD::PCMARKER:
2751 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002752 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002753 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002754 case ISD::STACKSAVE:
2755 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002756 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2757 Tmp1 = Result.getValue(0);
2758 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002759
Chris Lattner140d53c2006-01-13 02:50:02 +00002760 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2761 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002762 case TargetLowering::Legal: break;
2763 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002764 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002765 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002766 Tmp1 = LegalizeOp(Tmp3);
2767 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002768 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002769 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002770 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002771 // Expand to CopyFromReg if the target set
2772 // StackPointerRegisterToSaveRestore.
2773 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002774 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002775 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002776 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002777 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002778 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2779 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002780 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002781 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002782 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002783
2784 // Since stacksave produce two values, make sure to remember that we
2785 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002786 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2787 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002788 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002789
Chris Lattner140d53c2006-01-13 02:50:02 +00002790 case ISD::STACKRESTORE:
2791 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2792 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002793 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002794
2795 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2796 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002797 case TargetLowering::Legal: break;
2798 case TargetLowering::Custom:
2799 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002800 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002801 break;
2802 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002803 // Expand to CopyToReg if the target set
2804 // StackPointerRegisterToSaveRestore.
2805 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2806 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2807 } else {
2808 Result = Tmp1;
2809 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002810 break;
2811 }
2812 break;
2813
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002814 case ISD::READCYCLECOUNTER:
2815 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002816 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002817 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2818 Node->getValueType(0))) {
2819 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002820 case TargetLowering::Legal:
2821 Tmp1 = Result.getValue(0);
2822 Tmp2 = Result.getValue(1);
2823 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002824 case TargetLowering::Custom:
2825 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002826 Tmp1 = LegalizeOp(Result.getValue(0));
2827 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002828 break;
2829 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002830
2831 // Since rdcc produce two values, make sure to remember that we legalized
2832 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002833 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2834 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002835 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002836
Chris Lattner2ee743f2005-01-14 22:08:15 +00002837 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002838 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2839 case Expand: assert(0 && "It's impossible to expand bools");
2840 case Legal:
2841 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2842 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002843 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002844 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002845 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002846 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002847 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002848 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002849 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002850 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002851 break;
2852 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002853 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002854 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002855 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002856
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002857 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002858
Nate Begemanb942a3d2005-08-23 04:29:48 +00002859 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002860 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002861 case TargetLowering::Legal: break;
2862 case TargetLowering::Custom: {
2863 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002864 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002865 break;
2866 }
Nate Begeman9373a812005-08-10 20:51:12 +00002867 case TargetLowering::Expand:
2868 if (Tmp1.getOpcode() == ISD::SETCC) {
2869 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2870 Tmp2, Tmp3,
2871 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2872 } else {
2873 Result = DAG.getSelectCC(Tmp1,
2874 DAG.getConstant(0, Tmp1.getValueType()),
2875 Tmp2, Tmp3, ISD::SETNE);
2876 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002877 break;
2878 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002879 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002880 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2881 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002882 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002883 ExtOp = ISD::BIT_CONVERT;
2884 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002885 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002886 ExtOp = ISD::ANY_EXTEND;
2887 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002888 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002889 ExtOp = ISD::FP_EXTEND;
2890 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002891 }
2892 // Promote each of the values to the new type.
2893 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2894 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2895 // Perform the larger operation, then round down.
2896 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002897 if (TruncOp != ISD::FP_ROUND)
2898 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2899 else
2900 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2901 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002902 break;
2903 }
2904 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002905 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002906 case ISD::SELECT_CC: {
2907 Tmp1 = Node->getOperand(0); // LHS
2908 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002909 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2910 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002911 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002912
Dale Johannesen53e4e442008-11-07 22:54:33 +00002913 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002914
Evan Cheng7f042682008-10-15 02:05:31 +00002915 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002916 // the LHS is a legal SETCC itself. In this case, we need to compare
2917 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002918 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002919 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2920 CC = DAG.getCondCode(ISD::SETNE);
2921 }
2922 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2923
2924 // Everything is legal, see if we should expand this op or something.
2925 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2926 default: assert(0 && "This action is not supported yet!");
2927 case TargetLowering::Legal: break;
2928 case TargetLowering::Custom:
2929 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002930 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002931 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002932 }
2933 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002934 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002935 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002936 Tmp1 = Node->getOperand(0);
2937 Tmp2 = Node->getOperand(1);
2938 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00002939 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002940
2941 // If we had to Expand the SetCC operands into a SELECT node, then it may
2942 // not always be possible to return a true LHS & RHS. In this case, just
2943 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002944 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002945 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002946 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002947 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002948
Chris Lattner73e142f2006-01-30 22:43:50 +00002949 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002950 default: assert(0 && "Cannot handle this action for SETCC yet!");
2951 case TargetLowering::Custom:
2952 isCustom = true;
2953 // FALLTHROUGH.
2954 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002955 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002956 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002957 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002958 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002959 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002960 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002961 case TargetLowering::Promote: {
2962 // First step, figure out the appropriate operation to use.
2963 // Allow SETCC to not be supported for all legal data types
2964 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002965 MVT NewInTy = Node->getOperand(0).getValueType();
2966 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002967
2968 // Scan for the appropriate larger type to use.
2969 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002970 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002971
Duncan Sands83ec4b62008-06-06 12:08:01 +00002972 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002973 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002974 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002975 "Fell off of the edge of the floating point world");
2976
2977 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002978 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002979 break;
2980 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002981 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002982 assert(0 && "Cannot promote Legal Integer SETCC yet");
2983 else {
2984 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2985 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2986 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002987 Tmp1 = LegalizeOp(Tmp1);
2988 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002989 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002990 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002991 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002992 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002993 case TargetLowering::Expand:
2994 // Expand a setcc node into a select_cc of the same condition, lhs, and
2995 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002996 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002997 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
2998 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00002999 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003000 break;
3001 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003002 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003003 case ISD::VSETCC: {
3004 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3005 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003006 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003007
3008 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3009
3010 // Everything is legal, see if we should expand this op or something.
3011 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3012 default: assert(0 && "This action is not supported yet!");
3013 case TargetLowering::Legal: break;
3014 case TargetLowering::Custom:
3015 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003016 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003017 break;
3018 }
3019 break;
3020 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003021
Chris Lattner5b359c62005-04-02 04:00:59 +00003022 case ISD::SHL_PARTS:
3023 case ISD::SRA_PARTS:
3024 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003025 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003026 bool Changed = false;
3027 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3028 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3029 Changed |= Ops.back() != Node->getOperand(i);
3030 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003031 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003032 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003033
Evan Cheng05a2d562006-01-09 18:31:59 +00003034 switch (TLI.getOperationAction(Node->getOpcode(),
3035 Node->getValueType(0))) {
3036 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003037 case TargetLowering::Legal: break;
3038 case TargetLowering::Custom:
3039 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003040 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003041 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003042 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003043 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003044 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003045 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003046 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003047 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003048 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003049 return RetVal;
3050 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003051 break;
3052 }
3053
Chris Lattner2c8086f2005-04-02 05:00:07 +00003054 // Since these produce multiple values, make sure to remember that we
3055 // legalized all of them.
3056 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003057 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003058 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003059 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003060
3061 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003062 case ISD::ADD:
3063 case ISD::SUB:
3064 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003065 case ISD::MULHS:
3066 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003067 case ISD::UDIV:
3068 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003069 case ISD::AND:
3070 case ISD::OR:
3071 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003072 case ISD::SHL:
3073 case ISD::SRL:
3074 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003075 case ISD::FADD:
3076 case ISD::FSUB:
3077 case ISD::FMUL:
3078 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003079 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003080 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003081 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3082 case Expand: assert(0 && "Not possible");
3083 case Legal:
3084 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3085 break;
3086 case Promote:
3087 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3088 break;
3089 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003090
3091 if ((Node->getOpcode() == ISD::SHL ||
3092 Node->getOpcode() == ISD::SRL ||
3093 Node->getOpcode() == ISD::SRA) &&
3094 !Node->getValueType(0).isVector()) {
3095 if (TLI.getShiftAmountTy().bitsLT(Tmp2.getValueType()))
3096 Tmp2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Tmp2);
3097 else if (TLI.getShiftAmountTy().bitsGT(Tmp2.getValueType()))
3098 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Tmp2);
3099 }
3100
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003101 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00003102
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003103 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003104 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003105 case TargetLowering::Legal: break;
3106 case TargetLowering::Custom:
3107 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003108 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003109 Result = Tmp1;
3110 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003111 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003112 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003113 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003114 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003115
Dan Gohman525178c2007-10-08 18:33:35 +00003116 // See if multiply or divide can be lowered using two-result operations.
3117 SDVTList VTs = DAG.getVTList(VT, VT);
3118 if (Node->getOpcode() == ISD::MUL) {
3119 // We just need the low half of the multiply; try both the signed
3120 // and unsigned forms. If the target supports both SMUL_LOHI and
3121 // UMUL_LOHI, form a preference by checking which forms of plain
3122 // MULH it supports.
3123 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3124 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3125 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3126 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3127 unsigned OpToUse = 0;
3128 if (HasSMUL_LOHI && !HasMULHS) {
3129 OpToUse = ISD::SMUL_LOHI;
3130 } else if (HasUMUL_LOHI && !HasMULHU) {
3131 OpToUse = ISD::UMUL_LOHI;
3132 } else if (HasSMUL_LOHI) {
3133 OpToUse = ISD::SMUL_LOHI;
3134 } else if (HasUMUL_LOHI) {
3135 OpToUse = ISD::UMUL_LOHI;
3136 }
3137 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003138 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003139 break;
3140 }
3141 }
3142 if (Node->getOpcode() == ISD::MULHS &&
3143 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003144 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3145 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003146 break;
3147 }
3148 if (Node->getOpcode() == ISD::MULHU &&
3149 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003150 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3151 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003152 break;
3153 }
3154 if (Node->getOpcode() == ISD::SDIV &&
3155 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003156 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3157 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003158 break;
3159 }
3160 if (Node->getOpcode() == ISD::UDIV &&
3161 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003162 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3163 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003164 break;
3165 }
Mon P Wang0c397192008-10-30 08:01:45 +00003166
Dan Gohman82669522007-10-11 23:57:53 +00003167 // Check to see if we have a libcall for this operator.
3168 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3169 bool isSigned = false;
3170 switch (Node->getOpcode()) {
3171 case ISD::UDIV:
3172 case ISD::SDIV:
3173 if (VT == MVT::i32) {
3174 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003175 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003176 isSigned = Node->getOpcode() == ISD::SDIV;
3177 }
3178 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003179 case ISD::MUL:
3180 if (VT == MVT::i32)
3181 LC = RTLIB::MUL_I32;
3182 break;
Dan Gohman82669522007-10-11 23:57:53 +00003183 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003184 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3185 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003186 break;
3187 default: break;
3188 }
3189 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003190 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003191 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003192 break;
3193 }
Mon P Wang0c397192008-10-30 08:01:45 +00003194
Duncan Sands83ec4b62008-06-06 12:08:01 +00003195 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003196 "Cannot expand this binary operator!");
3197 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003198 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003199 break;
3200 }
Evan Chengcc987612006-04-12 21:20:24 +00003201 case TargetLowering::Promote: {
3202 switch (Node->getOpcode()) {
3203 default: assert(0 && "Do not know how to promote this BinOp!");
3204 case ISD::AND:
3205 case ISD::OR:
3206 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003207 MVT OVT = Node->getValueType(0);
3208 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3209 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003210 // Bit convert each of the values to the new type.
3211 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3212 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3213 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3214 // Bit convert the result back the original type.
3215 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3216 break;
3217 }
3218 }
3219 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003220 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003221 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003222
Dan Gohmane14ea862007-10-05 14:17:22 +00003223 case ISD::SMUL_LOHI:
3224 case ISD::UMUL_LOHI:
3225 case ISD::SDIVREM:
3226 case ISD::UDIVREM:
3227 // These nodes will only be produced by target-specific lowering, so
3228 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003229 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003230 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003231
3232 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3233 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3234 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003235 break;
3236
Chris Lattnera09f8482006-03-05 05:09:38 +00003237 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3238 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3239 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3240 case Expand: assert(0 && "Not possible");
3241 case Legal:
3242 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3243 break;
3244 case Promote:
3245 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3246 break;
3247 }
3248
3249 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3250
3251 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3252 default: assert(0 && "Operation not supported");
3253 case TargetLowering::Custom:
3254 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003255 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003256 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003257 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003258 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003259 // If this target supports fabs/fneg natively and select is cheap,
3260 // do this efficiently.
3261 if (!TLI.isSelectExpensive() &&
3262 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3263 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003264 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003265 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003266 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003267 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003268 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003269 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003270 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003271 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3272 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003273 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003274 // Select between the nabs and abs value based on the sign bit of
3275 // the input.
3276 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3277 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3278 AbsVal),
3279 AbsVal);
3280 Result = LegalizeOp(Result);
3281 break;
3282 }
3283
3284 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003285 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003286 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3287 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3288 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3289 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003290 break;
3291 }
Evan Cheng912095b2007-01-04 21:56:39 +00003292 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003293 break;
3294
Nate Begeman551bf3f2006-02-17 05:43:56 +00003295 case ISD::ADDC:
3296 case ISD::SUBC:
3297 Tmp1 = LegalizeOp(Node->getOperand(0));
3298 Tmp2 = LegalizeOp(Node->getOperand(1));
3299 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3300 // Since this produces two values, make sure to remember that we legalized
3301 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003302 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3303 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Nate Begeman551bf3f2006-02-17 05:43:56 +00003304 return Result;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003305
Nate Begeman551bf3f2006-02-17 05:43:56 +00003306 case ISD::ADDE:
3307 case ISD::SUBE:
3308 Tmp1 = LegalizeOp(Node->getOperand(0));
3309 Tmp2 = LegalizeOp(Node->getOperand(1));
3310 Tmp3 = LegalizeOp(Node->getOperand(2));
3311 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
3312 // Since this produces two values, make sure to remember that we legalized
3313 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003314 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
3315 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Nate Begeman551bf3f2006-02-17 05:43:56 +00003316 return Result;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003317
Nate Begeman419f8b62005-10-18 00:27:41 +00003318 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003319 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003320 // TODO: handle the case where the Lo and Hi operands are not of legal type
3321 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3322 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3323 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003324 case TargetLowering::Promote:
3325 case TargetLowering::Custom:
3326 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003327 case TargetLowering::Legal:
3328 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3329 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3330 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003331 case TargetLowering::Expand:
3332 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3333 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3334 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003335 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003336 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003337 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003338 break;
3339 }
3340 break;
3341 }
3342
Nate Begemanc105e192005-04-06 00:23:54 +00003343 case ISD::UREM:
3344 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003345 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003346 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3347 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003348
Nate Begemanc105e192005-04-06 00:23:54 +00003349 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003350 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3351 case TargetLowering::Custom:
3352 isCustom = true;
3353 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003354 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003355 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003356 if (isCustom) {
3357 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003358 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003359 }
Nate Begemanc105e192005-04-06 00:23:54 +00003360 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003361 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003362 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003363 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003364 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003365
3366 // See if remainder can be lowered using two-result operations.
3367 SDVTList VTs = DAG.getVTList(VT, VT);
3368 if (Node->getOpcode() == ISD::SREM &&
3369 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003370 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003371 break;
3372 }
3373 if (Node->getOpcode() == ISD::UREM &&
3374 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003375 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003376 break;
3377 }
3378
Duncan Sands83ec4b62008-06-06 12:08:01 +00003379 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003380 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003381 TargetLowering::Legal) {
3382 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003383 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003384 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3385 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003386 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003387 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003388 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003389 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003390 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003391 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3392 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003393 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003394 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003395 }
Dan Gohman0d974262007-11-06 22:11:54 +00003396 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003397 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003398 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003399 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003400 Result = LegalizeOp(UnrollVectorOp(Op));
3401 } else {
3402 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003403 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3404 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003405 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003406 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003407 }
Nate Begemanc105e192005-04-06 00:23:54 +00003408 }
3409 break;
3410 }
Dan Gohman525178c2007-10-08 18:33:35 +00003411 }
Nate Begemanc105e192005-04-06 00:23:54 +00003412 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003413 case ISD::VAARG: {
3414 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3415 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3416
Duncan Sands83ec4b62008-06-06 12:08:01 +00003417 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003418 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3419 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003420 case TargetLowering::Custom:
3421 isCustom = true;
3422 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003423 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003424 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3425 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003426 Tmp1 = Result.getValue(1);
3427
3428 if (isCustom) {
3429 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003430 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003431 Result = LegalizeOp(Tmp2);
3432 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3433 }
3434 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003435 break;
3436 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003437 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003438 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003439 // Increment the pointer, VAList, to the next vaarg
Duncan Sands5c58a312008-11-03 11:51:11 +00003440 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3441 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3442 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003443 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003444 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003445 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003446 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003447 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003448 Result = LegalizeOp(Result);
3449 break;
3450 }
3451 }
3452 // Since VAARG produces two values, make sure to remember that we
3453 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003454 AddLegalizedOperand(SDValue(Node, 0), Result);
3455 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003456 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003457 }
3458
3459 case ISD::VACOPY:
3460 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3461 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3462 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3463
3464 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3465 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003466 case TargetLowering::Custom:
3467 isCustom = true;
3468 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003469 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003470 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3471 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003472 if (isCustom) {
3473 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003474 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003475 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003476 break;
3477 case TargetLowering::Expand:
3478 // This defaults to loading a pointer from the input and storing it to the
3479 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003480 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3481 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003482 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3483 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003484 break;
3485 }
3486 break;
3487
3488 case ISD::VAEND:
3489 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3490 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3491
3492 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3493 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003494 case TargetLowering::Custom:
3495 isCustom = true;
3496 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003497 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003499 if (isCustom) {
3500 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003501 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003502 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003503 break;
3504 case TargetLowering::Expand:
3505 Result = Tmp1; // Default to a no-op, return the chain
3506 break;
3507 }
3508 break;
3509
3510 case ISD::VASTART:
3511 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3512 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3513
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003514 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3515
Nate Begemanacc398c2006-01-25 18:21:52 +00003516 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3517 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003518 case TargetLowering::Legal: break;
3519 case TargetLowering::Custom:
3520 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003521 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003522 break;
3523 }
3524 break;
3525
Nate Begeman35ef9132006-01-11 21:21:00 +00003526 case ISD::ROTL:
3527 case ISD::ROTR:
3528 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3529 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003530 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003531 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3532 default:
3533 assert(0 && "ROTL/ROTR legalize operation not supported");
3534 break;
3535 case TargetLowering::Legal:
3536 break;
3537 case TargetLowering::Custom:
3538 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003539 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003540 break;
3541 case TargetLowering::Promote:
3542 assert(0 && "Do not know how to promote ROTL/ROTR");
3543 break;
3544 case TargetLowering::Expand:
3545 assert(0 && "Do not know how to expand ROTL/ROTR");
3546 break;
3547 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003548 break;
3549
Nate Begemand88fc032006-01-14 03:14:10 +00003550 case ISD::BSWAP:
3551 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3552 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003553 case TargetLowering::Custom:
3554 assert(0 && "Cannot custom legalize this yet!");
3555 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003556 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003557 break;
3558 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003559 MVT OVT = Tmp1.getValueType();
3560 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3561 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003562
Chris Lattner456a93a2006-01-28 07:39:30 +00003563 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3564 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3565 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3566 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3567 break;
3568 }
3569 case TargetLowering::Expand:
3570 Result = ExpandBSWAP(Tmp1);
3571 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003572 }
3573 break;
3574
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003575 case ISD::CTPOP:
3576 case ISD::CTTZ:
3577 case ISD::CTLZ:
3578 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3579 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003580 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003581 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003582 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003583 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003584 TargetLowering::Custom) {
3585 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003586 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003587 Result = Tmp1;
3588 }
Scott Michel910b66d2007-07-30 21:00:31 +00003589 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003590 break;
3591 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003592 MVT OVT = Tmp1.getValueType();
3593 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003594
3595 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003596 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3597 // Perform the larger operation, then subtract if needed.
3598 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003599 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003600 case ISD::CTPOP:
3601 Result = Tmp1;
3602 break;
3603 case ISD::CTTZ:
3604 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003605 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003606 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003607 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003608 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003609 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003610 break;
3611 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003612 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003613 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003614 DAG.getConstant(NVT.getSizeInBits() -
3615 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003616 break;
3617 }
3618 break;
3619 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003620 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003621 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003622 break;
3623 }
3624 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003625
Chris Lattner2c8086f2005-04-02 05:00:07 +00003626 // Unary operators
3627 case ISD::FABS:
3628 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003629 case ISD::FSQRT:
3630 case ISD::FSIN:
3631 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003632 case ISD::FLOG:
3633 case ISD::FLOG2:
3634 case ISD::FLOG10:
3635 case ISD::FEXP:
3636 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003637 case ISD::FTRUNC:
3638 case ISD::FFLOOR:
3639 case ISD::FCEIL:
3640 case ISD::FRINT:
3641 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003642 Tmp1 = LegalizeOp(Node->getOperand(0));
3643 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003644 case TargetLowering::Promote:
3645 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003646 isCustom = true;
3647 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003648 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003649 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003650 if (isCustom) {
3651 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003652 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003653 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003654 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003655 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003656 switch (Node->getOpcode()) {
3657 default: assert(0 && "Unreachable!");
3658 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003659 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3660 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003661 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003662 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003663 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003664 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003665 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003666 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003667 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003668 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003669 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3670 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003671 break;
3672 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003673 case ISD::FSQRT:
3674 case ISD::FSIN:
3675 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003676 case ISD::FLOG:
3677 case ISD::FLOG2:
3678 case ISD::FLOG10:
3679 case ISD::FEXP:
3680 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003681 case ISD::FTRUNC:
3682 case ISD::FFLOOR:
3683 case ISD::FCEIL:
3684 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003685 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003686 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003687
3688 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003689 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003690 Result = LegalizeOp(UnrollVectorOp(Op));
3691 break;
3692 }
3693
Evan Cheng56966222007-01-12 02:11:51 +00003694 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003695 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003696 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003697 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3698 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003699 break;
3700 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003701 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3702 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003703 break;
3704 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003705 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3706 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003707 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003708 case ISD::FLOG:
3709 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3710 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3711 break;
3712 case ISD::FLOG2:
3713 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3714 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3715 break;
3716 case ISD::FLOG10:
3717 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3718 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3719 break;
3720 case ISD::FEXP:
3721 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3722 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3723 break;
3724 case ISD::FEXP2:
3725 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3726 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3727 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003728 case ISD::FTRUNC:
3729 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3730 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3731 break;
3732 case ISD::FFLOOR:
3733 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3734 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3735 break;
3736 case ISD::FCEIL:
3737 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3738 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3739 break;
3740 case ISD::FRINT:
3741 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3742 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3743 break;
3744 case ISD::FNEARBYINT:
3745 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3746 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3747 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003748 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003749 default: assert(0 && "Unreachable!");
3750 }
Dan Gohman475871a2008-07-27 21:46:04 +00003751 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003752 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003753 break;
3754 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003755 }
3756 break;
3757 }
3758 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003759 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003760 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003761
3762 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003763 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003764 Result = LegalizeOp(UnrollVectorOp(Op));
3765 break;
3766 }
3767
3768 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003769 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3770 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003771 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003772 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003773 break;
3774 }
Chris Lattner35481892005-12-23 00:16:34 +00003775 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003776 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003777 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3778 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003779 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003780 // The input has to be a vector type, we have to either scalarize it, pack
3781 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003782 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003783 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003784 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3785 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003786
3787 // Figure out if there is a simple type corresponding to this Vector
3788 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003789 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003790 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003791 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003792 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3793 LegalizeOp(Node->getOperand(0)));
3794 break;
3795 } else if (NumElems == 1) {
3796 // Turn this into a bit convert of the scalar input.
3797 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3798 ScalarizeVectorOp(Node->getOperand(0)));
3799 break;
3800 } else {
3801 // FIXME: UNIMP! Store then reload
3802 assert(0 && "Cast from unsupported vector type not implemented yet!");
3803 }
Chris Lattner67993f72006-01-23 07:30:46 +00003804 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003805 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3806 Node->getOperand(0).getValueType())) {
3807 default: assert(0 && "Unknown operation action!");
3808 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003809 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3810 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003811 break;
3812 case TargetLowering::Legal:
3813 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003814 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003815 break;
3816 }
3817 }
3818 break;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00003819
Chris Lattner2c8086f2005-04-02 05:00:07 +00003820 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003821 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003822 case ISD::UINT_TO_FP: {
3823 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003824 Result = LegalizeINT_TO_FP(Result, isSigned,
3825 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003826 break;
3827 }
3828 case ISD::TRUNCATE:
3829 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3830 case Legal:
3831 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003832 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003833 break;
3834 case Expand:
3835 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3836
3837 // Since the result is legal, we should just be able to truncate the low
3838 // part of the source.
3839 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3840 break;
3841 case Promote:
3842 Result = PromoteOp(Node->getOperand(0));
3843 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3844 break;
3845 }
3846 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003847
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003848 case ISD::FP_TO_SINT:
3849 case ISD::FP_TO_UINT:
3850 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3851 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003852 Tmp1 = LegalizeOp(Node->getOperand(0));
3853
Chris Lattner1618beb2005-07-29 00:11:56 +00003854 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3855 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003856 case TargetLowering::Custom:
3857 isCustom = true;
3858 // FALLTHROUGH
3859 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003860 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003861 if (isCustom) {
3862 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003863 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003864 }
3865 break;
3866 case TargetLowering::Promote:
3867 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3868 Node->getOpcode() == ISD::FP_TO_SINT);
3869 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003870 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003871 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00003872 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003873 MVT VT = Node->getOperand(0).getValueType();
3874 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003875 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00003876 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3877 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003878 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003879 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003880 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00003881 Node->getOperand(0), Tmp2, ISD::SETLT);
3882 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3883 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003884 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003885 Tmp2));
3886 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003887 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003888 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3889 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003890 } else {
3891 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3892 }
3893 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003894 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003895 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003896 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003897 MVT VT = Op.getValueType();
3898 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003899 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003900 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003901 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3902 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3903 Node->getOperand(0), DAG.getValueType(MVT::f64));
3904 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3905 DAG.getIntPtrConstant(1));
3906 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3907 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003908 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3909 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3910 Tmp2 = DAG.getConstantFP(apf, OVT);
3911 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3912 // FIXME: generated code sucks.
3913 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3914 DAG.getNode(ISD::ADD, MVT::i32,
3915 DAG.getNode(ISD::FP_TO_SINT, VT,
3916 DAG.getNode(ISD::FSUB, OVT,
3917 Node->getOperand(0), Tmp2)),
3918 DAG.getConstant(0x80000000, MVT::i32)),
3919 DAG.getNode(ISD::FP_TO_SINT, VT,
3920 Node->getOperand(0)),
3921 DAG.getCondCode(ISD::SETGE));
3922 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003923 break;
3924 }
Dan Gohmana2e94852008-03-10 23:03:31 +00003925 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00003926 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
3927 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
3928 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00003929 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003930 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00003931 break;
3932 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003933 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003934 Tmp1 = PromoteOp(Node->getOperand(0));
3935 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
3936 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003937 break;
3938 }
3939 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003940
Chris Lattnerf2670a82008-01-16 06:57:07 +00003941 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003942 MVT DstVT = Op.getValueType();
3943 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003944 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3945 // The only other way we can lower this is to turn it into a STORE,
3946 // LOAD pair, targetting a temporary location (a stack slot).
3947 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
3948 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00003949 }
3950 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3951 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3952 case Legal:
3953 Tmp1 = LegalizeOp(Node->getOperand(0));
3954 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3955 break;
3956 case Promote:
3957 Tmp1 = PromoteOp(Node->getOperand(0));
3958 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
3959 break;
3960 }
3961 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003962 }
Dale Johannesen5411a392007-08-09 01:04:01 +00003963 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003964 MVT DstVT = Op.getValueType();
3965 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00003966 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
3967 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00003968 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003969 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00003970 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00003971 if (DstVT!=MVT::f64)
3972 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00003973 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00003974 }
Chris Lattner0bd48932008-01-17 07:00:52 +00003975 // The only other way we can lower this is to turn it into a STORE,
3976 // LOAD pair, targetting a temporary location (a stack slot).
3977 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
3978 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00003979 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00003980 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3981 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3982 case Legal:
3983 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003984 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003985 break;
3986 case Promote:
3987 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00003988 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
3989 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00003990 break;
3991 }
3992 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00003993 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003994 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003995 case ISD::ZERO_EXTEND:
3996 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003997 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003998 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003999 case Legal:
4000 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004001 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004002 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4003 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004004 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004005 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004006 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004007 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004008 case Promote:
4009 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004010 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004011 Tmp1 = PromoteOp(Node->getOperand(0));
4012 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004013 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004014 case ISD::ZERO_EXTEND:
4015 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004016 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004017 Result = DAG.getZeroExtendInReg(Result,
4018 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004019 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004020 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004021 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004022 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004023 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004024 Result,
4025 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004026 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004027 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004028 }
4029 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004030 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004031 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004032 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004033 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004034
4035 // If this operation is not supported, convert it to a shl/shr or load/store
4036 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004037 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4038 default: assert(0 && "This action not supported for this op yet!");
4039 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004040 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004041 break;
4042 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004043 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004044 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004045 // NOTE: we could fall back on load/store here too for targets without
4046 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004047 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4048 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004049 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004050 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4051 Node->getOperand(0), ShiftCst);
4052 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4053 Result, ShiftCst);
4054 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004055 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004056 // EXTLOAD pair, targetting a temporary location (a stack slot).
4057
4058 // NOTE: there is a choice here between constantly creating new stack
4059 // slots and always reusing the same one. We currently always create
4060 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004061 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4062 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004063 } else {
4064 assert(0 && "Unknown op");
4065 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004066 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004067 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004068 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004069 }
Duncan Sands36397f52007-07-27 12:58:54 +00004070 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004071 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004072 for (unsigned i = 0; i != 6; ++i)
4073 Ops[i] = LegalizeOp(Node->getOperand(i));
4074 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4075 // The only option for this node is to custom lower it.
4076 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004077 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004078
4079 // Since trampoline produces two values, make sure to remember that we
4080 // legalized both of them.
4081 Tmp1 = LegalizeOp(Result.getValue(1));
4082 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004083 AddLegalizedOperand(SDValue(Node, 0), Result);
4084 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004085 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004086 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004087 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004088 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004089 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4090 default: assert(0 && "This action not supported for this op yet!");
4091 case TargetLowering::Custom:
4092 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004093 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004094 // Fall Thru
4095 case TargetLowering::Legal:
4096 // If this operation is not supported, lower it to constant 1
4097 Result = DAG.getConstant(1, VT);
4098 break;
4099 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004100 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004101 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004102 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004103 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004104 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4105 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004106 case TargetLowering::Legal:
4107 Tmp1 = LegalizeOp(Node->getOperand(0));
4108 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4109 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004110 case TargetLowering::Custom:
4111 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004112 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004113 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004114 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004115 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004116 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004117 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004118 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004119 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004120 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004121 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner034f12e2008-01-15 22:09:33 +00004122 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004123 Result = CallResult.second;
4124 break;
4125 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004126 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004127 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00004128 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004129
Chris Lattner4ddd2832006-04-08 04:13:17 +00004130 assert(Result.getValueType() == Op.getValueType() &&
4131 "Bad legalization!");
4132
Chris Lattner456a93a2006-01-28 07:39:30 +00004133 // Make sure that the generated code is itself legal.
4134 if (Result != Op)
4135 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004136
Chris Lattner45982da2005-05-12 16:53:42 +00004137 // Note that LegalizeOp may be reentered even from single-use nodes, which
4138 // means that we always must cache transformed nodes.
4139 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004140 return Result;
4141}
4142
Chris Lattner8b6fa222005-01-15 22:16:26 +00004143/// PromoteOp - Given an operation that produces a value in an invalid type,
4144/// promote it to compute the value into a larger type. The produced value will
4145/// have the correct bits for the low portion of the register, but no guarantee
4146/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004147SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004148 MVT VT = Op.getValueType();
4149 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004150 assert(getTypeAction(VT) == Promote &&
4151 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004152 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004153 "Cannot promote to smaller type!");
4154
Dan Gohman475871a2008-07-27 21:46:04 +00004155 SDValue Tmp1, Tmp2, Tmp3;
4156 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004157 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004158
Dan Gohman475871a2008-07-27 21:46:04 +00004159 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004160 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004161
Chris Lattner03c85462005-01-15 05:21:40 +00004162 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004163 case ISD::CopyFromReg:
4164 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004165 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004166#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004167 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004168#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004169 assert(0 && "Do not know how to promote this operator!");
4170 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004171 case ISD::UNDEF:
4172 Result = DAG.getNode(ISD::UNDEF, NVT);
4173 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004174 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004175 if (VT != MVT::i1)
4176 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4177 else
4178 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004179 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4180 break;
4181 case ISD::ConstantFP:
4182 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4183 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4184 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004185
Chris Lattner82fbfb62005-01-18 02:59:52 +00004186 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004187 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004188 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004189 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004190 TLI.getSetCCResultType(Node->getOperand(0)),
4191 Node->getOperand(0), Node->getOperand(1),
4192 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004193 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004194
Chris Lattner03c85462005-01-15 05:21:40 +00004195 case ISD::TRUNCATE:
4196 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4197 case Legal:
4198 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004199 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004200 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004201 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004202 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4203 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004204 case Promote:
4205 // The truncation is not required, because we don't guarantee anything
4206 // about high bits anyway.
4207 Result = PromoteOp(Node->getOperand(0));
4208 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004209 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004210 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4211 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004212 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004213 }
4214 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004215 case ISD::SIGN_EXTEND:
4216 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004217 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004218 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4219 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4220 case Legal:
4221 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004222 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004223 break;
4224 case Promote:
4225 // Promote the reg if it's smaller.
4226 Result = PromoteOp(Node->getOperand(0));
4227 // The high bits are not guaranteed to be anything. Insert an extend.
4228 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004229 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004230 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004231 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004232 Result = DAG.getZeroExtendInReg(Result,
4233 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004234 break;
4235 }
4236 break;
Chris Lattner35481892005-12-23 00:16:34 +00004237 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004238 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4239 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004240 Result = PromoteOp(Result);
4241 break;
4242
Chris Lattner8b6fa222005-01-15 22:16:26 +00004243 case ISD::FP_EXTEND:
4244 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4245 case ISD::FP_ROUND:
4246 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4247 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4248 case Promote: assert(0 && "Unreachable with 2 FP types!");
4249 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004250 if (Node->getConstantOperandVal(1) == 0) {
4251 // Input is legal? Do an FP_ROUND_INREG.
4252 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4253 DAG.getValueType(VT));
4254 } else {
4255 // Just remove the truncate, it isn't affecting the value.
4256 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4257 Node->getOperand(1));
4258 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004259 break;
4260 }
4261 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004262 case ISD::SINT_TO_FP:
4263 case ISD::UINT_TO_FP:
4264 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4265 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004266 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004267 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004268 break;
4269
4270 case Promote:
4271 Result = PromoteOp(Node->getOperand(0));
4272 if (Node->getOpcode() == ISD::SINT_TO_FP)
4273 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004274 Result,
4275 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004276 else
Chris Lattner23993562005-04-13 02:38:47 +00004277 Result = DAG.getZeroExtendInReg(Result,
4278 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004279 // No extra round required here.
4280 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004281 break;
4282 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004283 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4284 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004285 // Round if we cannot tolerate excess precision.
4286 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004287 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4288 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004289 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004290 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004291 break;
4292
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004293 case ISD::SIGN_EXTEND_INREG:
4294 Result = PromoteOp(Node->getOperand(0));
4295 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4296 Node->getOperand(1));
4297 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004298 case ISD::FP_TO_SINT:
4299 case ISD::FP_TO_UINT:
4300 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4301 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004302 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004303 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004304 break;
4305 case Promote:
4306 // The input result is prerounded, so we don't have to do anything
4307 // special.
4308 Tmp1 = PromoteOp(Node->getOperand(0));
4309 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004310 }
Nate Begemand2558e32005-08-14 01:20:53 +00004311 // If we're promoting a UINT to a larger size, check to see if the new node
4312 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4313 // we can use that instead. This allows us to generate better code for
4314 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4315 // legal, such as PowerPC.
4316 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004317 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004318 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4319 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004320 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4321 } else {
4322 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4323 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004324 break;
4325
Chris Lattner2c8086f2005-04-02 05:00:07 +00004326 case ISD::FABS:
4327 case ISD::FNEG:
4328 Tmp1 = PromoteOp(Node->getOperand(0));
4329 assert(Tmp1.getValueType() == NVT);
4330 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4331 // NOTE: we do not have to do any extra rounding here for
4332 // NoExcessFPPrecision, because we know the input will have the appropriate
4333 // precision, and these operations don't modify precision at all.
4334 break;
4335
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004336 case ISD::FLOG:
4337 case ISD::FLOG2:
4338 case ISD::FLOG10:
4339 case ISD::FEXP:
4340 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004341 case ISD::FSQRT:
4342 case ISD::FSIN:
4343 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004344 case ISD::FTRUNC:
4345 case ISD::FFLOOR:
4346 case ISD::FCEIL:
4347 case ISD::FRINT:
4348 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004349 Tmp1 = PromoteOp(Node->getOperand(0));
4350 assert(Tmp1.getValueType() == NVT);
4351 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004352 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004353 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4354 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004355 break;
4356
Evan Cheng9d24ac52008-09-09 23:35:53 +00004357 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004358 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004359 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004360 // directly as well, which may be better.
4361 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004362 Tmp2 = Node->getOperand(1);
4363 if (Node->getOpcode() == ISD::FPOW)
4364 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004365 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004366 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004367 if (NoExcessFPPrecision)
4368 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4369 DAG.getValueType(VT));
4370 break;
4371 }
4372
Dale Johannesene00a8a22008-08-28 02:44:49 +00004373 case ISD::ATOMIC_CMP_SWAP_8:
4374 case ISD::ATOMIC_CMP_SWAP_16:
4375 case ISD::ATOMIC_CMP_SWAP_32:
4376 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004377 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004378 Tmp2 = PromoteOp(Node->getOperand(2));
4379 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang28873102008-06-25 08:15:39 +00004380 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4381 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004382 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004383 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004384 // Remember that we legalized the chain.
4385 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4386 break;
4387 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00004388 case ISD::ATOMIC_LOAD_ADD_8:
4389 case ISD::ATOMIC_LOAD_SUB_8:
4390 case ISD::ATOMIC_LOAD_AND_8:
4391 case ISD::ATOMIC_LOAD_OR_8:
4392 case ISD::ATOMIC_LOAD_XOR_8:
4393 case ISD::ATOMIC_LOAD_NAND_8:
4394 case ISD::ATOMIC_LOAD_MIN_8:
4395 case ISD::ATOMIC_LOAD_MAX_8:
4396 case ISD::ATOMIC_LOAD_UMIN_8:
4397 case ISD::ATOMIC_LOAD_UMAX_8:
4398 case ISD::ATOMIC_SWAP_8:
4399 case ISD::ATOMIC_LOAD_ADD_16:
4400 case ISD::ATOMIC_LOAD_SUB_16:
4401 case ISD::ATOMIC_LOAD_AND_16:
4402 case ISD::ATOMIC_LOAD_OR_16:
4403 case ISD::ATOMIC_LOAD_XOR_16:
4404 case ISD::ATOMIC_LOAD_NAND_16:
4405 case ISD::ATOMIC_LOAD_MIN_16:
4406 case ISD::ATOMIC_LOAD_MAX_16:
4407 case ISD::ATOMIC_LOAD_UMIN_16:
4408 case ISD::ATOMIC_LOAD_UMAX_16:
4409 case ISD::ATOMIC_SWAP_16:
4410 case ISD::ATOMIC_LOAD_ADD_32:
4411 case ISD::ATOMIC_LOAD_SUB_32:
4412 case ISD::ATOMIC_LOAD_AND_32:
4413 case ISD::ATOMIC_LOAD_OR_32:
4414 case ISD::ATOMIC_LOAD_XOR_32:
4415 case ISD::ATOMIC_LOAD_NAND_32:
4416 case ISD::ATOMIC_LOAD_MIN_32:
4417 case ISD::ATOMIC_LOAD_MAX_32:
4418 case ISD::ATOMIC_LOAD_UMIN_32:
4419 case ISD::ATOMIC_LOAD_UMAX_32:
4420 case ISD::ATOMIC_SWAP_32:
4421 case ISD::ATOMIC_LOAD_ADD_64:
4422 case ISD::ATOMIC_LOAD_SUB_64:
4423 case ISD::ATOMIC_LOAD_AND_64:
4424 case ISD::ATOMIC_LOAD_OR_64:
4425 case ISD::ATOMIC_LOAD_XOR_64:
4426 case ISD::ATOMIC_LOAD_NAND_64:
4427 case ISD::ATOMIC_LOAD_MIN_64:
4428 case ISD::ATOMIC_LOAD_MAX_64:
4429 case ISD::ATOMIC_LOAD_UMIN_64:
4430 case ISD::ATOMIC_LOAD_UMAX_64:
4431 case ISD::ATOMIC_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004432 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004433 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00004434 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4435 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004436 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004437 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004438 // Remember that we legalized the chain.
4439 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4440 break;
4441 }
4442
Chris Lattner03c85462005-01-15 05:21:40 +00004443 case ISD::AND:
4444 case ISD::OR:
4445 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004446 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004447 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004448 case ISD::MUL:
4449 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004450 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004451 // that too is okay if they are integer operations.
4452 Tmp1 = PromoteOp(Node->getOperand(0));
4453 Tmp2 = PromoteOp(Node->getOperand(1));
4454 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4455 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004456 break;
4457 case ISD::FADD:
4458 case ISD::FSUB:
4459 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004460 Tmp1 = PromoteOp(Node->getOperand(0));
4461 Tmp2 = PromoteOp(Node->getOperand(1));
4462 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4463 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4464
4465 // Floating point operations will give excess precision that we may not be
4466 // able to tolerate. If we DO allow excess precision, just leave it,
4467 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004468 // FIXME: Why would we need to round FP ops more than integer ones?
4469 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004470 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004471 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4472 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004473 break;
4474
Chris Lattner8b6fa222005-01-15 22:16:26 +00004475 case ISD::SDIV:
4476 case ISD::SREM:
4477 // These operators require that their input be sign extended.
4478 Tmp1 = PromoteOp(Node->getOperand(0));
4479 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004480 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004481 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4482 DAG.getValueType(VT));
4483 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4484 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004485 }
4486 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4487
4488 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004489 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004490 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4491 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004492 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004493 case ISD::FDIV:
4494 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004495 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004496 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004497 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004498 case Expand: assert(0 && "not implemented");
4499 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4500 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004501 }
4502 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004503 case Expand: assert(0 && "not implemented");
4504 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4505 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004506 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004507 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4508
4509 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004510 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004511 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4512 DAG.getValueType(VT));
4513 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004514
4515 case ISD::UDIV:
4516 case ISD::UREM:
4517 // These operators require that their input be zero extended.
4518 Tmp1 = PromoteOp(Node->getOperand(0));
4519 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004520 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004521 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4522 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004523 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4524 break;
4525
4526 case ISD::SHL:
4527 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004528 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004529 break;
4530 case ISD::SRA:
4531 // The input value must be properly sign extended.
4532 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004533 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4534 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004535 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004536 break;
4537 case ISD::SRL:
4538 // The input value must be properly zero extended.
4539 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004540 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004541 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004542 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004543
4544 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004545 Tmp1 = Node->getOperand(0); // Get the chain.
4546 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004547 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4548 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004549 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004550 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004551 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004552 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004553 // Increment the pointer, VAList, to the next vaarg
4554 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004555 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004556 TLI.getPointerTy()));
4557 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004558 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004559 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004560 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004561 }
4562 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004563 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004564 break;
4565
Evan Cheng466685d2006-10-09 20:57:25 +00004566 case ISD::LOAD: {
4567 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004568 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4569 ? ISD::EXTLOAD : LD->getExtensionType();
4570 Result = DAG.getExtLoad(ExtType, NVT,
4571 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004572 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004573 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004574 LD->isVolatile(),
4575 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004576 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004577 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004578 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004579 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004580 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004581 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4582 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004583
Duncan Sands83ec4b62008-06-06 12:08:01 +00004584 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004585 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004586 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4587 // Ensure that the resulting node is at least the same size as the operands'
4588 // value types, because we cannot assume that TLI.getSetCCValueType() is
4589 // constant.
4590 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004591 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004592 }
Nate Begeman9373a812005-08-10 20:51:12 +00004593 case ISD::SELECT_CC:
4594 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4595 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4596 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004597 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004598 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004599 case ISD::BSWAP:
4600 Tmp1 = Node->getOperand(0);
4601 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4602 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4603 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004604 DAG.getConstant(NVT.getSizeInBits() -
4605 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004606 TLI.getShiftAmountTy()));
4607 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004608 case ISD::CTPOP:
4609 case ISD::CTTZ:
4610 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004611 // Zero extend the argument
4612 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004613 // Perform the larger operation, then subtract if needed.
4614 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004615 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004616 case ISD::CTPOP:
4617 Result = Tmp1;
4618 break;
4619 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004620 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004621 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004622 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004623 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004624 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004625 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004626 break;
4627 case ISD::CTLZ:
4628 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004629 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004630 DAG.getConstant(NVT.getSizeInBits() -
4631 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004632 break;
4633 }
4634 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004635 case ISD::EXTRACT_SUBVECTOR:
4636 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004637 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004638 case ISD::EXTRACT_VECTOR_ELT:
4639 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4640 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004641 }
4642
Gabor Greifba36cb52008-08-28 21:40:38 +00004643 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004644
4645 // Make sure the result is itself legal.
4646 Result = LegalizeOp(Result);
4647
4648 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004649 AddPromotedOperand(Op, Result);
4650 return Result;
4651}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004652
Dan Gohman7f321562007-06-25 16:23:39 +00004653/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4654/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4655/// based on the vector type. The return type of this matches the element type
4656/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004657SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004658 // We know that operand #0 is the Vec vector. If the index is a constant
4659 // or if the invec is a supported hardware type, we can use it. Otherwise,
4660 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004661 SDValue Vec = Op.getOperand(0);
4662 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004663
Duncan Sands83ec4b62008-06-06 12:08:01 +00004664 MVT TVT = Vec.getValueType();
4665 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004666
Dan Gohman7f321562007-06-25 16:23:39 +00004667 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4668 default: assert(0 && "This action is not supported yet!");
4669 case TargetLowering::Custom: {
4670 Vec = LegalizeOp(Vec);
4671 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004672 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004673 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004674 return Tmp3;
4675 break;
4676 }
4677 case TargetLowering::Legal:
4678 if (isTypeLegal(TVT)) {
4679 Vec = LegalizeOp(Vec);
4680 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004681 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004682 }
4683 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004684 case TargetLowering::Promote:
4685 assert(TVT.isVector() && "not vector type");
4686 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004687 case TargetLowering::Expand:
4688 break;
4689 }
4690
4691 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004692 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004693 Op = ScalarizeVectorOp(Vec);
4694 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004695 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004696 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004697 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00004698 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004699 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004700 Vec = Lo;
4701 } else {
4702 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004703 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004704 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004705 }
Dan Gohman7f321562007-06-25 16:23:39 +00004706
Chris Lattner15972212006-03-31 17:55:51 +00004707 // It's now an extract from the appropriate high or low part. Recurse.
4708 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004709 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004710 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004711 // Store the value to a temporary stack slot, then LOAD the scalar
4712 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00004713 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4714 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00004715
4716 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004717 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00004718 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4719 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004720
Duncan Sands8e4eb092008-06-08 20:54:56 +00004721 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004722 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004723 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004724 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004725
Dan Gohman7f321562007-06-25 16:23:39 +00004726 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4727
4728 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004729 }
Dan Gohman7f321562007-06-25 16:23:39 +00004730 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004731}
4732
Dan Gohman7f321562007-06-25 16:23:39 +00004733/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004734/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004735SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004736 // We know that operand #0 is the Vec vector. For now we assume the index
4737 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00004738 SDValue Vec = Op.getOperand(0);
4739 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00004740
Duncan Sands83ec4b62008-06-06 12:08:01 +00004741 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00004742
Duncan Sands83ec4b62008-06-06 12:08:01 +00004743 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00004744 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004745 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004746 }
4747
4748 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004749 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00004750 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004751 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00004752 Vec = Lo;
4753 } else {
4754 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004755 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4756 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004757 }
4758
4759 // It's now an extract from the appropriate high or low part. Recurse.
4760 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004761 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004762}
4763
Nate Begeman750ac1b2006-02-01 07:19:44 +00004764/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4765/// with condition CC on the current target. This usually involves legalizing
4766/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4767/// there may be no choice but to create a new SetCC node to represent the
4768/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00004769/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4770void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4771 SDValue &RHS,
4772 SDValue &CC) {
4773 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004774
4775 switch (getTypeAction(LHS.getValueType())) {
4776 case Legal:
4777 Tmp1 = LegalizeOp(LHS); // LHS
4778 Tmp2 = LegalizeOp(RHS); // RHS
4779 break;
4780 case Promote:
4781 Tmp1 = PromoteOp(LHS); // LHS
4782 Tmp2 = PromoteOp(RHS); // RHS
4783
4784 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004785 if (LHS.getValueType().isInteger()) {
4786 MVT VT = LHS.getValueType();
4787 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004788
4789 // Otherwise, we have to insert explicit sign or zero extends. Note
4790 // that we could insert sign extends for ALL conditions, but zero extend
4791 // is cheaper on many machines (an AND instead of two shifts), so prefer
4792 // it.
4793 switch (cast<CondCodeSDNode>(CC)->get()) {
4794 default: assert(0 && "Unknown integer comparison!");
4795 case ISD::SETEQ:
4796 case ISD::SETNE:
4797 case ISD::SETUGE:
4798 case ISD::SETUGT:
4799 case ISD::SETULE:
4800 case ISD::SETULT:
4801 // ALL of these operations will work if we either sign or zero extend
4802 // the operands (including the unsigned comparisons!). Zero extend is
4803 // usually a simpler/cheaper operation, so prefer it.
4804 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4805 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4806 break;
4807 case ISD::SETGE:
4808 case ISD::SETGT:
4809 case ISD::SETLT:
4810 case ISD::SETLE:
4811 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4812 DAG.getValueType(VT));
4813 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4814 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00004815 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
4816 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00004817 break;
4818 }
4819 }
4820 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004821 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004822 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00004823 if (VT == MVT::f32 || VT == MVT::f64) {
4824 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00004825 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004826 switch (cast<CondCodeSDNode>(CC)->get()) {
4827 case ISD::SETEQ:
4828 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004829 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004830 break;
4831 case ISD::SETNE:
4832 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004833 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004834 break;
4835 case ISD::SETGE:
4836 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004837 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004838 break;
4839 case ISD::SETLT:
4840 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00004841 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004842 break;
4843 case ISD::SETLE:
4844 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00004845 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004846 break;
4847 case ISD::SETGT:
4848 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00004849 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004850 break;
4851 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00004852 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
4853 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004854 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00004855 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004856 break;
4857 default:
Evan Cheng56966222007-01-12 02:11:51 +00004858 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004859 switch (cast<CondCodeSDNode>(CC)->get()) {
4860 case ISD::SETONE:
4861 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00004862 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004863 // Fallthrough
4864 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00004865 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004866 break;
4867 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00004868 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004869 break;
4870 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00004871 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004872 break;
4873 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00004874 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004875 break;
Evan Cheng56966222007-01-12 02:11:51 +00004876 case ISD::SETUEQ:
4877 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00004878 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004879 default: assert(0 && "Unsupported FP setcc!");
4880 }
4881 }
Duncan Sandsf9516202008-06-30 10:19:09 +00004882
Dan Gohman475871a2008-07-27 21:46:04 +00004883 SDValue Dummy;
4884 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00004885 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00004886 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00004887 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00004888 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00004889 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00004890 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00004891 CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00004892 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00004893 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004894 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00004895 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00004896 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00004897 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00004898 }
Evan Cheng21cacc42008-07-07 07:18:09 +00004899 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00004900 RHS = Tmp2;
4901 return;
4902 }
4903
Dan Gohman475871a2008-07-27 21:46:04 +00004904 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004905 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004906 ExpandOp(RHS, RHSLo, RHSHi);
4907 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
4908
4909 if (VT==MVT::ppcf128) {
4910 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00004911 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00004912 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00004913 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00004914 // The following can be improved, but not that much.
Dale Johannesene2f20832008-09-12 00:30:56 +00004915 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
4916 ISD::SETOEQ);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004917 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004918 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesene2f20832008-09-12 00:30:56 +00004919 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
4920 ISD::SETUNE);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004921 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00004922 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
4923 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00004924 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00004925 break;
4926 }
4927
4928 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004929 case ISD::SETEQ:
4930 case ISD::SETNE:
4931 if (RHSLo == RHSHi)
4932 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
4933 if (RHSCST->isAllOnesValue()) {
4934 // Comparison to -1.
4935 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
4936 Tmp2 = RHSLo;
4937 break;
4938 }
4939
4940 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
4941 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
4942 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
4943 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
4944 break;
4945 default:
4946 // If this is a comparison of the sign bit, just look at the top part.
4947 // X > -1, x < 0
4948 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
4949 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00004950 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00004951 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
4952 CST->isAllOnesValue())) { // X > -1
4953 Tmp1 = LHSHi;
4954 Tmp2 = RHSHi;
4955 break;
4956 }
4957
4958 // FIXME: This generated code sucks.
4959 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00004960 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00004961 default: assert(0 && "Unknown integer setcc!");
4962 case ISD::SETLT:
4963 case ISD::SETULT: LowCC = ISD::SETULT; break;
4964 case ISD::SETGT:
4965 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
4966 case ISD::SETLE:
4967 case ISD::SETULE: LowCC = ISD::SETULE; break;
4968 case ISD::SETGE:
4969 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
4970 }
4971
4972 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
4973 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
4974 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
4975
4976 // NOTE: on targets without efficient SELECT of bools, we can always use
4977 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00004978 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004979 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00004980 LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004981 if (!Tmp1.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00004982 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
4983 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00004984 CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00004985 if (!Tmp2.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00004986 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00004987 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00004988
Gabor Greifba36cb52008-08-28 21:40:38 +00004989 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
4990 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00004991 if ((Tmp1C && Tmp1C->isNullValue()) ||
4992 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00004993 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
4994 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00004995 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00004996 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
4997 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
4998 // low part is known false, returns high part.
4999 // For LE / GE, if high part is known false, ignore the low part.
5000 // For LT / GT, if high part is known true, ignore the low part.
5001 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005002 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005003 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005004 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005005 ISD::SETEQ, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005006 if (!Result.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005007 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005008 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005009 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5010 Result, Tmp1, Tmp2));
5011 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005012 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005013 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005014 }
5015 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005016 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005017 LHS = Tmp1;
5018 RHS = Tmp2;
5019}
5020
Evan Cheng7f042682008-10-15 02:05:31 +00005021/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5022/// condition code CC on the current target. This routine assumes LHS and rHS
5023/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5024/// illegal condition code into AND / OR of multiple SETCC values.
5025void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5026 SDValue &LHS, SDValue &RHS,
5027 SDValue &CC) {
5028 MVT OpVT = LHS.getValueType();
5029 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5030 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5031 default: assert(0 && "Unknown condition code action!");
5032 case TargetLowering::Legal:
5033 // Nothing to do.
5034 break;
5035 case TargetLowering::Expand: {
5036 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5037 unsigned Opc = 0;
5038 switch (CCCode) {
5039 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005040 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5041 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5042 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5043 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5044 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5045 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5046 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5047 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5048 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5049 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5050 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5051 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005052 // FIXME: Implement more expansions.
5053 }
5054
5055 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5056 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5057 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5058 RHS = SDValue();
5059 CC = SDValue();
5060 break;
5061 }
5062 }
5063}
5064
Chris Lattner1401d152008-01-16 07:45:30 +00005065/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5066/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5067/// a load from the stack slot to DestVT, extending it if needed.
5068/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005069SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5070 MVT SlotVT,
5071 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005072 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005073 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5074 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005075 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005076
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005077 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005078 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00005079
Duncan Sands83ec4b62008-06-06 12:08:01 +00005080 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5081 unsigned SlotSize = SlotVT.getSizeInBits();
5082 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005083 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5084 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005085
Chris Lattner1401d152008-01-16 07:45:30 +00005086 // Emit a store to the stack slot. Use a truncstore if the input value is
5087 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005088 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005089
Chris Lattner1401d152008-01-16 07:45:30 +00005090 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005091 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005092 PseudoSourceValue::getFixedStack(SPFI), 0,
5093 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005094 else {
5095 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005096 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005097 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00005098 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005099 }
5100
Chris Lattner35481892005-12-23 00:16:34 +00005101 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005102 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00005103 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005104
5105 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00005106 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5107 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005108}
5109
Dan Gohman475871a2008-07-27 21:46:04 +00005110SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005111 // Create a vector sized/aligned stack slot, store the value to element #0,
5112 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005113 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005114
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005115 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005116 int SPFI = StackPtrFI->getIndex();
5117
Dan Gohman475871a2008-07-27 21:46:04 +00005118 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005119 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005120 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005121 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005122}
5123
5124
Chris Lattnerce872152006-03-19 06:31:19 +00005125/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005126/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005127SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005128
5129 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005130 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005131 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005132 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005133 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005134
Dan Gohman475871a2008-07-27 21:46:04 +00005135 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005136 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005137 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005138 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005139 bool isConstant = true;
5140 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5141 SplatValue.getOpcode() != ISD::UNDEF)
5142 isConstant = false;
5143
Evan Cheng033e6812006-03-24 01:17:21 +00005144 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005145 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005146 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005147 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005148 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005149 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005150 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005151
5152 // If this isn't a constant element or an undef, we can't use a constant
5153 // pool load.
5154 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5155 V.getOpcode() != ISD::UNDEF)
5156 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005157 }
5158
5159 if (isOnlyLowElement) {
5160 // If the low element is an undef too, then this whole things is an undef.
5161 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5162 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5163 // Otherwise, turn this into a scalar_to_vector node.
5164 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5165 Node->getOperand(0));
5166 }
5167
Chris Lattner2eb86532006-03-24 07:29:17 +00005168 // If all elements are constants, create a load from the constant pool.
5169 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005170 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005171 std::vector<Constant*> CV;
5172 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5173 if (ConstantFPSDNode *V =
5174 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005175 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005176 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005177 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005178 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005179 } else {
5180 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005181 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005182 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005183 CV.push_back(UndefValue::get(OpNTy));
5184 }
5185 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005186 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005187 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005188 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005189 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005190 PseudoSourceValue::getConstantPool(), 0,
5191 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005192 }
5193
Gabor Greifba36cb52008-08-28 21:40:38 +00005194 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005195 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005196 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005197 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5198 std::vector<SDValue> ZeroVec(NumElems, Zero);
5199 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005200 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005201
5202 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005203 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005204 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005205 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005206 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5207
5208 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5209 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5210 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5211 SplatMask);
5212 }
5213 }
5214
Evan Cheng033e6812006-03-24 01:17:21 +00005215 // If there are only two unique elements, we may be able to turn this into a
5216 // vector shuffle.
5217 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005218 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005219 SDValue Val1 = Node->getOperand(1);
5220 SDValue Val2;
5221 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005222 if (MI->first != Val1)
5223 Val2 = MI->first;
5224 else
5225 Val2 = (++MI)->first;
5226
5227 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5228 // vector shuffle has the undef vector on the RHS.
5229 if (Val1.getOpcode() == ISD::UNDEF)
5230 std::swap(Val1, Val2);
5231
Evan Cheng033e6812006-03-24 01:17:21 +00005232 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005233 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5234 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005235 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005236
5237 // Set elements of the shuffle mask for Val1.
5238 std::vector<unsigned> &Val1Elts = Values[Val1];
5239 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5240 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5241
5242 // Set elements of the shuffle mask for Val2.
5243 std::vector<unsigned> &Val2Elts = Values[Val2];
5244 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5245 if (Val2.getOpcode() != ISD::UNDEF)
5246 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5247 else
5248 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5249
Dan Gohman475871a2008-07-27 21:46:04 +00005250 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005251 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005252
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005253 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005254 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5255 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005256 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5257 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005258 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005259
5260 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005261 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005262 }
5263 }
Chris Lattnerce872152006-03-19 06:31:19 +00005264
5265 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5266 // aligned object on the stack, store each element into it, then load
5267 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005268 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005269 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005270 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005271
5272 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005273 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005274 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005275 // Store (in the right endianness) the elements to memory.
5276 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5277 // Ignore undef elements.
5278 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5279
Chris Lattner841c8822006-03-22 01:46:54 +00005280 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005281
Dan Gohman475871a2008-07-27 21:46:04 +00005282 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005283 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5284
Evan Cheng786225a2006-10-05 23:01:46 +00005285 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005286 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005287 }
5288
Dan Gohman475871a2008-07-27 21:46:04 +00005289 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005290 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005291 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5292 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005293 else
5294 StoreChain = DAG.getEntryNode();
5295
5296 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005297 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005298}
5299
Chris Lattner5b359c62005-04-02 04:00:59 +00005300void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005301 SDValue Op, SDValue Amt,
5302 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005303 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005304 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005305 ExpandOp(Op, LHSL, LHSH);
5306
Dan Gohman475871a2008-07-27 21:46:04 +00005307 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005308 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005309 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005310 Hi = Lo.getValue(1);
5311}
5312
5313
Chris Lattnere34b3962005-01-19 04:19:40 +00005314/// ExpandShift - Try to find a clever way to expand this shift operation out to
5315/// smaller elements. If we can't find a way that is more efficient than a
5316/// libcall on this target, return false. Otherwise, return true with the
5317/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005318bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5319 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005320 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5321 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005322
Duncan Sands83ec4b62008-06-06 12:08:01 +00005323 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005324 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005325 MVT ShTy = ShAmt.getValueType();
5326 unsigned ShBits = ShTy.getSizeInBits();
5327 unsigned VTBits = Op.getValueType().getSizeInBits();
5328 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005329
Chris Lattner7a3c8552007-10-14 20:35:12 +00005330 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005331 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005332 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005333 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005334 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005335 ExpandOp(Op, InL, InH);
5336 switch(Opc) {
5337 case ISD::SHL:
5338 if (Cst > VTBits) {
5339 Lo = DAG.getConstant(0, NVT);
5340 Hi = DAG.getConstant(0, NVT);
5341 } else if (Cst > NVTBits) {
5342 Lo = DAG.getConstant(0, NVT);
5343 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005344 } else if (Cst == NVTBits) {
5345 Lo = DAG.getConstant(0, NVT);
5346 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005347 } else {
5348 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5349 Hi = DAG.getNode(ISD::OR, NVT,
5350 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5351 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5352 }
5353 return true;
5354 case ISD::SRL:
5355 if (Cst > VTBits) {
5356 Lo = DAG.getConstant(0, NVT);
5357 Hi = DAG.getConstant(0, NVT);
5358 } else if (Cst > NVTBits) {
5359 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5360 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005361 } else if (Cst == NVTBits) {
5362 Lo = InH;
5363 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005364 } else {
5365 Lo = DAG.getNode(ISD::OR, NVT,
5366 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5367 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5368 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5369 }
5370 return true;
5371 case ISD::SRA:
5372 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005373 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005374 DAG.getConstant(NVTBits-1, ShTy));
5375 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005376 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005377 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005378 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005379 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005380 } else if (Cst == NVTBits) {
5381 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005382 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005383 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005384 } else {
5385 Lo = DAG.getNode(ISD::OR, NVT,
5386 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5387 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5388 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5389 }
5390 return true;
5391 }
5392 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005393
5394 // Okay, the shift amount isn't constant. However, if we can tell that it is
5395 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005396 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5397 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005398 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005399
Dan Gohman9e255b72008-02-22 01:12:31 +00005400 // If we know that if any of the high bits of the shift amount are one, then
5401 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005402 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005403 // Mask out the high bit, which we know is set.
5404 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005405 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005406
5407 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005408 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005409 ExpandOp(Op, InL, InH);
5410 switch(Opc) {
5411 case ISD::SHL:
5412 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5413 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5414 return true;
5415 case ISD::SRL:
5416 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5417 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5418 return true;
5419 case ISD::SRA:
5420 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5421 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5422 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5423 return true;
5424 }
5425 }
5426
Dan Gohman9e255b72008-02-22 01:12:31 +00005427 // If we know that the high bits of the shift amount are all zero, then we can
5428 // do this as a couple of simple shifts.
5429 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005430 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005431 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005432 DAG.getConstant(NVTBits, Amt.getValueType()),
5433 Amt);
5434
5435 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005436 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005437 ExpandOp(Op, InL, InH);
5438 switch(Opc) {
5439 case ISD::SHL:
5440 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5441 Hi = DAG.getNode(ISD::OR, NVT,
5442 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5443 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5444 return true;
5445 case ISD::SRL:
5446 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5447 Lo = DAG.getNode(ISD::OR, NVT,
5448 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5449 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5450 return true;
5451 case ISD::SRA:
5452 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5453 Lo = DAG.getNode(ISD::OR, NVT,
5454 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5455 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5456 return true;
5457 }
5458 }
5459
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005460 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005461}
Chris Lattner77e77a62005-01-21 06:05:23 +00005462
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005463
Chris Lattner77e77a62005-01-21 06:05:23 +00005464// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5465// does not fit into a register, return the lo part and set the hi part to the
5466// by-reg argument. If it does fit into a single register, return the result
5467// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005468SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5469 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005470 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5471 // The input chain to this libcall is the entry node of the function.
5472 // Legalizing the call will automatically add the previous call to the
5473 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005474 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005475
Chris Lattner77e77a62005-01-21 06:05:23 +00005476 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005477 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005478 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005479 MVT ArgVT = Node->getOperand(i).getValueType();
5480 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005481 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005482 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005483 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005484 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005485 }
Bill Wendling056292f2008-09-16 21:48:12 +00005486 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005487 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005488
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005489 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005490 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005491 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005492 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5493 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005494
Chris Lattner6831a812006-02-13 09:18:02 +00005495 // Legalize the call sequence, starting with the chain. This will advance
5496 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5497 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5498 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005499 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005500 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005501 default: assert(0 && "Unknown thing");
5502 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005503 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005504 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005505 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005506 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005507 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005508 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005509 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005510}
5511
Dan Gohman7f8613e2008-08-14 20:04:46 +00005512/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5513///
5514SDValue SelectionDAGLegalize::
5515LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5516 bool isCustom = false;
5517 SDValue Tmp1;
5518 switch (getTypeAction(Op.getValueType())) {
5519 case Legal:
5520 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5521 Op.getValueType())) {
5522 default: assert(0 && "Unknown operation action!");
5523 case TargetLowering::Custom:
5524 isCustom = true;
5525 // FALLTHROUGH
5526 case TargetLowering::Legal:
5527 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005528 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005529 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5530 else
5531 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5532 DestTy, Tmp1);
5533 if (isCustom) {
5534 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005535 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005536 }
5537 break;
5538 case TargetLowering::Expand:
5539 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5540 break;
5541 case TargetLowering::Promote:
5542 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5543 break;
5544 }
5545 break;
5546 case Expand:
5547 Result = ExpandIntToFP(isSigned, DestTy, Op);
5548 break;
5549 case Promote:
5550 Tmp1 = PromoteOp(Op);
5551 if (isSigned) {
5552 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5553 Tmp1, DAG.getValueType(Op.getValueType()));
5554 } else {
5555 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5556 Op.getValueType());
5557 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005558 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005559 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5560 else
5561 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5562 DestTy, Tmp1);
5563 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5564 break;
5565 }
5566 return Result;
5567}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005568
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005569/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5570///
Dan Gohman475871a2008-07-27 21:46:04 +00005571SDValue SelectionDAGLegalize::
5572ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005573 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005574 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005575
Dan Gohman7f8613e2008-08-14 20:04:46 +00005576 // Expand unsupported int-to-fp vector casts by unrolling them.
5577 if (DestTy.isVector()) {
5578 if (!ExpandSource)
5579 return LegalizeOp(UnrollVectorOp(Source));
5580 MVT DestEltTy = DestTy.getVectorElementType();
5581 if (DestTy.getVectorNumElements() == 1) {
5582 SDValue Scalar = ScalarizeVectorOp(Source);
5583 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5584 DestEltTy, Scalar);
5585 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5586 }
5587 SDValue Lo, Hi;
5588 SplitVectorOp(Source, Lo, Hi);
5589 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5590 DestTy.getVectorNumElements() / 2);
5591 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5592 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengefa53392008-10-13 18:46:18 +00005593 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5594 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005595 }
5596
Evan Cheng9845eb52008-04-01 02:18:22 +00005597 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5598 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005599 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005600 // incoming integer is set. To handle this, we dynamically test to see if
5601 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005602 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005603 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005604 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005605 ExpandOp(Source, Lo, Hi);
5606 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5607 } else {
5608 // The comparison for the sign bit will use the entire operand.
5609 Hi = Source;
5610 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005611
Dale Johannesen53997b02008-11-04 20:52:49 +00005612 // Check to see if the target has a custom way to lower this. If so, use
5613 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005614 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5615 default: assert(0 && "This action not implemented for this operation!");
5616 case TargetLowering::Legal:
5617 case TargetLowering::Expand:
5618 break; // This case is handled below.
5619 case TargetLowering::Custom: {
5620 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5621 Source), DAG);
5622 if (NV.getNode())
5623 return LegalizeOp(NV);
5624 break; // The target decided this was legal after all
5625 }
5626 }
5627
Chris Lattner66de05b2005-05-13 04:45:13 +00005628 // If this is unsigned, and not supported, first perform the conversion to
5629 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005630 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005631
Dan Gohman475871a2008-07-27 21:46:04 +00005632 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005633 DAG.getConstant(0, Hi.getValueType()),
5634 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005635 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5636 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005637 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005638 uint64_t FF = 0x5f800000ULL;
5639 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005640 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005641
Dan Gohman475871a2008-07-27 21:46:04 +00005642 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005643 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere9c35e72005-04-13 05:09:42 +00005644 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005645 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005646 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005647 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005648 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005649 PseudoSourceValue::getConstantPool(), 0,
5650 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005651 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005652 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005653 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005654 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005655 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005656 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005657 else
5658 assert(0 && "Unexpected conversion");
5659
Duncan Sands83ec4b62008-06-06 12:08:01 +00005660 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005661 if (SCVT != DestTy) {
5662 // Destination type needs to be expanded as well. The FADD now we are
5663 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005664 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5665 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005666 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005667 SignedConv, SignedConv.getValue(1));
5668 }
5669 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5670 }
Chris Lattner473a9902005-09-29 06:44:39 +00005671 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005672 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005673
Chris Lattnera88a2602005-05-14 05:33:54 +00005674 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005675 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005676 default: assert(0 && "This action not implemented for this operation!");
5677 case TargetLowering::Legal:
5678 case TargetLowering::Expand:
5679 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005680 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005681 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005682 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005683 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005684 return LegalizeOp(NV);
5685 break; // The target decided this was legal after all
5686 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005687 }
5688
Chris Lattner13689e22005-05-12 07:00:44 +00005689 // Expand the source, then glue it back together for the call. We must expand
5690 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005691 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005692 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005693 ExpandOp(Source, SrcLo, SrcHi);
5694 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5695 }
Chris Lattner13689e22005-05-12 07:00:44 +00005696
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005697 RTLIB::Libcall LC = isSigned ?
5698 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5699 RTLIB::getUINTTOFP(SourceVT, DestTy);
5700 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5701
Chris Lattner6831a812006-02-13 09:18:02 +00005702 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00005703 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00005704 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5705 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmana2e94852008-03-10 23:03:31 +00005706 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5707 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005708}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005709
Chris Lattner22cde6a2006-01-28 08:25:58 +00005710/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5711/// INT_TO_FP operation of the specified operand when the target requests that
5712/// we expand it. At this point, we know that the result and operand types are
5713/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00005714SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5715 SDValue Op0,
5716 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005717 if (Op0.getValueType() == MVT::i32) {
5718 // simple 32-bit [signed|unsigned] integer to float/double expansion
5719
Chris Lattner23594d42008-01-16 07:03:22 +00005720 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00005721 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00005722
Chris Lattner22cde6a2006-01-28 08:25:58 +00005723 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00005724 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00005725 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00005726 SDValue Hi = StackSlot;
5727 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00005728 if (TLI.isLittleEndian())
5729 std::swap(Hi, Lo);
5730
Chris Lattner22cde6a2006-01-28 08:25:58 +00005731 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00005732 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005733 if (isSigned) {
5734 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00005735 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005736 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5737 } else {
5738 Op0Mapped = Op0;
5739 }
5740 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00005741 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005742 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005743 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005744 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005745 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00005746 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005747 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005748 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005749 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00005750 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00005751 BitsToDouble(0x4330000080000000ULL)
5752 : BitsToDouble(0x4330000000000000ULL),
5753 MVT::f64);
5754 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00005755 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005756 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00005757 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005758 // handle final rounding
5759 if (DestVT == MVT::f64) {
5760 // do nothing
5761 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00005762 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005763 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5764 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005765 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005766 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005767 }
5768 return Result;
5769 }
5770 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00005771 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005772
Dan Gohman475871a2008-07-27 21:46:04 +00005773 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005774 DAG.getConstant(0, Op0.getValueType()),
5775 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005776 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5777 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005778 SignSet, Four, Zero);
5779
5780 // If the sign bit of the integer is set, the large number will be treated
5781 // as a negative number. To counteract this, the dynamic code adds an
5782 // offset depending on the data type.
5783 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005784 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005785 default: assert(0 && "Unsupported integer type!");
5786 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5787 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5788 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5789 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5790 }
5791 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005792 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005793
Dan Gohman475871a2008-07-27 21:46:04 +00005794 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005795 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005796 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005797 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005798 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005799 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005800 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005801 PseudoSourceValue::getConstantPool(), 0,
5802 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005803 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005804 FudgeInReg =
5805 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5806 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005807 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005808 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005809 }
5810
5811 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5812}
5813
5814/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5815/// *INT_TO_FP operation of the specified operand when the target requests that
5816/// we promote it. At this point, we know that the result and operand types are
5817/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5818/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00005819SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5820 MVT DestVT,
5821 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005822 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005823 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005824
5825 unsigned OpToUse = 0;
5826
5827 // Scan for the appropriate larger type to use.
5828 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005829 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5830 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005831
5832 // If the target supports SINT_TO_FP of this type, use it.
5833 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5834 default: break;
5835 case TargetLowering::Legal:
5836 if (!TLI.isTypeLegal(NewInTy))
5837 break; // Can't use this datatype.
5838 // FALL THROUGH.
5839 case TargetLowering::Custom:
5840 OpToUse = ISD::SINT_TO_FP;
5841 break;
5842 }
5843 if (OpToUse) break;
5844 if (isSigned) continue;
5845
5846 // If the target supports UINT_TO_FP of this type, use it.
5847 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
5848 default: break;
5849 case TargetLowering::Legal:
5850 if (!TLI.isTypeLegal(NewInTy))
5851 break; // Can't use this datatype.
5852 // FALL THROUGH.
5853 case TargetLowering::Custom:
5854 OpToUse = ISD::UINT_TO_FP;
5855 break;
5856 }
5857 if (OpToUse) break;
5858
5859 // Otherwise, try a larger type.
5860 }
5861
5862 // Okay, we found the operation and type to use. Zero extend our input to the
5863 // desired type then run the operation on it.
5864 return DAG.getNode(OpToUse, DestVT,
5865 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
5866 NewInTy, LegalOp));
5867}
5868
5869/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
5870/// FP_TO_*INT operation of the specified operand when the target requests that
5871/// we promote it. At this point, we know that the result and operand types are
5872/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
5873/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00005874SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
5875 MVT DestVT,
5876 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005877 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005878 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005879
5880 unsigned OpToUse = 0;
5881
5882 // Scan for the appropriate larger type to use.
5883 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005884 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
5885 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005886
5887 // If the target supports FP_TO_SINT returning this type, use it.
5888 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
5889 default: break;
5890 case TargetLowering::Legal:
5891 if (!TLI.isTypeLegal(NewOutTy))
5892 break; // Can't use this datatype.
5893 // FALL THROUGH.
5894 case TargetLowering::Custom:
5895 OpToUse = ISD::FP_TO_SINT;
5896 break;
5897 }
5898 if (OpToUse) break;
5899
5900 // If the target supports FP_TO_UINT of this type, use it.
5901 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
5902 default: break;
5903 case TargetLowering::Legal:
5904 if (!TLI.isTypeLegal(NewOutTy))
5905 break; // Can't use this datatype.
5906 // FALL THROUGH.
5907 case TargetLowering::Custom:
5908 OpToUse = ISD::FP_TO_UINT;
5909 break;
5910 }
5911 if (OpToUse) break;
5912
5913 // Otherwise, try a larger type.
5914 }
5915
Chris Lattner27a6c732007-11-24 07:07:01 +00005916
5917 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00005918 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00005919
Chris Lattner27a6c732007-11-24 07:07:01 +00005920 // If the operation produces an invalid type, it must be custom lowered. Use
5921 // the target lowering hooks to expand it. Just keep the low part of the
5922 // expanded operation, we know that we're truncating anyway.
5923 if (getTypeAction(NewOutTy) == Expand) {
Gabor Greifba36cb52008-08-28 21:40:38 +00005924 Operation = SDValue(TLI.ReplaceNodeResults(Operation.getNode(), DAG), 0);
5925 assert(Operation.getNode() && "Didn't return anything");
Chris Lattner27a6c732007-11-24 07:07:01 +00005926 }
Duncan Sands126d9072008-07-04 11:47:58 +00005927
Chris Lattner27a6c732007-11-24 07:07:01 +00005928 // Truncate the result of the extended FP_TO_*INT operation to the desired
5929 // size.
5930 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005931}
5932
5933/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
5934///
Dan Gohman475871a2008-07-27 21:46:04 +00005935SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005936 MVT VT = Op.getValueType();
5937 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00005938 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005939 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005940 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
5941 case MVT::i16:
5942 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5943 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5944 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
5945 case MVT::i32:
5946 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5947 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5948 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5949 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5950 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
5951 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
5952 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5953 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5954 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5955 case MVT::i64:
5956 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
5957 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
5958 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
5959 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
5960 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
5961 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
5962 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
5963 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
5964 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
5965 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
5966 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
5967 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
5968 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
5969 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
5970 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
5971 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
5972 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
5973 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
5974 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
5975 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
5976 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
5977 }
5978}
5979
5980/// ExpandBitCount - Expand the specified bitcount instruction into operations.
5981///
Dan Gohman475871a2008-07-27 21:46:04 +00005982SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005983 switch (Opc) {
5984 default: assert(0 && "Cannot expand this yet!");
5985 case ISD::CTPOP: {
5986 static const uint64_t mask[6] = {
5987 0x5555555555555555ULL, 0x3333333333333333ULL,
5988 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
5989 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
5990 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005991 MVT VT = Op.getValueType();
5992 MVT ShVT = TLI.getShiftAmountTy();
5993 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005994 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
5995 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman475871a2008-07-27 21:46:04 +00005996 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
5997 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005998 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
5999 DAG.getNode(ISD::AND, VT,
6000 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6001 }
6002 return Op;
6003 }
6004 case ISD::CTLZ: {
6005 // for now, we do this:
6006 // x = x | (x >> 1);
6007 // x = x | (x >> 2);
6008 // ...
6009 // x = x | (x >>16);
6010 // x = x | (x >>32); // for 64-bit input
6011 // return popcount(~x);
6012 //
6013 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006014 MVT VT = Op.getValueType();
6015 MVT ShVT = TLI.getShiftAmountTy();
6016 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006017 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006018 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006019 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6020 }
6021 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6022 return DAG.getNode(ISD::CTPOP, VT, Op);
6023 }
6024 case ISD::CTTZ: {
6025 // for now, we use: { return popcount(~x & (x - 1)); }
6026 // unless the target has ctlz but not ctpop, in which case we use:
6027 // { return 32 - nlz(~x & (x-1)); }
6028 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006029 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00006030 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6031 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006032 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6033 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6034 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6035 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6036 TLI.isOperationLegal(ISD::CTLZ, VT))
6037 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006038 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006039 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6040 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6041 }
6042 }
6043}
Chris Lattnere34b3962005-01-19 04:19:40 +00006044
Dan Gohman475871a2008-07-27 21:46:04 +00006045/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006046/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006047/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006048/// ExpandedNodes map is filled in for any results that are expanded, and the
6049/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006050void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006051 MVT VT = Op.getValueType();
6052 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006053 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006054 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006055 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006056 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006057
Chris Lattner6fdcb252005-09-02 20:32:45 +00006058 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006059 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006060 = ExpandedNodes.find(Op);
6061 if (I != ExpandedNodes.end()) {
6062 Lo = I->second.first;
6063 Hi = I->second.second;
6064 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006065 }
6066
Chris Lattner3e928bb2005-01-07 07:47:09 +00006067 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006068 case ISD::CopyFromReg:
6069 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006070 case ISD::FP_ROUND_INREG:
6071 if (VT == MVT::ppcf128 &&
6072 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6073 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006074 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006075 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6076 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006077 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006078 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006079 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6080 Lo = Result.getNode()->getOperand(0);
6081 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006082 break;
6083 }
6084 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006085 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006086#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006087 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006088#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006089 assert(0 && "Do not know how to expand this operator!");
6090 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006091 case ISD::EXTRACT_ELEMENT:
6092 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006093 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006094 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006095 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006096 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006097 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6098 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6099 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006100 case ISD::UNDEF:
6101 Lo = DAG.getNode(ISD::UNDEF, NVT);
6102 Hi = DAG.getNode(ISD::UNDEF, NVT);
6103 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006104 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006105 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006106 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6107 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6108 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006109 break;
6110 }
Evan Cheng00495212006-12-12 21:32:44 +00006111 case ISD::ConstantFP: {
6112 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006113 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006114 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006115 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6116 MVT::f64);
6117 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6118 MVT::f64);
6119 break;
6120 }
Evan Cheng279101e2006-12-12 22:19:28 +00006121 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006122 if (getTypeAction(Lo.getValueType()) == Expand)
6123 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006124 break;
6125 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006126 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006127 // Return the operands.
6128 Lo = Node->getOperand(0);
6129 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006130 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006131
6132 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006133 if (Node->getNumValues() == 1) {
6134 ExpandOp(Op.getOperand(0), Lo, Hi);
6135 break;
6136 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006137 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006138 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006139 Op.getValue(1).getValueType() == MVT::Other &&
6140 "unhandled MERGE_VALUES");
6141 ExpandOp(Op.getOperand(0), Lo, Hi);
6142 // Remember that we legalized the chain.
6143 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6144 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006145
6146 case ISD::SIGN_EXTEND_INREG:
6147 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006148 // sext_inreg the low part if needed.
6149 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6150
6151 // The high part gets the sign extension from the lo-part. This handles
6152 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006153 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006154 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006155 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006156 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006157
Nate Begemand88fc032006-01-14 03:14:10 +00006158 case ISD::BSWAP: {
6159 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006160 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006161 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6162 Lo = TempLo;
6163 break;
6164 }
6165
Chris Lattneredb1add2005-05-11 04:51:16 +00006166 case ISD::CTPOP:
6167 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006168 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6169 DAG.getNode(ISD::CTPOP, NVT, Lo),
6170 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006171 Hi = DAG.getConstant(0, NVT);
6172 break;
6173
Chris Lattner39a8f332005-05-12 19:05:01 +00006174 case ISD::CTLZ: {
6175 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006176 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006177 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6178 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6179 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006180 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006181 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006182 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6183
6184 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6185 Hi = DAG.getConstant(0, NVT);
6186 break;
6187 }
6188
6189 case ISD::CTTZ: {
6190 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006191 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006192 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6193 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6194 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006195 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006196 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006197 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6198
6199 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6200 Hi = DAG.getConstant(0, NVT);
6201 break;
6202 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006203
Nate Begemanacc398c2006-01-25 18:21:52 +00006204 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006205 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6206 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006207 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6208 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6209
6210 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006211 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006212 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006213 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006214 std::swap(Lo, Hi);
6215 break;
6216 }
6217
Chris Lattner3e928bb2005-01-07 07:47:09 +00006218 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006219 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006220 SDValue Ch = LD->getChain(); // Legalize the chain.
6221 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006222 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006223 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006224 int SVOffset = LD->getSrcValueOffset();
6225 unsigned Alignment = LD->getAlignment();
6226 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006227
Evan Cheng466685d2006-10-09 20:57:25 +00006228 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006229 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006230 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006231 if (VT == MVT::f32 || VT == MVT::f64) {
6232 // f32->i32 or f64->i64 one to one expansion.
6233 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006234 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006235 // Recursively expand the new load.
6236 if (getTypeAction(NVT) == Expand)
6237 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006238 break;
6239 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006240
Evan Cheng466685d2006-10-09 20:57:25 +00006241 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006242 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006243 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006244 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006245 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006246 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006247 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006248 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006249
Evan Cheng466685d2006-10-09 20:57:25 +00006250 // Build a factor node to remember that this load is independent of the
6251 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006252 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006253 Hi.getValue(1));
6254
6255 // Remember that we legalized the chain.
6256 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006257 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006258 std::swap(Lo, Hi);
6259 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006260 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006261
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006262 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6263 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006264 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006265 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006266 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006267 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006268 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006269 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6270 break;
6271 }
Evan Cheng466685d2006-10-09 20:57:25 +00006272
6273 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006274 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006275 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006276 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006277 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006278 SVOffset, EVT, isVolatile,
6279 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006280
6281 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006282 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006283
6284 if (ExtType == ISD::SEXTLOAD) {
6285 // The high part is obtained by SRA'ing all but one of the bits of the
6286 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006287 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006288 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6289 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6290 } else if (ExtType == ISD::ZEXTLOAD) {
6291 // The high part is just a zero.
6292 Hi = DAG.getConstant(0, NVT);
6293 } else /* if (ExtType == ISD::EXTLOAD) */ {
6294 // The high part is undefined.
6295 Hi = DAG.getNode(ISD::UNDEF, NVT);
6296 }
6297 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006298 break;
6299 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006300 case ISD::AND:
6301 case ISD::OR:
6302 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006303 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006304 ExpandOp(Node->getOperand(0), LL, LH);
6305 ExpandOp(Node->getOperand(1), RL, RH);
6306 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6307 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6308 break;
6309 }
6310 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006311 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006312 ExpandOp(Node->getOperand(1), LL, LH);
6313 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006314 if (getTypeAction(NVT) == Expand)
6315 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006316 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006317 if (VT != MVT::f32)
6318 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006319 break;
6320 }
Nate Begeman9373a812005-08-10 20:51:12 +00006321 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006322 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006323 ExpandOp(Node->getOperand(2), TL, TH);
6324 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006325 if (getTypeAction(NVT) == Expand)
6326 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006327 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6328 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006329 if (VT != MVT::f32)
6330 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6331 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006332 break;
6333 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006334 case ISD::ANY_EXTEND:
6335 // The low part is any extension of the input (which degenerates to a copy).
6336 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6337 // The high part is undefined.
6338 Hi = DAG.getNode(ISD::UNDEF, NVT);
6339 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006340 case ISD::SIGN_EXTEND: {
6341 // The low part is just a sign extension of the input (which degenerates to
6342 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006343 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006344
Chris Lattner3e928bb2005-01-07 07:47:09 +00006345 // The high part is obtained by SRA'ing all but one of the bits of the lo
6346 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006347 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006348 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6349 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006350 break;
6351 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006352 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006353 // The low part is just a zero extension of the input (which degenerates to
6354 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006355 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006356
Chris Lattner3e928bb2005-01-07 07:47:09 +00006357 // The high part is just a zero.
6358 Hi = DAG.getConstant(0, NVT);
6359 break;
Chris Lattner35481892005-12-23 00:16:34 +00006360
Chris Lattner4c948eb2007-02-13 23:55:16 +00006361 case ISD::TRUNCATE: {
6362 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006363 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006364 ExpandOp(Node->getOperand(0), NewLo, Hi);
6365
6366 // The low part is now either the right size, or it is closer. If not the
6367 // right size, make an illegal truncate so we recursively expand it.
6368 if (NewLo.getValueType() != Node->getValueType(0))
6369 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6370 ExpandOp(NewLo, Lo, Hi);
6371 break;
6372 }
6373
Chris Lattner35481892005-12-23 00:16:34 +00006374 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006375 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006376 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6377 // If the target wants to, allow it to lower this itself.
6378 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6379 case Expand: assert(0 && "cannot expand FP!");
6380 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6381 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6382 }
6383 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6384 }
6385
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006386 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006387 if (VT == MVT::f32 || VT == MVT::f64) {
6388 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006389 if (getTypeAction(NVT) == Expand)
6390 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006391 break;
6392 }
6393
6394 // If source operand will be expanded to the same type as VT, i.e.
6395 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006396 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006397 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6398 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006399 break;
6400 }
6401
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006402 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006403 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006404 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006405
Chris Lattner35481892005-12-23 00:16:34 +00006406 ExpandOp(Tmp, Lo, Hi);
6407 break;
6408 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006409
Chris Lattner27a6c732007-11-24 07:07:01 +00006410 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006411 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6412 TargetLowering::Custom &&
6413 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006414 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006415 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006416 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006417 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006418 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006419 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006420 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006421
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006422 case ISD::ATOMIC_CMP_SWAP_64: {
6423 // This operation does not need a loop.
6424 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6425 assert(Tmp.getNode() && "Node must be custom expanded!");
6426 ExpandOp(Tmp.getValue(0), Lo, Hi);
6427 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6428 LegalizeOp(Tmp.getValue(1)));
6429 break;
6430 }
6431
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006432 case ISD::ATOMIC_LOAD_ADD_64:
6433 case ISD::ATOMIC_LOAD_SUB_64:
6434 case ISD::ATOMIC_LOAD_AND_64:
6435 case ISD::ATOMIC_LOAD_OR_64:
6436 case ISD::ATOMIC_LOAD_XOR_64:
6437 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006438 case ISD::ATOMIC_SWAP_64: {
6439 // These operations require a loop to be generated. We can't do that yet,
6440 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006441 SDValue In2Lo, In2Hi, In2;
6442 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6443 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006444 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6445 SDValue Replace =
6446 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6447 Anode->getSrcValue(), Anode->getAlignment());
6448 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006449 ExpandOp(Result.getValue(0), Lo, Hi);
6450 // Remember that we legalized the chain.
6451 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006452 break;
6453 }
6454
Chris Lattner4e6c7462005-01-08 19:27:05 +00006455 // These operators cannot be expanded directly, emit them as calls to
6456 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006457 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006458 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006459 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006460 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6461 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006462 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6463 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006464 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006465
Chris Lattnerf20d1832005-07-30 01:40:57 +00006466 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6467
Chris Lattner80a3e942005-07-29 00:33:32 +00006468 // Now that the custom expander is done, expand the result, which is still
6469 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006470 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006471 ExpandOp(Op, Lo, Hi);
6472 break;
6473 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006474 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006475
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006476 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6477 VT);
6478 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6479 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006480 break;
Evan Cheng56966222007-01-12 02:11:51 +00006481 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006482
Evan Cheng56966222007-01-12 02:11:51 +00006483 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006484 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006485 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006486 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6487 case Expand: assert(0 && "cannot expand FP!");
6488 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6489 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6490 }
6491
6492 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6493
6494 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006495 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006496 ExpandOp(Op, Lo, Hi);
6497 break;
6498 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006499 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006500
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006501 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6502 VT);
6503 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6504 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006505 break;
Evan Cheng56966222007-01-12 02:11:51 +00006506 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006507
Evan Cheng05a2d562006-01-09 18:31:59 +00006508 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006509 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006510 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006511 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006512 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006513 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006514 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006515 // Now that the custom expander is done, expand the result, which is
6516 // still VT.
6517 ExpandOp(Op, Lo, Hi);
6518 break;
6519 }
6520 }
6521
Chris Lattner79980b02006-09-13 03:50:39 +00006522 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6523 // this X << 1 as X+X.
6524 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006525 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006526 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006527 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006528 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6529 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6530 LoOps[1] = LoOps[0];
6531 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6532
6533 HiOps[1] = HiOps[0];
6534 HiOps[2] = Lo.getValue(1);
6535 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6536 break;
6537 }
6538 }
6539
Chris Lattnere34b3962005-01-19 04:19:40 +00006540 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006541 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006542 break;
Chris Lattner47599822005-04-02 03:38:53 +00006543
6544 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006545 TargetLowering::LegalizeAction Action =
6546 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6547 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6548 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006549 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006550 break;
6551 }
6552
Chris Lattnere34b3962005-01-19 04:19:40 +00006553 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006554 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006555 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006556 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006557
Evan Cheng05a2d562006-01-09 18:31:59 +00006558 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006559 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006560 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006561 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006562 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006563 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006564 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006565 // Now that the custom expander is done, expand the result, which is
6566 // still VT.
6567 ExpandOp(Op, Lo, Hi);
6568 break;
6569 }
6570 }
6571
Chris Lattnere34b3962005-01-19 04:19:40 +00006572 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006573 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006574 break;
Chris Lattner47599822005-04-02 03:38:53 +00006575
6576 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006577 TargetLowering::LegalizeAction Action =
6578 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6579 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6580 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006581 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006582 break;
6583 }
6584
Chris Lattnere34b3962005-01-19 04:19:40 +00006585 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006586 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006587 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006588 }
6589
6590 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006591 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006592 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006593 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006594 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006595 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006596 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006597 // Now that the custom expander is done, expand the result, which is
6598 // still VT.
6599 ExpandOp(Op, Lo, Hi);
6600 break;
6601 }
6602 }
6603
Chris Lattnere34b3962005-01-19 04:19:40 +00006604 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006605 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006606 break;
Chris Lattner47599822005-04-02 03:38:53 +00006607
6608 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006609 TargetLowering::LegalizeAction Action =
6610 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6611 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6612 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006613 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006614 break;
6615 }
6616
Chris Lattnere34b3962005-01-19 04:19:40 +00006617 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006618 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006619 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006620 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006621
Misha Brukmanedf128a2005-04-21 22:36:52 +00006622 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006623 case ISD::SUB: {
6624 // If the target wants to custom expand this, let them.
6625 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6626 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006627 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006628 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006629 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006630 break;
6631 }
6632 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006633 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006634 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006635 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6636 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006637 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006638 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006639 LoOps[0] = LHSL;
6640 LoOps[1] = RHSL;
6641 HiOps[0] = LHSH;
6642 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006643
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006644 //cascaded check to see if any smaller size has a a carry flag.
6645 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6646 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006647 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6648 MVT AVT = MVT::getIntegerVT(BitSize);
6649 if (TLI.isOperationLegal(OpV, AVT)) {
6650 hasCarry = true;
6651 break;
6652 }
6653 }
6654
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006655 if(hasCarry) {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006656 if (Node->getOpcode() == ISD::ADD) {
6657 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6658 HiOps[2] = Lo.getValue(1);
6659 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6660 } else {
6661 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6662 HiOps[2] = Lo.getValue(1);
6663 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6664 }
6665 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006666 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006667 if (Node->getOpcode() == ISD::ADD) {
6668 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
6669 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006670 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6671 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006672 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6673 DAG.getConstant(1, NVT),
6674 DAG.getConstant(0, NVT));
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006675 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6676 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006677 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6678 DAG.getConstant(1, NVT),
6679 Carry1);
6680 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6681 } else {
6682 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
6683 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
6684 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6685 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6686 DAG.getConstant(1, NVT),
6687 DAG.getConstant(0, NVT));
6688 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6689 }
6690 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006691 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006692 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006693
6694 case ISD::ADDC:
6695 case ISD::SUBC: {
6696 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006697 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006698 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6699 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6700 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006701 SDValue LoOps[2] = { LHSL, RHSL };
6702 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006703
6704 if (Node->getOpcode() == ISD::ADDC) {
6705 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6706 HiOps[2] = Lo.getValue(1);
6707 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6708 } else {
6709 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6710 HiOps[2] = Lo.getValue(1);
6711 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6712 }
6713 // Remember that we legalized the flag.
6714 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6715 break;
6716 }
6717 case ISD::ADDE:
6718 case ISD::SUBE: {
6719 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006720 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006721 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6722 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6723 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006724 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6725 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006726
6727 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6728 HiOps[2] = Lo.getValue(1);
6729 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6730
6731 // Remember that we legalized the flag.
6732 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6733 break;
6734 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006735 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006736 // If the target wants to custom expand this, let them.
6737 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006738 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006739 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006740 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006741 break;
6742 }
6743 }
6744
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006745 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6746 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006747 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6748 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6749 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00006750 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00006751 ExpandOp(Node->getOperand(0), LL, LH);
6752 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006753 unsigned OuterBitSize = Op.getValueSizeInBits();
6754 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006755 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6756 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006757 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6758 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6759 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006760 // The inputs are both zero-extended.
6761 if (HasUMUL_LOHI) {
6762 // We can emit a umul_lohi.
6763 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006764 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006765 break;
6766 }
6767 if (HasMULHU) {
6768 // We can emit a mulhu+mul.
6769 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6770 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6771 break;
6772 }
Dan Gohman525178c2007-10-08 18:33:35 +00006773 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006774 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006775 // The input values are both sign-extended.
6776 if (HasSMUL_LOHI) {
6777 // We can emit a smul_lohi.
6778 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006779 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006780 break;
6781 }
6782 if (HasMULHS) {
6783 // We can emit a mulhs+mul.
6784 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6785 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6786 break;
6787 }
6788 }
6789 if (HasUMUL_LOHI) {
6790 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00006791 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00006792 DAG.getVTList(NVT, NVT), LL, RL);
6793 Lo = UMulLOHI;
6794 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006795 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6796 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6797 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6798 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006799 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006800 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006801 if (HasMULHU) {
6802 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6803 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6804 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6805 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6806 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6807 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6808 break;
6809 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006810 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006811
Dan Gohman525178c2007-10-08 18:33:35 +00006812 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006813 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006814 break;
6815 }
Evan Cheng56966222007-01-12 02:11:51 +00006816 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006817 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006818 break;
6819 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006820 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006821 break;
6822 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006823 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006824 break;
6825 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006826 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006827 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006828
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006829 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00006830 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6831 RTLIB::ADD_F64,
6832 RTLIB::ADD_F80,
6833 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006834 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006835 break;
6836 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00006837 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
6838 RTLIB::SUB_F64,
6839 RTLIB::SUB_F80,
6840 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006841 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006842 break;
6843 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00006844 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
6845 RTLIB::MUL_F64,
6846 RTLIB::MUL_F80,
6847 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006848 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006849 break;
6850 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006851 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
6852 RTLIB::DIV_F64,
6853 RTLIB::DIV_F80,
6854 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006855 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006856 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006857 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00006858 if (VT == MVT::ppcf128) {
6859 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
6860 Node->getOperand(0).getValueType()==MVT::f64);
6861 const uint64_t zero = 0;
6862 if (Node->getOperand(0).getValueType()==MVT::f32)
6863 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
6864 else
6865 Hi = Node->getOperand(0);
6866 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
6867 break;
6868 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006869 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
6870 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
6871 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006872 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006873 }
6874 case ISD::FP_ROUND: {
6875 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
6876 VT);
6877 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
6878 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006879 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006880 }
Evan Cheng4b887022008-09-09 23:02:14 +00006881 case ISD::FSQRT:
6882 case ISD::FSIN:
6883 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00006884 case ISD::FLOG:
6885 case ISD::FLOG2:
6886 case ISD::FLOG10:
6887 case ISD::FEXP:
6888 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00006889 case ISD::FTRUNC:
6890 case ISD::FFLOOR:
6891 case ISD::FCEIL:
6892 case ISD::FRINT:
6893 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00006894 case ISD::FPOW:
6895 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00006896 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006897 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00006898 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00006899 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
6900 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006901 break;
6902 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00006903 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
6904 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006905 break;
6906 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00006907 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
6908 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00006909 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00006910 case ISD::FLOG:
6911 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
6912 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
6913 break;
6914 case ISD::FLOG2:
6915 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
6916 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
6917 break;
6918 case ISD::FLOG10:
6919 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
6920 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
6921 break;
6922 case ISD::FEXP:
6923 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
6924 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
6925 break;
6926 case ISD::FEXP2:
6927 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
6928 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
6929 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00006930 case ISD::FTRUNC:
6931 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
6932 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
6933 break;
6934 case ISD::FFLOOR:
6935 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
6936 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
6937 break;
6938 case ISD::FCEIL:
6939 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
6940 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
6941 break;
6942 case ISD::FRINT:
6943 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
6944 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
6945 break;
6946 case ISD::FNEARBYINT:
6947 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
6948 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
6949 break;
Evan Cheng4b887022008-09-09 23:02:14 +00006950 case ISD::FPOW:
6951 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
6952 RTLIB::POW_PPCF128);
6953 break;
6954 case ISD::FPOWI:
6955 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
6956 RTLIB::POWI_PPCF128);
6957 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00006958 default: assert(0 && "Unreachable!");
6959 }
Duncan Sands460a14e2008-04-12 17:14:18 +00006960 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00006961 break;
6962 }
Evan Cheng966bf242006-12-16 00:52:40 +00006963 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006964 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00006965 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00006966 ExpandOp(Node->getOperand(0), Lo, Tmp);
6967 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
6968 // lo = hi==fabs(hi) ? lo : -lo;
6969 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
6970 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
6971 DAG.getCondCode(ISD::SETEQ));
6972 break;
6973 }
Dan Gohman475871a2008-07-27 21:46:04 +00006974 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006975 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
6976 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
6977 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6978 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6979 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
6980 if (getTypeAction(NVT) == Expand)
6981 ExpandOp(Lo, Lo, Hi);
6982 break;
6983 }
6984 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00006985 if (VT == MVT::ppcf128) {
6986 ExpandOp(Node->getOperand(0), Lo, Hi);
6987 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
6988 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
6989 break;
6990 }
Dan Gohman475871a2008-07-27 21:46:04 +00006991 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00006992 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
6993 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
6994 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
6995 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
6996 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
6997 if (getTypeAction(NVT) == Expand)
6998 ExpandOp(Lo, Lo, Hi);
6999 break;
7000 }
Evan Cheng912095b2007-01-04 21:56:39 +00007001 case ISD::FCOPYSIGN: {
7002 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7003 if (getTypeAction(NVT) == Expand)
7004 ExpandOp(Lo, Lo, Hi);
7005 break;
7006 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007007 case ISD::SINT_TO_FP:
7008 case ISD::UINT_TO_FP: {
7009 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007010 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007011
7012 // Promote the operand if needed. Do this before checking for
7013 // ppcf128 so conversions of i16 and i8 work.
7014 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007015 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007016 Tmp = isSigned
7017 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7018 DAG.getValueType(SrcVT))
7019 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007020 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007021 SrcVT = Node->getOperand(0).getValueType();
7022 }
7023
Dan Gohmana2e94852008-03-10 23:03:31 +00007024 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007025 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007026 if (isSigned) {
7027 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7028 Node->getOperand(0)));
7029 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7030 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007031 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007032 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7033 Node->getOperand(0)));
7034 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7035 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007036 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007037 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7038 DAG.getConstant(0, MVT::i32),
7039 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7040 DAG.getConstantFP(
7041 APFloat(APInt(128, 2, TwoE32)),
7042 MVT::ppcf128)),
7043 Hi,
7044 DAG.getCondCode(ISD::SETLT)),
7045 Lo, Hi);
7046 }
7047 break;
7048 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007049 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7050 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007051 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007052 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7053 Lo, Hi);
7054 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7055 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7056 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7057 DAG.getConstant(0, MVT::i64),
7058 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7059 DAG.getConstantFP(
7060 APFloat(APInt(128, 2, TwoE64)),
7061 MVT::ppcf128)),
7062 Hi,
7063 DAG.getCondCode(ISD::SETLT)),
7064 Lo, Hi);
7065 break;
7066 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007067
Dan Gohmana2e94852008-03-10 23:03:31 +00007068 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7069 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00007070 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007071 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007072 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007073 break;
7074 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007075 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007076
Chris Lattner83397362005-12-21 18:02:52 +00007077 // Make sure the resultant values have been legalized themselves, unless this
7078 // is a type that requires multi-step expansion.
7079 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7080 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007081 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007082 // Don't legalize the high part if it is expanded to a single node.
7083 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007084 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007085
7086 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007087 bool isNew =
7088 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007089 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00007090}
7091
Dan Gohman7f321562007-06-25 16:23:39 +00007092/// SplitVectorOp - Given an operand of vector type, break it down into
7093/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007094void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7095 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007096 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007097 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007098 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007099 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007100
Duncan Sands83ec4b62008-06-06 12:08:01 +00007101 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007102
7103 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7104 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7105
Duncan Sands83ec4b62008-06-06 12:08:01 +00007106 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7107 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007108
Chris Lattnerc7029802006-03-18 01:44:44 +00007109 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007110 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007111 = SplitNodes.find(Op);
7112 if (I != SplitNodes.end()) {
7113 Lo = I->second.first;
7114 Hi = I->second.second;
7115 return;
7116 }
7117
7118 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007119 default:
7120#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007121 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007122#endif
7123 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007124 case ISD::UNDEF:
7125 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7126 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7127 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007128 case ISD::BUILD_PAIR:
7129 Lo = Node->getOperand(0);
7130 Hi = Node->getOperand(1);
7131 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007132 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007133 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7134 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007135 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007136 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007137 if (Index < NewNumElts_Lo)
7138 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7139 DAG.getIntPtrConstant(Index));
7140 else
7141 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7142 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7143 break;
7144 }
Dan Gohman475871a2008-07-27 21:46:04 +00007145 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007146 Node->getOperand(1),
7147 Node->getOperand(2));
7148 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007149 break;
7150 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007151 case ISD::VECTOR_SHUFFLE: {
7152 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007153 SDValue Mask = Node->getOperand(2);
7154 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007155 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007156
7157 // Insert all of the elements from the input that are needed. We use
7158 // buildvector of extractelement here because the input vectors will have
7159 // to be legalized, so this makes the code simpler.
7160 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007161 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007162 if (IdxNode.getOpcode() == ISD::UNDEF) {
7163 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7164 continue;
7165 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007166 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007167 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007168 if (Idx >= NumElements) {
7169 InVec = Node->getOperand(1);
7170 Idx -= NumElements;
7171 }
7172 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7173 DAG.getConstant(Idx, PtrVT)));
7174 }
7175 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7176 Ops.clear();
7177
7178 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007179 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007180 if (IdxNode.getOpcode() == ISD::UNDEF) {
7181 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7182 continue;
7183 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007184 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007185 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007186 if (Idx >= NumElements) {
7187 InVec = Node->getOperand(1);
7188 Idx -= NumElements;
7189 }
7190 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7191 DAG.getConstant(Idx, PtrVT)));
7192 }
Mon P Wang92879f32008-07-25 01:30:26 +00007193 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007194 break;
7195 }
Dan Gohman7f321562007-06-25 16:23:39 +00007196 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007197 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007198 Node->op_begin()+NewNumElts_Lo);
7199 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007200
Dan Gohman475871a2008-07-27 21:46:04 +00007201 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007202 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007203 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007204 break;
7205 }
Dan Gohman7f321562007-06-25 16:23:39 +00007206 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007207 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007208 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7209 if (NewNumSubvectors == 1) {
7210 Lo = Node->getOperand(0);
7211 Hi = Node->getOperand(1);
7212 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00007213 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Dan Gohman7f321562007-06-25 16:23:39 +00007214 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007215 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007216
Dan Gohman475871a2008-07-27 21:46:04 +00007217 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007218 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007219 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007220 }
Dan Gohman65956352007-06-13 15:12:02 +00007221 break;
7222 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007223 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007224 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007225
Dan Gohman475871a2008-07-27 21:46:04 +00007226 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007227 SplitVectorOp(Node->getOperand(1), LL, LH);
7228 SplitVectorOp(Node->getOperand(2), RL, RH);
7229
Duncan Sands83ec4b62008-06-06 12:08:01 +00007230 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007231 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007232 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007233 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007234 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7235 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007236 } else {
7237 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007238 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7239 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007240 }
7241 break;
7242 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007243 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007244 SDValue CondLHS = Node->getOperand(0);
7245 SDValue CondRHS = Node->getOperand(1);
7246 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007247
Dan Gohman475871a2008-07-27 21:46:04 +00007248 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007249 SplitVectorOp(Node->getOperand(2), LL, LH);
7250 SplitVectorOp(Node->getOperand(3), RL, RH);
7251
7252 // Handle a simple select with vector operands.
7253 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7254 LL, RL, CondCode);
7255 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7256 LH, RH, CondCode);
7257 break;
7258 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007259 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007260 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007261 SplitVectorOp(Node->getOperand(0), LL, LH);
7262 SplitVectorOp(Node->getOperand(1), RL, RH);
7263 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7264 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7265 break;
7266 }
Dan Gohman7f321562007-06-25 16:23:39 +00007267 case ISD::ADD:
7268 case ISD::SUB:
7269 case ISD::MUL:
7270 case ISD::FADD:
7271 case ISD::FSUB:
7272 case ISD::FMUL:
7273 case ISD::SDIV:
7274 case ISD::UDIV:
7275 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007276 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007277 case ISD::AND:
7278 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007279 case ISD::XOR:
7280 case ISD::UREM:
7281 case ISD::SREM:
7282 case ISD::FREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00007283 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007284 SplitVectorOp(Node->getOperand(0), LL, LH);
7285 SplitVectorOp(Node->getOperand(1), RL, RH);
7286
Nate Begeman5db1afb2007-11-15 21:15:26 +00007287 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7288 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007289 break;
7290 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007291 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007292 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007293 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007294 SplitVectorOp(Node->getOperand(0), L, H);
7295
Nate Begeman5db1afb2007-11-15 21:15:26 +00007296 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7297 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007298 break;
7299 }
7300 case ISD::CTTZ:
7301 case ISD::CTLZ:
7302 case ISD::CTPOP:
7303 case ISD::FNEG:
7304 case ISD::FABS:
7305 case ISD::FSQRT:
7306 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007307 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007308 case ISD::FLOG:
7309 case ISD::FLOG2:
7310 case ISD::FLOG10:
7311 case ISD::FEXP:
7312 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007313 case ISD::FP_TO_SINT:
7314 case ISD::FP_TO_UINT:
7315 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007316 case ISD::UINT_TO_FP:
7317 case ISD::TRUNCATE:
7318 case ISD::ANY_EXTEND:
7319 case ISD::SIGN_EXTEND:
7320 case ISD::ZERO_EXTEND:
7321 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007322 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007323 SplitVectorOp(Node->getOperand(0), L, H);
7324
Nate Begeman5db1afb2007-11-15 21:15:26 +00007325 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7326 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007327 break;
7328 }
Dan Gohman7f321562007-06-25 16:23:39 +00007329 case ISD::LOAD: {
7330 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007331 SDValue Ch = LD->getChain();
7332 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007333 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007334 const Value *SV = LD->getSrcValue();
7335 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007336 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007337 unsigned Alignment = LD->getAlignment();
7338 bool isVolatile = LD->isVolatile();
7339
Dan Gohman7f8613e2008-08-14 20:04:46 +00007340 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7341 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7342
7343 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7344 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7345 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7346
7347 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7348 NewVT_Lo, Ch, Ptr, Offset,
7349 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7350 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007351 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007352 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007353 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007354 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007355 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7356 NewVT_Hi, Ch, Ptr, Offset,
7357 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007358
7359 // Build a factor node to remember that this load is independent of the
7360 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007361 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007362 Hi.getValue(1));
7363
7364 // Remember that we legalized the chain.
7365 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007366 break;
7367 }
Dan Gohman7f321562007-06-25 16:23:39 +00007368 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007369 // We know the result is a vector. The input may be either a vector or a
7370 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007371 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007372 if (!InOp.getValueType().isVector() ||
7373 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007374 // The input is a scalar or single-element vector.
7375 // Lower to a store/load so that it can be split.
7376 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007377 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7378 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007379 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007380 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007381
Dan Gohman475871a2008-07-27 21:46:04 +00007382 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007383 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007384 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007385 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007386 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007387 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007388 // Split the vector and convert each of the pieces now.
7389 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007390 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7391 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007392 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007393 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007394 }
7395
7396 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007397 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007398 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007399 assert(isNew && "Value already split?!?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007400}
7401
7402
Dan Gohman89b20c02007-06-27 14:06:22 +00007403/// ScalarizeVectorOp - Given an operand of single-element vector type
7404/// (e.g. v1f32), convert it into the equivalent operation that returns a
7405/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007406SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007407 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007408 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007409 MVT NewVT = Op.getValueType().getVectorElementType();
7410 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007411
Dan Gohman7f321562007-06-25 16:23:39 +00007412 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007413 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007414 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007415
Dan Gohman475871a2008-07-27 21:46:04 +00007416 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007417 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007418 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007419#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007420 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007421#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007422 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7423 case ISD::ADD:
7424 case ISD::FADD:
7425 case ISD::SUB:
7426 case ISD::FSUB:
7427 case ISD::MUL:
7428 case ISD::FMUL:
7429 case ISD::SDIV:
7430 case ISD::UDIV:
7431 case ISD::FDIV:
7432 case ISD::SREM:
7433 case ISD::UREM:
7434 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007435 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007436 case ISD::AND:
7437 case ISD::OR:
7438 case ISD::XOR:
7439 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007440 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007441 ScalarizeVectorOp(Node->getOperand(0)),
7442 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007443 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007444 case ISD::FNEG:
7445 case ISD::FABS:
7446 case ISD::FSQRT:
7447 case ISD::FSIN:
7448 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007449 case ISD::FLOG:
7450 case ISD::FLOG2:
7451 case ISD::FLOG10:
7452 case ISD::FEXP:
7453 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007454 case ISD::FP_TO_SINT:
7455 case ISD::FP_TO_UINT:
7456 case ISD::SINT_TO_FP:
7457 case ISD::UINT_TO_FP:
7458 case ISD::SIGN_EXTEND:
7459 case ISD::ZERO_EXTEND:
7460 case ISD::ANY_EXTEND:
7461 case ISD::TRUNCATE:
7462 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007463 Result = DAG.getNode(Node->getOpcode(),
7464 NewVT,
7465 ScalarizeVectorOp(Node->getOperand(0)));
7466 break;
Dan Gohman9e04c822007-10-12 14:13:46 +00007467 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007468 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007469 Result = DAG.getNode(Node->getOpcode(),
7470 NewVT,
7471 ScalarizeVectorOp(Node->getOperand(0)),
7472 Node->getOperand(1));
7473 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007474 case ISD::LOAD: {
7475 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007476 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7477 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007478 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007479 const Value *SV = LD->getSrcValue();
7480 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007481 MVT MemoryVT = LD->getMemoryVT();
7482 unsigned Alignment = LD->getAlignment();
7483 bool isVolatile = LD->isVolatile();
7484
7485 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7486 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7487
7488 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7489 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7490 MemoryVT.getVectorElementType(),
7491 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007492
Chris Lattnerc7029802006-03-18 01:44:44 +00007493 // Remember that we legalized the chain.
7494 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7495 break;
7496 }
Dan Gohman7f321562007-06-25 16:23:39 +00007497 case ISD::BUILD_VECTOR:
7498 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007499 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007500 case ISD::INSERT_VECTOR_ELT:
7501 // Returning the inserted scalar element.
7502 Result = Node->getOperand(1);
7503 break;
7504 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007505 assert(Node->getOperand(0).getValueType() == NewVT &&
7506 "Concat of non-legal vectors not yet supported!");
7507 Result = Node->getOperand(0);
7508 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007509 case ISD::VECTOR_SHUFFLE: {
7510 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007511 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007512 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007513 Result = ScalarizeVectorOp(Node->getOperand(1));
7514 else
7515 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007516 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007517 }
7518 case ISD::EXTRACT_SUBVECTOR:
Mon P Wange0b436a2008-11-06 22:52:21 +00007519 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
7520 Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007521 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007522 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007523 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007524 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007525 Op0 = ScalarizeVectorOp(Op0);
7526 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007527 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007528 }
Dan Gohman7f321562007-06-25 16:23:39 +00007529 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007530 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007531 ScalarizeVectorOp(Op.getOperand(1)),
7532 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007533 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007534 case ISD::SELECT_CC:
7535 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7536 Node->getOperand(1),
7537 ScalarizeVectorOp(Op.getOperand(2)),
7538 ScalarizeVectorOp(Op.getOperand(3)),
7539 Node->getOperand(4));
7540 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007541 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007542 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7543 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007544 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7545 Op.getOperand(2));
7546 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7547 DAG.getConstant(-1ULL, NewVT),
7548 DAG.getConstant(0ULL, NewVT));
7549 break;
7550 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007551 }
7552
7553 if (TLI.isTypeLegal(NewVT))
7554 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007555 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7556 assert(isNew && "Value already scalarized?");
Chris Lattnerc7029802006-03-18 01:44:44 +00007557 return Result;
7558}
7559
Chris Lattner3e928bb2005-01-07 07:47:09 +00007560
Mon P Wang0c397192008-10-30 08:01:45 +00007561SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7562 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7563 if (I != WidenNodes.end()) return I->second;
7564
7565 MVT VT = Op.getValueType();
7566 assert(VT.isVector() && "Cannot widen non-vector type!");
7567
7568 SDValue Result;
7569 SDNode *Node = Op.getNode();
7570 MVT EVT = VT.getVectorElementType();
7571
7572 unsigned NumElts = VT.getVectorNumElements();
7573 unsigned NewNumElts = WidenVT.getVectorNumElements();
7574 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7575 assert(NewNumElts < 17);
7576
7577 // When widen is called, it is assumed that it is more efficient to use a
7578 // wide type. The default action is to widen to operation to a wider legal
7579 // vector type and then do the operation if it is legal by calling LegalizeOp
7580 // again. If there is no vector equivalent, we will unroll the operation, do
7581 // it, and rebuild the vector. If most of the operations are vectorizible to
7582 // the legal type, the resulting code will be more efficient. If this is not
7583 // the case, the resulting code will preform badly as we end up generating
7584 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007585 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007586 switch (Node->getOpcode()) {
7587 default:
7588#ifndef NDEBUG
7589 Node->dump(&DAG);
7590#endif
7591 assert(0 && "Unexpected operation in WidenVectorOp!");
7592 break;
7593 case ISD::CopyFromReg:
7594 assert(0 && "CopyFromReg must be legal!");
7595 case ISD::UNDEF:
7596 case ISD::Constant:
7597 case ISD::ConstantFP:
7598 // To build a vector of these elements, clients should call BuildVector
7599 // and with each element instead of creating a node with a vector type
7600 assert(0 && "Unexpected operation in WidenVectorOp!");
7601 case ISD::VAARG:
7602 // Variable Arguments with vector types doesn't make any sense to me
7603 assert(0 && "Unexpected operation in WidenVectorOp!");
7604 break;
7605 case ISD::BUILD_VECTOR: {
7606 // Build a vector with undefined for the new nodes
7607 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7608 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7609 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7610 }
7611 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7612 break;
7613 }
7614 case ISD::INSERT_VECTOR_ELT: {
7615 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7616 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7617 Node->getOperand(1), Node->getOperand(2));
7618 break;
7619 }
7620 case ISD::VECTOR_SHUFFLE: {
7621 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7622 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7623 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7624 // used as permutation array. We build the vector here instead of widening
7625 // because we don't want to legalize and have it turned to something else.
7626 SDValue PermOp = Node->getOperand(2);
7627 SDValueVector NewOps;
7628 MVT PVT = PermOp.getValueType().getVectorElementType();
7629 for (unsigned i = 0; i < NumElts; ++i) {
7630 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7631 NewOps.push_back(PermOp.getOperand(i));
7632 } else {
7633 unsigned Idx =
7634 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7635 if (Idx < NumElts) {
7636 NewOps.push_back(PermOp.getOperand(i));
7637 }
7638 else {
7639 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7640 PermOp.getOperand(i).getValueType()));
7641 }
7642 }
7643 }
7644 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7645 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7646 }
7647
7648 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7649 MVT::getVectorVT(PVT, NewOps.size()),
7650 &NewOps[0], NewOps.size());
7651
7652 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7653 break;
7654 }
7655 case ISD::LOAD: {
7656 // If the load widen returns true, we can use a single load for the
7657 // vector. Otherwise, it is returning a token factor for multiple
7658 // loads.
7659 SDValue TFOp;
7660 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7661 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7662 else
7663 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7664 break;
7665 }
7666
7667 case ISD::BIT_CONVERT: {
7668 SDValue Tmp1 = Node->getOperand(0);
7669 // Converts between two different types so we need to determine
7670 // the correct widen type for the input operand.
7671 MVT TVT = Tmp1.getValueType();
7672 assert(TVT.isVector() && "can not widen non vector type");
7673 MVT TEVT = TVT.getVectorElementType();
7674 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7675 "can not widen bit bit convert that are not multiple of element type");
7676 MVT TWidenVT = MVT::getVectorVT(TEVT,
7677 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7678 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7679 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7680 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7681
7682 TargetLowering::LegalizeAction action =
7683 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7684 switch (action) {
7685 default: assert(0 && "action not supported");
7686 case TargetLowering::Legal:
7687 break;
7688 case TargetLowering::Promote:
7689 // We defer the promotion to when we legalize the op
7690 break;
7691 case TargetLowering::Expand:
7692 // Expand the operation into a bunch of nasty scalar code.
7693 Result = LegalizeOp(UnrollVectorOp(Result));
7694 break;
7695 }
7696 break;
7697 }
7698
7699 case ISD::SINT_TO_FP:
7700 case ISD::UINT_TO_FP:
7701 case ISD::FP_TO_SINT:
7702 case ISD::FP_TO_UINT: {
7703 SDValue Tmp1 = Node->getOperand(0);
7704 // Converts between two different types so we need to determine
7705 // the correct widen type for the input operand.
7706 MVT TVT = Tmp1.getValueType();
7707 assert(TVT.isVector() && "can not widen non vector type");
7708 MVT TEVT = TVT.getVectorElementType();
7709 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7710 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7711 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7712 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7713
7714 TargetLowering::LegalizeAction action =
7715 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7716 switch (action) {
7717 default: assert(0 && "action not supported");
7718 case TargetLowering::Legal:
7719 break;
7720 case TargetLowering::Promote:
7721 // We defer the promotion to when we legalize the op
7722 break;
7723 case TargetLowering::Expand:
7724 // Expand the operation into a bunch of nasty scalar code.
7725 Result = LegalizeOp(UnrollVectorOp(Result));
7726 break;
7727 }
7728 break;
7729 }
7730
7731 case ISD::FP_EXTEND:
7732 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7733 case ISD::TRUNCATE:
7734 case ISD::SIGN_EXTEND:
7735 case ISD::ZERO_EXTEND:
7736 case ISD::ANY_EXTEND:
7737 case ISD::FP_ROUND:
7738 case ISD::SIGN_EXTEND_INREG:
7739 case ISD::FABS:
7740 case ISD::FNEG:
7741 case ISD::FSQRT:
7742 case ISD::FSIN:
7743 case ISD::FCOS: {
7744 // Unary op widening
7745 SDValue Tmp1;
7746 TargetLowering::LegalizeAction action =
7747 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7748
7749 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7750 assert(Tmp1.getValueType() == WidenVT);
7751 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7752 switch (action) {
7753 default: assert(0 && "action not supported");
7754 case TargetLowering::Legal:
7755 break;
7756 case TargetLowering::Promote:
7757 // We defer the promotion to when we legalize the op
7758 break;
7759 case TargetLowering::Expand:
7760 // Expand the operation into a bunch of nasty scalar code.
7761 Result = LegalizeOp(UnrollVectorOp(Result));
7762 break;
7763 }
7764 break;
7765 }
7766 case ISD::FPOW:
7767 case ISD::FPOWI:
7768 case ISD::ADD:
7769 case ISD::SUB:
7770 case ISD::MUL:
7771 case ISD::MULHS:
7772 case ISD::MULHU:
7773 case ISD::AND:
7774 case ISD::OR:
7775 case ISD::XOR:
7776 case ISD::FADD:
7777 case ISD::FSUB:
7778 case ISD::FMUL:
7779 case ISD::SDIV:
7780 case ISD::SREM:
7781 case ISD::FDIV:
7782 case ISD::FREM:
7783 case ISD::FCOPYSIGN:
7784 case ISD::UDIV:
7785 case ISD::UREM:
7786 case ISD::BSWAP: {
7787 // Binary op widening
7788 TargetLowering::LegalizeAction action =
7789 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7790
7791 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7792 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7793 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
7794 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
7795 switch (action) {
7796 default: assert(0 && "action not supported");
7797 case TargetLowering::Legal:
7798 break;
7799 case TargetLowering::Promote:
7800 // We defer the promotion to when we legalize the op
7801 break;
7802 case TargetLowering::Expand:
7803 // Expand the operation into a bunch of nasty scalar code by first
7804 // Widening to the right type and then unroll the beast.
7805 Result = LegalizeOp(UnrollVectorOp(Result));
7806 break;
7807 }
7808 break;
7809 }
7810
7811 case ISD::SHL:
7812 case ISD::SRA:
7813 case ISD::SRL: {
7814 // Binary op with one non vector operand
7815 TargetLowering::LegalizeAction action =
7816 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7817
7818 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7819 assert(Tmp1.getValueType() == WidenVT);
7820 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
7821 switch (action) {
7822 default: assert(0 && "action not supported");
7823 case TargetLowering::Legal:
7824 break;
7825 case TargetLowering::Promote:
7826 // We defer the promotion to when we legalize the op
7827 break;
7828 case TargetLowering::Expand:
7829 // Expand the operation into a bunch of nasty scalar code.
7830 Result = LegalizeOp(UnrollVectorOp(Result));
7831 break;
7832 }
7833 break;
7834 }
7835 case ISD::EXTRACT_VECTOR_ELT: {
7836 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7837 assert(Tmp1.getValueType() == WidenVT);
7838 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
7839 break;
7840 }
7841 case ISD::CONCAT_VECTORS: {
7842 // We concurrently support only widen on a multiple of the incoming vector.
7843 // We could widen on a multiple of the incoming operand if necessary.
7844 unsigned NumConcat = NewNumElts / NumElts;
7845 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
7846 std::vector<SDValue> UnOps(NumElts, DAG.getNode(ISD::UNDEF,
7847 VT.getVectorElementType()));
7848 SDValue UndefVal = DAG.getNode(ISD::BUILD_VECTOR, VT,
7849 &UnOps[0], UnOps.size());
7850 SmallVector<SDValue, 8> MOps;
7851 MOps.push_back(Op);
7852 for (unsigned i = 1; i != NumConcat; ++i) {
7853 MOps.push_back(UndefVal);
7854 }
7855 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
7856 &MOps[0], MOps.size()));
7857 break;
7858 }
7859 case ISD::EXTRACT_SUBVECTOR: {
7860 SDValue Tmp1;
7861
7862 // The incoming vector might already be the proper type
7863 if (Node->getOperand(0).getValueType() != WidenVT)
7864 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7865 else
7866 Tmp1 = Node->getOperand(0);
7867 assert(Tmp1.getValueType() == WidenVT);
7868 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
7869 break;
7870 }
7871
7872 case ISD::SELECT: {
7873 TargetLowering::LegalizeAction action =
7874 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7875
7876 // Determine new condition widen type and widen
7877 SDValue Cond1 = Node->getOperand(0);
7878 MVT CondVT = Cond1.getValueType();
7879 assert(CondVT.isVector() && "can not widen non vector type");
7880 MVT CondEVT = CondVT.getVectorElementType();
7881 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
7882 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
7883 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
7884
7885 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
7886 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
7887 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
7888 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
7889 switch (action) {
7890 default: assert(0 && "action not supported");
7891 case TargetLowering::Legal:
7892 break;
7893 case TargetLowering::Promote:
7894 // We defer the promotion to when we legalize the op
7895 break;
7896 case TargetLowering::Expand:
7897 // Expand the operation into a bunch of nasty scalar code by first
7898 // Widening to the right type and then unroll the beast.
7899 Result = LegalizeOp(UnrollVectorOp(Result));
7900 break;
7901 }
7902 break;
7903 }
7904
7905 case ISD::SELECT_CC: {
7906 TargetLowering::LegalizeAction action =
7907 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7908
7909 // Determine new condition widen type and widen
7910 SDValue Cond1 = Node->getOperand(0);
7911 SDValue Cond2 = Node->getOperand(1);
7912 MVT CondVT = Cond1.getValueType();
7913 assert(CondVT.isVector() && "can not widen non vector type");
7914 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
7915 MVT CondEVT = CondVT.getVectorElementType();
7916 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
7917 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
7918 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
7919 assert(Cond1.getValueType() == CondWidenVT &&
7920 Cond2.getValueType() == CondWidenVT && "condition not widen");
7921
7922 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
7923 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
7924 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
7925 "operands not widen");
7926 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
7927 Tmp2, Node->getOperand(4));
7928 switch (action) {
7929 default: assert(0 && "action not supported");
7930 case TargetLowering::Legal:
7931 break;
7932 case TargetLowering::Promote:
7933 // We defer the promotion to when we legalize the op
7934 break;
7935 case TargetLowering::Expand:
7936 // Expand the operation into a bunch of nasty scalar code by first
7937 // Widening to the right type and then unroll the beast.
7938 Result = LegalizeOp(UnrollVectorOp(Result));
7939 break;
7940 }
7941 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00007942 }
7943 case ISD::VSETCC: {
7944 // Determine widen for the operand
7945 SDValue Tmp1 = Node->getOperand(0);
7946 MVT TmpVT = Tmp1.getValueType();
7947 assert(TmpVT.isVector() && "can not widen non vector type");
7948 MVT TmpEVT = TmpVT.getVectorElementType();
7949 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
7950 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
7951 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
7952 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
7953 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00007954 break;
7955 }
Mon P Wang0c397192008-10-30 08:01:45 +00007956 case ISD::ATOMIC_CMP_SWAP_8:
7957 case ISD::ATOMIC_CMP_SWAP_16:
7958 case ISD::ATOMIC_CMP_SWAP_32:
7959 case ISD::ATOMIC_CMP_SWAP_64:
7960 case ISD::ATOMIC_LOAD_ADD_8:
7961 case ISD::ATOMIC_LOAD_SUB_8:
7962 case ISD::ATOMIC_LOAD_AND_8:
7963 case ISD::ATOMIC_LOAD_OR_8:
7964 case ISD::ATOMIC_LOAD_XOR_8:
7965 case ISD::ATOMIC_LOAD_NAND_8:
7966 case ISD::ATOMIC_LOAD_MIN_8:
7967 case ISD::ATOMIC_LOAD_MAX_8:
7968 case ISD::ATOMIC_LOAD_UMIN_8:
7969 case ISD::ATOMIC_LOAD_UMAX_8:
7970 case ISD::ATOMIC_SWAP_8:
7971 case ISD::ATOMIC_LOAD_ADD_16:
7972 case ISD::ATOMIC_LOAD_SUB_16:
7973 case ISD::ATOMIC_LOAD_AND_16:
7974 case ISD::ATOMIC_LOAD_OR_16:
7975 case ISD::ATOMIC_LOAD_XOR_16:
7976 case ISD::ATOMIC_LOAD_NAND_16:
7977 case ISD::ATOMIC_LOAD_MIN_16:
7978 case ISD::ATOMIC_LOAD_MAX_16:
7979 case ISD::ATOMIC_LOAD_UMIN_16:
7980 case ISD::ATOMIC_LOAD_UMAX_16:
7981 case ISD::ATOMIC_SWAP_16:
7982 case ISD::ATOMIC_LOAD_ADD_32:
7983 case ISD::ATOMIC_LOAD_SUB_32:
7984 case ISD::ATOMIC_LOAD_AND_32:
7985 case ISD::ATOMIC_LOAD_OR_32:
7986 case ISD::ATOMIC_LOAD_XOR_32:
7987 case ISD::ATOMIC_LOAD_NAND_32:
7988 case ISD::ATOMIC_LOAD_MIN_32:
7989 case ISD::ATOMIC_LOAD_MAX_32:
7990 case ISD::ATOMIC_LOAD_UMIN_32:
7991 case ISD::ATOMIC_LOAD_UMAX_32:
7992 case ISD::ATOMIC_SWAP_32:
7993 case ISD::ATOMIC_LOAD_ADD_64:
7994 case ISD::ATOMIC_LOAD_SUB_64:
7995 case ISD::ATOMIC_LOAD_AND_64:
7996 case ISD::ATOMIC_LOAD_OR_64:
7997 case ISD::ATOMIC_LOAD_XOR_64:
7998 case ISD::ATOMIC_LOAD_NAND_64:
7999 case ISD::ATOMIC_LOAD_MIN_64:
8000 case ISD::ATOMIC_LOAD_MAX_64:
8001 case ISD::ATOMIC_LOAD_UMIN_64:
8002 case ISD::ATOMIC_LOAD_UMAX_64:
8003 case ISD::ATOMIC_SWAP_64: {
8004 // For now, we assume that using vectors for these operations don't make
8005 // much sense so we just split it. We return an empty result
8006 SDValue X, Y;
8007 SplitVectorOp(Op, X, Y);
8008 return Result;
8009 break;
8010 }
8011
8012 } // end switch (Node->getOpcode())
8013
8014 assert(Result.getNode() && "Didn't set a result!");
8015 if (Result != Op)
8016 Result = LegalizeOp(Result);
8017
Mon P Wangf007a8b2008-11-06 05:31:54 +00008018 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008019 return Result;
8020}
8021
8022// Utility function to find a legal vector type and its associated element
8023// type from a preferred width and whose vector type must be the same size
8024// as the VVT.
8025// TLI: Target lowering used to determine legal types
8026// Width: Preferred width of element type
8027// VVT: Vector value type whose size we must match.
8028// Returns VecEVT and EVT - the vector type and its associated element type
8029static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8030 MVT& EVT, MVT& VecEVT) {
8031 // We start with the preferred width, make it a power of 2 and see if
8032 // we can find a vector type of that width. If not, we reduce it by
8033 // another power of 2. If we have widen the type, a vector of bytes should
8034 // always be legal.
8035 assert(TLI.isTypeLegal(VVT));
8036 unsigned EWidth = Width + 1;
8037 do {
8038 assert(EWidth > 0);
8039 EWidth = (1 << Log2_32(EWidth-1));
8040 EVT = MVT::getIntegerVT(EWidth);
8041 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8042 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8043 } while (!TLI.isTypeLegal(VecEVT) ||
8044 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8045}
8046
8047SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8048 SDValue Chain,
8049 SDValue BasePtr,
8050 const Value *SV,
8051 int SVOffset,
8052 unsigned Alignment,
8053 bool isVolatile,
8054 unsigned LdWidth,
8055 MVT ResType) {
8056 // We assume that we have good rules to handle loading power of two loads so
8057 // we break down the operations to power of 2 loads. The strategy is to
8058 // load the largest power of 2 that we can easily transform to a legal vector
8059 // and then insert into that vector, and the cast the result into the legal
8060 // vector that we want. This avoids unnecessary stack converts.
8061 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8062 // the load is nonvolatile, we an use a wider load for the value.
8063 // Find a vector length we can load a large chunk
8064 MVT EVT, VecEVT;
8065 unsigned EVTWidth;
8066 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8067 EVTWidth = EVT.getSizeInBits();
8068
8069 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8070 isVolatile, Alignment);
8071 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8072 LdChain.push_back(LdOp.getValue(1));
8073
8074 // Check if we can load the element with one instruction
8075 if (LdWidth == EVTWidth) {
8076 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8077 }
8078
8079 // The vector element order is endianness dependent.
8080 unsigned Idx = 1;
8081 LdWidth -= EVTWidth;
8082 unsigned Offset = 0;
8083
8084 while (LdWidth > 0) {
8085 unsigned Increment = EVTWidth / 8;
8086 Offset += Increment;
8087 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8088 DAG.getIntPtrConstant(Increment));
8089
8090 if (LdWidth < EVTWidth) {
8091 // Our current type we are using is too large, use a smaller size by
8092 // using a smaller power of 2
8093 unsigned oEVTWidth = EVTWidth;
8094 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8095 EVTWidth = EVT.getSizeInBits();
8096 // Readjust position and vector position based on new load type
8097 Idx = Idx * (oEVTWidth/EVTWidth)+1;
8098 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8099 }
8100
8101 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8102 SVOffset+Offset, isVolatile,
8103 MinAlign(Alignment, Offset));
8104 LdChain.push_back(LdOp.getValue(1));
8105 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8106 DAG.getIntPtrConstant(Idx++));
8107
8108 LdWidth -= EVTWidth;
8109 }
8110
8111 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8112}
8113
8114bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8115 SDValue& TFOp,
8116 SDValue Op,
8117 MVT NVT) {
8118 // TODO: Add support for ConcatVec and the ability to load many vector
8119 // types (e.g., v4i8). This will not work when a vector register
8120 // to memory mapping is strange (e.g., vector elements are not
8121 // stored in some sequential order).
8122
8123 // It must be true that the widen vector type is bigger than where
8124 // we need to load from.
8125 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8126 MVT LdVT = LD->getMemoryVT();
8127 assert(LdVT.isVector() && NVT.isVector());
8128 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8129
8130 // Load information
8131 SDValue Chain = LD->getChain();
8132 SDValue BasePtr = LD->getBasePtr();
8133 int SVOffset = LD->getSrcValueOffset();
8134 unsigned Alignment = LD->getAlignment();
8135 bool isVolatile = LD->isVolatile();
8136 const Value *SV = LD->getSrcValue();
8137 unsigned int LdWidth = LdVT.getSizeInBits();
8138
8139 // Load value as a large register
8140 SDValueVector LdChain;
8141 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8142 Alignment, isVolatile, LdWidth, NVT);
8143
8144 if (LdChain.size() == 1) {
8145 TFOp = LdChain[0];
8146 return true;
8147 }
8148 else {
8149 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8150 return false;
8151 }
8152}
8153
8154
8155void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8156 SDValue Chain,
8157 SDValue BasePtr,
8158 const Value *SV,
8159 int SVOffset,
8160 unsigned Alignment,
8161 bool isVolatile,
8162 SDValue ValOp,
8163 unsigned StWidth) {
8164 // Breaks the stores into a series of power of 2 width stores. For any
8165 // width, we convert the vector to the vector of element size that we
8166 // want to store. This avoids requiring a stack convert.
8167
8168 // Find a width of the element type we can store with
8169 MVT VVT = ValOp.getValueType();
8170 MVT EVT, VecEVT;
8171 unsigned EVTWidth;
8172 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8173 EVTWidth = EVT.getSizeInBits();
8174
8175 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8176 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008177 DAG.getIntPtrConstant(0));
Mon P Wang0c397192008-10-30 08:01:45 +00008178 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8179 isVolatile, Alignment);
8180 StChain.push_back(StOp);
8181
8182 // Check if we are done
8183 if (StWidth == EVTWidth) {
8184 return;
8185 }
8186
8187 unsigned Idx = 1;
8188 StWidth -= EVTWidth;
8189 unsigned Offset = 0;
8190
8191 while (StWidth > 0) {
8192 unsigned Increment = EVTWidth / 8;
8193 Offset += Increment;
8194 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8195 DAG.getIntPtrConstant(Increment));
8196
8197 if (StWidth < EVTWidth) {
8198 // Our current type we are using is too large, use a smaller size by
8199 // using a smaller power of 2
8200 unsigned oEVTWidth = EVTWidth;
8201 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8202 EVTWidth = EVT.getSizeInBits();
8203 // Readjust position and vector position based on new load type
8204 Idx = Idx * (oEVTWidth/EVTWidth)+1;
8205 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8206 }
8207
8208 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
8209 DAG.getIntPtrConstant(Idx));
8210 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8211 SVOffset + Offset, isVolatile,
8212 MinAlign(Alignment, Offset)));
8213 StWidth -= EVTWidth;
8214 }
8215}
8216
8217
8218SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8219 SDValue Chain,
8220 SDValue BasePtr) {
8221 // TODO: It might be cleaner if we can use SplitVector and have more legal
8222 // vector types that can be stored into memory (e.g., v4xi8 can
8223 // be stored as a word). This will not work when a vector register
8224 // to memory mapping is strange (e.g., vector elements are not
8225 // stored in some sequential order).
8226
8227 MVT StVT = ST->getMemoryVT();
8228 SDValue ValOp = ST->getValue();
8229
8230 // Check if we have widen this node with another value
8231 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8232 if (I != WidenNodes.end())
8233 ValOp = I->second;
8234
8235 MVT VVT = ValOp.getValueType();
8236
8237 // It must be true that we the widen vector type is bigger than where
8238 // we need to store.
8239 assert(StVT.isVector() && VVT.isVector());
8240 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8241 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8242
8243 // Store value
8244 SDValueVector StChain;
8245 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8246 ST->getSrcValueOffset(), ST->getAlignment(),
8247 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8248 if (StChain.size() == 1)
8249 return StChain[0];
8250 else
8251 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8252}
8253
8254
Chris Lattner3e928bb2005-01-07 07:47:09 +00008255// SelectionDAG::Legalize - This is the entry point for the file.
8256//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00008257void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008258 /// run - This is the main entry point to this class.
8259 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00008260 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008261}
8262