blob: 97b526f4fef67e379bec1058ab743f97109940f4 [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;
Duncan Sandsb6862bb2008-12-14 09:43:15 +000054 bool TypesNeedLegalizing;
Chris Lattner3e928bb2005-01-07 07:47:09 +000055
Chris Lattner6831a812006-02-13 09:18:02 +000056 // Libcall insertion helpers.
57
58 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
59 /// legalized. We use this to ensure that calls are properly serialized
60 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000061 SDValue LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +000062
63 /// IsLegalizingCall - This member is used *only* for purposes of providing
64 /// helpful assertions that a libcall isn't created while another call is
65 /// being legalized (which could lead to non-serialized call sequences).
66 bool IsLegalizingCall;
67
Chris Lattner3e928bb2005-01-07 07:47:09 +000068 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000069 Legal, // The target natively supports this operation.
70 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000071 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000072 };
Chris Lattner6831a812006-02-13 09:18:02 +000073
Chris Lattner3e928bb2005-01-07 07:47:09 +000074 /// ValueTypeActions - This is a bitvector that contains two bits for each
75 /// value type, where the two bits correspond to the LegalizeAction enum.
76 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000077 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000078
Chris Lattner3e928bb2005-01-07 07:47:09 +000079 /// LegalizedNodes - For nodes that are of legal width, and that have more
80 /// than one use, this map indicates what regularized operand to use. This
81 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000082 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000083
Chris Lattner03c85462005-01-15 05:21:40 +000084 /// PromotedNodes - For nodes that are below legal width, and that have more
85 /// than one use, this map indicates what promoted value to use. This allows
86 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000087 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000088
Chris Lattnerc7029802006-03-18 01:44:44 +000089 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000090 /// which operands are the expanded version of the input. This allows
Chris Lattnerc7029802006-03-18 01:44:44 +000091 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000092 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000093
Chris Lattnerc7029802006-03-18 01:44:44 +000094 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000095 /// which operands are the split version of the input. This allows us
Chris Lattnerc7029802006-03-18 01:44:44 +000096 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000097 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +000098
Dan Gohman7f321562007-06-25 16:23:39 +000099 /// ScalarizedNodes - For nodes that need to be converted from vector types to
100 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000101 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000102 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000103
Mon P Wangf007a8b2008-11-06 05:31:54 +0000104 /// WidenNodes - For nodes that need to be widened from one vector type to
105 /// another, this contains the mapping of those that we have already widen.
106 /// This allows us to avoid widening more than once.
Mon P Wang0c397192008-10-30 08:01:45 +0000107 std::map<SDValue, SDValue> WidenNodes;
108
Dan Gohman475871a2008-07-27 21:46:04 +0000109 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000110 LegalizedNodes.insert(std::make_pair(From, To));
111 // If someone requests legalization of the new node, return itself.
112 if (From != To)
113 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000114 }
Dan Gohman475871a2008-07-27 21:46:04 +0000115 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000116 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000117 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000118 isNew = isNew;
Chris Lattner69a889e2005-12-20 00:53:54 +0000119 // If someone requests legalization of the new node, return itself.
120 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000121 }
Mon P Wangf007a8b2008-11-06 05:31:54 +0000122 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang0c397192008-10-30 08:01:45 +0000123 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
124 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000125 isNew = isNew;
Mon P Wang0c397192008-10-30 08:01:45 +0000126 // If someone requests legalization of the new node, return itself.
127 LegalizedNodes.insert(std::make_pair(To, To));
128 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000129
Chris Lattner3e928bb2005-01-07 07:47:09 +0000130public:
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000131 explicit SelectionDAGLegalize(SelectionDAG &DAG, bool TypesNeedLegalizing);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133 /// getTypeAction - Return how we should legalize values of this type, either
134 /// it is already legal or we need to expand it into multiple registers of
135 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000136 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000137 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000138 }
139
140 /// isTypeLegal - Return true if this type is legal on this target.
141 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000142 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000143 return getTypeAction(VT) == Legal;
144 }
145
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 void LegalizeDAG();
147
Chris Lattner456a93a2006-01-28 07:39:30 +0000148private:
Dan Gohman7f321562007-06-25 16:23:39 +0000149 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000150 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000151 void HandleOp(SDValue Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000152
153 /// LegalizeOp - We know that the specified value has a legal type.
154 /// Recursively ensure that the operands have legal types, then return the
155 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000156 SDValue LegalizeOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000157
Dan Gohman82669522007-10-11 23:57:53 +0000158 /// UnrollVectorOp - We know that the given vector has a legal type, however
159 /// the operation it performs is not legal and is an operation that we have
160 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
161 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000162 SDValue UnrollVectorOp(SDValue O);
Nate Begeman68679912008-04-25 18:07:40 +0000163
164 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
165 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
166 /// is necessary to spill the vector being inserted into to memory, perform
167 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000168 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
169 SDValue Idx);
Dan Gohman82669522007-10-11 23:57:53 +0000170
Chris Lattnerc7029802006-03-18 01:44:44 +0000171 /// PromoteOp - Given an operation that produces a value in an invalid type,
172 /// promote it to compute the value into a larger type. The produced value
173 /// will have the correct bits for the low portion of the register, but no
174 /// guarantee is made about the top bits: it may be zero, sign-extended, or
175 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000176 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000177
Dan Gohman475871a2008-07-27 21:46:04 +0000178 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000179 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman929d3eb2008-10-01 15:07:49 +0000180 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattnerc7029802006-03-18 01:44:44 +0000181 /// the ExpandedNodes map is filled in for any results that are expanded, and
182 /// the Lo/Hi values are returned. This applies to integer types and Vector
183 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000184 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000185
Mon P Wangf007a8b2008-11-06 05:31:54 +0000186 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
187 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
188 /// for the existing elements but no guarantee is made about the new elements
189 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
190 /// when we have an instruction operating on an illegal vector type and we
191 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang0c397192008-10-30 08:01:45 +0000192 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
193
Dan Gohman7f321562007-06-25 16:23:39 +0000194 /// SplitVectorOp - Given an operand of vector type, break it down into
195 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000196 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000197
Dan Gohman89b20c02007-06-27 14:06:22 +0000198 /// ScalarizeVectorOp - Given an operand of single-element vector type
199 /// (e.g. v1f32), convert it into the equivalent operation that returns a
200 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000201 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000202
Mon P Wangf007a8b2008-11-06 05:31:54 +0000203 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang0c397192008-10-30 08:01:45 +0000204 typedef SmallVector<SDValue, 16> SDValueVector;
205
206 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
207 /// the LdChain contains a single load and false if it contains a token
208 /// factor for multiple loads. It takes
209 /// Result: location to return the result
210 /// LdChain: location to return the load chain
211 /// Op: load operation to widen
212 /// NVT: widen vector result type we want for the load
213 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
214 SDValue Op, MVT NVT);
215
216 /// Helper genWidenVectorLoads - Helper function to generate a set of
217 /// loads to load a vector with a resulting wider type. It takes
218 /// LdChain: list of chains for the load we have generated
219 /// Chain: incoming chain for the ld vector
220 /// BasePtr: base pointer to load from
221 /// SV: memory disambiguation source value
222 /// SVOffset: memory disambiugation offset
223 /// Alignment: alignment of the memory
224 /// isVolatile: volatile load
225 /// LdWidth: width of memory that we want to load
226 /// ResType: the wider result result type for the resulting loaded vector
227 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
228 SDValue BasePtr, const Value *SV,
229 int SVOffset, unsigned Alignment,
230 bool isVolatile, unsigned LdWidth,
231 MVT ResType);
232
233 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
234 /// location. It takes
235 /// ST: store node that we want to replace
236 /// Chain: incoming store chain
237 /// BasePtr: base address of where we want to store into
238 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
239 SDValue BasePtr);
240
241 /// Helper genWidenVectorStores - Helper function to generate a set of
242 /// stores to store a widen vector into non widen memory
243 // It takes
244 // StChain: list of chains for the stores we have generated
245 // Chain: incoming chain for the ld vector
246 // BasePtr: base pointer to load from
247 // SV: memory disambiguation source value
248 // SVOffset: memory disambiugation offset
249 // Alignment: alignment of the memory
250 // isVolatile: volatile lod
251 // ValOp: value to store
252 // StWidth: width of memory that we want to store
253 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
254 SDValue BasePtr, const Value *SV,
255 int SVOffset, unsigned Alignment,
256 bool isVolatile, SDValue ValOp,
257 unsigned StWidth);
258
Duncan Sandsd038e042008-07-21 10:20:31 +0000259 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000260 /// specified mask and type. Targets can specify exactly which masks they
261 /// support and the code generator is tasked with not creating illegal masks.
262 ///
263 /// Note that this will also return true for shuffles that are promoted to a
264 /// different type.
265 ///
266 /// If this is a legal shuffle, this method returns the (possibly promoted)
267 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000268 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000269
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000270 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000271 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000272
Dan Gohman475871a2008-07-27 21:46:04 +0000273 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng7f042682008-10-15 02:05:31 +0000274 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
275 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
276 LegalizeSetCCOperands(LHS, RHS, CC);
277 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
278 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000279
Dan Gohman475871a2008-07-27 21:46:04 +0000280 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
281 SDValue &Hi);
282 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000283
Dan Gohman475871a2008-07-27 21:46:04 +0000284 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
285 SDValue ExpandBUILD_VECTOR(SDNode *Node);
286 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman7f8613e2008-08-14 20:04:46 +0000287 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000288 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
289 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
290 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000291
Dan Gohman475871a2008-07-27 21:46:04 +0000292 SDValue ExpandBSWAP(SDValue Op);
293 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
294 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
295 SDValue &Lo, SDValue &Hi);
296 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
297 SDValue &Lo, SDValue &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000298
Dan Gohman475871a2008-07-27 21:46:04 +0000299 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
300 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Mon P Wange9f10152008-12-09 05:46:39 +0000301
302 // Returns the legalized (truncated or extended) shift amount.
303 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000304};
305}
306
Chris Lattner4352cc92006-04-04 17:23:26 +0000307/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
308/// specified mask and type. Targets can specify exactly which masks they
309/// support and the code generator is tasked with not creating illegal masks.
310///
311/// Note that this will also return true for shuffles that are promoted to a
312/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000313SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000314 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
315 default: return 0;
316 case TargetLowering::Legal:
317 case TargetLowering::Custom:
318 break;
319 case TargetLowering::Promote: {
320 // If this is promoted to a different type, convert the shuffle mask and
321 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000322 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000323 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000324
325 // If we changed # elements, change the shuffle mask.
326 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000327 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000328 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
329 if (NumEltsGrowth > 1) {
330 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000331 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000332 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000333 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000334 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
335 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000336 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000337 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000338 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000339 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000340 }
341 }
342 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000343 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000344 }
345 VT = NVT;
346 break;
347 }
348 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000349 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000350}
351
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000352SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag, bool types)
353 : TLI(dag.getTargetLoweringInfo()), DAG(dag), TypesNeedLegalizing(types),
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000354 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000355 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000356 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000357}
358
Chris Lattner3e928bb2005-01-07 07:47:09 +0000359void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000360 LastCALLSEQ_END = DAG.getEntryNode();
361 IsLegalizingCall = false;
362
Chris Lattnerab510a72005-10-02 17:49:46 +0000363 // The legalize process is inherently a bottom-up recursive process (users
364 // legalize their uses before themselves). Given infinite stack space, we
365 // could just start legalizing on the root and traverse the whole graph. In
366 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000367 // blocks. To avoid this problem, compute an ordering of the nodes where each
368 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000369 DAG.AssignTopologicalOrder();
370 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
371 E = prior(DAG.allnodes_end()); I != next(E); ++I)
372 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000373
374 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000375 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000376 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
377 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000378
379 ExpandedNodes.clear();
380 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000381 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000382 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000383 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000384 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000385
386 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000387 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000388}
389
Chris Lattner6831a812006-02-13 09:18:02 +0000390
391/// FindCallEndFromCallStart - Given a chained node that is part of a call
392/// sequence, find the CALLSEQ_END node that terminates the call sequence.
393static SDNode *FindCallEndFromCallStart(SDNode *Node) {
394 if (Node->getOpcode() == ISD::CALLSEQ_END)
395 return Node;
396 if (Node->use_empty())
397 return 0; // No CallSeqEnd
398
399 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000400 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000401 if (TheChain.getValueType() != MVT::Other) {
402 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000403 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000404 if (TheChain.getValueType() != MVT::Other) {
405 // Otherwise, hunt for it.
406 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
407 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000408 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000409 break;
410 }
411
412 // Otherwise, we walked into a node without a chain.
413 if (TheChain.getValueType() != MVT::Other)
414 return 0;
415 }
416 }
417
418 for (SDNode::use_iterator UI = Node->use_begin(),
419 E = Node->use_end(); UI != E; ++UI) {
420
421 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000422 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000423 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
424 if (User->getOperand(i) == TheChain)
425 if (SDNode *Result = FindCallEndFromCallStart(User))
426 return Result;
427 }
428 return 0;
429}
430
431/// FindCallStartFromCallEnd - Given a chained node that is part of a call
432/// sequence, find the CALLSEQ_START node that initiates the call sequence.
433static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
434 assert(Node && "Didn't find callseq_start for a call??");
435 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
436
437 assert(Node->getOperand(0).getValueType() == MVT::Other &&
438 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000439 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000440}
441
442/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
443/// see if any uses can reach Dest. If no dest operands can get to dest,
444/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000445///
446/// Keep track of the nodes we fine that actually do lead to Dest in
447/// NodesLeadingTo. This avoids retraversing them exponential number of times.
448///
449bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000450 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000451 if (N == Dest) return true; // N certainly leads to Dest :)
452
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000453 // If we've already processed this node and it does lead to Dest, there is no
454 // need to reprocess it.
455 if (NodesLeadingTo.count(N)) return true;
456
Chris Lattner6831a812006-02-13 09:18:02 +0000457 // If the first result of this node has been already legalized, then it cannot
458 // reach N.
459 switch (getTypeAction(N->getValueType(0))) {
460 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000461 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000462 break;
463 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000464 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000465 break;
466 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000467 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000468 break;
469 }
470
471 // Okay, this node has not already been legalized. Check and legalize all
472 // operands. If none lead to Dest, then we can legalize this node.
473 bool OperandsLeadToDest = false;
474 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
475 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000476 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000477
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000478 if (OperandsLeadToDest) {
479 NodesLeadingTo.insert(N);
480 return true;
481 }
Chris Lattner6831a812006-02-13 09:18:02 +0000482
483 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000484 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000485 return false;
486}
487
Mon P Wang0c397192008-10-30 08:01:45 +0000488/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000489/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000490void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000491 MVT VT = Op.getValueType();
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000492 // If the type legalizer was run then we should never see any illegal result
493 // types here except for target constants (the type legalizer does not touch
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000494 // those) or for build vector used as a mask for a vector shuffle.
495 // FIXME: We can removed the BUILD_VECTOR case when we fix PR2957.
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000496 assert((TypesNeedLegalizing || getTypeAction(VT) == Legal ||
Mon P Wang87c8a8f2008-12-18 20:03:17 +0000497 Op.getOpcode() == ISD::TargetConstant ||
498 Op.getOpcode() == ISD::BUILD_VECTOR) &&
Duncan Sandsb6862bb2008-12-14 09:43:15 +0000499 "Illegal type introduced after type legalization?");
Dan Gohman7f321562007-06-25 16:23:39 +0000500 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000501 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000502 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000503 case Promote:
504 if (!VT.isVector()) {
505 (void)PromoteOp(Op);
506 break;
507 }
508 else {
509 // See if we can widen otherwise use Expand to either scalarize or split
510 MVT WidenVT = TLI.getWidenVectorType(VT);
511 if (WidenVT != MVT::Other) {
512 (void) WidenVectorOp(Op, WidenVT);
513 break;
514 }
515 // else fall thru to expand since we can't widen the vector
516 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000517 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000518 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000519 // If this is an illegal scalar, expand it into its two component
520 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000521 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000522 if (Op.getOpcode() == ISD::TargetConstant)
523 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000524 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000525 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000526 // If this is an illegal single element vector, convert it to a
527 // scalar operation.
528 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000529 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000530 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000531 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000532 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000533 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000534 }
535 break;
536 }
537}
Chris Lattner6831a812006-02-13 09:18:02 +0000538
Evan Cheng9f877882006-12-13 20:57:08 +0000539/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
540/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000541static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000542 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000543 bool Extend = false;
544
545 // If a FP immediate is precise when represented as a float and if the
546 // target can do an extending load from float to double, we put it into
547 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000548 // double. This shrinks FP constants and canonicalizes them for targets where
549 // an FP extending load is the same cost as a normal load (such as on the x87
550 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000551 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000552 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000553 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000554 if (VT!=MVT::f64 && VT!=MVT::f32)
555 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000556 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000557 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000558 }
559
Duncan Sands83ec4b62008-06-06 12:08:01 +0000560 MVT OrigVT = VT;
561 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000562 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000563 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000564 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
565 // Only do this if the target has a native EXTLOAD instruction from
566 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000567 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000568 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000569 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000570 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
571 VT = SVT;
572 Extend = true;
573 }
Evan Cheng00495212006-12-12 21:32:44 +0000574 }
575
Dan Gohman475871a2008-07-27 21:46:04 +0000576 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000577 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000578 if (Extend)
579 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000580 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000581 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000582 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000583 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000584}
585
Chris Lattner6831a812006-02-13 09:18:02 +0000586
Evan Cheng912095b2007-01-04 21:56:39 +0000587/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
588/// operations.
589static
Dan Gohman475871a2008-07-27 21:46:04 +0000590SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
591 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000592 MVT VT = Node->getValueType(0);
593 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000594 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
595 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000596 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000597
Evan Cheng912095b2007-01-04 21:56:39 +0000598 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000599 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000600 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
601 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000602 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000603 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000604 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000605 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000606 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000607 if (SizeDiff > 0) {
608 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
609 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
610 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000611 } else if (SizeDiff < 0) {
612 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
613 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
614 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
615 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000616
617 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000618 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000619 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
620 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
621 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000622 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000623 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
624
625 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000626 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
627 return Result;
628}
629
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000630/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
631static
Dan Gohman475871a2008-07-27 21:46:04 +0000632SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
633 TargetLowering &TLI) {
634 SDValue Chain = ST->getChain();
635 SDValue Ptr = ST->getBasePtr();
636 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000637 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000638 int Alignment = ST->getAlignment();
639 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000640 if (ST->getMemoryVT().isFloatingPoint() ||
641 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000642 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
643 if (TLI.isTypeLegal(intVT)) {
644 // Expand to a bitconvert of the value to the integer type of the
645 // same size, then a (misaligned) int store.
646 // FIXME: Does not handle truncating floating point stores!
647 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
648 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
649 SVOffset, ST->isVolatile(), Alignment);
650 } else {
651 // Do a (aligned) store to a stack slot, then copy from the stack slot
652 // to the final destination using (unaligned) integer loads and stores.
653 MVT StoredVT = ST->getMemoryVT();
654 MVT RegVT =
655 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
656 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
657 unsigned RegBytes = RegVT.getSizeInBits() / 8;
658 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000659
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000660 // Make sure the stack slot is also aligned for the register type.
661 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT, RegVT);
662
663 // Perform the original store, only redirected to the stack slot.
Duncan Sands05e11fa2008-12-12 21:47:02 +0000664 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
665 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
666 SmallVector<SDValue, 8> Stores;
667 unsigned Offset = 0;
668
669 // Do all but one copies using the full register width.
670 for (unsigned i = 1; i < NumRegs; i++) {
671 // Load one integer register's worth from the stack slot.
672 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
673 // Store it to the final location. Remember the store.
674 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
675 ST->getSrcValue(), SVOffset + Offset,
676 ST->isVolatile(),
677 MinAlign(ST->getAlignment(), Offset)));
678 // Increment the pointers.
679 Offset += RegBytes;
680 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
681 Increment);
682 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
683 }
684
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000685 // The last store may be partial. Do a truncating store. On big-endian
686 // machines this requires an extending load from the stack slot to ensure
687 // that the bits are in the right place.
688 MVT MemVT = MVT::getIntegerVT(8 * (StoredBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000689
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000690 // Load from the stack slot.
691 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Store, StackPtr,
692 NULL, 0, MemVT);
693
Duncan Sands05e11fa2008-12-12 21:47:02 +0000694 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
695 ST->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000696 MemVT, ST->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000697 MinAlign(ST->getAlignment(), Offset)));
698 // The order of the stores doesn't matter - say it with a TokenFactor.
699 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
700 Stores.size());
701 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000702 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000703 assert(ST->getMemoryVT().isInteger() &&
704 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000705 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000706 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000707 MVT NewStoredVT =
708 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
709 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000710 int IncrementSize = NumBits / 8;
711
712 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000713 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
714 SDValue Lo = Val;
715 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000716
717 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000718 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000719 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
720 ST->getSrcValue(), SVOffset, NewStoredVT,
721 ST->isVolatile(), Alignment);
722 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
723 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000724 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000725 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
726 ST->getSrcValue(), SVOffset + IncrementSize,
727 NewStoredVT, ST->isVolatile(), Alignment);
728
729 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
730}
731
732/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
733static
Dan Gohman475871a2008-07-27 21:46:04 +0000734SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
735 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000736 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000737 SDValue Chain = LD->getChain();
738 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000739 MVT VT = LD->getValueType(0);
740 MVT LoadedVT = LD->getMemoryVT();
741 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000742 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
743 if (TLI.isTypeLegal(intVT)) {
744 // Expand to a (misaligned) integer load of the same size,
745 // then bitconvert to floating point or vector.
746 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
747 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000748 LD->getAlignment());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000749 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
750 if (VT.isFloatingPoint() && LoadedVT != VT)
751 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000752
Duncan Sands05e11fa2008-12-12 21:47:02 +0000753 SDValue Ops[] = { Result, Chain };
754 return DAG.getMergeValues(Ops, 2);
755 } else {
756 // Copy the value to a (aligned) stack slot using (unaligned) integer
757 // loads and stores, then do a (aligned) load from the stack slot.
758 MVT RegVT = TLI.getRegisterType(intVT);
759 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
760 unsigned RegBytes = RegVT.getSizeInBits() / 8;
761 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
762
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000763 // Make sure the stack slot is also aligned for the register type.
764 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT, RegVT);
765
Duncan Sands05e11fa2008-12-12 21:47:02 +0000766 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
767 SmallVector<SDValue, 8> Stores;
768 SDValue StackPtr = StackBase;
769 unsigned Offset = 0;
770
771 // Do all but one copies using the full register width.
772 for (unsigned i = 1; i < NumRegs; i++) {
773 // Load one integer register's worth from the original location.
774 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
775 SVOffset + Offset, LD->isVolatile(),
776 MinAlign(LD->getAlignment(), Offset));
777 // Follow the load with a store to the stack slot. Remember the store.
778 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
779 NULL, 0));
780 // Increment the pointers.
781 Offset += RegBytes;
782 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
783 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
784 Increment);
785 }
786
787 // The last copy may be partial. Do an extending load.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000788 MVT MemVT = MVT::getIntegerVT(8 * (LoadedBytes - Offset));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000789 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
790 LD->getSrcValue(), SVOffset + Offset,
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000791 MemVT, LD->isVolatile(),
Duncan Sands05e11fa2008-12-12 21:47:02 +0000792 MinAlign(LD->getAlignment(), Offset));
793 // Follow the load with a store to the stack slot. Remember the store.
Duncan Sandsfd6673c2008-12-13 07:18:38 +0000794 // On big-endian machines this requires a truncating store to ensure
795 // that the bits end up in the right place.
796 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, StackPtr,
797 NULL, 0, MemVT));
Duncan Sands05e11fa2008-12-12 21:47:02 +0000798
799 // The order of the stores doesn't matter - say it with a TokenFactor.
800 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
801 Stores.size());
802
803 // Finally, perform the original load only redirected to the stack slot.
804 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
805 NULL, 0, LoadedVT);
806
807 // Callers expect a MERGE_VALUES node.
808 SDValue Ops[] = { Load, TF };
809 return DAG.getMergeValues(Ops, 2);
810 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000811 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000812 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000813 "Unaligned load of unsupported type.");
814
Dale Johannesen8155d642008-02-27 22:36:00 +0000815 // Compute the new VT that is half the size of the old one. This is an
816 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817 unsigned NumBits = LoadedVT.getSizeInBits();
818 MVT NewLoadedVT;
819 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000820 NumBits >>= 1;
821
822 unsigned Alignment = LD->getAlignment();
823 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000824 ISD::LoadExtType HiExtType = LD->getExtensionType();
825
826 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
827 if (HiExtType == ISD::NON_EXTLOAD)
828 HiExtType = ISD::ZEXTLOAD;
829
830 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000831 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000832 if (TLI.isLittleEndian()) {
833 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
834 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
835 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
836 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
837 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
838 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000839 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000840 } else {
841 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
842 NewLoadedVT,LD->isVolatile(), Alignment);
843 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
844 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
845 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
846 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000847 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000848 }
849
850 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000851 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
852 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000853 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
854
Dan Gohman475871a2008-07-27 21:46:04 +0000855 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000856 Hi.getValue(1));
857
Dan Gohman475871a2008-07-27 21:46:04 +0000858 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000859 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000860}
Evan Cheng912095b2007-01-04 21:56:39 +0000861
Dan Gohman82669522007-10-11 23:57:53 +0000862/// UnrollVectorOp - We know that the given vector has a legal type, however
863/// the operation it performs is not legal and is an operation that we have
864/// no way of lowering. "Unroll" the vector, splitting out the scalars and
865/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000866SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000867 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000868 assert(isTypeLegal(VT) &&
869 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000870 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000871 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872 unsigned NE = VT.getVectorNumElements();
873 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000874
Dan Gohman475871a2008-07-27 21:46:04 +0000875 SmallVector<SDValue, 8> Scalars;
876 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000877 for (unsigned i = 0; i != NE; ++i) {
878 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000879 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000880 MVT OperandVT = Operand.getValueType();
881 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000882 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000883 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000884 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
885 OperandEltVT,
886 Operand,
887 DAG.getConstant(i, MVT::i32));
888 } else {
889 // A scalar operand; just use it as is.
890 Operands[j] = Operand;
891 }
892 }
Mon P Wange9f10152008-12-09 05:46:39 +0000893
894 switch (Op.getOpcode()) {
895 default:
896 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
897 &Operands[0], Operands.size()));
898 break;
899 case ISD::SHL:
900 case ISD::SRA:
901 case ISD::SRL:
902 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
903 LegalizeShiftAmount(Operands[1])));
904 break;
905 }
Dan Gohman82669522007-10-11 23:57:53 +0000906 }
907
908 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
909}
910
Duncan Sands007f9842008-01-10 10:28:30 +0000911/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000912static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000913 RTLIB::Libcall Call_F32,
914 RTLIB::Libcall Call_F64,
915 RTLIB::Libcall Call_F80,
916 RTLIB::Libcall Call_PPCF128) {
917 return
918 VT == MVT::f32 ? Call_F32 :
919 VT == MVT::f64 ? Call_F64 :
920 VT == MVT::f80 ? Call_F80 :
921 VT == MVT::ppcf128 ? Call_PPCF128 :
922 RTLIB::UNKNOWN_LIBCALL;
923}
924
Nate Begeman68679912008-04-25 18:07:40 +0000925/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
926/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
927/// is necessary to spill the vector being inserted into to memory, perform
928/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000929SDValue SelectionDAGLegalize::
930PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
931 SDValue Tmp1 = Vec;
932 SDValue Tmp2 = Val;
933 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000934
935 // If the target doesn't support this, we have to spill the input vector
936 // to a temporary stack slot, update the element, then reload it. This is
937 // badness. We could also load the value into a vector register (either
938 // with a "move to register" or "extload into register" instruction, then
939 // permute it into place, if the idx is a constant and if the idx is
940 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000941 MVT VT = Tmp1.getValueType();
942 MVT EltVT = VT.getVectorElementType();
943 MVT IdxVT = Tmp3.getValueType();
944 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000945 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000946
Gabor Greifba36cb52008-08-28 21:40:38 +0000947 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000948
949 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000950 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000951 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000952
953 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000954 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000955 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
956 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000957 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000958 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000959 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000960 // Store the scalar value.
961 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000962 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000963 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000964 return DAG.getLoad(VT, Ch, StackPtr,
965 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000966}
967
Mon P Wange9f10152008-12-09 05:46:39 +0000968SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
969 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
970 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
971
972 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
973 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
974
975 return ShiftAmt;
976}
977
978
Dan Gohmana3466152007-07-13 20:14:11 +0000979/// LegalizeOp - We know that the specified value has a legal type, and
980/// that its operands are legal. Now ensure that the operation itself
981/// is legal, recursively ensuring that the operands' operations remain
982/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000983SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000984 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
985 return Op;
986
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000987 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000988 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000989 SDNode *Node = Op.getNode();
Chris Lattnere3304a32005-01-08 20:35:13 +0000990
Chris Lattner3e928bb2005-01-07 07:47:09 +0000991 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000992 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000993 if (Node->getNumValues() > 1) {
994 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000995 if (getTypeAction(Node->getValueType(i)) != Legal) {
996 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000997 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000998 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000999 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +00001000 }
1001 }
1002
Chris Lattner45982da2005-05-12 16:53:42 +00001003 // Note that LegalizeOp may be reentered even from single-use nodes, which
1004 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00001005 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +00001006 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001007
Dan Gohman475871a2008-07-27 21:46:04 +00001008 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
1009 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +00001010 bool isCustom = false;
1011
Chris Lattner3e928bb2005-01-07 07:47:09 +00001012 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +00001013 case ISD::FrameIndex:
1014 case ISD::EntryToken:
1015 case ISD::Register:
1016 case ISD::BasicBlock:
1017 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +00001018 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +00001019 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +00001020 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +00001021 case ISD::TargetConstantPool:
1022 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001023 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001024 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +00001025 case ISD::VALUETYPE:
1026 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +00001027 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +00001028 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +00001029 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +00001030 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00001031 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +00001032 "This must be legal!");
1033 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001034 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001035 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1036 // If this is a target node, legalize it by legalizing the operands then
1037 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +00001038 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001039 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001040 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001041
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001042 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001043
1044 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1045 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001046 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001047 }
1048 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001049#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00001050 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001051#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00001052 assert(0 && "Do not know how to legalize this operator!");
1053 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +00001054 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001055 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001056 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001057 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +00001058 case ISD::ConstantPool:
1059 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001060 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1061 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +00001062 case TargetLowering::Custom:
1063 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001064 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +00001065 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001066 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001067 break;
1068 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001069 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +00001070 case ISD::FRAMEADDR:
1071 case ISD::RETURNADDR:
1072 // The only option for these nodes is to custom lower them. If the target
1073 // does not custom lower them, then return zero.
1074 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001075 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +00001076 Result = Tmp1;
1077 else
1078 Result = DAG.getConstant(0, TLI.getPointerTy());
1079 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001080 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001081 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001082 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1083 default: assert(0 && "This action is not supported yet!");
1084 case TargetLowering::Custom:
1085 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001086 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001087 // Fall Thru
1088 case TargetLowering::Legal:
1089 Result = DAG.getConstant(0, VT);
1090 break;
1091 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001092 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001093 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001094 case ISD::EXCEPTIONADDR: {
1095 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001096 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001097 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1098 default: assert(0 && "This action is not supported yet!");
1099 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +00001100 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001101 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001102 }
1103 break;
1104 case TargetLowering::Custom:
1105 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001106 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001107 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001108 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001109 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001110 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001111 break;
1112 }
1113 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001114 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001115 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001116
Gabor Greifba36cb52008-08-28 21:40:38 +00001117 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001118 "Cannot return more than two values!");
1119
1120 // Since we produced two values, make sure to remember that we
1121 // legalized both of them.
1122 Tmp1 = LegalizeOp(Result);
1123 Tmp2 = LegalizeOp(Result.getValue(1));
1124 AddLegalizedOperand(Op.getValue(0), Tmp1);
1125 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001126 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001127 case ISD::EHSELECTION: {
1128 Tmp1 = LegalizeOp(Node->getOperand(0));
1129 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001130 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001131 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1132 default: assert(0 && "This action is not supported yet!");
1133 case TargetLowering::Expand: {
1134 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001135 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001136 }
1137 break;
1138 case TargetLowering::Custom:
1139 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001140 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001141 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001142 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001143 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001144 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001145 break;
1146 }
1147 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001148 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001149 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001150
Gabor Greifba36cb52008-08-28 21:40:38 +00001151 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001152 "Cannot return more than two values!");
1153
1154 // Since we produced two values, make sure to remember that we
1155 // legalized both of them.
1156 Tmp1 = LegalizeOp(Result);
1157 Tmp2 = LegalizeOp(Result.getValue(1));
1158 AddLegalizedOperand(Op.getValue(0), Tmp1);
1159 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001160 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001161 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001162 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001163 // The only "good" option for this node is to custom lower it.
1164 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1165 default: assert(0 && "This action is not supported at all!");
1166 case TargetLowering::Custom:
1167 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001168 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001169 // Fall Thru
1170 case TargetLowering::Legal:
1171 // Target does not know, how to lower this, lower to noop
1172 Result = LegalizeOp(Node->getOperand(0));
1173 break;
1174 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001175 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001176 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001177 case ISD::AssertSext:
1178 case ISD::AssertZext:
1179 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001180 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001181 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001182 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001183 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001184 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001185 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001186 case ISD::CopyFromReg:
1187 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001188 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001189 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001190 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001191 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001192 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001193 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001194 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001195 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1196 } else {
1197 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1198 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001199 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1200 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001201 // Since CopyFromReg produces two values, make sure to remember that we
1202 // legalized both of them.
1203 AddLegalizedOperand(Op.getValue(0), Result);
1204 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001205 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001206 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001207 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001208 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001209 default: assert(0 && "This action is not supported yet!");
1210 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001211 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001212 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001213 else if (VT.isFloatingPoint())
1214 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001215 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001216 else
1217 assert(0 && "Unknown value type!");
1218 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001219 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001220 break;
1221 }
1222 break;
1223 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001224
Chris Lattner48b61a72006-03-28 00:40:33 +00001225 case ISD::INTRINSIC_W_CHAIN:
1226 case ISD::INTRINSIC_WO_CHAIN:
1227 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001228 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001229 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1230 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001231 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001232
1233 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001234 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001235 TargetLowering::Custom) {
1236 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001237 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001238 }
1239
Gabor Greifba36cb52008-08-28 21:40:38 +00001240 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001241
1242 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001243 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001244 "Cannot return more than two values!");
1245
1246 // Since loads produce two values, make sure to remember that we
1247 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001248 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1249 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001250 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001251 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001252
Dan Gohman7f460202008-06-30 20:59:49 +00001253 case ISD::DBG_STOPPOINT:
1254 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001255 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1256
Dan Gohman7f460202008-06-30 20:59:49 +00001257 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001258 case TargetLowering::Promote:
1259 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001260 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001261 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001262 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohman44066042008-07-01 00:05:16 +00001263 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001264
Dan Gohman7f460202008-06-30 20:59:49 +00001265 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001266 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman7f460202008-06-30 20:59:49 +00001267 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1268 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001269
Dan Gohman7f460202008-06-30 20:59:49 +00001270 unsigned Line = DSP->getLine();
1271 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001272
1273 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001274 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001275 DAG.getConstant(Col, MVT::i32),
1276 DAG.getConstant(SrcFile, MVT::i32) };
1277 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001278 } else {
Evan Chenga647c922008-02-01 02:05:57 +00001279 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001280 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001281 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001282 } else {
1283 Result = Tmp1; // chain
1284 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001285 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001286 }
Evan Cheng71e86852008-07-08 20:06:39 +00001287 case TargetLowering::Legal: {
1288 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1289 if (Action == Legal && Tmp1 == Node->getOperand(0))
1290 break;
1291
Dan Gohman475871a2008-07-27 21:46:04 +00001292 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001293 Ops.push_back(Tmp1);
1294 if (Action == Legal) {
1295 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1296 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1297 } else {
1298 // Otherwise promote them.
1299 Ops.push_back(PromoteOp(Node->getOperand(1)));
1300 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001301 }
Evan Cheng71e86852008-07-08 20:06:39 +00001302 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1303 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1304 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001305 break;
1306 }
Evan Cheng71e86852008-07-08 20:06:39 +00001307 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001308 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001309
1310 case ISD::DECLARE:
1311 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1312 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1313 default: assert(0 && "This action is not supported yet!");
1314 case TargetLowering::Legal:
1315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1316 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1317 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1318 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1319 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001320 case TargetLowering::Expand:
1321 Result = LegalizeOp(Node->getOperand(0));
1322 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001323 }
1324 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001325
1326 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001327 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001328 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001329 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001330 case TargetLowering::Legal: {
1331 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001332 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001333 if (Action == Legal && Tmp1 == Node->getOperand(0))
1334 break;
1335 if (Action == Legal) {
1336 Tmp2 = Node->getOperand(1);
1337 Tmp3 = Node->getOperand(2);
1338 Tmp4 = Node->getOperand(3);
1339 } else {
1340 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1341 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1342 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1343 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001344 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001345 break;
1346 }
Evan Cheng71e86852008-07-08 20:06:39 +00001347 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001348 break;
1349
Dan Gohman44066042008-07-01 00:05:16 +00001350 case ISD::DBG_LABEL:
1351 case ISD::EH_LABEL:
1352 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1353 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001354 default: assert(0 && "This action is not supported yet!");
1355 case TargetLowering::Legal:
1356 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001357 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001358 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001359 case TargetLowering::Expand:
1360 Result = LegalizeOp(Node->getOperand(0));
1361 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001362 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001363 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001364
Evan Cheng27b7db52008-03-08 00:58:38 +00001365 case ISD::PREFETCH:
1366 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1367 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1368 default: assert(0 && "This action is not supported yet!");
1369 case TargetLowering::Legal:
1370 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1371 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1372 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1373 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1374 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1375 break;
1376 case TargetLowering::Expand:
1377 // It's a noop.
1378 Result = LegalizeOp(Node->getOperand(0));
1379 break;
1380 }
1381 break;
1382
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001383 case ISD::MEMBARRIER: {
1384 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001385 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1386 default: assert(0 && "This action is not supported yet!");
1387 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001388 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001389 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001390 for (int x = 1; x < 6; ++x) {
1391 Ops[x] = Node->getOperand(x);
1392 if (!isTypeLegal(Ops[x].getValueType()))
1393 Ops[x] = PromoteOp(Ops[x]);
1394 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001395 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1396 break;
1397 }
1398 case TargetLowering::Expand:
1399 //There is no libgcc call for this op
1400 Result = Node->getOperand(0); // Noop
1401 break;
1402 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001403 break;
1404 }
1405
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001406 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001407 unsigned int num_operands = 4;
1408 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001410 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001411 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001412 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1413
1414 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1415 default: assert(0 && "This action is not supported yet!");
1416 case TargetLowering::Custom:
1417 Result = TLI.LowerOperation(Result, DAG);
1418 break;
1419 case TargetLowering::Legal:
1420 break;
1421 }
Dan Gohman475871a2008-07-27 21:46:04 +00001422 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1423 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001424 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001425 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00001426 case ISD::ATOMIC_LOAD_ADD:
1427 case ISD::ATOMIC_LOAD_SUB:
1428 case ISD::ATOMIC_LOAD_AND:
1429 case ISD::ATOMIC_LOAD_OR:
1430 case ISD::ATOMIC_LOAD_XOR:
1431 case ISD::ATOMIC_LOAD_NAND:
1432 case ISD::ATOMIC_LOAD_MIN:
1433 case ISD::ATOMIC_LOAD_MAX:
1434 case ISD::ATOMIC_LOAD_UMIN:
1435 case ISD::ATOMIC_LOAD_UMAX:
1436 case ISD::ATOMIC_SWAP: {
Mon P Wang63307c32008-05-05 19:05:59 +00001437 unsigned int num_operands = 3;
1438 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001439 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001440 for (unsigned int x = 0; x < num_operands; ++x)
1441 Ops[x] = LegalizeOp(Node->getOperand(x));
1442 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001443
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001444 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001445 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001446 case TargetLowering::Custom:
1447 Result = TLI.LowerOperation(Result, DAG);
1448 break;
1449 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001450 break;
1451 }
Dan Gohman475871a2008-07-27 21:46:04 +00001452 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1453 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001454 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001455 }
Scott Michelc1513d22007-08-08 23:23:31 +00001456 case ISD::Constant: {
1457 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1458 unsigned opAction =
1459 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1460
Chris Lattner3e928bb2005-01-07 07:47:09 +00001461 // We know we don't need to expand constants here, constants only have one
1462 // value and we check that it is fine above.
1463
Scott Michelc1513d22007-08-08 23:23:31 +00001464 if (opAction == TargetLowering::Custom) {
1465 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001466 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001467 Result = Tmp1;
1468 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001469 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001470 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001471 case ISD::ConstantFP: {
1472 // Spill FP immediates to the constant pool if the target cannot directly
1473 // codegen them. Targets often have some immediate values that can be
1474 // efficiently generated into an FP register without a load. We explicitly
1475 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001476 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1477
Chris Lattner3181a772006-01-29 06:26:56 +00001478 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1479 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001480 case TargetLowering::Legal:
1481 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001482 case TargetLowering::Custom:
1483 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001484 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001485 Result = Tmp3;
1486 break;
1487 }
1488 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001489 case TargetLowering::Expand: {
1490 // Check to see if this FP immediate is already legal.
1491 bool isLegal = false;
1492 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1493 E = TLI.legal_fpimm_end(); I != E; ++I) {
1494 if (CFP->isExactlyValue(*I)) {
1495 isLegal = true;
1496 break;
1497 }
1498 }
1499 // If this is a legal constant, turn it into a TargetConstantFP node.
1500 if (isLegal)
1501 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001502 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001503 }
Nate Begemane1795842008-02-14 08:57:00 +00001504 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001505 break;
1506 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001507 case ISD::TokenFactor:
1508 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001509 Tmp1 = LegalizeOp(Node->getOperand(0));
1510 Tmp2 = LegalizeOp(Node->getOperand(1));
1511 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1512 } else if (Node->getNumOperands() == 3) {
1513 Tmp1 = LegalizeOp(Node->getOperand(0));
1514 Tmp2 = LegalizeOp(Node->getOperand(1));
1515 Tmp3 = LegalizeOp(Node->getOperand(2));
1516 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001517 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001518 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001519 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001520 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1521 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001522 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001523 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001524 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001525
1526 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001527 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001528 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001529 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001530 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001531 // A call within a calling sequence must be legalized to something
1532 // other than the normal CALLSEQ_END. Violating this gets Legalize
1533 // into an infinite loop.
1534 assert ((!IsLegalizingCall ||
1535 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001536 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001537 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001538
1539 // The number of incoming and outgoing values should match; unless the final
1540 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001541 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1542 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1543 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001544 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001545 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001546
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001547 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001548 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001549 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1550 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001551 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001552 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001553 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001554 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001555 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001556 }
1557 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001558 case ISD::EXTRACT_SUBREG: {
1559 Tmp1 = LegalizeOp(Node->getOperand(0));
1560 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1561 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001562 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001563 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1564 }
1565 break;
1566 case ISD::INSERT_SUBREG: {
1567 Tmp1 = LegalizeOp(Node->getOperand(0));
1568 Tmp2 = LegalizeOp(Node->getOperand(1));
1569 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1570 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001571 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001572 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1573 }
1574 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001575 case ISD::BUILD_VECTOR:
1576 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001577 default: assert(0 && "This action is not supported yet!");
1578 case TargetLowering::Custom:
1579 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001580 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001581 Result = Tmp3;
1582 break;
1583 }
1584 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001585 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001586 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001587 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001588 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001589 break;
1590 case ISD::INSERT_VECTOR_ELT:
1591 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001592 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001593
1594 // The type of the value to insert may not be legal, even though the vector
1595 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1596 // here.
1597 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1598 default: assert(0 && "Cannot expand insert element operand");
1599 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1600 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001601 case Expand:
1602 // FIXME: An alternative would be to check to see if the target is not
1603 // going to custom lower this operation, we could bitcast to half elt
1604 // width and perform two inserts at that width, if that is legal.
1605 Tmp2 = Node->getOperand(1);
1606 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001607 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001608 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1609
1610 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1611 Node->getValueType(0))) {
1612 default: assert(0 && "This action is not supported yet!");
1613 case TargetLowering::Legal:
1614 break;
1615 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001616 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001617 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001618 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001619 break;
1620 }
1621 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001622 case TargetLowering::Promote:
1623 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001624 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001625 // If the insert index is a constant, codegen this as a scalar_to_vector,
1626 // then a shuffle that inserts it into the right position in the vector.
1627 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001628 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1629 // match the element type of the vector being created.
1630 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001631 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001632 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001633 Tmp1.getValueType(), Tmp2);
1634
Duncan Sands83ec4b62008-06-06 12:08:01 +00001635 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1636 MVT ShufMaskVT =
1637 MVT::getIntVectorWithNumElements(NumElts);
1638 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001639
1640 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1641 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1642 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001643 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001644 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001645 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001646 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1647 else
1648 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1649 }
Dan Gohman475871a2008-07-27 21:46:04 +00001650 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001651 &ShufOps[0], ShufOps.size());
1652
1653 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1654 Tmp1, ScVec, ShufMask);
1655 Result = LegalizeOp(Result);
1656 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001657 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001658 }
Nate Begeman68679912008-04-25 18:07:40 +00001659 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001660 break;
1661 }
1662 }
1663 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001664 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001665 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1666 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1667 break;
1668 }
1669
Chris Lattnerce872152006-03-19 06:31:19 +00001670 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1671 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1672 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1673 Node->getValueType(0))) {
1674 default: assert(0 && "This action is not supported yet!");
1675 case TargetLowering::Legal:
1676 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001677 case TargetLowering::Custom:
1678 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001679 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001680 Result = Tmp3;
1681 break;
1682 }
1683 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001684 case TargetLowering::Expand:
1685 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001686 break;
1687 }
Chris Lattnerce872152006-03-19 06:31:19 +00001688 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001689 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001690 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1691 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1692 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1693
1694 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001695 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1696 default: assert(0 && "Unknown operation action!");
1697 case TargetLowering::Legal:
1698 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1699 "vector shuffle should not be created if not legal!");
1700 break;
1701 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001702 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001703 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001704 Result = Tmp3;
1705 break;
1706 }
1707 // FALLTHROUGH
1708 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001709 MVT VT = Node->getValueType(0);
1710 MVT EltVT = VT.getVectorElementType();
1711 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001712 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001713 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001714 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001715 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001716 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001717 if (Arg.getOpcode() == ISD::UNDEF) {
1718 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1719 } else {
1720 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001721 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001722 if (Idx < NumElems)
1723 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1724 DAG.getConstant(Idx, PtrVT)));
1725 else
1726 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1727 DAG.getConstant(Idx - NumElems, PtrVT)));
1728 }
1729 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001730 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001731 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001732 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001733 case TargetLowering::Promote: {
1734 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001735 MVT OVT = Node->getValueType(0);
1736 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001737
1738 // Cast the two input vectors.
1739 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1740 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1741
1742 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001743 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001744 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001745 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1746 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1747 break;
1748 }
Chris Lattner87100e02006-03-20 01:52:29 +00001749 }
1750 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001751
1752 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001753 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001754 Tmp2 = LegalizeOp(Node->getOperand(1));
1755 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001756 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001757 break;
1758
Dan Gohman7f321562007-06-25 16:23:39 +00001759 case ISD::EXTRACT_SUBVECTOR:
1760 Tmp1 = Node->getOperand(0);
1761 Tmp2 = LegalizeOp(Node->getOperand(1));
1762 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1763 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001764 break;
1765
Mon P Wang0c397192008-10-30 08:01:45 +00001766 case ISD::CONCAT_VECTORS: {
1767 // Use extract/insert/build vector for now. We might try to be
1768 // more clever later.
1769 MVT PtrVT = TLI.getPointerTy();
1770 SmallVector<SDValue, 8> Ops;
1771 unsigned NumOperands = Node->getNumOperands();
1772 for (unsigned i=0; i < NumOperands; ++i) {
1773 SDValue SubOp = Node->getOperand(i);
1774 MVT VVT = SubOp.getNode()->getValueType(0);
1775 MVT EltVT = VVT.getVectorElementType();
1776 unsigned NumSubElem = VVT.getVectorNumElements();
1777 for (unsigned j=0; j < NumSubElem; ++j) {
1778 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1779 DAG.getConstant(j, PtrVT)));
1780 }
1781 }
1782 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1783 &Ops[0], Ops.size()));
1784 }
1785
Chris Lattner6831a812006-02-13 09:18:02 +00001786 case ISD::CALLSEQ_START: {
1787 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1788
1789 // Recursively Legalize all of the inputs of the call end that do not lead
1790 // to this call start. This ensures that any libcalls that need be inserted
1791 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001792 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001793 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001794 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001795 NodesLeadingTo);
1796 }
Chris Lattner6831a812006-02-13 09:18:02 +00001797
1798 // Now that we legalized all of the inputs (which may have inserted
1799 // libcalls) create the new CALLSEQ_START node.
1800 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1801
1802 // Merge in the last call, to ensure that this call start after the last
1803 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001804 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001805 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1806 Tmp1 = LegalizeOp(Tmp1);
1807 }
Chris Lattner6831a812006-02-13 09:18:02 +00001808
1809 // Do not try to legalize the target-specific arguments (#1+).
1810 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001811 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001812 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001813 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001814 }
1815
1816 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001817 AddLegalizedOperand(Op.getValue(0), Result);
1818 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1819 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1820
Chris Lattner6831a812006-02-13 09:18:02 +00001821 // Now that the callseq_start and all of the non-call nodes above this call
1822 // sequence have been legalized, legalize the call itself. During this
1823 // process, no libcalls can/will be inserted, guaranteeing that no calls
1824 // can overlap.
1825 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001826 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001827 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001828 IsLegalizingCall = true;
1829
1830 // Legalize the call, starting from the CALLSEQ_END.
1831 LegalizeOp(LastCALLSEQ_END);
1832 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1833 return Result;
1834 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001835 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001836 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1837 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001838 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001839 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1840 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001841 assert(I != LegalizedNodes.end() &&
1842 "Legalizing the call start should have legalized this node!");
1843 return I->second;
1844 }
1845
1846 // Otherwise, the call start has been legalized and everything is going
1847 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001848 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001849 // Do not try to legalize the target-specific arguments (#1+), except for
1850 // an optional flag input.
1851 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1852 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001853 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001854 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001855 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001856 }
1857 } else {
1858 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1859 if (Tmp1 != Node->getOperand(0) ||
1860 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001861 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001862 Ops[0] = Tmp1;
1863 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001864 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001865 }
Chris Lattner6a542892006-01-24 05:48:21 +00001866 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001867 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001868 // This finishes up call legalization.
1869 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001870
1871 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001872 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001873 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001874 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001875 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001876 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001877 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001878 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1879 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1880 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001881 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001882
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001883 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001884 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001885 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001886 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001887 case TargetLowering::Expand: {
1888 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1889 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1890 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001891 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001892
1893 // Chain the dynamic stack allocation so that it doesn't modify the stack
1894 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001895 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001896
Dan Gohman475871a2008-07-27 21:46:04 +00001897 SDValue Size = Tmp2.getOperand(1);
1898 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001899 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001900 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001901 unsigned StackAlign =
1902 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1903 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001904 SP = DAG.getNode(ISD::AND, VT, SP,
1905 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001906 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001907 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1908
Chris Lattnere563bbc2008-10-11 22:08:30 +00001909 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1910 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001911
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001912 Tmp1 = LegalizeOp(Tmp1);
1913 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001914 break;
1915 }
1916 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001917 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001918 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001919 Tmp1 = LegalizeOp(Tmp3);
1920 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001921 }
Chris Lattner903d2782006-01-15 08:54:32 +00001922 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001923 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001924 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001925 }
Chris Lattner903d2782006-01-15 08:54:32 +00001926 // Since this op produce two values, make sure to remember that we
1927 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001928 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1929 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001930 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001931 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001932 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001933 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001934 bool Changed = false;
1935 // Legalize all of the operands of the inline asm, in case they are nodes
1936 // that need to be expanded or something. Note we skip the asm string and
1937 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001938 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001939 Changed = Op != Ops[0];
1940 Ops[0] = Op;
1941
1942 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1943 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001944 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001945 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001946 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001947 if (Op != Ops[i]) {
1948 Changed = true;
1949 Ops[i] = Op;
1950 }
1951 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001952 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001953
1954 if (HasInFlag) {
1955 Op = LegalizeOp(Ops.back());
1956 Changed |= Op != Ops.back();
1957 Ops.back() = Op;
1958 }
1959
1960 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001961 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001962
1963 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001964 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1965 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001966 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001967 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001968 case ISD::BR:
1969 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001970 // Ensure that libcalls are emitted before a branch.
1971 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1972 Tmp1 = LegalizeOp(Tmp1);
1973 LastCALLSEQ_END = DAG.getEntryNode();
1974
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001975 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001976 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001977 case ISD::BRIND:
1978 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1979 // Ensure that libcalls are emitted before a branch.
1980 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1981 Tmp1 = LegalizeOp(Tmp1);
1982 LastCALLSEQ_END = DAG.getEntryNode();
1983
1984 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1985 default: assert(0 && "Indirect target must be legal type (pointer)!");
1986 case Legal:
1987 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1988 break;
1989 }
1990 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1991 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001992 case ISD::BR_JT:
1993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1994 // Ensure that libcalls are emitted before a branch.
1995 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1996 Tmp1 = LegalizeOp(Tmp1);
1997 LastCALLSEQ_END = DAG.getEntryNode();
1998
1999 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2000 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2001
2002 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2003 default: assert(0 && "This action is not supported yet!");
2004 case TargetLowering::Legal: break;
2005 case TargetLowering::Custom:
2006 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002007 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002008 break;
2009 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002010 SDValue Chain = Result.getOperand(0);
2011 SDValue Table = Result.getOperand(1);
2012 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002013
Duncan Sands83ec4b62008-06-06 12:08:01 +00002014 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002015 MachineFunction &MF = DAG.getMachineFunction();
2016 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00002017 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00002018 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002019
Duncan Sands712f7b32008-12-12 08:13:38 +00002020 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2021 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2022 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00002023 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002024 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00002025 // For PIC, the sequence is:
2026 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00002027 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Chengcc415862007-11-09 01:32:10 +00002028 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2029 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00002030 }
Evan Chengcc415862007-11-09 01:32:10 +00002031 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002032 }
2033 }
2034 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002035 case ISD::BRCOND:
2036 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002037 // Ensure that libcalls are emitted before a return.
2038 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2039 Tmp1 = LegalizeOp(Tmp1);
2040 LastCALLSEQ_END = DAG.getEntryNode();
2041
Chris Lattner47e92232005-01-18 19:27:06 +00002042 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2043 case Expand: assert(0 && "It's impossible to expand bools");
2044 case Legal:
2045 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2046 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002047 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002048 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00002049
2050 // The top bits of the promoted condition are not necessarily zero, ensure
2051 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002052 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002053 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002054 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00002055 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002056 break;
2057 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002058 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002059
2060 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002061 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00002062
2063 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2064 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002065 case TargetLowering::Legal: break;
2066 case TargetLowering::Custom:
2067 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002068 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002069 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002070 case TargetLowering::Expand:
2071 // Expand brcond's setcc into its constituent parts and create a BR_CC
2072 // Node.
2073 if (Tmp2.getOpcode() == ISD::SETCC) {
2074 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2075 Tmp2.getOperand(0), Tmp2.getOperand(1),
2076 Node->getOperand(2));
2077 } else {
2078 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2079 DAG.getCondCode(ISD::SETNE), Tmp2,
2080 DAG.getConstant(0, Tmp2.getValueType()),
2081 Node->getOperand(2));
2082 }
2083 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002084 }
2085 break;
2086 case ISD::BR_CC:
2087 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002088 // Ensure that libcalls are emitted before a branch.
2089 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2090 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002091 Tmp2 = Node->getOperand(2); // LHS
2092 Tmp3 = Node->getOperand(3); // RHS
2093 Tmp4 = Node->getOperand(1); // CC
2094
Duncan Sands5480c042009-01-01 15:52:00 +00002095 LegalizeSetCC(TLI.getSetCCResultType(Tmp2.getValueType()), Tmp2, Tmp3,Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002096 LastCALLSEQ_END = DAG.getEntryNode();
2097
Evan Cheng7f042682008-10-15 02:05:31 +00002098 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002099 // the LHS is a legal SETCC itself. In this case, we need to compare
2100 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002101 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002102 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2103 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002104 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002105
2106 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2107 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002108
Chris Lattner181b7a32005-12-17 23:46:46 +00002109 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2110 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002111 case TargetLowering::Legal: break;
2112 case TargetLowering::Custom:
2113 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002114 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002115 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002116 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002117 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002118 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002119 LoadSDNode *LD = cast<LoadSDNode>(Node);
2120 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2121 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002122
Evan Cheng466685d2006-10-09 20:57:25 +00002123 ISD::LoadExtType ExtType = LD->getExtensionType();
2124 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002125 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002126 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2127 Tmp3 = Result.getValue(0);
2128 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002129
Evan Cheng466685d2006-10-09 20:57:25 +00002130 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2131 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002132 case TargetLowering::Legal:
2133 // If this is an unaligned load and the target doesn't support it,
2134 // expand it.
2135 if (!TLI.allowsUnalignedMemoryAccesses()) {
2136 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002137 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002138 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002139 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002140 TLI);
2141 Tmp3 = Result.getOperand(0);
2142 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002143 Tmp3 = LegalizeOp(Tmp3);
2144 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002145 }
2146 }
2147 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002148 case TargetLowering::Custom:
2149 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002150 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002151 Tmp3 = LegalizeOp(Tmp1);
2152 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002153 }
Evan Cheng466685d2006-10-09 20:57:25 +00002154 break;
2155 case TargetLowering::Promote: {
2156 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002157 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002158 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002159 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002160
2161 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002162 LD->getSrcValueOffset(),
2163 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002164 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2165 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002166 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002167 }
Evan Cheng466685d2006-10-09 20:57:25 +00002168 }
2169 // Since loads produce two values, make sure to remember that we
2170 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002171 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2172 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002173 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002174 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002175 MVT SrcVT = LD->getMemoryVT();
2176 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002177 int SVOffset = LD->getSrcValueOffset();
2178 unsigned Alignment = LD->getAlignment();
2179 bool isVolatile = LD->isVolatile();
2180
Duncan Sands83ec4b62008-06-06 12:08:01 +00002181 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002182 // Some targets pretend to have an i1 loading operation, and actually
2183 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2184 // bits are guaranteed to be zero; it helps the optimizers understand
2185 // that these bits are zero. It is also useful for EXTLOAD, since it
2186 // tells the optimizers that those bits are undefined. It would be
2187 // nice to have an effective generic way of getting these benefits...
2188 // Until such a way is found, don't insist on promoting i1 here.
2189 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002190 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002191 // Promote to a byte-sized load if not loading an integral number of
2192 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002193 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2194 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002195 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002196
2197 // The extra bits are guaranteed to be zero, since we stored them that
2198 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2199
2200 ISD::LoadExtType NewExtType =
2201 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2202
2203 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2204 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2205 NVT, isVolatile, Alignment);
2206
2207 Ch = Result.getValue(1); // The chain.
2208
2209 if (ExtType == ISD::SEXTLOAD)
2210 // Having the top bits zero doesn't help when sign extending.
2211 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2212 Result, DAG.getValueType(SrcVT));
2213 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2214 // All the top bits are guaranteed to be zero - inform the optimizers.
2215 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2216 DAG.getValueType(SrcVT));
2217
2218 Tmp1 = LegalizeOp(Result);
2219 Tmp2 = LegalizeOp(Ch);
2220 } else if (SrcWidth & (SrcWidth - 1)) {
2221 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002222 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002223 "Unsupported extload!");
2224 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2225 assert(RoundWidth < SrcWidth);
2226 unsigned ExtraWidth = SrcWidth - RoundWidth;
2227 assert(ExtraWidth < RoundWidth);
2228 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2229 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002230 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2231 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002232 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002233 unsigned IncrementSize;
2234
2235 if (TLI.isLittleEndian()) {
2236 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2237 // Load the bottom RoundWidth bits.
2238 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2239 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2240 Alignment);
2241
2242 // Load the remaining ExtraWidth bits.
2243 IncrementSize = RoundWidth / 8;
2244 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2245 DAG.getIntPtrConstant(IncrementSize));
2246 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2247 LD->getSrcValue(), SVOffset + IncrementSize,
2248 ExtraVT, isVolatile,
2249 MinAlign(Alignment, IncrementSize));
2250
2251 // Build a factor node to remember that this load is independent of the
2252 // other one.
2253 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2254 Hi.getValue(1));
2255
2256 // Move the top bits to the right place.
2257 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2258 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2259
2260 // Join the hi and lo parts.
2261 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002262 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002263 // Big endian - avoid unaligned loads.
2264 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2265 // Load the top RoundWidth bits.
2266 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2267 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2268 Alignment);
2269
2270 // Load the remaining ExtraWidth bits.
2271 IncrementSize = RoundWidth / 8;
2272 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2273 DAG.getIntPtrConstant(IncrementSize));
2274 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2275 LD->getSrcValue(), SVOffset + IncrementSize,
2276 ExtraVT, isVolatile,
2277 MinAlign(Alignment, IncrementSize));
2278
2279 // Build a factor node to remember that this load is independent of the
2280 // other one.
2281 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2282 Hi.getValue(1));
2283
2284 // Move the top bits to the right place.
2285 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2286 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2287
2288 // Join the hi and lo parts.
2289 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2290 }
2291
2292 Tmp1 = LegalizeOp(Result);
2293 Tmp2 = LegalizeOp(Ch);
2294 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002295 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002296 default: assert(0 && "This action is not supported yet!");
2297 case TargetLowering::Custom:
2298 isCustom = true;
2299 // FALLTHROUGH
2300 case TargetLowering::Legal:
2301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2302 Tmp1 = Result.getValue(0);
2303 Tmp2 = Result.getValue(1);
2304
2305 if (isCustom) {
2306 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002307 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002308 Tmp1 = LegalizeOp(Tmp3);
2309 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2310 }
2311 } else {
2312 // If this is an unaligned load and the target doesn't support it,
2313 // expand it.
2314 if (!TLI.allowsUnalignedMemoryAccesses()) {
2315 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002316 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002317 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002318 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002319 TLI);
2320 Tmp1 = Result.getOperand(0);
2321 Tmp2 = Result.getOperand(1);
2322 Tmp1 = LegalizeOp(Tmp1);
2323 Tmp2 = LegalizeOp(Tmp2);
2324 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002325 }
2326 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002327 break;
2328 case TargetLowering::Expand:
2329 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2330 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002331 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002332 LD->getSrcValueOffset(),
2333 LD->isVolatile(), LD->getAlignment());
2334 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2335 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2336 Tmp2 = LegalizeOp(Load.getValue(1));
2337 break;
2338 }
2339 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2340 // Turn the unsupported load into an EXTLOAD followed by an explicit
2341 // zero/sign extend inreg.
2342 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2343 Tmp1, Tmp2, LD->getSrcValue(),
2344 LD->getSrcValueOffset(), SrcVT,
2345 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002346 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002347 if (ExtType == ISD::SEXTLOAD)
2348 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2349 Result, DAG.getValueType(SrcVT));
2350 else
2351 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2352 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2353 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002354 break;
2355 }
Evan Cheng466685d2006-10-09 20:57:25 +00002356 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002357
Evan Cheng466685d2006-10-09 20:57:25 +00002358 // Since loads produce two values, make sure to remember that we legalized
2359 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002360 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2361 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002362 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002363 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002364 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002365 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002366 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002367 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002368 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002369 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002370 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002371 // 1 -> Hi
2372 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002373 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002374 TLI.getShiftAmountTy()));
2375 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2376 } else {
2377 // 0 -> Lo
2378 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2379 Node->getOperand(0));
2380 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002381 break;
2382 case Expand:
2383 // Get both the low and high parts.
2384 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002385 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002386 Result = Tmp2; // 1 -> Hi
2387 else
2388 Result = Tmp1; // 0 -> Lo
2389 break;
2390 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002391 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002392 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002393
2394 case ISD::CopyToReg:
2395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002396
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002397 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002398 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002399 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002400 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002401 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002402 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002403 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002404 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002405 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002406 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002407 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2408 Tmp3);
2409 } else {
2410 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002411 }
2412
2413 // Since this produces two values, make sure to remember that we legalized
2414 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002415 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2416 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002417 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002418 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002419 break;
2420
2421 case ISD::RET:
2422 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002423
2424 // Ensure that libcalls are emitted before a return.
2425 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2426 Tmp1 = LegalizeOp(Tmp1);
2427 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002428
Chris Lattner3e928bb2005-01-07 07:47:09 +00002429 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002430 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002431 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002432 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002433 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002434 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002435 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002436 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002437 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002438 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002439 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002440 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002441
2442 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002443 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002444 std::swap(Lo, Hi);
2445
Gabor Greifba36cb52008-08-28 21:40:38 +00002446 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002447 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2448 else
2449 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002450 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002451 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002452 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002453 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002454 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2455 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002456
Dan Gohman7f321562007-06-25 16:23:39 +00002457 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002458 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002459 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002460 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002461 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002462 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002463 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002464 } else if (NumElems == 1) {
2465 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002466 Tmp2 = ScalarizeVectorOp(Tmp2);
2467 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002468 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002469
2470 // FIXME: Returns of gcc generic vectors smaller than a legal type
2471 // should be returned in integer registers!
2472
Chris Lattnerf87324e2006-04-11 01:31:51 +00002473 // The scalarized value type may not be legal, e.g. it might require
2474 // promotion or expansion. Relegalize the return.
2475 Result = LegalizeOp(Result);
2476 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002477 // FIXME: Returns of gcc generic vectors larger than a legal vector
2478 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002479 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002480 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002481 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002482 Result = LegalizeOp(Result);
2483 }
2484 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002485 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002486 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002487 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002488 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002489 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002490 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002491 }
2492 break;
2493 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002494 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002495 break;
2496 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002497 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002498 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002499 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002500 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2501 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002502 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002503 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002504 break;
2505 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002506 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002507 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002508 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002509 ExpandOp(Node->getOperand(i), Lo, Hi);
2510 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002511 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002512 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002513 NewValues.push_back(Hi);
2514 NewValues.push_back(Node->getOperand(i+1));
2515 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002516 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002517 }
2518 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002519 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002520 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002521
2522 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002523 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002524 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002525 Result = DAG.getNode(ISD::RET, MVT::Other,
2526 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002527 break;
2528 }
2529 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002530
Chris Lattner6862dbc2006-01-29 21:02:23 +00002531 if (Result.getOpcode() == ISD::RET) {
2532 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2533 default: assert(0 && "This action is not supported yet!");
2534 case TargetLowering::Legal: break;
2535 case TargetLowering::Custom:
2536 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002537 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002538 break;
2539 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002540 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002541 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002542 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002543 StoreSDNode *ST = cast<StoreSDNode>(Node);
2544 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2545 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002546 int SVOffset = ST->getSrcValueOffset();
2547 unsigned Alignment = ST->getAlignment();
2548 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002549
Evan Cheng8b2794a2006-10-13 21:14:26 +00002550 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002551 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2552 // FIXME: We shouldn't do this for TargetConstantFP's.
2553 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2554 // to phase ordering between legalized code and the dag combiner. This
2555 // probably means that we need to integrate dag combiner and legalizer
2556 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002557 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002558 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002559 if (CFP->getValueType(0) == MVT::f32 &&
2560 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002561 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002562 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002563 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002564 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2565 SVOffset, isVolatile, Alignment);
2566 break;
2567 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002568 // If this target supports 64-bit registers, do a single 64-bit store.
2569 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002570 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002571 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002572 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2573 SVOffset, isVolatile, Alignment);
2574 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002575 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002576 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2577 // stores. If the target supports neither 32- nor 64-bits, this
2578 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002579 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002580 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2581 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002582 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002583
2584 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2585 SVOffset, isVolatile, Alignment);
2586 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002587 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002588 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002589 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002590
2591 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2592 break;
2593 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002594 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002595 }
2596
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002597 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002598 case Legal: {
2599 Tmp3 = LegalizeOp(ST->getValue());
2600 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2601 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002602
Duncan Sands83ec4b62008-06-06 12:08:01 +00002603 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002604 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2605 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002606 case TargetLowering::Legal:
2607 // If this is an unaligned store and the target doesn't support it,
2608 // expand it.
2609 if (!TLI.allowsUnalignedMemoryAccesses()) {
2610 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002611 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002612 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002613 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002614 TLI);
2615 }
2616 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002617 case TargetLowering::Custom:
2618 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002619 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002620 break;
2621 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002622 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002623 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2624 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2625 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002626 ST->getSrcValue(), SVOffset, isVolatile,
2627 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002628 break;
2629 }
2630 break;
2631 }
2632 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002633 if (!ST->getMemoryVT().isVector()) {
2634 // Truncate the value and store the result.
2635 Tmp3 = PromoteOp(ST->getValue());
2636 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2637 SVOffset, ST->getMemoryVT(),
2638 isVolatile, Alignment);
2639 break;
2640 }
2641 // Fall thru to expand for vector
2642 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002643 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002644 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002645
2646 // If this is a vector type, then we have to calculate the increment as
2647 // the product of the element size in bytes, and the number of elements
2648 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002649 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002650 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002651 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002652 MVT InVT = InVal->getValueType(InIx);
2653 unsigned NumElems = InVT.getVectorNumElements();
2654 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002655
Dan Gohman7f321562007-06-25 16:23:39 +00002656 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002657 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002658 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002659 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002660 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002661 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002662 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002663 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002664 Result = LegalizeOp(Result);
2665 break;
2666 } else if (NumElems == 1) {
2667 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002668 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002669 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002670 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002671 // The scalarized value type may not be legal, e.g. it might require
2672 // promotion or expansion. Relegalize the scalar store.
2673 Result = LegalizeOp(Result);
2674 break;
2675 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002676 // Check if we have widen this node with another value
2677 std::map<SDValue, SDValue>::iterator I =
2678 WidenNodes.find(ST->getValue());
2679 if (I != WidenNodes.end()) {
2680 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2681 break;
2682 }
2683 else {
2684 SplitVectorOp(ST->getValue(), Lo, Hi);
2685 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2686 EVT.getSizeInBits()/8;
2687 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002688 }
2689 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002690 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002691 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002692
Richard Pennington4b052dc2008-09-25 16:15:10 +00002693 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002694 std::swap(Lo, Hi);
2695 }
2696
2697 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002698 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002699
Gabor Greifba36cb52008-08-28 21:40:38 +00002700 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002701 // Must be int <-> float one-to-one expansion.
2702 Result = Lo;
2703 break;
2704 }
2705
Evan Cheng8b2794a2006-10-13 21:14:26 +00002706 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002707 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002708 assert(isTypeLegal(Tmp2.getValueType()) &&
2709 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002710 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002711 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002712 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002713 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002714 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2715 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002716 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002717 }
2718 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002719 switch (getTypeAction(ST->getValue().getValueType())) {
2720 case Legal:
2721 Tmp3 = LegalizeOp(ST->getValue());
2722 break;
2723 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002724 if (!ST->getValue().getValueType().isVector()) {
2725 // We can promote the value, the truncstore will still take care of it.
2726 Tmp3 = PromoteOp(ST->getValue());
2727 break;
2728 }
2729 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002730 case Expand:
2731 // Just store the low part. This may become a non-trunc store, so make
2732 // sure to use getTruncStore, not UpdateNodeOperands below.
2733 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2734 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2735 SVOffset, MVT::i8, isVolatile, Alignment);
2736 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002737
Duncan Sands83ec4b62008-06-06 12:08:01 +00002738 MVT StVT = ST->getMemoryVT();
2739 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002740
Duncan Sands83ec4b62008-06-06 12:08:01 +00002741 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002742 // Promote to a byte-sized store with upper bits zero if not
2743 // storing an integral number of bytes. For example, promote
2744 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002745 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002746 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2747 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2748 SVOffset, NVT, isVolatile, Alignment);
2749 } else if (StWidth & (StWidth - 1)) {
2750 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002751 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002752 "Unsupported truncstore!");
2753 unsigned RoundWidth = 1 << Log2_32(StWidth);
2754 assert(RoundWidth < StWidth);
2755 unsigned ExtraWidth = StWidth - RoundWidth;
2756 assert(ExtraWidth < RoundWidth);
2757 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2758 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002759 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2760 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002761 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002762 unsigned IncrementSize;
2763
2764 if (TLI.isLittleEndian()) {
2765 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2766 // Store the bottom RoundWidth bits.
2767 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2768 SVOffset, RoundVT,
2769 isVolatile, Alignment);
2770
2771 // Store the remaining ExtraWidth bits.
2772 IncrementSize = RoundWidth / 8;
2773 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2774 DAG.getIntPtrConstant(IncrementSize));
2775 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2776 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2777 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2778 SVOffset + IncrementSize, ExtraVT, isVolatile,
2779 MinAlign(Alignment, IncrementSize));
2780 } else {
2781 // Big endian - avoid unaligned stores.
2782 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2783 // Store the top RoundWidth bits.
2784 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2785 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2786 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2787 RoundVT, isVolatile, Alignment);
2788
2789 // Store the remaining ExtraWidth bits.
2790 IncrementSize = RoundWidth / 8;
2791 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2792 DAG.getIntPtrConstant(IncrementSize));
2793 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2794 SVOffset + IncrementSize, ExtraVT, isVolatile,
2795 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002796 }
Duncan Sands7e857202008-01-22 07:17:34 +00002797
2798 // The order of the stores doesn't matter.
2799 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2800 } else {
2801 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2802 Tmp2 != ST->getBasePtr())
2803 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2804 ST->getOffset());
2805
2806 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2807 default: assert(0 && "This action is not supported yet!");
2808 case TargetLowering::Legal:
2809 // If this is an unaligned store and the target doesn't support it,
2810 // expand it.
2811 if (!TLI.allowsUnalignedMemoryAccesses()) {
2812 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002813 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002814 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002815 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002816 TLI);
2817 }
2818 break;
2819 case TargetLowering::Custom:
2820 Result = TLI.LowerOperation(Result, DAG);
2821 break;
2822 case Expand:
2823 // TRUNCSTORE:i16 i32 -> STORE i16
2824 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2825 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2826 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2827 isVolatile, Alignment);
2828 break;
2829 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002830 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002831 }
2832 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002833 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002834 case ISD::PCMARKER:
2835 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002836 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002837 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002838 case ISD::STACKSAVE:
2839 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002840 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2841 Tmp1 = Result.getValue(0);
2842 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002843
Chris Lattner140d53c2006-01-13 02:50:02 +00002844 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2845 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002846 case TargetLowering::Legal: break;
2847 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002848 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002849 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002850 Tmp1 = LegalizeOp(Tmp3);
2851 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002852 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002853 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002854 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002855 // Expand to CopyFromReg if the target set
2856 // StackPointerRegisterToSaveRestore.
2857 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002858 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002859 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002860 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002861 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002862 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2863 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002864 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002865 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002866 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002867
2868 // Since stacksave produce two values, make sure to remember that we
2869 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002870 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2871 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002872 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002873
Chris Lattner140d53c2006-01-13 02:50:02 +00002874 case ISD::STACKRESTORE:
2875 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2876 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002877 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002878
2879 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2880 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002881 case TargetLowering::Legal: break;
2882 case TargetLowering::Custom:
2883 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002884 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002885 break;
2886 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002887 // Expand to CopyToReg if the target set
2888 // StackPointerRegisterToSaveRestore.
2889 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2890 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2891 } else {
2892 Result = Tmp1;
2893 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002894 break;
2895 }
2896 break;
2897
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002898 case ISD::READCYCLECOUNTER:
2899 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002900 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002901 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2902 Node->getValueType(0))) {
2903 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002904 case TargetLowering::Legal:
2905 Tmp1 = Result.getValue(0);
2906 Tmp2 = Result.getValue(1);
2907 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002908 case TargetLowering::Custom:
2909 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002910 Tmp1 = LegalizeOp(Result.getValue(0));
2911 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002912 break;
2913 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002914
2915 // Since rdcc produce two values, make sure to remember that we legalized
2916 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002917 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2918 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002919 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002920
Chris Lattner2ee743f2005-01-14 22:08:15 +00002921 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002922 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2923 case Expand: assert(0 && "It's impossible to expand bools");
2924 case Legal:
2925 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2926 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002927 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002928 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002929 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002930 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002931 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002932 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002933 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002934 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002935 break;
2936 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002937 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002938 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002939 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002940
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002941 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002942
Nate Begemanb942a3d2005-08-23 04:29:48 +00002943 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002944 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002945 case TargetLowering::Legal: break;
2946 case TargetLowering::Custom: {
2947 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002948 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002949 break;
2950 }
Nate Begeman9373a812005-08-10 20:51:12 +00002951 case TargetLowering::Expand:
2952 if (Tmp1.getOpcode() == ISD::SETCC) {
2953 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2954 Tmp2, Tmp3,
2955 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2956 } else {
2957 Result = DAG.getSelectCC(Tmp1,
2958 DAG.getConstant(0, Tmp1.getValueType()),
2959 Tmp2, Tmp3, ISD::SETNE);
2960 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002961 break;
2962 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002963 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002964 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2965 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002966 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002967 ExtOp = ISD::BIT_CONVERT;
2968 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002969 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002970 ExtOp = ISD::ANY_EXTEND;
2971 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002972 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002973 ExtOp = ISD::FP_EXTEND;
2974 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002975 }
2976 // Promote each of the values to the new type.
2977 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2978 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2979 // Perform the larger operation, then round down.
2980 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002981 if (TruncOp != ISD::FP_ROUND)
2982 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2983 else
2984 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2985 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002986 break;
2987 }
2988 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002989 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002990 case ISD::SELECT_CC: {
2991 Tmp1 = Node->getOperand(0); // LHS
2992 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002993 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2994 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002995 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002996
Duncan Sands5480c042009-01-01 15:52:00 +00002997 LegalizeSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002998
Evan Cheng7f042682008-10-15 02:05:31 +00002999 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00003000 // the LHS is a legal SETCC itself. In this case, we need to compare
3001 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00003002 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003003 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3004 CC = DAG.getCondCode(ISD::SETNE);
3005 }
3006 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3007
3008 // Everything is legal, see if we should expand this op or something.
3009 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3010 default: assert(0 && "This action is not supported yet!");
3011 case TargetLowering::Legal: break;
3012 case TargetLowering::Custom:
3013 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003014 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00003015 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003016 }
3017 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003018 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003019 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00003020 Tmp1 = Node->getOperand(0);
3021 Tmp2 = Node->getOperand(1);
3022 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00003023 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003024
3025 // If we had to Expand the SetCC operands into a SELECT node, then it may
3026 // not always be possible to return a true LHS & RHS. In this case, just
3027 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003028 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003029 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003030 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003031 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003032
Chris Lattner73e142f2006-01-30 22:43:50 +00003033 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003034 default: assert(0 && "Cannot handle this action for SETCC yet!");
3035 case TargetLowering::Custom:
3036 isCustom = true;
3037 // FALLTHROUGH.
3038 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00003039 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00003040 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003041 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003042 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00003043 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003044 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003045 case TargetLowering::Promote: {
3046 // First step, figure out the appropriate operation to use.
3047 // Allow SETCC to not be supported for all legal data types
3048 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00003049 MVT NewInTy = Node->getOperand(0).getValueType();
3050 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00003051
3052 // Scan for the appropriate larger type to use.
3053 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003054 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00003055
Duncan Sands83ec4b62008-06-06 12:08:01 +00003056 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003057 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003058 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003059 "Fell off of the edge of the floating point world");
3060
3061 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00003062 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00003063 break;
3064 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003065 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00003066 assert(0 && "Cannot promote Legal Integer SETCC yet");
3067 else {
3068 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3069 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3070 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003071 Tmp1 = LegalizeOp(Tmp1);
3072 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00003073 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00003074 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00003075 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003076 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003077 case TargetLowering::Expand:
3078 // Expand a setcc node into a select_cc of the same condition, lhs, and
3079 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003080 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003081 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3082 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003083 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003084 break;
3085 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003086 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003087 case ISD::VSETCC: {
3088 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3089 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003090 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003091
3092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3093
3094 // Everything is legal, see if we should expand this op or something.
3095 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3096 default: assert(0 && "This action is not supported yet!");
3097 case TargetLowering::Legal: break;
3098 case TargetLowering::Custom:
3099 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003100 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003101 break;
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003102 case TargetLowering::Expand: {
3103 // Unroll into a nasty set of scalar code for now.
3104 MVT VT = Node->getValueType(0);
3105 unsigned NumElems = VT.getVectorNumElements();
3106 MVT EltVT = VT.getVectorElementType();
3107 MVT TmpEltVT = Tmp1.getValueType().getVectorElementType();
3108 SmallVector<SDValue, 8> Ops(NumElems);
3109 for (unsigned i = 0; i < NumElems; ++i) {
3110 SDValue In1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3111 Tmp1, DAG.getIntPtrConstant(i));
Duncan Sands5480c042009-01-01 15:52:00 +00003112 Ops[i] = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(TmpEltVT), In1,
Mon P Wang84aff842008-12-17 08:49:47 +00003113 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, TmpEltVT,
3114 Tmp2, DAG.getIntPtrConstant(i)),
3115 CC);
3116 Ops[i] = DAG.getNode(ISD::SELECT, EltVT, Ops[i],
3117 DAG.getConstant(EltVT.getIntegerVTBitMask(),EltVT),
3118 DAG.getConstant(0, EltVT));
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003119 }
3120 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], NumElems);
3121 break;
3122 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00003123 }
3124 break;
3125 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003126
Chris Lattner5b359c62005-04-02 04:00:59 +00003127 case ISD::SHL_PARTS:
3128 case ISD::SRA_PARTS:
3129 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003130 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003131 bool Changed = false;
3132 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3133 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3134 Changed |= Ops.back() != Node->getOperand(i);
3135 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003136 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003137 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003138
Evan Cheng05a2d562006-01-09 18:31:59 +00003139 switch (TLI.getOperationAction(Node->getOpcode(),
3140 Node->getValueType(0))) {
3141 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003142 case TargetLowering::Legal: break;
3143 case TargetLowering::Custom:
3144 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003145 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003146 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003147 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003148 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003149 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003150 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003151 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003152 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003153 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003154 return RetVal;
3155 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003156 break;
3157 }
3158
Chris Lattner2c8086f2005-04-02 05:00:07 +00003159 // Since these produce multiple values, make sure to remember that we
3160 // legalized all of them.
3161 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003162 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003163 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003164 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003165
3166 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003167 case ISD::ADD:
3168 case ISD::SUB:
3169 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003170 case ISD::MULHS:
3171 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003172 case ISD::UDIV:
3173 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003174 case ISD::AND:
3175 case ISD::OR:
3176 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003177 case ISD::SHL:
3178 case ISD::SRL:
3179 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003180 case ISD::FADD:
3181 case ISD::FSUB:
3182 case ISD::FMUL:
3183 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003184 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003185 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003186 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3187 case Expand: assert(0 && "Not possible");
3188 case Legal:
3189 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3190 break;
3191 case Promote:
3192 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3193 break;
3194 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003195
3196 if ((Node->getOpcode() == ISD::SHL ||
3197 Node->getOpcode() == ISD::SRL ||
3198 Node->getOpcode() == ISD::SRA) &&
3199 !Node->getValueType(0).isVector()) {
Mon P Wange9f10152008-12-09 05:46:39 +00003200 Tmp2 = LegalizeShiftAmount(Tmp2);
Mon P Wange1a0b2e2008-12-13 08:15:14 +00003201 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003202
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003203 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003204
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003205 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003206 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003207 case TargetLowering::Legal: break;
3208 case TargetLowering::Custom:
3209 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003210 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003211 Result = Tmp1;
3212 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003213 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003214 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003215 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003216 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003217
Dan Gohman525178c2007-10-08 18:33:35 +00003218 // See if multiply or divide can be lowered using two-result operations.
3219 SDVTList VTs = DAG.getVTList(VT, VT);
3220 if (Node->getOpcode() == ISD::MUL) {
3221 // We just need the low half of the multiply; try both the signed
3222 // and unsigned forms. If the target supports both SMUL_LOHI and
3223 // UMUL_LOHI, form a preference by checking which forms of plain
3224 // MULH it supports.
3225 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3226 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3227 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3228 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3229 unsigned OpToUse = 0;
3230 if (HasSMUL_LOHI && !HasMULHS) {
3231 OpToUse = ISD::SMUL_LOHI;
3232 } else if (HasUMUL_LOHI && !HasMULHU) {
3233 OpToUse = ISD::UMUL_LOHI;
3234 } else if (HasSMUL_LOHI) {
3235 OpToUse = ISD::SMUL_LOHI;
3236 } else if (HasUMUL_LOHI) {
3237 OpToUse = ISD::UMUL_LOHI;
3238 }
3239 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003240 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003241 break;
3242 }
3243 }
3244 if (Node->getOpcode() == ISD::MULHS &&
3245 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003246 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3247 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003248 break;
3249 }
3250 if (Node->getOpcode() == ISD::MULHU &&
3251 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003252 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3253 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003254 break;
3255 }
3256 if (Node->getOpcode() == ISD::SDIV &&
3257 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003258 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3259 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003260 break;
3261 }
3262 if (Node->getOpcode() == ISD::UDIV &&
3263 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003264 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3265 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003266 break;
3267 }
Mon P Wang87c8a8f2008-12-18 20:03:17 +00003268
Dan Gohman82669522007-10-11 23:57:53 +00003269 // Check to see if we have a libcall for this operator.
3270 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3271 bool isSigned = false;
3272 switch (Node->getOpcode()) {
3273 case ISD::UDIV:
3274 case ISD::SDIV:
3275 if (VT == MVT::i32) {
3276 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003277 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003278 isSigned = Node->getOpcode() == ISD::SDIV;
3279 }
3280 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003281 case ISD::MUL:
3282 if (VT == MVT::i32)
3283 LC = RTLIB::MUL_I32;
Scott Michel845145f2008-12-29 03:21:37 +00003284 else if (VT == MVT::i64)
3285 LC = RTLIB::MUL_I64;
Chris Lattner31d71612008-10-04 21:27:46 +00003286 break;
Dan Gohman82669522007-10-11 23:57:53 +00003287 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003288 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3289 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003290 break;
3291 default: break;
3292 }
3293 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003294 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003295 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003296 break;
3297 }
Mon P Wang0c397192008-10-30 08:01:45 +00003298
Duncan Sands83ec4b62008-06-06 12:08:01 +00003299 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003300 "Cannot expand this binary operator!");
3301 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003302 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003303 break;
3304 }
Evan Chengcc987612006-04-12 21:20:24 +00003305 case TargetLowering::Promote: {
3306 switch (Node->getOpcode()) {
3307 default: assert(0 && "Do not know how to promote this BinOp!");
3308 case ISD::AND:
3309 case ISD::OR:
3310 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003311 MVT OVT = Node->getValueType(0);
3312 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3313 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003314 // Bit convert each of the values to the new type.
3315 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3316 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3317 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3318 // Bit convert the result back the original type.
3319 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3320 break;
3321 }
3322 }
3323 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003324 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003325 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003326
Dan Gohmane14ea862007-10-05 14:17:22 +00003327 case ISD::SMUL_LOHI:
3328 case ISD::UMUL_LOHI:
3329 case ISD::SDIVREM:
3330 case ISD::UDIVREM:
3331 // These nodes will only be produced by target-specific lowering, so
3332 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003333 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003334 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003335
3336 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3337 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3338 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003339 break;
3340
Chris Lattnera09f8482006-03-05 05:09:38 +00003341 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3342 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3343 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3344 case Expand: assert(0 && "Not possible");
3345 case Legal:
3346 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3347 break;
3348 case Promote:
3349 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3350 break;
3351 }
3352
3353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3354
3355 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3356 default: assert(0 && "Operation not supported");
3357 case TargetLowering::Custom:
3358 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003359 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003360 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003361 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003362 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003363 // If this target supports fabs/fneg natively and select is cheap,
3364 // do this efficiently.
3365 if (!TLI.isSelectExpensive() &&
3366 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3367 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003368 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003369 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003370 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003371 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003372 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003373 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Duncan Sands5480c042009-01-01 15:52:00 +00003374 SignBit = DAG.getSetCC(TLI.getSetCCResultType(IVT),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003375 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3376 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003377 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003378 // Select between the nabs and abs value based on the sign bit of
3379 // the input.
3380 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3381 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3382 AbsVal),
3383 AbsVal);
3384 Result = LegalizeOp(Result);
3385 break;
3386 }
3387
3388 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003389 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003390 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3391 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3392 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3393 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003394 break;
3395 }
Evan Cheng912095b2007-01-04 21:56:39 +00003396 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003397 break;
3398
Nate Begeman551bf3f2006-02-17 05:43:56 +00003399 case ISD::ADDC:
3400 case ISD::SUBC:
3401 Tmp1 = LegalizeOp(Node->getOperand(0));
3402 Tmp2 = LegalizeOp(Node->getOperand(1));
3403 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003404 Tmp3 = Result.getValue(0);
3405 Tmp4 = Result.getValue(1);
3406
3407 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3408 default: assert(0 && "This action is not supported yet!");
3409 case TargetLowering::Legal:
3410 break;
3411 case TargetLowering::Custom:
3412 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3413 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003414 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003415 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3416 }
3417 break;
3418 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003419 // Since this produces two values, make sure to remember that we legalized
3420 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003421 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3422 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3423 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003424
Nate Begeman551bf3f2006-02-17 05:43:56 +00003425 case ISD::ADDE:
3426 case ISD::SUBE:
3427 Tmp1 = LegalizeOp(Node->getOperand(0));
3428 Tmp2 = LegalizeOp(Node->getOperand(1));
3429 Tmp3 = LegalizeOp(Node->getOperand(2));
3430 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003431 Tmp3 = Result.getValue(0);
3432 Tmp4 = Result.getValue(1);
3433
3434 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3435 default: assert(0 && "This action is not supported yet!");
3436 case TargetLowering::Legal:
3437 break;
3438 case TargetLowering::Custom:
3439 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3440 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003441 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003442 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3443 }
3444 break;
3445 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003446 // Since this produces two values, make sure to remember that we legalized
3447 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003448 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3449 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3450 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003451
Nate Begeman419f8b62005-10-18 00:27:41 +00003452 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003453 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003454 // TODO: handle the case where the Lo and Hi operands are not of legal type
3455 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3456 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3457 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003458 case TargetLowering::Promote:
3459 case TargetLowering::Custom:
3460 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003461 case TargetLowering::Legal:
3462 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3463 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3464 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003465 case TargetLowering::Expand:
3466 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3467 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3468 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003469 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003470 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003471 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003472 break;
3473 }
3474 break;
3475 }
3476
Nate Begemanc105e192005-04-06 00:23:54 +00003477 case ISD::UREM:
3478 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003479 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003480 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3481 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003482
Nate Begemanc105e192005-04-06 00:23:54 +00003483 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003484 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3485 case TargetLowering::Custom:
3486 isCustom = true;
3487 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003488 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003489 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003490 if (isCustom) {
3491 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003492 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003493 }
Nate Begemanc105e192005-04-06 00:23:54 +00003494 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003495 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003496 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003497 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003498 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003499
3500 // See if remainder can be lowered using two-result operations.
3501 SDVTList VTs = DAG.getVTList(VT, VT);
3502 if (Node->getOpcode() == ISD::SREM &&
3503 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003504 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003505 break;
3506 }
3507 if (Node->getOpcode() == ISD::UREM &&
3508 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003509 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003510 break;
3511 }
3512
Duncan Sands83ec4b62008-06-06 12:08:01 +00003513 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003514 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003515 TargetLowering::Legal) {
3516 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003517 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003518 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3519 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003520 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003521 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003522 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003523 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003524 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003525 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3526 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003527 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003528 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003529 }
Dan Gohman0d974262007-11-06 22:11:54 +00003530 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003531 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003532 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003533 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003534 Result = LegalizeOp(UnrollVectorOp(Op));
3535 } else {
3536 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003537 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3538 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003539 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003540 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003541 }
Nate Begemanc105e192005-04-06 00:23:54 +00003542 }
3543 break;
3544 }
Dan Gohman525178c2007-10-08 18:33:35 +00003545 }
Nate Begemanc105e192005-04-06 00:23:54 +00003546 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003547 case ISD::VAARG: {
3548 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3549 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3550
Duncan Sands83ec4b62008-06-06 12:08:01 +00003551 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003552 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3553 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003554 case TargetLowering::Custom:
3555 isCustom = true;
3556 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003557 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003558 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3559 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003560 Tmp1 = Result.getValue(1);
3561
3562 if (isCustom) {
3563 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003564 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003565 Result = LegalizeOp(Tmp2);
3566 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3567 }
3568 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003569 break;
3570 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003571 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003572 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003573 // Increment the pointer, VAList, to the next vaarg
Duncan Sands5c58a312008-11-03 11:51:11 +00003574 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3575 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3576 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003577 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003578 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003579 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003580 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003581 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003582 Result = LegalizeOp(Result);
3583 break;
3584 }
3585 }
3586 // Since VAARG produces two values, make sure to remember that we
3587 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003588 AddLegalizedOperand(SDValue(Node, 0), Result);
3589 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003590 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003591 }
3592
3593 case ISD::VACOPY:
3594 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3595 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3596 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3597
3598 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3599 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003600 case TargetLowering::Custom:
3601 isCustom = true;
3602 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003603 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003604 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3605 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003606 if (isCustom) {
3607 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003608 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003609 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003610 break;
3611 case TargetLowering::Expand:
3612 // This defaults to loading a pointer from the input and storing it to the
3613 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003614 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3615 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003616 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3617 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003618 break;
3619 }
3620 break;
3621
3622 case ISD::VAEND:
3623 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3624 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3625
3626 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3627 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003628 case TargetLowering::Custom:
3629 isCustom = true;
3630 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003631 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003632 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003633 if (isCustom) {
3634 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003635 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003636 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003637 break;
3638 case TargetLowering::Expand:
3639 Result = Tmp1; // Default to a no-op, return the chain
3640 break;
3641 }
3642 break;
3643
3644 case ISD::VASTART:
3645 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3646 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3647
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003648 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3649
Nate Begemanacc398c2006-01-25 18:21:52 +00003650 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3651 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003652 case TargetLowering::Legal: break;
3653 case TargetLowering::Custom:
3654 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003655 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003656 break;
3657 }
3658 break;
3659
Nate Begeman35ef9132006-01-11 21:21:00 +00003660 case ISD::ROTL:
3661 case ISD::ROTR:
3662 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3663 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003664 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003665 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3666 default:
3667 assert(0 && "ROTL/ROTR legalize operation not supported");
3668 break;
3669 case TargetLowering::Legal:
3670 break;
3671 case TargetLowering::Custom:
3672 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003673 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003674 break;
3675 case TargetLowering::Promote:
3676 assert(0 && "Do not know how to promote ROTL/ROTR");
3677 break;
3678 case TargetLowering::Expand:
3679 assert(0 && "Do not know how to expand ROTL/ROTR");
3680 break;
3681 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003682 break;
3683
Nate Begemand88fc032006-01-14 03:14:10 +00003684 case ISD::BSWAP:
3685 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3686 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003687 case TargetLowering::Custom:
3688 assert(0 && "Cannot custom legalize this yet!");
3689 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003690 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003691 break;
3692 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003693 MVT OVT = Tmp1.getValueType();
3694 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3695 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003696
Chris Lattner456a93a2006-01-28 07:39:30 +00003697 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3698 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3699 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3700 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3701 break;
3702 }
3703 case TargetLowering::Expand:
3704 Result = ExpandBSWAP(Tmp1);
3705 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003706 }
3707 break;
3708
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003709 case ISD::CTPOP:
3710 case ISD::CTTZ:
3711 case ISD::CTLZ:
3712 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3713 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003714 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003715 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003716 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003717 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003718 TargetLowering::Custom) {
3719 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003720 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003721 Result = Tmp1;
3722 }
Scott Michel910b66d2007-07-30 21:00:31 +00003723 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003724 break;
3725 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003726 MVT OVT = Tmp1.getValueType();
3727 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003728
3729 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003730 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3731 // Perform the larger operation, then subtract if needed.
3732 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003733 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003734 case ISD::CTPOP:
3735 Result = Tmp1;
3736 break;
3737 case ISD::CTTZ:
3738 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands5480c042009-01-01 15:52:00 +00003739 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003740 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003741 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003742 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003743 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003744 break;
3745 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003746 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003747 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003748 DAG.getConstant(NVT.getSizeInBits() -
3749 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003750 break;
3751 }
3752 break;
3753 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003754 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003755 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003756 break;
3757 }
3758 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003759
Chris Lattner2c8086f2005-04-02 05:00:07 +00003760 // Unary operators
3761 case ISD::FABS:
3762 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003763 case ISD::FSQRT:
3764 case ISD::FSIN:
3765 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003766 case ISD::FLOG:
3767 case ISD::FLOG2:
3768 case ISD::FLOG10:
3769 case ISD::FEXP:
3770 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003771 case ISD::FTRUNC:
3772 case ISD::FFLOOR:
3773 case ISD::FCEIL:
3774 case ISD::FRINT:
3775 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003776 Tmp1 = LegalizeOp(Node->getOperand(0));
3777 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003778 case TargetLowering::Promote:
3779 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003780 isCustom = true;
3781 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003782 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003783 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003784 if (isCustom) {
3785 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003786 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003787 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003788 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003789 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003790 switch (Node->getOpcode()) {
3791 default: assert(0 && "Unreachable!");
3792 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003793 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3794 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003795 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003796 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003797 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003798 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003799 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003800 Tmp2 = DAG.getConstantFP(0.0, VT);
Duncan Sands5480c042009-01-01 15:52:00 +00003801 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()),
3802 Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003803 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3804 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003805 break;
3806 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003807 case ISD::FSQRT:
3808 case ISD::FSIN:
3809 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003810 case ISD::FLOG:
3811 case ISD::FLOG2:
3812 case ISD::FLOG10:
3813 case ISD::FEXP:
3814 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003815 case ISD::FTRUNC:
3816 case ISD::FFLOOR:
3817 case ISD::FCEIL:
3818 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003819 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003820 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003821
3822 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003823 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003824 Result = LegalizeOp(UnrollVectorOp(Op));
3825 break;
3826 }
3827
Evan Cheng56966222007-01-12 02:11:51 +00003828 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003829 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003830 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003831 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3832 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003833 break;
3834 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003835 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3836 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003837 break;
3838 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003839 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3840 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003841 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003842 case ISD::FLOG:
3843 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3844 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3845 break;
3846 case ISD::FLOG2:
3847 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3848 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3849 break;
3850 case ISD::FLOG10:
3851 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3852 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3853 break;
3854 case ISD::FEXP:
3855 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3856 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3857 break;
3858 case ISD::FEXP2:
3859 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3860 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3861 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003862 case ISD::FTRUNC:
3863 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3864 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3865 break;
3866 case ISD::FFLOOR:
3867 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3868 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3869 break;
3870 case ISD::FCEIL:
3871 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3872 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3873 break;
3874 case ISD::FRINT:
3875 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3876 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3877 break;
3878 case ISD::FNEARBYINT:
3879 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3880 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3881 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003882 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003883 default: assert(0 && "Unreachable!");
3884 }
Dan Gohman475871a2008-07-27 21:46:04 +00003885 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003886 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003887 break;
3888 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003889 }
3890 break;
3891 }
3892 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003893 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003894 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003895
3896 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003897 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003898 Result = LegalizeOp(UnrollVectorOp(Op));
3899 break;
3900 }
3901
3902 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003903 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3904 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003905 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003906 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003907 break;
3908 }
Chris Lattner35481892005-12-23 00:16:34 +00003909 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003910 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003911 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3912 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003913 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003914 // The input has to be a vector type, we have to either scalarize it, pack
3915 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003916 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003917 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003918 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3919 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003920
3921 // Figure out if there is a simple type corresponding to this Vector
3922 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003923 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003924 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003925 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003926 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3927 LegalizeOp(Node->getOperand(0)));
3928 break;
3929 } else if (NumElems == 1) {
3930 // Turn this into a bit convert of the scalar input.
3931 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3932 ScalarizeVectorOp(Node->getOperand(0)));
3933 break;
3934 } else {
3935 // FIXME: UNIMP! Store then reload
3936 assert(0 && "Cast from unsupported vector type not implemented yet!");
3937 }
Chris Lattner67993f72006-01-23 07:30:46 +00003938 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003939 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3940 Node->getOperand(0).getValueType())) {
3941 default: assert(0 && "Unknown operation action!");
3942 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003943 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3944 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003945 break;
3946 case TargetLowering::Legal:
3947 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003948 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003949 break;
3950 }
3951 }
3952 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003953 case ISD::CONVERT_RNDSAT: {
3954 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3955 switch (CvtCode) {
3956 default: assert(0 && "Unknown cvt code!");
3957 case ISD::CVT_SF:
3958 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00003959 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003960 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003961 case ISD::CVT_FS:
3962 case ISD::CVT_FU:
3963 case ISD::CVT_SS:
3964 case ISD::CVT_SU:
3965 case ISD::CVT_US:
3966 case ISD::CVT_UU: {
3967 SDValue DTyOp = Node->getOperand(1);
3968 SDValue STyOp = Node->getOperand(2);
3969 SDValue RndOp = Node->getOperand(3);
3970 SDValue SatOp = Node->getOperand(4);
3971 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3972 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3973 case Legal:
3974 Tmp1 = LegalizeOp(Node->getOperand(0));
3975 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3976 RndOp, SatOp);
3977 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3978 TargetLowering::Custom) {
3979 Tmp1 = TLI.LowerOperation(Result, DAG);
3980 if (Tmp1.getNode()) Result = Tmp1;
3981 }
3982 break;
3983 case Promote:
3984 Result = PromoteOp(Node->getOperand(0));
3985 // For FP, make Op1 a i32
3986
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003987 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang77cdf302008-11-10 20:54:11 +00003988 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3989 break;
3990 }
3991 break;
3992 }
3993 } // end switch CvtCode
3994 break;
3995 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003996 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003997 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003998 case ISD::UINT_TO_FP: {
3999 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00004000 Result = LegalizeINT_TO_FP(Result, isSigned,
4001 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004002 break;
4003 }
4004 case ISD::TRUNCATE:
4005 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4006 case Legal:
4007 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel546d7b52008-12-02 19:55:08 +00004008 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4009 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4010 case TargetLowering::Custom:
Mon P Wangf67303d2008-12-11 00:44:22 +00004011 isCustom = true;
4012 // FALLTHROUGH
Scott Michel546d7b52008-12-02 19:55:08 +00004013 case TargetLowering::Legal:
Mon P Wangf67303d2008-12-11 00:44:22 +00004014 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4015 if (isCustom) {
4016 Tmp1 = TLI.LowerOperation(Result, DAG);
4017 if (Tmp1.getNode()) Result = Tmp1;
4018 }
4019 break;
Mon P Wang9e5ecb82008-12-12 01:25:51 +00004020 case TargetLowering::Expand:
4021 assert(Result.getValueType().isVector() && "must be vector type");
4022 // Unroll the truncate. We should do better.
4023 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00004024 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004025 break;
4026 case Expand:
4027 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4028
4029 // Since the result is legal, we should just be able to truncate the low
4030 // part of the source.
4031 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4032 break;
4033 case Promote:
4034 Result = PromoteOp(Node->getOperand(0));
4035 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4036 break;
4037 }
4038 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004039
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004040 case ISD::FP_TO_SINT:
4041 case ISD::FP_TO_UINT:
4042 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4043 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00004044 Tmp1 = LegalizeOp(Node->getOperand(0));
4045
Chris Lattner1618beb2005-07-29 00:11:56 +00004046 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4047 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004048 case TargetLowering::Custom:
4049 isCustom = true;
4050 // FALLTHROUGH
4051 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004052 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004053 if (isCustom) {
4054 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004055 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00004056 }
4057 break;
4058 case TargetLowering::Promote:
4059 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4060 Node->getOpcode() == ISD::FP_TO_SINT);
4061 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00004062 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00004063 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004064 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004065 MVT VT = Node->getOperand(0).getValueType();
4066 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00004067 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00004068 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4069 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004070 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00004071 Tmp2 = DAG.getConstantFP(apf, VT);
Duncan Sands5480c042009-01-01 15:52:00 +00004072 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(VT), Node->getOperand(0),
4073 Tmp2, ISD::SETLT);
Nate Begemand2558e32005-08-14 01:20:53 +00004074 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4075 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00004076 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00004077 Tmp2));
4078 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004079 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004080 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4081 break;
Nate Begemand2558e32005-08-14 01:20:53 +00004082 } else {
4083 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4084 }
4085 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00004086 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004087 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00004088 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004089 MVT VT = Op.getValueType();
4090 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004091 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004092 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004093 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4094 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4095 Node->getOperand(0), DAG.getValueType(MVT::f64));
4096 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4097 DAG.getIntPtrConstant(1));
4098 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4099 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004100 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4101 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4102 Tmp2 = DAG.getConstantFP(apf, OVT);
4103 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4104 // FIXME: generated code sucks.
4105 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4106 DAG.getNode(ISD::ADD, MVT::i32,
4107 DAG.getNode(ISD::FP_TO_SINT, VT,
4108 DAG.getNode(ISD::FSUB, OVT,
4109 Node->getOperand(0), Tmp2)),
4110 DAG.getConstant(0x80000000, MVT::i32)),
4111 DAG.getNode(ISD::FP_TO_SINT, VT,
4112 Node->getOperand(0)),
4113 DAG.getCondCode(ISD::SETGE));
4114 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004115 break;
4116 }
Dan Gohmana2e94852008-03-10 23:03:31 +00004117 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004118 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4119 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4120 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004121 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004122 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004123 break;
4124 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004125 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004126 Tmp1 = PromoteOp(Node->getOperand(0));
4127 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4128 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004129 break;
4130 }
4131 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004132
Chris Lattnerf2670a82008-01-16 06:57:07 +00004133 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004134 MVT DstVT = Op.getValueType();
4135 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004136 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4137 // The only other way we can lower this is to turn it into a STORE,
4138 // LOAD pair, targetting a temporary location (a stack slot).
4139 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4140 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004141 }
4142 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4143 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4144 case Legal:
4145 Tmp1 = LegalizeOp(Node->getOperand(0));
4146 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4147 break;
4148 case Promote:
4149 Tmp1 = PromoteOp(Node->getOperand(0));
4150 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4151 break;
4152 }
4153 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004154 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004155 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004156 MVT DstVT = Op.getValueType();
4157 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004158 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4159 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004160 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004161 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004162 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004163 if (DstVT!=MVT::f64)
4164 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004165 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004166 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004167 // The only other way we can lower this is to turn it into a STORE,
4168 // LOAD pair, targetting a temporary location (a stack slot).
4169 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4170 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004171 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004172 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4173 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4174 case Legal:
4175 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004176 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004177 break;
4178 case Promote:
4179 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004180 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4181 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004182 break;
4183 }
4184 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004185 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004186 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004187 case ISD::ZERO_EXTEND:
4188 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004189 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004190 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004191 case Legal:
4192 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004193 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004194 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4195 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004196 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004197 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004198 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004199 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004200 case Promote:
4201 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004202 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004203 Tmp1 = PromoteOp(Node->getOperand(0));
4204 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004205 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004206 case ISD::ZERO_EXTEND:
4207 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004208 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004209 Result = DAG.getZeroExtendInReg(Result,
4210 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004211 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004212 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004213 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004214 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004215 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004216 Result,
4217 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004218 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004219 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004220 }
4221 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004222 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004223 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004224 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004225 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004226
4227 // If this operation is not supported, convert it to a shl/shr or load/store
4228 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004229 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4230 default: assert(0 && "This action not supported for this op yet!");
4231 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004232 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004233 break;
4234 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004235 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004236 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004237 // NOTE: we could fall back on load/store here too for targets without
4238 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004239 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4240 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004241 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004242 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4243 Node->getOperand(0), ShiftCst);
4244 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4245 Result, ShiftCst);
4246 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004247 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004248 // EXTLOAD pair, targetting a temporary location (a stack slot).
4249
4250 // NOTE: there is a choice here between constantly creating new stack
4251 // slots and always reusing the same one. We currently always create
4252 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004253 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4254 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004255 } else {
4256 assert(0 && "Unknown op");
4257 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004258 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004259 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004260 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004261 }
Duncan Sands36397f52007-07-27 12:58:54 +00004262 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004263 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004264 for (unsigned i = 0; i != 6; ++i)
4265 Ops[i] = LegalizeOp(Node->getOperand(i));
4266 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4267 // The only option for this node is to custom lower it.
4268 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004269 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004270
4271 // Since trampoline produces two values, make sure to remember that we
4272 // legalized both of them.
4273 Tmp1 = LegalizeOp(Result.getValue(1));
4274 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004275 AddLegalizedOperand(SDValue(Node, 0), Result);
4276 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004277 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004278 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004279 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004280 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004281 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4282 default: assert(0 && "This action not supported for this op yet!");
4283 case TargetLowering::Custom:
4284 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004285 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004286 // Fall Thru
4287 case TargetLowering::Legal:
4288 // If this operation is not supported, lower it to constant 1
4289 Result = DAG.getConstant(1, VT);
4290 break;
4291 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004292 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004293 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004294 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004295 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004296 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4297 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004298 case TargetLowering::Legal:
4299 Tmp1 = LegalizeOp(Node->getOperand(0));
4300 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4301 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004302 case TargetLowering::Custom:
4303 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004304 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004305 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004306 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004307 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004308 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004309 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004310 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004311 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004312 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004313 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner034f12e2008-01-15 22:09:33 +00004314 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004315 Result = CallResult.second;
4316 break;
4317 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004318 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004319 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004320
Bill Wendling74c37652008-12-09 22:08:41 +00004321 case ISD::SADDO:
4322 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004323 MVT VT = Node->getValueType(0);
4324 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4325 default: assert(0 && "This action not supported for this op yet!");
4326 case TargetLowering::Custom:
4327 Result = TLI.LowerOperation(Op, DAG);
4328 if (Result.getNode()) break;
4329 // FALLTHROUGH
4330 case TargetLowering::Legal: {
4331 SDValue LHS = LegalizeOp(Node->getOperand(0));
4332 SDValue RHS = LegalizeOp(Node->getOperand(1));
4333
Bill Wendling74c37652008-12-09 22:08:41 +00004334 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4335 ISD::ADD : ISD::SUB, LHS.getValueType(),
4336 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004337 MVT OType = Node->getValueType(1);
4338
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004339 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004340
Bill Wendling740464e2008-11-25 19:40:17 +00004341 // LHSSign -> LHS >= 0
4342 // RHSSign -> RHS >= 0
4343 // SumSign -> Sum >= 0
4344 //
Bill Wendling74c37652008-12-09 22:08:41 +00004345 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00004346 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00004347 // Sub:
4348 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00004349 //
4350 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4351 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling74c37652008-12-09 22:08:41 +00004352 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4353 Node->getOpcode() == ISD::SADDO ?
4354 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004355
Bill Wendling740464e2008-11-25 19:40:17 +00004356 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4357 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004358
Bill Wendling74c37652008-12-09 22:08:41 +00004359 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004360
4361 MVT ValueVTs[] = { LHS.getValueType(), OType };
4362 SDValue Ops[] = { Sum, Cmp };
4363
Duncan Sandsaaffa052008-12-01 11:41:29 +00004364 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4365 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004366 SDNode *RNode = Result.getNode();
4367 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4368 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4369 break;
4370 }
4371 }
4372
4373 break;
4374 }
Bill Wendling74c37652008-12-09 22:08:41 +00004375 case ISD::UADDO:
4376 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004377 MVT VT = Node->getValueType(0);
4378 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4379 default: assert(0 && "This action not supported for this op yet!");
4380 case TargetLowering::Custom:
4381 Result = TLI.LowerOperation(Op, DAG);
4382 if (Result.getNode()) break;
4383 // FALLTHROUGH
4384 case TargetLowering::Legal: {
4385 SDValue LHS = LegalizeOp(Node->getOperand(0));
4386 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004387
Bill Wendling74c37652008-12-09 22:08:41 +00004388 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4389 ISD::ADD : ISD::SUB, LHS.getValueType(),
4390 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004391 MVT OType = Node->getValueType(1);
Bill Wendling74c37652008-12-09 22:08:41 +00004392 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4393 Node->getOpcode () == ISD::UADDO ?
4394 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004395
Bill Wendling41ea7e72008-11-24 19:21:46 +00004396 MVT ValueVTs[] = { LHS.getValueType(), OType };
4397 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004398
Duncan Sandsaaffa052008-12-01 11:41:29 +00004399 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4400 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004401 SDNode *RNode = Result.getNode();
4402 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4403 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4404 break;
4405 }
4406 }
4407
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004408 break;
4409 }
Bill Wendling74c37652008-12-09 22:08:41 +00004410 case ISD::SMULO:
4411 case ISD::UMULO: {
4412 MVT VT = Node->getValueType(0);
4413 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4414 default: assert(0 && "This action is not supported at all!");
4415 case TargetLowering::Custom:
4416 Result = TLI.LowerOperation(Op, DAG);
4417 if (Result.getNode()) break;
4418 // Fall Thru
4419 case TargetLowering::Legal:
4420 // FIXME: According to Hacker's Delight, this can be implemented in
4421 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004422 // requires a division + a branch.
Bill Wendling74c37652008-12-09 22:08:41 +00004423 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4424 break;
4425 }
4426 break;
4427 }
4428
Chris Lattner45b8caf2005-01-15 07:15:18 +00004429 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004430
Chris Lattner4ddd2832006-04-08 04:13:17 +00004431 assert(Result.getValueType() == Op.getValueType() &&
4432 "Bad legalization!");
4433
Chris Lattner456a93a2006-01-28 07:39:30 +00004434 // Make sure that the generated code is itself legal.
4435 if (Result != Op)
4436 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004437
Chris Lattner45982da2005-05-12 16:53:42 +00004438 // Note that LegalizeOp may be reentered even from single-use nodes, which
4439 // means that we always must cache transformed nodes.
4440 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004441 return Result;
4442}
4443
Chris Lattner8b6fa222005-01-15 22:16:26 +00004444/// PromoteOp - Given an operation that produces a value in an invalid type,
4445/// promote it to compute the value into a larger type. The produced value will
4446/// have the correct bits for the low portion of the register, but no guarantee
4447/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004448SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004449 MVT VT = Op.getValueType();
4450 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004451 assert(getTypeAction(VT) == Promote &&
4452 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004453 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004454 "Cannot promote to smaller type!");
4455
Dan Gohman475871a2008-07-27 21:46:04 +00004456 SDValue Tmp1, Tmp2, Tmp3;
4457 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004458 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004459
Dan Gohman475871a2008-07-27 21:46:04 +00004460 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004461 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004462
Chris Lattner03c85462005-01-15 05:21:40 +00004463 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004464 case ISD::CopyFromReg:
4465 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004466 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004467#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004468 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004469#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004470 assert(0 && "Do not know how to promote this operator!");
4471 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004472 case ISD::UNDEF:
4473 Result = DAG.getNode(ISD::UNDEF, NVT);
4474 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004475 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004476 if (VT != MVT::i1)
4477 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4478 else
4479 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004480 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4481 break;
4482 case ISD::ConstantFP:
4483 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4484 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4485 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004486
Duncan Sands5480c042009-01-01 15:52:00 +00004487 case ISD::SETCC: {
4488 MVT VT0 = Node->getOperand(0).getValueType();
4489 assert(isTypeLegal(TLI.getSetCCResultType(VT0))
Nate Begeman5922f562008-03-14 00:53:31 +00004490 && "SetCC type is not legal??");
Duncan Sands5480c042009-01-01 15:52:00 +00004491 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(VT0),
Nate Begeman5922f562008-03-14 00:53:31 +00004492 Node->getOperand(0), Node->getOperand(1),
4493 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004494 break;
Duncan Sands5480c042009-01-01 15:52:00 +00004495 }
Chris Lattner03c85462005-01-15 05:21:40 +00004496 case ISD::TRUNCATE:
4497 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4498 case Legal:
4499 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004500 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004501 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004502 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004503 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4504 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004505 case Promote:
4506 // The truncation is not required, because we don't guarantee anything
4507 // about high bits anyway.
4508 Result = PromoteOp(Node->getOperand(0));
4509 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004510 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004511 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4512 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004513 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004514 }
4515 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004516 case ISD::SIGN_EXTEND:
4517 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004518 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004519 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4520 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4521 case Legal:
4522 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004523 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004524 break;
4525 case Promote:
4526 // Promote the reg if it's smaller.
4527 Result = PromoteOp(Node->getOperand(0));
4528 // The high bits are not guaranteed to be anything. Insert an extend.
4529 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004530 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004531 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004532 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004533 Result = DAG.getZeroExtendInReg(Result,
4534 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004535 break;
4536 }
4537 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004538 case ISD::CONVERT_RNDSAT: {
4539 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4540 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4541 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4542 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4543 "can only promote integers");
4544 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4545 Node->getOperand(1), Node->getOperand(2),
4546 Node->getOperand(3), Node->getOperand(4),
4547 CvtCode);
4548 break;
4549
4550 }
Chris Lattner35481892005-12-23 00:16:34 +00004551 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004552 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4553 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004554 Result = PromoteOp(Result);
4555 break;
4556
Chris Lattner8b6fa222005-01-15 22:16:26 +00004557 case ISD::FP_EXTEND:
4558 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4559 case ISD::FP_ROUND:
4560 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4561 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4562 case Promote: assert(0 && "Unreachable with 2 FP types!");
4563 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004564 if (Node->getConstantOperandVal(1) == 0) {
4565 // Input is legal? Do an FP_ROUND_INREG.
4566 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4567 DAG.getValueType(VT));
4568 } else {
4569 // Just remove the truncate, it isn't affecting the value.
4570 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4571 Node->getOperand(1));
4572 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004573 break;
4574 }
4575 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004576 case ISD::SINT_TO_FP:
4577 case ISD::UINT_TO_FP:
4578 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4579 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004580 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004581 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004582 break;
4583
4584 case Promote:
4585 Result = PromoteOp(Node->getOperand(0));
4586 if (Node->getOpcode() == ISD::SINT_TO_FP)
4587 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004588 Result,
4589 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004590 else
Chris Lattner23993562005-04-13 02:38:47 +00004591 Result = DAG.getZeroExtendInReg(Result,
4592 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004593 // No extra round required here.
4594 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004595 break;
4596 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004597 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4598 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004599 // Round if we cannot tolerate excess precision.
4600 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004601 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4602 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004603 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004604 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004605 break;
4606
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004607 case ISD::SIGN_EXTEND_INREG:
4608 Result = PromoteOp(Node->getOperand(0));
4609 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4610 Node->getOperand(1));
4611 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004612 case ISD::FP_TO_SINT:
4613 case ISD::FP_TO_UINT:
4614 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4615 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004616 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004617 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004618 break;
4619 case Promote:
4620 // The input result is prerounded, so we don't have to do anything
4621 // special.
4622 Tmp1 = PromoteOp(Node->getOperand(0));
4623 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004624 }
Nate Begemand2558e32005-08-14 01:20:53 +00004625 // If we're promoting a UINT to a larger size, check to see if the new node
4626 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4627 // we can use that instead. This allows us to generate better code for
4628 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4629 // legal, such as PowerPC.
4630 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004631 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004632 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4633 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004634 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4635 } else {
4636 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4637 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004638 break;
4639
Chris Lattner2c8086f2005-04-02 05:00:07 +00004640 case ISD::FABS:
4641 case ISD::FNEG:
4642 Tmp1 = PromoteOp(Node->getOperand(0));
4643 assert(Tmp1.getValueType() == NVT);
4644 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4645 // NOTE: we do not have to do any extra rounding here for
4646 // NoExcessFPPrecision, because we know the input will have the appropriate
4647 // precision, and these operations don't modify precision at all.
4648 break;
4649
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004650 case ISD::FLOG:
4651 case ISD::FLOG2:
4652 case ISD::FLOG10:
4653 case ISD::FEXP:
4654 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004655 case ISD::FSQRT:
4656 case ISD::FSIN:
4657 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004658 case ISD::FTRUNC:
4659 case ISD::FFLOOR:
4660 case ISD::FCEIL:
4661 case ISD::FRINT:
4662 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004663 Tmp1 = PromoteOp(Node->getOperand(0));
4664 assert(Tmp1.getValueType() == NVT);
4665 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004666 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004667 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4668 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004669 break;
4670
Evan Cheng9d24ac52008-09-09 23:35:53 +00004671 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004672 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004673 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004674 // directly as well, which may be better.
4675 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004676 Tmp2 = Node->getOperand(1);
4677 if (Node->getOpcode() == ISD::FPOW)
4678 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004679 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004680 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004681 if (NoExcessFPPrecision)
4682 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4683 DAG.getValueType(VT));
4684 break;
4685 }
4686
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004687 case ISD::ATOMIC_CMP_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004688 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004689 Tmp2 = PromoteOp(Node->getOperand(2));
4690 Tmp3 = PromoteOp(Node->getOperand(3));
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004691 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4692 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004693 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004694 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004695 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004696 // Remember that we legalized the chain.
4697 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4698 break;
4699 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004700 case ISD::ATOMIC_LOAD_ADD:
4701 case ISD::ATOMIC_LOAD_SUB:
4702 case ISD::ATOMIC_LOAD_AND:
4703 case ISD::ATOMIC_LOAD_OR:
4704 case ISD::ATOMIC_LOAD_XOR:
4705 case ISD::ATOMIC_LOAD_NAND:
4706 case ISD::ATOMIC_LOAD_MIN:
4707 case ISD::ATOMIC_LOAD_MAX:
4708 case ISD::ATOMIC_LOAD_UMIN:
4709 case ISD::ATOMIC_LOAD_UMAX:
4710 case ISD::ATOMIC_SWAP: {
Mon P Wang28873102008-06-25 08:15:39 +00004711 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004712 Tmp2 = PromoteOp(Node->getOperand(2));
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004713 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getMemoryVT(),
4714 AtomNode->getChain(),
Mon P Wang28873102008-06-25 08:15:39 +00004715 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004716 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004717 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004718 // Remember that we legalized the chain.
4719 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4720 break;
4721 }
4722
Chris Lattner03c85462005-01-15 05:21:40 +00004723 case ISD::AND:
4724 case ISD::OR:
4725 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004726 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004727 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004728 case ISD::MUL:
4729 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004730 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004731 // that too is okay if they are integer operations.
4732 Tmp1 = PromoteOp(Node->getOperand(0));
4733 Tmp2 = PromoteOp(Node->getOperand(1));
4734 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4735 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004736 break;
4737 case ISD::FADD:
4738 case ISD::FSUB:
4739 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004740 Tmp1 = PromoteOp(Node->getOperand(0));
4741 Tmp2 = PromoteOp(Node->getOperand(1));
4742 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4743 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4744
4745 // Floating point operations will give excess precision that we may not be
4746 // able to tolerate. If we DO allow excess precision, just leave it,
4747 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004748 // FIXME: Why would we need to round FP ops more than integer ones?
4749 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004750 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004751 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4752 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004753 break;
4754
Chris Lattner8b6fa222005-01-15 22:16:26 +00004755 case ISD::SDIV:
4756 case ISD::SREM:
4757 // These operators require that their input be sign extended.
4758 Tmp1 = PromoteOp(Node->getOperand(0));
4759 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004760 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004761 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4762 DAG.getValueType(VT));
4763 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4764 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004765 }
4766 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4767
4768 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004769 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004770 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4771 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004772 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004773 case ISD::FDIV:
4774 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004775 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004776 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004777 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004778 case Expand: assert(0 && "not implemented");
4779 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4780 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004781 }
4782 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004783 case Expand: assert(0 && "not implemented");
4784 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4785 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004786 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004787 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4788
4789 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004790 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004791 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4792 DAG.getValueType(VT));
4793 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004794
4795 case ISD::UDIV:
4796 case ISD::UREM:
4797 // These operators require that their input be zero extended.
4798 Tmp1 = PromoteOp(Node->getOperand(0));
4799 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004800 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004801 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4802 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004803 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4804 break;
4805
4806 case ISD::SHL:
4807 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004808 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004809 break;
4810 case ISD::SRA:
4811 // The input value must be properly sign extended.
4812 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004813 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4814 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004815 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004816 break;
4817 case ISD::SRL:
4818 // The input value must be properly zero extended.
4819 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004820 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004821 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004822 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004823
4824 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004825 Tmp1 = Node->getOperand(0); // Get the chain.
4826 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004827 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4828 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004829 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004830 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004831 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004832 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004833 // Increment the pointer, VAList, to the next vaarg
4834 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004835 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004836 TLI.getPointerTy()));
4837 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004838 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004839 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004840 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004841 }
4842 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004843 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004844 break;
4845
Evan Cheng466685d2006-10-09 20:57:25 +00004846 case ISD::LOAD: {
4847 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004848 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4849 ? ISD::EXTLOAD : LD->getExtensionType();
4850 Result = DAG.getExtLoad(ExtType, NVT,
4851 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004852 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004853 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004854 LD->isVolatile(),
4855 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004856 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004857 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004858 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004859 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004860 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004861 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4862 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004863
Duncan Sands83ec4b62008-06-06 12:08:01 +00004864 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004865 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004866 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4867 // Ensure that the resulting node is at least the same size as the operands'
4868 // value types, because we cannot assume that TLI.getSetCCValueType() is
4869 // constant.
4870 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004871 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004872 }
Nate Begeman9373a812005-08-10 20:51:12 +00004873 case ISD::SELECT_CC:
4874 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4875 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4876 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004877 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004878 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004879 case ISD::BSWAP:
4880 Tmp1 = Node->getOperand(0);
4881 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4882 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4883 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004884 DAG.getConstant(NVT.getSizeInBits() -
4885 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004886 TLI.getShiftAmountTy()));
4887 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004888 case ISD::CTPOP:
4889 case ISD::CTTZ:
4890 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004891 // Zero extend the argument
4892 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004893 // Perform the larger operation, then subtract if needed.
4894 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004895 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004896 case ISD::CTPOP:
4897 Result = Tmp1;
4898 break;
4899 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004900 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Duncan Sands5480c042009-01-01 15:52:00 +00004901 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1.getValueType()), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004902 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004903 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004904 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004905 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004906 break;
4907 case ISD::CTLZ:
4908 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004909 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004910 DAG.getConstant(NVT.getSizeInBits() -
4911 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004912 break;
4913 }
4914 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004915 case ISD::EXTRACT_SUBVECTOR:
4916 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004917 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004918 case ISD::EXTRACT_VECTOR_ELT:
4919 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4920 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004921 }
4922
Gabor Greifba36cb52008-08-28 21:40:38 +00004923 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004924
4925 // Make sure the result is itself legal.
4926 Result = LegalizeOp(Result);
4927
4928 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004929 AddPromotedOperand(Op, Result);
4930 return Result;
4931}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004932
Dan Gohman7f321562007-06-25 16:23:39 +00004933/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4934/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4935/// based on the vector type. The return type of this matches the element type
4936/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004937SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004938 // We know that operand #0 is the Vec vector. If the index is a constant
4939 // or if the invec is a supported hardware type, we can use it. Otherwise,
4940 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004941 SDValue Vec = Op.getOperand(0);
4942 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004943
Duncan Sands83ec4b62008-06-06 12:08:01 +00004944 MVT TVT = Vec.getValueType();
4945 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004946
Dan Gohman7f321562007-06-25 16:23:39 +00004947 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4948 default: assert(0 && "This action is not supported yet!");
4949 case TargetLowering::Custom: {
4950 Vec = LegalizeOp(Vec);
4951 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004952 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004953 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004954 return Tmp3;
4955 break;
4956 }
4957 case TargetLowering::Legal:
4958 if (isTypeLegal(TVT)) {
4959 Vec = LegalizeOp(Vec);
4960 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004961 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004962 }
4963 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004964 case TargetLowering::Promote:
4965 assert(TVT.isVector() && "not vector type");
4966 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004967 case TargetLowering::Expand:
4968 break;
4969 }
4970
4971 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004972 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004973 Op = ScalarizeVectorOp(Vec);
4974 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004975 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004976 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004977 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00004978 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004979 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004980 Vec = Lo;
4981 } else {
4982 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004983 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004984 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004985 }
Dan Gohman7f321562007-06-25 16:23:39 +00004986
Chris Lattner15972212006-03-31 17:55:51 +00004987 // It's now an extract from the appropriate high or low part. Recurse.
4988 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004989 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004990 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004991 // Store the value to a temporary stack slot, then LOAD the scalar
4992 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00004993 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4994 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00004995
4996 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004997 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00004998 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4999 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005000
Duncan Sands8e4eb092008-06-08 20:54:56 +00005001 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00005002 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005003 else
Chris Lattnerf185e672007-10-19 16:47:35 +00005004 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005005
Dan Gohman7f321562007-06-25 16:23:39 +00005006 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5007
5008 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00005009 }
Dan Gohman7f321562007-06-25 16:23:39 +00005010 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00005011}
5012
Dan Gohman7f321562007-06-25 16:23:39 +00005013/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00005014/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005015SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00005016 // We know that operand #0 is the Vec vector. For now we assume the index
5017 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00005018 SDValue Vec = Op.getOperand(0);
5019 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00005020
Duncan Sands83ec4b62008-06-06 12:08:01 +00005021 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00005022
Duncan Sands83ec4b62008-06-06 12:08:01 +00005023 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00005024 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005025 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00005026 }
5027
5028 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005029 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00005030 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005031 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00005032 Vec = Lo;
5033 } else {
5034 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005035 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5036 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00005037 }
5038
5039 // It's now an extract from the appropriate high or low part. Recurse.
5040 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005041 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00005042}
5043
Nate Begeman750ac1b2006-02-01 07:19:44 +00005044/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5045/// with condition CC on the current target. This usually involves legalizing
5046/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5047/// there may be no choice but to create a new SetCC node to represent the
5048/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00005049/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5050void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5051 SDValue &RHS,
5052 SDValue &CC) {
5053 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005054
5055 switch (getTypeAction(LHS.getValueType())) {
5056 case Legal:
5057 Tmp1 = LegalizeOp(LHS); // LHS
5058 Tmp2 = LegalizeOp(RHS); // RHS
5059 break;
5060 case Promote:
5061 Tmp1 = PromoteOp(LHS); // LHS
5062 Tmp2 = PromoteOp(RHS); // RHS
5063
5064 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005065 if (LHS.getValueType().isInteger()) {
5066 MVT VT = LHS.getValueType();
5067 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005068
5069 // Otherwise, we have to insert explicit sign or zero extends. Note
5070 // that we could insert sign extends for ALL conditions, but zero extend
5071 // is cheaper on many machines (an AND instead of two shifts), so prefer
5072 // it.
5073 switch (cast<CondCodeSDNode>(CC)->get()) {
5074 default: assert(0 && "Unknown integer comparison!");
5075 case ISD::SETEQ:
5076 case ISD::SETNE:
5077 case ISD::SETUGE:
5078 case ISD::SETUGT:
5079 case ISD::SETULE:
5080 case ISD::SETULT:
5081 // ALL of these operations will work if we either sign or zero extend
5082 // the operands (including the unsigned comparisons!). Zero extend is
5083 // usually a simpler/cheaper operation, so prefer it.
5084 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5085 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5086 break;
5087 case ISD::SETGE:
5088 case ISD::SETGT:
5089 case ISD::SETLT:
5090 case ISD::SETLE:
5091 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5092 DAG.getValueType(VT));
5093 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5094 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00005095 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5096 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00005097 break;
5098 }
5099 }
5100 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005101 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005102 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00005103 if (VT == MVT::f32 || VT == MVT::f64) {
5104 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00005105 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00005106 switch (cast<CondCodeSDNode>(CC)->get()) {
5107 case ISD::SETEQ:
5108 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00005109 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005110 break;
5111 case ISD::SETNE:
5112 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00005113 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005114 break;
5115 case ISD::SETGE:
5116 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00005117 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005118 break;
5119 case ISD::SETLT:
5120 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005121 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005122 break;
5123 case ISD::SETLE:
5124 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005125 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005126 break;
5127 case ISD::SETGT:
5128 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005129 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005130 break;
5131 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005132 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5133 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005134 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005135 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005136 break;
5137 default:
Evan Cheng56966222007-01-12 02:11:51 +00005138 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005139 switch (cast<CondCodeSDNode>(CC)->get()) {
5140 case ISD::SETONE:
5141 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005142 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005143 // Fallthrough
5144 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005145 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005146 break;
5147 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005148 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005149 break;
5150 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005151 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005152 break;
5153 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005154 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005155 break;
Evan Cheng56966222007-01-12 02:11:51 +00005156 case ISD::SETUEQ:
5157 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005158 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005159 default: assert(0 && "Unsupported FP setcc!");
5160 }
5161 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005162
Dan Gohman475871a2008-07-27 21:46:04 +00005163 SDValue Dummy;
5164 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00005165 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005166 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005167 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005168 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005169 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Duncan Sands5480c042009-01-01 15:52:00 +00005170 Tmp1 = DAG.getNode(ISD::SETCC,
5171 TLI.getSetCCResultType(Tmp1.getValueType()),
5172 Tmp1, Tmp2, CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00005173 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005174 false /*sign irrelevant*/, Dummy);
Duncan Sands5480c042009-01-01 15:52:00 +00005175 Tmp2 = DAG.getNode(ISD::SETCC,
5176 TLI.getSetCCResultType(LHS.getValueType()), LHS,
5177 Tmp2, DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00005178 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005179 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005180 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005181 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005182 RHS = Tmp2;
5183 return;
5184 }
5185
Dan Gohman475871a2008-07-27 21:46:04 +00005186 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005187 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005188 ExpandOp(RHS, RHSLo, RHSHi);
5189 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5190
5191 if (VT==MVT::ppcf128) {
5192 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005193 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005194 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005195 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005196 // The following can be improved, but not that much.
Duncan Sands5480c042009-01-01 15:52:00 +00005197 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5198 LHSHi, RHSHi, ISD::SETOEQ);
5199 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5200 LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005201 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Duncan Sands5480c042009-01-01 15:52:00 +00005202 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5203 LHSHi, RHSHi, ISD::SETUNE);
5204 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5205 LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005206 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5207 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005208 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005209 break;
5210 }
5211
5212 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005213 case ISD::SETEQ:
5214 case ISD::SETNE:
5215 if (RHSLo == RHSHi)
5216 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5217 if (RHSCST->isAllOnesValue()) {
5218 // Comparison to -1.
5219 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5220 Tmp2 = RHSLo;
5221 break;
5222 }
5223
5224 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5225 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5226 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5227 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5228 break;
5229 default:
5230 // If this is a comparison of the sign bit, just look at the top part.
5231 // X > -1, x < 0
5232 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5233 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005234 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005235 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5236 CST->isAllOnesValue())) { // X > -1
5237 Tmp1 = LHSHi;
5238 Tmp2 = RHSHi;
5239 break;
5240 }
5241
5242 // FIXME: This generated code sucks.
5243 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005244 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005245 default: assert(0 && "Unknown integer setcc!");
5246 case ISD::SETLT:
5247 case ISD::SETULT: LowCC = ISD::SETULT; break;
5248 case ISD::SETGT:
5249 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5250 case ISD::SETLE:
5251 case ISD::SETULE: LowCC = ISD::SETULE; break;
5252 case ISD::SETGE:
5253 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5254 }
5255
5256 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5257 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5258 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5259
5260 // NOTE: on targets without efficient SELECT of bools, we can always use
5261 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005262 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Duncan Sands5480c042009-01-01 15:52:00 +00005263 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5264 LHSLo, RHSLo, LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005265 if (!Tmp1.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005266 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo.getValueType()),
5267 LHSLo, RHSLo, LowCC);
5268 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5269 LHSHi, RHSHi, CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005270 if (!Tmp2.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005271 Tmp2 = DAG.getNode(ISD::SETCC,
5272 TLI.getSetCCResultType(LHSHi.getValueType()),
5273 LHSHi, RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005274
Gabor Greifba36cb52008-08-28 21:40:38 +00005275 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5276 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005277 if ((Tmp1C && Tmp1C->isNullValue()) ||
5278 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005279 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5280 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005281 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005282 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5283 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5284 // low part is known false, returns high part.
5285 // For LE / GE, if high part is known false, ignore the low part.
5286 // For LT / GT, if high part is known true, ignore the low part.
5287 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005288 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005289 } else {
Duncan Sands5480c042009-01-01 15:52:00 +00005290 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5291 LHSHi, RHSHi, ISD::SETEQ, false,
5292 DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005293 if (!Result.getNode())
Duncan Sands5480c042009-01-01 15:52:00 +00005294 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi.getValueType()),
5295 LHSHi, RHSHi, ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005296 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5297 Result, Tmp1, Tmp2));
5298 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005299 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005300 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005301 }
5302 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005303 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005304 LHS = Tmp1;
5305 RHS = Tmp2;
5306}
5307
Evan Cheng7f042682008-10-15 02:05:31 +00005308/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5309/// condition code CC on the current target. This routine assumes LHS and rHS
5310/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5311/// illegal condition code into AND / OR of multiple SETCC values.
5312void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5313 SDValue &LHS, SDValue &RHS,
5314 SDValue &CC) {
5315 MVT OpVT = LHS.getValueType();
5316 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5317 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5318 default: assert(0 && "Unknown condition code action!");
5319 case TargetLowering::Legal:
5320 // Nothing to do.
5321 break;
5322 case TargetLowering::Expand: {
5323 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5324 unsigned Opc = 0;
5325 switch (CCCode) {
5326 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005327 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5328 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5329 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5330 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5331 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5332 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5333 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5334 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5335 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5336 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5337 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5338 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005339 // FIXME: Implement more expansions.
5340 }
5341
5342 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5343 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5344 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5345 RHS = SDValue();
5346 CC = SDValue();
5347 break;
5348 }
5349 }
5350}
5351
Chris Lattner1401d152008-01-16 07:45:30 +00005352/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5353/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5354/// a load from the stack slot to DestVT, extending it if needed.
5355/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005356SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5357 MVT SlotVT,
5358 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005359 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005360 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5361 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005362 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005363
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005364 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005365 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00005366
Duncan Sands83ec4b62008-06-06 12:08:01 +00005367 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5368 unsigned SlotSize = SlotVT.getSizeInBits();
5369 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005370 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5371 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005372
Chris Lattner1401d152008-01-16 07:45:30 +00005373 // Emit a store to the stack slot. Use a truncstore if the input value is
5374 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005375 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005376
Chris Lattner1401d152008-01-16 07:45:30 +00005377 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005378 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005379 PseudoSourceValue::getFixedStack(SPFI), 0,
5380 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005381 else {
5382 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005383 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005384 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00005385 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005386 }
5387
Chris Lattner35481892005-12-23 00:16:34 +00005388 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005389 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00005390 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005391
5392 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00005393 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5394 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005395}
5396
Dan Gohman475871a2008-07-27 21:46:04 +00005397SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005398 // Create a vector sized/aligned stack slot, store the value to element #0,
5399 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005400 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005401
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005402 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005403 int SPFI = StackPtrFI->getIndex();
5404
Dan Gohman475871a2008-07-27 21:46:04 +00005405 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005406 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005407 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005408 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005409}
5410
5411
Chris Lattnerce872152006-03-19 06:31:19 +00005412/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005413/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005414SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005415
5416 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005417 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005418 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005419 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005420 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005421
Dan Gohman475871a2008-07-27 21:46:04 +00005422 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005423 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005424 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005425 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005426 bool isConstant = true;
5427 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5428 SplatValue.getOpcode() != ISD::UNDEF)
5429 isConstant = false;
5430
Evan Cheng033e6812006-03-24 01:17:21 +00005431 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005432 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005433 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005434 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005435 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005436 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005437 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005438
5439 // If this isn't a constant element or an undef, we can't use a constant
5440 // pool load.
5441 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5442 V.getOpcode() != ISD::UNDEF)
5443 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005444 }
5445
5446 if (isOnlyLowElement) {
5447 // If the low element is an undef too, then this whole things is an undef.
5448 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5449 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5450 // Otherwise, turn this into a scalar_to_vector node.
5451 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5452 Node->getOperand(0));
5453 }
5454
Chris Lattner2eb86532006-03-24 07:29:17 +00005455 // If all elements are constants, create a load from the constant pool.
5456 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005457 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005458 std::vector<Constant*> CV;
5459 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5460 if (ConstantFPSDNode *V =
5461 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005462 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005463 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005464 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005465 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005466 } else {
5467 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005468 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005469 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005470 CV.push_back(UndefValue::get(OpNTy));
5471 }
5472 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005473 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005474 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005475 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005476 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005477 PseudoSourceValue::getConstantPool(), 0,
5478 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005479 }
5480
Gabor Greifba36cb52008-08-28 21:40:38 +00005481 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005482 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005483 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005484 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5485 std::vector<SDValue> ZeroVec(NumElems, Zero);
5486 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005487 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005488
5489 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005490 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005491 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005492 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005493 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5494
5495 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5496 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5497 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5498 SplatMask);
5499 }
5500 }
5501
Evan Cheng033e6812006-03-24 01:17:21 +00005502 // If there are only two unique elements, we may be able to turn this into a
5503 // vector shuffle.
5504 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005505 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005506 SDValue Val1 = Node->getOperand(1);
5507 SDValue Val2;
5508 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005509 if (MI->first != Val1)
5510 Val2 = MI->first;
5511 else
5512 Val2 = (++MI)->first;
5513
5514 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5515 // vector shuffle has the undef vector on the RHS.
5516 if (Val1.getOpcode() == ISD::UNDEF)
5517 std::swap(Val1, Val2);
5518
Evan Cheng033e6812006-03-24 01:17:21 +00005519 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005520 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5521 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005522 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005523
5524 // Set elements of the shuffle mask for Val1.
5525 std::vector<unsigned> &Val1Elts = Values[Val1];
5526 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5527 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5528
5529 // Set elements of the shuffle mask for Val2.
5530 std::vector<unsigned> &Val2Elts = Values[Val2];
5531 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5532 if (Val2.getOpcode() != ISD::UNDEF)
5533 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5534 else
5535 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5536
Dan Gohman475871a2008-07-27 21:46:04 +00005537 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005538 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005539
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005540 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005541 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5542 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005543 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5544 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005545 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005546
5547 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005548 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005549 }
5550 }
Chris Lattnerce872152006-03-19 06:31:19 +00005551
5552 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5553 // aligned object on the stack, store each element into it, then load
5554 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005555 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005556 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005557 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005558
5559 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005560 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005561 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005562 // Store (in the right endianness) the elements to memory.
5563 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5564 // Ignore undef elements.
5565 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5566
Chris Lattner841c8822006-03-22 01:46:54 +00005567 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005568
Dan Gohman475871a2008-07-27 21:46:04 +00005569 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005570 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5571
Evan Cheng786225a2006-10-05 23:01:46 +00005572 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005573 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005574 }
5575
Dan Gohman475871a2008-07-27 21:46:04 +00005576 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005577 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005578 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5579 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005580 else
5581 StoreChain = DAG.getEntryNode();
5582
5583 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005584 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005585}
5586
Chris Lattner5b359c62005-04-02 04:00:59 +00005587void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005588 SDValue Op, SDValue Amt,
5589 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005590 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005591 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005592 ExpandOp(Op, LHSL, LHSH);
5593
Dan Gohman475871a2008-07-27 21:46:04 +00005594 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005595 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005596 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005597 Hi = Lo.getValue(1);
5598}
5599
5600
Chris Lattnere34b3962005-01-19 04:19:40 +00005601/// ExpandShift - Try to find a clever way to expand this shift operation out to
5602/// smaller elements. If we can't find a way that is more efficient than a
5603/// libcall on this target, return false. Otherwise, return true with the
5604/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005605bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5606 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005607 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5608 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005609
Duncan Sands83ec4b62008-06-06 12:08:01 +00005610 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005611 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005612 MVT ShTy = ShAmt.getValueType();
5613 unsigned ShBits = ShTy.getSizeInBits();
5614 unsigned VTBits = Op.getValueType().getSizeInBits();
5615 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005616
Chris Lattner7a3c8552007-10-14 20:35:12 +00005617 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005618 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005619 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005620 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005621 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005622 ExpandOp(Op, InL, InH);
5623 switch(Opc) {
5624 case ISD::SHL:
5625 if (Cst > VTBits) {
5626 Lo = DAG.getConstant(0, NVT);
5627 Hi = DAG.getConstant(0, NVT);
5628 } else if (Cst > NVTBits) {
5629 Lo = DAG.getConstant(0, NVT);
5630 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005631 } else if (Cst == NVTBits) {
5632 Lo = DAG.getConstant(0, NVT);
5633 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005634 } else {
5635 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5636 Hi = DAG.getNode(ISD::OR, NVT,
5637 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5638 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5639 }
5640 return true;
5641 case ISD::SRL:
5642 if (Cst > VTBits) {
5643 Lo = DAG.getConstant(0, NVT);
5644 Hi = DAG.getConstant(0, NVT);
5645 } else if (Cst > NVTBits) {
5646 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5647 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005648 } else if (Cst == NVTBits) {
5649 Lo = InH;
5650 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005651 } else {
5652 Lo = DAG.getNode(ISD::OR, NVT,
5653 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5654 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5655 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5656 }
5657 return true;
5658 case ISD::SRA:
5659 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005660 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005661 DAG.getConstant(NVTBits-1, ShTy));
5662 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005663 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005664 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005665 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005666 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005667 } else if (Cst == NVTBits) {
5668 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005669 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005670 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005671 } else {
5672 Lo = DAG.getNode(ISD::OR, NVT,
5673 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5674 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5675 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5676 }
5677 return true;
5678 }
5679 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005680
5681 // Okay, the shift amount isn't constant. However, if we can tell that it is
5682 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005683 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5684 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005685 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005686
Dan Gohman9e255b72008-02-22 01:12:31 +00005687 // If we know that if any of the high bits of the shift amount are one, then
5688 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005689 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005690 // Mask out the high bit, which we know is set.
5691 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005692 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005693
5694 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005695 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005696 ExpandOp(Op, InL, InH);
5697 switch(Opc) {
5698 case ISD::SHL:
5699 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5700 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5701 return true;
5702 case ISD::SRL:
5703 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5704 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5705 return true;
5706 case ISD::SRA:
5707 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5708 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5709 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5710 return true;
5711 }
5712 }
5713
Dan Gohman9e255b72008-02-22 01:12:31 +00005714 // If we know that the high bits of the shift amount are all zero, then we can
5715 // do this as a couple of simple shifts.
5716 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005717 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005718 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005719 DAG.getConstant(NVTBits, Amt.getValueType()),
5720 Amt);
5721
5722 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005723 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005724 ExpandOp(Op, InL, InH);
5725 switch(Opc) {
5726 case ISD::SHL:
5727 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5728 Hi = DAG.getNode(ISD::OR, NVT,
5729 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5730 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5731 return true;
5732 case ISD::SRL:
5733 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5734 Lo = DAG.getNode(ISD::OR, NVT,
5735 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5736 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5737 return true;
5738 case ISD::SRA:
5739 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5740 Lo = DAG.getNode(ISD::OR, NVT,
5741 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5742 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5743 return true;
5744 }
5745 }
5746
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005747 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005748}
Chris Lattner77e77a62005-01-21 06:05:23 +00005749
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005750
Chris Lattner77e77a62005-01-21 06:05:23 +00005751// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5752// does not fit into a register, return the lo part and set the hi part to the
5753// by-reg argument. If it does fit into a single register, return the result
5754// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005755SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5756 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005757 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5758 // The input chain to this libcall is the entry node of the function.
5759 // Legalizing the call will automatically add the previous call to the
5760 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005761 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005762
Chris Lattner77e77a62005-01-21 06:05:23 +00005763 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005764 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005765 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005766 MVT ArgVT = Node->getOperand(i).getValueType();
5767 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005768 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005769 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005770 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005771 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005772 }
Bill Wendling056292f2008-09-16 21:48:12 +00005773 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005774 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005775
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005776 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005777 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005778 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005779 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5780 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005781
Chris Lattner6831a812006-02-13 09:18:02 +00005782 // Legalize the call sequence, starting with the chain. This will advance
5783 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5784 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5785 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005786 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005787 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005788 default: assert(0 && "Unknown thing");
5789 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005790 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005791 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005792 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005793 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005794 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005795 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005796 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005797}
5798
Dan Gohman7f8613e2008-08-14 20:04:46 +00005799/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5800///
5801SDValue SelectionDAGLegalize::
5802LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5803 bool isCustom = false;
5804 SDValue Tmp1;
5805 switch (getTypeAction(Op.getValueType())) {
5806 case Legal:
5807 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5808 Op.getValueType())) {
5809 default: assert(0 && "Unknown operation action!");
5810 case TargetLowering::Custom:
5811 isCustom = true;
5812 // FALLTHROUGH
5813 case TargetLowering::Legal:
5814 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005815 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005816 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5817 else
5818 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5819 DestTy, Tmp1);
5820 if (isCustom) {
5821 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005822 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005823 }
5824 break;
5825 case TargetLowering::Expand:
5826 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5827 break;
5828 case TargetLowering::Promote:
5829 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5830 break;
5831 }
5832 break;
5833 case Expand:
5834 Result = ExpandIntToFP(isSigned, DestTy, Op);
5835 break;
5836 case Promote:
5837 Tmp1 = PromoteOp(Op);
5838 if (isSigned) {
5839 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5840 Tmp1, DAG.getValueType(Op.getValueType()));
5841 } else {
5842 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5843 Op.getValueType());
5844 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005845 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005846 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5847 else
5848 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5849 DestTy, Tmp1);
5850 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5851 break;
5852 }
5853 return Result;
5854}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005855
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005856/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5857///
Dan Gohman475871a2008-07-27 21:46:04 +00005858SDValue SelectionDAGLegalize::
5859ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005860 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005861 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005862
Dan Gohman7f8613e2008-08-14 20:04:46 +00005863 // Expand unsupported int-to-fp vector casts by unrolling them.
5864 if (DestTy.isVector()) {
5865 if (!ExpandSource)
5866 return LegalizeOp(UnrollVectorOp(Source));
5867 MVT DestEltTy = DestTy.getVectorElementType();
5868 if (DestTy.getVectorNumElements() == 1) {
5869 SDValue Scalar = ScalarizeVectorOp(Source);
5870 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5871 DestEltTy, Scalar);
5872 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5873 }
5874 SDValue Lo, Hi;
5875 SplitVectorOp(Source, Lo, Hi);
5876 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5877 DestTy.getVectorNumElements() / 2);
5878 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5879 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengefa53392008-10-13 18:46:18 +00005880 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5881 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005882 }
5883
Evan Cheng9845eb52008-04-01 02:18:22 +00005884 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5885 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005886 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005887 // incoming integer is set. To handle this, we dynamically test to see if
5888 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005889 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005890 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005891 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005892 ExpandOp(Source, Lo, Hi);
5893 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5894 } else {
5895 // The comparison for the sign bit will use the entire operand.
5896 Hi = Source;
5897 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005898
Dale Johannesen53997b02008-11-04 20:52:49 +00005899 // Check to see if the target has a custom way to lower this. If so, use
5900 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005901 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5902 default: assert(0 && "This action not implemented for this operation!");
5903 case TargetLowering::Legal:
5904 case TargetLowering::Expand:
5905 break; // This case is handled below.
5906 case TargetLowering::Custom: {
5907 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5908 Source), DAG);
5909 if (NV.getNode())
5910 return LegalizeOp(NV);
5911 break; // The target decided this was legal after all
5912 }
5913 }
5914
Chris Lattner66de05b2005-05-13 04:45:13 +00005915 // If this is unsigned, and not supported, first perform the conversion to
5916 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005917 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005918
Duncan Sands5480c042009-01-01 15:52:00 +00005919 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi.getValueType()),
5920 Hi, DAG.getConstant(0, Hi.getValueType()),
5921 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005922 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5923 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005924 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005925 uint64_t FF = 0x5f800000ULL;
5926 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005927 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005928
Dan Gohman475871a2008-07-27 21:46:04 +00005929 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005930 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere9c35e72005-04-13 05:09:42 +00005931 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005932 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005933 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005934 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005935 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005936 PseudoSourceValue::getConstantPool(), 0,
5937 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005938 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005939 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005940 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005941 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005942 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005943 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005944 else
5945 assert(0 && "Unexpected conversion");
5946
Duncan Sands83ec4b62008-06-06 12:08:01 +00005947 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005948 if (SCVT != DestTy) {
5949 // Destination type needs to be expanded as well. The FADD now we are
5950 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005951 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5952 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005953 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005954 SignedConv, SignedConv.getValue(1));
5955 }
5956 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5957 }
Chris Lattner473a9902005-09-29 06:44:39 +00005958 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005959 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005960
Chris Lattnera88a2602005-05-14 05:33:54 +00005961 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005962 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005963 default: assert(0 && "This action not implemented for this operation!");
5964 case TargetLowering::Legal:
5965 case TargetLowering::Expand:
5966 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005967 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005968 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005969 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005970 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005971 return LegalizeOp(NV);
5972 break; // The target decided this was legal after all
5973 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005974 }
5975
Chris Lattner13689e22005-05-12 07:00:44 +00005976 // Expand the source, then glue it back together for the call. We must expand
5977 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005978 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005979 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005980 ExpandOp(Source, SrcLo, SrcHi);
5981 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5982 }
Chris Lattner13689e22005-05-12 07:00:44 +00005983
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005984 RTLIB::Libcall LC = isSigned ?
5985 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5986 RTLIB::getUINTTOFP(SourceVT, DestTy);
5987 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5988
Chris Lattner6831a812006-02-13 09:18:02 +00005989 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00005990 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00005991 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5992 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmana2e94852008-03-10 23:03:31 +00005993 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5994 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005995}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005996
Chris Lattner22cde6a2006-01-28 08:25:58 +00005997/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5998/// INT_TO_FP operation of the specified operand when the target requests that
5999/// we expand it. At this point, we know that the result and operand types are
6000/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00006001SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6002 SDValue Op0,
6003 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006004 if (Op0.getValueType() == MVT::i32) {
6005 // simple 32-bit [signed|unsigned] integer to float/double expansion
6006
Chris Lattner23594d42008-01-16 07:03:22 +00006007 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00006008 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00006009
Chris Lattner22cde6a2006-01-28 08:25:58 +00006010 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00006011 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00006012 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00006013 SDValue Hi = StackSlot;
6014 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00006015 if (TLI.isLittleEndian())
6016 std::swap(Hi, Lo);
6017
Chris Lattner22cde6a2006-01-28 08:25:58 +00006018 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00006019 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006020 if (isSigned) {
6021 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00006022 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006023 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6024 } else {
6025 Op0Mapped = Op0;
6026 }
6027 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00006028 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00006029 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006030 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006031 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006032 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00006033 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006034 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006035 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006036 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00006037 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00006038 BitsToDouble(0x4330000080000000ULL)
6039 : BitsToDouble(0x4330000000000000ULL),
6040 MVT::f64);
6041 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00006042 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006043 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00006044 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006045 // handle final rounding
6046 if (DestVT == MVT::f64) {
6047 // do nothing
6048 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006049 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00006050 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6051 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00006052 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00006053 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006054 }
6055 return Result;
6056 }
6057 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00006058 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006059
Duncan Sands5480c042009-01-01 15:52:00 +00006060 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0.getValueType()),
6061 Op0, DAG.getConstant(0, Op0.getValueType()),
6062 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006063 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6064 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006065 SignSet, Four, Zero);
6066
6067 // If the sign bit of the integer is set, the large number will be treated
6068 // as a negative number. To counteract this, the dynamic code adds an
6069 // offset depending on the data type.
6070 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006071 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006072 default: assert(0 && "Unsupported integer type!");
6073 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6074 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6075 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6076 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6077 }
6078 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00006079 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006080
Dan Gohman475871a2008-07-27 21:46:04 +00006081 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006082 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006083 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006084 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006085 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006086 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00006087 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006088 PseudoSourceValue::getConstantPool(), 0,
6089 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006090 else {
Dan Gohman69de1932008-02-06 22:27:42 +00006091 FudgeInReg =
6092 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6093 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006094 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006095 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006096 }
6097
6098 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6099}
6100
6101/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6102/// *INT_TO_FP operation of the specified operand when the target requests that
6103/// we promote it. At this point, we know that the result and operand types are
6104/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6105/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00006106SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6107 MVT DestVT,
6108 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006109 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006110 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006111
6112 unsigned OpToUse = 0;
6113
6114 // Scan for the appropriate larger type to use.
6115 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006116 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6117 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006118
6119 // If the target supports SINT_TO_FP of this type, use it.
6120 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6121 default: break;
6122 case TargetLowering::Legal:
6123 if (!TLI.isTypeLegal(NewInTy))
6124 break; // Can't use this datatype.
6125 // FALL THROUGH.
6126 case TargetLowering::Custom:
6127 OpToUse = ISD::SINT_TO_FP;
6128 break;
6129 }
6130 if (OpToUse) break;
6131 if (isSigned) continue;
6132
6133 // If the target supports UINT_TO_FP of this type, use it.
6134 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6135 default: break;
6136 case TargetLowering::Legal:
6137 if (!TLI.isTypeLegal(NewInTy))
6138 break; // Can't use this datatype.
6139 // FALL THROUGH.
6140 case TargetLowering::Custom:
6141 OpToUse = ISD::UINT_TO_FP;
6142 break;
6143 }
6144 if (OpToUse) break;
6145
6146 // Otherwise, try a larger type.
6147 }
6148
6149 // Okay, we found the operation and type to use. Zero extend our input to the
6150 // desired type then run the operation on it.
6151 return DAG.getNode(OpToUse, DestVT,
6152 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6153 NewInTy, LegalOp));
6154}
6155
6156/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6157/// FP_TO_*INT operation of the specified operand when the target requests that
6158/// we promote it. At this point, we know that the result and operand types are
6159/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6160/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006161SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6162 MVT DestVT,
6163 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006164 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006165 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006166
6167 unsigned OpToUse = 0;
6168
6169 // Scan for the appropriate larger type to use.
6170 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006171 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6172 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006173
6174 // If the target supports FP_TO_SINT returning this type, use it.
6175 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6176 default: break;
6177 case TargetLowering::Legal:
6178 if (!TLI.isTypeLegal(NewOutTy))
6179 break; // Can't use this datatype.
6180 // FALL THROUGH.
6181 case TargetLowering::Custom:
6182 OpToUse = ISD::FP_TO_SINT;
6183 break;
6184 }
6185 if (OpToUse) break;
6186
6187 // If the target supports FP_TO_UINT of this type, use it.
6188 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6189 default: break;
6190 case TargetLowering::Legal:
6191 if (!TLI.isTypeLegal(NewOutTy))
6192 break; // Can't use this datatype.
6193 // FALL THROUGH.
6194 case TargetLowering::Custom:
6195 OpToUse = ISD::FP_TO_UINT;
6196 break;
6197 }
6198 if (OpToUse) break;
6199
6200 // Otherwise, try a larger type.
6201 }
6202
Chris Lattner27a6c732007-11-24 07:07:01 +00006203
6204 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00006205 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006206
Chris Lattner27a6c732007-11-24 07:07:01 +00006207 // If the operation produces an invalid type, it must be custom lowered. Use
6208 // the target lowering hooks to expand it. Just keep the low part of the
6209 // expanded operation, we know that we're truncating anyway.
6210 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00006211 SmallVector<SDValue, 2> Results;
6212 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6213 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6214 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00006215 }
Duncan Sands126d9072008-07-04 11:47:58 +00006216
Chris Lattner27a6c732007-11-24 07:07:01 +00006217 // Truncate the result of the extended FP_TO_*INT operation to the desired
6218 // size.
6219 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006220}
6221
6222/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6223///
Dan Gohman475871a2008-07-27 21:46:04 +00006224SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006225 MVT VT = Op.getValueType();
6226 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006227 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006228 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006229 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6230 case MVT::i16:
6231 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6232 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6233 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6234 case MVT::i32:
6235 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6236 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6237 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6238 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6239 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6240 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6241 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6242 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6243 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6244 case MVT::i64:
6245 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6246 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6247 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6248 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6249 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6250 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6251 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6252 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6253 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6254 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6255 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6256 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6257 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6258 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6259 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6260 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6261 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6262 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6263 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6264 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6265 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6266 }
6267}
6268
6269/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6270///
Dan Gohman475871a2008-07-27 21:46:04 +00006271SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006272 switch (Opc) {
6273 default: assert(0 && "Cannot expand this yet!");
6274 case ISD::CTPOP: {
6275 static const uint64_t mask[6] = {
6276 0x5555555555555555ULL, 0x3333333333333333ULL,
6277 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6278 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6279 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006280 MVT VT = Op.getValueType();
6281 MVT ShVT = TLI.getShiftAmountTy();
6282 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006283 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6284 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman475871a2008-07-27 21:46:04 +00006285 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6286 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006287 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6288 DAG.getNode(ISD::AND, VT,
6289 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6290 }
6291 return Op;
6292 }
6293 case ISD::CTLZ: {
6294 // for now, we do this:
6295 // x = x | (x >> 1);
6296 // x = x | (x >> 2);
6297 // ...
6298 // x = x | (x >>16);
6299 // x = x | (x >>32); // for 64-bit input
6300 // return popcount(~x);
6301 //
6302 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006303 MVT VT = Op.getValueType();
6304 MVT ShVT = TLI.getShiftAmountTy();
6305 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006306 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006307 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006308 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6309 }
6310 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6311 return DAG.getNode(ISD::CTPOP, VT, Op);
6312 }
6313 case ISD::CTTZ: {
6314 // for now, we use: { return popcount(~x & (x - 1)); }
6315 // unless the target has ctlz but not ctpop, in which case we use:
6316 // { return 32 - nlz(~x & (x-1)); }
6317 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006318 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00006319 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6320 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006321 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6322 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6323 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6324 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6325 TLI.isOperationLegal(ISD::CTLZ, VT))
6326 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006327 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006328 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6329 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6330 }
6331 }
6332}
Chris Lattnere34b3962005-01-19 04:19:40 +00006333
Dan Gohman475871a2008-07-27 21:46:04 +00006334/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006335/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006336/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006337/// ExpandedNodes map is filled in for any results that are expanded, and the
6338/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006339void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006340 MVT VT = Op.getValueType();
6341 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006342 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006343 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006344 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006345 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006346
Chris Lattner6fdcb252005-09-02 20:32:45 +00006347 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006348 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006349 = ExpandedNodes.find(Op);
6350 if (I != ExpandedNodes.end()) {
6351 Lo = I->second.first;
6352 Hi = I->second.second;
6353 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006354 }
6355
Chris Lattner3e928bb2005-01-07 07:47:09 +00006356 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006357 case ISD::CopyFromReg:
6358 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006359 case ISD::FP_ROUND_INREG:
6360 if (VT == MVT::ppcf128 &&
6361 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6362 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006363 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006364 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6365 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006366 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006367 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006368 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6369 Lo = Result.getNode()->getOperand(0);
6370 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006371 break;
6372 }
6373 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006374 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006375#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006376 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006377#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006378 assert(0 && "Do not know how to expand this operator!");
6379 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006380 case ISD::EXTRACT_ELEMENT:
6381 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006382 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006383 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006384 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006385 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006386 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6387 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6388 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006389 case ISD::UNDEF:
6390 Lo = DAG.getNode(ISD::UNDEF, NVT);
6391 Hi = DAG.getNode(ISD::UNDEF, NVT);
6392 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006393 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006394 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006395 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6396 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6397 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006398 break;
6399 }
Evan Cheng00495212006-12-12 21:32:44 +00006400 case ISD::ConstantFP: {
6401 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006402 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006403 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006404 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6405 MVT::f64);
6406 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6407 MVT::f64);
6408 break;
6409 }
Evan Cheng279101e2006-12-12 22:19:28 +00006410 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006411 if (getTypeAction(Lo.getValueType()) == Expand)
6412 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006413 break;
6414 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006415 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006416 // Return the operands.
6417 Lo = Node->getOperand(0);
6418 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006419 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006420
6421 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006422 if (Node->getNumValues() == 1) {
6423 ExpandOp(Op.getOperand(0), Lo, Hi);
6424 break;
6425 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006426 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006427 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006428 Op.getValue(1).getValueType() == MVT::Other &&
6429 "unhandled MERGE_VALUES");
6430 ExpandOp(Op.getOperand(0), Lo, Hi);
6431 // Remember that we legalized the chain.
6432 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6433 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006434
6435 case ISD::SIGN_EXTEND_INREG:
6436 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006437 // sext_inreg the low part if needed.
6438 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6439
6440 // The high part gets the sign extension from the lo-part. This handles
6441 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006442 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006443 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006444 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006445 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006446
Nate Begemand88fc032006-01-14 03:14:10 +00006447 case ISD::BSWAP: {
6448 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006449 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006450 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6451 Lo = TempLo;
6452 break;
6453 }
6454
Chris Lattneredb1add2005-05-11 04:51:16 +00006455 case ISD::CTPOP:
6456 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006457 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6458 DAG.getNode(ISD::CTPOP, NVT, Lo),
6459 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006460 Hi = DAG.getConstant(0, NVT);
6461 break;
6462
Chris Lattner39a8f332005-05-12 19:05:01 +00006463 case ISD::CTLZ: {
6464 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006465 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006466 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6467 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Duncan Sands5480c042009-01-01 15:52:00 +00006468 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), HLZ, BitsC,
6469 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006470 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006471 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6472
6473 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6474 Hi = DAG.getConstant(0, NVT);
6475 break;
6476 }
6477
6478 case ISD::CTTZ: {
6479 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006480 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006481 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6482 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Duncan Sands5480c042009-01-01 15:52:00 +00006483 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(NVT), LTZ, BitsC,
6484 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006485 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006486 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6487
6488 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6489 Hi = DAG.getConstant(0, NVT);
6490 break;
6491 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006492
Nate Begemanacc398c2006-01-25 18:21:52 +00006493 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006494 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6495 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006496 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6497 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6498
6499 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006500 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006501 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006502 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006503 std::swap(Lo, Hi);
6504 break;
6505 }
6506
Chris Lattner3e928bb2005-01-07 07:47:09 +00006507 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006508 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006509 SDValue Ch = LD->getChain(); // Legalize the chain.
6510 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006511 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006512 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006513 int SVOffset = LD->getSrcValueOffset();
6514 unsigned Alignment = LD->getAlignment();
6515 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006516
Evan Cheng466685d2006-10-09 20:57:25 +00006517 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006518 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006519 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006520 if (VT == MVT::f32 || VT == MVT::f64) {
6521 // f32->i32 or f64->i64 one to one expansion.
6522 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006523 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006524 // Recursively expand the new load.
6525 if (getTypeAction(NVT) == Expand)
6526 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006527 break;
6528 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006529
Evan Cheng466685d2006-10-09 20:57:25 +00006530 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006531 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006532 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006533 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006534 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006535 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006536 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006537 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006538
Evan Cheng466685d2006-10-09 20:57:25 +00006539 // Build a factor node to remember that this load is independent of the
6540 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006541 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006542 Hi.getValue(1));
6543
6544 // Remember that we legalized the chain.
6545 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006546 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006547 std::swap(Lo, Hi);
6548 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006549 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006550
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006551 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6552 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006553 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006554 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006555 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006556 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006557 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006558 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6559 break;
6560 }
Evan Cheng466685d2006-10-09 20:57:25 +00006561
6562 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006563 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006564 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006565 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006566 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006567 SVOffset, EVT, isVolatile,
6568 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006569
6570 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006571 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006572
6573 if (ExtType == ISD::SEXTLOAD) {
6574 // The high part is obtained by SRA'ing all but one of the bits of the
6575 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006576 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006577 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6578 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6579 } else if (ExtType == ISD::ZEXTLOAD) {
6580 // The high part is just a zero.
6581 Hi = DAG.getConstant(0, NVT);
6582 } else /* if (ExtType == ISD::EXTLOAD) */ {
6583 // The high part is undefined.
6584 Hi = DAG.getNode(ISD::UNDEF, NVT);
6585 }
6586 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006587 break;
6588 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006589 case ISD::AND:
6590 case ISD::OR:
6591 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006592 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006593 ExpandOp(Node->getOperand(0), LL, LH);
6594 ExpandOp(Node->getOperand(1), RL, RH);
6595 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6596 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6597 break;
6598 }
6599 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006600 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006601 ExpandOp(Node->getOperand(1), LL, LH);
6602 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006603 if (getTypeAction(NVT) == Expand)
6604 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006605 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006606 if (VT != MVT::f32)
6607 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006608 break;
6609 }
Nate Begeman9373a812005-08-10 20:51:12 +00006610 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006611 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006612 ExpandOp(Node->getOperand(2), TL, TH);
6613 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006614 if (getTypeAction(NVT) == Expand)
6615 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006616 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6617 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006618 if (VT != MVT::f32)
6619 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6620 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006621 break;
6622 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006623 case ISD::ANY_EXTEND:
6624 // The low part is any extension of the input (which degenerates to a copy).
6625 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6626 // The high part is undefined.
6627 Hi = DAG.getNode(ISD::UNDEF, NVT);
6628 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006629 case ISD::SIGN_EXTEND: {
6630 // The low part is just a sign extension of the input (which degenerates to
6631 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006632 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006633
Chris Lattner3e928bb2005-01-07 07:47:09 +00006634 // The high part is obtained by SRA'ing all but one of the bits of the lo
6635 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006636 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006637 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6638 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006639 break;
6640 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006641 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006642 // The low part is just a zero extension of the input (which degenerates to
6643 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006644 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006645
Chris Lattner3e928bb2005-01-07 07:47:09 +00006646 // The high part is just a zero.
6647 Hi = DAG.getConstant(0, NVT);
6648 break;
Chris Lattner35481892005-12-23 00:16:34 +00006649
Chris Lattner4c948eb2007-02-13 23:55:16 +00006650 case ISD::TRUNCATE: {
6651 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006652 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006653 ExpandOp(Node->getOperand(0), NewLo, Hi);
6654
6655 // The low part is now either the right size, or it is closer. If not the
6656 // right size, make an illegal truncate so we recursively expand it.
6657 if (NewLo.getValueType() != Node->getValueType(0))
6658 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6659 ExpandOp(NewLo, Lo, Hi);
6660 break;
6661 }
6662
Chris Lattner35481892005-12-23 00:16:34 +00006663 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006664 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006665 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6666 // If the target wants to, allow it to lower this itself.
6667 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6668 case Expand: assert(0 && "cannot expand FP!");
6669 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6670 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6671 }
6672 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6673 }
6674
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006675 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006676 if (VT == MVT::f32 || VT == MVT::f64) {
6677 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006678 if (getTypeAction(NVT) == Expand)
6679 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006680 break;
6681 }
6682
6683 // If source operand will be expanded to the same type as VT, i.e.
6684 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006685 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006686 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6687 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006688 break;
6689 }
6690
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006691 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006692 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006693 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006694
Chris Lattner35481892005-12-23 00:16:34 +00006695 ExpandOp(Tmp, Lo, Hi);
6696 break;
6697 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006698
Chris Lattner27a6c732007-11-24 07:07:01 +00006699 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006700 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6701 TargetLowering::Custom &&
6702 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006703 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006704 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006705 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006706 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006707 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006708 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006709 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006710
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006711 case ISD::ATOMIC_CMP_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006712 // This operation does not need a loop.
6713 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6714 assert(Tmp.getNode() && "Node must be custom expanded!");
6715 ExpandOp(Tmp.getValue(0), Lo, Hi);
6716 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6717 LegalizeOp(Tmp.getValue(1)));
6718 break;
6719 }
6720
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006721 case ISD::ATOMIC_LOAD_ADD:
6722 case ISD::ATOMIC_LOAD_SUB:
6723 case ISD::ATOMIC_LOAD_AND:
6724 case ISD::ATOMIC_LOAD_OR:
6725 case ISD::ATOMIC_LOAD_XOR:
6726 case ISD::ATOMIC_LOAD_NAND:
6727 case ISD::ATOMIC_SWAP: {
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006728 // These operations require a loop to be generated. We can't do that yet,
6729 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006730 SDValue In2Lo, In2Hi, In2;
6731 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6732 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006733 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6734 SDValue Replace =
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006735 DAG.getAtomic(Op.getOpcode(), Anode->getMemoryVT(),
6736 Op.getOperand(0), Op.getOperand(1), In2,
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006737 Anode->getSrcValue(), Anode->getAlignment());
6738 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006739 ExpandOp(Result.getValue(0), Lo, Hi);
6740 // Remember that we legalized the chain.
6741 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006742 break;
6743 }
6744
Chris Lattner4e6c7462005-01-08 19:27:05 +00006745 // These operators cannot be expanded directly, emit them as calls to
6746 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006747 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006748 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006749 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006750 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6751 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006752 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6753 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006754 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006755
Chris Lattnerf20d1832005-07-30 01:40:57 +00006756 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6757
Chris Lattner80a3e942005-07-29 00:33:32 +00006758 // Now that the custom expander is done, expand the result, which is still
6759 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006760 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006761 ExpandOp(Op, Lo, Hi);
6762 break;
6763 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006764 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006765
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006766 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6767 VT);
6768 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6769 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006770 break;
Evan Cheng56966222007-01-12 02:11:51 +00006771 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006772
Evan Cheng56966222007-01-12 02:11:51 +00006773 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006774 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006775 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006776 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6777 case Expand: assert(0 && "cannot expand FP!");
6778 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6779 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6780 }
6781
6782 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6783
6784 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006785 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006786 ExpandOp(Op, Lo, Hi);
6787 break;
6788 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006789 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006790
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006791 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6792 VT);
6793 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6794 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006795 break;
Evan Cheng56966222007-01-12 02:11:51 +00006796 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006797
Evan Cheng05a2d562006-01-09 18:31:59 +00006798 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006799 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006800 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006801 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006802 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006803 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006804 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006805 // Now that the custom expander is done, expand the result, which is
6806 // still VT.
6807 ExpandOp(Op, Lo, Hi);
6808 break;
6809 }
6810 }
6811
Chris Lattner79980b02006-09-13 03:50:39 +00006812 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6813 // this X << 1 as X+X.
6814 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006815 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006816 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006817 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006818 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6819 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6820 LoOps[1] = LoOps[0];
6821 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6822
6823 HiOps[1] = HiOps[0];
6824 HiOps[2] = Lo.getValue(1);
6825 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6826 break;
6827 }
6828 }
6829
Chris Lattnere34b3962005-01-19 04:19:40 +00006830 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006831 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006832 break;
Chris Lattner47599822005-04-02 03:38:53 +00006833
6834 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006835 TargetLowering::LegalizeAction Action =
6836 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6837 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6838 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006839 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006840 break;
6841 }
6842
Chris Lattnere34b3962005-01-19 04:19:40 +00006843 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006844 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006845 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006846 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006847
Evan Cheng05a2d562006-01-09 18:31:59 +00006848 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006849 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006850 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006851 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006852 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006853 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006854 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006855 // Now that the custom expander is done, expand the result, which is
6856 // still VT.
6857 ExpandOp(Op, Lo, Hi);
6858 break;
6859 }
6860 }
6861
Chris Lattnere34b3962005-01-19 04:19:40 +00006862 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006863 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006864 break;
Chris Lattner47599822005-04-02 03:38:53 +00006865
6866 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006867 TargetLowering::LegalizeAction Action =
6868 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6869 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6870 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006871 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006872 break;
6873 }
6874
Chris Lattnere34b3962005-01-19 04:19:40 +00006875 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006876 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006877 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006878 }
6879
6880 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006881 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006882 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006883 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006884 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006885 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006886 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006887 // Now that the custom expander is done, expand the result, which is
6888 // still VT.
6889 ExpandOp(Op, Lo, Hi);
6890 break;
6891 }
6892 }
6893
Chris Lattnere34b3962005-01-19 04:19:40 +00006894 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006895 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006896 break;
Chris Lattner47599822005-04-02 03:38:53 +00006897
6898 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006899 TargetLowering::LegalizeAction Action =
6900 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6901 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6902 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006903 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006904 break;
6905 }
6906
Chris Lattnere34b3962005-01-19 04:19:40 +00006907 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006908 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006909 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006910 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006911
Misha Brukmanedf128a2005-04-21 22:36:52 +00006912 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006913 case ISD::SUB: {
6914 // If the target wants to custom expand this, let them.
6915 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6916 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006917 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006918 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006919 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006920 break;
6921 }
6922 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006923 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006924 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006925 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6926 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00006927 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006928 LoOps[0] = LHSL;
6929 LoOps[1] = RHSL;
6930 HiOps[0] = LHSH;
6931 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006932
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006933 //cascaded check to see if any smaller size has a a carry flag.
6934 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6935 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006936 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6937 MVT AVT = MVT::getIntegerVT(BitSize);
6938 if (TLI.isOperationLegal(OpV, AVT)) {
6939 hasCarry = true;
6940 break;
6941 }
6942 }
6943
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006944 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00006945 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006946 if (Node->getOpcode() == ISD::ADD) {
6947 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6948 HiOps[2] = Lo.getValue(1);
6949 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6950 } else {
6951 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6952 HiOps[2] = Lo.getValue(1);
6953 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6954 }
6955 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006956 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006957 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng637ed032008-12-12 18:49:09 +00006958 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6959 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Duncan Sands5480c042009-01-01 15:52:00 +00006960 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006961 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006962 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6963 DAG.getConstant(1, NVT),
6964 DAG.getConstant(0, NVT));
Duncan Sands5480c042009-01-01 15:52:00 +00006965 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(NVT),
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006966 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006967 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6968 DAG.getConstant(1, NVT),
6969 Carry1);
6970 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6971 } else {
Evan Cheng637ed032008-12-12 18:49:09 +00006972 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
6973 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006974 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6975 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6976 DAG.getConstant(1, NVT),
6977 DAG.getConstant(0, NVT));
6978 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6979 }
6980 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006981 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006982 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006983
6984 case ISD::ADDC:
6985 case ISD::SUBC: {
6986 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006987 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006988 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6989 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6990 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006991 SDValue LoOps[2] = { LHSL, RHSL };
6992 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006993
6994 if (Node->getOpcode() == ISD::ADDC) {
6995 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6996 HiOps[2] = Lo.getValue(1);
6997 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6998 } else {
6999 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7000 HiOps[2] = Lo.getValue(1);
7001 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7002 }
7003 // Remember that we legalized the flag.
7004 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7005 break;
7006 }
7007 case ISD::ADDE:
7008 case ISD::SUBE: {
7009 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007010 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007011 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7012 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7013 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007014 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7015 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007016
7017 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7018 HiOps[2] = Lo.getValue(1);
7019 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7020
7021 // Remember that we legalized the flag.
7022 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7023 break;
7024 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007025 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007026 // If the target wants to custom expand this, let them.
7027 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007028 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007029 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00007030 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007031 break;
7032 }
7033 }
7034
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007035 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
7036 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00007037 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
7038 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
7039 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00007040 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00007041 ExpandOp(Node->getOperand(0), LL, LH);
7042 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007043 unsigned OuterBitSize = Op.getValueSizeInBits();
7044 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00007045 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7046 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00007047 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7048 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7049 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00007050 // The inputs are both zero-extended.
7051 if (HasUMUL_LOHI) {
7052 // We can emit a umul_lohi.
7053 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007054 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007055 break;
7056 }
7057 if (HasMULHU) {
7058 // We can emit a mulhu+mul.
7059 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7060 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7061 break;
7062 }
Dan Gohman525178c2007-10-08 18:33:35 +00007063 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007064 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00007065 // The input values are both sign-extended.
7066 if (HasSMUL_LOHI) {
7067 // We can emit a smul_lohi.
7068 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007069 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007070 break;
7071 }
7072 if (HasMULHS) {
7073 // We can emit a mulhs+mul.
7074 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7075 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7076 break;
7077 }
7078 }
7079 if (HasUMUL_LOHI) {
7080 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00007081 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00007082 DAG.getVTList(NVT, NVT), LL, RL);
7083 Lo = UMulLOHI;
7084 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00007085 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7086 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7087 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7088 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00007089 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00007090 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007091 if (HasMULHU) {
7092 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7093 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7094 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7095 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7096 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7097 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7098 break;
7099 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007100 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007101
Dan Gohman525178c2007-10-08 18:33:35 +00007102 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007103 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00007104 break;
7105 }
Evan Cheng56966222007-01-12 02:11:51 +00007106 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007107 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007108 break;
7109 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007110 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007111 break;
7112 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007113 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007114 break;
7115 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007116 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007117 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00007118
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007119 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00007120 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7121 RTLIB::ADD_F64,
7122 RTLIB::ADD_F80,
7123 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007124 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007125 break;
7126 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00007127 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7128 RTLIB::SUB_F64,
7129 RTLIB::SUB_F80,
7130 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007131 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007132 break;
7133 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007134 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7135 RTLIB::MUL_F64,
7136 RTLIB::MUL_F80,
7137 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007138 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007139 break;
7140 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007141 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7142 RTLIB::DIV_F64,
7143 RTLIB::DIV_F80,
7144 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007145 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007146 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007147 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007148 if (VT == MVT::ppcf128) {
7149 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7150 Node->getOperand(0).getValueType()==MVT::f64);
7151 const uint64_t zero = 0;
7152 if (Node->getOperand(0).getValueType()==MVT::f32)
7153 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7154 else
7155 Hi = Node->getOperand(0);
7156 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7157 break;
7158 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007159 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7160 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7161 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007162 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007163 }
7164 case ISD::FP_ROUND: {
7165 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7166 VT);
7167 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7168 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007169 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007170 }
Evan Cheng4b887022008-09-09 23:02:14 +00007171 case ISD::FSQRT:
7172 case ISD::FSIN:
7173 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007174 case ISD::FLOG:
7175 case ISD::FLOG2:
7176 case ISD::FLOG10:
7177 case ISD::FEXP:
7178 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007179 case ISD::FTRUNC:
7180 case ISD::FFLOOR:
7181 case ISD::FCEIL:
7182 case ISD::FRINT:
7183 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007184 case ISD::FPOW:
7185 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007186 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007187 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007188 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007189 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7190 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007191 break;
7192 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007193 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7194 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007195 break;
7196 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007197 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7198 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007199 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007200 case ISD::FLOG:
7201 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7202 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7203 break;
7204 case ISD::FLOG2:
7205 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7206 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7207 break;
7208 case ISD::FLOG10:
7209 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7210 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7211 break;
7212 case ISD::FEXP:
7213 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7214 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7215 break;
7216 case ISD::FEXP2:
7217 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7218 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7219 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007220 case ISD::FTRUNC:
7221 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7222 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7223 break;
7224 case ISD::FFLOOR:
7225 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7226 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7227 break;
7228 case ISD::FCEIL:
7229 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7230 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7231 break;
7232 case ISD::FRINT:
7233 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7234 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7235 break;
7236 case ISD::FNEARBYINT:
7237 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7238 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7239 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007240 case ISD::FPOW:
7241 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7242 RTLIB::POW_PPCF128);
7243 break;
7244 case ISD::FPOWI:
7245 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7246 RTLIB::POWI_PPCF128);
7247 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007248 default: assert(0 && "Unreachable!");
7249 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007250 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007251 break;
7252 }
Evan Cheng966bf242006-12-16 00:52:40 +00007253 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007254 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007255 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007256 ExpandOp(Node->getOperand(0), Lo, Tmp);
7257 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7258 // lo = hi==fabs(hi) ? lo : -lo;
7259 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7260 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7261 DAG.getCondCode(ISD::SETEQ));
7262 break;
7263 }
Dan Gohman475871a2008-07-27 21:46:04 +00007264 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007265 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7266 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7267 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7268 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7269 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7270 if (getTypeAction(NVT) == Expand)
7271 ExpandOp(Lo, Lo, Hi);
7272 break;
7273 }
7274 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007275 if (VT == MVT::ppcf128) {
7276 ExpandOp(Node->getOperand(0), Lo, Hi);
7277 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7278 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7279 break;
7280 }
Dan Gohman475871a2008-07-27 21:46:04 +00007281 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007282 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7283 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7284 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7285 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7286 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7287 if (getTypeAction(NVT) == Expand)
7288 ExpandOp(Lo, Lo, Hi);
7289 break;
7290 }
Evan Cheng912095b2007-01-04 21:56:39 +00007291 case ISD::FCOPYSIGN: {
7292 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7293 if (getTypeAction(NVT) == Expand)
7294 ExpandOp(Lo, Lo, Hi);
7295 break;
7296 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007297 case ISD::SINT_TO_FP:
7298 case ISD::UINT_TO_FP: {
7299 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007300 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007301
7302 // Promote the operand if needed. Do this before checking for
7303 // ppcf128 so conversions of i16 and i8 work.
7304 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007305 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007306 Tmp = isSigned
7307 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7308 DAG.getValueType(SrcVT))
7309 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007310 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007311 SrcVT = Node->getOperand(0).getValueType();
7312 }
7313
Dan Gohmana2e94852008-03-10 23:03:31 +00007314 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007315 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007316 if (isSigned) {
7317 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7318 Node->getOperand(0)));
7319 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7320 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007321 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007322 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7323 Node->getOperand(0)));
7324 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7325 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007326 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007327 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7328 DAG.getConstant(0, MVT::i32),
7329 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7330 DAG.getConstantFP(
7331 APFloat(APInt(128, 2, TwoE32)),
7332 MVT::ppcf128)),
7333 Hi,
7334 DAG.getCondCode(ISD::SETLT)),
7335 Lo, Hi);
7336 }
7337 break;
7338 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007339 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7340 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007341 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007342 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7343 Lo, Hi);
7344 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7345 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7346 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7347 DAG.getConstant(0, MVT::i64),
7348 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7349 DAG.getConstantFP(
7350 APFloat(APInt(128, 2, TwoE64)),
7351 MVT::ppcf128)),
7352 Hi,
7353 DAG.getCondCode(ISD::SETLT)),
7354 Lo, Hi);
7355 break;
7356 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007357
Dan Gohmana2e94852008-03-10 23:03:31 +00007358 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7359 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00007360 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007361 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007362 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007363 break;
7364 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007365 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007366
Chris Lattner83397362005-12-21 18:02:52 +00007367 // Make sure the resultant values have been legalized themselves, unless this
7368 // is a type that requires multi-step expansion.
7369 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7370 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007371 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007372 // Don't legalize the high part if it is expanded to a single node.
7373 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007374 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007375
7376 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007377 bool isNew =
7378 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007379 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007380 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007381}
7382
Dan Gohman7f321562007-06-25 16:23:39 +00007383/// SplitVectorOp - Given an operand of vector type, break it down into
7384/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007385void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7386 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007387 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007388 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007389 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007390 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007391
Duncan Sands83ec4b62008-06-06 12:08:01 +00007392 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007393
7394 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7395 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7396
Duncan Sands83ec4b62008-06-06 12:08:01 +00007397 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7398 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007399
Chris Lattnerc7029802006-03-18 01:44:44 +00007400 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007401 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007402 = SplitNodes.find(Op);
7403 if (I != SplitNodes.end()) {
7404 Lo = I->second.first;
7405 Hi = I->second.second;
7406 return;
7407 }
7408
7409 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007410 default:
7411#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007412 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007413#endif
7414 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007415 case ISD::UNDEF:
7416 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7417 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7418 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007419 case ISD::BUILD_PAIR:
7420 Lo = Node->getOperand(0);
7421 Hi = Node->getOperand(1);
7422 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007423 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007424 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7425 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007426 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007427 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007428 if (Index < NewNumElts_Lo)
7429 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7430 DAG.getIntPtrConstant(Index));
7431 else
7432 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7433 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7434 break;
7435 }
Dan Gohman475871a2008-07-27 21:46:04 +00007436 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007437 Node->getOperand(1),
7438 Node->getOperand(2));
7439 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007440 break;
7441 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007442 case ISD::VECTOR_SHUFFLE: {
7443 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007444 SDValue Mask = Node->getOperand(2);
7445 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007446 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007447
7448 // Insert all of the elements from the input that are needed. We use
7449 // buildvector of extractelement here because the input vectors will have
7450 // to be legalized, so this makes the code simpler.
7451 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007452 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007453 if (IdxNode.getOpcode() == ISD::UNDEF) {
7454 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7455 continue;
7456 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007457 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007458 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007459 if (Idx >= NumElements) {
7460 InVec = Node->getOperand(1);
7461 Idx -= NumElements;
7462 }
7463 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7464 DAG.getConstant(Idx, PtrVT)));
7465 }
7466 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7467 Ops.clear();
7468
7469 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007470 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007471 if (IdxNode.getOpcode() == ISD::UNDEF) {
7472 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7473 continue;
7474 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007475 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007476 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007477 if (Idx >= NumElements) {
7478 InVec = Node->getOperand(1);
7479 Idx -= NumElements;
7480 }
7481 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7482 DAG.getConstant(Idx, PtrVT)));
7483 }
Mon P Wang92879f32008-07-25 01:30:26 +00007484 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007485 break;
7486 }
Dan Gohman7f321562007-06-25 16:23:39 +00007487 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007488 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007489 Node->op_begin()+NewNumElts_Lo);
7490 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007491
Dan Gohman475871a2008-07-27 21:46:04 +00007492 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007493 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007494 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007495 break;
7496 }
Dan Gohman7f321562007-06-25 16:23:39 +00007497 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007498 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007499 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7500 if (NewNumSubvectors == 1) {
7501 Lo = Node->getOperand(0);
7502 Hi = Node->getOperand(1);
7503 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007504 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7505 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007506 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007507
Mon P Wangaeb06d22008-11-10 04:46:22 +00007508 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007509 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007510 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007511 }
Dan Gohman65956352007-06-13 15:12:02 +00007512 break;
7513 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007514 case ISD::EXTRACT_SUBVECTOR: {
7515 SDValue Vec = Op.getOperand(0);
7516 SDValue Idx = Op.getOperand(1);
7517 MVT IdxVT = Idx.getValueType();
7518
7519 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7520 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7521 if (CIdx) {
7522 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7523 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7524 IdxVT));
7525 } else {
7526 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7527 DAG.getConstant(NewNumElts_Lo, IdxVT));
7528 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7529 }
7530 break;
7531 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007532 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007533 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007534
Dan Gohman475871a2008-07-27 21:46:04 +00007535 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007536 SplitVectorOp(Node->getOperand(1), LL, LH);
7537 SplitVectorOp(Node->getOperand(2), RL, RH);
7538
Duncan Sands83ec4b62008-06-06 12:08:01 +00007539 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007540 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007541 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007542 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007543 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7544 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007545 } else {
7546 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007547 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7548 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007549 }
7550 break;
7551 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007552 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007553 SDValue CondLHS = Node->getOperand(0);
7554 SDValue CondRHS = Node->getOperand(1);
7555 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007556
Dan Gohman475871a2008-07-27 21:46:04 +00007557 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007558 SplitVectorOp(Node->getOperand(2), LL, LH);
7559 SplitVectorOp(Node->getOperand(3), RL, RH);
7560
7561 // Handle a simple select with vector operands.
7562 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7563 LL, RL, CondCode);
7564 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7565 LH, RH, CondCode);
7566 break;
7567 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007568 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007569 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007570 SplitVectorOp(Node->getOperand(0), LL, LH);
7571 SplitVectorOp(Node->getOperand(1), RL, RH);
7572 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7573 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7574 break;
7575 }
Dan Gohman7f321562007-06-25 16:23:39 +00007576 case ISD::ADD:
7577 case ISD::SUB:
7578 case ISD::MUL:
7579 case ISD::FADD:
7580 case ISD::FSUB:
7581 case ISD::FMUL:
7582 case ISD::SDIV:
7583 case ISD::UDIV:
7584 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007585 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007586 case ISD::AND:
7587 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007588 case ISD::XOR:
7589 case ISD::UREM:
7590 case ISD::SREM:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00007591 case ISD::FREM:
7592 case ISD::SHL:
7593 case ISD::SRA:
7594 case ISD::SRL: {
Dan Gohman475871a2008-07-27 21:46:04 +00007595 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007596 SplitVectorOp(Node->getOperand(0), LL, LH);
7597 SplitVectorOp(Node->getOperand(1), RL, RH);
7598
Nate Begeman5db1afb2007-11-15 21:15:26 +00007599 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7600 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007601 break;
7602 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007603 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007604 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007605 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007606 SplitVectorOp(Node->getOperand(0), L, H);
7607
Nate Begeman5db1afb2007-11-15 21:15:26 +00007608 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7609 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007610 break;
7611 }
7612 case ISD::CTTZ:
7613 case ISD::CTLZ:
7614 case ISD::CTPOP:
7615 case ISD::FNEG:
7616 case ISD::FABS:
7617 case ISD::FSQRT:
7618 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007619 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007620 case ISD::FLOG:
7621 case ISD::FLOG2:
7622 case ISD::FLOG10:
7623 case ISD::FEXP:
7624 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007625 case ISD::FP_TO_SINT:
7626 case ISD::FP_TO_UINT:
7627 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007628 case ISD::UINT_TO_FP:
7629 case ISD::TRUNCATE:
7630 case ISD::ANY_EXTEND:
7631 case ISD::SIGN_EXTEND:
7632 case ISD::ZERO_EXTEND:
7633 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007634 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007635 SplitVectorOp(Node->getOperand(0), L, H);
7636
Nate Begeman5db1afb2007-11-15 21:15:26 +00007637 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7638 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007639 break;
7640 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007641 case ISD::CONVERT_RNDSAT: {
7642 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7643 SDValue L, H;
7644 SplitVectorOp(Node->getOperand(0), L, H);
7645 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7646 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7647 SDValue STyOpL = DAG.getValueType(L.getValueType());
7648 SDValue STyOpH = DAG.getValueType(H.getValueType());
7649
7650 SDValue RndOp = Node->getOperand(3);
7651 SDValue SatOp = Node->getOperand(4);
7652
7653 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7654 RndOp, SatOp, CvtCode);
7655 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7656 RndOp, SatOp, CvtCode);
7657 break;
7658 }
Dan Gohman7f321562007-06-25 16:23:39 +00007659 case ISD::LOAD: {
7660 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007661 SDValue Ch = LD->getChain();
7662 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007663 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007664 const Value *SV = LD->getSrcValue();
7665 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007666 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007667 unsigned Alignment = LD->getAlignment();
7668 bool isVolatile = LD->isVolatile();
7669
Dan Gohman7f8613e2008-08-14 20:04:46 +00007670 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7671 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7672
7673 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7674 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7675 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7676
7677 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7678 NewVT_Lo, Ch, Ptr, Offset,
7679 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7680 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007681 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007682 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007683 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007684 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007685 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7686 NewVT_Hi, Ch, Ptr, Offset,
7687 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007688
7689 // Build a factor node to remember that this load is independent of the
7690 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007691 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007692 Hi.getValue(1));
7693
7694 // Remember that we legalized the chain.
7695 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007696 break;
7697 }
Dan Gohman7f321562007-06-25 16:23:39 +00007698 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007699 // We know the result is a vector. The input may be either a vector or a
7700 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007701 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007702 if (!InOp.getValueType().isVector() ||
7703 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007704 // The input is a scalar or single-element vector.
7705 // Lower to a store/load so that it can be split.
7706 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007707 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7708 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007709 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007710 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007711
Dan Gohman475871a2008-07-27 21:46:04 +00007712 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007713 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007714 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007715 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007716 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007717 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007718 // Split the vector and convert each of the pieces now.
7719 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007720 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7721 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007722 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007723 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007724 }
7725
7726 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007727 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007728 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007729 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007730 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007731}
7732
7733
Dan Gohman89b20c02007-06-27 14:06:22 +00007734/// ScalarizeVectorOp - Given an operand of single-element vector type
7735/// (e.g. v1f32), convert it into the equivalent operation that returns a
7736/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007737SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007738 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007739 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007740 MVT NewVT = Op.getValueType().getVectorElementType();
7741 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007742
Dan Gohman7f321562007-06-25 16:23:39 +00007743 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007744 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007745 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007746
Dan Gohman475871a2008-07-27 21:46:04 +00007747 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007748 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007749 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007750#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007751 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007752#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007753 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7754 case ISD::ADD:
7755 case ISD::FADD:
7756 case ISD::SUB:
7757 case ISD::FSUB:
7758 case ISD::MUL:
7759 case ISD::FMUL:
7760 case ISD::SDIV:
7761 case ISD::UDIV:
7762 case ISD::FDIV:
7763 case ISD::SREM:
7764 case ISD::UREM:
7765 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007766 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007767 case ISD::AND:
7768 case ISD::OR:
7769 case ISD::XOR:
7770 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007771 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007772 ScalarizeVectorOp(Node->getOperand(0)),
7773 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007774 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007775 case ISD::FNEG:
7776 case ISD::FABS:
7777 case ISD::FSQRT:
7778 case ISD::FSIN:
7779 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007780 case ISD::FLOG:
7781 case ISD::FLOG2:
7782 case ISD::FLOG10:
7783 case ISD::FEXP:
7784 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007785 case ISD::FP_TO_SINT:
7786 case ISD::FP_TO_UINT:
7787 case ISD::SINT_TO_FP:
7788 case ISD::UINT_TO_FP:
7789 case ISD::SIGN_EXTEND:
7790 case ISD::ZERO_EXTEND:
7791 case ISD::ANY_EXTEND:
7792 case ISD::TRUNCATE:
7793 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007794 Result = DAG.getNode(Node->getOpcode(),
7795 NewVT,
7796 ScalarizeVectorOp(Node->getOperand(0)));
7797 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007798 case ISD::CONVERT_RNDSAT: {
7799 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7800 Result = DAG.getConvertRndSat(NewVT, Op0,
7801 DAG.getValueType(NewVT),
7802 DAG.getValueType(Op0.getValueType()),
7803 Node->getOperand(3),
7804 Node->getOperand(4),
7805 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7806 break;
7807 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007808 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007809 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007810 Result = DAG.getNode(Node->getOpcode(),
7811 NewVT,
7812 ScalarizeVectorOp(Node->getOperand(0)),
7813 Node->getOperand(1));
7814 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007815 case ISD::LOAD: {
7816 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007817 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7818 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007819 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007820 const Value *SV = LD->getSrcValue();
7821 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007822 MVT MemoryVT = LD->getMemoryVT();
7823 unsigned Alignment = LD->getAlignment();
7824 bool isVolatile = LD->isVolatile();
7825
7826 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7827 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7828
7829 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7830 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7831 MemoryVT.getVectorElementType(),
7832 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007833
Chris Lattnerc7029802006-03-18 01:44:44 +00007834 // Remember that we legalized the chain.
7835 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7836 break;
7837 }
Dan Gohman7f321562007-06-25 16:23:39 +00007838 case ISD::BUILD_VECTOR:
7839 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007840 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007841 case ISD::INSERT_VECTOR_ELT:
7842 // Returning the inserted scalar element.
7843 Result = Node->getOperand(1);
7844 break;
7845 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007846 assert(Node->getOperand(0).getValueType() == NewVT &&
7847 "Concat of non-legal vectors not yet supported!");
7848 Result = Node->getOperand(0);
7849 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007850 case ISD::VECTOR_SHUFFLE: {
7851 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007852 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007853 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007854 Result = ScalarizeVectorOp(Node->getOperand(1));
7855 else
7856 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007857 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007858 }
7859 case ISD::EXTRACT_SUBVECTOR:
Mon P Wange0b436a2008-11-06 22:52:21 +00007860 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangaeb06d22008-11-10 04:46:22 +00007861 Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007862 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007863 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007864 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007865 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007866 Op0 = ScalarizeVectorOp(Op0);
7867 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007868 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007869 }
Dan Gohman7f321562007-06-25 16:23:39 +00007870 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007871 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007872 ScalarizeVectorOp(Op.getOperand(1)),
7873 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007874 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007875 case ISD::SELECT_CC:
7876 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7877 Node->getOperand(1),
7878 ScalarizeVectorOp(Op.getOperand(2)),
7879 ScalarizeVectorOp(Op.getOperand(3)),
7880 Node->getOperand(4));
7881 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007882 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007883 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7884 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Duncan Sands5480c042009-01-01 15:52:00 +00007885 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0.getValueType()),
7886 Op0, Op1, Op.getOperand(2));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007887 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7888 DAG.getConstant(-1ULL, NewVT),
7889 DAG.getConstant(0ULL, NewVT));
7890 break;
7891 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007892 }
7893
7894 if (TLI.isTypeLegal(NewVT))
7895 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007896 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7897 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007898 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007899 return Result;
7900}
7901
Chris Lattner3e928bb2005-01-07 07:47:09 +00007902
Mon P Wang0c397192008-10-30 08:01:45 +00007903SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7904 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7905 if (I != WidenNodes.end()) return I->second;
7906
7907 MVT VT = Op.getValueType();
7908 assert(VT.isVector() && "Cannot widen non-vector type!");
7909
7910 SDValue Result;
7911 SDNode *Node = Op.getNode();
7912 MVT EVT = VT.getVectorElementType();
7913
7914 unsigned NumElts = VT.getVectorNumElements();
7915 unsigned NewNumElts = WidenVT.getVectorNumElements();
7916 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7917 assert(NewNumElts < 17);
7918
7919 // When widen is called, it is assumed that it is more efficient to use a
7920 // wide type. The default action is to widen to operation to a wider legal
7921 // vector type and then do the operation if it is legal by calling LegalizeOp
7922 // again. If there is no vector equivalent, we will unroll the operation, do
7923 // it, and rebuild the vector. If most of the operations are vectorizible to
7924 // the legal type, the resulting code will be more efficient. If this is not
7925 // the case, the resulting code will preform badly as we end up generating
7926 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007927 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007928 switch (Node->getOpcode()) {
7929 default:
7930#ifndef NDEBUG
7931 Node->dump(&DAG);
7932#endif
7933 assert(0 && "Unexpected operation in WidenVectorOp!");
7934 break;
7935 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007936 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007937 case ISD::Constant:
7938 case ISD::ConstantFP:
7939 // To build a vector of these elements, clients should call BuildVector
7940 // and with each element instead of creating a node with a vector type
7941 assert(0 && "Unexpected operation in WidenVectorOp!");
7942 case ISD::VAARG:
7943 // Variable Arguments with vector types doesn't make any sense to me
7944 assert(0 && "Unexpected operation in WidenVectorOp!");
7945 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007946 case ISD::UNDEF:
7947 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7948 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007949 case ISD::BUILD_VECTOR: {
7950 // Build a vector with undefined for the new nodes
7951 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7952 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7953 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7954 }
7955 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7956 break;
7957 }
7958 case ISD::INSERT_VECTOR_ELT: {
7959 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7960 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7961 Node->getOperand(1), Node->getOperand(2));
7962 break;
7963 }
7964 case ISD::VECTOR_SHUFFLE: {
7965 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7966 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7967 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7968 // used as permutation array. We build the vector here instead of widening
7969 // because we don't want to legalize and have it turned to something else.
7970 SDValue PermOp = Node->getOperand(2);
7971 SDValueVector NewOps;
7972 MVT PVT = PermOp.getValueType().getVectorElementType();
7973 for (unsigned i = 0; i < NumElts; ++i) {
7974 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7975 NewOps.push_back(PermOp.getOperand(i));
7976 } else {
7977 unsigned Idx =
Mon P Wange1a0b2e2008-12-13 08:15:14 +00007978 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
Mon P Wang0c397192008-10-30 08:01:45 +00007979 if (Idx < NumElts) {
7980 NewOps.push_back(PermOp.getOperand(i));
7981 }
7982 else {
7983 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7984 PermOp.getOperand(i).getValueType()));
7985 }
7986 }
7987 }
7988 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7989 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7990 }
7991
7992 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7993 MVT::getVectorVT(PVT, NewOps.size()),
7994 &NewOps[0], NewOps.size());
7995
7996 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7997 break;
7998 }
7999 case ISD::LOAD: {
8000 // If the load widen returns true, we can use a single load for the
8001 // vector. Otherwise, it is returning a token factor for multiple
8002 // loads.
8003 SDValue TFOp;
8004 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8005 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8006 else
8007 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8008 break;
8009 }
8010
8011 case ISD::BIT_CONVERT: {
8012 SDValue Tmp1 = Node->getOperand(0);
8013 // Converts between two different types so we need to determine
8014 // the correct widen type for the input operand.
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008015 MVT InVT = Tmp1.getValueType();
8016 unsigned WidenSize = WidenVT.getSizeInBits();
8017 if (InVT.isVector()) {
8018 MVT InEltVT = InVT.getVectorElementType();
8019 unsigned InEltSize = InEltVT.getSizeInBits();
8020 assert(WidenSize % InEltSize == 0 &&
8021 "can not widen bit convert that are not multiple of element type");
8022 MVT NewInWidenVT = MVT::getVectorVT(InEltVT, WidenSize / InEltSize);
8023 Tmp1 = WidenVectorOp(Tmp1, NewInWidenVT);
8024 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8025 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Tmp1);
8026 } else {
8027 // If the result size is a multiple of the input size, widen the input
8028 // and then convert.
8029 unsigned InSize = InVT.getSizeInBits();
8030 assert(WidenSize % InSize == 0 &&
8031 "can not widen bit convert that are not multiple of element type");
8032 unsigned NewNumElts = WidenSize / InSize;
8033 SmallVector<SDValue, 16> Ops(NewNumElts);
8034 SDValue UndefVal = DAG.getNode(ISD::UNDEF, InVT);
8035 Ops[0] = Tmp1;
8036 for (unsigned i = 1; i < NewNumElts; ++i)
8037 Ops[i] = UndefVal;
Mon P Wang0c397192008-10-30 08:01:45 +00008038
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008039 MVT NewInVT = MVT::getVectorVT(InVT, NewNumElts);
8040 Result = DAG.getNode(ISD::BUILD_VECTOR, NewInVT, &Ops[0], NewNumElts);
8041 Result = DAG.getNode(ISD::BIT_CONVERT, WidenVT, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008042 }
8043 break;
8044 }
8045
8046 case ISD::SINT_TO_FP:
8047 case ISD::UINT_TO_FP:
8048 case ISD::FP_TO_SINT:
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008049 case ISD::FP_TO_UINT:
8050 case ISD::FP_ROUND: {
Mon P Wang0c397192008-10-30 08:01:45 +00008051 SDValue Tmp1 = Node->getOperand(0);
8052 // Converts between two different types so we need to determine
8053 // the correct widen type for the input operand.
8054 MVT TVT = Tmp1.getValueType();
8055 assert(TVT.isVector() && "can not widen non vector type");
8056 MVT TEVT = TVT.getVectorElementType();
8057 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8058 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8059 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8060 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008061 break;
8062 }
8063
8064 case ISD::FP_EXTEND:
8065 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8066 case ISD::TRUNCATE:
8067 case ISD::SIGN_EXTEND:
8068 case ISD::ZERO_EXTEND:
8069 case ISD::ANY_EXTEND:
Mon P Wang0c397192008-10-30 08:01:45 +00008070 case ISD::SIGN_EXTEND_INREG:
8071 case ISD::FABS:
8072 case ISD::FNEG:
8073 case ISD::FSQRT:
8074 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00008075 case ISD::FCOS:
8076 case ISD::CTPOP:
8077 case ISD::CTTZ:
8078 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00008079 // Unary op widening
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008080 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00008081 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8082 assert(Tmp1.getValueType() == WidenVT);
8083 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008084 break;
8085 }
Mon P Wang77cdf302008-11-10 20:54:11 +00008086 case ISD::CONVERT_RNDSAT: {
8087 SDValue RndOp = Node->getOperand(3);
8088 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00008089 SDValue SrcOp = Node->getOperand(0);
8090
8091 // Converts between two different types so we need to determine
8092 // the correct widen type for the input operand.
8093 MVT SVT = SrcOp.getValueType();
8094 assert(SVT.isVector() && "can not widen non vector type");
8095 MVT SEVT = SVT.getVectorElementType();
8096 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8097
8098 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8099 assert(SrcOp.getValueType() == WidenVT);
8100 SDValue DTyOp = DAG.getValueType(WidenVT);
8101 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8102 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8103
8104 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8105 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00008106 break;
8107 }
Mon P Wang0c397192008-10-30 08:01:45 +00008108 case ISD::FPOW:
8109 case ISD::FPOWI:
8110 case ISD::ADD:
8111 case ISD::SUB:
8112 case ISD::MUL:
8113 case ISD::MULHS:
8114 case ISD::MULHU:
8115 case ISD::AND:
8116 case ISD::OR:
8117 case ISD::XOR:
8118 case ISD::FADD:
8119 case ISD::FSUB:
8120 case ISD::FMUL:
8121 case ISD::SDIV:
8122 case ISD::SREM:
8123 case ISD::FDIV:
8124 case ISD::FREM:
8125 case ISD::FCOPYSIGN:
8126 case ISD::UDIV:
8127 case ISD::UREM:
8128 case ISD::BSWAP: {
8129 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00008130 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8131 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8132 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8133 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008134 break;
8135 }
8136
8137 case ISD::SHL:
8138 case ISD::SRA:
8139 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00008140 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8141 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00008142 SDValue ShOp = Node->getOperand(1);
8143 MVT ShVT = ShOp.getValueType();
8144 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8145 WidenVT.getVectorNumElements());
8146 ShOp = WidenVectorOp(ShOp, NewShVT);
8147 assert(ShOp.getValueType() == NewShVT);
8148 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008149 break;
8150 }
Mon P Wangfb13f002008-12-02 07:35:08 +00008151
Mon P Wang0c397192008-10-30 08:01:45 +00008152 case ISD::EXTRACT_VECTOR_ELT: {
8153 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8154 assert(Tmp1.getValueType() == WidenVT);
8155 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8156 break;
8157 }
8158 case ISD::CONCAT_VECTORS: {
8159 // We concurrently support only widen on a multiple of the incoming vector.
8160 // We could widen on a multiple of the incoming operand if necessary.
8161 unsigned NumConcat = NewNumElts / NumElts;
8162 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangfb13f002008-12-02 07:35:08 +00008163 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang0c397192008-10-30 08:01:45 +00008164 SmallVector<SDValue, 8> MOps;
8165 MOps.push_back(Op);
8166 for (unsigned i = 1; i != NumConcat; ++i) {
8167 MOps.push_back(UndefVal);
8168 }
8169 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8170 &MOps[0], MOps.size()));
8171 break;
8172 }
8173 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008174 SDValue Tmp1 = Node->getOperand(0);
8175 SDValue Idx = Node->getOperand(1);
8176 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8177 if (CIdx && CIdx->getZExtValue() == 0) {
8178 // Since we are access the start of the vector, the incoming
8179 // vector type might be the proper.
8180 MVT Tmp1VT = Tmp1.getValueType();
8181 if (Tmp1VT == WidenVT)
8182 return Tmp1;
8183 else {
8184 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8185 if (Tmp1VTNumElts < NewNumElts)
8186 Result = WidenVectorOp(Tmp1, WidenVT);
8187 else
8188 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8189 }
8190 } else if (NewNumElts % NumElts == 0) {
8191 // Widen the extracted subvector.
8192 unsigned NumConcat = NewNumElts / NumElts;
8193 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8194 SmallVector<SDValue, 8> MOps;
8195 MOps.push_back(Op);
8196 for (unsigned i = 1; i != NumConcat; ++i) {
8197 MOps.push_back(UndefVal);
8198 }
8199 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8200 &MOps[0], MOps.size()));
8201 } else {
8202 assert(0 && "can not widen extract subvector");
8203 // This could be implemented using insert and build vector but I would
8204 // like to see when this happens.
8205 }
Mon P Wang0c397192008-10-30 08:01:45 +00008206 break;
8207 }
8208
8209 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00008210 // Determine new condition widen type and widen
8211 SDValue Cond1 = Node->getOperand(0);
8212 MVT CondVT = Cond1.getValueType();
8213 assert(CondVT.isVector() && "can not widen non vector type");
8214 MVT CondEVT = CondVT.getVectorElementType();
8215 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8216 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8217 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8218
8219 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8220 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8221 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8222 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008223 break;
8224 }
8225
8226 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00008227 // Determine new condition widen type and widen
8228 SDValue Cond1 = Node->getOperand(0);
8229 SDValue Cond2 = Node->getOperand(1);
8230 MVT CondVT = Cond1.getValueType();
8231 assert(CondVT.isVector() && "can not widen non vector type");
8232 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8233 MVT CondEVT = CondVT.getVectorElementType();
8234 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8235 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8236 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8237 assert(Cond1.getValueType() == CondWidenVT &&
8238 Cond2.getValueType() == CondWidenVT && "condition not widen");
8239
8240 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8241 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8242 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8243 "operands not widen");
8244 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8245 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00008246 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008247 }
8248 case ISD::VSETCC: {
8249 // Determine widen for the operand
8250 SDValue Tmp1 = Node->getOperand(0);
8251 MVT TmpVT = Tmp1.getValueType();
8252 assert(TmpVT.isVector() && "can not widen non vector type");
8253 MVT TmpEVT = TmpVT.getVectorElementType();
8254 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8255 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8256 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
Mon P Wang87c8a8f2008-12-18 20:03:17 +00008257 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
Mon P Wang2eb13c32008-10-30 18:21:52 +00008258 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008259 break;
8260 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00008261 case ISD::ATOMIC_CMP_SWAP:
8262 case ISD::ATOMIC_LOAD_ADD:
8263 case ISD::ATOMIC_LOAD_SUB:
8264 case ISD::ATOMIC_LOAD_AND:
8265 case ISD::ATOMIC_LOAD_OR:
8266 case ISD::ATOMIC_LOAD_XOR:
8267 case ISD::ATOMIC_LOAD_NAND:
8268 case ISD::ATOMIC_LOAD_MIN:
8269 case ISD::ATOMIC_LOAD_MAX:
8270 case ISD::ATOMIC_LOAD_UMIN:
8271 case ISD::ATOMIC_LOAD_UMAX:
8272 case ISD::ATOMIC_SWAP: {
Mon P Wang0c397192008-10-30 08:01:45 +00008273 // For now, we assume that using vectors for these operations don't make
8274 // much sense so we just split it. We return an empty result
8275 SDValue X, Y;
8276 SplitVectorOp(Op, X, Y);
8277 return Result;
8278 break;
8279 }
8280
8281 } // end switch (Node->getOpcode())
8282
8283 assert(Result.getNode() && "Didn't set a result!");
8284 if (Result != Op)
8285 Result = LegalizeOp(Result);
8286
Mon P Wangf007a8b2008-11-06 05:31:54 +00008287 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008288 return Result;
8289}
8290
8291// Utility function to find a legal vector type and its associated element
8292// type from a preferred width and whose vector type must be the same size
8293// as the VVT.
8294// TLI: Target lowering used to determine legal types
8295// Width: Preferred width of element type
8296// VVT: Vector value type whose size we must match.
8297// Returns VecEVT and EVT - the vector type and its associated element type
8298static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8299 MVT& EVT, MVT& VecEVT) {
8300 // We start with the preferred width, make it a power of 2 and see if
8301 // we can find a vector type of that width. If not, we reduce it by
8302 // another power of 2. If we have widen the type, a vector of bytes should
8303 // always be legal.
8304 assert(TLI.isTypeLegal(VVT));
8305 unsigned EWidth = Width + 1;
8306 do {
8307 assert(EWidth > 0);
8308 EWidth = (1 << Log2_32(EWidth-1));
8309 EVT = MVT::getIntegerVT(EWidth);
8310 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8311 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8312 } while (!TLI.isTypeLegal(VecEVT) ||
8313 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8314}
8315
8316SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8317 SDValue Chain,
8318 SDValue BasePtr,
8319 const Value *SV,
8320 int SVOffset,
8321 unsigned Alignment,
8322 bool isVolatile,
8323 unsigned LdWidth,
8324 MVT ResType) {
8325 // We assume that we have good rules to handle loading power of two loads so
8326 // we break down the operations to power of 2 loads. The strategy is to
8327 // load the largest power of 2 that we can easily transform to a legal vector
8328 // and then insert into that vector, and the cast the result into the legal
8329 // vector that we want. This avoids unnecessary stack converts.
8330 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8331 // the load is nonvolatile, we an use a wider load for the value.
8332 // Find a vector length we can load a large chunk
8333 MVT EVT, VecEVT;
8334 unsigned EVTWidth;
8335 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8336 EVTWidth = EVT.getSizeInBits();
8337
8338 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8339 isVolatile, Alignment);
8340 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8341 LdChain.push_back(LdOp.getValue(1));
8342
8343 // Check if we can load the element with one instruction
8344 if (LdWidth == EVTWidth) {
8345 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8346 }
8347
8348 // The vector element order is endianness dependent.
8349 unsigned Idx = 1;
8350 LdWidth -= EVTWidth;
8351 unsigned Offset = 0;
8352
8353 while (LdWidth > 0) {
8354 unsigned Increment = EVTWidth / 8;
8355 Offset += Increment;
8356 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8357 DAG.getIntPtrConstant(Increment));
8358
8359 if (LdWidth < EVTWidth) {
8360 // Our current type we are using is too large, use a smaller size by
8361 // using a smaller power of 2
8362 unsigned oEVTWidth = EVTWidth;
8363 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8364 EVTWidth = EVT.getSizeInBits();
8365 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008366 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008367 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8368 }
8369
8370 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8371 SVOffset+Offset, isVolatile,
8372 MinAlign(Alignment, Offset));
8373 LdChain.push_back(LdOp.getValue(1));
8374 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8375 DAG.getIntPtrConstant(Idx++));
8376
8377 LdWidth -= EVTWidth;
8378 }
8379
8380 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8381}
8382
8383bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8384 SDValue& TFOp,
8385 SDValue Op,
8386 MVT NVT) {
8387 // TODO: Add support for ConcatVec and the ability to load many vector
8388 // types (e.g., v4i8). This will not work when a vector register
8389 // to memory mapping is strange (e.g., vector elements are not
8390 // stored in some sequential order).
8391
8392 // It must be true that the widen vector type is bigger than where
8393 // we need to load from.
8394 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8395 MVT LdVT = LD->getMemoryVT();
8396 assert(LdVT.isVector() && NVT.isVector());
8397 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8398
8399 // Load information
8400 SDValue Chain = LD->getChain();
8401 SDValue BasePtr = LD->getBasePtr();
8402 int SVOffset = LD->getSrcValueOffset();
8403 unsigned Alignment = LD->getAlignment();
8404 bool isVolatile = LD->isVolatile();
8405 const Value *SV = LD->getSrcValue();
8406 unsigned int LdWidth = LdVT.getSizeInBits();
8407
8408 // Load value as a large register
8409 SDValueVector LdChain;
8410 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8411 Alignment, isVolatile, LdWidth, NVT);
8412
8413 if (LdChain.size() == 1) {
8414 TFOp = LdChain[0];
8415 return true;
8416 }
8417 else {
8418 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8419 return false;
8420 }
8421}
8422
8423
8424void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8425 SDValue Chain,
8426 SDValue BasePtr,
8427 const Value *SV,
8428 int SVOffset,
8429 unsigned Alignment,
8430 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008431 SDValue ValOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008432 unsigned StWidth) {
8433 // Breaks the stores into a series of power of 2 width stores. For any
8434 // width, we convert the vector to the vector of element size that we
8435 // want to store. This avoids requiring a stack convert.
8436
8437 // Find a width of the element type we can store with
8438 MVT VVT = ValOp.getValueType();
8439 MVT EVT, VecEVT;
8440 unsigned EVTWidth;
8441 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8442 EVTWidth = EVT.getSizeInBits();
8443
8444 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8445 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008446 DAG.getIntPtrConstant(0));
Mon P Wang0c397192008-10-30 08:01:45 +00008447 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8448 isVolatile, Alignment);
8449 StChain.push_back(StOp);
8450
8451 // Check if we are done
8452 if (StWidth == EVTWidth) {
8453 return;
8454 }
8455
8456 unsigned Idx = 1;
8457 StWidth -= EVTWidth;
8458 unsigned Offset = 0;
8459
8460 while (StWidth > 0) {
8461 unsigned Increment = EVTWidth / 8;
8462 Offset += Increment;
8463 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8464 DAG.getIntPtrConstant(Increment));
8465
8466 if (StWidth < EVTWidth) {
8467 // Our current type we are using is too large, use a smaller size by
8468 // using a smaller power of 2
8469 unsigned oEVTWidth = EVTWidth;
8470 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8471 EVTWidth = EVT.getSizeInBits();
8472 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008473 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008474 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8475 }
8476
8477 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008478 DAG.getIntPtrConstant(Idx++));
Mon P Wang0c397192008-10-30 08:01:45 +00008479 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8480 SVOffset + Offset, isVolatile,
8481 MinAlign(Alignment, Offset)));
8482 StWidth -= EVTWidth;
8483 }
8484}
8485
8486
8487SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8488 SDValue Chain,
8489 SDValue BasePtr) {
8490 // TODO: It might be cleaner if we can use SplitVector and have more legal
8491 // vector types that can be stored into memory (e.g., v4xi8 can
8492 // be stored as a word). This will not work when a vector register
8493 // to memory mapping is strange (e.g., vector elements are not
8494 // stored in some sequential order).
8495
8496 MVT StVT = ST->getMemoryVT();
8497 SDValue ValOp = ST->getValue();
8498
8499 // Check if we have widen this node with another value
8500 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8501 if (I != WidenNodes.end())
8502 ValOp = I->second;
8503
8504 MVT VVT = ValOp.getValueType();
8505
8506 // It must be true that we the widen vector type is bigger than where
8507 // we need to store.
8508 assert(StVT.isVector() && VVT.isVector());
8509 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8510 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8511
8512 // Store value
8513 SDValueVector StChain;
8514 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8515 ST->getSrcValueOffset(), ST->getAlignment(),
8516 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8517 if (StChain.size() == 1)
8518 return StChain[0];
8519 else
8520 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8521}
8522
8523
Chris Lattner3e928bb2005-01-07 07:47:09 +00008524// SelectionDAG::Legalize - This is the entry point for the file.
8525//
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008526void SelectionDAG::Legalize(bool TypesNeedLegalizing) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008527 /// run - This is the main entry point to this class.
8528 ///
Duncan Sandsb6862bb2008-12-14 09:43:15 +00008529 SelectionDAGLegalize(*this, TypesNeedLegalizing).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008530}
8531