blob: 6671e0f2fb3c6c527ed3a17c62e0d5df2b509936 [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);
Mon P Wange9f10152008-12-09 05:46:39 +0000300
301 // Returns the legalized (truncated or extended) shift amount.
302 SDValue LegalizeShiftAmount(SDValue ShiftAmt);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000303};
304}
305
Chris Lattner4352cc92006-04-04 17:23:26 +0000306/// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
307/// specified mask and type. Targets can specify exactly which masks they
308/// support and the code generator is tasked with not creating illegal masks.
309///
310/// Note that this will also return true for shuffles that are promoted to a
311/// different type.
Dan Gohman475871a2008-07-27 21:46:04 +0000312SDNode *SelectionDAGLegalize::isShuffleLegal(MVT VT, SDValue Mask) const {
Chris Lattner4352cc92006-04-04 17:23:26 +0000313 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE, VT)) {
314 default: return 0;
315 case TargetLowering::Legal:
316 case TargetLowering::Custom:
317 break;
318 case TargetLowering::Promote: {
319 // If this is promoted to a different type, convert the shuffle mask and
320 // ask if it is legal in the promoted type!
Duncan Sands83ec4b62008-06-06 12:08:01 +0000321 MVT NVT = TLI.getTypeToPromoteTo(ISD::VECTOR_SHUFFLE, VT);
Duncan Sandsd038e042008-07-21 10:20:31 +0000322 MVT EltVT = NVT.getVectorElementType();
Chris Lattner4352cc92006-04-04 17:23:26 +0000323
324 // If we changed # elements, change the shuffle mask.
325 unsigned NumEltsGrowth =
Duncan Sands83ec4b62008-06-06 12:08:01 +0000326 NVT.getVectorNumElements() / VT.getVectorNumElements();
Chris Lattner4352cc92006-04-04 17:23:26 +0000327 assert(NumEltsGrowth && "Cannot promote to vector type with fewer elts!");
328 if (NumEltsGrowth > 1) {
329 // Renumber the elements.
Dan Gohman475871a2008-07-27 21:46:04 +0000330 SmallVector<SDValue, 8> Ops;
Chris Lattner4352cc92006-04-04 17:23:26 +0000331 for (unsigned i = 0, e = Mask.getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +0000332 SDValue InOp = Mask.getOperand(i);
Chris Lattner4352cc92006-04-04 17:23:26 +0000333 for (unsigned j = 0; j != NumEltsGrowth; ++j) {
334 if (InOp.getOpcode() == ISD::UNDEF)
Duncan Sandsd038e042008-07-21 10:20:31 +0000335 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000336 else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +0000337 unsigned InEltNo = cast<ConstantSDNode>(InOp)->getZExtValue();
Duncan Sandsd038e042008-07-21 10:20:31 +0000338 Ops.push_back(DAG.getConstant(InEltNo*NumEltsGrowth+j, EltVT));
Chris Lattner4352cc92006-04-04 17:23:26 +0000339 }
340 }
341 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000342 Mask = DAG.getNode(ISD::BUILD_VECTOR, NVT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +0000343 }
344 VT = NVT;
345 break;
346 }
347 }
Gabor Greifba36cb52008-08-28 21:40:38 +0000348 return TLI.isShuffleMaskLegal(Mask, VT) ? Mask.getNode() : 0;
Chris Lattner4352cc92006-04-04 17:23:26 +0000349}
350
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000351SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
352 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
353 ValueTypeActions(TLI.getValueTypeActions()) {
Nate Begeman6a648612005-11-29 05:45:29 +0000354 assert(MVT::LAST_VALUETYPE <= 32 &&
Chris Lattner3e928bb2005-01-07 07:47:09 +0000355 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000356}
357
Chris Lattner3e928bb2005-01-07 07:47:09 +0000358void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner6831a812006-02-13 09:18:02 +0000359 LastCALLSEQ_END = DAG.getEntryNode();
360 IsLegalizingCall = false;
361
Chris Lattnerab510a72005-10-02 17:49:46 +0000362 // The legalize process is inherently a bottom-up recursive process (users
363 // legalize their uses before themselves). Given infinite stack space, we
364 // could just start legalizing on the root and traverse the whole graph. In
365 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000366 // blocks. To avoid this problem, compute an ordering of the nodes where each
367 // node is only legalized after all of its operands are legalized.
Dan Gohmanf06c8352008-09-30 18:30:35 +0000368 DAG.AssignTopologicalOrder();
369 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
370 E = prior(DAG.allnodes_end()); I != next(E); ++I)
371 HandleOp(SDValue(I, 0));
Chris Lattner32fca002005-10-06 01:20:27 +0000372
373 // Finally, it's possible the root changed. Get the new root.
Dan Gohman475871a2008-07-27 21:46:04 +0000374 SDValue OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000375 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
376 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000377
378 ExpandedNodes.clear();
379 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000380 PromotedNodes.clear();
Chris Lattnerc7029802006-03-18 01:44:44 +0000381 SplitNodes.clear();
Dan Gohman7f321562007-06-25 16:23:39 +0000382 ScalarizedNodes.clear();
Mon P Wang0c397192008-10-30 08:01:45 +0000383 WidenNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000384
385 // Remove dead nodes now.
Chris Lattner190a4182006-08-04 17:45:20 +0000386 DAG.RemoveDeadNodes();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000387}
388
Chris Lattner6831a812006-02-13 09:18:02 +0000389
390/// FindCallEndFromCallStart - Given a chained node that is part of a call
391/// sequence, find the CALLSEQ_END node that terminates the call sequence.
392static SDNode *FindCallEndFromCallStart(SDNode *Node) {
393 if (Node->getOpcode() == ISD::CALLSEQ_END)
394 return Node;
395 if (Node->use_empty())
396 return 0; // No CallSeqEnd
397
398 // The chain is usually at the end.
Dan Gohman475871a2008-07-27 21:46:04 +0000399 SDValue TheChain(Node, Node->getNumValues()-1);
Chris Lattner6831a812006-02-13 09:18:02 +0000400 if (TheChain.getValueType() != MVT::Other) {
401 // Sometimes it's at the beginning.
Dan Gohman475871a2008-07-27 21:46:04 +0000402 TheChain = SDValue(Node, 0);
Chris Lattner6831a812006-02-13 09:18:02 +0000403 if (TheChain.getValueType() != MVT::Other) {
404 // Otherwise, hunt for it.
405 for (unsigned i = 1, e = Node->getNumValues(); i != e; ++i)
406 if (Node->getValueType(i) == MVT::Other) {
Dan Gohman475871a2008-07-27 21:46:04 +0000407 TheChain = SDValue(Node, i);
Chris Lattner6831a812006-02-13 09:18:02 +0000408 break;
409 }
410
411 // Otherwise, we walked into a node without a chain.
412 if (TheChain.getValueType() != MVT::Other)
413 return 0;
414 }
415 }
416
417 for (SDNode::use_iterator UI = Node->use_begin(),
418 E = Node->use_end(); UI != E; ++UI) {
419
420 // Make sure to only follow users of our token chain.
Dan Gohman89684502008-07-27 20:43:25 +0000421 SDNode *User = *UI;
Chris Lattner6831a812006-02-13 09:18:02 +0000422 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
423 if (User->getOperand(i) == TheChain)
424 if (SDNode *Result = FindCallEndFromCallStart(User))
425 return Result;
426 }
427 return 0;
428}
429
430/// FindCallStartFromCallEnd - Given a chained node that is part of a call
431/// sequence, find the CALLSEQ_START node that initiates the call sequence.
432static SDNode *FindCallStartFromCallEnd(SDNode *Node) {
433 assert(Node && "Didn't find callseq_start for a call??");
434 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
435
436 assert(Node->getOperand(0).getValueType() == MVT::Other &&
437 "Node doesn't have a token chain argument!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000438 return FindCallStartFromCallEnd(Node->getOperand(0).getNode());
Chris Lattner6831a812006-02-13 09:18:02 +0000439}
440
441/// LegalizeAllNodesNotLeadingTo - Recursively walk the uses of N, looking to
442/// see if any uses can reach Dest. If no dest operands can get to dest,
443/// legalize them, legalize ourself, and return false, otherwise, return true.
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000444///
445/// Keep track of the nodes we fine that actually do lead to Dest in
446/// NodesLeadingTo. This avoids retraversing them exponential number of times.
447///
448bool SelectionDAGLegalize::LegalizeAllNodesNotLeadingTo(SDNode *N, SDNode *Dest,
Chris Lattner00755df2007-02-04 00:27:56 +0000449 SmallPtrSet<SDNode*, 32> &NodesLeadingTo) {
Chris Lattner6831a812006-02-13 09:18:02 +0000450 if (N == Dest) return true; // N certainly leads to Dest :)
451
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000452 // If we've already processed this node and it does lead to Dest, there is no
453 // need to reprocess it.
454 if (NodesLeadingTo.count(N)) return true;
455
Chris Lattner6831a812006-02-13 09:18:02 +0000456 // If the first result of this node has been already legalized, then it cannot
457 // reach N.
458 switch (getTypeAction(N->getValueType(0))) {
459 case Legal:
Dan Gohman475871a2008-07-27 21:46:04 +0000460 if (LegalizedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000461 break;
462 case Promote:
Dan Gohman475871a2008-07-27 21:46:04 +0000463 if (PromotedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000464 break;
465 case Expand:
Dan Gohman475871a2008-07-27 21:46:04 +0000466 if (ExpandedNodes.count(SDValue(N, 0))) return false;
Chris Lattner6831a812006-02-13 09:18:02 +0000467 break;
468 }
469
470 // Okay, this node has not already been legalized. Check and legalize all
471 // operands. If none lead to Dest, then we can legalize this node.
472 bool OperandsLeadToDest = false;
473 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
474 OperandsLeadToDest |= // If an operand leads to Dest, so do we.
Gabor Greifba36cb52008-08-28 21:40:38 +0000475 LegalizeAllNodesNotLeadingTo(N->getOperand(i).getNode(), Dest, NodesLeadingTo);
Chris Lattner6831a812006-02-13 09:18:02 +0000476
Chris Lattnerc9cf4f12006-07-26 23:55:56 +0000477 if (OperandsLeadToDest) {
478 NodesLeadingTo.insert(N);
479 return true;
480 }
Chris Lattner6831a812006-02-13 09:18:02 +0000481
482 // Okay, this node looks safe, legalize it and return false.
Dan Gohman475871a2008-07-27 21:46:04 +0000483 HandleOp(SDValue(N, 0));
Chris Lattner6831a812006-02-13 09:18:02 +0000484 return false;
485}
486
Mon P Wang0c397192008-10-30 08:01:45 +0000487/// HandleOp - Legalize, Promote, Widen, or Expand the specified operand as
Chris Lattnerc7029802006-03-18 01:44:44 +0000488/// appropriate for its type.
Dan Gohman475871a2008-07-27 21:46:04 +0000489void SelectionDAGLegalize::HandleOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000490 MVT VT = Op.getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000491 switch (getTypeAction(VT)) {
Chris Lattnerc7029802006-03-18 01:44:44 +0000492 default: assert(0 && "Bad type action!");
Dan Gohman7f321562007-06-25 16:23:39 +0000493 case Legal: (void)LegalizeOp(Op); break;
Mon P Wang0c397192008-10-30 08:01:45 +0000494 case Promote:
495 if (!VT.isVector()) {
496 (void)PromoteOp(Op);
497 break;
498 }
499 else {
500 // See if we can widen otherwise use Expand to either scalarize or split
501 MVT WidenVT = TLI.getWidenVectorType(VT);
502 if (WidenVT != MVT::Other) {
503 (void) WidenVectorOp(Op, WidenVT);
504 break;
505 }
506 // else fall thru to expand since we can't widen the vector
507 }
Chris Lattnerc7029802006-03-18 01:44:44 +0000508 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +0000509 if (!VT.isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +0000510 // If this is an illegal scalar, expand it into its two component
511 // pieces.
Dan Gohman475871a2008-07-27 21:46:04 +0000512 SDValue X, Y;
Chris Lattner09ec1b02007-08-25 01:00:22 +0000513 if (Op.getOpcode() == ISD::TargetConstant)
514 break; // Allow illegal target nodes.
Chris Lattnerc7029802006-03-18 01:44:44 +0000515 ExpandOp(Op, X, Y);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000516 } else if (VT.getVectorNumElements() == 1) {
Dan Gohman7f321562007-06-25 16:23:39 +0000517 // If this is an illegal single element vector, convert it to a
518 // scalar operation.
519 (void)ScalarizeVectorOp(Op);
Chris Lattnerc7029802006-03-18 01:44:44 +0000520 } else {
Mon P Wang0c397192008-10-30 08:01:45 +0000521 // This is an illegal multiple element vector.
Dan Gohman7f321562007-06-25 16:23:39 +0000522 // Split it in half and legalize both parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000523 SDValue X, Y;
Dan Gohman7f321562007-06-25 16:23:39 +0000524 SplitVectorOp(Op, X, Y);
Chris Lattnerc7029802006-03-18 01:44:44 +0000525 }
526 break;
527 }
528}
Chris Lattner6831a812006-02-13 09:18:02 +0000529
Evan Cheng9f877882006-12-13 20:57:08 +0000530/// ExpandConstantFP - Expands the ConstantFP node to an integer constant or
531/// a load from the constant pool.
Dan Gohman475871a2008-07-27 21:46:04 +0000532static SDValue ExpandConstantFP(ConstantFPSDNode *CFP, bool UseCP,
Evan Cheng279101e2006-12-12 22:19:28 +0000533 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Cheng00495212006-12-12 21:32:44 +0000534 bool Extend = false;
535
536 // If a FP immediate is precise when represented as a float and if the
537 // target can do an extending load from float to double, we put it into
538 // the constant pool as a float, even if it's is statically typed as a
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000539 // double. This shrinks FP constants and canonicalizes them for targets where
540 // an FP extending load is the same cost as a normal load (such as on the x87
541 // fp stack or PPC FP unit).
Duncan Sands83ec4b62008-06-06 12:08:01 +0000542 MVT VT = CFP->getValueType(0);
Dan Gohman4fbd7962008-09-12 18:08:03 +0000543 ConstantFP *LLVMC = const_cast<ConstantFP*>(CFP->getConstantFPValue());
Evan Cheng9f877882006-12-13 20:57:08 +0000544 if (!UseCP) {
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000545 if (VT!=MVT::f64 && VT!=MVT::f32)
546 assert(0 && "Invalid type expansion");
Dale Johannesen7111b022008-10-09 18:53:47 +0000547 return DAG.getConstant(LLVMC->getValueAPF().bitcastToAPInt(),
Evan Chengef120572008-03-04 08:05:30 +0000548 (VT == MVT::f64) ? MVT::i64 : MVT::i32);
Evan Cheng279101e2006-12-12 22:19:28 +0000549 }
550
Duncan Sands83ec4b62008-06-06 12:08:01 +0000551 MVT OrigVT = VT;
552 MVT SVT = VT;
Evan Chengef120572008-03-04 08:05:30 +0000553 while (SVT != MVT::f32) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000554 SVT = (MVT::SimpleValueType)(SVT.getSimpleVT() - 1);
Evan Chengef120572008-03-04 08:05:30 +0000555 if (CFP->isValueValidForType(SVT, CFP->getValueAPF()) &&
556 // Only do this if the target has a native EXTLOAD instruction from
557 // smaller type.
Evan Cheng03294662008-10-14 21:26:46 +0000558 TLI.isLoadExtLegal(ISD::EXTLOAD, SVT) &&
Chris Lattneraa2acbb2008-03-05 06:46:58 +0000559 TLI.ShouldShrinkFPConstant(OrigVT)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000560 const Type *SType = SVT.getTypeForMVT();
Evan Chengef120572008-03-04 08:05:30 +0000561 LLVMC = cast<ConstantFP>(ConstantExpr::getFPTrunc(LLVMC, SType));
562 VT = SVT;
563 Extend = true;
564 }
Evan Cheng00495212006-12-12 21:32:44 +0000565 }
566
Dan Gohman475871a2008-07-27 21:46:04 +0000567 SDValue CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +0000568 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Evan Chengef120572008-03-04 08:05:30 +0000569 if (Extend)
570 return DAG.getExtLoad(ISD::EXTLOAD, OrigVT, DAG.getEntryNode(),
Dan Gohman3069b872008-02-07 18:41:25 +0000571 CPIdx, PseudoSourceValue::getConstantPool(),
Dan Gohman50284d82008-09-16 22:05:41 +0000572 0, VT, false, Alignment);
Evan Chengef120572008-03-04 08:05:30 +0000573 return DAG.getLoad(OrigVT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +0000574 PseudoSourceValue::getConstantPool(), 0, false, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +0000575}
576
Chris Lattner6831a812006-02-13 09:18:02 +0000577
Evan Cheng912095b2007-01-04 21:56:39 +0000578/// ExpandFCOPYSIGNToBitwiseOps - Expands fcopysign to a series of bitwise
579/// operations.
580static
Dan Gohman475871a2008-07-27 21:46:04 +0000581SDValue ExpandFCOPYSIGNToBitwiseOps(SDNode *Node, MVT NVT,
582 SelectionDAG &DAG, TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000583 MVT VT = Node->getValueType(0);
584 MVT SrcVT = Node->getOperand(1).getValueType();
Dan Gohman7f321562007-06-25 16:23:39 +0000585 assert((SrcVT == MVT::f32 || SrcVT == MVT::f64) &&
586 "fcopysign expansion only supported for f32 and f64");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000587 MVT SrcNVT = (SrcVT == MVT::f64) ? MVT::i64 : MVT::i32;
Evan Cheng068c5f42007-01-05 21:31:51 +0000588
Evan Cheng912095b2007-01-04 21:56:39 +0000589 // First get the sign bit of second operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000590 SDValue Mask1 = (SrcVT == MVT::f64)
Evan Cheng912095b2007-01-04 21:56:39 +0000591 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), SrcVT)
592 : DAG.getConstantFP(BitsToFloat(1U << 31), SrcVT);
Evan Cheng068c5f42007-01-05 21:31:51 +0000593 Mask1 = DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Mask1);
Dan Gohman475871a2008-07-27 21:46:04 +0000594 SDValue SignBit= DAG.getNode(ISD::BIT_CONVERT, SrcNVT, Node->getOperand(1));
Evan Cheng068c5f42007-01-05 21:31:51 +0000595 SignBit = DAG.getNode(ISD::AND, SrcNVT, SignBit, Mask1);
Evan Cheng912095b2007-01-04 21:56:39 +0000596 // Shift right or sign-extend it if the two operands have different types.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000597 int SizeDiff = SrcNVT.getSizeInBits() - NVT.getSizeInBits();
Evan Cheng912095b2007-01-04 21:56:39 +0000598 if (SizeDiff > 0) {
599 SignBit = DAG.getNode(ISD::SRL, SrcNVT, SignBit,
600 DAG.getConstant(SizeDiff, TLI.getShiftAmountTy()));
601 SignBit = DAG.getNode(ISD::TRUNCATE, NVT, SignBit);
Chris Lattnerc563e1d2008-07-10 23:46:13 +0000602 } else if (SizeDiff < 0) {
603 SignBit = DAG.getNode(ISD::ZERO_EXTEND, NVT, SignBit);
604 SignBit = DAG.getNode(ISD::SHL, NVT, SignBit,
605 DAG.getConstant(-SizeDiff, TLI.getShiftAmountTy()));
606 }
Evan Cheng068c5f42007-01-05 21:31:51 +0000607
608 // Clear the sign bit of first operand.
Dan Gohman475871a2008-07-27 21:46:04 +0000609 SDValue Mask2 = (VT == MVT::f64)
Evan Cheng068c5f42007-01-05 21:31:51 +0000610 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
611 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
612 Mask2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask2);
Dan Gohman475871a2008-07-27 21:46:04 +0000613 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng068c5f42007-01-05 21:31:51 +0000614 Result = DAG.getNode(ISD::AND, NVT, Result, Mask2);
615
616 // Or the value with the sign bit.
Evan Cheng912095b2007-01-04 21:56:39 +0000617 Result = DAG.getNode(ISD::OR, NVT, Result, SignBit);
618 return Result;
619}
620
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000621/// ExpandUnalignedStore - Expands an unaligned store to 2 half-size stores.
622static
Dan Gohman475871a2008-07-27 21:46:04 +0000623SDValue ExpandUnalignedStore(StoreSDNode *ST, SelectionDAG &DAG,
624 TargetLowering &TLI) {
625 SDValue Chain = ST->getChain();
626 SDValue Ptr = ST->getBasePtr();
627 SDValue Val = ST->getValue();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000628 MVT VT = Val.getValueType();
Dale Johannesen907f28c2007-09-08 19:29:23 +0000629 int Alignment = ST->getAlignment();
630 int SVOffset = ST->getSrcValueOffset();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000631 if (ST->getMemoryVT().isFloatingPoint() ||
632 ST->getMemoryVT().isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000633 MVT intVT = MVT::getIntegerVT(VT.getSizeInBits());
634 if (TLI.isTypeLegal(intVT)) {
635 // Expand to a bitconvert of the value to the integer type of the
636 // same size, then a (misaligned) int store.
637 // FIXME: Does not handle truncating floating point stores!
638 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
639 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
640 SVOffset, ST->isVolatile(), Alignment);
641 } else {
642 // Do a (aligned) store to a stack slot, then copy from the stack slot
643 // to the final destination using (unaligned) integer loads and stores.
644 MVT StoredVT = ST->getMemoryVT();
645 MVT RegVT =
646 TLI.getRegisterType(MVT::getIntegerVT(StoredVT.getSizeInBits()));
647 unsigned StoredBytes = StoredVT.getSizeInBits() / 8;
648 unsigned RegBytes = RegVT.getSizeInBits() / 8;
649 unsigned NumRegs = (StoredBytes + RegBytes - 1) / RegBytes;
Dale Johannesen907f28c2007-09-08 19:29:23 +0000650
Duncan Sands05e11fa2008-12-12 21:47:02 +0000651 // Make sure the stack slot is wide enough that we can do NumRegs full
652 // width loads from it.
653 SDValue StackPtr = DAG.CreateStackTemporary(StoredVT,
654 MVT::getIntegerVT(NumRegs * RegBytes * 8));
655 // Perform the original store only redirected to the stack slot.
656 SDValue Store = DAG.getTruncStore(Chain, Val, StackPtr, NULL, 0,StoredVT);
657 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
658 SmallVector<SDValue, 8> Stores;
659 unsigned Offset = 0;
660
661 // Do all but one copies using the full register width.
662 for (unsigned i = 1; i < NumRegs; i++) {
663 // Load one integer register's worth from the stack slot.
664 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
665 // Store it to the final location. Remember the store.
666 Stores.push_back(DAG.getStore(Load.getValue(1), Load, Ptr,
667 ST->getSrcValue(), SVOffset + Offset,
668 ST->isVolatile(),
669 MinAlign(ST->getAlignment(), Offset)));
670 // Increment the pointers.
671 Offset += RegBytes;
672 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
673 Increment);
674 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
675 }
676
677 // Load one integer register's worth from the stack slot.
678 SDValue Load = DAG.getLoad(RegVT, Store, StackPtr, NULL, 0);
679
680 // The last store may be partial. Do a truncating store.
681 unsigned BytesLeft = StoredBytes - Offset;
682 Stores.push_back(DAG.getTruncStore(Load.getValue(1), Load, Ptr,
683 ST->getSrcValue(), SVOffset + Offset,
684 MVT::getIntegerVT(BytesLeft * 8),
685 ST->isVolatile(),
686 MinAlign(ST->getAlignment(), Offset)));
687 // The order of the stores doesn't matter - say it with a TokenFactor.
688 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
689 Stores.size());
690 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000691 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000692 assert(ST->getMemoryVT().isInteger() &&
693 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000694 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000695 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000696 MVT NewStoredVT =
697 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
698 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000699 int IncrementSize = NumBits / 8;
700
701 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000702 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
703 SDValue Lo = Val;
704 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000705
706 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000707 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000708 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
709 ST->getSrcValue(), SVOffset, NewStoredVT,
710 ST->isVolatile(), Alignment);
711 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
712 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000713 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000714 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
715 ST->getSrcValue(), SVOffset + IncrementSize,
716 NewStoredVT, ST->isVolatile(), Alignment);
717
718 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
719}
720
721/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
722static
Dan Gohman475871a2008-07-27 21:46:04 +0000723SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
724 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000725 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000726 SDValue Chain = LD->getChain();
727 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000728 MVT VT = LD->getValueType(0);
729 MVT LoadedVT = LD->getMemoryVT();
730 if (VT.isFloatingPoint() || VT.isVector()) {
Duncan Sands05e11fa2008-12-12 21:47:02 +0000731 MVT intVT = MVT::getIntegerVT(LoadedVT.getSizeInBits());
732 if (TLI.isTypeLegal(intVT)) {
733 // Expand to a (misaligned) integer load of the same size,
734 // then bitconvert to floating point or vector.
735 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
736 SVOffset, LD->isVolatile(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000737 LD->getAlignment());
Duncan Sands05e11fa2008-12-12 21:47:02 +0000738 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
739 if (VT.isFloatingPoint() && LoadedVT != VT)
740 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000741
Duncan Sands05e11fa2008-12-12 21:47:02 +0000742 SDValue Ops[] = { Result, Chain };
743 return DAG.getMergeValues(Ops, 2);
744 } else {
745 // Copy the value to a (aligned) stack slot using (unaligned) integer
746 // loads and stores, then do a (aligned) load from the stack slot.
747 MVT RegVT = TLI.getRegisterType(intVT);
748 unsigned LoadedBytes = LoadedVT.getSizeInBits() / 8;
749 unsigned RegBytes = RegVT.getSizeInBits() / 8;
750 unsigned NumRegs = (LoadedBytes + RegBytes - 1) / RegBytes;
751
752 // Make sure the stack slot wide enough that we can do NumRegs full width
753 // stores to it.
754 SDValue StackBase = DAG.CreateStackTemporary(LoadedVT,
755 MVT::getIntegerVT(NumRegs * RegBytes * 8));
756 SDValue Increment = DAG.getConstant(RegBytes, TLI.getPointerTy());
757 SmallVector<SDValue, 8> Stores;
758 SDValue StackPtr = StackBase;
759 unsigned Offset = 0;
760
761 // Do all but one copies using the full register width.
762 for (unsigned i = 1; i < NumRegs; i++) {
763 // Load one integer register's worth from the original location.
764 SDValue Load = DAG.getLoad(RegVT, Chain, Ptr, LD->getSrcValue(),
765 SVOffset + Offset, LD->isVolatile(),
766 MinAlign(LD->getAlignment(), Offset));
767 // Follow the load with a store to the stack slot. Remember the store.
768 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr,
769 NULL, 0));
770 // Increment the pointers.
771 Offset += RegBytes;
772 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr, Increment);
773 StackPtr = DAG.getNode(ISD::ADD, StackPtr.getValueType(), StackPtr,
774 Increment);
775 }
776
777 // The last copy may be partial. Do an extending load.
778 unsigned BytesLeft = LoadedBytes - Offset;
779 SDValue Load = DAG.getExtLoad(ISD::EXTLOAD, RegVT, Chain, Ptr,
780 LD->getSrcValue(), SVOffset + Offset,
781 MVT::getIntegerVT(BytesLeft * 8),
782 LD->isVolatile(),
783 MinAlign(LD->getAlignment(), Offset));
784 // Follow the load with a store to the stack slot. Remember the store.
785 Stores.push_back(DAG.getStore(Load.getValue(1), Load, StackPtr, NULL, 0));
786
787 // The order of the stores doesn't matter - say it with a TokenFactor.
788 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, &Stores[0],
789 Stores.size());
790
791 // Finally, perform the original load only redirected to the stack slot.
792 Load = DAG.getExtLoad(LD->getExtensionType(), VT, TF, StackBase,
793 NULL, 0, LoadedVT);
794
795 // Callers expect a MERGE_VALUES node.
796 SDValue Ops[] = { Load, TF };
797 return DAG.getMergeValues(Ops, 2);
798 }
Dale Johannesen907f28c2007-09-08 19:29:23 +0000799 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000800 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000801 "Unaligned load of unsupported type.");
802
Dale Johannesen8155d642008-02-27 22:36:00 +0000803 // Compute the new VT that is half the size of the old one. This is an
804 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000805 unsigned NumBits = LoadedVT.getSizeInBits();
806 MVT NewLoadedVT;
807 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000808 NumBits >>= 1;
809
810 unsigned Alignment = LD->getAlignment();
811 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000812 ISD::LoadExtType HiExtType = LD->getExtensionType();
813
814 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
815 if (HiExtType == ISD::NON_EXTLOAD)
816 HiExtType = ISD::ZEXTLOAD;
817
818 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000819 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000820 if (TLI.isLittleEndian()) {
821 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
822 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
823 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
824 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
825 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
826 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000827 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000828 } else {
829 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
830 NewLoadedVT,LD->isVolatile(), Alignment);
831 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
832 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
833 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
834 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000835 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000836 }
837
838 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000839 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
840 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000841 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
842
Dan Gohman475871a2008-07-27 21:46:04 +0000843 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000844 Hi.getValue(1));
845
Dan Gohman475871a2008-07-27 21:46:04 +0000846 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000847 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000848}
Evan Cheng912095b2007-01-04 21:56:39 +0000849
Dan Gohman82669522007-10-11 23:57:53 +0000850/// UnrollVectorOp - We know that the given vector has a legal type, however
851/// the operation it performs is not legal and is an operation that we have
852/// no way of lowering. "Unroll" the vector, splitting out the scalars and
853/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000854SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000855 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000856 assert(isTypeLegal(VT) &&
857 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000858 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000859 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000860 unsigned NE = VT.getVectorNumElements();
861 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000862
Dan Gohman475871a2008-07-27 21:46:04 +0000863 SmallVector<SDValue, 8> Scalars;
864 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000865 for (unsigned i = 0; i != NE; ++i) {
866 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000867 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000868 MVT OperandVT = Operand.getValueType();
869 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000870 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000871 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000872 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
873 OperandEltVT,
874 Operand,
875 DAG.getConstant(i, MVT::i32));
876 } else {
877 // A scalar operand; just use it as is.
878 Operands[j] = Operand;
879 }
880 }
Mon P Wange9f10152008-12-09 05:46:39 +0000881
882 switch (Op.getOpcode()) {
883 default:
884 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
885 &Operands[0], Operands.size()));
886 break;
887 case ISD::SHL:
888 case ISD::SRA:
889 case ISD::SRL:
890 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
891 LegalizeShiftAmount(Operands[1])));
892 break;
893 }
Dan Gohman82669522007-10-11 23:57:53 +0000894 }
895
896 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
897}
898
Duncan Sands007f9842008-01-10 10:28:30 +0000899/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000900static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000901 RTLIB::Libcall Call_F32,
902 RTLIB::Libcall Call_F64,
903 RTLIB::Libcall Call_F80,
904 RTLIB::Libcall Call_PPCF128) {
905 return
906 VT == MVT::f32 ? Call_F32 :
907 VT == MVT::f64 ? Call_F64 :
908 VT == MVT::f80 ? Call_F80 :
909 VT == MVT::ppcf128 ? Call_PPCF128 :
910 RTLIB::UNKNOWN_LIBCALL;
911}
912
Nate Begeman68679912008-04-25 18:07:40 +0000913/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
914/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
915/// is necessary to spill the vector being inserted into to memory, perform
916/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000917SDValue SelectionDAGLegalize::
918PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
919 SDValue Tmp1 = Vec;
920 SDValue Tmp2 = Val;
921 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000922
923 // If the target doesn't support this, we have to spill the input vector
924 // to a temporary stack slot, update the element, then reload it. This is
925 // badness. We could also load the value into a vector register (either
926 // with a "move to register" or "extload into register" instruction, then
927 // permute it into place, if the idx is a constant and if the idx is
928 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000929 MVT VT = Tmp1.getValueType();
930 MVT EltVT = VT.getVectorElementType();
931 MVT IdxVT = Tmp3.getValueType();
932 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000933 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000934
Gabor Greifba36cb52008-08-28 21:40:38 +0000935 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000936
937 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000939 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000940
941 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000942 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000943 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
944 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000945 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000946 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000947 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000948 // Store the scalar value.
949 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000950 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000951 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000952 return DAG.getLoad(VT, Ch, StackPtr,
953 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000954}
955
Mon P Wange9f10152008-12-09 05:46:39 +0000956SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
957 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
958 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
959
960 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
961 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
962
963 return ShiftAmt;
964}
965
966
Dan Gohmana3466152007-07-13 20:14:11 +0000967/// LegalizeOp - We know that the specified value has a legal type, and
968/// that its operands are legal. Now ensure that the operation itself
969/// is legal, recursively ensuring that the operands' operations remain
970/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000971SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000972 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
973 return Op;
974
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000975 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000976 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000977 SDNode *Node = Op.getNode();
Chris Lattnere3304a32005-01-08 20:35:13 +0000978
Chris Lattner3e928bb2005-01-07 07:47:09 +0000979 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000980 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000981 if (Node->getNumValues() > 1) {
982 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000983 if (getTypeAction(Node->getValueType(i)) != Legal) {
984 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000985 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000986 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000987 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000988 }
989 }
990
Chris Lattner45982da2005-05-12 16:53:42 +0000991 // Note that LegalizeOp may be reentered even from single-use nodes, which
992 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000993 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000994 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000995
Dan Gohman475871a2008-07-27 21:46:04 +0000996 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
997 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000998 bool isCustom = false;
999
Chris Lattner3e928bb2005-01-07 07:47:09 +00001000 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +00001001 case ISD::FrameIndex:
1002 case ISD::EntryToken:
1003 case ISD::Register:
1004 case ISD::BasicBlock:
1005 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +00001006 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +00001007 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +00001008 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +00001009 case ISD::TargetConstantPool:
1010 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001011 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001012 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +00001013 case ISD::VALUETYPE:
1014 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +00001015 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +00001016 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +00001017 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +00001018 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00001019 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +00001020 "This must be legal!");
1021 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001022 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001023 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
1024 // If this is a target node, legalize it by legalizing the operands then
1025 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +00001026 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001027 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001028 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001029
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001030 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001031
1032 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1033 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001034 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +00001035 }
1036 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001037#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00001038 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00001039#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00001040 assert(0 && "Do not know how to legalize this operator!");
1041 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +00001042 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001043 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00001044 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +00001045 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +00001046 case ISD::ConstantPool:
1047 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001048 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1049 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +00001050 case TargetLowering::Custom:
1051 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001052 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +00001053 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001054 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +00001055 break;
1056 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001057 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +00001058 case ISD::FRAMEADDR:
1059 case ISD::RETURNADDR:
1060 // The only option for these nodes is to custom lower them. If the target
1061 // does not custom lower them, then return zero.
1062 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001063 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +00001064 Result = Tmp1;
1065 else
1066 Result = DAG.getConstant(0, TLI.getPointerTy());
1067 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001068 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001069 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001070 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1071 default: assert(0 && "This action is not supported yet!");
1072 case TargetLowering::Custom:
1073 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001074 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001075 // Fall Thru
1076 case TargetLowering::Legal:
1077 Result = DAG.getConstant(0, VT);
1078 break;
1079 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +00001080 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +00001081 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001082 case ISD::EXCEPTIONADDR: {
1083 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001084 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001085 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1086 default: assert(0 && "This action is not supported yet!");
1087 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +00001088 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001089 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001090 }
1091 break;
1092 case TargetLowering::Custom:
1093 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001094 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001095 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001096 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001097 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001098 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001099 break;
1100 }
1101 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001102 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001103 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001104
Gabor Greifba36cb52008-08-28 21:40:38 +00001105 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001106 "Cannot return more than two values!");
1107
1108 // Since we produced two values, make sure to remember that we
1109 // legalized both of them.
1110 Tmp1 = LegalizeOp(Result);
1111 Tmp2 = LegalizeOp(Result.getValue(1));
1112 AddLegalizedOperand(Op.getValue(0), Tmp1);
1113 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001114 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001115 case ISD::EHSELECTION: {
1116 Tmp1 = LegalizeOp(Node->getOperand(0));
1117 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001118 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001119 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1120 default: assert(0 && "This action is not supported yet!");
1121 case TargetLowering::Expand: {
1122 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001123 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001124 }
1125 break;
1126 case TargetLowering::Custom:
1127 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001128 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001129 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001130 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001131 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001132 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001133 break;
1134 }
1135 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001136 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001137 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001138
Gabor Greifba36cb52008-08-28 21:40:38 +00001139 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001140 "Cannot return more than two values!");
1141
1142 // Since we produced two values, make sure to remember that we
1143 // legalized both of them.
1144 Tmp1 = LegalizeOp(Result);
1145 Tmp2 = LegalizeOp(Result.getValue(1));
1146 AddLegalizedOperand(Op.getValue(0), Tmp1);
1147 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001148 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001149 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001150 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001151 // The only "good" option for this node is to custom lower it.
1152 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1153 default: assert(0 && "This action is not supported at all!");
1154 case TargetLowering::Custom:
1155 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001156 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001157 // Fall Thru
1158 case TargetLowering::Legal:
1159 // Target does not know, how to lower this, lower to noop
1160 Result = LegalizeOp(Node->getOperand(0));
1161 break;
1162 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001163 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001164 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001165 case ISD::AssertSext:
1166 case ISD::AssertZext:
1167 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001168 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001169 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001170 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001171 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001172 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001173 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001174 case ISD::CopyFromReg:
1175 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001176 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001177 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001178 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001179 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001180 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001181 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001182 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001183 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1184 } else {
1185 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1186 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001187 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1188 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001189 // Since CopyFromReg produces two values, make sure to remember that we
1190 // legalized both of them.
1191 AddLegalizedOperand(Op.getValue(0), Result);
1192 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001193 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001194 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001195 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001196 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001197 default: assert(0 && "This action is not supported yet!");
1198 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001199 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001200 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001201 else if (VT.isFloatingPoint())
1202 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001203 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001204 else
1205 assert(0 && "Unknown value type!");
1206 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001207 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001208 break;
1209 }
1210 break;
1211 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001212
Chris Lattner48b61a72006-03-28 00:40:33 +00001213 case ISD::INTRINSIC_W_CHAIN:
1214 case ISD::INTRINSIC_WO_CHAIN:
1215 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001216 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001217 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1218 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001219 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001220
1221 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001222 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001223 TargetLowering::Custom) {
1224 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001225 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001226 }
1227
Gabor Greifba36cb52008-08-28 21:40:38 +00001228 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001229
1230 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001231 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001232 "Cannot return more than two values!");
1233
1234 // Since loads produce two values, make sure to remember that we
1235 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001236 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1237 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001238 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001239 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001240
Dan Gohman7f460202008-06-30 20:59:49 +00001241 case ISD::DBG_STOPPOINT:
1242 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001243 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1244
Dan Gohman7f460202008-06-30 20:59:49 +00001245 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001246 case TargetLowering::Promote:
1247 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001248 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001249 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001250 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohman44066042008-07-01 00:05:16 +00001251 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001252
Dan Gohman7f460202008-06-30 20:59:49 +00001253 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001254 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman7f460202008-06-30 20:59:49 +00001255 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1256 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001257
Dan Gohman7f460202008-06-30 20:59:49 +00001258 unsigned Line = DSP->getLine();
1259 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001260
1261 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001262 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001263 DAG.getConstant(Col, MVT::i32),
1264 DAG.getConstant(SrcFile, MVT::i32) };
1265 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001266 } else {
Evan Chenga647c922008-02-01 02:05:57 +00001267 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001268 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001269 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001270 } else {
1271 Result = Tmp1; // chain
1272 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001273 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001274 }
Evan Cheng71e86852008-07-08 20:06:39 +00001275 case TargetLowering::Legal: {
1276 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1277 if (Action == Legal && Tmp1 == Node->getOperand(0))
1278 break;
1279
Dan Gohman475871a2008-07-27 21:46:04 +00001280 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001281 Ops.push_back(Tmp1);
1282 if (Action == Legal) {
1283 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1284 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1285 } else {
1286 // Otherwise promote them.
1287 Ops.push_back(PromoteOp(Node->getOperand(1)));
1288 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001289 }
Evan Cheng71e86852008-07-08 20:06:39 +00001290 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1291 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1292 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001293 break;
1294 }
Evan Cheng71e86852008-07-08 20:06:39 +00001295 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001296 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001297
1298 case ISD::DECLARE:
1299 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1300 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1301 default: assert(0 && "This action is not supported yet!");
1302 case TargetLowering::Legal:
1303 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1304 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1305 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1306 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1307 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001308 case TargetLowering::Expand:
1309 Result = LegalizeOp(Node->getOperand(0));
1310 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001311 }
1312 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001313
1314 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001315 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001316 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001317 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001318 case TargetLowering::Legal: {
1319 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001320 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001321 if (Action == Legal && Tmp1 == Node->getOperand(0))
1322 break;
1323 if (Action == Legal) {
1324 Tmp2 = Node->getOperand(1);
1325 Tmp3 = Node->getOperand(2);
1326 Tmp4 = Node->getOperand(3);
1327 } else {
1328 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1329 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1330 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1331 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001332 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001333 break;
1334 }
Evan Cheng71e86852008-07-08 20:06:39 +00001335 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001336 break;
1337
Dan Gohman44066042008-07-01 00:05:16 +00001338 case ISD::DBG_LABEL:
1339 case ISD::EH_LABEL:
1340 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1341 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001342 default: assert(0 && "This action is not supported yet!");
1343 case TargetLowering::Legal:
1344 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001345 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001346 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001347 case TargetLowering::Expand:
1348 Result = LegalizeOp(Node->getOperand(0));
1349 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001350 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001351 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001352
Evan Cheng27b7db52008-03-08 00:58:38 +00001353 case ISD::PREFETCH:
1354 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1355 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1356 default: assert(0 && "This action is not supported yet!");
1357 case TargetLowering::Legal:
1358 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1359 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1360 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1361 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1362 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1363 break;
1364 case TargetLowering::Expand:
1365 // It's a noop.
1366 Result = LegalizeOp(Node->getOperand(0));
1367 break;
1368 }
1369 break;
1370
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001371 case ISD::MEMBARRIER: {
1372 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001373 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1374 default: assert(0 && "This action is not supported yet!");
1375 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001376 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001377 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001378 for (int x = 1; x < 6; ++x) {
1379 Ops[x] = Node->getOperand(x);
1380 if (!isTypeLegal(Ops[x].getValueType()))
1381 Ops[x] = PromoteOp(Ops[x]);
1382 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001383 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1384 break;
1385 }
1386 case TargetLowering::Expand:
1387 //There is no libgcc call for this op
1388 Result = Node->getOperand(0); // Noop
1389 break;
1390 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001391 break;
1392 }
1393
Dale Johannesene00a8a22008-08-28 02:44:49 +00001394 case ISD::ATOMIC_CMP_SWAP_8:
1395 case ISD::ATOMIC_CMP_SWAP_16:
1396 case ISD::ATOMIC_CMP_SWAP_32:
1397 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001398 unsigned int num_operands = 4;
1399 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001400 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001401 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001402 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001403 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1404
1405 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1406 default: assert(0 && "This action is not supported yet!");
1407 case TargetLowering::Custom:
1408 Result = TLI.LowerOperation(Result, DAG);
1409 break;
1410 case TargetLowering::Legal:
1411 break;
1412 }
Dan Gohman475871a2008-07-27 21:46:04 +00001413 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1414 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001415 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001416 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00001417 case ISD::ATOMIC_LOAD_ADD_8:
1418 case ISD::ATOMIC_LOAD_SUB_8:
1419 case ISD::ATOMIC_LOAD_AND_8:
1420 case ISD::ATOMIC_LOAD_OR_8:
1421 case ISD::ATOMIC_LOAD_XOR_8:
1422 case ISD::ATOMIC_LOAD_NAND_8:
1423 case ISD::ATOMIC_LOAD_MIN_8:
1424 case ISD::ATOMIC_LOAD_MAX_8:
1425 case ISD::ATOMIC_LOAD_UMIN_8:
1426 case ISD::ATOMIC_LOAD_UMAX_8:
1427 case ISD::ATOMIC_SWAP_8:
1428 case ISD::ATOMIC_LOAD_ADD_16:
1429 case ISD::ATOMIC_LOAD_SUB_16:
1430 case ISD::ATOMIC_LOAD_AND_16:
1431 case ISD::ATOMIC_LOAD_OR_16:
1432 case ISD::ATOMIC_LOAD_XOR_16:
1433 case ISD::ATOMIC_LOAD_NAND_16:
1434 case ISD::ATOMIC_LOAD_MIN_16:
1435 case ISD::ATOMIC_LOAD_MAX_16:
1436 case ISD::ATOMIC_LOAD_UMIN_16:
1437 case ISD::ATOMIC_LOAD_UMAX_16:
1438 case ISD::ATOMIC_SWAP_16:
1439 case ISD::ATOMIC_LOAD_ADD_32:
1440 case ISD::ATOMIC_LOAD_SUB_32:
1441 case ISD::ATOMIC_LOAD_AND_32:
1442 case ISD::ATOMIC_LOAD_OR_32:
1443 case ISD::ATOMIC_LOAD_XOR_32:
1444 case ISD::ATOMIC_LOAD_NAND_32:
1445 case ISD::ATOMIC_LOAD_MIN_32:
1446 case ISD::ATOMIC_LOAD_MAX_32:
1447 case ISD::ATOMIC_LOAD_UMIN_32:
1448 case ISD::ATOMIC_LOAD_UMAX_32:
1449 case ISD::ATOMIC_SWAP_32:
1450 case ISD::ATOMIC_LOAD_ADD_64:
1451 case ISD::ATOMIC_LOAD_SUB_64:
1452 case ISD::ATOMIC_LOAD_AND_64:
1453 case ISD::ATOMIC_LOAD_OR_64:
1454 case ISD::ATOMIC_LOAD_XOR_64:
1455 case ISD::ATOMIC_LOAD_NAND_64:
1456 case ISD::ATOMIC_LOAD_MIN_64:
1457 case ISD::ATOMIC_LOAD_MAX_64:
1458 case ISD::ATOMIC_LOAD_UMIN_64:
1459 case ISD::ATOMIC_LOAD_UMAX_64:
1460 case ISD::ATOMIC_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001461 unsigned int num_operands = 3;
1462 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001463 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001464 for (unsigned int x = 0; x < num_operands; ++x)
1465 Ops[x] = LegalizeOp(Node->getOperand(x));
1466 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001467
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001468 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001469 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001470 case TargetLowering::Custom:
1471 Result = TLI.LowerOperation(Result, DAG);
1472 break;
1473 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001474 break;
1475 }
Dan Gohman475871a2008-07-27 21:46:04 +00001476 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1477 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001478 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001479 }
Scott Michelc1513d22007-08-08 23:23:31 +00001480 case ISD::Constant: {
1481 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1482 unsigned opAction =
1483 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1484
Chris Lattner3e928bb2005-01-07 07:47:09 +00001485 // We know we don't need to expand constants here, constants only have one
1486 // value and we check that it is fine above.
1487
Scott Michelc1513d22007-08-08 23:23:31 +00001488 if (opAction == TargetLowering::Custom) {
1489 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001490 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001491 Result = Tmp1;
1492 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001493 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001494 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001495 case ISD::ConstantFP: {
1496 // Spill FP immediates to the constant pool if the target cannot directly
1497 // codegen them. Targets often have some immediate values that can be
1498 // efficiently generated into an FP register without a load. We explicitly
1499 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001500 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1501
Chris Lattner3181a772006-01-29 06:26:56 +00001502 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1503 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001504 case TargetLowering::Legal:
1505 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001506 case TargetLowering::Custom:
1507 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001508 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001509 Result = Tmp3;
1510 break;
1511 }
1512 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001513 case TargetLowering::Expand: {
1514 // Check to see if this FP immediate is already legal.
1515 bool isLegal = false;
1516 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1517 E = TLI.legal_fpimm_end(); I != E; ++I) {
1518 if (CFP->isExactlyValue(*I)) {
1519 isLegal = true;
1520 break;
1521 }
1522 }
1523 // If this is a legal constant, turn it into a TargetConstantFP node.
1524 if (isLegal)
1525 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001526 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001527 }
Nate Begemane1795842008-02-14 08:57:00 +00001528 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001529 break;
1530 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001531 case ISD::TokenFactor:
1532 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001533 Tmp1 = LegalizeOp(Node->getOperand(0));
1534 Tmp2 = LegalizeOp(Node->getOperand(1));
1535 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1536 } else if (Node->getNumOperands() == 3) {
1537 Tmp1 = LegalizeOp(Node->getOperand(0));
1538 Tmp2 = LegalizeOp(Node->getOperand(1));
1539 Tmp3 = LegalizeOp(Node->getOperand(2));
1540 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001541 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001542 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001543 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001544 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1545 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001546 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001547 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001548 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001549
1550 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001551 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001552 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001553 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001554 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001555 // A call within a calling sequence must be legalized to something
1556 // other than the normal CALLSEQ_END. Violating this gets Legalize
1557 // into an infinite loop.
1558 assert ((!IsLegalizingCall ||
1559 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001560 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001561 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001562
1563 // The number of incoming and outgoing values should match; unless the final
1564 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001565 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1566 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1567 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001568 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001569 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001570
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001571 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001572 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001573 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1574 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001575 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001576 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001577 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001578 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001579 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001580 }
1581 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001582 case ISD::EXTRACT_SUBREG: {
1583 Tmp1 = LegalizeOp(Node->getOperand(0));
1584 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1585 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001586 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001587 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1588 }
1589 break;
1590 case ISD::INSERT_SUBREG: {
1591 Tmp1 = LegalizeOp(Node->getOperand(0));
1592 Tmp2 = LegalizeOp(Node->getOperand(1));
1593 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1594 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001595 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001596 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1597 }
1598 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001599 case ISD::BUILD_VECTOR:
1600 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001601 default: assert(0 && "This action is not supported yet!");
1602 case TargetLowering::Custom:
1603 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001604 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001605 Result = Tmp3;
1606 break;
1607 }
1608 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001609 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001610 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001611 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001612 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001613 break;
1614 case ISD::INSERT_VECTOR_ELT:
1615 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001616 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001617
1618 // The type of the value to insert may not be legal, even though the vector
1619 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1620 // here.
1621 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1622 default: assert(0 && "Cannot expand insert element operand");
1623 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1624 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001625 case Expand:
1626 // FIXME: An alternative would be to check to see if the target is not
1627 // going to custom lower this operation, we could bitcast to half elt
1628 // width and perform two inserts at that width, if that is legal.
1629 Tmp2 = Node->getOperand(1);
1630 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001631 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001632 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1633
1634 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1635 Node->getValueType(0))) {
1636 default: assert(0 && "This action is not supported yet!");
1637 case TargetLowering::Legal:
1638 break;
1639 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001640 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001641 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001642 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001643 break;
1644 }
1645 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001646 case TargetLowering::Promote:
1647 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001648 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001649 // If the insert index is a constant, codegen this as a scalar_to_vector,
1650 // then a shuffle that inserts it into the right position in the vector.
1651 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001652 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1653 // match the element type of the vector being created.
1654 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001655 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001656 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001657 Tmp1.getValueType(), Tmp2);
1658
Duncan Sands83ec4b62008-06-06 12:08:01 +00001659 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1660 MVT ShufMaskVT =
1661 MVT::getIntVectorWithNumElements(NumElts);
1662 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001663
1664 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1665 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1666 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001667 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001668 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001669 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001670 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1671 else
1672 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1673 }
Dan Gohman475871a2008-07-27 21:46:04 +00001674 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001675 &ShufOps[0], ShufOps.size());
1676
1677 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1678 Tmp1, ScVec, ShufMask);
1679 Result = LegalizeOp(Result);
1680 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001681 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001682 }
Nate Begeman68679912008-04-25 18:07:40 +00001683 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001684 break;
1685 }
1686 }
1687 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001688 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001689 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1690 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1691 break;
1692 }
1693
Chris Lattnerce872152006-03-19 06:31:19 +00001694 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1695 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1696 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1697 Node->getValueType(0))) {
1698 default: assert(0 && "This action is not supported yet!");
1699 case TargetLowering::Legal:
1700 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001701 case TargetLowering::Custom:
1702 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001703 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001704 Result = Tmp3;
1705 break;
1706 }
1707 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001708 case TargetLowering::Expand:
1709 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001710 break;
1711 }
Chris Lattnerce872152006-03-19 06:31:19 +00001712 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001713 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001714 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1715 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1716 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1717
1718 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001719 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1720 default: assert(0 && "Unknown operation action!");
1721 case TargetLowering::Legal:
1722 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1723 "vector shuffle should not be created if not legal!");
1724 break;
1725 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001726 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001727 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001728 Result = Tmp3;
1729 break;
1730 }
1731 // FALLTHROUGH
1732 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001733 MVT VT = Node->getValueType(0);
1734 MVT EltVT = VT.getVectorElementType();
1735 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001736 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001737 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001738 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001739 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001740 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001741 if (Arg.getOpcode() == ISD::UNDEF) {
1742 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1743 } else {
1744 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001745 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001746 if (Idx < NumElems)
1747 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1748 DAG.getConstant(Idx, PtrVT)));
1749 else
1750 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1751 DAG.getConstant(Idx - NumElems, PtrVT)));
1752 }
1753 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001754 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001755 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001756 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001757 case TargetLowering::Promote: {
1758 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001759 MVT OVT = Node->getValueType(0);
1760 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001761
1762 // Cast the two input vectors.
1763 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1764 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1765
1766 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001767 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001768 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001769 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1770 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1771 break;
1772 }
Chris Lattner87100e02006-03-20 01:52:29 +00001773 }
1774 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001775
1776 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001777 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001778 Tmp2 = LegalizeOp(Node->getOperand(1));
1779 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001780 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001781 break;
1782
Dan Gohman7f321562007-06-25 16:23:39 +00001783 case ISD::EXTRACT_SUBVECTOR:
1784 Tmp1 = Node->getOperand(0);
1785 Tmp2 = LegalizeOp(Node->getOperand(1));
1786 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1787 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001788 break;
1789
Mon P Wang0c397192008-10-30 08:01:45 +00001790 case ISD::CONCAT_VECTORS: {
1791 // Use extract/insert/build vector for now. We might try to be
1792 // more clever later.
1793 MVT PtrVT = TLI.getPointerTy();
1794 SmallVector<SDValue, 8> Ops;
1795 unsigned NumOperands = Node->getNumOperands();
1796 for (unsigned i=0; i < NumOperands; ++i) {
1797 SDValue SubOp = Node->getOperand(i);
1798 MVT VVT = SubOp.getNode()->getValueType(0);
1799 MVT EltVT = VVT.getVectorElementType();
1800 unsigned NumSubElem = VVT.getVectorNumElements();
1801 for (unsigned j=0; j < NumSubElem; ++j) {
1802 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1803 DAG.getConstant(j, PtrVT)));
1804 }
1805 }
1806 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1807 &Ops[0], Ops.size()));
1808 }
1809
Chris Lattner6831a812006-02-13 09:18:02 +00001810 case ISD::CALLSEQ_START: {
1811 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1812
1813 // Recursively Legalize all of the inputs of the call end that do not lead
1814 // to this call start. This ensures that any libcalls that need be inserted
1815 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001816 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001817 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001818 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001819 NodesLeadingTo);
1820 }
Chris Lattner6831a812006-02-13 09:18:02 +00001821
1822 // Now that we legalized all of the inputs (which may have inserted
1823 // libcalls) create the new CALLSEQ_START node.
1824 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1825
1826 // Merge in the last call, to ensure that this call start after the last
1827 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001828 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001829 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1830 Tmp1 = LegalizeOp(Tmp1);
1831 }
Chris Lattner6831a812006-02-13 09:18:02 +00001832
1833 // Do not try to legalize the target-specific arguments (#1+).
1834 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001835 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001836 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001837 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001838 }
1839
1840 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001841 AddLegalizedOperand(Op.getValue(0), Result);
1842 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1843 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1844
Chris Lattner6831a812006-02-13 09:18:02 +00001845 // Now that the callseq_start and all of the non-call nodes above this call
1846 // sequence have been legalized, legalize the call itself. During this
1847 // process, no libcalls can/will be inserted, guaranteeing that no calls
1848 // can overlap.
1849 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001850 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001851 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001852 IsLegalizingCall = true;
1853
1854 // Legalize the call, starting from the CALLSEQ_END.
1855 LegalizeOp(LastCALLSEQ_END);
1856 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1857 return Result;
1858 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001859 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001860 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1861 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001862 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001863 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1864 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001865 assert(I != LegalizedNodes.end() &&
1866 "Legalizing the call start should have legalized this node!");
1867 return I->second;
1868 }
1869
1870 // Otherwise, the call start has been legalized and everything is going
1871 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001872 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001873 // Do not try to legalize the target-specific arguments (#1+), except for
1874 // an optional flag input.
1875 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1876 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001877 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001878 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001879 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001880 }
1881 } else {
1882 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1883 if (Tmp1 != Node->getOperand(0) ||
1884 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001885 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001886 Ops[0] = Tmp1;
1887 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001888 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001889 }
Chris Lattner6a542892006-01-24 05:48:21 +00001890 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001891 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001892 // This finishes up call legalization.
1893 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001894
1895 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001896 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001897 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001898 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001899 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001900 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001901 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001902 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1903 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1904 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001905 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001906
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001907 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001908 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001909 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001910 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001911 case TargetLowering::Expand: {
1912 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1913 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1914 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001915 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001916
1917 // Chain the dynamic stack allocation so that it doesn't modify the stack
1918 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001919 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001920
Dan Gohman475871a2008-07-27 21:46:04 +00001921 SDValue Size = Tmp2.getOperand(1);
1922 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001923 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001924 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001925 unsigned StackAlign =
1926 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1927 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001928 SP = DAG.getNode(ISD::AND, VT, SP,
1929 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001930 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001931 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1932
Chris Lattnere563bbc2008-10-11 22:08:30 +00001933 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1934 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001935
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001936 Tmp1 = LegalizeOp(Tmp1);
1937 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001938 break;
1939 }
1940 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001941 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001942 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001943 Tmp1 = LegalizeOp(Tmp3);
1944 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001945 }
Chris Lattner903d2782006-01-15 08:54:32 +00001946 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001947 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001948 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001949 }
Chris Lattner903d2782006-01-15 08:54:32 +00001950 // Since this op produce two values, make sure to remember that we
1951 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001952 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1953 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001954 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001955 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001956 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001957 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001958 bool Changed = false;
1959 // Legalize all of the operands of the inline asm, in case they are nodes
1960 // that need to be expanded or something. Note we skip the asm string and
1961 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001962 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001963 Changed = Op != Ops[0];
1964 Ops[0] = Op;
1965
1966 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1967 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001968 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001969 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001970 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001971 if (Op != Ops[i]) {
1972 Changed = true;
1973 Ops[i] = Op;
1974 }
1975 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001976 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001977
1978 if (HasInFlag) {
1979 Op = LegalizeOp(Ops.back());
1980 Changed |= Op != Ops.back();
1981 Ops.back() = Op;
1982 }
1983
1984 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001985 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001986
1987 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001988 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1989 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001990 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001991 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001992 case ISD::BR:
1993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001994 // Ensure that libcalls are emitted before a branch.
1995 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1996 Tmp1 = LegalizeOp(Tmp1);
1997 LastCALLSEQ_END = DAG.getEntryNode();
1998
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001999 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00002000 break;
Nate Begeman37efe672006-04-22 18:53:45 +00002001 case ISD::BRIND:
2002 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2003 // Ensure that libcalls are emitted before a branch.
2004 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2005 Tmp1 = LegalizeOp(Tmp1);
2006 LastCALLSEQ_END = DAG.getEntryNode();
2007
2008 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2009 default: assert(0 && "Indirect target must be legal type (pointer)!");
2010 case Legal:
2011 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2012 break;
2013 }
2014 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
2015 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002016 case ISD::BR_JT:
2017 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2018 // Ensure that libcalls are emitted before a branch.
2019 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2020 Tmp1 = LegalizeOp(Tmp1);
2021 LastCALLSEQ_END = DAG.getEntryNode();
2022
2023 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
2024 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
2025
2026 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
2027 default: assert(0 && "This action is not supported yet!");
2028 case TargetLowering::Legal: break;
2029 case TargetLowering::Custom:
2030 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002031 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00002032 break;
2033 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002034 SDValue Chain = Result.getOperand(0);
2035 SDValue Table = Result.getOperand(1);
2036 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002037
Duncan Sands83ec4b62008-06-06 12:08:01 +00002038 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002039 MachineFunction &MF = DAG.getMachineFunction();
2040 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00002041 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00002042 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002043
Duncan Sands712f7b32008-12-12 08:13:38 +00002044 MVT MemVT = MVT::getIntegerVT(EntrySize * 8);
2045 SDValue LD = DAG.getExtLoad(ISD::SEXTLOAD, PTy, Chain, Addr,
2046 PseudoSourceValue::getJumpTable(), 0, MemVT);
Evan Chengcc415862007-11-09 01:32:10 +00002047 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00002048 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00002049 // For PIC, the sequence is:
2050 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00002051 // RelocBase can be JumpTable, GOT or some sort of global base.
Evan Chengcc415862007-11-09 01:32:10 +00002052 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
2053 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00002054 }
Evan Chengcc415862007-11-09 01:32:10 +00002055 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00002056 }
2057 }
2058 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002059 case ISD::BRCOND:
2060 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002061 // Ensure that libcalls are emitted before a return.
2062 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2063 Tmp1 = LegalizeOp(Tmp1);
2064 LastCALLSEQ_END = DAG.getEntryNode();
2065
Chris Lattner47e92232005-01-18 19:27:06 +00002066 switch (getTypeAction(Node->getOperand(1).getValueType())) {
2067 case Expand: assert(0 && "It's impossible to expand bools");
2068 case Legal:
2069 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
2070 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002071 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00002072 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00002073
2074 // The top bits of the promoted condition are not necessarily zero, ensure
2075 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002076 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002077 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002078 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00002079 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002080 break;
2081 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002082 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002083
2084 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002085 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00002086
2087 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2088 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002089 case TargetLowering::Legal: break;
2090 case TargetLowering::Custom:
2091 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002092 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002093 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002094 case TargetLowering::Expand:
2095 // Expand brcond's setcc into its constituent parts and create a BR_CC
2096 // Node.
2097 if (Tmp2.getOpcode() == ISD::SETCC) {
2098 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2099 Tmp2.getOperand(0), Tmp2.getOperand(1),
2100 Node->getOperand(2));
2101 } else {
2102 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2103 DAG.getCondCode(ISD::SETNE), Tmp2,
2104 DAG.getConstant(0, Tmp2.getValueType()),
2105 Node->getOperand(2));
2106 }
2107 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002108 }
2109 break;
2110 case ISD::BR_CC:
2111 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002112 // Ensure that libcalls are emitted before a branch.
2113 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2114 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002115 Tmp2 = Node->getOperand(2); // LHS
2116 Tmp3 = Node->getOperand(3); // RHS
2117 Tmp4 = Node->getOperand(1); // CC
2118
Dale Johannesen53e4e442008-11-07 22:54:33 +00002119 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002120 LastCALLSEQ_END = DAG.getEntryNode();
2121
Evan Cheng7f042682008-10-15 02:05:31 +00002122 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002123 // the LHS is a legal SETCC itself. In this case, we need to compare
2124 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002125 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002126 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2127 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002128 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002129
2130 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2131 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002132
Chris Lattner181b7a32005-12-17 23:46:46 +00002133 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2134 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002135 case TargetLowering::Legal: break;
2136 case TargetLowering::Custom:
2137 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002138 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002139 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002140 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002141 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002142 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002143 LoadSDNode *LD = cast<LoadSDNode>(Node);
2144 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2145 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002146
Evan Cheng466685d2006-10-09 20:57:25 +00002147 ISD::LoadExtType ExtType = LD->getExtensionType();
2148 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002149 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002150 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2151 Tmp3 = Result.getValue(0);
2152 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002153
Evan Cheng466685d2006-10-09 20:57:25 +00002154 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2155 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002156 case TargetLowering::Legal:
2157 // If this is an unaligned load and the target doesn't support it,
2158 // expand it.
2159 if (!TLI.allowsUnalignedMemoryAccesses()) {
2160 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002161 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002162 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002163 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002164 TLI);
2165 Tmp3 = Result.getOperand(0);
2166 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002167 Tmp3 = LegalizeOp(Tmp3);
2168 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002169 }
2170 }
2171 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002172 case TargetLowering::Custom:
2173 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002174 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002175 Tmp3 = LegalizeOp(Tmp1);
2176 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002177 }
Evan Cheng466685d2006-10-09 20:57:25 +00002178 break;
2179 case TargetLowering::Promote: {
2180 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002181 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002182 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002183 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002184
2185 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002186 LD->getSrcValueOffset(),
2187 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002188 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2189 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002190 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002191 }
Evan Cheng466685d2006-10-09 20:57:25 +00002192 }
2193 // Since loads produce two values, make sure to remember that we
2194 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002195 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2196 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002197 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002198 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002199 MVT SrcVT = LD->getMemoryVT();
2200 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002201 int SVOffset = LD->getSrcValueOffset();
2202 unsigned Alignment = LD->getAlignment();
2203 bool isVolatile = LD->isVolatile();
2204
Duncan Sands83ec4b62008-06-06 12:08:01 +00002205 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002206 // Some targets pretend to have an i1 loading operation, and actually
2207 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2208 // bits are guaranteed to be zero; it helps the optimizers understand
2209 // that these bits are zero. It is also useful for EXTLOAD, since it
2210 // tells the optimizers that those bits are undefined. It would be
2211 // nice to have an effective generic way of getting these benefits...
2212 // Until such a way is found, don't insist on promoting i1 here.
2213 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002214 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002215 // Promote to a byte-sized load if not loading an integral number of
2216 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002217 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2218 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002219 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002220
2221 // The extra bits are guaranteed to be zero, since we stored them that
2222 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2223
2224 ISD::LoadExtType NewExtType =
2225 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2226
2227 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2228 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2229 NVT, isVolatile, Alignment);
2230
2231 Ch = Result.getValue(1); // The chain.
2232
2233 if (ExtType == ISD::SEXTLOAD)
2234 // Having the top bits zero doesn't help when sign extending.
2235 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2236 Result, DAG.getValueType(SrcVT));
2237 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2238 // All the top bits are guaranteed to be zero - inform the optimizers.
2239 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2240 DAG.getValueType(SrcVT));
2241
2242 Tmp1 = LegalizeOp(Result);
2243 Tmp2 = LegalizeOp(Ch);
2244 } else if (SrcWidth & (SrcWidth - 1)) {
2245 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002246 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002247 "Unsupported extload!");
2248 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2249 assert(RoundWidth < SrcWidth);
2250 unsigned ExtraWidth = SrcWidth - RoundWidth;
2251 assert(ExtraWidth < RoundWidth);
2252 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2253 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002254 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2255 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002256 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002257 unsigned IncrementSize;
2258
2259 if (TLI.isLittleEndian()) {
2260 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2261 // Load the bottom RoundWidth bits.
2262 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2263 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2264 Alignment);
2265
2266 // Load the remaining ExtraWidth bits.
2267 IncrementSize = RoundWidth / 8;
2268 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2269 DAG.getIntPtrConstant(IncrementSize));
2270 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2271 LD->getSrcValue(), SVOffset + IncrementSize,
2272 ExtraVT, isVolatile,
2273 MinAlign(Alignment, IncrementSize));
2274
2275 // Build a factor node to remember that this load is independent of the
2276 // other one.
2277 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2278 Hi.getValue(1));
2279
2280 // Move the top bits to the right place.
2281 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2282 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2283
2284 // Join the hi and lo parts.
2285 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002286 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002287 // Big endian - avoid unaligned loads.
2288 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2289 // Load the top RoundWidth bits.
2290 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2291 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2292 Alignment);
2293
2294 // Load the remaining ExtraWidth bits.
2295 IncrementSize = RoundWidth / 8;
2296 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2297 DAG.getIntPtrConstant(IncrementSize));
2298 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2299 LD->getSrcValue(), SVOffset + IncrementSize,
2300 ExtraVT, isVolatile,
2301 MinAlign(Alignment, IncrementSize));
2302
2303 // Build a factor node to remember that this load is independent of the
2304 // other one.
2305 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2306 Hi.getValue(1));
2307
2308 // Move the top bits to the right place.
2309 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2310 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2311
2312 // Join the hi and lo parts.
2313 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2314 }
2315
2316 Tmp1 = LegalizeOp(Result);
2317 Tmp2 = LegalizeOp(Ch);
2318 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002319 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002320 default: assert(0 && "This action is not supported yet!");
2321 case TargetLowering::Custom:
2322 isCustom = true;
2323 // FALLTHROUGH
2324 case TargetLowering::Legal:
2325 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2326 Tmp1 = Result.getValue(0);
2327 Tmp2 = Result.getValue(1);
2328
2329 if (isCustom) {
2330 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002331 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002332 Tmp1 = LegalizeOp(Tmp3);
2333 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2334 }
2335 } else {
2336 // If this is an unaligned load and the target doesn't support it,
2337 // expand it.
2338 if (!TLI.allowsUnalignedMemoryAccesses()) {
2339 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002340 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002341 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002342 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002343 TLI);
2344 Tmp1 = Result.getOperand(0);
2345 Tmp2 = Result.getOperand(1);
2346 Tmp1 = LegalizeOp(Tmp1);
2347 Tmp2 = LegalizeOp(Tmp2);
2348 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002349 }
2350 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002351 break;
2352 case TargetLowering::Expand:
2353 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2354 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002355 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002356 LD->getSrcValueOffset(),
2357 LD->isVolatile(), LD->getAlignment());
2358 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2359 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2360 Tmp2 = LegalizeOp(Load.getValue(1));
2361 break;
2362 }
2363 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2364 // Turn the unsupported load into an EXTLOAD followed by an explicit
2365 // zero/sign extend inreg.
2366 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2367 Tmp1, Tmp2, LD->getSrcValue(),
2368 LD->getSrcValueOffset(), SrcVT,
2369 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002370 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002371 if (ExtType == ISD::SEXTLOAD)
2372 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2373 Result, DAG.getValueType(SrcVT));
2374 else
2375 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2376 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2377 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002378 break;
2379 }
Evan Cheng466685d2006-10-09 20:57:25 +00002380 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002381
Evan Cheng466685d2006-10-09 20:57:25 +00002382 // Since loads produce two values, make sure to remember that we legalized
2383 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002384 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2385 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002386 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002387 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002388 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002389 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002390 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002391 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002392 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002393 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002394 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002395 // 1 -> Hi
2396 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002397 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002398 TLI.getShiftAmountTy()));
2399 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2400 } else {
2401 // 0 -> Lo
2402 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2403 Node->getOperand(0));
2404 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002405 break;
2406 case Expand:
2407 // Get both the low and high parts.
2408 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002409 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002410 Result = Tmp2; // 1 -> Hi
2411 else
2412 Result = Tmp1; // 0 -> Lo
2413 break;
2414 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002415 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002416 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002417
2418 case ISD::CopyToReg:
2419 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002420
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002421 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002422 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002423 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002424 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002425 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002426 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002427 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002428 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002429 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002430 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002431 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2432 Tmp3);
2433 } else {
2434 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002435 }
2436
2437 // Since this produces two values, make sure to remember that we legalized
2438 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002439 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2440 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002441 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002442 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002443 break;
2444
2445 case ISD::RET:
2446 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002447
2448 // Ensure that libcalls are emitted before a return.
2449 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2450 Tmp1 = LegalizeOp(Tmp1);
2451 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002452
Chris Lattner3e928bb2005-01-07 07:47:09 +00002453 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002454 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002455 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002456 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002457 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002458 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002459 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002460 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002461 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002462 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002463 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002464 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002465
2466 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002467 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002468 std::swap(Lo, Hi);
2469
Gabor Greifba36cb52008-08-28 21:40:38 +00002470 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002471 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2472 else
2473 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002474 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002475 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002476 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002477 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002478 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2479 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002480
Dan Gohman7f321562007-06-25 16:23:39 +00002481 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002482 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002483 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002484 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002485 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002486 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002487 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002488 } else if (NumElems == 1) {
2489 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002490 Tmp2 = ScalarizeVectorOp(Tmp2);
2491 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002492 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002493
2494 // FIXME: Returns of gcc generic vectors smaller than a legal type
2495 // should be returned in integer registers!
2496
Chris Lattnerf87324e2006-04-11 01:31:51 +00002497 // The scalarized value type may not be legal, e.g. it might require
2498 // promotion or expansion. Relegalize the return.
2499 Result = LegalizeOp(Result);
2500 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002501 // FIXME: Returns of gcc generic vectors larger than a legal vector
2502 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002503 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002504 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002505 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002506 Result = LegalizeOp(Result);
2507 }
2508 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002509 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002510 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002511 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002512 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002513 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002514 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002515 }
2516 break;
2517 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002518 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002519 break;
2520 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002521 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002522 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002523 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002524 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2525 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002526 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002527 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002528 break;
2529 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002530 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002531 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002532 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002533 ExpandOp(Node->getOperand(i), Lo, Hi);
2534 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002535 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002536 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002537 NewValues.push_back(Hi);
2538 NewValues.push_back(Node->getOperand(i+1));
2539 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002540 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002541 }
2542 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002543 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002544 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002545
2546 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002547 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002548 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002549 Result = DAG.getNode(ISD::RET, MVT::Other,
2550 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002551 break;
2552 }
2553 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002554
Chris Lattner6862dbc2006-01-29 21:02:23 +00002555 if (Result.getOpcode() == ISD::RET) {
2556 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2557 default: assert(0 && "This action is not supported yet!");
2558 case TargetLowering::Legal: break;
2559 case TargetLowering::Custom:
2560 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002561 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002562 break;
2563 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002564 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002565 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002566 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002567 StoreSDNode *ST = cast<StoreSDNode>(Node);
2568 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2569 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002570 int SVOffset = ST->getSrcValueOffset();
2571 unsigned Alignment = ST->getAlignment();
2572 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002573
Evan Cheng8b2794a2006-10-13 21:14:26 +00002574 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002575 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2576 // FIXME: We shouldn't do this for TargetConstantFP's.
2577 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2578 // to phase ordering between legalized code and the dag combiner. This
2579 // probably means that we need to integrate dag combiner and legalizer
2580 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002581 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002582 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002583 if (CFP->getValueType(0) == MVT::f32 &&
2584 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002585 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002586 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002587 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002588 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2589 SVOffset, isVolatile, Alignment);
2590 break;
2591 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002592 // If this target supports 64-bit registers, do a single 64-bit store.
2593 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002594 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002595 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002596 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2597 SVOffset, isVolatile, Alignment);
2598 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002599 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002600 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2601 // stores. If the target supports neither 32- nor 64-bits, this
2602 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002603 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002604 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2605 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002606 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002607
2608 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2609 SVOffset, isVolatile, Alignment);
2610 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002611 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002612 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002613 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002614
2615 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2616 break;
2617 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002618 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002619 }
2620
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002621 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002622 case Legal: {
2623 Tmp3 = LegalizeOp(ST->getValue());
2624 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2625 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002626
Duncan Sands83ec4b62008-06-06 12:08:01 +00002627 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002628 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2629 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002630 case TargetLowering::Legal:
2631 // If this is an unaligned store and the target doesn't support it,
2632 // expand it.
2633 if (!TLI.allowsUnalignedMemoryAccesses()) {
2634 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002635 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002636 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002637 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002638 TLI);
2639 }
2640 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002641 case TargetLowering::Custom:
2642 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002643 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002644 break;
2645 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002646 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002647 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2648 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2649 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002650 ST->getSrcValue(), SVOffset, isVolatile,
2651 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002652 break;
2653 }
2654 break;
2655 }
2656 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002657 if (!ST->getMemoryVT().isVector()) {
2658 // Truncate the value and store the result.
2659 Tmp3 = PromoteOp(ST->getValue());
2660 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2661 SVOffset, ST->getMemoryVT(),
2662 isVolatile, Alignment);
2663 break;
2664 }
2665 // Fall thru to expand for vector
2666 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002667 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002668 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002669
2670 // If this is a vector type, then we have to calculate the increment as
2671 // the product of the element size in bytes, and the number of elements
2672 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002673 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002674 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002675 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002676 MVT InVT = InVal->getValueType(InIx);
2677 unsigned NumElems = InVT.getVectorNumElements();
2678 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002679
Dan Gohman7f321562007-06-25 16:23:39 +00002680 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002681 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002682 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002683 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002684 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002685 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002686 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002687 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002688 Result = LegalizeOp(Result);
2689 break;
2690 } else if (NumElems == 1) {
2691 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002692 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002693 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002694 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002695 // The scalarized value type may not be legal, e.g. it might require
2696 // promotion or expansion. Relegalize the scalar store.
2697 Result = LegalizeOp(Result);
2698 break;
2699 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002700 // Check if we have widen this node with another value
2701 std::map<SDValue, SDValue>::iterator I =
2702 WidenNodes.find(ST->getValue());
2703 if (I != WidenNodes.end()) {
2704 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2705 break;
2706 }
2707 else {
2708 SplitVectorOp(ST->getValue(), Lo, Hi);
2709 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2710 EVT.getSizeInBits()/8;
2711 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002712 }
2713 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002714 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002715 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002716
Richard Pennington4b052dc2008-09-25 16:15:10 +00002717 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002718 std::swap(Lo, Hi);
2719 }
2720
2721 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002722 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002723
Gabor Greifba36cb52008-08-28 21:40:38 +00002724 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002725 // Must be int <-> float one-to-one expansion.
2726 Result = Lo;
2727 break;
2728 }
2729
Evan Cheng8b2794a2006-10-13 21:14:26 +00002730 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002731 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002732 assert(isTypeLegal(Tmp2.getValueType()) &&
2733 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002734 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002735 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002736 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002737 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002738 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2739 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002740 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002741 }
2742 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002743 switch (getTypeAction(ST->getValue().getValueType())) {
2744 case Legal:
2745 Tmp3 = LegalizeOp(ST->getValue());
2746 break;
2747 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002748 if (!ST->getValue().getValueType().isVector()) {
2749 // We can promote the value, the truncstore will still take care of it.
2750 Tmp3 = PromoteOp(ST->getValue());
2751 break;
2752 }
2753 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002754 case Expand:
2755 // Just store the low part. This may become a non-trunc store, so make
2756 // sure to use getTruncStore, not UpdateNodeOperands below.
2757 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2758 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2759 SVOffset, MVT::i8, isVolatile, Alignment);
2760 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002761
Duncan Sands83ec4b62008-06-06 12:08:01 +00002762 MVT StVT = ST->getMemoryVT();
2763 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002764
Duncan Sands83ec4b62008-06-06 12:08:01 +00002765 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002766 // Promote to a byte-sized store with upper bits zero if not
2767 // storing an integral number of bytes. For example, promote
2768 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002769 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002770 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2771 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2772 SVOffset, NVT, isVolatile, Alignment);
2773 } else if (StWidth & (StWidth - 1)) {
2774 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002775 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002776 "Unsupported truncstore!");
2777 unsigned RoundWidth = 1 << Log2_32(StWidth);
2778 assert(RoundWidth < StWidth);
2779 unsigned ExtraWidth = StWidth - RoundWidth;
2780 assert(ExtraWidth < RoundWidth);
2781 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2782 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002783 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2784 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002785 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002786 unsigned IncrementSize;
2787
2788 if (TLI.isLittleEndian()) {
2789 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2790 // Store the bottom RoundWidth bits.
2791 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2792 SVOffset, RoundVT,
2793 isVolatile, Alignment);
2794
2795 // Store the remaining ExtraWidth bits.
2796 IncrementSize = RoundWidth / 8;
2797 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2798 DAG.getIntPtrConstant(IncrementSize));
2799 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2800 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2801 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2802 SVOffset + IncrementSize, ExtraVT, isVolatile,
2803 MinAlign(Alignment, IncrementSize));
2804 } else {
2805 // Big endian - avoid unaligned stores.
2806 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2807 // Store the top RoundWidth bits.
2808 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2809 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2810 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2811 RoundVT, isVolatile, Alignment);
2812
2813 // Store the remaining ExtraWidth bits.
2814 IncrementSize = RoundWidth / 8;
2815 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2816 DAG.getIntPtrConstant(IncrementSize));
2817 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2818 SVOffset + IncrementSize, ExtraVT, isVolatile,
2819 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002820 }
Duncan Sands7e857202008-01-22 07:17:34 +00002821
2822 // The order of the stores doesn't matter.
2823 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2824 } else {
2825 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2826 Tmp2 != ST->getBasePtr())
2827 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2828 ST->getOffset());
2829
2830 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2831 default: assert(0 && "This action is not supported yet!");
2832 case TargetLowering::Legal:
2833 // If this is an unaligned store and the target doesn't support it,
2834 // expand it.
2835 if (!TLI.allowsUnalignedMemoryAccesses()) {
2836 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002837 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002838 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002839 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002840 TLI);
2841 }
2842 break;
2843 case TargetLowering::Custom:
2844 Result = TLI.LowerOperation(Result, DAG);
2845 break;
2846 case Expand:
2847 // TRUNCSTORE:i16 i32 -> STORE i16
2848 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2849 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2850 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2851 isVolatile, Alignment);
2852 break;
2853 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002854 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002855 }
2856 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002857 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002858 case ISD::PCMARKER:
2859 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002860 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002861 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002862 case ISD::STACKSAVE:
2863 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002864 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2865 Tmp1 = Result.getValue(0);
2866 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002867
Chris Lattner140d53c2006-01-13 02:50:02 +00002868 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2869 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002870 case TargetLowering::Legal: break;
2871 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002872 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002873 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002874 Tmp1 = LegalizeOp(Tmp3);
2875 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002876 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002877 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002878 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002879 // Expand to CopyFromReg if the target set
2880 // StackPointerRegisterToSaveRestore.
2881 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002882 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002883 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002884 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002885 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002886 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2887 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002888 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002889 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002890 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002891
2892 // Since stacksave produce two values, make sure to remember that we
2893 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002894 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2895 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002896 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002897
Chris Lattner140d53c2006-01-13 02:50:02 +00002898 case ISD::STACKRESTORE:
2899 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2900 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002901 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002902
2903 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2904 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002905 case TargetLowering::Legal: break;
2906 case TargetLowering::Custom:
2907 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002908 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002909 break;
2910 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002911 // Expand to CopyToReg if the target set
2912 // StackPointerRegisterToSaveRestore.
2913 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2914 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2915 } else {
2916 Result = Tmp1;
2917 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002918 break;
2919 }
2920 break;
2921
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002922 case ISD::READCYCLECOUNTER:
2923 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002924 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002925 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2926 Node->getValueType(0))) {
2927 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002928 case TargetLowering::Legal:
2929 Tmp1 = Result.getValue(0);
2930 Tmp2 = Result.getValue(1);
2931 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002932 case TargetLowering::Custom:
2933 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002934 Tmp1 = LegalizeOp(Result.getValue(0));
2935 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002936 break;
2937 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002938
2939 // Since rdcc produce two values, make sure to remember that we legalized
2940 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002941 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2942 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002943 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002944
Chris Lattner2ee743f2005-01-14 22:08:15 +00002945 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002946 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2947 case Expand: assert(0 && "It's impossible to expand bools");
2948 case Legal:
2949 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2950 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002951 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002952 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002953 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002954 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002955 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002956 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002957 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002958 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002959 break;
2960 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002961 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002962 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002963 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002964
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002965 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002966
Nate Begemanb942a3d2005-08-23 04:29:48 +00002967 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002968 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002969 case TargetLowering::Legal: break;
2970 case TargetLowering::Custom: {
2971 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002972 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002973 break;
2974 }
Nate Begeman9373a812005-08-10 20:51:12 +00002975 case TargetLowering::Expand:
2976 if (Tmp1.getOpcode() == ISD::SETCC) {
2977 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2978 Tmp2, Tmp3,
2979 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2980 } else {
2981 Result = DAG.getSelectCC(Tmp1,
2982 DAG.getConstant(0, Tmp1.getValueType()),
2983 Tmp2, Tmp3, ISD::SETNE);
2984 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002985 break;
2986 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002987 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002988 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2989 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002990 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002991 ExtOp = ISD::BIT_CONVERT;
2992 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002993 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002994 ExtOp = ISD::ANY_EXTEND;
2995 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002996 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002997 ExtOp = ISD::FP_EXTEND;
2998 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002999 }
3000 // Promote each of the values to the new type.
3001 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
3002 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
3003 // Perform the larger operation, then round down.
3004 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00003005 if (TruncOp != ISD::FP_ROUND)
3006 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
3007 else
3008 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
3009 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00003010 break;
3011 }
3012 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003013 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003014 case ISD::SELECT_CC: {
3015 Tmp1 = Node->getOperand(0); // LHS
3016 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00003017 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
3018 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00003019 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00003020
Dale Johannesen53e4e442008-11-07 22:54:33 +00003021 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003022
Evan Cheng7f042682008-10-15 02:05:31 +00003023 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00003024 // the LHS is a legal SETCC itself. In this case, we need to compare
3025 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00003026 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003027 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
3028 CC = DAG.getCondCode(ISD::SETNE);
3029 }
3030 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
3031
3032 // Everything is legal, see if we should expand this op or something.
3033 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
3034 default: assert(0 && "This action is not supported yet!");
3035 case TargetLowering::Legal: break;
3036 case TargetLowering::Custom:
3037 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003038 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00003039 break;
Nate Begeman9373a812005-08-10 20:51:12 +00003040 }
3041 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00003042 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003043 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00003044 Tmp1 = Node->getOperand(0);
3045 Tmp2 = Node->getOperand(1);
3046 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00003047 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00003048
3049 // If we had to Expand the SetCC operands into a SELECT node, then it may
3050 // not always be possible to return a true LHS & RHS. In this case, just
3051 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00003052 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00003053 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003054 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003055 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003056
Chris Lattner73e142f2006-01-30 22:43:50 +00003057 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003058 default: assert(0 && "Cannot handle this action for SETCC yet!");
3059 case TargetLowering::Custom:
3060 isCustom = true;
3061 // FALLTHROUGH.
3062 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00003063 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00003064 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00003065 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003066 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00003067 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003068 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003069 case TargetLowering::Promote: {
3070 // First step, figure out the appropriate operation to use.
3071 // Allow SETCC to not be supported for all legal data types
3072 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00003073 MVT NewInTy = Node->getOperand(0).getValueType();
3074 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00003075
3076 // Scan for the appropriate larger type to use.
3077 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003078 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00003079
Duncan Sands83ec4b62008-06-06 12:08:01 +00003080 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003081 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003082 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003083 "Fell off of the edge of the floating point world");
3084
3085 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00003086 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00003087 break;
3088 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003089 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00003090 assert(0 && "Cannot promote Legal Integer SETCC yet");
3091 else {
3092 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3093 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3094 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003095 Tmp1 = LegalizeOp(Tmp1);
3096 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00003097 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00003098 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00003099 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003100 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003101 case TargetLowering::Expand:
3102 // Expand a setcc node into a select_cc of the same condition, lhs, and
3103 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003104 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003105 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3106 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003107 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003108 break;
3109 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003110 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003111 case ISD::VSETCC: {
3112 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3113 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003114 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003115
3116 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3117
3118 // Everything is legal, see if we should expand this op or something.
3119 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3120 default: assert(0 && "This action is not supported yet!");
3121 case TargetLowering::Legal: break;
3122 case TargetLowering::Custom:
3123 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003124 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003125 break;
3126 }
3127 break;
3128 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003129
Chris Lattner5b359c62005-04-02 04:00:59 +00003130 case ISD::SHL_PARTS:
3131 case ISD::SRA_PARTS:
3132 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003133 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003134 bool Changed = false;
3135 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3136 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3137 Changed |= Ops.back() != Node->getOperand(i);
3138 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003139 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003140 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003141
Evan Cheng05a2d562006-01-09 18:31:59 +00003142 switch (TLI.getOperationAction(Node->getOpcode(),
3143 Node->getValueType(0))) {
3144 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003145 case TargetLowering::Legal: break;
3146 case TargetLowering::Custom:
3147 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003148 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003149 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003150 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003151 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003152 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003153 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003154 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003155 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003156 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003157 return RetVal;
3158 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003159 break;
3160 }
3161
Chris Lattner2c8086f2005-04-02 05:00:07 +00003162 // Since these produce multiple values, make sure to remember that we
3163 // legalized all of them.
3164 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003165 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003166 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003167 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003168
3169 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003170 case ISD::ADD:
3171 case ISD::SUB:
3172 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003173 case ISD::MULHS:
3174 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003175 case ISD::UDIV:
3176 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003177 case ISD::AND:
3178 case ISD::OR:
3179 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003180 case ISD::SHL:
3181 case ISD::SRL:
3182 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003183 case ISD::FADD:
3184 case ISD::FSUB:
3185 case ISD::FMUL:
3186 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003187 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003188 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003189 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3190 case Expand: assert(0 && "Not possible");
3191 case Legal:
3192 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3193 break;
3194 case Promote:
3195 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3196 break;
3197 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003198
3199 if ((Node->getOpcode() == ISD::SHL ||
3200 Node->getOpcode() == ISD::SRL ||
3201 Node->getOpcode() == ISD::SRA) &&
3202 !Node->getValueType(0).isVector()) {
Mon P Wange9f10152008-12-09 05:46:39 +00003203 Tmp2 = LegalizeShiftAmount(Tmp2);
3204 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003205
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003206 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003207
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003208 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003209 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003210 case TargetLowering::Legal: break;
3211 case TargetLowering::Custom:
3212 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003213 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003214 Result = Tmp1;
3215 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003216 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003217 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003218 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003219 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003220
Dan Gohman525178c2007-10-08 18:33:35 +00003221 // See if multiply or divide can be lowered using two-result operations.
3222 SDVTList VTs = DAG.getVTList(VT, VT);
3223 if (Node->getOpcode() == ISD::MUL) {
3224 // We just need the low half of the multiply; try both the signed
3225 // and unsigned forms. If the target supports both SMUL_LOHI and
3226 // UMUL_LOHI, form a preference by checking which forms of plain
3227 // MULH it supports.
3228 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3229 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3230 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3231 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3232 unsigned OpToUse = 0;
3233 if (HasSMUL_LOHI && !HasMULHS) {
3234 OpToUse = ISD::SMUL_LOHI;
3235 } else if (HasUMUL_LOHI && !HasMULHU) {
3236 OpToUse = ISD::UMUL_LOHI;
3237 } else if (HasSMUL_LOHI) {
3238 OpToUse = ISD::SMUL_LOHI;
3239 } else if (HasUMUL_LOHI) {
3240 OpToUse = ISD::UMUL_LOHI;
3241 }
3242 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003243 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003244 break;
3245 }
3246 }
3247 if (Node->getOpcode() == ISD::MULHS &&
3248 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003249 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3250 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003251 break;
3252 }
3253 if (Node->getOpcode() == ISD::MULHU &&
3254 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003255 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3256 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003257 break;
3258 }
3259 if (Node->getOpcode() == ISD::SDIV &&
3260 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003261 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3262 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003263 break;
3264 }
3265 if (Node->getOpcode() == ISD::UDIV &&
3266 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003267 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3268 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003269 break;
3270 }
Mon P Wang0c397192008-10-30 08:01:45 +00003271
Dan Gohman82669522007-10-11 23:57:53 +00003272 // Check to see if we have a libcall for this operator.
3273 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3274 bool isSigned = false;
3275 switch (Node->getOpcode()) {
3276 case ISD::UDIV:
3277 case ISD::SDIV:
3278 if (VT == MVT::i32) {
3279 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003280 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003281 isSigned = Node->getOpcode() == ISD::SDIV;
3282 }
3283 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003284 case ISD::MUL:
3285 if (VT == MVT::i32)
3286 LC = RTLIB::MUL_I32;
3287 break;
Dan Gohman82669522007-10-11 23:57:53 +00003288 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003289 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3290 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003291 break;
3292 default: break;
3293 }
3294 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003295 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003296 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003297 break;
3298 }
Mon P Wang0c397192008-10-30 08:01:45 +00003299
Duncan Sands83ec4b62008-06-06 12:08:01 +00003300 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003301 "Cannot expand this binary operator!");
3302 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003303 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003304 break;
3305 }
Evan Chengcc987612006-04-12 21:20:24 +00003306 case TargetLowering::Promote: {
3307 switch (Node->getOpcode()) {
3308 default: assert(0 && "Do not know how to promote this BinOp!");
3309 case ISD::AND:
3310 case ISD::OR:
3311 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003312 MVT OVT = Node->getValueType(0);
3313 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3314 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003315 // Bit convert each of the values to the new type.
3316 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3317 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3318 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3319 // Bit convert the result back the original type.
3320 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3321 break;
3322 }
3323 }
3324 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003325 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003326 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003327
Dan Gohmane14ea862007-10-05 14:17:22 +00003328 case ISD::SMUL_LOHI:
3329 case ISD::UMUL_LOHI:
3330 case ISD::SDIVREM:
3331 case ISD::UDIVREM:
3332 // These nodes will only be produced by target-specific lowering, so
3333 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003334 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003335 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003336
3337 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3338 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3339 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003340 break;
3341
Chris Lattnera09f8482006-03-05 05:09:38 +00003342 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3343 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3344 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3345 case Expand: assert(0 && "Not possible");
3346 case Legal:
3347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3348 break;
3349 case Promote:
3350 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3351 break;
3352 }
3353
3354 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3355
3356 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3357 default: assert(0 && "Operation not supported");
3358 case TargetLowering::Custom:
3359 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003360 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003361 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003362 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003363 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003364 // If this target supports fabs/fneg natively and select is cheap,
3365 // do this efficiently.
3366 if (!TLI.isSelectExpensive() &&
3367 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3368 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003369 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003370 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003371 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003372 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003373 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003374 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003375 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003376 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3377 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003378 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003379 // Select between the nabs and abs value based on the sign bit of
3380 // the input.
3381 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3382 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3383 AbsVal),
3384 AbsVal);
3385 Result = LegalizeOp(Result);
3386 break;
3387 }
3388
3389 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003390 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003391 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3392 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3393 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3394 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003395 break;
3396 }
Evan Cheng912095b2007-01-04 21:56:39 +00003397 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003398 break;
3399
Nate Begeman551bf3f2006-02-17 05:43:56 +00003400 case ISD::ADDC:
3401 case ISD::SUBC:
3402 Tmp1 = LegalizeOp(Node->getOperand(0));
3403 Tmp2 = LegalizeOp(Node->getOperand(1));
3404 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003405 Tmp3 = Result.getValue(0);
3406 Tmp4 = Result.getValue(1);
3407
3408 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3409 default: assert(0 && "This action is not supported yet!");
3410 case TargetLowering::Legal:
3411 break;
3412 case TargetLowering::Custom:
3413 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3414 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003415 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003416 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3417 }
3418 break;
3419 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003420 // Since this produces two values, make sure to remember that we legalized
3421 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003422 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3423 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3424 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003425
Nate Begeman551bf3f2006-02-17 05:43:56 +00003426 case ISD::ADDE:
3427 case ISD::SUBE:
3428 Tmp1 = LegalizeOp(Node->getOperand(0));
3429 Tmp2 = LegalizeOp(Node->getOperand(1));
3430 Tmp3 = LegalizeOp(Node->getOperand(2));
3431 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003432 Tmp3 = Result.getValue(0);
3433 Tmp4 = Result.getValue(1);
3434
3435 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3436 default: assert(0 && "This action is not supported yet!");
3437 case TargetLowering::Legal:
3438 break;
3439 case TargetLowering::Custom:
3440 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3441 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003442 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003443 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3444 }
3445 break;
3446 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003447 // Since this produces two values, make sure to remember that we legalized
3448 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003449 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3450 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3451 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003452
Nate Begeman419f8b62005-10-18 00:27:41 +00003453 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003454 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003455 // TODO: handle the case where the Lo and Hi operands are not of legal type
3456 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3457 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3458 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003459 case TargetLowering::Promote:
3460 case TargetLowering::Custom:
3461 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003462 case TargetLowering::Legal:
3463 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3464 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3465 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003466 case TargetLowering::Expand:
3467 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3468 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3469 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003470 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003471 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003472 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003473 break;
3474 }
3475 break;
3476 }
3477
Nate Begemanc105e192005-04-06 00:23:54 +00003478 case ISD::UREM:
3479 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003480 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003481 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3482 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003483
Nate Begemanc105e192005-04-06 00:23:54 +00003484 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003485 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3486 case TargetLowering::Custom:
3487 isCustom = true;
3488 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003489 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003490 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003491 if (isCustom) {
3492 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003493 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003494 }
Nate Begemanc105e192005-04-06 00:23:54 +00003495 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003496 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003497 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003498 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003499 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003500
3501 // See if remainder can be lowered using two-result operations.
3502 SDVTList VTs = DAG.getVTList(VT, VT);
3503 if (Node->getOpcode() == ISD::SREM &&
3504 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003505 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003506 break;
3507 }
3508 if (Node->getOpcode() == ISD::UREM &&
3509 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003510 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003511 break;
3512 }
3513
Duncan Sands83ec4b62008-06-06 12:08:01 +00003514 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003515 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003516 TargetLowering::Legal) {
3517 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003518 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003519 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3520 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003521 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003522 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003523 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003524 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003525 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003526 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3527 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003528 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003529 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003530 }
Dan Gohman0d974262007-11-06 22:11:54 +00003531 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003532 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003533 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003534 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003535 Result = LegalizeOp(UnrollVectorOp(Op));
3536 } else {
3537 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003538 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3539 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003540 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003541 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003542 }
Nate Begemanc105e192005-04-06 00:23:54 +00003543 }
3544 break;
3545 }
Dan Gohman525178c2007-10-08 18:33:35 +00003546 }
Nate Begemanc105e192005-04-06 00:23:54 +00003547 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003548 case ISD::VAARG: {
3549 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3550 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3551
Duncan Sands83ec4b62008-06-06 12:08:01 +00003552 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003553 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3554 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003555 case TargetLowering::Custom:
3556 isCustom = true;
3557 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003558 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003559 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3560 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003561 Tmp1 = Result.getValue(1);
3562
3563 if (isCustom) {
3564 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003565 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003566 Result = LegalizeOp(Tmp2);
3567 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3568 }
3569 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003570 break;
3571 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003572 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003573 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003574 // Increment the pointer, VAList, to the next vaarg
Duncan Sands5c58a312008-11-03 11:51:11 +00003575 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3576 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3577 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003578 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003579 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003580 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003581 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003582 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003583 Result = LegalizeOp(Result);
3584 break;
3585 }
3586 }
3587 // Since VAARG produces two values, make sure to remember that we
3588 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003589 AddLegalizedOperand(SDValue(Node, 0), Result);
3590 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003591 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003592 }
3593
3594 case ISD::VACOPY:
3595 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3596 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3597 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3598
3599 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3600 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003601 case TargetLowering::Custom:
3602 isCustom = true;
3603 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003604 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003605 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3606 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003607 if (isCustom) {
3608 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003609 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003610 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003611 break;
3612 case TargetLowering::Expand:
3613 // This defaults to loading a pointer from the input and storing it to the
3614 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003615 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3616 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003617 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3618 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003619 break;
3620 }
3621 break;
3622
3623 case ISD::VAEND:
3624 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3625 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3626
3627 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3628 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003629 case TargetLowering::Custom:
3630 isCustom = true;
3631 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003632 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003633 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003634 if (isCustom) {
3635 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003636 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003637 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003638 break;
3639 case TargetLowering::Expand:
3640 Result = Tmp1; // Default to a no-op, return the chain
3641 break;
3642 }
3643 break;
3644
3645 case ISD::VASTART:
3646 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3647 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3648
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003649 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3650
Nate Begemanacc398c2006-01-25 18:21:52 +00003651 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3652 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003653 case TargetLowering::Legal: break;
3654 case TargetLowering::Custom:
3655 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003656 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003657 break;
3658 }
3659 break;
3660
Nate Begeman35ef9132006-01-11 21:21:00 +00003661 case ISD::ROTL:
3662 case ISD::ROTR:
3663 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3664 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003665 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003666 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3667 default:
3668 assert(0 && "ROTL/ROTR legalize operation not supported");
3669 break;
3670 case TargetLowering::Legal:
3671 break;
3672 case TargetLowering::Custom:
3673 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003674 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003675 break;
3676 case TargetLowering::Promote:
3677 assert(0 && "Do not know how to promote ROTL/ROTR");
3678 break;
3679 case TargetLowering::Expand:
3680 assert(0 && "Do not know how to expand ROTL/ROTR");
3681 break;
3682 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003683 break;
3684
Nate Begemand88fc032006-01-14 03:14:10 +00003685 case ISD::BSWAP:
3686 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3687 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003688 case TargetLowering::Custom:
3689 assert(0 && "Cannot custom legalize this yet!");
3690 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003691 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003692 break;
3693 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003694 MVT OVT = Tmp1.getValueType();
3695 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3696 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003697
Chris Lattner456a93a2006-01-28 07:39:30 +00003698 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3699 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3700 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3701 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3702 break;
3703 }
3704 case TargetLowering::Expand:
3705 Result = ExpandBSWAP(Tmp1);
3706 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003707 }
3708 break;
3709
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003710 case ISD::CTPOP:
3711 case ISD::CTTZ:
3712 case ISD::CTLZ:
3713 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3714 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003715 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003716 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003717 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003718 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003719 TargetLowering::Custom) {
3720 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003721 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003722 Result = Tmp1;
3723 }
Scott Michel910b66d2007-07-30 21:00:31 +00003724 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003725 break;
3726 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003727 MVT OVT = Tmp1.getValueType();
3728 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003729
3730 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003731 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3732 // Perform the larger operation, then subtract if needed.
3733 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003734 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003735 case ISD::CTPOP:
3736 Result = Tmp1;
3737 break;
3738 case ISD::CTTZ:
3739 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003740 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003741 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003742 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003743 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003744 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003745 break;
3746 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003747 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003748 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003749 DAG.getConstant(NVT.getSizeInBits() -
3750 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003751 break;
3752 }
3753 break;
3754 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003755 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003756 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003757 break;
3758 }
3759 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003760
Chris Lattner2c8086f2005-04-02 05:00:07 +00003761 // Unary operators
3762 case ISD::FABS:
3763 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003764 case ISD::FSQRT:
3765 case ISD::FSIN:
3766 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003767 case ISD::FLOG:
3768 case ISD::FLOG2:
3769 case ISD::FLOG10:
3770 case ISD::FEXP:
3771 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003772 case ISD::FTRUNC:
3773 case ISD::FFLOOR:
3774 case ISD::FCEIL:
3775 case ISD::FRINT:
3776 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003777 Tmp1 = LegalizeOp(Node->getOperand(0));
3778 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003779 case TargetLowering::Promote:
3780 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003781 isCustom = true;
3782 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003783 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003784 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003785 if (isCustom) {
3786 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003787 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003788 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003789 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003790 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003791 switch (Node->getOpcode()) {
3792 default: assert(0 && "Unreachable!");
3793 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003794 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3795 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003796 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003797 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003798 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003799 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003800 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003801 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003802 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003803 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003804 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3805 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003806 break;
3807 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003808 case ISD::FSQRT:
3809 case ISD::FSIN:
3810 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003811 case ISD::FLOG:
3812 case ISD::FLOG2:
3813 case ISD::FLOG10:
3814 case ISD::FEXP:
3815 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003816 case ISD::FTRUNC:
3817 case ISD::FFLOOR:
3818 case ISD::FCEIL:
3819 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003820 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003821 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003822
3823 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003824 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003825 Result = LegalizeOp(UnrollVectorOp(Op));
3826 break;
3827 }
3828
Evan Cheng56966222007-01-12 02:11:51 +00003829 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003830 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003831 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003832 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3833 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003834 break;
3835 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003836 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3837 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003838 break;
3839 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003840 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3841 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003842 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003843 case ISD::FLOG:
3844 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3845 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3846 break;
3847 case ISD::FLOG2:
3848 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3849 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3850 break;
3851 case ISD::FLOG10:
3852 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3853 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3854 break;
3855 case ISD::FEXP:
3856 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3857 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3858 break;
3859 case ISD::FEXP2:
3860 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3861 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3862 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003863 case ISD::FTRUNC:
3864 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3865 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3866 break;
3867 case ISD::FFLOOR:
3868 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3869 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3870 break;
3871 case ISD::FCEIL:
3872 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3873 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3874 break;
3875 case ISD::FRINT:
3876 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3877 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3878 break;
3879 case ISD::FNEARBYINT:
3880 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3881 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3882 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003883 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003884 default: assert(0 && "Unreachable!");
3885 }
Dan Gohman475871a2008-07-27 21:46:04 +00003886 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003887 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003888 break;
3889 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003890 }
3891 break;
3892 }
3893 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003894 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003895 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003896
3897 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003898 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003899 Result = LegalizeOp(UnrollVectorOp(Op));
3900 break;
3901 }
3902
3903 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003904 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3905 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003906 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003907 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003908 break;
3909 }
Chris Lattner35481892005-12-23 00:16:34 +00003910 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003911 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003912 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3913 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003914 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003915 // The input has to be a vector type, we have to either scalarize it, pack
3916 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003917 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003918 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003919 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3920 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003921
3922 // Figure out if there is a simple type corresponding to this Vector
3923 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003924 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003925 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003926 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003927 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3928 LegalizeOp(Node->getOperand(0)));
3929 break;
3930 } else if (NumElems == 1) {
3931 // Turn this into a bit convert of the scalar input.
3932 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3933 ScalarizeVectorOp(Node->getOperand(0)));
3934 break;
3935 } else {
3936 // FIXME: UNIMP! Store then reload
3937 assert(0 && "Cast from unsupported vector type not implemented yet!");
3938 }
Chris Lattner67993f72006-01-23 07:30:46 +00003939 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003940 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3941 Node->getOperand(0).getValueType())) {
3942 default: assert(0 && "Unknown operation action!");
3943 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003944 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3945 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003946 break;
3947 case TargetLowering::Legal:
3948 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003949 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003950 break;
3951 }
3952 }
3953 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003954 case ISD::CONVERT_RNDSAT: {
3955 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3956 switch (CvtCode) {
3957 default: assert(0 && "Unknown cvt code!");
3958 case ISD::CVT_SF:
3959 case ISD::CVT_UF:
Mon P Wang77cdf302008-11-10 20:54:11 +00003960 case ISD::CVT_FF:
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003961 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003962 case ISD::CVT_FS:
3963 case ISD::CVT_FU:
3964 case ISD::CVT_SS:
3965 case ISD::CVT_SU:
3966 case ISD::CVT_US:
3967 case ISD::CVT_UU: {
3968 SDValue DTyOp = Node->getOperand(1);
3969 SDValue STyOp = Node->getOperand(2);
3970 SDValue RndOp = Node->getOperand(3);
3971 SDValue SatOp = Node->getOperand(4);
3972 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3973 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3974 case Legal:
3975 Tmp1 = LegalizeOp(Node->getOperand(0));
3976 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3977 RndOp, SatOp);
3978 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3979 TargetLowering::Custom) {
3980 Tmp1 = TLI.LowerOperation(Result, DAG);
3981 if (Tmp1.getNode()) Result = Tmp1;
3982 }
3983 break;
3984 case Promote:
3985 Result = PromoteOp(Node->getOperand(0));
3986 // For FP, make Op1 a i32
3987
Mon P Wang1cd46bb2008-12-09 07:27:39 +00003988 Result = DAG.getConvertRndSat(Op.getValueType(), Result,
Mon P Wang77cdf302008-11-10 20:54:11 +00003989 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3990 break;
3991 }
3992 break;
3993 }
3994 } // end switch CvtCode
3995 break;
3996 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003997 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003998 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003999 case ISD::UINT_TO_FP: {
4000 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00004001 Result = LegalizeINT_TO_FP(Result, isSigned,
4002 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004003 break;
4004 }
4005 case ISD::TRUNCATE:
4006 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4007 case Legal:
4008 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel546d7b52008-12-02 19:55:08 +00004009 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
4010 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
4011 case TargetLowering::Custom:
Mon P Wangf67303d2008-12-11 00:44:22 +00004012 isCustom = true;
4013 // FALLTHROUGH
Scott Michel546d7b52008-12-02 19:55:08 +00004014 case TargetLowering::Legal:
Mon P Wangf67303d2008-12-11 00:44:22 +00004015 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4016 if (isCustom) {
4017 Tmp1 = TLI.LowerOperation(Result, DAG);
4018 if (Tmp1.getNode()) Result = Tmp1;
4019 }
4020 break;
Mon P Wang9e5ecb82008-12-12 01:25:51 +00004021 case TargetLowering::Expand:
4022 assert(Result.getValueType().isVector() && "must be vector type");
4023 // Unroll the truncate. We should do better.
4024 Result = LegalizeOp(UnrollVectorOp(Result));
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00004025 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004026 break;
4027 case Expand:
4028 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4029
4030 // Since the result is legal, we should just be able to truncate the low
4031 // part of the source.
4032 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
4033 break;
4034 case Promote:
4035 Result = PromoteOp(Node->getOperand(0));
4036 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
4037 break;
4038 }
4039 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004040
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004041 case ISD::FP_TO_SINT:
4042 case ISD::FP_TO_UINT:
4043 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4044 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00004045 Tmp1 = LegalizeOp(Node->getOperand(0));
4046
Chris Lattner1618beb2005-07-29 00:11:56 +00004047 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
4048 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004049 case TargetLowering::Custom:
4050 isCustom = true;
4051 // FALLTHROUGH
4052 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004053 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004054 if (isCustom) {
4055 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004056 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00004057 }
4058 break;
4059 case TargetLowering::Promote:
4060 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
4061 Node->getOpcode() == ISD::FP_TO_SINT);
4062 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00004063 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00004064 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004065 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004066 MVT VT = Node->getOperand(0).getValueType();
4067 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00004068 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00004069 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
4070 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004071 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00004072 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00004073 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00004074 Node->getOperand(0), Tmp2, ISD::SETLT);
4075 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
4076 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00004077 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00004078 Tmp2));
4079 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00004080 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004081 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
4082 break;
Nate Begemand2558e32005-08-14 01:20:53 +00004083 } else {
4084 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4085 }
4086 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00004087 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004088 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00004089 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004090 MVT VT = Op.getValueType();
4091 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004092 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004093 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004094 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4095 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4096 Node->getOperand(0), DAG.getValueType(MVT::f64));
4097 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4098 DAG.getIntPtrConstant(1));
4099 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4100 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004101 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4102 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4103 Tmp2 = DAG.getConstantFP(apf, OVT);
4104 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4105 // FIXME: generated code sucks.
4106 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4107 DAG.getNode(ISD::ADD, MVT::i32,
4108 DAG.getNode(ISD::FP_TO_SINT, VT,
4109 DAG.getNode(ISD::FSUB, OVT,
4110 Node->getOperand(0), Tmp2)),
4111 DAG.getConstant(0x80000000, MVT::i32)),
4112 DAG.getNode(ISD::FP_TO_SINT, VT,
4113 Node->getOperand(0)),
4114 DAG.getCondCode(ISD::SETGE));
4115 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004116 break;
4117 }
Dan Gohmana2e94852008-03-10 23:03:31 +00004118 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004119 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4120 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4121 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004122 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004123 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004124 break;
4125 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004126 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004127 Tmp1 = PromoteOp(Node->getOperand(0));
4128 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4129 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004130 break;
4131 }
4132 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004133
Chris Lattnerf2670a82008-01-16 06:57:07 +00004134 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004135 MVT DstVT = Op.getValueType();
4136 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004137 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4138 // The only other way we can lower this is to turn it into a STORE,
4139 // LOAD pair, targetting a temporary location (a stack slot).
4140 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4141 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004142 }
4143 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4144 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4145 case Legal:
4146 Tmp1 = LegalizeOp(Node->getOperand(0));
4147 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4148 break;
4149 case Promote:
4150 Tmp1 = PromoteOp(Node->getOperand(0));
4151 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4152 break;
4153 }
4154 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004155 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004156 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004157 MVT DstVT = Op.getValueType();
4158 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004159 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4160 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004161 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004162 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004163 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004164 if (DstVT!=MVT::f64)
4165 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004166 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004167 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004168 // The only other way we can lower this is to turn it into a STORE,
4169 // LOAD pair, targetting a temporary location (a stack slot).
4170 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4171 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004172 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004173 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4174 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4175 case Legal:
4176 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004177 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004178 break;
4179 case Promote:
4180 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004181 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4182 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004183 break;
4184 }
4185 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004186 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004187 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004188 case ISD::ZERO_EXTEND:
4189 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004190 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004191 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004192 case Legal:
4193 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004194 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004195 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4196 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004197 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004198 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004199 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004200 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004201 case Promote:
4202 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004203 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004204 Tmp1 = PromoteOp(Node->getOperand(0));
4205 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004206 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004207 case ISD::ZERO_EXTEND:
4208 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004209 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004210 Result = DAG.getZeroExtendInReg(Result,
4211 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004212 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004213 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004214 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004215 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004216 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004217 Result,
4218 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004219 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004220 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004221 }
4222 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004223 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004224 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004225 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004226 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004227
4228 // If this operation is not supported, convert it to a shl/shr or load/store
4229 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004230 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4231 default: assert(0 && "This action not supported for this op yet!");
4232 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004233 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004234 break;
4235 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004236 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004237 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004238 // NOTE: we could fall back on load/store here too for targets without
4239 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004240 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4241 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004242 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004243 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4244 Node->getOperand(0), ShiftCst);
4245 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4246 Result, ShiftCst);
4247 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004248 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004249 // EXTLOAD pair, targetting a temporary location (a stack slot).
4250
4251 // NOTE: there is a choice here between constantly creating new stack
4252 // slots and always reusing the same one. We currently always create
4253 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004254 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4255 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004256 } else {
4257 assert(0 && "Unknown op");
4258 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004259 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004260 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004261 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004262 }
Duncan Sands36397f52007-07-27 12:58:54 +00004263 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004264 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004265 for (unsigned i = 0; i != 6; ++i)
4266 Ops[i] = LegalizeOp(Node->getOperand(i));
4267 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4268 // The only option for this node is to custom lower it.
4269 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004270 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004271
4272 // Since trampoline produces two values, make sure to remember that we
4273 // legalized both of them.
4274 Tmp1 = LegalizeOp(Result.getValue(1));
4275 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004276 AddLegalizedOperand(SDValue(Node, 0), Result);
4277 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004278 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004279 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004280 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004281 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004282 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4283 default: assert(0 && "This action not supported for this op yet!");
4284 case TargetLowering::Custom:
4285 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004286 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004287 // Fall Thru
4288 case TargetLowering::Legal:
4289 // If this operation is not supported, lower it to constant 1
4290 Result = DAG.getConstant(1, VT);
4291 break;
4292 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004293 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004294 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004295 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004296 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004297 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4298 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004299 case TargetLowering::Legal:
4300 Tmp1 = LegalizeOp(Node->getOperand(0));
4301 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4302 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004303 case TargetLowering::Custom:
4304 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004305 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004306 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004307 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004308 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004309 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004310 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004311 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004312 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004313 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004314 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner034f12e2008-01-15 22:09:33 +00004315 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004316 Result = CallResult.second;
4317 break;
4318 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004319 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004320 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004321
Bill Wendling74c37652008-12-09 22:08:41 +00004322 case ISD::SADDO:
4323 case ISD::SSUBO: {
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004324 MVT VT = Node->getValueType(0);
4325 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4326 default: assert(0 && "This action not supported for this op yet!");
4327 case TargetLowering::Custom:
4328 Result = TLI.LowerOperation(Op, DAG);
4329 if (Result.getNode()) break;
4330 // FALLTHROUGH
4331 case TargetLowering::Legal: {
4332 SDValue LHS = LegalizeOp(Node->getOperand(0));
4333 SDValue RHS = LegalizeOp(Node->getOperand(1));
4334
Bill Wendling74c37652008-12-09 22:08:41 +00004335 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::SADDO ?
4336 ISD::ADD : ISD::SUB, LHS.getValueType(),
4337 LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004338 MVT OType = Node->getValueType(1);
4339
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004340 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004341
Bill Wendling740464e2008-11-25 19:40:17 +00004342 // LHSSign -> LHS >= 0
4343 // RHSSign -> RHS >= 0
4344 // SumSign -> Sum >= 0
4345 //
Bill Wendling74c37652008-12-09 22:08:41 +00004346 // Add:
Bill Wendling740464e2008-11-25 19:40:17 +00004347 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
Bill Wendling74c37652008-12-09 22:08:41 +00004348 // Sub:
4349 // Overflow -> (LHSSign != RHSSign) && (LHSSign != SumSign)
Bill Wendling740464e2008-11-25 19:40:17 +00004350 //
4351 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4352 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
Bill Wendling74c37652008-12-09 22:08:41 +00004353 SDValue SignsMatch = DAG.getSetCC(OType, LHSSign, RHSSign,
4354 Node->getOpcode() == ISD::SADDO ?
4355 ISD::SETEQ : ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004356
Bill Wendling740464e2008-11-25 19:40:17 +00004357 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4358 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004359
Bill Wendling74c37652008-12-09 22:08:41 +00004360 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsMatch, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004361
4362 MVT ValueVTs[] = { LHS.getValueType(), OType };
4363 SDValue Ops[] = { Sum, Cmp };
4364
Duncan Sandsaaffa052008-12-01 11:41:29 +00004365 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4366 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004367 SDNode *RNode = Result.getNode();
4368 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4369 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4370 break;
4371 }
4372 }
4373
4374 break;
4375 }
Bill Wendling74c37652008-12-09 22:08:41 +00004376 case ISD::UADDO:
4377 case ISD::USUBO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004378 MVT VT = Node->getValueType(0);
4379 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4380 default: assert(0 && "This action not supported for this op yet!");
4381 case TargetLowering::Custom:
4382 Result = TLI.LowerOperation(Op, DAG);
4383 if (Result.getNode()) break;
4384 // FALLTHROUGH
4385 case TargetLowering::Legal: {
4386 SDValue LHS = LegalizeOp(Node->getOperand(0));
4387 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004388
Bill Wendling74c37652008-12-09 22:08:41 +00004389 SDValue Sum = DAG.getNode(Node->getOpcode() == ISD::UADDO ?
4390 ISD::ADD : ISD::SUB, LHS.getValueType(),
4391 LHS, RHS);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004392 MVT OType = Node->getValueType(1);
Bill Wendling74c37652008-12-09 22:08:41 +00004393 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS,
4394 Node->getOpcode () == ISD::UADDO ?
4395 ISD::SETULT : ISD::SETUGT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004396
Bill Wendling41ea7e72008-11-24 19:21:46 +00004397 MVT ValueVTs[] = { LHS.getValueType(), OType };
4398 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004399
Duncan Sandsaaffa052008-12-01 11:41:29 +00004400 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4401 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004402 SDNode *RNode = Result.getNode();
4403 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4404 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4405 break;
4406 }
4407 }
4408
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004409 break;
4410 }
Bill Wendling74c37652008-12-09 22:08:41 +00004411 case ISD::SMULO:
4412 case ISD::UMULO: {
4413 MVT VT = Node->getValueType(0);
4414 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4415 default: assert(0 && "This action is not supported at all!");
4416 case TargetLowering::Custom:
4417 Result = TLI.LowerOperation(Op, DAG);
4418 if (Result.getNode()) break;
4419 // Fall Thru
4420 case TargetLowering::Legal:
4421 // FIXME: According to Hacker's Delight, this can be implemented in
4422 // target independent lowering, but it would be inefficient, since it
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00004423 // requires a division + a branch.
Bill Wendling74c37652008-12-09 22:08:41 +00004424 assert(0 && "Target independent lowering is not supported for SMULO/UMULO!");
4425 break;
4426 }
4427 break;
4428 }
4429
Chris Lattner45b8caf2005-01-15 07:15:18 +00004430 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004431
Chris Lattner4ddd2832006-04-08 04:13:17 +00004432 assert(Result.getValueType() == Op.getValueType() &&
4433 "Bad legalization!");
4434
Chris Lattner456a93a2006-01-28 07:39:30 +00004435 // Make sure that the generated code is itself legal.
4436 if (Result != Op)
4437 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004438
Chris Lattner45982da2005-05-12 16:53:42 +00004439 // Note that LegalizeOp may be reentered even from single-use nodes, which
4440 // means that we always must cache transformed nodes.
4441 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004442 return Result;
4443}
4444
Chris Lattner8b6fa222005-01-15 22:16:26 +00004445/// PromoteOp - Given an operation that produces a value in an invalid type,
4446/// promote it to compute the value into a larger type. The produced value will
4447/// have the correct bits for the low portion of the register, but no guarantee
4448/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004449SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004450 MVT VT = Op.getValueType();
4451 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004452 assert(getTypeAction(VT) == Promote &&
4453 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004454 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004455 "Cannot promote to smaller type!");
4456
Dan Gohman475871a2008-07-27 21:46:04 +00004457 SDValue Tmp1, Tmp2, Tmp3;
4458 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004459 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004460
Dan Gohman475871a2008-07-27 21:46:04 +00004461 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004462 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004463
Chris Lattner03c85462005-01-15 05:21:40 +00004464 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004465 case ISD::CopyFromReg:
4466 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004467 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004468#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004469 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004470#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004471 assert(0 && "Do not know how to promote this operator!");
4472 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004473 case ISD::UNDEF:
4474 Result = DAG.getNode(ISD::UNDEF, NVT);
4475 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004476 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004477 if (VT != MVT::i1)
4478 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4479 else
4480 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004481 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4482 break;
4483 case ISD::ConstantFP:
4484 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4485 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4486 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004487
Chris Lattner82fbfb62005-01-18 02:59:52 +00004488 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004489 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004490 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004491 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004492 TLI.getSetCCResultType(Node->getOperand(0)),
4493 Node->getOperand(0), Node->getOperand(1),
4494 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004495 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004496
Chris Lattner03c85462005-01-15 05:21:40 +00004497 case ISD::TRUNCATE:
4498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4499 case Legal:
4500 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004501 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004502 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004503 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004504 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4505 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004506 case Promote:
4507 // The truncation is not required, because we don't guarantee anything
4508 // about high bits anyway.
4509 Result = PromoteOp(Node->getOperand(0));
4510 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004511 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004512 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4513 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004514 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004515 }
4516 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004517 case ISD::SIGN_EXTEND:
4518 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004519 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004520 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4521 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4522 case Legal:
4523 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004524 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004525 break;
4526 case Promote:
4527 // Promote the reg if it's smaller.
4528 Result = PromoteOp(Node->getOperand(0));
4529 // The high bits are not guaranteed to be anything. Insert an extend.
4530 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004531 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004532 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004533 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004534 Result = DAG.getZeroExtendInReg(Result,
4535 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004536 break;
4537 }
4538 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004539 case ISD::CONVERT_RNDSAT: {
4540 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4541 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4542 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4543 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4544 "can only promote integers");
4545 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4546 Node->getOperand(1), Node->getOperand(2),
4547 Node->getOperand(3), Node->getOperand(4),
4548 CvtCode);
4549 break;
4550
4551 }
Chris Lattner35481892005-12-23 00:16:34 +00004552 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004553 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4554 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004555 Result = PromoteOp(Result);
4556 break;
4557
Chris Lattner8b6fa222005-01-15 22:16:26 +00004558 case ISD::FP_EXTEND:
4559 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4560 case ISD::FP_ROUND:
4561 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4562 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4563 case Promote: assert(0 && "Unreachable with 2 FP types!");
4564 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004565 if (Node->getConstantOperandVal(1) == 0) {
4566 // Input is legal? Do an FP_ROUND_INREG.
4567 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4568 DAG.getValueType(VT));
4569 } else {
4570 // Just remove the truncate, it isn't affecting the value.
4571 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4572 Node->getOperand(1));
4573 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004574 break;
4575 }
4576 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004577 case ISD::SINT_TO_FP:
4578 case ISD::UINT_TO_FP:
4579 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4580 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004581 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004582 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004583 break;
4584
4585 case Promote:
4586 Result = PromoteOp(Node->getOperand(0));
4587 if (Node->getOpcode() == ISD::SINT_TO_FP)
4588 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004589 Result,
4590 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004591 else
Chris Lattner23993562005-04-13 02:38:47 +00004592 Result = DAG.getZeroExtendInReg(Result,
4593 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004594 // No extra round required here.
4595 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004596 break;
4597 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004598 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4599 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004600 // Round if we cannot tolerate excess precision.
4601 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004602 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4603 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004604 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004605 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004606 break;
4607
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004608 case ISD::SIGN_EXTEND_INREG:
4609 Result = PromoteOp(Node->getOperand(0));
4610 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4611 Node->getOperand(1));
4612 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004613 case ISD::FP_TO_SINT:
4614 case ISD::FP_TO_UINT:
4615 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4616 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004617 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004618 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004619 break;
4620 case Promote:
4621 // The input result is prerounded, so we don't have to do anything
4622 // special.
4623 Tmp1 = PromoteOp(Node->getOperand(0));
4624 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004625 }
Nate Begemand2558e32005-08-14 01:20:53 +00004626 // If we're promoting a UINT to a larger size, check to see if the new node
4627 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4628 // we can use that instead. This allows us to generate better code for
4629 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4630 // legal, such as PowerPC.
4631 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004632 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004633 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4634 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004635 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4636 } else {
4637 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4638 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004639 break;
4640
Chris Lattner2c8086f2005-04-02 05:00:07 +00004641 case ISD::FABS:
4642 case ISD::FNEG:
4643 Tmp1 = PromoteOp(Node->getOperand(0));
4644 assert(Tmp1.getValueType() == NVT);
4645 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4646 // NOTE: we do not have to do any extra rounding here for
4647 // NoExcessFPPrecision, because we know the input will have the appropriate
4648 // precision, and these operations don't modify precision at all.
4649 break;
4650
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004651 case ISD::FLOG:
4652 case ISD::FLOG2:
4653 case ISD::FLOG10:
4654 case ISD::FEXP:
4655 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004656 case ISD::FSQRT:
4657 case ISD::FSIN:
4658 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004659 case ISD::FTRUNC:
4660 case ISD::FFLOOR:
4661 case ISD::FCEIL:
4662 case ISD::FRINT:
4663 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004664 Tmp1 = PromoteOp(Node->getOperand(0));
4665 assert(Tmp1.getValueType() == NVT);
4666 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004667 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004668 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4669 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004670 break;
4671
Evan Cheng9d24ac52008-09-09 23:35:53 +00004672 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004673 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004674 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004675 // directly as well, which may be better.
4676 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004677 Tmp2 = Node->getOperand(1);
4678 if (Node->getOpcode() == ISD::FPOW)
4679 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004680 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004681 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004682 if (NoExcessFPPrecision)
4683 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4684 DAG.getValueType(VT));
4685 break;
4686 }
4687
Dale Johannesene00a8a22008-08-28 02:44:49 +00004688 case ISD::ATOMIC_CMP_SWAP_8:
4689 case ISD::ATOMIC_CMP_SWAP_16:
4690 case ISD::ATOMIC_CMP_SWAP_32:
4691 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004692 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004693 Tmp2 = PromoteOp(Node->getOperand(2));
4694 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang28873102008-06-25 08:15:39 +00004695 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4696 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004697 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004698 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004699 // Remember that we legalized the chain.
4700 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4701 break;
4702 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00004703 case ISD::ATOMIC_LOAD_ADD_8:
4704 case ISD::ATOMIC_LOAD_SUB_8:
4705 case ISD::ATOMIC_LOAD_AND_8:
4706 case ISD::ATOMIC_LOAD_OR_8:
4707 case ISD::ATOMIC_LOAD_XOR_8:
4708 case ISD::ATOMIC_LOAD_NAND_8:
4709 case ISD::ATOMIC_LOAD_MIN_8:
4710 case ISD::ATOMIC_LOAD_MAX_8:
4711 case ISD::ATOMIC_LOAD_UMIN_8:
4712 case ISD::ATOMIC_LOAD_UMAX_8:
4713 case ISD::ATOMIC_SWAP_8:
4714 case ISD::ATOMIC_LOAD_ADD_16:
4715 case ISD::ATOMIC_LOAD_SUB_16:
4716 case ISD::ATOMIC_LOAD_AND_16:
4717 case ISD::ATOMIC_LOAD_OR_16:
4718 case ISD::ATOMIC_LOAD_XOR_16:
4719 case ISD::ATOMIC_LOAD_NAND_16:
4720 case ISD::ATOMIC_LOAD_MIN_16:
4721 case ISD::ATOMIC_LOAD_MAX_16:
4722 case ISD::ATOMIC_LOAD_UMIN_16:
4723 case ISD::ATOMIC_LOAD_UMAX_16:
4724 case ISD::ATOMIC_SWAP_16:
4725 case ISD::ATOMIC_LOAD_ADD_32:
4726 case ISD::ATOMIC_LOAD_SUB_32:
4727 case ISD::ATOMIC_LOAD_AND_32:
4728 case ISD::ATOMIC_LOAD_OR_32:
4729 case ISD::ATOMIC_LOAD_XOR_32:
4730 case ISD::ATOMIC_LOAD_NAND_32:
4731 case ISD::ATOMIC_LOAD_MIN_32:
4732 case ISD::ATOMIC_LOAD_MAX_32:
4733 case ISD::ATOMIC_LOAD_UMIN_32:
4734 case ISD::ATOMIC_LOAD_UMAX_32:
4735 case ISD::ATOMIC_SWAP_32:
4736 case ISD::ATOMIC_LOAD_ADD_64:
4737 case ISD::ATOMIC_LOAD_SUB_64:
4738 case ISD::ATOMIC_LOAD_AND_64:
4739 case ISD::ATOMIC_LOAD_OR_64:
4740 case ISD::ATOMIC_LOAD_XOR_64:
4741 case ISD::ATOMIC_LOAD_NAND_64:
4742 case ISD::ATOMIC_LOAD_MIN_64:
4743 case ISD::ATOMIC_LOAD_MAX_64:
4744 case ISD::ATOMIC_LOAD_UMIN_64:
4745 case ISD::ATOMIC_LOAD_UMAX_64:
4746 case ISD::ATOMIC_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004747 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004748 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00004749 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4750 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004751 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004752 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004753 // Remember that we legalized the chain.
4754 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4755 break;
4756 }
4757
Chris Lattner03c85462005-01-15 05:21:40 +00004758 case ISD::AND:
4759 case ISD::OR:
4760 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004761 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004762 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004763 case ISD::MUL:
4764 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004765 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004766 // that too is okay if they are integer operations.
4767 Tmp1 = PromoteOp(Node->getOperand(0));
4768 Tmp2 = PromoteOp(Node->getOperand(1));
4769 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4770 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004771 break;
4772 case ISD::FADD:
4773 case ISD::FSUB:
4774 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004775 Tmp1 = PromoteOp(Node->getOperand(0));
4776 Tmp2 = PromoteOp(Node->getOperand(1));
4777 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4778 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4779
4780 // Floating point operations will give excess precision that we may not be
4781 // able to tolerate. If we DO allow excess precision, just leave it,
4782 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004783 // FIXME: Why would we need to round FP ops more than integer ones?
4784 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004785 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004786 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4787 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004788 break;
4789
Chris Lattner8b6fa222005-01-15 22:16:26 +00004790 case ISD::SDIV:
4791 case ISD::SREM:
4792 // These operators require that their input be sign extended.
4793 Tmp1 = PromoteOp(Node->getOperand(0));
4794 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004795 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004796 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4797 DAG.getValueType(VT));
4798 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4799 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004800 }
4801 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4802
4803 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004804 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004805 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4806 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004807 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004808 case ISD::FDIV:
4809 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004810 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004811 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004812 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004813 case Expand: assert(0 && "not implemented");
4814 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4815 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004816 }
4817 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004818 case Expand: assert(0 && "not implemented");
4819 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4820 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004821 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004822 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4823
4824 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004825 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004826 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4827 DAG.getValueType(VT));
4828 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004829
4830 case ISD::UDIV:
4831 case ISD::UREM:
4832 // These operators require that their input be zero extended.
4833 Tmp1 = PromoteOp(Node->getOperand(0));
4834 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004835 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004836 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4837 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004838 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4839 break;
4840
4841 case ISD::SHL:
4842 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004843 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004844 break;
4845 case ISD::SRA:
4846 // The input value must be properly sign extended.
4847 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004848 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4849 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004850 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004851 break;
4852 case ISD::SRL:
4853 // The input value must be properly zero extended.
4854 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004855 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004856 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004857 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004858
4859 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004860 Tmp1 = Node->getOperand(0); // Get the chain.
4861 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004862 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4863 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004864 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004865 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004866 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004867 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004868 // Increment the pointer, VAList, to the next vaarg
4869 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004870 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004871 TLI.getPointerTy()));
4872 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004873 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004874 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004875 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004876 }
4877 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004878 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004879 break;
4880
Evan Cheng466685d2006-10-09 20:57:25 +00004881 case ISD::LOAD: {
4882 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004883 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4884 ? ISD::EXTLOAD : LD->getExtensionType();
4885 Result = DAG.getExtLoad(ExtType, NVT,
4886 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004887 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004888 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004889 LD->isVolatile(),
4890 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004891 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004892 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004893 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004894 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004895 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004896 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4897 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004898
Duncan Sands83ec4b62008-06-06 12:08:01 +00004899 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004900 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004901 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4902 // Ensure that the resulting node is at least the same size as the operands'
4903 // value types, because we cannot assume that TLI.getSetCCValueType() is
4904 // constant.
4905 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004906 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004907 }
Nate Begeman9373a812005-08-10 20:51:12 +00004908 case ISD::SELECT_CC:
4909 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4910 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4911 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004912 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004913 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004914 case ISD::BSWAP:
4915 Tmp1 = Node->getOperand(0);
4916 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4917 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4918 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004919 DAG.getConstant(NVT.getSizeInBits() -
4920 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004921 TLI.getShiftAmountTy()));
4922 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004923 case ISD::CTPOP:
4924 case ISD::CTTZ:
4925 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004926 // Zero extend the argument
4927 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004928 // Perform the larger operation, then subtract if needed.
4929 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004930 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004931 case ISD::CTPOP:
4932 Result = Tmp1;
4933 break;
4934 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004935 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004936 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004937 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004938 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004939 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004940 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004941 break;
4942 case ISD::CTLZ:
4943 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004944 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004945 DAG.getConstant(NVT.getSizeInBits() -
4946 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004947 break;
4948 }
4949 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004950 case ISD::EXTRACT_SUBVECTOR:
4951 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004952 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004953 case ISD::EXTRACT_VECTOR_ELT:
4954 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4955 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004956 }
4957
Gabor Greifba36cb52008-08-28 21:40:38 +00004958 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004959
4960 // Make sure the result is itself legal.
4961 Result = LegalizeOp(Result);
4962
4963 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004964 AddPromotedOperand(Op, Result);
4965 return Result;
4966}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004967
Dan Gohman7f321562007-06-25 16:23:39 +00004968/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4969/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4970/// based on the vector type. The return type of this matches the element type
4971/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004972SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004973 // We know that operand #0 is the Vec vector. If the index is a constant
4974 // or if the invec is a supported hardware type, we can use it. Otherwise,
4975 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004976 SDValue Vec = Op.getOperand(0);
4977 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004978
Duncan Sands83ec4b62008-06-06 12:08:01 +00004979 MVT TVT = Vec.getValueType();
4980 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004981
Dan Gohman7f321562007-06-25 16:23:39 +00004982 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4983 default: assert(0 && "This action is not supported yet!");
4984 case TargetLowering::Custom: {
4985 Vec = LegalizeOp(Vec);
4986 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004987 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004988 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004989 return Tmp3;
4990 break;
4991 }
4992 case TargetLowering::Legal:
4993 if (isTypeLegal(TVT)) {
4994 Vec = LegalizeOp(Vec);
4995 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004996 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004997 }
4998 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004999 case TargetLowering::Promote:
5000 assert(TVT.isVector() && "not vector type");
5001 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00005002 case TargetLowering::Expand:
5003 break;
5004 }
5005
5006 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00005007 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005008 Op = ScalarizeVectorOp(Vec);
5009 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00005010 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00005011 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005012 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00005013 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005014 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00005015 Vec = Lo;
5016 } else {
5017 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005018 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00005019 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00005020 }
Dan Gohman7f321562007-06-25 16:23:39 +00005021
Chris Lattner15972212006-03-31 17:55:51 +00005022 // It's now an extract from the appropriate high or low part. Recurse.
5023 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005024 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00005025 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00005026 // Store the value to a temporary stack slot, then LOAD the scalar
5027 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005028 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
5029 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00005030
5031 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005032 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00005033 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
5034 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005035
Duncan Sands8e4eb092008-06-08 20:54:56 +00005036 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00005037 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005038 else
Chris Lattnerf185e672007-10-19 16:47:35 +00005039 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00005040
Dan Gohman7f321562007-06-25 16:23:39 +00005041 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
5042
5043 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00005044 }
Dan Gohman7f321562007-06-25 16:23:39 +00005045 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00005046}
5047
Dan Gohman7f321562007-06-25 16:23:39 +00005048/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00005049/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005050SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00005051 // We know that operand #0 is the Vec vector. For now we assume the index
5052 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00005053 SDValue Vec = Op.getOperand(0);
5054 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00005055
Duncan Sands83ec4b62008-06-06 12:08:01 +00005056 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00005057
Duncan Sands83ec4b62008-06-06 12:08:01 +00005058 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00005059 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00005060 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00005061 }
5062
5063 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00005064 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00005065 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005066 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00005067 Vec = Lo;
5068 } else {
5069 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005070 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
5071 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00005072 }
5073
5074 // It's now an extract from the appropriate high or low part. Recurse.
5075 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00005076 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00005077}
5078
Nate Begeman750ac1b2006-02-01 07:19:44 +00005079/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
5080/// with condition CC on the current target. This usually involves legalizing
5081/// or promoting the arguments. In the case where LHS and RHS must be expanded,
5082/// there may be no choice but to create a new SetCC node to represent the
5083/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00005084/// LHS, and the SDValue returned in RHS has a nil SDNode value.
5085void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
5086 SDValue &RHS,
5087 SDValue &CC) {
5088 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005089
5090 switch (getTypeAction(LHS.getValueType())) {
5091 case Legal:
5092 Tmp1 = LegalizeOp(LHS); // LHS
5093 Tmp2 = LegalizeOp(RHS); // RHS
5094 break;
5095 case Promote:
5096 Tmp1 = PromoteOp(LHS); // LHS
5097 Tmp2 = PromoteOp(RHS); // RHS
5098
5099 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005100 if (LHS.getValueType().isInteger()) {
5101 MVT VT = LHS.getValueType();
5102 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00005103
5104 // Otherwise, we have to insert explicit sign or zero extends. Note
5105 // that we could insert sign extends for ALL conditions, but zero extend
5106 // is cheaper on many machines (an AND instead of two shifts), so prefer
5107 // it.
5108 switch (cast<CondCodeSDNode>(CC)->get()) {
5109 default: assert(0 && "Unknown integer comparison!");
5110 case ISD::SETEQ:
5111 case ISD::SETNE:
5112 case ISD::SETUGE:
5113 case ISD::SETUGT:
5114 case ISD::SETULE:
5115 case ISD::SETULT:
5116 // ALL of these operations will work if we either sign or zero extend
5117 // the operands (including the unsigned comparisons!). Zero extend is
5118 // usually a simpler/cheaper operation, so prefer it.
5119 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5120 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5121 break;
5122 case ISD::SETGE:
5123 case ISD::SETGT:
5124 case ISD::SETLT:
5125 case ISD::SETLE:
5126 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5127 DAG.getValueType(VT));
5128 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5129 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00005130 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5131 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00005132 break;
5133 }
5134 }
5135 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005136 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005137 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00005138 if (VT == MVT::f32 || VT == MVT::f64) {
5139 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00005140 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00005141 switch (cast<CondCodeSDNode>(CC)->get()) {
5142 case ISD::SETEQ:
5143 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00005144 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005145 break;
5146 case ISD::SETNE:
5147 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00005148 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005149 break;
5150 case ISD::SETGE:
5151 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00005152 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005153 break;
5154 case ISD::SETLT:
5155 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005156 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005157 break;
5158 case ISD::SETLE:
5159 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005160 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005161 break;
5162 case ISD::SETGT:
5163 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005164 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005165 break;
5166 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005167 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5168 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005169 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005170 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005171 break;
5172 default:
Evan Cheng56966222007-01-12 02:11:51 +00005173 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005174 switch (cast<CondCodeSDNode>(CC)->get()) {
5175 case ISD::SETONE:
5176 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005177 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005178 // Fallthrough
5179 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005180 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005181 break;
5182 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005183 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005184 break;
5185 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005186 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005187 break;
5188 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005189 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005190 break;
Evan Cheng56966222007-01-12 02:11:51 +00005191 case ISD::SETUEQ:
5192 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005193 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005194 default: assert(0 && "Unsupported FP setcc!");
5195 }
5196 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005197
Dan Gohman475871a2008-07-27 21:46:04 +00005198 SDValue Dummy;
5199 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00005200 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005201 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005202 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005203 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005204 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005205 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00005206 CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00005207 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005208 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005209 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00005210 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00005211 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005212 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005213 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005214 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005215 RHS = Tmp2;
5216 return;
5217 }
5218
Dan Gohman475871a2008-07-27 21:46:04 +00005219 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005220 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005221 ExpandOp(RHS, RHSLo, RHSHi);
5222 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5223
5224 if (VT==MVT::ppcf128) {
5225 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005226 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005227 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005228 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005229 // The following can be improved, but not that much.
Dale Johannesene2f20832008-09-12 00:30:56 +00005230 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5231 ISD::SETOEQ);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005232 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005233 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesene2f20832008-09-12 00:30:56 +00005234 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5235 ISD::SETUNE);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005236 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005237 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5238 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005239 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005240 break;
5241 }
5242
5243 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005244 case ISD::SETEQ:
5245 case ISD::SETNE:
5246 if (RHSLo == RHSHi)
5247 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5248 if (RHSCST->isAllOnesValue()) {
5249 // Comparison to -1.
5250 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5251 Tmp2 = RHSLo;
5252 break;
5253 }
5254
5255 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5256 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5257 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5258 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5259 break;
5260 default:
5261 // If this is a comparison of the sign bit, just look at the top part.
5262 // X > -1, x < 0
5263 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5264 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005265 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005266 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5267 CST->isAllOnesValue())) { // X > -1
5268 Tmp1 = LHSHi;
5269 Tmp2 = RHSHi;
5270 break;
5271 }
5272
5273 // FIXME: This generated code sucks.
5274 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005275 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005276 default: assert(0 && "Unknown integer setcc!");
5277 case ISD::SETLT:
5278 case ISD::SETULT: LowCC = ISD::SETULT; break;
5279 case ISD::SETGT:
5280 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5281 case ISD::SETLE:
5282 case ISD::SETULE: LowCC = ISD::SETULE; break;
5283 case ISD::SETGE:
5284 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5285 }
5286
5287 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5288 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5289 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5290
5291 // NOTE: on targets without efficient SELECT of bools, we can always use
5292 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005293 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005294 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00005295 LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005296 if (!Tmp1.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005297 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5298 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005299 CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005300 if (!Tmp2.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005301 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005302 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005303
Gabor Greifba36cb52008-08-28 21:40:38 +00005304 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5305 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005306 if ((Tmp1C && Tmp1C->isNullValue()) ||
5307 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005308 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5309 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005310 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005311 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5312 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5313 // low part is known false, returns high part.
5314 // For LE / GE, if high part is known false, ignore the low part.
5315 // For LT / GT, if high part is known true, ignore the low part.
5316 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005317 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005318 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005319 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005320 ISD::SETEQ, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005321 if (!Result.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005322 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005323 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005324 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5325 Result, Tmp1, Tmp2));
5326 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005327 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005328 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005329 }
5330 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005331 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005332 LHS = Tmp1;
5333 RHS = Tmp2;
5334}
5335
Evan Cheng7f042682008-10-15 02:05:31 +00005336/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5337/// condition code CC on the current target. This routine assumes LHS and rHS
5338/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5339/// illegal condition code into AND / OR of multiple SETCC values.
5340void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5341 SDValue &LHS, SDValue &RHS,
5342 SDValue &CC) {
5343 MVT OpVT = LHS.getValueType();
5344 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5345 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5346 default: assert(0 && "Unknown condition code action!");
5347 case TargetLowering::Legal:
5348 // Nothing to do.
5349 break;
5350 case TargetLowering::Expand: {
5351 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5352 unsigned Opc = 0;
5353 switch (CCCode) {
5354 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005355 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5356 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5357 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5358 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5359 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5360 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5361 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5362 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5363 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5364 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5365 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5366 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005367 // FIXME: Implement more expansions.
5368 }
5369
5370 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5371 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5372 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5373 RHS = SDValue();
5374 CC = SDValue();
5375 break;
5376 }
5377 }
5378}
5379
Chris Lattner1401d152008-01-16 07:45:30 +00005380/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5381/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5382/// a load from the stack slot to DestVT, extending it if needed.
5383/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005384SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5385 MVT SlotVT,
5386 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005387 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005388 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5389 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005390 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005391
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005392 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005393 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00005394
Duncan Sands83ec4b62008-06-06 12:08:01 +00005395 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5396 unsigned SlotSize = SlotVT.getSizeInBits();
5397 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005398 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5399 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005400
Chris Lattner1401d152008-01-16 07:45:30 +00005401 // Emit a store to the stack slot. Use a truncstore if the input value is
5402 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005404
Chris Lattner1401d152008-01-16 07:45:30 +00005405 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005406 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005407 PseudoSourceValue::getFixedStack(SPFI), 0,
5408 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005409 else {
5410 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005411 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005412 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00005413 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005414 }
5415
Chris Lattner35481892005-12-23 00:16:34 +00005416 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005417 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00005418 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005419
5420 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00005421 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5422 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005423}
5424
Dan Gohman475871a2008-07-27 21:46:04 +00005425SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005426 // Create a vector sized/aligned stack slot, store the value to element #0,
5427 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005428 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005429
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005430 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005431 int SPFI = StackPtrFI->getIndex();
5432
Dan Gohman475871a2008-07-27 21:46:04 +00005433 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005434 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005435 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005436 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005437}
5438
5439
Chris Lattnerce872152006-03-19 06:31:19 +00005440/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005441/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005442SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005443
5444 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005445 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005446 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005447 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005448 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005449
Dan Gohman475871a2008-07-27 21:46:04 +00005450 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005451 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005452 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005453 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005454 bool isConstant = true;
5455 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5456 SplatValue.getOpcode() != ISD::UNDEF)
5457 isConstant = false;
5458
Evan Cheng033e6812006-03-24 01:17:21 +00005459 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005460 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005461 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005462 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005463 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005464 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005465 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005466
5467 // If this isn't a constant element or an undef, we can't use a constant
5468 // pool load.
5469 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5470 V.getOpcode() != ISD::UNDEF)
5471 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005472 }
5473
5474 if (isOnlyLowElement) {
5475 // If the low element is an undef too, then this whole things is an undef.
5476 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5477 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5478 // Otherwise, turn this into a scalar_to_vector node.
5479 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5480 Node->getOperand(0));
5481 }
5482
Chris Lattner2eb86532006-03-24 07:29:17 +00005483 // If all elements are constants, create a load from the constant pool.
5484 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005485 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005486 std::vector<Constant*> CV;
5487 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5488 if (ConstantFPSDNode *V =
5489 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005490 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005491 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005492 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005493 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005494 } else {
5495 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005496 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005497 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005498 CV.push_back(UndefValue::get(OpNTy));
5499 }
5500 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005501 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005502 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005503 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005504 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005505 PseudoSourceValue::getConstantPool(), 0,
5506 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005507 }
5508
Gabor Greifba36cb52008-08-28 21:40:38 +00005509 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005510 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005511 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005512 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5513 std::vector<SDValue> ZeroVec(NumElems, Zero);
5514 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005515 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005516
5517 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005518 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005519 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005520 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005521 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5522
5523 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5524 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5525 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5526 SplatMask);
5527 }
5528 }
5529
Evan Cheng033e6812006-03-24 01:17:21 +00005530 // If there are only two unique elements, we may be able to turn this into a
5531 // vector shuffle.
5532 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005533 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005534 SDValue Val1 = Node->getOperand(1);
5535 SDValue Val2;
5536 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005537 if (MI->first != Val1)
5538 Val2 = MI->first;
5539 else
5540 Val2 = (++MI)->first;
5541
5542 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5543 // vector shuffle has the undef vector on the RHS.
5544 if (Val1.getOpcode() == ISD::UNDEF)
5545 std::swap(Val1, Val2);
5546
Evan Cheng033e6812006-03-24 01:17:21 +00005547 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005548 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5549 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005550 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005551
5552 // Set elements of the shuffle mask for Val1.
5553 std::vector<unsigned> &Val1Elts = Values[Val1];
5554 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5555 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5556
5557 // Set elements of the shuffle mask for Val2.
5558 std::vector<unsigned> &Val2Elts = Values[Val2];
5559 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5560 if (Val2.getOpcode() != ISD::UNDEF)
5561 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5562 else
5563 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5564
Dan Gohman475871a2008-07-27 21:46:04 +00005565 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005566 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005567
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005568 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005569 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5570 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005571 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5572 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005573 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005574
5575 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005576 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005577 }
5578 }
Chris Lattnerce872152006-03-19 06:31:19 +00005579
5580 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5581 // aligned object on the stack, store each element into it, then load
5582 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005583 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005584 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005585 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005586
5587 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005588 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005589 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005590 // Store (in the right endianness) the elements to memory.
5591 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5592 // Ignore undef elements.
5593 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5594
Chris Lattner841c8822006-03-22 01:46:54 +00005595 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005596
Dan Gohman475871a2008-07-27 21:46:04 +00005597 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005598 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5599
Evan Cheng786225a2006-10-05 23:01:46 +00005600 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005601 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005602 }
5603
Dan Gohman475871a2008-07-27 21:46:04 +00005604 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005605 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005606 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5607 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005608 else
5609 StoreChain = DAG.getEntryNode();
5610
5611 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005612 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005613}
5614
Chris Lattner5b359c62005-04-02 04:00:59 +00005615void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005616 SDValue Op, SDValue Amt,
5617 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005618 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005619 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005620 ExpandOp(Op, LHSL, LHSH);
5621
Dan Gohman475871a2008-07-27 21:46:04 +00005622 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005623 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005624 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005625 Hi = Lo.getValue(1);
5626}
5627
5628
Chris Lattnere34b3962005-01-19 04:19:40 +00005629/// ExpandShift - Try to find a clever way to expand this shift operation out to
5630/// smaller elements. If we can't find a way that is more efficient than a
5631/// libcall on this target, return false. Otherwise, return true with the
5632/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005633bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5634 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005635 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5636 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005637
Duncan Sands83ec4b62008-06-06 12:08:01 +00005638 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005639 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005640 MVT ShTy = ShAmt.getValueType();
5641 unsigned ShBits = ShTy.getSizeInBits();
5642 unsigned VTBits = Op.getValueType().getSizeInBits();
5643 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005644
Chris Lattner7a3c8552007-10-14 20:35:12 +00005645 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005646 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005647 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005648 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005649 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005650 ExpandOp(Op, InL, InH);
5651 switch(Opc) {
5652 case ISD::SHL:
5653 if (Cst > VTBits) {
5654 Lo = DAG.getConstant(0, NVT);
5655 Hi = DAG.getConstant(0, NVT);
5656 } else if (Cst > NVTBits) {
5657 Lo = DAG.getConstant(0, NVT);
5658 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005659 } else if (Cst == NVTBits) {
5660 Lo = DAG.getConstant(0, NVT);
5661 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005662 } else {
5663 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5664 Hi = DAG.getNode(ISD::OR, NVT,
5665 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5666 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5667 }
5668 return true;
5669 case ISD::SRL:
5670 if (Cst > VTBits) {
5671 Lo = DAG.getConstant(0, NVT);
5672 Hi = DAG.getConstant(0, NVT);
5673 } else if (Cst > NVTBits) {
5674 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5675 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005676 } else if (Cst == NVTBits) {
5677 Lo = InH;
5678 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005679 } else {
5680 Lo = DAG.getNode(ISD::OR, NVT,
5681 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5682 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5683 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5684 }
5685 return true;
5686 case ISD::SRA:
5687 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005688 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005689 DAG.getConstant(NVTBits-1, ShTy));
5690 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005691 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005692 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005693 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005694 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005695 } else if (Cst == NVTBits) {
5696 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005697 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005698 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005699 } else {
5700 Lo = DAG.getNode(ISD::OR, NVT,
5701 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5702 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5703 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5704 }
5705 return true;
5706 }
5707 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005708
5709 // Okay, the shift amount isn't constant. However, if we can tell that it is
5710 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005711 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5712 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005713 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005714
Dan Gohman9e255b72008-02-22 01:12:31 +00005715 // If we know that if any of the high bits of the shift amount are one, then
5716 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005717 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005718 // Mask out the high bit, which we know is set.
5719 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005720 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005721
5722 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005723 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005724 ExpandOp(Op, InL, InH);
5725 switch(Opc) {
5726 case ISD::SHL:
5727 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5728 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5729 return true;
5730 case ISD::SRL:
5731 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5732 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5733 return true;
5734 case ISD::SRA:
5735 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5736 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5737 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5738 return true;
5739 }
5740 }
5741
Dan Gohman9e255b72008-02-22 01:12:31 +00005742 // If we know that the high bits of the shift amount are all zero, then we can
5743 // do this as a couple of simple shifts.
5744 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005745 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005746 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005747 DAG.getConstant(NVTBits, Amt.getValueType()),
5748 Amt);
5749
5750 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005751 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005752 ExpandOp(Op, InL, InH);
5753 switch(Opc) {
5754 case ISD::SHL:
5755 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5756 Hi = DAG.getNode(ISD::OR, NVT,
5757 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5758 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5759 return true;
5760 case ISD::SRL:
5761 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5762 Lo = DAG.getNode(ISD::OR, NVT,
5763 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5764 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5765 return true;
5766 case ISD::SRA:
5767 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5768 Lo = DAG.getNode(ISD::OR, NVT,
5769 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5770 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5771 return true;
5772 }
5773 }
5774
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005775 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005776}
Chris Lattner77e77a62005-01-21 06:05:23 +00005777
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005778
Chris Lattner77e77a62005-01-21 06:05:23 +00005779// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5780// does not fit into a register, return the lo part and set the hi part to the
5781// by-reg argument. If it does fit into a single register, return the result
5782// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005783SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5784 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005785 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5786 // The input chain to this libcall is the entry node of the function.
5787 // Legalizing the call will automatically add the previous call to the
5788 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005789 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005790
Chris Lattner77e77a62005-01-21 06:05:23 +00005791 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005792 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005793 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005794 MVT ArgVT = Node->getOperand(i).getValueType();
5795 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005796 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005797 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005798 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005799 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005800 }
Bill Wendling056292f2008-09-16 21:48:12 +00005801 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005802 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005803
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005804 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005805 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005806 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005807 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5808 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005809
Chris Lattner6831a812006-02-13 09:18:02 +00005810 // Legalize the call sequence, starting with the chain. This will advance
5811 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5812 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5813 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005814 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005815 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005816 default: assert(0 && "Unknown thing");
5817 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005818 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005819 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005820 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005821 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005822 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005823 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005824 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005825}
5826
Dan Gohman7f8613e2008-08-14 20:04:46 +00005827/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5828///
5829SDValue SelectionDAGLegalize::
5830LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5831 bool isCustom = false;
5832 SDValue Tmp1;
5833 switch (getTypeAction(Op.getValueType())) {
5834 case Legal:
5835 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5836 Op.getValueType())) {
5837 default: assert(0 && "Unknown operation action!");
5838 case TargetLowering::Custom:
5839 isCustom = true;
5840 // FALLTHROUGH
5841 case TargetLowering::Legal:
5842 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005843 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005844 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5845 else
5846 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5847 DestTy, Tmp1);
5848 if (isCustom) {
5849 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005850 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005851 }
5852 break;
5853 case TargetLowering::Expand:
5854 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5855 break;
5856 case TargetLowering::Promote:
5857 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5858 break;
5859 }
5860 break;
5861 case Expand:
5862 Result = ExpandIntToFP(isSigned, DestTy, Op);
5863 break;
5864 case Promote:
5865 Tmp1 = PromoteOp(Op);
5866 if (isSigned) {
5867 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5868 Tmp1, DAG.getValueType(Op.getValueType()));
5869 } else {
5870 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5871 Op.getValueType());
5872 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005873 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005874 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5875 else
5876 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5877 DestTy, Tmp1);
5878 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5879 break;
5880 }
5881 return Result;
5882}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005883
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005884/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5885///
Dan Gohman475871a2008-07-27 21:46:04 +00005886SDValue SelectionDAGLegalize::
5887ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005888 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005889 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005890
Dan Gohman7f8613e2008-08-14 20:04:46 +00005891 // Expand unsupported int-to-fp vector casts by unrolling them.
5892 if (DestTy.isVector()) {
5893 if (!ExpandSource)
5894 return LegalizeOp(UnrollVectorOp(Source));
5895 MVT DestEltTy = DestTy.getVectorElementType();
5896 if (DestTy.getVectorNumElements() == 1) {
5897 SDValue Scalar = ScalarizeVectorOp(Source);
5898 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5899 DestEltTy, Scalar);
5900 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5901 }
5902 SDValue Lo, Hi;
5903 SplitVectorOp(Source, Lo, Hi);
5904 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5905 DestTy.getVectorNumElements() / 2);
5906 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5907 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengefa53392008-10-13 18:46:18 +00005908 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5909 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005910 }
5911
Evan Cheng9845eb52008-04-01 02:18:22 +00005912 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5913 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005914 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005915 // incoming integer is set. To handle this, we dynamically test to see if
5916 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005917 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005918 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005919 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005920 ExpandOp(Source, Lo, Hi);
5921 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5922 } else {
5923 // The comparison for the sign bit will use the entire operand.
5924 Hi = Source;
5925 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005926
Dale Johannesen53997b02008-11-04 20:52:49 +00005927 // Check to see if the target has a custom way to lower this. If so, use
5928 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005929 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5930 default: assert(0 && "This action not implemented for this operation!");
5931 case TargetLowering::Legal:
5932 case TargetLowering::Expand:
5933 break; // This case is handled below.
5934 case TargetLowering::Custom: {
5935 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5936 Source), DAG);
5937 if (NV.getNode())
5938 return LegalizeOp(NV);
5939 break; // The target decided this was legal after all
5940 }
5941 }
5942
Chris Lattner66de05b2005-05-13 04:45:13 +00005943 // If this is unsigned, and not supported, first perform the conversion to
5944 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005945 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005946
Dan Gohman475871a2008-07-27 21:46:04 +00005947 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005948 DAG.getConstant(0, Hi.getValueType()),
5949 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005950 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5951 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005952 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005953 uint64_t FF = 0x5f800000ULL;
5954 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005955 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005956
Dan Gohman475871a2008-07-27 21:46:04 +00005957 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005958 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere9c35e72005-04-13 05:09:42 +00005959 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005960 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005961 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005962 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005963 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005964 PseudoSourceValue::getConstantPool(), 0,
5965 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005966 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005967 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005968 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005969 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005970 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005971 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005972 else
5973 assert(0 && "Unexpected conversion");
5974
Duncan Sands83ec4b62008-06-06 12:08:01 +00005975 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005976 if (SCVT != DestTy) {
5977 // Destination type needs to be expanded as well. The FADD now we are
5978 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005979 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5980 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005981 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005982 SignedConv, SignedConv.getValue(1));
5983 }
5984 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5985 }
Chris Lattner473a9902005-09-29 06:44:39 +00005986 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005987 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005988
Chris Lattnera88a2602005-05-14 05:33:54 +00005989 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005990 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005991 default: assert(0 && "This action not implemented for this operation!");
5992 case TargetLowering::Legal:
5993 case TargetLowering::Expand:
5994 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005995 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005996 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005997 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005998 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005999 return LegalizeOp(NV);
6000 break; // The target decided this was legal after all
6001 }
Chris Lattnera88a2602005-05-14 05:33:54 +00006002 }
6003
Chris Lattner13689e22005-05-12 07:00:44 +00006004 // Expand the source, then glue it back together for the call. We must expand
6005 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00006006 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00006007 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00006008 ExpandOp(Source, SrcLo, SrcHi);
6009 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
6010 }
Chris Lattner13689e22005-05-12 07:00:44 +00006011
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006012 RTLIB::Libcall LC = isSigned ?
6013 RTLIB::getSINTTOFP(SourceVT, DestTy) :
6014 RTLIB::getUINTTOFP(SourceVT, DestTy);
6015 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
6016
Chris Lattner6831a812006-02-13 09:18:02 +00006017 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00006018 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00006019 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
6020 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmana2e94852008-03-10 23:03:31 +00006021 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
6022 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00006023}
Misha Brukmanedf128a2005-04-21 22:36:52 +00006024
Chris Lattner22cde6a2006-01-28 08:25:58 +00006025/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
6026/// INT_TO_FP operation of the specified operand when the target requests that
6027/// we expand it. At this point, we know that the result and operand types are
6028/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00006029SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
6030 SDValue Op0,
6031 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006032 if (Op0.getValueType() == MVT::i32) {
6033 // simple 32-bit [signed|unsigned] integer to float/double expansion
6034
Chris Lattner23594d42008-01-16 07:03:22 +00006035 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00006036 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00006037
Chris Lattner22cde6a2006-01-28 08:25:58 +00006038 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00006039 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00006040 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00006041 SDValue Hi = StackSlot;
6042 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00006043 if (TLI.isLittleEndian())
6044 std::swap(Hi, Lo);
6045
Chris Lattner22cde6a2006-01-28 08:25:58 +00006046 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00006047 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006048 if (isSigned) {
6049 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00006050 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006051 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
6052 } else {
6053 Op0Mapped = Op0;
6054 }
6055 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00006056 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00006057 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006058 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006059 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006060 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00006061 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006062 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00006063 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006064 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00006065 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00006066 BitsToDouble(0x4330000080000000ULL)
6067 : BitsToDouble(0x4330000000000000ULL),
6068 MVT::f64);
6069 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00006070 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006071 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00006072 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006073 // handle final rounding
6074 if (DestVT == MVT::f64) {
6075 // do nothing
6076 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00006077 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00006078 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
6079 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00006080 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00006081 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006082 }
6083 return Result;
6084 }
6085 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00006086 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006087
Dan Gohman475871a2008-07-27 21:46:04 +00006088 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006089 DAG.getConstant(0, Op0.getValueType()),
6090 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00006091 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
6092 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006093 SignSet, Four, Zero);
6094
6095 // If the sign bit of the integer is set, the large number will be treated
6096 // as a negative number. To counteract this, the dynamic code adds an
6097 // offset depending on the data type.
6098 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006099 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006100 default: assert(0 && "Unsupported integer type!");
6101 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
6102 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
6103 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
6104 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
6105 }
6106 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00006107 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006108
Dan Gohman475871a2008-07-27 21:46:04 +00006109 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00006110 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006111 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00006112 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00006113 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006114 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00006115 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00006116 PseudoSourceValue::getConstantPool(), 0,
6117 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006118 else {
Dan Gohman69de1932008-02-06 22:27:42 +00006119 FudgeInReg =
6120 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6121 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006122 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006123 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006124 }
6125
6126 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6127}
6128
6129/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6130/// *INT_TO_FP operation of the specified operand when the target requests that
6131/// we promote it. At this point, we know that the result and operand types are
6132/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6133/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00006134SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6135 MVT DestVT,
6136 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006137 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006138 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006139
6140 unsigned OpToUse = 0;
6141
6142 // Scan for the appropriate larger type to use.
6143 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006144 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6145 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006146
6147 // If the target supports SINT_TO_FP of this type, use it.
6148 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6149 default: break;
6150 case TargetLowering::Legal:
6151 if (!TLI.isTypeLegal(NewInTy))
6152 break; // Can't use this datatype.
6153 // FALL THROUGH.
6154 case TargetLowering::Custom:
6155 OpToUse = ISD::SINT_TO_FP;
6156 break;
6157 }
6158 if (OpToUse) break;
6159 if (isSigned) continue;
6160
6161 // If the target supports UINT_TO_FP of this type, use it.
6162 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6163 default: break;
6164 case TargetLowering::Legal:
6165 if (!TLI.isTypeLegal(NewInTy))
6166 break; // Can't use this datatype.
6167 // FALL THROUGH.
6168 case TargetLowering::Custom:
6169 OpToUse = ISD::UINT_TO_FP;
6170 break;
6171 }
6172 if (OpToUse) break;
6173
6174 // Otherwise, try a larger type.
6175 }
6176
6177 // Okay, we found the operation and type to use. Zero extend our input to the
6178 // desired type then run the operation on it.
6179 return DAG.getNode(OpToUse, DestVT,
6180 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6181 NewInTy, LegalOp));
6182}
6183
6184/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6185/// FP_TO_*INT operation of the specified operand when the target requests that
6186/// we promote it. At this point, we know that the result and operand types are
6187/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6188/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006189SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6190 MVT DestVT,
6191 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006192 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006193 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006194
6195 unsigned OpToUse = 0;
6196
6197 // Scan for the appropriate larger type to use.
6198 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006199 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6200 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006201
6202 // If the target supports FP_TO_SINT returning this type, use it.
6203 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6204 default: break;
6205 case TargetLowering::Legal:
6206 if (!TLI.isTypeLegal(NewOutTy))
6207 break; // Can't use this datatype.
6208 // FALL THROUGH.
6209 case TargetLowering::Custom:
6210 OpToUse = ISD::FP_TO_SINT;
6211 break;
6212 }
6213 if (OpToUse) break;
6214
6215 // If the target supports FP_TO_UINT of this type, use it.
6216 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6217 default: break;
6218 case TargetLowering::Legal:
6219 if (!TLI.isTypeLegal(NewOutTy))
6220 break; // Can't use this datatype.
6221 // FALL THROUGH.
6222 case TargetLowering::Custom:
6223 OpToUse = ISD::FP_TO_UINT;
6224 break;
6225 }
6226 if (OpToUse) break;
6227
6228 // Otherwise, try a larger type.
6229 }
6230
Chris Lattner27a6c732007-11-24 07:07:01 +00006231
6232 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00006233 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006234
Chris Lattner27a6c732007-11-24 07:07:01 +00006235 // If the operation produces an invalid type, it must be custom lowered. Use
6236 // the target lowering hooks to expand it. Just keep the low part of the
6237 // expanded operation, we know that we're truncating anyway.
6238 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00006239 SmallVector<SDValue, 2> Results;
6240 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6241 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6242 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00006243 }
Duncan Sands126d9072008-07-04 11:47:58 +00006244
Chris Lattner27a6c732007-11-24 07:07:01 +00006245 // Truncate the result of the extended FP_TO_*INT operation to the desired
6246 // size.
6247 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006248}
6249
6250/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6251///
Dan Gohman475871a2008-07-27 21:46:04 +00006252SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006253 MVT VT = Op.getValueType();
6254 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006255 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006256 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006257 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6258 case MVT::i16:
6259 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6260 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6261 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6262 case MVT::i32:
6263 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6264 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6265 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6266 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6267 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6268 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6269 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6270 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6271 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6272 case MVT::i64:
6273 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6274 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6275 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6276 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6277 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6278 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6279 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6280 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6281 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6282 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6283 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6284 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6285 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6286 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6287 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6288 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6289 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6290 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6291 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6292 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6293 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6294 }
6295}
6296
6297/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6298///
Dan Gohman475871a2008-07-27 21:46:04 +00006299SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006300 switch (Opc) {
6301 default: assert(0 && "Cannot expand this yet!");
6302 case ISD::CTPOP: {
6303 static const uint64_t mask[6] = {
6304 0x5555555555555555ULL, 0x3333333333333333ULL,
6305 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6306 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6307 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006308 MVT VT = Op.getValueType();
6309 MVT ShVT = TLI.getShiftAmountTy();
6310 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006311 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6312 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman475871a2008-07-27 21:46:04 +00006313 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6314 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006315 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6316 DAG.getNode(ISD::AND, VT,
6317 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6318 }
6319 return Op;
6320 }
6321 case ISD::CTLZ: {
6322 // for now, we do this:
6323 // x = x | (x >> 1);
6324 // x = x | (x >> 2);
6325 // ...
6326 // x = x | (x >>16);
6327 // x = x | (x >>32); // for 64-bit input
6328 // return popcount(~x);
6329 //
6330 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006331 MVT VT = Op.getValueType();
6332 MVT ShVT = TLI.getShiftAmountTy();
6333 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006334 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006335 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006336 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6337 }
6338 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6339 return DAG.getNode(ISD::CTPOP, VT, Op);
6340 }
6341 case ISD::CTTZ: {
6342 // for now, we use: { return popcount(~x & (x - 1)); }
6343 // unless the target has ctlz but not ctpop, in which case we use:
6344 // { return 32 - nlz(~x & (x-1)); }
6345 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006346 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00006347 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6348 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006349 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6350 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6351 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6352 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6353 TLI.isOperationLegal(ISD::CTLZ, VT))
6354 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006355 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006356 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6357 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6358 }
6359 }
6360}
Chris Lattnere34b3962005-01-19 04:19:40 +00006361
Dan Gohman475871a2008-07-27 21:46:04 +00006362/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006363/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006364/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006365/// ExpandedNodes map is filled in for any results that are expanded, and the
6366/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006367void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006368 MVT VT = Op.getValueType();
6369 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006370 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006371 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006372 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006373 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006374
Chris Lattner6fdcb252005-09-02 20:32:45 +00006375 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006376 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006377 = ExpandedNodes.find(Op);
6378 if (I != ExpandedNodes.end()) {
6379 Lo = I->second.first;
6380 Hi = I->second.second;
6381 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006382 }
6383
Chris Lattner3e928bb2005-01-07 07:47:09 +00006384 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006385 case ISD::CopyFromReg:
6386 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006387 case ISD::FP_ROUND_INREG:
6388 if (VT == MVT::ppcf128 &&
6389 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6390 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006391 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006392 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6393 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006394 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006395 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006396 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6397 Lo = Result.getNode()->getOperand(0);
6398 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006399 break;
6400 }
6401 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006402 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006403#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006404 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006405#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006406 assert(0 && "Do not know how to expand this operator!");
6407 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006408 case ISD::EXTRACT_ELEMENT:
6409 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006410 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006411 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006412 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006413 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006414 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6415 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6416 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006417 case ISD::UNDEF:
6418 Lo = DAG.getNode(ISD::UNDEF, NVT);
6419 Hi = DAG.getNode(ISD::UNDEF, NVT);
6420 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006421 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006422 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006423 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6424 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6425 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006426 break;
6427 }
Evan Cheng00495212006-12-12 21:32:44 +00006428 case ISD::ConstantFP: {
6429 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006430 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006431 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006432 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6433 MVT::f64);
6434 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6435 MVT::f64);
6436 break;
6437 }
Evan Cheng279101e2006-12-12 22:19:28 +00006438 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006439 if (getTypeAction(Lo.getValueType()) == Expand)
6440 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006441 break;
6442 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006443 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006444 // Return the operands.
6445 Lo = Node->getOperand(0);
6446 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006447 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006448
6449 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006450 if (Node->getNumValues() == 1) {
6451 ExpandOp(Op.getOperand(0), Lo, Hi);
6452 break;
6453 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006454 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006455 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006456 Op.getValue(1).getValueType() == MVT::Other &&
6457 "unhandled MERGE_VALUES");
6458 ExpandOp(Op.getOperand(0), Lo, Hi);
6459 // Remember that we legalized the chain.
6460 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6461 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006462
6463 case ISD::SIGN_EXTEND_INREG:
6464 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006465 // sext_inreg the low part if needed.
6466 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6467
6468 // The high part gets the sign extension from the lo-part. This handles
6469 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006470 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006471 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006472 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006473 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006474
Nate Begemand88fc032006-01-14 03:14:10 +00006475 case ISD::BSWAP: {
6476 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006477 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006478 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6479 Lo = TempLo;
6480 break;
6481 }
6482
Chris Lattneredb1add2005-05-11 04:51:16 +00006483 case ISD::CTPOP:
6484 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006485 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6486 DAG.getNode(ISD::CTPOP, NVT, Lo),
6487 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006488 Hi = DAG.getConstant(0, NVT);
6489 break;
6490
Chris Lattner39a8f332005-05-12 19:05:01 +00006491 case ISD::CTLZ: {
6492 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006493 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006494 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6495 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6496 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006497 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006498 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006499 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6500
6501 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6502 Hi = DAG.getConstant(0, NVT);
6503 break;
6504 }
6505
6506 case ISD::CTTZ: {
6507 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006508 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006509 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6510 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6511 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006512 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006513 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006514 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6515
6516 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6517 Hi = DAG.getConstant(0, NVT);
6518 break;
6519 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006520
Nate Begemanacc398c2006-01-25 18:21:52 +00006521 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006522 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6523 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006524 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6525 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6526
6527 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006528 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006529 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006530 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006531 std::swap(Lo, Hi);
6532 break;
6533 }
6534
Chris Lattner3e928bb2005-01-07 07:47:09 +00006535 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006536 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006537 SDValue Ch = LD->getChain(); // Legalize the chain.
6538 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006539 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006540 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006541 int SVOffset = LD->getSrcValueOffset();
6542 unsigned Alignment = LD->getAlignment();
6543 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006544
Evan Cheng466685d2006-10-09 20:57:25 +00006545 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006546 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006547 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006548 if (VT == MVT::f32 || VT == MVT::f64) {
6549 // f32->i32 or f64->i64 one to one expansion.
6550 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006551 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006552 // Recursively expand the new load.
6553 if (getTypeAction(NVT) == Expand)
6554 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006555 break;
6556 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006557
Evan Cheng466685d2006-10-09 20:57:25 +00006558 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006559 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006560 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006561 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006562 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006563 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006564 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006565 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006566
Evan Cheng466685d2006-10-09 20:57:25 +00006567 // Build a factor node to remember that this load is independent of the
6568 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006569 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006570 Hi.getValue(1));
6571
6572 // Remember that we legalized the chain.
6573 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006574 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006575 std::swap(Lo, Hi);
6576 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006577 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006578
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006579 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6580 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006581 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006582 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006583 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006584 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006585 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006586 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6587 break;
6588 }
Evan Cheng466685d2006-10-09 20:57:25 +00006589
6590 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006591 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006592 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006593 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006594 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006595 SVOffset, EVT, isVolatile,
6596 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006597
6598 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006599 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006600
6601 if (ExtType == ISD::SEXTLOAD) {
6602 // The high part is obtained by SRA'ing all but one of the bits of the
6603 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006604 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006605 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6606 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6607 } else if (ExtType == ISD::ZEXTLOAD) {
6608 // The high part is just a zero.
6609 Hi = DAG.getConstant(0, NVT);
6610 } else /* if (ExtType == ISD::EXTLOAD) */ {
6611 // The high part is undefined.
6612 Hi = DAG.getNode(ISD::UNDEF, NVT);
6613 }
6614 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006615 break;
6616 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006617 case ISD::AND:
6618 case ISD::OR:
6619 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006620 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006621 ExpandOp(Node->getOperand(0), LL, LH);
6622 ExpandOp(Node->getOperand(1), RL, RH);
6623 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6624 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6625 break;
6626 }
6627 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006628 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006629 ExpandOp(Node->getOperand(1), LL, LH);
6630 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006631 if (getTypeAction(NVT) == Expand)
6632 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006633 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006634 if (VT != MVT::f32)
6635 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006636 break;
6637 }
Nate Begeman9373a812005-08-10 20:51:12 +00006638 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006639 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006640 ExpandOp(Node->getOperand(2), TL, TH);
6641 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006642 if (getTypeAction(NVT) == Expand)
6643 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006644 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6645 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006646 if (VT != MVT::f32)
6647 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6648 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006649 break;
6650 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006651 case ISD::ANY_EXTEND:
6652 // The low part is any extension of the input (which degenerates to a copy).
6653 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6654 // The high part is undefined.
6655 Hi = DAG.getNode(ISD::UNDEF, NVT);
6656 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006657 case ISD::SIGN_EXTEND: {
6658 // The low part is just a sign extension of the input (which degenerates to
6659 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006660 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006661
Chris Lattner3e928bb2005-01-07 07:47:09 +00006662 // The high part is obtained by SRA'ing all but one of the bits of the lo
6663 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006664 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006665 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6666 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006667 break;
6668 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006669 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006670 // The low part is just a zero extension of the input (which degenerates to
6671 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006672 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006673
Chris Lattner3e928bb2005-01-07 07:47:09 +00006674 // The high part is just a zero.
6675 Hi = DAG.getConstant(0, NVT);
6676 break;
Chris Lattner35481892005-12-23 00:16:34 +00006677
Chris Lattner4c948eb2007-02-13 23:55:16 +00006678 case ISD::TRUNCATE: {
6679 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006680 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006681 ExpandOp(Node->getOperand(0), NewLo, Hi);
6682
6683 // The low part is now either the right size, or it is closer. If not the
6684 // right size, make an illegal truncate so we recursively expand it.
6685 if (NewLo.getValueType() != Node->getValueType(0))
6686 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6687 ExpandOp(NewLo, Lo, Hi);
6688 break;
6689 }
6690
Chris Lattner35481892005-12-23 00:16:34 +00006691 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006692 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006693 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6694 // If the target wants to, allow it to lower this itself.
6695 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6696 case Expand: assert(0 && "cannot expand FP!");
6697 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6698 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6699 }
6700 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6701 }
6702
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006703 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006704 if (VT == MVT::f32 || VT == MVT::f64) {
6705 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006706 if (getTypeAction(NVT) == Expand)
6707 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006708 break;
6709 }
6710
6711 // If source operand will be expanded to the same type as VT, i.e.
6712 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006713 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006714 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6715 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006716 break;
6717 }
6718
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006719 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006720 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006721 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006722
Chris Lattner35481892005-12-23 00:16:34 +00006723 ExpandOp(Tmp, Lo, Hi);
6724 break;
6725 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006726
Chris Lattner27a6c732007-11-24 07:07:01 +00006727 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006728 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6729 TargetLowering::Custom &&
6730 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006731 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006732 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006733 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006734 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006735 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006736 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006737 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006738
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006739 case ISD::ATOMIC_CMP_SWAP_64: {
6740 // This operation does not need a loop.
6741 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6742 assert(Tmp.getNode() && "Node must be custom expanded!");
6743 ExpandOp(Tmp.getValue(0), Lo, Hi);
6744 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6745 LegalizeOp(Tmp.getValue(1)));
6746 break;
6747 }
6748
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006749 case ISD::ATOMIC_LOAD_ADD_64:
6750 case ISD::ATOMIC_LOAD_SUB_64:
6751 case ISD::ATOMIC_LOAD_AND_64:
6752 case ISD::ATOMIC_LOAD_OR_64:
6753 case ISD::ATOMIC_LOAD_XOR_64:
6754 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006755 case ISD::ATOMIC_SWAP_64: {
6756 // These operations require a loop to be generated. We can't do that yet,
6757 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006758 SDValue In2Lo, In2Hi, In2;
6759 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6760 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006761 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6762 SDValue Replace =
6763 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6764 Anode->getSrcValue(), Anode->getAlignment());
6765 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006766 ExpandOp(Result.getValue(0), Lo, Hi);
6767 // Remember that we legalized the chain.
6768 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006769 break;
6770 }
6771
Chris Lattner4e6c7462005-01-08 19:27:05 +00006772 // These operators cannot be expanded directly, emit them as calls to
6773 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006774 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006775 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006776 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006777 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6778 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006779 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6780 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006781 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006782
Chris Lattnerf20d1832005-07-30 01:40:57 +00006783 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6784
Chris Lattner80a3e942005-07-29 00:33:32 +00006785 // Now that the custom expander is done, expand the result, which is still
6786 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006787 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006788 ExpandOp(Op, Lo, Hi);
6789 break;
6790 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006791 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006792
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006793 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6794 VT);
6795 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6796 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006797 break;
Evan Cheng56966222007-01-12 02:11:51 +00006798 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006799
Evan Cheng56966222007-01-12 02:11:51 +00006800 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006801 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006802 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006803 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6804 case Expand: assert(0 && "cannot expand FP!");
6805 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6806 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6807 }
6808
6809 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6810
6811 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006812 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006813 ExpandOp(Op, Lo, Hi);
6814 break;
6815 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006816 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006817
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006818 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6819 VT);
6820 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6821 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006822 break;
Evan Cheng56966222007-01-12 02:11:51 +00006823 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006824
Evan Cheng05a2d562006-01-09 18:31:59 +00006825 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006826 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006827 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006828 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006829 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006830 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006831 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006832 // Now that the custom expander is done, expand the result, which is
6833 // still VT.
6834 ExpandOp(Op, Lo, Hi);
6835 break;
6836 }
6837 }
6838
Chris Lattner79980b02006-09-13 03:50:39 +00006839 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6840 // this X << 1 as X+X.
6841 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006842 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006843 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006844 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006845 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6846 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6847 LoOps[1] = LoOps[0];
6848 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6849
6850 HiOps[1] = HiOps[0];
6851 HiOps[2] = Lo.getValue(1);
6852 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6853 break;
6854 }
6855 }
6856
Chris Lattnere34b3962005-01-19 04:19:40 +00006857 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006858 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006859 break;
Chris Lattner47599822005-04-02 03:38:53 +00006860
6861 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006862 TargetLowering::LegalizeAction Action =
6863 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6864 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6865 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006866 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006867 break;
6868 }
6869
Chris Lattnere34b3962005-01-19 04:19:40 +00006870 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006871 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006872 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006873 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006874
Evan Cheng05a2d562006-01-09 18:31:59 +00006875 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006876 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006877 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006878 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006879 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006880 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006881 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006882 // Now that the custom expander is done, expand the result, which is
6883 // still VT.
6884 ExpandOp(Op, Lo, Hi);
6885 break;
6886 }
6887 }
6888
Chris Lattnere34b3962005-01-19 04:19:40 +00006889 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006890 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006891 break;
Chris Lattner47599822005-04-02 03:38:53 +00006892
6893 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006894 TargetLowering::LegalizeAction Action =
6895 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6896 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6897 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006898 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006899 break;
6900 }
6901
Chris Lattnere34b3962005-01-19 04:19:40 +00006902 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006903 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006904 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006905 }
6906
6907 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006908 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006909 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006910 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006911 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006912 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006913 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006914 // Now that the custom expander is done, expand the result, which is
6915 // still VT.
6916 ExpandOp(Op, Lo, Hi);
6917 break;
6918 }
6919 }
6920
Chris Lattnere34b3962005-01-19 04:19:40 +00006921 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006922 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006923 break;
Chris Lattner47599822005-04-02 03:38:53 +00006924
6925 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006926 TargetLowering::LegalizeAction Action =
6927 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6928 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6929 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006930 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006931 break;
6932 }
6933
Chris Lattnere34b3962005-01-19 04:19:40 +00006934 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006935 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006936 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006937 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006938
Misha Brukmanedf128a2005-04-21 22:36:52 +00006939 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006940 case ISD::SUB: {
6941 // If the target wants to custom expand this, let them.
6942 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6943 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006944 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006945 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006946 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006947 break;
6948 }
6949 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006950 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006951 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006952 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6953 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Dan Gohman475871a2008-07-27 21:46:04 +00006954 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006955 LoOps[0] = LHSL;
6956 LoOps[1] = RHSL;
6957 HiOps[0] = LHSH;
6958 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006959
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006960 //cascaded check to see if any smaller size has a a carry flag.
6961 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6962 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006963 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6964 MVT AVT = MVT::getIntegerVT(BitSize);
6965 if (TLI.isOperationLegal(OpV, AVT)) {
6966 hasCarry = true;
6967 break;
6968 }
6969 }
6970
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006971 if(hasCarry) {
Evan Cheng637ed032008-12-12 18:49:09 +00006972 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006973 if (Node->getOpcode() == ISD::ADD) {
6974 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6975 HiOps[2] = Lo.getValue(1);
6976 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6977 } else {
6978 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6979 HiOps[2] = Lo.getValue(1);
6980 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6981 }
6982 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006983 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006984 if (Node->getOpcode() == ISD::ADD) {
Evan Cheng637ed032008-12-12 18:49:09 +00006985 Lo = DAG.getNode(ISD::ADD, NVT, LoOps, 2);
6986 Hi = DAG.getNode(ISD::ADD, NVT, HiOps, 2);
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006987 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6988 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006989 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6990 DAG.getConstant(1, NVT),
6991 DAG.getConstant(0, NVT));
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006992 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6993 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006994 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6995 DAG.getConstant(1, NVT),
6996 Carry1);
6997 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6998 } else {
Evan Cheng637ed032008-12-12 18:49:09 +00006999 Lo = DAG.getNode(ISD::SUB, NVT, LoOps, 2);
7000 Hi = DAG.getNode(ISD::SUB, NVT, HiOps, 2);
Andrew Lenharth40d51392008-10-07 14:15:42 +00007001 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
7002 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
7003 DAG.getConstant(1, NVT),
7004 DAG.getConstant(0, NVT));
7005 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
7006 }
7007 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00007008 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00007009 }
Chris Lattnerb429f732007-05-17 18:15:41 +00007010
7011 case ISD::ADDC:
7012 case ISD::SUBC: {
7013 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007014 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007015 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7016 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7017 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007018 SDValue LoOps[2] = { LHSL, RHSL };
7019 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007020
7021 if (Node->getOpcode() == ISD::ADDC) {
7022 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
7023 HiOps[2] = Lo.getValue(1);
7024 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
7025 } else {
7026 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
7027 HiOps[2] = Lo.getValue(1);
7028 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
7029 }
7030 // Remember that we legalized the flag.
7031 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7032 break;
7033 }
7034 case ISD::ADDE:
7035 case ISD::SUBE: {
7036 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00007037 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00007038 ExpandOp(Node->getOperand(0), LHSL, LHSH);
7039 ExpandOp(Node->getOperand(1), RHSL, RHSH);
7040 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00007041 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
7042 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00007043
7044 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
7045 HiOps[2] = Lo.getValue(1);
7046 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
7047
7048 // Remember that we legalized the flag.
7049 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
7050 break;
7051 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007052 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007053 // If the target wants to custom expand this, let them.
7054 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00007055 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00007056 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00007057 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00007058 break;
7059 }
7060 }
7061
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007062 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
7063 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00007064 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
7065 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
7066 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00007067 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00007068 ExpandOp(Node->getOperand(0), LL, LH);
7069 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007070 unsigned OuterBitSize = Op.getValueSizeInBits();
7071 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00007072 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
7073 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00007074 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
7075 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
7076 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00007077 // The inputs are both zero-extended.
7078 if (HasUMUL_LOHI) {
7079 // We can emit a umul_lohi.
7080 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007081 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007082 break;
7083 }
7084 if (HasMULHU) {
7085 // We can emit a mulhu+mul.
7086 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7087 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7088 break;
7089 }
Dan Gohman525178c2007-10-08 18:33:35 +00007090 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00007091 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00007092 // The input values are both sign-extended.
7093 if (HasSMUL_LOHI) {
7094 // We can emit a smul_lohi.
7095 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00007096 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00007097 break;
7098 }
7099 if (HasMULHS) {
7100 // We can emit a mulhs+mul.
7101 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7102 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
7103 break;
7104 }
7105 }
7106 if (HasUMUL_LOHI) {
7107 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00007108 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00007109 DAG.getVTList(NVT, NVT), LL, RL);
7110 Lo = UMulLOHI;
7111 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00007112 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7113 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7114 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7115 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00007116 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00007117 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007118 if (HasMULHU) {
7119 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7120 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7121 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7122 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7123 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7124 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7125 break;
7126 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007127 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007128
Dan Gohman525178c2007-10-08 18:33:35 +00007129 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007130 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00007131 break;
7132 }
Evan Cheng56966222007-01-12 02:11:51 +00007133 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007134 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007135 break;
7136 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007137 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007138 break;
7139 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007140 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007141 break;
7142 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007143 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007144 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00007145
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007146 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00007147 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7148 RTLIB::ADD_F64,
7149 RTLIB::ADD_F80,
7150 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007151 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007152 break;
7153 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00007154 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7155 RTLIB::SUB_F64,
7156 RTLIB::SUB_F80,
7157 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007158 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007159 break;
7160 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007161 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7162 RTLIB::MUL_F64,
7163 RTLIB::MUL_F80,
7164 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007165 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007166 break;
7167 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007168 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7169 RTLIB::DIV_F64,
7170 RTLIB::DIV_F80,
7171 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007172 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007173 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007174 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007175 if (VT == MVT::ppcf128) {
7176 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7177 Node->getOperand(0).getValueType()==MVT::f64);
7178 const uint64_t zero = 0;
7179 if (Node->getOperand(0).getValueType()==MVT::f32)
7180 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7181 else
7182 Hi = Node->getOperand(0);
7183 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7184 break;
7185 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007186 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7187 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7188 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007189 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007190 }
7191 case ISD::FP_ROUND: {
7192 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7193 VT);
7194 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7195 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007196 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007197 }
Evan Cheng4b887022008-09-09 23:02:14 +00007198 case ISD::FSQRT:
7199 case ISD::FSIN:
7200 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007201 case ISD::FLOG:
7202 case ISD::FLOG2:
7203 case ISD::FLOG10:
7204 case ISD::FEXP:
7205 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007206 case ISD::FTRUNC:
7207 case ISD::FFLOOR:
7208 case ISD::FCEIL:
7209 case ISD::FRINT:
7210 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007211 case ISD::FPOW:
7212 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007213 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007214 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007215 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007216 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7217 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007218 break;
7219 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007220 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7221 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007222 break;
7223 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007224 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7225 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007226 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007227 case ISD::FLOG:
7228 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7229 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7230 break;
7231 case ISD::FLOG2:
7232 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7233 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7234 break;
7235 case ISD::FLOG10:
7236 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7237 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7238 break;
7239 case ISD::FEXP:
7240 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7241 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7242 break;
7243 case ISD::FEXP2:
7244 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7245 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7246 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007247 case ISD::FTRUNC:
7248 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7249 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7250 break;
7251 case ISD::FFLOOR:
7252 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7253 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7254 break;
7255 case ISD::FCEIL:
7256 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7257 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7258 break;
7259 case ISD::FRINT:
7260 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7261 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7262 break;
7263 case ISD::FNEARBYINT:
7264 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7265 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7266 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007267 case ISD::FPOW:
7268 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7269 RTLIB::POW_PPCF128);
7270 break;
7271 case ISD::FPOWI:
7272 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7273 RTLIB::POWI_PPCF128);
7274 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007275 default: assert(0 && "Unreachable!");
7276 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007277 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007278 break;
7279 }
Evan Cheng966bf242006-12-16 00:52:40 +00007280 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007281 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007282 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007283 ExpandOp(Node->getOperand(0), Lo, Tmp);
7284 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7285 // lo = hi==fabs(hi) ? lo : -lo;
7286 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7287 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7288 DAG.getCondCode(ISD::SETEQ));
7289 break;
7290 }
Dan Gohman475871a2008-07-27 21:46:04 +00007291 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007292 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7293 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7294 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7295 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7296 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7297 if (getTypeAction(NVT) == Expand)
7298 ExpandOp(Lo, Lo, Hi);
7299 break;
7300 }
7301 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007302 if (VT == MVT::ppcf128) {
7303 ExpandOp(Node->getOperand(0), Lo, Hi);
7304 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7305 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7306 break;
7307 }
Dan Gohman475871a2008-07-27 21:46:04 +00007308 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007309 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7310 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7311 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7312 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7313 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7314 if (getTypeAction(NVT) == Expand)
7315 ExpandOp(Lo, Lo, Hi);
7316 break;
7317 }
Evan Cheng912095b2007-01-04 21:56:39 +00007318 case ISD::FCOPYSIGN: {
7319 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7320 if (getTypeAction(NVT) == Expand)
7321 ExpandOp(Lo, Lo, Hi);
7322 break;
7323 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007324 case ISD::SINT_TO_FP:
7325 case ISD::UINT_TO_FP: {
7326 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007327 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007328
7329 // Promote the operand if needed. Do this before checking for
7330 // ppcf128 so conversions of i16 and i8 work.
7331 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007332 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007333 Tmp = isSigned
7334 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7335 DAG.getValueType(SrcVT))
7336 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007337 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007338 SrcVT = Node->getOperand(0).getValueType();
7339 }
7340
Dan Gohmana2e94852008-03-10 23:03:31 +00007341 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007342 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007343 if (isSigned) {
7344 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7345 Node->getOperand(0)));
7346 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7347 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007348 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007349 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7350 Node->getOperand(0)));
7351 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7352 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007353 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007354 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7355 DAG.getConstant(0, MVT::i32),
7356 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7357 DAG.getConstantFP(
7358 APFloat(APInt(128, 2, TwoE32)),
7359 MVT::ppcf128)),
7360 Hi,
7361 DAG.getCondCode(ISD::SETLT)),
7362 Lo, Hi);
7363 }
7364 break;
7365 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007366 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7367 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007368 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007369 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7370 Lo, Hi);
7371 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7372 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7373 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7374 DAG.getConstant(0, MVT::i64),
7375 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7376 DAG.getConstantFP(
7377 APFloat(APInt(128, 2, TwoE64)),
7378 MVT::ppcf128)),
7379 Hi,
7380 DAG.getCondCode(ISD::SETLT)),
7381 Lo, Hi);
7382 break;
7383 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007384
Dan Gohmana2e94852008-03-10 23:03:31 +00007385 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7386 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00007387 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007388 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007389 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007390 break;
7391 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007392 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007393
Chris Lattner83397362005-12-21 18:02:52 +00007394 // Make sure the resultant values have been legalized themselves, unless this
7395 // is a type that requires multi-step expansion.
7396 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7397 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007398 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007399 // Don't legalize the high part if it is expanded to a single node.
7400 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007401 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007402
7403 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007404 bool isNew =
7405 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007406 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007407 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007408}
7409
Dan Gohman7f321562007-06-25 16:23:39 +00007410/// SplitVectorOp - Given an operand of vector type, break it down into
7411/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007412void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7413 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007414 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007415 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007416 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007417 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007418
Duncan Sands83ec4b62008-06-06 12:08:01 +00007419 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007420
7421 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7422 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7423
Duncan Sands83ec4b62008-06-06 12:08:01 +00007424 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7425 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007426
Chris Lattnerc7029802006-03-18 01:44:44 +00007427 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007428 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007429 = SplitNodes.find(Op);
7430 if (I != SplitNodes.end()) {
7431 Lo = I->second.first;
7432 Hi = I->second.second;
7433 return;
7434 }
7435
7436 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007437 default:
7438#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007439 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007440#endif
7441 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007442 case ISD::UNDEF:
7443 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7444 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7445 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007446 case ISD::BUILD_PAIR:
7447 Lo = Node->getOperand(0);
7448 Hi = Node->getOperand(1);
7449 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007450 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007451 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7452 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007453 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007454 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007455 if (Index < NewNumElts_Lo)
7456 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7457 DAG.getIntPtrConstant(Index));
7458 else
7459 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7460 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7461 break;
7462 }
Dan Gohman475871a2008-07-27 21:46:04 +00007463 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007464 Node->getOperand(1),
7465 Node->getOperand(2));
7466 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007467 break;
7468 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007469 case ISD::VECTOR_SHUFFLE: {
7470 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007471 SDValue Mask = Node->getOperand(2);
7472 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007473 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007474
7475 // Insert all of the elements from the input that are needed. We use
7476 // buildvector of extractelement here because the input vectors will have
7477 // to be legalized, so this makes the code simpler.
7478 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007479 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007480 if (IdxNode.getOpcode() == ISD::UNDEF) {
7481 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7482 continue;
7483 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007484 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007485 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007486 if (Idx >= NumElements) {
7487 InVec = Node->getOperand(1);
7488 Idx -= NumElements;
7489 }
7490 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7491 DAG.getConstant(Idx, PtrVT)));
7492 }
7493 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7494 Ops.clear();
7495
7496 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007497 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007498 if (IdxNode.getOpcode() == ISD::UNDEF) {
7499 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7500 continue;
7501 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007502 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007503 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007504 if (Idx >= NumElements) {
7505 InVec = Node->getOperand(1);
7506 Idx -= NumElements;
7507 }
7508 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7509 DAG.getConstant(Idx, PtrVT)));
7510 }
Mon P Wang92879f32008-07-25 01:30:26 +00007511 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007512 break;
7513 }
Dan Gohman7f321562007-06-25 16:23:39 +00007514 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007515 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007516 Node->op_begin()+NewNumElts_Lo);
7517 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007518
Dan Gohman475871a2008-07-27 21:46:04 +00007519 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007520 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007521 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007522 break;
7523 }
Dan Gohman7f321562007-06-25 16:23:39 +00007524 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007525 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007526 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7527 if (NewNumSubvectors == 1) {
7528 Lo = Node->getOperand(0);
7529 Hi = Node->getOperand(1);
7530 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007531 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7532 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007533 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007534
Mon P Wangaeb06d22008-11-10 04:46:22 +00007535 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007536 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007537 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007538 }
Dan Gohman65956352007-06-13 15:12:02 +00007539 break;
7540 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007541 case ISD::EXTRACT_SUBVECTOR: {
7542 SDValue Vec = Op.getOperand(0);
7543 SDValue Idx = Op.getOperand(1);
7544 MVT IdxVT = Idx.getValueType();
7545
7546 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7547 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7548 if (CIdx) {
7549 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7550 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7551 IdxVT));
7552 } else {
7553 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7554 DAG.getConstant(NewNumElts_Lo, IdxVT));
7555 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7556 }
7557 break;
7558 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007559 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007560 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007561
Dan Gohman475871a2008-07-27 21:46:04 +00007562 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007563 SplitVectorOp(Node->getOperand(1), LL, LH);
7564 SplitVectorOp(Node->getOperand(2), RL, RH);
7565
Duncan Sands83ec4b62008-06-06 12:08:01 +00007566 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007567 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007568 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007569 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007570 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7571 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007572 } else {
7573 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007574 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7575 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007576 }
7577 break;
7578 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007579 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007580 SDValue CondLHS = Node->getOperand(0);
7581 SDValue CondRHS = Node->getOperand(1);
7582 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007583
Dan Gohman475871a2008-07-27 21:46:04 +00007584 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007585 SplitVectorOp(Node->getOperand(2), LL, LH);
7586 SplitVectorOp(Node->getOperand(3), RL, RH);
7587
7588 // Handle a simple select with vector operands.
7589 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7590 LL, RL, CondCode);
7591 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7592 LH, RH, CondCode);
7593 break;
7594 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007595 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007596 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007597 SplitVectorOp(Node->getOperand(0), LL, LH);
7598 SplitVectorOp(Node->getOperand(1), RL, RH);
7599 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7600 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7601 break;
7602 }
Dan Gohman7f321562007-06-25 16:23:39 +00007603 case ISD::ADD:
7604 case ISD::SUB:
7605 case ISD::MUL:
7606 case ISD::FADD:
7607 case ISD::FSUB:
7608 case ISD::FMUL:
7609 case ISD::SDIV:
7610 case ISD::UDIV:
7611 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007612 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007613 case ISD::AND:
7614 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007615 case ISD::XOR:
7616 case ISD::UREM:
7617 case ISD::SREM:
7618 case ISD::FREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00007619 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007620 SplitVectorOp(Node->getOperand(0), LL, LH);
7621 SplitVectorOp(Node->getOperand(1), RL, RH);
7622
Nate Begeman5db1afb2007-11-15 21:15:26 +00007623 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7624 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007625 break;
7626 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007627 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007628 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007629 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007630 SplitVectorOp(Node->getOperand(0), L, H);
7631
Nate Begeman5db1afb2007-11-15 21:15:26 +00007632 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7633 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007634 break;
7635 }
7636 case ISD::CTTZ:
7637 case ISD::CTLZ:
7638 case ISD::CTPOP:
7639 case ISD::FNEG:
7640 case ISD::FABS:
7641 case ISD::FSQRT:
7642 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007643 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007644 case ISD::FLOG:
7645 case ISD::FLOG2:
7646 case ISD::FLOG10:
7647 case ISD::FEXP:
7648 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007649 case ISD::FP_TO_SINT:
7650 case ISD::FP_TO_UINT:
7651 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007652 case ISD::UINT_TO_FP:
7653 case ISD::TRUNCATE:
7654 case ISD::ANY_EXTEND:
7655 case ISD::SIGN_EXTEND:
7656 case ISD::ZERO_EXTEND:
7657 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007658 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007659 SplitVectorOp(Node->getOperand(0), L, H);
7660
Nate Begeman5db1afb2007-11-15 21:15:26 +00007661 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7662 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007663 break;
7664 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007665 case ISD::CONVERT_RNDSAT: {
7666 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7667 SDValue L, H;
7668 SplitVectorOp(Node->getOperand(0), L, H);
7669 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7670 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7671 SDValue STyOpL = DAG.getValueType(L.getValueType());
7672 SDValue STyOpH = DAG.getValueType(H.getValueType());
7673
7674 SDValue RndOp = Node->getOperand(3);
7675 SDValue SatOp = Node->getOperand(4);
7676
7677 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7678 RndOp, SatOp, CvtCode);
7679 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7680 RndOp, SatOp, CvtCode);
7681 break;
7682 }
Dan Gohman7f321562007-06-25 16:23:39 +00007683 case ISD::LOAD: {
7684 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007685 SDValue Ch = LD->getChain();
7686 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007687 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007688 const Value *SV = LD->getSrcValue();
7689 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007690 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007691 unsigned Alignment = LD->getAlignment();
7692 bool isVolatile = LD->isVolatile();
7693
Dan Gohman7f8613e2008-08-14 20:04:46 +00007694 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7695 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7696
7697 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7698 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7699 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7700
7701 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7702 NewVT_Lo, Ch, Ptr, Offset,
7703 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7704 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007705 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007706 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007707 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007708 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007709 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7710 NewVT_Hi, Ch, Ptr, Offset,
7711 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007712
7713 // Build a factor node to remember that this load is independent of the
7714 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007715 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007716 Hi.getValue(1));
7717
7718 // Remember that we legalized the chain.
7719 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007720 break;
7721 }
Dan Gohman7f321562007-06-25 16:23:39 +00007722 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007723 // We know the result is a vector. The input may be either a vector or a
7724 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007725 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007726 if (!InOp.getValueType().isVector() ||
7727 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007728 // The input is a scalar or single-element vector.
7729 // Lower to a store/load so that it can be split.
7730 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007731 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7732 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007733 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007734 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007735
Dan Gohman475871a2008-07-27 21:46:04 +00007736 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007737 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007738 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007739 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007740 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007741 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007742 // Split the vector and convert each of the pieces now.
7743 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007744 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7745 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007746 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007747 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007748 }
7749
7750 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007751 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007752 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007753 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007754 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007755}
7756
7757
Dan Gohman89b20c02007-06-27 14:06:22 +00007758/// ScalarizeVectorOp - Given an operand of single-element vector type
7759/// (e.g. v1f32), convert it into the equivalent operation that returns a
7760/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007761SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007762 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007763 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007764 MVT NewVT = Op.getValueType().getVectorElementType();
7765 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007766
Dan Gohman7f321562007-06-25 16:23:39 +00007767 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007768 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007769 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007770
Dan Gohman475871a2008-07-27 21:46:04 +00007771 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007772 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007773 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007774#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007775 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007776#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007777 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7778 case ISD::ADD:
7779 case ISD::FADD:
7780 case ISD::SUB:
7781 case ISD::FSUB:
7782 case ISD::MUL:
7783 case ISD::FMUL:
7784 case ISD::SDIV:
7785 case ISD::UDIV:
7786 case ISD::FDIV:
7787 case ISD::SREM:
7788 case ISD::UREM:
7789 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007790 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007791 case ISD::AND:
7792 case ISD::OR:
7793 case ISD::XOR:
7794 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007795 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007796 ScalarizeVectorOp(Node->getOperand(0)),
7797 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007798 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007799 case ISD::FNEG:
7800 case ISD::FABS:
7801 case ISD::FSQRT:
7802 case ISD::FSIN:
7803 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007804 case ISD::FLOG:
7805 case ISD::FLOG2:
7806 case ISD::FLOG10:
7807 case ISD::FEXP:
7808 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007809 case ISD::FP_TO_SINT:
7810 case ISD::FP_TO_UINT:
7811 case ISD::SINT_TO_FP:
7812 case ISD::UINT_TO_FP:
7813 case ISD::SIGN_EXTEND:
7814 case ISD::ZERO_EXTEND:
7815 case ISD::ANY_EXTEND:
7816 case ISD::TRUNCATE:
7817 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007818 Result = DAG.getNode(Node->getOpcode(),
7819 NewVT,
7820 ScalarizeVectorOp(Node->getOperand(0)));
7821 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007822 case ISD::CONVERT_RNDSAT: {
7823 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7824 Result = DAG.getConvertRndSat(NewVT, Op0,
7825 DAG.getValueType(NewVT),
7826 DAG.getValueType(Op0.getValueType()),
7827 Node->getOperand(3),
7828 Node->getOperand(4),
7829 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7830 break;
7831 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007832 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007833 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007834 Result = DAG.getNode(Node->getOpcode(),
7835 NewVT,
7836 ScalarizeVectorOp(Node->getOperand(0)),
7837 Node->getOperand(1));
7838 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007839 case ISD::LOAD: {
7840 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007841 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7842 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007843 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007844 const Value *SV = LD->getSrcValue();
7845 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007846 MVT MemoryVT = LD->getMemoryVT();
7847 unsigned Alignment = LD->getAlignment();
7848 bool isVolatile = LD->isVolatile();
7849
7850 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7851 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7852
7853 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7854 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7855 MemoryVT.getVectorElementType(),
7856 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007857
Chris Lattnerc7029802006-03-18 01:44:44 +00007858 // Remember that we legalized the chain.
7859 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7860 break;
7861 }
Dan Gohman7f321562007-06-25 16:23:39 +00007862 case ISD::BUILD_VECTOR:
7863 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007864 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007865 case ISD::INSERT_VECTOR_ELT:
7866 // Returning the inserted scalar element.
7867 Result = Node->getOperand(1);
7868 break;
7869 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007870 assert(Node->getOperand(0).getValueType() == NewVT &&
7871 "Concat of non-legal vectors not yet supported!");
7872 Result = Node->getOperand(0);
7873 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007874 case ISD::VECTOR_SHUFFLE: {
7875 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007876 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007877 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007878 Result = ScalarizeVectorOp(Node->getOperand(1));
7879 else
7880 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007881 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007882 }
7883 case ISD::EXTRACT_SUBVECTOR:
Mon P Wange0b436a2008-11-06 22:52:21 +00007884 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangaeb06d22008-11-10 04:46:22 +00007885 Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007886 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007887 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007888 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007889 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007890 Op0 = ScalarizeVectorOp(Op0);
7891 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007892 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007893 }
Dan Gohman7f321562007-06-25 16:23:39 +00007894 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007895 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007896 ScalarizeVectorOp(Op.getOperand(1)),
7897 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007898 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007899 case ISD::SELECT_CC:
7900 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7901 Node->getOperand(1),
7902 ScalarizeVectorOp(Op.getOperand(2)),
7903 ScalarizeVectorOp(Op.getOperand(3)),
7904 Node->getOperand(4));
7905 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007906 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007907 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7908 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007909 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7910 Op.getOperand(2));
7911 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7912 DAG.getConstant(-1ULL, NewVT),
7913 DAG.getConstant(0ULL, NewVT));
7914 break;
7915 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007916 }
7917
7918 if (TLI.isTypeLegal(NewVT))
7919 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007920 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7921 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007922 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007923 return Result;
7924}
7925
Chris Lattner3e928bb2005-01-07 07:47:09 +00007926
Mon P Wang0c397192008-10-30 08:01:45 +00007927SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7928 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7929 if (I != WidenNodes.end()) return I->second;
7930
7931 MVT VT = Op.getValueType();
7932 assert(VT.isVector() && "Cannot widen non-vector type!");
7933
7934 SDValue Result;
7935 SDNode *Node = Op.getNode();
7936 MVT EVT = VT.getVectorElementType();
7937
7938 unsigned NumElts = VT.getVectorNumElements();
7939 unsigned NewNumElts = WidenVT.getVectorNumElements();
7940 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7941 assert(NewNumElts < 17);
7942
7943 // When widen is called, it is assumed that it is more efficient to use a
7944 // wide type. The default action is to widen to operation to a wider legal
7945 // vector type and then do the operation if it is legal by calling LegalizeOp
7946 // again. If there is no vector equivalent, we will unroll the operation, do
7947 // it, and rebuild the vector. If most of the operations are vectorizible to
7948 // the legal type, the resulting code will be more efficient. If this is not
7949 // the case, the resulting code will preform badly as we end up generating
7950 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007951 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007952 switch (Node->getOpcode()) {
7953 default:
7954#ifndef NDEBUG
7955 Node->dump(&DAG);
7956#endif
7957 assert(0 && "Unexpected operation in WidenVectorOp!");
7958 break;
7959 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007960 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007961 case ISD::Constant:
7962 case ISD::ConstantFP:
7963 // To build a vector of these elements, clients should call BuildVector
7964 // and with each element instead of creating a node with a vector type
7965 assert(0 && "Unexpected operation in WidenVectorOp!");
7966 case ISD::VAARG:
7967 // Variable Arguments with vector types doesn't make any sense to me
7968 assert(0 && "Unexpected operation in WidenVectorOp!");
7969 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007970 case ISD::UNDEF:
7971 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7972 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007973 case ISD::BUILD_VECTOR: {
7974 // Build a vector with undefined for the new nodes
7975 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7976 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7977 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7978 }
7979 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7980 break;
7981 }
7982 case ISD::INSERT_VECTOR_ELT: {
7983 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7984 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7985 Node->getOperand(1), Node->getOperand(2));
7986 break;
7987 }
7988 case ISD::VECTOR_SHUFFLE: {
7989 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7990 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7991 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7992 // used as permutation array. We build the vector here instead of widening
7993 // because we don't want to legalize and have it turned to something else.
7994 SDValue PermOp = Node->getOperand(2);
7995 SDValueVector NewOps;
7996 MVT PVT = PermOp.getValueType().getVectorElementType();
7997 for (unsigned i = 0; i < NumElts; ++i) {
7998 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7999 NewOps.push_back(PermOp.getOperand(i));
8000 } else {
8001 unsigned Idx =
8002 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
8003 if (Idx < NumElts) {
8004 NewOps.push_back(PermOp.getOperand(i));
8005 }
8006 else {
8007 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
8008 PermOp.getOperand(i).getValueType()));
8009 }
8010 }
8011 }
8012 for (unsigned i = NumElts; i < NewNumElts; ++i) {
8013 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
8014 }
8015
8016 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
8017 MVT::getVectorVT(PVT, NewOps.size()),
8018 &NewOps[0], NewOps.size());
8019
8020 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
8021 break;
8022 }
8023 case ISD::LOAD: {
8024 // If the load widen returns true, we can use a single load for the
8025 // vector. Otherwise, it is returning a token factor for multiple
8026 // loads.
8027 SDValue TFOp;
8028 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
8029 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
8030 else
8031 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
8032 break;
8033 }
8034
8035 case ISD::BIT_CONVERT: {
8036 SDValue Tmp1 = Node->getOperand(0);
8037 // Converts between two different types so we need to determine
8038 // the correct widen type for the input operand.
8039 MVT TVT = Tmp1.getValueType();
8040 assert(TVT.isVector() && "can not widen non vector type");
8041 MVT TEVT = TVT.getVectorElementType();
8042 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
8043 "can not widen bit bit convert that are not multiple of element type");
8044 MVT TWidenVT = MVT::getVectorVT(TEVT,
8045 WidenVT.getSizeInBits()/EVT.getSizeInBits());
8046 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8047 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
8048 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
8049
8050 TargetLowering::LegalizeAction action =
8051 TLI.getOperationAction(Node->getOpcode(), WidenVT);
8052 switch (action) {
8053 default: assert(0 && "action not supported");
8054 case TargetLowering::Legal:
8055 break;
8056 case TargetLowering::Promote:
8057 // We defer the promotion to when we legalize the op
8058 break;
8059 case TargetLowering::Expand:
8060 // Expand the operation into a bunch of nasty scalar code.
8061 Result = LegalizeOp(UnrollVectorOp(Result));
8062 break;
8063 }
8064 break;
8065 }
8066
8067 case ISD::SINT_TO_FP:
8068 case ISD::UINT_TO_FP:
8069 case ISD::FP_TO_SINT:
8070 case ISD::FP_TO_UINT: {
8071 SDValue Tmp1 = Node->getOperand(0);
8072 // Converts between two different types so we need to determine
8073 // the correct widen type for the input operand.
8074 MVT TVT = Tmp1.getValueType();
8075 assert(TVT.isVector() && "can not widen non vector type");
8076 MVT TEVT = TVT.getVectorElementType();
8077 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
8078 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
8079 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
8080 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008081 break;
8082 }
8083
8084 case ISD::FP_EXTEND:
8085 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
8086 case ISD::TRUNCATE:
8087 case ISD::SIGN_EXTEND:
8088 case ISD::ZERO_EXTEND:
8089 case ISD::ANY_EXTEND:
8090 case ISD::FP_ROUND:
8091 case ISD::SIGN_EXTEND_INREG:
8092 case ISD::FABS:
8093 case ISD::FNEG:
8094 case ISD::FSQRT:
8095 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00008096 case ISD::FCOS:
8097 case ISD::CTPOP:
8098 case ISD::CTTZ:
8099 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00008100 // Unary op widening
8101 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00008102 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8103 assert(Tmp1.getValueType() == WidenVT);
8104 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00008105 break;
8106 }
Mon P Wang77cdf302008-11-10 20:54:11 +00008107 case ISD::CONVERT_RNDSAT: {
8108 SDValue RndOp = Node->getOperand(3);
8109 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00008110 SDValue SrcOp = Node->getOperand(0);
8111
8112 // Converts between two different types so we need to determine
8113 // the correct widen type for the input operand.
8114 MVT SVT = SrcOp.getValueType();
8115 assert(SVT.isVector() && "can not widen non vector type");
8116 MVT SEVT = SVT.getVectorElementType();
8117 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8118
8119 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8120 assert(SrcOp.getValueType() == WidenVT);
8121 SDValue DTyOp = DAG.getValueType(WidenVT);
8122 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8123 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8124
8125 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8126 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00008127 break;
8128 }
Mon P Wang0c397192008-10-30 08:01:45 +00008129 case ISD::FPOW:
8130 case ISD::FPOWI:
8131 case ISD::ADD:
8132 case ISD::SUB:
8133 case ISD::MUL:
8134 case ISD::MULHS:
8135 case ISD::MULHU:
8136 case ISD::AND:
8137 case ISD::OR:
8138 case ISD::XOR:
8139 case ISD::FADD:
8140 case ISD::FSUB:
8141 case ISD::FMUL:
8142 case ISD::SDIV:
8143 case ISD::SREM:
8144 case ISD::FDIV:
8145 case ISD::FREM:
8146 case ISD::FCOPYSIGN:
8147 case ISD::UDIV:
8148 case ISD::UREM:
8149 case ISD::BSWAP: {
8150 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00008151 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8152 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8153 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8154 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008155 break;
8156 }
8157
8158 case ISD::SHL:
8159 case ISD::SRA:
8160 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00008161 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8162 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00008163 SDValue ShOp = Node->getOperand(1);
8164 MVT ShVT = ShOp.getValueType();
8165 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8166 WidenVT.getVectorNumElements());
8167 ShOp = WidenVectorOp(ShOp, NewShVT);
8168 assert(ShOp.getValueType() == NewShVT);
8169 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008170 break;
8171 }
Mon P Wangfb13f002008-12-02 07:35:08 +00008172
Mon P Wang0c397192008-10-30 08:01:45 +00008173 case ISD::EXTRACT_VECTOR_ELT: {
8174 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8175 assert(Tmp1.getValueType() == WidenVT);
8176 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8177 break;
8178 }
8179 case ISD::CONCAT_VECTORS: {
8180 // We concurrently support only widen on a multiple of the incoming vector.
8181 // We could widen on a multiple of the incoming operand if necessary.
8182 unsigned NumConcat = NewNumElts / NumElts;
8183 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangfb13f002008-12-02 07:35:08 +00008184 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang0c397192008-10-30 08:01:45 +00008185 SmallVector<SDValue, 8> MOps;
8186 MOps.push_back(Op);
8187 for (unsigned i = 1; i != NumConcat; ++i) {
8188 MOps.push_back(UndefVal);
8189 }
8190 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8191 &MOps[0], MOps.size()));
8192 break;
8193 }
8194 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008195 SDValue Tmp1 = Node->getOperand(0);
8196 SDValue Idx = Node->getOperand(1);
8197 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8198 if (CIdx && CIdx->getZExtValue() == 0) {
8199 // Since we are access the start of the vector, the incoming
8200 // vector type might be the proper.
8201 MVT Tmp1VT = Tmp1.getValueType();
8202 if (Tmp1VT == WidenVT)
8203 return Tmp1;
8204 else {
8205 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8206 if (Tmp1VTNumElts < NewNumElts)
8207 Result = WidenVectorOp(Tmp1, WidenVT);
8208 else
8209 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8210 }
8211 } else if (NewNumElts % NumElts == 0) {
8212 // Widen the extracted subvector.
8213 unsigned NumConcat = NewNumElts / NumElts;
8214 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8215 SmallVector<SDValue, 8> MOps;
8216 MOps.push_back(Op);
8217 for (unsigned i = 1; i != NumConcat; ++i) {
8218 MOps.push_back(UndefVal);
8219 }
8220 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8221 &MOps[0], MOps.size()));
8222 } else {
8223 assert(0 && "can not widen extract subvector");
8224 // This could be implemented using insert and build vector but I would
8225 // like to see when this happens.
8226 }
Mon P Wang0c397192008-10-30 08:01:45 +00008227 break;
8228 }
8229
8230 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00008231 // Determine new condition widen type and widen
8232 SDValue Cond1 = Node->getOperand(0);
8233 MVT CondVT = Cond1.getValueType();
8234 assert(CondVT.isVector() && "can not widen non vector type");
8235 MVT CondEVT = CondVT.getVectorElementType();
8236 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8237 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8238 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8239
8240 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8241 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8242 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8243 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008244 break;
8245 }
8246
8247 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00008248 // Determine new condition widen type and widen
8249 SDValue Cond1 = Node->getOperand(0);
8250 SDValue Cond2 = Node->getOperand(1);
8251 MVT CondVT = Cond1.getValueType();
8252 assert(CondVT.isVector() && "can not widen non vector type");
8253 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8254 MVT CondEVT = CondVT.getVectorElementType();
8255 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8256 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8257 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8258 assert(Cond1.getValueType() == CondWidenVT &&
8259 Cond2.getValueType() == CondWidenVT && "condition not widen");
8260
8261 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8262 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8263 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8264 "operands not widen");
8265 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8266 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00008267 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008268 }
8269 case ISD::VSETCC: {
8270 // Determine widen for the operand
8271 SDValue Tmp1 = Node->getOperand(0);
8272 MVT TmpVT = Tmp1.getValueType();
8273 assert(TmpVT.isVector() && "can not widen non vector type");
8274 MVT TmpEVT = TmpVT.getVectorElementType();
8275 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8276 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8277 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8278 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8279 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008280 break;
8281 }
Mon P Wang0c397192008-10-30 08:01:45 +00008282 case ISD::ATOMIC_CMP_SWAP_8:
8283 case ISD::ATOMIC_CMP_SWAP_16:
8284 case ISD::ATOMIC_CMP_SWAP_32:
8285 case ISD::ATOMIC_CMP_SWAP_64:
8286 case ISD::ATOMIC_LOAD_ADD_8:
8287 case ISD::ATOMIC_LOAD_SUB_8:
8288 case ISD::ATOMIC_LOAD_AND_8:
8289 case ISD::ATOMIC_LOAD_OR_8:
8290 case ISD::ATOMIC_LOAD_XOR_8:
8291 case ISD::ATOMIC_LOAD_NAND_8:
8292 case ISD::ATOMIC_LOAD_MIN_8:
8293 case ISD::ATOMIC_LOAD_MAX_8:
8294 case ISD::ATOMIC_LOAD_UMIN_8:
8295 case ISD::ATOMIC_LOAD_UMAX_8:
8296 case ISD::ATOMIC_SWAP_8:
8297 case ISD::ATOMIC_LOAD_ADD_16:
8298 case ISD::ATOMIC_LOAD_SUB_16:
8299 case ISD::ATOMIC_LOAD_AND_16:
8300 case ISD::ATOMIC_LOAD_OR_16:
8301 case ISD::ATOMIC_LOAD_XOR_16:
8302 case ISD::ATOMIC_LOAD_NAND_16:
8303 case ISD::ATOMIC_LOAD_MIN_16:
8304 case ISD::ATOMIC_LOAD_MAX_16:
8305 case ISD::ATOMIC_LOAD_UMIN_16:
8306 case ISD::ATOMIC_LOAD_UMAX_16:
8307 case ISD::ATOMIC_SWAP_16:
8308 case ISD::ATOMIC_LOAD_ADD_32:
8309 case ISD::ATOMIC_LOAD_SUB_32:
8310 case ISD::ATOMIC_LOAD_AND_32:
8311 case ISD::ATOMIC_LOAD_OR_32:
8312 case ISD::ATOMIC_LOAD_XOR_32:
8313 case ISD::ATOMIC_LOAD_NAND_32:
8314 case ISD::ATOMIC_LOAD_MIN_32:
8315 case ISD::ATOMIC_LOAD_MAX_32:
8316 case ISD::ATOMIC_LOAD_UMIN_32:
8317 case ISD::ATOMIC_LOAD_UMAX_32:
8318 case ISD::ATOMIC_SWAP_32:
8319 case ISD::ATOMIC_LOAD_ADD_64:
8320 case ISD::ATOMIC_LOAD_SUB_64:
8321 case ISD::ATOMIC_LOAD_AND_64:
8322 case ISD::ATOMIC_LOAD_OR_64:
8323 case ISD::ATOMIC_LOAD_XOR_64:
8324 case ISD::ATOMIC_LOAD_NAND_64:
8325 case ISD::ATOMIC_LOAD_MIN_64:
8326 case ISD::ATOMIC_LOAD_MAX_64:
8327 case ISD::ATOMIC_LOAD_UMIN_64:
8328 case ISD::ATOMIC_LOAD_UMAX_64:
8329 case ISD::ATOMIC_SWAP_64: {
8330 // For now, we assume that using vectors for these operations don't make
8331 // much sense so we just split it. We return an empty result
8332 SDValue X, Y;
8333 SplitVectorOp(Op, X, Y);
8334 return Result;
8335 break;
8336 }
8337
8338 } // end switch (Node->getOpcode())
8339
8340 assert(Result.getNode() && "Didn't set a result!");
8341 if (Result != Op)
8342 Result = LegalizeOp(Result);
8343
Mon P Wangf007a8b2008-11-06 05:31:54 +00008344 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008345 return Result;
8346}
8347
8348// Utility function to find a legal vector type and its associated element
8349// type from a preferred width and whose vector type must be the same size
8350// as the VVT.
8351// TLI: Target lowering used to determine legal types
8352// Width: Preferred width of element type
8353// VVT: Vector value type whose size we must match.
8354// Returns VecEVT and EVT - the vector type and its associated element type
8355static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8356 MVT& EVT, MVT& VecEVT) {
8357 // We start with the preferred width, make it a power of 2 and see if
8358 // we can find a vector type of that width. If not, we reduce it by
8359 // another power of 2. If we have widen the type, a vector of bytes should
8360 // always be legal.
8361 assert(TLI.isTypeLegal(VVT));
8362 unsigned EWidth = Width + 1;
8363 do {
8364 assert(EWidth > 0);
8365 EWidth = (1 << Log2_32(EWidth-1));
8366 EVT = MVT::getIntegerVT(EWidth);
8367 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8368 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8369 } while (!TLI.isTypeLegal(VecEVT) ||
8370 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8371}
8372
8373SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8374 SDValue Chain,
8375 SDValue BasePtr,
8376 const Value *SV,
8377 int SVOffset,
8378 unsigned Alignment,
8379 bool isVolatile,
8380 unsigned LdWidth,
8381 MVT ResType) {
8382 // We assume that we have good rules to handle loading power of two loads so
8383 // we break down the operations to power of 2 loads. The strategy is to
8384 // load the largest power of 2 that we can easily transform to a legal vector
8385 // and then insert into that vector, and the cast the result into the legal
8386 // vector that we want. This avoids unnecessary stack converts.
8387 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8388 // the load is nonvolatile, we an use a wider load for the value.
8389 // Find a vector length we can load a large chunk
8390 MVT EVT, VecEVT;
8391 unsigned EVTWidth;
8392 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8393 EVTWidth = EVT.getSizeInBits();
8394
8395 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8396 isVolatile, Alignment);
8397 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8398 LdChain.push_back(LdOp.getValue(1));
8399
8400 // Check if we can load the element with one instruction
8401 if (LdWidth == EVTWidth) {
8402 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8403 }
8404
8405 // The vector element order is endianness dependent.
8406 unsigned Idx = 1;
8407 LdWidth -= EVTWidth;
8408 unsigned Offset = 0;
8409
8410 while (LdWidth > 0) {
8411 unsigned Increment = EVTWidth / 8;
8412 Offset += Increment;
8413 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8414 DAG.getIntPtrConstant(Increment));
8415
8416 if (LdWidth < EVTWidth) {
8417 // Our current type we are using is too large, use a smaller size by
8418 // using a smaller power of 2
8419 unsigned oEVTWidth = EVTWidth;
8420 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8421 EVTWidth = EVT.getSizeInBits();
8422 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008423 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008424 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8425 }
8426
8427 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8428 SVOffset+Offset, isVolatile,
8429 MinAlign(Alignment, Offset));
8430 LdChain.push_back(LdOp.getValue(1));
8431 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8432 DAG.getIntPtrConstant(Idx++));
8433
8434 LdWidth -= EVTWidth;
8435 }
8436
8437 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8438}
8439
8440bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8441 SDValue& TFOp,
8442 SDValue Op,
8443 MVT NVT) {
8444 // TODO: Add support for ConcatVec and the ability to load many vector
8445 // types (e.g., v4i8). This will not work when a vector register
8446 // to memory mapping is strange (e.g., vector elements are not
8447 // stored in some sequential order).
8448
8449 // It must be true that the widen vector type is bigger than where
8450 // we need to load from.
8451 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8452 MVT LdVT = LD->getMemoryVT();
8453 assert(LdVT.isVector() && NVT.isVector());
8454 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8455
8456 // Load information
8457 SDValue Chain = LD->getChain();
8458 SDValue BasePtr = LD->getBasePtr();
8459 int SVOffset = LD->getSrcValueOffset();
8460 unsigned Alignment = LD->getAlignment();
8461 bool isVolatile = LD->isVolatile();
8462 const Value *SV = LD->getSrcValue();
8463 unsigned int LdWidth = LdVT.getSizeInBits();
8464
8465 // Load value as a large register
8466 SDValueVector LdChain;
8467 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8468 Alignment, isVolatile, LdWidth, NVT);
8469
8470 if (LdChain.size() == 1) {
8471 TFOp = LdChain[0];
8472 return true;
8473 }
8474 else {
8475 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8476 return false;
8477 }
8478}
8479
8480
8481void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8482 SDValue Chain,
8483 SDValue BasePtr,
8484 const Value *SV,
8485 int SVOffset,
8486 unsigned Alignment,
8487 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008488 SDValue ValOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008489 unsigned StWidth) {
8490 // Breaks the stores into a series of power of 2 width stores. For any
8491 // width, we convert the vector to the vector of element size that we
8492 // want to store. This avoids requiring a stack convert.
8493
8494 // Find a width of the element type we can store with
8495 MVT VVT = ValOp.getValueType();
8496 MVT EVT, VecEVT;
8497 unsigned EVTWidth;
8498 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8499 EVTWidth = EVT.getSizeInBits();
8500
8501 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8502 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008503 DAG.getIntPtrConstant(0));
Mon P Wang0c397192008-10-30 08:01:45 +00008504 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8505 isVolatile, Alignment);
8506 StChain.push_back(StOp);
8507
8508 // Check if we are done
8509 if (StWidth == EVTWidth) {
8510 return;
8511 }
8512
8513 unsigned Idx = 1;
8514 StWidth -= EVTWidth;
8515 unsigned Offset = 0;
8516
8517 while (StWidth > 0) {
8518 unsigned Increment = EVTWidth / 8;
8519 Offset += Increment;
8520 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8521 DAG.getIntPtrConstant(Increment));
8522
8523 if (StWidth < EVTWidth) {
8524 // Our current type we are using is too large, use a smaller size by
8525 // using a smaller power of 2
8526 unsigned oEVTWidth = EVTWidth;
8527 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8528 EVTWidth = EVT.getSizeInBits();
8529 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008530 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008531 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8532 }
8533
8534 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008535 DAG.getIntPtrConstant(Idx++));
Mon P Wang0c397192008-10-30 08:01:45 +00008536 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8537 SVOffset + Offset, isVolatile,
8538 MinAlign(Alignment, Offset)));
8539 StWidth -= EVTWidth;
8540 }
8541}
8542
8543
8544SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8545 SDValue Chain,
8546 SDValue BasePtr) {
8547 // TODO: It might be cleaner if we can use SplitVector and have more legal
8548 // vector types that can be stored into memory (e.g., v4xi8 can
8549 // be stored as a word). This will not work when a vector register
8550 // to memory mapping is strange (e.g., vector elements are not
8551 // stored in some sequential order).
8552
8553 MVT StVT = ST->getMemoryVT();
8554 SDValue ValOp = ST->getValue();
8555
8556 // Check if we have widen this node with another value
8557 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8558 if (I != WidenNodes.end())
8559 ValOp = I->second;
8560
8561 MVT VVT = ValOp.getValueType();
8562
8563 // It must be true that we the widen vector type is bigger than where
8564 // we need to store.
8565 assert(StVT.isVector() && VVT.isVector());
8566 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8567 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8568
8569 // Store value
8570 SDValueVector StChain;
8571 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8572 ST->getSrcValueOffset(), ST->getAlignment(),
8573 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8574 if (StChain.size() == 1)
8575 return StChain[0];
8576 else
8577 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8578}
8579
8580
Chris Lattner3e928bb2005-01-07 07:47:09 +00008581// SelectionDAG::Legalize - This is the entry point for the file.
8582//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00008583void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008584 /// run - This is the main entry point to this class.
8585 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00008586 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008587}
8588