blob: 445d32ff1b84b3b214e3dacc56176b8e75ff219d [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()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000633 // Expand to a bitconvert of the value to the integer type of the
634 // same size, then a (misaligned) int store.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000635 MVT intVT;
636 if (VT.is128BitVector() || VT == MVT::ppcf128 || VT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000637 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000638 else if (VT.is64BitVector() || VT==MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000639 intVT = MVT::i64;
640 else if (VT==MVT::f32)
641 intVT = MVT::i32;
642 else
Dale Johannesencd9f1742008-02-28 18:36:51 +0000643 assert(0 && "Unaligned store of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000644
Dan Gohman475871a2008-07-27 21:46:04 +0000645 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, intVT, Val);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000646 return DAG.getStore(Chain, Result, Ptr, ST->getSrcValue(),
647 SVOffset, ST->isVolatile(), Alignment);
648 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000649 assert(ST->getMemoryVT().isInteger() &&
650 !ST->getMemoryVT().isVector() &&
Dale Johannesen907f28c2007-09-08 19:29:23 +0000651 "Unaligned store of unknown type.");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000652 // Get the half-size VT
Duncan Sands83ec4b62008-06-06 12:08:01 +0000653 MVT NewStoredVT =
654 (MVT::SimpleValueType)(ST->getMemoryVT().getSimpleVT() - 1);
655 int NumBits = NewStoredVT.getSizeInBits();
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000656 int IncrementSize = NumBits / 8;
657
658 // Divide the stored value in two parts.
Dan Gohman475871a2008-07-27 21:46:04 +0000659 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
660 SDValue Lo = Val;
661 SDValue Hi = DAG.getNode(ISD::SRL, VT, Val, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000662
663 // Store the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000664 SDValue Store1, Store2;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000665 Store1 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Lo:Hi, Ptr,
666 ST->getSrcValue(), SVOffset, NewStoredVT,
667 ST->isVolatile(), Alignment);
668 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
669 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
Duncan Sandsdc846502007-10-28 12:59:45 +0000670 Alignment = MinAlign(Alignment, IncrementSize);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000671 Store2 = DAG.getTruncStore(Chain, TLI.isLittleEndian()?Hi:Lo, Ptr,
672 ST->getSrcValue(), SVOffset + IncrementSize,
673 NewStoredVT, ST->isVolatile(), Alignment);
674
675 return DAG.getNode(ISD::TokenFactor, MVT::Other, Store1, Store2);
676}
677
678/// ExpandUnalignedLoad - Expands an unaligned load to 2 half-size loads.
679static
Dan Gohman475871a2008-07-27 21:46:04 +0000680SDValue ExpandUnalignedLoad(LoadSDNode *LD, SelectionDAG &DAG,
681 TargetLowering &TLI) {
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000682 int SVOffset = LD->getSrcValueOffset();
Dan Gohman475871a2008-07-27 21:46:04 +0000683 SDValue Chain = LD->getChain();
684 SDValue Ptr = LD->getBasePtr();
Duncan Sands83ec4b62008-06-06 12:08:01 +0000685 MVT VT = LD->getValueType(0);
686 MVT LoadedVT = LD->getMemoryVT();
687 if (VT.isFloatingPoint() || VT.isVector()) {
Dale Johannesen907f28c2007-09-08 19:29:23 +0000688 // Expand to a (misaligned) integer load of the same size,
Dale Johannesen8155d642008-02-27 22:36:00 +0000689 // then bitconvert to floating point or vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000690 MVT intVT;
691 if (LoadedVT.is128BitVector() ||
Dale Johannesen3c8b59c2008-03-01 03:40:57 +0000692 LoadedVT == MVT::ppcf128 || LoadedVT == MVT::f128)
Dale Johannesen8155d642008-02-27 22:36:00 +0000693 intVT = MVT::i128;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000694 else if (LoadedVT.is64BitVector() || LoadedVT == MVT::f64)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000695 intVT = MVT::i64;
Chris Lattnere400af82007-11-19 21:38:03 +0000696 else if (LoadedVT == MVT::f32)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000697 intVT = MVT::i32;
698 else
Dale Johannesen8155d642008-02-27 22:36:00 +0000699 assert(0 && "Unaligned load of unsupported type");
Dale Johannesen907f28c2007-09-08 19:29:23 +0000700
Dan Gohman475871a2008-07-27 21:46:04 +0000701 SDValue newLoad = DAG.getLoad(intVT, Chain, Ptr, LD->getSrcValue(),
Dale Johannesen907f28c2007-09-08 19:29:23 +0000702 SVOffset, LD->isVolatile(),
703 LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +0000704 SDValue Result = DAG.getNode(ISD::BIT_CONVERT, LoadedVT, newLoad);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000705 if (VT.isFloatingPoint() && LoadedVT != VT)
Dale Johannesen907f28c2007-09-08 19:29:23 +0000706 Result = DAG.getNode(ISD::FP_EXTEND, VT, Result);
707
Dan Gohman475871a2008-07-27 21:46:04 +0000708 SDValue Ops[] = { Result, Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000709 return DAG.getMergeValues(Ops, 2);
Dale Johannesen907f28c2007-09-08 19:29:23 +0000710 }
Duncan Sands83ec4b62008-06-06 12:08:01 +0000711 assert(LoadedVT.isInteger() && !LoadedVT.isVector() &&
Chris Lattnere400af82007-11-19 21:38:03 +0000712 "Unaligned load of unsupported type.");
713
Dale Johannesen8155d642008-02-27 22:36:00 +0000714 // Compute the new VT that is half the size of the old one. This is an
715 // integer MVT.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000716 unsigned NumBits = LoadedVT.getSizeInBits();
717 MVT NewLoadedVT;
718 NewLoadedVT = MVT::getIntegerVT(NumBits/2);
Chris Lattnere400af82007-11-19 21:38:03 +0000719 NumBits >>= 1;
720
721 unsigned Alignment = LD->getAlignment();
722 unsigned IncrementSize = NumBits / 8;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000723 ISD::LoadExtType HiExtType = LD->getExtensionType();
724
725 // If the original load is NON_EXTLOAD, the hi part load must be ZEXTLOAD.
726 if (HiExtType == ISD::NON_EXTLOAD)
727 HiExtType = ISD::ZEXTLOAD;
728
729 // Load the value in two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000730 SDValue Lo, Hi;
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000731 if (TLI.isLittleEndian()) {
732 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
733 SVOffset, NewLoadedVT, LD->isVolatile(), Alignment);
734 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
735 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
736 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(),
737 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000738 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000739 } else {
740 Hi = DAG.getExtLoad(HiExtType, VT, Chain, Ptr, LD->getSrcValue(), SVOffset,
741 NewLoadedVT,LD->isVolatile(), Alignment);
742 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
743 DAG.getConstant(IncrementSize, TLI.getPointerTy()));
744 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, VT, Chain, Ptr, LD->getSrcValue(),
745 SVOffset + IncrementSize, NewLoadedVT, LD->isVolatile(),
Duncan Sandsdc846502007-10-28 12:59:45 +0000746 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000747 }
748
749 // aggregate the two parts
Dan Gohman475871a2008-07-27 21:46:04 +0000750 SDValue ShiftAmount = DAG.getConstant(NumBits, TLI.getShiftAmountTy());
751 SDValue Result = DAG.getNode(ISD::SHL, VT, Hi, ShiftAmount);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000752 Result = DAG.getNode(ISD::OR, VT, Result, Lo);
753
Dan Gohman475871a2008-07-27 21:46:04 +0000754 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000755 Hi.getValue(1));
756
Dan Gohman475871a2008-07-27 21:46:04 +0000757 SDValue Ops[] = { Result, TF };
Duncan Sands4bdcb612008-07-02 17:40:58 +0000758 return DAG.getMergeValues(Ops, 2);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +0000759}
Evan Cheng912095b2007-01-04 21:56:39 +0000760
Dan Gohman82669522007-10-11 23:57:53 +0000761/// UnrollVectorOp - We know that the given vector has a legal type, however
762/// the operation it performs is not legal and is an operation that we have
763/// no way of lowering. "Unroll" the vector, splitting out the scalars and
764/// operating on each element individually.
Dan Gohman475871a2008-07-27 21:46:04 +0000765SDValue SelectionDAGLegalize::UnrollVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000766 MVT VT = Op.getValueType();
Dan Gohman82669522007-10-11 23:57:53 +0000767 assert(isTypeLegal(VT) &&
768 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000769 assert(Op.getNode()->getNumValues() == 1 &&
Dan Gohman82669522007-10-11 23:57:53 +0000770 "Can't unroll a vector with multiple results!");
Duncan Sands83ec4b62008-06-06 12:08:01 +0000771 unsigned NE = VT.getVectorNumElements();
772 MVT EltVT = VT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000773
Dan Gohman475871a2008-07-27 21:46:04 +0000774 SmallVector<SDValue, 8> Scalars;
775 SmallVector<SDValue, 4> Operands(Op.getNumOperands());
Dan Gohman82669522007-10-11 23:57:53 +0000776 for (unsigned i = 0; i != NE; ++i) {
777 for (unsigned j = 0; j != Op.getNumOperands(); ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +0000778 SDValue Operand = Op.getOperand(j);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000779 MVT OperandVT = Operand.getValueType();
780 if (OperandVT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +0000781 // A vector operand; extract a single element.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000782 MVT OperandEltVT = OperandVT.getVectorElementType();
Dan Gohman82669522007-10-11 23:57:53 +0000783 Operands[j] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
784 OperandEltVT,
785 Operand,
786 DAG.getConstant(i, MVT::i32));
787 } else {
788 // A scalar operand; just use it as is.
789 Operands[j] = Operand;
790 }
791 }
Mon P Wange9f10152008-12-09 05:46:39 +0000792
793 switch (Op.getOpcode()) {
794 default:
795 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT,
796 &Operands[0], Operands.size()));
797 break;
798 case ISD::SHL:
799 case ISD::SRA:
800 case ISD::SRL:
801 Scalars.push_back(DAG.getNode(Op.getOpcode(), EltVT, Operands[0],
802 LegalizeShiftAmount(Operands[1])));
803 break;
804 }
Dan Gohman82669522007-10-11 23:57:53 +0000805 }
806
807 return DAG.getNode(ISD::BUILD_VECTOR, VT, &Scalars[0], Scalars.size());
808}
809
Duncan Sands007f9842008-01-10 10:28:30 +0000810/// GetFPLibCall - Return the right libcall for the given floating point type.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000811static RTLIB::Libcall GetFPLibCall(MVT VT,
Duncan Sands007f9842008-01-10 10:28:30 +0000812 RTLIB::Libcall Call_F32,
813 RTLIB::Libcall Call_F64,
814 RTLIB::Libcall Call_F80,
815 RTLIB::Libcall Call_PPCF128) {
816 return
817 VT == MVT::f32 ? Call_F32 :
818 VT == MVT::f64 ? Call_F64 :
819 VT == MVT::f80 ? Call_F80 :
820 VT == MVT::ppcf128 ? Call_PPCF128 :
821 RTLIB::UNKNOWN_LIBCALL;
822}
823
Nate Begeman68679912008-04-25 18:07:40 +0000824/// PerformInsertVectorEltInMemory - Some target cannot handle a variable
825/// insertion index for the INSERT_VECTOR_ELT instruction. In this case, it
826/// is necessary to spill the vector being inserted into to memory, perform
827/// the insert there, and then read the result back.
Dan Gohman475871a2008-07-27 21:46:04 +0000828SDValue SelectionDAGLegalize::
829PerformInsertVectorEltInMemory(SDValue Vec, SDValue Val, SDValue Idx) {
830 SDValue Tmp1 = Vec;
831 SDValue Tmp2 = Val;
832 SDValue Tmp3 = Idx;
Nate Begeman68679912008-04-25 18:07:40 +0000833
834 // If the target doesn't support this, we have to spill the input vector
835 // to a temporary stack slot, update the element, then reload it. This is
836 // badness. We could also load the value into a vector register (either
837 // with a "move to register" or "extload into register" instruction, then
838 // permute it into place, if the idx is a constant and if the idx is
839 // supported by the target.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000840 MVT VT = Tmp1.getValueType();
841 MVT EltVT = VT.getVectorElementType();
842 MVT IdxVT = Tmp3.getValueType();
843 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +0000844 SDValue StackPtr = DAG.CreateStackTemporary(VT);
Nate Begeman68679912008-04-25 18:07:40 +0000845
Gabor Greifba36cb52008-08-28 21:40:38 +0000846 int SPFI = cast<FrameIndexSDNode>(StackPtr.getNode())->getIndex();
Nate Begeman68679912008-04-25 18:07:40 +0000847
848 // Store the vector.
Dan Gohman475871a2008-07-27 21:46:04 +0000849 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Tmp1, StackPtr,
Mon P Wang0c397192008-10-30 08:01:45 +0000850 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000851
852 // Truncate or zero extend offset to target pointer type.
Duncan Sands8e4eb092008-06-08 20:54:56 +0000853 unsigned CastOpc = IdxVT.bitsGT(PtrVT) ? ISD::TRUNCATE : ISD::ZERO_EXTEND;
Nate Begeman68679912008-04-25 18:07:40 +0000854 Tmp3 = DAG.getNode(CastOpc, PtrVT, Tmp3);
855 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000856 unsigned EltSize = EltVT.getSizeInBits()/8;
Nate Begeman68679912008-04-25 18:07:40 +0000857 Tmp3 = DAG.getNode(ISD::MUL, IdxVT, Tmp3,DAG.getConstant(EltSize, IdxVT));
Dan Gohman475871a2008-07-27 21:46:04 +0000858 SDValue StackPtr2 = DAG.getNode(ISD::ADD, IdxVT, Tmp3, StackPtr);
Nate Begeman68679912008-04-25 18:07:40 +0000859 // Store the scalar value.
860 Ch = DAG.getTruncStore(Ch, Tmp2, StackPtr2,
Dan Gohmana54cf172008-07-11 22:44:52 +0000861 PseudoSourceValue::getFixedStack(SPFI), 0, EltVT);
Nate Begeman68679912008-04-25 18:07:40 +0000862 // Load the updated vector.
Dan Gohmana54cf172008-07-11 22:44:52 +0000863 return DAG.getLoad(VT, Ch, StackPtr,
864 PseudoSourceValue::getFixedStack(SPFI), 0);
Nate Begeman68679912008-04-25 18:07:40 +0000865}
866
Mon P Wange9f10152008-12-09 05:46:39 +0000867SDValue SelectionDAGLegalize::LegalizeShiftAmount(SDValue ShiftAmt) {
868 if (TLI.getShiftAmountTy().bitsLT(ShiftAmt.getValueType()))
869 return DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), ShiftAmt);
870
871 if (TLI.getShiftAmountTy().bitsGT(ShiftAmt.getValueType()))
872 return DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), ShiftAmt);
873
874 return ShiftAmt;
875}
876
877
Dan Gohmana3466152007-07-13 20:14:11 +0000878/// LegalizeOp - We know that the specified value has a legal type, and
879/// that its operands are legal. Now ensure that the operation itself
880/// is legal, recursively ensuring that the operands' operations remain
881/// legal.
Dan Gohman475871a2008-07-27 21:46:04 +0000882SDValue SelectionDAGLegalize::LegalizeOp(SDValue Op) {
Chris Lattner09ec1b02007-08-25 01:00:22 +0000883 if (Op.getOpcode() == ISD::TargetConstant) // Allow illegal target nodes.
884 return Op;
885
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000886 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000887 "Caller should expand or promote operands that are not legal!");
Gabor Greifba36cb52008-08-28 21:40:38 +0000888 SDNode *Node = Op.getNode();
Chris Lattnere3304a32005-01-08 20:35:13 +0000889
Chris Lattner3e928bb2005-01-07 07:47:09 +0000890 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000891 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000892 if (Node->getNumValues() > 1) {
893 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerc7029802006-03-18 01:44:44 +0000894 if (getTypeAction(Node->getValueType(i)) != Legal) {
895 HandleOp(Op.getValue(i));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000896 assert(LegalizedNodes.count(Op) &&
Chris Lattnerc7029802006-03-18 01:44:44 +0000897 "Handling didn't add legal operands!");
Chris Lattner03c85462005-01-15 05:21:40 +0000898 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000899 }
900 }
901
Chris Lattner45982da2005-05-12 16:53:42 +0000902 // Note that LegalizeOp may be reentered even from single-use nodes, which
903 // means that we always must cache transformed nodes.
Dan Gohman475871a2008-07-27 21:46:04 +0000904 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattnere1bd8222005-01-11 05:57:22 +0000905 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000906
Dan Gohman475871a2008-07-27 21:46:04 +0000907 SDValue Tmp1, Tmp2, Tmp3, Tmp4;
908 SDValue Result = Op;
Chris Lattner456a93a2006-01-28 07:39:30 +0000909 bool isCustom = false;
910
Chris Lattner3e928bb2005-01-07 07:47:09 +0000911 switch (Node->getOpcode()) {
Chris Lattner948c1b12006-01-28 08:31:04 +0000912 case ISD::FrameIndex:
913 case ISD::EntryToken:
914 case ISD::Register:
915 case ISD::BasicBlock:
916 case ISD::TargetFrameIndex:
Nate Begeman37efe672006-04-22 18:53:45 +0000917 case ISD::TargetJumpTable:
Chris Lattner948c1b12006-01-28 08:31:04 +0000918 case ISD::TargetConstant:
Chris Lattner3181a772006-01-29 06:26:56 +0000919 case ISD::TargetConstantFP:
Chris Lattner948c1b12006-01-28 08:31:04 +0000920 case ISD::TargetConstantPool:
921 case ISD::TargetGlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000922 case ISD::TargetGlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000923 case ISD::TargetExternalSymbol:
Chris Lattner948c1b12006-01-28 08:31:04 +0000924 case ISD::VALUETYPE:
925 case ISD::SRCVALUE:
Dan Gohman69de1932008-02-06 22:27:42 +0000926 case ISD::MEMOPERAND:
Chris Lattner948c1b12006-01-28 08:31:04 +0000927 case ISD::CONDCODE:
Duncan Sands276dcbd2008-03-21 09:14:45 +0000928 case ISD::ARG_FLAGS:
Chris Lattner948c1b12006-01-28 08:31:04 +0000929 // Primitives must all be legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +0000930 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Chris Lattner948c1b12006-01-28 08:31:04 +0000931 "This must be legal!");
932 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000933 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000934 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
935 // If this is a target node, legalize it by legalizing the operands then
936 // passing it through.
Dan Gohman475871a2008-07-27 21:46:04 +0000937 SmallVector<SDValue, 8> Ops;
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000938 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000939 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +0000940
Chris Lattnerf06f35e2006-08-08 01:09:31 +0000941 Result = DAG.UpdateNodeOperands(Result.getValue(0), &Ops[0], Ops.size());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000942
943 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
944 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +0000945 return Result.getValue(Op.getResNo());
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000946 }
947 // Otherwise this is an unhandled builtin node. splat.
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000948#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +0000949 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +0000950#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +0000951 assert(0 && "Do not know how to legalize this operator!");
952 abort();
Lauro Ramos Venancio0d3b6782007-04-20 23:02:39 +0000953 case ISD::GLOBAL_OFFSET_TABLE:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000954 case ISD::GlobalAddress:
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000955 case ISD::GlobalTLSAddress:
Bill Wendling056292f2008-09-16 21:48:12 +0000956 case ISD::ExternalSymbol:
Nate Begeman37efe672006-04-22 18:53:45 +0000957 case ISD::ConstantPool:
958 case ISD::JumpTable: // Nothing to do.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000959 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
960 default: assert(0 && "This action is not supported yet!");
Chris Lattner948c1b12006-01-28 08:31:04 +0000961 case TargetLowering::Custom:
962 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000963 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner948c1b12006-01-28 08:31:04 +0000964 // FALLTHROUGH if the target doesn't want to lower this op after all.
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000965 case TargetLowering::Legal:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000966 break;
967 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000968 break;
Nate Begemanbcc5f362007-01-29 22:58:52 +0000969 case ISD::FRAMEADDR:
970 case ISD::RETURNADDR:
971 // The only option for these nodes is to custom lower them. If the target
972 // does not custom lower them, then return zero.
973 Tmp1 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000974 if (Tmp1.getNode())
Nate Begemanbcc5f362007-01-29 22:58:52 +0000975 Result = Tmp1;
976 else
977 Result = DAG.getConstant(0, TLI.getPointerTy());
978 break;
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000979 case ISD::FRAME_TO_ARGS_OFFSET: {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000980 MVT VT = Node->getValueType(0);
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000981 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
982 default: assert(0 && "This action is not supported yet!");
983 case TargetLowering::Custom:
984 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +0000985 if (Result.getNode()) break;
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000986 // Fall Thru
987 case TargetLowering::Legal:
988 Result = DAG.getConstant(0, VT);
989 break;
990 }
Anton Korobeynikov055c5442007-08-29 23:18:48 +0000991 }
Anton Korobeynikov066f7b42007-08-29 19:28:29 +0000992 break;
Jim Laskey2bc210d2007-02-22 15:37:19 +0000993 case ISD::EXCEPTIONADDR: {
994 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +0000995 MVT VT = Node->getValueType(0);
Jim Laskey2bc210d2007-02-22 15:37:19 +0000996 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
997 default: assert(0 && "This action is not supported yet!");
998 case TargetLowering::Expand: {
Jim Laskey8782d482007-02-28 20:43:58 +0000999 unsigned Reg = TLI.getExceptionAddressRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001000 Result = DAG.getCopyFromReg(Tmp1, Reg, VT);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001001 }
1002 break;
1003 case TargetLowering::Custom:
1004 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001005 if (Result.getNode()) break;
Jim Laskey2bc210d2007-02-22 15:37:19 +00001006 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001007 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001008 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp1 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001009 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey2bc210d2007-02-22 15:37:19 +00001010 break;
1011 }
1012 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001013 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001014 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001015
Gabor Greifba36cb52008-08-28 21:40:38 +00001016 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001017 "Cannot return more than two values!");
1018
1019 // Since we produced two values, make sure to remember that we
1020 // legalized both of them.
1021 Tmp1 = LegalizeOp(Result);
1022 Tmp2 = LegalizeOp(Result.getValue(1));
1023 AddLegalizedOperand(Op.getValue(0), Tmp1);
1024 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001025 return Op.getResNo() ? Tmp2 : Tmp1;
Jim Laskey8782d482007-02-28 20:43:58 +00001026 case ISD::EHSELECTION: {
1027 Tmp1 = LegalizeOp(Node->getOperand(0));
1028 Tmp2 = LegalizeOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00001029 MVT VT = Node->getValueType(0);
Jim Laskey8782d482007-02-28 20:43:58 +00001030 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1031 default: assert(0 && "This action is not supported yet!");
1032 case TargetLowering::Expand: {
1033 unsigned Reg = TLI.getExceptionSelectorRegister();
Duncan Sandsb027fa02007-12-31 18:35:50 +00001034 Result = DAG.getCopyFromReg(Tmp2, Reg, VT);
Jim Laskey8782d482007-02-28 20:43:58 +00001035 }
1036 break;
1037 case TargetLowering::Custom:
1038 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001039 if (Result.getNode()) break;
Jim Laskey8782d482007-02-28 20:43:58 +00001040 // Fall Thru
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001041 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001042 SDValue Ops[] = { DAG.getConstant(0, VT), Tmp2 };
Duncan Sands4bdcb612008-07-02 17:40:58 +00001043 Result = DAG.getMergeValues(Ops, 2);
Jim Laskey8782d482007-02-28 20:43:58 +00001044 break;
1045 }
1046 }
Chris Lattnereb7f34f2007-04-27 17:12:52 +00001047 }
Gabor Greifba36cb52008-08-28 21:40:38 +00001048 if (Result.getNode()->getNumValues() == 1) break;
Duncan Sandsb027fa02007-12-31 18:35:50 +00001049
Gabor Greifba36cb52008-08-28 21:40:38 +00001050 assert(Result.getNode()->getNumValues() == 2 &&
Duncan Sandsb027fa02007-12-31 18:35:50 +00001051 "Cannot return more than two values!");
1052
1053 // Since we produced two values, make sure to remember that we
1054 // legalized both of them.
1055 Tmp1 = LegalizeOp(Result);
1056 Tmp2 = LegalizeOp(Result.getValue(1));
1057 AddLegalizedOperand(Op.getValue(0), Tmp1);
1058 AddLegalizedOperand(Op.getValue(1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001059 return Op.getResNo() ? Tmp2 : Tmp1;
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001060 case ISD::EH_RETURN: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001061 MVT VT = Node->getValueType(0);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001062 // The only "good" option for this node is to custom lower it.
1063 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
1064 default: assert(0 && "This action is not supported at all!");
1065 case TargetLowering::Custom:
1066 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001067 if (Result.getNode()) break;
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001068 // Fall Thru
1069 case TargetLowering::Legal:
1070 // Target does not know, how to lower this, lower to noop
1071 Result = LegalizeOp(Node->getOperand(0));
1072 break;
1073 }
Nick Lewycky6d4b7112007-07-14 15:11:14 +00001074 }
Anton Korobeynikov2365f512007-07-14 14:06:15 +00001075 break;
Chris Lattner08951a32005-09-02 01:15:01 +00001076 case ISD::AssertSext:
1077 case ISD::AssertZext:
1078 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001079 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner08951a32005-09-02 01:15:01 +00001080 break;
Chris Lattner308575b2005-11-20 22:56:56 +00001081 case ISD::MERGE_VALUES:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001082 // Legalize eliminates MERGE_VALUES nodes.
Gabor Greif99a6cb92008-08-26 22:36:50 +00001083 Result = Node->getOperand(Op.getResNo());
Chris Lattner456a93a2006-01-28 07:39:30 +00001084 break;
Chris Lattner69a52152005-01-14 22:38:01 +00001085 case ISD::CopyFromReg:
1086 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner7310fb12005-12-18 15:27:43 +00001087 Result = Op.getValue(0);
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001088 if (Node->getNumValues() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001089 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner7310fb12005-12-18 15:27:43 +00001090 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001091 assert(Node->getNumValues() == 3 && "Invalid copyfromreg!");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001092 if (Node->getNumOperands() == 3) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00001093 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001094 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
1095 } else {
1096 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
1097 }
Chris Lattner7310fb12005-12-18 15:27:43 +00001098 AddLegalizedOperand(Op.getValue(2), Result.getValue(2));
1099 }
Chris Lattner13c184d2005-01-28 06:27:38 +00001100 // Since CopyFromReg produces two values, make sure to remember that we
1101 // legalized both of them.
1102 AddLegalizedOperand(Op.getValue(0), Result);
1103 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001104 return Result.getValue(Op.getResNo());
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001105 case ISD::UNDEF: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001106 MVT VT = Op.getValueType();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001107 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +00001108 default: assert(0 && "This action is not supported yet!");
1109 case TargetLowering::Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00001110 if (VT.isInteger())
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001111 Result = DAG.getConstant(0, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001112 else if (VT.isFloatingPoint())
1113 Result = DAG.getConstantFP(APFloat(APInt(VT.getSizeInBits(), 0)),
Dale Johannesenf41db212007-09-26 17:26:49 +00001114 VT);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001115 else
1116 assert(0 && "Unknown value type!");
1117 break;
Nate Begemanea19cd52005-04-02 00:41:14 +00001118 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001119 break;
1120 }
1121 break;
1122 }
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001123
Chris Lattner48b61a72006-03-28 00:40:33 +00001124 case ISD::INTRINSIC_W_CHAIN:
1125 case ISD::INTRINSIC_WO_CHAIN:
1126 case ISD::INTRINSIC_VOID: {
Dan Gohman475871a2008-07-27 21:46:04 +00001127 SmallVector<SDValue, 8> Ops;
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001128 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1129 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001130 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner10d7fa62006-03-26 09:12:51 +00001131
1132 // Allow the target to custom lower its intrinsics if it wants to.
Chris Lattner48b61a72006-03-28 00:40:33 +00001133 if (TLI.getOperationAction(Node->getOpcode(), MVT::Other) ==
Chris Lattner10d7fa62006-03-26 09:12:51 +00001134 TargetLowering::Custom) {
1135 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001136 if (Tmp3.getNode()) Result = Tmp3;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001137 }
1138
Gabor Greifba36cb52008-08-28 21:40:38 +00001139 if (Result.getNode()->getNumValues() == 1) break;
Chris Lattner13fc2f12006-03-27 20:28:29 +00001140
1141 // Must have return value and chain result.
Gabor Greifba36cb52008-08-28 21:40:38 +00001142 assert(Result.getNode()->getNumValues() == 2 &&
Chris Lattner13fc2f12006-03-27 20:28:29 +00001143 "Cannot return more than two values!");
1144
1145 // Since loads produce two values, make sure to remember that we
1146 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001147 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1148 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001149 return Result.getValue(Op.getResNo());
Chris Lattnerd1f04d42006-03-24 02:26:29 +00001150 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001151
Dan Gohman7f460202008-06-30 20:59:49 +00001152 case ISD::DBG_STOPPOINT:
1153 assert(Node->getNumOperands() == 1 && "Invalid DBG_STOPPOINT node!");
Chris Lattner36ce6912005-11-29 06:21:05 +00001154 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input chain.
1155
Dan Gohman7f460202008-06-30 20:59:49 +00001156 switch (TLI.getOperationAction(ISD::DBG_STOPPOINT, MVT::Other)) {
Chris Lattner36ce6912005-11-29 06:21:05 +00001157 case TargetLowering::Promote:
1158 default: assert(0 && "This action is not supported yet!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001159 case TargetLowering::Expand: {
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001160 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001161 bool useDEBUG_LOC = TLI.isOperationLegal(ISD::DEBUG_LOC, MVT::Other);
Dan Gohman44066042008-07-01 00:05:16 +00001162 bool useLABEL = TLI.isOperationLegal(ISD::DBG_LABEL, MVT::Other);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001163
Dan Gohman7f460202008-06-30 20:59:49 +00001164 const DbgStopPointSDNode *DSP = cast<DbgStopPointSDNode>(Node);
Jim Laskey44c3b9f2007-01-26 21:22:28 +00001165 if (MMI && (useDEBUG_LOC || useLABEL)) {
Dan Gohman7f460202008-06-30 20:59:49 +00001166 const CompileUnitDesc *CompileUnit = DSP->getCompileUnit();
1167 unsigned SrcFile = MMI->RecordSource(CompileUnit);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001168
Dan Gohman7f460202008-06-30 20:59:49 +00001169 unsigned Line = DSP->getLine();
1170 unsigned Col = DSP->getColumn();
Jim Laskeyabf6d172006-01-05 01:25:28 +00001171
1172 if (useDEBUG_LOC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001173 SDValue Ops[] = { Tmp1, DAG.getConstant(Line, MVT::i32),
Evan Cheng71e86852008-07-08 20:06:39 +00001174 DAG.getConstant(Col, MVT::i32),
1175 DAG.getConstant(SrcFile, MVT::i32) };
1176 Result = DAG.getNode(ISD::DEBUG_LOC, MVT::Other, Ops, 4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001177 } else {
Evan Chenga647c922008-02-01 02:05:57 +00001178 unsigned ID = MMI->RecordSourceLine(Line, Col, SrcFile);
Dan Gohman44066042008-07-01 00:05:16 +00001179 Result = DAG.getLabel(ISD::DBG_LABEL, Tmp1, ID);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001180 }
Jim Laskeye81aecb2005-12-21 20:51:37 +00001181 } else {
1182 Result = Tmp1; // chain
1183 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001184 break;
Chris Lattnere7736732005-12-18 23:54:29 +00001185 }
Evan Cheng71e86852008-07-08 20:06:39 +00001186 case TargetLowering::Legal: {
1187 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
1188 if (Action == Legal && Tmp1 == Node->getOperand(0))
1189 break;
1190
Dan Gohman475871a2008-07-27 21:46:04 +00001191 SmallVector<SDValue, 8> Ops;
Evan Cheng71e86852008-07-08 20:06:39 +00001192 Ops.push_back(Tmp1);
1193 if (Action == Legal) {
1194 Ops.push_back(Node->getOperand(1)); // line # must be legal.
1195 Ops.push_back(Node->getOperand(2)); // col # must be legal.
1196 } else {
1197 // Otherwise promote them.
1198 Ops.push_back(PromoteOp(Node->getOperand(1)));
1199 Ops.push_back(PromoteOp(Node->getOperand(2)));
Chris Lattner36ce6912005-11-29 06:21:05 +00001200 }
Evan Cheng71e86852008-07-08 20:06:39 +00001201 Ops.push_back(Node->getOperand(3)); // filename must be legal.
1202 Ops.push_back(Node->getOperand(4)); // working dir # must be legal.
1203 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner36ce6912005-11-29 06:21:05 +00001204 break;
1205 }
Evan Cheng71e86852008-07-08 20:06:39 +00001206 }
Chris Lattner36ce6912005-11-29 06:21:05 +00001207 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001208
1209 case ISD::DECLARE:
1210 assert(Node->getNumOperands() == 3 && "Invalid DECLARE node!");
1211 switch (TLI.getOperationAction(ISD::DECLARE, MVT::Other)) {
1212 default: assert(0 && "This action is not supported yet!");
1213 case TargetLowering::Legal:
1214 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1215 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1216 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the variable.
1217 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1218 break;
Chris Lattnere07415d2008-02-28 05:53:40 +00001219 case TargetLowering::Expand:
1220 Result = LegalizeOp(Node->getOperand(0));
1221 break;
Evan Chenga844bde2008-02-02 04:07:54 +00001222 }
1223 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001224
1225 case ISD::DEBUG_LOC:
Jim Laskeyabf6d172006-01-05 01:25:28 +00001226 assert(Node->getNumOperands() == 4 && "Invalid DEBUG_LOC node!");
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001227 switch (TLI.getOperationAction(ISD::DEBUG_LOC, MVT::Other)) {
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001228 default: assert(0 && "This action is not supported yet!");
Evan Cheng71e86852008-07-08 20:06:39 +00001229 case TargetLowering::Legal: {
1230 LegalizeAction Action = getTypeAction(Node->getOperand(1).getValueType());
Jim Laskeyabf6d172006-01-05 01:25:28 +00001231 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Evan Cheng71e86852008-07-08 20:06:39 +00001232 if (Action == Legal && Tmp1 == Node->getOperand(0))
1233 break;
1234 if (Action == Legal) {
1235 Tmp2 = Node->getOperand(1);
1236 Tmp3 = Node->getOperand(2);
1237 Tmp4 = Node->getOperand(3);
1238 } else {
1239 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the line #.
1240 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the col #.
1241 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize the source file id.
1242 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001243 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
Jim Laskeyabf6d172006-01-05 01:25:28 +00001244 break;
1245 }
Evan Cheng71e86852008-07-08 20:06:39 +00001246 }
Jim Laskeyabf6d172006-01-05 01:25:28 +00001247 break;
1248
Dan Gohman44066042008-07-01 00:05:16 +00001249 case ISD::DBG_LABEL:
1250 case ISD::EH_LABEL:
1251 assert(Node->getNumOperands() == 1 && "Invalid LABEL node!");
1252 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
Jim Laskeyabf6d172006-01-05 01:25:28 +00001253 default: assert(0 && "This action is not supported yet!");
1254 case TargetLowering::Legal:
1255 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Dan Gohman44066042008-07-01 00:05:16 +00001256 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001257 break;
Chris Lattnera9569f12007-03-03 19:21:38 +00001258 case TargetLowering::Expand:
1259 Result = LegalizeOp(Node->getOperand(0));
1260 break;
Jim Laskeyf5395ce2005-12-16 22:45:29 +00001261 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00001262 break;
Chris Lattner36ce6912005-11-29 06:21:05 +00001263
Evan Cheng27b7db52008-03-08 00:58:38 +00001264 case ISD::PREFETCH:
1265 assert(Node->getNumOperands() == 4 && "Invalid Prefetch node!");
1266 switch (TLI.getOperationAction(ISD::PREFETCH, MVT::Other)) {
1267 default: assert(0 && "This action is not supported yet!");
1268 case TargetLowering::Legal:
1269 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1270 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the address.
1271 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the rw specifier.
1272 Tmp4 = LegalizeOp(Node->getOperand(3)); // Legalize locality specifier.
1273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4);
1274 break;
1275 case TargetLowering::Expand:
1276 // It's a noop.
1277 Result = LegalizeOp(Node->getOperand(0));
1278 break;
1279 }
1280 break;
1281
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001282 case ISD::MEMBARRIER: {
1283 assert(Node->getNumOperands() == 6 && "Invalid MemBarrier node!");
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001284 switch (TLI.getOperationAction(ISD::MEMBARRIER, MVT::Other)) {
1285 default: assert(0 && "This action is not supported yet!");
1286 case TargetLowering::Legal: {
Dan Gohman475871a2008-07-27 21:46:04 +00001287 SDValue Ops[6];
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001288 Ops[0] = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Duncan Sandse90a6152008-02-27 08:53:44 +00001289 for (int x = 1; x < 6; ++x) {
1290 Ops[x] = Node->getOperand(x);
1291 if (!isTypeLegal(Ops[x].getValueType()))
1292 Ops[x] = PromoteOp(Ops[x]);
1293 }
Andrew Lenharthd497d9f2008-02-16 14:46:26 +00001294 Result = DAG.UpdateNodeOperands(Result, &Ops[0], 6);
1295 break;
1296 }
1297 case TargetLowering::Expand:
1298 //There is no libgcc call for this op
1299 Result = Node->getOperand(0); // Noop
1300 break;
1301 }
Andrew Lenharth22c5c1b2008-02-16 01:24:58 +00001302 break;
1303 }
1304
Dale Johannesene00a8a22008-08-28 02:44:49 +00001305 case ISD::ATOMIC_CMP_SWAP_8:
1306 case ISD::ATOMIC_CMP_SWAP_16:
1307 case ISD::ATOMIC_CMP_SWAP_32:
1308 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001309 unsigned int num_operands = 4;
1310 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001311 SDValue Ops[4];
Mon P Wang63307c32008-05-05 19:05:59 +00001312 for (unsigned int x = 0; x < num_operands; ++x)
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001313 Ops[x] = LegalizeOp(Node->getOperand(x));
Mon P Wang63307c32008-05-05 19:05:59 +00001314 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
1315
1316 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1317 default: assert(0 && "This action is not supported yet!");
1318 case TargetLowering::Custom:
1319 Result = TLI.LowerOperation(Result, DAG);
1320 break;
1321 case TargetLowering::Legal:
1322 break;
1323 }
Dan Gohman475871a2008-07-27 21:46:04 +00001324 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1325 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001326 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001327 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00001328 case ISD::ATOMIC_LOAD_ADD_8:
1329 case ISD::ATOMIC_LOAD_SUB_8:
1330 case ISD::ATOMIC_LOAD_AND_8:
1331 case ISD::ATOMIC_LOAD_OR_8:
1332 case ISD::ATOMIC_LOAD_XOR_8:
1333 case ISD::ATOMIC_LOAD_NAND_8:
1334 case ISD::ATOMIC_LOAD_MIN_8:
1335 case ISD::ATOMIC_LOAD_MAX_8:
1336 case ISD::ATOMIC_LOAD_UMIN_8:
1337 case ISD::ATOMIC_LOAD_UMAX_8:
1338 case ISD::ATOMIC_SWAP_8:
1339 case ISD::ATOMIC_LOAD_ADD_16:
1340 case ISD::ATOMIC_LOAD_SUB_16:
1341 case ISD::ATOMIC_LOAD_AND_16:
1342 case ISD::ATOMIC_LOAD_OR_16:
1343 case ISD::ATOMIC_LOAD_XOR_16:
1344 case ISD::ATOMIC_LOAD_NAND_16:
1345 case ISD::ATOMIC_LOAD_MIN_16:
1346 case ISD::ATOMIC_LOAD_MAX_16:
1347 case ISD::ATOMIC_LOAD_UMIN_16:
1348 case ISD::ATOMIC_LOAD_UMAX_16:
1349 case ISD::ATOMIC_SWAP_16:
1350 case ISD::ATOMIC_LOAD_ADD_32:
1351 case ISD::ATOMIC_LOAD_SUB_32:
1352 case ISD::ATOMIC_LOAD_AND_32:
1353 case ISD::ATOMIC_LOAD_OR_32:
1354 case ISD::ATOMIC_LOAD_XOR_32:
1355 case ISD::ATOMIC_LOAD_NAND_32:
1356 case ISD::ATOMIC_LOAD_MIN_32:
1357 case ISD::ATOMIC_LOAD_MAX_32:
1358 case ISD::ATOMIC_LOAD_UMIN_32:
1359 case ISD::ATOMIC_LOAD_UMAX_32:
1360 case ISD::ATOMIC_SWAP_32:
1361 case ISD::ATOMIC_LOAD_ADD_64:
1362 case ISD::ATOMIC_LOAD_SUB_64:
1363 case ISD::ATOMIC_LOAD_AND_64:
1364 case ISD::ATOMIC_LOAD_OR_64:
1365 case ISD::ATOMIC_LOAD_XOR_64:
1366 case ISD::ATOMIC_LOAD_NAND_64:
1367 case ISD::ATOMIC_LOAD_MIN_64:
1368 case ISD::ATOMIC_LOAD_MAX_64:
1369 case ISD::ATOMIC_LOAD_UMIN_64:
1370 case ISD::ATOMIC_LOAD_UMAX_64:
1371 case ISD::ATOMIC_SWAP_64: {
Mon P Wang63307c32008-05-05 19:05:59 +00001372 unsigned int num_operands = 3;
1373 assert(Node->getNumOperands() == num_operands && "Invalid Atomic node!");
Dan Gohman475871a2008-07-27 21:46:04 +00001374 SDValue Ops[3];
Mon P Wang63307c32008-05-05 19:05:59 +00001375 for (unsigned int x = 0; x < num_operands; ++x)
1376 Ops[x] = LegalizeOp(Node->getOperand(x));
1377 Result = DAG.UpdateNodeOperands(Result, &Ops[0], num_operands);
Duncan Sands126d9072008-07-04 11:47:58 +00001378
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001379 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001380 default: assert(0 && "This action is not supported yet!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00001381 case TargetLowering::Custom:
1382 Result = TLI.LowerOperation(Result, DAG);
1383 break;
1384 case TargetLowering::Legal:
Andrew Lenharthab0b9492008-02-21 06:45:13 +00001385 break;
1386 }
Dan Gohman475871a2008-07-27 21:46:04 +00001387 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1388 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001389 return Result.getValue(Op.getResNo());
Duncan Sands126d9072008-07-04 11:47:58 +00001390 }
Scott Michelc1513d22007-08-08 23:23:31 +00001391 case ISD::Constant: {
1392 ConstantSDNode *CN = cast<ConstantSDNode>(Node);
1393 unsigned opAction =
1394 TLI.getOperationAction(ISD::Constant, CN->getValueType(0));
1395
Chris Lattner3e928bb2005-01-07 07:47:09 +00001396 // We know we don't need to expand constants here, constants only have one
1397 // value and we check that it is fine above.
1398
Scott Michelc1513d22007-08-08 23:23:31 +00001399 if (opAction == TargetLowering::Custom) {
1400 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001401 if (Tmp1.getNode())
Scott Michelc1513d22007-08-08 23:23:31 +00001402 Result = Tmp1;
1403 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001404 break;
Scott Michelc1513d22007-08-08 23:23:31 +00001405 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001406 case ISD::ConstantFP: {
1407 // Spill FP immediates to the constant pool if the target cannot directly
1408 // codegen them. Targets often have some immediate values that can be
1409 // efficiently generated into an FP register without a load. We explicitly
1410 // leave these constants as ConstantFP nodes for the target to deal with.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001411 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
1412
Chris Lattner3181a772006-01-29 06:26:56 +00001413 switch (TLI.getOperationAction(ISD::ConstantFP, CFP->getValueType(0))) {
1414 default: assert(0 && "This action is not supported yet!");
Nate Begemane1795842008-02-14 08:57:00 +00001415 case TargetLowering::Legal:
1416 break;
Chris Lattner3181a772006-01-29 06:26:56 +00001417 case TargetLowering::Custom:
1418 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001419 if (Tmp3.getNode()) {
Chris Lattner3181a772006-01-29 06:26:56 +00001420 Result = Tmp3;
1421 break;
1422 }
1423 // FALLTHROUGH
Nate Begemane1795842008-02-14 08:57:00 +00001424 case TargetLowering::Expand: {
1425 // Check to see if this FP immediate is already legal.
1426 bool isLegal = false;
1427 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
1428 E = TLI.legal_fpimm_end(); I != E; ++I) {
1429 if (CFP->isExactlyValue(*I)) {
1430 isLegal = true;
1431 break;
1432 }
1433 }
1434 // If this is a legal constant, turn it into a TargetConstantFP node.
1435 if (isLegal)
1436 break;
Evan Cheng279101e2006-12-12 22:19:28 +00001437 Result = ExpandConstantFP(CFP, true, DAG, TLI);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001438 }
Nate Begemane1795842008-02-14 08:57:00 +00001439 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001440 break;
1441 }
Chris Lattner040c11c2005-11-09 18:48:57 +00001442 case ISD::TokenFactor:
1443 if (Node->getNumOperands() == 2) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001444 Tmp1 = LegalizeOp(Node->getOperand(0));
1445 Tmp2 = LegalizeOp(Node->getOperand(1));
1446 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1447 } else if (Node->getNumOperands() == 3) {
1448 Tmp1 = LegalizeOp(Node->getOperand(0));
1449 Tmp2 = LegalizeOp(Node->getOperand(1));
1450 Tmp3 = LegalizeOp(Node->getOperand(2));
1451 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner040c11c2005-11-09 18:48:57 +00001452 } else {
Dan Gohman475871a2008-07-27 21:46:04 +00001453 SmallVector<SDValue, 8> Ops;
Chris Lattner040c11c2005-11-09 18:48:57 +00001454 // Legalize the operands.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001455 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1456 Ops.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001457 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnera385e9b2005-01-13 17:59:25 +00001458 }
Chris Lattnera385e9b2005-01-13 17:59:25 +00001459 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00001460
1461 case ISD::FORMAL_ARGUMENTS:
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001462 case ISD::CALL:
Chris Lattnerfdfded52006-04-12 16:20:43 +00001463 // The only option for this is to custom lower it.
Chris Lattnerb248e162006-05-17 17:55:45 +00001464 Tmp3 = TLI.LowerOperation(Result.getValue(0), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001465 assert(Tmp3.getNode() && "Target didn't custom lower this node!");
Dale Johannesen0ea03562008-03-05 19:14:03 +00001466 // A call within a calling sequence must be legalized to something
1467 // other than the normal CALLSEQ_END. Violating this gets Legalize
1468 // into an infinite loop.
1469 assert ((!IsLegalizingCall ||
1470 Node->getOpcode() != ISD::CALL ||
Gabor Greifba36cb52008-08-28 21:40:38 +00001471 Tmp3.getNode()->getOpcode() != ISD::CALLSEQ_END) &&
Dale Johannesen0ea03562008-03-05 19:14:03 +00001472 "Nested CALLSEQ_START..CALLSEQ_END not supported.");
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001473
1474 // The number of incoming and outgoing values should match; unless the final
1475 // outgoing value is a flag.
Gabor Greifba36cb52008-08-28 21:40:38 +00001476 assert((Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() ||
1477 (Tmp3.getNode()->getNumValues() == Result.getNode()->getNumValues() + 1 &&
1478 Tmp3.getNode()->getValueType(Tmp3.getNode()->getNumValues() - 1) ==
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001479 MVT::Flag)) &&
Chris Lattnerb248e162006-05-17 17:55:45 +00001480 "Lowering call/formal_arguments produced unexpected # results!");
Chris Lattnere2e41732006-05-16 05:49:56 +00001481
Chris Lattnerf4ec8172006-05-16 22:53:20 +00001482 // Since CALL/FORMAL_ARGUMENTS nodes produce multiple values, make sure to
Chris Lattnere2e41732006-05-16 05:49:56 +00001483 // remember that we legalized all of them, so it doesn't get relegalized.
Gabor Greifba36cb52008-08-28 21:40:38 +00001484 for (unsigned i = 0, e = Tmp3.getNode()->getNumValues(); i != e; ++i) {
1485 if (Tmp3.getNode()->getValueType(i) == MVT::Flag)
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001486 continue;
Chris Lattnerb248e162006-05-17 17:55:45 +00001487 Tmp1 = LegalizeOp(Tmp3.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001488 if (Op.getResNo() == i)
Chris Lattnere2e41732006-05-16 05:49:56 +00001489 Tmp2 = Tmp1;
Dan Gohman475871a2008-07-27 21:46:04 +00001490 AddLegalizedOperand(SDValue(Node, i), Tmp1);
Chris Lattnere2e41732006-05-16 05:49:56 +00001491 }
1492 return Tmp2;
Christopher Lamb557c3632007-07-26 07:34:40 +00001493 case ISD::EXTRACT_SUBREG: {
1494 Tmp1 = LegalizeOp(Node->getOperand(0));
1495 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(1));
1496 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001497 Tmp2 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001498 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1499 }
1500 break;
1501 case ISD::INSERT_SUBREG: {
1502 Tmp1 = LegalizeOp(Node->getOperand(0));
1503 Tmp2 = LegalizeOp(Node->getOperand(1));
1504 ConstantSDNode *idx = dyn_cast<ConstantSDNode>(Node->getOperand(2));
1505 assert(idx && "Operand must be a constant");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001506 Tmp3 = DAG.getTargetConstant(idx->getAPIntValue(), idx->getValueType(0));
Christopher Lamb557c3632007-07-26 07:34:40 +00001507 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1508 }
1509 break;
Chris Lattnerb2827b02006-03-19 00:52:58 +00001510 case ISD::BUILD_VECTOR:
1511 switch (TLI.getOperationAction(ISD::BUILD_VECTOR, Node->getValueType(0))) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001512 default: assert(0 && "This action is not supported yet!");
1513 case TargetLowering::Custom:
1514 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001515 if (Tmp3.getNode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00001516 Result = Tmp3;
1517 break;
1518 }
1519 // FALLTHROUGH
Chris Lattnerce872152006-03-19 06:31:19 +00001520 case TargetLowering::Expand:
Gabor Greifba36cb52008-08-28 21:40:38 +00001521 Result = ExpandBUILD_VECTOR(Result.getNode());
Chris Lattnerb2827b02006-03-19 00:52:58 +00001522 break;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001523 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001524 break;
1525 case ISD::INSERT_VECTOR_ELT:
1526 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVec
Chris Lattner2332b9f2006-03-19 01:17:20 +00001527 Tmp3 = LegalizeOp(Node->getOperand(2)); // InEltNo
Nate Begeman0325d902008-02-13 06:43:04 +00001528
1529 // The type of the value to insert may not be legal, even though the vector
1530 // type is legal. Legalize/Promote accordingly. We do not handle Expand
1531 // here.
1532 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1533 default: assert(0 && "Cannot expand insert element operand");
1534 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
1535 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Mon P Wang0c397192008-10-30 08:01:45 +00001536 case Expand:
1537 // FIXME: An alternative would be to check to see if the target is not
1538 // going to custom lower this operation, we could bitcast to half elt
1539 // width and perform two inserts at that width, if that is legal.
1540 Tmp2 = Node->getOperand(1);
1541 break;
Nate Begeman0325d902008-02-13 06:43:04 +00001542 }
Chris Lattner2332b9f2006-03-19 01:17:20 +00001543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
1544
1545 switch (TLI.getOperationAction(ISD::INSERT_VECTOR_ELT,
1546 Node->getValueType(0))) {
1547 default: assert(0 && "This action is not supported yet!");
1548 case TargetLowering::Legal:
1549 break;
1550 case TargetLowering::Custom:
Nate Begeman2281a992008-01-05 20:47:37 +00001551 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001552 if (Tmp4.getNode()) {
Nate Begeman2281a992008-01-05 20:47:37 +00001553 Result = Tmp4;
Chris Lattner2332b9f2006-03-19 01:17:20 +00001554 break;
1555 }
1556 // FALLTHROUGH
Mon P Wang0c397192008-10-30 08:01:45 +00001557 case TargetLowering::Promote:
1558 // Fall thru for vector case
Chris Lattner2332b9f2006-03-19 01:17:20 +00001559 case TargetLowering::Expand: {
Chris Lattner8d5a8942006-04-17 19:21:01 +00001560 // If the insert index is a constant, codegen this as a scalar_to_vector,
1561 // then a shuffle that inserts it into the right position in the vector.
1562 if (ConstantSDNode *InsertPos = dyn_cast<ConstantSDNode>(Tmp3)) {
Nate Begeman0325d902008-02-13 06:43:04 +00001563 // SCALAR_TO_VECTOR requires that the type of the value being inserted
1564 // match the element type of the vector being created.
1565 if (Tmp2.getValueType() ==
Duncan Sands83ec4b62008-06-06 12:08:01 +00001566 Op.getValueType().getVectorElementType()) {
Dan Gohman475871a2008-07-27 21:46:04 +00001567 SDValue ScVec = DAG.getNode(ISD::SCALAR_TO_VECTOR,
Nate Begeman0325d902008-02-13 06:43:04 +00001568 Tmp1.getValueType(), Tmp2);
1569
Duncan Sands83ec4b62008-06-06 12:08:01 +00001570 unsigned NumElts = Tmp1.getValueType().getVectorNumElements();
1571 MVT ShufMaskVT =
1572 MVT::getIntVectorWithNumElements(NumElts);
1573 MVT ShufMaskEltVT = ShufMaskVT.getVectorElementType();
Nate Begeman0325d902008-02-13 06:43:04 +00001574
1575 // We generate a shuffle of InVec and ScVec, so the shuffle mask
1576 // should be 0,1,2,3,4,5... with the appropriate element replaced with
1577 // elt 0 of the RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00001578 SmallVector<SDValue, 8> ShufOps;
Nate Begeman0325d902008-02-13 06:43:04 +00001579 for (unsigned i = 0; i != NumElts; ++i) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001580 if (i != InsertPos->getZExtValue())
Nate Begeman0325d902008-02-13 06:43:04 +00001581 ShufOps.push_back(DAG.getConstant(i, ShufMaskEltVT));
1582 else
1583 ShufOps.push_back(DAG.getConstant(NumElts, ShufMaskEltVT));
1584 }
Dan Gohman475871a2008-07-27 21:46:04 +00001585 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, ShufMaskVT,
Nate Begeman0325d902008-02-13 06:43:04 +00001586 &ShufOps[0], ShufOps.size());
1587
1588 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, Tmp1.getValueType(),
1589 Tmp1, ScVec, ShufMask);
1590 Result = LegalizeOp(Result);
1591 break;
Chris Lattner8d5a8942006-04-17 19:21:01 +00001592 }
Chris Lattner8d5a8942006-04-17 19:21:01 +00001593 }
Nate Begeman68679912008-04-25 18:07:40 +00001594 Result = PerformInsertVectorEltInMemory(Tmp1, Tmp2, Tmp3);
Chris Lattner2332b9f2006-03-19 01:17:20 +00001595 break;
1596 }
1597 }
1598 break;
Chris Lattnerce872152006-03-19 06:31:19 +00001599 case ISD::SCALAR_TO_VECTOR:
Chris Lattner4352cc92006-04-04 17:23:26 +00001600 if (!TLI.isTypeLegal(Node->getOperand(0).getValueType())) {
1601 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
1602 break;
1603 }
1604
Chris Lattnerce872152006-03-19 06:31:19 +00001605 Tmp1 = LegalizeOp(Node->getOperand(0)); // InVal
1606 Result = DAG.UpdateNodeOperands(Result, Tmp1);
1607 switch (TLI.getOperationAction(ISD::SCALAR_TO_VECTOR,
1608 Node->getValueType(0))) {
1609 default: assert(0 && "This action is not supported yet!");
1610 case TargetLowering::Legal:
1611 break;
Chris Lattner4d3abee2006-03-19 06:47:21 +00001612 case TargetLowering::Custom:
1613 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001614 if (Tmp3.getNode()) {
Chris Lattner4d3abee2006-03-19 06:47:21 +00001615 Result = Tmp3;
1616 break;
1617 }
1618 // FALLTHROUGH
Chris Lattner4352cc92006-04-04 17:23:26 +00001619 case TargetLowering::Expand:
1620 Result = LegalizeOp(ExpandSCALAR_TO_VECTOR(Node));
Chris Lattnerce872152006-03-19 06:31:19 +00001621 break;
1622 }
Chris Lattnerce872152006-03-19 06:31:19 +00001623 break;
Chris Lattner87100e02006-03-20 01:52:29 +00001624 case ISD::VECTOR_SHUFFLE:
Chris Lattner87100e02006-03-20 01:52:29 +00001625 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the input vectors,
1626 Tmp2 = LegalizeOp(Node->getOperand(1)); // but not the shuffle mask.
1627 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1628
1629 // Allow targets to custom lower the SHUFFLEs they support.
Chris Lattner4352cc92006-04-04 17:23:26 +00001630 switch (TLI.getOperationAction(ISD::VECTOR_SHUFFLE,Result.getValueType())) {
1631 default: assert(0 && "Unknown operation action!");
1632 case TargetLowering::Legal:
1633 assert(isShuffleLegal(Result.getValueType(), Node->getOperand(2)) &&
1634 "vector shuffle should not be created if not legal!");
1635 break;
1636 case TargetLowering::Custom:
Evan Cheng18dd6d02006-04-05 06:07:11 +00001637 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001638 if (Tmp3.getNode()) {
Evan Cheng18dd6d02006-04-05 06:07:11 +00001639 Result = Tmp3;
1640 break;
1641 }
1642 // FALLTHROUGH
1643 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001644 MVT VT = Node->getValueType(0);
1645 MVT EltVT = VT.getVectorElementType();
1646 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00001647 SDValue Mask = Node->getOperand(2);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001648 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00001649 SmallVector<SDValue,8> Ops;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001650 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00001651 SDValue Arg = Mask.getOperand(i);
Evan Cheng18dd6d02006-04-05 06:07:11 +00001652 if (Arg.getOpcode() == ISD::UNDEF) {
1653 Ops.push_back(DAG.getNode(ISD::UNDEF, EltVT));
1654 } else {
1655 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001656 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng18dd6d02006-04-05 06:07:11 +00001657 if (Idx < NumElems)
1658 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp1,
1659 DAG.getConstant(Idx, PtrVT)));
1660 else
1661 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, Tmp2,
1662 DAG.getConstant(Idx - NumElems, PtrVT)));
1663 }
1664 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001665 Result = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
Chris Lattner4352cc92006-04-04 17:23:26 +00001666 break;
Evan Cheng18dd6d02006-04-05 06:07:11 +00001667 }
Chris Lattner4352cc92006-04-04 17:23:26 +00001668 case TargetLowering::Promote: {
1669 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001670 MVT OVT = Node->getValueType(0);
1671 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner4352cc92006-04-04 17:23:26 +00001672
1673 // Cast the two input vectors.
1674 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
1675 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
1676
1677 // Convert the shuffle mask to the right # elements.
Dan Gohman475871a2008-07-27 21:46:04 +00001678 Tmp3 = SDValue(isShuffleLegal(OVT, Node->getOperand(2)), 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001679 assert(Tmp3.getNode() && "Shuffle not legal?");
Chris Lattner4352cc92006-04-04 17:23:26 +00001680 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, NVT, Tmp1, Tmp2, Tmp3);
1681 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
1682 break;
1683 }
Chris Lattner87100e02006-03-20 01:52:29 +00001684 }
1685 break;
Chris Lattner384504c2006-03-21 20:44:12 +00001686
1687 case ISD::EXTRACT_VECTOR_ELT:
Dan Gohman7f321562007-06-25 16:23:39 +00001688 Tmp1 = Node->getOperand(0);
Chris Lattner384504c2006-03-21 20:44:12 +00001689 Tmp2 = LegalizeOp(Node->getOperand(1));
1690 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohman7f321562007-06-25 16:23:39 +00001691 Result = ExpandEXTRACT_VECTOR_ELT(Result);
Chris Lattner384504c2006-03-21 20:44:12 +00001692 break;
1693
Dan Gohman7f321562007-06-25 16:23:39 +00001694 case ISD::EXTRACT_SUBVECTOR:
1695 Tmp1 = Node->getOperand(0);
1696 Tmp2 = LegalizeOp(Node->getOperand(1));
1697 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1698 Result = ExpandEXTRACT_SUBVECTOR(Result);
Dan Gohman65956352007-06-13 15:12:02 +00001699 break;
1700
Mon P Wang0c397192008-10-30 08:01:45 +00001701 case ISD::CONCAT_VECTORS: {
1702 // Use extract/insert/build vector for now. We might try to be
1703 // more clever later.
1704 MVT PtrVT = TLI.getPointerTy();
1705 SmallVector<SDValue, 8> Ops;
1706 unsigned NumOperands = Node->getNumOperands();
1707 for (unsigned i=0; i < NumOperands; ++i) {
1708 SDValue SubOp = Node->getOperand(i);
1709 MVT VVT = SubOp.getNode()->getValueType(0);
1710 MVT EltVT = VVT.getVectorElementType();
1711 unsigned NumSubElem = VVT.getVectorNumElements();
1712 for (unsigned j=0; j < NumSubElem; ++j) {
1713 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EltVT, SubOp,
1714 DAG.getConstant(j, PtrVT)));
1715 }
1716 }
1717 return LegalizeOp(DAG.getNode(ISD::BUILD_VECTOR, Node->getValueType(0),
1718 &Ops[0], Ops.size()));
1719 }
1720
Chris Lattner6831a812006-02-13 09:18:02 +00001721 case ISD::CALLSEQ_START: {
1722 SDNode *CallEnd = FindCallEndFromCallStart(Node);
1723
1724 // Recursively Legalize all of the inputs of the call end that do not lead
1725 // to this call start. This ensures that any libcalls that need be inserted
1726 // are inserted *before* the CALLSEQ_START.
Chris Lattner00755df2007-02-04 00:27:56 +00001727 {SmallPtrSet<SDNode*, 32> NodesLeadingTo;
Chris Lattner6831a812006-02-13 09:18:02 +00001728 for (unsigned i = 0, e = CallEnd->getNumOperands(); i != e; ++i)
Gabor Greifba36cb52008-08-28 21:40:38 +00001729 LegalizeAllNodesNotLeadingTo(CallEnd->getOperand(i).getNode(), Node,
Chris Lattnerc9cf4f12006-07-26 23:55:56 +00001730 NodesLeadingTo);
1731 }
Chris Lattner6831a812006-02-13 09:18:02 +00001732
1733 // Now that we legalized all of the inputs (which may have inserted
1734 // libcalls) create the new CALLSEQ_START node.
1735 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1736
1737 // Merge in the last call, to ensure that this call start after the last
1738 // call ended.
Chris Lattnerc5d7d7c2006-05-17 18:00:08 +00001739 if (LastCALLSEQ_END.getOpcode() != ISD::EntryToken) {
Chris Lattnerb248e162006-05-17 17:55:45 +00001740 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1741 Tmp1 = LegalizeOp(Tmp1);
1742 }
Chris Lattner6831a812006-02-13 09:18:02 +00001743
1744 // Do not try to legalize the target-specific arguments (#1+).
1745 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001746 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner6831a812006-02-13 09:18:02 +00001747 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001748 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner6831a812006-02-13 09:18:02 +00001749 }
1750
1751 // Remember that the CALLSEQ_START is legalized.
Chris Lattner4b653a02006-02-14 00:55:02 +00001752 AddLegalizedOperand(Op.getValue(0), Result);
1753 if (Node->getNumValues() == 2) // If this has a flag result, remember it.
1754 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1755
Chris Lattner6831a812006-02-13 09:18:02 +00001756 // Now that the callseq_start and all of the non-call nodes above this call
1757 // sequence have been legalized, legalize the call itself. During this
1758 // process, no libcalls can/will be inserted, guaranteeing that no calls
1759 // can overlap.
1760 assert(!IsLegalizingCall && "Inconsistent sequentialization of calls!");
Chris Lattner6831a812006-02-13 09:18:02 +00001761 // Note that we are selecting this call!
Dan Gohman475871a2008-07-27 21:46:04 +00001762 LastCALLSEQ_END = SDValue(CallEnd, 0);
Chris Lattner6831a812006-02-13 09:18:02 +00001763 IsLegalizingCall = true;
1764
1765 // Legalize the call, starting from the CALLSEQ_END.
1766 LegalizeOp(LastCALLSEQ_END);
1767 assert(!IsLegalizingCall && "CALLSEQ_END should have cleared this!");
1768 return Result;
1769 }
Chris Lattner16cd04d2005-05-12 23:24:06 +00001770 case ISD::CALLSEQ_END:
Chris Lattner6831a812006-02-13 09:18:02 +00001771 // If the CALLSEQ_START node hasn't been legalized first, legalize it. This
1772 // will cause this node to be legalized as well as handling libcalls right.
Gabor Greifba36cb52008-08-28 21:40:38 +00001773 if (LastCALLSEQ_END.getNode() != Node) {
Dan Gohman475871a2008-07-27 21:46:04 +00001774 LegalizeOp(SDValue(FindCallStartFromCallEnd(Node), 0));
1775 DenseMap<SDValue, SDValue>::iterator I = LegalizedNodes.find(Op);
Chris Lattner6831a812006-02-13 09:18:02 +00001776 assert(I != LegalizedNodes.end() &&
1777 "Legalizing the call start should have legalized this node!");
1778 return I->second;
1779 }
1780
1781 // Otherwise, the call start has been legalized and everything is going
1782 // according to plan. Just legalize ourselves normally here.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001783 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner70814bc2006-01-29 07:58:15 +00001784 // Do not try to legalize the target-specific arguments (#1+), except for
1785 // an optional flag input.
1786 if (Node->getOperand(Node->getNumOperands()-1).getValueType() != MVT::Flag){
1787 if (Tmp1 != Node->getOperand(0)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001788 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001789 Ops[0] = Tmp1;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001790 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001791 }
1792 } else {
1793 Tmp2 = LegalizeOp(Node->getOperand(Node->getNumOperands()-1));
1794 if (Tmp1 != Node->getOperand(0) ||
1795 Tmp2 != Node->getOperand(Node->getNumOperands()-1)) {
Dan Gohman475871a2008-07-27 21:46:04 +00001796 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner70814bc2006-01-29 07:58:15 +00001797 Ops[0] = Tmp1;
1798 Ops.back() = Tmp2;
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001799 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner70814bc2006-01-29 07:58:15 +00001800 }
Chris Lattner6a542892006-01-24 05:48:21 +00001801 }
Chris Lattner4b653a02006-02-14 00:55:02 +00001802 assert(IsLegalizingCall && "Call sequence imbalance between start/end?");
Chris Lattner6831a812006-02-13 09:18:02 +00001803 // This finishes up call legalization.
1804 IsLegalizingCall = false;
Chris Lattner4b653a02006-02-14 00:55:02 +00001805
1806 // If the CALLSEQ_END node has a flag, remember that we legalized it.
Dan Gohman475871a2008-07-27 21:46:04 +00001807 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
Chris Lattner4b653a02006-02-14 00:55:02 +00001808 if (Node->getNumValues() == 2)
Dan Gohman475871a2008-07-27 21:46:04 +00001809 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001810 return Result.getValue(Op.getResNo());
Evan Chenga7dce3c2006-01-11 22:14:47 +00001811 case ISD::DYNAMIC_STACKALLOC: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001812 MVT VT = Node->getValueType(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001813 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1814 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
1815 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001816 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerfa404e82005-01-09 19:03:49 +00001817
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001818 Tmp1 = Result.getValue(0);
Chris Lattner5f652292006-01-15 08:43:08 +00001819 Tmp2 = Result.getValue(1);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001820 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
Evan Chenga7dce3c2006-01-11 22:14:47 +00001821 default: assert(0 && "This action is not supported yet!");
Chris Lattner903d2782006-01-15 08:54:32 +00001822 case TargetLowering::Expand: {
1823 unsigned SPReg = TLI.getStackPointerRegisterToSaveRestore();
1824 assert(SPReg && "Target cannot require DYNAMIC_STACKALLOC expansion and"
1825 " not tell us which reg is the stack pointer!");
Dan Gohman475871a2008-07-27 21:46:04 +00001826 SDValue Chain = Tmp1.getOperand(0);
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001827
1828 // Chain the dynamic stack allocation so that it doesn't modify the stack
1829 // pointer when other instructions are using the stack.
Chris Lattnere563bbc2008-10-11 22:08:30 +00001830 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001831
Dan Gohman475871a2008-07-27 21:46:04 +00001832 SDValue Size = Tmp2.getOperand(1);
1833 SDValue SP = DAG.getCopyFromReg(Chain, SPReg, VT);
Evan Cheng61bbbab2007-08-16 23:50:06 +00001834 Chain = SP.getValue(1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001835 unsigned Align = cast<ConstantSDNode>(Tmp3)->getZExtValue();
Evan Cheng61bbbab2007-08-16 23:50:06 +00001836 unsigned StackAlign =
1837 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
1838 if (Align > StackAlign)
Evan Cheng3e20bba2007-08-17 18:02:22 +00001839 SP = DAG.getNode(ISD::AND, VT, SP,
1840 DAG.getConstant(-(uint64_t)Align, VT));
Evan Cheng61bbbab2007-08-16 23:50:06 +00001841 Tmp1 = DAG.getNode(ISD::SUB, VT, SP, Size); // Value
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001842 Chain = DAG.getCopyToReg(Chain, SPReg, Tmp1); // Output chain
1843
Chris Lattnere563bbc2008-10-11 22:08:30 +00001844 Tmp2 = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(0, true),
1845 DAG.getIntPtrConstant(0, true), SDValue());
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001846
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001847 Tmp1 = LegalizeOp(Tmp1);
1848 Tmp2 = LegalizeOp(Tmp2);
Chris Lattner903d2782006-01-15 08:54:32 +00001849 break;
1850 }
1851 case TargetLowering::Custom:
Chris Lattner5f652292006-01-15 08:43:08 +00001852 Tmp3 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001853 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001854 Tmp1 = LegalizeOp(Tmp3);
1855 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Evan Chenga7dce3c2006-01-11 22:14:47 +00001856 }
Chris Lattner903d2782006-01-15 08:54:32 +00001857 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001858 case TargetLowering::Legal:
Chris Lattner903d2782006-01-15 08:54:32 +00001859 break;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001860 }
Chris Lattner903d2782006-01-15 08:54:32 +00001861 // Since this op produce two values, make sure to remember that we
1862 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00001863 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
1864 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00001865 return Op.getResNo() ? Tmp2 : Tmp1;
Evan Chenga7dce3c2006-01-11 22:14:47 +00001866 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001867 case ISD::INLINEASM: {
Dan Gohman475871a2008-07-27 21:46:04 +00001868 SmallVector<SDValue, 8> Ops(Node->op_begin(), Node->op_end());
Chris Lattner25a022c2006-07-11 01:40:09 +00001869 bool Changed = false;
1870 // Legalize all of the operands of the inline asm, in case they are nodes
1871 // that need to be expanded or something. Note we skip the asm string and
1872 // all of the TargetConstant flags.
Dan Gohman475871a2008-07-27 21:46:04 +00001873 SDValue Op = LegalizeOp(Ops[0]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001874 Changed = Op != Ops[0];
1875 Ops[0] = Op;
1876
1877 bool HasInFlag = Ops.back().getValueType() == MVT::Flag;
1878 for (unsigned i = 2, e = Ops.size()-HasInFlag; i < e; ) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001879 unsigned NumVals = cast<ConstantSDNode>(Ops[i])->getZExtValue() >> 3;
Chris Lattner25a022c2006-07-11 01:40:09 +00001880 for (++i; NumVals; ++i, --NumVals) {
Dan Gohman475871a2008-07-27 21:46:04 +00001881 SDValue Op = LegalizeOp(Ops[i]);
Chris Lattner25a022c2006-07-11 01:40:09 +00001882 if (Op != Ops[i]) {
1883 Changed = true;
1884 Ops[i] = Op;
1885 }
1886 }
Chris Lattnerce7518c2006-01-26 22:24:51 +00001887 }
Chris Lattner25a022c2006-07-11 01:40:09 +00001888
1889 if (HasInFlag) {
1890 Op = LegalizeOp(Ops.back());
1891 Changed |= Op != Ops.back();
1892 Ops.back() = Op;
1893 }
1894
1895 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00001896 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattnerce7518c2006-01-26 22:24:51 +00001897
1898 // INLINE asm returns a chain and flag, make sure to add both to the map.
Dan Gohman475871a2008-07-27 21:46:04 +00001899 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
1900 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Gabor Greif99a6cb92008-08-26 22:36:50 +00001901 return Result.getValue(Op.getResNo());
Chris Lattner25a022c2006-07-11 01:40:09 +00001902 }
Chris Lattnerc7af1792005-01-07 22:12:08 +00001903 case ISD::BR:
1904 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001905 // Ensure that libcalls are emitted before a branch.
1906 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1907 Tmp1 = LegalizeOp(Tmp1);
1908 LastCALLSEQ_END = DAG.getEntryNode();
1909
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00001910 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerc7af1792005-01-07 22:12:08 +00001911 break;
Nate Begeman37efe672006-04-22 18:53:45 +00001912 case ISD::BRIND:
1913 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1914 // Ensure that libcalls are emitted before a branch.
1915 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1916 Tmp1 = LegalizeOp(Tmp1);
1917 LastCALLSEQ_END = DAG.getEntryNode();
1918
1919 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1920 default: assert(0 && "Indirect target must be legal type (pointer)!");
1921 case Legal:
1922 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1923 break;
1924 }
1925 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
1926 break;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001927 case ISD::BR_JT:
1928 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1929 // Ensure that libcalls are emitted before a branch.
1930 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1931 Tmp1 = LegalizeOp(Tmp1);
1932 LastCALLSEQ_END = DAG.getEntryNode();
1933
1934 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the jumptable node.
1935 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
1936
1937 switch (TLI.getOperationAction(ISD::BR_JT, MVT::Other)) {
1938 default: assert(0 && "This action is not supported yet!");
1939 case TargetLowering::Legal: break;
1940 case TargetLowering::Custom:
1941 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00001942 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng3d4ce112006-10-30 08:00:44 +00001943 break;
1944 case TargetLowering::Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00001945 SDValue Chain = Result.getOperand(0);
1946 SDValue Table = Result.getOperand(1);
1947 SDValue Index = Result.getOperand(2);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001948
Duncan Sands83ec4b62008-06-06 12:08:01 +00001949 MVT PTy = TLI.getPointerTy();
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001950 MachineFunction &MF = DAG.getMachineFunction();
1951 unsigned EntrySize = MF.getJumpTableInfo()->getEntrySize();
Evan Cheng3d4ce112006-10-30 08:00:44 +00001952 Index= DAG.getNode(ISD::MUL, PTy, Index, DAG.getConstant(EntrySize, PTy));
Dan Gohman475871a2008-07-27 21:46:04 +00001953 SDValue Addr = DAG.getNode(ISD::ADD, PTy, Index, Table);
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001954
Dan Gohman475871a2008-07-27 21:46:04 +00001955 SDValue LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001956 switch (EntrySize) {
1957 default: assert(0 && "Size of jump table not supported yet."); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001958 case 4: LD = DAG.getLoad(MVT::i32, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001959 PseudoSourceValue::getJumpTable(), 0); break;
Dan Gohman69de1932008-02-06 22:27:42 +00001960 case 8: LD = DAG.getLoad(MVT::i64, Chain, Addr,
Dan Gohman3069b872008-02-07 18:41:25 +00001961 PseudoSourceValue::getJumpTable(), 0); break;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001962 }
1963
Evan Chengcc415862007-11-09 01:32:10 +00001964 Addr = LD;
Jim Laskeyacd80ac2006-12-14 19:17:33 +00001965 if (TLI.getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Evan Cheng3d4ce112006-10-30 08:00:44 +00001966 // For PIC, the sequence is:
1967 // BRIND(load(Jumptable + index) + RelocBase)
Evan Chengcc415862007-11-09 01:32:10 +00001968 // RelocBase can be JumpTable, GOT or some sort of global base.
1969 if (PTy != MVT::i32)
1970 Addr = DAG.getNode(ISD::SIGN_EXTEND, PTy, Addr);
1971 Addr = DAG.getNode(ISD::ADD, PTy, Addr,
1972 TLI.getPICJumpTableRelocBase(Table, DAG));
Evan Cheng3d4ce112006-10-30 08:00:44 +00001973 }
Evan Chengcc415862007-11-09 01:32:10 +00001974 Result = DAG.getNode(ISD::BRIND, MVT::Other, LD.getValue(1), Addr);
Evan Cheng3d4ce112006-10-30 08:00:44 +00001975 }
1976 }
1977 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00001978 case ISD::BRCOND:
1979 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00001980 // Ensure that libcalls are emitted before a return.
1981 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
1982 Tmp1 = LegalizeOp(Tmp1);
1983 LastCALLSEQ_END = DAG.getEntryNode();
1984
Chris Lattner47e92232005-01-18 19:27:06 +00001985 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1986 case Expand: assert(0 && "It's impossible to expand bools");
1987 case Legal:
1988 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
1989 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001990 case Promote: {
Chris Lattner47e92232005-01-18 19:27:06 +00001991 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
Chris Lattnerf9908172006-11-27 04:39:56 +00001992
1993 // The top bits of the promoted condition are not necessarily zero, ensure
1994 // that the value is properly zero extended.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001995 unsigned BitWidth = Tmp2.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00001996 if (!DAG.MaskedValueIsZero(Tmp2,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00001997 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerf9908172006-11-27 04:39:56 +00001998 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00001999 break;
2000 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002001 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002002
2003 // Basic block destination (Op#2) is always legal.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002004 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +00002005
2006 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
2007 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002008 case TargetLowering::Legal: break;
2009 case TargetLowering::Custom:
2010 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002011 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002012 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002013 case TargetLowering::Expand:
2014 // Expand brcond's setcc into its constituent parts and create a BR_CC
2015 // Node.
2016 if (Tmp2.getOpcode() == ISD::SETCC) {
2017 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
2018 Tmp2.getOperand(0), Tmp2.getOperand(1),
2019 Node->getOperand(2));
2020 } else {
2021 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
2022 DAG.getCondCode(ISD::SETNE), Tmp2,
2023 DAG.getConstant(0, Tmp2.getValueType()),
2024 Node->getOperand(2));
2025 }
2026 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002027 }
2028 break;
2029 case ISD::BR_CC:
2030 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002031 // Ensure that libcalls are emitted before a branch.
2032 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2033 Tmp1 = LegalizeOp(Tmp1);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002034 Tmp2 = Node->getOperand(2); // LHS
2035 Tmp3 = Node->getOperand(3); // RHS
2036 Tmp4 = Node->getOperand(1); // CC
2037
Dale Johannesen53e4e442008-11-07 22:54:33 +00002038 LegalizeSetCC(TLI.getSetCCResultType(Tmp2), Tmp2, Tmp3, Tmp4);
Evan Cheng722cb362006-12-18 22:55:34 +00002039 LastCALLSEQ_END = DAG.getEntryNode();
2040
Evan Cheng7f042682008-10-15 02:05:31 +00002041 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002042 // the LHS is a legal SETCC itself. In this case, we need to compare
2043 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002044 if (Tmp3.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002045 Tmp3 = DAG.getConstant(0, Tmp2.getValueType());
2046 Tmp4 = DAG.getCondCode(ISD::SETNE);
Chris Lattner181b7a32005-12-17 23:46:46 +00002047 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00002048
2049 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp4, Tmp2, Tmp3,
2050 Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00002051
Chris Lattner181b7a32005-12-17 23:46:46 +00002052 switch (TLI.getOperationAction(ISD::BR_CC, Tmp3.getValueType())) {
2053 default: assert(0 && "Unexpected action for BR_CC!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002054 case TargetLowering::Legal: break;
2055 case TargetLowering::Custom:
2056 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002057 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner181b7a32005-12-17 23:46:46 +00002058 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +00002059 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +00002060 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002061 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00002062 LoadSDNode *LD = cast<LoadSDNode>(Node);
2063 Tmp1 = LegalizeOp(LD->getChain()); // Legalize the chain.
2064 Tmp2 = LegalizeOp(LD->getBasePtr()); // Legalize the base pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002065
Evan Cheng466685d2006-10-09 20:57:25 +00002066 ISD::LoadExtType ExtType = LD->getExtensionType();
2067 if (ExtType == ISD::NON_EXTLOAD) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002068 MVT VT = Node->getValueType(0);
Evan Cheng466685d2006-10-09 20:57:25 +00002069 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2070 Tmp3 = Result.getValue(0);
2071 Tmp4 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002072
Evan Cheng466685d2006-10-09 20:57:25 +00002073 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
2074 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002075 case TargetLowering::Legal:
2076 // If this is an unaligned load and the target doesn't support it,
2077 // expand it.
2078 if (!TLI.allowsUnalignedMemoryAccesses()) {
2079 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002080 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002081 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002082 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002083 TLI);
2084 Tmp3 = Result.getOperand(0);
2085 Tmp4 = Result.getOperand(1);
Dale Johannesen907f28c2007-09-08 19:29:23 +00002086 Tmp3 = LegalizeOp(Tmp3);
2087 Tmp4 = LegalizeOp(Tmp4);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002088 }
2089 }
2090 break;
Evan Cheng466685d2006-10-09 20:57:25 +00002091 case TargetLowering::Custom:
2092 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002093 if (Tmp1.getNode()) {
Evan Cheng466685d2006-10-09 20:57:25 +00002094 Tmp3 = LegalizeOp(Tmp1);
2095 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattner456a93a2006-01-28 07:39:30 +00002096 }
Evan Cheng466685d2006-10-09 20:57:25 +00002097 break;
2098 case TargetLowering::Promote: {
2099 // Only promote a load of vector type to another.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002100 assert(VT.isVector() && "Cannot promote this load!");
Evan Cheng466685d2006-10-09 20:57:25 +00002101 // Change base type to a different vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002102 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), VT);
Evan Cheng466685d2006-10-09 20:57:25 +00002103
2104 Tmp1 = DAG.getLoad(NVT, Tmp1, Tmp2, LD->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002105 LD->getSrcValueOffset(),
2106 LD->isVolatile(), LD->getAlignment());
Evan Cheng466685d2006-10-09 20:57:25 +00002107 Tmp3 = LegalizeOp(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp1));
2108 Tmp4 = LegalizeOp(Tmp1.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002109 break;
Andrew Lenharth9d416f72005-06-30 19:22:37 +00002110 }
Evan Cheng466685d2006-10-09 20:57:25 +00002111 }
2112 // Since loads produce two values, make sure to remember that we
2113 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002114 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
2115 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002116 return Op.getResNo() ? Tmp4 : Tmp3;
Evan Cheng466685d2006-10-09 20:57:25 +00002117 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002118 MVT SrcVT = LD->getMemoryVT();
2119 unsigned SrcWidth = SrcVT.getSizeInBits();
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002120 int SVOffset = LD->getSrcValueOffset();
2121 unsigned Alignment = LD->getAlignment();
2122 bool isVolatile = LD->isVolatile();
2123
Duncan Sands83ec4b62008-06-06 12:08:01 +00002124 if (SrcWidth != SrcVT.getStoreSizeInBits() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002125 // Some targets pretend to have an i1 loading operation, and actually
2126 // load an i8. This trick is correct for ZEXTLOAD because the top 7
2127 // bits are guaranteed to be zero; it helps the optimizers understand
2128 // that these bits are zero. It is also useful for EXTLOAD, since it
2129 // tells the optimizers that those bits are undefined. It would be
2130 // nice to have an effective generic way of getting these benefits...
2131 // Until such a way is found, don't insist on promoting i1 here.
2132 (SrcVT != MVT::i1 ||
Evan Cheng03294662008-10-14 21:26:46 +00002133 TLI.getLoadExtAction(ExtType, MVT::i1) == TargetLowering::Promote)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002134 // Promote to a byte-sized load if not loading an integral number of
2135 // bytes. For example, promote EXTLOAD:i20 -> EXTLOAD:i24.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002136 unsigned NewWidth = SrcVT.getStoreSizeInBits();
2137 MVT NVT = MVT::getIntegerVT(NewWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002138 SDValue Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002139
2140 // The extra bits are guaranteed to be zero, since we stored them that
2141 // way. A zext load from NVT thus automatically gives zext from SrcVT.
2142
2143 ISD::LoadExtType NewExtType =
2144 ExtType == ISD::ZEXTLOAD ? ISD::ZEXTLOAD : ISD::EXTLOAD;
2145
2146 Result = DAG.getExtLoad(NewExtType, Node->getValueType(0),
2147 Tmp1, Tmp2, LD->getSrcValue(), SVOffset,
2148 NVT, isVolatile, Alignment);
2149
2150 Ch = Result.getValue(1); // The chain.
2151
2152 if (ExtType == ISD::SEXTLOAD)
2153 // Having the top bits zero doesn't help when sign extending.
2154 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2155 Result, DAG.getValueType(SrcVT));
2156 else if (ExtType == ISD::ZEXTLOAD || NVT == Result.getValueType())
2157 // All the top bits are guaranteed to be zero - inform the optimizers.
2158 Result = DAG.getNode(ISD::AssertZext, Result.getValueType(), Result,
2159 DAG.getValueType(SrcVT));
2160
2161 Tmp1 = LegalizeOp(Result);
2162 Tmp2 = LegalizeOp(Ch);
2163 } else if (SrcWidth & (SrcWidth - 1)) {
2164 // If not loading a power-of-2 number of bits, expand as two loads.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002165 assert(SrcVT.isExtended() && !SrcVT.isVector() &&
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002166 "Unsupported extload!");
2167 unsigned RoundWidth = 1 << Log2_32(SrcWidth);
2168 assert(RoundWidth < SrcWidth);
2169 unsigned ExtraWidth = SrcWidth - RoundWidth;
2170 assert(ExtraWidth < RoundWidth);
2171 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2172 "Load size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002173 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2174 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002175 SDValue Lo, Hi, Ch;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002176 unsigned IncrementSize;
2177
2178 if (TLI.isLittleEndian()) {
2179 // EXTLOAD:i24 -> ZEXTLOAD:i16 | (shl EXTLOAD@+2:i8, 16)
2180 // Load the bottom RoundWidth bits.
2181 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2182 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2183 Alignment);
2184
2185 // Load the remaining ExtraWidth bits.
2186 IncrementSize = RoundWidth / 8;
2187 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2188 DAG.getIntPtrConstant(IncrementSize));
2189 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2190 LD->getSrcValue(), SVOffset + IncrementSize,
2191 ExtraVT, isVolatile,
2192 MinAlign(Alignment, IncrementSize));
2193
2194 // Build a factor node to remember that this load is independent of the
2195 // other one.
2196 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2197 Hi.getValue(1));
2198
2199 // Move the top bits to the right place.
2200 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2201 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2202
2203 // Join the hi and lo parts.
2204 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002205 } else {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002206 // Big endian - avoid unaligned loads.
2207 // EXTLOAD:i24 -> (shl EXTLOAD:i16, 8) | ZEXTLOAD@+2:i8
2208 // Load the top RoundWidth bits.
2209 Hi = DAG.getExtLoad(ExtType, Node->getValueType(0), Tmp1, Tmp2,
2210 LD->getSrcValue(), SVOffset, RoundVT, isVolatile,
2211 Alignment);
2212
2213 // Load the remaining ExtraWidth bits.
2214 IncrementSize = RoundWidth / 8;
2215 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2216 DAG.getIntPtrConstant(IncrementSize));
2217 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, Node->getValueType(0), Tmp1, Tmp2,
2218 LD->getSrcValue(), SVOffset + IncrementSize,
2219 ExtraVT, isVolatile,
2220 MinAlign(Alignment, IncrementSize));
2221
2222 // Build a factor node to remember that this load is independent of the
2223 // other one.
2224 Ch = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2225 Hi.getValue(1));
2226
2227 // Move the top bits to the right place.
2228 Hi = DAG.getNode(ISD::SHL, Hi.getValueType(), Hi,
2229 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2230
2231 // Join the hi and lo parts.
2232 Result = DAG.getNode(ISD::OR, Node->getValueType(0), Lo, Hi);
2233 }
2234
2235 Tmp1 = LegalizeOp(Result);
2236 Tmp2 = LegalizeOp(Ch);
2237 } else {
Evan Cheng03294662008-10-14 21:26:46 +00002238 switch (TLI.getLoadExtAction(ExtType, SrcVT)) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002239 default: assert(0 && "This action is not supported yet!");
2240 case TargetLowering::Custom:
2241 isCustom = true;
2242 // FALLTHROUGH
2243 case TargetLowering::Legal:
2244 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, LD->getOffset());
2245 Tmp1 = Result.getValue(0);
2246 Tmp2 = Result.getValue(1);
2247
2248 if (isCustom) {
2249 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002250 if (Tmp3.getNode()) {
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002251 Tmp1 = LegalizeOp(Tmp3);
2252 Tmp2 = LegalizeOp(Tmp3.getValue(1));
2253 }
2254 } else {
2255 // If this is an unaligned load and the target doesn't support it,
2256 // expand it.
2257 if (!TLI.allowsUnalignedMemoryAccesses()) {
2258 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002259 getABITypeAlignment(LD->getMemoryVT().getTypeForMVT());
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002260 if (LD->getAlignment() < ABIAlignment){
Gabor Greifba36cb52008-08-28 21:40:38 +00002261 Result = ExpandUnalignedLoad(cast<LoadSDNode>(Result.getNode()), DAG,
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002262 TLI);
2263 Tmp1 = Result.getOperand(0);
2264 Tmp2 = Result.getOperand(1);
2265 Tmp1 = LegalizeOp(Tmp1);
2266 Tmp2 = LegalizeOp(Tmp2);
2267 }
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002268 }
2269 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002270 break;
2271 case TargetLowering::Expand:
2272 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
2273 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
Dan Gohman475871a2008-07-27 21:46:04 +00002274 SDValue Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, LD->getSrcValue(),
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002275 LD->getSrcValueOffset(),
2276 LD->isVolatile(), LD->getAlignment());
2277 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
2278 Tmp1 = LegalizeOp(Result); // Relegalize new nodes.
2279 Tmp2 = LegalizeOp(Load.getValue(1));
2280 break;
2281 }
2282 assert(ExtType != ISD::EXTLOAD &&"EXTLOAD should always be supported!");
2283 // Turn the unsupported load into an EXTLOAD followed by an explicit
2284 // zero/sign extend inreg.
2285 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2286 Tmp1, Tmp2, LD->getSrcValue(),
2287 LD->getSrcValueOffset(), SrcVT,
2288 LD->isVolatile(), LD->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00002289 SDValue ValRes;
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002290 if (ExtType == ISD::SEXTLOAD)
2291 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
2292 Result, DAG.getValueType(SrcVT));
2293 else
2294 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
2295 Tmp1 = LegalizeOp(ValRes); // Relegalize new nodes.
2296 Tmp2 = LegalizeOp(Result.getValue(1)); // Relegalize new nodes.
Evan Cheng466685d2006-10-09 20:57:25 +00002297 break;
2298 }
Evan Cheng466685d2006-10-09 20:57:25 +00002299 }
Duncan Sandsf9c98e62008-01-23 20:39:46 +00002300
Evan Cheng466685d2006-10-09 20:57:25 +00002301 // Since loads produce two values, make sure to remember that we legalized
2302 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002303 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2304 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002305 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner01ff7212005-04-10 22:54:25 +00002306 }
Chris Lattner01ff7212005-04-10 22:54:25 +00002307 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002308 case ISD::EXTRACT_ELEMENT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002309 MVT OpTy = Node->getOperand(0).getValueType();
Nate Begeman5dc897b2005-10-19 00:06:56 +00002310 switch (getTypeAction(OpTy)) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002311 default: assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
Nate Begeman5dc897b2005-10-19 00:06:56 +00002312 case Legal:
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002313 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue()) {
Nate Begeman5dc897b2005-10-19 00:06:56 +00002314 // 1 -> Hi
2315 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
Duncan Sands83ec4b62008-06-06 12:08:01 +00002316 DAG.getConstant(OpTy.getSizeInBits()/2,
Nate Begeman5dc897b2005-10-19 00:06:56 +00002317 TLI.getShiftAmountTy()));
2318 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
2319 } else {
2320 // 0 -> Lo
2321 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
2322 Node->getOperand(0));
2323 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00002324 break;
2325 case Expand:
2326 // Get both the low and high parts.
2327 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002328 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Nate Begeman5dc897b2005-10-19 00:06:56 +00002329 Result = Tmp2; // 1 -> Hi
2330 else
2331 Result = Tmp1; // 0 -> Lo
2332 break;
2333 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002334 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00002335 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002336
2337 case ISD::CopyToReg:
2338 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00002339
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002340 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002341 "Register type must be legal!");
Chris Lattner7310fb12005-12-18 15:27:43 +00002342 // Legalize the incoming value (must be a legal type).
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002343 Tmp2 = LegalizeOp(Node->getOperand(2));
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002344 if (Node->getNumValues() == 1) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002345 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002346 } else {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002347 assert(Node->getNumValues() == 2 && "Unknown CopyToReg");
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002348 if (Node->getNumOperands() == 4) {
Chris Lattnerf1a47c32005-12-18 15:36:21 +00002349 Tmp3 = LegalizeOp(Node->getOperand(3));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1), Tmp2,
2351 Tmp3);
2352 } else {
2353 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1),Tmp2);
Chris Lattner7310fb12005-12-18 15:27:43 +00002354 }
2355
2356 // Since this produces two values, make sure to remember that we legalized
2357 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002358 AddLegalizedOperand(SDValue(Node, 0), Result.getValue(0));
2359 AddLegalizedOperand(SDValue(Node, 1), Result.getValue(1));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002360 return Result;
Chris Lattner7310fb12005-12-18 15:27:43 +00002361 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002362 break;
2363
2364 case ISD::RET:
2365 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner6831a812006-02-13 09:18:02 +00002366
2367 // Ensure that libcalls are emitted before a return.
2368 Tmp1 = DAG.getNode(ISD::TokenFactor, MVT::Other, Tmp1, LastCALLSEQ_END);
2369 Tmp1 = LegalizeOp(Tmp1);
2370 LastCALLSEQ_END = DAG.getEntryNode();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002371
Chris Lattner3e928bb2005-01-07 07:47:09 +00002372 switch (Node->getNumOperands()) {
Evan Cheng8e7d0562006-05-26 23:09:09 +00002373 case 3: // ret val
Evan Cheng98f8aeb2006-04-11 06:33:39 +00002374 Tmp2 = Node->getOperand(1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002375 Tmp3 = Node->getOperand(2); // Signness
Chris Lattnerf87324e2006-04-11 01:31:51 +00002376 switch (getTypeAction(Tmp2.getValueType())) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002377 case Legal:
Evan Cheng8e7d0562006-05-26 23:09:09 +00002378 Result = DAG.UpdateNodeOperands(Result, Tmp1, LegalizeOp(Tmp2), Tmp3);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002379 break;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002380 case Expand:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002381 if (!Tmp2.getValueType().isVector()) {
Dan Gohman475871a2008-07-27 21:46:04 +00002382 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002383 ExpandOp(Tmp2, Lo, Hi);
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002384
2385 // Big endian systems want the hi reg first.
Duncan Sands0753fc12008-02-11 10:37:04 +00002386 if (TLI.isBigEndian())
Chris Lattneredf2e8d2007-03-06 20:01:06 +00002387 std::swap(Lo, Hi);
2388
Gabor Greifba36cb52008-08-28 21:40:38 +00002389 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00002390 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
2391 else
2392 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3);
Chris Lattnerf921a512006-08-21 20:24:53 +00002393 Result = LegalizeOp(Result);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002394 } else {
Gabor Greifba36cb52008-08-28 21:40:38 +00002395 SDNode *InVal = Tmp2.getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002396 int InIx = Tmp2.getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002397 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
2398 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Chris Lattnerf87324e2006-04-11 01:31:51 +00002399
Dan Gohman7f321562007-06-25 16:23:39 +00002400 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002401 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002402 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002403 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002404 // Turn this into a return of the vector type.
Dan Gohman7f321562007-06-25 16:23:39 +00002405 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002406 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002407 } else if (NumElems == 1) {
2408 // Turn this into a return of the scalar type.
Dan Gohman7f321562007-06-25 16:23:39 +00002409 Tmp2 = ScalarizeVectorOp(Tmp2);
2410 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002411 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002412
2413 // FIXME: Returns of gcc generic vectors smaller than a legal type
2414 // should be returned in integer registers!
2415
Chris Lattnerf87324e2006-04-11 01:31:51 +00002416 // The scalarized value type may not be legal, e.g. it might require
2417 // promotion or expansion. Relegalize the return.
2418 Result = LegalizeOp(Result);
2419 } else {
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002420 // FIXME: Returns of gcc generic vectors larger than a legal vector
2421 // type should be returned by reference!
Dan Gohman475871a2008-07-27 21:46:04 +00002422 SDValue Lo, Hi;
Chris Lattnerf87324e2006-04-11 01:31:51 +00002423 SplitVectorOp(Tmp2, Lo, Hi);
Chris Lattnerfea997a2007-02-01 04:55:59 +00002424 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Tmp3, Hi,Tmp3);
Chris Lattnerf87324e2006-04-11 01:31:51 +00002425 Result = LegalizeOp(Result);
2426 }
2427 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002428 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002429 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002430 Tmp2 = PromoteOp(Node->getOperand(1));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002431 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002432 Result = LegalizeOp(Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002433 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002434 }
2435 break;
2436 case 1: // ret void
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002437 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002438 break;
2439 default: { // ret <values>
Dan Gohman475871a2008-07-27 21:46:04 +00002440 SmallVector<SDValue, 8> NewValues;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002441 NewValues.push_back(Tmp1);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002442 for (unsigned i = 1, e = Node->getNumOperands(); i < e; i += 2)
Chris Lattner3e928bb2005-01-07 07:47:09 +00002443 switch (getTypeAction(Node->getOperand(i).getValueType())) {
2444 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00002445 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Evan Cheng8e7d0562006-05-26 23:09:09 +00002446 NewValues.push_back(Node->getOperand(i+1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002447 break;
2448 case Expand: {
Dan Gohman475871a2008-07-27 21:46:04 +00002449 SDValue Lo, Hi;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002450 assert(!Node->getOperand(i).getValueType().isExtended() &&
Chris Lattnerb49e52c2006-04-11 02:00:08 +00002451 "FIXME: TODO: implement returning non-legal vector types!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002452 ExpandOp(Node->getOperand(i), Lo, Hi);
2453 NewValues.push_back(Lo);
Evan Cheng8e7d0562006-05-26 23:09:09 +00002454 NewValues.push_back(Node->getOperand(i+1));
Gabor Greifba36cb52008-08-28 21:40:38 +00002455 if (Hi.getNode()) {
Evan Cheng13acce32006-12-11 19:27:14 +00002456 NewValues.push_back(Hi);
2457 NewValues.push_back(Node->getOperand(i+1));
2458 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00002459 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002460 }
2461 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002462 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002463 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002464
2465 if (NewValues.size() == Node->getNumOperands())
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002466 Result = DAG.UpdateNodeOperands(Result, &NewValues[0],NewValues.size());
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002467 else
Chris Lattnerf06f35e2006-08-08 01:09:31 +00002468 Result = DAG.getNode(ISD::RET, MVT::Other,
2469 &NewValues[0], NewValues.size());
Chris Lattner3e928bb2005-01-07 07:47:09 +00002470 break;
2471 }
2472 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002473
Chris Lattner6862dbc2006-01-29 21:02:23 +00002474 if (Result.getOpcode() == ISD::RET) {
2475 switch (TLI.getOperationAction(Result.getOpcode(), MVT::Other)) {
2476 default: assert(0 && "This action is not supported yet!");
2477 case TargetLowering::Legal: break;
2478 case TargetLowering::Custom:
2479 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002480 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner6862dbc2006-01-29 21:02:23 +00002481 break;
2482 }
Evan Cheng17c428e2006-01-06 00:41:43 +00002483 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002484 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002485 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002486 StoreSDNode *ST = cast<StoreSDNode>(Node);
2487 Tmp1 = LegalizeOp(ST->getChain()); // Legalize the chain.
2488 Tmp2 = LegalizeOp(ST->getBasePtr()); // Legalize the pointer.
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002489 int SVOffset = ST->getSrcValueOffset();
2490 unsigned Alignment = ST->getAlignment();
2491 bool isVolatile = ST->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002492
Evan Cheng8b2794a2006-10-13 21:14:26 +00002493 if (!ST->isTruncatingStore()) {
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002494 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
2495 // FIXME: We shouldn't do this for TargetConstantFP's.
2496 // FIXME: move this to the DAG Combiner! Note that we can't regress due
2497 // to phase ordering between legalized code and the dag combiner. This
2498 // probably means that we need to integrate dag combiner and legalizer
2499 // together.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002500 // We generally can't do this one for long doubles.
Chris Lattner2b4c2792007-10-13 06:35:54 +00002501 if (ConstantFPSDNode *CFP = dyn_cast<ConstantFPSDNode>(ST->getValue())) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002502 if (CFP->getValueType(0) == MVT::f32 &&
2503 getTypeAction(MVT::i32) == Legal) {
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002504 Tmp3 = DAG.getConstant(CFP->getValueAPF().
Dale Johannesen7111b022008-10-09 18:53:47 +00002505 bitcastToAPInt().zextOrTrunc(32),
Dale Johannesen3f6eb742007-09-11 18:32:33 +00002506 MVT::i32);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00002507 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2508 SVOffset, isVolatile, Alignment);
2509 break;
2510 } else if (CFP->getValueType(0) == MVT::f64) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002511 // If this target supports 64-bit registers, do a single 64-bit store.
2512 if (getTypeAction(MVT::i64) == Legal) {
Dale Johannesen7111b022008-10-09 18:53:47 +00002513 Tmp3 = DAG.getConstant(CFP->getValueAPF().bitcastToAPInt().
Dan Gohman6cf9b8a2008-03-11 00:11:06 +00002514 zextOrTrunc(64), MVT::i64);
Chris Lattner3cb93512007-10-15 05:46:06 +00002515 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2516 SVOffset, isVolatile, Alignment);
2517 break;
Duncan Sandsd4b9c172008-06-13 19:07:40 +00002518 } else if (getTypeAction(MVT::i32) == Legal && !ST->isVolatile()) {
Chris Lattner3cb93512007-10-15 05:46:06 +00002519 // Otherwise, if the target supports 32-bit registers, use 2 32-bit
2520 // stores. If the target supports neither 32- nor 64-bits, this
2521 // xform is certainly not worth it.
Dale Johannesen7111b022008-10-09 18:53:47 +00002522 const APInt &IntVal =CFP->getValueAPF().bitcastToAPInt();
Dan Gohman475871a2008-07-27 21:46:04 +00002523 SDValue Lo = DAG.getConstant(APInt(IntVal).trunc(32), MVT::i32);
2524 SDValue Hi = DAG.getConstant(IntVal.lshr(32).trunc(32), MVT::i32);
Duncan Sands0753fc12008-02-11 10:37:04 +00002525 if (TLI.isBigEndian()) std::swap(Lo, Hi);
Chris Lattner3cb93512007-10-15 05:46:06 +00002526
2527 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
2528 SVOffset, isVolatile, Alignment);
2529 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002530 DAG.getIntPtrConstant(4));
Chris Lattner3cb93512007-10-15 05:46:06 +00002531 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset+4,
Duncan Sandsdc846502007-10-28 12:59:45 +00002532 isVolatile, MinAlign(Alignment, 4U));
Chris Lattner3cb93512007-10-15 05:46:06 +00002533
2534 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2535 break;
2536 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002537 }
Chris Lattnerd93d46e2006-12-12 04:18:56 +00002538 }
2539
Dan Gohmanb625f2f2008-01-30 00:15:11 +00002540 switch (getTypeAction(ST->getMemoryVT())) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002541 case Legal: {
2542 Tmp3 = LegalizeOp(ST->getValue());
2543 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2544 ST->getOffset());
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002545
Duncan Sands83ec4b62008-06-06 12:08:01 +00002546 MVT VT = Tmp3.getValueType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002547 switch (TLI.getOperationAction(ISD::STORE, VT)) {
2548 default: assert(0 && "This action is not supported yet!");
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002549 case TargetLowering::Legal:
2550 // If this is an unaligned store and the target doesn't support it,
2551 // expand it.
2552 if (!TLI.allowsUnalignedMemoryAccesses()) {
2553 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002554 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002555 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002556 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002557 TLI);
2558 }
2559 break;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002560 case TargetLowering::Custom:
2561 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002562 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002563 break;
2564 case TargetLowering::Promote:
Duncan Sands83ec4b62008-06-06 12:08:01 +00002565 assert(VT.isVector() && "Unknown legal promote case!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00002566 Tmp3 = DAG.getNode(ISD::BIT_CONVERT,
2567 TLI.getTypeToPromoteTo(ISD::STORE, VT), Tmp3);
2568 Result = DAG.getStore(Tmp1, Tmp3, Tmp2,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002569 ST->getSrcValue(), SVOffset, isVolatile,
2570 Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002571 break;
2572 }
2573 break;
2574 }
2575 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002576 if (!ST->getMemoryVT().isVector()) {
2577 // Truncate the value and store the result.
2578 Tmp3 = PromoteOp(ST->getValue());
2579 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2580 SVOffset, ST->getMemoryVT(),
2581 isVolatile, Alignment);
2582 break;
2583 }
2584 // Fall thru to expand for vector
2585 case Expand: {
Evan Cheng8b2794a2006-10-13 21:14:26 +00002586 unsigned IncrementSize = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002587 SDValue Lo, Hi;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002588
2589 // If this is a vector type, then we have to calculate the increment as
2590 // the product of the element size in bytes, and the number of elements
2591 // in the high half of the vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002592 if (ST->getValue().getValueType().isVector()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002593 SDNode *InVal = ST->getValue().getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00002594 int InIx = ST->getValue().getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00002595 MVT InVT = InVal->getValueType(InIx);
2596 unsigned NumElems = InVT.getVectorNumElements();
2597 MVT EVT = InVT.getVectorElementType();
Evan Cheng8b2794a2006-10-13 21:14:26 +00002598
Dan Gohman7f321562007-06-25 16:23:39 +00002599 // Figure out if there is a simple type corresponding to this Vector
Reid Spencerac9dcb92007-02-15 03:39:18 +00002600 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002601 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00002602 if (TLI.isTypeLegal(TVT)) {
Reid Spencerac9dcb92007-02-15 03:39:18 +00002603 // Turn this into a normal store of the vector type.
Dan Gohman21be3842008-02-15 18:11:59 +00002604 Tmp3 = LegalizeOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002605 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002606 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002607 Result = LegalizeOp(Result);
2608 break;
2609 } else if (NumElems == 1) {
2610 // Turn this into a normal store of the scalar type.
Dan Gohman21be3842008-02-15 18:11:59 +00002611 Tmp3 = ScalarizeVectorOp(ST->getValue());
Evan Cheng8b2794a2006-10-13 21:14:26 +00002612 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002613 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002614 // The scalarized value type may not be legal, e.g. it might require
2615 // promotion or expansion. Relegalize the scalar store.
2616 Result = LegalizeOp(Result);
2617 break;
2618 } else {
Mon P Wang0c397192008-10-30 08:01:45 +00002619 // Check if we have widen this node with another value
2620 std::map<SDValue, SDValue>::iterator I =
2621 WidenNodes.find(ST->getValue());
2622 if (I != WidenNodes.end()) {
2623 Result = StoreWidenVectorOp(ST, Tmp1, Tmp2);
2624 break;
2625 }
2626 else {
2627 SplitVectorOp(ST->getValue(), Lo, Hi);
2628 IncrementSize = Lo.getNode()->getValueType(0).getVectorNumElements() *
2629 EVT.getSizeInBits()/8;
2630 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002631 }
2632 } else {
Dan Gohman21be3842008-02-15 18:11:59 +00002633 ExpandOp(ST->getValue(), Lo, Hi);
Gabor Greifba36cb52008-08-28 21:40:38 +00002634 IncrementSize = Hi.getNode() ? Hi.getValueType().getSizeInBits()/8 : 0;
Evan Cheng8b2794a2006-10-13 21:14:26 +00002635
Richard Pennington4b052dc2008-09-25 16:15:10 +00002636 if (Hi.getNode() && TLI.isBigEndian())
Evan Cheng8b2794a2006-10-13 21:14:26 +00002637 std::swap(Lo, Hi);
2638 }
2639
2640 Lo = DAG.getStore(Tmp1, Lo, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002641 SVOffset, isVolatile, Alignment);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002642
Gabor Greifba36cb52008-08-28 21:40:38 +00002643 if (Hi.getNode() == NULL) {
Evan Cheng7b2b5c82006-12-12 19:53:13 +00002644 // Must be int <-> float one-to-one expansion.
2645 Result = Lo;
2646 break;
2647 }
2648
Evan Cheng8b2794a2006-10-13 21:14:26 +00002649 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
Chris Lattner0bd48932008-01-17 07:00:52 +00002650 DAG.getIntPtrConstant(IncrementSize));
Evan Cheng8b2794a2006-10-13 21:14:26 +00002651 assert(isTypeLegal(Tmp2.getValueType()) &&
2652 "Pointers must be legal!");
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002653 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00002654 Alignment = MinAlign(Alignment, IncrementSize);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002655 Hi = DAG.getStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00002656 SVOffset, isVolatile, Alignment);
Evan Cheng8b2794a2006-10-13 21:14:26 +00002657 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2658 break;
Mon P Wang0c397192008-10-30 08:01:45 +00002659 } // case Expand
Evan Cheng8b2794a2006-10-13 21:14:26 +00002660 }
2661 } else {
Chris Lattnerddf89562008-01-17 19:59:44 +00002662 switch (getTypeAction(ST->getValue().getValueType())) {
2663 case Legal:
2664 Tmp3 = LegalizeOp(ST->getValue());
2665 break;
2666 case Promote:
Mon P Wang0c397192008-10-30 08:01:45 +00002667 if (!ST->getValue().getValueType().isVector()) {
2668 // We can promote the value, the truncstore will still take care of it.
2669 Tmp3 = PromoteOp(ST->getValue());
2670 break;
2671 }
2672 // Vector case falls through to expand
Chris Lattnerddf89562008-01-17 19:59:44 +00002673 case Expand:
2674 // Just store the low part. This may become a non-trunc store, so make
2675 // sure to use getTruncStore, not UpdateNodeOperands below.
2676 ExpandOp(ST->getValue(), Tmp3, Tmp4);
2677 return DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2678 SVOffset, MVT::i8, isVolatile, Alignment);
2679 }
Evan Cheng8b2794a2006-10-13 21:14:26 +00002680
Duncan Sands83ec4b62008-06-06 12:08:01 +00002681 MVT StVT = ST->getMemoryVT();
2682 unsigned StWidth = StVT.getSizeInBits();
Duncan Sands7e857202008-01-22 07:17:34 +00002683
Duncan Sands83ec4b62008-06-06 12:08:01 +00002684 if (StWidth != StVT.getStoreSizeInBits()) {
Duncan Sands7e857202008-01-22 07:17:34 +00002685 // Promote to a byte-sized store with upper bits zero if not
2686 // storing an integral number of bytes. For example, promote
2687 // TRUNCSTORE:i1 X -> TRUNCSTORE:i8 (and X, 1)
Duncan Sands83ec4b62008-06-06 12:08:01 +00002688 MVT NVT = MVT::getIntegerVT(StVT.getStoreSizeInBits());
Duncan Sands7e857202008-01-22 07:17:34 +00002689 Tmp3 = DAG.getZeroExtendInReg(Tmp3, StVT);
2690 Result = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2691 SVOffset, NVT, isVolatile, Alignment);
2692 } else if (StWidth & (StWidth - 1)) {
2693 // If not storing a power-of-2 number of bits, expand as two stores.
Duncan Sands83ec4b62008-06-06 12:08:01 +00002694 assert(StVT.isExtended() && !StVT.isVector() &&
Duncan Sands7e857202008-01-22 07:17:34 +00002695 "Unsupported truncstore!");
2696 unsigned RoundWidth = 1 << Log2_32(StWidth);
2697 assert(RoundWidth < StWidth);
2698 unsigned ExtraWidth = StWidth - RoundWidth;
2699 assert(ExtraWidth < RoundWidth);
2700 assert(!(RoundWidth % 8) && !(ExtraWidth % 8) &&
2701 "Store size not an integral number of bytes!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00002702 MVT RoundVT = MVT::getIntegerVT(RoundWidth);
2703 MVT ExtraVT = MVT::getIntegerVT(ExtraWidth);
Dan Gohman475871a2008-07-27 21:46:04 +00002704 SDValue Lo, Hi;
Duncan Sands7e857202008-01-22 07:17:34 +00002705 unsigned IncrementSize;
2706
2707 if (TLI.isLittleEndian()) {
2708 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 X, TRUNCSTORE@+2:i8 (srl X, 16)
2709 // Store the bottom RoundWidth bits.
2710 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2711 SVOffset, RoundVT,
2712 isVolatile, Alignment);
2713
2714 // Store the remaining ExtraWidth bits.
2715 IncrementSize = RoundWidth / 8;
2716 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2717 DAG.getIntPtrConstant(IncrementSize));
2718 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2719 DAG.getConstant(RoundWidth, TLI.getShiftAmountTy()));
2720 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(),
2721 SVOffset + IncrementSize, ExtraVT, isVolatile,
2722 MinAlign(Alignment, IncrementSize));
2723 } else {
2724 // Big endian - avoid unaligned stores.
2725 // TRUNCSTORE:i24 X -> TRUNCSTORE:i16 (srl X, 8), TRUNCSTORE@+2:i8 X
2726 // Store the top RoundWidth bits.
2727 Hi = DAG.getNode(ISD::SRL, Tmp3.getValueType(), Tmp3,
2728 DAG.getConstant(ExtraWidth, TLI.getShiftAmountTy()));
2729 Hi = DAG.getTruncStore(Tmp1, Hi, Tmp2, ST->getSrcValue(), SVOffset,
2730 RoundVT, isVolatile, Alignment);
2731
2732 // Store the remaining ExtraWidth bits.
2733 IncrementSize = RoundWidth / 8;
2734 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
2735 DAG.getIntPtrConstant(IncrementSize));
2736 Lo = DAG.getTruncStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(),
2737 SVOffset + IncrementSize, ExtraVT, isVolatile,
2738 MinAlign(Alignment, IncrementSize));
Lauro Ramos Venanciof3c13c82007-08-01 19:34:21 +00002739 }
Duncan Sands7e857202008-01-22 07:17:34 +00002740
2741 // The order of the stores doesn't matter.
2742 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
2743 } else {
2744 if (Tmp1 != ST->getChain() || Tmp3 != ST->getValue() ||
2745 Tmp2 != ST->getBasePtr())
2746 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp3, Tmp2,
2747 ST->getOffset());
2748
2749 switch (TLI.getTruncStoreAction(ST->getValue().getValueType(), StVT)) {
2750 default: assert(0 && "This action is not supported yet!");
2751 case TargetLowering::Legal:
2752 // If this is an unaligned store and the target doesn't support it,
2753 // expand it.
2754 if (!TLI.allowsUnalignedMemoryAccesses()) {
2755 unsigned ABIAlignment = TLI.getTargetData()->
Duncan Sands83ec4b62008-06-06 12:08:01 +00002756 getABITypeAlignment(ST->getMemoryVT().getTypeForMVT());
Duncan Sands7e857202008-01-22 07:17:34 +00002757 if (ST->getAlignment() < ABIAlignment)
Gabor Greifba36cb52008-08-28 21:40:38 +00002758 Result = ExpandUnalignedStore(cast<StoreSDNode>(Result.getNode()), DAG,
Duncan Sands7e857202008-01-22 07:17:34 +00002759 TLI);
2760 }
2761 break;
2762 case TargetLowering::Custom:
2763 Result = TLI.LowerOperation(Result, DAG);
2764 break;
2765 case Expand:
2766 // TRUNCSTORE:i16 i32 -> STORE i16
2767 assert(isTypeLegal(StVT) && "Do not know how to expand this store!");
2768 Tmp3 = DAG.getNode(ISD::TRUNCATE, StVT, Tmp3);
2769 Result = DAG.getStore(Tmp1, Tmp3, Tmp2, ST->getSrcValue(), SVOffset,
2770 isVolatile, Alignment);
2771 break;
2772 }
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002773 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002774 }
2775 break;
Evan Chengf3fd9fe2005-12-23 07:29:34 +00002776 }
Andrew Lenharth95762122005-03-31 21:24:06 +00002777 case ISD::PCMARKER:
2778 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002779 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00002780 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002781 case ISD::STACKSAVE:
2782 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002783 Result = DAG.UpdateNodeOperands(Result, Tmp1);
2784 Tmp1 = Result.getValue(0);
2785 Tmp2 = Result.getValue(1);
Chris Lattner456a93a2006-01-28 07:39:30 +00002786
Chris Lattner140d53c2006-01-13 02:50:02 +00002787 switch (TLI.getOperationAction(ISD::STACKSAVE, MVT::Other)) {
2788 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002789 case TargetLowering::Legal: break;
2790 case TargetLowering::Custom:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002791 Tmp3 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002792 if (Tmp3.getNode()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002793 Tmp1 = LegalizeOp(Tmp3);
2794 Tmp2 = LegalizeOp(Tmp3.getValue(1));
Chris Lattner140d53c2006-01-13 02:50:02 +00002795 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002796 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002797 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002798 // Expand to CopyFromReg if the target set
2799 // StackPointerRegisterToSaveRestore.
2800 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002801 Tmp1 = DAG.getCopyFromReg(Result.getOperand(0), SP,
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002802 Node->getValueType(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002803 Tmp2 = Tmp1.getValue(1);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002804 } else {
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002805 Tmp1 = DAG.getNode(ISD::UNDEF, Node->getValueType(0));
2806 Tmp2 = Node->getOperand(0);
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002807 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002808 break;
Chris Lattner140d53c2006-01-13 02:50:02 +00002809 }
Chris Lattner456a93a2006-01-28 07:39:30 +00002810
2811 // Since stacksave produce two values, make sure to remember that we
2812 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002813 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2814 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00002815 return Op.getResNo() ? Tmp2 : Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002816
Chris Lattner140d53c2006-01-13 02:50:02 +00002817 case ISD::STACKRESTORE:
2818 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2819 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002820 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner140d53c2006-01-13 02:50:02 +00002821
2822 switch (TLI.getOperationAction(ISD::STACKRESTORE, MVT::Other)) {
2823 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002824 case TargetLowering::Legal: break;
2825 case TargetLowering::Custom:
2826 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002827 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner140d53c2006-01-13 02:50:02 +00002828 break;
2829 case TargetLowering::Expand:
Chris Lattner4f0d8e42006-01-13 17:48:44 +00002830 // Expand to CopyToReg if the target set
2831 // StackPointerRegisterToSaveRestore.
2832 if (unsigned SP = TLI.getStackPointerRegisterToSaveRestore()) {
2833 Result = DAG.getCopyToReg(Tmp1, SP, Tmp2);
2834 } else {
2835 Result = Tmp1;
2836 }
Chris Lattner140d53c2006-01-13 02:50:02 +00002837 break;
2838 }
2839 break;
2840
Andrew Lenharth51b8d542005-11-11 16:47:30 +00002841 case ISD::READCYCLECOUNTER:
2842 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002843 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Chengf0b3ba62006-11-29 08:26:18 +00002844 switch (TLI.getOperationAction(ISD::READCYCLECOUNTER,
2845 Node->getValueType(0))) {
2846 default: assert(0 && "This action is not supported yet!");
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002847 case TargetLowering::Legal:
2848 Tmp1 = Result.getValue(0);
2849 Tmp2 = Result.getValue(1);
2850 break;
Evan Chengf0b3ba62006-11-29 08:26:18 +00002851 case TargetLowering::Custom:
2852 Result = TLI.LowerOperation(Result, DAG);
Evan Cheng6a16c5a2006-11-29 19:13:47 +00002853 Tmp1 = LegalizeOp(Result.getValue(0));
2854 Tmp2 = LegalizeOp(Result.getValue(1));
Evan Chengf0b3ba62006-11-29 08:26:18 +00002855 break;
2856 }
Andrew Lenharth49c709f2005-12-02 04:56:24 +00002857
2858 // Since rdcc produce two values, make sure to remember that we legalized
2859 // both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00002860 AddLegalizedOperand(SDValue(Node, 0), Tmp1);
2861 AddLegalizedOperand(SDValue(Node, 1), Tmp2);
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002862 return Result;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00002863
Chris Lattner2ee743f2005-01-14 22:08:15 +00002864 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002865 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2866 case Expand: assert(0 && "It's impossible to expand bools");
2867 case Legal:
2868 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2869 break;
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002870 case Promote: {
Mon P Wang0c397192008-10-30 08:01:45 +00002871 assert(!Node->getOperand(0).getValueType().isVector() && "not possible");
Chris Lattner47e92232005-01-18 19:27:06 +00002872 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
Chris Lattnerb6c80602006-11-28 01:03:30 +00002873 // Make sure the condition is either zero or one.
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002874 unsigned BitWidth = Tmp1.getValueSizeInBits();
Dan Gohmanea859be2007-06-22 14:59:07 +00002875 if (!DAG.MaskedValueIsZero(Tmp1,
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002876 APInt::getHighBitsSet(BitWidth, BitWidth-1)))
Chris Lattnerb6c80602006-11-28 01:03:30 +00002877 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Chris Lattner47e92232005-01-18 19:27:06 +00002878 break;
2879 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00002880 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002881 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00002882 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002883
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00002884 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002885
Nate Begemanb942a3d2005-08-23 04:29:48 +00002886 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002887 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00002888 case TargetLowering::Legal: break;
2889 case TargetLowering::Custom: {
2890 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002891 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00002892 break;
2893 }
Nate Begeman9373a812005-08-10 20:51:12 +00002894 case TargetLowering::Expand:
2895 if (Tmp1.getOpcode() == ISD::SETCC) {
2896 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
2897 Tmp2, Tmp3,
2898 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
2899 } else {
2900 Result = DAG.getSelectCC(Tmp1,
2901 DAG.getConstant(0, Tmp1.getValueType()),
2902 Tmp2, Tmp3, ISD::SETNE);
2903 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002904 break;
2905 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002906 MVT NVT =
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002907 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
2908 unsigned ExtOp, TruncOp;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002909 if (Tmp2.getValueType().isVector()) {
Evan Cheng41f6cbb2006-04-12 16:33:18 +00002910 ExtOp = ISD::BIT_CONVERT;
2911 TruncOp = ISD::BIT_CONVERT;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002912 } else if (Tmp2.getValueType().isInteger()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002913 ExtOp = ISD::ANY_EXTEND;
2914 TruncOp = ISD::TRUNCATE;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002915 } else {
Chris Lattner456a93a2006-01-28 07:39:30 +00002916 ExtOp = ISD::FP_EXTEND;
2917 TruncOp = ISD::FP_ROUND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002918 }
2919 // Promote each of the values to the new type.
2920 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
2921 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
2922 // Perform the larger operation, then round down.
2923 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
Chris Lattner0bd48932008-01-17 07:00:52 +00002924 if (TruncOp != ISD::FP_ROUND)
2925 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
2926 else
2927 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result,
2928 DAG.getIntPtrConstant(0));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002929 break;
2930 }
2931 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002932 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002933 case ISD::SELECT_CC: {
2934 Tmp1 = Node->getOperand(0); // LHS
2935 Tmp2 = Node->getOperand(1); // RHS
Nate Begeman9373a812005-08-10 20:51:12 +00002936 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
2937 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
Dan Gohman475871a2008-07-27 21:46:04 +00002938 SDValue CC = Node->getOperand(4);
Nate Begeman9373a812005-08-10 20:51:12 +00002939
Dale Johannesen53e4e442008-11-07 22:54:33 +00002940 LegalizeSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2, CC);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002941
Evan Cheng7f042682008-10-15 02:05:31 +00002942 // If we didn't get both a LHS and RHS back from LegalizeSetCC,
Nate Begeman750ac1b2006-02-01 07:19:44 +00002943 // the LHS is a legal SETCC itself. In this case, we need to compare
2944 // the result against zero to select between true and false values.
Gabor Greifba36cb52008-08-28 21:40:38 +00002945 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002946 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
2947 CC = DAG.getCondCode(ISD::SETNE);
2948 }
2949 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3, Tmp4, CC);
2950
2951 // Everything is legal, see if we should expand this op or something.
2952 switch (TLI.getOperationAction(ISD::SELECT_CC, Tmp3.getValueType())) {
2953 default: assert(0 && "This action is not supported yet!");
2954 case TargetLowering::Legal: break;
2955 case TargetLowering::Custom:
2956 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002957 if (Tmp1.getNode()) Result = Tmp1;
Nate Begeman9373a812005-08-10 20:51:12 +00002958 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002959 }
2960 break;
Nate Begeman750ac1b2006-02-01 07:19:44 +00002961 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002962 case ISD::SETCC:
Nate Begeman750ac1b2006-02-01 07:19:44 +00002963 Tmp1 = Node->getOperand(0);
2964 Tmp2 = Node->getOperand(1);
2965 Tmp3 = Node->getOperand(2);
Evan Cheng7f042682008-10-15 02:05:31 +00002966 LegalizeSetCC(Node->getValueType(0), Tmp1, Tmp2, Tmp3);
Nate Begeman750ac1b2006-02-01 07:19:44 +00002967
2968 // If we had to Expand the SetCC operands into a SELECT node, then it may
2969 // not always be possible to return a true LHS & RHS. In this case, just
2970 // return the value we legalized, returned in the LHS
Gabor Greifba36cb52008-08-28 21:40:38 +00002971 if (Tmp2.getNode() == 0) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00002972 Result = Tmp1;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002973 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002974 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002975
Chris Lattner73e142f2006-01-30 22:43:50 +00002976 switch (TLI.getOperationAction(ISD::SETCC, Tmp1.getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00002977 default: assert(0 && "Cannot handle this action for SETCC yet!");
2978 case TargetLowering::Custom:
2979 isCustom = true;
2980 // FALLTHROUGH.
2981 case TargetLowering::Legal:
Evan Cheng2b49c502006-12-15 02:59:56 +00002982 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Chris Lattner456a93a2006-01-28 07:39:30 +00002983 if (isCustom) {
Evan Cheng2b49c502006-12-15 02:59:56 +00002984 Tmp4 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00002985 if (Tmp4.getNode()) Result = Tmp4;
Chris Lattner456a93a2006-01-28 07:39:30 +00002986 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00002987 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00002988 case TargetLowering::Promote: {
2989 // First step, figure out the appropriate operation to use.
2990 // Allow SETCC to not be supported for all legal data types
2991 // Mostly this targets FP
Duncan Sands83ec4b62008-06-06 12:08:01 +00002992 MVT NewInTy = Node->getOperand(0).getValueType();
2993 MVT OldVT = NewInTy; OldVT = OldVT;
Andrew Lenharthae355752005-11-30 17:12:26 +00002994
2995 // Scan for the appropriate larger type to use.
2996 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002997 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
Andrew Lenharthae355752005-11-30 17:12:26 +00002998
Duncan Sands83ec4b62008-06-06 12:08:01 +00002999 assert(NewInTy.isInteger() == OldVT.isInteger() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003000 "Fell off of the edge of the integer world");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003001 assert(NewInTy.isFloatingPoint() == OldVT.isFloatingPoint() &&
Andrew Lenharthae355752005-11-30 17:12:26 +00003002 "Fell off of the edge of the floating point world");
3003
3004 // If the target supports SETCC of this type, use it.
Chris Lattner1ccf26a2005-12-22 05:23:45 +00003005 if (TLI.isOperationLegal(ISD::SETCC, NewInTy))
Andrew Lenharthae355752005-11-30 17:12:26 +00003006 break;
3007 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00003008 if (NewInTy.isInteger())
Andrew Lenharthae355752005-11-30 17:12:26 +00003009 assert(0 && "Cannot promote Legal Integer SETCC yet");
3010 else {
3011 Tmp1 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp1);
3012 Tmp2 = DAG.getNode(ISD::FP_EXTEND, NewInTy, Tmp2);
3013 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003014 Tmp1 = LegalizeOp(Tmp1);
3015 Tmp2 = LegalizeOp(Tmp2);
Evan Cheng2b49c502006-12-15 02:59:56 +00003016 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Evan Cheng433f8ac2006-01-17 19:47:13 +00003017 Result = LegalizeOp(Result);
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00003018 break;
Andrew Lenharthae355752005-11-30 17:12:26 +00003019 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00003020 case TargetLowering::Expand:
3021 // Expand a setcc node into a select_cc of the same condition, lhs, and
3022 // rhs that selects between const 1 (true) and const 0 (false).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003023 MVT VT = Node->getValueType(0);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003024 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
3025 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
Evan Cheng2b49c502006-12-15 02:59:56 +00003026 Tmp3);
Nate Begemanb942a3d2005-08-23 04:29:48 +00003027 break;
3028 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003029 break;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003030 case ISD::VSETCC: {
3031 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3032 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Dan Gohman475871a2008-07-27 21:46:04 +00003033 SDValue CC = Node->getOperand(2);
Nate Begemanb43e9c12008-05-12 19:40:03 +00003034
3035 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, CC);
3036
3037 // Everything is legal, see if we should expand this op or something.
3038 switch (TLI.getOperationAction(ISD::VSETCC, Tmp1.getValueType())) {
3039 default: assert(0 && "This action is not supported yet!");
3040 case TargetLowering::Legal: break;
3041 case TargetLowering::Custom:
3042 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003043 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanb43e9c12008-05-12 19:40:03 +00003044 break;
3045 }
3046 break;
3047 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00003048
Chris Lattner5b359c62005-04-02 04:00:59 +00003049 case ISD::SHL_PARTS:
3050 case ISD::SRA_PARTS:
3051 case ISD::SRL_PARTS: {
Dan Gohman475871a2008-07-27 21:46:04 +00003052 SmallVector<SDValue, 8> Ops;
Chris Lattner84f67882005-01-20 18:52:28 +00003053 bool Changed = false;
3054 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
3055 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3056 Changed |= Ops.back() != Node->getOperand(i);
3057 }
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003058 if (Changed)
Chris Lattnerf06f35e2006-08-08 01:09:31 +00003059 Result = DAG.UpdateNodeOperands(Result, &Ops[0], Ops.size());
Chris Lattner2c8086f2005-04-02 05:00:07 +00003060
Evan Cheng05a2d562006-01-09 18:31:59 +00003061 switch (TLI.getOperationAction(Node->getOpcode(),
3062 Node->getValueType(0))) {
3063 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003064 case TargetLowering::Legal: break;
3065 case TargetLowering::Custom:
3066 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003067 if (Tmp1.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003068 SDValue Tmp2, RetVal(0, 0);
Evan Cheng05a2d562006-01-09 18:31:59 +00003069 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003070 Tmp2 = LegalizeOp(Tmp1.getValue(i));
Dan Gohman475871a2008-07-27 21:46:04 +00003071 AddLegalizedOperand(SDValue(Node, i), Tmp2);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003072 if (i == Op.getResNo())
Evan Cheng12f22742006-01-19 04:54:52 +00003073 RetVal = Tmp2;
Evan Cheng05a2d562006-01-09 18:31:59 +00003074 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003075 assert(RetVal.getNode() && "Illegal result number");
Evan Cheng05a2d562006-01-09 18:31:59 +00003076 return RetVal;
3077 }
Evan Cheng05a2d562006-01-09 18:31:59 +00003078 break;
3079 }
3080
Chris Lattner2c8086f2005-04-02 05:00:07 +00003081 // Since these produce multiple values, make sure to remember that we
3082 // legalized all of them.
3083 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Dan Gohman475871a2008-07-27 21:46:04 +00003084 AddLegalizedOperand(SDValue(Node, i), Result.getValue(i));
Gabor Greif99a6cb92008-08-26 22:36:50 +00003085 return Result.getValue(Op.getResNo());
Chris Lattner84f67882005-01-20 18:52:28 +00003086 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003087
3088 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00003089 case ISD::ADD:
3090 case ISD::SUB:
3091 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00003092 case ISD::MULHS:
3093 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003094 case ISD::UDIV:
3095 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003096 case ISD::AND:
3097 case ISD::OR:
3098 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00003099 case ISD::SHL:
3100 case ISD::SRL:
3101 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00003102 case ISD::FADD:
3103 case ISD::FSUB:
3104 case ISD::FMUL:
3105 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00003106 case ISD::FPOW:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003107 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00003108 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3109 case Expand: assert(0 && "Not possible");
3110 case Legal:
3111 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3112 break;
3113 case Promote:
3114 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3115 break;
3116 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003117
3118 if ((Node->getOpcode() == ISD::SHL ||
3119 Node->getOpcode() == ISD::SRL ||
3120 Node->getOpcode() == ISD::SRA) &&
3121 !Node->getValueType(0).isVector()) {
Mon P Wange9f10152008-12-09 05:46:39 +00003122 Tmp2 = LegalizeShiftAmount(Tmp2);
3123 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003124
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003125 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00003126
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003127 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003128 default: assert(0 && "BinOp legalize operation not supported");
Chris Lattner456a93a2006-01-28 07:39:30 +00003129 case TargetLowering::Legal: break;
3130 case TargetLowering::Custom:
3131 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003132 if (Tmp1.getNode()) {
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003133 Result = Tmp1;
3134 break;
Nate Begeman24dc3462008-07-29 19:07:27 +00003135 }
Nate Begeman5bc1ea02008-07-29 15:49:41 +00003136 // Fall through if the custom lower can't deal with the operation
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003137 case TargetLowering::Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003138 MVT VT = Op.getValueType();
Mon P Wang0c397192008-10-30 08:01:45 +00003139
Dan Gohman525178c2007-10-08 18:33:35 +00003140 // See if multiply or divide can be lowered using two-result operations.
3141 SDVTList VTs = DAG.getVTList(VT, VT);
3142 if (Node->getOpcode() == ISD::MUL) {
3143 // We just need the low half of the multiply; try both the signed
3144 // and unsigned forms. If the target supports both SMUL_LOHI and
3145 // UMUL_LOHI, form a preference by checking which forms of plain
3146 // MULH it supports.
3147 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, VT);
3148 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, VT);
3149 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, VT);
3150 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, VT);
3151 unsigned OpToUse = 0;
3152 if (HasSMUL_LOHI && !HasMULHS) {
3153 OpToUse = ISD::SMUL_LOHI;
3154 } else if (HasUMUL_LOHI && !HasMULHU) {
3155 OpToUse = ISD::UMUL_LOHI;
3156 } else if (HasSMUL_LOHI) {
3157 OpToUse = ISD::SMUL_LOHI;
3158 } else if (HasUMUL_LOHI) {
3159 OpToUse = ISD::UMUL_LOHI;
3160 }
3161 if (OpToUse) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003162 Result = SDValue(DAG.getNode(OpToUse, VTs, Tmp1, Tmp2).getNode(), 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003163 break;
3164 }
3165 }
3166 if (Node->getOpcode() == ISD::MULHS &&
3167 TLI.isOperationLegal(ISD::SMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003168 Result = SDValue(DAG.getNode(ISD::SMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3169 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003170 break;
3171 }
3172 if (Node->getOpcode() == ISD::MULHU &&
3173 TLI.isOperationLegal(ISD::UMUL_LOHI, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003174 Result = SDValue(DAG.getNode(ISD::UMUL_LOHI, VTs, Tmp1, Tmp2).getNode(),
3175 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003176 break;
3177 }
3178 if (Node->getOpcode() == ISD::SDIV &&
3179 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003180 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(),
3181 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003182 break;
3183 }
3184 if (Node->getOpcode() == ISD::UDIV &&
3185 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Chris Lattner31d71612008-10-04 21:27:46 +00003186 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(),
3187 0);
Dan Gohman525178c2007-10-08 18:33:35 +00003188 break;
3189 }
Mon P Wang0c397192008-10-30 08:01:45 +00003190
Dan Gohman82669522007-10-11 23:57:53 +00003191 // Check to see if we have a libcall for this operator.
3192 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
3193 bool isSigned = false;
3194 switch (Node->getOpcode()) {
3195 case ISD::UDIV:
3196 case ISD::SDIV:
3197 if (VT == MVT::i32) {
3198 LC = Node->getOpcode() == ISD::UDIV
Mon P Wang0c397192008-10-30 08:01:45 +00003199 ? RTLIB::UDIV_I32 : RTLIB::SDIV_I32;
Dan Gohman82669522007-10-11 23:57:53 +00003200 isSigned = Node->getOpcode() == ISD::SDIV;
3201 }
3202 break;
Chris Lattner31d71612008-10-04 21:27:46 +00003203 case ISD::MUL:
3204 if (VT == MVT::i32)
3205 LC = RTLIB::MUL_I32;
3206 break;
Dan Gohman82669522007-10-11 23:57:53 +00003207 case ISD::FPOW:
Duncan Sands007f9842008-01-10 10:28:30 +00003208 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
3209 RTLIB::POW_PPCF128);
Dan Gohman82669522007-10-11 23:57:53 +00003210 break;
3211 default: break;
3212 }
3213 if (LC != RTLIB::UNKNOWN_LIBCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +00003214 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003215 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003216 break;
3217 }
Mon P Wang0c397192008-10-30 08:01:45 +00003218
Duncan Sands83ec4b62008-06-06 12:08:01 +00003219 assert(Node->getValueType(0).isVector() &&
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003220 "Cannot expand this binary operator!");
3221 // Expand the operation into a bunch of nasty scalar code.
Dan Gohman82669522007-10-11 23:57:53 +00003222 Result = LegalizeOp(UnrollVectorOp(Op));
Chris Lattnerbc70cf82006-04-02 03:57:31 +00003223 break;
3224 }
Evan Chengcc987612006-04-12 21:20:24 +00003225 case TargetLowering::Promote: {
3226 switch (Node->getOpcode()) {
3227 default: assert(0 && "Do not know how to promote this BinOp!");
3228 case ISD::AND:
3229 case ISD::OR:
3230 case ISD::XOR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003231 MVT OVT = Node->getValueType(0);
3232 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3233 assert(OVT.isVector() && "Cannot promote this BinOp!");
Evan Chengcc987612006-04-12 21:20:24 +00003234 // Bit convert each of the values to the new type.
3235 Tmp1 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp1);
3236 Tmp2 = DAG.getNode(ISD::BIT_CONVERT, NVT, Tmp2);
3237 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
3238 // Bit convert the result back the original type.
3239 Result = DAG.getNode(ISD::BIT_CONVERT, OVT, Result);
3240 break;
3241 }
3242 }
3243 }
Andrew Lenharthe8f65f12005-12-24 23:42:32 +00003244 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003245 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003246
Dan Gohmane14ea862007-10-05 14:17:22 +00003247 case ISD::SMUL_LOHI:
3248 case ISD::UMUL_LOHI:
3249 case ISD::SDIVREM:
3250 case ISD::UDIVREM:
3251 // These nodes will only be produced by target-specific lowering, so
3252 // they shouldn't be here if they aren't legal.
Duncan Sandsa7c97a72007-10-16 09:07:20 +00003253 assert(TLI.isOperationLegal(Node->getOpcode(), Node->getValueType(0)) &&
Dan Gohmane14ea862007-10-05 14:17:22 +00003254 "This must be legal!");
Dan Gohman525178c2007-10-08 18:33:35 +00003255
3256 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3257 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
3258 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Dan Gohmane14ea862007-10-05 14:17:22 +00003259 break;
3260
Chris Lattnera09f8482006-03-05 05:09:38 +00003261 case ISD::FCOPYSIGN: // FCOPYSIGN does not require LHS/RHS to match type!
3262 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3263 switch (getTypeAction(Node->getOperand(1).getValueType())) {
3264 case Expand: assert(0 && "Not possible");
3265 case Legal:
3266 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
3267 break;
3268 case Promote:
3269 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
3270 break;
3271 }
3272
3273 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
3274
3275 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3276 default: assert(0 && "Operation not supported");
3277 case TargetLowering::Custom:
3278 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003279 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner8f4191d2006-03-13 06:08:38 +00003280 break;
Chris Lattnera09f8482006-03-05 05:09:38 +00003281 case TargetLowering::Legal: break;
Evan Cheng912095b2007-01-04 21:56:39 +00003282 case TargetLowering::Expand: {
Evan Cheng636c7532007-01-05 23:33:44 +00003283 // If this target supports fabs/fneg natively and select is cheap,
3284 // do this efficiently.
3285 if (!TLI.isSelectExpensive() &&
3286 TLI.getOperationAction(ISD::FABS, Tmp1.getValueType()) ==
3287 TargetLowering::Legal &&
Evan Cheng912095b2007-01-04 21:56:39 +00003288 TLI.getOperationAction(ISD::FNEG, Tmp1.getValueType()) ==
Evan Cheng636c7532007-01-05 23:33:44 +00003289 TargetLowering::Legal) {
Chris Lattner8f4191d2006-03-13 06:08:38 +00003290 // Get the sign bit of the RHS.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003291 MVT IVT =
Chris Lattner8f4191d2006-03-13 06:08:38 +00003292 Tmp2.getValueType() == MVT::f32 ? MVT::i32 : MVT::i64;
Dan Gohman475871a2008-07-27 21:46:04 +00003293 SDValue SignBit = DAG.getNode(ISD::BIT_CONVERT, IVT, Tmp2);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003294 SignBit = DAG.getSetCC(TLI.getSetCCResultType(SignBit),
Chris Lattner8f4191d2006-03-13 06:08:38 +00003295 SignBit, DAG.getConstant(0, IVT), ISD::SETLT);
3296 // Get the absolute value of the result.
Dan Gohman475871a2008-07-27 21:46:04 +00003297 SDValue AbsVal = DAG.getNode(ISD::FABS, Tmp1.getValueType(), Tmp1);
Chris Lattner8f4191d2006-03-13 06:08:38 +00003298 // Select between the nabs and abs value based on the sign bit of
3299 // the input.
3300 Result = DAG.getNode(ISD::SELECT, AbsVal.getValueType(), SignBit,
3301 DAG.getNode(ISD::FNEG, AbsVal.getValueType(),
3302 AbsVal),
3303 AbsVal);
3304 Result = LegalizeOp(Result);
3305 break;
3306 }
3307
3308 // Otherwise, do bitwise ops!
Duncan Sands83ec4b62008-06-06 12:08:01 +00003309 MVT NVT =
Evan Cheng912095b2007-01-04 21:56:39 +00003310 Node->getValueType(0) == MVT::f32 ? MVT::i32 : MVT::i64;
3311 Result = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
3312 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0), Result);
3313 Result = LegalizeOp(Result);
Chris Lattnera09f8482006-03-05 05:09:38 +00003314 break;
3315 }
Evan Cheng912095b2007-01-04 21:56:39 +00003316 }
Chris Lattnera09f8482006-03-05 05:09:38 +00003317 break;
3318
Nate Begeman551bf3f2006-02-17 05:43:56 +00003319 case ISD::ADDC:
3320 case ISD::SUBC:
3321 Tmp1 = LegalizeOp(Node->getOperand(0));
3322 Tmp2 = LegalizeOp(Node->getOperand(1));
3323 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003324 Tmp3 = Result.getValue(0);
3325 Tmp4 = Result.getValue(1);
3326
3327 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3328 default: assert(0 && "This action is not supported yet!");
3329 case TargetLowering::Legal:
3330 break;
3331 case TargetLowering::Custom:
3332 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3333 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003334 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003335 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3336 }
3337 break;
3338 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003339 // Since this produces two values, make sure to remember that we legalized
3340 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003341 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3342 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3343 return Op.getResNo() ? Tmp4 : Tmp3;
Misha Brukmanedf128a2005-04-21 22:36:52 +00003344
Nate Begeman551bf3f2006-02-17 05:43:56 +00003345 case ISD::ADDE:
3346 case ISD::SUBE:
3347 Tmp1 = LegalizeOp(Node->getOperand(0));
3348 Tmp2 = LegalizeOp(Node->getOperand(1));
3349 Tmp3 = LegalizeOp(Node->getOperand(2));
3350 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003351 Tmp3 = Result.getValue(0);
3352 Tmp4 = Result.getValue(1);
3353
3354 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3355 default: assert(0 && "This action is not supported yet!");
3356 case TargetLowering::Legal:
3357 break;
3358 case TargetLowering::Custom:
3359 Tmp1 = TLI.LowerOperation(Tmp3, DAG);
3360 if (Tmp1.getNode() != NULL) {
Sanjiv Gupta9b0f0b52008-11-27 05:58:04 +00003361 Tmp3 = LegalizeOp(Tmp1);
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003362 Tmp4 = LegalizeOp(Tmp1.getValue(1));
3363 }
3364 break;
3365 }
Nate Begeman551bf3f2006-02-17 05:43:56 +00003366 // Since this produces two values, make sure to remember that we legalized
3367 // both of them.
Sanjiv Guptad3f01aa2008-11-26 11:19:00 +00003368 AddLegalizedOperand(SDValue(Node, 0), Tmp3);
3369 AddLegalizedOperand(SDValue(Node, 1), Tmp4);
3370 return Op.getResNo() ? Tmp4 : Tmp3;
Nate Begeman551bf3f2006-02-17 05:43:56 +00003371
Nate Begeman419f8b62005-10-18 00:27:41 +00003372 case ISD::BUILD_PAIR: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003373 MVT PairTy = Node->getValueType(0);
Nate Begeman419f8b62005-10-18 00:27:41 +00003374 // TODO: handle the case where the Lo and Hi operands are not of legal type
3375 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
3376 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
3377 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003378 case TargetLowering::Promote:
3379 case TargetLowering::Custom:
3380 assert(0 && "Cannot promote/custom this yet!");
Nate Begeman419f8b62005-10-18 00:27:41 +00003381 case TargetLowering::Legal:
3382 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
3383 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
3384 break;
Nate Begeman419f8b62005-10-18 00:27:41 +00003385 case TargetLowering::Expand:
3386 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
3387 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
3388 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003389 DAG.getConstant(PairTy.getSizeInBits()/2,
Nate Begeman419f8b62005-10-18 00:27:41 +00003390 TLI.getShiftAmountTy()));
Chris Lattner456a93a2006-01-28 07:39:30 +00003391 Result = DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2);
Nate Begeman419f8b62005-10-18 00:27:41 +00003392 break;
3393 }
3394 break;
3395 }
3396
Nate Begemanc105e192005-04-06 00:23:54 +00003397 case ISD::UREM:
3398 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00003399 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00003400 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3401 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner456a93a2006-01-28 07:39:30 +00003402
Nate Begemanc105e192005-04-06 00:23:54 +00003403 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003404 case TargetLowering::Promote: assert(0 && "Cannot promote this yet!");
3405 case TargetLowering::Custom:
3406 isCustom = true;
3407 // FALLTHROUGH
Nate Begemanc105e192005-04-06 00:23:54 +00003408 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003409 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Chris Lattner456a93a2006-01-28 07:39:30 +00003410 if (isCustom) {
3411 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003412 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003413 }
Nate Begemanc105e192005-04-06 00:23:54 +00003414 break;
Dan Gohman525178c2007-10-08 18:33:35 +00003415 case TargetLowering::Expand: {
Evan Cheng6b5578f2006-09-18 23:28:33 +00003416 unsigned DivOpc= (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
Reid Spencer47857812006-12-31 05:55:36 +00003417 bool isSigned = DivOpc == ISD::SDIV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003418 MVT VT = Node->getValueType(0);
Dan Gohman525178c2007-10-08 18:33:35 +00003419
3420 // See if remainder can be lowered using two-result operations.
3421 SDVTList VTs = DAG.getVTList(VT, VT);
3422 if (Node->getOpcode() == ISD::SREM &&
3423 TLI.isOperationLegal(ISD::SDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003424 Result = SDValue(DAG.getNode(ISD::SDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003425 break;
3426 }
3427 if (Node->getOpcode() == ISD::UREM &&
3428 TLI.isOperationLegal(ISD::UDIVREM, VT)) {
Gabor Greifba36cb52008-08-28 21:40:38 +00003429 Result = SDValue(DAG.getNode(ISD::UDIVREM, VTs, Tmp1, Tmp2).getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00003430 break;
3431 }
3432
Duncan Sands83ec4b62008-06-06 12:08:01 +00003433 if (VT.isInteger()) {
Dan Gohman525178c2007-10-08 18:33:35 +00003434 if (TLI.getOperationAction(DivOpc, VT) ==
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003435 TargetLowering::Legal) {
3436 // X % Y -> X-X/Y*Y
Evan Cheng6b5578f2006-09-18 23:28:33 +00003437 Result = DAG.getNode(DivOpc, VT, Tmp1, Tmp2);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003438 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
3439 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003440 } else if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003441 Result = LegalizeOp(UnrollVectorOp(Op));
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003442 } else {
Dan Gohman525178c2007-10-08 18:33:35 +00003443 assert(VT == MVT::i32 &&
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003444 "Cannot expand this binary operator!");
Evan Cheng56966222007-01-12 02:11:51 +00003445 RTLIB::Libcall LC = Node->getOpcode() == ISD::UREM
3446 ? RTLIB::UREM_I32 : RTLIB::SREM_I32;
Dan Gohman475871a2008-07-27 21:46:04 +00003447 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003448 Result = ExpandLibCall(LC, Node, isSigned, Dummy);
Evan Cheng52cc1ea2006-09-18 21:49:04 +00003449 }
Dan Gohman0d974262007-11-06 22:11:54 +00003450 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003451 assert(VT.isFloatingPoint() &&
Dan Gohman0d974262007-11-06 22:11:54 +00003452 "remainder op must have integer or floating-point type");
Duncan Sands83ec4b62008-06-06 12:08:01 +00003453 if (VT.isVector()) {
Dan Gohman80176312007-11-05 23:35:22 +00003454 Result = LegalizeOp(UnrollVectorOp(Op));
3455 } else {
3456 // Floating point mod -> fmod libcall.
Duncan Sands007f9842008-01-10 10:28:30 +00003457 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::REM_F32, RTLIB::REM_F64,
3458 RTLIB::REM_F80, RTLIB::REM_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003460 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Dan Gohman80176312007-11-05 23:35:22 +00003461 }
Nate Begemanc105e192005-04-06 00:23:54 +00003462 }
3463 break;
3464 }
Dan Gohman525178c2007-10-08 18:33:35 +00003465 }
Nate Begemanc105e192005-04-06 00:23:54 +00003466 break;
Nate Begemanacc398c2006-01-25 18:21:52 +00003467 case ISD::VAARG: {
3468 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3469 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3470
Duncan Sands83ec4b62008-06-06 12:08:01 +00003471 MVT VT = Node->getValueType(0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003472 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
3473 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003474 case TargetLowering::Custom:
3475 isCustom = true;
3476 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003477 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003478 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3479 Result = Result.getValue(0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003480 Tmp1 = Result.getValue(1);
3481
3482 if (isCustom) {
3483 Tmp2 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003484 if (Tmp2.getNode()) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003485 Result = LegalizeOp(Tmp2);
3486 Tmp1 = LegalizeOp(Tmp2.getValue(1));
3487 }
3488 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003489 break;
3490 case TargetLowering::Expand: {
Dan Gohman69de1932008-02-06 22:27:42 +00003491 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003492 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003493 // Increment the pointer, VAList, to the next vaarg
Duncan Sands5c58a312008-11-03 11:51:11 +00003494 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
3495 DAG.getConstant(TLI.getTargetData()->getABITypeSize(VT.getTypeForMVT()),
3496 TLI.getPointerTy()));
Nate Begemanacc398c2006-01-25 18:21:52 +00003497 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00003498 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003499 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00003500 Result = DAG.getLoad(VT, Tmp3, VAList, NULL, 0);
Chris Lattner456a93a2006-01-28 07:39:30 +00003501 Tmp1 = LegalizeOp(Result.getValue(1));
Nate Begemanacc398c2006-01-25 18:21:52 +00003502 Result = LegalizeOp(Result);
3503 break;
3504 }
3505 }
3506 // Since VAARG produces two values, make sure to remember that we
3507 // legalized both of them.
Dan Gohman475871a2008-07-27 21:46:04 +00003508 AddLegalizedOperand(SDValue(Node, 0), Result);
3509 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00003510 return Op.getResNo() ? Tmp1 : Result;
Nate Begemanacc398c2006-01-25 18:21:52 +00003511 }
3512
3513 case ISD::VACOPY:
3514 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3515 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the dest pointer.
3516 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the source pointer.
3517
3518 switch (TLI.getOperationAction(ISD::VACOPY, MVT::Other)) {
3519 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003520 case TargetLowering::Custom:
3521 isCustom = true;
3522 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003523 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003524 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Tmp3,
3525 Node->getOperand(3), Node->getOperand(4));
Chris Lattner456a93a2006-01-28 07:39:30 +00003526 if (isCustom) {
3527 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003528 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003529 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003530 break;
3531 case TargetLowering::Expand:
3532 // This defaults to loading a pointer from the input and storing it to the
3533 // output, returning the chain.
Dan Gohman69de1932008-02-06 22:27:42 +00003534 const Value *VD = cast<SrcValueSDNode>(Node->getOperand(3))->getValue();
3535 const Value *VS = cast<SrcValueSDNode>(Node->getOperand(4))->getValue();
Dan Gohman499c1bd2008-04-17 02:09:26 +00003536 Tmp4 = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp3, VS, 0);
3537 Result = DAG.getStore(Tmp4.getValue(1), Tmp4, Tmp2, VD, 0);
Nate Begemanacc398c2006-01-25 18:21:52 +00003538 break;
3539 }
3540 break;
3541
3542 case ISD::VAEND:
3543 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3544 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3545
3546 switch (TLI.getOperationAction(ISD::VAEND, MVT::Other)) {
3547 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003548 case TargetLowering::Custom:
3549 isCustom = true;
3550 // FALLTHROUGH
Nate Begemanacc398c2006-01-25 18:21:52 +00003551 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003552 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
Chris Lattner456a93a2006-01-28 07:39:30 +00003553 if (isCustom) {
3554 Tmp1 = TLI.LowerOperation(Tmp1, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003555 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003556 }
Nate Begemanacc398c2006-01-25 18:21:52 +00003557 break;
3558 case TargetLowering::Expand:
3559 Result = Tmp1; // Default to a no-op, return the chain
3560 break;
3561 }
3562 break;
3563
3564 case ISD::VASTART:
3565 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3566 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
3567
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003568 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2, Node->getOperand(2));
3569
Nate Begemanacc398c2006-01-25 18:21:52 +00003570 switch (TLI.getOperationAction(ISD::VASTART, MVT::Other)) {
3571 default: assert(0 && "This action is not supported yet!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003572 case TargetLowering::Legal: break;
3573 case TargetLowering::Custom:
3574 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003575 if (Tmp1.getNode()) Result = Tmp1;
Nate Begemanacc398c2006-01-25 18:21:52 +00003576 break;
3577 }
3578 break;
3579
Nate Begeman35ef9132006-01-11 21:21:00 +00003580 case ISD::ROTL:
3581 case ISD::ROTR:
3582 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
3583 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003584 Result = DAG.UpdateNodeOperands(Result, Tmp1, Tmp2);
Scott Michelc9dc1142007-04-02 21:36:32 +00003585 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3586 default:
3587 assert(0 && "ROTL/ROTR legalize operation not supported");
3588 break;
3589 case TargetLowering::Legal:
3590 break;
3591 case TargetLowering::Custom:
3592 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003593 if (Tmp1.getNode()) Result = Tmp1;
Scott Michelc9dc1142007-04-02 21:36:32 +00003594 break;
3595 case TargetLowering::Promote:
3596 assert(0 && "Do not know how to promote ROTL/ROTR");
3597 break;
3598 case TargetLowering::Expand:
3599 assert(0 && "Do not know how to expand ROTL/ROTR");
3600 break;
3601 }
Nate Begeman35ef9132006-01-11 21:21:00 +00003602 break;
3603
Nate Begemand88fc032006-01-14 03:14:10 +00003604 case ISD::BSWAP:
3605 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3606 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003607 case TargetLowering::Custom:
3608 assert(0 && "Cannot custom legalize this yet!");
3609 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003610 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003611 break;
3612 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003613 MVT OVT = Tmp1.getValueType();
3614 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
3615 unsigned DiffBits = NVT.getSizeInBits() - OVT.getSizeInBits();
Nate Begemand88fc032006-01-14 03:14:10 +00003616
Chris Lattner456a93a2006-01-28 07:39:30 +00003617 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3618 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
3619 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
3620 DAG.getConstant(DiffBits, TLI.getShiftAmountTy()));
3621 break;
3622 }
3623 case TargetLowering::Expand:
3624 Result = ExpandBSWAP(Tmp1);
3625 break;
Nate Begemand88fc032006-01-14 03:14:10 +00003626 }
3627 break;
3628
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003629 case ISD::CTPOP:
3630 case ISD::CTTZ:
3631 case ISD::CTLZ:
3632 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
3633 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Scott Michel910b66d2007-07-30 21:00:31 +00003634 case TargetLowering::Custom:
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003635 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003636 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel910b66d2007-07-30 21:00:31 +00003637 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
Scott Michel335f4f72007-08-02 02:22:46 +00003638 TargetLowering::Custom) {
3639 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003640 if (Tmp1.getNode()) {
Scott Michel335f4f72007-08-02 02:22:46 +00003641 Result = Tmp1;
3642 }
Scott Michel910b66d2007-07-30 21:00:31 +00003643 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003644 break;
3645 case TargetLowering::Promote: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003646 MVT OVT = Tmp1.getValueType();
3647 MVT NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00003648
3649 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003650 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
3651 // Perform the larger operation, then subtract if needed.
3652 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003653 switch (Node->getOpcode()) {
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003654 case ISD::CTPOP:
3655 Result = Tmp1;
3656 break;
3657 case ISD::CTTZ:
3658 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00003659 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003660 DAG.getConstant(NVT.getSizeInBits(), NVT),
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003661 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00003662 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003663 DAG.getConstant(OVT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003664 break;
3665 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00003666 // Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00003667 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003668 DAG.getConstant(NVT.getSizeInBits() -
3669 OVT.getSizeInBits(), NVT));
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003670 break;
3671 }
3672 break;
3673 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003674 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003675 Result = ExpandBitCount(Node->getOpcode(), Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00003676 break;
3677 }
3678 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00003679
Chris Lattner2c8086f2005-04-02 05:00:07 +00003680 // Unary operators
3681 case ISD::FABS:
3682 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00003683 case ISD::FSQRT:
3684 case ISD::FSIN:
3685 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003686 case ISD::FLOG:
3687 case ISD::FLOG2:
3688 case ISD::FLOG10:
3689 case ISD::FEXP:
3690 case ISD::FEXP2:
Dan Gohman509e84f2008-08-21 17:55:02 +00003691 case ISD::FTRUNC:
3692 case ISD::FFLOOR:
3693 case ISD::FCEIL:
3694 case ISD::FRINT:
3695 case ISD::FNEARBYINT:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003696 Tmp1 = LegalizeOp(Node->getOperand(0));
3697 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
Chris Lattner456a93a2006-01-28 07:39:30 +00003698 case TargetLowering::Promote:
3699 case TargetLowering::Custom:
Evan Cheng59ad7812006-01-31 18:14:25 +00003700 isCustom = true;
3701 // FALLTHROUGH
Chris Lattner2c8086f2005-04-02 05:00:07 +00003702 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003703 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Evan Cheng59ad7812006-01-31 18:14:25 +00003704 if (isCustom) {
3705 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003706 if (Tmp1.getNode()) Result = Tmp1;
Evan Cheng59ad7812006-01-31 18:14:25 +00003707 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003708 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00003709 case TargetLowering::Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00003710 switch (Node->getOpcode()) {
3711 default: assert(0 && "Unreachable!");
3712 case ISD::FNEG:
Chris Lattner2c8086f2005-04-02 05:00:07 +00003713 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
3714 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00003715 Result = DAG.getNode(ISD::FSUB, Node->getValueType(0), Tmp2, Tmp1);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003716 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003717 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003718 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
Duncan Sands83ec4b62008-06-06 12:08:01 +00003719 MVT VT = Node->getValueType(0);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003720 Tmp2 = DAG.getConstantFP(0.0, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003721 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00003722 ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00003723 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
3724 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003725 break;
3726 }
Evan Cheng9d24ac52008-09-09 23:35:53 +00003727 case ISD::FSQRT:
3728 case ISD::FSIN:
3729 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003730 case ISD::FLOG:
3731 case ISD::FLOG2:
3732 case ISD::FLOG10:
3733 case ISD::FEXP:
3734 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003735 case ISD::FTRUNC:
3736 case ISD::FFLOOR:
3737 case ISD::FCEIL:
3738 case ISD::FRINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00003739 case ISD::FNEARBYINT: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003740 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003741
3742 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003743 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003744 Result = LegalizeOp(UnrollVectorOp(Op));
3745 break;
3746 }
3747
Evan Cheng56966222007-01-12 02:11:51 +00003748 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003749 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00003750 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00003751 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
3752 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003753 break;
3754 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00003755 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
3756 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003757 break;
3758 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00003759 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
3760 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00003761 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003762 case ISD::FLOG:
3763 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
3764 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
3765 break;
3766 case ISD::FLOG2:
3767 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
3768 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
3769 break;
3770 case ISD::FLOG10:
3771 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
3772 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
3773 break;
3774 case ISD::FEXP:
3775 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
3776 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
3777 break;
3778 case ISD::FEXP2:
3779 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
3780 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
3781 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00003782 case ISD::FTRUNC:
3783 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
3784 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
3785 break;
3786 case ISD::FFLOOR:
3787 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
3788 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
3789 break;
3790 case ISD::FCEIL:
3791 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
3792 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
3793 break;
3794 case ISD::FRINT:
3795 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
3796 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
3797 break;
3798 case ISD::FNEARBYINT:
3799 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
3800 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
3801 break;
Evan Cheng9d24ac52008-09-09 23:35:53 +00003802 break;
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003803 default: assert(0 && "Unreachable!");
3804 }
Dan Gohman475871a2008-07-27 21:46:04 +00003805 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003806 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00003807 break;
3808 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003809 }
3810 break;
3811 }
3812 break;
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003813 case ISD::FPOWI: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003814 MVT VT = Node->getValueType(0);
Dan Gohman82669522007-10-11 23:57:53 +00003815
3816 // Expand unsupported unary vector operators by unrolling them.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003817 if (VT.isVector()) {
Dan Gohman82669522007-10-11 23:57:53 +00003818 Result = LegalizeOp(UnrollVectorOp(Op));
3819 break;
3820 }
3821
3822 // We always lower FPOWI into a libcall. No target support for it yet.
Duncan Sands007f9842008-01-10 10:28:30 +00003823 RTLIB::Libcall LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64,
3824 RTLIB::POWI_F80, RTLIB::POWI_PPCF128);
Dan Gohman475871a2008-07-27 21:46:04 +00003825 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00003826 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Chris Lattner6ddf8ed2006-09-09 06:03:30 +00003827 break;
3828 }
Chris Lattner35481892005-12-23 00:16:34 +00003829 case ISD::BIT_CONVERT:
Chris Lattner67993f72006-01-23 07:30:46 +00003830 if (!isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner1401d152008-01-16 07:45:30 +00003831 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3832 Node->getValueType(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00003833 } else if (Op.getOperand(0).getValueType().isVector()) {
Dan Gohman7f321562007-06-25 16:23:39 +00003834 // The input has to be a vector type, we have to either scalarize it, pack
3835 // it, or convert it based on whether the input vector type is legal.
Gabor Greifba36cb52008-08-28 21:40:38 +00003836 SDNode *InVal = Node->getOperand(0).getNode();
Gabor Greif99a6cb92008-08-26 22:36:50 +00003837 int InIx = Node->getOperand(0).getResNo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003838 unsigned NumElems = InVal->getValueType(InIx).getVectorNumElements();
3839 MVT EVT = InVal->getValueType(InIx).getVectorElementType();
Dan Gohman7f321562007-06-25 16:23:39 +00003840
3841 // Figure out if there is a simple type corresponding to this Vector
3842 // type. If so, convert to the vector type.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003843 MVT TVT = MVT::getVectorVT(EVT, NumElems);
Dan Gohman7f321562007-06-25 16:23:39 +00003844 if (TLI.isTypeLegal(TVT)) {
Dan Gohman07a96762007-07-16 14:29:03 +00003845 // Turn this into a bit convert of the vector input.
Dan Gohman7f321562007-06-25 16:23:39 +00003846 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3847 LegalizeOp(Node->getOperand(0)));
3848 break;
3849 } else if (NumElems == 1) {
3850 // Turn this into a bit convert of the scalar input.
3851 Result = DAG.getNode(ISD::BIT_CONVERT, Node->getValueType(0),
3852 ScalarizeVectorOp(Node->getOperand(0)));
3853 break;
3854 } else {
3855 // FIXME: UNIMP! Store then reload
3856 assert(0 && "Cast from unsupported vector type not implemented yet!");
3857 }
Chris Lattner67993f72006-01-23 07:30:46 +00003858 } else {
Chris Lattner35481892005-12-23 00:16:34 +00003859 switch (TLI.getOperationAction(ISD::BIT_CONVERT,
3860 Node->getOperand(0).getValueType())) {
3861 default: assert(0 && "Unknown operation action!");
3862 case TargetLowering::Expand:
Chris Lattner1401d152008-01-16 07:45:30 +00003863 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
3864 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00003865 break;
3866 case TargetLowering::Legal:
3867 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003868 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner35481892005-12-23 00:16:34 +00003869 break;
3870 }
3871 }
3872 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00003873 case ISD::CONVERT_RNDSAT: {
3874 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
3875 switch (CvtCode) {
3876 default: assert(0 && "Unknown cvt code!");
3877 case ISD::CVT_SF:
3878 case ISD::CVT_UF:
3879 break;
3880 case ISD::CVT_FF:
3881 case ISD::CVT_FS:
3882 case ISD::CVT_FU:
3883 case ISD::CVT_SS:
3884 case ISD::CVT_SU:
3885 case ISD::CVT_US:
3886 case ISD::CVT_UU: {
3887 SDValue DTyOp = Node->getOperand(1);
3888 SDValue STyOp = Node->getOperand(2);
3889 SDValue RndOp = Node->getOperand(3);
3890 SDValue SatOp = Node->getOperand(4);
3891 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3892 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
3893 case Legal:
3894 Tmp1 = LegalizeOp(Node->getOperand(0));
3895 Result = DAG.UpdateNodeOperands(Result, Tmp1, DTyOp, STyOp,
3896 RndOp, SatOp);
3897 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
3898 TargetLowering::Custom) {
3899 Tmp1 = TLI.LowerOperation(Result, DAG);
3900 if (Tmp1.getNode()) Result = Tmp1;
3901 }
3902 break;
3903 case Promote:
3904 Result = PromoteOp(Node->getOperand(0));
3905 // For FP, make Op1 a i32
3906
3907 Result = DAG.getConvertRndSat(Result.getValueType(), Result,
3908 DTyOp, STyOp, RndOp, SatOp, CvtCode);
3909 break;
3910 }
3911 break;
3912 }
3913 } // end switch CvtCode
3914 break;
3915 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00003916 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00003917 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003918 case ISD::UINT_TO_FP: {
3919 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Dan Gohman7f8613e2008-08-14 20:04:46 +00003920 Result = LegalizeINT_TO_FP(Result, isSigned,
3921 Node->getValueType(0), Node->getOperand(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003922 break;
3923 }
3924 case ISD::TRUNCATE:
3925 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3926 case Legal:
3927 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel546d7b52008-12-02 19:55:08 +00003928 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
3929 default: assert(0 && "Unknown TRUNCATE legalization operation action!");
3930 case TargetLowering::Custom:
3931 isCustom = true;
3932 // FALLTHROUGH
3933 case TargetLowering::Legal:
3934 Result = DAG.UpdateNodeOperands(Result, Tmp1);
3935 if (isCustom) {
3936 Tmp1 = TLI.LowerOperation(Result, DAG);
3937 if (Tmp1.getNode()) Result = Tmp1;
3938 }
3939 break;
Tilmann Schellerb0a5cdd2008-12-02 12:12:25 +00003940 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003941 break;
3942 case Expand:
3943 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
3944
3945 // Since the result is legal, we should just be able to truncate the low
3946 // part of the source.
3947 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
3948 break;
3949 case Promote:
3950 Result = PromoteOp(Node->getOperand(0));
3951 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
3952 break;
3953 }
3954 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003955
Chris Lattnerfa9c8012005-07-28 23:31:12 +00003956 case ISD::FP_TO_SINT:
3957 case ISD::FP_TO_UINT:
3958 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3959 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00003960 Tmp1 = LegalizeOp(Node->getOperand(0));
3961
Chris Lattner1618beb2005-07-29 00:11:56 +00003962 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
3963 default: assert(0 && "Unknown operation action!");
Chris Lattner456a93a2006-01-28 07:39:30 +00003964 case TargetLowering::Custom:
3965 isCustom = true;
3966 // FALLTHROUGH
3967 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00003968 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00003969 if (isCustom) {
3970 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003971 if (Tmp1.getNode()) Result = Tmp1;
Chris Lattner456a93a2006-01-28 07:39:30 +00003972 }
3973 break;
3974 case TargetLowering::Promote:
3975 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
3976 Node->getOpcode() == ISD::FP_TO_SINT);
3977 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00003978 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00003979 if (Node->getOpcode() == ISD::FP_TO_UINT) {
Dan Gohman475871a2008-07-27 21:46:04 +00003980 SDValue True, False;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003981 MVT VT = Node->getOperand(0).getValueType();
3982 MVT NVT = Node->getValueType(0);
Dale Johannesen73328d12007-09-19 23:55:34 +00003983 const uint64_t zero[] = {0, 0};
Duncan Sands83ec4b62008-06-06 12:08:01 +00003984 APFloat apf = APFloat(APInt(VT.getSizeInBits(), 2, zero));
3985 APInt x = APInt::getSignBit(NVT.getSizeInBits());
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003986 (void)apf.convertFromAPInt(x, false, APFloat::rmNearestTiesToEven);
Dale Johannesen73328d12007-09-19 23:55:34 +00003987 Tmp2 = DAG.getConstantFP(apf, VT);
Scott Michel5b8f82e2008-03-10 15:42:14 +00003988 Tmp3 = DAG.getSetCC(TLI.getSetCCResultType(Node->getOperand(0)),
Nate Begemand2558e32005-08-14 01:20:53 +00003989 Node->getOperand(0), Tmp2, ISD::SETLT);
3990 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
3991 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00003992 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00003993 Tmp2));
3994 False = DAG.getNode(ISD::XOR, NVT, False,
Dan Gohmanc7773bf2008-02-29 01:44:25 +00003995 DAG.getConstant(x, NVT));
Chris Lattner456a93a2006-01-28 07:39:30 +00003996 Result = DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False);
3997 break;
Nate Begemand2558e32005-08-14 01:20:53 +00003998 } else {
3999 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
4000 }
4001 break;
Chris Lattner07dffd62005-08-26 00:14:16 +00004002 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004003 break;
Evan Cheng6af00d52006-12-13 01:57:55 +00004004 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004005 MVT VT = Op.getValueType();
4006 MVT OVT = Node->getOperand(0).getValueType();
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004007 // Convert ppcf128 to i32
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004008 if (OVT == MVT::ppcf128 && VT == MVT::i32) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004009 if (Node->getOpcode() == ISD::FP_TO_SINT) {
4010 Result = DAG.getNode(ISD::FP_ROUND_INREG, MVT::ppcf128,
4011 Node->getOperand(0), DAG.getValueType(MVT::f64));
4012 Result = DAG.getNode(ISD::FP_ROUND, MVT::f64, Result,
4013 DAG.getIntPtrConstant(1));
4014 Result = DAG.getNode(ISD::FP_TO_SINT, VT, Result);
4015 } else {
Dale Johannesenfcf4d242007-10-11 23:32:15 +00004016 const uint64_t TwoE31[] = {0x41e0000000000000LL, 0};
4017 APFloat apf = APFloat(APInt(128, 2, TwoE31));
4018 Tmp2 = DAG.getConstantFP(apf, OVT);
4019 // X>=2^31 ? (int)(X-2^31)+0x80000000 : (int)X
4020 // FIXME: generated code sucks.
4021 Result = DAG.getNode(ISD::SELECT_CC, VT, Node->getOperand(0), Tmp2,
4022 DAG.getNode(ISD::ADD, MVT::i32,
4023 DAG.getNode(ISD::FP_TO_SINT, VT,
4024 DAG.getNode(ISD::FSUB, OVT,
4025 Node->getOperand(0), Tmp2)),
4026 DAG.getConstant(0x80000000, MVT::i32)),
4027 DAG.getNode(ISD::FP_TO_SINT, VT,
4028 Node->getOperand(0)),
4029 DAG.getCondCode(ISD::SETGE));
4030 }
Dale Johannesen6eaeff22007-10-10 01:01:31 +00004031 break;
4032 }
Dan Gohmana2e94852008-03-10 23:03:31 +00004033 // Convert f32 / f64 to i32 / i64 / i128.
Duncan Sandsb2ff8852008-07-17 02:36:29 +00004034 RTLIB::Libcall LC = (Node->getOpcode() == ISD::FP_TO_SINT) ?
4035 RTLIB::getFPTOSINT(OVT, VT) : RTLIB::getFPTOUINT(OVT, VT);
4036 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpectd fp-to-int conversion!");
Dan Gohman475871a2008-07-27 21:46:04 +00004037 SDValue Dummy;
Duncan Sands460a14e2008-04-12 17:14:18 +00004038 Result = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Dummy);
Evan Cheng6af00d52006-12-13 01:57:55 +00004039 break;
4040 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004041 case Promote:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004042 Tmp1 = PromoteOp(Node->getOperand(0));
4043 Result = DAG.UpdateNodeOperands(Result, LegalizeOp(Tmp1));
4044 Result = LegalizeOp(Result);
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004045 break;
4046 }
4047 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00004048
Chris Lattnerf2670a82008-01-16 06:57:07 +00004049 case ISD::FP_EXTEND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004050 MVT DstVT = Op.getValueType();
4051 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004052 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4053 // The only other way we can lower this is to turn it into a STORE,
4054 // LOAD pair, targetting a temporary location (a stack slot).
4055 Result = EmitStackConvert(Node->getOperand(0), SrcVT, DstVT);
4056 break;
Chris Lattnerf2670a82008-01-16 06:57:07 +00004057 }
4058 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4059 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4060 case Legal:
4061 Tmp1 = LegalizeOp(Node->getOperand(0));
4062 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4063 break;
4064 case Promote:
4065 Tmp1 = PromoteOp(Node->getOperand(0));
4066 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Tmp1);
4067 break;
4068 }
4069 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004070 }
Dale Johannesen5411a392007-08-09 01:04:01 +00004071 case ISD::FP_ROUND: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004072 MVT DstVT = Op.getValueType();
4073 MVT SrcVT = Op.getOperand(0).getValueType();
Chris Lattner0bd48932008-01-17 07:00:52 +00004074 if (TLI.getConvertAction(SrcVT, DstVT) == TargetLowering::Expand) {
4075 if (SrcVT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00004076 SDValue Lo;
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004077 ExpandOp(Node->getOperand(0), Lo, Result);
Chris Lattner0bd48932008-01-17 07:00:52 +00004078 // Round it the rest of the way (e.g. to f32) if needed.
Dale Johannesen713ed3f2008-01-20 01:18:38 +00004079 if (DstVT!=MVT::f64)
4080 Result = DAG.getNode(ISD::FP_ROUND, DstVT, Result, Op.getOperand(1));
Chris Lattner0bd48932008-01-17 07:00:52 +00004081 break;
Dale Johannesen5411a392007-08-09 01:04:01 +00004082 }
Chris Lattner0bd48932008-01-17 07:00:52 +00004083 // The only other way we can lower this is to turn it into a STORE,
4084 // LOAD pair, targetting a temporary location (a stack slot).
4085 Result = EmitStackConvert(Node->getOperand(0), DstVT, DstVT);
4086 break;
Dale Johannesen849f2142007-07-03 00:53:03 +00004087 }
Chris Lattnerf2670a82008-01-16 06:57:07 +00004088 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4089 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
4090 case Legal:
4091 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004092 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004093 break;
4094 case Promote:
4095 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0bd48932008-01-17 07:00:52 +00004096 Result = DAG.getNode(ISD::FP_ROUND, Op.getValueType(), Tmp1,
4097 Node->getOperand(1));
Chris Lattnerf2670a82008-01-16 06:57:07 +00004098 break;
4099 }
4100 break;
Chris Lattner0bd48932008-01-17 07:00:52 +00004101 }
Chris Lattner13c78e22005-09-02 00:18:10 +00004102 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004103 case ISD::ZERO_EXTEND:
4104 case ISD::SIGN_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004105 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner456a93a2006-01-28 07:39:30 +00004106 case Expand: assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004107 case Legal:
4108 Tmp1 = LegalizeOp(Node->getOperand(0));
Scott Michel82747a52008-04-30 00:26:38 +00004109 Result = DAG.UpdateNodeOperands(Result, Tmp1);
Scott Michel0123b7d2008-02-15 23:05:48 +00004110 if (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0)) ==
4111 TargetLowering::Custom) {
Scott Michel82747a52008-04-30 00:26:38 +00004112 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004113 if (Tmp1.getNode()) Result = Tmp1;
Scott Michel0123b7d2008-02-15 23:05:48 +00004114 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00004115 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004116 case Promote:
4117 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00004118 case ISD::ANY_EXTEND:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004119 Tmp1 = PromoteOp(Node->getOperand(0));
4120 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Tmp1);
Chris Lattner13c78e22005-09-02 00:18:10 +00004121 break;
Chris Lattner1713e732005-01-16 00:38:00 +00004122 case ISD::ZERO_EXTEND:
4123 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004124 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00004125 Result = DAG.getZeroExtendInReg(Result,
4126 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00004127 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004128 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00004129 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00004130 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00004131 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004132 Result,
4133 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00004134 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004135 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00004136 }
4137 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00004138 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00004139 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00004140 Tmp1 = LegalizeOp(Node->getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004141 MVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00004142
4143 // If this operation is not supported, convert it to a shl/shr or load/store
4144 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004145 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
4146 default: assert(0 && "This action not supported for this op yet!");
4147 case TargetLowering::Legal:
Chris Lattnerc52ad4f2006-01-28 10:58:55 +00004148 Result = DAG.UpdateNodeOperands(Result, Tmp1, Node->getOperand(1));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004149 break;
4150 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00004151 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00004152 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00004153 // NOTE: we could fall back on load/store here too for targets without
4154 // SAR. However, it is doubtful that any exist.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004155 unsigned BitsDiff = Node->getValueType(0).getSizeInBits() -
4156 ExtraVT.getSizeInBits();
Dan Gohman475871a2008-07-27 21:46:04 +00004157 SDValue ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00004158 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
4159 Node->getOperand(0), ShiftCst);
4160 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
4161 Result, ShiftCst);
4162 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
Jim Laskey2d84c4c2006-10-11 17:52:19 +00004163 // The only way we can lower this is to turn it into a TRUNCSTORE,
Chris Lattner45b8caf2005-01-15 07:15:18 +00004164 // EXTLOAD pair, targetting a temporary location (a stack slot).
4165
4166 // NOTE: there is a choice here between constantly creating new stack
4167 // slots and always reusing the same one. We currently always create
4168 // new ones, as reuse may inhibit scheduling.
Chris Lattnera66bb392008-01-16 07:51:34 +00004169 Result = EmitStackConvert(Node->getOperand(0), ExtraVT,
4170 Node->getValueType(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00004171 } else {
4172 assert(0 && "Unknown op");
4173 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00004174 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00004175 }
Chris Lattner0f69b292005-01-15 06:18:18 +00004176 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00004177 }
Duncan Sands36397f52007-07-27 12:58:54 +00004178 case ISD::TRAMPOLINE: {
Dan Gohman475871a2008-07-27 21:46:04 +00004179 SDValue Ops[6];
Duncan Sands36397f52007-07-27 12:58:54 +00004180 for (unsigned i = 0; i != 6; ++i)
4181 Ops[i] = LegalizeOp(Node->getOperand(i));
4182 Result = DAG.UpdateNodeOperands(Result, Ops, 6);
4183 // The only option for this node is to custom lower it.
4184 Result = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004185 assert(Result.getNode() && "Should always custom lower!");
Duncan Sandsf7331b32007-09-11 14:10:23 +00004186
4187 // Since trampoline produces two values, make sure to remember that we
4188 // legalized both of them.
4189 Tmp1 = LegalizeOp(Result.getValue(1));
4190 Result = LegalizeOp(Result);
Dan Gohman475871a2008-07-27 21:46:04 +00004191 AddLegalizedOperand(SDValue(Node, 0), Result);
4192 AddLegalizedOperand(SDValue(Node, 1), Tmp1);
Gabor Greif99a6cb92008-08-26 22:36:50 +00004193 return Op.getResNo() ? Tmp1 : Result;
Duncan Sands36397f52007-07-27 12:58:54 +00004194 }
Dan Gohman9c78a392008-05-14 00:43:10 +00004195 case ISD::FLT_ROUNDS_: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004196 MVT VT = Node->getValueType(0);
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004197 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4198 default: assert(0 && "This action not supported for this op yet!");
4199 case TargetLowering::Custom:
4200 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004201 if (Result.getNode()) break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004202 // Fall Thru
4203 case TargetLowering::Legal:
4204 // If this operation is not supported, lower it to constant 1
4205 Result = DAG.getConstant(1, VT);
4206 break;
4207 }
Dan Gohman9ab9ee82008-05-12 16:07:15 +00004208 break;
Anton Korobeynikov917c2a62007-11-15 23:25:33 +00004209 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004210 case ISD::TRAP: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004211 MVT VT = Node->getValueType(0);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004212 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4213 default: assert(0 && "This action not supported for this op yet!");
Chris Lattner41bab0b2008-01-15 21:58:08 +00004214 case TargetLowering::Legal:
4215 Tmp1 = LegalizeOp(Node->getOperand(0));
4216 Result = DAG.UpdateNodeOperands(Result, Tmp1);
4217 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004218 case TargetLowering::Custom:
4219 Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004220 if (Result.getNode()) break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004221 // Fall Thru
Chris Lattner41bab0b2008-01-15 21:58:08 +00004222 case TargetLowering::Expand:
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004223 // If this operation is not supported, lower it to 'abort()' call
Chris Lattner41bab0b2008-01-15 21:58:08 +00004224 Tmp1 = LegalizeOp(Node->getOperand(0));
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004225 TargetLowering::ArgListTy Args;
Dan Gohman475871a2008-07-27 21:46:04 +00004226 std::pair<SDValue,SDValue> CallResult =
Duncan Sands00fee652008-02-14 17:28:50 +00004227 TLI.LowerCallTo(Tmp1, Type::VoidTy,
Dale Johannesen86098bd2008-09-26 19:31:26 +00004228 false, false, false, false, CallingConv::C, false,
Bill Wendling056292f2008-09-16 21:48:12 +00004229 DAG.getExternalSymbol("abort", TLI.getPointerTy()),
Chris Lattner034f12e2008-01-15 22:09:33 +00004230 Args, DAG);
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004231 Result = CallResult.second;
4232 break;
4233 }
Chris Lattner41bab0b2008-01-15 21:58:08 +00004234 break;
Anton Korobeynikov66fac792008-01-15 07:02:33 +00004235 }
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004236
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004237 case ISD::SADDO: {
4238 MVT VT = Node->getValueType(0);
4239 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4240 default: assert(0 && "This action not supported for this op yet!");
4241 case TargetLowering::Custom:
4242 Result = TLI.LowerOperation(Op, DAG);
4243 if (Result.getNode()) break;
4244 // FALLTHROUGH
4245 case TargetLowering::Legal: {
4246 SDValue LHS = LegalizeOp(Node->getOperand(0));
4247 SDValue RHS = LegalizeOp(Node->getOperand(1));
4248
4249 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004250 MVT OType = Node->getValueType(1);
4251
Bill Wendlinga6af91a2008-11-25 08:19:22 +00004252 SDValue Zero = DAG.getConstant(0, LHS.getValueType());
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004253
Bill Wendling740464e2008-11-25 19:40:17 +00004254 // LHSSign -> LHS >= 0
4255 // RHSSign -> RHS >= 0
4256 // SumSign -> Sum >= 0
4257 //
4258 // Overflow -> (LHSSign == RHSSign) && (LHSSign != SumSign)
4259 //
4260 SDValue LHSSign = DAG.getSetCC(OType, LHS, Zero, ISD::SETGE);
4261 SDValue RHSSign = DAG.getSetCC(OType, RHS, Zero, ISD::SETGE);
4262 SDValue SignsEq = DAG.getSetCC(OType, LHSSign, RHSSign, ISD::SETEQ);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004263
Bill Wendling740464e2008-11-25 19:40:17 +00004264 SDValue SumSign = DAG.getSetCC(OType, Sum, Zero, ISD::SETGE);
4265 SDValue SumSignNE = DAG.getSetCC(OType, LHSSign, SumSign, ISD::SETNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004266
Bill Wendling740464e2008-11-25 19:40:17 +00004267 SDValue Cmp = DAG.getNode(ISD::AND, OType, SignsEq, SumSignNE);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004268
4269 MVT ValueVTs[] = { LHS.getValueType(), OType };
4270 SDValue Ops[] = { Sum, Cmp };
4271
Duncan Sandsaaffa052008-12-01 11:41:29 +00004272 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4273 &Ops[0], 2);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004274 SDNode *RNode = Result.getNode();
4275 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4276 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4277 break;
4278 }
4279 }
4280
4281 break;
4282 }
Bill Wendlingdef27392008-11-24 01:38:29 +00004283 case ISD::UADDO: {
Bill Wendling41ea7e72008-11-24 19:21:46 +00004284 MVT VT = Node->getValueType(0);
4285 switch (TLI.getOperationAction(Node->getOpcode(), VT)) {
4286 default: assert(0 && "This action not supported for this op yet!");
4287 case TargetLowering::Custom:
4288 Result = TLI.LowerOperation(Op, DAG);
4289 if (Result.getNode()) break;
4290 // FALLTHROUGH
4291 case TargetLowering::Legal: {
4292 SDValue LHS = LegalizeOp(Node->getOperand(0));
4293 SDValue RHS = LegalizeOp(Node->getOperand(1));
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004294
Bill Wendling41ea7e72008-11-24 19:21:46 +00004295 SDValue Sum = DAG.getNode(ISD::ADD, LHS.getValueType(), LHS, RHS);
4296 MVT OType = Node->getValueType(1);
Bill Wendlingc0062fb2008-11-25 08:12:19 +00004297 SDValue Cmp = DAG.getSetCC(OType, Sum, LHS, ISD::SETULT);
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004298
Bill Wendling41ea7e72008-11-24 19:21:46 +00004299 MVT ValueVTs[] = { LHS.getValueType(), OType };
4300 SDValue Ops[] = { Sum, Cmp };
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004301
Duncan Sandsaaffa052008-12-01 11:41:29 +00004302 Result = DAG.getNode(ISD::MERGE_VALUES, DAG.getVTList(&ValueVTs[0], 2),
4303 &Ops[0], 2);
Bill Wendling41ea7e72008-11-24 19:21:46 +00004304 SDNode *RNode = Result.getNode();
4305 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 0), SDValue(RNode, 0));
4306 DAG.ReplaceAllUsesOfValueWith(SDValue(Node, 1), SDValue(RNode, 1));
4307 break;
4308 }
4309 }
4310
Bill Wendling8ac0d4b2008-11-22 00:22:52 +00004311 break;
4312 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00004313 }
Chris Lattner456a93a2006-01-28 07:39:30 +00004314
Chris Lattner4ddd2832006-04-08 04:13:17 +00004315 assert(Result.getValueType() == Op.getValueType() &&
4316 "Bad legalization!");
4317
Chris Lattner456a93a2006-01-28 07:39:30 +00004318 // Make sure that the generated code is itself legal.
4319 if (Result != Op)
4320 Result = LegalizeOp(Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004321
Chris Lattner45982da2005-05-12 16:53:42 +00004322 // Note that LegalizeOp may be reentered even from single-use nodes, which
4323 // means that we always must cache transformed nodes.
4324 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00004325 return Result;
4326}
4327
Chris Lattner8b6fa222005-01-15 22:16:26 +00004328/// PromoteOp - Given an operation that produces a value in an invalid type,
4329/// promote it to compute the value into a larger type. The produced value will
4330/// have the correct bits for the low portion of the register, but no guarantee
4331/// is made about the top bits: it may be zero, sign-extended, or garbage.
Dan Gohman475871a2008-07-27 21:46:04 +00004332SDValue SelectionDAGLegalize::PromoteOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004333 MVT VT = Op.getValueType();
4334 MVT NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00004335 assert(getTypeAction(VT) == Promote &&
4336 "Caller should expand or legalize operands that are not promotable!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004337 assert(NVT.bitsGT(VT) && NVT.isInteger() == VT.isInteger() &&
Chris Lattner03c85462005-01-15 05:21:40 +00004338 "Cannot promote to smaller type!");
4339
Dan Gohman475871a2008-07-27 21:46:04 +00004340 SDValue Tmp1, Tmp2, Tmp3;
4341 SDValue Result;
Gabor Greifba36cb52008-08-28 21:40:38 +00004342 SDNode *Node = Op.getNode();
Chris Lattner03c85462005-01-15 05:21:40 +00004343
Dan Gohman475871a2008-07-27 21:46:04 +00004344 DenseMap<SDValue, SDValue>::iterator I = PromotedNodes.find(Op);
Chris Lattner6fdcb252005-09-02 20:32:45 +00004345 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00004346
Chris Lattner03c85462005-01-15 05:21:40 +00004347 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00004348 case ISD::CopyFromReg:
4349 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00004350 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004351#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00004352 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00004353#endif
Chris Lattner03c85462005-01-15 05:21:40 +00004354 assert(0 && "Do not know how to promote this operator!");
4355 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00004356 case ISD::UNDEF:
4357 Result = DAG.getNode(ISD::UNDEF, NVT);
4358 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004359 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00004360 if (VT != MVT::i1)
4361 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
4362 else
4363 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00004364 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
4365 break;
4366 case ISD::ConstantFP:
4367 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
4368 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
4369 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00004370
Chris Lattner82fbfb62005-01-18 02:59:52 +00004371 case ISD::SETCC:
Scott Michel5b8f82e2008-03-10 15:42:14 +00004372 assert(isTypeLegal(TLI.getSetCCResultType(Node->getOperand(0)))
Nate Begeman5922f562008-03-14 00:53:31 +00004373 && "SetCC type is not legal??");
Scott Michel5b8f82e2008-03-10 15:42:14 +00004374 Result = DAG.getNode(ISD::SETCC,
Nate Begeman5922f562008-03-14 00:53:31 +00004375 TLI.getSetCCResultType(Node->getOperand(0)),
4376 Node->getOperand(0), Node->getOperand(1),
4377 Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00004378 break;
Chris Lattnerfdfded52006-04-12 16:20:43 +00004379
Chris Lattner03c85462005-01-15 05:21:40 +00004380 case ISD::TRUNCATE:
4381 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4382 case Legal:
4383 Result = LegalizeOp(Node->getOperand(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00004384 assert(Result.getValueType().bitsGE(NVT) &&
Chris Lattner03c85462005-01-15 05:21:40 +00004385 "This truncation doesn't make sense!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00004386 if (Result.getValueType().bitsGT(NVT)) // Truncate to NVT instead of VT
Chris Lattner03c85462005-01-15 05:21:40 +00004387 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
4388 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00004389 case Promote:
4390 // The truncation is not required, because we don't guarantee anything
4391 // about high bits anyway.
4392 Result = PromoteOp(Node->getOperand(0));
4393 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004394 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00004395 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
4396 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00004397 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00004398 }
4399 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004400 case ISD::SIGN_EXTEND:
4401 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00004402 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004403 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4404 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
4405 case Legal:
4406 // Input is legal? Just do extend all the way to the larger type.
Chris Lattner456a93a2006-01-28 07:39:30 +00004407 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004408 break;
4409 case Promote:
4410 // Promote the reg if it's smaller.
4411 Result = PromoteOp(Node->getOperand(0));
4412 // The high bits are not guaranteed to be anything. Insert an extend.
4413 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00004414 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00004415 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00004416 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00004417 Result = DAG.getZeroExtendInReg(Result,
4418 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00004419 break;
4420 }
4421 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00004422 case ISD::CONVERT_RNDSAT: {
4423 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
4424 assert ((CvtCode == ISD::CVT_SS || CvtCode == ISD::CVT_SU ||
4425 CvtCode == ISD::CVT_US || CvtCode == ISD::CVT_UU ||
4426 CvtCode == ISD::CVT_SF || CvtCode == ISD::CVT_UF) &&
4427 "can only promote integers");
4428 Result = DAG.getConvertRndSat(NVT, Node->getOperand(0),
4429 Node->getOperand(1), Node->getOperand(2),
4430 Node->getOperand(3), Node->getOperand(4),
4431 CvtCode);
4432 break;
4433
4434 }
Chris Lattner35481892005-12-23 00:16:34 +00004435 case ISD::BIT_CONVERT:
Chris Lattner1401d152008-01-16 07:45:30 +00004436 Result = EmitStackConvert(Node->getOperand(0), Node->getValueType(0),
4437 Node->getValueType(0));
Chris Lattner35481892005-12-23 00:16:34 +00004438 Result = PromoteOp(Result);
4439 break;
4440
Chris Lattner8b6fa222005-01-15 22:16:26 +00004441 case ISD::FP_EXTEND:
4442 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
4443 case ISD::FP_ROUND:
4444 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4445 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
4446 case Promote: assert(0 && "Unreachable with 2 FP types!");
4447 case Legal:
Chris Lattner0bd48932008-01-17 07:00:52 +00004448 if (Node->getConstantOperandVal(1) == 0) {
4449 // Input is legal? Do an FP_ROUND_INREG.
4450 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Node->getOperand(0),
4451 DAG.getValueType(VT));
4452 } else {
4453 // Just remove the truncate, it isn't affecting the value.
4454 Result = DAG.getNode(ISD::FP_ROUND, NVT, Node->getOperand(0),
4455 Node->getOperand(1));
4456 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004457 break;
4458 }
4459 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004460 case ISD::SINT_TO_FP:
4461 case ISD::UINT_TO_FP:
4462 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4463 case Legal:
Chris Lattner77e77a62005-01-21 06:05:23 +00004464 // No extra round required here.
Chris Lattner456a93a2006-01-28 07:39:30 +00004465 Result = DAG.getNode(Node->getOpcode(), NVT, Node->getOperand(0));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004466 break;
4467
4468 case Promote:
4469 Result = PromoteOp(Node->getOperand(0));
4470 if (Node->getOpcode() == ISD::SINT_TO_FP)
4471 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00004472 Result,
4473 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004474 else
Chris Lattner23993562005-04-13 02:38:47 +00004475 Result = DAG.getZeroExtendInReg(Result,
4476 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00004477 // No extra round required here.
4478 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004479 break;
4480 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00004481 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
4482 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00004483 // Round if we cannot tolerate excess precision.
4484 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004485 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4486 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00004487 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004488 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004489 break;
4490
Chris Lattner5e3c5b42005-12-09 17:32:47 +00004491 case ISD::SIGN_EXTEND_INREG:
4492 Result = PromoteOp(Node->getOperand(0));
4493 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
4494 Node->getOperand(1));
4495 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004496 case ISD::FP_TO_SINT:
4497 case ISD::FP_TO_UINT:
4498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
4499 case Legal:
Evan Cheng0b1b9dc2006-12-16 02:10:30 +00004500 case Expand:
Chris Lattner456a93a2006-01-28 07:39:30 +00004501 Tmp1 = Node->getOperand(0);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004502 break;
4503 case Promote:
4504 // The input result is prerounded, so we don't have to do anything
4505 // special.
4506 Tmp1 = PromoteOp(Node->getOperand(0));
4507 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004508 }
Nate Begemand2558e32005-08-14 01:20:53 +00004509 // If we're promoting a UINT to a larger size, check to see if the new node
4510 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
4511 // we can use that instead. This allows us to generate better code for
4512 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
4513 // legal, such as PowerPC.
4514 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00004515 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00004516 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
4517 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00004518 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
4519 } else {
4520 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4521 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00004522 break;
4523
Chris Lattner2c8086f2005-04-02 05:00:07 +00004524 case ISD::FABS:
4525 case ISD::FNEG:
4526 Tmp1 = PromoteOp(Node->getOperand(0));
4527 assert(Tmp1.getValueType() == NVT);
4528 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
4529 // NOTE: we do not have to do any extra rounding here for
4530 // NoExcessFPPrecision, because we know the input will have the appropriate
4531 // precision, and these operations don't modify precision at all.
4532 break;
4533
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004534 case ISD::FLOG:
4535 case ISD::FLOG2:
4536 case ISD::FLOG10:
4537 case ISD::FEXP:
4538 case ISD::FEXP2:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004539 case ISD::FSQRT:
4540 case ISD::FSIN:
4541 case ISD::FCOS:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00004542 case ISD::FTRUNC:
4543 case ISD::FFLOOR:
4544 case ISD::FCEIL:
4545 case ISD::FRINT:
4546 case ISD::FNEARBYINT:
Chris Lattnerda6ba872005-04-28 21:44:33 +00004547 Tmp1 = PromoteOp(Node->getOperand(0));
4548 assert(Tmp1.getValueType() == NVT);
4549 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004550 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004551 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4552 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00004553 break;
4554
Evan Cheng9d24ac52008-09-09 23:35:53 +00004555 case ISD::FPOW:
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004556 case ISD::FPOWI: {
Evan Cheng9d24ac52008-09-09 23:35:53 +00004557 // Promote f32 pow(i) to f64 pow(i). Note that this could insert a libcall
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004558 // directly as well, which may be better.
4559 Tmp1 = PromoteOp(Node->getOperand(0));
Evan Cheng9d24ac52008-09-09 23:35:53 +00004560 Tmp2 = Node->getOperand(1);
4561 if (Node->getOpcode() == ISD::FPOW)
4562 Tmp2 = PromoteOp(Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004563 assert(Tmp1.getValueType() == NVT);
Evan Cheng9d24ac52008-09-09 23:35:53 +00004564 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner8b2d42c2007-03-03 23:43:21 +00004565 if (NoExcessFPPrecision)
4566 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4567 DAG.getValueType(VT));
4568 break;
4569 }
4570
Dale Johannesene00a8a22008-08-28 02:44:49 +00004571 case ISD::ATOMIC_CMP_SWAP_8:
4572 case ISD::ATOMIC_CMP_SWAP_16:
4573 case ISD::ATOMIC_CMP_SWAP_32:
4574 case ISD::ATOMIC_CMP_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004575 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004576 Tmp2 = PromoteOp(Node->getOperand(2));
4577 Tmp3 = PromoteOp(Node->getOperand(3));
Mon P Wang28873102008-06-25 08:15:39 +00004578 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4579 AtomNode->getBasePtr(), Tmp2, Tmp3,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004580 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004581 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004582 // Remember that we legalized the chain.
4583 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4584 break;
4585 }
Dale Johannesene00a8a22008-08-28 02:44:49 +00004586 case ISD::ATOMIC_LOAD_ADD_8:
4587 case ISD::ATOMIC_LOAD_SUB_8:
4588 case ISD::ATOMIC_LOAD_AND_8:
4589 case ISD::ATOMIC_LOAD_OR_8:
4590 case ISD::ATOMIC_LOAD_XOR_8:
4591 case ISD::ATOMIC_LOAD_NAND_8:
4592 case ISD::ATOMIC_LOAD_MIN_8:
4593 case ISD::ATOMIC_LOAD_MAX_8:
4594 case ISD::ATOMIC_LOAD_UMIN_8:
4595 case ISD::ATOMIC_LOAD_UMAX_8:
4596 case ISD::ATOMIC_SWAP_8:
4597 case ISD::ATOMIC_LOAD_ADD_16:
4598 case ISD::ATOMIC_LOAD_SUB_16:
4599 case ISD::ATOMIC_LOAD_AND_16:
4600 case ISD::ATOMIC_LOAD_OR_16:
4601 case ISD::ATOMIC_LOAD_XOR_16:
4602 case ISD::ATOMIC_LOAD_NAND_16:
4603 case ISD::ATOMIC_LOAD_MIN_16:
4604 case ISD::ATOMIC_LOAD_MAX_16:
4605 case ISD::ATOMIC_LOAD_UMIN_16:
4606 case ISD::ATOMIC_LOAD_UMAX_16:
4607 case ISD::ATOMIC_SWAP_16:
4608 case ISD::ATOMIC_LOAD_ADD_32:
4609 case ISD::ATOMIC_LOAD_SUB_32:
4610 case ISD::ATOMIC_LOAD_AND_32:
4611 case ISD::ATOMIC_LOAD_OR_32:
4612 case ISD::ATOMIC_LOAD_XOR_32:
4613 case ISD::ATOMIC_LOAD_NAND_32:
4614 case ISD::ATOMIC_LOAD_MIN_32:
4615 case ISD::ATOMIC_LOAD_MAX_32:
4616 case ISD::ATOMIC_LOAD_UMIN_32:
4617 case ISD::ATOMIC_LOAD_UMAX_32:
4618 case ISD::ATOMIC_SWAP_32:
4619 case ISD::ATOMIC_LOAD_ADD_64:
4620 case ISD::ATOMIC_LOAD_SUB_64:
4621 case ISD::ATOMIC_LOAD_AND_64:
4622 case ISD::ATOMIC_LOAD_OR_64:
4623 case ISD::ATOMIC_LOAD_XOR_64:
4624 case ISD::ATOMIC_LOAD_NAND_64:
4625 case ISD::ATOMIC_LOAD_MIN_64:
4626 case ISD::ATOMIC_LOAD_MAX_64:
4627 case ISD::ATOMIC_LOAD_UMIN_64:
4628 case ISD::ATOMIC_LOAD_UMAX_64:
4629 case ISD::ATOMIC_SWAP_64: {
Mon P Wang28873102008-06-25 08:15:39 +00004630 AtomicSDNode* AtomNode = cast<AtomicSDNode>(Node);
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004631 Tmp2 = PromoteOp(Node->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00004632 Result = DAG.getAtomic(Node->getOpcode(), AtomNode->getChain(),
4633 AtomNode->getBasePtr(), Tmp2,
Dan Gohmanfd4418f2008-06-25 16:07:49 +00004634 AtomNode->getSrcValue(),
Mon P Wang28873102008-06-25 08:15:39 +00004635 AtomNode->getAlignment());
Andrew Lenharthab0b9492008-02-21 06:45:13 +00004636 // Remember that we legalized the chain.
4637 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
4638 break;
4639 }
4640
Chris Lattner03c85462005-01-15 05:21:40 +00004641 case ISD::AND:
4642 case ISD::OR:
4643 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00004644 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00004645 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00004646 case ISD::MUL:
4647 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00004648 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00004649 // that too is okay if they are integer operations.
4650 Tmp1 = PromoteOp(Node->getOperand(0));
4651 Tmp2 = PromoteOp(Node->getOperand(1));
4652 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4653 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00004654 break;
4655 case ISD::FADD:
4656 case ISD::FSUB:
4657 case ISD::FMUL:
Chris Lattner01b3d732005-09-28 22:28:18 +00004658 Tmp1 = PromoteOp(Node->getOperand(0));
4659 Tmp2 = PromoteOp(Node->getOperand(1));
4660 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
4661 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4662
4663 // Floating point operations will give excess precision that we may not be
4664 // able to tolerate. If we DO allow excess precision, just leave it,
4665 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00004666 // FIXME: Why would we need to round FP ops more than integer ones?
4667 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00004668 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004669 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4670 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00004671 break;
4672
Chris Lattner8b6fa222005-01-15 22:16:26 +00004673 case ISD::SDIV:
4674 case ISD::SREM:
4675 // These operators require that their input be sign extended.
4676 Tmp1 = PromoteOp(Node->getOperand(0));
4677 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004678 if (NVT.isInteger()) {
Chris Lattner15e4b012005-07-10 00:07:11 +00004679 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4680 DAG.getValueType(VT));
4681 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
4682 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004683 }
4684 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4685
4686 // Perform FP_ROUND: this is probably overly pessimistic.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004687 if (NVT.isFloatingPoint() && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00004688 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4689 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004690 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00004691 case ISD::FDIV:
4692 case ISD::FREM:
Chris Lattnera09f8482006-03-05 05:09:38 +00004693 case ISD::FCOPYSIGN:
Chris Lattner01b3d732005-09-28 22:28:18 +00004694 // These operators require that their input be fp extended.
Nate Begemanec57fd92006-05-09 18:20:51 +00004695 switch (getTypeAction(Node->getOperand(0).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004696 case Expand: assert(0 && "not implemented");
4697 case Legal: Tmp1 = LegalizeOp(Node->getOperand(0)); break;
4698 case Promote: Tmp1 = PromoteOp(Node->getOperand(0)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004699 }
4700 switch (getTypeAction(Node->getOperand(1).getValueType())) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004701 case Expand: assert(0 && "not implemented");
4702 case Legal: Tmp2 = LegalizeOp(Node->getOperand(1)); break;
4703 case Promote: Tmp2 = PromoteOp(Node->getOperand(1)); break;
Nate Begemanec57fd92006-05-09 18:20:51 +00004704 }
Chris Lattner01b3d732005-09-28 22:28:18 +00004705 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4706
4707 // Perform FP_ROUND: this is probably overly pessimistic.
Chris Lattnera09f8482006-03-05 05:09:38 +00004708 if (NoExcessFPPrecision && Node->getOpcode() != ISD::FCOPYSIGN)
Chris Lattner01b3d732005-09-28 22:28:18 +00004709 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
4710 DAG.getValueType(VT));
4711 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00004712
4713 case ISD::UDIV:
4714 case ISD::UREM:
4715 // These operators require that their input be zero extended.
4716 Tmp1 = PromoteOp(Node->getOperand(0));
4717 Tmp2 = PromoteOp(Node->getOperand(1));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004718 assert(NVT.isInteger() && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00004719 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
4720 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00004721 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
4722 break;
4723
4724 case ISD::SHL:
4725 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner456a93a2006-01-28 07:39:30 +00004726 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004727 break;
4728 case ISD::SRA:
4729 // The input value must be properly sign extended.
4730 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00004731 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
4732 DAG.getValueType(VT));
Chris Lattner456a93a2006-01-28 07:39:30 +00004733 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004734 break;
4735 case ISD::SRL:
4736 // The input value must be properly zero extended.
4737 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00004738 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner456a93a2006-01-28 07:39:30 +00004739 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Node->getOperand(1));
Chris Lattner8b6fa222005-01-15 22:16:26 +00004740 break;
Nate Begeman0aed7842006-01-28 03:14:31 +00004741
4742 case ISD::VAARG:
Chris Lattner456a93a2006-01-28 07:39:30 +00004743 Tmp1 = Node->getOperand(0); // Get the chain.
4744 Tmp2 = Node->getOperand(1); // Get the pointer.
Nate Begeman0aed7842006-01-28 03:14:31 +00004745 if (TLI.getOperationAction(ISD::VAARG, VT) == TargetLowering::Custom) {
4746 Tmp3 = DAG.getVAArg(VT, Tmp1, Tmp2, Node->getOperand(2));
Duncan Sands126d9072008-07-04 11:47:58 +00004747 Result = TLI.LowerOperation(Tmp3, DAG);
Nate Begeman0aed7842006-01-28 03:14:31 +00004748 } else {
Dan Gohman69de1932008-02-06 22:27:42 +00004749 const Value *V = cast<SrcValueSDNode>(Node->getOperand(2))->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00004750 SDValue VAList = DAG.getLoad(TLI.getPointerTy(), Tmp1, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004751 // Increment the pointer, VAList, to the next vaarg
4752 Tmp3 = DAG.getNode(ISD::ADD, TLI.getPointerTy(), VAList,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004753 DAG.getConstant(VT.getSizeInBits()/8,
Nate Begeman0aed7842006-01-28 03:14:31 +00004754 TLI.getPointerTy()));
4755 // Store the incremented VAList to the legalized pointer
Dan Gohman69de1932008-02-06 22:27:42 +00004756 Tmp3 = DAG.getStore(VAList.getValue(1), Tmp3, Tmp2, V, 0);
Nate Begeman0aed7842006-01-28 03:14:31 +00004757 // Load the actual argument out of the pointer VAList
Evan Cheng466685d2006-10-09 20:57:25 +00004758 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp3, VAList, NULL, 0, VT);
Nate Begeman0aed7842006-01-28 03:14:31 +00004759 }
4760 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004761 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Nate Begeman0aed7842006-01-28 03:14:31 +00004762 break;
4763
Evan Cheng466685d2006-10-09 20:57:25 +00004764 case ISD::LOAD: {
4765 LoadSDNode *LD = cast<LoadSDNode>(Node);
Evan Cheng62f2a3c2006-10-10 07:51:21 +00004766 ISD::LoadExtType ExtType = ISD::isNON_EXTLoad(Node)
4767 ? ISD::EXTLOAD : LD->getExtensionType();
4768 Result = DAG.getExtLoad(ExtType, NVT,
4769 LD->getChain(), LD->getBasePtr(),
Chris Lattner55b57082006-10-10 18:54:19 +00004770 LD->getSrcValue(), LD->getSrcValueOffset(),
Dan Gohmanb625f2f2008-01-30 00:15:11 +00004771 LD->getMemoryVT(),
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00004772 LD->isVolatile(),
4773 LD->getAlignment());
Chris Lattner03c85462005-01-15 05:21:40 +00004774 // Remember that we legalized the chain.
Chris Lattner456a93a2006-01-28 07:39:30 +00004775 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
Chris Lattner03c85462005-01-15 05:21:40 +00004776 break;
Evan Cheng466685d2006-10-09 20:57:25 +00004777 }
Scott Michel8bf61e82008-06-02 22:18:03 +00004778 case ISD::SELECT: {
Chris Lattner03c85462005-01-15 05:21:40 +00004779 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
4780 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
Scott Michel8bf61e82008-06-02 22:18:03 +00004781
Duncan Sands83ec4b62008-06-06 12:08:01 +00004782 MVT VT2 = Tmp2.getValueType();
Scott Michel8bf61e82008-06-02 22:18:03 +00004783 assert(VT2 == Tmp3.getValueType()
Scott Michelba12f572008-06-03 19:13:20 +00004784 && "PromoteOp SELECT: Operands 2 and 3 ValueTypes don't match");
4785 // Ensure that the resulting node is at least the same size as the operands'
4786 // value types, because we cannot assume that TLI.getSetCCValueType() is
4787 // constant.
4788 Result = DAG.getNode(ISD::SELECT, VT2, Node->getOperand(0), Tmp2, Tmp3);
Chris Lattner03c85462005-01-15 05:21:40 +00004789 break;
Scott Michel8bf61e82008-06-02 22:18:03 +00004790 }
Nate Begeman9373a812005-08-10 20:51:12 +00004791 case ISD::SELECT_CC:
4792 Tmp2 = PromoteOp(Node->getOperand(2)); // True
4793 Tmp3 = PromoteOp(Node->getOperand(3)); // False
4794 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
Chris Lattner456a93a2006-01-28 07:39:30 +00004795 Node->getOperand(1), Tmp2, Tmp3, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00004796 break;
Nate Begemand88fc032006-01-14 03:14:10 +00004797 case ISD::BSWAP:
4798 Tmp1 = Node->getOperand(0);
4799 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
4800 Tmp1 = DAG.getNode(ISD::BSWAP, NVT, Tmp1);
4801 Result = DAG.getNode(ISD::SRL, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004802 DAG.getConstant(NVT.getSizeInBits() -
4803 VT.getSizeInBits(),
Nate Begemand88fc032006-01-14 03:14:10 +00004804 TLI.getShiftAmountTy()));
4805 break;
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004806 case ISD::CTPOP:
4807 case ISD::CTTZ:
4808 case ISD::CTLZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004809 // Zero extend the argument
4810 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004811 // Perform the larger operation, then subtract if needed.
4812 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
Chris Lattner456a93a2006-01-28 07:39:30 +00004813 switch(Node->getOpcode()) {
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004814 case ISD::CTPOP:
4815 Result = Tmp1;
4816 break;
4817 case ISD::CTTZ:
Chris Lattner456a93a2006-01-28 07:39:30 +00004818 // if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Scott Michel5b8f82e2008-03-10 15:42:14 +00004819 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(Tmp1), Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004820 DAG.getConstant(NVT.getSizeInBits(), NVT),
Dan Gohmanb55757e2007-05-18 17:52:13 +00004821 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00004822 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004823 DAG.getConstant(VT.getSizeInBits(), NVT), Tmp1);
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004824 break;
4825 case ISD::CTLZ:
4826 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00004827 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004828 DAG.getConstant(NVT.getSizeInBits() -
4829 VT.getSizeInBits(), NVT));
Andrew Lenharthfecf0952005-05-04 19:11:05 +00004830 break;
4831 }
4832 break;
Dan Gohman7f321562007-06-25 16:23:39 +00004833 case ISD::EXTRACT_SUBVECTOR:
4834 Result = PromoteOp(ExpandEXTRACT_SUBVECTOR(Op));
Dan Gohman65956352007-06-13 15:12:02 +00004835 break;
Chris Lattner4aab2f42006-04-02 05:06:04 +00004836 case ISD::EXTRACT_VECTOR_ELT:
4837 Result = PromoteOp(ExpandEXTRACT_VECTOR_ELT(Op));
4838 break;
Chris Lattner03c85462005-01-15 05:21:40 +00004839 }
4840
Gabor Greifba36cb52008-08-28 21:40:38 +00004841 assert(Result.getNode() && "Didn't set a result!");
Chris Lattner456a93a2006-01-28 07:39:30 +00004842
4843 // Make sure the result is itself legal.
4844 Result = LegalizeOp(Result);
4845
4846 // Remember that we promoted this!
Chris Lattner03c85462005-01-15 05:21:40 +00004847 AddPromotedOperand(Op, Result);
4848 return Result;
4849}
Chris Lattner3e928bb2005-01-07 07:47:09 +00004850
Dan Gohman7f321562007-06-25 16:23:39 +00004851/// ExpandEXTRACT_VECTOR_ELT - Expand an EXTRACT_VECTOR_ELT operation into
4852/// a legal EXTRACT_VECTOR_ELT operation, scalar code, or memory traffic,
4853/// based on the vector type. The return type of this matches the element type
4854/// of the vector, which may not be legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00004855SDValue SelectionDAGLegalize::ExpandEXTRACT_VECTOR_ELT(SDValue Op) {
Chris Lattner15972212006-03-31 17:55:51 +00004856 // We know that operand #0 is the Vec vector. If the index is a constant
4857 // or if the invec is a supported hardware type, we can use it. Otherwise,
4858 // lower to a store then an indexed load.
Dan Gohman475871a2008-07-27 21:46:04 +00004859 SDValue Vec = Op.getOperand(0);
4860 SDValue Idx = Op.getOperand(1);
Chris Lattner15972212006-03-31 17:55:51 +00004861
Duncan Sands83ec4b62008-06-06 12:08:01 +00004862 MVT TVT = Vec.getValueType();
4863 unsigned NumElems = TVT.getVectorNumElements();
Chris Lattner15972212006-03-31 17:55:51 +00004864
Dan Gohman7f321562007-06-25 16:23:39 +00004865 switch (TLI.getOperationAction(ISD::EXTRACT_VECTOR_ELT, TVT)) {
4866 default: assert(0 && "This action is not supported yet!");
4867 case TargetLowering::Custom: {
4868 Vec = LegalizeOp(Vec);
4869 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004870 SDValue Tmp3 = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004871 if (Tmp3.getNode())
Dan Gohman7f321562007-06-25 16:23:39 +00004872 return Tmp3;
4873 break;
4874 }
4875 case TargetLowering::Legal:
4876 if (isTypeLegal(TVT)) {
4877 Vec = LegalizeOp(Vec);
4878 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Christopher Lamb844228a2007-07-26 03:33:13 +00004879 return Op;
Dan Gohman7f321562007-06-25 16:23:39 +00004880 }
4881 break;
Mon P Wang0c397192008-10-30 08:01:45 +00004882 case TargetLowering::Promote:
4883 assert(TVT.isVector() && "not vector type");
4884 // fall thru to expand since vectors are by default are promote
Dan Gohman7f321562007-06-25 16:23:39 +00004885 case TargetLowering::Expand:
4886 break;
4887 }
4888
4889 if (NumElems == 1) {
Chris Lattner15972212006-03-31 17:55:51 +00004890 // This must be an access of the only element. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004891 Op = ScalarizeVectorOp(Vec);
4892 } else if (!TLI.isTypeLegal(TVT) && isa<ConstantSDNode>(Idx)) {
Nate Begeman55030dc2008-01-29 02:24:00 +00004893 unsigned NumLoElts = 1 << Log2_32(NumElems-1);
Dan Gohman7f321562007-06-25 16:23:39 +00004894 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004895 SDValue Lo, Hi;
Chris Lattner15972212006-03-31 17:55:51 +00004896 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004897 if (CIdx->getZExtValue() < NumLoElts) {
Chris Lattner15972212006-03-31 17:55:51 +00004898 Vec = Lo;
4899 } else {
4900 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004901 Idx = DAG.getConstant(CIdx->getZExtValue() - NumLoElts,
Dan Gohman7f321562007-06-25 16:23:39 +00004902 Idx.getValueType());
Chris Lattner15972212006-03-31 17:55:51 +00004903 }
Dan Gohman7f321562007-06-25 16:23:39 +00004904
Chris Lattner15972212006-03-31 17:55:51 +00004905 // It's now an extract from the appropriate high or low part. Recurse.
4906 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004907 Op = ExpandEXTRACT_VECTOR_ELT(Op);
Chris Lattner15972212006-03-31 17:55:51 +00004908 } else {
Dan Gohman7f321562007-06-25 16:23:39 +00004909 // Store the value to a temporary stack slot, then LOAD the scalar
4910 // element back out.
Dan Gohman475871a2008-07-27 21:46:04 +00004911 SDValue StackPtr = DAG.CreateStackTemporary(Vec.getValueType());
4912 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Vec, StackPtr, NULL, 0);
Dan Gohman7f321562007-06-25 16:23:39 +00004913
4914 // Add the offset to the index.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004915 unsigned EltSize = Op.getValueType().getSizeInBits()/8;
Dan Gohman7f321562007-06-25 16:23:39 +00004916 Idx = DAG.getNode(ISD::MUL, Idx.getValueType(), Idx,
4917 DAG.getConstant(EltSize, Idx.getValueType()));
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004918
Duncan Sands8e4eb092008-06-08 20:54:56 +00004919 if (Idx.getValueType().bitsGT(TLI.getPointerTy()))
Chris Lattnerf185e672007-10-19 16:47:35 +00004920 Idx = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004921 else
Chris Lattnerf185e672007-10-19 16:47:35 +00004922 Idx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), Idx);
Bill Wendling90bfc2d2007-10-18 08:32:37 +00004923
Dan Gohman7f321562007-06-25 16:23:39 +00004924 StackPtr = DAG.getNode(ISD::ADD, Idx.getValueType(), Idx, StackPtr);
4925
4926 Op = DAG.getLoad(Op.getValueType(), Ch, StackPtr, NULL, 0);
Chris Lattner15972212006-03-31 17:55:51 +00004927 }
Dan Gohman7f321562007-06-25 16:23:39 +00004928 return Op;
Chris Lattner15972212006-03-31 17:55:51 +00004929}
4930
Dan Gohman7f321562007-06-25 16:23:39 +00004931/// ExpandEXTRACT_SUBVECTOR - Expand a EXTRACT_SUBVECTOR operation. For now
Dan Gohman65956352007-06-13 15:12:02 +00004932/// we assume the operation can be split if it is not already legal.
Dan Gohman475871a2008-07-27 21:46:04 +00004933SDValue SelectionDAGLegalize::ExpandEXTRACT_SUBVECTOR(SDValue Op) {
Dan Gohman65956352007-06-13 15:12:02 +00004934 // We know that operand #0 is the Vec vector. For now we assume the index
4935 // is a constant and that the extracted result is a supported hardware type.
Dan Gohman475871a2008-07-27 21:46:04 +00004936 SDValue Vec = Op.getOperand(0);
4937 SDValue Idx = LegalizeOp(Op.getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00004938
Duncan Sands83ec4b62008-06-06 12:08:01 +00004939 unsigned NumElems = Vec.getValueType().getVectorNumElements();
Dan Gohman65956352007-06-13 15:12:02 +00004940
Duncan Sands83ec4b62008-06-06 12:08:01 +00004941 if (NumElems == Op.getValueType().getVectorNumElements()) {
Dan Gohman65956352007-06-13 15:12:02 +00004942 // This must be an access of the desired vector length. Return it.
Dan Gohman7f321562007-06-25 16:23:39 +00004943 return Vec;
Dan Gohman65956352007-06-13 15:12:02 +00004944 }
4945
4946 ConstantSDNode *CIdx = cast<ConstantSDNode>(Idx);
Dan Gohman475871a2008-07-27 21:46:04 +00004947 SDValue Lo, Hi;
Dan Gohman65956352007-06-13 15:12:02 +00004948 SplitVectorOp(Vec, Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004949 if (CIdx->getZExtValue() < NumElems/2) {
Dan Gohman65956352007-06-13 15:12:02 +00004950 Vec = Lo;
4951 } else {
4952 Vec = Hi;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004953 Idx = DAG.getConstant(CIdx->getZExtValue() - NumElems/2,
4954 Idx.getValueType());
Dan Gohman65956352007-06-13 15:12:02 +00004955 }
4956
4957 // It's now an extract from the appropriate high or low part. Recurse.
4958 Op = DAG.UpdateNodeOperands(Op, Vec, Idx);
Dan Gohman7f321562007-06-25 16:23:39 +00004959 return ExpandEXTRACT_SUBVECTOR(Op);
Dan Gohman65956352007-06-13 15:12:02 +00004960}
4961
Nate Begeman750ac1b2006-02-01 07:19:44 +00004962/// LegalizeSetCCOperands - Attempts to create a legal LHS and RHS for a SETCC
4963/// with condition CC on the current target. This usually involves legalizing
4964/// or promoting the arguments. In the case where LHS and RHS must be expanded,
4965/// there may be no choice but to create a new SetCC node to represent the
4966/// legalized value of setcc lhs, rhs. In this case, the value is returned in
Dan Gohman475871a2008-07-27 21:46:04 +00004967/// LHS, and the SDValue returned in RHS has a nil SDNode value.
4968void SelectionDAGLegalize::LegalizeSetCCOperands(SDValue &LHS,
4969 SDValue &RHS,
4970 SDValue &CC) {
4971 SDValue Tmp1, Tmp2, Tmp3, Result;
Nate Begeman750ac1b2006-02-01 07:19:44 +00004972
4973 switch (getTypeAction(LHS.getValueType())) {
4974 case Legal:
4975 Tmp1 = LegalizeOp(LHS); // LHS
4976 Tmp2 = LegalizeOp(RHS); // RHS
4977 break;
4978 case Promote:
4979 Tmp1 = PromoteOp(LHS); // LHS
4980 Tmp2 = PromoteOp(RHS); // RHS
4981
4982 // If this is an FP compare, the operands have already been extended.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004983 if (LHS.getValueType().isInteger()) {
4984 MVT VT = LHS.getValueType();
4985 MVT NVT = TLI.getTypeToTransformTo(VT);
Nate Begeman750ac1b2006-02-01 07:19:44 +00004986
4987 // Otherwise, we have to insert explicit sign or zero extends. Note
4988 // that we could insert sign extends for ALL conditions, but zero extend
4989 // is cheaper on many machines (an AND instead of two shifts), so prefer
4990 // it.
4991 switch (cast<CondCodeSDNode>(CC)->get()) {
4992 default: assert(0 && "Unknown integer comparison!");
4993 case ISD::SETEQ:
4994 case ISD::SETNE:
4995 case ISD::SETUGE:
4996 case ISD::SETUGT:
4997 case ISD::SETULE:
4998 case ISD::SETULT:
4999 // ALL of these operations will work if we either sign or zero extend
5000 // the operands (including the unsigned comparisons!). Zero extend is
5001 // usually a simpler/cheaper operation, so prefer it.
5002 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
5003 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
5004 break;
5005 case ISD::SETGE:
5006 case ISD::SETGT:
5007 case ISD::SETLT:
5008 case ISD::SETLE:
5009 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
5010 DAG.getValueType(VT));
5011 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
5012 DAG.getValueType(VT));
Evan Chengefa53392008-10-13 18:46:18 +00005013 Tmp1 = LegalizeOp(Tmp1); // Relegalize new nodes.
5014 Tmp2 = LegalizeOp(Tmp2); // Relegalize new nodes.
Nate Begeman750ac1b2006-02-01 07:19:44 +00005015 break;
5016 }
5017 }
5018 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005019 case Expand: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005020 MVT VT = LHS.getValueType();
Evan Cheng2b49c502006-12-15 02:59:56 +00005021 if (VT == MVT::f32 || VT == MVT::f64) {
5022 // Expand into one or more soft-fp libcall(s).
Evan Cheng6518c6e2008-07-01 21:35:46 +00005023 RTLIB::Libcall LC1 = RTLIB::UNKNOWN_LIBCALL, LC2 = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng2b49c502006-12-15 02:59:56 +00005024 switch (cast<CondCodeSDNode>(CC)->get()) {
5025 case ISD::SETEQ:
5026 case ISD::SETOEQ:
Evan Cheng56966222007-01-12 02:11:51 +00005027 LC1 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005028 break;
5029 case ISD::SETNE:
5030 case ISD::SETUNE:
Evan Cheng56966222007-01-12 02:11:51 +00005031 LC1 = (VT == MVT::f32) ? RTLIB::UNE_F32 : RTLIB::UNE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005032 break;
5033 case ISD::SETGE:
5034 case ISD::SETOGE:
Evan Cheng56966222007-01-12 02:11:51 +00005035 LC1 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005036 break;
5037 case ISD::SETLT:
5038 case ISD::SETOLT:
Evan Cheng56966222007-01-12 02:11:51 +00005039 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005040 break;
5041 case ISD::SETLE:
5042 case ISD::SETOLE:
Evan Cheng56966222007-01-12 02:11:51 +00005043 LC1 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005044 break;
5045 case ISD::SETGT:
5046 case ISD::SETOGT:
Evan Cheng56966222007-01-12 02:11:51 +00005047 LC1 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005048 break;
5049 case ISD::SETUO:
Evan Chengd385fd62007-01-31 09:29:11 +00005050 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
5051 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005052 case ISD::SETO:
Evan Cheng5efdecc2007-02-03 00:43:46 +00005053 LC1 = (VT == MVT::f32) ? RTLIB::O_F32 : RTLIB::O_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005054 break;
5055 default:
Evan Cheng56966222007-01-12 02:11:51 +00005056 LC1 = (VT == MVT::f32) ? RTLIB::UO_F32 : RTLIB::UO_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005057 switch (cast<CondCodeSDNode>(CC)->get()) {
5058 case ISD::SETONE:
5059 // SETONE = SETOLT | SETOGT
Evan Cheng56966222007-01-12 02:11:51 +00005060 LC1 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005061 // Fallthrough
5062 case ISD::SETUGT:
Evan Cheng56966222007-01-12 02:11:51 +00005063 LC2 = (VT == MVT::f32) ? RTLIB::OGT_F32 : RTLIB::OGT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005064 break;
5065 case ISD::SETUGE:
Evan Cheng56966222007-01-12 02:11:51 +00005066 LC2 = (VT == MVT::f32) ? RTLIB::OGE_F32 : RTLIB::OGE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005067 break;
5068 case ISD::SETULT:
Evan Cheng56966222007-01-12 02:11:51 +00005069 LC2 = (VT == MVT::f32) ? RTLIB::OLT_F32 : RTLIB::OLT_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005070 break;
5071 case ISD::SETULE:
Evan Cheng56966222007-01-12 02:11:51 +00005072 LC2 = (VT == MVT::f32) ? RTLIB::OLE_F32 : RTLIB::OLE_F64;
Evan Cheng2b49c502006-12-15 02:59:56 +00005073 break;
Evan Cheng56966222007-01-12 02:11:51 +00005074 case ISD::SETUEQ:
5075 LC2 = (VT == MVT::f32) ? RTLIB::OEQ_F32 : RTLIB::OEQ_F64;
Evan Cheng56966222007-01-12 02:11:51 +00005076 break;
Evan Cheng2b49c502006-12-15 02:59:56 +00005077 default: assert(0 && "Unsupported FP setcc!");
5078 }
5079 }
Duncan Sandsf9516202008-06-30 10:19:09 +00005080
Dan Gohman475871a2008-07-27 21:46:04 +00005081 SDValue Dummy;
5082 SDValue Ops[2] = { LHS, RHS };
Gabor Greifba36cb52008-08-28 21:40:38 +00005083 Tmp1 = ExpandLibCall(LC1, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005084 false /*sign irrelevant*/, Dummy);
Evan Cheng2b49c502006-12-15 02:59:56 +00005085 Tmp2 = DAG.getConstant(0, MVT::i32);
Evan Chengd385fd62007-01-31 09:29:11 +00005086 CC = DAG.getCondCode(TLI.getCmpLibcallCC(LC1));
Evan Cheng56966222007-01-12 02:11:51 +00005087 if (LC2 != RTLIB::UNKNOWN_LIBCALL) {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005088 Tmp1 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Tmp1), Tmp1, Tmp2,
Nate Begeman5922f562008-03-14 00:53:31 +00005089 CC);
Gabor Greifba36cb52008-08-28 21:40:38 +00005090 LHS = ExpandLibCall(LC2, DAG.getMergeValues(Ops, 2).getNode(),
Reid Spencerb47b25c2007-01-03 04:22:32 +00005091 false /*sign irrelevant*/, Dummy);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005092 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHS), LHS, Tmp2,
Evan Chengd385fd62007-01-31 09:29:11 +00005093 DAG.getCondCode(TLI.getCmpLibcallCC(LC2)));
Evan Cheng2b49c502006-12-15 02:59:56 +00005094 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Dan Gohman475871a2008-07-27 21:46:04 +00005095 Tmp2 = SDValue();
Evan Cheng2b49c502006-12-15 02:59:56 +00005096 }
Evan Cheng21cacc42008-07-07 07:18:09 +00005097 LHS = LegalizeOp(Tmp1);
Evan Cheng2b49c502006-12-15 02:59:56 +00005098 RHS = Tmp2;
5099 return;
5100 }
5101
Dan Gohman475871a2008-07-27 21:46:04 +00005102 SDValue LHSLo, LHSHi, RHSLo, RHSHi;
Nate Begeman750ac1b2006-02-01 07:19:44 +00005103 ExpandOp(LHS, LHSLo, LHSHi);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005104 ExpandOp(RHS, RHSLo, RHSHi);
5105 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5106
5107 if (VT==MVT::ppcf128) {
5108 // FIXME: This generated code sucks. We want to generate
Dale Johannesene2f20832008-09-12 00:30:56 +00005109 // FCMPU crN, hi1, hi2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005110 // BNE crN, L:
Dale Johannesene2f20832008-09-12 00:30:56 +00005111 // FCMPU crN, lo1, lo2
Dale Johannesen638ccd52007-10-06 01:24:11 +00005112 // The following can be improved, but not that much.
Dale Johannesene2f20832008-09-12 00:30:56 +00005113 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5114 ISD::SETOEQ);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005115 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005116 Tmp3 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
Dale Johannesene2f20832008-09-12 00:30:56 +00005117 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
5118 ISD::SETUNE);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005119 Tmp2 = DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi, CCCode);
Dale Johannesen638ccd52007-10-06 01:24:11 +00005120 Tmp1 = DAG.getNode(ISD::AND, Tmp1.getValueType(), Tmp1, Tmp2);
5121 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp3);
Dan Gohman475871a2008-07-27 21:46:04 +00005122 Tmp2 = SDValue();
Dale Johannesen638ccd52007-10-06 01:24:11 +00005123 break;
5124 }
5125
5126 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005127 case ISD::SETEQ:
5128 case ISD::SETNE:
5129 if (RHSLo == RHSHi)
5130 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
5131 if (RHSCST->isAllOnesValue()) {
5132 // Comparison to -1.
5133 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
5134 Tmp2 = RHSLo;
5135 break;
5136 }
5137
5138 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
5139 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
5140 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
5141 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
5142 break;
5143 default:
5144 // If this is a comparison of the sign bit, just look at the top part.
5145 // X > -1, x < 0
5146 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(RHS))
5147 if ((cast<CondCodeSDNode>(CC)->get() == ISD::SETLT &&
Dan Gohman002e5d02008-03-13 22:13:53 +00005148 CST->isNullValue()) || // X < 0
Nate Begeman750ac1b2006-02-01 07:19:44 +00005149 (cast<CondCodeSDNode>(CC)->get() == ISD::SETGT &&
5150 CST->isAllOnesValue())) { // X > -1
5151 Tmp1 = LHSHi;
5152 Tmp2 = RHSHi;
5153 break;
5154 }
5155
5156 // FIXME: This generated code sucks.
5157 ISD::CondCode LowCC;
Evan Cheng2e677812007-02-08 22:16:19 +00005158 switch (CCCode) {
Nate Begeman750ac1b2006-02-01 07:19:44 +00005159 default: assert(0 && "Unknown integer setcc!");
5160 case ISD::SETLT:
5161 case ISD::SETULT: LowCC = ISD::SETULT; break;
5162 case ISD::SETGT:
5163 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
5164 case ISD::SETLE:
5165 case ISD::SETULE: LowCC = ISD::SETULE; break;
5166 case ISD::SETGE:
5167 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
5168 }
5169
5170 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
5171 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
5172 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
5173
5174 // NOTE: on targets without efficient SELECT of bools, we can always use
5175 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Evan Cheng2e677812007-02-08 22:16:19 +00005176 TargetLowering::DAGCombinerInfo DagCombineInfo(DAG, false, true, NULL);
Scott Michel5b8f82e2008-03-10 15:42:14 +00005177 Tmp1 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo,
Nate Begeman5922f562008-03-14 00:53:31 +00005178 LowCC, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005179 if (!Tmp1.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005180 Tmp1 = DAG.getSetCC(TLI.getSetCCResultType(LHSLo), LHSLo, RHSLo, LowCC);
5181 Tmp2 = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005182 CCCode, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005183 if (!Tmp2.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005184 Tmp2 = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(LHSHi), LHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005185 RHSHi,CC);
Evan Cheng2e677812007-02-08 22:16:19 +00005186
Gabor Greifba36cb52008-08-28 21:40:38 +00005187 ConstantSDNode *Tmp1C = dyn_cast<ConstantSDNode>(Tmp1.getNode());
5188 ConstantSDNode *Tmp2C = dyn_cast<ConstantSDNode>(Tmp2.getNode());
Dan Gohman002e5d02008-03-13 22:13:53 +00005189 if ((Tmp1C && Tmp1C->isNullValue()) ||
5190 (Tmp2C && Tmp2C->isNullValue() &&
Evan Cheng2e677812007-02-08 22:16:19 +00005191 (CCCode == ISD::SETLE || CCCode == ISD::SETGE ||
5192 CCCode == ISD::SETUGE || CCCode == ISD::SETULE)) ||
Dan Gohman002e5d02008-03-13 22:13:53 +00005193 (Tmp2C && Tmp2C->getAPIntValue() == 1 &&
Evan Cheng2e677812007-02-08 22:16:19 +00005194 (CCCode == ISD::SETLT || CCCode == ISD::SETGT ||
5195 CCCode == ISD::SETUGT || CCCode == ISD::SETULT))) {
5196 // low part is known false, returns high part.
5197 // For LE / GE, if high part is known false, ignore the low part.
5198 // For LT / GT, if high part is known true, ignore the low part.
5199 Tmp1 = Tmp2;
Dan Gohman475871a2008-07-27 21:46:04 +00005200 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005201 } else {
Scott Michel5b8f82e2008-03-10 15:42:14 +00005202 Result = TLI.SimplifySetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Evan Cheng2e677812007-02-08 22:16:19 +00005203 ISD::SETEQ, false, DagCombineInfo);
Gabor Greifba36cb52008-08-28 21:40:38 +00005204 if (!Result.getNode())
Scott Michel5b8f82e2008-03-10 15:42:14 +00005205 Result=DAG.getSetCC(TLI.getSetCCResultType(LHSHi), LHSHi, RHSHi,
Nate Begeman5922f562008-03-14 00:53:31 +00005206 ISD::SETEQ);
Evan Cheng2e677812007-02-08 22:16:19 +00005207 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
5208 Result, Tmp1, Tmp2));
5209 Tmp1 = Result;
Dan Gohman475871a2008-07-27 21:46:04 +00005210 Tmp2 = SDValue();
Evan Cheng2e677812007-02-08 22:16:19 +00005211 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005212 }
5213 }
Evan Cheng2b49c502006-12-15 02:59:56 +00005214 }
Nate Begeman750ac1b2006-02-01 07:19:44 +00005215 LHS = Tmp1;
5216 RHS = Tmp2;
5217}
5218
Evan Cheng7f042682008-10-15 02:05:31 +00005219/// LegalizeSetCCCondCode - Legalize a SETCC with given LHS and RHS and
5220/// condition code CC on the current target. This routine assumes LHS and rHS
5221/// have already been legalized by LegalizeSetCCOperands. It expands SETCC with
5222/// illegal condition code into AND / OR of multiple SETCC values.
5223void SelectionDAGLegalize::LegalizeSetCCCondCode(MVT VT,
5224 SDValue &LHS, SDValue &RHS,
5225 SDValue &CC) {
5226 MVT OpVT = LHS.getValueType();
5227 ISD::CondCode CCCode = cast<CondCodeSDNode>(CC)->get();
5228 switch (TLI.getCondCodeAction(CCCode, OpVT)) {
5229 default: assert(0 && "Unknown condition code action!");
5230 case TargetLowering::Legal:
5231 // Nothing to do.
5232 break;
5233 case TargetLowering::Expand: {
5234 ISD::CondCode CC1 = ISD::SETCC_INVALID, CC2 = ISD::SETCC_INVALID;
5235 unsigned Opc = 0;
5236 switch (CCCode) {
5237 default: assert(0 && "Don't know how to expand this condition!"); abort();
Dan Gohmane7d238e2008-10-21 03:12:54 +00005238 case ISD::SETOEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETO; Opc = ISD::AND; break;
5239 case ISD::SETOGT: CC1 = ISD::SETGT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5240 case ISD::SETOGE: CC1 = ISD::SETGE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5241 case ISD::SETOLT: CC1 = ISD::SETLT; CC2 = ISD::SETO; Opc = ISD::AND; break;
5242 case ISD::SETOLE: CC1 = ISD::SETLE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5243 case ISD::SETONE: CC1 = ISD::SETNE; CC2 = ISD::SETO; Opc = ISD::AND; break;
5244 case ISD::SETUEQ: CC1 = ISD::SETEQ; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5245 case ISD::SETUGT: CC1 = ISD::SETGT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5246 case ISD::SETUGE: CC1 = ISD::SETGE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5247 case ISD::SETULT: CC1 = ISD::SETLT; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5248 case ISD::SETULE: CC1 = ISD::SETLE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
5249 case ISD::SETUNE: CC1 = ISD::SETNE; CC2 = ISD::SETUO; Opc = ISD::OR; break;
Evan Cheng7f042682008-10-15 02:05:31 +00005250 // FIXME: Implement more expansions.
5251 }
5252
5253 SDValue SetCC1 = DAG.getSetCC(VT, LHS, RHS, CC1);
5254 SDValue SetCC2 = DAG.getSetCC(VT, LHS, RHS, CC2);
5255 LHS = DAG.getNode(Opc, VT, SetCC1, SetCC2);
5256 RHS = SDValue();
5257 CC = SDValue();
5258 break;
5259 }
5260 }
5261}
5262
Chris Lattner1401d152008-01-16 07:45:30 +00005263/// EmitStackConvert - Emit a store/load combination to the stack. This stores
5264/// SrcOp to a stack slot of type SlotVT, truncating it if needed. It then does
5265/// a load from the stack slot to DestVT, extending it if needed.
5266/// The resultant code need not be legal.
Dan Gohman475871a2008-07-27 21:46:04 +00005267SDValue SelectionDAGLegalize::EmitStackConvert(SDValue SrcOp,
5268 MVT SlotVT,
5269 MVT DestVT) {
Chris Lattner35481892005-12-23 00:16:34 +00005270 // Create the stack frame object.
Mon P Wang364d73d2008-07-05 20:40:31 +00005271 unsigned SrcAlign = TLI.getTargetData()->getPrefTypeAlignment(
5272 SrcOp.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00005273 SDValue FIPtr = DAG.CreateStackTemporary(SlotVT, SrcAlign);
Mon P Wang364d73d2008-07-05 20:40:31 +00005274
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005275 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(FIPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005276 int SPFI = StackPtrFI->getIndex();
Mon P Wang364d73d2008-07-05 20:40:31 +00005277
Duncan Sands83ec4b62008-06-06 12:08:01 +00005278 unsigned SrcSize = SrcOp.getValueType().getSizeInBits();
5279 unsigned SlotSize = SlotVT.getSizeInBits();
5280 unsigned DestSize = DestVT.getSizeInBits();
Mon P Wang364d73d2008-07-05 20:40:31 +00005281 unsigned DestAlign = TLI.getTargetData()->getPrefTypeAlignment(
5282 DestVT.getTypeForMVT());
Chris Lattner35481892005-12-23 00:16:34 +00005283
Chris Lattner1401d152008-01-16 07:45:30 +00005284 // Emit a store to the stack slot. Use a truncstore if the input value is
5285 // later than DestVT.
Dan Gohman475871a2008-07-27 21:46:04 +00005286 SDValue Store;
Mon P Wang364d73d2008-07-05 20:40:31 +00005287
Chris Lattner1401d152008-01-16 07:45:30 +00005288 if (SrcSize > SlotSize)
Dan Gohman69de1932008-02-06 22:27:42 +00005289 Store = DAG.getTruncStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005290 PseudoSourceValue::getFixedStack(SPFI), 0,
5291 SlotVT, false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005292 else {
5293 assert(SrcSize == SlotSize && "Invalid store");
Dan Gohman69de1932008-02-06 22:27:42 +00005294 Store = DAG.getStore(DAG.getEntryNode(), SrcOp, FIPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005295 PseudoSourceValue::getFixedStack(SPFI), 0,
Mon P Wang364d73d2008-07-05 20:40:31 +00005296 false, SrcAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005297 }
5298
Chris Lattner35481892005-12-23 00:16:34 +00005299 // Result is a load from the stack slot.
Chris Lattner1401d152008-01-16 07:45:30 +00005300 if (SlotSize == DestSize)
Mon P Wang364d73d2008-07-05 20:40:31 +00005301 return DAG.getLoad(DestVT, Store, FIPtr, NULL, 0, false, DestAlign);
Chris Lattner1401d152008-01-16 07:45:30 +00005302
5303 assert(SlotSize < DestSize && "Unknown extension!");
Mon P Wang364d73d2008-07-05 20:40:31 +00005304 return DAG.getExtLoad(ISD::EXTLOAD, DestVT, Store, FIPtr, NULL, 0, SlotVT,
5305 false, DestAlign);
Chris Lattner35481892005-12-23 00:16:34 +00005306}
5307
Dan Gohman475871a2008-07-27 21:46:04 +00005308SDValue SelectionDAGLegalize::ExpandSCALAR_TO_VECTOR(SDNode *Node) {
Chris Lattner4352cc92006-04-04 17:23:26 +00005309 // Create a vector sized/aligned stack slot, store the value to element #0,
5310 // then load the whole vector back out.
Dan Gohman475871a2008-07-27 21:46:04 +00005311 SDValue StackPtr = DAG.CreateStackTemporary(Node->getValueType(0));
Dan Gohman69de1932008-02-06 22:27:42 +00005312
Dan Gohmanbbbbb9c2008-02-11 18:58:42 +00005313 FrameIndexSDNode *StackPtrFI = cast<FrameIndexSDNode>(StackPtr);
Dan Gohman69de1932008-02-06 22:27:42 +00005314 int SPFI = StackPtrFI->getIndex();
5315
Dan Gohman475871a2008-07-27 21:46:04 +00005316 SDValue Ch = DAG.getStore(DAG.getEntryNode(), Node->getOperand(0), StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005317 PseudoSourceValue::getFixedStack(SPFI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00005318 return DAG.getLoad(Node->getValueType(0), Ch, StackPtr,
Dan Gohmana54cf172008-07-11 22:44:52 +00005319 PseudoSourceValue::getFixedStack(SPFI), 0);
Chris Lattner4352cc92006-04-04 17:23:26 +00005320}
5321
5322
Chris Lattnerce872152006-03-19 06:31:19 +00005323/// ExpandBUILD_VECTOR - Expand a BUILD_VECTOR node on targets that don't
Dan Gohman07a96762007-07-16 14:29:03 +00005324/// support the operation, but do support the resultant vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00005325SDValue SelectionDAGLegalize::ExpandBUILD_VECTOR(SDNode *Node) {
Chris Lattnerce872152006-03-19 06:31:19 +00005326
5327 // If the only non-undef value is the low element, turn this into a
Chris Lattner87100e02006-03-20 01:52:29 +00005328 // SCALAR_TO_VECTOR node. If this is { X, X, X, X }, determine X.
Evan Cheng033e6812006-03-24 01:17:21 +00005329 unsigned NumElems = Node->getNumOperands();
Chris Lattnerce872152006-03-19 06:31:19 +00005330 bool isOnlyLowElement = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005331 SDValue SplatValue = Node->getOperand(0);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005332
Dan Gohman475871a2008-07-27 21:46:04 +00005333 // FIXME: it would be far nicer to change this into map<SDValue,uint64_t>
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005334 // and use a bitmask instead of a list of elements.
Dan Gohman475871a2008-07-27 21:46:04 +00005335 std::map<SDValue, std::vector<unsigned> > Values;
Evan Cheng033e6812006-03-24 01:17:21 +00005336 Values[SplatValue].push_back(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005337 bool isConstant = true;
5338 if (!isa<ConstantFPSDNode>(SplatValue) && !isa<ConstantSDNode>(SplatValue) &&
5339 SplatValue.getOpcode() != ISD::UNDEF)
5340 isConstant = false;
5341
Evan Cheng033e6812006-03-24 01:17:21 +00005342 for (unsigned i = 1; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00005343 SDValue V = Node->getOperand(i);
Chris Lattner89a1b382006-04-19 23:17:50 +00005344 Values[V].push_back(i);
Evan Cheng033e6812006-03-24 01:17:21 +00005345 if (V.getOpcode() != ISD::UNDEF)
Chris Lattnerce872152006-03-19 06:31:19 +00005346 isOnlyLowElement = false;
Evan Cheng033e6812006-03-24 01:17:21 +00005347 if (SplatValue != V)
Dan Gohman475871a2008-07-27 21:46:04 +00005348 SplatValue = SDValue(0,0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005349
5350 // If this isn't a constant element or an undef, we can't use a constant
5351 // pool load.
5352 if (!isa<ConstantFPSDNode>(V) && !isa<ConstantSDNode>(V) &&
5353 V.getOpcode() != ISD::UNDEF)
5354 isConstant = false;
Chris Lattnerce872152006-03-19 06:31:19 +00005355 }
5356
5357 if (isOnlyLowElement) {
5358 // If the low element is an undef too, then this whole things is an undef.
5359 if (Node->getOperand(0).getOpcode() == ISD::UNDEF)
5360 return DAG.getNode(ISD::UNDEF, Node->getValueType(0));
5361 // Otherwise, turn this into a scalar_to_vector node.
5362 return DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0),
5363 Node->getOperand(0));
5364 }
5365
Chris Lattner2eb86532006-03-24 07:29:17 +00005366 // If all elements are constants, create a load from the constant pool.
5367 if (isConstant) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005368 MVT VT = Node->getValueType(0);
Chris Lattner2eb86532006-03-24 07:29:17 +00005369 std::vector<Constant*> CV;
5370 for (unsigned i = 0, e = NumElems; i != e; ++i) {
5371 if (ConstantFPSDNode *V =
5372 dyn_cast<ConstantFPSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005373 CV.push_back(const_cast<ConstantFP *>(V->getConstantFPValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005374 } else if (ConstantSDNode *V =
Chris Lattner02a260a2008-04-20 00:41:09 +00005375 dyn_cast<ConstantSDNode>(Node->getOperand(i))) {
Dan Gohman4fbd7962008-09-12 18:08:03 +00005376 CV.push_back(const_cast<ConstantInt *>(V->getConstantIntValue()));
Chris Lattner2eb86532006-03-24 07:29:17 +00005377 } else {
5378 assert(Node->getOperand(i).getOpcode() == ISD::UNDEF);
Chris Lattner02a260a2008-04-20 00:41:09 +00005379 const Type *OpNTy =
Duncan Sands83ec4b62008-06-06 12:08:01 +00005380 Node->getOperand(0).getValueType().getTypeForMVT();
Chris Lattner2eb86532006-03-24 07:29:17 +00005381 CV.push_back(UndefValue::get(OpNTy));
5382 }
5383 }
Reid Spencer9d6565a2007-02-15 02:26:10 +00005384 Constant *CP = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00005385 SDValue CPIdx = DAG.getConstantPool(CP, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005386 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Dan Gohman69de1932008-02-06 22:27:42 +00005387 return DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005388 PseudoSourceValue::getConstantPool(), 0,
5389 false, Alignment);
Chris Lattner2eb86532006-03-24 07:29:17 +00005390 }
5391
Gabor Greifba36cb52008-08-28 21:40:38 +00005392 if (SplatValue.getNode()) { // Splat of one value?
Chris Lattner87100e02006-03-20 01:52:29 +00005393 // Build the shuffle constant vector: <0, 0, 0, 0>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005394 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman475871a2008-07-27 21:46:04 +00005395 SDValue Zero = DAG.getConstant(0, MaskVT.getVectorElementType());
5396 std::vector<SDValue> ZeroVec(NumElems, Zero);
5397 SDValue SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005398 &ZeroVec[0], ZeroVec.size());
Chris Lattner87100e02006-03-20 01:52:29 +00005399
5400 // If the target supports VECTOR_SHUFFLE and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005401 if (isShuffleLegal(Node->getValueType(0), SplatMask)) {
Chris Lattner87100e02006-03-20 01:52:29 +00005402 // Get the splatted value into the low element of a vector register.
Dan Gohman475871a2008-07-27 21:46:04 +00005403 SDValue LowValVec =
Chris Lattner87100e02006-03-20 01:52:29 +00005404 DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), SplatValue);
5405
5406 // Return shuffle(LowValVec, undef, <0,0,0,0>)
5407 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), LowValVec,
5408 DAG.getNode(ISD::UNDEF, Node->getValueType(0)),
5409 SplatMask);
5410 }
5411 }
5412
Evan Cheng033e6812006-03-24 01:17:21 +00005413 // If there are only two unique elements, we may be able to turn this into a
5414 // vector shuffle.
5415 if (Values.size() == 2) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005416 // Get the two values in deterministic order.
Dan Gohman475871a2008-07-27 21:46:04 +00005417 SDValue Val1 = Node->getOperand(1);
5418 SDValue Val2;
5419 std::map<SDValue, std::vector<unsigned> >::iterator MI = Values.begin();
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005420 if (MI->first != Val1)
5421 Val2 = MI->first;
5422 else
5423 Val2 = (++MI)->first;
5424
5425 // If Val1 is an undef, make sure end ends up as Val2, to ensure that our
5426 // vector shuffle has the undef vector on the RHS.
5427 if (Val1.getOpcode() == ISD::UNDEF)
5428 std::swap(Val1, Val2);
5429
Evan Cheng033e6812006-03-24 01:17:21 +00005430 // Build the shuffle constant vector: e.g. <0, 4, 0, 4>
Duncan Sands83ec4b62008-06-06 12:08:01 +00005431 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
5432 MVT MaskEltVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005433 std::vector<SDValue> MaskVec(NumElems);
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005434
5435 // Set elements of the shuffle mask for Val1.
5436 std::vector<unsigned> &Val1Elts = Values[Val1];
5437 for (unsigned i = 0, e = Val1Elts.size(); i != e; ++i)
5438 MaskVec[Val1Elts[i]] = DAG.getConstant(0, MaskEltVT);
5439
5440 // Set elements of the shuffle mask for Val2.
5441 std::vector<unsigned> &Val2Elts = Values[Val2];
5442 for (unsigned i = 0, e = Val2Elts.size(); i != e; ++i)
5443 if (Val2.getOpcode() != ISD::UNDEF)
5444 MaskVec[Val2Elts[i]] = DAG.getConstant(NumElems, MaskEltVT);
5445 else
5446 MaskVec[Val2Elts[i]] = DAG.getNode(ISD::UNDEF, MaskEltVT);
5447
Dan Gohman475871a2008-07-27 21:46:04 +00005448 SDValue ShuffleMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005449 &MaskVec[0], MaskVec.size());
Evan Cheng033e6812006-03-24 01:17:21 +00005450
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005451 // If the target supports SCALAR_TO_VECTOR and this shuffle mask, use it.
Chris Lattner4352cc92006-04-04 17:23:26 +00005452 if (TLI.isOperationLegal(ISD::SCALAR_TO_VECTOR, Node->getValueType(0)) &&
5453 isShuffleLegal(Node->getValueType(0), ShuffleMask)) {
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005454 Val1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val1);
5455 Val2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, Node->getValueType(0), Val2);
Dan Gohman475871a2008-07-27 21:46:04 +00005456 SDValue Ops[] = { Val1, Val2, ShuffleMask };
Evan Cheng033e6812006-03-24 01:17:21 +00005457
5458 // Return shuffle(LoValVec, HiValVec, <0,1,0,1>)
Chris Lattnerf9d95c82008-03-09 00:29:42 +00005459 return DAG.getNode(ISD::VECTOR_SHUFFLE, Node->getValueType(0), Ops, 3);
Evan Cheng033e6812006-03-24 01:17:21 +00005460 }
5461 }
Chris Lattnerce872152006-03-19 06:31:19 +00005462
5463 // Otherwise, we can't handle this case efficiently. Allocate a sufficiently
5464 // aligned object on the stack, store each element into it, then load
5465 // the result as a vector.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005466 MVT VT = Node->getValueType(0);
Chris Lattnerce872152006-03-19 06:31:19 +00005467 // Create the stack frame object.
Dan Gohman475871a2008-07-27 21:46:04 +00005468 SDValue FIPtr = DAG.CreateStackTemporary(VT);
Chris Lattnerce872152006-03-19 06:31:19 +00005469
5470 // Emit a store of each element to the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00005471 SmallVector<SDValue, 8> Stores;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005472 unsigned TypeByteSize = Node->getOperand(0).getValueType().getSizeInBits()/8;
Chris Lattnerce872152006-03-19 06:31:19 +00005473 // Store (in the right endianness) the elements to memory.
5474 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
5475 // Ignore undef elements.
5476 if (Node->getOperand(i).getOpcode() == ISD::UNDEF) continue;
5477
Chris Lattner841c8822006-03-22 01:46:54 +00005478 unsigned Offset = TypeByteSize*i;
Chris Lattnerce872152006-03-19 06:31:19 +00005479
Dan Gohman475871a2008-07-27 21:46:04 +00005480 SDValue Idx = DAG.getConstant(Offset, FIPtr.getValueType());
Chris Lattnerce872152006-03-19 06:31:19 +00005481 Idx = DAG.getNode(ISD::ADD, FIPtr.getValueType(), FIPtr, Idx);
5482
Evan Cheng786225a2006-10-05 23:01:46 +00005483 Stores.push_back(DAG.getStore(DAG.getEntryNode(), Node->getOperand(i), Idx,
Evan Cheng8b2794a2006-10-13 21:14:26 +00005484 NULL, 0));
Chris Lattnerce872152006-03-19 06:31:19 +00005485 }
5486
Dan Gohman475871a2008-07-27 21:46:04 +00005487 SDValue StoreChain;
Chris Lattnerce872152006-03-19 06:31:19 +00005488 if (!Stores.empty()) // Not all undef elements?
Chris Lattnerbd564bf2006-08-08 02:23:42 +00005489 StoreChain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5490 &Stores[0], Stores.size());
Chris Lattnerce872152006-03-19 06:31:19 +00005491 else
5492 StoreChain = DAG.getEntryNode();
5493
5494 // Result is a load from the stack slot.
Evan Cheng466685d2006-10-09 20:57:25 +00005495 return DAG.getLoad(VT, StoreChain, FIPtr, NULL, 0);
Chris Lattnerce872152006-03-19 06:31:19 +00005496}
5497
Chris Lattner5b359c62005-04-02 04:00:59 +00005498void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
Dan Gohman475871a2008-07-27 21:46:04 +00005499 SDValue Op, SDValue Amt,
5500 SDValue &Lo, SDValue &Hi) {
Chris Lattner5b359c62005-04-02 04:00:59 +00005501 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00005502 SDValue LHSL, LHSH;
Chris Lattner5b359c62005-04-02 04:00:59 +00005503 ExpandOp(Op, LHSL, LHSH);
5504
Dan Gohman475871a2008-07-27 21:46:04 +00005505 SDValue Ops[] = { LHSL, LHSH, Amt };
Duncan Sands83ec4b62008-06-06 12:08:01 +00005506 MVT VT = LHSL.getValueType();
Chris Lattnerf9f37fc2006-08-14 23:53:35 +00005507 Lo = DAG.getNode(NodeOp, DAG.getNodeValueTypes(VT, VT), 2, Ops, 3);
Chris Lattner5b359c62005-04-02 04:00:59 +00005508 Hi = Lo.getValue(1);
5509}
5510
5511
Chris Lattnere34b3962005-01-19 04:19:40 +00005512/// ExpandShift - Try to find a clever way to expand this shift operation out to
5513/// smaller elements. If we can't find a way that is more efficient than a
5514/// libcall on this target, return false. Otherwise, return true with the
5515/// low-parts expanded into Lo and Hi.
Dan Gohman475871a2008-07-27 21:46:04 +00005516bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDValue Op,SDValue Amt,
5517 SDValue &Lo, SDValue &Hi) {
Chris Lattnere34b3962005-01-19 04:19:40 +00005518 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
5519 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005520
Duncan Sands83ec4b62008-06-06 12:08:01 +00005521 MVT NVT = TLI.getTypeToTransformTo(Op.getValueType());
Dan Gohman475871a2008-07-27 21:46:04 +00005522 SDValue ShAmt = LegalizeOp(Amt);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005523 MVT ShTy = ShAmt.getValueType();
5524 unsigned ShBits = ShTy.getSizeInBits();
5525 unsigned VTBits = Op.getValueType().getSizeInBits();
5526 unsigned NVTBits = NVT.getSizeInBits();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005527
Chris Lattner7a3c8552007-10-14 20:35:12 +00005528 // Handle the case when Amt is an immediate.
Gabor Greifba36cb52008-08-28 21:40:38 +00005529 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.getNode())) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005530 unsigned Cst = CN->getZExtValue();
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005531 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005532 SDValue InL, InH;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005533 ExpandOp(Op, InL, InH);
5534 switch(Opc) {
5535 case ISD::SHL:
5536 if (Cst > VTBits) {
5537 Lo = DAG.getConstant(0, NVT);
5538 Hi = DAG.getConstant(0, NVT);
5539 } else if (Cst > NVTBits) {
5540 Lo = DAG.getConstant(0, NVT);
5541 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005542 } else if (Cst == NVTBits) {
5543 Lo = DAG.getConstant(0, NVT);
5544 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005545 } else {
5546 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
5547 Hi = DAG.getNode(ISD::OR, NVT,
5548 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
5549 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
5550 }
5551 return true;
5552 case ISD::SRL:
5553 if (Cst > VTBits) {
5554 Lo = DAG.getConstant(0, NVT);
5555 Hi = DAG.getConstant(0, NVT);
5556 } else if (Cst > NVTBits) {
5557 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
5558 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00005559 } else if (Cst == NVTBits) {
5560 Lo = InH;
5561 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005562 } else {
5563 Lo = DAG.getNode(ISD::OR, NVT,
5564 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5565 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5566 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
5567 }
5568 return true;
5569 case ISD::SRA:
5570 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005571 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005572 DAG.getConstant(NVTBits-1, ShTy));
5573 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00005574 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005575 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00005576 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005577 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00005578 } else if (Cst == NVTBits) {
5579 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00005580 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00005581 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005582 } else {
5583 Lo = DAG.getNode(ISD::OR, NVT,
5584 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
5585 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
5586 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
5587 }
5588 return true;
5589 }
5590 }
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005591
5592 // Okay, the shift amount isn't constant. However, if we can tell that it is
5593 // >= 32 or < 32, we can still simplify it, without knowing the actual value.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005594 APInt Mask = APInt::getHighBitsSet(ShBits, ShBits - Log2_32(NVTBits));
5595 APInt KnownZero, KnownOne;
Dan Gohmanea859be2007-06-22 14:59:07 +00005596 DAG.ComputeMaskedBits(Amt, Mask, KnownZero, KnownOne);
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005597
Dan Gohman9e255b72008-02-22 01:12:31 +00005598 // If we know that if any of the high bits of the shift amount are one, then
5599 // we can do this as a couple of simple shifts.
Dan Gohman91dc17b2008-02-20 16:57:27 +00005600 if (KnownOne.intersects(Mask)) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005601 // Mask out the high bit, which we know is set.
5602 Amt = DAG.getNode(ISD::AND, Amt.getValueType(), Amt,
Dan Gohman91dc17b2008-02-20 16:57:27 +00005603 DAG.getConstant(~Mask, Amt.getValueType()));
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005604
5605 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005606 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005607 ExpandOp(Op, InL, InH);
5608 switch(Opc) {
5609 case ISD::SHL:
5610 Lo = DAG.getConstant(0, NVT); // Low part is zero.
5611 Hi = DAG.getNode(ISD::SHL, NVT, InL, Amt); // High part from Lo part.
5612 return true;
5613 case ISD::SRL:
5614 Hi = DAG.getConstant(0, NVT); // Hi part is zero.
5615 Lo = DAG.getNode(ISD::SRL, NVT, InH, Amt); // Lo part from Hi part.
5616 return true;
5617 case ISD::SRA:
5618 Hi = DAG.getNode(ISD::SRA, NVT, InH, // Sign extend high part.
5619 DAG.getConstant(NVTBits-1, Amt.getValueType()));
5620 Lo = DAG.getNode(ISD::SRA, NVT, InH, Amt); // Lo part from Hi part.
5621 return true;
5622 }
5623 }
5624
Dan Gohman9e255b72008-02-22 01:12:31 +00005625 // If we know that the high bits of the shift amount are all zero, then we can
5626 // do this as a couple of simple shifts.
5627 if ((KnownZero & Mask) == Mask) {
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005628 // Compute 32-amt.
Dan Gohman475871a2008-07-27 21:46:04 +00005629 SDValue Amt2 = DAG.getNode(ISD::SUB, Amt.getValueType(),
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005630 DAG.getConstant(NVTBits, Amt.getValueType()),
5631 Amt);
5632
5633 // Expand the incoming operand to be shifted, so that we have its parts
Dan Gohman475871a2008-07-27 21:46:04 +00005634 SDValue InL, InH;
Chris Lattner0ea26ca2006-09-20 03:38:48 +00005635 ExpandOp(Op, InL, InH);
5636 switch(Opc) {
5637 case ISD::SHL:
5638 Lo = DAG.getNode(ISD::SHL, NVT, InL, Amt);
5639 Hi = DAG.getNode(ISD::OR, NVT,
5640 DAG.getNode(ISD::SHL, NVT, InH, Amt),
5641 DAG.getNode(ISD::SRL, NVT, InL, Amt2));
5642 return true;
5643 case ISD::SRL:
5644 Hi = DAG.getNode(ISD::SRL, NVT, InH, Amt);
5645 Lo = DAG.getNode(ISD::OR, NVT,
5646 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5647 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5648 return true;
5649 case ISD::SRA:
5650 Hi = DAG.getNode(ISD::SRA, NVT, InH, Amt);
5651 Lo = DAG.getNode(ISD::OR, NVT,
5652 DAG.getNode(ISD::SRL, NVT, InL, Amt),
5653 DAG.getNode(ISD::SHL, NVT, InH, Amt2));
5654 return true;
5655 }
5656 }
5657
Nate Begemanf1fe32e2005-04-06 21:13:14 +00005658 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00005659}
Chris Lattner77e77a62005-01-21 06:05:23 +00005660
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005661
Chris Lattner77e77a62005-01-21 06:05:23 +00005662// ExpandLibCall - Expand a node into a call to a libcall. If the result value
5663// does not fit into a register, return the lo part and set the hi part to the
5664// by-reg argument. If it does fit into a single register, return the result
5665// and leave the Hi part unset.
Dan Gohman475871a2008-07-27 21:46:04 +00005666SDValue SelectionDAGLegalize::ExpandLibCall(RTLIB::Libcall LC, SDNode *Node,
5667 bool isSigned, SDValue &Hi) {
Chris Lattner6831a812006-02-13 09:18:02 +00005668 assert(!IsLegalizingCall && "Cannot overlap legalization of calls!");
5669 // The input chain to this libcall is the entry node of the function.
5670 // Legalizing the call will automatically add the previous call to the
5671 // dependence.
Dan Gohman475871a2008-07-27 21:46:04 +00005672 SDValue InChain = DAG.getEntryNode();
Chris Lattner6831a812006-02-13 09:18:02 +00005673
Chris Lattner77e77a62005-01-21 06:05:23 +00005674 TargetLowering::ArgListTy Args;
Reid Spencer47857812006-12-31 05:55:36 +00005675 TargetLowering::ArgListEntry Entry;
Chris Lattner77e77a62005-01-21 06:05:23 +00005676 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005677 MVT ArgVT = Node->getOperand(i).getValueType();
5678 const Type *ArgTy = ArgVT.getTypeForMVT();
Reid Spencer47857812006-12-31 05:55:36 +00005679 Entry.Node = Node->getOperand(i); Entry.Ty = ArgTy;
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +00005680 Entry.isSExt = isSigned;
Duncan Sands00fee652008-02-14 17:28:50 +00005681 Entry.isZExt = !isSigned;
Reid Spencer47857812006-12-31 05:55:36 +00005682 Args.push_back(Entry);
Chris Lattner77e77a62005-01-21 06:05:23 +00005683 }
Bill Wendling056292f2008-09-16 21:48:12 +00005684 SDValue Callee = DAG.getExternalSymbol(TLI.getLibcallName(LC),
Mon P Wang0c397192008-10-30 08:01:45 +00005685 TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00005686
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005687 // Splice the libcall in wherever FindInputOutputChains tells us to.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005688 const Type *RetTy = Node->getValueType(0).getTypeForMVT();
Dan Gohman475871a2008-07-27 21:46:04 +00005689 std::pair<SDValue,SDValue> CallInfo =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005690 TLI.LowerCallTo(InChain, RetTy, isSigned, !isSigned, false, false,
5691 CallingConv::C, false, Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00005692
Chris Lattner6831a812006-02-13 09:18:02 +00005693 // Legalize the call sequence, starting with the chain. This will advance
5694 // the LastCALLSEQ_END to the legalized version of the CALLSEQ_END node that
5695 // was added by LowerCallTo (guaranteeing proper serialization of calls).
5696 LegalizeOp(CallInfo.second);
Dan Gohman475871a2008-07-27 21:46:04 +00005697 SDValue Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005698 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00005699 default: assert(0 && "Unknown thing");
5700 case Legal:
Chris Lattner456a93a2006-01-28 07:39:30 +00005701 Result = CallInfo.first;
Chris Lattner99c25b82005-09-02 20:26:58 +00005702 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005703 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00005704 ExpandOp(CallInfo.first, Result, Hi);
Chris Lattner99c25b82005-09-02 20:26:58 +00005705 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00005706 }
Chris Lattner99c25b82005-09-02 20:26:58 +00005707 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005708}
5709
Dan Gohman7f8613e2008-08-14 20:04:46 +00005710/// LegalizeINT_TO_FP - Legalize a [US]INT_TO_FP operation.
5711///
5712SDValue SelectionDAGLegalize::
5713LegalizeINT_TO_FP(SDValue Result, bool isSigned, MVT DestTy, SDValue Op) {
5714 bool isCustom = false;
5715 SDValue Tmp1;
5716 switch (getTypeAction(Op.getValueType())) {
5717 case Legal:
5718 switch (TLI.getOperationAction(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5719 Op.getValueType())) {
5720 default: assert(0 && "Unknown operation action!");
5721 case TargetLowering::Custom:
5722 isCustom = true;
5723 // FALLTHROUGH
5724 case TargetLowering::Legal:
5725 Tmp1 = LegalizeOp(Op);
Gabor Greifba36cb52008-08-28 21:40:38 +00005726 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005727 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5728 else
5729 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5730 DestTy, Tmp1);
5731 if (isCustom) {
5732 Tmp1 = TLI.LowerOperation(Result, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005733 if (Tmp1.getNode()) Result = Tmp1;
Dan Gohman7f8613e2008-08-14 20:04:46 +00005734 }
5735 break;
5736 case TargetLowering::Expand:
5737 Result = ExpandLegalINT_TO_FP(isSigned, LegalizeOp(Op), DestTy);
5738 break;
5739 case TargetLowering::Promote:
5740 Result = PromoteLegalINT_TO_FP(LegalizeOp(Op), DestTy, isSigned);
5741 break;
5742 }
5743 break;
5744 case Expand:
5745 Result = ExpandIntToFP(isSigned, DestTy, Op);
5746 break;
5747 case Promote:
5748 Tmp1 = PromoteOp(Op);
5749 if (isSigned) {
5750 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp1.getValueType(),
5751 Tmp1, DAG.getValueType(Op.getValueType()));
5752 } else {
5753 Tmp1 = DAG.getZeroExtendInReg(Tmp1,
5754 Op.getValueType());
5755 }
Gabor Greifba36cb52008-08-28 21:40:38 +00005756 if (Result.getNode())
Dan Gohman7f8613e2008-08-14 20:04:46 +00005757 Result = DAG.UpdateNodeOperands(Result, Tmp1);
5758 else
5759 Result = DAG.getNode(isSigned ? ISD::SINT_TO_FP : ISD::UINT_TO_FP,
5760 DestTy, Tmp1);
5761 Result = LegalizeOp(Result); // The 'op' is not necessarily legal!
5762 break;
5763 }
5764 return Result;
5765}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00005766
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005767/// ExpandIntToFP - Expand a [US]INT_TO_FP operation.
5768///
Dan Gohman475871a2008-07-27 21:46:04 +00005769SDValue SelectionDAGLegalize::
5770ExpandIntToFP(bool isSigned, MVT DestTy, SDValue Source) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005771 MVT SourceVT = Source.getValueType();
Dan Gohman034f60e2008-03-11 01:59:03 +00005772 bool ExpandSource = getTypeAction(SourceVT) == Expand;
Chris Lattner77e77a62005-01-21 06:05:23 +00005773
Dan Gohman7f8613e2008-08-14 20:04:46 +00005774 // Expand unsupported int-to-fp vector casts by unrolling them.
5775 if (DestTy.isVector()) {
5776 if (!ExpandSource)
5777 return LegalizeOp(UnrollVectorOp(Source));
5778 MVT DestEltTy = DestTy.getVectorElementType();
5779 if (DestTy.getVectorNumElements() == 1) {
5780 SDValue Scalar = ScalarizeVectorOp(Source);
5781 SDValue Result = LegalizeINT_TO_FP(SDValue(), isSigned,
5782 DestEltTy, Scalar);
5783 return DAG.getNode(ISD::BUILD_VECTOR, DestTy, Result);
5784 }
5785 SDValue Lo, Hi;
5786 SplitVectorOp(Source, Lo, Hi);
5787 MVT SplitDestTy = MVT::getVectorVT(DestEltTy,
5788 DestTy.getVectorNumElements() / 2);
5789 SDValue LoResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Lo);
5790 SDValue HiResult = LegalizeINT_TO_FP(SDValue(), isSigned, SplitDestTy, Hi);
Evan Chengefa53392008-10-13 18:46:18 +00005791 return LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, DestTy, LoResult,
5792 HiResult));
Dan Gohman7f8613e2008-08-14 20:04:46 +00005793 }
5794
Evan Cheng9845eb52008-04-01 02:18:22 +00005795 // Special case for i32 source to take advantage of UINTTOFP_I32_F32, etc.
5796 if (!isSigned && SourceVT != MVT::i32) {
Dan Gohman34bc1782008-03-05 02:07:31 +00005797 // The integer value loaded will be incorrectly if the 'sign bit' of the
Chris Lattnere9c35e72005-04-13 05:09:42 +00005798 // incoming integer is set. To handle this, we dynamically test to see if
5799 // it is set, and, if so, add a fudge factor.
Dan Gohman475871a2008-07-27 21:46:04 +00005800 SDValue Hi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005801 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005802 SDValue Lo;
Dan Gohman034f60e2008-03-11 01:59:03 +00005803 ExpandOp(Source, Lo, Hi);
5804 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, Lo, Hi);
5805 } else {
5806 // The comparison for the sign bit will use the entire operand.
5807 Hi = Source;
5808 }
Chris Lattnere9c35e72005-04-13 05:09:42 +00005809
Dale Johannesen53997b02008-11-04 20:52:49 +00005810 // Check to see if the target has a custom way to lower this. If so, use
5811 // it. (Note we've already expanded the operand in this case.)
Dale Johannesen1c15bf52008-10-21 20:50:01 +00005812 switch (TLI.getOperationAction(ISD::UINT_TO_FP, SourceVT)) {
5813 default: assert(0 && "This action not implemented for this operation!");
5814 case TargetLowering::Legal:
5815 case TargetLowering::Expand:
5816 break; // This case is handled below.
5817 case TargetLowering::Custom: {
5818 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::UINT_TO_FP, DestTy,
5819 Source), DAG);
5820 if (NV.getNode())
5821 return LegalizeOp(NV);
5822 break; // The target decided this was legal after all
5823 }
5824 }
5825
Chris Lattner66de05b2005-05-13 04:45:13 +00005826 // If this is unsigned, and not supported, first perform the conversion to
5827 // signed, then adjust the result if the sign bit is set.
Dan Gohman475871a2008-07-27 21:46:04 +00005828 SDValue SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner66de05b2005-05-13 04:45:13 +00005829
Dan Gohman475871a2008-07-27 21:46:04 +00005830 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Hi), Hi,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00005831 DAG.getConstant(0, Hi.getValueType()),
5832 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005833 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5834 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattnere9c35e72005-04-13 05:09:42 +00005835 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00005836 uint64_t FF = 0x5f800000ULL;
5837 if (TLI.isLittleEndian()) FF <<= 32;
Dan Gohman34bc1782008-03-05 02:07:31 +00005838 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00005839
Dan Gohman475871a2008-07-27 21:46:04 +00005840 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005841 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattnere9c35e72005-04-13 05:09:42 +00005842 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005843 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005844 SDValue FudgeInReg;
Chris Lattnere9c35e72005-04-13 05:09:42 +00005845 if (DestTy == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005846 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005847 PseudoSourceValue::getConstantPool(), 0,
5848 false, Alignment);
Duncan Sands8e4eb092008-06-08 20:54:56 +00005849 else if (DestTy.bitsGT(MVT::f32))
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005850 // FIXME: Avoid the extend by construction the right constantpool?
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005851 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, DestTy, DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00005852 CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005853 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00005854 MVT::f32, false, Alignment);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005855 else
5856 assert(0 && "Unexpected conversion");
5857
Duncan Sands83ec4b62008-06-06 12:08:01 +00005858 MVT SCVT = SignedConv.getValueType();
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005859 if (SCVT != DestTy) {
5860 // Destination type needs to be expanded as well. The FADD now we are
5861 // constructing will be expanded into a libcall.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005862 if (SCVT.getSizeInBits() != DestTy.getSizeInBits()) {
5863 assert(SCVT.getSizeInBits() * 2 == DestTy.getSizeInBits());
Dan Gohmand91446d2008-03-05 01:08:17 +00005864 SignedConv = DAG.getNode(ISD::BUILD_PAIR, DestTy,
Evan Cheng4c6cfad2007-04-27 07:33:31 +00005865 SignedConv, SignedConv.getValue(1));
5866 }
5867 SignedConv = DAG.getNode(ISD::BIT_CONVERT, DestTy, SignedConv);
5868 }
Chris Lattner473a9902005-09-29 06:44:39 +00005869 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00005870 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00005871
Chris Lattnera88a2602005-05-14 05:33:54 +00005872 // Check to see if the target has a custom way to lower this. If so, use it.
Dan Gohmand91446d2008-03-05 01:08:17 +00005873 switch (TLI.getOperationAction(ISD::SINT_TO_FP, SourceVT)) {
Chris Lattnera88a2602005-05-14 05:33:54 +00005874 default: assert(0 && "This action not implemented for this operation!");
5875 case TargetLowering::Legal:
5876 case TargetLowering::Expand:
5877 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00005878 case TargetLowering::Custom: {
Dan Gohman475871a2008-07-27 21:46:04 +00005879 SDValue NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
Chris Lattner07dffd62005-08-26 00:14:16 +00005880 Source), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00005881 if (NV.getNode())
Chris Lattner07dffd62005-08-26 00:14:16 +00005882 return LegalizeOp(NV);
5883 break; // The target decided this was legal after all
5884 }
Chris Lattnera88a2602005-05-14 05:33:54 +00005885 }
5886
Chris Lattner13689e22005-05-12 07:00:44 +00005887 // Expand the source, then glue it back together for the call. We must expand
5888 // the source in case it is shared (this pass of legalize must traverse it).
Dan Gohman034f60e2008-03-11 01:59:03 +00005889 if (ExpandSource) {
Dan Gohman475871a2008-07-27 21:46:04 +00005890 SDValue SrcLo, SrcHi;
Dan Gohman034f60e2008-03-11 01:59:03 +00005891 ExpandOp(Source, SrcLo, SrcHi);
5892 Source = DAG.getNode(ISD::BUILD_PAIR, SourceVT, SrcLo, SrcHi);
5893 }
Chris Lattner13689e22005-05-12 07:00:44 +00005894
Duncan Sandsb2ff8852008-07-17 02:36:29 +00005895 RTLIB::Libcall LC = isSigned ?
5896 RTLIB::getSINTTOFP(SourceVT, DestTy) :
5897 RTLIB::getUINTTOFP(SourceVT, DestTy);
5898 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unknown int value type");
5899
Chris Lattner6831a812006-02-13 09:18:02 +00005900 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Dan Gohman475871a2008-07-27 21:46:04 +00005901 SDValue HiPart;
Gabor Greifba36cb52008-08-28 21:40:38 +00005902 SDValue Result = ExpandLibCall(LC, Source.getNode(), isSigned, HiPart);
5903 if (Result.getValueType() != DestTy && HiPart.getNode())
Dan Gohmana2e94852008-03-10 23:03:31 +00005904 Result = DAG.getNode(ISD::BUILD_PAIR, DestTy, Result, HiPart);
5905 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00005906}
Misha Brukmanedf128a2005-04-21 22:36:52 +00005907
Chris Lattner22cde6a2006-01-28 08:25:58 +00005908/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
5909/// INT_TO_FP operation of the specified operand when the target requests that
5910/// we expand it. At this point, we know that the result and operand types are
5911/// legal for the target.
Dan Gohman475871a2008-07-27 21:46:04 +00005912SDValue SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
5913 SDValue Op0,
5914 MVT DestVT) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005915 if (Op0.getValueType() == MVT::i32) {
5916 // simple 32-bit [signed|unsigned] integer to float/double expansion
5917
Chris Lattner23594d42008-01-16 07:03:22 +00005918 // Get the stack frame index of a 8 byte buffer.
Dan Gohman475871a2008-07-27 21:46:04 +00005919 SDValue StackSlot = DAG.CreateStackTemporary(MVT::f64);
Chris Lattner23594d42008-01-16 07:03:22 +00005920
Chris Lattner22cde6a2006-01-28 08:25:58 +00005921 // word offset constant for Hi/Lo address computation
Dan Gohman475871a2008-07-27 21:46:04 +00005922 SDValue WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
Chris Lattner22cde6a2006-01-28 08:25:58 +00005923 // set up Hi and Lo (into buffer) address based on endian
Dan Gohman475871a2008-07-27 21:46:04 +00005924 SDValue Hi = StackSlot;
5925 SDValue Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot,WordOff);
Chris Lattner408c4282006-03-23 05:29:04 +00005926 if (TLI.isLittleEndian())
5927 std::swap(Hi, Lo);
5928
Chris Lattner22cde6a2006-01-28 08:25:58 +00005929 // if signed map to unsigned space
Dan Gohman475871a2008-07-27 21:46:04 +00005930 SDValue Op0Mapped;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005931 if (isSigned) {
5932 // constant used to invert sign bit (signed to unsigned mapping)
Dan Gohman475871a2008-07-27 21:46:04 +00005933 SDValue SignBit = DAG.getConstant(0x80000000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005934 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
5935 } else {
5936 Op0Mapped = Op0;
5937 }
5938 // store the lo of the constructed double - based on integer input
Dan Gohman475871a2008-07-27 21:46:04 +00005939 SDValue Store1 = DAG.getStore(DAG.getEntryNode(),
Evan Cheng8b2794a2006-10-13 21:14:26 +00005940 Op0Mapped, Lo, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005941 // initial hi portion of constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005942 SDValue InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005943 // store the hi of the constructed double - biased exponent
Dan Gohman475871a2008-07-27 21:46:04 +00005944 SDValue Store2=DAG.getStore(Store1, InitialHi, Hi, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005945 // load the constructed double
Dan Gohman475871a2008-07-27 21:46:04 +00005946 SDValue Load = DAG.getLoad(MVT::f64, Store2, StackSlot, NULL, 0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005947 // FP constant to bias correct the final result
Dan Gohman475871a2008-07-27 21:46:04 +00005948 SDValue Bias = DAG.getConstantFP(isSigned ?
Chris Lattner22cde6a2006-01-28 08:25:58 +00005949 BitsToDouble(0x4330000080000000ULL)
5950 : BitsToDouble(0x4330000000000000ULL),
5951 MVT::f64);
5952 // subtract the bias
Dan Gohman475871a2008-07-27 21:46:04 +00005953 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005954 // final result
Dan Gohman475871a2008-07-27 21:46:04 +00005955 SDValue Result;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005956 // handle final rounding
5957 if (DestVT == MVT::f64) {
5958 // do nothing
5959 Result = Sub;
Duncan Sands8e4eb092008-06-08 20:54:56 +00005960 } else if (DestVT.bitsLT(MVT::f64)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00005961 Result = DAG.getNode(ISD::FP_ROUND, DestVT, Sub,
5962 DAG.getIntPtrConstant(0));
Duncan Sands8e4eb092008-06-08 20:54:56 +00005963 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesen118cd9d2007-09-16 16:51:49 +00005964 Result = DAG.getNode(ISD::FP_EXTEND, DestVT, Sub);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005965 }
5966 return Result;
5967 }
5968 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Dan Gohman475871a2008-07-27 21:46:04 +00005969 SDValue Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005970
Dan Gohman475871a2008-07-27 21:46:04 +00005971 SDValue SignSet = DAG.getSetCC(TLI.getSetCCResultType(Op0), Op0,
Chris Lattner22cde6a2006-01-28 08:25:58 +00005972 DAG.getConstant(0, Op0.getValueType()),
5973 ISD::SETLT);
Dan Gohman475871a2008-07-27 21:46:04 +00005974 SDValue Zero = DAG.getIntPtrConstant(0), Four = DAG.getIntPtrConstant(4);
5975 SDValue CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
Chris Lattner22cde6a2006-01-28 08:25:58 +00005976 SignSet, Four, Zero);
5977
5978 // If the sign bit of the integer is set, the large number will be treated
5979 // as a negative number. To counteract this, the dynamic code adds an
5980 // offset depending on the data type.
5981 uint64_t FF;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005982 switch (Op0.getValueType().getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00005983 default: assert(0 && "Unsupported integer type!");
5984 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
5985 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
5986 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
5987 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
5988 }
5989 if (TLI.isLittleEndian()) FF <<= 32;
Reid Spencer47857812006-12-31 05:55:36 +00005990 static Constant *FudgeFactor = ConstantInt::get(Type::Int64Ty, FF);
Chris Lattner22cde6a2006-01-28 08:25:58 +00005991
Dan Gohman475871a2008-07-27 21:46:04 +00005992 SDValue CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Dan Gohman50284d82008-09-16 22:05:41 +00005993 unsigned Alignment = 1 << cast<ConstantPoolSDNode>(CPIdx)->getAlignment();
Chris Lattner22cde6a2006-01-28 08:25:58 +00005994 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
Dan Gohman87a0f102008-09-22 22:40:08 +00005995 Alignment = std::min(Alignment, 4u);
Dan Gohman475871a2008-07-27 21:46:04 +00005996 SDValue FudgeInReg;
Chris Lattner22cde6a2006-01-28 08:25:58 +00005997 if (DestVT == MVT::f32)
Dan Gohman69de1932008-02-06 22:27:42 +00005998 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
Dan Gohman50284d82008-09-16 22:05:41 +00005999 PseudoSourceValue::getConstantPool(), 0,
6000 false, Alignment);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006001 else {
Dan Gohman69de1932008-02-06 22:27:42 +00006002 FudgeInReg =
6003 LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, DestVT,
6004 DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00006005 PseudoSourceValue::getConstantPool(), 0,
Dan Gohman50284d82008-09-16 22:05:41 +00006006 MVT::f32, false, Alignment));
Chris Lattner22cde6a2006-01-28 08:25:58 +00006007 }
6008
6009 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
6010}
6011
6012/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
6013/// *INT_TO_FP operation of the specified operand when the target requests that
6014/// we promote it. At this point, we know that the result and operand types are
6015/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
6016/// operation that takes a larger input.
Dan Gohman475871a2008-07-27 21:46:04 +00006017SDValue SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDValue LegalOp,
6018 MVT DestVT,
6019 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006020 // First step, figure out the appropriate *INT_TO_FP operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006021 MVT NewInTy = LegalOp.getValueType();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006022
6023 unsigned OpToUse = 0;
6024
6025 // Scan for the appropriate larger type to use.
6026 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006027 NewInTy = (MVT::SimpleValueType)(NewInTy.getSimpleVT()+1);
6028 assert(NewInTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006029
6030 // If the target supports SINT_TO_FP of this type, use it.
6031 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
6032 default: break;
6033 case TargetLowering::Legal:
6034 if (!TLI.isTypeLegal(NewInTy))
6035 break; // Can't use this datatype.
6036 // FALL THROUGH.
6037 case TargetLowering::Custom:
6038 OpToUse = ISD::SINT_TO_FP;
6039 break;
6040 }
6041 if (OpToUse) break;
6042 if (isSigned) continue;
6043
6044 // If the target supports UINT_TO_FP of this type, use it.
6045 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
6046 default: break;
6047 case TargetLowering::Legal:
6048 if (!TLI.isTypeLegal(NewInTy))
6049 break; // Can't use this datatype.
6050 // FALL THROUGH.
6051 case TargetLowering::Custom:
6052 OpToUse = ISD::UINT_TO_FP;
6053 break;
6054 }
6055 if (OpToUse) break;
6056
6057 // Otherwise, try a larger type.
6058 }
6059
6060 // Okay, we found the operation and type to use. Zero extend our input to the
6061 // desired type then run the operation on it.
6062 return DAG.getNode(OpToUse, DestVT,
6063 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
6064 NewInTy, LegalOp));
6065}
6066
6067/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
6068/// FP_TO_*INT operation of the specified operand when the target requests that
6069/// we promote it. At this point, we know that the result and operand types are
6070/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
6071/// operation that returns a larger result.
Dan Gohman475871a2008-07-27 21:46:04 +00006072SDValue SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDValue LegalOp,
6073 MVT DestVT,
6074 bool isSigned) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006075 // First step, figure out the appropriate FP_TO*INT operation to use.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006076 MVT NewOutTy = DestVT;
Chris Lattner22cde6a2006-01-28 08:25:58 +00006077
6078 unsigned OpToUse = 0;
6079
6080 // Scan for the appropriate larger type to use.
6081 while (1) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006082 NewOutTy = (MVT::SimpleValueType)(NewOutTy.getSimpleVT()+1);
6083 assert(NewOutTy.isInteger() && "Ran out of possibilities!");
Chris Lattner22cde6a2006-01-28 08:25:58 +00006084
6085 // If the target supports FP_TO_SINT returning this type, use it.
6086 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
6087 default: break;
6088 case TargetLowering::Legal:
6089 if (!TLI.isTypeLegal(NewOutTy))
6090 break; // Can't use this datatype.
6091 // FALL THROUGH.
6092 case TargetLowering::Custom:
6093 OpToUse = ISD::FP_TO_SINT;
6094 break;
6095 }
6096 if (OpToUse) break;
6097
6098 // If the target supports FP_TO_UINT of this type, use it.
6099 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
6100 default: break;
6101 case TargetLowering::Legal:
6102 if (!TLI.isTypeLegal(NewOutTy))
6103 break; // Can't use this datatype.
6104 // FALL THROUGH.
6105 case TargetLowering::Custom:
6106 OpToUse = ISD::FP_TO_UINT;
6107 break;
6108 }
6109 if (OpToUse) break;
6110
6111 // Otherwise, try a larger type.
6112 }
6113
Chris Lattner27a6c732007-11-24 07:07:01 +00006114
6115 // Okay, we found the operation and type to use.
Dan Gohman475871a2008-07-27 21:46:04 +00006116 SDValue Operation = DAG.getNode(OpToUse, NewOutTy, LegalOp);
Duncan Sands126d9072008-07-04 11:47:58 +00006117
Chris Lattner27a6c732007-11-24 07:07:01 +00006118 // If the operation produces an invalid type, it must be custom lowered. Use
6119 // the target lowering hooks to expand it. Just keep the low part of the
6120 // expanded operation, we know that we're truncating anyway.
6121 if (getTypeAction(NewOutTy) == Expand) {
Duncan Sands1607f052008-12-01 11:39:25 +00006122 SmallVector<SDValue, 2> Results;
6123 TLI.ReplaceNodeResults(Operation.getNode(), Results, DAG);
6124 assert(Results.size() == 1 && "Incorrect FP_TO_XINT lowering!");
6125 Operation = Results[0];
Chris Lattner27a6c732007-11-24 07:07:01 +00006126 }
Duncan Sands126d9072008-07-04 11:47:58 +00006127
Chris Lattner27a6c732007-11-24 07:07:01 +00006128 // Truncate the result of the extended FP_TO_*INT operation to the desired
6129 // size.
6130 return DAG.getNode(ISD::TRUNCATE, DestVT, Operation);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006131}
6132
6133/// ExpandBSWAP - Open code the operations for BSWAP of the specified operation.
6134///
Dan Gohman475871a2008-07-27 21:46:04 +00006135SDValue SelectionDAGLegalize::ExpandBSWAP(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006136 MVT VT = Op.getValueType();
6137 MVT SHVT = TLI.getShiftAmountTy();
Dan Gohman475871a2008-07-27 21:46:04 +00006138 SDValue Tmp1, Tmp2, Tmp3, Tmp4, Tmp5, Tmp6, Tmp7, Tmp8;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006139 switch (VT.getSimpleVT()) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006140 default: assert(0 && "Unhandled Expand type in BSWAP!"); abort();
6141 case MVT::i16:
6142 Tmp2 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6143 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6144 return DAG.getNode(ISD::OR, VT, Tmp1, Tmp2);
6145 case MVT::i32:
6146 Tmp4 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6147 Tmp3 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6148 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6149 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6150 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(0xFF0000, VT));
6151 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(0xFF00, VT));
6152 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6153 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6154 return DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6155 case MVT::i64:
6156 Tmp8 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(56, SHVT));
6157 Tmp7 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(40, SHVT));
6158 Tmp6 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(24, SHVT));
6159 Tmp5 = DAG.getNode(ISD::SHL, VT, Op, DAG.getConstant(8, SHVT));
6160 Tmp4 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(8, SHVT));
6161 Tmp3 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(24, SHVT));
6162 Tmp2 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(40, SHVT));
6163 Tmp1 = DAG.getNode(ISD::SRL, VT, Op, DAG.getConstant(56, SHVT));
6164 Tmp7 = DAG.getNode(ISD::AND, VT, Tmp7, DAG.getConstant(255ULL<<48, VT));
6165 Tmp6 = DAG.getNode(ISD::AND, VT, Tmp6, DAG.getConstant(255ULL<<40, VT));
6166 Tmp5 = DAG.getNode(ISD::AND, VT, Tmp5, DAG.getConstant(255ULL<<32, VT));
6167 Tmp4 = DAG.getNode(ISD::AND, VT, Tmp4, DAG.getConstant(255ULL<<24, VT));
6168 Tmp3 = DAG.getNode(ISD::AND, VT, Tmp3, DAG.getConstant(255ULL<<16, VT));
6169 Tmp2 = DAG.getNode(ISD::AND, VT, Tmp2, DAG.getConstant(255ULL<<8 , VT));
6170 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp7);
6171 Tmp6 = DAG.getNode(ISD::OR, VT, Tmp6, Tmp5);
6172 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp3);
6173 Tmp2 = DAG.getNode(ISD::OR, VT, Tmp2, Tmp1);
6174 Tmp8 = DAG.getNode(ISD::OR, VT, Tmp8, Tmp6);
6175 Tmp4 = DAG.getNode(ISD::OR, VT, Tmp4, Tmp2);
6176 return DAG.getNode(ISD::OR, VT, Tmp8, Tmp4);
6177 }
6178}
6179
6180/// ExpandBitCount - Expand the specified bitcount instruction into operations.
6181///
Dan Gohman475871a2008-07-27 21:46:04 +00006182SDValue SelectionDAGLegalize::ExpandBitCount(unsigned Opc, SDValue Op) {
Chris Lattner22cde6a2006-01-28 08:25:58 +00006183 switch (Opc) {
6184 default: assert(0 && "Cannot expand this yet!");
6185 case ISD::CTPOP: {
6186 static const uint64_t mask[6] = {
6187 0x5555555555555555ULL, 0x3333333333333333ULL,
6188 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
6189 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
6190 };
Duncan Sands83ec4b62008-06-06 12:08:01 +00006191 MVT VT = Op.getValueType();
6192 MVT ShVT = TLI.getShiftAmountTy();
6193 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006194 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
6195 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Dan Gohman475871a2008-07-27 21:46:04 +00006196 SDValue Tmp2 = DAG.getConstant(mask[i], VT);
6197 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006198 Op = DAG.getNode(ISD::ADD, VT, DAG.getNode(ISD::AND, VT, Op, Tmp2),
6199 DAG.getNode(ISD::AND, VT,
6200 DAG.getNode(ISD::SRL, VT, Op, Tmp3),Tmp2));
6201 }
6202 return Op;
6203 }
6204 case ISD::CTLZ: {
6205 // for now, we do this:
6206 // x = x | (x >> 1);
6207 // x = x | (x >> 2);
6208 // ...
6209 // x = x | (x >>16);
6210 // x = x | (x >>32); // for 64-bit input
6211 // return popcount(~x);
6212 //
6213 // but see also: http://www.hackersdelight.org/HDcode/nlz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006214 MVT VT = Op.getValueType();
6215 MVT ShVT = TLI.getShiftAmountTy();
6216 unsigned len = VT.getSizeInBits();
Chris Lattner22cde6a2006-01-28 08:25:58 +00006217 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006218 SDValue Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Chris Lattner22cde6a2006-01-28 08:25:58 +00006219 Op = DAG.getNode(ISD::OR, VT, Op, DAG.getNode(ISD::SRL, VT, Op, Tmp3));
6220 }
6221 Op = DAG.getNode(ISD::XOR, VT, Op, DAG.getConstant(~0ULL, VT));
6222 return DAG.getNode(ISD::CTPOP, VT, Op);
6223 }
6224 case ISD::CTTZ: {
6225 // for now, we use: { return popcount(~x & (x - 1)); }
6226 // unless the target has ctlz but not ctpop, in which case we use:
6227 // { return 32 - nlz(~x & (x-1)); }
6228 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Duncan Sands83ec4b62008-06-06 12:08:01 +00006229 MVT VT = Op.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00006230 SDValue Tmp2 = DAG.getConstant(~0ULL, VT);
6231 SDValue Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner22cde6a2006-01-28 08:25:58 +00006232 DAG.getNode(ISD::XOR, VT, Op, Tmp2),
6233 DAG.getNode(ISD::SUB, VT, Op, DAG.getConstant(1, VT)));
6234 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead.
6235 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
6236 TLI.isOperationLegal(ISD::CTLZ, VT))
6237 return DAG.getNode(ISD::SUB, VT,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006238 DAG.getConstant(VT.getSizeInBits(), VT),
Chris Lattner22cde6a2006-01-28 08:25:58 +00006239 DAG.getNode(ISD::CTLZ, VT, Tmp3));
6240 return DAG.getNode(ISD::CTPOP, VT, Tmp3);
6241 }
6242 }
6243}
Chris Lattnere34b3962005-01-19 04:19:40 +00006244
Dan Gohman475871a2008-07-27 21:46:04 +00006245/// ExpandOp - Expand the specified SDValue into its two component pieces
Chris Lattner3e928bb2005-01-07 07:47:09 +00006246/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
Dan Gohman929d3eb2008-10-01 15:07:49 +00006247/// LegalizedNodes map is filled in for any results that are not expanded, the
Chris Lattner3e928bb2005-01-07 07:47:09 +00006248/// ExpandedNodes map is filled in for any results that are expanded, and the
6249/// Lo/Hi values are returned.
Dan Gohman475871a2008-07-27 21:46:04 +00006250void SelectionDAGLegalize::ExpandOp(SDValue Op, SDValue &Lo, SDValue &Hi){
Duncan Sands83ec4b62008-06-06 12:08:01 +00006251 MVT VT = Op.getValueType();
6252 MVT NVT = TLI.getTypeToTransformTo(VT);
Gabor Greifba36cb52008-08-28 21:40:38 +00006253 SDNode *Node = Op.getNode();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006254 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
Duncan Sands8e4eb092008-06-08 20:54:56 +00006255 assert(((NVT.isInteger() && NVT.bitsLT(VT)) || VT.isFloatingPoint() ||
Duncan Sands83ec4b62008-06-06 12:08:01 +00006256 VT.isVector()) && "Cannot expand to FP value or to larger int value!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00006257
Chris Lattner6fdcb252005-09-02 20:32:45 +00006258 // See if we already expanded it.
Dan Gohman475871a2008-07-27 21:46:04 +00006259 DenseMap<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattner6fdcb252005-09-02 20:32:45 +00006260 = ExpandedNodes.find(Op);
6261 if (I != ExpandedNodes.end()) {
6262 Lo = I->second.first;
6263 Hi = I->second.second;
6264 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006265 }
6266
Chris Lattner3e928bb2005-01-07 07:47:09 +00006267 switch (Node->getOpcode()) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006268 case ISD::CopyFromReg:
6269 assert(0 && "CopyFromReg must be legal!");
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006270 case ISD::FP_ROUND_INREG:
6271 if (VT == MVT::ppcf128 &&
6272 TLI.getOperationAction(ISD::FP_ROUND_INREG, VT) ==
6273 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006274 SDValue SrcLo, SrcHi, Src;
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006275 ExpandOp(Op.getOperand(0), SrcLo, SrcHi);
6276 Src = DAG.getNode(ISD::BUILD_PAIR, VT, SrcLo, SrcHi);
Dan Gohman475871a2008-07-27 21:46:04 +00006277 SDValue Result = TLI.LowerOperation(
Dale Johannesenfcf4d242007-10-11 23:32:15 +00006278 DAG.getNode(ISD::FP_ROUND_INREG, VT, Src, Op.getOperand(1)), DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006279 assert(Result.getNode()->getOpcode() == ISD::BUILD_PAIR);
6280 Lo = Result.getNode()->getOperand(0);
6281 Hi = Result.getNode()->getOperand(1);
Dale Johannesen6eaeff22007-10-10 01:01:31 +00006282 break;
6283 }
6284 // fall through
Chris Lattner348e93c2006-01-21 04:27:00 +00006285 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006286#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00006287 cerr << "NODE: "; Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00006288#endif
Chris Lattner3e928bb2005-01-07 07:47:09 +00006289 assert(0 && "Do not know how to expand this operator!");
6290 abort();
Dan Gohman1953ecb2008-02-27 01:52:30 +00006291 case ISD::EXTRACT_ELEMENT:
6292 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006293 if (cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue())
Dan Gohman1953ecb2008-02-27 01:52:30 +00006294 return ExpandOp(Hi, Lo, Hi);
Dan Gohman18714ae2008-02-27 19:44:57 +00006295 return ExpandOp(Lo, Lo, Hi);
Dale Johannesen25f1d082007-10-31 00:32:36 +00006296 case ISD::EXTRACT_VECTOR_ELT:
Dale Johannesen25f1d082007-10-31 00:32:36 +00006297 // ExpandEXTRACT_VECTOR_ELT tolerates invalid result types.
6298 Lo = ExpandEXTRACT_VECTOR_ELT(Op);
6299 return ExpandOp(Lo, Lo, Hi);
Nate Begemanfc1b1da2005-04-01 22:34:39 +00006300 case ISD::UNDEF:
6301 Lo = DAG.getNode(ISD::UNDEF, NVT);
6302 Hi = DAG.getNode(ISD::UNDEF, NVT);
6303 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006304 case ISD::Constant: {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006305 unsigned NVTBits = NVT.getSizeInBits();
Dan Gohman050f5502008-03-03 22:20:46 +00006306 const APInt &Cst = cast<ConstantSDNode>(Node)->getAPIntValue();
6307 Lo = DAG.getConstant(APInt(Cst).trunc(NVTBits), NVT);
6308 Hi = DAG.getConstant(Cst.lshr(NVTBits).trunc(NVTBits), NVT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006309 break;
6310 }
Evan Cheng00495212006-12-12 21:32:44 +00006311 case ISD::ConstantFP: {
6312 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
Dale Johannesena471c2e2007-10-11 18:07:22 +00006313 if (CFP->getValueType(0) == MVT::ppcf128) {
Dale Johannesen7111b022008-10-09 18:53:47 +00006314 APInt api = CFP->getValueAPF().bitcastToAPInt();
Dale Johannesena471c2e2007-10-11 18:07:22 +00006315 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[1])),
6316 MVT::f64);
6317 Hi = DAG.getConstantFP(APFloat(APInt(64, 1, &api.getRawData()[0])),
6318 MVT::f64);
6319 break;
6320 }
Evan Cheng279101e2006-12-12 22:19:28 +00006321 Lo = ExpandConstantFP(CFP, false, DAG, TLI);
Evan Cheng9f877882006-12-13 20:57:08 +00006322 if (getTypeAction(Lo.getValueType()) == Expand)
6323 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006324 break;
6325 }
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006326 case ISD::BUILD_PAIR:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006327 // Return the operands.
6328 Lo = Node->getOperand(0);
6329 Hi = Node->getOperand(1);
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006330 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006331
6332 case ISD::MERGE_VALUES:
Chris Lattnerc58d5582007-11-24 19:12:15 +00006333 if (Node->getNumValues() == 1) {
6334 ExpandOp(Op.getOperand(0), Lo, Hi);
6335 break;
6336 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006337 // FIXME: For now only expand i64,chain = MERGE_VALUES (x, y)
Gabor Greif99a6cb92008-08-26 22:36:50 +00006338 assert(Op.getResNo() == 0 && Node->getNumValues() == 2 &&
Chris Lattner27a6c732007-11-24 07:07:01 +00006339 Op.getValue(1).getValueType() == MVT::Other &&
6340 "unhandled MERGE_VALUES");
6341 ExpandOp(Op.getOperand(0), Lo, Hi);
6342 // Remember that we legalized the chain.
6343 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Op.getOperand(1)));
6344 break;
Chris Lattner58f79632005-12-12 22:27:43 +00006345
6346 case ISD::SIGN_EXTEND_INREG:
6347 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner4bdd2752006-10-06 17:34:12 +00006348 // sext_inreg the low part if needed.
6349 Lo = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Lo, Node->getOperand(1));
6350
6351 // The high part gets the sign extension from the lo-part. This handles
6352 // things like sextinreg V:i64 from i8.
Chris Lattner58f79632005-12-12 22:27:43 +00006353 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006354 DAG.getConstant(NVT.getSizeInBits()-1,
Chris Lattner58f79632005-12-12 22:27:43 +00006355 TLI.getShiftAmountTy()));
Chris Lattner58f79632005-12-12 22:27:43 +00006356 break;
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00006357
Nate Begemand88fc032006-01-14 03:14:10 +00006358 case ISD::BSWAP: {
6359 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006360 SDValue TempLo = DAG.getNode(ISD::BSWAP, NVT, Hi);
Nate Begemand88fc032006-01-14 03:14:10 +00006361 Hi = DAG.getNode(ISD::BSWAP, NVT, Lo);
6362 Lo = TempLo;
6363 break;
6364 }
6365
Chris Lattneredb1add2005-05-11 04:51:16 +00006366 case ISD::CTPOP:
6367 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00006368 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
6369 DAG.getNode(ISD::CTPOP, NVT, Lo),
6370 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00006371 Hi = DAG.getConstant(0, NVT);
6372 break;
6373
Chris Lattner39a8f332005-05-12 19:05:01 +00006374 case ISD::CTLZ: {
6375 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006376 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006377 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6378 SDValue HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
6379 SDValue TopNotZero = DAG.getSetCC(TLI.getSetCCResultType(HLZ), HLZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006380 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006381 SDValue LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
Chris Lattner39a8f332005-05-12 19:05:01 +00006382 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
6383
6384 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
6385 Hi = DAG.getConstant(0, NVT);
6386 break;
6387 }
6388
6389 case ISD::CTTZ: {
6390 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00006391 ExpandOp(Node->getOperand(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006392 SDValue BitsC = DAG.getConstant(NVT.getSizeInBits(), NVT);
6393 SDValue LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
6394 SDValue BotNotZero = DAG.getSetCC(TLI.getSetCCResultType(LTZ), LTZ, BitsC,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00006395 ISD::SETNE);
Dan Gohman475871a2008-07-27 21:46:04 +00006396 SDValue HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00006397 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
6398
6399 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
6400 Hi = DAG.getConstant(0, NVT);
6401 break;
6402 }
Chris Lattneredb1add2005-05-11 04:51:16 +00006403
Nate Begemanacc398c2006-01-25 18:21:52 +00006404 case ISD::VAARG: {
Dan Gohman475871a2008-07-27 21:46:04 +00006405 SDValue Ch = Node->getOperand(0); // Legalize the chain.
6406 SDValue Ptr = Node->getOperand(1); // Legalize the pointer.
Nate Begemanacc398c2006-01-25 18:21:52 +00006407 Lo = DAG.getVAArg(NVT, Ch, Ptr, Node->getOperand(2));
6408 Hi = DAG.getVAArg(NVT, Lo.getValue(1), Ptr, Node->getOperand(2));
6409
6410 // Remember that we legalized the chain.
Chris Lattner8137c9e2006-01-28 05:07:51 +00006411 Hi = LegalizeOp(Hi);
Nate Begemanacc398c2006-01-25 18:21:52 +00006412 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
Duncan Sands0753fc12008-02-11 10:37:04 +00006413 if (TLI.isBigEndian())
Nate Begemanacc398c2006-01-25 18:21:52 +00006414 std::swap(Lo, Hi);
6415 break;
6416 }
6417
Chris Lattner3e928bb2005-01-07 07:47:09 +00006418 case ISD::LOAD: {
Evan Cheng466685d2006-10-09 20:57:25 +00006419 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00006420 SDValue Ch = LD->getChain(); // Legalize the chain.
6421 SDValue Ptr = LD->getBasePtr(); // Legalize the pointer.
Evan Cheng466685d2006-10-09 20:57:25 +00006422 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f8613e2008-08-14 20:04:46 +00006423 const Value *SV = LD->getSrcValue();
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006424 int SVOffset = LD->getSrcValueOffset();
6425 unsigned Alignment = LD->getAlignment();
6426 bool isVolatile = LD->isVolatile();
Chris Lattner3e928bb2005-01-07 07:47:09 +00006427
Evan Cheng466685d2006-10-09 20:57:25 +00006428 if (ExtType == ISD::NON_EXTLOAD) {
Dan Gohman7f8613e2008-08-14 20:04:46 +00006429 Lo = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006430 isVolatile, Alignment);
Evan Cheng00495212006-12-12 21:32:44 +00006431 if (VT == MVT::f32 || VT == MVT::f64) {
6432 // f32->i32 or f64->i64 one to one expansion.
6433 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006434 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng9f877882006-12-13 20:57:08 +00006435 // Recursively expand the new load.
6436 if (getTypeAction(NVT) == Expand)
6437 ExpandOp(Lo, Lo, Hi);
Evan Cheng00495212006-12-12 21:32:44 +00006438 break;
6439 }
Chris Lattnerec39a452005-01-19 18:02:17 +00006440
Evan Cheng466685d2006-10-09 20:57:25 +00006441 // Increment the pointer to the other half.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006442 unsigned IncrementSize = Lo.getValueType().getSizeInBits()/8;
Evan Cheng466685d2006-10-09 20:57:25 +00006443 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00006444 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00006445 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00006446 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00006447 Hi = DAG.getLoad(NVT, Ch, Ptr, SV, SVOffset,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006448 isVolatile, Alignment);
Misha Brukmanedf128a2005-04-21 22:36:52 +00006449
Evan Cheng466685d2006-10-09 20:57:25 +00006450 // Build a factor node to remember that this load is independent of the
6451 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00006452 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Evan Cheng466685d2006-10-09 20:57:25 +00006453 Hi.getValue(1));
6454
6455 // Remember that we legalized the chain.
6456 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Duncan Sands0753fc12008-02-11 10:37:04 +00006457 if (TLI.isBigEndian())
Evan Cheng466685d2006-10-09 20:57:25 +00006458 std::swap(Lo, Hi);
6459 } else {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006460 MVT EVT = LD->getMemoryVT();
Evan Cheng548f6112006-12-13 03:19:57 +00006461
Dale Johannesenb6210fc2007-10-19 20:29:00 +00006462 if ((VT == MVT::f64 && EVT == MVT::f32) ||
6463 (VT == MVT::ppcf128 && (EVT==MVT::f64 || EVT==MVT::f32))) {
Evan Cheng548f6112006-12-13 03:19:57 +00006464 // f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
Dan Gohman7f8613e2008-08-14 20:04:46 +00006465 SDValue Load = DAG.getLoad(EVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006466 SVOffset, isVolatile, Alignment);
Evan Cheng548f6112006-12-13 03:19:57 +00006467 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006468 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Load.getValue(1)));
Evan Cheng548f6112006-12-13 03:19:57 +00006469 ExpandOp(DAG.getNode(ISD::FP_EXTEND, VT, Load), Lo, Hi);
6470 break;
6471 }
Evan Cheng466685d2006-10-09 20:57:25 +00006472
6473 if (EVT == NVT)
Dan Gohman7f8613e2008-08-14 20:04:46 +00006474 Lo = DAG.getLoad(NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006475 SVOffset, isVolatile, Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006476 else
Dan Gohman7f8613e2008-08-14 20:04:46 +00006477 Lo = DAG.getExtLoad(ExtType, NVT, Ch, Ptr, SV,
Dan Gohmand6fd1bc2007-07-09 22:18:38 +00006478 SVOffset, EVT, isVolatile,
6479 Alignment);
Evan Cheng466685d2006-10-09 20:57:25 +00006480
6481 // Remember that we legalized the chain.
Dan Gohman475871a2008-07-27 21:46:04 +00006482 AddLegalizedOperand(SDValue(Node, 1), LegalizeOp(Lo.getValue(1)));
Evan Cheng466685d2006-10-09 20:57:25 +00006483
6484 if (ExtType == ISD::SEXTLOAD) {
6485 // The high part is obtained by SRA'ing all but one of the bits of the
6486 // lo part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006487 unsigned LoSize = Lo.getValueType().getSizeInBits();
Evan Cheng466685d2006-10-09 20:57:25 +00006488 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6489 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
6490 } else if (ExtType == ISD::ZEXTLOAD) {
6491 // The high part is just a zero.
6492 Hi = DAG.getConstant(0, NVT);
6493 } else /* if (ExtType == ISD::EXTLOAD) */ {
6494 // The high part is undefined.
6495 Hi = DAG.getNode(ISD::UNDEF, NVT);
6496 }
6497 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006498 break;
6499 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00006500 case ISD::AND:
6501 case ISD::OR:
6502 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
Dan Gohman475871a2008-07-27 21:46:04 +00006503 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006504 ExpandOp(Node->getOperand(0), LL, LH);
6505 ExpandOp(Node->getOperand(1), RL, RH);
6506 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
6507 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
6508 break;
6509 }
6510 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006511 SDValue LL, LH, RL, RH;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006512 ExpandOp(Node->getOperand(1), LL, LH);
6513 ExpandOp(Node->getOperand(2), RL, RH);
Evan Cheng19103b12006-12-15 22:42:55 +00006514 if (getTypeAction(NVT) == Expand)
6515 NVT = TLI.getTypeToExpandTo(NVT);
Chris Lattner456a93a2006-01-28 07:39:30 +00006516 Lo = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LL, RL);
Evan Cheng19103b12006-12-15 22:42:55 +00006517 if (VT != MVT::f32)
6518 Hi = DAG.getNode(ISD::SELECT, NVT, Node->getOperand(0), LH, RH);
Chris Lattner3e928bb2005-01-07 07:47:09 +00006519 break;
6520 }
Nate Begeman9373a812005-08-10 20:51:12 +00006521 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00006522 SDValue TL, TH, FL, FH;
Nate Begeman9373a812005-08-10 20:51:12 +00006523 ExpandOp(Node->getOperand(2), TL, TH);
6524 ExpandOp(Node->getOperand(3), FL, FH);
Evan Cheng19103b12006-12-15 22:42:55 +00006525 if (getTypeAction(NVT) == Expand)
6526 NVT = TLI.getTypeToExpandTo(NVT);
Nate Begeman9373a812005-08-10 20:51:12 +00006527 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6528 Node->getOperand(1), TL, FL, Node->getOperand(4));
Evan Cheng19103b12006-12-15 22:42:55 +00006529 if (VT != MVT::f32)
6530 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
6531 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman9373a812005-08-10 20:51:12 +00006532 break;
6533 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006534 case ISD::ANY_EXTEND:
6535 // The low part is any extension of the input (which degenerates to a copy).
6536 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, Node->getOperand(0));
6537 // The high part is undefined.
6538 Hi = DAG.getNode(ISD::UNDEF, NVT);
6539 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00006540 case ISD::SIGN_EXTEND: {
6541 // The low part is just a sign extension of the input (which degenerates to
6542 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006543 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006544
Chris Lattner3e928bb2005-01-07 07:47:09 +00006545 // The high part is obtained by SRA'ing all but one of the bits of the lo
6546 // part.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006547 unsigned LoSize = Lo.getValueType().getSizeInBits();
Chris Lattner8137c9e2006-01-28 05:07:51 +00006548 Hi = DAG.getNode(ISD::SRA, NVT, Lo,
6549 DAG.getConstant(LoSize-1, TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00006550 break;
6551 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006552 case ISD::ZERO_EXTEND:
Chris Lattner3e928bb2005-01-07 07:47:09 +00006553 // The low part is just a zero extension of the input (which degenerates to
6554 // a copy).
Chris Lattner8137c9e2006-01-28 05:07:51 +00006555 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, Node->getOperand(0));
Misha Brukmanedf128a2005-04-21 22:36:52 +00006556
Chris Lattner3e928bb2005-01-07 07:47:09 +00006557 // The high part is just a zero.
6558 Hi = DAG.getConstant(0, NVT);
6559 break;
Chris Lattner35481892005-12-23 00:16:34 +00006560
Chris Lattner4c948eb2007-02-13 23:55:16 +00006561 case ISD::TRUNCATE: {
6562 // The input value must be larger than this value. Expand *it*.
Dan Gohman475871a2008-07-27 21:46:04 +00006563 SDValue NewLo;
Chris Lattner4c948eb2007-02-13 23:55:16 +00006564 ExpandOp(Node->getOperand(0), NewLo, Hi);
6565
6566 // The low part is now either the right size, or it is closer. If not the
6567 // right size, make an illegal truncate so we recursively expand it.
6568 if (NewLo.getValueType() != Node->getValueType(0))
6569 NewLo = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), NewLo);
6570 ExpandOp(NewLo, Lo, Hi);
6571 break;
6572 }
6573
Chris Lattner35481892005-12-23 00:16:34 +00006574 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00006575 SDValue Tmp;
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006576 if (TLI.getOperationAction(ISD::BIT_CONVERT, VT) == TargetLowering::Custom){
6577 // If the target wants to, allow it to lower this itself.
6578 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6579 case Expand: assert(0 && "cannot expand FP!");
6580 case Legal: Tmp = LegalizeOp(Node->getOperand(0)); break;
6581 case Promote: Tmp = PromoteOp (Node->getOperand(0)); break;
6582 }
6583 Tmp = TLI.LowerOperation(DAG.getNode(ISD::BIT_CONVERT, VT, Tmp), DAG);
6584 }
6585
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006586 // f32 / f64 must be expanded to i32 / i64.
Evan Cheng13acce32006-12-11 19:27:14 +00006587 if (VT == MVT::f32 || VT == MVT::f64) {
6588 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
Evan Cheng19103b12006-12-15 22:42:55 +00006589 if (getTypeAction(NVT) == Expand)
6590 ExpandOp(Lo, Lo, Hi);
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006591 break;
6592 }
6593
6594 // If source operand will be expanded to the same type as VT, i.e.
6595 // i64 <- f64, i32 <- f32, expand the source operand instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006596 MVT VT0 = Node->getOperand(0).getValueType();
Evan Cheng7b2b5c82006-12-12 19:53:13 +00006597 if (getTypeAction(VT0) == Expand && TLI.getTypeToTransformTo(VT0) == VT) {
6598 ExpandOp(Node->getOperand(0), Lo, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00006599 break;
6600 }
6601
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006602 // Turn this into a load/store pair by default.
Gabor Greifba36cb52008-08-28 21:40:38 +00006603 if (Tmp.getNode() == 0)
Chris Lattner1401d152008-01-16 07:45:30 +00006604 Tmp = EmitStackConvert(Node->getOperand(0), VT, VT);
Chris Lattnerf3f333d2006-09-09 00:20:27 +00006605
Chris Lattner35481892005-12-23 00:16:34 +00006606 ExpandOp(Tmp, Lo, Hi);
6607 break;
6608 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006609
Chris Lattner27a6c732007-11-24 07:07:01 +00006610 case ISD::READCYCLECOUNTER: {
Chris Lattner308575b2005-11-20 22:56:56 +00006611 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
6612 TargetLowering::Custom &&
6613 "Must custom expand ReadCycleCounter");
Dan Gohman475871a2008-07-27 21:46:04 +00006614 SDValue Tmp = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006615 assert(Tmp.getNode() && "Node must be custom expanded!");
Chris Lattner27a6c732007-11-24 07:07:01 +00006616 ExpandOp(Tmp.getValue(0), Lo, Hi);
Dan Gohman475871a2008-07-27 21:46:04 +00006617 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
Chris Lattner27a6c732007-11-24 07:07:01 +00006618 LegalizeOp(Tmp.getValue(1)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006619 break;
Chris Lattner27a6c732007-11-24 07:07:01 +00006620 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00006621
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006622 case ISD::ATOMIC_CMP_SWAP_64: {
6623 // This operation does not need a loop.
6624 SDValue Tmp = TLI.LowerOperation(Op, DAG);
6625 assert(Tmp.getNode() && "Node must be custom expanded!");
6626 ExpandOp(Tmp.getValue(0), Lo, Hi);
6627 AddLegalizedOperand(SDValue(Node, 1), // Remember we legalized the chain.
6628 LegalizeOp(Tmp.getValue(1)));
6629 break;
6630 }
6631
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006632 case ISD::ATOMIC_LOAD_ADD_64:
6633 case ISD::ATOMIC_LOAD_SUB_64:
6634 case ISD::ATOMIC_LOAD_AND_64:
6635 case ISD::ATOMIC_LOAD_OR_64:
6636 case ISD::ATOMIC_LOAD_XOR_64:
6637 case ISD::ATOMIC_LOAD_NAND_64:
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006638 case ISD::ATOMIC_SWAP_64: {
6639 // These operations require a loop to be generated. We can't do that yet,
6640 // so substitute a target-dependent pseudo and expand that later.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006641 SDValue In2Lo, In2Hi, In2;
6642 ExpandOp(Op.getOperand(2), In2Lo, In2Hi);
6643 In2 = DAG.getNode(ISD::BUILD_PAIR, VT, In2Lo, In2Hi);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006644 AtomicSDNode* Anode = cast<AtomicSDNode>(Node);
6645 SDValue Replace =
6646 DAG.getAtomic(Op.getOpcode(), Op.getOperand(0), Op.getOperand(1), In2,
6647 Anode->getSrcValue(), Anode->getAlignment());
6648 SDValue Result = TLI.LowerOperation(Replace, DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006649 ExpandOp(Result.getValue(0), Lo, Hi);
6650 // Remember that we legalized the chain.
6651 AddLegalizedOperand(SDValue(Node,1), LegalizeOp(Result.getValue(1)));
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006652 break;
6653 }
6654
Chris Lattner4e6c7462005-01-08 19:27:05 +00006655 // These operators cannot be expanded directly, emit them as calls to
6656 // library functions.
Evan Cheng56966222007-01-12 02:11:51 +00006657 case ISD::FP_TO_SINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006658 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006659 SDValue Op;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006660 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6661 case Expand: assert(0 && "cannot expand FP!");
Chris Lattner8137c9e2006-01-28 05:07:51 +00006662 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6663 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
Chris Lattnerf20d1832005-07-30 01:40:57 +00006664 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006665
Chris Lattnerf20d1832005-07-30 01:40:57 +00006666 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
6667
Chris Lattner80a3e942005-07-29 00:33:32 +00006668 // Now that the custom expander is done, expand the result, which is still
6669 // VT.
Gabor Greifba36cb52008-08-28 21:40:38 +00006670 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006671 ExpandOp(Op, Lo, Hi);
6672 break;
6673 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006674 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006675
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006676 RTLIB::Libcall LC = RTLIB::getFPTOSINT(Node->getOperand(0).getValueType(),
6677 VT);
6678 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected uint-to-fp conversion!");
6679 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006680 break;
Evan Cheng56966222007-01-12 02:11:51 +00006681 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006682
Evan Cheng56966222007-01-12 02:11:51 +00006683 case ISD::FP_TO_UINT: {
Chris Lattner80a3e942005-07-29 00:33:32 +00006684 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006685 SDValue Op;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006686 switch (getTypeAction(Node->getOperand(0).getValueType())) {
6687 case Expand: assert(0 && "cannot expand FP!");
6688 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
6689 case Promote: Op = PromoteOp (Node->getOperand(0)); break;
6690 }
6691
6692 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_UINT, VT, Op), DAG);
6693
6694 // Now that the custom expander is done, expand the result.
Gabor Greifba36cb52008-08-28 21:40:38 +00006695 if (Op.getNode()) {
Chris Lattner07dffd62005-08-26 00:14:16 +00006696 ExpandOp(Op, Lo, Hi);
6697 break;
6698 }
Chris Lattner80a3e942005-07-29 00:33:32 +00006699 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00006700
Duncan Sandsb2ff8852008-07-17 02:36:29 +00006701 RTLIB::Libcall LC = RTLIB::getFPTOUINT(Node->getOperand(0).getValueType(),
6702 VT);
6703 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unexpected fp-to-uint conversion!");
6704 Lo = ExpandLibCall(LC, Node, false/*sign irrelevant*/, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00006705 break;
Evan Cheng56966222007-01-12 02:11:51 +00006706 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00006707
Evan Cheng05a2d562006-01-09 18:31:59 +00006708 case ISD::SHL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006709 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006710 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006711 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006712 SDValue Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006713 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006714 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006715 // Now that the custom expander is done, expand the result, which is
6716 // still VT.
6717 ExpandOp(Op, Lo, Hi);
6718 break;
6719 }
6720 }
6721
Chris Lattner79980b02006-09-13 03:50:39 +00006722 // If ADDC/ADDE are supported and if the shift amount is a constant 1, emit
6723 // this X << 1 as X+X.
6724 if (ConstantSDNode *ShAmt = dyn_cast<ConstantSDNode>(ShiftAmt)) {
Dan Gohman002e5d02008-03-13 22:13:53 +00006725 if (ShAmt->getAPIntValue() == 1 && TLI.isOperationLegal(ISD::ADDC, NVT) &&
Chris Lattner79980b02006-09-13 03:50:39 +00006726 TLI.isOperationLegal(ISD::ADDE, NVT)) {
Dan Gohman475871a2008-07-27 21:46:04 +00006727 SDValue LoOps[2], HiOps[3];
Chris Lattner79980b02006-09-13 03:50:39 +00006728 ExpandOp(Node->getOperand(0), LoOps[0], HiOps[0]);
6729 SDVTList VTList = DAG.getVTList(LoOps[0].getValueType(), MVT::Flag);
6730 LoOps[1] = LoOps[0];
6731 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6732
6733 HiOps[1] = HiOps[0];
6734 HiOps[2] = Lo.getValue(1);
6735 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6736 break;
6737 }
6738 }
6739
Chris Lattnere34b3962005-01-19 04:19:40 +00006740 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006741 if (ExpandShift(ISD::SHL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006742 break;
Chris Lattner47599822005-04-02 03:38:53 +00006743
6744 // If this target supports SHL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006745 TargetLowering::LegalizeAction Action =
6746 TLI.getOperationAction(ISD::SHL_PARTS, NVT);
6747 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6748 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006749 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006750 break;
6751 }
6752
Chris Lattnere34b3962005-01-19 04:19:40 +00006753 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006754 Lo = ExpandLibCall(RTLIB::SHL_I64, Node, false/*left shift=unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006755 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006756 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006757
Evan Cheng05a2d562006-01-09 18:31:59 +00006758 case ISD::SRA: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006759 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006760 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006761 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006762 SDValue Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006763 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006764 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006765 // Now that the custom expander is done, expand the result, which is
6766 // still VT.
6767 ExpandOp(Op, Lo, Hi);
6768 break;
6769 }
6770 }
6771
Chris Lattnere34b3962005-01-19 04:19:40 +00006772 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006773 if (ExpandShift(ISD::SRA, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006774 break;
Chris Lattner47599822005-04-02 03:38:53 +00006775
6776 // If this target supports SRA_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006777 TargetLowering::LegalizeAction Action =
6778 TLI.getOperationAction(ISD::SRA_PARTS, NVT);
6779 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6780 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006781 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006782 break;
6783 }
6784
Chris Lattnere34b3962005-01-19 04:19:40 +00006785 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006786 Lo = ExpandLibCall(RTLIB::SRA_I64, Node, true/*ashr is signed*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006787 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006788 }
6789
6790 case ISD::SRL: {
Chris Lattner50ec8972005-08-31 19:01:53 +00006791 // If the target wants custom lowering, do so.
Dan Gohman475871a2008-07-27 21:46:04 +00006792 SDValue ShiftAmt = LegalizeOp(Node->getOperand(1));
Chris Lattner50ec8972005-08-31 19:01:53 +00006793 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006794 SDValue Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0), ShiftAmt);
Chris Lattner50ec8972005-08-31 19:01:53 +00006795 Op = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006796 if (Op.getNode()) {
Chris Lattner50ec8972005-08-31 19:01:53 +00006797 // Now that the custom expander is done, expand the result, which is
6798 // still VT.
6799 ExpandOp(Op, Lo, Hi);
6800 break;
6801 }
6802 }
6803
Chris Lattnere34b3962005-01-19 04:19:40 +00006804 // If we can emit an efficient shift operation, do so now.
Chris Lattner348e93c2006-01-21 04:27:00 +00006805 if (ExpandShift(ISD::SRL, Node->getOperand(0), ShiftAmt, Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00006806 break;
Chris Lattner47599822005-04-02 03:38:53 +00006807
6808 // If this target supports SRL_PARTS, use it.
Evan Cheng05a2d562006-01-09 18:31:59 +00006809 TargetLowering::LegalizeAction Action =
6810 TLI.getOperationAction(ISD::SRL_PARTS, NVT);
6811 if ((Action == TargetLowering::Legal && TLI.isTypeLegal(NVT)) ||
6812 Action == TargetLowering::Custom) {
Chris Lattner348e93c2006-01-21 04:27:00 +00006813 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), ShiftAmt, Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00006814 break;
6815 }
6816
Chris Lattnere34b3962005-01-19 04:19:40 +00006817 // Otherwise, emit a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00006818 Lo = ExpandLibCall(RTLIB::SRL_I64, Node, false/*lshr is unsigned*/, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00006819 break;
Evan Cheng05a2d562006-01-09 18:31:59 +00006820 }
Chris Lattnere34b3962005-01-19 04:19:40 +00006821
Misha Brukmanedf128a2005-04-21 22:36:52 +00006822 case ISD::ADD:
Chris Lattner8137c9e2006-01-28 05:07:51 +00006823 case ISD::SUB: {
6824 // If the target wants to custom expand this, let them.
6825 if (TLI.getOperationAction(Node->getOpcode(), VT) ==
6826 TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006827 SDValue Result = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006828 if (Result.getNode()) {
Duncan Sands69bfb152008-06-22 09:42:16 +00006829 ExpandOp(Result, Lo, Hi);
Chris Lattner8137c9e2006-01-28 05:07:51 +00006830 break;
6831 }
6832 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006833 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006834 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattner8137c9e2006-01-28 05:07:51 +00006835 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6836 ExpandOp(Node->getOperand(1), RHSL, RHSH);
Chris Lattner79980b02006-09-13 03:50:39 +00006837 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006838 SDValue LoOps[2], HiOps[3];
Chris Lattnerbd564bf2006-08-08 02:23:42 +00006839 LoOps[0] = LHSL;
6840 LoOps[1] = RHSL;
6841 HiOps[0] = LHSH;
6842 HiOps[1] = RHSH;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006843
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006844 //cascaded check to see if any smaller size has a a carry flag.
6845 unsigned OpV = Node->getOpcode() == ISD::ADD ? ISD::ADDC : ISD::SUBC;
6846 bool hasCarry = false;
Andrew Lenharth2163ca12008-10-07 18:27:23 +00006847 for (unsigned BitSize = NVT.getSizeInBits(); BitSize != 0; BitSize /= 2) {
6848 MVT AVT = MVT::getIntegerVT(BitSize);
6849 if (TLI.isOperationLegal(OpV, AVT)) {
6850 hasCarry = true;
6851 break;
6852 }
6853 }
6854
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006855 if(hasCarry) {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006856 if (Node->getOpcode() == ISD::ADD) {
6857 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6858 HiOps[2] = Lo.getValue(1);
6859 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6860 } else {
6861 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6862 HiOps[2] = Lo.getValue(1);
6863 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6864 }
6865 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006866 } else {
Andrew Lenharth40d51392008-10-07 14:15:42 +00006867 if (Node->getOpcode() == ISD::ADD) {
6868 Lo = DAG.getNode(ISD::ADD, VTList, LoOps, 2);
6869 Hi = DAG.getNode(ISD::ADD, VTList, HiOps, 2);
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006870 SDValue Cmp1 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6871 Lo, LoOps[0], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006872 SDValue Carry1 = DAG.getNode(ISD::SELECT, NVT, Cmp1,
6873 DAG.getConstant(1, NVT),
6874 DAG.getConstant(0, NVT));
Andrew Lenharth5c9cc132008-10-07 17:03:15 +00006875 SDValue Cmp2 = DAG.getSetCC(TLI.getSetCCResultType(Lo),
6876 Lo, LoOps[1], ISD::SETULT);
Andrew Lenharth40d51392008-10-07 14:15:42 +00006877 SDValue Carry2 = DAG.getNode(ISD::SELECT, NVT, Cmp2,
6878 DAG.getConstant(1, NVT),
6879 Carry1);
6880 Hi = DAG.getNode(ISD::ADD, NVT, Hi, Carry2);
6881 } else {
6882 Lo = DAG.getNode(ISD::SUB, VTList, LoOps, 2);
6883 Hi = DAG.getNode(ISD::SUB, VTList, HiOps, 2);
6884 SDValue Cmp = DAG.getSetCC(NVT, LoOps[0], LoOps[1], ISD::SETULT);
6885 SDValue Borrow = DAG.getNode(ISD::SELECT, NVT, Cmp,
6886 DAG.getConstant(1, NVT),
6887 DAG.getConstant(0, NVT));
6888 Hi = DAG.getNode(ISD::SUB, NVT, Hi, Borrow);
6889 }
6890 break;
Nate Begeman551bf3f2006-02-17 05:43:56 +00006891 }
Chris Lattner8137c9e2006-01-28 05:07:51 +00006892 }
Chris Lattnerb429f732007-05-17 18:15:41 +00006893
6894 case ISD::ADDC:
6895 case ISD::SUBC: {
6896 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006897 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006898 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6899 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6900 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006901 SDValue LoOps[2] = { LHSL, RHSL };
6902 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006903
6904 if (Node->getOpcode() == ISD::ADDC) {
6905 Lo = DAG.getNode(ISD::ADDC, VTList, LoOps, 2);
6906 HiOps[2] = Lo.getValue(1);
6907 Hi = DAG.getNode(ISD::ADDE, VTList, HiOps, 3);
6908 } else {
6909 Lo = DAG.getNode(ISD::SUBC, VTList, LoOps, 2);
6910 HiOps[2] = Lo.getValue(1);
6911 Hi = DAG.getNode(ISD::SUBE, VTList, HiOps, 3);
6912 }
6913 // Remember that we legalized the flag.
6914 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6915 break;
6916 }
6917 case ISD::ADDE:
6918 case ISD::SUBE: {
6919 // Expand the subcomponents.
Dan Gohman475871a2008-07-27 21:46:04 +00006920 SDValue LHSL, LHSH, RHSL, RHSH;
Chris Lattnerb429f732007-05-17 18:15:41 +00006921 ExpandOp(Node->getOperand(0), LHSL, LHSH);
6922 ExpandOp(Node->getOperand(1), RHSL, RHSH);
6923 SDVTList VTList = DAG.getVTList(LHSL.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006924 SDValue LoOps[3] = { LHSL, RHSL, Node->getOperand(2) };
6925 SDValue HiOps[3] = { LHSH, RHSH };
Chris Lattnerb429f732007-05-17 18:15:41 +00006926
6927 Lo = DAG.getNode(Node->getOpcode(), VTList, LoOps, 3);
6928 HiOps[2] = Lo.getValue(1);
6929 Hi = DAG.getNode(Node->getOpcode(), VTList, HiOps, 3);
6930
6931 // Remember that we legalized the flag.
6932 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Hi.getValue(1)));
6933 break;
6934 }
Nate Begemanc7c16572005-04-11 03:01:51 +00006935 case ISD::MUL: {
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006936 // If the target wants to custom expand this, let them.
6937 if (TLI.getOperationAction(ISD::MUL, VT) == TargetLowering::Custom) {
Dan Gohman475871a2008-07-27 21:46:04 +00006938 SDValue New = TLI.LowerOperation(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00006939 if (New.getNode()) {
Chris Lattner8829dc82006-09-16 05:08:34 +00006940 ExpandOp(New, Lo, Hi);
Chris Lattner7d7bffe2006-09-16 00:09:24 +00006941 break;
6942 }
6943 }
6944
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00006945 bool HasMULHS = TLI.isOperationLegal(ISD::MULHS, NVT);
6946 bool HasMULHU = TLI.isOperationLegal(ISD::MULHU, NVT);
Dan Gohman525178c2007-10-08 18:33:35 +00006947 bool HasSMUL_LOHI = TLI.isOperationLegal(ISD::SMUL_LOHI, NVT);
6948 bool HasUMUL_LOHI = TLI.isOperationLegal(ISD::UMUL_LOHI, NVT);
6949 if (HasMULHU || HasMULHS || HasUMUL_LOHI || HasSMUL_LOHI) {
Dan Gohman475871a2008-07-27 21:46:04 +00006950 SDValue LL, LH, RL, RH;
Nate Begemanc7c16572005-04-11 03:01:51 +00006951 ExpandOp(Node->getOperand(0), LL, LH);
6952 ExpandOp(Node->getOperand(1), RL, RH);
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006953 unsigned OuterBitSize = Op.getValueSizeInBits();
6954 unsigned InnerBitSize = RH.getValueSizeInBits();
Dan Gohman525178c2007-10-08 18:33:35 +00006955 unsigned LHSSB = DAG.ComputeNumSignBits(Op.getOperand(0));
6956 unsigned RHSSB = DAG.ComputeNumSignBits(Op.getOperand(1));
Dan Gohman76c605b2008-03-10 20:42:19 +00006957 APInt HighMask = APInt::getHighBitsSet(OuterBitSize, InnerBitSize);
6958 if (DAG.MaskedValueIsZero(Node->getOperand(0), HighMask) &&
6959 DAG.MaskedValueIsZero(Node->getOperand(1), HighMask)) {
Dan Gohman525178c2007-10-08 18:33:35 +00006960 // The inputs are both zero-extended.
6961 if (HasUMUL_LOHI) {
6962 // We can emit a umul_lohi.
6963 Lo = DAG.getNode(ISD::UMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006964 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006965 break;
6966 }
6967 if (HasMULHU) {
6968 // We can emit a mulhu+mul.
6969 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6970 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
6971 break;
6972 }
Dan Gohman525178c2007-10-08 18:33:35 +00006973 }
Dan Gohman2e68b6f2008-02-25 21:11:39 +00006974 if (LHSSB > InnerBitSize && RHSSB > InnerBitSize) {
Dan Gohman525178c2007-10-08 18:33:35 +00006975 // The input values are both sign-extended.
6976 if (HasSMUL_LOHI) {
6977 // We can emit a smul_lohi.
6978 Lo = DAG.getNode(ISD::SMUL_LOHI, DAG.getVTList(NVT, NVT), LL, RL);
Gabor Greifba36cb52008-08-28 21:40:38 +00006979 Hi = SDValue(Lo.getNode(), 1);
Dan Gohman525178c2007-10-08 18:33:35 +00006980 break;
6981 }
6982 if (HasMULHS) {
6983 // We can emit a mulhs+mul.
6984 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
6985 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
6986 break;
6987 }
6988 }
6989 if (HasUMUL_LOHI) {
6990 // Lo,Hi = umul LHS, RHS.
Dan Gohman475871a2008-07-27 21:46:04 +00006991 SDValue UMulLOHI = DAG.getNode(ISD::UMUL_LOHI,
Dan Gohman525178c2007-10-08 18:33:35 +00006992 DAG.getVTList(NVT, NVT), LL, RL);
6993 Lo = UMulLOHI;
6994 Hi = UMulLOHI.getValue(1);
Nate Begeman56eb8682005-08-30 02:44:00 +00006995 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
6996 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
6997 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
6998 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
Chris Lattnera89654b2006-09-16 00:21:44 +00006999 break;
Nate Begeman56eb8682005-08-30 02:44:00 +00007000 }
Dale Johannesen8eadd5a2007-10-24 22:26:08 +00007001 if (HasMULHU) {
7002 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
7003 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
7004 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
7005 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
7006 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
7007 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
7008 break;
7009 }
Nate Begemanc7c16572005-04-11 03:01:51 +00007010 }
Evan Cheng3f4fd0f2006-09-01 18:17:58 +00007011
Dan Gohman525178c2007-10-08 18:33:35 +00007012 // If nothing else, we can make a libcall.
Duncan Sands460a14e2008-04-12 17:14:18 +00007013 Lo = ExpandLibCall(RTLIB::MUL_I64, Node, false/*sign irrelevant*/, Hi);
Nate Begemanc7c16572005-04-11 03:01:51 +00007014 break;
7015 }
Evan Cheng56966222007-01-12 02:11:51 +00007016 case ISD::SDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007017 Lo = ExpandLibCall(RTLIB::SDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007018 break;
7019 case ISD::UDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007020 Lo = ExpandLibCall(RTLIB::UDIV_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007021 break;
7022 case ISD::SREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007023 Lo = ExpandLibCall(RTLIB::SREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007024 break;
7025 case ISD::UREM:
Duncan Sands460a14e2008-04-12 17:14:18 +00007026 Lo = ExpandLibCall(RTLIB::UREM_I64, Node, true, Hi);
Evan Cheng56966222007-01-12 02:11:51 +00007027 break;
Evan Cheng5c9ce182006-12-12 21:51:17 +00007028
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007029 case ISD::FADD:
Duncan Sands460a14e2008-04-12 17:14:18 +00007030 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::ADD_F32,
7031 RTLIB::ADD_F64,
7032 RTLIB::ADD_F80,
7033 RTLIB::ADD_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007034 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007035 break;
7036 case ISD::FSUB:
Duncan Sands460a14e2008-04-12 17:14:18 +00007037 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::SUB_F32,
7038 RTLIB::SUB_F64,
7039 RTLIB::SUB_F80,
7040 RTLIB::SUB_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007041 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007042 break;
7043 case ISD::FMUL:
Duncan Sands460a14e2008-04-12 17:14:18 +00007044 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::MUL_F32,
7045 RTLIB::MUL_F64,
7046 RTLIB::MUL_F80,
7047 RTLIB::MUL_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007048 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007049 break;
7050 case ISD::FDIV:
Duncan Sands460a14e2008-04-12 17:14:18 +00007051 Lo = ExpandLibCall(GetFPLibCall(VT, RTLIB::DIV_F32,
7052 RTLIB::DIV_F64,
7053 RTLIB::DIV_F80,
7054 RTLIB::DIV_PPCF128),
Evan Cheng56966222007-01-12 02:11:51 +00007055 Node, false, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007056 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007057 case ISD::FP_EXTEND: {
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007058 if (VT == MVT::ppcf128) {
7059 assert(Node->getOperand(0).getValueType()==MVT::f32 ||
7060 Node->getOperand(0).getValueType()==MVT::f64);
7061 const uint64_t zero = 0;
7062 if (Node->getOperand(0).getValueType()==MVT::f32)
7063 Hi = DAG.getNode(ISD::FP_EXTEND, MVT::f64, Node->getOperand(0));
7064 else
7065 Hi = Node->getOperand(0);
7066 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7067 break;
7068 }
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007069 RTLIB::Libcall LC = RTLIB::getFPEXT(Node->getOperand(0).getValueType(), VT);
7070 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_EXTEND!");
7071 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007072 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007073 }
7074 case ISD::FP_ROUND: {
7075 RTLIB::Libcall LC = RTLIB::getFPROUND(Node->getOperand(0).getValueType(),
7076 VT);
7077 assert(LC != RTLIB::UNKNOWN_LIBCALL && "Unsupported FP_ROUND!");
7078 Lo = ExpandLibCall(LC, Node, true, Hi);
Evan Cheng1a8f1fe2006-12-09 02:42:38 +00007079 break;
Duncan Sandsb2ff8852008-07-17 02:36:29 +00007080 }
Evan Cheng4b887022008-09-09 23:02:14 +00007081 case ISD::FSQRT:
7082 case ISD::FSIN:
7083 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007084 case ISD::FLOG:
7085 case ISD::FLOG2:
7086 case ISD::FLOG10:
7087 case ISD::FEXP:
7088 case ISD::FEXP2:
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007089 case ISD::FTRUNC:
7090 case ISD::FFLOOR:
7091 case ISD::FCEIL:
7092 case ISD::FRINT:
7093 case ISD::FNEARBYINT:
Evan Cheng9d24ac52008-09-09 23:35:53 +00007094 case ISD::FPOW:
7095 case ISD::FPOWI: {
Evan Cheng56966222007-01-12 02:11:51 +00007096 RTLIB::Libcall LC = RTLIB::UNKNOWN_LIBCALL;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007097 switch(Node->getOpcode()) {
Evan Cheng56966222007-01-12 02:11:51 +00007098 case ISD::FSQRT:
Duncan Sands007f9842008-01-10 10:28:30 +00007099 LC = GetFPLibCall(VT, RTLIB::SQRT_F32, RTLIB::SQRT_F64,
7100 RTLIB::SQRT_F80, RTLIB::SQRT_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007101 break;
7102 case ISD::FSIN:
Duncan Sands007f9842008-01-10 10:28:30 +00007103 LC = GetFPLibCall(VT, RTLIB::SIN_F32, RTLIB::SIN_F64,
7104 RTLIB::SIN_F80, RTLIB::SIN_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007105 break;
7106 case ISD::FCOS:
Duncan Sands007f9842008-01-10 10:28:30 +00007107 LC = GetFPLibCall(VT, RTLIB::COS_F32, RTLIB::COS_F64,
7108 RTLIB::COS_F80, RTLIB::COS_PPCF128);
Evan Cheng56966222007-01-12 02:11:51 +00007109 break;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007110 case ISD::FLOG:
7111 LC = GetFPLibCall(VT, RTLIB::LOG_F32, RTLIB::LOG_F64,
7112 RTLIB::LOG_F80, RTLIB::LOG_PPCF128);
7113 break;
7114 case ISD::FLOG2:
7115 LC = GetFPLibCall(VT, RTLIB::LOG2_F32, RTLIB::LOG2_F64,
7116 RTLIB::LOG2_F80, RTLIB::LOG2_PPCF128);
7117 break;
7118 case ISD::FLOG10:
7119 LC = GetFPLibCall(VT, RTLIB::LOG10_F32, RTLIB::LOG10_F64,
7120 RTLIB::LOG10_F80, RTLIB::LOG10_PPCF128);
7121 break;
7122 case ISD::FEXP:
7123 LC = GetFPLibCall(VT, RTLIB::EXP_F32, RTLIB::EXP_F64,
7124 RTLIB::EXP_F80, RTLIB::EXP_PPCF128);
7125 break;
7126 case ISD::FEXP2:
7127 LC = GetFPLibCall(VT, RTLIB::EXP2_F32, RTLIB::EXP2_F64,
7128 RTLIB::EXP2_F80, RTLIB::EXP2_PPCF128);
7129 break;
Dan Gohman2bb1e3e2008-08-21 18:38:14 +00007130 case ISD::FTRUNC:
7131 LC = GetFPLibCall(VT, RTLIB::TRUNC_F32, RTLIB::TRUNC_F64,
7132 RTLIB::TRUNC_F80, RTLIB::TRUNC_PPCF128);
7133 break;
7134 case ISD::FFLOOR:
7135 LC = GetFPLibCall(VT, RTLIB::FLOOR_F32, RTLIB::FLOOR_F64,
7136 RTLIB::FLOOR_F80, RTLIB::FLOOR_PPCF128);
7137 break;
7138 case ISD::FCEIL:
7139 LC = GetFPLibCall(VT, RTLIB::CEIL_F32, RTLIB::CEIL_F64,
7140 RTLIB::CEIL_F80, RTLIB::CEIL_PPCF128);
7141 break;
7142 case ISD::FRINT:
7143 LC = GetFPLibCall(VT, RTLIB::RINT_F32, RTLIB::RINT_F64,
7144 RTLIB::RINT_F80, RTLIB::RINT_PPCF128);
7145 break;
7146 case ISD::FNEARBYINT:
7147 LC = GetFPLibCall(VT, RTLIB::NEARBYINT_F32, RTLIB::NEARBYINT_F64,
7148 RTLIB::NEARBYINT_F80, RTLIB::NEARBYINT_PPCF128);
7149 break;
Evan Cheng4b887022008-09-09 23:02:14 +00007150 case ISD::FPOW:
7151 LC = GetFPLibCall(VT, RTLIB::POW_F32, RTLIB::POW_F64, RTLIB::POW_F80,
7152 RTLIB::POW_PPCF128);
7153 break;
7154 case ISD::FPOWI:
7155 LC = GetFPLibCall(VT, RTLIB::POWI_F32, RTLIB::POWI_F64, RTLIB::POWI_F80,
7156 RTLIB::POWI_PPCF128);
7157 break;
Evan Cheng98ff3b92006-12-13 02:38:13 +00007158 default: assert(0 && "Unreachable!");
7159 }
Duncan Sands460a14e2008-04-12 17:14:18 +00007160 Lo = ExpandLibCall(LC, Node, false, Hi);
Evan Cheng98ff3b92006-12-13 02:38:13 +00007161 break;
7162 }
Evan Cheng966bf242006-12-16 00:52:40 +00007163 case ISD::FABS: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007164 if (VT == MVT::ppcf128) {
Dan Gohman475871a2008-07-27 21:46:04 +00007165 SDValue Tmp;
Dale Johannesenf6467742007-10-12 19:02:17 +00007166 ExpandOp(Node->getOperand(0), Lo, Tmp);
7167 Hi = DAG.getNode(ISD::FABS, NVT, Tmp);
7168 // lo = hi==fabs(hi) ? lo : -lo;
7169 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Hi, Tmp,
7170 Lo, DAG.getNode(ISD::FNEG, NVT, Lo),
7171 DAG.getCondCode(ISD::SETEQ));
7172 break;
7173 }
Dan Gohman475871a2008-07-27 21:46:04 +00007174 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007175 ? DAG.getConstantFP(BitsToDouble(~(1ULL << 63)), VT)
7176 : DAG.getConstantFP(BitsToFloat(~(1U << 31)), VT);
7177 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7178 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7179 Lo = DAG.getNode(ISD::AND, NVT, Lo, Mask);
7180 if (getTypeAction(NVT) == Expand)
7181 ExpandOp(Lo, Lo, Hi);
7182 break;
7183 }
7184 case ISD::FNEG: {
Dale Johannesenf6467742007-10-12 19:02:17 +00007185 if (VT == MVT::ppcf128) {
7186 ExpandOp(Node->getOperand(0), Lo, Hi);
7187 Lo = DAG.getNode(ISD::FNEG, MVT::f64, Lo);
7188 Hi = DAG.getNode(ISD::FNEG, MVT::f64, Hi);
7189 break;
7190 }
Dan Gohman475871a2008-07-27 21:46:04 +00007191 SDValue Mask = (VT == MVT::f64)
Evan Cheng966bf242006-12-16 00:52:40 +00007192 ? DAG.getConstantFP(BitsToDouble(1ULL << 63), VT)
7193 : DAG.getConstantFP(BitsToFloat(1U << 31), VT);
7194 Mask = DAG.getNode(ISD::BIT_CONVERT, NVT, Mask);
7195 Lo = DAG.getNode(ISD::BIT_CONVERT, NVT, Node->getOperand(0));
7196 Lo = DAG.getNode(ISD::XOR, NVT, Lo, Mask);
7197 if (getTypeAction(NVT) == Expand)
7198 ExpandOp(Lo, Lo, Hi);
7199 break;
7200 }
Evan Cheng912095b2007-01-04 21:56:39 +00007201 case ISD::FCOPYSIGN: {
7202 Lo = ExpandFCOPYSIGNToBitwiseOps(Node, NVT, DAG, TLI);
7203 if (getTypeAction(NVT) == Expand)
7204 ExpandOp(Lo, Lo, Hi);
7205 break;
7206 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007207 case ISD::SINT_TO_FP:
7208 case ISD::UINT_TO_FP: {
7209 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007210 MVT SrcVT = Node->getOperand(0).getValueType();
Dale Johannesencf498192008-03-18 17:28:38 +00007211
7212 // Promote the operand if needed. Do this before checking for
7213 // ppcf128 so conversions of i16 and i8 work.
7214 if (getTypeAction(SrcVT) == Promote) {
Dan Gohman475871a2008-07-27 21:46:04 +00007215 SDValue Tmp = PromoteOp(Node->getOperand(0));
Dale Johannesencf498192008-03-18 17:28:38 +00007216 Tmp = isSigned
7217 ? DAG.getNode(ISD::SIGN_EXTEND_INREG, Tmp.getValueType(), Tmp,
7218 DAG.getValueType(SrcVT))
7219 : DAG.getZeroExtendInReg(Tmp, SrcVT);
Gabor Greifba36cb52008-08-28 21:40:38 +00007220 Node = DAG.UpdateNodeOperands(Op, Tmp).getNode();
Dale Johannesencf498192008-03-18 17:28:38 +00007221 SrcVT = Node->getOperand(0).getValueType();
7222 }
7223
Dan Gohmana2e94852008-03-10 23:03:31 +00007224 if (VT == MVT::ppcf128 && SrcVT == MVT::i32) {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007225 static const uint64_t zero = 0;
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007226 if (isSigned) {
7227 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7228 Node->getOperand(0)));
7229 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7230 } else {
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007231 static const uint64_t TwoE32[] = { 0x41f0000000000000LL, 0 };
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007232 Hi = LegalizeOp(DAG.getNode(ISD::SINT_TO_FP, MVT::f64,
7233 Node->getOperand(0)));
7234 Lo = DAG.getConstantFP(APFloat(APInt(64, 1, &zero)), MVT::f64);
7235 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
Dale Johannesen6e63e092007-10-12 17:52:03 +00007236 // X>=0 ? {(f64)x, 0} : {(f64)x, 0} + 2^32
Dale Johannesenca68aaa2007-10-12 01:37:08 +00007237 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7238 DAG.getConstant(0, MVT::i32),
7239 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7240 DAG.getConstantFP(
7241 APFloat(APInt(128, 2, TwoE32)),
7242 MVT::ppcf128)),
7243 Hi,
7244 DAG.getCondCode(ISD::SETLT)),
7245 Lo, Hi);
7246 }
7247 break;
7248 }
Dale Johannesen6e63e092007-10-12 17:52:03 +00007249 if (VT == MVT::ppcf128 && SrcVT == MVT::i64 && !isSigned) {
7250 // si64->ppcf128 done by libcall, below
Dan Gohmanf6283fd2008-02-25 21:39:34 +00007251 static const uint64_t TwoE64[] = { 0x43f0000000000000LL, 0 };
Dale Johannesen6e63e092007-10-12 17:52:03 +00007252 ExpandOp(DAG.getNode(ISD::SINT_TO_FP, MVT::ppcf128, Node->getOperand(0)),
7253 Lo, Hi);
7254 Hi = DAG.getNode(ISD::BUILD_PAIR, VT, Lo, Hi);
7255 // x>=0 ? (ppcf128)(i64)x : (ppcf128)(i64)x + 2^64
7256 ExpandOp(DAG.getNode(ISD::SELECT_CC, MVT::ppcf128, Node->getOperand(0),
7257 DAG.getConstant(0, MVT::i64),
7258 DAG.getNode(ISD::FADD, MVT::ppcf128, Hi,
7259 DAG.getConstantFP(
7260 APFloat(APInt(128, 2, TwoE64)),
7261 MVT::ppcf128)),
7262 Hi,
7263 DAG.getCondCode(ISD::SETLT)),
7264 Lo, Hi);
7265 break;
7266 }
Evan Cheng7df28dc2006-12-19 01:44:04 +00007267
Dan Gohmana2e94852008-03-10 23:03:31 +00007268 Lo = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, VT,
7269 Node->getOperand(0));
Evan Cheng110cf482008-04-01 01:50:16 +00007270 if (getTypeAction(Lo.getValueType()) == Expand)
Evan Cheng6ad2f932008-04-01 01:51:26 +00007271 // float to i32 etc. can be 'expanded' to a single node.
Evan Cheng110cf482008-04-01 01:50:16 +00007272 ExpandOp(Lo, Lo, Hi);
Evan Cheng7df28dc2006-12-19 01:44:04 +00007273 break;
7274 }
Evan Cheng98ff3b92006-12-13 02:38:13 +00007275 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00007276
Chris Lattner83397362005-12-21 18:02:52 +00007277 // Make sure the resultant values have been legalized themselves, unless this
7278 // is a type that requires multi-step expansion.
7279 if (getTypeAction(NVT) != Expand && NVT != MVT::isVoid) {
7280 Lo = LegalizeOp(Lo);
Gabor Greifba36cb52008-08-28 21:40:38 +00007281 if (Hi.getNode())
Evan Cheng13acce32006-12-11 19:27:14 +00007282 // Don't legalize the high part if it is expanded to a single node.
7283 Hi = LegalizeOp(Hi);
Chris Lattner83397362005-12-21 18:02:52 +00007284 }
Evan Cheng05a2d562006-01-09 18:31:59 +00007285
7286 // Remember in a map if the values will be reused later.
Dan Gohman6b345ee2008-07-07 17:46:23 +00007287 bool isNew =
7288 ExpandedNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Evan Cheng05a2d562006-01-09 18:31:59 +00007289 assert(isNew && "Value already expanded?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007290 isNew = isNew;
Chris Lattner3e928bb2005-01-07 07:47:09 +00007291}
7292
Dan Gohman7f321562007-06-25 16:23:39 +00007293/// SplitVectorOp - Given an operand of vector type, break it down into
7294/// two smaller values, still of vector type.
Dan Gohman475871a2008-07-27 21:46:04 +00007295void SelectionDAGLegalize::SplitVectorOp(SDValue Op, SDValue &Lo,
7296 SDValue &Hi) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007297 assert(Op.getValueType().isVector() && "Cannot split non-vector type!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007298 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007299 unsigned NumElements = Op.getValueType().getVectorNumElements();
Chris Lattnerc7029802006-03-18 01:44:44 +00007300 assert(NumElements > 1 && "Cannot split a single element vector!");
Nate Begeman5db1afb2007-11-15 21:15:26 +00007301
Duncan Sands83ec4b62008-06-06 12:08:01 +00007302 MVT NewEltVT = Op.getValueType().getVectorElementType();
Nate Begeman5db1afb2007-11-15 21:15:26 +00007303
7304 unsigned NewNumElts_Lo = 1 << Log2_32(NumElements-1);
7305 unsigned NewNumElts_Hi = NumElements - NewNumElts_Lo;
7306
Duncan Sands83ec4b62008-06-06 12:08:01 +00007307 MVT NewVT_Lo = MVT::getVectorVT(NewEltVT, NewNumElts_Lo);
7308 MVT NewVT_Hi = MVT::getVectorVT(NewEltVT, NewNumElts_Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007309
Chris Lattnerc7029802006-03-18 01:44:44 +00007310 // See if we already split it.
Dan Gohman475871a2008-07-27 21:46:04 +00007311 std::map<SDValue, std::pair<SDValue, SDValue> >::iterator I
Chris Lattnerc7029802006-03-18 01:44:44 +00007312 = SplitNodes.find(Op);
7313 if (I != SplitNodes.end()) {
7314 Lo = I->second.first;
7315 Hi = I->second.second;
7316 return;
7317 }
7318
7319 switch (Node->getOpcode()) {
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007320 default:
7321#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007322 Node->dump(&DAG);
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007323#endif
7324 assert(0 && "Unhandled operation in SplitVectorOp!");
Chris Lattnerdaf9bc82007-11-19 20:21:32 +00007325 case ISD::UNDEF:
7326 Lo = DAG.getNode(ISD::UNDEF, NewVT_Lo);
7327 Hi = DAG.getNode(ISD::UNDEF, NewVT_Hi);
7328 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007329 case ISD::BUILD_PAIR:
7330 Lo = Node->getOperand(0);
7331 Hi = Node->getOperand(1);
7332 break;
Dan Gohman9fe46622007-09-28 23:53:40 +00007333 case ISD::INSERT_VECTOR_ELT: {
Nate Begeman68679912008-04-25 18:07:40 +00007334 if (ConstantSDNode *Idx = dyn_cast<ConstantSDNode>(Node->getOperand(2))) {
7335 SplitVectorOp(Node->getOperand(0), Lo, Hi);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007336 unsigned Index = Idx->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007337 SDValue ScalarOp = Node->getOperand(1);
Nate Begeman68679912008-04-25 18:07:40 +00007338 if (Index < NewNumElts_Lo)
7339 Lo = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Lo, Lo, ScalarOp,
7340 DAG.getIntPtrConstant(Index));
7341 else
7342 Hi = DAG.getNode(ISD::INSERT_VECTOR_ELT, NewVT_Hi, Hi, ScalarOp,
7343 DAG.getIntPtrConstant(Index - NewNumElts_Lo));
7344 break;
7345 }
Dan Gohman475871a2008-07-27 21:46:04 +00007346 SDValue Tmp = PerformInsertVectorEltInMemory(Node->getOperand(0),
Nate Begeman68679912008-04-25 18:07:40 +00007347 Node->getOperand(1),
7348 Node->getOperand(2));
7349 SplitVectorOp(Tmp, Lo, Hi);
Dan Gohman9fe46622007-09-28 23:53:40 +00007350 break;
7351 }
Chris Lattner6c9c6802007-11-19 21:16:54 +00007352 case ISD::VECTOR_SHUFFLE: {
7353 // Build the low part.
Dan Gohman475871a2008-07-27 21:46:04 +00007354 SDValue Mask = Node->getOperand(2);
7355 SmallVector<SDValue, 8> Ops;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007356 MVT PtrVT = TLI.getPointerTy();
Chris Lattner6c9c6802007-11-19 21:16:54 +00007357
7358 // Insert all of the elements from the input that are needed. We use
7359 // buildvector of extractelement here because the input vectors will have
7360 // to be legalized, so this makes the code simpler.
7361 for (unsigned i = 0; i != NewNumElts_Lo; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007362 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007363 if (IdxNode.getOpcode() == ISD::UNDEF) {
7364 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7365 continue;
7366 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007367 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007368 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007369 if (Idx >= NumElements) {
7370 InVec = Node->getOperand(1);
7371 Idx -= NumElements;
7372 }
7373 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7374 DAG.getConstant(Idx, PtrVT)));
7375 }
7376 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &Ops[0], Ops.size());
7377 Ops.clear();
7378
7379 for (unsigned i = NewNumElts_Lo; i != NumElements; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007380 SDValue IdxNode = Mask.getOperand(i);
Nate Begeman5922f562008-03-14 00:53:31 +00007381 if (IdxNode.getOpcode() == ISD::UNDEF) {
7382 Ops.push_back(DAG.getNode(ISD::UNDEF, NewEltVT));
7383 continue;
7384 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007385 unsigned Idx = cast<ConstantSDNode>(IdxNode)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00007386 SDValue InVec = Node->getOperand(0);
Chris Lattner6c9c6802007-11-19 21:16:54 +00007387 if (Idx >= NumElements) {
7388 InVec = Node->getOperand(1);
7389 Idx -= NumElements;
7390 }
7391 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewEltVT, InVec,
7392 DAG.getConstant(Idx, PtrVT)));
7393 }
Mon P Wang92879f32008-07-25 01:30:26 +00007394 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &Ops[0], Ops.size());
Chris Lattner6c9c6802007-11-19 21:16:54 +00007395 break;
7396 }
Dan Gohman7f321562007-06-25 16:23:39 +00007397 case ISD::BUILD_VECTOR: {
Dan Gohman475871a2008-07-27 21:46:04 +00007398 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
Nate Begeman5db1afb2007-11-15 21:15:26 +00007399 Node->op_begin()+NewNumElts_Lo);
7400 Lo = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Lo, &LoOps[0], LoOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007401
Dan Gohman475871a2008-07-27 21:46:04 +00007402 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumElts_Lo,
Dan Gohman7f321562007-06-25 16:23:39 +00007403 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007404 Hi = DAG.getNode(ISD::BUILD_VECTOR, NewVT_Hi, &HiOps[0], HiOps.size());
Chris Lattnerc7029802006-03-18 01:44:44 +00007405 break;
7406 }
Dan Gohman7f321562007-06-25 16:23:39 +00007407 case ISD::CONCAT_VECTORS: {
Nate Begeman5db1afb2007-11-15 21:15:26 +00007408 // FIXME: Handle non-power-of-two vectors?
Dan Gohman7f321562007-06-25 16:23:39 +00007409 unsigned NewNumSubvectors = Node->getNumOperands() / 2;
7410 if (NewNumSubvectors == 1) {
7411 Lo = Node->getOperand(0);
7412 Hi = Node->getOperand(1);
7413 } else {
Mon P Wangaeb06d22008-11-10 04:46:22 +00007414 SmallVector<SDValue, 8> LoOps(Node->op_begin(),
7415 Node->op_begin()+NewNumSubvectors);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007416 Lo = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Lo, &LoOps[0], LoOps.size());
Dan Gohman65956352007-06-13 15:12:02 +00007417
Mon P Wangaeb06d22008-11-10 04:46:22 +00007418 SmallVector<SDValue, 8> HiOps(Node->op_begin()+NewNumSubvectors,
Dan Gohman7f321562007-06-25 16:23:39 +00007419 Node->op_end());
Nate Begeman5db1afb2007-11-15 21:15:26 +00007420 Hi = DAG.getNode(ISD::CONCAT_VECTORS, NewVT_Hi, &HiOps[0], HiOps.size());
Dan Gohman7f321562007-06-25 16:23:39 +00007421 }
Dan Gohman65956352007-06-13 15:12:02 +00007422 break;
7423 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00007424 case ISD::EXTRACT_SUBVECTOR: {
7425 SDValue Vec = Op.getOperand(0);
7426 SDValue Idx = Op.getOperand(1);
7427 MVT IdxVT = Idx.getValueType();
7428
7429 Lo = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Lo, Vec, Idx);
7430 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
7431 if (CIdx) {
7432 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec,
7433 DAG.getConstant(CIdx->getZExtValue() + NewNumElts_Lo,
7434 IdxVT));
7435 } else {
7436 Idx = DAG.getNode(ISD::ADD, IdxVT, Idx,
7437 DAG.getConstant(NewNumElts_Lo, IdxVT));
7438 Hi = DAG.getNode(ISD::EXTRACT_SUBVECTOR, NewVT_Hi, Vec, Idx);
7439 }
7440 break;
7441 }
Dan Gohmanc6230962007-10-17 14:48:28 +00007442 case ISD::SELECT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007443 SDValue Cond = Node->getOperand(0);
Dan Gohmanc6230962007-10-17 14:48:28 +00007444
Dan Gohman475871a2008-07-27 21:46:04 +00007445 SDValue LL, LH, RL, RH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007446 SplitVectorOp(Node->getOperand(1), LL, LH);
7447 SplitVectorOp(Node->getOperand(2), RL, RH);
7448
Duncan Sands83ec4b62008-06-06 12:08:01 +00007449 if (Cond.getValueType().isVector()) {
Dan Gohmanc6230962007-10-17 14:48:28 +00007450 // Handle a vector merge.
Dan Gohman475871a2008-07-27 21:46:04 +00007451 SDValue CL, CH;
Dan Gohmanc6230962007-10-17 14:48:28 +00007452 SplitVectorOp(Cond, CL, CH);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007453 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, CL, LL, RL);
7454 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, CH, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007455 } else {
7456 // Handle a simple select with vector operands.
Nate Begeman5db1afb2007-11-15 21:15:26 +00007457 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, Cond, LL, RL);
7458 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, Cond, LH, RH);
Dan Gohmanc6230962007-10-17 14:48:28 +00007459 }
7460 break;
7461 }
Chris Lattner80c1a562008-06-30 02:43:01 +00007462 case ISD::SELECT_CC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007463 SDValue CondLHS = Node->getOperand(0);
7464 SDValue CondRHS = Node->getOperand(1);
7465 SDValue CondCode = Node->getOperand(4);
Chris Lattner80c1a562008-06-30 02:43:01 +00007466
Dan Gohman475871a2008-07-27 21:46:04 +00007467 SDValue LL, LH, RL, RH;
Chris Lattner80c1a562008-06-30 02:43:01 +00007468 SplitVectorOp(Node->getOperand(2), LL, LH);
7469 SplitVectorOp(Node->getOperand(3), RL, RH);
7470
7471 // Handle a simple select with vector operands.
7472 Lo = DAG.getNode(ISD::SELECT_CC, NewVT_Lo, CondLHS, CondRHS,
7473 LL, RL, CondCode);
7474 Hi = DAG.getNode(ISD::SELECT_CC, NewVT_Hi, CondLHS, CondRHS,
7475 LH, RH, CondCode);
7476 break;
7477 }
Nate Begemanb43e9c12008-05-12 19:40:03 +00007478 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007479 SDValue LL, LH, RL, RH;
Nate Begemanb43e9c12008-05-12 19:40:03 +00007480 SplitVectorOp(Node->getOperand(0), LL, LH);
7481 SplitVectorOp(Node->getOperand(1), RL, RH);
7482 Lo = DAG.getNode(ISD::VSETCC, NewVT_Lo, LL, RL, Node->getOperand(2));
7483 Hi = DAG.getNode(ISD::VSETCC, NewVT_Hi, LH, RH, Node->getOperand(2));
7484 break;
7485 }
Dan Gohman7f321562007-06-25 16:23:39 +00007486 case ISD::ADD:
7487 case ISD::SUB:
7488 case ISD::MUL:
7489 case ISD::FADD:
7490 case ISD::FSUB:
7491 case ISD::FMUL:
7492 case ISD::SDIV:
7493 case ISD::UDIV:
7494 case ISD::FDIV:
Dan Gohman82669522007-10-11 23:57:53 +00007495 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007496 case ISD::AND:
7497 case ISD::OR:
Dan Gohman089617d2007-11-19 15:15:03 +00007498 case ISD::XOR:
7499 case ISD::UREM:
7500 case ISD::SREM:
7501 case ISD::FREM: {
Dan Gohman475871a2008-07-27 21:46:04 +00007502 SDValue LL, LH, RL, RH;
Chris Lattnerc7029802006-03-18 01:44:44 +00007503 SplitVectorOp(Node->getOperand(0), LL, LH);
7504 SplitVectorOp(Node->getOperand(1), RL, RH);
7505
Nate Begeman5db1afb2007-11-15 21:15:26 +00007506 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, LL, RL);
7507 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, LH, RH);
Chris Lattnerc7029802006-03-18 01:44:44 +00007508 break;
7509 }
Dan Gohman7f8613e2008-08-14 20:04:46 +00007510 case ISD::FP_ROUND:
Dan Gohman82669522007-10-11 23:57:53 +00007511 case ISD::FPOWI: {
Dan Gohman475871a2008-07-27 21:46:04 +00007512 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007513 SplitVectorOp(Node->getOperand(0), L, H);
7514
Nate Begeman5db1afb2007-11-15 21:15:26 +00007515 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L, Node->getOperand(1));
7516 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H, Node->getOperand(1));
Dan Gohman82669522007-10-11 23:57:53 +00007517 break;
7518 }
7519 case ISD::CTTZ:
7520 case ISD::CTLZ:
7521 case ISD::CTPOP:
7522 case ISD::FNEG:
7523 case ISD::FABS:
7524 case ISD::FSQRT:
7525 case ISD::FSIN:
Nate Begemanb348d182007-11-17 03:58:34 +00007526 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007527 case ISD::FLOG:
7528 case ISD::FLOG2:
7529 case ISD::FLOG10:
7530 case ISD::FEXP:
7531 case ISD::FEXP2:
Nate Begemanb348d182007-11-17 03:58:34 +00007532 case ISD::FP_TO_SINT:
7533 case ISD::FP_TO_UINT:
7534 case ISD::SINT_TO_FP:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007535 case ISD::UINT_TO_FP:
7536 case ISD::TRUNCATE:
7537 case ISD::ANY_EXTEND:
7538 case ISD::SIGN_EXTEND:
7539 case ISD::ZERO_EXTEND:
7540 case ISD::FP_EXTEND: {
Dan Gohman475871a2008-07-27 21:46:04 +00007541 SDValue L, H;
Dan Gohman82669522007-10-11 23:57:53 +00007542 SplitVectorOp(Node->getOperand(0), L, H);
7543
Nate Begeman5db1afb2007-11-15 21:15:26 +00007544 Lo = DAG.getNode(Node->getOpcode(), NewVT_Lo, L);
7545 Hi = DAG.getNode(Node->getOpcode(), NewVT_Hi, H);
Dan Gohman82669522007-10-11 23:57:53 +00007546 break;
7547 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007548 case ISD::CONVERT_RNDSAT: {
7549 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
7550 SDValue L, H;
7551 SplitVectorOp(Node->getOperand(0), L, H);
7552 SDValue DTyOpL = DAG.getValueType(NewVT_Lo);
7553 SDValue DTyOpH = DAG.getValueType(NewVT_Hi);
7554 SDValue STyOpL = DAG.getValueType(L.getValueType());
7555 SDValue STyOpH = DAG.getValueType(H.getValueType());
7556
7557 SDValue RndOp = Node->getOperand(3);
7558 SDValue SatOp = Node->getOperand(4);
7559
7560 Lo = DAG.getConvertRndSat(NewVT_Lo, L, DTyOpL, STyOpL,
7561 RndOp, SatOp, CvtCode);
7562 Hi = DAG.getConvertRndSat(NewVT_Hi, H, DTyOpH, STyOpH,
7563 RndOp, SatOp, CvtCode);
7564 break;
7565 }
Dan Gohman7f321562007-06-25 16:23:39 +00007566 case ISD::LOAD: {
7567 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007568 SDValue Ch = LD->getChain();
7569 SDValue Ptr = LD->getBasePtr();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007570 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007571 const Value *SV = LD->getSrcValue();
7572 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007573 MVT MemoryVT = LD->getMemoryVT();
Dan Gohman7f321562007-06-25 16:23:39 +00007574 unsigned Alignment = LD->getAlignment();
7575 bool isVolatile = LD->isVolatile();
7576
Dan Gohman7f8613e2008-08-14 20:04:46 +00007577 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7578 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7579
7580 MVT MemNewEltVT = MemoryVT.getVectorElementType();
7581 MVT MemNewVT_Lo = MVT::getVectorVT(MemNewEltVT, NewNumElts_Lo);
7582 MVT MemNewVT_Hi = MVT::getVectorVT(MemNewEltVT, NewNumElts_Hi);
7583
7584 Lo = DAG.getLoad(ISD::UNINDEXED, ExtType,
7585 NewVT_Lo, Ch, Ptr, Offset,
7586 SV, SVOffset, MemNewVT_Lo, isVolatile, Alignment);
7587 unsigned IncrementSize = NewNumElts_Lo * MemNewEltVT.getSizeInBits()/8;
Chris Lattnerc7029802006-03-18 01:44:44 +00007588 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
Chris Lattner0bd48932008-01-17 07:00:52 +00007589 DAG.getIntPtrConstant(IncrementSize));
Dan Gohman7f321562007-06-25 16:23:39 +00007590 SVOffset += IncrementSize;
Duncan Sandsdc846502007-10-28 12:59:45 +00007591 Alignment = MinAlign(Alignment, IncrementSize);
Dan Gohman7f8613e2008-08-14 20:04:46 +00007592 Hi = DAG.getLoad(ISD::UNINDEXED, ExtType,
7593 NewVT_Hi, Ch, Ptr, Offset,
7594 SV, SVOffset, MemNewVT_Hi, isVolatile, Alignment);
Chris Lattnerc7029802006-03-18 01:44:44 +00007595
7596 // Build a factor node to remember that this load is independent of the
7597 // other one.
Dan Gohman475871a2008-07-27 21:46:04 +00007598 SDValue TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
Chris Lattnerc7029802006-03-18 01:44:44 +00007599 Hi.getValue(1));
7600
7601 // Remember that we legalized the chain.
7602 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TF));
Chris Lattnerc7029802006-03-18 01:44:44 +00007603 break;
7604 }
Dan Gohman7f321562007-06-25 16:23:39 +00007605 case ISD::BIT_CONVERT: {
Chris Lattner7692eb42006-03-23 21:16:34 +00007606 // We know the result is a vector. The input may be either a vector or a
7607 // scalar value.
Dan Gohman475871a2008-07-27 21:46:04 +00007608 SDValue InOp = Node->getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007609 if (!InOp.getValueType().isVector() ||
7610 InOp.getValueType().getVectorNumElements() == 1) {
Dan Gohman10a7aa62007-06-29 00:09:08 +00007611 // The input is a scalar or single-element vector.
7612 // Lower to a store/load so that it can be split.
7613 // FIXME: this could be improved probably.
Mon P Wang2920d2b2008-07-15 05:28:34 +00007614 unsigned LdAlign = TLI.getTargetData()->getPrefTypeAlignment(
7615 Op.getValueType().getTypeForMVT());
Dan Gohman475871a2008-07-27 21:46:04 +00007616 SDValue Ptr = DAG.CreateStackTemporary(InOp.getValueType(), LdAlign);
Gabor Greifba36cb52008-08-28 21:40:38 +00007617 int FI = cast<FrameIndexSDNode>(Ptr.getNode())->getIndex();
Chris Lattner7692eb42006-03-23 21:16:34 +00007618
Dan Gohman475871a2008-07-27 21:46:04 +00007619 SDValue St = DAG.getStore(DAG.getEntryNode(),
Dan Gohman69de1932008-02-06 22:27:42 +00007620 InOp, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007621 PseudoSourceValue::getFixedStack(FI), 0);
Dan Gohman69de1932008-02-06 22:27:42 +00007622 InOp = DAG.getLoad(Op.getValueType(), St, Ptr,
Dan Gohmana54cf172008-07-11 22:44:52 +00007623 PseudoSourceValue::getFixedStack(FI), 0);
Chris Lattner7692eb42006-03-23 21:16:34 +00007624 }
Dan Gohman10a7aa62007-06-29 00:09:08 +00007625 // Split the vector and convert each of the pieces now.
7626 SplitVectorOp(InOp, Lo, Hi);
Nate Begeman5db1afb2007-11-15 21:15:26 +00007627 Lo = DAG.getNode(ISD::BIT_CONVERT, NewVT_Lo, Lo);
7628 Hi = DAG.getNode(ISD::BIT_CONVERT, NewVT_Hi, Hi);
Dan Gohman10a7aa62007-06-29 00:09:08 +00007629 break;
Chris Lattner7692eb42006-03-23 21:16:34 +00007630 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007631 }
7632
7633 // Remember in a map if the values will be reused later.
Chris Lattner40030bf2007-02-04 01:17:38 +00007634 bool isNew =
Chris Lattnerc7029802006-03-18 01:44:44 +00007635 SplitNodes.insert(std::make_pair(Op, std::make_pair(Lo, Hi))).second;
Dan Gohman10a7aa62007-06-29 00:09:08 +00007636 assert(isNew && "Value already split?!?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007637 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007638}
7639
7640
Dan Gohman89b20c02007-06-27 14:06:22 +00007641/// ScalarizeVectorOp - Given an operand of single-element vector type
7642/// (e.g. v1f32), convert it into the equivalent operation that returns a
7643/// scalar (e.g. f32) value.
Dan Gohman475871a2008-07-27 21:46:04 +00007644SDValue SelectionDAGLegalize::ScalarizeVectorOp(SDValue Op) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00007645 assert(Op.getValueType().isVector() && "Bad ScalarizeVectorOp invocation!");
Gabor Greifba36cb52008-08-28 21:40:38 +00007646 SDNode *Node = Op.getNode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007647 MVT NewVT = Op.getValueType().getVectorElementType();
7648 assert(Op.getValueType().getVectorNumElements() == 1);
Chris Lattnerc7029802006-03-18 01:44:44 +00007649
Dan Gohman7f321562007-06-25 16:23:39 +00007650 // See if we already scalarized it.
Dan Gohman475871a2008-07-27 21:46:04 +00007651 std::map<SDValue, SDValue>::iterator I = ScalarizedNodes.find(Op);
Dan Gohman7f321562007-06-25 16:23:39 +00007652 if (I != ScalarizedNodes.end()) return I->second;
Chris Lattnerc7029802006-03-18 01:44:44 +00007653
Dan Gohman475871a2008-07-27 21:46:04 +00007654 SDValue Result;
Chris Lattnerc7029802006-03-18 01:44:44 +00007655 switch (Node->getOpcode()) {
Chris Lattner2332b9f2006-03-19 01:17:20 +00007656 default:
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007657#ifndef NDEBUG
Dan Gohman39833582007-06-04 16:17:33 +00007658 Node->dump(&DAG); cerr << "\n";
Jim Laskeye37fe9b2006-07-11 17:58:07 +00007659#endif
Dan Gohman7f321562007-06-25 16:23:39 +00007660 assert(0 && "Unknown vector operation in ScalarizeVectorOp!");
7661 case ISD::ADD:
7662 case ISD::FADD:
7663 case ISD::SUB:
7664 case ISD::FSUB:
7665 case ISD::MUL:
7666 case ISD::FMUL:
7667 case ISD::SDIV:
7668 case ISD::UDIV:
7669 case ISD::FDIV:
7670 case ISD::SREM:
7671 case ISD::UREM:
7672 case ISD::FREM:
Dan Gohman82669522007-10-11 23:57:53 +00007673 case ISD::FPOW:
Dan Gohman7f321562007-06-25 16:23:39 +00007674 case ISD::AND:
7675 case ISD::OR:
7676 case ISD::XOR:
7677 Result = DAG.getNode(Node->getOpcode(),
Chris Lattnerc7029802006-03-18 01:44:44 +00007678 NewVT,
Dan Gohman7f321562007-06-25 16:23:39 +00007679 ScalarizeVectorOp(Node->getOperand(0)),
7680 ScalarizeVectorOp(Node->getOperand(1)));
Chris Lattnerc7029802006-03-18 01:44:44 +00007681 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007682 case ISD::FNEG:
7683 case ISD::FABS:
7684 case ISD::FSQRT:
7685 case ISD::FSIN:
7686 case ISD::FCOS:
Dale Johannesen7794f2a2008-09-04 00:47:13 +00007687 case ISD::FLOG:
7688 case ISD::FLOG2:
7689 case ISD::FLOG10:
7690 case ISD::FEXP:
7691 case ISD::FEXP2:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007692 case ISD::FP_TO_SINT:
7693 case ISD::FP_TO_UINT:
7694 case ISD::SINT_TO_FP:
7695 case ISD::UINT_TO_FP:
7696 case ISD::SIGN_EXTEND:
7697 case ISD::ZERO_EXTEND:
7698 case ISD::ANY_EXTEND:
7699 case ISD::TRUNCATE:
7700 case ISD::FP_EXTEND:
Dan Gohman7f321562007-06-25 16:23:39 +00007701 Result = DAG.getNode(Node->getOpcode(),
7702 NewVT,
7703 ScalarizeVectorOp(Node->getOperand(0)));
7704 break;
Mon P Wang77cdf302008-11-10 20:54:11 +00007705 case ISD::CONVERT_RNDSAT: {
7706 SDValue Op0 = ScalarizeVectorOp(Node->getOperand(0));
7707 Result = DAG.getConvertRndSat(NewVT, Op0,
7708 DAG.getValueType(NewVT),
7709 DAG.getValueType(Op0.getValueType()),
7710 Node->getOperand(3),
7711 Node->getOperand(4),
7712 cast<CvtRndSatSDNode>(Node)->getCvtCode());
7713 break;
7714 }
Dan Gohman9e04c822007-10-12 14:13:46 +00007715 case ISD::FPOWI:
Dan Gohman7f8613e2008-08-14 20:04:46 +00007716 case ISD::FP_ROUND:
Dan Gohman9e04c822007-10-12 14:13:46 +00007717 Result = DAG.getNode(Node->getOpcode(),
7718 NewVT,
7719 ScalarizeVectorOp(Node->getOperand(0)),
7720 Node->getOperand(1));
7721 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007722 case ISD::LOAD: {
7723 LoadSDNode *LD = cast<LoadSDNode>(Node);
Dan Gohman475871a2008-07-27 21:46:04 +00007724 SDValue Ch = LegalizeOp(LD->getChain()); // Legalize the chain.
7725 SDValue Ptr = LegalizeOp(LD->getBasePtr()); // Legalize the pointer.
Dan Gohman7f8613e2008-08-14 20:04:46 +00007726 ISD::LoadExtType ExtType = LD->getExtensionType();
Dan Gohman7f321562007-06-25 16:23:39 +00007727 const Value *SV = LD->getSrcValue();
7728 int SVOffset = LD->getSrcValueOffset();
Dan Gohman7f8613e2008-08-14 20:04:46 +00007729 MVT MemoryVT = LD->getMemoryVT();
7730 unsigned Alignment = LD->getAlignment();
7731 bool isVolatile = LD->isVolatile();
7732
7733 assert(LD->isUnindexed() && "Indexed vector loads are not supported yet!");
7734 SDValue Offset = DAG.getNode(ISD::UNDEF, Ptr.getValueType());
7735
7736 Result = DAG.getLoad(ISD::UNINDEXED, ExtType,
7737 NewVT, Ch, Ptr, Offset, SV, SVOffset,
7738 MemoryVT.getVectorElementType(),
7739 isVolatile, Alignment);
Dan Gohman7f321562007-06-25 16:23:39 +00007740
Chris Lattnerc7029802006-03-18 01:44:44 +00007741 // Remember that we legalized the chain.
7742 AddLegalizedOperand(Op.getValue(1), LegalizeOp(Result.getValue(1)));
7743 break;
7744 }
Dan Gohman7f321562007-06-25 16:23:39 +00007745 case ISD::BUILD_VECTOR:
7746 Result = Node->getOperand(0);
Chris Lattnerc7029802006-03-18 01:44:44 +00007747 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007748 case ISD::INSERT_VECTOR_ELT:
7749 // Returning the inserted scalar element.
7750 Result = Node->getOperand(1);
7751 break;
7752 case ISD::CONCAT_VECTORS:
Dan Gohman65956352007-06-13 15:12:02 +00007753 assert(Node->getOperand(0).getValueType() == NewVT &&
7754 "Concat of non-legal vectors not yet supported!");
7755 Result = Node->getOperand(0);
7756 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007757 case ISD::VECTOR_SHUFFLE: {
7758 // Figure out if the scalar is the LHS or RHS and return it.
Dan Gohman475871a2008-07-27 21:46:04 +00007759 SDValue EltNum = Node->getOperand(2).getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007760 if (cast<ConstantSDNode>(EltNum)->getZExtValue())
Dan Gohman7f321562007-06-25 16:23:39 +00007761 Result = ScalarizeVectorOp(Node->getOperand(1));
7762 else
7763 Result = ScalarizeVectorOp(Node->getOperand(0));
Chris Lattner2332b9f2006-03-19 01:17:20 +00007764 break;
Dan Gohman7f321562007-06-25 16:23:39 +00007765 }
7766 case ISD::EXTRACT_SUBVECTOR:
Mon P Wange0b436a2008-11-06 22:52:21 +00007767 Result = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, NewVT, Node->getOperand(0),
Mon P Wangaeb06d22008-11-10 04:46:22 +00007768 Node->getOperand(1));
Dan Gohman65956352007-06-13 15:12:02 +00007769 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007770 case ISD::BIT_CONVERT: {
Dan Gohman475871a2008-07-27 21:46:04 +00007771 SDValue Op0 = Op.getOperand(0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007772 if (Op0.getValueType().getVectorNumElements() == 1)
Evan Cheng446efdd2008-05-16 17:19:05 +00007773 Op0 = ScalarizeVectorOp(Op0);
7774 Result = DAG.getNode(ISD::BIT_CONVERT, NewVT, Op0);
Chris Lattner5b2316e2006-03-28 20:24:43 +00007775 break;
Evan Cheng446efdd2008-05-16 17:19:05 +00007776 }
Dan Gohman7f321562007-06-25 16:23:39 +00007777 case ISD::SELECT:
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007778 Result = DAG.getNode(ISD::SELECT, NewVT, Op.getOperand(0),
Dan Gohman7f321562007-06-25 16:23:39 +00007779 ScalarizeVectorOp(Op.getOperand(1)),
7780 ScalarizeVectorOp(Op.getOperand(2)));
Chris Lattnerb22e35a2006-04-08 22:22:57 +00007781 break;
Chris Lattner80c1a562008-06-30 02:43:01 +00007782 case ISD::SELECT_CC:
7783 Result = DAG.getNode(ISD::SELECT_CC, NewVT, Node->getOperand(0),
7784 Node->getOperand(1),
7785 ScalarizeVectorOp(Op.getOperand(2)),
7786 ScalarizeVectorOp(Op.getOperand(3)),
7787 Node->getOperand(4));
7788 break;
Nate Begeman0d1704b2008-05-12 23:09:43 +00007789 case ISD::VSETCC: {
Dan Gohman475871a2008-07-27 21:46:04 +00007790 SDValue Op0 = ScalarizeVectorOp(Op.getOperand(0));
7791 SDValue Op1 = ScalarizeVectorOp(Op.getOperand(1));
Nate Begeman0d1704b2008-05-12 23:09:43 +00007792 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultType(Op0), Op0, Op1,
7793 Op.getOperand(2));
7794 Result = DAG.getNode(ISD::SELECT, NewVT, Result,
7795 DAG.getConstant(-1ULL, NewVT),
7796 DAG.getConstant(0ULL, NewVT));
7797 break;
7798 }
Chris Lattnerc7029802006-03-18 01:44:44 +00007799 }
7800
7801 if (TLI.isTypeLegal(NewVT))
7802 Result = LegalizeOp(Result);
Dan Gohman7f321562007-06-25 16:23:39 +00007803 bool isNew = ScalarizedNodes.insert(std::make_pair(Op, Result)).second;
7804 assert(isNew && "Value already scalarized?");
Evan Cheng24ac4082008-11-24 07:09:49 +00007805 isNew = isNew;
Chris Lattnerc7029802006-03-18 01:44:44 +00007806 return Result;
7807}
7808
Chris Lattner3e928bb2005-01-07 07:47:09 +00007809
Mon P Wang0c397192008-10-30 08:01:45 +00007810SDValue SelectionDAGLegalize::WidenVectorOp(SDValue Op, MVT WidenVT) {
7811 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(Op);
7812 if (I != WidenNodes.end()) return I->second;
7813
7814 MVT VT = Op.getValueType();
7815 assert(VT.isVector() && "Cannot widen non-vector type!");
7816
7817 SDValue Result;
7818 SDNode *Node = Op.getNode();
7819 MVT EVT = VT.getVectorElementType();
7820
7821 unsigned NumElts = VT.getVectorNumElements();
7822 unsigned NewNumElts = WidenVT.getVectorNumElements();
7823 assert(NewNumElts > NumElts && "Cannot widen to smaller type!");
7824 assert(NewNumElts < 17);
7825
7826 // When widen is called, it is assumed that it is more efficient to use a
7827 // wide type. The default action is to widen to operation to a wider legal
7828 // vector type and then do the operation if it is legal by calling LegalizeOp
7829 // again. If there is no vector equivalent, we will unroll the operation, do
7830 // it, and rebuild the vector. If most of the operations are vectorizible to
7831 // the legal type, the resulting code will be more efficient. If this is not
7832 // the case, the resulting code will preform badly as we end up generating
7833 // code to pack/unpack the results. It is the function that calls widen
Mon P Wangf007a8b2008-11-06 05:31:54 +00007834 // that is responsible for seeing this doesn't happen.
Mon P Wang0c397192008-10-30 08:01:45 +00007835 switch (Node->getOpcode()) {
7836 default:
7837#ifndef NDEBUG
7838 Node->dump(&DAG);
7839#endif
7840 assert(0 && "Unexpected operation in WidenVectorOp!");
7841 break;
7842 case ISD::CopyFromReg:
Mon P Wang49292f12008-11-15 06:05:52 +00007843 assert(0 && "CopyFromReg doesn't need widening!");
Mon P Wang0c397192008-10-30 08:01:45 +00007844 case ISD::Constant:
7845 case ISD::ConstantFP:
7846 // To build a vector of these elements, clients should call BuildVector
7847 // and with each element instead of creating a node with a vector type
7848 assert(0 && "Unexpected operation in WidenVectorOp!");
7849 case ISD::VAARG:
7850 // Variable Arguments with vector types doesn't make any sense to me
7851 assert(0 && "Unexpected operation in WidenVectorOp!");
7852 break;
Mon P Wang49292f12008-11-15 06:05:52 +00007853 case ISD::UNDEF:
7854 Result = DAG.getNode(ISD::UNDEF, WidenVT);
7855 break;
Mon P Wang0c397192008-10-30 08:01:45 +00007856 case ISD::BUILD_VECTOR: {
7857 // Build a vector with undefined for the new nodes
7858 SDValueVector NewOps(Node->op_begin(), Node->op_end());
7859 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7860 NewOps.push_back(DAG.getNode(ISD::UNDEF,EVT));
7861 }
7862 Result = DAG.getNode(ISD::BUILD_VECTOR, WidenVT, &NewOps[0], NewOps.size());
7863 break;
7864 }
7865 case ISD::INSERT_VECTOR_ELT: {
7866 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7867 Result = DAG.getNode(ISD::INSERT_VECTOR_ELT, WidenVT, Tmp1,
7868 Node->getOperand(1), Node->getOperand(2));
7869 break;
7870 }
7871 case ISD::VECTOR_SHUFFLE: {
7872 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7873 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
7874 // VECTOR_SHUFFLE 3rd operand must be a constant build vector that is
7875 // used as permutation array. We build the vector here instead of widening
7876 // because we don't want to legalize and have it turned to something else.
7877 SDValue PermOp = Node->getOperand(2);
7878 SDValueVector NewOps;
7879 MVT PVT = PermOp.getValueType().getVectorElementType();
7880 for (unsigned i = 0; i < NumElts; ++i) {
7881 if (PermOp.getOperand(i).getOpcode() == ISD::UNDEF) {
7882 NewOps.push_back(PermOp.getOperand(i));
7883 } else {
7884 unsigned Idx =
7885 cast<ConstantSDNode>(PermOp.getOperand(i))->getZExtValue();
7886 if (Idx < NumElts) {
7887 NewOps.push_back(PermOp.getOperand(i));
7888 }
7889 else {
7890 NewOps.push_back(DAG.getConstant(Idx + NewNumElts - NumElts,
7891 PermOp.getOperand(i).getValueType()));
7892 }
7893 }
7894 }
7895 for (unsigned i = NumElts; i < NewNumElts; ++i) {
7896 NewOps.push_back(DAG.getNode(ISD::UNDEF,PVT));
7897 }
7898
7899 SDValue Tmp3 = DAG.getNode(ISD::BUILD_VECTOR,
7900 MVT::getVectorVT(PVT, NewOps.size()),
7901 &NewOps[0], NewOps.size());
7902
7903 Result = DAG.getNode(ISD::VECTOR_SHUFFLE, WidenVT, Tmp1, Tmp2, Tmp3);
7904 break;
7905 }
7906 case ISD::LOAD: {
7907 // If the load widen returns true, we can use a single load for the
7908 // vector. Otherwise, it is returning a token factor for multiple
7909 // loads.
7910 SDValue TFOp;
7911 if (LoadWidenVectorOp(Result, TFOp, Op, WidenVT))
7912 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(1)));
7913 else
7914 AddLegalizedOperand(Op.getValue(1), LegalizeOp(TFOp.getValue(0)));
7915 break;
7916 }
7917
7918 case ISD::BIT_CONVERT: {
7919 SDValue Tmp1 = Node->getOperand(0);
7920 // Converts between two different types so we need to determine
7921 // the correct widen type for the input operand.
7922 MVT TVT = Tmp1.getValueType();
7923 assert(TVT.isVector() && "can not widen non vector type");
7924 MVT TEVT = TVT.getVectorElementType();
7925 assert(WidenVT.getSizeInBits() % EVT.getSizeInBits() == 0 &&
7926 "can not widen bit bit convert that are not multiple of element type");
7927 MVT TWidenVT = MVT::getVectorVT(TEVT,
7928 WidenVT.getSizeInBits()/EVT.getSizeInBits());
7929 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7930 assert(Tmp1.getValueType().getSizeInBits() == WidenVT.getSizeInBits());
7931 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
7932
7933 TargetLowering::LegalizeAction action =
7934 TLI.getOperationAction(Node->getOpcode(), WidenVT);
7935 switch (action) {
7936 default: assert(0 && "action not supported");
7937 case TargetLowering::Legal:
7938 break;
7939 case TargetLowering::Promote:
7940 // We defer the promotion to when we legalize the op
7941 break;
7942 case TargetLowering::Expand:
7943 // Expand the operation into a bunch of nasty scalar code.
7944 Result = LegalizeOp(UnrollVectorOp(Result));
7945 break;
7946 }
7947 break;
7948 }
7949
7950 case ISD::SINT_TO_FP:
7951 case ISD::UINT_TO_FP:
7952 case ISD::FP_TO_SINT:
7953 case ISD::FP_TO_UINT: {
7954 SDValue Tmp1 = Node->getOperand(0);
7955 // Converts between two different types so we need to determine
7956 // the correct widen type for the input operand.
7957 MVT TVT = Tmp1.getValueType();
7958 assert(TVT.isVector() && "can not widen non vector type");
7959 MVT TEVT = TVT.getVectorElementType();
7960 MVT TWidenVT = MVT::getVectorVT(TEVT, NewNumElts);
7961 Tmp1 = WidenVectorOp(Tmp1, TWidenVT);
7962 assert(Tmp1.getValueType().getVectorNumElements() == NewNumElts);
7963 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00007964 break;
7965 }
7966
7967 case ISD::FP_EXTEND:
7968 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
7969 case ISD::TRUNCATE:
7970 case ISD::SIGN_EXTEND:
7971 case ISD::ZERO_EXTEND:
7972 case ISD::ANY_EXTEND:
7973 case ISD::FP_ROUND:
7974 case ISD::SIGN_EXTEND_INREG:
7975 case ISD::FABS:
7976 case ISD::FNEG:
7977 case ISD::FSQRT:
7978 case ISD::FSIN:
Mon P Wang49292f12008-11-15 06:05:52 +00007979 case ISD::FCOS:
7980 case ISD::CTPOP:
7981 case ISD::CTTZ:
7982 case ISD::CTLZ: {
Mon P Wang0c397192008-10-30 08:01:45 +00007983 // Unary op widening
7984 SDValue Tmp1;
Mon P Wang0c397192008-10-30 08:01:45 +00007985 Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
7986 assert(Tmp1.getValueType() == WidenVT);
7987 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1);
Mon P Wang0c397192008-10-30 08:01:45 +00007988 break;
7989 }
Mon P Wang77cdf302008-11-10 20:54:11 +00007990 case ISD::CONVERT_RNDSAT: {
7991 SDValue RndOp = Node->getOperand(3);
7992 SDValue SatOp = Node->getOperand(4);
Mon P Wang77cdf302008-11-10 20:54:11 +00007993 SDValue SrcOp = Node->getOperand(0);
7994
7995 // Converts between two different types so we need to determine
7996 // the correct widen type for the input operand.
7997 MVT SVT = SrcOp.getValueType();
7998 assert(SVT.isVector() && "can not widen non vector type");
7999 MVT SEVT = SVT.getVectorElementType();
8000 MVT SWidenVT = MVT::getVectorVT(SEVT, NewNumElts);
8001
8002 SrcOp = WidenVectorOp(SrcOp, SWidenVT);
8003 assert(SrcOp.getValueType() == WidenVT);
8004 SDValue DTyOp = DAG.getValueType(WidenVT);
8005 SDValue STyOp = DAG.getValueType(SrcOp.getValueType());
8006 ISD::CvtCode CvtCode = cast<CvtRndSatSDNode>(Node)->getCvtCode();
8007
8008 Result = DAG.getConvertRndSat(WidenVT, SrcOp, DTyOp, STyOp,
8009 RndOp, SatOp, CvtCode);
Mon P Wang77cdf302008-11-10 20:54:11 +00008010 break;
8011 }
Mon P Wang0c397192008-10-30 08:01:45 +00008012 case ISD::FPOW:
8013 case ISD::FPOWI:
8014 case ISD::ADD:
8015 case ISD::SUB:
8016 case ISD::MUL:
8017 case ISD::MULHS:
8018 case ISD::MULHU:
8019 case ISD::AND:
8020 case ISD::OR:
8021 case ISD::XOR:
8022 case ISD::FADD:
8023 case ISD::FSUB:
8024 case ISD::FMUL:
8025 case ISD::SDIV:
8026 case ISD::SREM:
8027 case ISD::FDIV:
8028 case ISD::FREM:
8029 case ISD::FCOPYSIGN:
8030 case ISD::UDIV:
8031 case ISD::UREM:
8032 case ISD::BSWAP: {
8033 // Binary op widening
Mon P Wang0c397192008-10-30 08:01:45 +00008034 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8035 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), WidenVT);
8036 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8037 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008038 break;
8039 }
8040
8041 case ISD::SHL:
8042 case ISD::SRA:
8043 case ISD::SRL: {
Mon P Wang0c397192008-10-30 08:01:45 +00008044 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8045 assert(Tmp1.getValueType() == WidenVT);
Mon P Wangfb13f002008-12-02 07:35:08 +00008046 SDValue ShOp = Node->getOperand(1);
8047 MVT ShVT = ShOp.getValueType();
8048 MVT NewShVT = MVT::getVectorVT(ShVT.getVectorElementType(),
8049 WidenVT.getVectorNumElements());
8050 ShOp = WidenVectorOp(ShOp, NewShVT);
8051 assert(ShOp.getValueType() == NewShVT);
8052 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, ShOp);
Mon P Wang0c397192008-10-30 08:01:45 +00008053 break;
8054 }
Mon P Wangfb13f002008-12-02 07:35:08 +00008055
Mon P Wang0c397192008-10-30 08:01:45 +00008056 case ISD::EXTRACT_VECTOR_ELT: {
8057 SDValue Tmp1 = WidenVectorOp(Node->getOperand(0), WidenVT);
8058 assert(Tmp1.getValueType() == WidenVT);
8059 Result = DAG.getNode(Node->getOpcode(), EVT, Tmp1, Node->getOperand(1));
8060 break;
8061 }
8062 case ISD::CONCAT_VECTORS: {
8063 // We concurrently support only widen on a multiple of the incoming vector.
8064 // We could widen on a multiple of the incoming operand if necessary.
8065 unsigned NumConcat = NewNumElts / NumElts;
8066 assert(NewNumElts % NumElts == 0 && "Can widen only a multiple of vector");
Mon P Wangfb13f002008-12-02 07:35:08 +00008067 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
Mon P Wang0c397192008-10-30 08:01:45 +00008068 SmallVector<SDValue, 8> MOps;
8069 MOps.push_back(Op);
8070 for (unsigned i = 1; i != NumConcat; ++i) {
8071 MOps.push_back(UndefVal);
8072 }
8073 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8074 &MOps[0], MOps.size()));
8075 break;
8076 }
8077 case ISD::EXTRACT_SUBVECTOR: {
Mon P Wang49292f12008-11-15 06:05:52 +00008078 SDValue Tmp1 = Node->getOperand(0);
8079 SDValue Idx = Node->getOperand(1);
8080 ConstantSDNode *CIdx = dyn_cast<ConstantSDNode>(Idx);
8081 if (CIdx && CIdx->getZExtValue() == 0) {
8082 // Since we are access the start of the vector, the incoming
8083 // vector type might be the proper.
8084 MVT Tmp1VT = Tmp1.getValueType();
8085 if (Tmp1VT == WidenVT)
8086 return Tmp1;
8087 else {
8088 unsigned Tmp1VTNumElts = Tmp1VT.getVectorNumElements();
8089 if (Tmp1VTNumElts < NewNumElts)
8090 Result = WidenVectorOp(Tmp1, WidenVT);
8091 else
8092 Result = DAG.getNode(ISD::EXTRACT_SUBVECTOR, WidenVT, Tmp1, Idx);
8093 }
8094 } else if (NewNumElts % NumElts == 0) {
8095 // Widen the extracted subvector.
8096 unsigned NumConcat = NewNumElts / NumElts;
8097 SDValue UndefVal = DAG.getNode(ISD::UNDEF, VT);
8098 SmallVector<SDValue, 8> MOps;
8099 MOps.push_back(Op);
8100 for (unsigned i = 1; i != NumConcat; ++i) {
8101 MOps.push_back(UndefVal);
8102 }
8103 Result = LegalizeOp(DAG.getNode(ISD::CONCAT_VECTORS, WidenVT,
8104 &MOps[0], MOps.size()));
8105 } else {
8106 assert(0 && "can not widen extract subvector");
8107 // This could be implemented using insert and build vector but I would
8108 // like to see when this happens.
8109 }
Mon P Wang0c397192008-10-30 08:01:45 +00008110 break;
8111 }
8112
8113 case ISD::SELECT: {
Mon P Wang0c397192008-10-30 08:01:45 +00008114 // Determine new condition widen type and widen
8115 SDValue Cond1 = Node->getOperand(0);
8116 MVT CondVT = Cond1.getValueType();
8117 assert(CondVT.isVector() && "can not widen non vector type");
8118 MVT CondEVT = CondVT.getVectorElementType();
8119 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8120 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8121 assert(Cond1.getValueType() == CondWidenVT && "Condition not widen");
8122
8123 SDValue Tmp1 = WidenVectorOp(Node->getOperand(1), WidenVT);
8124 SDValue Tmp2 = WidenVectorOp(Node->getOperand(2), WidenVT);
8125 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT);
8126 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Tmp1, Tmp2);
Mon P Wang0c397192008-10-30 08:01:45 +00008127 break;
8128 }
8129
8130 case ISD::SELECT_CC: {
Mon P Wang0c397192008-10-30 08:01:45 +00008131 // Determine new condition widen type and widen
8132 SDValue Cond1 = Node->getOperand(0);
8133 SDValue Cond2 = Node->getOperand(1);
8134 MVT CondVT = Cond1.getValueType();
8135 assert(CondVT.isVector() && "can not widen non vector type");
8136 assert(CondVT == Cond2.getValueType() && "mismatch lhs/rhs");
8137 MVT CondEVT = CondVT.getVectorElementType();
8138 MVT CondWidenVT = MVT::getVectorVT(CondEVT, NewNumElts);
8139 Cond1 = WidenVectorOp(Cond1, CondWidenVT);
8140 Cond2 = WidenVectorOp(Cond2, CondWidenVT);
8141 assert(Cond1.getValueType() == CondWidenVT &&
8142 Cond2.getValueType() == CondWidenVT && "condition not widen");
8143
8144 SDValue Tmp1 = WidenVectorOp(Node->getOperand(2), WidenVT);
8145 SDValue Tmp2 = WidenVectorOp(Node->getOperand(3), WidenVT);
8146 assert(Tmp1.getValueType() == WidenVT && Tmp2.getValueType() == WidenVT &&
8147 "operands not widen");
8148 Result = DAG.getNode(Node->getOpcode(), WidenVT, Cond1, Cond2, Tmp1,
8149 Tmp2, Node->getOperand(4));
Mon P Wang0c397192008-10-30 08:01:45 +00008150 break;
Mon P Wang2eb13c32008-10-30 18:21:52 +00008151 }
8152 case ISD::VSETCC: {
8153 // Determine widen for the operand
8154 SDValue Tmp1 = Node->getOperand(0);
8155 MVT TmpVT = Tmp1.getValueType();
8156 assert(TmpVT.isVector() && "can not widen non vector type");
8157 MVT TmpEVT = TmpVT.getVectorElementType();
8158 MVT TmpWidenVT = MVT::getVectorVT(TmpEVT, NewNumElts);
8159 Tmp1 = WidenVectorOp(Tmp1, TmpWidenVT);
8160 SDValue Tmp2 = WidenVectorOp(Node->getOperand(1), TmpWidenVT);
8161 Result = DAG.getNode(Node->getOpcode(), WidenVT, Tmp1, Tmp2,
8162 Node->getOperand(2));
Mon P Wang0c397192008-10-30 08:01:45 +00008163 break;
8164 }
Mon P Wang0c397192008-10-30 08:01:45 +00008165 case ISD::ATOMIC_CMP_SWAP_8:
8166 case ISD::ATOMIC_CMP_SWAP_16:
8167 case ISD::ATOMIC_CMP_SWAP_32:
8168 case ISD::ATOMIC_CMP_SWAP_64:
8169 case ISD::ATOMIC_LOAD_ADD_8:
8170 case ISD::ATOMIC_LOAD_SUB_8:
8171 case ISD::ATOMIC_LOAD_AND_8:
8172 case ISD::ATOMIC_LOAD_OR_8:
8173 case ISD::ATOMIC_LOAD_XOR_8:
8174 case ISD::ATOMIC_LOAD_NAND_8:
8175 case ISD::ATOMIC_LOAD_MIN_8:
8176 case ISD::ATOMIC_LOAD_MAX_8:
8177 case ISD::ATOMIC_LOAD_UMIN_8:
8178 case ISD::ATOMIC_LOAD_UMAX_8:
8179 case ISD::ATOMIC_SWAP_8:
8180 case ISD::ATOMIC_LOAD_ADD_16:
8181 case ISD::ATOMIC_LOAD_SUB_16:
8182 case ISD::ATOMIC_LOAD_AND_16:
8183 case ISD::ATOMIC_LOAD_OR_16:
8184 case ISD::ATOMIC_LOAD_XOR_16:
8185 case ISD::ATOMIC_LOAD_NAND_16:
8186 case ISD::ATOMIC_LOAD_MIN_16:
8187 case ISD::ATOMIC_LOAD_MAX_16:
8188 case ISD::ATOMIC_LOAD_UMIN_16:
8189 case ISD::ATOMIC_LOAD_UMAX_16:
8190 case ISD::ATOMIC_SWAP_16:
8191 case ISD::ATOMIC_LOAD_ADD_32:
8192 case ISD::ATOMIC_LOAD_SUB_32:
8193 case ISD::ATOMIC_LOAD_AND_32:
8194 case ISD::ATOMIC_LOAD_OR_32:
8195 case ISD::ATOMIC_LOAD_XOR_32:
8196 case ISD::ATOMIC_LOAD_NAND_32:
8197 case ISD::ATOMIC_LOAD_MIN_32:
8198 case ISD::ATOMIC_LOAD_MAX_32:
8199 case ISD::ATOMIC_LOAD_UMIN_32:
8200 case ISD::ATOMIC_LOAD_UMAX_32:
8201 case ISD::ATOMIC_SWAP_32:
8202 case ISD::ATOMIC_LOAD_ADD_64:
8203 case ISD::ATOMIC_LOAD_SUB_64:
8204 case ISD::ATOMIC_LOAD_AND_64:
8205 case ISD::ATOMIC_LOAD_OR_64:
8206 case ISD::ATOMIC_LOAD_XOR_64:
8207 case ISD::ATOMIC_LOAD_NAND_64:
8208 case ISD::ATOMIC_LOAD_MIN_64:
8209 case ISD::ATOMIC_LOAD_MAX_64:
8210 case ISD::ATOMIC_LOAD_UMIN_64:
8211 case ISD::ATOMIC_LOAD_UMAX_64:
8212 case ISD::ATOMIC_SWAP_64: {
8213 // For now, we assume that using vectors for these operations don't make
8214 // much sense so we just split it. We return an empty result
8215 SDValue X, Y;
8216 SplitVectorOp(Op, X, Y);
8217 return Result;
8218 break;
8219 }
8220
8221 } // end switch (Node->getOpcode())
8222
8223 assert(Result.getNode() && "Didn't set a result!");
8224 if (Result != Op)
8225 Result = LegalizeOp(Result);
8226
Mon P Wangf007a8b2008-11-06 05:31:54 +00008227 AddWidenedOperand(Op, Result);
Mon P Wang0c397192008-10-30 08:01:45 +00008228 return Result;
8229}
8230
8231// Utility function to find a legal vector type and its associated element
8232// type from a preferred width and whose vector type must be the same size
8233// as the VVT.
8234// TLI: Target lowering used to determine legal types
8235// Width: Preferred width of element type
8236// VVT: Vector value type whose size we must match.
8237// Returns VecEVT and EVT - the vector type and its associated element type
8238static void FindWidenVecType(TargetLowering &TLI, unsigned Width, MVT VVT,
8239 MVT& EVT, MVT& VecEVT) {
8240 // We start with the preferred width, make it a power of 2 and see if
8241 // we can find a vector type of that width. If not, we reduce it by
8242 // another power of 2. If we have widen the type, a vector of bytes should
8243 // always be legal.
8244 assert(TLI.isTypeLegal(VVT));
8245 unsigned EWidth = Width + 1;
8246 do {
8247 assert(EWidth > 0);
8248 EWidth = (1 << Log2_32(EWidth-1));
8249 EVT = MVT::getIntegerVT(EWidth);
8250 unsigned NumEVT = VVT.getSizeInBits()/EWidth;
8251 VecEVT = MVT::getVectorVT(EVT, NumEVT);
8252 } while (!TLI.isTypeLegal(VecEVT) ||
8253 VVT.getSizeInBits() != VecEVT.getSizeInBits());
8254}
8255
8256SDValue SelectionDAGLegalize::genWidenVectorLoads(SDValueVector& LdChain,
8257 SDValue Chain,
8258 SDValue BasePtr,
8259 const Value *SV,
8260 int SVOffset,
8261 unsigned Alignment,
8262 bool isVolatile,
8263 unsigned LdWidth,
8264 MVT ResType) {
8265 // We assume that we have good rules to handle loading power of two loads so
8266 // we break down the operations to power of 2 loads. The strategy is to
8267 // load the largest power of 2 that we can easily transform to a legal vector
8268 // and then insert into that vector, and the cast the result into the legal
8269 // vector that we want. This avoids unnecessary stack converts.
8270 // TODO: If the Ldwidth is legal, alignment is the same as the LdWidth, and
8271 // the load is nonvolatile, we an use a wider load for the value.
8272 // Find a vector length we can load a large chunk
8273 MVT EVT, VecEVT;
8274 unsigned EVTWidth;
8275 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8276 EVTWidth = EVT.getSizeInBits();
8277
8278 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV, SVOffset,
8279 isVolatile, Alignment);
8280 SDValue VecOp = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecEVT, LdOp);
8281 LdChain.push_back(LdOp.getValue(1));
8282
8283 // Check if we can load the element with one instruction
8284 if (LdWidth == EVTWidth) {
8285 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8286 }
8287
8288 // The vector element order is endianness dependent.
8289 unsigned Idx = 1;
8290 LdWidth -= EVTWidth;
8291 unsigned Offset = 0;
8292
8293 while (LdWidth > 0) {
8294 unsigned Increment = EVTWidth / 8;
8295 Offset += Increment;
8296 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8297 DAG.getIntPtrConstant(Increment));
8298
8299 if (LdWidth < EVTWidth) {
8300 // Our current type we are using is too large, use a smaller size by
8301 // using a smaller power of 2
8302 unsigned oEVTWidth = EVTWidth;
8303 FindWidenVecType(TLI, LdWidth, ResType, EVT, VecEVT);
8304 EVTWidth = EVT.getSizeInBits();
8305 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008306 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008307 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8308 }
8309
8310 SDValue LdOp = DAG.getLoad(EVT, Chain, BasePtr, SV,
8311 SVOffset+Offset, isVolatile,
8312 MinAlign(Alignment, Offset));
8313 LdChain.push_back(LdOp.getValue(1));
8314 VecOp = DAG.getNode(ISD::INSERT_VECTOR_ELT, VecEVT, VecOp, LdOp,
8315 DAG.getIntPtrConstant(Idx++));
8316
8317 LdWidth -= EVTWidth;
8318 }
8319
8320 return DAG.getNode(ISD::BIT_CONVERT, ResType, VecOp);
8321}
8322
8323bool SelectionDAGLegalize::LoadWidenVectorOp(SDValue& Result,
8324 SDValue& TFOp,
8325 SDValue Op,
8326 MVT NVT) {
8327 // TODO: Add support for ConcatVec and the ability to load many vector
8328 // types (e.g., v4i8). This will not work when a vector register
8329 // to memory mapping is strange (e.g., vector elements are not
8330 // stored in some sequential order).
8331
8332 // It must be true that the widen vector type is bigger than where
8333 // we need to load from.
8334 LoadSDNode *LD = cast<LoadSDNode>(Op.getNode());
8335 MVT LdVT = LD->getMemoryVT();
8336 assert(LdVT.isVector() && NVT.isVector());
8337 assert(LdVT.getVectorElementType() == NVT.getVectorElementType());
8338
8339 // Load information
8340 SDValue Chain = LD->getChain();
8341 SDValue BasePtr = LD->getBasePtr();
8342 int SVOffset = LD->getSrcValueOffset();
8343 unsigned Alignment = LD->getAlignment();
8344 bool isVolatile = LD->isVolatile();
8345 const Value *SV = LD->getSrcValue();
8346 unsigned int LdWidth = LdVT.getSizeInBits();
8347
8348 // Load value as a large register
8349 SDValueVector LdChain;
8350 Result = genWidenVectorLoads(LdChain, Chain, BasePtr, SV, SVOffset,
8351 Alignment, isVolatile, LdWidth, NVT);
8352
8353 if (LdChain.size() == 1) {
8354 TFOp = LdChain[0];
8355 return true;
8356 }
8357 else {
8358 TFOp=DAG.getNode(ISD::TokenFactor, MVT::Other, &LdChain[0], LdChain.size());
8359 return false;
8360 }
8361}
8362
8363
8364void SelectionDAGLegalize::genWidenVectorStores(SDValueVector& StChain,
8365 SDValue Chain,
8366 SDValue BasePtr,
8367 const Value *SV,
8368 int SVOffset,
8369 unsigned Alignment,
8370 bool isVolatile,
Mon P Wang49292f12008-11-15 06:05:52 +00008371 SDValue ValOp,
Mon P Wang0c397192008-10-30 08:01:45 +00008372 unsigned StWidth) {
8373 // Breaks the stores into a series of power of 2 width stores. For any
8374 // width, we convert the vector to the vector of element size that we
8375 // want to store. This avoids requiring a stack convert.
8376
8377 // Find a width of the element type we can store with
8378 MVT VVT = ValOp.getValueType();
8379 MVT EVT, VecEVT;
8380 unsigned EVTWidth;
8381 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8382 EVTWidth = EVT.getSizeInBits();
8383
8384 SDValue VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, ValOp);
8385 SDValue EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wange0b436a2008-11-06 22:52:21 +00008386 DAG.getIntPtrConstant(0));
Mon P Wang0c397192008-10-30 08:01:45 +00008387 SDValue StOp = DAG.getStore(Chain, EOp, BasePtr, SV, SVOffset,
8388 isVolatile, Alignment);
8389 StChain.push_back(StOp);
8390
8391 // Check if we are done
8392 if (StWidth == EVTWidth) {
8393 return;
8394 }
8395
8396 unsigned Idx = 1;
8397 StWidth -= EVTWidth;
8398 unsigned Offset = 0;
8399
8400 while (StWidth > 0) {
8401 unsigned Increment = EVTWidth / 8;
8402 Offset += Increment;
8403 BasePtr = DAG.getNode(ISD::ADD, BasePtr.getValueType(), BasePtr,
8404 DAG.getIntPtrConstant(Increment));
8405
8406 if (StWidth < EVTWidth) {
8407 // Our current type we are using is too large, use a smaller size by
8408 // using a smaller power of 2
8409 unsigned oEVTWidth = EVTWidth;
8410 FindWidenVecType(TLI, StWidth, VVT, EVT, VecEVT);
8411 EVTWidth = EVT.getSizeInBits();
8412 // Readjust position and vector position based on new load type
Mon P Wang49292f12008-11-15 06:05:52 +00008413 Idx = Idx * (oEVTWidth/EVTWidth);
Mon P Wang0c397192008-10-30 08:01:45 +00008414 VecOp = DAG.getNode(ISD::BIT_CONVERT, VecEVT, VecOp);
8415 }
8416
8417 EOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, EVT, VecOp,
Mon P Wang49292f12008-11-15 06:05:52 +00008418 DAG.getIntPtrConstant(Idx++));
Mon P Wang0c397192008-10-30 08:01:45 +00008419 StChain.push_back(DAG.getStore(Chain, EOp, BasePtr, SV,
8420 SVOffset + Offset, isVolatile,
8421 MinAlign(Alignment, Offset)));
8422 StWidth -= EVTWidth;
8423 }
8424}
8425
8426
8427SDValue SelectionDAGLegalize::StoreWidenVectorOp(StoreSDNode *ST,
8428 SDValue Chain,
8429 SDValue BasePtr) {
8430 // TODO: It might be cleaner if we can use SplitVector and have more legal
8431 // vector types that can be stored into memory (e.g., v4xi8 can
8432 // be stored as a word). This will not work when a vector register
8433 // to memory mapping is strange (e.g., vector elements are not
8434 // stored in some sequential order).
8435
8436 MVT StVT = ST->getMemoryVT();
8437 SDValue ValOp = ST->getValue();
8438
8439 // Check if we have widen this node with another value
8440 std::map<SDValue, SDValue>::iterator I = WidenNodes.find(ValOp);
8441 if (I != WidenNodes.end())
8442 ValOp = I->second;
8443
8444 MVT VVT = ValOp.getValueType();
8445
8446 // It must be true that we the widen vector type is bigger than where
8447 // we need to store.
8448 assert(StVT.isVector() && VVT.isVector());
8449 assert(StVT.getSizeInBits() < VVT.getSizeInBits());
8450 assert(StVT.getVectorElementType() == VVT.getVectorElementType());
8451
8452 // Store value
8453 SDValueVector StChain;
8454 genWidenVectorStores(StChain, Chain, BasePtr, ST->getSrcValue(),
8455 ST->getSrcValueOffset(), ST->getAlignment(),
8456 ST->isVolatile(), ValOp, StVT.getSizeInBits());
8457 if (StChain.size() == 1)
8458 return StChain[0];
8459 else
8460 return DAG.getNode(ISD::TokenFactor, MVT::Other,&StChain[0],StChain.size());
8461}
8462
8463
Chris Lattner3e928bb2005-01-07 07:47:09 +00008464// SelectionDAG::Legalize - This is the entry point for the file.
8465//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00008466void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00008467 /// run - This is the main entry point to this class.
8468 ///
Chris Lattner456a93a2006-01-28 07:39:30 +00008469 SelectionDAGLegalize(*this).LegalizeDAG();
Chris Lattner3e928bb2005-01-07 07:47:09 +00008470}
8471