blob: 3ff6645fb3e8ac528782b0083f658b3b44ea5718 [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskeyacd80ac2006-12-14 19:17:33 +000017#include "llvm/CodeGen/MachineJumpTableInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000018#include "llvm/CodeGen/MachineModuleInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000019#include "llvm/CodeGen/PseudoSourceValue.h"
Evan Cheng61bbbab2007-08-16 23:50:06 +000020#include "llvm/Target/TargetFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000022#include "llvm/Target/TargetData.h"
Evan Cheng3d4ce112006-10-30 08:00:44 +000023#include "llvm/Target/TargetMachine.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000024#include "llvm/Target/TargetOptions.h"
Dan Gohman707e0182008-04-12 04:36:06 +000025#include "llvm/Target/TargetSubtarget.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000026#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000027#include "llvm/Constants.h"
Reid Spencerc1030572007-01-19 21:13:56 +000028#include "llvm/DerivedTypes.h"
Chris Lattner5e46a192006-04-02 03:07:27 +000029#include "llvm/Support/CommandLine.h"
Chris Lattnera4f0b3a2006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Duncan Sandsdc846502007-10-28 12:59:45 +000031#include "llvm/Support/MathExtras.h"
Chris Lattner79715142007-02-03 01:12:36 +000032#include "llvm/ADT/DenseMap.h"
Chris Lattnerf06f35e2006-08-08 01:09:31 +000033#include "llvm/ADT/SmallVector.h"
Chris Lattner00755df2007-02-04 00:27:56 +000034#include "llvm/ADT/SmallPtrSet.h"
Evan Cheng033e6812006-03-24 01:17:21 +000035#include <map>
Chris Lattner3e928bb2005-01-07 07:47:09 +000036using namespace llvm;
37
38//===----------------------------------------------------------------------===//
39/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
40/// hacks on it until the target machine can handle it. This involves
41/// eliminating value sizes the machine cannot handle (promoting small sizes to
42/// large sizes or splitting up large values into small values) as well as
43/// eliminating operations the machine cannot handle.
44///
45/// This code also does a small amount of optimization and recognition of idioms
46/// as part of its processing. For example, if a target does not support a
47/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
48/// will attempt merge setcc and brc instructions into brcc's.
49///
50namespace {
Chris Lattner360e8202006-06-28 21:58:30 +000051class VISIBILITY_HIDDEN SelectionDAGLegalize {
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 TargetLowering &TLI;
53 SelectionDAG &DAG;
54
Chris Lattner6831a812006-02-13 09:18:02 +000055 // Libcall insertion helpers.
56
57 /// LastCALLSEQ_END - This keeps track of the CALLSEQ_END node that has been
58 /// legalized. We use this to ensure that calls are properly serialized
59 /// against each other, including inserted libcalls.
Dan Gohman475871a2008-07-27 21:46:04 +000060 SDValue LastCALLSEQ_END;
Chris Lattner6831a812006-02-13 09:18:02 +000061
62 /// IsLegalizingCall - This member is used *only* for purposes of providing
63 /// helpful assertions that a libcall isn't created while another call is
64 /// being legalized (which could lead to non-serialized call sequences).
65 bool IsLegalizingCall;
66
Chris Lattner3e928bb2005-01-07 07:47:09 +000067 enum LegalizeAction {
Chris Lattner68a17fe2006-01-29 08:42:06 +000068 Legal, // The target natively supports this operation.
69 Promote, // This operation should be executed in a larger type.
Chris Lattnerd74ea2b2006-05-24 17:04:05 +000070 Expand // Try to expand this to other ops, otherwise use a libcall.
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 };
Chris Lattner6831a812006-02-13 09:18:02 +000072
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ValueTypeActions - This is a bitvector that contains two bits for each
74 /// value type, where the two bits correspond to the LegalizeAction enum.
75 /// This can be queried with "getTypeAction(VT)".
Chris Lattner68a17fe2006-01-29 08:42:06 +000076 TargetLowering::ValueTypeActionImpl ValueTypeActions;
Chris Lattner3e928bb2005-01-07 07:47:09 +000077
Chris Lattner3e928bb2005-01-07 07:47:09 +000078 /// LegalizedNodes - For nodes that are of legal width, and that have more
79 /// than one use, this map indicates what regularized operand to use. This
80 /// allows us to avoid legalizing the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000081 DenseMap<SDValue, SDValue> LegalizedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000082
Chris Lattner03c85462005-01-15 05:21:40 +000083 /// PromotedNodes - For nodes that are below legal width, and that have more
84 /// than one use, this map indicates what promoted value to use. This allows
85 /// us to avoid promoting the same thing more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000086 DenseMap<SDValue, SDValue> PromotedNodes;
Chris Lattner03c85462005-01-15 05:21:40 +000087
Chris Lattnerc7029802006-03-18 01:44:44 +000088 /// ExpandedNodes - For nodes that need to be expanded this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000089 /// which operands are the expanded version of the input. This allows
Chris Lattnerc7029802006-03-18 01:44:44 +000090 /// us to avoid expanding the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000091 DenseMap<SDValue, std::pair<SDValue, SDValue> > ExpandedNodes;
Chris Lattner3e928bb2005-01-07 07:47:09 +000092
Chris Lattnerc7029802006-03-18 01:44:44 +000093 /// SplitNodes - For vector nodes that need to be split, this map indicates
Mon P Wang0c397192008-10-30 08:01:45 +000094 /// which operands are the split version of the input. This allows us
Chris Lattnerc7029802006-03-18 01:44:44 +000095 /// to avoid splitting the same node more than once.
Dan Gohman475871a2008-07-27 21:46:04 +000096 std::map<SDValue, std::pair<SDValue, SDValue> > SplitNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +000097
Dan Gohman7f321562007-06-25 16:23:39 +000098 /// ScalarizedNodes - For nodes that need to be converted from vector types to
99 /// scalar types, this contains the mapping of ones we have already
Chris Lattnerc7029802006-03-18 01:44:44 +0000100 /// processed to the result.
Dan Gohman475871a2008-07-27 21:46:04 +0000101 std::map<SDValue, SDValue> ScalarizedNodes;
Chris Lattnerc7029802006-03-18 01:44:44 +0000102
Mon P Wangf007a8b2008-11-06 05:31:54 +0000103 /// WidenNodes - For nodes that need to be widened from one vector type to
104 /// another, this contains the mapping of those that we have already widen.
105 /// This allows us to avoid widening more than once.
Mon P Wang0c397192008-10-30 08:01:45 +0000106 std::map<SDValue, SDValue> WidenNodes;
107
Dan Gohman475871a2008-07-27 21:46:04 +0000108 void AddLegalizedOperand(SDValue From, SDValue To) {
Chris Lattner69a889e2005-12-20 00:53:54 +0000109 LegalizedNodes.insert(std::make_pair(From, To));
110 // If someone requests legalization of the new node, return itself.
111 if (From != To)
112 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000113 }
Dan Gohman475871a2008-07-27 21:46:04 +0000114 void AddPromotedOperand(SDValue From, SDValue To) {
Dan Gohman6b345ee2008-07-07 17:46:23 +0000115 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
Chris Lattner03c85462005-01-15 05:21:40 +0000116 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000117 isNew = isNew;
Chris Lattner69a889e2005-12-20 00:53:54 +0000118 // If someone requests legalization of the new node, return itself.
119 LegalizedNodes.insert(std::make_pair(To, To));
Chris Lattner03c85462005-01-15 05:21:40 +0000120 }
Mon P Wangf007a8b2008-11-06 05:31:54 +0000121 void AddWidenedOperand(SDValue From, SDValue To) {
Mon P Wang0c397192008-10-30 08:01:45 +0000122 bool isNew = WidenNodes.insert(std::make_pair(From, To)).second;
123 assert(isNew && "Got into the map somehow?");
Evan Cheng24ac4082008-11-24 07:09:49 +0000124 isNew = isNew;
Mon P Wang0c397192008-10-30 08:01:45 +0000125 // If someone requests legalization of the new node, return itself.
126 LegalizedNodes.insert(std::make_pair(To, To));
127 }
Chris Lattner8afc48e2005-01-07 22:28:47 +0000128
Chris Lattner3e928bb2005-01-07 07:47:09 +0000129public:
Dan Gohman1002c022008-07-07 18:00:37 +0000130 explicit SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000131
Chris Lattner3e928bb2005-01-07 07:47:09 +0000132 /// getTypeAction - Return how we should legalize values of this type, either
133 /// it is already legal or we need to expand it into multiple registers of
134 /// smaller integer type, or we need to promote it to a larger type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000135 LegalizeAction getTypeAction(MVT VT) const {
Chris Lattner68a17fe2006-01-29 08:42:06 +0000136 return (LegalizeAction)ValueTypeActions.getTypeAction(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000137 }
138
139 /// isTypeLegal - Return true if this type is legal on this target.
140 ///
Duncan Sands83ec4b62008-06-06 12:08:01 +0000141 bool isTypeLegal(MVT VT) const {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000142 return getTypeAction(VT) == Legal;
143 }
144
Chris Lattner3e928bb2005-01-07 07:47:09 +0000145 void LegalizeDAG();
146
Chris Lattner456a93a2006-01-28 07:39:30 +0000147private:
Dan Gohman7f321562007-06-25 16:23:39 +0000148 /// HandleOp - Legalize, Promote, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000149 /// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000150 void HandleOp(SDValue Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000151
152 /// LegalizeOp - We know that the specified value has a legal type.
153 /// Recursively ensure that the operands have legal types, then return the
154 /// result.
Dan Gohman475871a2008-07-27 21:46:04 +0000155 SDValue LegalizeOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000156
Dan Gohman82669522007-10-11 23:57:53 +0000157 /// UnrollVectorOp - We know that the given vector has a legal type, however
158 /// the operation it performs is not legal and is an operation that we have
159 /// no way of lowering. "Unroll" the vector, splitting out the scalars and
160 /// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000161 SDValue UnrollVectorOp(SDValue O);
Nate Begeman68679912008-04-25 18:07:40 +0000162
163 /// PerformInsertVectorEltInMemory - Some target cannot handle a variable
164 /// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
165 /// is necessary to spill the vector being inserted into to memory, perform
166 /// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000167 SDValue PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val,
168 SDValue Idx);
Dan Gohman82669522007-10-11 23:57:53 +0000169
Chris Lattnerc7029802006-03-18 01:44:44 +0000170 /// PromoteOp - Given an operation that produces a value in an invalid type,
171 /// promote it to compute the value into a larger type. The produced value
172 /// will have the correct bits for the low portion of the register, but no
173 /// guarantee is made about the top bits: it may be zero, sign-extended, or
174 /// garbage.
Dan Gohman475871a2008-07-27 21:46:04 +0000175 SDValue PromoteOp(SDValue O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000176
Dan Gohman475871a2008-07-27 21:46:04 +0000177 /// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattnerc7029802006-03-18 01:44:44 +0000178 /// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this,
Dan Gohman929d3eb2008-10-01 15:07:49 +0000179 /// the LegalizedNodes map is filled in for any results that are not expanded,
Chris Lattnerc7029802006-03-18 01:44:44 +0000180 /// the ExpandedNodes map is filled in for any results that are expanded, and
181 /// the Lo/Hi values are returned. This applies to integer types and Vector
182 /// types.
Dan Gohman475871a2008-07-27 21:46:04 +0000183 void ExpandOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000184
Mon P Wangf007a8b2008-11-06 05:31:54 +0000185 /// WidenVectorOp - Widen a vector operation to a wider type given by WidenVT
186 /// (e.g., v3i32 to v4i32). The produced value will have the correct value
187 /// for the existing elements but no guarantee is made about the new elements
188 /// at the end of the vector: it may be zero, ones, or garbage. This is useful
189 /// when we have an instruction operating on an illegal vector type and we
190 /// want to widen it to do the computation on a legal wider vector type.
Mon P Wang0c397192008-10-30 08:01:45 +0000191 SDValue WidenVectorOp(SDValue Op, MVT WidenVT);
192
Dan Gohman7f321562007-06-25 16:23:39 +0000193 /// SplitVectorOp - Given an operand of vector type, break it down into
194 /// two smaller values.
Dan Gohman475871a2008-07-27 21:46:04 +0000195 void SplitVectorOp(SDValue O, SDValue &Lo, SDValue &Hi);
Chris Lattnerc7029802006-03-18 01:44:44 +0000196
Dan Gohman89b20c02007-06-27 14:06:22 +0000197 /// ScalarizeVectorOp - Given an operand of single-element vector type
198 /// (e.g. v1f32), convert it into the equivalent operation that returns a
199 /// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +0000200 SDValue ScalarizeVectorOp(SDValue O);
Chris Lattnerc7029802006-03-18 01:44:44 +0000201
Mon P Wangf007a8b2008-11-06 05:31:54 +0000202 /// Useful 16 element vector type that is used to pass operands for widening.
Mon P Wang0c397192008-10-30 08:01:45 +0000203 typedef SmallVector<SDValue, 16> SDValueVector;
204
205 /// LoadWidenVectorOp - Load a vector for a wider type. Returns true if
206 /// the LdChain contains a single load and false if it contains a token
207 /// factor for multiple loads. It takes
208 /// Result: location to return the result
209 /// LdChain: location to return the load chain
210 /// Op: load operation to widen
211 /// NVT: widen vector result type we want for the load
212 bool LoadWidenVectorOp(SDValue& Result, SDValue& LdChain,
213 SDValue Op, MVT NVT);
214
215 /// Helper genWidenVectorLoads - Helper function to generate a set of
216 /// loads to load a vector with a resulting wider type. It takes
217 /// LdChain: list of chains for the load we have generated
218 /// Chain: incoming chain for the ld vector
219 /// BasePtr: base pointer to load from
220 /// SV: memory disambiguation source value
221 /// SVOffset: memory disambiugation offset
222 /// Alignment: alignment of the memory
223 /// isVolatile: volatile load
224 /// LdWidth: width of memory that we want to load
225 /// ResType: the wider result result type for the resulting loaded vector
226 SDValue genWidenVectorLoads(SDValueVector& LdChain, SDValue Chain,
227 SDValue BasePtr, const Value *SV,
228 int SVOffset, unsigned Alignment,
229 bool isVolatile, unsigned LdWidth,
230 MVT ResType);
231
232 /// StoreWidenVectorOp - Stores a widen vector into non widen memory
233 /// location. It takes
234 /// ST: store node that we want to replace
235 /// Chain: incoming store chain
236 /// BasePtr: base address of where we want to store into
237 SDValue StoreWidenVectorOp(StoreSDNode *ST, SDValue Chain,
238 SDValue BasePtr);
239
240 /// Helper genWidenVectorStores - Helper function to generate a set of
241 /// stores to store a widen vector into non widen memory
242 // It takes
243 // StChain: list of chains for the stores we have generated
244 // Chain: incoming chain for the ld vector
245 // BasePtr: base pointer to load from
246 // SV: memory disambiguation source value
247 // SVOffset: memory disambiugation offset
248 // Alignment: alignment of the memory
249 // isVolatile: volatile lod
250 // ValOp: value to store
251 // StWidth: width of memory that we want to store
252 void genWidenVectorStores(SDValueVector& StChain, SDValue Chain,
253 SDValue BasePtr, const Value *SV,
254 int SVOffset, unsigned Alignment,
255 bool isVolatile, SDValue ValOp,
256 unsigned StWidth);
257
Duncan Sandsd038e042008-07-21 10:20:31 +0000258 /// isShuffleLegal - Return non-null if a vector shuffle is legal with the
Chris Lattner4352cc92006-04-04 17:23:26 +0000259 /// specified mask and type. Targets can specify exactly which masks they
260 /// support and the code generator is tasked with not creating illegal masks.
261 ///
262 /// Note that this will also return true for shuffles that are promoted to a
263 /// different type.
264 ///
265 /// If this is a legal shuffle, this method returns the (possibly promoted)
266 /// build_vector Mask. If it's not a legal shuffle, it returns null.
Dan Gohman475871a2008-07-27 21:46:04 +0000267 SDNode *isShuffleLegal(MVT VT, SDValue Mask) const;
Chris Lattner4352cc92006-04-04 17:23:26 +0000268
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000269 bool LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000270 SmallPtrSet<SDNode*, 32> &NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000271
Dan Gohman475871a2008-07-27 21:46:04 +0000272 void LegalizeSetCCOperands(SDValue &LHS, SDValue &RHS, SDValue &CC);
Evan Cheng7f042682008-10-15 02:05:31 +0000273 void LegalizeSetCCCondCode(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC);
274 void LegalizeSetCC(MVT VT, SDValue &LHS, SDValue &RHS, SDValue &CC) {
275 LegalizeSetCCOperands(LHS, RHS, CC);
276 LegalizeSetCCCondCode(VT, LHS, RHS, CC);
277 }
Nate Begeman750ac1b2006-02-01 07:19:44 +0000278
Dan Gohman475871a2008-07-27 21:46:04 +0000279 SDValue ExpandLibCall(RTLIB::Libcall LC, SDNode *Node, bool isSigned,
280 SDValue &Hi);
281 SDValue ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000282
Dan Gohman475871a2008-07-27 21:46:04 +0000283 SDValue EmitStackConvert(SDValue SrcOp, MVT SlotVT, MVT DestVT);
284 SDValue ExpandBUILD_VECTOR(SDNode *Node);
285 SDValue ExpandSCALAR_TO_VECTOR(SDNode *Node);
Dan Gohman7f8613e2008-08-14 20:04:46 +0000286 SDValue LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op);
Dan Gohman475871a2008-07-27 21:46:04 +0000287 SDValue ExpandLegalINT_TO_FP(bool isSigned, SDValue LegalOp, MVT DestVT);
288 SDValue PromoteLegalINT_TO_FP(SDValue LegalOp, MVT DestVT, bool isSigned);
289 SDValue PromoteLegalFP_TO_INT(SDValue LegalOp, MVT DestVT, bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000290
Dan Gohman475871a2008-07-27 21:46:04 +0000291 SDValue ExpandBSWAP(SDValue Op);
292 SDValue ExpandBitCount(unsigned Opc, SDValue Op);
293 bool ExpandShift(unsigned Opc, SDValue Op, SDValue Amt,
294 SDValue &Lo, SDValue &Hi);
295 void ExpandShiftParts(unsigned NodeOp, SDValue Op, SDValue Amt,
296 SDValue &Lo, SDValue &Hi);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000297
Dan Gohman475871a2008-07-27 21:46:04 +0000298 SDValue ExpandEXTRACT_SUBVECTOR(SDValue Op);
299 SDValue ExpandEXTRACT_VECTOR_ELT(SDValue Op);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000300};
301}
302
Chris Lattner4352cc92006-04-04 17:23:26 +0000303/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
304/// specified mask and type. Targets can specify exactly which masks they
305/// support and the code generator is tasked with not creating illegal masks.
306///
307/// Note that this will also return true for shuffles that are promoted to a
308/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000309SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000310 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
311 default: return 0;
312 case TargetLowering::Legal:
313 case TargetLowering::Custom:
314 break;
315 case TargetLowering::Promote: {
316 // If this is promoted to a different type, convert the shuffle mask and
317 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000318 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000319 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000320
321 // If we changed # elements, change the shuffle mask.
322 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000323 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000324 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
325 if (NumEltsGrowth > 1) {
326 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000327 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000328 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000329 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000330 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
331 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000332 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000333 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000334 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000335 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000336 }
337 }
338 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000339 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000340 }
341 VT = NVT;
342 break;
343 }
344 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000345 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000346}
347
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000348SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
349 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
350 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000351 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000352 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000353}
354
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000356 LastCALLSEQ_END = DAG.getEntryNode();
357 IsLegalizingCall = false;
358
Chris Lattnerab510a72005-10-02 17:49:46 +0000359 // The legalize process is inherently a bottom-up recursive process (users
360 // legalize their uses before themselves). Given infinite stack space, we
361 // could just start legalizing on the root and traverse the whole graph. In
362 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000363 // blocks. To avoid this problem, compute an ordering of the nodes where each
364 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000365 DAG.AssignTopologicalOrder();
366 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
367 E = prior(DAG.allnodes_end()); I != next(E); ++I)
368 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000369
370 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000371 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000372 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
373 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000374
375 ExpandedNodes.clear();
376 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000377 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000378 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000379 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000380 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000381
382 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000383 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000384}
385
Chris Lattner6831a812006-02-13 09:18:02 +0000386
387/// FindCallEndFromCallStart - Given a chained node that is part of a call
388/// sequence, find the CALLSEQ_END node that terminates the call sequence.
389static SDNode *FindCallEndFromCallStart(SDNode *Node) {
390 if (Node->getOpcode() == ISD::CALLSEQ_END)
391 return Node;
392 if (Node->use_empty())
393 return 0; // No CallSeqEnd
394
395 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000396 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000397 if (TheChain.getValueType() != MVT::Other) {
398 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000399 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000400 if (TheChain.getValueType() != MVT::Other) {
401 // Otherwise, hunt for it.
402 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
403 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000404 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000405 break;
406 }
407
408 // Otherwise, we walked into a node without a chain.
409 if (TheChain.getValueType() != MVT::Other)
410 return 0;
411 }
412 }
413
414 for (SDNode::use_iterator UI = Node->use_begin(),
415 E = Node->use_end(); UI != E; ++UI) {
416
417 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000418 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000419 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
420 if (User->getOperand(i) == TheChain)
421 if (SDNode *Result = FindCallEndFromCallStart(User))
422 return Result;
423 }
424 return 0;
425}
426
427/// FindCallStartFromCallEnd - Given a chained node that is part of a call
428/// sequence, find the CALLSEQ_START node that initiates the call sequence.
429static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
430 assert(Node && "Didn't find callseq_start for a call??");
431 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
432
433 assert(Node->getOperand(0).getValueType() == MVT::Other &&
434 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000435 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000436}
437
438/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
439/// see if any uses can reach Dest. If no dest operands can get to dest,
440/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000441///
442/// Keep track of the nodes we fine that actually do lead to Dest in
443/// NodesLeadingTo. This avoids retraversing them exponential number of times.
444///
445bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000446 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000447 if (N == Dest) return true; // N certainly leads to Dest :)
448
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000449 // If we've already processed this node and it does lead to Dest, there is no
450 // need to reprocess it.
451 if (NodesLeadingTo.count(N)) return true;
452
Chris Lattner6831a812006-02-13 09:18:02 +0000453 // If the first result of this node has been already legalized, then it cannot
454 // reach N.
455 switch (getTypeAction(N->getValueType(0))) {
456 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000457 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000458 break;
459 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000460 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000461 break;
462 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000463 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000464 break;
465 }
466
467 // Okay, this node has not already been legalized. Check and legalize all
468 // operands. If none lead to Dest, then we can legalize this node.
469 bool OperandsLeadToDest = false;
470 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
471 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000472 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000473
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000474 if (OperandsLeadToDest) {
475 NodesLeadingTo.insert(N);
476 return true;
477 }
Chris Lattner6831a812006-02-13 09:18:02 +0000478
479 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000480 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000481 return false;
482}
483
Mon P Wang0c397192008-10-30 08:01:45 +0000484/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000485/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000486void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000487 MVT VT = Op.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000488 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000489 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000490 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000491 case Promote:
492 if (!VT.isVector()) {
493 (void)PromoteOp(Op);
494 break;
495 }
496 else {
497 // See if we can widen otherwise use Expand to either scalarize or split
498 MVT WidenVT = TLI.getWidenVectorType(VT);
499 if (WidenVT != MVT::Other) {
500 (void) WidenVectorOp(Op, WidenVT);
501 break;
502 }
503 // else fall thru to expand since we can't widen the vector
504 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000505 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000506 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000507 // If this is an illegal scalar, expand it into its two component
508 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000509 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000510 if (Op.getOpcode() == ISD::TargetConstant)
511 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000512 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000513 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000514 // If this is an illegal single element vector, convert it to a
515 // scalar operation.
516 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000517 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000518 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000519 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000520 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000521 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000522 }
523 break;
524 }
525}
Chris Lattner6831a812006-02-13 09:18:02 +0000526
Evan Cheng9f877882006-12-13 20:57:08 +0000527/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
528/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000529static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000530 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000531 bool Extend = false;
532
533 // If a FP immediate is precise when represented as a float and if the
534 // target can do an extending load from float to double, we put it into
535 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000536 // double. This shrinks FP constants and canonicalizes them for targets where
537 // an FP extending load is the same cost as a normal load (such as on the x87
538 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000539 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000540 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000541 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000542 if (VT!=MVT::f64 && VT!=MVT::f32)
543 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000544 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000545 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000546 }
547
Duncan Sands83ec4b62008-06-06 12:08:01 +0000548 MVT OrigVT = VT;
549 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000550 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000551 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000552 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
553 // Only do this if the target has a native EXTLOAD instruction from
554 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000555 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000556 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000557 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000558 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
559 VT = SVT;
560 Extend = true;
561 }
Evan Cheng00495212006-12-12 21:32:44 +0000562 }
563
Dan Gohman475871a2008-07-27 21:46:04 +0000564 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000565 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000566 if (Extend)
567 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000568 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000569 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000570 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000571 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000572}
573
Chris Lattner6831a812006-02-13 09:18:02 +0000574
Evan Cheng912095b2007-01-04 21:56:39 +0000575/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
576/// operations.
577static
Dan Gohman475871a2008-07-27 21:46:04 +0000578SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
579 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000580 MVT VT = Node->getValueType(0);
581 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000582 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
583 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000584 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000585
Evan Cheng912095b2007-01-04 21:56:39 +0000586 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000587 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000588 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
589 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000590 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000591 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000592 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000593 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000594 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000595 if (SizeDiff > 0) {
596 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
597 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
598 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000599 } else if (SizeDiff < 0) {
600 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
601 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
602 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
603 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000604
605 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000606 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000607 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
608 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
609 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000610 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000611 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
612
613 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000614 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
615 return Result;
616}
617
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000618/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
619static
Dan Gohman475871a2008-07-27 21:46:04 +0000620SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
621 TargetLowering &TLI) {
622 SDValue Chain = ST->getChain();
623 SDValue Ptr = ST->getBasePtr();
624 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000625 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000626 int Alignment = ST->getAlignment();
627 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000628 if (ST->getMemoryVT().isFloatingPoint() ||
629 ST->getMemoryVT().isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000630 // Expand to a bitconvert of the value to the integer type of the
631 // same size, then a (misaligned) int store.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000632 MVT intVT;
633 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000634 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000635 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000636 intVT = MVT::i64;
637 else if (VT==MVT::f32)
638 intVT = MVT::i32;
639 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000640 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000641
Dan Gohman475871a2008-07-27 21:46:04 +0000642 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000643 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
644 SVOffset, ST->isVolatile(), Alignment);
645 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000646 assert(ST->getMemoryVT().isInteger() &&
647 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000648 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000649 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000650 MVT NewStoredVT =
651 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
652 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000653 int IncrementSize = NumBits / 8;
654
655 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000656 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
657 SDValue Lo = Val;
658 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000659
660 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000661 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000662 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
663 ST->getSrcValue(), SVOffset, NewStoredVT,
664 ST->isVolatile(), Alignment);
665 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
666 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000667 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000668 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
669 ST->getSrcValue(), SVOffset + IncrementSize,
670 NewStoredVT, ST->isVolatile(), Alignment);
671
672 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
673}
674
675/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
676static
Dan Gohman475871a2008-07-27 21:46:04 +0000677SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
678 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000679 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000680 SDValue Chain = LD->getChain();
681 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000682 MVT VT = LD->getValueType(0);
683 MVT LoadedVT = LD->getMemoryVT();
684 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000685 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000686 // then bitconvert to floating point or vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000687 MVT intVT;
688 if (LoadedVT.is128BitVector() ||
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000689 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000690 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000691 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000692 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000693 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000694 intVT = MVT::i32;
695 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000696 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000697
Dan Gohman475871a2008-07-27 21:46:04 +0000698 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000699 SVOffset, LD->isVolatile(),
700 LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +0000701 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000702 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000703 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
704
Dan Gohman475871a2008-07-27 21:46:04 +0000705 SDValue Ops[] = { Result, Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000706 return DAG.getMergeValues(Ops, 2);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000707 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000708 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000709 "Unaligned load of unsupported type.");
710
Dale Johannesen8155d642008-02-27 22:36:00 +0000711 // Compute the new VT that is half the size of the old one. This is an
712 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000713 unsigned NumBits = LoadedVT.getSizeInBits();
714 MVT NewLoadedVT;
715 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000716 NumBits >>= 1;
717
718 unsigned Alignment = LD->getAlignment();
719 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000720 ISD::LoadExtType HiExtType = LD->getExtensionType();
721
722 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
723 if (HiExtType == ISD::NON_EXTLOAD)
724 HiExtType = ISD::ZEXTLOAD;
725
726 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000727 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000728 if (TLI.isLittleEndian()) {
729 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
730 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
731 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
732 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
733 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
734 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000735 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000736 } else {
737 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
738 NewLoadedVT,LD->isVolatile(), Alignment);
739 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
740 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
741 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
742 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000743 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000744 }
745
746 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000747 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
748 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000749 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
750
Dan Gohman475871a2008-07-27 21:46:04 +0000751 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000752 Hi.getValue(1));
753
Dan Gohman475871a2008-07-27 21:46:04 +0000754 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000755 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000756}
Evan Cheng912095b2007-01-04 21:56:39 +0000757
Dan Gohman82669522007-10-11 23:57:53 +0000758/// UnrollVectorOp - We know that the given vector has a legal type, however
759/// the operation it performs is not legal and is an operation that we have
760/// no way of lowering. "Unroll" the vector, splitting out the scalars and
761/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000762SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000763 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000764 assert(isTypeLegal(VT) &&
765 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000766 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000767 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000768 unsigned NE = VT.getVectorNumElements();
769 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000770
Dan Gohman475871a2008-07-27 21:46:04 +0000771 SmallVector<SDValue, 8> Scalars;
772 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000773 for (unsigned i = 0; i != NE; ++i) {
774 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000775 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000776 MVT OperandVT = Operand.getValueType();
777 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000778 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000779 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000780 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
781 OperandEltVT,
782 Operand,
783 DAG.getConstant(i, MVT::i32));
784 } else {
785 // A scalar operand; just use it as is.
786 Operands[j] = Operand;
787 }
788 }
789 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
790 &Operands[0], Operands.size()));
791 }
792
793 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
794}
795
Duncan Sands007f9842008-01-10 10:28:30 +0000796/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000797static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000798 RTLIB::Libcall Call_F32,
799 RTLIB::Libcall Call_F64,
800 RTLIB::Libcall Call_F80,
801 RTLIB::Libcall Call_PPCF128) {
802 return
803 VT == MVT::f32 ? Call_F32 :
804 VT == MVT::f64 ? Call_F64 :
805 VT == MVT::f80 ? Call_F80 :
806 VT == MVT::ppcf128 ? Call_PPCF128 :
807 RTLIB::UNKNOWN_LIBCALL;
808}
809
Nate Begeman68679912008-04-25 18:07:40 +0000810/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
811/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
812/// is necessary to spill the vector being inserted into to memory, perform
813/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000814SDValue SelectionDAGLegalize::
815PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
816 SDValue Tmp1 = Vec;
817 SDValue Tmp2 = Val;
818 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000819
820 // If the target doesn't support this, we have to spill the input vector
821 // to a temporary stack slot, update the element, then reload it. This is
822 // badness. We could also load the value into a vector register (either
823 // with a "move to register" or "extload into register" instruction, then
824 // permute it into place, if the idx is a constant and if the idx is
825 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000826 MVT VT = Tmp1.getValueType();
827 MVT EltVT = VT.getVectorElementType();
828 MVT IdxVT = Tmp3.getValueType();
829 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000830 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000831
Gabor Greifba36cb52008-08-28 21:40:38 +0000832 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000833
834 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000835 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000836 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000837
838 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000839 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000840 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
841 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000842 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000843 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000844 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000845 // Store the scalar value.
846 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000847 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000848 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000849 return DAG.getLoad(VT, Ch, StackPtr,
850 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000851}
852
Dan Gohmana3466152007-07-13 20:14:11 +0000853/// LegalizeOp - We know that the specified value has a legal type, and
854/// that its operands are legal. Now ensure that the operation itself
855/// is legal, recursively ensuring that the operands' operations remain
856/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000857SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000858 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
859 return Op;
860
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000861 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000862 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000863 SDNode *Node = Op.getNode();
Chris Lattnere3304a32005-01-08 20:35:13 +0000864
Chris Lattner3e928bb2005-01-07 07:47:09 +0000865 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000866 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000867 if (Node->getNumValues() > 1) {
868 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000869 if (getTypeAction(Node->getValueType(i)) != Legal) {
870 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000871 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000872 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000873 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000874 }
875 }
876
Chris Lattner45982da2005-05-12 16:53:42 +0000877 // Note that LegalizeOp may be reentered even from single-use nodes, which
878 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000879 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000880 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000881
Dan Gohman475871a2008-07-27 21:46:04 +0000882 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
883 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000884 bool isCustom = false;
885
Chris Lattner3e928bb2005-01-07 07:47:09 +0000886 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000887 case ISD::FrameIndex:
888 case ISD::EntryToken:
889 case ISD::Register:
890 case ISD::BasicBlock:
891 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000892 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000893 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000894 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000895 case ISD::TargetConstantPool:
896 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000897 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000898 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +0000899 case ISD::VALUETYPE:
900 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000901 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000902 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +0000903 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +0000904 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000905 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000906 "This must be legal!");
907 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000908 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000909 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
910 // If this is a target node, legalize it by legalizing the operands then
911 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +0000912 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000913 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000914 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000915
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000916 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000917
918 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
919 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000920 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000921 }
922 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000923#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000924 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000925#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000926 assert(0 && "Do not know how to legalize this operator!");
927 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000928 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000929 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000930 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000931 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000932 case ISD::ConstantPool:
933 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000934 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
935 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000936 case TargetLowering::Custom:
937 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000938 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +0000939 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000940 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000941 break;
942 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000943 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000944 case ISD::FRAMEADDR:
945 case ISD::RETURNADDR:
946 // The only option for these nodes is to custom lower them. If the target
947 // does not custom lower them, then return zero.
948 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000949 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +0000950 Result = Tmp1;
951 else
952 Result = DAG.getConstant(0, TLI.getPointerTy());
953 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000954 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000955 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000956 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
957 default: assert(0 && "This action is not supported yet!");
958 case TargetLowering::Custom:
959 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000960 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000961 // Fall Thru
962 case TargetLowering::Legal:
963 Result = DAG.getConstant(0, VT);
964 break;
965 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000966 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000967 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000968 case ISD::EXCEPTIONADDR: {
969 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000970 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000971 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
972 default: assert(0 && "This action is not supported yet!");
973 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000974 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +0000975 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000976 }
977 break;
978 case TargetLowering::Custom:
979 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000980 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000981 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000982 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +0000983 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000984 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000985 break;
986 }
987 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +0000988 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000989 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +0000990
Gabor Greifba36cb52008-08-28 21:40:38 +0000991 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +0000992 "Cannot return more than two values!");
993
994 // Since we produced two values, make sure to remember that we
995 // legalized both of them.
996 Tmp1 = LegalizeOp(Result);
997 Tmp2 = LegalizeOp(Result.getValue(1));
998 AddLegalizedOperand(Op.getValue(0), Tmp1);
999 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001000 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001001 case ISD::EHSELECTION: {
1002 Tmp1 = LegalizeOp(Node->getOperand(0));
1003 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001004 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001005 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1006 default: assert(0 && "This action is not supported yet!");
1007 case TargetLowering::Expand: {
1008 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001009 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001010 }
1011 break;
1012 case TargetLowering::Custom:
1013 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001014 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001015 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001016 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001017 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001018 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001019 break;
1020 }
1021 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001022 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001023 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001024
Gabor Greifba36cb52008-08-28 21:40:38 +00001025 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001026 "Cannot return more than two values!");
1027
1028 // Since we produced two values, make sure to remember that we
1029 // legalized both of them.
1030 Tmp1 = LegalizeOp(Result);
1031 Tmp2 = LegalizeOp(Result.getValue(1));
1032 AddLegalizedOperand(Op.getValue(0), Tmp1);
1033 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001034 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001035 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001036 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001037 // The only "good" option for this node is to custom lower it.
1038 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1039 default: assert(0 && "This action is not supported at all!");
1040 case TargetLowering::Custom:
1041 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001042 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001043 // Fall Thru
1044 case TargetLowering::Legal:
1045 // Target does not know, how to lower this, lower to noop
1046 Result = LegalizeOp(Node->getOperand(0));
1047 break;
1048 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001049 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001050 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001051 case ISD::AssertSext:
1052 case ISD::AssertZext:
1053 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001054 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001055 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001056 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001057 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001058 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001059 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001060 case ISD::CopyFromReg:
1061 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001062 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001063 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001064 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001065 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001066 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001067 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001068 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1070 } else {
1071 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1072 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001073 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1074 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001075 // Since CopyFromReg produces two values, make sure to remember that we
1076 // legalized both of them.
1077 AddLegalizedOperand(Op.getValue(0), Result);
1078 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001079 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001080 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001081 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001082 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001083 default: assert(0 && "This action is not supported yet!");
1084 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001085 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001086 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001087 else if (VT.isFloatingPoint())
1088 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001089 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001090 else
1091 assert(0 && "Unknown value type!");
1092 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001093 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001094 break;
1095 }
1096 break;
1097 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001098
Chris Lattner48b61a72006-03-28 00:40:33 +00001099 case ISD::INTRINSIC_W_CHAIN:
1100 case ISD::INTRINSIC_WO_CHAIN:
1101 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001102 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001103 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1104 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001105 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001106
1107 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001108 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001109 TargetLowering::Custom) {
1110 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001111 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001112 }
1113
Gabor Greifba36cb52008-08-28 21:40:38 +00001114 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001115
1116 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001117 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001118 "Cannot return more than two values!");
1119
1120 // Since loads produce two values, make sure to remember that we
1121 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001122 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1123 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001124 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001125 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001126
Dan Gohman7f460202008-06-30 20:59:49 +00001127 case ISD::DBG_STOPPOINT:
1128 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001129 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1130
Dan Gohman7f460202008-06-30 20:59:49 +00001131 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001132 case TargetLowering::Promote:
1133 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001134 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001135 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001136 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohman44066042008-07-01 00:05:16 +00001137 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001138
Dan Gohman7f460202008-06-30 20:59:49 +00001139 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001140 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman7f460202008-06-30 20:59:49 +00001141 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1142 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001143
Dan Gohman7f460202008-06-30 20:59:49 +00001144 unsigned Line = DSP->getLine();
1145 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001146
1147 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001148 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001149 DAG.getConstant(Col, MVT::i32),
1150 DAG.getConstant(SrcFile, MVT::i32) };
1151 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001152 } else {
Evan Chenga647c922008-02-01 02:05:57 +00001153 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001154 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001155 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001156 } else {
1157 Result = Tmp1; // chain
1158 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001159 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001160 }
Evan Cheng71e86852008-07-08 20:06:39 +00001161 case TargetLowering::Legal: {
1162 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1163 if (Action == Legal && Tmp1 == Node->getOperand(0))
1164 break;
1165
Dan Gohman475871a2008-07-27 21:46:04 +00001166 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001167 Ops.push_back(Tmp1);
1168 if (Action == Legal) {
1169 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1170 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1171 } else {
1172 // Otherwise promote them.
1173 Ops.push_back(PromoteOp(Node->getOperand(1)));
1174 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001175 }
Evan Cheng71e86852008-07-08 20:06:39 +00001176 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1177 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1178 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001179 break;
1180 }
Evan Cheng71e86852008-07-08 20:06:39 +00001181 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001182 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001183
1184 case ISD::DECLARE:
1185 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1186 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1187 default: assert(0 && "This action is not supported yet!");
1188 case TargetLowering::Legal:
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1190 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1191 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1192 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1193 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001194 case TargetLowering::Expand:
1195 Result = LegalizeOp(Node->getOperand(0));
1196 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001197 }
1198 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001199
1200 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001201 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001202 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001203 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001204 case TargetLowering::Legal: {
1205 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001206 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001207 if (Action == Legal && Tmp1 == Node->getOperand(0))
1208 break;
1209 if (Action == Legal) {
1210 Tmp2 = Node->getOperand(1);
1211 Tmp3 = Node->getOperand(2);
1212 Tmp4 = Node->getOperand(3);
1213 } else {
1214 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1215 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1216 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1217 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001218 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001219 break;
1220 }
Evan Cheng71e86852008-07-08 20:06:39 +00001221 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001222 break;
1223
Dan Gohman44066042008-07-01 00:05:16 +00001224 case ISD::DBG_LABEL:
1225 case ISD::EH_LABEL:
1226 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1227 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001228 default: assert(0 && "This action is not supported yet!");
1229 case TargetLowering::Legal:
1230 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001231 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001232 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001233 case TargetLowering::Expand:
1234 Result = LegalizeOp(Node->getOperand(0));
1235 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001236 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001237 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001238
Evan Cheng27b7db52008-03-08 00:58:38 +00001239 case ISD::PREFETCH:
1240 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1241 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1242 default: assert(0 && "This action is not supported yet!");
1243 case TargetLowering::Legal:
1244 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1245 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1246 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1247 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1248 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1249 break;
1250 case TargetLowering::Expand:
1251 // It's a noop.
1252 Result = LegalizeOp(Node->getOperand(0));
1253 break;
1254 }
1255 break;
1256
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001257 case ISD::MEMBARRIER: {
1258 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001259 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1260 default: assert(0 && "This action is not supported yet!");
1261 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001262 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001263 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001264 for (int x = 1; x < 6; ++x) {
1265 Ops[x] = Node->getOperand(x);
1266 if (!isTypeLegal(Ops[x].getValueType()))
1267 Ops[x] = PromoteOp(Ops[x]);
1268 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001269 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1270 break;
1271 }
1272 case TargetLowering::Expand:
1273 //There is no libgcc call for this op
1274 Result = Node->getOperand(0); // Noop
1275 break;
1276 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001277 break;
1278 }
1279
Dale Johannesene00a8a22008-08-28 02:44:49 +00001280 case ISD::ATOMIC_CMP_SWAP_8:
1281 case ISD::ATOMIC_CMP_SWAP_16:
1282 case ISD::ATOMIC_CMP_SWAP_32:
1283 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001284 unsigned int num_operands = 4;
1285 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001286 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001287 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001288 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001289 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1290
1291 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1292 default: assert(0 && "This action is not supported yet!");
1293 case TargetLowering::Custom:
1294 Result = TLI.LowerOperation(Result, DAG);
1295 break;
1296 case TargetLowering::Legal:
1297 break;
1298 }
Dan Gohman475871a2008-07-27 21:46:04 +00001299 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1300 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001301 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001302 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00001303 case ISD::ATOMIC_LOAD_ADD_8:
1304 case ISD::ATOMIC_LOAD_SUB_8:
1305 case ISD::ATOMIC_LOAD_AND_8:
1306 case ISD::ATOMIC_LOAD_OR_8:
1307 case ISD::ATOMIC_LOAD_XOR_8:
1308 case ISD::ATOMIC_LOAD_NAND_8:
1309 case ISD::ATOMIC_LOAD_MIN_8:
1310 case ISD::ATOMIC_LOAD_MAX_8:
1311 case ISD::ATOMIC_LOAD_UMIN_8:
1312 case ISD::ATOMIC_LOAD_UMAX_8:
1313 case ISD::ATOMIC_SWAP_8:
1314 case ISD::ATOMIC_LOAD_ADD_16:
1315 case ISD::ATOMIC_LOAD_SUB_16:
1316 case ISD::ATOMIC_LOAD_AND_16:
1317 case ISD::ATOMIC_LOAD_OR_16:
1318 case ISD::ATOMIC_LOAD_XOR_16:
1319 case ISD::ATOMIC_LOAD_NAND_16:
1320 case ISD::ATOMIC_LOAD_MIN_16:
1321 case ISD::ATOMIC_LOAD_MAX_16:
1322 case ISD::ATOMIC_LOAD_UMIN_16:
1323 case ISD::ATOMIC_LOAD_UMAX_16:
1324 case ISD::ATOMIC_SWAP_16:
1325 case ISD::ATOMIC_LOAD_ADD_32:
1326 case ISD::ATOMIC_LOAD_SUB_32:
1327 case ISD::ATOMIC_LOAD_AND_32:
1328 case ISD::ATOMIC_LOAD_OR_32:
1329 case ISD::ATOMIC_LOAD_XOR_32:
1330 case ISD::ATOMIC_LOAD_NAND_32:
1331 case ISD::ATOMIC_LOAD_MIN_32:
1332 case ISD::ATOMIC_LOAD_MAX_32:
1333 case ISD::ATOMIC_LOAD_UMIN_32:
1334 case ISD::ATOMIC_LOAD_UMAX_32:
1335 case ISD::ATOMIC_SWAP_32:
1336 case ISD::ATOMIC_LOAD_ADD_64:
1337 case ISD::ATOMIC_LOAD_SUB_64:
1338 case ISD::ATOMIC_LOAD_AND_64:
1339 case ISD::ATOMIC_LOAD_OR_64:
1340 case ISD::ATOMIC_LOAD_XOR_64:
1341 case ISD::ATOMIC_LOAD_NAND_64:
1342 case ISD::ATOMIC_LOAD_MIN_64:
1343 case ISD::ATOMIC_LOAD_MAX_64:
1344 case ISD::ATOMIC_LOAD_UMIN_64:
1345 case ISD::ATOMIC_LOAD_UMAX_64:
1346 case ISD::ATOMIC_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001347 unsigned int num_operands = 3;
1348 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001349 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001350 for (unsigned int x = 0; x < num_operands; ++x)
1351 Ops[x] = LegalizeOp(Node->getOperand(x));
1352 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001353
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001354 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001355 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001356 case TargetLowering::Custom:
1357 Result = TLI.LowerOperation(Result, DAG);
1358 break;
1359 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001360 break;
1361 }
Dan Gohman475871a2008-07-27 21:46:04 +00001362 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1363 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001364 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001365 }
Scott Michelc1513d22007-08-08 23:23:31 +00001366 case ISD::Constant: {
1367 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1368 unsigned opAction =
1369 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1370
Chris Lattner3e928bb2005-01-07 07:47:09 +00001371 // We know we don't need to expand constants here, constants only have one
1372 // value and we check that it is fine above.
1373
Scott Michelc1513d22007-08-08 23:23:31 +00001374 if (opAction == TargetLowering::Custom) {
1375 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001376 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001377 Result = Tmp1;
1378 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001379 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001380 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001381 case ISD::ConstantFP: {
1382 // Spill FP immediates to the constant pool if the target cannot directly
1383 // codegen them. Targets often have some immediate values that can be
1384 // efficiently generated into an FP register without a load. We explicitly
1385 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001386 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1387
Chris Lattner3181a772006-01-29 06:26:56 +00001388 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1389 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001390 case TargetLowering::Legal:
1391 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001392 case TargetLowering::Custom:
1393 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001394 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001395 Result = Tmp3;
1396 break;
1397 }
1398 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001399 case TargetLowering::Expand: {
1400 // Check to see if this FP immediate is already legal.
1401 bool isLegal = false;
1402 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1403 E = TLI.legal_fpimm_end(); I != E; ++I) {
1404 if (CFP->isExactlyValue(*I)) {
1405 isLegal = true;
1406 break;
1407 }
1408 }
1409 // If this is a legal constant, turn it into a TargetConstantFP node.
1410 if (isLegal)
1411 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001412 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001413 }
Nate Begemane1795842008-02-14 08:57:00 +00001414 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001415 break;
1416 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001417 case ISD::TokenFactor:
1418 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001419 Tmp1 = LegalizeOp(Node->getOperand(0));
1420 Tmp2 = LegalizeOp(Node->getOperand(1));
1421 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1422 } else if (Node->getNumOperands() == 3) {
1423 Tmp1 = LegalizeOp(Node->getOperand(0));
1424 Tmp2 = LegalizeOp(Node->getOperand(1));
1425 Tmp3 = LegalizeOp(Node->getOperand(2));
1426 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001427 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001428 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001429 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001430 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1431 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001432 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001433 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001434 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001435
1436 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001437 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001438 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001439 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001440 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001441 // A call within a calling sequence must be legalized to something
1442 // other than the normal CALLSEQ_END. Violating this gets Legalize
1443 // into an infinite loop.
1444 assert ((!IsLegalizingCall ||
1445 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001446 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001447 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001448
1449 // The number of incoming and outgoing values should match; unless the final
1450 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001451 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1452 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1453 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001454 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001455 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001456
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001457 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001458 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001459 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1460 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001461 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001462 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001463 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001464 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001465 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001466 }
1467 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001468 case ISD::EXTRACT_SUBREG: {
1469 Tmp1 = LegalizeOp(Node->getOperand(0));
1470 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1471 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001472 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001473 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1474 }
1475 break;
1476 case ISD::INSERT_SUBREG: {
1477 Tmp1 = LegalizeOp(Node->getOperand(0));
1478 Tmp2 = LegalizeOp(Node->getOperand(1));
1479 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1480 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001481 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001482 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1483 }
1484 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001485 case ISD::BUILD_VECTOR:
1486 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001487 default: assert(0 && "This action is not supported yet!");
1488 case TargetLowering::Custom:
1489 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001490 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001491 Result = Tmp3;
1492 break;
1493 }
1494 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001495 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001496 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001497 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001498 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001499 break;
1500 case ISD::INSERT_VECTOR_ELT:
1501 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001502 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001503
1504 // The type of the value to insert may not be legal, even though the vector
1505 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1506 // here.
1507 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1508 default: assert(0 && "Cannot expand insert element operand");
1509 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1510 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001511 case Expand:
1512 // FIXME: An alternative would be to check to see if the target is not
1513 // going to custom lower this operation, we could bitcast to half elt
1514 // width and perform two inserts at that width, if that is legal.
1515 Tmp2 = Node->getOperand(1);
1516 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001517 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001518 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1519
1520 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1521 Node->getValueType(0))) {
1522 default: assert(0 && "This action is not supported yet!");
1523 case TargetLowering::Legal:
1524 break;
1525 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001526 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001527 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001528 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001529 break;
1530 }
1531 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001532 case TargetLowering::Promote:
1533 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001534 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001535 // If the insert index is a constant, codegen this as a scalar_to_vector,
1536 // then a shuffle that inserts it into the right position in the vector.
1537 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001538 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1539 // match the element type of the vector being created.
1540 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001541 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001542 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001543 Tmp1.getValueType(), Tmp2);
1544
Duncan Sands83ec4b62008-06-06 12:08:01 +00001545 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1546 MVT ShufMaskVT =
1547 MVT::getIntVectorWithNumElements(NumElts);
1548 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001549
1550 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1551 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1552 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001553 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001554 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001555 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001556 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1557 else
1558 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1559 }
Dan Gohman475871a2008-07-27 21:46:04 +00001560 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001561 &ShufOps[0], ShufOps.size());
1562
1563 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1564 Tmp1, ScVec, ShufMask);
1565 Result = LegalizeOp(Result);
1566 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001567 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001568 }
Nate Begeman68679912008-04-25 18:07:40 +00001569 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001570 break;
1571 }
1572 }
1573 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001574 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001575 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1576 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1577 break;
1578 }
1579
Chris Lattnerce872152006-03-19 06:31:19 +00001580 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1581 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1582 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1583 Node->getValueType(0))) {
1584 default: assert(0 && "This action is not supported yet!");
1585 case TargetLowering::Legal:
1586 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001587 case TargetLowering::Custom:
1588 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001589 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001590 Result = Tmp3;
1591 break;
1592 }
1593 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001594 case TargetLowering::Expand:
1595 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001596 break;
1597 }
Chris Lattnerce872152006-03-19 06:31:19 +00001598 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001599 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001600 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1601 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1602 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1603
1604 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001605 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1606 default: assert(0 && "Unknown operation action!");
1607 case TargetLowering::Legal:
1608 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1609 "vector shuffle should not be created if not legal!");
1610 break;
1611 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001612 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001613 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001614 Result = Tmp3;
1615 break;
1616 }
1617 // FALLTHROUGH
1618 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001619 MVT VT = Node->getValueType(0);
1620 MVT EltVT = VT.getVectorElementType();
1621 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001622 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001623 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001624 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001625 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001626 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001627 if (Arg.getOpcode() == ISD::UNDEF) {
1628 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1629 } else {
1630 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001631 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001632 if (Idx < NumElems)
1633 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1634 DAG.getConstant(Idx, PtrVT)));
1635 else
1636 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1637 DAG.getConstant(Idx - NumElems, PtrVT)));
1638 }
1639 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001640 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001641 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001642 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001643 case TargetLowering::Promote: {
1644 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001645 MVT OVT = Node->getValueType(0);
1646 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001647
1648 // Cast the two input vectors.
1649 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1650 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1651
1652 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001653 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001654 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001655 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1656 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1657 break;
1658 }
Chris Lattner87100e02006-03-20 01:52:29 +00001659 }
1660 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001661
1662 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001663 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001664 Tmp2 = LegalizeOp(Node->getOperand(1));
1665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001666 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001667 break;
1668
Dan Gohman7f321562007-06-25 16:23:39 +00001669 case ISD::EXTRACT_SUBVECTOR:
1670 Tmp1 = Node->getOperand(0);
1671 Tmp2 = LegalizeOp(Node->getOperand(1));
1672 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1673 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001674 break;
1675
Mon P Wang0c397192008-10-30 08:01:45 +00001676 case ISD::CONCAT_VECTORS: {
1677 // Use extract/insert/build vector for now. We might try to be
1678 // more clever later.
1679 MVT PtrVT = TLI.getPointerTy();
1680 SmallVector<SDValue, 8> Ops;
1681 unsigned NumOperands = Node->getNumOperands();
1682 for (unsigned i=0; i < NumOperands; ++i) {
1683 SDValue SubOp = Node->getOperand(i);
1684 MVT VVT = SubOp.getNode()->getValueType(0);
1685 MVT EltVT = VVT.getVectorElementType();
1686 unsigned NumSubElem = VVT.getVectorNumElements();
1687 for (unsigned j=0; j < NumSubElem; ++j) {
1688 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1689 DAG.getConstant(j, PtrVT)));
1690 }
1691 }
1692 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1693 &Ops[0], Ops.size()));
1694 }
1695
Chris Lattner6831a812006-02-13 09:18:02 +00001696 case ISD::CALLSEQ_START: {
1697 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1698
1699 // Recursively Legalize all of the inputs of the call end that do not lead
1700 // to this call start. This ensures that any libcalls that need be inserted
1701 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001702 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001703 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001704 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001705 NodesLeadingTo);
1706 }
Chris Lattner6831a812006-02-13 09:18:02 +00001707
1708 // Now that we legalized all of the inputs (which may have inserted
1709 // libcalls) create the new CALLSEQ_START node.
1710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1711
1712 // Merge in the last call, to ensure that this call start after the last
1713 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001714 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001715 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1716 Tmp1 = LegalizeOp(Tmp1);
1717 }
Chris Lattner6831a812006-02-13 09:18:02 +00001718
1719 // Do not try to legalize the target-specific arguments (#1+).
1720 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001721 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001722 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001723 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001724 }
1725
1726 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001727 AddLegalizedOperand(Op.getValue(0), Result);
1728 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1729 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1730
Chris Lattner6831a812006-02-13 09:18:02 +00001731 // Now that the callseq_start and all of the non-call nodes above this call
1732 // sequence have been legalized, legalize the call itself. During this
1733 // process, no libcalls can/will be inserted, guaranteeing that no calls
1734 // can overlap.
1735 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001736 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001737 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001738 IsLegalizingCall = true;
1739
1740 // Legalize the call, starting from the CALLSEQ_END.
1741 LegalizeOp(LastCALLSEQ_END);
1742 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1743 return Result;
1744 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001745 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001746 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1747 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001748 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001749 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1750 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001751 assert(I != LegalizedNodes.end() &&
1752 "Legalizing the call start should have legalized this node!");
1753 return I->second;
1754 }
1755
1756 // Otherwise, the call start has been legalized and everything is going
1757 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001758 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001759 // Do not try to legalize the target-specific arguments (#1+), except for
1760 // an optional flag input.
1761 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1762 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001763 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001764 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001765 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001766 }
1767 } else {
1768 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1769 if (Tmp1 != Node->getOperand(0) ||
1770 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001771 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001772 Ops[0] = Tmp1;
1773 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001774 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001775 }
Chris Lattner6a542892006-01-24 05:48:21 +00001776 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001777 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001778 // This finishes up call legalization.
1779 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001780
1781 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001782 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001783 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001784 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001785 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001786 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001787 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1789 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1790 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001791 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001792
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001793 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001794 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001795 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001796 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001797 case TargetLowering::Expand: {
1798 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1799 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1800 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001801 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001802
1803 // Chain the dynamic stack allocation so that it doesn't modify the stack
1804 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001805 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001806
Dan Gohman475871a2008-07-27 21:46:04 +00001807 SDValue Size = Tmp2.getOperand(1);
1808 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001809 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001810 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001811 unsigned StackAlign =
1812 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1813 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001814 SP = DAG.getNode(ISD::AND, VT, SP,
1815 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001816 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001817 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1818
Chris Lattnere563bbc2008-10-11 22:08:30 +00001819 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1820 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001821
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001822 Tmp1 = LegalizeOp(Tmp1);
1823 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001824 break;
1825 }
1826 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001827 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001828 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001829 Tmp1 = LegalizeOp(Tmp3);
1830 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001831 }
Chris Lattner903d2782006-01-15 08:54:32 +00001832 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001833 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001834 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001835 }
Chris Lattner903d2782006-01-15 08:54:32 +00001836 // Since this op produce two values, make sure to remember that we
1837 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001838 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1839 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001840 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001841 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001842 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001843 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001844 bool Changed = false;
1845 // Legalize all of the operands of the inline asm, in case they are nodes
1846 // that need to be expanded or something. Note we skip the asm string and
1847 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001848 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001849 Changed = Op != Ops[0];
1850 Ops[0] = Op;
1851
1852 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1853 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001854 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001855 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001856 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001857 if (Op != Ops[i]) {
1858 Changed = true;
1859 Ops[i] = Op;
1860 }
1861 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001862 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001863
1864 if (HasInFlag) {
1865 Op = LegalizeOp(Ops.back());
1866 Changed |= Op != Ops.back();
1867 Ops.back() = Op;
1868 }
1869
1870 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001871 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001872
1873 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001874 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1875 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001876 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001877 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001878 case ISD::BR:
1879 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001880 // Ensure that libcalls are emitted before a branch.
1881 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1882 Tmp1 = LegalizeOp(Tmp1);
1883 LastCALLSEQ_END = DAG.getEntryNode();
1884
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001885 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001886 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001887 case ISD::BRIND:
1888 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1889 // Ensure that libcalls are emitted before a branch.
1890 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1891 Tmp1 = LegalizeOp(Tmp1);
1892 LastCALLSEQ_END = DAG.getEntryNode();
1893
1894 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1895 default: assert(0 && "Indirect target must be legal type (pointer)!");
1896 case Legal:
1897 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1898 break;
1899 }
1900 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1901 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001902 case ISD::BR_JT:
1903 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1904 // Ensure that libcalls are emitted before a branch.
1905 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1906 Tmp1 = LegalizeOp(Tmp1);
1907 LastCALLSEQ_END = DAG.getEntryNode();
1908
1909 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1911
1912 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1913 default: assert(0 && "This action is not supported yet!");
1914 case TargetLowering::Legal: break;
1915 case TargetLowering::Custom:
1916 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001917 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001918 break;
1919 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001920 SDValue Chain = Result.getOperand(0);
1921 SDValue Table = Result.getOperand(1);
1922 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001923
Duncan Sands83ec4b62008-06-06 12:08:01 +00001924 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001925 MachineFunction &MF = DAG.getMachineFunction();
1926 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001927 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00001928 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001929
Dan Gohman475871a2008-07-27 21:46:04 +00001930 SDValue LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001931 switch (EntrySize) {
1932 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001933 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001934 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001935 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001936 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001937 }
1938
Evan Chengcc415862007-11-09 01:32:10 +00001939 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001940 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001941 // For PIC, the sequence is:
1942 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001943 // RelocBase can be JumpTable, GOT or some sort of global base.
1944 if (PTy != MVT::i32)
1945 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1946 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1947 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001948 }
Evan Chengcc415862007-11-09 01:32:10 +00001949 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001950 }
1951 }
1952 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001953 case ISD::BRCOND:
1954 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001955 // Ensure that libcalls are emitted before a return.
1956 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1957 Tmp1 = LegalizeOp(Tmp1);
1958 LastCALLSEQ_END = DAG.getEntryNode();
1959
Chris Lattner47e92232005-01-18 19:27:06 +00001960 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1961 case Expand: assert(0 && "It's impossible to expand bools");
1962 case Legal:
1963 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1964 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001965 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001966 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001967
1968 // The top bits of the promoted condition are not necessarily zero, ensure
1969 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001970 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001971 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001972 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001973 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001974 break;
1975 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001976 }
Chris Lattner456a93a2006-01-28 07:39:30 +00001977
1978 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001979 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001980
1981 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
1982 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00001983 case TargetLowering::Legal: break;
1984 case TargetLowering::Custom:
1985 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001986 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00001987 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00001988 case TargetLowering::Expand:
1989 // Expand brcond's setcc into its constituent parts and create a BR_CC
1990 // Node.
1991 if (Tmp2.getOpcode() == ISD::SETCC) {
1992 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
1993 Tmp2.getOperand(0), Tmp2.getOperand(1),
1994 Node->getOperand(2));
1995 } else {
1996 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
1997 DAG.getCondCode(ISD::SETNE), Tmp2,
1998 DAG.getConstant(0, Tmp2.getValueType()),
1999 Node->getOperand(2));
2000 }
2001 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002002 }
2003 break;
2004 case ISD::BR_CC:
2005 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002006 // Ensure that libcalls are emitted before a branch.
2007 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2008 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002009 Tmp2 = Node->getOperand(2); // LHS
2010 Tmp3 = Node->getOperand(3); // RHS
2011 Tmp4 = Node->getOperand(1); // CC
2012
Dale Johannesen53e4e442008-11-07 22:54:33 +00002013 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002014 LastCALLSEQ_END = DAG.getEntryNode();
2015
Evan Cheng7f042682008-10-15 02:05:31 +00002016 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002017 // the LHS is a legal SETCC itself. In this case, we need to compare
2018 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002019 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002020 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2021 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002022 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002023
2024 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2025 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002026
Chris Lattner181b7a32005-12-17 23:46:46 +00002027 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2028 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002029 case TargetLowering::Legal: break;
2030 case TargetLowering::Custom:
2031 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002032 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002033 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002034 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002035 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002036 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002037 LoadSDNode *LD = cast<LoadSDNode>(Node);
2038 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2039 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002040
Evan Cheng466685d2006-10-09 20:57:25 +00002041 ISD::LoadExtType ExtType = LD->getExtensionType();
2042 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002043 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002044 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2045 Tmp3 = Result.getValue(0);
2046 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002047
Evan Cheng466685d2006-10-09 20:57:25 +00002048 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2049 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002050 case TargetLowering::Legal:
2051 // If this is an unaligned load and the target doesn't support it,
2052 // expand it.
2053 if (!TLI.allowsUnalignedMemoryAccesses()) {
2054 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002055 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002056 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002057 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002058 TLI);
2059 Tmp3 = Result.getOperand(0);
2060 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002061 Tmp3 = LegalizeOp(Tmp3);
2062 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002063 }
2064 }
2065 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002066 case TargetLowering::Custom:
2067 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002068 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002069 Tmp3 = LegalizeOp(Tmp1);
2070 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002071 }
Evan Cheng466685d2006-10-09 20:57:25 +00002072 break;
2073 case TargetLowering::Promote: {
2074 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002075 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002076 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002077 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002078
2079 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002080 LD->getSrcValueOffset(),
2081 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002082 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2083 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002084 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002085 }
Evan Cheng466685d2006-10-09 20:57:25 +00002086 }
2087 // Since loads produce two values, make sure to remember that we
2088 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002089 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2090 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002091 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002092 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002093 MVT SrcVT = LD->getMemoryVT();
2094 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002095 int SVOffset = LD->getSrcValueOffset();
2096 unsigned Alignment = LD->getAlignment();
2097 bool isVolatile = LD->isVolatile();
2098
Duncan Sands83ec4b62008-06-06 12:08:01 +00002099 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002100 // Some targets pretend to have an i1 loading operation, and actually
2101 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2102 // bits are guaranteed to be zero; it helps the optimizers understand
2103 // that these bits are zero. It is also useful for EXTLOAD, since it
2104 // tells the optimizers that those bits are undefined. It would be
2105 // nice to have an effective generic way of getting these benefits...
2106 // Until such a way is found, don't insist on promoting i1 here.
2107 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002108 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002109 // Promote to a byte-sized load if not loading an integral number of
2110 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002111 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2112 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002113 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002114
2115 // The extra bits are guaranteed to be zero, since we stored them that
2116 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2117
2118 ISD::LoadExtType NewExtType =
2119 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2120
2121 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2122 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2123 NVT, isVolatile, Alignment);
2124
2125 Ch = Result.getValue(1); // The chain.
2126
2127 if (ExtType == ISD::SEXTLOAD)
2128 // Having the top bits zero doesn't help when sign extending.
2129 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2130 Result, DAG.getValueType(SrcVT));
2131 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2132 // All the top bits are guaranteed to be zero - inform the optimizers.
2133 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2134 DAG.getValueType(SrcVT));
2135
2136 Tmp1 = LegalizeOp(Result);
2137 Tmp2 = LegalizeOp(Ch);
2138 } else if (SrcWidth & (SrcWidth - 1)) {
2139 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002140 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002141 "Unsupported extload!");
2142 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2143 assert(RoundWidth < SrcWidth);
2144 unsigned ExtraWidth = SrcWidth - RoundWidth;
2145 assert(ExtraWidth < RoundWidth);
2146 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2147 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002148 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2149 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002150 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002151 unsigned IncrementSize;
2152
2153 if (TLI.isLittleEndian()) {
2154 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2155 // Load the bottom RoundWidth bits.
2156 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2157 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2158 Alignment);
2159
2160 // Load the remaining ExtraWidth bits.
2161 IncrementSize = RoundWidth / 8;
2162 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2163 DAG.getIntPtrConstant(IncrementSize));
2164 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2165 LD->getSrcValue(), SVOffset + IncrementSize,
2166 ExtraVT, isVolatile,
2167 MinAlign(Alignment, IncrementSize));
2168
2169 // Build a factor node to remember that this load is independent of the
2170 // other one.
2171 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2172 Hi.getValue(1));
2173
2174 // Move the top bits to the right place.
2175 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2176 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2177
2178 // Join the hi and lo parts.
2179 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002180 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002181 // Big endian - avoid unaligned loads.
2182 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2183 // Load the top RoundWidth bits.
2184 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2185 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2186 Alignment);
2187
2188 // Load the remaining ExtraWidth bits.
2189 IncrementSize = RoundWidth / 8;
2190 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2191 DAG.getIntPtrConstant(IncrementSize));
2192 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2193 LD->getSrcValue(), SVOffset + IncrementSize,
2194 ExtraVT, isVolatile,
2195 MinAlign(Alignment, IncrementSize));
2196
2197 // Build a factor node to remember that this load is independent of the
2198 // other one.
2199 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2200 Hi.getValue(1));
2201
2202 // Move the top bits to the right place.
2203 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2204 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2205
2206 // Join the hi and lo parts.
2207 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2208 }
2209
2210 Tmp1 = LegalizeOp(Result);
2211 Tmp2 = LegalizeOp(Ch);
2212 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002213 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002214 default: assert(0 && "This action is not supported yet!");
2215 case TargetLowering::Custom:
2216 isCustom = true;
2217 // FALLTHROUGH
2218 case TargetLowering::Legal:
2219 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2220 Tmp1 = Result.getValue(0);
2221 Tmp2 = Result.getValue(1);
2222
2223 if (isCustom) {
2224 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002225 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002226 Tmp1 = LegalizeOp(Tmp3);
2227 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2228 }
2229 } else {
2230 // If this is an unaligned load and the target doesn't support it,
2231 // expand it.
2232 if (!TLI.allowsUnalignedMemoryAccesses()) {
2233 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002234 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002235 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002236 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002237 TLI);
2238 Tmp1 = Result.getOperand(0);
2239 Tmp2 = Result.getOperand(1);
2240 Tmp1 = LegalizeOp(Tmp1);
2241 Tmp2 = LegalizeOp(Tmp2);
2242 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002243 }
2244 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002245 break;
2246 case TargetLowering::Expand:
2247 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2248 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002249 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002250 LD->getSrcValueOffset(),
2251 LD->isVolatile(), LD->getAlignment());
2252 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2253 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2254 Tmp2 = LegalizeOp(Load.getValue(1));
2255 break;
2256 }
2257 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2258 // Turn the unsupported load into an EXTLOAD followed by an explicit
2259 // zero/sign extend inreg.
2260 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2261 Tmp1, Tmp2, LD->getSrcValue(),
2262 LD->getSrcValueOffset(), SrcVT,
2263 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002264 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002265 if (ExtType == ISD::SEXTLOAD)
2266 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2267 Result, DAG.getValueType(SrcVT));
2268 else
2269 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2270 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2271 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002272 break;
2273 }
Evan Cheng466685d2006-10-09 20:57:25 +00002274 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002275
Evan Cheng466685d2006-10-09 20:57:25 +00002276 // Since loads produce two values, make sure to remember that we legalized
2277 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002278 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2279 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002280 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002281 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002282 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002283 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002284 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002285 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002286 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002287 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002288 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002289 // 1 -> Hi
2290 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002291 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002292 TLI.getShiftAmountTy()));
2293 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2294 } else {
2295 // 0 -> Lo
2296 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2297 Node->getOperand(0));
2298 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002299 break;
2300 case Expand:
2301 // Get both the low and high parts.
2302 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002303 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002304 Result = Tmp2; // 1 -> Hi
2305 else
2306 Result = Tmp1; // 0 -> Lo
2307 break;
2308 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002309 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002310 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002311
2312 case ISD::CopyToReg:
2313 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002314
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002315 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002316 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002317 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002318 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002319 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002320 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002321 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002322 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002323 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002324 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002325 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2326 Tmp3);
2327 } else {
2328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002329 }
2330
2331 // Since this produces two values, make sure to remember that we legalized
2332 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002333 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2334 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002335 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002336 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002337 break;
2338
2339 case ISD::RET:
2340 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002341
2342 // Ensure that libcalls are emitted before a return.
2343 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2344 Tmp1 = LegalizeOp(Tmp1);
2345 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002346
Chris Lattner3e928bb2005-01-07 07:47:09 +00002347 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002348 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002349 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002350 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002351 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002352 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002353 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002354 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002355 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002356 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002357 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002358 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002359
2360 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002361 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002362 std::swap(Lo, Hi);
2363
Gabor Greifba36cb52008-08-28 21:40:38 +00002364 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002365 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2366 else
2367 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002368 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002369 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002370 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002371 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002372 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2373 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002374
Dan Gohman7f321562007-06-25 16:23:39 +00002375 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002376 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002377 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002378 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002379 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002380 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002381 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002382 } else if (NumElems == 1) {
2383 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002384 Tmp2 = ScalarizeVectorOp(Tmp2);
2385 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002386 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002387
2388 // FIXME: Returns of gcc generic vectors smaller than a legal type
2389 // should be returned in integer registers!
2390
Chris Lattnerf87324e2006-04-11 01:31:51 +00002391 // The scalarized value type may not be legal, e.g. it might require
2392 // promotion or expansion. Relegalize the return.
2393 Result = LegalizeOp(Result);
2394 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002395 // FIXME: Returns of gcc generic vectors larger than a legal vector
2396 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002397 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002398 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002399 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002400 Result = LegalizeOp(Result);
2401 }
2402 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002403 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002404 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002405 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002407 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002408 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002409 }
2410 break;
2411 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002412 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002413 break;
2414 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002415 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002416 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002417 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002418 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2419 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002420 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002421 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002422 break;
2423 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002424 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002425 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002426 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002427 ExpandOp(Node->getOperand(i), Lo, Hi);
2428 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002429 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002430 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002431 NewValues.push_back(Hi);
2432 NewValues.push_back(Node->getOperand(i+1));
2433 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002434 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002435 }
2436 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002437 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002438 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002439
2440 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002441 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002442 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002443 Result = DAG.getNode(ISD::RET, MVT::Other,
2444 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002445 break;
2446 }
2447 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002448
Chris Lattner6862dbc2006-01-29 21:02:23 +00002449 if (Result.getOpcode() == ISD::RET) {
2450 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2451 default: assert(0 && "This action is not supported yet!");
2452 case TargetLowering::Legal: break;
2453 case TargetLowering::Custom:
2454 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002455 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002456 break;
2457 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002458 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002459 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002460 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002461 StoreSDNode *ST = cast<StoreSDNode>(Node);
2462 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2463 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002464 int SVOffset = ST->getSrcValueOffset();
2465 unsigned Alignment = ST->getAlignment();
2466 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002467
Evan Cheng8b2794a2006-10-13 21:14:26 +00002468 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002469 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2470 // FIXME: We shouldn't do this for TargetConstantFP's.
2471 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2472 // to phase ordering between legalized code and the dag combiner. This
2473 // probably means that we need to integrate dag combiner and legalizer
2474 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002475 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002476 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002477 if (CFP->getValueType(0) == MVT::f32 &&
2478 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002479 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002480 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002481 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002482 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2483 SVOffset, isVolatile, Alignment);
2484 break;
2485 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002486 // If this target supports 64-bit registers, do a single 64-bit store.
2487 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002488 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002489 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002490 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2491 SVOffset, isVolatile, Alignment);
2492 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002493 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002494 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2495 // stores. If the target supports neither 32- nor 64-bits, this
2496 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002497 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002498 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2499 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002500 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002501
2502 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2503 SVOffset, isVolatile, Alignment);
2504 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002505 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002506 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002507 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002508
2509 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2510 break;
2511 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002512 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002513 }
2514
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002515 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002516 case Legal: {
2517 Tmp3 = LegalizeOp(ST->getValue());
2518 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2519 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002520
Duncan Sands83ec4b62008-06-06 12:08:01 +00002521 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002522 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2523 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002524 case TargetLowering::Legal:
2525 // If this is an unaligned store and the target doesn't support it,
2526 // expand it.
2527 if (!TLI.allowsUnalignedMemoryAccesses()) {
2528 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002529 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002530 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002531 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002532 TLI);
2533 }
2534 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002535 case TargetLowering::Custom:
2536 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002537 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002538 break;
2539 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002540 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002541 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2542 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2543 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002544 ST->getSrcValue(), SVOffset, isVolatile,
2545 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002546 break;
2547 }
2548 break;
2549 }
2550 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002551 if (!ST->getMemoryVT().isVector()) {
2552 // Truncate the value and store the result.
2553 Tmp3 = PromoteOp(ST->getValue());
2554 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2555 SVOffset, ST->getMemoryVT(),
2556 isVolatile, Alignment);
2557 break;
2558 }
2559 // Fall thru to expand for vector
2560 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002561 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002562 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002563
2564 // If this is a vector type, then we have to calculate the increment as
2565 // the product of the element size in bytes, and the number of elements
2566 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002567 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002568 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002569 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002570 MVT InVT = InVal->getValueType(InIx);
2571 unsigned NumElems = InVT.getVectorNumElements();
2572 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002573
Dan Gohman7f321562007-06-25 16:23:39 +00002574 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002575 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002576 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002577 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002578 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002579 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002580 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002581 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002582 Result = LegalizeOp(Result);
2583 break;
2584 } else if (NumElems == 1) {
2585 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002586 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002587 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002588 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002589 // The scalarized value type may not be legal, e.g. it might require
2590 // promotion or expansion. Relegalize the scalar store.
2591 Result = LegalizeOp(Result);
2592 break;
2593 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002594 // Check if we have widen this node with another value
2595 std::map<SDValue, SDValue>::iterator I =
2596 WidenNodes.find(ST->getValue());
2597 if (I != WidenNodes.end()) {
2598 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2599 break;
2600 }
2601 else {
2602 SplitVectorOp(ST->getValue(), Lo, Hi);
2603 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2604 EVT.getSizeInBits()/8;
2605 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002606 }
2607 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002608 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002609 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002610
Richard Pennington4b052dc2008-09-25 16:15:10 +00002611 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002612 std::swap(Lo, Hi);
2613 }
2614
2615 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002616 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002617
Gabor Greifba36cb52008-08-28 21:40:38 +00002618 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002619 // Must be int <-> float one-to-one expansion.
2620 Result = Lo;
2621 break;
2622 }
2623
Evan Cheng8b2794a2006-10-13 21:14:26 +00002624 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002625 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002626 assert(isTypeLegal(Tmp2.getValueType()) &&
2627 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002628 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002629 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002630 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002631 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002632 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2633 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002634 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002635 }
2636 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002637 switch (getTypeAction(ST->getValue().getValueType())) {
2638 case Legal:
2639 Tmp3 = LegalizeOp(ST->getValue());
2640 break;
2641 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002642 if (!ST->getValue().getValueType().isVector()) {
2643 // We can promote the value, the truncstore will still take care of it.
2644 Tmp3 = PromoteOp(ST->getValue());
2645 break;
2646 }
2647 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002648 case Expand:
2649 // Just store the low part. This may become a non-trunc store, so make
2650 // sure to use getTruncStore, not UpdateNodeOperands below.
2651 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2652 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2653 SVOffset, MVT::i8, isVolatile, Alignment);
2654 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002655
Duncan Sands83ec4b62008-06-06 12:08:01 +00002656 MVT StVT = ST->getMemoryVT();
2657 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002658
Duncan Sands83ec4b62008-06-06 12:08:01 +00002659 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002660 // Promote to a byte-sized store with upper bits zero if not
2661 // storing an integral number of bytes. For example, promote
2662 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002663 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002664 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2665 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2666 SVOffset, NVT, isVolatile, Alignment);
2667 } else if (StWidth & (StWidth - 1)) {
2668 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002669 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002670 "Unsupported truncstore!");
2671 unsigned RoundWidth = 1 << Log2_32(StWidth);
2672 assert(RoundWidth < StWidth);
2673 unsigned ExtraWidth = StWidth - RoundWidth;
2674 assert(ExtraWidth < RoundWidth);
2675 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2676 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002677 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2678 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002679 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002680 unsigned IncrementSize;
2681
2682 if (TLI.isLittleEndian()) {
2683 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2684 // Store the bottom RoundWidth bits.
2685 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2686 SVOffset, RoundVT,
2687 isVolatile, Alignment);
2688
2689 // Store the remaining ExtraWidth bits.
2690 IncrementSize = RoundWidth / 8;
2691 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2692 DAG.getIntPtrConstant(IncrementSize));
2693 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2694 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2695 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2696 SVOffset + IncrementSize, ExtraVT, isVolatile,
2697 MinAlign(Alignment, IncrementSize));
2698 } else {
2699 // Big endian - avoid unaligned stores.
2700 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2701 // Store the top RoundWidth bits.
2702 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2703 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2704 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2705 RoundVT, isVolatile, Alignment);
2706
2707 // Store the remaining ExtraWidth bits.
2708 IncrementSize = RoundWidth / 8;
2709 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2710 DAG.getIntPtrConstant(IncrementSize));
2711 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2712 SVOffset + IncrementSize, ExtraVT, isVolatile,
2713 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002714 }
Duncan Sands7e857202008-01-22 07:17:34 +00002715
2716 // The order of the stores doesn't matter.
2717 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2718 } else {
2719 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2720 Tmp2 != ST->getBasePtr())
2721 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2722 ST->getOffset());
2723
2724 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2725 default: assert(0 && "This action is not supported yet!");
2726 case TargetLowering::Legal:
2727 // If this is an unaligned store and the target doesn't support it,
2728 // expand it.
2729 if (!TLI.allowsUnalignedMemoryAccesses()) {
2730 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002731 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002732 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002733 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002734 TLI);
2735 }
2736 break;
2737 case TargetLowering::Custom:
2738 Result = TLI.LowerOperation(Result, DAG);
2739 break;
2740 case Expand:
2741 // TRUNCSTORE:i16 i32 -> STORE i16
2742 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2743 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2744 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2745 isVolatile, Alignment);
2746 break;
2747 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002748 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002749 }
2750 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002751 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002752 case ISD::PCMARKER:
2753 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002754 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002755 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002756 case ISD::STACKSAVE:
2757 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002758 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2759 Tmp1 = Result.getValue(0);
2760 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002761
Chris Lattner140d53c2006-01-13 02:50:02 +00002762 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2763 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002764 case TargetLowering::Legal: break;
2765 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002766 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002767 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002768 Tmp1 = LegalizeOp(Tmp3);
2769 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002770 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002771 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002772 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002773 // Expand to CopyFromReg if the target set
2774 // StackPointerRegisterToSaveRestore.
2775 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002776 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002777 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002778 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002779 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002780 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2781 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002782 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002783 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002784 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002785
2786 // Since stacksave produce two values, make sure to remember that we
2787 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002788 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2789 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002790 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002791
Chris Lattner140d53c2006-01-13 02:50:02 +00002792 case ISD::STACKRESTORE:
2793 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2794 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002795 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002796
2797 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2798 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002799 case TargetLowering::Legal: break;
2800 case TargetLowering::Custom:
2801 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002802 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002803 break;
2804 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002805 // Expand to CopyToReg if the target set
2806 // StackPointerRegisterToSaveRestore.
2807 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2808 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2809 } else {
2810 Result = Tmp1;
2811 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002812 break;
2813 }
2814 break;
2815
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002816 case ISD::READCYCLECOUNTER:
2817 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002818 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002819 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2820 Node->getValueType(0))) {
2821 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002822 case TargetLowering::Legal:
2823 Tmp1 = Result.getValue(0);
2824 Tmp2 = Result.getValue(1);
2825 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002826 case TargetLowering::Custom:
2827 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002828 Tmp1 = LegalizeOp(Result.getValue(0));
2829 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002830 break;
2831 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002832
2833 // Since rdcc produce two values, make sure to remember that we legalized
2834 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002835 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2836 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002837 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002838
Chris Lattner2ee743f2005-01-14 22:08:15 +00002839 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002840 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2841 case Expand: assert(0 && "It's impossible to expand bools");
2842 case Legal:
2843 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2844 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002845 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002846 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002847 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002848 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002849 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002850 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002851 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002852 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002853 break;
2854 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002855 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002856 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002857 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002858
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002859 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002860
Nate Begemanb942a3d2005-08-23 04:29:48 +00002861 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002862 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002863 case TargetLowering::Legal: break;
2864 case TargetLowering::Custom: {
2865 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002866 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002867 break;
2868 }
Nate Begeman9373a812005-08-10 20:51:12 +00002869 case TargetLowering::Expand:
2870 if (Tmp1.getOpcode() == ISD::SETCC) {
2871 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2872 Tmp2, Tmp3,
2873 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2874 } else {
2875 Result = DAG.getSelectCC(Tmp1,
2876 DAG.getConstant(0, Tmp1.getValueType()),
2877 Tmp2, Tmp3, ISD::SETNE);
2878 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002879 break;
2880 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002881 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002882 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2883 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002884 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002885 ExtOp = ISD::BIT_CONVERT;
2886 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002887 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002888 ExtOp = ISD::ANY_EXTEND;
2889 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002890 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002891 ExtOp = ISD::FP_EXTEND;
2892 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002893 }
2894 // Promote each of the values to the new type.
2895 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2896 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2897 // Perform the larger operation, then round down.
2898 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002899 if (TruncOp != ISD::FP_ROUND)
2900 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2901 else
2902 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2903 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002904 break;
2905 }
2906 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002907 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002908 case ISD::SELECT_CC: {
2909 Tmp1 = Node->getOperand(0); // LHS
2910 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002911 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2912 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002913 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002914
Dale Johannesen53e4e442008-11-07 22:54:33 +00002915 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002916
Evan Cheng7f042682008-10-15 02:05:31 +00002917 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002918 // the LHS is a legal SETCC itself. In this case, we need to compare
2919 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002920 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002921 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2922 CC = DAG.getCondCode(ISD::SETNE);
2923 }
2924 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2925
2926 // Everything is legal, see if we should expand this op or something.
2927 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2928 default: assert(0 && "This action is not supported yet!");
2929 case TargetLowering::Legal: break;
2930 case TargetLowering::Custom:
2931 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002932 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002933 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002934 }
2935 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002936 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002937 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002938 Tmp1 = Node->getOperand(0);
2939 Tmp2 = Node->getOperand(1);
2940 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00002941 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002942
2943 // If we had to Expand the SetCC operands into a SELECT node, then it may
2944 // not always be possible to return a true LHS & RHS. In this case, just
2945 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002946 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002947 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002948 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002949 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002950
Chris Lattner73e142f2006-01-30 22:43:50 +00002951 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002952 default: assert(0 && "Cannot handle this action for SETCC yet!");
2953 case TargetLowering::Custom:
2954 isCustom = true;
2955 // FALLTHROUGH.
2956 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002957 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002958 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002959 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002960 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002961 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002962 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002963 case TargetLowering::Promote: {
2964 // First step, figure out the appropriate operation to use.
2965 // Allow SETCC to not be supported for all legal data types
2966 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002967 MVT NewInTy = Node->getOperand(0).getValueType();
2968 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002969
2970 // Scan for the appropriate larger type to use.
2971 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002972 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002973
Duncan Sands83ec4b62008-06-06 12:08:01 +00002974 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002975 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002976 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00002977 "Fell off of the edge of the floating point world");
2978
2979 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00002980 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00002981 break;
2982 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00002983 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00002984 assert(0 && "Cannot promote Legal Integer SETCC yet");
2985 else {
2986 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
2987 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
2988 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002989 Tmp1 = LegalizeOp(Tmp1);
2990 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00002991 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00002992 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00002993 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002994 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002995 case TargetLowering::Expand:
2996 // Expand a setcc node into a select_cc of the same condition, lhs, and
2997 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00002998 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00002999 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3000 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003001 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003002 break;
3003 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003004 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003005 case ISD::VSETCC: {
3006 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3007 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003008 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003009
3010 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3011
3012 // Everything is legal, see if we should expand this op or something.
3013 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3014 default: assert(0 && "This action is not supported yet!");
3015 case TargetLowering::Legal: break;
3016 case TargetLowering::Custom:
3017 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003018 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003019 break;
3020 }
3021 break;
3022 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003023
Chris Lattner5b359c62005-04-02 04:00:59 +00003024 case ISD::SHL_PARTS:
3025 case ISD::SRA_PARTS:
3026 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003027 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003028 bool Changed = false;
3029 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3030 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3031 Changed |= Ops.back() != Node->getOperand(i);
3032 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003033 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003034 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003035
Evan Cheng05a2d562006-01-09 18:31:59 +00003036 switch (TLI.getOperationAction(Node->getOpcode(),
3037 Node->getValueType(0))) {
3038 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003039 case TargetLowering::Legal: break;
3040 case TargetLowering::Custom:
3041 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003042 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003043 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003044 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003045 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003046 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003047 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003048 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003049 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003050 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003051 return RetVal;
3052 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003053 break;
3054 }
3055
Chris Lattner2c8086f2005-04-02 05:00:07 +00003056 // Since these produce multiple values, make sure to remember that we
3057 // legalized all of them.
3058 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003059 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003060 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003061 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003062
3063 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003064 case ISD::ADD:
3065 case ISD::SUB:
3066 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003067 case ISD::MULHS:
3068 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003069 case ISD::UDIV:
3070 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003071 case ISD::AND:
3072 case ISD::OR:
3073 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003074 case ISD::SHL:
3075 case ISD::SRL:
3076 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003077 case ISD::FADD:
3078 case ISD::FSUB:
3079 case ISD::FMUL:
3080 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003081 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003082 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003083 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3084 case Expand: assert(0 && "Not possible");
3085 case Legal:
3086 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3087 break;
3088 case Promote:
3089 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3090 break;
3091 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003092
3093 if ((Node->getOpcode() == ISD::SHL ||
3094 Node->getOpcode() == ISD::SRL ||
3095 Node->getOpcode() == ISD::SRA) &&
3096 !Node->getValueType(0).isVector()) {
3097 if (TLI.getShiftAmountTy().bitsLT(Tmp2.getValueType()))
3098 Tmp2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Tmp2);
3099 else if (TLI.getShiftAmountTy().bitsGT(Tmp2.getValueType()))
3100 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Tmp2);
3101 }
3102
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003103 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003104
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003105 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003106 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003107 case TargetLowering::Legal: break;
3108 case TargetLowering::Custom:
3109 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003110 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003111 Result = Tmp1;
3112 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003113 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003114 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003115 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003116 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003117
Dan Gohman525178c2007-10-08 18:33:35 +00003118 // See if multiply or divide can be lowered using two-result operations.
3119 SDVTList VTs = DAG.getVTList(VT, VT);
3120 if (Node->getOpcode() == ISD::MUL) {
3121 // We just need the low half of the multiply; try both the signed
3122 // and unsigned forms. If the target supports both SMUL_LOHI and
3123 // UMUL_LOHI, form a preference by checking which forms of plain
3124 // MULH it supports.
3125 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3126 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3127 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3128 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3129 unsigned OpToUse = 0;
3130 if (HasSMUL_LOHI && !HasMULHS) {
3131 OpToUse = ISD::SMUL_LOHI;
3132 } else if (HasUMUL_LOHI && !HasMULHU) {
3133 OpToUse = ISD::UMUL_LOHI;
3134 } else if (HasSMUL_LOHI) {
3135 OpToUse = ISD::SMUL_LOHI;
3136 } else if (HasUMUL_LOHI) {
3137 OpToUse = ISD::UMUL_LOHI;
3138 }
3139 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003140 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003141 break;
3142 }
3143 }
3144 if (Node->getOpcode() == ISD::MULHS &&
3145 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003146 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3147 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003148 break;
3149 }
3150 if (Node->getOpcode() == ISD::MULHU &&
3151 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003152 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3153 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003154 break;
3155 }
3156 if (Node->getOpcode() == ISD::SDIV &&
3157 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003158 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3159 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003160 break;
3161 }
3162 if (Node->getOpcode() == ISD::UDIV &&
3163 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003164 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3165 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003166 break;
3167 }
Mon P Wang0c397192008-10-30 08:01:45 +00003168
Dan Gohman82669522007-10-11 23:57:53 +00003169 // Check to see if we have a libcall for this operator.
3170 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3171 bool isSigned = false;
3172 switch (Node->getOpcode()) {
3173 case ISD::UDIV:
3174 case ISD::SDIV:
3175 if (VT == MVT::i32) {
3176 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003177 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003178 isSigned = Node->getOpcode() == ISD::SDIV;
3179 }
3180 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003181 case ISD::MUL:
3182 if (VT == MVT::i32)
3183 LC = RTLIB::MUL_I32;
3184 break;
Dan Gohman82669522007-10-11 23:57:53 +00003185 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003186 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3187 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003188 break;
3189 default: break;
3190 }
3191 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003192 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003193 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003194 break;
3195 }
Mon P Wang0c397192008-10-30 08:01:45 +00003196
Duncan Sands83ec4b62008-06-06 12:08:01 +00003197 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003198 "Cannot expand this binary operator!");
3199 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003200 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003201 break;
3202 }
Evan Chengcc987612006-04-12 21:20:24 +00003203 case TargetLowering::Promote: {
3204 switch (Node->getOpcode()) {
3205 default: assert(0 && "Do not know how to promote this BinOp!");
3206 case ISD::AND:
3207 case ISD::OR:
3208 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003209 MVT OVT = Node->getValueType(0);
3210 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3211 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003212 // Bit convert each of the values to the new type.
3213 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3214 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3215 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3216 // Bit convert the result back the original type.
3217 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3218 break;
3219 }
3220 }
3221 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003222 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003223 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003224
Dan Gohmane14ea862007-10-05 14:17:22 +00003225 case ISD::SMUL_LOHI:
3226 case ISD::UMUL_LOHI:
3227 case ISD::SDIVREM:
3228 case ISD::UDIVREM:
3229 // These nodes will only be produced by target-specific lowering, so
3230 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003231 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003232 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003233
3234 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3235 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3236 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003237 break;
3238
Chris Lattnera09f8482006-03-05 05:09:38 +00003239 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3240 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3241 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3242 case Expand: assert(0 && "Not possible");
3243 case Legal:
3244 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3245 break;
3246 case Promote:
3247 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3248 break;
3249 }
3250
3251 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3252
3253 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3254 default: assert(0 && "Operation not supported");
3255 case TargetLowering::Custom:
3256 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003257 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003258 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003259 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003260 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003261 // If this target supports fabs/fneg natively and select is cheap,
3262 // do this efficiently.
3263 if (!TLI.isSelectExpensive() &&
3264 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3265 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003266 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003267 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003268 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003269 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003270 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003271 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003272 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003273 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3274 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003275 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003276 // Select between the nabs and abs value based on the sign bit of
3277 // the input.
3278 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3279 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3280 AbsVal),
3281 AbsVal);
3282 Result = LegalizeOp(Result);
3283 break;
3284 }
3285
3286 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003287 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003288 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3289 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3290 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3291 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003292 break;
3293 }
Evan Cheng912095b2007-01-04 21:56:39 +00003294 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003295 break;
3296
Nate Begeman551bf3f2006-02-17 05:43:56 +00003297 case ISD::ADDC:
3298 case ISD::SUBC:
3299 Tmp1 = LegalizeOp(Node->getOperand(0));
3300 Tmp2 = LegalizeOp(Node->getOperand(1));
3301 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003302 Tmp3 = Result.getValue(0);
3303 Tmp4 = Result.getValue(1);
3304
3305 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3306 default: assert(0 && "This action is not supported yet!");
3307 case TargetLowering::Legal:
3308 break;
3309 case TargetLowering::Custom:
3310 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3311 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003312 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003313 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3314 }
3315 break;
3316 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003317 // Since this produces two values, make sure to remember that we legalized
3318 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003319 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3320 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3321 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003322
Nate Begeman551bf3f2006-02-17 05:43:56 +00003323 case ISD::ADDE:
3324 case ISD::SUBE:
3325 Tmp1 = LegalizeOp(Node->getOperand(0));
3326 Tmp2 = LegalizeOp(Node->getOperand(1));
3327 Tmp3 = LegalizeOp(Node->getOperand(2));
3328 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003329 Tmp3 = Result.getValue(0);
3330 Tmp4 = Result.getValue(1);
3331
3332 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3333 default: assert(0 && "This action is not supported yet!");
3334 case TargetLowering::Legal:
3335 break;
3336 case TargetLowering::Custom:
3337 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3338 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003339 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003340 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3341 }
3342 break;
3343 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003344 // Since this produces two values, make sure to remember that we legalized
3345 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003346 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3347 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3348 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003349
Nate Begeman419f8b62005-10-18 00:27:41 +00003350 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003351 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003352 // TODO: handle the case where the Lo and Hi operands are not of legal type
3353 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3354 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3355 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003356 case TargetLowering::Promote:
3357 case TargetLowering::Custom:
3358 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003359 case TargetLowering::Legal:
3360 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3361 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3362 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003363 case TargetLowering::Expand:
3364 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3365 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3366 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003367 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003368 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003369 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003370 break;
3371 }
3372 break;
3373 }
3374
Nate Begemanc105e192005-04-06 00:23:54 +00003375 case ISD::UREM:
3376 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003377 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003378 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3379 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003380
Nate Begemanc105e192005-04-06 00:23:54 +00003381 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003382 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3383 case TargetLowering::Custom:
3384 isCustom = true;
3385 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003386 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003387 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003388 if (isCustom) {
3389 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003390 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003391 }
Nate Begemanc105e192005-04-06 00:23:54 +00003392 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003393 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003394 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003395 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003396 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003397
3398 // See if remainder can be lowered using two-result operations.
3399 SDVTList VTs = DAG.getVTList(VT, VT);
3400 if (Node->getOpcode() == ISD::SREM &&
3401 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003402 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003403 break;
3404 }
3405 if (Node->getOpcode() == ISD::UREM &&
3406 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003407 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003408 break;
3409 }
3410
Duncan Sands83ec4b62008-06-06 12:08:01 +00003411 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003412 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003413 TargetLowering::Legal) {
3414 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003415 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003416 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3417 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003418 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003419 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003420 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003421 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003422 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003423 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3424 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003425 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003426 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003427 }
Dan Gohman0d974262007-11-06 22:11:54 +00003428 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003429 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003430 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003431 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003432 Result = LegalizeOp(UnrollVectorOp(Op));
3433 } else {
3434 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003435 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3436 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003437 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003438 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003439 }
Nate Begemanc105e192005-04-06 00:23:54 +00003440 }
3441 break;
3442 }
Dan Gohman525178c2007-10-08 18:33:35 +00003443 }
Nate Begemanc105e192005-04-06 00:23:54 +00003444 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003445 case ISD::VAARG: {
3446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3447 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3448
Duncan Sands83ec4b62008-06-06 12:08:01 +00003449 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003450 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3451 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003452 case TargetLowering::Custom:
3453 isCustom = true;
3454 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003455 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003456 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3457 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003458 Tmp1 = Result.getValue(1);
3459
3460 if (isCustom) {
3461 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003462 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003463 Result = LegalizeOp(Tmp2);
3464 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3465 }
3466 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003467 break;
3468 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003469 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003470 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003471 // Increment the pointer, VAList, to the next vaarg
Duncan Sands5c58a312008-11-03 11:51:11 +00003472 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3473 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3474 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003475 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003476 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003477 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003478 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003479 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003480 Result = LegalizeOp(Result);
3481 break;
3482 }
3483 }
3484 // Since VAARG produces two values, make sure to remember that we
3485 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003486 AddLegalizedOperand(SDValue(Node, 0), Result);
3487 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003488 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003489 }
3490
3491 case ISD::VACOPY:
3492 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3493 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3494 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3495
3496 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3497 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003498 case TargetLowering::Custom:
3499 isCustom = true;
3500 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003501 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003502 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3503 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003504 if (isCustom) {
3505 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003506 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003507 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003508 break;
3509 case TargetLowering::Expand:
3510 // This defaults to loading a pointer from the input and storing it to the
3511 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003512 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3513 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003514 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3515 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003516 break;
3517 }
3518 break;
3519
3520 case ISD::VAEND:
3521 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3522 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3523
3524 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3525 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003526 case TargetLowering::Custom:
3527 isCustom = true;
3528 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003529 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003530 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003531 if (isCustom) {
3532 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003533 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003534 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003535 break;
3536 case TargetLowering::Expand:
3537 Result = Tmp1; // Default to a no-op, return the chain
3538 break;
3539 }
3540 break;
3541
3542 case ISD::VASTART:
3543 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3544 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3545
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003546 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3547
Nate Begemanacc398c2006-01-25 18:21:52 +00003548 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3549 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003550 case TargetLowering::Legal: break;
3551 case TargetLowering::Custom:
3552 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003553 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003554 break;
3555 }
3556 break;
3557
Nate Begeman35ef9132006-01-11 21:21:00 +00003558 case ISD::ROTL:
3559 case ISD::ROTR:
3560 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3561 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003562 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003563 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3564 default:
3565 assert(0 && "ROTL/ROTR legalize operation not supported");
3566 break;
3567 case TargetLowering::Legal:
3568 break;
3569 case TargetLowering::Custom:
3570 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003571 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003572 break;
3573 case TargetLowering::Promote:
3574 assert(0 && "Do not know how to promote ROTL/ROTR");
3575 break;
3576 case TargetLowering::Expand:
3577 assert(0 && "Do not know how to expand ROTL/ROTR");
3578 break;
3579 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003580 break;
3581
Nate Begemand88fc032006-01-14 03:14:10 +00003582 case ISD::BSWAP:
3583 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3584 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003585 case TargetLowering::Custom:
3586 assert(0 && "Cannot custom legalize this yet!");
3587 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003588 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003589 break;
3590 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003591 MVT OVT = Tmp1.getValueType();
3592 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3593 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003594
Chris Lattner456a93a2006-01-28 07:39:30 +00003595 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3596 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3597 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3598 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3599 break;
3600 }
3601 case TargetLowering::Expand:
3602 Result = ExpandBSWAP(Tmp1);
3603 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003604 }
3605 break;
3606
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003607 case ISD::CTPOP:
3608 case ISD::CTTZ:
3609 case ISD::CTLZ:
3610 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3611 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003612 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003613 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003614 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003615 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003616 TargetLowering::Custom) {
3617 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003618 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003619 Result = Tmp1;
3620 }
Scott Michel910b66d2007-07-30 21:00:31 +00003621 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003622 break;
3623 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003624 MVT OVT = Tmp1.getValueType();
3625 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003626
3627 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003628 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3629 // Perform the larger operation, then subtract if needed.
3630 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003631 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003632 case ISD::CTPOP:
3633 Result = Tmp1;
3634 break;
3635 case ISD::CTTZ:
3636 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003637 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003638 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003639 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003640 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003641 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003642 break;
3643 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003644 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003645 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003646 DAG.getConstant(NVT.getSizeInBits() -
3647 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003648 break;
3649 }
3650 break;
3651 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003652 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003653 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003654 break;
3655 }
3656 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003657
Chris Lattner2c8086f2005-04-02 05:00:07 +00003658 // Unary operators
3659 case ISD::FABS:
3660 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003661 case ISD::FSQRT:
3662 case ISD::FSIN:
3663 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003664 case ISD::FLOG:
3665 case ISD::FLOG2:
3666 case ISD::FLOG10:
3667 case ISD::FEXP:
3668 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003669 case ISD::FTRUNC:
3670 case ISD::FFLOOR:
3671 case ISD::FCEIL:
3672 case ISD::FRINT:
3673 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003674 Tmp1 = LegalizeOp(Node->getOperand(0));
3675 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003676 case TargetLowering::Promote:
3677 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003678 isCustom = true;
3679 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003680 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003681 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003682 if (isCustom) {
3683 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003684 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003685 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003686 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003687 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003688 switch (Node->getOpcode()) {
3689 default: assert(0 && "Unreachable!");
3690 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003691 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3692 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003693 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003694 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003695 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003696 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003697 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003698 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003699 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003700 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003701 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3702 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003703 break;
3704 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003705 case ISD::FSQRT:
3706 case ISD::FSIN:
3707 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003708 case ISD::FLOG:
3709 case ISD::FLOG2:
3710 case ISD::FLOG10:
3711 case ISD::FEXP:
3712 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003713 case ISD::FTRUNC:
3714 case ISD::FFLOOR:
3715 case ISD::FCEIL:
3716 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003717 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003718 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003719
3720 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003721 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003722 Result = LegalizeOp(UnrollVectorOp(Op));
3723 break;
3724 }
3725
Evan Cheng56966222007-01-12 02:11:51 +00003726 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003727 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003728 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003729 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3730 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003731 break;
3732 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003733 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3734 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003735 break;
3736 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003737 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3738 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003739 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003740 case ISD::FLOG:
3741 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3742 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3743 break;
3744 case ISD::FLOG2:
3745 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3746 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3747 break;
3748 case ISD::FLOG10:
3749 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3750 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3751 break;
3752 case ISD::FEXP:
3753 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3754 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3755 break;
3756 case ISD::FEXP2:
3757 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3758 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3759 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003760 case ISD::FTRUNC:
3761 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3762 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3763 break;
3764 case ISD::FFLOOR:
3765 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3766 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3767 break;
3768 case ISD::FCEIL:
3769 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3770 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3771 break;
3772 case ISD::FRINT:
3773 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3774 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3775 break;
3776 case ISD::FNEARBYINT:
3777 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3778 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3779 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003780 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003781 default: assert(0 && "Unreachable!");
3782 }
Dan Gohman475871a2008-07-27 21:46:04 +00003783 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003784 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003785 break;
3786 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003787 }
3788 break;
3789 }
3790 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003791 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003792 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003793
3794 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003795 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003796 Result = LegalizeOp(UnrollVectorOp(Op));
3797 break;
3798 }
3799
3800 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003801 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3802 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003803 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003804 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003805 break;
3806 }
Chris Lattner35481892005-12-23 00:16:34 +00003807 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003808 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003809 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3810 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003811 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003812 // The input has to be a vector type, we have to either scalarize it, pack
3813 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003814 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003815 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003816 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3817 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003818
3819 // Figure out if there is a simple type corresponding to this Vector
3820 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003821 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003822 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003823 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003824 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3825 LegalizeOp(Node->getOperand(0)));
3826 break;
3827 } else if (NumElems == 1) {
3828 // Turn this into a bit convert of the scalar input.
3829 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3830 ScalarizeVectorOp(Node->getOperand(0)));
3831 break;
3832 } else {
3833 // FIXME: UNIMP! Store then reload
3834 assert(0 && "Cast from unsupported vector type not implemented yet!");
3835 }
Chris Lattner67993f72006-01-23 07:30:46 +00003836 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003837 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3838 Node->getOperand(0).getValueType())) {
3839 default: assert(0 && "Unknown operation action!");
3840 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003841 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3842 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003843 break;
3844 case TargetLowering::Legal:
3845 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003846 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003847 break;
3848 }
3849 }
3850 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003851 case ISD::CONVERT_RNDSAT: {
3852 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3853 switch (CvtCode) {
3854 default: assert(0 && "Unknown cvt code!");
3855 case ISD::CVT_SF:
3856 case ISD::CVT_UF:
3857 break;
3858 case ISD::CVT_FF:
3859 case ISD::CVT_FS:
3860 case ISD::CVT_FU:
3861 case ISD::CVT_SS:
3862 case ISD::CVT_SU:
3863 case ISD::CVT_US:
3864 case ISD::CVT_UU: {
3865 SDValue DTyOp = Node->getOperand(1);
3866 SDValue STyOp = Node->getOperand(2);
3867 SDValue RndOp = Node->getOperand(3);
3868 SDValue SatOp = Node->getOperand(4);
3869 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3870 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3871 case Legal:
3872 Tmp1 = LegalizeOp(Node->getOperand(0));
3873 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3874 RndOp, SatOp);
3875 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3876 TargetLowering::Custom) {
3877 Tmp1 = TLI.LowerOperation(Result, DAG);
3878 if (Tmp1.getNode()) Result = Tmp1;
3879 }
3880 break;
3881 case Promote:
3882 Result = PromoteOp(Node->getOperand(0));
3883 // For FP, make Op1 a i32
3884
3885 Result = DAG.getConvertRndSat(Result.getValueType(), Result,
3886 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3887 break;
3888 }
3889 break;
3890 }
3891 } // end switch CvtCode
3892 break;
3893 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003894 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003895 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003896 case ISD::UINT_TO_FP: {
3897 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003898 Result = LegalizeINT_TO_FP(Result, isSigned,
3899 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003900 break;
3901 }
3902 case ISD::TRUNCATE:
3903 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3904 case Legal:
3905 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003906 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003907 break;
3908 case Expand:
3909 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3910
3911 // Since the result is legal, we should just be able to truncate the low
3912 // part of the source.
3913 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3914 break;
3915 case Promote:
3916 Result = PromoteOp(Node->getOperand(0));
3917 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3918 break;
3919 }
3920 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003921
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003922 case ISD::FP_TO_SINT:
3923 case ISD::FP_TO_UINT:
3924 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3925 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003926 Tmp1 = LegalizeOp(Node->getOperand(0));
3927
Chris Lattner1618beb2005-07-29 00:11:56 +00003928 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3929 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003930 case TargetLowering::Custom:
3931 isCustom = true;
3932 // FALLTHROUGH
3933 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003934 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003935 if (isCustom) {
3936 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003937 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003938 }
3939 break;
3940 case TargetLowering::Promote:
3941 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3942 Node->getOpcode() == ISD::FP_TO_SINT);
3943 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003944 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003945 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00003946 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003947 MVT VT = Node->getOperand(0).getValueType();
3948 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003949 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00003950 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3951 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003952 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003953 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003954 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00003955 Node->getOperand(0), Tmp2, ISD::SETLT);
3956 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3957 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003958 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003959 Tmp2));
3960 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003961 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003962 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3963 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003964 } else {
3965 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
3966 }
3967 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00003968 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003969 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00003970 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003971 MVT VT = Op.getValueType();
3972 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003973 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003974 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00003975 if (Node->getOpcode() == ISD::FP_TO_SINT) {
3976 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
3977 Node->getOperand(0), DAG.getValueType(MVT::f64));
3978 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
3979 DAG.getIntPtrConstant(1));
3980 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
3981 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00003982 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
3983 APFloat apf = APFloat(APInt(128, 2, TwoE31));
3984 Tmp2 = DAG.getConstantFP(apf, OVT);
3985 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
3986 // FIXME: generated code sucks.
3987 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
3988 DAG.getNode(ISD::ADD, MVT::i32,
3989 DAG.getNode(ISD::FP_TO_SINT, VT,
3990 DAG.getNode(ISD::FSUB, OVT,
3991 Node->getOperand(0), Tmp2)),
3992 DAG.getConstant(0x80000000, MVT::i32)),
3993 DAG.getNode(ISD::FP_TO_SINT, VT,
3994 Node->getOperand(0)),
3995 DAG.getCondCode(ISD::SETGE));
3996 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00003997 break;
3998 }
Dan Gohmana2e94852008-03-10 23:03:31 +00003999 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004000 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4001 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4002 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004003 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004004 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004005 break;
4006 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004007 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004008 Tmp1 = PromoteOp(Node->getOperand(0));
4009 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4010 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004011 break;
4012 }
4013 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004014
Chris Lattnerf2670a82008-01-16 06:57:07 +00004015 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004016 MVT DstVT = Op.getValueType();
4017 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004018 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4019 // The only other way we can lower this is to turn it into a STORE,
4020 // LOAD pair, targetting a temporary location (a stack slot).
4021 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4022 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004023 }
4024 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4025 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4026 case Legal:
4027 Tmp1 = LegalizeOp(Node->getOperand(0));
4028 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4029 break;
4030 case Promote:
4031 Tmp1 = PromoteOp(Node->getOperand(0));
4032 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4033 break;
4034 }
4035 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004036 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004037 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004038 MVT DstVT = Op.getValueType();
4039 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004040 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4041 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004042 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004043 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004044 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004045 if (DstVT!=MVT::f64)
4046 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004047 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004048 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004049 // The only other way we can lower this is to turn it into a STORE,
4050 // LOAD pair, targetting a temporary location (a stack slot).
4051 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4052 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004053 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004054 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4055 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4056 case Legal:
4057 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004058 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004059 break;
4060 case Promote:
4061 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004062 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4063 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004064 break;
4065 }
4066 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004067 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004068 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004069 case ISD::ZERO_EXTEND:
4070 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004071 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004072 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004073 case Legal:
4074 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004075 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004076 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4077 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004078 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004079 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004080 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004081 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004082 case Promote:
4083 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004084 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004085 Tmp1 = PromoteOp(Node->getOperand(0));
4086 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004087 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004088 case ISD::ZERO_EXTEND:
4089 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004090 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004091 Result = DAG.getZeroExtendInReg(Result,
4092 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004093 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004094 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004095 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004096 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004097 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004098 Result,
4099 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004100 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004101 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004102 }
4103 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004104 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004105 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004106 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004107 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004108
4109 // If this operation is not supported, convert it to a shl/shr or load/store
4110 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004111 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4112 default: assert(0 && "This action not supported for this op yet!");
4113 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004114 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004115 break;
4116 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004117 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004118 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004119 // NOTE: we could fall back on load/store here too for targets without
4120 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004121 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4122 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004123 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004124 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4125 Node->getOperand(0), ShiftCst);
4126 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4127 Result, ShiftCst);
4128 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004129 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004130 // EXTLOAD pair, targetting a temporary location (a stack slot).
4131
4132 // NOTE: there is a choice here between constantly creating new stack
4133 // slots and always reusing the same one. We currently always create
4134 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004135 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4136 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004137 } else {
4138 assert(0 && "Unknown op");
4139 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004140 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004141 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004142 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004143 }
Duncan Sands36397f52007-07-27 12:58:54 +00004144 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004145 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004146 for (unsigned i = 0; i != 6; ++i)
4147 Ops[i] = LegalizeOp(Node->getOperand(i));
4148 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4149 // The only option for this node is to custom lower it.
4150 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004151 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004152
4153 // Since trampoline produces two values, make sure to remember that we
4154 // legalized both of them.
4155 Tmp1 = LegalizeOp(Result.getValue(1));
4156 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004157 AddLegalizedOperand(SDValue(Node, 0), Result);
4158 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004159 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004160 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004161 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004162 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004163 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4164 default: assert(0 && "This action not supported for this op yet!");
4165 case TargetLowering::Custom:
4166 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004167 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004168 // Fall Thru
4169 case TargetLowering::Legal:
4170 // If this operation is not supported, lower it to constant 1
4171 Result = DAG.getConstant(1, VT);
4172 break;
4173 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004174 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004175 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004176 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004177 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004178 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4179 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004180 case TargetLowering::Legal:
4181 Tmp1 = LegalizeOp(Node->getOperand(0));
4182 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4183 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004184 case TargetLowering::Custom:
4185 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004186 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004187 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004188 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004189 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004190 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004191 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004192 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004193 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004194 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004195 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner034f12e2008-01-15 22:09:33 +00004196 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004197 Result = CallResult.second;
4198 break;
4199 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004200 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004201 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004202
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004203 case ISD::SADDO: {
4204 MVT VT = Node->getValueType(0);
4205 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4206 default: assert(0 && "This action not supported for this op yet!");
4207 case TargetLowering::Custom:
4208 Result = TLI.LowerOperation(Op, DAG);
4209 if (Result.getNode()) break;
4210 // FALLTHROUGH
4211 case TargetLowering::Legal: {
4212 SDValue LHS = LegalizeOp(Node->getOperand(0));
4213 SDValue RHS = LegalizeOp(Node->getOperand(1));
4214
4215 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004216 MVT OType = Node->getValueType(1);
4217
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004218 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004219
Bill Wendling740464e2008-11-25 19:40:17 +00004220 // LHSSign -> LHS >= 0
4221 // RHSSign -> RHS >= 0
4222 // SumSign -> Sum >= 0
4223 //
4224 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
4225 //
4226 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4227 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
4228 SDValue SignsEq = DAG.getSetCC(OType, LHSSign, RHSSign, ISD::SETEQ);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004229
Bill Wendling740464e2008-11-25 19:40:17 +00004230 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4231 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004232
Bill Wendling740464e2008-11-25 19:40:17 +00004233 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsEq, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004234
4235 MVT ValueVTs[] = { LHS.getValueType(), OType };
4236 SDValue Ops[] = { Sum, Cmp };
4237
4238 Result = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 2), &Ops[0], 2);
4239 SDNode *RNode = Result.getNode();
4240 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4241 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4242 break;
4243 }
4244 }
4245
4246 break;
4247 }
Bill Wendlingdef27392008-11-24 01:38:29 +00004248 case ISD::UADDO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004249 MVT VT = Node->getValueType(0);
4250 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4251 default: assert(0 && "This action not supported for this op yet!");
4252 case TargetLowering::Custom:
4253 Result = TLI.LowerOperation(Op, DAG);
4254 if (Result.getNode()) break;
4255 // FALLTHROUGH
4256 case TargetLowering::Legal: {
4257 SDValue LHS = LegalizeOp(Node->getOperand(0));
4258 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004259
Bill Wendling41ea7e72008-11-24 19:21:46 +00004260 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
4261 MVT OType = Node->getValueType(1);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004262 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS, ISD::SETULT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004263
Bill Wendling41ea7e72008-11-24 19:21:46 +00004264 MVT ValueVTs[] = { LHS.getValueType(), OType };
4265 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004266
Bill Wendling41ea7e72008-11-24 19:21:46 +00004267 Result = DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], 2), &Ops[0], 2);
4268 SDNode *RNode = Result.getNode();
4269 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4270 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4271 break;
4272 }
4273 }
4274
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004275 break;
4276 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00004277 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004278
Chris Lattner4ddd2832006-04-08 04:13:17 +00004279 assert(Result.getValueType() == Op.getValueType() &&
4280 "Bad legalization!");
4281
Chris Lattner456a93a2006-01-28 07:39:30 +00004282 // Make sure that the generated code is itself legal.
4283 if (Result != Op)
4284 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004285
Chris Lattner45982da2005-05-12 16:53:42 +00004286 // Note that LegalizeOp may be reentered even from single-use nodes, which
4287 // means that we always must cache transformed nodes.
4288 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004289 return Result;
4290}
4291
Chris Lattner8b6fa222005-01-15 22:16:26 +00004292/// PromoteOp - Given an operation that produces a value in an invalid type,
4293/// promote it to compute the value into a larger type. The produced value will
4294/// have the correct bits for the low portion of the register, but no guarantee
4295/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004296SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004297 MVT VT = Op.getValueType();
4298 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004299 assert(getTypeAction(VT) == Promote &&
4300 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004301 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004302 "Cannot promote to smaller type!");
4303
Dan Gohman475871a2008-07-27 21:46:04 +00004304 SDValue Tmp1, Tmp2, Tmp3;
4305 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004306 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004307
Dan Gohman475871a2008-07-27 21:46:04 +00004308 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004309 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004310
Chris Lattner03c85462005-01-15 05:21:40 +00004311 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004312 case ISD::CopyFromReg:
4313 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004314 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004315#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004316 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004317#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004318 assert(0 && "Do not know how to promote this operator!");
4319 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004320 case ISD::UNDEF:
4321 Result = DAG.getNode(ISD::UNDEF, NVT);
4322 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004323 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004324 if (VT != MVT::i1)
4325 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4326 else
4327 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004328 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4329 break;
4330 case ISD::ConstantFP:
4331 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4332 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4333 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004334
Chris Lattner82fbfb62005-01-18 02:59:52 +00004335 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004336 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004337 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004338 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004339 TLI.getSetCCResultType(Node->getOperand(0)),
4340 Node->getOperand(0), Node->getOperand(1),
4341 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004342 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004343
Chris Lattner03c85462005-01-15 05:21:40 +00004344 case ISD::TRUNCATE:
4345 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4346 case Legal:
4347 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004348 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004349 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004350 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004351 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4352 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004353 case Promote:
4354 // The truncation is not required, because we don't guarantee anything
4355 // about high bits anyway.
4356 Result = PromoteOp(Node->getOperand(0));
4357 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004358 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004359 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4360 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004361 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004362 }
4363 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004364 case ISD::SIGN_EXTEND:
4365 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004366 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004367 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4368 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4369 case Legal:
4370 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004371 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004372 break;
4373 case Promote:
4374 // Promote the reg if it's smaller.
4375 Result = PromoteOp(Node->getOperand(0));
4376 // The high bits are not guaranteed to be anything. Insert an extend.
4377 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004378 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004379 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004380 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004381 Result = DAG.getZeroExtendInReg(Result,
4382 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004383 break;
4384 }
4385 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004386 case ISD::CONVERT_RNDSAT: {
4387 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4388 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4389 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4390 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4391 "can only promote integers");
4392 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4393 Node->getOperand(1), Node->getOperand(2),
4394 Node->getOperand(3), Node->getOperand(4),
4395 CvtCode);
4396 break;
4397
4398 }
Chris Lattner35481892005-12-23 00:16:34 +00004399 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004400 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4401 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004402 Result = PromoteOp(Result);
4403 break;
4404
Chris Lattner8b6fa222005-01-15 22:16:26 +00004405 case ISD::FP_EXTEND:
4406 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4407 case ISD::FP_ROUND:
4408 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4409 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4410 case Promote: assert(0 && "Unreachable with 2 FP types!");
4411 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004412 if (Node->getConstantOperandVal(1) == 0) {
4413 // Input is legal? Do an FP_ROUND_INREG.
4414 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4415 DAG.getValueType(VT));
4416 } else {
4417 // Just remove the truncate, it isn't affecting the value.
4418 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4419 Node->getOperand(1));
4420 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004421 break;
4422 }
4423 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004424 case ISD::SINT_TO_FP:
4425 case ISD::UINT_TO_FP:
4426 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4427 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004428 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004429 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004430 break;
4431
4432 case Promote:
4433 Result = PromoteOp(Node->getOperand(0));
4434 if (Node->getOpcode() == ISD::SINT_TO_FP)
4435 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004436 Result,
4437 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004438 else
Chris Lattner23993562005-04-13 02:38:47 +00004439 Result = DAG.getZeroExtendInReg(Result,
4440 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004441 // No extra round required here.
4442 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004443 break;
4444 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004445 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4446 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004447 // Round if we cannot tolerate excess precision.
4448 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004449 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4450 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004451 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004452 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004453 break;
4454
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004455 case ISD::SIGN_EXTEND_INREG:
4456 Result = PromoteOp(Node->getOperand(0));
4457 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4458 Node->getOperand(1));
4459 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004460 case ISD::FP_TO_SINT:
4461 case ISD::FP_TO_UINT:
4462 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4463 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004464 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004465 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004466 break;
4467 case Promote:
4468 // The input result is prerounded, so we don't have to do anything
4469 // special.
4470 Tmp1 = PromoteOp(Node->getOperand(0));
4471 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004472 }
Nate Begemand2558e32005-08-14 01:20:53 +00004473 // If we're promoting a UINT to a larger size, check to see if the new node
4474 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4475 // we can use that instead. This allows us to generate better code for
4476 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4477 // legal, such as PowerPC.
4478 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004479 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004480 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4481 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004482 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4483 } else {
4484 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4485 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004486 break;
4487
Chris Lattner2c8086f2005-04-02 05:00:07 +00004488 case ISD::FABS:
4489 case ISD::FNEG:
4490 Tmp1 = PromoteOp(Node->getOperand(0));
4491 assert(Tmp1.getValueType() == NVT);
4492 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4493 // NOTE: we do not have to do any extra rounding here for
4494 // NoExcessFPPrecision, because we know the input will have the appropriate
4495 // precision, and these operations don't modify precision at all.
4496 break;
4497
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004498 case ISD::FLOG:
4499 case ISD::FLOG2:
4500 case ISD::FLOG10:
4501 case ISD::FEXP:
4502 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004503 case ISD::FSQRT:
4504 case ISD::FSIN:
4505 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004506 case ISD::FTRUNC:
4507 case ISD::FFLOOR:
4508 case ISD::FCEIL:
4509 case ISD::FRINT:
4510 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004511 Tmp1 = PromoteOp(Node->getOperand(0));
4512 assert(Tmp1.getValueType() == NVT);
4513 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004514 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004515 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4516 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004517 break;
4518
Evan Cheng9d24ac52008-09-09 23:35:53 +00004519 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004520 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004521 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004522 // directly as well, which may be better.
4523 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004524 Tmp2 = Node->getOperand(1);
4525 if (Node->getOpcode() == ISD::FPOW)
4526 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004527 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004528 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004529 if (NoExcessFPPrecision)
4530 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4531 DAG.getValueType(VT));
4532 break;
4533 }
4534
Dale Johannesene00a8a22008-08-28 02:44:49 +00004535 case ISD::ATOMIC_CMP_SWAP_8:
4536 case ISD::ATOMIC_CMP_SWAP_16:
4537 case ISD::ATOMIC_CMP_SWAP_32:
4538 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004539 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004540 Tmp2 = PromoteOp(Node->getOperand(2));
4541 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang28873102008-06-25 08:15:39 +00004542 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4543 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004544 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004545 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004546 // Remember that we legalized the chain.
4547 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4548 break;
4549 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00004550 case ISD::ATOMIC_LOAD_ADD_8:
4551 case ISD::ATOMIC_LOAD_SUB_8:
4552 case ISD::ATOMIC_LOAD_AND_8:
4553 case ISD::ATOMIC_LOAD_OR_8:
4554 case ISD::ATOMIC_LOAD_XOR_8:
4555 case ISD::ATOMIC_LOAD_NAND_8:
4556 case ISD::ATOMIC_LOAD_MIN_8:
4557 case ISD::ATOMIC_LOAD_MAX_8:
4558 case ISD::ATOMIC_LOAD_UMIN_8:
4559 case ISD::ATOMIC_LOAD_UMAX_8:
4560 case ISD::ATOMIC_SWAP_8:
4561 case ISD::ATOMIC_LOAD_ADD_16:
4562 case ISD::ATOMIC_LOAD_SUB_16:
4563 case ISD::ATOMIC_LOAD_AND_16:
4564 case ISD::ATOMIC_LOAD_OR_16:
4565 case ISD::ATOMIC_LOAD_XOR_16:
4566 case ISD::ATOMIC_LOAD_NAND_16:
4567 case ISD::ATOMIC_LOAD_MIN_16:
4568 case ISD::ATOMIC_LOAD_MAX_16:
4569 case ISD::ATOMIC_LOAD_UMIN_16:
4570 case ISD::ATOMIC_LOAD_UMAX_16:
4571 case ISD::ATOMIC_SWAP_16:
4572 case ISD::ATOMIC_LOAD_ADD_32:
4573 case ISD::ATOMIC_LOAD_SUB_32:
4574 case ISD::ATOMIC_LOAD_AND_32:
4575 case ISD::ATOMIC_LOAD_OR_32:
4576 case ISD::ATOMIC_LOAD_XOR_32:
4577 case ISD::ATOMIC_LOAD_NAND_32:
4578 case ISD::ATOMIC_LOAD_MIN_32:
4579 case ISD::ATOMIC_LOAD_MAX_32:
4580 case ISD::ATOMIC_LOAD_UMIN_32:
4581 case ISD::ATOMIC_LOAD_UMAX_32:
4582 case ISD::ATOMIC_SWAP_32:
4583 case ISD::ATOMIC_LOAD_ADD_64:
4584 case ISD::ATOMIC_LOAD_SUB_64:
4585 case ISD::ATOMIC_LOAD_AND_64:
4586 case ISD::ATOMIC_LOAD_OR_64:
4587 case ISD::ATOMIC_LOAD_XOR_64:
4588 case ISD::ATOMIC_LOAD_NAND_64:
4589 case ISD::ATOMIC_LOAD_MIN_64:
4590 case ISD::ATOMIC_LOAD_MAX_64:
4591 case ISD::ATOMIC_LOAD_UMIN_64:
4592 case ISD::ATOMIC_LOAD_UMAX_64:
4593 case ISD::ATOMIC_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004594 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004595 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00004596 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4597 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004598 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004599 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004600 // Remember that we legalized the chain.
4601 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4602 break;
4603 }
4604
Chris Lattner03c85462005-01-15 05:21:40 +00004605 case ISD::AND:
4606 case ISD::OR:
4607 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004608 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004609 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004610 case ISD::MUL:
4611 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004612 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004613 // that too is okay if they are integer operations.
4614 Tmp1 = PromoteOp(Node->getOperand(0));
4615 Tmp2 = PromoteOp(Node->getOperand(1));
4616 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4617 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004618 break;
4619 case ISD::FADD:
4620 case ISD::FSUB:
4621 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004622 Tmp1 = PromoteOp(Node->getOperand(0));
4623 Tmp2 = PromoteOp(Node->getOperand(1));
4624 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4625 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4626
4627 // Floating point operations will give excess precision that we may not be
4628 // able to tolerate. If we DO allow excess precision, just leave it,
4629 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004630 // FIXME: Why would we need to round FP ops more than integer ones?
4631 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004632 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004633 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4634 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004635 break;
4636
Chris Lattner8b6fa222005-01-15 22:16:26 +00004637 case ISD::SDIV:
4638 case ISD::SREM:
4639 // These operators require that their input be sign extended.
4640 Tmp1 = PromoteOp(Node->getOperand(0));
4641 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004642 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004643 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4644 DAG.getValueType(VT));
4645 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4646 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004647 }
4648 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4649
4650 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004651 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004652 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4653 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004654 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004655 case ISD::FDIV:
4656 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004657 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004658 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004659 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004660 case Expand: assert(0 && "not implemented");
4661 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4662 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004663 }
4664 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004665 case Expand: assert(0 && "not implemented");
4666 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4667 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004668 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004669 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4670
4671 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004672 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004673 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4674 DAG.getValueType(VT));
4675 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004676
4677 case ISD::UDIV:
4678 case ISD::UREM:
4679 // These operators require that their input be zero extended.
4680 Tmp1 = PromoteOp(Node->getOperand(0));
4681 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004682 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004683 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4684 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004685 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4686 break;
4687
4688 case ISD::SHL:
4689 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004690 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004691 break;
4692 case ISD::SRA:
4693 // The input value must be properly sign extended.
4694 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004695 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4696 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004697 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004698 break;
4699 case ISD::SRL:
4700 // The input value must be properly zero extended.
4701 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004702 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004703 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004704 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004705
4706 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004707 Tmp1 = Node->getOperand(0); // Get the chain.
4708 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004709 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4710 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004711 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004712 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004713 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004714 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004715 // Increment the pointer, VAList, to the next vaarg
4716 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004717 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004718 TLI.getPointerTy()));
4719 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004720 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004721 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004722 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004723 }
4724 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004725 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004726 break;
4727
Evan Cheng466685d2006-10-09 20:57:25 +00004728 case ISD::LOAD: {
4729 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004730 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4731 ? ISD::EXTLOAD : LD->getExtensionType();
4732 Result = DAG.getExtLoad(ExtType, NVT,
4733 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004734 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004735 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004736 LD->isVolatile(),
4737 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004738 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004739 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004740 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004741 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004742 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004743 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4744 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004745
Duncan Sands83ec4b62008-06-06 12:08:01 +00004746 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004747 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004748 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4749 // Ensure that the resulting node is at least the same size as the operands'
4750 // value types, because we cannot assume that TLI.getSetCCValueType() is
4751 // constant.
4752 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004753 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004754 }
Nate Begeman9373a812005-08-10 20:51:12 +00004755 case ISD::SELECT_CC:
4756 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4757 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4758 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004759 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004760 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004761 case ISD::BSWAP:
4762 Tmp1 = Node->getOperand(0);
4763 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4764 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4765 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004766 DAG.getConstant(NVT.getSizeInBits() -
4767 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004768 TLI.getShiftAmountTy()));
4769 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004770 case ISD::CTPOP:
4771 case ISD::CTTZ:
4772 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004773 // Zero extend the argument
4774 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004775 // Perform the larger operation, then subtract if needed.
4776 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004777 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004778 case ISD::CTPOP:
4779 Result = Tmp1;
4780 break;
4781 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004782 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004783 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004784 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004785 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004786 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004787 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004788 break;
4789 case ISD::CTLZ:
4790 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004791 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004792 DAG.getConstant(NVT.getSizeInBits() -
4793 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004794 break;
4795 }
4796 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004797 case ISD::EXTRACT_SUBVECTOR:
4798 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004799 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004800 case ISD::EXTRACT_VECTOR_ELT:
4801 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4802 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004803 }
4804
Gabor Greifba36cb52008-08-28 21:40:38 +00004805 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004806
4807 // Make sure the result is itself legal.
4808 Result = LegalizeOp(Result);
4809
4810 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004811 AddPromotedOperand(Op, Result);
4812 return Result;
4813}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004814
Dan Gohman7f321562007-06-25 16:23:39 +00004815/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4816/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4817/// based on the vector type. The return type of this matches the element type
4818/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004819SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004820 // We know that operand #0 is the Vec vector. If the index is a constant
4821 // or if the invec is a supported hardware type, we can use it. Otherwise,
4822 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004823 SDValue Vec = Op.getOperand(0);
4824 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004825
Duncan Sands83ec4b62008-06-06 12:08:01 +00004826 MVT TVT = Vec.getValueType();
4827 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004828
Dan Gohman7f321562007-06-25 16:23:39 +00004829 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4830 default: assert(0 && "This action is not supported yet!");
4831 case TargetLowering::Custom: {
4832 Vec = LegalizeOp(Vec);
4833 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004834 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004835 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004836 return Tmp3;
4837 break;
4838 }
4839 case TargetLowering::Legal:
4840 if (isTypeLegal(TVT)) {
4841 Vec = LegalizeOp(Vec);
4842 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004843 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004844 }
4845 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004846 case TargetLowering::Promote:
4847 assert(TVT.isVector() && "not vector type");
4848 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004849 case TargetLowering::Expand:
4850 break;
4851 }
4852
4853 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004854 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004855 Op = ScalarizeVectorOp(Vec);
4856 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004857 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004858 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004859 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00004860 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004861 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004862 Vec = Lo;
4863 } else {
4864 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004865 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004866 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004867 }
Dan Gohman7f321562007-06-25 16:23:39 +00004868
Chris Lattner15972212006-03-31 17:55:51 +00004869 // It's now an extract from the appropriate high or low part. Recurse.
4870 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004871 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004872 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004873 // Store the value to a temporary stack slot, then LOAD the scalar
4874 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00004875 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4876 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00004877
4878 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004879 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00004880 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4881 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004882
Duncan Sands8e4eb092008-06-08 20:54:56 +00004883 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004884 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004885 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004886 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004887
Dan Gohman7f321562007-06-25 16:23:39 +00004888 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4889
4890 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004891 }
Dan Gohman7f321562007-06-25 16:23:39 +00004892 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004893}
4894
Dan Gohman7f321562007-06-25 16:23:39 +00004895/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004896/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004897SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004898 // We know that operand #0 is the Vec vector. For now we assume the index
4899 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00004900 SDValue Vec = Op.getOperand(0);
4901 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00004902
Duncan Sands83ec4b62008-06-06 12:08:01 +00004903 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00004904
Duncan Sands83ec4b62008-06-06 12:08:01 +00004905 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00004906 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004907 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004908 }
4909
4910 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004911 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00004912 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004913 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00004914 Vec = Lo;
4915 } else {
4916 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004917 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4918 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004919 }
4920
4921 // It's now an extract from the appropriate high or low part. Recurse.
4922 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004923 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004924}
4925
Nate Begeman750ac1b2006-02-01 07:19:44 +00004926/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4927/// with condition CC on the current target. This usually involves legalizing
4928/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4929/// there may be no choice but to create a new SetCC node to represent the
4930/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00004931/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4932void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4933 SDValue &RHS,
4934 SDValue &CC) {
4935 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004936
4937 switch (getTypeAction(LHS.getValueType())) {
4938 case Legal:
4939 Tmp1 = LegalizeOp(LHS); // LHS
4940 Tmp2 = LegalizeOp(RHS); // RHS
4941 break;
4942 case Promote:
4943 Tmp1 = PromoteOp(LHS); // LHS
4944 Tmp2 = PromoteOp(RHS); // RHS
4945
4946 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004947 if (LHS.getValueType().isInteger()) {
4948 MVT VT = LHS.getValueType();
4949 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004950
4951 // Otherwise, we have to insert explicit sign or zero extends. Note
4952 // that we could insert sign extends for ALL conditions, but zero extend
4953 // is cheaper on many machines (an AND instead of two shifts), so prefer
4954 // it.
4955 switch (cast<CondCodeSDNode>(CC)->get()) {
4956 default: assert(0 && "Unknown integer comparison!");
4957 case ISD::SETEQ:
4958 case ISD::SETNE:
4959 case ISD::SETUGE:
4960 case ISD::SETUGT:
4961 case ISD::SETULE:
4962 case ISD::SETULT:
4963 // ALL of these operations will work if we either sign or zero extend
4964 // the operands (including the unsigned comparisons!). Zero extend is
4965 // usually a simpler/cheaper operation, so prefer it.
4966 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4967 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
4968 break;
4969 case ISD::SETGE:
4970 case ISD::SETGT:
4971 case ISD::SETLT:
4972 case ISD::SETLE:
4973 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4974 DAG.getValueType(VT));
4975 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4976 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00004977 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
4978 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00004979 break;
4980 }
4981 }
4982 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00004983 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004984 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00004985 if (VT == MVT::f32 || VT == MVT::f64) {
4986 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00004987 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00004988 switch (cast<CondCodeSDNode>(CC)->get()) {
4989 case ISD::SETEQ:
4990 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00004991 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004992 break;
4993 case ISD::SETNE:
4994 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00004995 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00004996 break;
4997 case ISD::SETGE:
4998 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00004999 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005000 break;
5001 case ISD::SETLT:
5002 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005003 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005004 break;
5005 case ISD::SETLE:
5006 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005007 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005008 break;
5009 case ISD::SETGT:
5010 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005011 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005012 break;
5013 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005014 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5015 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005016 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005017 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005018 break;
5019 default:
Evan Cheng56966222007-01-12 02:11:51 +00005020 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005021 switch (cast<CondCodeSDNode>(CC)->get()) {
5022 case ISD::SETONE:
5023 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005024 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005025 // Fallthrough
5026 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005027 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005028 break;
5029 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005030 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005031 break;
5032 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005033 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005034 break;
5035 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005036 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005037 break;
Evan Cheng56966222007-01-12 02:11:51 +00005038 case ISD::SETUEQ:
5039 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005040 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005041 default: assert(0 && "Unsupported FP setcc!");
5042 }
5043 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005044
Dan Gohman475871a2008-07-27 21:46:04 +00005045 SDValue Dummy;
5046 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00005047 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005048 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005049 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005050 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005051 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005052 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00005053 CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00005054 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005055 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005056 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00005057 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00005058 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005059 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005060 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005061 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005062 RHS = Tmp2;
5063 return;
5064 }
5065
Dan Gohman475871a2008-07-27 21:46:04 +00005066 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005067 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005068 ExpandOp(RHS, RHSLo, RHSHi);
5069 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5070
5071 if (VT==MVT::ppcf128) {
5072 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005073 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005074 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005075 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005076 // The following can be improved, but not that much.
Dale Johannesene2f20832008-09-12 00:30:56 +00005077 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5078 ISD::SETOEQ);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005079 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005080 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesene2f20832008-09-12 00:30:56 +00005081 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5082 ISD::SETUNE);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005083 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005084 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5085 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005086 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005087 break;
5088 }
5089
5090 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005091 case ISD::SETEQ:
5092 case ISD::SETNE:
5093 if (RHSLo == RHSHi)
5094 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5095 if (RHSCST->isAllOnesValue()) {
5096 // Comparison to -1.
5097 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5098 Tmp2 = RHSLo;
5099 break;
5100 }
5101
5102 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5103 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5104 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5105 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5106 break;
5107 default:
5108 // If this is a comparison of the sign bit, just look at the top part.
5109 // X > -1, x < 0
5110 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5111 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005112 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005113 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5114 CST->isAllOnesValue())) { // X > -1
5115 Tmp1 = LHSHi;
5116 Tmp2 = RHSHi;
5117 break;
5118 }
5119
5120 // FIXME: This generated code sucks.
5121 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005122 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005123 default: assert(0 && "Unknown integer setcc!");
5124 case ISD::SETLT:
5125 case ISD::SETULT: LowCC = ISD::SETULT; break;
5126 case ISD::SETGT:
5127 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5128 case ISD::SETLE:
5129 case ISD::SETULE: LowCC = ISD::SETULE; break;
5130 case ISD::SETGE:
5131 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5132 }
5133
5134 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5135 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5136 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5137
5138 // NOTE: on targets without efficient SELECT of bools, we can always use
5139 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005140 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005141 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00005142 LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005143 if (!Tmp1.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005144 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5145 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005146 CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005147 if (!Tmp2.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005148 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005149 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005150
Gabor Greifba36cb52008-08-28 21:40:38 +00005151 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5152 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005153 if ((Tmp1C && Tmp1C->isNullValue()) ||
5154 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005155 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5156 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005157 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005158 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5159 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5160 // low part is known false, returns high part.
5161 // For LE / GE, if high part is known false, ignore the low part.
5162 // For LT / GT, if high part is known true, ignore the low part.
5163 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005164 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005165 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005166 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005167 ISD::SETEQ, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005168 if (!Result.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005169 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005170 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005171 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5172 Result, Tmp1, Tmp2));
5173 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005174 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005175 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005176 }
5177 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005178 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005179 LHS = Tmp1;
5180 RHS = Tmp2;
5181}
5182
Evan Cheng7f042682008-10-15 02:05:31 +00005183/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5184/// condition code CC on the current target. This routine assumes LHS and rHS
5185/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5186/// illegal condition code into AND / OR of multiple SETCC values.
5187void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5188 SDValue &LHS, SDValue &RHS,
5189 SDValue &CC) {
5190 MVT OpVT = LHS.getValueType();
5191 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5192 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5193 default: assert(0 && "Unknown condition code action!");
5194 case TargetLowering::Legal:
5195 // Nothing to do.
5196 break;
5197 case TargetLowering::Expand: {
5198 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5199 unsigned Opc = 0;
5200 switch (CCCode) {
5201 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005202 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5203 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5204 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5205 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5206 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5207 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5208 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5209 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5210 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5211 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5212 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5213 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005214 // FIXME: Implement more expansions.
5215 }
5216
5217 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5218 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5219 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5220 RHS = SDValue();
5221 CC = SDValue();
5222 break;
5223 }
5224 }
5225}
5226
Chris Lattner1401d152008-01-16 07:45:30 +00005227/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5228/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5229/// a load from the stack slot to DestVT, extending it if needed.
5230/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005231SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5232 MVT SlotVT,
5233 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005234 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005235 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5236 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005237 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005238
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005239 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005240 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00005241
Duncan Sands83ec4b62008-06-06 12:08:01 +00005242 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5243 unsigned SlotSize = SlotVT.getSizeInBits();
5244 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005245 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5246 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005247
Chris Lattner1401d152008-01-16 07:45:30 +00005248 // Emit a store to the stack slot. Use a truncstore if the input value is
5249 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005250 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005251
Chris Lattner1401d152008-01-16 07:45:30 +00005252 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005253 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005254 PseudoSourceValue::getFixedStack(SPFI), 0,
5255 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005256 else {
5257 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005258 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005259 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00005260 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005261 }
5262
Chris Lattner35481892005-12-23 00:16:34 +00005263 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005264 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00005265 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005266
5267 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00005268 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5269 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005270}
5271
Dan Gohman475871a2008-07-27 21:46:04 +00005272SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005273 // Create a vector sized/aligned stack slot, store the value to element #0,
5274 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005275 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005276
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005277 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005278 int SPFI = StackPtrFI->getIndex();
5279
Dan Gohman475871a2008-07-27 21:46:04 +00005280 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005281 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005282 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005283 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005284}
5285
5286
Chris Lattnerce872152006-03-19 06:31:19 +00005287/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005288/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005289SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005290
5291 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005292 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005293 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005294 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005295 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005296
Dan Gohman475871a2008-07-27 21:46:04 +00005297 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005298 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005299 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005300 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005301 bool isConstant = true;
5302 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5303 SplatValue.getOpcode() != ISD::UNDEF)
5304 isConstant = false;
5305
Evan Cheng033e6812006-03-24 01:17:21 +00005306 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005307 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005308 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005309 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005310 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005311 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005312 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005313
5314 // If this isn't a constant element or an undef, we can't use a constant
5315 // pool load.
5316 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5317 V.getOpcode() != ISD::UNDEF)
5318 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005319 }
5320
5321 if (isOnlyLowElement) {
5322 // If the low element is an undef too, then this whole things is an undef.
5323 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5324 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5325 // Otherwise, turn this into a scalar_to_vector node.
5326 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5327 Node->getOperand(0));
5328 }
5329
Chris Lattner2eb86532006-03-24 07:29:17 +00005330 // If all elements are constants, create a load from the constant pool.
5331 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005332 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005333 std::vector<Constant*> CV;
5334 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5335 if (ConstantFPSDNode *V =
5336 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005337 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005338 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005339 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005340 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005341 } else {
5342 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005343 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005344 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005345 CV.push_back(UndefValue::get(OpNTy));
5346 }
5347 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005348 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005349 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005350 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005351 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005352 PseudoSourceValue::getConstantPool(), 0,
5353 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005354 }
5355
Gabor Greifba36cb52008-08-28 21:40:38 +00005356 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005357 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005358 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005359 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5360 std::vector<SDValue> ZeroVec(NumElems, Zero);
5361 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005362 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005363
5364 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005365 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005366 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005367 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005368 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5369
5370 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5371 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5372 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5373 SplatMask);
5374 }
5375 }
5376
Evan Cheng033e6812006-03-24 01:17:21 +00005377 // If there are only two unique elements, we may be able to turn this into a
5378 // vector shuffle.
5379 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005380 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005381 SDValue Val1 = Node->getOperand(1);
5382 SDValue Val2;
5383 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005384 if (MI->first != Val1)
5385 Val2 = MI->first;
5386 else
5387 Val2 = (++MI)->first;
5388
5389 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5390 // vector shuffle has the undef vector on the RHS.
5391 if (Val1.getOpcode() == ISD::UNDEF)
5392 std::swap(Val1, Val2);
5393
Evan Cheng033e6812006-03-24 01:17:21 +00005394 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005395 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5396 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005397 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005398
5399 // Set elements of the shuffle mask for Val1.
5400 std::vector<unsigned> &Val1Elts = Values[Val1];
5401 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5402 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5403
5404 // Set elements of the shuffle mask for Val2.
5405 std::vector<unsigned> &Val2Elts = Values[Val2];
5406 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5407 if (Val2.getOpcode() != ISD::UNDEF)
5408 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5409 else
5410 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5411
Dan Gohman475871a2008-07-27 21:46:04 +00005412 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005413 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005414
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005415 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005416 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5417 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005418 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5419 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005420 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005421
5422 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005423 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005424 }
5425 }
Chris Lattnerce872152006-03-19 06:31:19 +00005426
5427 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5428 // aligned object on the stack, store each element into it, then load
5429 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005430 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005431 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005432 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005433
5434 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005435 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005436 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005437 // Store (in the right endianness) the elements to memory.
5438 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5439 // Ignore undef elements.
5440 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5441
Chris Lattner841c8822006-03-22 01:46:54 +00005442 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005443
Dan Gohman475871a2008-07-27 21:46:04 +00005444 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005445 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5446
Evan Cheng786225a2006-10-05 23:01:46 +00005447 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005448 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005449 }
5450
Dan Gohman475871a2008-07-27 21:46:04 +00005451 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005452 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005453 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5454 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005455 else
5456 StoreChain = DAG.getEntryNode();
5457
5458 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005459 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005460}
5461
Chris Lattner5b359c62005-04-02 04:00:59 +00005462void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005463 SDValue Op, SDValue Amt,
5464 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005465 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005466 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005467 ExpandOp(Op, LHSL, LHSH);
5468
Dan Gohman475871a2008-07-27 21:46:04 +00005469 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005470 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005471 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005472 Hi = Lo.getValue(1);
5473}
5474
5475
Chris Lattnere34b3962005-01-19 04:19:40 +00005476/// ExpandShift - Try to find a clever way to expand this shift operation out to
5477/// smaller elements. If we can't find a way that is more efficient than a
5478/// libcall on this target, return false. Otherwise, return true with the
5479/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005480bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5481 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005482 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5483 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005484
Duncan Sands83ec4b62008-06-06 12:08:01 +00005485 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005486 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005487 MVT ShTy = ShAmt.getValueType();
5488 unsigned ShBits = ShTy.getSizeInBits();
5489 unsigned VTBits = Op.getValueType().getSizeInBits();
5490 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005491
Chris Lattner7a3c8552007-10-14 20:35:12 +00005492 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005493 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005494 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005495 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005496 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005497 ExpandOp(Op, InL, InH);
5498 switch(Opc) {
5499 case ISD::SHL:
5500 if (Cst > VTBits) {
5501 Lo = DAG.getConstant(0, NVT);
5502 Hi = DAG.getConstant(0, NVT);
5503 } else if (Cst > NVTBits) {
5504 Lo = DAG.getConstant(0, NVT);
5505 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005506 } else if (Cst == NVTBits) {
5507 Lo = DAG.getConstant(0, NVT);
5508 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005509 } else {
5510 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5511 Hi = DAG.getNode(ISD::OR, NVT,
5512 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5513 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5514 }
5515 return true;
5516 case ISD::SRL:
5517 if (Cst > VTBits) {
5518 Lo = DAG.getConstant(0, NVT);
5519 Hi = DAG.getConstant(0, NVT);
5520 } else if (Cst > NVTBits) {
5521 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5522 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005523 } else if (Cst == NVTBits) {
5524 Lo = InH;
5525 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005526 } else {
5527 Lo = DAG.getNode(ISD::OR, NVT,
5528 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5529 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5530 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5531 }
5532 return true;
5533 case ISD::SRA:
5534 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005535 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005536 DAG.getConstant(NVTBits-1, ShTy));
5537 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005538 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005539 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005540 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005541 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005542 } else if (Cst == NVTBits) {
5543 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005544 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005545 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005546 } else {
5547 Lo = DAG.getNode(ISD::OR, NVT,
5548 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5549 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5550 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5551 }
5552 return true;
5553 }
5554 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005555
5556 // Okay, the shift amount isn't constant. However, if we can tell that it is
5557 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005558 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5559 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005560 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005561
Dan Gohman9e255b72008-02-22 01:12:31 +00005562 // If we know that if any of the high bits of the shift amount are one, then
5563 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005564 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005565 // Mask out the high bit, which we know is set.
5566 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005567 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005568
5569 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005570 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005571 ExpandOp(Op, InL, InH);
5572 switch(Opc) {
5573 case ISD::SHL:
5574 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5575 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5576 return true;
5577 case ISD::SRL:
5578 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5579 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5580 return true;
5581 case ISD::SRA:
5582 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5583 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5584 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5585 return true;
5586 }
5587 }
5588
Dan Gohman9e255b72008-02-22 01:12:31 +00005589 // If we know that the high bits of the shift amount are all zero, then we can
5590 // do this as a couple of simple shifts.
5591 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005592 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005593 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005594 DAG.getConstant(NVTBits, Amt.getValueType()),
5595 Amt);
5596
5597 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005598 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005599 ExpandOp(Op, InL, InH);
5600 switch(Opc) {
5601 case ISD::SHL:
5602 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5603 Hi = DAG.getNode(ISD::OR, NVT,
5604 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5605 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5606 return true;
5607 case ISD::SRL:
5608 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5609 Lo = DAG.getNode(ISD::OR, NVT,
5610 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5611 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5612 return true;
5613 case ISD::SRA:
5614 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5615 Lo = DAG.getNode(ISD::OR, NVT,
5616 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5617 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5618 return true;
5619 }
5620 }
5621
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005622 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005623}
Chris Lattner77e77a62005-01-21 06:05:23 +00005624
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005625
Chris Lattner77e77a62005-01-21 06:05:23 +00005626// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5627// does not fit into a register, return the lo part and set the hi part to the
5628// by-reg argument. If it does fit into a single register, return the result
5629// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005630SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5631 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005632 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5633 // The input chain to this libcall is the entry node of the function.
5634 // Legalizing the call will automatically add the previous call to the
5635 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005636 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005637
Chris Lattner77e77a62005-01-21 06:05:23 +00005638 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005639 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005640 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005641 MVT ArgVT = Node->getOperand(i).getValueType();
5642 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005643 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005644 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005645 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005646 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005647 }
Bill Wendling056292f2008-09-16 21:48:12 +00005648 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005649 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005650
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005651 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005652 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005653 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005654 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5655 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005656
Chris Lattner6831a812006-02-13 09:18:02 +00005657 // Legalize the call sequence, starting with the chain. This will advance
5658 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5659 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5660 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005661 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005662 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005663 default: assert(0 && "Unknown thing");
5664 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005665 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005666 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005667 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005668 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005669 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005670 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005671 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005672}
5673
Dan Gohman7f8613e2008-08-14 20:04:46 +00005674/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5675///
5676SDValue SelectionDAGLegalize::
5677LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5678 bool isCustom = false;
5679 SDValue Tmp1;
5680 switch (getTypeAction(Op.getValueType())) {
5681 case Legal:
5682 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5683 Op.getValueType())) {
5684 default: assert(0 && "Unknown operation action!");
5685 case TargetLowering::Custom:
5686 isCustom = true;
5687 // FALLTHROUGH
5688 case TargetLowering::Legal:
5689 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005690 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005691 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5692 else
5693 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5694 DestTy, Tmp1);
5695 if (isCustom) {
5696 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005697 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005698 }
5699 break;
5700 case TargetLowering::Expand:
5701 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5702 break;
5703 case TargetLowering::Promote:
5704 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5705 break;
5706 }
5707 break;
5708 case Expand:
5709 Result = ExpandIntToFP(isSigned, DestTy, Op);
5710 break;
5711 case Promote:
5712 Tmp1 = PromoteOp(Op);
5713 if (isSigned) {
5714 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5715 Tmp1, DAG.getValueType(Op.getValueType()));
5716 } else {
5717 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5718 Op.getValueType());
5719 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005720 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005721 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5722 else
5723 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5724 DestTy, Tmp1);
5725 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5726 break;
5727 }
5728 return Result;
5729}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005730
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005731/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5732///
Dan Gohman475871a2008-07-27 21:46:04 +00005733SDValue SelectionDAGLegalize::
5734ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005735 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005736 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005737
Dan Gohman7f8613e2008-08-14 20:04:46 +00005738 // Expand unsupported int-to-fp vector casts by unrolling them.
5739 if (DestTy.isVector()) {
5740 if (!ExpandSource)
5741 return LegalizeOp(UnrollVectorOp(Source));
5742 MVT DestEltTy = DestTy.getVectorElementType();
5743 if (DestTy.getVectorNumElements() == 1) {
5744 SDValue Scalar = ScalarizeVectorOp(Source);
5745 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5746 DestEltTy, Scalar);
5747 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5748 }
5749 SDValue Lo, Hi;
5750 SplitVectorOp(Source, Lo, Hi);
5751 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5752 DestTy.getVectorNumElements() / 2);
5753 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5754 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengefa53392008-10-13 18:46:18 +00005755 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5756 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005757 }
5758
Evan Cheng9845eb52008-04-01 02:18:22 +00005759 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5760 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005761 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005762 // incoming integer is set. To handle this, we dynamically test to see if
5763 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005764 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005765 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005766 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005767 ExpandOp(Source, Lo, Hi);
5768 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5769 } else {
5770 // The comparison for the sign bit will use the entire operand.
5771 Hi = Source;
5772 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005773
Dale Johannesen53997b02008-11-04 20:52:49 +00005774 // Check to see if the target has a custom way to lower this. If so, use
5775 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005776 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5777 default: assert(0 && "This action not implemented for this operation!");
5778 case TargetLowering::Legal:
5779 case TargetLowering::Expand:
5780 break; // This case is handled below.
5781 case TargetLowering::Custom: {
5782 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5783 Source), DAG);
5784 if (NV.getNode())
5785 return LegalizeOp(NV);
5786 break; // The target decided this was legal after all
5787 }
5788 }
5789
Chris Lattner66de05b2005-05-13 04:45:13 +00005790 // If this is unsigned, and not supported, first perform the conversion to
5791 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005792 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005793
Dan Gohman475871a2008-07-27 21:46:04 +00005794 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005795 DAG.getConstant(0, Hi.getValueType()),
5796 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005797 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5798 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005799 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005800 uint64_t FF = 0x5f800000ULL;
5801 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005802 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005803
Dan Gohman475871a2008-07-27 21:46:04 +00005804 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005805 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere9c35e72005-04-13 05:09:42 +00005806 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005807 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005808 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005809 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005810 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005811 PseudoSourceValue::getConstantPool(), 0,
5812 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005813 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005814 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005815 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005816 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005817 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005818 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005819 else
5820 assert(0 && "Unexpected conversion");
5821
Duncan Sands83ec4b62008-06-06 12:08:01 +00005822 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005823 if (SCVT != DestTy) {
5824 // Destination type needs to be expanded as well. The FADD now we are
5825 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005826 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5827 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005828 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005829 SignedConv, SignedConv.getValue(1));
5830 }
5831 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5832 }
Chris Lattner473a9902005-09-29 06:44:39 +00005833 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005834 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005835
Chris Lattnera88a2602005-05-14 05:33:54 +00005836 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005837 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005838 default: assert(0 && "This action not implemented for this operation!");
5839 case TargetLowering::Legal:
5840 case TargetLowering::Expand:
5841 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005842 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005843 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005844 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005845 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005846 return LegalizeOp(NV);
5847 break; // The target decided this was legal after all
5848 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005849 }
5850
Chris Lattner13689e22005-05-12 07:00:44 +00005851 // Expand the source, then glue it back together for the call. We must expand
5852 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005853 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005854 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005855 ExpandOp(Source, SrcLo, SrcHi);
5856 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5857 }
Chris Lattner13689e22005-05-12 07:00:44 +00005858
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005859 RTLIB::Libcall LC = isSigned ?
5860 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5861 RTLIB::getUINTTOFP(SourceVT, DestTy);
5862 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5863
Chris Lattner6831a812006-02-13 09:18:02 +00005864 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00005865 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00005866 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5867 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmana2e94852008-03-10 23:03:31 +00005868 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5869 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005870}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005871
Chris Lattner22cde6a2006-01-28 08:25:58 +00005872/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5873/// INT_TO_FP operation of the specified operand when the target requests that
5874/// we expand it. At this point, we know that the result and operand types are
5875/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00005876SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5877 SDValue Op0,
5878 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005879 if (Op0.getValueType() == MVT::i32) {
5880 // simple 32-bit [signed|unsigned] integer to float/double expansion
5881
Chris Lattner23594d42008-01-16 07:03:22 +00005882 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00005883 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00005884
Chris Lattner22cde6a2006-01-28 08:25:58 +00005885 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00005886 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00005887 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00005888 SDValue Hi = StackSlot;
5889 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00005890 if (TLI.isLittleEndian())
5891 std::swap(Hi, Lo);
5892
Chris Lattner22cde6a2006-01-28 08:25:58 +00005893 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00005894 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005895 if (isSigned) {
5896 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00005897 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005898 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5899 } else {
5900 Op0Mapped = Op0;
5901 }
5902 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00005903 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005904 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005905 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005906 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005907 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00005908 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005909 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005910 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005911 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00005912 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00005913 BitsToDouble(0x4330000080000000ULL)
5914 : BitsToDouble(0x4330000000000000ULL),
5915 MVT::f64);
5916 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00005917 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005918 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00005919 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005920 // handle final rounding
5921 if (DestVT == MVT::f64) {
5922 // do nothing
5923 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00005924 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005925 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5926 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005927 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005928 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005929 }
5930 return Result;
5931 }
5932 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00005933 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005934
Dan Gohman475871a2008-07-27 21:46:04 +00005935 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005936 DAG.getConstant(0, Op0.getValueType()),
5937 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005938 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5939 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005940 SignSet, Four, Zero);
5941
5942 // If the sign bit of the integer is set, the large number will be treated
5943 // as a negative number. To counteract this, the dynamic code adds an
5944 // offset depending on the data type.
5945 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005946 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005947 default: assert(0 && "Unsupported integer type!");
5948 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5949 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5950 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5951 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5952 }
5953 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005954 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005955
Dan Gohman475871a2008-07-27 21:46:04 +00005956 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005957 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005958 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005959 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005960 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005961 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005962 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005963 PseudoSourceValue::getConstantPool(), 0,
5964 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005965 else {
Dan Gohman69de1932008-02-06 22:27:42 +00005966 FudgeInReg =
5967 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
5968 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005969 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005970 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00005971 }
5972
5973 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
5974}
5975
5976/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
5977/// *INT_TO_FP operation of the specified operand when the target requests that
5978/// we promote it. At this point, we know that the result and operand types are
5979/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
5980/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00005981SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
5982 MVT DestVT,
5983 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005984 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005985 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005986
5987 unsigned OpToUse = 0;
5988
5989 // Scan for the appropriate larger type to use.
5990 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005991 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
5992 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00005993
5994 // If the target supports SINT_TO_FP of this type, use it.
5995 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
5996 default: break;
5997 case TargetLowering::Legal:
5998 if (!TLI.isTypeLegal(NewInTy))
5999 break; // Can't use this datatype.
6000 // FALL THROUGH.
6001 case TargetLowering::Custom:
6002 OpToUse = ISD::SINT_TO_FP;
6003 break;
6004 }
6005 if (OpToUse) break;
6006 if (isSigned) continue;
6007
6008 // If the target supports UINT_TO_FP of this type, use it.
6009 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6010 default: break;
6011 case TargetLowering::Legal:
6012 if (!TLI.isTypeLegal(NewInTy))
6013 break; // Can't use this datatype.
6014 // FALL THROUGH.
6015 case TargetLowering::Custom:
6016 OpToUse = ISD::UINT_TO_FP;
6017 break;
6018 }
6019 if (OpToUse) break;
6020
6021 // Otherwise, try a larger type.
6022 }
6023
6024 // Okay, we found the operation and type to use. Zero extend our input to the
6025 // desired type then run the operation on it.
6026 return DAG.getNode(OpToUse, DestVT,
6027 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6028 NewInTy, LegalOp));
6029}
6030
6031/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6032/// FP_TO_*INT operation of the specified operand when the target requests that
6033/// we promote it. At this point, we know that the result and operand types are
6034/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6035/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006036SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6037 MVT DestVT,
6038 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006039 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006040 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006041
6042 unsigned OpToUse = 0;
6043
6044 // Scan for the appropriate larger type to use.
6045 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006046 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6047 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006048
6049 // If the target supports FP_TO_SINT returning this type, use it.
6050 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6051 default: break;
6052 case TargetLowering::Legal:
6053 if (!TLI.isTypeLegal(NewOutTy))
6054 break; // Can't use this datatype.
6055 // FALL THROUGH.
6056 case TargetLowering::Custom:
6057 OpToUse = ISD::FP_TO_SINT;
6058 break;
6059 }
6060 if (OpToUse) break;
6061
6062 // If the target supports FP_TO_UINT of this type, use it.
6063 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6064 default: break;
6065 case TargetLowering::Legal:
6066 if (!TLI.isTypeLegal(NewOutTy))
6067 break; // Can't use this datatype.
6068 // FALL THROUGH.
6069 case TargetLowering::Custom:
6070 OpToUse = ISD::FP_TO_UINT;
6071 break;
6072 }
6073 if (OpToUse) break;
6074
6075 // Otherwise, try a larger type.
6076 }
6077
Chris Lattner27a6c732007-11-24 07:07:01 +00006078
6079 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00006080 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006081
Chris Lattner27a6c732007-11-24 07:07:01 +00006082 // If the operation produces an invalid type, it must be custom lowered. Use
6083 // the target lowering hooks to expand it. Just keep the low part of the
6084 // expanded operation, we know that we're truncating anyway.
6085 if (getTypeAction(NewOutTy) == Expand) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006086 Operation = SDValue(TLI.ReplaceNodeResults(Operation.getNode(), DAG), 0);
6087 assert(Operation.getNode() && "Didn't return anything");
Chris Lattner27a6c732007-11-24 07:07:01 +00006088 }
Duncan Sands126d9072008-07-04 11:47:58 +00006089
Chris Lattner27a6c732007-11-24 07:07:01 +00006090 // Truncate the result of the extended FP_TO_*INT operation to the desired
6091 // size.
6092 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006093}
6094
6095/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6096///
Dan Gohman475871a2008-07-27 21:46:04 +00006097SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006098 MVT VT = Op.getValueType();
6099 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006100 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006101 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006102 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6103 case MVT::i16:
6104 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6105 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6106 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6107 case MVT::i32:
6108 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6109 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6110 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6111 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6112 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6113 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6114 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6115 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6116 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6117 case MVT::i64:
6118 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6119 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6120 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6121 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6122 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6123 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6124 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6125 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6126 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6127 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6128 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6129 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6130 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6131 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6132 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6133 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6134 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6135 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6136 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6137 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6138 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6139 }
6140}
6141
6142/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6143///
Dan Gohman475871a2008-07-27 21:46:04 +00006144SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006145 switch (Opc) {
6146 default: assert(0 && "Cannot expand this yet!");
6147 case ISD::CTPOP: {
6148 static const uint64_t mask[6] = {
6149 0x5555555555555555ULL, 0x3333333333333333ULL,
6150 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6151 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6152 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006153 MVT VT = Op.getValueType();
6154 MVT ShVT = TLI.getShiftAmountTy();
6155 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006156 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6157 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman475871a2008-07-27 21:46:04 +00006158 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6159 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006160 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6161 DAG.getNode(ISD::AND, VT,
6162 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6163 }
6164 return Op;
6165 }
6166 case ISD::CTLZ: {
6167 // for now, we do this:
6168 // x = x | (x >> 1);
6169 // x = x | (x >> 2);
6170 // ...
6171 // x = x | (x >>16);
6172 // x = x | (x >>32); // for 64-bit input
6173 // return popcount(~x);
6174 //
6175 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006176 MVT VT = Op.getValueType();
6177 MVT ShVT = TLI.getShiftAmountTy();
6178 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006179 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006180 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006181 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6182 }
6183 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6184 return DAG.getNode(ISD::CTPOP, VT, Op);
6185 }
6186 case ISD::CTTZ: {
6187 // for now, we use: { return popcount(~x & (x - 1)); }
6188 // unless the target has ctlz but not ctpop, in which case we use:
6189 // { return 32 - nlz(~x & (x-1)); }
6190 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006191 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00006192 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6193 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006194 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6195 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6196 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6197 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6198 TLI.isOperationLegal(ISD::CTLZ, VT))
6199 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006200 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006201 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6202 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6203 }
6204 }
6205}
Chris Lattnere34b3962005-01-19 04:19:40 +00006206
Dan Gohman475871a2008-07-27 21:46:04 +00006207/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006208/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006209/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006210/// ExpandedNodes map is filled in for any results that are expanded, and the
6211/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006212void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006213 MVT VT = Op.getValueType();
6214 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006215 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006216 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006217 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006218 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006219
Chris Lattner6fdcb252005-09-02 20:32:45 +00006220 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006221 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006222 = ExpandedNodes.find(Op);
6223 if (I != ExpandedNodes.end()) {
6224 Lo = I->second.first;
6225 Hi = I->second.second;
6226 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006227 }
6228
Chris Lattner3e928bb2005-01-07 07:47:09 +00006229 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006230 case ISD::CopyFromReg:
6231 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006232 case ISD::FP_ROUND_INREG:
6233 if (VT == MVT::ppcf128 &&
6234 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6235 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006236 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006237 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6238 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006239 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006240 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006241 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6242 Lo = Result.getNode()->getOperand(0);
6243 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006244 break;
6245 }
6246 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006247 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006248#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006249 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006250#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006251 assert(0 && "Do not know how to expand this operator!");
6252 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006253 case ISD::EXTRACT_ELEMENT:
6254 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006255 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006256 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006257 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006258 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006259 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6260 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6261 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006262 case ISD::UNDEF:
6263 Lo = DAG.getNode(ISD::UNDEF, NVT);
6264 Hi = DAG.getNode(ISD::UNDEF, NVT);
6265 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006266 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006267 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006268 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6269 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6270 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006271 break;
6272 }
Evan Cheng00495212006-12-12 21:32:44 +00006273 case ISD::ConstantFP: {
6274 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006275 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006276 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006277 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6278 MVT::f64);
6279 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6280 MVT::f64);
6281 break;
6282 }
Evan Cheng279101e2006-12-12 22:19:28 +00006283 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006284 if (getTypeAction(Lo.getValueType()) == Expand)
6285 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006286 break;
6287 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006288 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006289 // Return the operands.
6290 Lo = Node->getOperand(0);
6291 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006292 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006293
6294 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006295 if (Node->getNumValues() == 1) {
6296 ExpandOp(Op.getOperand(0), Lo, Hi);
6297 break;
6298 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006299 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006300 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006301 Op.getValue(1).getValueType() == MVT::Other &&
6302 "unhandled MERGE_VALUES");
6303 ExpandOp(Op.getOperand(0), Lo, Hi);
6304 // Remember that we legalized the chain.
6305 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6306 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006307
6308 case ISD::SIGN_EXTEND_INREG:
6309 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006310 // sext_inreg the low part if needed.
6311 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6312
6313 // The high part gets the sign extension from the lo-part. This handles
6314 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006315 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006316 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006317 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006318 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006319
Nate Begemand88fc032006-01-14 03:14:10 +00006320 case ISD::BSWAP: {
6321 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006322 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006323 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6324 Lo = TempLo;
6325 break;
6326 }
6327
Chris Lattneredb1add2005-05-11 04:51:16 +00006328 case ISD::CTPOP:
6329 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006330 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6331 DAG.getNode(ISD::CTPOP, NVT, Lo),
6332 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006333 Hi = DAG.getConstant(0, NVT);
6334 break;
6335
Chris Lattner39a8f332005-05-12 19:05:01 +00006336 case ISD::CTLZ: {
6337 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006338 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006339 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6340 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6341 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006342 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006343 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006344 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6345
6346 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6347 Hi = DAG.getConstant(0, NVT);
6348 break;
6349 }
6350
6351 case ISD::CTTZ: {
6352 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006353 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006354 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6355 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6356 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006357 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006358 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006359 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6360
6361 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6362 Hi = DAG.getConstant(0, NVT);
6363 break;
6364 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006365
Nate Begemanacc398c2006-01-25 18:21:52 +00006366 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006367 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6368 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006369 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6370 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6371
6372 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006373 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006374 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006375 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006376 std::swap(Lo, Hi);
6377 break;
6378 }
6379
Chris Lattner3e928bb2005-01-07 07:47:09 +00006380 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006381 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006382 SDValue Ch = LD->getChain(); // Legalize the chain.
6383 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006384 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006385 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006386 int SVOffset = LD->getSrcValueOffset();
6387 unsigned Alignment = LD->getAlignment();
6388 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006389
Evan Cheng466685d2006-10-09 20:57:25 +00006390 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006391 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006392 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006393 if (VT == MVT::f32 || VT == MVT::f64) {
6394 // f32->i32 or f64->i64 one to one expansion.
6395 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006396 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006397 // Recursively expand the new load.
6398 if (getTypeAction(NVT) == Expand)
6399 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006400 break;
6401 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006402
Evan Cheng466685d2006-10-09 20:57:25 +00006403 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006404 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006405 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006406 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006407 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006408 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006409 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006410 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006411
Evan Cheng466685d2006-10-09 20:57:25 +00006412 // Build a factor node to remember that this load is independent of the
6413 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006414 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006415 Hi.getValue(1));
6416
6417 // Remember that we legalized the chain.
6418 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006419 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006420 std::swap(Lo, Hi);
6421 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006422 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006423
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006424 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6425 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006426 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006427 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006428 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006429 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006430 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006431 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6432 break;
6433 }
Evan Cheng466685d2006-10-09 20:57:25 +00006434
6435 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006436 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006437 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006438 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006439 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006440 SVOffset, EVT, isVolatile,
6441 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006442
6443 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006444 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006445
6446 if (ExtType == ISD::SEXTLOAD) {
6447 // The high part is obtained by SRA'ing all but one of the bits of the
6448 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006449 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006450 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6451 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6452 } else if (ExtType == ISD::ZEXTLOAD) {
6453 // The high part is just a zero.
6454 Hi = DAG.getConstant(0, NVT);
6455 } else /* if (ExtType == ISD::EXTLOAD) */ {
6456 // The high part is undefined.
6457 Hi = DAG.getNode(ISD::UNDEF, NVT);
6458 }
6459 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006460 break;
6461 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006462 case ISD::AND:
6463 case ISD::OR:
6464 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006465 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006466 ExpandOp(Node->getOperand(0), LL, LH);
6467 ExpandOp(Node->getOperand(1), RL, RH);
6468 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6469 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6470 break;
6471 }
6472 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006473 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006474 ExpandOp(Node->getOperand(1), LL, LH);
6475 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006476 if (getTypeAction(NVT) == Expand)
6477 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006478 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006479 if (VT != MVT::f32)
6480 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006481 break;
6482 }
Nate Begeman9373a812005-08-10 20:51:12 +00006483 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006484 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006485 ExpandOp(Node->getOperand(2), TL, TH);
6486 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006487 if (getTypeAction(NVT) == Expand)
6488 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006489 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6490 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006491 if (VT != MVT::f32)
6492 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6493 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006494 break;
6495 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006496 case ISD::ANY_EXTEND:
6497 // The low part is any extension of the input (which degenerates to a copy).
6498 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6499 // The high part is undefined.
6500 Hi = DAG.getNode(ISD::UNDEF, NVT);
6501 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006502 case ISD::SIGN_EXTEND: {
6503 // The low part is just a sign extension of the input (which degenerates to
6504 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006505 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006506
Chris Lattner3e928bb2005-01-07 07:47:09 +00006507 // The high part is obtained by SRA'ing all but one of the bits of the lo
6508 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006509 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006510 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6511 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006512 break;
6513 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006514 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006515 // The low part is just a zero extension of the input (which degenerates to
6516 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006517 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006518
Chris Lattner3e928bb2005-01-07 07:47:09 +00006519 // The high part is just a zero.
6520 Hi = DAG.getConstant(0, NVT);
6521 break;
Chris Lattner35481892005-12-23 00:16:34 +00006522
Chris Lattner4c948eb2007-02-13 23:55:16 +00006523 case ISD::TRUNCATE: {
6524 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006525 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006526 ExpandOp(Node->getOperand(0), NewLo, Hi);
6527
6528 // The low part is now either the right size, or it is closer. If not the
6529 // right size, make an illegal truncate so we recursively expand it.
6530 if (NewLo.getValueType() != Node->getValueType(0))
6531 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6532 ExpandOp(NewLo, Lo, Hi);
6533 break;
6534 }
6535
Chris Lattner35481892005-12-23 00:16:34 +00006536 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006537 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006538 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6539 // If the target wants to, allow it to lower this itself.
6540 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6541 case Expand: assert(0 && "cannot expand FP!");
6542 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6543 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6544 }
6545 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6546 }
6547
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006548 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006549 if (VT == MVT::f32 || VT == MVT::f64) {
6550 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006551 if (getTypeAction(NVT) == Expand)
6552 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006553 break;
6554 }
6555
6556 // If source operand will be expanded to the same type as VT, i.e.
6557 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006558 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006559 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6560 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006561 break;
6562 }
6563
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006564 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006565 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006566 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006567
Chris Lattner35481892005-12-23 00:16:34 +00006568 ExpandOp(Tmp, Lo, Hi);
6569 break;
6570 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006571
Chris Lattner27a6c732007-11-24 07:07:01 +00006572 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006573 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6574 TargetLowering::Custom &&
6575 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006576 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006577 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006578 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006579 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006580 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006581 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006582 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006583
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006584 case ISD::ATOMIC_CMP_SWAP_64: {
6585 // This operation does not need a loop.
6586 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6587 assert(Tmp.getNode() && "Node must be custom expanded!");
6588 ExpandOp(Tmp.getValue(0), Lo, Hi);
6589 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6590 LegalizeOp(Tmp.getValue(1)));
6591 break;
6592 }
6593
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006594 case ISD::ATOMIC_LOAD_ADD_64:
6595 case ISD::ATOMIC_LOAD_SUB_64:
6596 case ISD::ATOMIC_LOAD_AND_64:
6597 case ISD::ATOMIC_LOAD_OR_64:
6598 case ISD::ATOMIC_LOAD_XOR_64:
6599 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006600 case ISD::ATOMIC_SWAP_64: {
6601 // These operations require a loop to be generated. We can't do that yet,
6602 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006603 SDValue In2Lo, In2Hi, In2;
6604 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6605 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006606 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6607 SDValue Replace =
6608 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6609 Anode->getSrcValue(), Anode->getAlignment());
6610 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006611 ExpandOp(Result.getValue(0), Lo, Hi);
6612 // Remember that we legalized the chain.
6613 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006614 break;
6615 }
6616
Chris Lattner4e6c7462005-01-08 19:27:05 +00006617 // These operators cannot be expanded directly, emit them as calls to
6618 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006619 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006620 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006621 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006622 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6623 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006624 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6625 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006626 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006627
Chris Lattnerf20d1832005-07-30 01:40:57 +00006628 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6629
Chris Lattner80a3e942005-07-29 00:33:32 +00006630 // Now that the custom expander is done, expand the result, which is still
6631 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006632 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006633 ExpandOp(Op, Lo, Hi);
6634 break;
6635 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006636 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006637
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006638 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6639 VT);
6640 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6641 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006642 break;
Evan Cheng56966222007-01-12 02:11:51 +00006643 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006644
Evan Cheng56966222007-01-12 02:11:51 +00006645 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006646 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006647 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006648 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6649 case Expand: assert(0 && "cannot expand FP!");
6650 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6651 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6652 }
6653
6654 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6655
6656 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006657 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006658 ExpandOp(Op, Lo, Hi);
6659 break;
6660 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006661 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006662
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006663 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6664 VT);
6665 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6666 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006667 break;
Evan Cheng56966222007-01-12 02:11:51 +00006668 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006669
Evan Cheng05a2d562006-01-09 18:31:59 +00006670 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006671 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006672 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006673 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006674 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006675 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006676 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006677 // Now that the custom expander is done, expand the result, which is
6678 // still VT.
6679 ExpandOp(Op, Lo, Hi);
6680 break;
6681 }
6682 }
6683
Chris Lattner79980b02006-09-13 03:50:39 +00006684 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6685 // this X << 1 as X+X.
6686 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006687 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006688 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006689 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006690 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6691 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6692 LoOps[1] = LoOps[0];
6693 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6694
6695 HiOps[1] = HiOps[0];
6696 HiOps[2] = Lo.getValue(1);
6697 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6698 break;
6699 }
6700 }
6701
Chris Lattnere34b3962005-01-19 04:19:40 +00006702 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006703 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006704 break;
Chris Lattner47599822005-04-02 03:38:53 +00006705
6706 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006707 TargetLowering::LegalizeAction Action =
6708 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6709 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6710 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006711 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006712 break;
6713 }
6714
Chris Lattnere34b3962005-01-19 04:19:40 +00006715 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006716 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006717 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006718 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006719
Evan Cheng05a2d562006-01-09 18:31:59 +00006720 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006721 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006722 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006723 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006724 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006725 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006726 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006727 // Now that the custom expander is done, expand the result, which is
6728 // still VT.
6729 ExpandOp(Op, Lo, Hi);
6730 break;
6731 }
6732 }
6733
Chris Lattnere34b3962005-01-19 04:19:40 +00006734 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006735 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006736 break;
Chris Lattner47599822005-04-02 03:38:53 +00006737
6738 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006739 TargetLowering::LegalizeAction Action =
6740 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6741 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6742 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006743 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006744 break;
6745 }
6746
Chris Lattnere34b3962005-01-19 04:19:40 +00006747 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006748 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006749 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006750 }
6751
6752 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006753 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006754 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006755 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006756 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006757 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006758 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006759 // Now that the custom expander is done, expand the result, which is
6760 // still VT.
6761 ExpandOp(Op, Lo, Hi);
6762 break;
6763 }
6764 }
6765
Chris Lattnere34b3962005-01-19 04:19:40 +00006766 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006767 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006768 break;
Chris Lattner47599822005-04-02 03:38:53 +00006769
6770 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006771 TargetLowering::LegalizeAction Action =
6772 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6773 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6774 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006775 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006776 break;
6777 }
6778
Chris Lattnere34b3962005-01-19 04:19:40 +00006779 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006780 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006781 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006782 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006783
Misha Brukmanedf128a2005-04-21 22:36:52 +00006784 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006785 case ISD::SUB: {
6786 // If the target wants to custom expand this, let them.
6787 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6788 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006789 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006790 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006791 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006792 break;
6793 }
6794 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006795 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006796 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006797 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6798 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006799 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006800 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006801 LoOps[0] = LHSL;
6802 LoOps[1] = RHSL;
6803 HiOps[0] = LHSH;
6804 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006805
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006806 //cascaded check to see if any smaller size has a a carry flag.
6807 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6808 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006809 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6810 MVT AVT = MVT::getIntegerVT(BitSize);
6811 if (TLI.isOperationLegal(OpV, AVT)) {
6812 hasCarry = true;
6813 break;
6814 }
6815 }
6816
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006817 if(hasCarry) {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006818 if (Node->getOpcode() == ISD::ADD) {
6819 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6820 HiOps[2] = Lo.getValue(1);
6821 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6822 } else {
6823 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6824 HiOps[2] = Lo.getValue(1);
6825 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6826 }
6827 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006828 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006829 if (Node->getOpcode() == ISD::ADD) {
6830 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
6831 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006832 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6833 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006834 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6835 DAG.getConstant(1, NVT),
6836 DAG.getConstant(0, NVT));
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006837 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6838 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006839 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6840 DAG.getConstant(1, NVT),
6841 Carry1);
6842 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6843 } else {
6844 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
6845 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
6846 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6847 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6848 DAG.getConstant(1, NVT),
6849 DAG.getConstant(0, NVT));
6850 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6851 }
6852 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006853 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006854 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006855
6856 case ISD::ADDC:
6857 case ISD::SUBC: {
6858 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006859 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006860 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6861 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6862 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006863 SDValue LoOps[2] = { LHSL, RHSL };
6864 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006865
6866 if (Node->getOpcode() == ISD::ADDC) {
6867 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6868 HiOps[2] = Lo.getValue(1);
6869 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6870 } else {
6871 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6872 HiOps[2] = Lo.getValue(1);
6873 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6874 }
6875 // Remember that we legalized the flag.
6876 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6877 break;
6878 }
6879 case ISD::ADDE:
6880 case ISD::SUBE: {
6881 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006882 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006883 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6884 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6885 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006886 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6887 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006888
6889 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6890 HiOps[2] = Lo.getValue(1);
6891 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6892
6893 // Remember that we legalized the flag.
6894 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6895 break;
6896 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006897 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006898 // If the target wants to custom expand this, let them.
6899 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006900 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006901 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006902 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006903 break;
6904 }
6905 }
6906
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006907 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6908 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006909 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6910 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6911 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00006912 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00006913 ExpandOp(Node->getOperand(0), LL, LH);
6914 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006915 unsigned OuterBitSize = Op.getValueSizeInBits();
6916 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006917 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6918 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006919 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6920 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6921 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006922 // The inputs are both zero-extended.
6923 if (HasUMUL_LOHI) {
6924 // We can emit a umul_lohi.
6925 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006926 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006927 break;
6928 }
6929 if (HasMULHU) {
6930 // We can emit a mulhu+mul.
6931 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6932 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6933 break;
6934 }
Dan Gohman525178c2007-10-08 18:33:35 +00006935 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006936 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006937 // The input values are both sign-extended.
6938 if (HasSMUL_LOHI) {
6939 // We can emit a smul_lohi.
6940 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006941 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006942 break;
6943 }
6944 if (HasMULHS) {
6945 // We can emit a mulhs+mul.
6946 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6947 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6948 break;
6949 }
6950 }
6951 if (HasUMUL_LOHI) {
6952 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00006953 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00006954 DAG.getVTList(NVT, NVT), LL, RL);
6955 Lo = UMulLOHI;
6956 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006957 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6958 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6959 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6960 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006961 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00006962 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00006963 if (HasMULHU) {
6964 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6965 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6966 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6967 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6968 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6969 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
6970 break;
6971 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006972 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006973
Dan Gohman525178c2007-10-08 18:33:35 +00006974 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006975 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00006976 break;
6977 }
Evan Cheng56966222007-01-12 02:11:51 +00006978 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006979 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006980 break;
6981 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00006982 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006983 break;
6984 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006985 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006986 break;
6987 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00006988 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00006989 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00006990
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006991 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00006992 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
6993 RTLIB::ADD_F64,
6994 RTLIB::ADD_F80,
6995 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00006996 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006997 break;
6998 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00006999 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7000 RTLIB::SUB_F64,
7001 RTLIB::SUB_F80,
7002 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007003 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007004 break;
7005 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007006 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7007 RTLIB::MUL_F64,
7008 RTLIB::MUL_F80,
7009 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007010 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007011 break;
7012 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007013 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7014 RTLIB::DIV_F64,
7015 RTLIB::DIV_F80,
7016 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007017 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007018 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007019 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007020 if (VT == MVT::ppcf128) {
7021 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7022 Node->getOperand(0).getValueType()==MVT::f64);
7023 const uint64_t zero = 0;
7024 if (Node->getOperand(0).getValueType()==MVT::f32)
7025 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7026 else
7027 Hi = Node->getOperand(0);
7028 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7029 break;
7030 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007031 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7032 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7033 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007034 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007035 }
7036 case ISD::FP_ROUND: {
7037 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7038 VT);
7039 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7040 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007041 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007042 }
Evan Cheng4b887022008-09-09 23:02:14 +00007043 case ISD::FSQRT:
7044 case ISD::FSIN:
7045 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007046 case ISD::FLOG:
7047 case ISD::FLOG2:
7048 case ISD::FLOG10:
7049 case ISD::FEXP:
7050 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007051 case ISD::FTRUNC:
7052 case ISD::FFLOOR:
7053 case ISD::FCEIL:
7054 case ISD::FRINT:
7055 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007056 case ISD::FPOW:
7057 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007058 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007059 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007060 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007061 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7062 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007063 break;
7064 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007065 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7066 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007067 break;
7068 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007069 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7070 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007071 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007072 case ISD::FLOG:
7073 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7074 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7075 break;
7076 case ISD::FLOG2:
7077 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7078 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7079 break;
7080 case ISD::FLOG10:
7081 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7082 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7083 break;
7084 case ISD::FEXP:
7085 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7086 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7087 break;
7088 case ISD::FEXP2:
7089 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7090 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7091 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007092 case ISD::FTRUNC:
7093 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7094 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7095 break;
7096 case ISD::FFLOOR:
7097 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7098 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7099 break;
7100 case ISD::FCEIL:
7101 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7102 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7103 break;
7104 case ISD::FRINT:
7105 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7106 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7107 break;
7108 case ISD::FNEARBYINT:
7109 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7110 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7111 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007112 case ISD::FPOW:
7113 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7114 RTLIB::POW_PPCF128);
7115 break;
7116 case ISD::FPOWI:
7117 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7118 RTLIB::POWI_PPCF128);
7119 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007120 default: assert(0 && "Unreachable!");
7121 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007122 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007123 break;
7124 }
Evan Cheng966bf242006-12-16 00:52:40 +00007125 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007126 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007127 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007128 ExpandOp(Node->getOperand(0), Lo, Tmp);
7129 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7130 // lo = hi==fabs(hi) ? lo : -lo;
7131 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7132 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7133 DAG.getCondCode(ISD::SETEQ));
7134 break;
7135 }
Dan Gohman475871a2008-07-27 21:46:04 +00007136 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007137 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7138 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7139 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7140 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7141 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7142 if (getTypeAction(NVT) == Expand)
7143 ExpandOp(Lo, Lo, Hi);
7144 break;
7145 }
7146 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007147 if (VT == MVT::ppcf128) {
7148 ExpandOp(Node->getOperand(0), Lo, Hi);
7149 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7150 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7151 break;
7152 }
Dan Gohman475871a2008-07-27 21:46:04 +00007153 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007154 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7155 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7156 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7157 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7158 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7159 if (getTypeAction(NVT) == Expand)
7160 ExpandOp(Lo, Lo, Hi);
7161 break;
7162 }
Evan Cheng912095b2007-01-04 21:56:39 +00007163 case ISD::FCOPYSIGN: {
7164 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7165 if (getTypeAction(NVT) == Expand)
7166 ExpandOp(Lo, Lo, Hi);
7167 break;
7168 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007169 case ISD::SINT_TO_FP:
7170 case ISD::UINT_TO_FP: {
7171 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007172 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007173
7174 // Promote the operand if needed. Do this before checking for
7175 // ppcf128 so conversions of i16 and i8 work.
7176 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007177 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007178 Tmp = isSigned
7179 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7180 DAG.getValueType(SrcVT))
7181 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007182 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007183 SrcVT = Node->getOperand(0).getValueType();
7184 }
7185
Dan Gohmana2e94852008-03-10 23:03:31 +00007186 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007187 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007188 if (isSigned) {
7189 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7190 Node->getOperand(0)));
7191 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7192 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007193 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007194 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7195 Node->getOperand(0)));
7196 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7197 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007198 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007199 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7200 DAG.getConstant(0, MVT::i32),
7201 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7202 DAG.getConstantFP(
7203 APFloat(APInt(128, 2, TwoE32)),
7204 MVT::ppcf128)),
7205 Hi,
7206 DAG.getCondCode(ISD::SETLT)),
7207 Lo, Hi);
7208 }
7209 break;
7210 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007211 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7212 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007213 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007214 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7215 Lo, Hi);
7216 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7217 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7218 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7219 DAG.getConstant(0, MVT::i64),
7220 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7221 DAG.getConstantFP(
7222 APFloat(APInt(128, 2, TwoE64)),
7223 MVT::ppcf128)),
7224 Hi,
7225 DAG.getCondCode(ISD::SETLT)),
7226 Lo, Hi);
7227 break;
7228 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007229
Dan Gohmana2e94852008-03-10 23:03:31 +00007230 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7231 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00007232 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007233 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007234 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007235 break;
7236 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007237 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007238
Chris Lattner83397362005-12-21 18:02:52 +00007239 // Make sure the resultant values have been legalized themselves, unless this
7240 // is a type that requires multi-step expansion.
7241 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7242 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007243 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007244 // Don't legalize the high part if it is expanded to a single node.
7245 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007246 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007247
7248 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007249 bool isNew =
7250 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007251 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007252 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007253}
7254
Dan Gohman7f321562007-06-25 16:23:39 +00007255/// SplitVectorOp - Given an operand of vector type, break it down into
7256/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007257void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7258 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007259 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007260 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007261 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007262 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007263
Duncan Sands83ec4b62008-06-06 12:08:01 +00007264 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007265
7266 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7267 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7268
Duncan Sands83ec4b62008-06-06 12:08:01 +00007269 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7270 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007271
Chris Lattnerc7029802006-03-18 01:44:44 +00007272 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007273 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007274 = SplitNodes.find(Op);
7275 if (I != SplitNodes.end()) {
7276 Lo = I->second.first;
7277 Hi = I->second.second;
7278 return;
7279 }
7280
7281 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007282 default:
7283#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007284 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007285#endif
7286 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007287 case ISD::UNDEF:
7288 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7289 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7290 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007291 case ISD::BUILD_PAIR:
7292 Lo = Node->getOperand(0);
7293 Hi = Node->getOperand(1);
7294 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007295 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007296 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7297 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007298 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007299 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007300 if (Index < NewNumElts_Lo)
7301 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7302 DAG.getIntPtrConstant(Index));
7303 else
7304 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7305 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7306 break;
7307 }
Dan Gohman475871a2008-07-27 21:46:04 +00007308 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007309 Node->getOperand(1),
7310 Node->getOperand(2));
7311 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007312 break;
7313 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007314 case ISD::VECTOR_SHUFFLE: {
7315 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007316 SDValue Mask = Node->getOperand(2);
7317 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007318 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007319
7320 // Insert all of the elements from the input that are needed. We use
7321 // buildvector of extractelement here because the input vectors will have
7322 // to be legalized, so this makes the code simpler.
7323 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007324 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007325 if (IdxNode.getOpcode() == ISD::UNDEF) {
7326 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7327 continue;
7328 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007329 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007330 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007331 if (Idx >= NumElements) {
7332 InVec = Node->getOperand(1);
7333 Idx -= NumElements;
7334 }
7335 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7336 DAG.getConstant(Idx, PtrVT)));
7337 }
7338 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7339 Ops.clear();
7340
7341 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007342 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007343 if (IdxNode.getOpcode() == ISD::UNDEF) {
7344 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7345 continue;
7346 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007347 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007348 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007349 if (Idx >= NumElements) {
7350 InVec = Node->getOperand(1);
7351 Idx -= NumElements;
7352 }
7353 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7354 DAG.getConstant(Idx, PtrVT)));
7355 }
Mon P Wang92879f32008-07-25 01:30:26 +00007356 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007357 break;
7358 }
Dan Gohman7f321562007-06-25 16:23:39 +00007359 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007360 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007361 Node->op_begin()+NewNumElts_Lo);
7362 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007363
Dan Gohman475871a2008-07-27 21:46:04 +00007364 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007365 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007366 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007367 break;
7368 }
Dan Gohman7f321562007-06-25 16:23:39 +00007369 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007370 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007371 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7372 if (NewNumSubvectors == 1) {
7373 Lo = Node->getOperand(0);
7374 Hi = Node->getOperand(1);
7375 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007376 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7377 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007378 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007379
Mon P Wangaeb06d22008-11-10 04:46:22 +00007380 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007381 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007382 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007383 }
Dan Gohman65956352007-06-13 15:12:02 +00007384 break;
7385 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007386 case ISD::EXTRACT_SUBVECTOR: {
7387 SDValue Vec = Op.getOperand(0);
7388 SDValue Idx = Op.getOperand(1);
7389 MVT IdxVT = Idx.getValueType();
7390
7391 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7392 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7393 if (CIdx) {
7394 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7395 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7396 IdxVT));
7397 } else {
7398 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7399 DAG.getConstant(NewNumElts_Lo, IdxVT));
7400 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7401 }
7402 break;
7403 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007404 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007405 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007406
Dan Gohman475871a2008-07-27 21:46:04 +00007407 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007408 SplitVectorOp(Node->getOperand(1), LL, LH);
7409 SplitVectorOp(Node->getOperand(2), RL, RH);
7410
Duncan Sands83ec4b62008-06-06 12:08:01 +00007411 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007412 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007413 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007414 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007415 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7416 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007417 } else {
7418 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007419 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7420 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007421 }
7422 break;
7423 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007424 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007425 SDValue CondLHS = Node->getOperand(0);
7426 SDValue CondRHS = Node->getOperand(1);
7427 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007428
Dan Gohman475871a2008-07-27 21:46:04 +00007429 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007430 SplitVectorOp(Node->getOperand(2), LL, LH);
7431 SplitVectorOp(Node->getOperand(3), RL, RH);
7432
7433 // Handle a simple select with vector operands.
7434 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7435 LL, RL, CondCode);
7436 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7437 LH, RH, CondCode);
7438 break;
7439 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007440 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007441 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007442 SplitVectorOp(Node->getOperand(0), LL, LH);
7443 SplitVectorOp(Node->getOperand(1), RL, RH);
7444 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7445 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7446 break;
7447 }
Dan Gohman7f321562007-06-25 16:23:39 +00007448 case ISD::ADD:
7449 case ISD::SUB:
7450 case ISD::MUL:
7451 case ISD::FADD:
7452 case ISD::FSUB:
7453 case ISD::FMUL:
7454 case ISD::SDIV:
7455 case ISD::UDIV:
7456 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007457 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007458 case ISD::AND:
7459 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007460 case ISD::XOR:
7461 case ISD::UREM:
7462 case ISD::SREM:
7463 case ISD::FREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00007464 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007465 SplitVectorOp(Node->getOperand(0), LL, LH);
7466 SplitVectorOp(Node->getOperand(1), RL, RH);
7467
Nate Begeman5db1afb2007-11-15 21:15:26 +00007468 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7469 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007470 break;
7471 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007472 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007473 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007474 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007475 SplitVectorOp(Node->getOperand(0), L, H);
7476
Nate Begeman5db1afb2007-11-15 21:15:26 +00007477 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7478 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007479 break;
7480 }
7481 case ISD::CTTZ:
7482 case ISD::CTLZ:
7483 case ISD::CTPOP:
7484 case ISD::FNEG:
7485 case ISD::FABS:
7486 case ISD::FSQRT:
7487 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007488 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007489 case ISD::FLOG:
7490 case ISD::FLOG2:
7491 case ISD::FLOG10:
7492 case ISD::FEXP:
7493 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007494 case ISD::FP_TO_SINT:
7495 case ISD::FP_TO_UINT:
7496 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007497 case ISD::UINT_TO_FP:
7498 case ISD::TRUNCATE:
7499 case ISD::ANY_EXTEND:
7500 case ISD::SIGN_EXTEND:
7501 case ISD::ZERO_EXTEND:
7502 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007503 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007504 SplitVectorOp(Node->getOperand(0), L, H);
7505
Nate Begeman5db1afb2007-11-15 21:15:26 +00007506 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7507 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007508 break;
7509 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007510 case ISD::CONVERT_RNDSAT: {
7511 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7512 SDValue L, H;
7513 SplitVectorOp(Node->getOperand(0), L, H);
7514 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7515 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7516 SDValue STyOpL = DAG.getValueType(L.getValueType());
7517 SDValue STyOpH = DAG.getValueType(H.getValueType());
7518
7519 SDValue RndOp = Node->getOperand(3);
7520 SDValue SatOp = Node->getOperand(4);
7521
7522 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7523 RndOp, SatOp, CvtCode);
7524 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7525 RndOp, SatOp, CvtCode);
7526 break;
7527 }
Dan Gohman7f321562007-06-25 16:23:39 +00007528 case ISD::LOAD: {
7529 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007530 SDValue Ch = LD->getChain();
7531 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007532 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007533 const Value *SV = LD->getSrcValue();
7534 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007535 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007536 unsigned Alignment = LD->getAlignment();
7537 bool isVolatile = LD->isVolatile();
7538
Dan Gohman7f8613e2008-08-14 20:04:46 +00007539 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7540 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7541
7542 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7543 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7544 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7545
7546 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7547 NewVT_Lo, Ch, Ptr, Offset,
7548 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7549 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007550 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007551 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007552 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007553 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007554 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7555 NewVT_Hi, Ch, Ptr, Offset,
7556 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007557
7558 // Build a factor node to remember that this load is independent of the
7559 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007560 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007561 Hi.getValue(1));
7562
7563 // Remember that we legalized the chain.
7564 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007565 break;
7566 }
Dan Gohman7f321562007-06-25 16:23:39 +00007567 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007568 // We know the result is a vector. The input may be either a vector or a
7569 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007570 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007571 if (!InOp.getValueType().isVector() ||
7572 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007573 // The input is a scalar or single-element vector.
7574 // Lower to a store/load so that it can be split.
7575 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007576 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7577 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007578 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007579 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007580
Dan Gohman475871a2008-07-27 21:46:04 +00007581 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007582 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007583 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007584 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007585 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007586 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007587 // Split the vector and convert each of the pieces now.
7588 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007589 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7590 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007591 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007592 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007593 }
7594
7595 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007596 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007597 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007598 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007599 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007600}
7601
7602
Dan Gohman89b20c02007-06-27 14:06:22 +00007603/// ScalarizeVectorOp - Given an operand of single-element vector type
7604/// (e.g. v1f32), convert it into the equivalent operation that returns a
7605/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007606SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007607 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007608 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007609 MVT NewVT = Op.getValueType().getVectorElementType();
7610 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007611
Dan Gohman7f321562007-06-25 16:23:39 +00007612 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007613 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007614 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007615
Dan Gohman475871a2008-07-27 21:46:04 +00007616 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007617 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007618 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007619#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007620 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007621#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007622 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7623 case ISD::ADD:
7624 case ISD::FADD:
7625 case ISD::SUB:
7626 case ISD::FSUB:
7627 case ISD::MUL:
7628 case ISD::FMUL:
7629 case ISD::SDIV:
7630 case ISD::UDIV:
7631 case ISD::FDIV:
7632 case ISD::SREM:
7633 case ISD::UREM:
7634 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007635 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007636 case ISD::AND:
7637 case ISD::OR:
7638 case ISD::XOR:
7639 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007640 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007641 ScalarizeVectorOp(Node->getOperand(0)),
7642 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007643 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007644 case ISD::FNEG:
7645 case ISD::FABS:
7646 case ISD::FSQRT:
7647 case ISD::FSIN:
7648 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007649 case ISD::FLOG:
7650 case ISD::FLOG2:
7651 case ISD::FLOG10:
7652 case ISD::FEXP:
7653 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007654 case ISD::FP_TO_SINT:
7655 case ISD::FP_TO_UINT:
7656 case ISD::SINT_TO_FP:
7657 case ISD::UINT_TO_FP:
7658 case ISD::SIGN_EXTEND:
7659 case ISD::ZERO_EXTEND:
7660 case ISD::ANY_EXTEND:
7661 case ISD::TRUNCATE:
7662 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007663 Result = DAG.getNode(Node->getOpcode(),
7664 NewVT,
7665 ScalarizeVectorOp(Node->getOperand(0)));
7666 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007667 case ISD::CONVERT_RNDSAT: {
7668 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7669 Result = DAG.getConvertRndSat(NewVT, Op0,
7670 DAG.getValueType(NewVT),
7671 DAG.getValueType(Op0.getValueType()),
7672 Node->getOperand(3),
7673 Node->getOperand(4),
7674 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7675 break;
7676 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007677 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007678 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007679 Result = DAG.getNode(Node->getOpcode(),
7680 NewVT,
7681 ScalarizeVectorOp(Node->getOperand(0)),
7682 Node->getOperand(1));
7683 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007684 case ISD::LOAD: {
7685 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007686 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7687 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007688 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007689 const Value *SV = LD->getSrcValue();
7690 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007691 MVT MemoryVT = LD->getMemoryVT();
7692 unsigned Alignment = LD->getAlignment();
7693 bool isVolatile = LD->isVolatile();
7694
7695 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7696 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7697
7698 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7699 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7700 MemoryVT.getVectorElementType(),
7701 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007702
Chris Lattnerc7029802006-03-18 01:44:44 +00007703 // Remember that we legalized the chain.
7704 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7705 break;
7706 }
Dan Gohman7f321562007-06-25 16:23:39 +00007707 case ISD::BUILD_VECTOR:
7708 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007709 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007710 case ISD::INSERT_VECTOR_ELT:
7711 // Returning the inserted scalar element.
7712 Result = Node->getOperand(1);
7713 break;
7714 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007715 assert(Node->getOperand(0).getValueType() == NewVT &&
7716 "Concat of non-legal vectors not yet supported!");
7717 Result = Node->getOperand(0);
7718 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007719 case ISD::VECTOR_SHUFFLE: {
7720 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007721 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007722 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007723 Result = ScalarizeVectorOp(Node->getOperand(1));
7724 else
7725 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007726 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007727 }
7728 case ISD::EXTRACT_SUBVECTOR:
Mon P Wange0b436a2008-11-06 22:52:21 +00007729 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangaeb06d22008-11-10 04:46:22 +00007730 Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007731 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007732 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007733 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007734 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007735 Op0 = ScalarizeVectorOp(Op0);
7736 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007737 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007738 }
Dan Gohman7f321562007-06-25 16:23:39 +00007739 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007740 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007741 ScalarizeVectorOp(Op.getOperand(1)),
7742 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007743 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007744 case ISD::SELECT_CC:
7745 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7746 Node->getOperand(1),
7747 ScalarizeVectorOp(Op.getOperand(2)),
7748 ScalarizeVectorOp(Op.getOperand(3)),
7749 Node->getOperand(4));
7750 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007751 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007752 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7753 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007754 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7755 Op.getOperand(2));
7756 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7757 DAG.getConstant(-1ULL, NewVT),
7758 DAG.getConstant(0ULL, NewVT));
7759 break;
7760 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007761 }
7762
7763 if (TLI.isTypeLegal(NewVT))
7764 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007765 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7766 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007767 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007768 return Result;
7769}
7770
Chris Lattner3e928bb2005-01-07 07:47:09 +00007771
Mon P Wang0c397192008-10-30 08:01:45 +00007772SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7773 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7774 if (I != WidenNodes.end()) return I->second;
7775
7776 MVT VT = Op.getValueType();
7777 assert(VT.isVector() && "Cannot widen non-vector type!");
7778
7779 SDValue Result;
7780 SDNode *Node = Op.getNode();
7781 MVT EVT = VT.getVectorElementType();
7782
7783 unsigned NumElts = VT.getVectorNumElements();
7784 unsigned NewNumElts = WidenVT.getVectorNumElements();
7785 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7786 assert(NewNumElts < 17);
7787
7788 // When widen is called, it is assumed that it is more efficient to use a
7789 // wide type. The default action is to widen to operation to a wider legal
7790 // vector type and then do the operation if it is legal by calling LegalizeOp
7791 // again. If there is no vector equivalent, we will unroll the operation, do
7792 // it, and rebuild the vector. If most of the operations are vectorizible to
7793 // the legal type, the resulting code will be more efficient. If this is not
7794 // the case, the resulting code will preform badly as we end up generating
7795 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007796 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007797 switch (Node->getOpcode()) {
7798 default:
7799#ifndef NDEBUG
7800 Node->dump(&DAG);
7801#endif
7802 assert(0 && "Unexpected operation in WidenVectorOp!");
7803 break;
7804 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007805 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007806 case ISD::Constant:
7807 case ISD::ConstantFP:
7808 // To build a vector of these elements, clients should call BuildVector
7809 // and with each element instead of creating a node with a vector type
7810 assert(0 && "Unexpected operation in WidenVectorOp!");
7811 case ISD::VAARG:
7812 // Variable Arguments with vector types doesn't make any sense to me
7813 assert(0 && "Unexpected operation in WidenVectorOp!");
7814 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007815 case ISD::UNDEF:
7816 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7817 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007818 case ISD::BUILD_VECTOR: {
7819 // Build a vector with undefined for the new nodes
7820 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7821 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7822 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7823 }
7824 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7825 break;
7826 }
7827 case ISD::INSERT_VECTOR_ELT: {
7828 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7829 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7830 Node->getOperand(1), Node->getOperand(2));
7831 break;
7832 }
7833 case ISD::VECTOR_SHUFFLE: {
7834 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7835 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7836 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7837 // used as permutation array. We build the vector here instead of widening
7838 // because we don't want to legalize and have it turned to something else.
7839 SDValue PermOp = Node->getOperand(2);
7840 SDValueVector NewOps;
7841 MVT PVT = PermOp.getValueType().getVectorElementType();
7842 for (unsigned i = 0; i < NumElts; ++i) {
7843 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7844 NewOps.push_back(PermOp.getOperand(i));
7845 } else {
7846 unsigned Idx =
7847 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7848 if (Idx < NumElts) {
7849 NewOps.push_back(PermOp.getOperand(i));
7850 }
7851 else {
7852 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7853 PermOp.getOperand(i).getValueType()));
7854 }
7855 }
7856 }
7857 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7858 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7859 }
7860
7861 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7862 MVT::getVectorVT(PVT, NewOps.size()),
7863 &NewOps[0], NewOps.size());
7864
7865 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7866 break;
7867 }
7868 case ISD::LOAD: {
7869 // If the load widen returns true, we can use a single load for the
7870 // vector. Otherwise, it is returning a token factor for multiple
7871 // loads.
7872 SDValue TFOp;
7873 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7874 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7875 else
7876 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7877 break;
7878 }
7879
7880 case ISD::BIT_CONVERT: {
7881 SDValue Tmp1 = Node->getOperand(0);
7882 // Converts between two different types so we need to determine
7883 // the correct widen type for the input operand.
7884 MVT TVT = Tmp1.getValueType();
7885 assert(TVT.isVector() && "can not widen non vector type");
7886 MVT TEVT = TVT.getVectorElementType();
7887 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7888 "can not widen bit bit convert that are not multiple of element type");
7889 MVT TWidenVT = MVT::getVectorVT(TEVT,
7890 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7891 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7892 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7893 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7894
7895 TargetLowering::LegalizeAction action =
7896 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7897 switch (action) {
7898 default: assert(0 && "action not supported");
7899 case TargetLowering::Legal:
7900 break;
7901 case TargetLowering::Promote:
7902 // We defer the promotion to when we legalize the op
7903 break;
7904 case TargetLowering::Expand:
7905 // Expand the operation into a bunch of nasty scalar code.
7906 Result = LegalizeOp(UnrollVectorOp(Result));
7907 break;
7908 }
7909 break;
7910 }
7911
7912 case ISD::SINT_TO_FP:
7913 case ISD::UINT_TO_FP:
7914 case ISD::FP_TO_SINT:
7915 case ISD::FP_TO_UINT: {
7916 SDValue Tmp1 = Node->getOperand(0);
7917 // Converts between two different types so we need to determine
7918 // the correct widen type for the input operand.
7919 MVT TVT = Tmp1.getValueType();
7920 assert(TVT.isVector() && "can not widen non vector type");
7921 MVT TEVT = TVT.getVectorElementType();
7922 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7923 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7924 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7925 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7926
7927 TargetLowering::LegalizeAction action =
7928 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7929 switch (action) {
7930 default: assert(0 && "action not supported");
7931 case TargetLowering::Legal:
7932 break;
7933 case TargetLowering::Promote:
7934 // We defer the promotion to when we legalize the op
7935 break;
7936 case TargetLowering::Expand:
7937 // Expand the operation into a bunch of nasty scalar code.
7938 Result = LegalizeOp(UnrollVectorOp(Result));
7939 break;
7940 }
7941 break;
7942 }
7943
7944 case ISD::FP_EXTEND:
7945 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7946 case ISD::TRUNCATE:
7947 case ISD::SIGN_EXTEND:
7948 case ISD::ZERO_EXTEND:
7949 case ISD::ANY_EXTEND:
7950 case ISD::FP_ROUND:
7951 case ISD::SIGN_EXTEND_INREG:
7952 case ISD::FABS:
7953 case ISD::FNEG:
7954 case ISD::FSQRT:
7955 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00007956 case ISD::FCOS:
7957 case ISD::CTPOP:
7958 case ISD::CTTZ:
7959 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00007960 // Unary op widening
7961 SDValue Tmp1;
7962 TargetLowering::LegalizeAction action =
7963 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7964
7965 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7966 assert(Tmp1.getValueType() == WidenVT);
7967 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7968 switch (action) {
7969 default: assert(0 && "action not supported");
7970 case TargetLowering::Legal:
7971 break;
7972 case TargetLowering::Promote:
7973 // We defer the promotion to when we legalize the op
7974 break;
7975 case TargetLowering::Expand:
7976 // Expand the operation into a bunch of nasty scalar code.
7977 Result = LegalizeOp(UnrollVectorOp(Result));
7978 break;
7979 }
7980 break;
7981 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007982 case ISD::CONVERT_RNDSAT: {
7983 SDValue RndOp = Node->getOperand(3);
7984 SDValue SatOp = Node->getOperand(4);
7985
7986 TargetLowering::LegalizeAction action =
7987 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7988
7989 SDValue SrcOp = Node->getOperand(0);
7990
7991 // Converts between two different types so we need to determine
7992 // the correct widen type for the input operand.
7993 MVT SVT = SrcOp.getValueType();
7994 assert(SVT.isVector() && "can not widen non vector type");
7995 MVT SEVT = SVT.getVectorElementType();
7996 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
7997
7998 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
7999 assert(SrcOp.getValueType() == WidenVT);
8000 SDValue DTyOp = DAG.getValueType(WidenVT);
8001 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8002 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8003
8004 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8005 RndOp, SatOp, CvtCode);
8006 switch (action) {
8007 default: assert(0 && "action not supported");
8008 case TargetLowering::Legal:
8009 break;
8010 case TargetLowering::Promote:
8011 // We defer the promotion to when we legalize the op
8012 break;
8013 case TargetLowering::Expand:
8014 // Expand the operation into a bunch of nasty scalar code.
8015 Result = LegalizeOp(UnrollVectorOp(Result));
8016 break;
8017 }
8018 break;
8019 }
Mon P Wang0c397192008-10-30 08:01:45 +00008020 case ISD::FPOW:
8021 case ISD::FPOWI:
8022 case ISD::ADD:
8023 case ISD::SUB:
8024 case ISD::MUL:
8025 case ISD::MULHS:
8026 case ISD::MULHU:
8027 case ISD::AND:
8028 case ISD::OR:
8029 case ISD::XOR:
8030 case ISD::FADD:
8031 case ISD::FSUB:
8032 case ISD::FMUL:
8033 case ISD::SDIV:
8034 case ISD::SREM:
8035 case ISD::FDIV:
8036 case ISD::FREM:
8037 case ISD::FCOPYSIGN:
8038 case ISD::UDIV:
8039 case ISD::UREM:
8040 case ISD::BSWAP: {
8041 // Binary op widening
8042 TargetLowering::LegalizeAction action =
8043 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8044
8045 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8046 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8047 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8048 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
8049 switch (action) {
8050 default: assert(0 && "action not supported");
8051 case TargetLowering::Legal:
8052 break;
8053 case TargetLowering::Promote:
8054 // We defer the promotion to when we legalize the op
8055 break;
8056 case TargetLowering::Expand:
8057 // Expand the operation into a bunch of nasty scalar code by first
8058 // Widening to the right type and then unroll the beast.
8059 Result = LegalizeOp(UnrollVectorOp(Result));
8060 break;
8061 }
8062 break;
8063 }
8064
8065 case ISD::SHL:
8066 case ISD::SRA:
8067 case ISD::SRL: {
8068 // Binary op with one non vector operand
8069 TargetLowering::LegalizeAction action =
8070 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8071
8072 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8073 assert(Tmp1.getValueType() == WidenVT);
8074 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Node->getOperand(1));
8075 switch (action) {
8076 default: assert(0 && "action not supported");
8077 case TargetLowering::Legal:
8078 break;
8079 case TargetLowering::Promote:
8080 // We defer the promotion to when we legalize the op
8081 break;
8082 case TargetLowering::Expand:
8083 // Expand the operation into a bunch of nasty scalar code.
8084 Result = LegalizeOp(UnrollVectorOp(Result));
8085 break;
8086 }
8087 break;
8088 }
8089 case ISD::EXTRACT_VECTOR_ELT: {
8090 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8091 assert(Tmp1.getValueType() == WidenVT);
8092 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8093 break;
8094 }
8095 case ISD::CONCAT_VECTORS: {
8096 // We concurrently support only widen on a multiple of the incoming vector.
8097 // We could widen on a multiple of the incoming operand if necessary.
8098 unsigned NumConcat = NewNumElts / NumElts;
8099 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
8100 std::vector<SDValue> UnOps(NumElts, DAG.getNode(ISD::UNDEF,
8101 VT.getVectorElementType()));
8102 SDValue UndefVal = DAG.getNode(ISD::BUILD_VECTOR, VT,
8103 &UnOps[0], UnOps.size());
8104 SmallVector<SDValue, 8> MOps;
8105 MOps.push_back(Op);
8106 for (unsigned i = 1; i != NumConcat; ++i) {
8107 MOps.push_back(UndefVal);
8108 }
8109 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8110 &MOps[0], MOps.size()));
8111 break;
8112 }
8113 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008114 SDValue Tmp1 = Node->getOperand(0);
8115 SDValue Idx = Node->getOperand(1);
8116 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8117 if (CIdx && CIdx->getZExtValue() == 0) {
8118 // Since we are access the start of the vector, the incoming
8119 // vector type might be the proper.
8120 MVT Tmp1VT = Tmp1.getValueType();
8121 if (Tmp1VT == WidenVT)
8122 return Tmp1;
8123 else {
8124 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8125 if (Tmp1VTNumElts < NewNumElts)
8126 Result = WidenVectorOp(Tmp1, WidenVT);
8127 else
8128 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8129 }
8130 } else if (NewNumElts % NumElts == 0) {
8131 // Widen the extracted subvector.
8132 unsigned NumConcat = NewNumElts / NumElts;
8133 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8134 SmallVector<SDValue, 8> MOps;
8135 MOps.push_back(Op);
8136 for (unsigned i = 1; i != NumConcat; ++i) {
8137 MOps.push_back(UndefVal);
8138 }
8139 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8140 &MOps[0], MOps.size()));
8141 } else {
8142 assert(0 && "can not widen extract subvector");
8143 // This could be implemented using insert and build vector but I would
8144 // like to see when this happens.
8145 }
Mon P Wang0c397192008-10-30 08:01:45 +00008146 break;
8147 }
8148
8149 case ISD::SELECT: {
8150 TargetLowering::LegalizeAction action =
8151 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8152
8153 // Determine new condition widen type and widen
8154 SDValue Cond1 = Node->getOperand(0);
8155 MVT CondVT = Cond1.getValueType();
8156 assert(CondVT.isVector() && "can not widen non vector type");
8157 MVT CondEVT = CondVT.getVectorElementType();
8158 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8159 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8160 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8161
8162 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8163 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8164 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8165 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
8166 switch (action) {
8167 default: assert(0 && "action not supported");
8168 case TargetLowering::Legal:
8169 break;
8170 case TargetLowering::Promote:
8171 // We defer the promotion to when we legalize the op
8172 break;
8173 case TargetLowering::Expand:
8174 // Expand the operation into a bunch of nasty scalar code by first
8175 // Widening to the right type and then unroll the beast.
8176 Result = LegalizeOp(UnrollVectorOp(Result));
8177 break;
8178 }
8179 break;
8180 }
8181
8182 case ISD::SELECT_CC: {
8183 TargetLowering::LegalizeAction action =
8184 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8185
8186 // Determine new condition widen type and widen
8187 SDValue Cond1 = Node->getOperand(0);
8188 SDValue Cond2 = Node->getOperand(1);
8189 MVT CondVT = Cond1.getValueType();
8190 assert(CondVT.isVector() && "can not widen non vector type");
8191 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8192 MVT CondEVT = CondVT.getVectorElementType();
8193 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8194 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8195 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8196 assert(Cond1.getValueType() == CondWidenVT &&
8197 Cond2.getValueType() == CondWidenVT && "condition not widen");
8198
8199 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8200 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8201 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8202 "operands not widen");
8203 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8204 Tmp2, Node->getOperand(4));
8205 switch (action) {
8206 default: assert(0 && "action not supported");
8207 case TargetLowering::Legal:
8208 break;
8209 case TargetLowering::Promote:
8210 // We defer the promotion to when we legalize the op
8211 break;
8212 case TargetLowering::Expand:
8213 // Expand the operation into a bunch of nasty scalar code by first
8214 // Widening to the right type and then unroll the beast.
8215 Result = LegalizeOp(UnrollVectorOp(Result));
8216 break;
8217 }
8218 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008219 }
8220 case ISD::VSETCC: {
8221 // Determine widen for the operand
8222 SDValue Tmp1 = Node->getOperand(0);
8223 MVT TmpVT = Tmp1.getValueType();
8224 assert(TmpVT.isVector() && "can not widen non vector type");
8225 MVT TmpEVT = TmpVT.getVectorElementType();
8226 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8227 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8228 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8229 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8230 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008231 break;
8232 }
Mon P Wang0c397192008-10-30 08:01:45 +00008233 case ISD::ATOMIC_CMP_SWAP_8:
8234 case ISD::ATOMIC_CMP_SWAP_16:
8235 case ISD::ATOMIC_CMP_SWAP_32:
8236 case ISD::ATOMIC_CMP_SWAP_64:
8237 case ISD::ATOMIC_LOAD_ADD_8:
8238 case ISD::ATOMIC_LOAD_SUB_8:
8239 case ISD::ATOMIC_LOAD_AND_8:
8240 case ISD::ATOMIC_LOAD_OR_8:
8241 case ISD::ATOMIC_LOAD_XOR_8:
8242 case ISD::ATOMIC_LOAD_NAND_8:
8243 case ISD::ATOMIC_LOAD_MIN_8:
8244 case ISD::ATOMIC_LOAD_MAX_8:
8245 case ISD::ATOMIC_LOAD_UMIN_8:
8246 case ISD::ATOMIC_LOAD_UMAX_8:
8247 case ISD::ATOMIC_SWAP_8:
8248 case ISD::ATOMIC_LOAD_ADD_16:
8249 case ISD::ATOMIC_LOAD_SUB_16:
8250 case ISD::ATOMIC_LOAD_AND_16:
8251 case ISD::ATOMIC_LOAD_OR_16:
8252 case ISD::ATOMIC_LOAD_XOR_16:
8253 case ISD::ATOMIC_LOAD_NAND_16:
8254 case ISD::ATOMIC_LOAD_MIN_16:
8255 case ISD::ATOMIC_LOAD_MAX_16:
8256 case ISD::ATOMIC_LOAD_UMIN_16:
8257 case ISD::ATOMIC_LOAD_UMAX_16:
8258 case ISD::ATOMIC_SWAP_16:
8259 case ISD::ATOMIC_LOAD_ADD_32:
8260 case ISD::ATOMIC_LOAD_SUB_32:
8261 case ISD::ATOMIC_LOAD_AND_32:
8262 case ISD::ATOMIC_LOAD_OR_32:
8263 case ISD::ATOMIC_LOAD_XOR_32:
8264 case ISD::ATOMIC_LOAD_NAND_32:
8265 case ISD::ATOMIC_LOAD_MIN_32:
8266 case ISD::ATOMIC_LOAD_MAX_32:
8267 case ISD::ATOMIC_LOAD_UMIN_32:
8268 case ISD::ATOMIC_LOAD_UMAX_32:
8269 case ISD::ATOMIC_SWAP_32:
8270 case ISD::ATOMIC_LOAD_ADD_64:
8271 case ISD::ATOMIC_LOAD_SUB_64:
8272 case ISD::ATOMIC_LOAD_AND_64:
8273 case ISD::ATOMIC_LOAD_OR_64:
8274 case ISD::ATOMIC_LOAD_XOR_64:
8275 case ISD::ATOMIC_LOAD_NAND_64:
8276 case ISD::ATOMIC_LOAD_MIN_64:
8277 case ISD::ATOMIC_LOAD_MAX_64:
8278 case ISD::ATOMIC_LOAD_UMIN_64:
8279 case ISD::ATOMIC_LOAD_UMAX_64:
8280 case ISD::ATOMIC_SWAP_64: {
8281 // For now, we assume that using vectors for these operations don't make
8282 // much sense so we just split it. We return an empty result
8283 SDValue X, Y;
8284 SplitVectorOp(Op, X, Y);
8285 return Result;
8286 break;
8287 }
8288
8289 } // end switch (Node->getOpcode())
8290
8291 assert(Result.getNode() && "Didn't set a result!");
8292 if (Result != Op)
8293 Result = LegalizeOp(Result);
8294
Mon P Wangf007a8b2008-11-06 05:31:54 +00008295 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008296 return Result;
8297}
8298
8299// Utility function to find a legal vector type and its associated element
8300// type from a preferred width and whose vector type must be the same size
8301// as the VVT.
8302// TLI: Target lowering used to determine legal types
8303// Width: Preferred width of element type
8304// VVT: Vector value type whose size we must match.
8305// Returns VecEVT and EVT - the vector type and its associated element type
8306static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8307 MVT& EVT, MVT& VecEVT) {
8308 // We start with the preferred width, make it a power of 2 and see if
8309 // we can find a vector type of that width. If not, we reduce it by
8310 // another power of 2. If we have widen the type, a vector of bytes should
8311 // always be legal.
8312 assert(TLI.isTypeLegal(VVT));
8313 unsigned EWidth = Width + 1;
8314 do {
8315 assert(EWidth > 0);
8316 EWidth = (1 << Log2_32(EWidth-1));
8317 EVT = MVT::getIntegerVT(EWidth);
8318 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8319 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8320 } while (!TLI.isTypeLegal(VecEVT) ||
8321 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8322}
8323
8324SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8325 SDValue Chain,
8326 SDValue BasePtr,
8327 const Value *SV,
8328 int SVOffset,
8329 unsigned Alignment,
8330 bool isVolatile,
8331 unsigned LdWidth,
8332 MVT ResType) {
8333 // We assume that we have good rules to handle loading power of two loads so
8334 // we break down the operations to power of 2 loads. The strategy is to
8335 // load the largest power of 2 that we can easily transform to a legal vector
8336 // and then insert into that vector, and the cast the result into the legal
8337 // vector that we want. This avoids unnecessary stack converts.
8338 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8339 // the load is nonvolatile, we an use a wider load for the value.
8340 // Find a vector length we can load a large chunk
8341 MVT EVT, VecEVT;
8342 unsigned EVTWidth;
8343 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8344 EVTWidth = EVT.getSizeInBits();
8345
8346 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8347 isVolatile, Alignment);
8348 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8349 LdChain.push_back(LdOp.getValue(1));
8350
8351 // Check if we can load the element with one instruction
8352 if (LdWidth == EVTWidth) {
8353 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8354 }
8355
8356 // The vector element order is endianness dependent.
8357 unsigned Idx = 1;
8358 LdWidth -= EVTWidth;
8359 unsigned Offset = 0;
8360
8361 while (LdWidth > 0) {
8362 unsigned Increment = EVTWidth / 8;
8363 Offset += Increment;
8364 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8365 DAG.getIntPtrConstant(Increment));
8366
8367 if (LdWidth < EVTWidth) {
8368 // Our current type we are using is too large, use a smaller size by
8369 // using a smaller power of 2
8370 unsigned oEVTWidth = EVTWidth;
8371 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8372 EVTWidth = EVT.getSizeInBits();
8373 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008374 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008375 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8376 }
8377
8378 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8379 SVOffset+Offset, isVolatile,
8380 MinAlign(Alignment, Offset));
8381 LdChain.push_back(LdOp.getValue(1));
8382 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8383 DAG.getIntPtrConstant(Idx++));
8384
8385 LdWidth -= EVTWidth;
8386 }
8387
8388 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8389}
8390
8391bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8392 SDValue& TFOp,
8393 SDValue Op,
8394 MVT NVT) {
8395 // TODO: Add support for ConcatVec and the ability to load many vector
8396 // types (e.g., v4i8). This will not work when a vector register
8397 // to memory mapping is strange (e.g., vector elements are not
8398 // stored in some sequential order).
8399
8400 // It must be true that the widen vector type is bigger than where
8401 // we need to load from.
8402 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8403 MVT LdVT = LD->getMemoryVT();
8404 assert(LdVT.isVector() && NVT.isVector());
8405 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8406
8407 // Load information
8408 SDValue Chain = LD->getChain();
8409 SDValue BasePtr = LD->getBasePtr();
8410 int SVOffset = LD->getSrcValueOffset();
8411 unsigned Alignment = LD->getAlignment();
8412 bool isVolatile = LD->isVolatile();
8413 const Value *SV = LD->getSrcValue();
8414 unsigned int LdWidth = LdVT.getSizeInBits();
8415
8416 // Load value as a large register
8417 SDValueVector LdChain;
8418 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8419 Alignment, isVolatile, LdWidth, NVT);
8420
8421 if (LdChain.size() == 1) {
8422 TFOp = LdChain[0];
8423 return true;
8424 }
8425 else {
8426 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8427 return false;
8428 }
8429}
8430
8431
8432void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8433 SDValue Chain,
8434 SDValue BasePtr,
8435 const Value *SV,
8436 int SVOffset,
8437 unsigned Alignment,
8438 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008439 SDValue ValOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008440 unsigned StWidth) {
8441 // Breaks the stores into a series of power of 2 width stores. For any
8442 // width, we convert the vector to the vector of element size that we
8443 // want to store. This avoids requiring a stack convert.
8444
8445 // Find a width of the element type we can store with
8446 MVT VVT = ValOp.getValueType();
8447 MVT EVT, VecEVT;
8448 unsigned EVTWidth;
8449 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8450 EVTWidth = EVT.getSizeInBits();
8451
8452 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8453 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008454 DAG.getIntPtrConstant(0));
Mon P Wang0c397192008-10-30 08:01:45 +00008455 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8456 isVolatile, Alignment);
8457 StChain.push_back(StOp);
8458
8459 // Check if we are done
8460 if (StWidth == EVTWidth) {
8461 return;
8462 }
8463
8464 unsigned Idx = 1;
8465 StWidth -= EVTWidth;
8466 unsigned Offset = 0;
8467
8468 while (StWidth > 0) {
8469 unsigned Increment = EVTWidth / 8;
8470 Offset += Increment;
8471 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8472 DAG.getIntPtrConstant(Increment));
8473
8474 if (StWidth < EVTWidth) {
8475 // Our current type we are using is too large, use a smaller size by
8476 // using a smaller power of 2
8477 unsigned oEVTWidth = EVTWidth;
8478 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8479 EVTWidth = EVT.getSizeInBits();
8480 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008481 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008482 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8483 }
8484
8485 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008486 DAG.getIntPtrConstant(Idx++));
Mon P Wang0c397192008-10-30 08:01:45 +00008487 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8488 SVOffset + Offset, isVolatile,
8489 MinAlign(Alignment, Offset)));
8490 StWidth -= EVTWidth;
8491 }
8492}
8493
8494
8495SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8496 SDValue Chain,
8497 SDValue BasePtr) {
8498 // TODO: It might be cleaner if we can use SplitVector and have more legal
8499 // vector types that can be stored into memory (e.g., v4xi8 can
8500 // be stored as a word). This will not work when a vector register
8501 // to memory mapping is strange (e.g., vector elements are not
8502 // stored in some sequential order).
8503
8504 MVT StVT = ST->getMemoryVT();
8505 SDValue ValOp = ST->getValue();
8506
8507 // Check if we have widen this node with another value
8508 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8509 if (I != WidenNodes.end())
8510 ValOp = I->second;
8511
8512 MVT VVT = ValOp.getValueType();
8513
8514 // It must be true that we the widen vector type is bigger than where
8515 // we need to store.
8516 assert(StVT.isVector() && VVT.isVector());
8517 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8518 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8519
8520 // Store value
8521 SDValueVector StChain;
8522 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8523 ST->getSrcValueOffset(), ST->getAlignment(),
8524 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8525 if (StChain.size() == 1)
8526 return StChain[0];
8527 else
8528 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8529}
8530
8531
Chris Lattner3e928bb2005-01-07 07:47:09 +00008532// SelectionDAG::Legalize - This is the entry point for the file.
8533//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00008534void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008535 /// run - This is the main entry point to this class.
8536 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00008537 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008538}
8539